Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
Johan Hedberg | ea585ab | 2012-02-17 14:50:39 +0200 | [diff] [blame] | 3 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4 | Copyright (C) 2010 Nokia Corporation |
Johan Hedberg | ea585ab | 2012-02-17 14:50:39 +0200 | [diff] [blame] | 5 | Copyright (C) 2011-2012 Intel Corporation |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License version 2 as |
| 9 | published by the Free Software Foundation; |
| 10 | |
| 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 12 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 14 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 15 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | |
| 20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 22 | SOFTWARE IS DISCLAIMED. |
| 23 | */ |
| 24 | |
| 25 | /* Bluetooth HCI Management interface */ |
| 26 | |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
| 29 | |
| 30 | #include <net/bluetooth/bluetooth.h> |
| 31 | #include <net/bluetooth/hci_core.h> |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 32 | #include <net/bluetooth/l2cap.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 33 | #include <net/bluetooth/mgmt.h> |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 34 | |
| 35 | #include "smp.h" |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 36 | |
Johan Hedberg | 2da9c55 | 2012-02-17 14:39:28 +0200 | [diff] [blame] | 37 | #define MGMT_VERSION 1 |
Marcel Holtmann | 730f091 | 2014-06-28 12:36:10 +0200 | [diff] [blame] | 38 | #define MGMT_REVISION 7 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 39 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 40 | static const u16 mgmt_commands[] = { |
| 41 | MGMT_OP_READ_INDEX_LIST, |
| 42 | MGMT_OP_READ_INFO, |
| 43 | MGMT_OP_SET_POWERED, |
| 44 | MGMT_OP_SET_DISCOVERABLE, |
| 45 | MGMT_OP_SET_CONNECTABLE, |
| 46 | MGMT_OP_SET_FAST_CONNECTABLE, |
| 47 | MGMT_OP_SET_PAIRABLE, |
| 48 | MGMT_OP_SET_LINK_SECURITY, |
| 49 | MGMT_OP_SET_SSP, |
| 50 | MGMT_OP_SET_HS, |
| 51 | MGMT_OP_SET_LE, |
| 52 | MGMT_OP_SET_DEV_CLASS, |
| 53 | MGMT_OP_SET_LOCAL_NAME, |
| 54 | MGMT_OP_ADD_UUID, |
| 55 | MGMT_OP_REMOVE_UUID, |
| 56 | MGMT_OP_LOAD_LINK_KEYS, |
| 57 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 58 | MGMT_OP_DISCONNECT, |
| 59 | MGMT_OP_GET_CONNECTIONS, |
| 60 | MGMT_OP_PIN_CODE_REPLY, |
| 61 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 62 | MGMT_OP_SET_IO_CAPABILITY, |
| 63 | MGMT_OP_PAIR_DEVICE, |
| 64 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 65 | MGMT_OP_UNPAIR_DEVICE, |
| 66 | MGMT_OP_USER_CONFIRM_REPLY, |
| 67 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 68 | MGMT_OP_USER_PASSKEY_REPLY, |
| 69 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 70 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 71 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 72 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 73 | MGMT_OP_START_DISCOVERY, |
| 74 | MGMT_OP_STOP_DISCOVERY, |
| 75 | MGMT_OP_CONFIRM_NAME, |
| 76 | MGMT_OP_BLOCK_DEVICE, |
| 77 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 78 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 79 | MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 80 | MGMT_OP_SET_BREDR, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 81 | MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 7f72134e | 2013-10-11 14:44:58 -0700 | [diff] [blame] | 82 | MGMT_OP_SET_SCAN_PARAMS, |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 83 | MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 84 | MGMT_OP_SET_DEBUG_KEYS, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 85 | MGMT_OP_SET_PRIVACY, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 86 | MGMT_OP_LOAD_IRKS, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 87 | MGMT_OP_GET_CONN_INFO, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 88 | MGMT_OP_GET_CLOCK_INFO, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 89 | MGMT_OP_ADD_DEVICE, |
| 90 | MGMT_OP_REMOVE_DEVICE, |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame^] | 91 | MGMT_OP_LOAD_CONN_PARAM, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | static const u16 mgmt_events[] = { |
| 95 | MGMT_EV_CONTROLLER_ERROR, |
| 96 | MGMT_EV_INDEX_ADDED, |
| 97 | MGMT_EV_INDEX_REMOVED, |
| 98 | MGMT_EV_NEW_SETTINGS, |
| 99 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 100 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 101 | MGMT_EV_NEW_LINK_KEY, |
| 102 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 103 | MGMT_EV_DEVICE_CONNECTED, |
| 104 | MGMT_EV_DEVICE_DISCONNECTED, |
| 105 | MGMT_EV_CONNECT_FAILED, |
| 106 | MGMT_EV_PIN_CODE_REQUEST, |
| 107 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 108 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 109 | MGMT_EV_AUTH_FAILED, |
| 110 | MGMT_EV_DEVICE_FOUND, |
| 111 | MGMT_EV_DISCOVERING, |
| 112 | MGMT_EV_DEVICE_BLOCKED, |
| 113 | MGMT_EV_DEVICE_UNBLOCKED, |
| 114 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 115 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 116 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 117 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 118 | MGMT_EV_DEVICE_ADDED, |
| 119 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 120 | MGMT_EV_NEW_CONN_PARAM, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 121 | }; |
| 122 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 123 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 124 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 125 | #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \ |
| 126 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 127 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 128 | struct pending_cmd { |
| 129 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 130 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 131 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 132 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 133 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 134 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 135 | }; |
| 136 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 137 | /* HCI to MGMT error code conversion table */ |
| 138 | static u8 mgmt_status_table[] = { |
| 139 | MGMT_STATUS_SUCCESS, |
| 140 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 141 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 142 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 143 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 144 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 145 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 146 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 147 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 148 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 149 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 150 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 151 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 152 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 153 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 154 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 155 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 156 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 157 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 158 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 159 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 160 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 161 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 162 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 163 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 164 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 165 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 166 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 167 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 168 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 169 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 170 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 171 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 172 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 173 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 174 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 175 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 176 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 177 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 178 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 179 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 180 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 181 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 182 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 183 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 184 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 185 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 186 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 187 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 188 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 189 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 190 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 191 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 192 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 193 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 194 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 195 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 196 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 197 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 198 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 199 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 200 | }; |
| 201 | |
| 202 | static u8 mgmt_status(u8 hci_status) |
| 203 | { |
| 204 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 205 | return mgmt_status_table[hci_status]; |
| 206 | |
| 207 | return MGMT_STATUS_FAILED; |
| 208 | } |
| 209 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 210 | 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] | 211 | { |
| 212 | struct sk_buff *skb; |
| 213 | struct mgmt_hdr *hdr; |
| 214 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 215 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 216 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 217 | 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] | 218 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 219 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 220 | if (!skb) |
| 221 | return -ENOMEM; |
| 222 | |
| 223 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 224 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 225 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 226 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 227 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 228 | |
| 229 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 230 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 231 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 232 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 233 | err = sock_queue_rcv_skb(sk, skb); |
| 234 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 235 | kfree_skb(skb); |
| 236 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 237 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 238 | } |
| 239 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 240 | 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] | 241 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 242 | { |
| 243 | struct sk_buff *skb; |
| 244 | struct mgmt_hdr *hdr; |
| 245 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 246 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 247 | |
| 248 | BT_DBG("sock %p", sk); |
| 249 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 250 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 251 | if (!skb) |
| 252 | return -ENOMEM; |
| 253 | |
| 254 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 255 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 256 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 257 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 258 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 259 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 260 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 261 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 262 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 263 | |
| 264 | if (rp) |
| 265 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 266 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 267 | err = sock_queue_rcv_skb(sk, skb); |
| 268 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 269 | kfree_skb(skb); |
| 270 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 271 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 274 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 275 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 276 | { |
| 277 | struct mgmt_rp_read_version rp; |
| 278 | |
| 279 | BT_DBG("sock %p", sk); |
| 280 | |
| 281 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 282 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 283 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 284 | 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] | 285 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 286 | } |
| 287 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 288 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 289 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 290 | { |
| 291 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 292 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 293 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 294 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 295 | size_t rp_size; |
| 296 | int i, err; |
| 297 | |
| 298 | BT_DBG("sock %p", sk); |
| 299 | |
| 300 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 301 | |
| 302 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 303 | if (!rp) |
| 304 | return -ENOMEM; |
| 305 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 306 | rp->num_commands = cpu_to_le16(num_commands); |
| 307 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 308 | |
| 309 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 310 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 311 | |
| 312 | for (i = 0; i < num_events; i++, opcode++) |
| 313 | put_unaligned_le16(mgmt_events[i], opcode); |
| 314 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 315 | 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] | 316 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 317 | kfree(rp); |
| 318 | |
| 319 | return err; |
| 320 | } |
| 321 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 322 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 323 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 324 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 325 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 326 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 327 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 328 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 329 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 330 | |
| 331 | BT_DBG("sock %p", sk); |
| 332 | |
| 333 | read_lock(&hci_dev_list_lock); |
| 334 | |
| 335 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 336 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 337 | if (d->dev_type == HCI_BREDR) |
| 338 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 339 | } |
| 340 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 341 | rp_len = sizeof(*rp) + (2 * count); |
| 342 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 343 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 344 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 345 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 346 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 347 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 348 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 349 | list_for_each_entry(d, &hci_dev_list, list) { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 350 | if (test_bit(HCI_SETUP, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 351 | continue; |
| 352 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 353 | if (test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 354 | continue; |
| 355 | |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 356 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 357 | continue; |
| 358 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 359 | if (d->dev_type == HCI_BREDR) { |
| 360 | rp->index[count++] = cpu_to_le16(d->id); |
| 361 | BT_DBG("Added hci%u", d->id); |
| 362 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 363 | } |
| 364 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 365 | rp->num_controllers = cpu_to_le16(count); |
| 366 | rp_len = sizeof(*rp) + (2 * count); |
| 367 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 368 | read_unlock(&hci_dev_list_lock); |
| 369 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 370 | 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] | 371 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 372 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 373 | kfree(rp); |
| 374 | |
| 375 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 378 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 379 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 380 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 381 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 382 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 383 | settings |= MGMT_SETTING_PAIRABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 384 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 385 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 386 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 387 | settings |= MGMT_SETTING_CONNECTABLE; |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 388 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 389 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 390 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 391 | settings |= MGMT_SETTING_BREDR; |
| 392 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 393 | |
| 394 | if (lmp_ssp_capable(hdev)) { |
| 395 | settings |= MGMT_SETTING_SSP; |
| 396 | settings |= MGMT_SETTING_HS; |
| 397 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 398 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 399 | if (lmp_sc_capable(hdev) || |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 400 | test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 401 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 402 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 403 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 404 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 405 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 406 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 407 | settings |= MGMT_SETTING_PRIVACY; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 408 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 409 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 410 | return settings; |
| 411 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 412 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 413 | static u32 get_current_settings(struct hci_dev *hdev) |
| 414 | { |
| 415 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 416 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 417 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 418 | settings |= MGMT_SETTING_POWERED; |
| 419 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 420 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 421 | settings |= MGMT_SETTING_CONNECTABLE; |
| 422 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 423 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 424 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 425 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 426 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 427 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 428 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 429 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 430 | settings |= MGMT_SETTING_PAIRABLE; |
| 431 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 432 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 433 | settings |= MGMT_SETTING_BREDR; |
| 434 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 435 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 436 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 437 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 438 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 439 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 440 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 441 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 442 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 443 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 444 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 445 | settings |= MGMT_SETTING_HS; |
| 446 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 447 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 448 | settings |= MGMT_SETTING_ADVERTISING; |
| 449 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 450 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 451 | settings |= MGMT_SETTING_SECURE_CONN; |
| 452 | |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 453 | if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 454 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 455 | |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 456 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 457 | settings |= MGMT_SETTING_PRIVACY; |
| 458 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 459 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 462 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 463 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 464 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 465 | { |
| 466 | u8 *ptr = data, *uuids_start = NULL; |
| 467 | struct bt_uuid *uuid; |
| 468 | |
| 469 | if (len < 4) |
| 470 | return ptr; |
| 471 | |
| 472 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 473 | u16 uuid16; |
| 474 | |
| 475 | if (uuid->size != 16) |
| 476 | continue; |
| 477 | |
| 478 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 479 | if (uuid16 < 0x1100) |
| 480 | continue; |
| 481 | |
| 482 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 483 | continue; |
| 484 | |
| 485 | if (!uuids_start) { |
| 486 | uuids_start = ptr; |
| 487 | uuids_start[0] = 1; |
| 488 | uuids_start[1] = EIR_UUID16_ALL; |
| 489 | ptr += 2; |
| 490 | } |
| 491 | |
| 492 | /* Stop if not enough space to put next UUID */ |
| 493 | if ((ptr - data) + sizeof(u16) > len) { |
| 494 | uuids_start[1] = EIR_UUID16_SOME; |
| 495 | break; |
| 496 | } |
| 497 | |
| 498 | *ptr++ = (uuid16 & 0x00ff); |
| 499 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 500 | uuids_start[0] += sizeof(uuid16); |
| 501 | } |
| 502 | |
| 503 | return ptr; |
| 504 | } |
| 505 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 506 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 507 | { |
| 508 | u8 *ptr = data, *uuids_start = NULL; |
| 509 | struct bt_uuid *uuid; |
| 510 | |
| 511 | if (len < 6) |
| 512 | return ptr; |
| 513 | |
| 514 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 515 | if (uuid->size != 32) |
| 516 | continue; |
| 517 | |
| 518 | if (!uuids_start) { |
| 519 | uuids_start = ptr; |
| 520 | uuids_start[0] = 1; |
| 521 | uuids_start[1] = EIR_UUID32_ALL; |
| 522 | ptr += 2; |
| 523 | } |
| 524 | |
| 525 | /* Stop if not enough space to put next UUID */ |
| 526 | if ((ptr - data) + sizeof(u32) > len) { |
| 527 | uuids_start[1] = EIR_UUID32_SOME; |
| 528 | break; |
| 529 | } |
| 530 | |
| 531 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 532 | ptr += sizeof(u32); |
| 533 | uuids_start[0] += sizeof(u32); |
| 534 | } |
| 535 | |
| 536 | return ptr; |
| 537 | } |
| 538 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 539 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 540 | { |
| 541 | u8 *ptr = data, *uuids_start = NULL; |
| 542 | struct bt_uuid *uuid; |
| 543 | |
| 544 | if (len < 18) |
| 545 | return ptr; |
| 546 | |
| 547 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 548 | if (uuid->size != 128) |
| 549 | continue; |
| 550 | |
| 551 | if (!uuids_start) { |
| 552 | uuids_start = ptr; |
| 553 | uuids_start[0] = 1; |
| 554 | uuids_start[1] = EIR_UUID128_ALL; |
| 555 | ptr += 2; |
| 556 | } |
| 557 | |
| 558 | /* Stop if not enough space to put next UUID */ |
| 559 | if ((ptr - data) + 16 > len) { |
| 560 | uuids_start[1] = EIR_UUID128_SOME; |
| 561 | break; |
| 562 | } |
| 563 | |
| 564 | memcpy(ptr, uuid->uuid, 16); |
| 565 | ptr += 16; |
| 566 | uuids_start[0] += 16; |
| 567 | } |
| 568 | |
| 569 | return ptr; |
| 570 | } |
| 571 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 572 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 573 | { |
| 574 | struct pending_cmd *cmd; |
| 575 | |
| 576 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 577 | if (cmd->opcode == opcode) |
| 578 | return cmd; |
| 579 | } |
| 580 | |
| 581 | return NULL; |
| 582 | } |
| 583 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 584 | static struct pending_cmd *mgmt_pending_find_data(u16 opcode, |
| 585 | struct hci_dev *hdev, |
| 586 | const void *data) |
| 587 | { |
| 588 | struct pending_cmd *cmd; |
| 589 | |
| 590 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 591 | if (cmd->user_data != data) |
| 592 | continue; |
| 593 | if (cmd->opcode == opcode) |
| 594 | return cmd; |
| 595 | } |
| 596 | |
| 597 | return NULL; |
| 598 | } |
| 599 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 600 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 601 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 602 | u8 ad_len = 0; |
| 603 | size_t name_len; |
| 604 | |
| 605 | name_len = strlen(hdev->dev_name); |
| 606 | if (name_len > 0) { |
| 607 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 608 | |
| 609 | if (name_len > max_len) { |
| 610 | name_len = max_len; |
| 611 | ptr[1] = EIR_NAME_SHORT; |
| 612 | } else |
| 613 | ptr[1] = EIR_NAME_COMPLETE; |
| 614 | |
| 615 | ptr[0] = name_len + 1; |
| 616 | |
| 617 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 618 | |
| 619 | ad_len += (name_len + 2); |
| 620 | ptr += (name_len + 2); |
| 621 | } |
| 622 | |
| 623 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | static void update_scan_rsp_data(struct hci_request *req) |
| 627 | { |
| 628 | struct hci_dev *hdev = req->hdev; |
| 629 | struct hci_cp_le_set_scan_rsp_data cp; |
| 630 | u8 len; |
| 631 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 632 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 633 | return; |
| 634 | |
| 635 | memset(&cp, 0, sizeof(cp)); |
| 636 | |
| 637 | len = create_scan_rsp_data(hdev, cp.data); |
| 638 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 639 | if (hdev->scan_rsp_data_len == len && |
| 640 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 641 | return; |
| 642 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 643 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 644 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 645 | |
| 646 | cp.length = len; |
| 647 | |
| 648 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 649 | } |
| 650 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 651 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 652 | { |
| 653 | struct pending_cmd *cmd; |
| 654 | |
| 655 | /* If there's a pending mgmt command the flags will not yet have |
| 656 | * their final values, so check for this first. |
| 657 | */ |
| 658 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 659 | if (cmd) { |
| 660 | struct mgmt_mode *cp = cmd->param; |
| 661 | if (cp->val == 0x01) |
| 662 | return LE_AD_GENERAL; |
| 663 | else if (cp->val == 0x02) |
| 664 | return LE_AD_LIMITED; |
| 665 | } else { |
| 666 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 667 | return LE_AD_LIMITED; |
| 668 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 669 | return LE_AD_GENERAL; |
| 670 | } |
| 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 675 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 676 | { |
| 677 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 678 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 679 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 680 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 681 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 682 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 683 | |
| 684 | if (flags) { |
| 685 | BT_DBG("adv flags 0x%02x", flags); |
| 686 | |
| 687 | ptr[0] = 2; |
| 688 | ptr[1] = EIR_FLAGS; |
| 689 | ptr[2] = flags; |
| 690 | |
| 691 | ad_len += 3; |
| 692 | ptr += 3; |
| 693 | } |
| 694 | |
| 695 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 696 | ptr[0] = 2; |
| 697 | ptr[1] = EIR_TX_POWER; |
| 698 | ptr[2] = (u8) hdev->adv_tx_power; |
| 699 | |
| 700 | ad_len += 3; |
| 701 | ptr += 3; |
| 702 | } |
| 703 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 704 | return ad_len; |
| 705 | } |
| 706 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 707 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 708 | { |
| 709 | struct hci_dev *hdev = req->hdev; |
| 710 | struct hci_cp_le_set_adv_data cp; |
| 711 | u8 len; |
| 712 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 713 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 714 | return; |
| 715 | |
| 716 | memset(&cp, 0, sizeof(cp)); |
| 717 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 718 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 719 | |
| 720 | if (hdev->adv_data_len == len && |
| 721 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 722 | return; |
| 723 | |
| 724 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 725 | hdev->adv_data_len = len; |
| 726 | |
| 727 | cp.length = len; |
| 728 | |
| 729 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 730 | } |
| 731 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 732 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 733 | { |
| 734 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 735 | size_t name_len; |
| 736 | |
| 737 | name_len = strlen(hdev->dev_name); |
| 738 | |
| 739 | if (name_len > 0) { |
| 740 | /* EIR Data type */ |
| 741 | if (name_len > 48) { |
| 742 | name_len = 48; |
| 743 | ptr[1] = EIR_NAME_SHORT; |
| 744 | } else |
| 745 | ptr[1] = EIR_NAME_COMPLETE; |
| 746 | |
| 747 | /* EIR Data length */ |
| 748 | ptr[0] = name_len + 1; |
| 749 | |
| 750 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 751 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 752 | ptr += (name_len + 2); |
| 753 | } |
| 754 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 755 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 756 | ptr[0] = 2; |
| 757 | ptr[1] = EIR_TX_POWER; |
| 758 | ptr[2] = (u8) hdev->inq_tx_power; |
| 759 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 760 | ptr += 3; |
| 761 | } |
| 762 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 763 | if (hdev->devid_source > 0) { |
| 764 | ptr[0] = 9; |
| 765 | ptr[1] = EIR_DEVICE_ID; |
| 766 | |
| 767 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 768 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 769 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 770 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 771 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 772 | ptr += 10; |
| 773 | } |
| 774 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 775 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 776 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 777 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 778 | } |
| 779 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 780 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 781 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 782 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 783 | struct hci_cp_write_eir cp; |
| 784 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 785 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 786 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 787 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 788 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 789 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 790 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 791 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 792 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 793 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 794 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 795 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 796 | |
| 797 | memset(&cp, 0, sizeof(cp)); |
| 798 | |
| 799 | create_eir(hdev, cp.data); |
| 800 | |
| 801 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 802 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 803 | |
| 804 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 805 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 806 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 807 | } |
| 808 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 809 | static u8 get_service_classes(struct hci_dev *hdev) |
| 810 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 811 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 812 | u8 val = 0; |
| 813 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 814 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 815 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 816 | |
| 817 | return val; |
| 818 | } |
| 819 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 820 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 821 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 822 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 823 | u8 cod[3]; |
| 824 | |
| 825 | BT_DBG("%s", hdev->name); |
| 826 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 827 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 828 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 829 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 830 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 831 | return; |
| 832 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 833 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 834 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 835 | |
| 836 | cod[0] = hdev->minor_class; |
| 837 | cod[1] = hdev->major_class; |
| 838 | cod[2] = get_service_classes(hdev); |
| 839 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 840 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 841 | cod[1] |= 0x20; |
| 842 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 843 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 844 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 845 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 846 | 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] | 847 | } |
| 848 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 849 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 850 | { |
| 851 | struct pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 852 | |
| 853 | /* If there's a pending mgmt command the flag will not yet have |
| 854 | * it's final value, so check for this first. |
| 855 | */ |
| 856 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 857 | if (cmd) { |
| 858 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 859 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 860 | } |
| 861 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 862 | return test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | static void enable_advertising(struct hci_request *req) |
| 866 | { |
| 867 | struct hci_dev *hdev = req->hdev; |
| 868 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 869 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 870 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 871 | |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 872 | /* Clear the HCI_ADVERTISING bit temporarily so that the |
| 873 | * hci_update_random_address knows that it's safe to go ahead |
| 874 | * and write a new random address. The flag will be set back on |
| 875 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 876 | */ |
| 877 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 878 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 879 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 880 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 881 | /* Set require_privacy to true only when non-connectable |
| 882 | * advertising is used. In that case it is fine to use a |
| 883 | * non-resolvable private address. |
| 884 | */ |
| 885 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 886 | return; |
| 887 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 888 | memset(&cp, 0, sizeof(cp)); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 889 | cp.min_interval = cpu_to_le16(0x0800); |
| 890 | cp.max_interval = cpu_to_le16(0x0800); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 891 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 892 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 893 | cp.channel_map = hdev->le_adv_channel_map; |
| 894 | |
| 895 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 896 | |
| 897 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 898 | } |
| 899 | |
| 900 | static void disable_advertising(struct hci_request *req) |
| 901 | { |
| 902 | u8 enable = 0x00; |
| 903 | |
| 904 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 905 | } |
| 906 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 907 | static void service_cache_off(struct work_struct *work) |
| 908 | { |
| 909 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 910 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 911 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 912 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 913 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 914 | return; |
| 915 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 916 | hci_req_init(&req, hdev); |
| 917 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 918 | hci_dev_lock(hdev); |
| 919 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 920 | update_eir(&req); |
| 921 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 922 | |
| 923 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 924 | |
| 925 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 926 | } |
| 927 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 928 | static void rpa_expired(struct work_struct *work) |
| 929 | { |
| 930 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 931 | rpa_expired.work); |
| 932 | struct hci_request req; |
| 933 | |
| 934 | BT_DBG(""); |
| 935 | |
| 936 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 937 | |
| 938 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags) || |
| 939 | hci_conn_num(hdev, LE_LINK) > 0) |
| 940 | return; |
| 941 | |
| 942 | /* The generation of a new RPA and programming it into the |
| 943 | * controller happens in the enable_advertising() function. |
| 944 | */ |
| 945 | |
| 946 | hci_req_init(&req, hdev); |
| 947 | |
| 948 | disable_advertising(&req); |
| 949 | enable_advertising(&req); |
| 950 | |
| 951 | hci_req_run(&req, NULL); |
| 952 | } |
| 953 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 954 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 955 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 956 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 957 | return; |
| 958 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 959 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 960 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 961 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 962 | /* Non-mgmt controlled devices get this bit set |
| 963 | * implicitly so that pairing works for them, however |
| 964 | * for mgmt we require user-space to explicitly enable |
| 965 | * it |
| 966 | */ |
| 967 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 968 | } |
| 969 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 970 | 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] | 971 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 972 | { |
| 973 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 974 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 975 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 976 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 977 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 978 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 979 | memset(&rp, 0, sizeof(rp)); |
| 980 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 981 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 982 | |
| 983 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 984 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 985 | |
| 986 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 987 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 988 | |
| 989 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 990 | |
| 991 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 992 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 993 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 994 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 995 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 996 | 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] | 997 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 1001 | { |
| 1002 | sock_put(cmd->sk); |
| 1003 | kfree(cmd->param); |
| 1004 | kfree(cmd); |
| 1005 | } |
| 1006 | |
| 1007 | 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] | 1008 | struct hci_dev *hdev, void *data, |
| 1009 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1010 | { |
| 1011 | struct pending_cmd *cmd; |
| 1012 | |
Johan Hedberg | fca2001 | 2014-06-28 17:54:05 +0300 | [diff] [blame] | 1013 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1014 | if (!cmd) |
| 1015 | return NULL; |
| 1016 | |
| 1017 | cmd->opcode = opcode; |
| 1018 | cmd->index = hdev->id; |
| 1019 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 1020 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1021 | if (!cmd->param) { |
| 1022 | kfree(cmd); |
| 1023 | return NULL; |
| 1024 | } |
| 1025 | |
| 1026 | if (data) |
| 1027 | memcpy(cmd->param, data, len); |
| 1028 | |
| 1029 | cmd->sk = sk; |
| 1030 | sock_hold(sk); |
| 1031 | |
| 1032 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1033 | |
| 1034 | return cmd; |
| 1035 | } |
| 1036 | |
| 1037 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1038 | void (*cb)(struct pending_cmd *cmd, |
| 1039 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1040 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1041 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1042 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1043 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1044 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1045 | if (opcode > 0 && cmd->opcode != opcode) |
| 1046 | continue; |
| 1047 | |
| 1048 | cb(cmd, data); |
| 1049 | } |
| 1050 | } |
| 1051 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1052 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 1053 | { |
| 1054 | list_del(&cmd->list); |
| 1055 | mgmt_pending_free(cmd); |
| 1056 | } |
| 1057 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1058 | 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] | 1059 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1060 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1061 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1062 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1063 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1064 | } |
| 1065 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1066 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status) |
| 1067 | { |
| 1068 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1069 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1070 | if (hci_conn_count(hdev) == 0) { |
| 1071 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1072 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1073 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1074 | } |
| 1075 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1076 | static void hci_stop_discovery(struct hci_request *req) |
| 1077 | { |
| 1078 | struct hci_dev *hdev = req->hdev; |
| 1079 | struct hci_cp_remote_name_req_cancel cp; |
| 1080 | struct inquiry_entry *e; |
| 1081 | |
| 1082 | switch (hdev->discovery.state) { |
| 1083 | case DISCOVERY_FINDING: |
| 1084 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 1085 | hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 1086 | } else { |
| 1087 | cancel_delayed_work(&hdev->le_scan_disable); |
| 1088 | hci_req_add_le_scan_disable(req); |
| 1089 | } |
| 1090 | |
| 1091 | break; |
| 1092 | |
| 1093 | case DISCOVERY_RESOLVING: |
| 1094 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 1095 | NAME_PENDING); |
| 1096 | if (!e) |
| 1097 | return; |
| 1098 | |
| 1099 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 1100 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 1101 | &cp); |
| 1102 | |
| 1103 | break; |
| 1104 | |
| 1105 | default: |
| 1106 | /* Passive scanning */ |
| 1107 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 1108 | hci_req_add_le_scan_disable(req); |
| 1109 | break; |
| 1110 | } |
| 1111 | } |
| 1112 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1113 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1114 | { |
| 1115 | struct hci_request req; |
| 1116 | struct hci_conn *conn; |
| 1117 | |
| 1118 | hci_req_init(&req, hdev); |
| 1119 | |
| 1120 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1121 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1122 | u8 scan = 0x00; |
| 1123 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1124 | } |
| 1125 | |
| 1126 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 1127 | disable_advertising(&req); |
| 1128 | |
Johan Hedberg | f8680f1 | 2014-06-10 14:05:59 +0300 | [diff] [blame] | 1129 | hci_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1130 | |
| 1131 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1132 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1133 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1134 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1135 | switch (conn->state) { |
| 1136 | case BT_CONNECTED: |
| 1137 | case BT_CONFIG: |
| 1138 | dc.handle = cpu_to_le16(conn->handle); |
| 1139 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1140 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1141 | break; |
| 1142 | case BT_CONNECT: |
| 1143 | if (conn->type == LE_LINK) |
| 1144 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1145 | 0, NULL); |
| 1146 | else if (conn->type == ACL_LINK) |
| 1147 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1148 | 6, &conn->dst); |
| 1149 | break; |
| 1150 | case BT_CONNECT2: |
| 1151 | bacpy(&rej.bdaddr, &conn->dst); |
| 1152 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1153 | if (conn->type == ACL_LINK) |
| 1154 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1155 | sizeof(rej), &rej); |
| 1156 | else if (conn->type == SCO_LINK) |
| 1157 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1158 | sizeof(rej), &rej); |
| 1159 | break; |
| 1160 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | return hci_req_run(&req, clean_up_hci_complete); |
| 1164 | } |
| 1165 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1166 | 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] | 1167 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1168 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1169 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1170 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1171 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1172 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1173 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1174 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1175 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1176 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1177 | MGMT_STATUS_INVALID_PARAMS); |
| 1178 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1179 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1180 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1181 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 1182 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1183 | MGMT_STATUS_BUSY); |
| 1184 | goto failed; |
| 1185 | } |
| 1186 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1187 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1188 | cancel_delayed_work(&hdev->power_off); |
| 1189 | |
| 1190 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1191 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1192 | data, len); |
| 1193 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1194 | goto failed; |
| 1195 | } |
| 1196 | } |
| 1197 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1198 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1199 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1200 | goto failed; |
| 1201 | } |
| 1202 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1203 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1204 | if (!cmd) { |
| 1205 | err = -ENOMEM; |
| 1206 | goto failed; |
| 1207 | } |
| 1208 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1209 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1210 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1211 | err = 0; |
| 1212 | } else { |
| 1213 | /* Disconnect connections, stop scans, etc */ |
| 1214 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1215 | if (!err) |
| 1216 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1217 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1218 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1219 | /* ENODATA means there were no HCI commands queued */ |
| 1220 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1221 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1222 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1223 | err = 0; |
| 1224 | } |
| 1225 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1226 | |
| 1227 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1228 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1229 | return err; |
| 1230 | } |
| 1231 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1232 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 1233 | struct sock *skip_sk) |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1234 | { |
| 1235 | struct sk_buff *skb; |
| 1236 | struct mgmt_hdr *hdr; |
| 1237 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 1238 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1239 | if (!skb) |
| 1240 | return -ENOMEM; |
| 1241 | |
| 1242 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 1243 | hdr->opcode = cpu_to_le16(event); |
| 1244 | if (hdev) |
| 1245 | hdr->index = cpu_to_le16(hdev->id); |
| 1246 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1247 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1248 | hdr->len = cpu_to_le16(data_len); |
| 1249 | |
| 1250 | if (data) |
| 1251 | memcpy(skb_put(skb, data_len), data, data_len); |
| 1252 | |
Marcel Holtmann | 97e0bde | 2012-02-22 13:49:28 +0100 | [diff] [blame] | 1253 | /* Time stamp */ |
| 1254 | __net_timestamp(skb); |
| 1255 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1256 | hci_send_to_control(skb, skip_sk); |
| 1257 | kfree_skb(skb); |
| 1258 | |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1263 | { |
| 1264 | __le32 ev; |
| 1265 | |
| 1266 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1267 | |
| 1268 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1269 | } |
| 1270 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1271 | struct cmd_lookup { |
| 1272 | struct sock *sk; |
| 1273 | struct hci_dev *hdev; |
| 1274 | u8 mgmt_status; |
| 1275 | }; |
| 1276 | |
| 1277 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1278 | { |
| 1279 | struct cmd_lookup *match = data; |
| 1280 | |
| 1281 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1282 | |
| 1283 | list_del(&cmd->list); |
| 1284 | |
| 1285 | if (match->sk == NULL) { |
| 1286 | match->sk = cmd->sk; |
| 1287 | sock_hold(match->sk); |
| 1288 | } |
| 1289 | |
| 1290 | mgmt_pending_free(cmd); |
| 1291 | } |
| 1292 | |
| 1293 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1294 | { |
| 1295 | u8 *status = data; |
| 1296 | |
| 1297 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1298 | mgmt_pending_remove(cmd); |
| 1299 | } |
| 1300 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1301 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1302 | { |
| 1303 | if (!lmp_bredr_capable(hdev)) |
| 1304 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1305 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1306 | return MGMT_STATUS_REJECTED; |
| 1307 | else |
| 1308 | return MGMT_STATUS_SUCCESS; |
| 1309 | } |
| 1310 | |
| 1311 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1312 | { |
| 1313 | if (!lmp_le_capable(hdev)) |
| 1314 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1315 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1316 | return MGMT_STATUS_REJECTED; |
| 1317 | else |
| 1318 | return MGMT_STATUS_SUCCESS; |
| 1319 | } |
| 1320 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1321 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1322 | { |
| 1323 | struct pending_cmd *cmd; |
| 1324 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1325 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1326 | bool changed; |
| 1327 | |
| 1328 | BT_DBG("status 0x%02x", status); |
| 1329 | |
| 1330 | hci_dev_lock(hdev); |
| 1331 | |
| 1332 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1333 | if (!cmd) |
| 1334 | goto unlock; |
| 1335 | |
| 1336 | if (status) { |
| 1337 | u8 mgmt_err = mgmt_status(status); |
| 1338 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1339 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1340 | goto remove_cmd; |
| 1341 | } |
| 1342 | |
| 1343 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1344 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1345 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1346 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1347 | |
| 1348 | if (hdev->discov_timeout > 0) { |
| 1349 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1350 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1351 | to); |
| 1352 | } |
| 1353 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1354 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1355 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1356 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1357 | |
| 1358 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1359 | |
| 1360 | if (changed) |
| 1361 | new_settings(hdev, cmd->sk); |
| 1362 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1363 | /* When the discoverable mode gets changed, make sure |
| 1364 | * that class of device has the limited discoverable |
| 1365 | * bit correctly set. |
| 1366 | */ |
| 1367 | hci_req_init(&req, hdev); |
| 1368 | update_class(&req); |
| 1369 | hci_req_run(&req, NULL); |
| 1370 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1371 | remove_cmd: |
| 1372 | mgmt_pending_remove(cmd); |
| 1373 | |
| 1374 | unlock: |
| 1375 | hci_dev_unlock(hdev); |
| 1376 | } |
| 1377 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1378 | 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] | 1379 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1380 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1381 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1382 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1383 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1384 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1385 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1386 | int err; |
| 1387 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1388 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1389 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1390 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1391 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1392 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1393 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1394 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1395 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1396 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1397 | MGMT_STATUS_INVALID_PARAMS); |
| 1398 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1399 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1400 | |
| 1401 | /* Disabling discoverable requires that no timeout is set, |
| 1402 | * and enabling limited discoverable requires a timeout. |
| 1403 | */ |
| 1404 | if ((cp->val == 0x00 && timeout > 0) || |
| 1405 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1406 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1407 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1408 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1409 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1410 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1411 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1412 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1413 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1414 | goto failed; |
| 1415 | } |
| 1416 | |
| 1417 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1418 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1419 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1420 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1421 | goto failed; |
| 1422 | } |
| 1423 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1424 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1425 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1426 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1427 | goto failed; |
| 1428 | } |
| 1429 | |
| 1430 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1431 | bool changed = false; |
| 1432 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1433 | /* Setting limited discoverable when powered off is |
| 1434 | * not a valid operation since it requires a timeout |
| 1435 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1436 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1437 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1438 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1439 | changed = true; |
| 1440 | } |
| 1441 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1442 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1443 | if (err < 0) |
| 1444 | goto failed; |
| 1445 | |
| 1446 | if (changed) |
| 1447 | err = new_settings(hdev, sk); |
| 1448 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1449 | goto failed; |
| 1450 | } |
| 1451 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1452 | /* If the current mode is the same, then just update the timeout |
| 1453 | * value with the new value. And if only the timeout gets updated, |
| 1454 | * then no need for any HCI transactions. |
| 1455 | */ |
| 1456 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1457 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1458 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1459 | cancel_delayed_work(&hdev->discov_off); |
| 1460 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1461 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1462 | if (cp->val && hdev->discov_timeout > 0) { |
| 1463 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1464 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1465 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1466 | } |
| 1467 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1468 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1469 | goto failed; |
| 1470 | } |
| 1471 | |
| 1472 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1473 | if (!cmd) { |
| 1474 | err = -ENOMEM; |
| 1475 | goto failed; |
| 1476 | } |
| 1477 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1478 | /* Cancel any potential discoverable timeout that might be |
| 1479 | * still active and store new timeout value. The arming of |
| 1480 | * the timeout happens in the complete handler. |
| 1481 | */ |
| 1482 | cancel_delayed_work(&hdev->discov_off); |
| 1483 | hdev->discov_timeout = timeout; |
| 1484 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1485 | /* Limited discoverable mode */ |
| 1486 | if (cp->val == 0x02) |
| 1487 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1488 | else |
| 1489 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1490 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1491 | hci_req_init(&req, hdev); |
| 1492 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1493 | /* The procedure for LE-only controllers is much simpler - just |
| 1494 | * update the advertising data. |
| 1495 | */ |
| 1496 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1497 | goto update_ad; |
| 1498 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1499 | scan = SCAN_PAGE; |
| 1500 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1501 | if (cp->val) { |
| 1502 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1503 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1504 | if (cp->val == 0x02) { |
| 1505 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1506 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1507 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1508 | hci_cp.iac_lap[1] = 0x8b; |
| 1509 | hci_cp.iac_lap[2] = 0x9e; |
| 1510 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1511 | hci_cp.iac_lap[4] = 0x8b; |
| 1512 | hci_cp.iac_lap[5] = 0x9e; |
| 1513 | } else { |
| 1514 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1515 | hci_cp.num_iac = 1; |
| 1516 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1517 | hci_cp.iac_lap[1] = 0x8b; |
| 1518 | hci_cp.iac_lap[2] = 0x9e; |
| 1519 | } |
| 1520 | |
| 1521 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1522 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1523 | |
| 1524 | scan |= SCAN_INQUIRY; |
| 1525 | } else { |
| 1526 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1527 | } |
| 1528 | |
| 1529 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1530 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1531 | update_ad: |
| 1532 | update_adv_data(&req); |
| 1533 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1534 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1535 | if (err < 0) |
| 1536 | mgmt_pending_remove(cmd); |
| 1537 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1538 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1539 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1540 | return err; |
| 1541 | } |
| 1542 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1543 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1544 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1545 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1546 | struct hci_cp_write_page_scan_activity acp; |
| 1547 | u8 type; |
| 1548 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1549 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1550 | return; |
| 1551 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1552 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1553 | return; |
| 1554 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1555 | if (enable) { |
| 1556 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1557 | |
| 1558 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1559 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1560 | } else { |
| 1561 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1562 | |
| 1563 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1564 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1565 | } |
| 1566 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1567 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1568 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1569 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1570 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1571 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1572 | sizeof(acp), &acp); |
| 1573 | |
| 1574 | if (hdev->page_scan_type != type) |
| 1575 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1576 | } |
| 1577 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1578 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1579 | { |
| 1580 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1581 | struct mgmt_mode *cp; |
| 1582 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1583 | |
| 1584 | BT_DBG("status 0x%02x", status); |
| 1585 | |
| 1586 | hci_dev_lock(hdev); |
| 1587 | |
| 1588 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1589 | if (!cmd) |
| 1590 | goto unlock; |
| 1591 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1592 | if (status) { |
| 1593 | u8 mgmt_err = mgmt_status(status); |
| 1594 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1595 | goto remove_cmd; |
| 1596 | } |
| 1597 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1598 | cp = cmd->param; |
| 1599 | if (cp->val) |
| 1600 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1601 | else |
| 1602 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1603 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1604 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1605 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1606 | if (changed) |
| 1607 | new_settings(hdev, cmd->sk); |
| 1608 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1609 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1610 | mgmt_pending_remove(cmd); |
| 1611 | |
| 1612 | unlock: |
| 1613 | hci_dev_unlock(hdev); |
| 1614 | } |
| 1615 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1616 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1617 | struct sock *sk, u8 val) |
| 1618 | { |
| 1619 | bool changed = false; |
| 1620 | int err; |
| 1621 | |
| 1622 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1623 | changed = true; |
| 1624 | |
| 1625 | if (val) { |
| 1626 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1627 | } else { |
| 1628 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1629 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1630 | } |
| 1631 | |
| 1632 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1633 | if (err < 0) |
| 1634 | return err; |
| 1635 | |
| 1636 | if (changed) |
| 1637 | return new_settings(hdev, sk); |
| 1638 | |
| 1639 | return 0; |
| 1640 | } |
| 1641 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1642 | 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] | 1643 | u16 len) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1644 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1645 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1646 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1647 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1648 | u8 scan; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1649 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1650 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1651 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1652 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1653 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1654 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1655 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1656 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1657 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1658 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1659 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1660 | MGMT_STATUS_INVALID_PARAMS); |
| 1661 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1662 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1663 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1664 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1665 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1666 | goto failed; |
| 1667 | } |
| 1668 | |
| 1669 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1670 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1671 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1672 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1673 | goto failed; |
| 1674 | } |
| 1675 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1676 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1677 | if (!cmd) { |
| 1678 | err = -ENOMEM; |
| 1679 | goto failed; |
| 1680 | } |
| 1681 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1682 | hci_req_init(&req, hdev); |
| 1683 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1684 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1685 | * by-product of disabling connectable, we need to update the |
| 1686 | * advertising flags. |
| 1687 | */ |
| 1688 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1689 | if (!cp->val) { |
| 1690 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1691 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1692 | } |
| 1693 | update_adv_data(&req); |
| 1694 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1695 | if (cp->val) { |
| 1696 | scan = SCAN_PAGE; |
| 1697 | } else { |
| 1698 | scan = 0; |
| 1699 | |
| 1700 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1701 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1702 | cancel_delayed_work(&hdev->discov_off); |
| 1703 | } |
| 1704 | |
| 1705 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1706 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1707 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1708 | /* If we're going from non-connectable to connectable or |
| 1709 | * vice-versa when fast connectable is enabled ensure that fast |
| 1710 | * connectable gets disabled. write_fast_connectable won't do |
| 1711 | * anything if the page scan parameters are already what they |
| 1712 | * should be. |
| 1713 | */ |
| 1714 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1715 | write_fast_connectable(&req, false); |
| 1716 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1717 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) && |
| 1718 | hci_conn_num(hdev, LE_LINK) == 0) { |
| 1719 | disable_advertising(&req); |
| 1720 | enable_advertising(&req); |
| 1721 | } |
| 1722 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1723 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1724 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1725 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1726 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1727 | err = set_connectable_update_settings(hdev, sk, |
| 1728 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1729 | goto failed; |
| 1730 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1731 | |
| 1732 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1733 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1734 | return err; |
| 1735 | } |
| 1736 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1737 | 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] | 1738 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1739 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1740 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1741 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1742 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1743 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1744 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1745 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1746 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1747 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1748 | MGMT_STATUS_INVALID_PARAMS); |
| 1749 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1750 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1751 | |
| 1752 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1753 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1754 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1755 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1756 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1757 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1758 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1759 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1760 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1761 | if (changed) |
| 1762 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1763 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1764 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1765 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1766 | return err; |
| 1767 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1768 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1769 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1770 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1771 | { |
| 1772 | struct mgmt_mode *cp = data; |
| 1773 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1774 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1775 | int err; |
| 1776 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1777 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1778 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1779 | status = mgmt_bredr_support(hdev); |
| 1780 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1781 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1782 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1783 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1784 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1785 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1786 | MGMT_STATUS_INVALID_PARAMS); |
| 1787 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1788 | hci_dev_lock(hdev); |
| 1789 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1790 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1791 | bool changed = false; |
| 1792 | |
| 1793 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1794 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1795 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1796 | changed = true; |
| 1797 | } |
| 1798 | |
| 1799 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1800 | if (err < 0) |
| 1801 | goto failed; |
| 1802 | |
| 1803 | if (changed) |
| 1804 | err = new_settings(hdev, sk); |
| 1805 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1806 | goto failed; |
| 1807 | } |
| 1808 | |
| 1809 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1810 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1811 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1812 | goto failed; |
| 1813 | } |
| 1814 | |
| 1815 | val = !!cp->val; |
| 1816 | |
| 1817 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1818 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1819 | goto failed; |
| 1820 | } |
| 1821 | |
| 1822 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1823 | if (!cmd) { |
| 1824 | err = -ENOMEM; |
| 1825 | goto failed; |
| 1826 | } |
| 1827 | |
| 1828 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1829 | if (err < 0) { |
| 1830 | mgmt_pending_remove(cmd); |
| 1831 | goto failed; |
| 1832 | } |
| 1833 | |
| 1834 | failed: |
| 1835 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1836 | return err; |
| 1837 | } |
| 1838 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1839 | 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] | 1840 | { |
| 1841 | struct mgmt_mode *cp = data; |
| 1842 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1843 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1844 | int err; |
| 1845 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1846 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1847 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1848 | status = mgmt_bredr_support(hdev); |
| 1849 | if (status) |
| 1850 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 1851 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1852 | if (!lmp_ssp_capable(hdev)) |
| 1853 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1854 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1855 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1856 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1857 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1858 | MGMT_STATUS_INVALID_PARAMS); |
| 1859 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1860 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1861 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1862 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1863 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1864 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1865 | if (cp->val) { |
| 1866 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 1867 | &hdev->dev_flags); |
| 1868 | } else { |
| 1869 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 1870 | &hdev->dev_flags); |
| 1871 | if (!changed) |
| 1872 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 1873 | &hdev->dev_flags); |
| 1874 | else |
| 1875 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1879 | if (err < 0) |
| 1880 | goto failed; |
| 1881 | |
| 1882 | if (changed) |
| 1883 | err = new_settings(hdev, sk); |
| 1884 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1885 | goto failed; |
| 1886 | } |
| 1887 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1888 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 1889 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 1890 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1891 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1892 | goto failed; |
| 1893 | } |
| 1894 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1895 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1896 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1897 | goto failed; |
| 1898 | } |
| 1899 | |
| 1900 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1901 | if (!cmd) { |
| 1902 | err = -ENOMEM; |
| 1903 | goto failed; |
| 1904 | } |
| 1905 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1906 | if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 1907 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1908 | sizeof(cp->val), &cp->val); |
| 1909 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1910 | 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] | 1911 | if (err < 0) { |
| 1912 | mgmt_pending_remove(cmd); |
| 1913 | goto failed; |
| 1914 | } |
| 1915 | |
| 1916 | failed: |
| 1917 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1918 | return err; |
| 1919 | } |
| 1920 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1921 | 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] | 1922 | { |
| 1923 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1924 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1925 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1926 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1927 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1928 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1929 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1930 | status = mgmt_bredr_support(hdev); |
| 1931 | if (status) |
| 1932 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1933 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1934 | if (!lmp_ssp_capable(hdev)) |
| 1935 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1936 | MGMT_STATUS_NOT_SUPPORTED); |
| 1937 | |
| 1938 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 1939 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1940 | MGMT_STATUS_REJECTED); |
| 1941 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1942 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1943 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1944 | MGMT_STATUS_INVALID_PARAMS); |
| 1945 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1946 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1947 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1948 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1949 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1950 | } else { |
| 1951 | if (hdev_is_powered(hdev)) { |
| 1952 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1953 | MGMT_STATUS_REJECTED); |
| 1954 | goto unlock; |
| 1955 | } |
| 1956 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1957 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1958 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1959 | |
| 1960 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1961 | if (err < 0) |
| 1962 | goto unlock; |
| 1963 | |
| 1964 | if (changed) |
| 1965 | err = new_settings(hdev, sk); |
| 1966 | |
| 1967 | unlock: |
| 1968 | hci_dev_unlock(hdev); |
| 1969 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1970 | } |
| 1971 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1972 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 1973 | { |
| 1974 | struct cmd_lookup match = { NULL, hdev }; |
| 1975 | |
| 1976 | if (status) { |
| 1977 | u8 mgmt_err = mgmt_status(status); |
| 1978 | |
| 1979 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1980 | &mgmt_err); |
| 1981 | return; |
| 1982 | } |
| 1983 | |
| 1984 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1985 | |
| 1986 | new_settings(hdev, match.sk); |
| 1987 | |
| 1988 | if (match.sk) |
| 1989 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1990 | |
| 1991 | /* Make sure the controller has a good default for |
| 1992 | * advertising data. Restrict the update to when LE |
| 1993 | * has actually been enabled. During power on, the |
| 1994 | * update in powered_update_hci will take care of it. |
| 1995 | */ |
| 1996 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 1997 | struct hci_request req; |
| 1998 | |
| 1999 | hci_dev_lock(hdev); |
| 2000 | |
| 2001 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 2002 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 2003 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2004 | hci_req_run(&req, NULL); |
| 2005 | |
| 2006 | hci_dev_unlock(hdev); |
| 2007 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2008 | } |
| 2009 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2010 | 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] | 2011 | { |
| 2012 | struct mgmt_mode *cp = data; |
| 2013 | struct hci_cp_write_le_host_supported hci_cp; |
| 2014 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2015 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2016 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2017 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2018 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2019 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2020 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2021 | if (!lmp_le_capable(hdev)) |
| 2022 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2023 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2024 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2025 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2026 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2027 | MGMT_STATUS_INVALID_PARAMS); |
| 2028 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2029 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2030 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2031 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2032 | MGMT_STATUS_REJECTED); |
| 2033 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2034 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2035 | |
| 2036 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2037 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2038 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2039 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2040 | bool changed = false; |
| 2041 | |
| 2042 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2043 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 2044 | changed = true; |
| 2045 | } |
| 2046 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 2047 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 2048 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2049 | changed = true; |
| 2050 | } |
| 2051 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2052 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2053 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2054 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2055 | |
| 2056 | if (changed) |
| 2057 | err = new_settings(hdev, sk); |
| 2058 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2059 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2060 | } |
| 2061 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2062 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2063 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2064 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2065 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2066 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2070 | if (!cmd) { |
| 2071 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2072 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2073 | } |
| 2074 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2075 | hci_req_init(&req, hdev); |
| 2076 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2077 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2078 | |
| 2079 | if (val) { |
| 2080 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2081 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2082 | } else { |
| 2083 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 2084 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2085 | } |
| 2086 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2087 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2088 | &hci_cp); |
| 2089 | |
| 2090 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2091 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2092 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2093 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2094 | unlock: |
| 2095 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2096 | return err; |
| 2097 | } |
| 2098 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2099 | /* This is a helper function to test for pending mgmt commands that can |
| 2100 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2101 | * mgmt command at a time since otherwise we cannot easily track what |
| 2102 | * the current values are, will be, and based on that calculate if a new |
| 2103 | * HCI command needs to be sent and if yes with what value. |
| 2104 | */ |
| 2105 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2106 | { |
| 2107 | struct pending_cmd *cmd; |
| 2108 | |
| 2109 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2110 | switch (cmd->opcode) { |
| 2111 | case MGMT_OP_ADD_UUID: |
| 2112 | case MGMT_OP_REMOVE_UUID: |
| 2113 | case MGMT_OP_SET_DEV_CLASS: |
| 2114 | case MGMT_OP_SET_POWERED: |
| 2115 | return true; |
| 2116 | } |
| 2117 | } |
| 2118 | |
| 2119 | return false; |
| 2120 | } |
| 2121 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2122 | static const u8 bluetooth_base_uuid[] = { |
| 2123 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2124 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2125 | }; |
| 2126 | |
| 2127 | static u8 get_uuid_size(const u8 *uuid) |
| 2128 | { |
| 2129 | u32 val; |
| 2130 | |
| 2131 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2132 | return 128; |
| 2133 | |
| 2134 | val = get_unaligned_le32(&uuid[12]); |
| 2135 | if (val > 0xffff) |
| 2136 | return 32; |
| 2137 | |
| 2138 | return 16; |
| 2139 | } |
| 2140 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2141 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2142 | { |
| 2143 | struct pending_cmd *cmd; |
| 2144 | |
| 2145 | hci_dev_lock(hdev); |
| 2146 | |
| 2147 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2148 | if (!cmd) |
| 2149 | goto unlock; |
| 2150 | |
| 2151 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 2152 | hdev->dev_class, 3); |
| 2153 | |
| 2154 | mgmt_pending_remove(cmd); |
| 2155 | |
| 2156 | unlock: |
| 2157 | hci_dev_unlock(hdev); |
| 2158 | } |
| 2159 | |
| 2160 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2161 | { |
| 2162 | BT_DBG("status 0x%02x", status); |
| 2163 | |
| 2164 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2165 | } |
| 2166 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2167 | 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] | 2168 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2169 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2170 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2171 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2172 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2173 | int err; |
| 2174 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2175 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2176 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2177 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2178 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2179 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2180 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2181 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2182 | goto failed; |
| 2183 | } |
| 2184 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2185 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2186 | if (!uuid) { |
| 2187 | err = -ENOMEM; |
| 2188 | goto failed; |
| 2189 | } |
| 2190 | |
| 2191 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2192 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2193 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2194 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2195 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2196 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2197 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2198 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2199 | update_class(&req); |
| 2200 | update_eir(&req); |
| 2201 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2202 | err = hci_req_run(&req, add_uuid_complete); |
| 2203 | if (err < 0) { |
| 2204 | if (err != -ENODATA) |
| 2205 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2206 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2207 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2208 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2209 | goto failed; |
| 2210 | } |
| 2211 | |
| 2212 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2213 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2214 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2215 | goto failed; |
| 2216 | } |
| 2217 | |
| 2218 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2219 | |
| 2220 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2221 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2222 | return err; |
| 2223 | } |
| 2224 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2225 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2226 | { |
| 2227 | if (!hdev_is_powered(hdev)) |
| 2228 | return false; |
| 2229 | |
| 2230 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2231 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2232 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2233 | return true; |
| 2234 | } |
| 2235 | |
| 2236 | return false; |
| 2237 | } |
| 2238 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2239 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2240 | { |
| 2241 | BT_DBG("status 0x%02x", status); |
| 2242 | |
| 2243 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2244 | } |
| 2245 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2246 | 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] | 2247 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2248 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2249 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2250 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2251 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2252 | 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] | 2253 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2254 | int err, found; |
| 2255 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2256 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2257 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2258 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2259 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2260 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2261 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2262 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2263 | goto unlock; |
| 2264 | } |
| 2265 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2266 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2267 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2268 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2269 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2270 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2271 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2272 | goto unlock; |
| 2273 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2274 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2275 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | found = 0; |
| 2279 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2280 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2281 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2282 | continue; |
| 2283 | |
| 2284 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2285 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2286 | found++; |
| 2287 | } |
| 2288 | |
| 2289 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2290 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2291 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2292 | goto unlock; |
| 2293 | } |
| 2294 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2295 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2296 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2297 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2298 | update_class(&req); |
| 2299 | update_eir(&req); |
| 2300 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2301 | err = hci_req_run(&req, remove_uuid_complete); |
| 2302 | if (err < 0) { |
| 2303 | if (err != -ENODATA) |
| 2304 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2305 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2306 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2307 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2308 | goto unlock; |
| 2309 | } |
| 2310 | |
| 2311 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2312 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2313 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2314 | goto unlock; |
| 2315 | } |
| 2316 | |
| 2317 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2318 | |
| 2319 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2320 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2321 | return err; |
| 2322 | } |
| 2323 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2324 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2325 | { |
| 2326 | BT_DBG("status 0x%02x", status); |
| 2327 | |
| 2328 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2329 | } |
| 2330 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2331 | 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] | 2332 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2333 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2334 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2335 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2336 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2337 | int err; |
| 2338 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2339 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2340 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2341 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2342 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2343 | MGMT_STATUS_NOT_SUPPORTED); |
| 2344 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2345 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2346 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2347 | if (pending_eir_or_class(hdev)) { |
| 2348 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2349 | MGMT_STATUS_BUSY); |
| 2350 | goto unlock; |
| 2351 | } |
| 2352 | |
| 2353 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2354 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2355 | MGMT_STATUS_INVALID_PARAMS); |
| 2356 | goto unlock; |
| 2357 | } |
| 2358 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2359 | hdev->major_class = cp->major; |
| 2360 | hdev->minor_class = cp->minor; |
| 2361 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2362 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2363 | 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] | 2364 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2365 | goto unlock; |
| 2366 | } |
| 2367 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2368 | hci_req_init(&req, hdev); |
| 2369 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2370 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2371 | hci_dev_unlock(hdev); |
| 2372 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2373 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2374 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2375 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2376 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2377 | update_class(&req); |
| 2378 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2379 | err = hci_req_run(&req, set_class_complete); |
| 2380 | if (err < 0) { |
| 2381 | if (err != -ENODATA) |
| 2382 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2383 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2384 | 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] | 2385 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2386 | goto unlock; |
| 2387 | } |
| 2388 | |
| 2389 | 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] | 2390 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2391 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2392 | goto unlock; |
| 2393 | } |
| 2394 | |
| 2395 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2396 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2397 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2398 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2399 | return err; |
| 2400 | } |
| 2401 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2402 | 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] | 2403 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2404 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2405 | struct mgmt_cp_load_link_keys *cp = data; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2406 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2407 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2408 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2409 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2410 | BT_DBG("request for %s", hdev->name); |
| 2411 | |
| 2412 | if (!lmp_bredr_capable(hdev)) |
| 2413 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2414 | MGMT_STATUS_NOT_SUPPORTED); |
| 2415 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2416 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2417 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2418 | expected_len = sizeof(*cp) + key_count * |
| 2419 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2420 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2421 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2422 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2423 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2424 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2425 | } |
| 2426 | |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2427 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2428 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2429 | MGMT_STATUS_INVALID_PARAMS); |
| 2430 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2431 | 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] | 2432 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2433 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2434 | for (i = 0; i < key_count; i++) { |
| 2435 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2436 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2437 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2438 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2439 | MGMT_STATUS_INVALID_PARAMS); |
| 2440 | } |
| 2441 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2442 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2443 | |
| 2444 | hci_link_keys_clear(hdev); |
| 2445 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2446 | if (cp->debug_keys) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2447 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 2448 | &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2449 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2450 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 2451 | &hdev->dev_flags); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2452 | |
| 2453 | if (changed) |
| 2454 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2455 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2456 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2457 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2458 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2459 | /* Always ignore debug keys and require a new pairing if |
| 2460 | * the user wants to use them. |
| 2461 | */ |
| 2462 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2463 | continue; |
| 2464 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2465 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2466 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2467 | } |
| 2468 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2469 | 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] | 2470 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2471 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2472 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2473 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2474 | } |
| 2475 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2476 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2477 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2478 | { |
| 2479 | struct mgmt_ev_device_unpaired ev; |
| 2480 | |
| 2481 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2482 | ev.addr.type = addr_type; |
| 2483 | |
| 2484 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2485 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2486 | } |
| 2487 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2488 | 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] | 2489 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2490 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2491 | struct mgmt_cp_unpair_device *cp = data; |
| 2492 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2493 | struct hci_cp_disconnect dc; |
| 2494 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2495 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2496 | int err; |
| 2497 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2498 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2499 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2500 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2501 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2502 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2503 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2504 | MGMT_STATUS_INVALID_PARAMS, |
| 2505 | &rp, sizeof(rp)); |
| 2506 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2507 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2508 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2509 | MGMT_STATUS_INVALID_PARAMS, |
| 2510 | &rp, sizeof(rp)); |
| 2511 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2512 | hci_dev_lock(hdev); |
| 2513 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2514 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2515 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2516 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2517 | goto unlock; |
| 2518 | } |
| 2519 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2520 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2521 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2522 | } else { |
| 2523 | u8 addr_type; |
| 2524 | |
| 2525 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2526 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2527 | else |
| 2528 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2529 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2530 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2531 | |
Andre Guedes | a9b0a04 | 2014-02-26 20:21:52 -0300 | [diff] [blame] | 2532 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2533 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2534 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2535 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2536 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2537 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2538 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2539 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2540 | goto unlock; |
| 2541 | } |
| 2542 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2543 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2544 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2545 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2546 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2547 | else |
| 2548 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2549 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2550 | } else { |
| 2551 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2552 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2553 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2554 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2555 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2556 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2557 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2558 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2559 | } |
| 2560 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2561 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2562 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2563 | if (!cmd) { |
| 2564 | err = -ENOMEM; |
| 2565 | goto unlock; |
| 2566 | } |
| 2567 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2568 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2569 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2570 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2571 | if (err < 0) |
| 2572 | mgmt_pending_remove(cmd); |
| 2573 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2574 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2575 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2576 | return err; |
| 2577 | } |
| 2578 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2579 | 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] | 2580 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2581 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2582 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2583 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2584 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2585 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2586 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2587 | int err; |
| 2588 | |
| 2589 | BT_DBG(""); |
| 2590 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2591 | memset(&rp, 0, sizeof(rp)); |
| 2592 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2593 | rp.addr.type = cp->addr.type; |
| 2594 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2595 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2596 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2597 | MGMT_STATUS_INVALID_PARAMS, |
| 2598 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2599 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2600 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2601 | |
| 2602 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2603 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2604 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2605 | goto failed; |
| 2606 | } |
| 2607 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2608 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2609 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2610 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2611 | goto failed; |
| 2612 | } |
| 2613 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2614 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2615 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2616 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2617 | else |
| 2618 | 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] | 2619 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2620 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2621 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2622 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2623 | goto failed; |
| 2624 | } |
| 2625 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2626 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2627 | if (!cmd) { |
| 2628 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2629 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2630 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2631 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2632 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2633 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2634 | |
| 2635 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2636 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2637 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2638 | |
| 2639 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2640 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2641 | return err; |
| 2642 | } |
| 2643 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2644 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2645 | { |
| 2646 | switch (link_type) { |
| 2647 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2648 | switch (addr_type) { |
| 2649 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2650 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2651 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2652 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2653 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2654 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2655 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2656 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2657 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2658 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2659 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2660 | } |
| 2661 | } |
| 2662 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2663 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2664 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2665 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2666 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2667 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2668 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2669 | int err; |
| 2670 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2671 | |
| 2672 | BT_DBG(""); |
| 2673 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2674 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2675 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2676 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2677 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2678 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2679 | goto unlock; |
| 2680 | } |
| 2681 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2682 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2683 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2684 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2685 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2686 | } |
| 2687 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2688 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2689 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2690 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2691 | err = -ENOMEM; |
| 2692 | goto unlock; |
| 2693 | } |
| 2694 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2695 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2696 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2697 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2698 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2699 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2700 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2701 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2702 | continue; |
| 2703 | i++; |
| 2704 | } |
| 2705 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2706 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2707 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2708 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2709 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2710 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2711 | 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] | 2712 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2713 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2714 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2715 | |
| 2716 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2717 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2718 | return err; |
| 2719 | } |
| 2720 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2721 | 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] | 2722 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2723 | { |
| 2724 | struct pending_cmd *cmd; |
| 2725 | int err; |
| 2726 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2727 | 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] | 2728 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2729 | if (!cmd) |
| 2730 | return -ENOMEM; |
| 2731 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2732 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2733 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2734 | if (err < 0) |
| 2735 | mgmt_pending_remove(cmd); |
| 2736 | |
| 2737 | return err; |
| 2738 | } |
| 2739 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2740 | 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] | 2741 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2742 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2743 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2744 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2745 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2746 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2747 | int err; |
| 2748 | |
| 2749 | BT_DBG(""); |
| 2750 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2751 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2752 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2753 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2754 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2755 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2756 | goto failed; |
| 2757 | } |
| 2758 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2759 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2760 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2761 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2762 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2763 | goto failed; |
| 2764 | } |
| 2765 | |
| 2766 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2767 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2768 | |
| 2769 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2770 | |
| 2771 | BT_ERR("PIN code is not 16 bytes long"); |
| 2772 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2773 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2774 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2775 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2776 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2777 | |
| 2778 | goto failed; |
| 2779 | } |
| 2780 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2781 | 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] | 2782 | if (!cmd) { |
| 2783 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2784 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2785 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2786 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2787 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2788 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2789 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2790 | |
| 2791 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2792 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2793 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2794 | |
| 2795 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2796 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2797 | return err; |
| 2798 | } |
| 2799 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2800 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2801 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2802 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2803 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2804 | |
| 2805 | BT_DBG(""); |
| 2806 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2807 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
| 2808 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2809 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
| 2810 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2811 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2812 | |
| 2813 | hdev->io_capability = cp->io_capability; |
| 2814 | |
| 2815 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2816 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2817 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2818 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2819 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2820 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2821 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2822 | } |
| 2823 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2824 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2825 | { |
| 2826 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2827 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2828 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2829 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2830 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2831 | continue; |
| 2832 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2833 | if (cmd->user_data != conn) |
| 2834 | continue; |
| 2835 | |
| 2836 | return cmd; |
| 2837 | } |
| 2838 | |
| 2839 | return NULL; |
| 2840 | } |
| 2841 | |
| 2842 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 2843 | { |
| 2844 | struct mgmt_rp_pair_device rp; |
| 2845 | struct hci_conn *conn = cmd->user_data; |
| 2846 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2847 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2848 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2849 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 2850 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2851 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2852 | |
| 2853 | /* So we don't get further callbacks for this connection */ |
| 2854 | conn->connect_cfm_cb = NULL; |
| 2855 | conn->security_cfm_cb = NULL; |
| 2856 | conn->disconn_cfm_cb = NULL; |
| 2857 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2858 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2859 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2860 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2861 | } |
| 2862 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2863 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2864 | { |
| 2865 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 2866 | struct pending_cmd *cmd; |
| 2867 | |
| 2868 | cmd = find_pairing(conn); |
| 2869 | if (cmd) |
| 2870 | pairing_complete(cmd, status); |
| 2871 | } |
| 2872 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2873 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2874 | { |
| 2875 | struct pending_cmd *cmd; |
| 2876 | |
| 2877 | BT_DBG("status %u", status); |
| 2878 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2879 | cmd = find_pairing(conn); |
| 2880 | if (!cmd) |
| 2881 | BT_DBG("Unable to find a pending command"); |
| 2882 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 2883 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2884 | } |
| 2885 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2886 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2887 | { |
| 2888 | struct pending_cmd *cmd; |
| 2889 | |
| 2890 | BT_DBG("status %u", status); |
| 2891 | |
| 2892 | if (!status) |
| 2893 | return; |
| 2894 | |
| 2895 | cmd = find_pairing(conn); |
| 2896 | if (!cmd) |
| 2897 | BT_DBG("Unable to find a pending command"); |
| 2898 | else |
| 2899 | pairing_complete(cmd, mgmt_status(status)); |
| 2900 | } |
| 2901 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2902 | 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] | 2903 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2904 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2905 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2906 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2907 | struct pending_cmd *cmd; |
| 2908 | u8 sec_level, auth_type; |
| 2909 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2910 | int err; |
| 2911 | |
| 2912 | BT_DBG(""); |
| 2913 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2914 | memset(&rp, 0, sizeof(rp)); |
| 2915 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2916 | rp.addr.type = cp->addr.type; |
| 2917 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2918 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2919 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2920 | MGMT_STATUS_INVALID_PARAMS, |
| 2921 | &rp, sizeof(rp)); |
| 2922 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2923 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
| 2924 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2925 | MGMT_STATUS_INVALID_PARAMS, |
| 2926 | &rp, sizeof(rp)); |
| 2927 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2928 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2929 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2930 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2931 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2932 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2933 | goto unlock; |
| 2934 | } |
| 2935 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2936 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2937 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2938 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2939 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2940 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 2941 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2942 | } else { |
| 2943 | u8 addr_type; |
| 2944 | |
| 2945 | /* Convert from L2CAP channel address type to HCI address type |
| 2946 | */ |
| 2947 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2948 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2949 | else |
| 2950 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2951 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2952 | /* When pairing a new device, it is expected to remember |
| 2953 | * this device for future connections. Adding the connection |
| 2954 | * parameter information ahead of time allows tracking |
| 2955 | * of the slave preferred values and will speed up any |
| 2956 | * further connection establishment. |
| 2957 | * |
| 2958 | * If connection parameters already exist, then they |
| 2959 | * will be kept and this function does nothing. |
| 2960 | */ |
| 2961 | hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 2962 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2963 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2964 | sec_level, auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2965 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2966 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2967 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2968 | int status; |
| 2969 | |
| 2970 | if (PTR_ERR(conn) == -EBUSY) |
| 2971 | status = MGMT_STATUS_BUSY; |
| 2972 | else |
| 2973 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2974 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2975 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2976 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2977 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2978 | goto unlock; |
| 2979 | } |
| 2980 | |
| 2981 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2982 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2983 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2984 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2985 | goto unlock; |
| 2986 | } |
| 2987 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2988 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2989 | if (!cmd) { |
| 2990 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2991 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2992 | goto unlock; |
| 2993 | } |
| 2994 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2995 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2996 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2997 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2998 | conn->security_cfm_cb = pairing_complete_cb; |
| 2999 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3000 | } else { |
| 3001 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3002 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3003 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3004 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3005 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3006 | conn->io_capability = cp->io_cap; |
| 3007 | cmd->user_data = conn; |
| 3008 | |
| 3009 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3010 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3011 | pairing_complete(cmd, 0); |
| 3012 | |
| 3013 | err = 0; |
| 3014 | |
| 3015 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3016 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3017 | return err; |
| 3018 | } |
| 3019 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3020 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3021 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3022 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3023 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3024 | struct pending_cmd *cmd; |
| 3025 | struct hci_conn *conn; |
| 3026 | int err; |
| 3027 | |
| 3028 | BT_DBG(""); |
| 3029 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3030 | hci_dev_lock(hdev); |
| 3031 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3032 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3033 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3034 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3035 | goto unlock; |
| 3036 | } |
| 3037 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3038 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3039 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3040 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3041 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3042 | goto unlock; |
| 3043 | } |
| 3044 | |
| 3045 | conn = cmd->user_data; |
| 3046 | |
| 3047 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3048 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3049 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3050 | goto unlock; |
| 3051 | } |
| 3052 | |
| 3053 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3054 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3055 | 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] | 3056 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3057 | unlock: |
| 3058 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3059 | return err; |
| 3060 | } |
| 3061 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3062 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3063 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3064 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3065 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3066 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3067 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3068 | int err; |
| 3069 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3070 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3071 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3072 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3073 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3074 | MGMT_STATUS_NOT_POWERED, addr, |
| 3075 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3076 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3077 | } |
| 3078 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3079 | if (addr->type == BDADDR_BREDR) |
| 3080 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3081 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3082 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3083 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3084 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3085 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3086 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3087 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3088 | goto done; |
| 3089 | } |
| 3090 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3091 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3092 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3093 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3094 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3095 | MGMT_STATUS_SUCCESS, addr, |
| 3096 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3097 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3098 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3099 | MGMT_STATUS_FAILED, addr, |
| 3100 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3101 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3102 | goto done; |
| 3103 | } |
| 3104 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3105 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3106 | if (!cmd) { |
| 3107 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3108 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3109 | } |
| 3110 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3111 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3112 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3113 | struct hci_cp_user_passkey_reply cp; |
| 3114 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3115 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3116 | cp.passkey = passkey; |
| 3117 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3118 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3119 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3120 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3121 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3122 | if (err < 0) |
| 3123 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3124 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3125 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3126 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3127 | return err; |
| 3128 | } |
| 3129 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3130 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3131 | void *data, u16 len) |
| 3132 | { |
| 3133 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3134 | |
| 3135 | BT_DBG(""); |
| 3136 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3137 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3138 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3139 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3140 | } |
| 3141 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3142 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3143 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3144 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3145 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3146 | |
| 3147 | BT_DBG(""); |
| 3148 | |
| 3149 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3150 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3151 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3152 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3153 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3154 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3155 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3156 | } |
| 3157 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3158 | 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] | 3159 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3160 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3161 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3162 | |
| 3163 | BT_DBG(""); |
| 3164 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3165 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3166 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3167 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3168 | } |
| 3169 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3170 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3171 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3172 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3173 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3174 | |
| 3175 | BT_DBG(""); |
| 3176 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3177 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3178 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3179 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3180 | } |
| 3181 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3182 | 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] | 3183 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3184 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3185 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3186 | |
| 3187 | BT_DBG(""); |
| 3188 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3189 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3190 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3191 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3192 | } |
| 3193 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3194 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3195 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3196 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3197 | struct hci_cp_write_local_name cp; |
| 3198 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3199 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3200 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3201 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3202 | } |
| 3203 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3204 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3205 | { |
| 3206 | struct mgmt_cp_set_local_name *cp; |
| 3207 | struct pending_cmd *cmd; |
| 3208 | |
| 3209 | BT_DBG("status 0x%02x", status); |
| 3210 | |
| 3211 | hci_dev_lock(hdev); |
| 3212 | |
| 3213 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3214 | if (!cmd) |
| 3215 | goto unlock; |
| 3216 | |
| 3217 | cp = cmd->param; |
| 3218 | |
| 3219 | if (status) |
| 3220 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3221 | mgmt_status(status)); |
| 3222 | else |
| 3223 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3224 | cp, sizeof(*cp)); |
| 3225 | |
| 3226 | mgmt_pending_remove(cmd); |
| 3227 | |
| 3228 | unlock: |
| 3229 | hci_dev_unlock(hdev); |
| 3230 | } |
| 3231 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3232 | 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] | 3233 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3234 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3235 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3236 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3237 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3238 | int err; |
| 3239 | |
| 3240 | BT_DBG(""); |
| 3241 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3242 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3243 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3244 | /* If the old values are the same as the new ones just return a |
| 3245 | * direct command complete event. |
| 3246 | */ |
| 3247 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3248 | !memcmp(hdev->short_name, cp->short_name, |
| 3249 | sizeof(hdev->short_name))) { |
| 3250 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3251 | data, len); |
| 3252 | goto failed; |
| 3253 | } |
| 3254 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3255 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3256 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3257 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3258 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3259 | |
| 3260 | 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] | 3261 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3262 | if (err < 0) |
| 3263 | goto failed; |
| 3264 | |
| 3265 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3266 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3267 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3268 | goto failed; |
| 3269 | } |
| 3270 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3271 | 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] | 3272 | if (!cmd) { |
| 3273 | err = -ENOMEM; |
| 3274 | goto failed; |
| 3275 | } |
| 3276 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3277 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3278 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3279 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3280 | |
| 3281 | if (lmp_bredr_capable(hdev)) { |
| 3282 | update_name(&req); |
| 3283 | update_eir(&req); |
| 3284 | } |
| 3285 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3286 | /* The name is stored in the scan response data and so |
| 3287 | * no need to udpate the advertising data here. |
| 3288 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3289 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3290 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3291 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3292 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3293 | if (err < 0) |
| 3294 | mgmt_pending_remove(cmd); |
| 3295 | |
| 3296 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3297 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3298 | return err; |
| 3299 | } |
| 3300 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3301 | 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] | 3302 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3303 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3304 | struct pending_cmd *cmd; |
| 3305 | int err; |
| 3306 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3307 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3308 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3309 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3310 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3311 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3312 | 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] | 3313 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3314 | goto unlock; |
| 3315 | } |
| 3316 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3317 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3318 | 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] | 3319 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3320 | goto unlock; |
| 3321 | } |
| 3322 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3323 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3324 | 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] | 3325 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3326 | goto unlock; |
| 3327 | } |
| 3328 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3329 | 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] | 3330 | if (!cmd) { |
| 3331 | err = -ENOMEM; |
| 3332 | goto unlock; |
| 3333 | } |
| 3334 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3335 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3336 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3337 | 0, NULL); |
| 3338 | else |
| 3339 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3340 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3341 | if (err < 0) |
| 3342 | mgmt_pending_remove(cmd); |
| 3343 | |
| 3344 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3345 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3346 | return err; |
| 3347 | } |
| 3348 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3349 | 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] | 3350 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3351 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3352 | int err; |
| 3353 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3354 | BT_DBG("%s ", hdev->name); |
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_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3357 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3358 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3359 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3360 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3361 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3362 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3363 | cp->hash, cp->randomizer); |
| 3364 | if (err < 0) |
| 3365 | status = MGMT_STATUS_FAILED; |
| 3366 | else |
| 3367 | status = MGMT_STATUS_SUCCESS; |
| 3368 | |
| 3369 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3370 | status, &cp->addr, sizeof(cp->addr)); |
| 3371 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3372 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3373 | u8 status; |
| 3374 | |
| 3375 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3376 | cp->hash192, |
| 3377 | cp->randomizer192, |
| 3378 | cp->hash256, |
| 3379 | cp->randomizer256); |
| 3380 | if (err < 0) |
| 3381 | status = MGMT_STATUS_FAILED; |
| 3382 | else |
| 3383 | status = MGMT_STATUS_SUCCESS; |
| 3384 | |
| 3385 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3386 | status, &cp->addr, sizeof(cp->addr)); |
| 3387 | } else { |
| 3388 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3389 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3390 | MGMT_STATUS_INVALID_PARAMS); |
| 3391 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3392 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3393 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3394 | return err; |
| 3395 | } |
| 3396 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3397 | 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] | 3398 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3399 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3400 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3401 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3402 | int err; |
| 3403 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3404 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3405 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3406 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3407 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3408 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3409 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3410 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3411 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3412 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3413 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3414 | 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] | 3415 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3416 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3417 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3418 | return err; |
| 3419 | } |
| 3420 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3421 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3422 | { |
| 3423 | struct pending_cmd *cmd; |
| 3424 | u8 type; |
| 3425 | int err; |
| 3426 | |
| 3427 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3428 | |
| 3429 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3430 | if (!cmd) |
| 3431 | return -ENOENT; |
| 3432 | |
| 3433 | type = hdev->discovery.type; |
| 3434 | |
| 3435 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3436 | &type, sizeof(type)); |
| 3437 | mgmt_pending_remove(cmd); |
| 3438 | |
| 3439 | return err; |
| 3440 | } |
| 3441 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3442 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3443 | { |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3444 | unsigned long timeout = 0; |
| 3445 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3446 | BT_DBG("status %d", status); |
| 3447 | |
| 3448 | if (status) { |
| 3449 | hci_dev_lock(hdev); |
| 3450 | mgmt_start_discovery_failed(hdev, status); |
| 3451 | hci_dev_unlock(hdev); |
| 3452 | return; |
| 3453 | } |
| 3454 | |
| 3455 | hci_dev_lock(hdev); |
| 3456 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3457 | hci_dev_unlock(hdev); |
| 3458 | |
| 3459 | switch (hdev->discovery.type) { |
| 3460 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3461 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3462 | break; |
| 3463 | |
| 3464 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3465 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3466 | break; |
| 3467 | |
| 3468 | case DISCOV_TYPE_BREDR: |
| 3469 | break; |
| 3470 | |
| 3471 | default: |
| 3472 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3473 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3474 | |
| 3475 | if (!timeout) |
| 3476 | return; |
| 3477 | |
| 3478 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3479 | } |
| 3480 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3481 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3482 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3483 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3484 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3485 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3486 | struct hci_cp_le_set_scan_param param_cp; |
| 3487 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3488 | struct hci_cp_inquiry inq_cp; |
| 3489 | struct hci_request req; |
| 3490 | /* General inquiry access code (GIAC) */ |
| 3491 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3492 | u8 status, own_addr_type; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3493 | int err; |
| 3494 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3495 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3496 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3497 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3498 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3499 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3500 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3501 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3502 | goto failed; |
| 3503 | } |
| 3504 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3505 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3506 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3507 | MGMT_STATUS_BUSY); |
| 3508 | goto failed; |
| 3509 | } |
| 3510 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3511 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3512 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3513 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3514 | goto failed; |
| 3515 | } |
| 3516 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3517 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3518 | if (!cmd) { |
| 3519 | err = -ENOMEM; |
| 3520 | goto failed; |
| 3521 | } |
| 3522 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3523 | hdev->discovery.type = cp->type; |
| 3524 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3525 | hci_req_init(&req, hdev); |
| 3526 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3527 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3528 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3529 | status = mgmt_bredr_support(hdev); |
| 3530 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3531 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3532 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3533 | mgmt_pending_remove(cmd); |
| 3534 | goto failed; |
| 3535 | } |
| 3536 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3537 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3538 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3539 | MGMT_STATUS_BUSY); |
| 3540 | mgmt_pending_remove(cmd); |
| 3541 | goto failed; |
| 3542 | } |
| 3543 | |
| 3544 | hci_inquiry_cache_flush(hdev); |
| 3545 | |
| 3546 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3547 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3548 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3549 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3550 | break; |
| 3551 | |
| 3552 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3553 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3554 | status = mgmt_le_support(hdev); |
| 3555 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3556 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3557 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3558 | mgmt_pending_remove(cmd); |
| 3559 | goto failed; |
| 3560 | } |
| 3561 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3562 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3563 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3564 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3565 | MGMT_STATUS_NOT_SUPPORTED); |
| 3566 | mgmt_pending_remove(cmd); |
| 3567 | goto failed; |
| 3568 | } |
| 3569 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3570 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3571 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3572 | MGMT_STATUS_REJECTED); |
| 3573 | mgmt_pending_remove(cmd); |
| 3574 | goto failed; |
| 3575 | } |
| 3576 | |
Andre Guedes | c54c386 | 2014-02-26 20:21:50 -0300 | [diff] [blame] | 3577 | /* If controller is scanning, it means the background scanning |
| 3578 | * is running. Thus, we should temporarily stop it in order to |
| 3579 | * set the discovery scanning parameters. |
| 3580 | */ |
| 3581 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3582 | hci_req_add_le_scan_disable(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3583 | |
| 3584 | memset(¶m_cp, 0, sizeof(param_cp)); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3585 | |
Marcel Holtmann | 94b1fc9 | 2014-02-23 20:25:54 -0800 | [diff] [blame] | 3586 | /* All active scans will be done with either a resolvable |
| 3587 | * private address (when privacy feature has been enabled) |
| 3588 | * or unresolvable private address. |
| 3589 | */ |
| 3590 | err = hci_update_random_address(&req, true, &own_addr_type); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3591 | if (err < 0) { |
| 3592 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3593 | MGMT_STATUS_FAILED); |
| 3594 | mgmt_pending_remove(cmd); |
| 3595 | goto failed; |
| 3596 | } |
| 3597 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3598 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3599 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3600 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3601 | param_cp.own_address_type = own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3602 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3603 | ¶m_cp); |
| 3604 | |
| 3605 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3606 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3607 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3608 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3609 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3610 | break; |
| 3611 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3612 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3613 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3614 | MGMT_STATUS_INVALID_PARAMS); |
| 3615 | mgmt_pending_remove(cmd); |
| 3616 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3617 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3618 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3619 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3620 | if (err < 0) |
| 3621 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3622 | else |
| 3623 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3624 | |
| 3625 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3626 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3627 | return err; |
| 3628 | } |
| 3629 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3630 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3631 | { |
| 3632 | struct pending_cmd *cmd; |
| 3633 | int err; |
| 3634 | |
| 3635 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3636 | if (!cmd) |
| 3637 | return -ENOENT; |
| 3638 | |
| 3639 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3640 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3641 | mgmt_pending_remove(cmd); |
| 3642 | |
| 3643 | return err; |
| 3644 | } |
| 3645 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3646 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3647 | { |
| 3648 | BT_DBG("status %d", status); |
| 3649 | |
| 3650 | hci_dev_lock(hdev); |
| 3651 | |
| 3652 | if (status) { |
| 3653 | mgmt_stop_discovery_failed(hdev, status); |
| 3654 | goto unlock; |
| 3655 | } |
| 3656 | |
| 3657 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3658 | |
| 3659 | unlock: |
| 3660 | hci_dev_unlock(hdev); |
| 3661 | } |
| 3662 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3663 | 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] | 3664 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3665 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3666 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3667 | struct pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3668 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3669 | int err; |
| 3670 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3671 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3672 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3673 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3674 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3675 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3676 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3677 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3678 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3679 | goto unlock; |
| 3680 | } |
| 3681 | |
| 3682 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3683 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3684 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3685 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3686 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3687 | } |
| 3688 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3689 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3690 | if (!cmd) { |
| 3691 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3692 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3693 | } |
| 3694 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3695 | hci_req_init(&req, hdev); |
| 3696 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3697 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3698 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3699 | err = hci_req_run(&req, stop_discovery_complete); |
| 3700 | if (!err) { |
| 3701 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3702 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3703 | } |
| 3704 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3705 | mgmt_pending_remove(cmd); |
| 3706 | |
| 3707 | /* If no HCI commands were sent we're done */ |
| 3708 | if (err == -ENODATA) { |
| 3709 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 3710 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 3711 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3712 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3713 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3714 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3715 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3716 | return err; |
| 3717 | } |
| 3718 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3719 | 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] | 3720 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3721 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3722 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3723 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3724 | int err; |
| 3725 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3726 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3727 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3728 | hci_dev_lock(hdev); |
| 3729 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3730 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3731 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3732 | MGMT_STATUS_FAILED, &cp->addr, |
| 3733 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3734 | goto failed; |
| 3735 | } |
| 3736 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3737 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3738 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3739 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3740 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3741 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3742 | goto failed; |
| 3743 | } |
| 3744 | |
| 3745 | if (cp->name_known) { |
| 3746 | e->name_state = NAME_KNOWN; |
| 3747 | list_del(&e->list); |
| 3748 | } else { |
| 3749 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3750 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3751 | } |
| 3752 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3753 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3754 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3755 | |
| 3756 | failed: |
| 3757 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3758 | return err; |
| 3759 | } |
| 3760 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3761 | 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] | 3762 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3763 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3764 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3765 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3766 | int err; |
| 3767 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3768 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3769 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3770 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3771 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3772 | MGMT_STATUS_INVALID_PARAMS, |
| 3773 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3774 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3775 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3776 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3777 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3778 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3779 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3780 | goto done; |
| 3781 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3782 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3783 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3784 | sk); |
| 3785 | status = MGMT_STATUS_SUCCESS; |
| 3786 | |
| 3787 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3788 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3789 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3790 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3791 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3792 | |
| 3793 | return err; |
| 3794 | } |
| 3795 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3796 | 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] | 3797 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3798 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3799 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3800 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3801 | int err; |
| 3802 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3803 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3804 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3805 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3806 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3807 | MGMT_STATUS_INVALID_PARAMS, |
| 3808 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3809 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3810 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3811 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3812 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3813 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3814 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3815 | goto done; |
| 3816 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3817 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3818 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3819 | sk); |
| 3820 | status = MGMT_STATUS_SUCCESS; |
| 3821 | |
| 3822 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3823 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3824 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3825 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3826 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3827 | |
| 3828 | return err; |
| 3829 | } |
| 3830 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3831 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3832 | u16 len) |
| 3833 | { |
| 3834 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3835 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3836 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3837 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3838 | |
| 3839 | BT_DBG("%s", hdev->name); |
| 3840 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3841 | source = __le16_to_cpu(cp->source); |
| 3842 | |
| 3843 | if (source > 0x0002) |
| 3844 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3845 | MGMT_STATUS_INVALID_PARAMS); |
| 3846 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3847 | hci_dev_lock(hdev); |
| 3848 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3849 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3850 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 3851 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 3852 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 3853 | |
| 3854 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 3855 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3856 | hci_req_init(&req, hdev); |
| 3857 | update_eir(&req); |
| 3858 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3859 | |
| 3860 | hci_dev_unlock(hdev); |
| 3861 | |
| 3862 | return err; |
| 3863 | } |
| 3864 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3865 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 3866 | { |
| 3867 | struct cmd_lookup match = { NULL, hdev }; |
| 3868 | |
| 3869 | if (status) { |
| 3870 | u8 mgmt_err = mgmt_status(status); |
| 3871 | |
| 3872 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 3873 | cmd_status_rsp, &mgmt_err); |
| 3874 | return; |
| 3875 | } |
| 3876 | |
| 3877 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 3878 | &match); |
| 3879 | |
| 3880 | new_settings(hdev, match.sk); |
| 3881 | |
| 3882 | if (match.sk) |
| 3883 | sock_put(match.sk); |
| 3884 | } |
| 3885 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 3886 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3887 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3888 | { |
| 3889 | struct mgmt_mode *cp = data; |
| 3890 | struct pending_cmd *cmd; |
| 3891 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3892 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3893 | int err; |
| 3894 | |
| 3895 | BT_DBG("request for %s", hdev->name); |
| 3896 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3897 | status = mgmt_le_support(hdev); |
| 3898 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3899 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3900 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3901 | |
| 3902 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3903 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3904 | MGMT_STATUS_INVALID_PARAMS); |
| 3905 | |
| 3906 | hci_dev_lock(hdev); |
| 3907 | |
| 3908 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3909 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3910 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 3911 | /* The following conditions are ones which mean that we should |
| 3912 | * not do any HCI communication but directly send a mgmt |
| 3913 | * response to user space (after toggling the flag if |
| 3914 | * necessary). |
| 3915 | */ |
| 3916 | if (!hdev_is_powered(hdev) || val == enabled || |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 3917 | hci_conn_num(hdev, LE_LINK) > 0) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3918 | bool changed = false; |
| 3919 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3920 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 3921 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3922 | changed = true; |
| 3923 | } |
| 3924 | |
| 3925 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 3926 | if (err < 0) |
| 3927 | goto unlock; |
| 3928 | |
| 3929 | if (changed) |
| 3930 | err = new_settings(hdev, sk); |
| 3931 | |
| 3932 | goto unlock; |
| 3933 | } |
| 3934 | |
| 3935 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 3936 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 3937 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3938 | MGMT_STATUS_BUSY); |
| 3939 | goto unlock; |
| 3940 | } |
| 3941 | |
| 3942 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 3943 | if (!cmd) { |
| 3944 | err = -ENOMEM; |
| 3945 | goto unlock; |
| 3946 | } |
| 3947 | |
| 3948 | hci_req_init(&req, hdev); |
| 3949 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 3950 | if (val) |
| 3951 | enable_advertising(&req); |
| 3952 | else |
| 3953 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3954 | |
| 3955 | err = hci_req_run(&req, set_advertising_complete); |
| 3956 | if (err < 0) |
| 3957 | mgmt_pending_remove(cmd); |
| 3958 | |
| 3959 | unlock: |
| 3960 | hci_dev_unlock(hdev); |
| 3961 | return err; |
| 3962 | } |
| 3963 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3964 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 3965 | void *data, u16 len) |
| 3966 | { |
| 3967 | struct mgmt_cp_set_static_address *cp = data; |
| 3968 | int err; |
| 3969 | |
| 3970 | BT_DBG("%s", hdev->name); |
| 3971 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3972 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3973 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3974 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3975 | |
| 3976 | if (hdev_is_powered(hdev)) |
| 3977 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 3978 | MGMT_STATUS_REJECTED); |
| 3979 | |
| 3980 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 3981 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 3982 | return cmd_status(sk, hdev->id, |
| 3983 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3984 | MGMT_STATUS_INVALID_PARAMS); |
| 3985 | |
| 3986 | /* Two most significant bits shall be set */ |
| 3987 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 3988 | return cmd_status(sk, hdev->id, |
| 3989 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3990 | MGMT_STATUS_INVALID_PARAMS); |
| 3991 | } |
| 3992 | |
| 3993 | hci_dev_lock(hdev); |
| 3994 | |
| 3995 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 3996 | |
| 3997 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 3998 | |
| 3999 | hci_dev_unlock(hdev); |
| 4000 | |
| 4001 | return err; |
| 4002 | } |
| 4003 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4004 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4005 | void *data, u16 len) |
| 4006 | { |
| 4007 | struct mgmt_cp_set_scan_params *cp = data; |
| 4008 | __u16 interval, window; |
| 4009 | int err; |
| 4010 | |
| 4011 | BT_DBG("%s", hdev->name); |
| 4012 | |
| 4013 | if (!lmp_le_capable(hdev)) |
| 4014 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4015 | MGMT_STATUS_NOT_SUPPORTED); |
| 4016 | |
| 4017 | interval = __le16_to_cpu(cp->interval); |
| 4018 | |
| 4019 | if (interval < 0x0004 || interval > 0x4000) |
| 4020 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4021 | MGMT_STATUS_INVALID_PARAMS); |
| 4022 | |
| 4023 | window = __le16_to_cpu(cp->window); |
| 4024 | |
| 4025 | if (window < 0x0004 || window > 0x4000) |
| 4026 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4027 | MGMT_STATUS_INVALID_PARAMS); |
| 4028 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4029 | if (window > interval) |
| 4030 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4031 | MGMT_STATUS_INVALID_PARAMS); |
| 4032 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4033 | hci_dev_lock(hdev); |
| 4034 | |
| 4035 | hdev->le_scan_interval = interval; |
| 4036 | hdev->le_scan_window = window; |
| 4037 | |
| 4038 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 4039 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4040 | /* If background scan is running, restart it so new parameters are |
| 4041 | * loaded. |
| 4042 | */ |
| 4043 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4044 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4045 | struct hci_request req; |
| 4046 | |
| 4047 | hci_req_init(&req, hdev); |
| 4048 | |
| 4049 | hci_req_add_le_scan_disable(&req); |
| 4050 | hci_req_add_le_passive_scan(&req); |
| 4051 | |
| 4052 | hci_req_run(&req, NULL); |
| 4053 | } |
| 4054 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4055 | hci_dev_unlock(hdev); |
| 4056 | |
| 4057 | return err; |
| 4058 | } |
| 4059 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4060 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 4061 | { |
| 4062 | struct pending_cmd *cmd; |
| 4063 | |
| 4064 | BT_DBG("status 0x%02x", status); |
| 4065 | |
| 4066 | hci_dev_lock(hdev); |
| 4067 | |
| 4068 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4069 | if (!cmd) |
| 4070 | goto unlock; |
| 4071 | |
| 4072 | if (status) { |
| 4073 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4074 | mgmt_status(status)); |
| 4075 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4076 | struct mgmt_mode *cp = cmd->param; |
| 4077 | |
| 4078 | if (cp->val) |
| 4079 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4080 | else |
| 4081 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4082 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4083 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4084 | new_settings(hdev, cmd->sk); |
| 4085 | } |
| 4086 | |
| 4087 | mgmt_pending_remove(cmd); |
| 4088 | |
| 4089 | unlock: |
| 4090 | hci_dev_unlock(hdev); |
| 4091 | } |
| 4092 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4093 | 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] | 4094 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4095 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4096 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4097 | struct pending_cmd *cmd; |
| 4098 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4099 | int err; |
| 4100 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4101 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4102 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4103 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4104 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4105 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4106 | MGMT_STATUS_NOT_SUPPORTED); |
| 4107 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4108 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4109 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4110 | MGMT_STATUS_INVALID_PARAMS); |
| 4111 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4112 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4113 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4114 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4115 | |
| 4116 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4117 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4118 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4119 | |
| 4120 | hci_dev_lock(hdev); |
| 4121 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4122 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4123 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4124 | MGMT_STATUS_BUSY); |
| 4125 | goto unlock; |
| 4126 | } |
| 4127 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4128 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4129 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4130 | hdev); |
| 4131 | goto unlock; |
| 4132 | } |
| 4133 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4134 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4135 | data, len); |
| 4136 | if (!cmd) { |
| 4137 | err = -ENOMEM; |
| 4138 | goto unlock; |
| 4139 | } |
| 4140 | |
| 4141 | hci_req_init(&req, hdev); |
| 4142 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4143 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4144 | |
| 4145 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4146 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4147 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4148 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4149 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4150 | } |
| 4151 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4152 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4153 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4154 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4155 | return err; |
| 4156 | } |
| 4157 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 4158 | static void set_bredr_scan(struct hci_request *req) |
| 4159 | { |
| 4160 | struct hci_dev *hdev = req->hdev; |
| 4161 | u8 scan = 0; |
| 4162 | |
| 4163 | /* Ensure that fast connectable is disabled. This function will |
| 4164 | * not do anything if the page scan parameters are already what |
| 4165 | * they should be. |
| 4166 | */ |
| 4167 | write_fast_connectable(req, false); |
| 4168 | |
| 4169 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4170 | scan |= SCAN_PAGE; |
| 4171 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 4172 | scan |= SCAN_INQUIRY; |
| 4173 | |
| 4174 | if (scan) |
| 4175 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 4176 | } |
| 4177 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4178 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4179 | { |
| 4180 | struct pending_cmd *cmd; |
| 4181 | |
| 4182 | BT_DBG("status 0x%02x", status); |
| 4183 | |
| 4184 | hci_dev_lock(hdev); |
| 4185 | |
| 4186 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4187 | if (!cmd) |
| 4188 | goto unlock; |
| 4189 | |
| 4190 | if (status) { |
| 4191 | u8 mgmt_err = mgmt_status(status); |
| 4192 | |
| 4193 | /* We need to restore the flag if related HCI commands |
| 4194 | * failed. |
| 4195 | */ |
| 4196 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4197 | |
| 4198 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4199 | } else { |
| 4200 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4201 | new_settings(hdev, cmd->sk); |
| 4202 | } |
| 4203 | |
| 4204 | mgmt_pending_remove(cmd); |
| 4205 | |
| 4206 | unlock: |
| 4207 | hci_dev_unlock(hdev); |
| 4208 | } |
| 4209 | |
| 4210 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4211 | { |
| 4212 | struct mgmt_mode *cp = data; |
| 4213 | struct pending_cmd *cmd; |
| 4214 | struct hci_request req; |
| 4215 | int err; |
| 4216 | |
| 4217 | BT_DBG("request for %s", hdev->name); |
| 4218 | |
| 4219 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4220 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4221 | MGMT_STATUS_NOT_SUPPORTED); |
| 4222 | |
| 4223 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4224 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4225 | MGMT_STATUS_REJECTED); |
| 4226 | |
| 4227 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4228 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4229 | MGMT_STATUS_INVALID_PARAMS); |
| 4230 | |
| 4231 | hci_dev_lock(hdev); |
| 4232 | |
| 4233 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4234 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4235 | goto unlock; |
| 4236 | } |
| 4237 | |
| 4238 | if (!hdev_is_powered(hdev)) { |
| 4239 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4240 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4241 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4242 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4243 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4244 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4245 | } |
| 4246 | |
| 4247 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4248 | |
| 4249 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4250 | if (err < 0) |
| 4251 | goto unlock; |
| 4252 | |
| 4253 | err = new_settings(hdev, sk); |
| 4254 | goto unlock; |
| 4255 | } |
| 4256 | |
| 4257 | /* Reject disabling when powered on */ |
| 4258 | if (!cp->val) { |
| 4259 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4260 | MGMT_STATUS_REJECTED); |
| 4261 | goto unlock; |
| 4262 | } |
| 4263 | |
| 4264 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4265 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4266 | MGMT_STATUS_BUSY); |
| 4267 | goto unlock; |
| 4268 | } |
| 4269 | |
| 4270 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4271 | if (!cmd) { |
| 4272 | err = -ENOMEM; |
| 4273 | goto unlock; |
| 4274 | } |
| 4275 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4276 | /* 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] | 4277 | * generates the correct flags. |
| 4278 | */ |
| 4279 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4280 | |
| 4281 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4282 | |
| 4283 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4284 | set_bredr_scan(&req); |
| 4285 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4286 | /* Since only the advertising data flags will change, there |
| 4287 | * is no need to update the scan response data. |
| 4288 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4289 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4290 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4291 | err = hci_req_run(&req, set_bredr_complete); |
| 4292 | if (err < 0) |
| 4293 | mgmt_pending_remove(cmd); |
| 4294 | |
| 4295 | unlock: |
| 4296 | hci_dev_unlock(hdev); |
| 4297 | return err; |
| 4298 | } |
| 4299 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4300 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4301 | void *data, u16 len) |
| 4302 | { |
| 4303 | struct mgmt_mode *cp = data; |
| 4304 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4305 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4306 | int err; |
| 4307 | |
| 4308 | BT_DBG("request for %s", hdev->name); |
| 4309 | |
| 4310 | status = mgmt_bredr_support(hdev); |
| 4311 | if (status) |
| 4312 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4313 | status); |
| 4314 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4315 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 4316 | !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4317 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4318 | MGMT_STATUS_NOT_SUPPORTED); |
| 4319 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4320 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4321 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4322 | MGMT_STATUS_INVALID_PARAMS); |
| 4323 | |
| 4324 | hci_dev_lock(hdev); |
| 4325 | |
| 4326 | if (!hdev_is_powered(hdev)) { |
| 4327 | bool changed; |
| 4328 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4329 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4330 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4331 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4332 | if (cp->val == 0x02) |
| 4333 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4334 | else |
| 4335 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4336 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4337 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4338 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4339 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4340 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4341 | |
| 4342 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4343 | if (err < 0) |
| 4344 | goto failed; |
| 4345 | |
| 4346 | if (changed) |
| 4347 | err = new_settings(hdev, sk); |
| 4348 | |
| 4349 | goto failed; |
| 4350 | } |
| 4351 | |
| 4352 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4353 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4354 | MGMT_STATUS_BUSY); |
| 4355 | goto failed; |
| 4356 | } |
| 4357 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4358 | val = !!cp->val; |
| 4359 | |
| 4360 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4361 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4362 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4363 | goto failed; |
| 4364 | } |
| 4365 | |
| 4366 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4367 | if (!cmd) { |
| 4368 | err = -ENOMEM; |
| 4369 | goto failed; |
| 4370 | } |
| 4371 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4372 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4373 | if (err < 0) { |
| 4374 | mgmt_pending_remove(cmd); |
| 4375 | goto failed; |
| 4376 | } |
| 4377 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4378 | if (cp->val == 0x02) |
| 4379 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4380 | else |
| 4381 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4382 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4383 | failed: |
| 4384 | hci_dev_unlock(hdev); |
| 4385 | return err; |
| 4386 | } |
| 4387 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4388 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4389 | void *data, u16 len) |
| 4390 | { |
| 4391 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4392 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4393 | int err; |
| 4394 | |
| 4395 | BT_DBG("request for %s", hdev->name); |
| 4396 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4397 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4398 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4399 | MGMT_STATUS_INVALID_PARAMS); |
| 4400 | |
| 4401 | hci_dev_lock(hdev); |
| 4402 | |
| 4403 | if (cp->val) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4404 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 4405 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4406 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4407 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 4408 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4409 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4410 | if (cp->val == 0x02) |
| 4411 | use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, |
| 4412 | &hdev->dev_flags); |
| 4413 | else |
| 4414 | use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, |
| 4415 | &hdev->dev_flags); |
| 4416 | |
| 4417 | if (hdev_is_powered(hdev) && use_changed && |
| 4418 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 4419 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4420 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4421 | sizeof(mode), &mode); |
| 4422 | } |
| 4423 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4424 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4425 | if (err < 0) |
| 4426 | goto unlock; |
| 4427 | |
| 4428 | if (changed) |
| 4429 | err = new_settings(hdev, sk); |
| 4430 | |
| 4431 | unlock: |
| 4432 | hci_dev_unlock(hdev); |
| 4433 | return err; |
| 4434 | } |
| 4435 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4436 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4437 | u16 len) |
| 4438 | { |
| 4439 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4440 | bool changed; |
| 4441 | int err; |
| 4442 | |
| 4443 | BT_DBG("request for %s", hdev->name); |
| 4444 | |
| 4445 | if (!lmp_le_capable(hdev)) |
| 4446 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4447 | MGMT_STATUS_NOT_SUPPORTED); |
| 4448 | |
| 4449 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4450 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4451 | MGMT_STATUS_INVALID_PARAMS); |
| 4452 | |
| 4453 | if (hdev_is_powered(hdev)) |
| 4454 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4455 | MGMT_STATUS_REJECTED); |
| 4456 | |
| 4457 | hci_dev_lock(hdev); |
| 4458 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4459 | /* If user space supports this command it is also expected to |
| 4460 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4461 | */ |
| 4462 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4463 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4464 | if (cp->privacy) { |
| 4465 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4466 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4467 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4468 | } else { |
| 4469 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4470 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4471 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4472 | } |
| 4473 | |
| 4474 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4475 | if (err < 0) |
| 4476 | goto unlock; |
| 4477 | |
| 4478 | if (changed) |
| 4479 | err = new_settings(hdev, sk); |
| 4480 | |
| 4481 | unlock: |
| 4482 | hci_dev_unlock(hdev); |
| 4483 | return err; |
| 4484 | } |
| 4485 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4486 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4487 | { |
| 4488 | switch (irk->addr.type) { |
| 4489 | case BDADDR_LE_PUBLIC: |
| 4490 | return true; |
| 4491 | |
| 4492 | case BDADDR_LE_RANDOM: |
| 4493 | /* Two most significant bits shall be set */ |
| 4494 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4495 | return false; |
| 4496 | return true; |
| 4497 | } |
| 4498 | |
| 4499 | return false; |
| 4500 | } |
| 4501 | |
| 4502 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4503 | u16 len) |
| 4504 | { |
| 4505 | struct mgmt_cp_load_irks *cp = cp_data; |
| 4506 | u16 irk_count, expected_len; |
| 4507 | int i, err; |
| 4508 | |
| 4509 | BT_DBG("request for %s", hdev->name); |
| 4510 | |
| 4511 | if (!lmp_le_capable(hdev)) |
| 4512 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4513 | MGMT_STATUS_NOT_SUPPORTED); |
| 4514 | |
| 4515 | irk_count = __le16_to_cpu(cp->irk_count); |
| 4516 | |
| 4517 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4518 | if (expected_len != len) { |
| 4519 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4520 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4521 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4522 | MGMT_STATUS_INVALID_PARAMS); |
| 4523 | } |
| 4524 | |
| 4525 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4526 | |
| 4527 | for (i = 0; i < irk_count; i++) { |
| 4528 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4529 | |
| 4530 | if (!irk_is_valid(key)) |
| 4531 | return cmd_status(sk, hdev->id, |
| 4532 | MGMT_OP_LOAD_IRKS, |
| 4533 | MGMT_STATUS_INVALID_PARAMS); |
| 4534 | } |
| 4535 | |
| 4536 | hci_dev_lock(hdev); |
| 4537 | |
| 4538 | hci_smp_irks_clear(hdev); |
| 4539 | |
| 4540 | for (i = 0; i < irk_count; i++) { |
| 4541 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4542 | u8 addr_type; |
| 4543 | |
| 4544 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4545 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4546 | else |
| 4547 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4548 | |
| 4549 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4550 | BDADDR_ANY); |
| 4551 | } |
| 4552 | |
| 4553 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4554 | |
| 4555 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4556 | |
| 4557 | hci_dev_unlock(hdev); |
| 4558 | |
| 4559 | return err; |
| 4560 | } |
| 4561 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4562 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4563 | { |
| 4564 | if (key->master != 0x00 && key->master != 0x01) |
| 4565 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4566 | |
| 4567 | switch (key->addr.type) { |
| 4568 | case BDADDR_LE_PUBLIC: |
| 4569 | return true; |
| 4570 | |
| 4571 | case BDADDR_LE_RANDOM: |
| 4572 | /* Two most significant bits shall be set */ |
| 4573 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4574 | return false; |
| 4575 | return true; |
| 4576 | } |
| 4577 | |
| 4578 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4579 | } |
| 4580 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4581 | 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] | 4582 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4583 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4584 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
| 4585 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4586 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4587 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4588 | BT_DBG("request for %s", hdev->name); |
| 4589 | |
| 4590 | if (!lmp_le_capable(hdev)) |
| 4591 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4592 | MGMT_STATUS_NOT_SUPPORTED); |
| 4593 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4594 | key_count = __le16_to_cpu(cp->key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4595 | |
| 4596 | expected_len = sizeof(*cp) + key_count * |
| 4597 | sizeof(struct mgmt_ltk_info); |
| 4598 | if (expected_len != len) { |
| 4599 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4600 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4601 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4602 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4603 | } |
| 4604 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4605 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4606 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4607 | for (i = 0; i < key_count; i++) { |
| 4608 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4609 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4610 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4611 | return cmd_status(sk, hdev->id, |
| 4612 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4613 | MGMT_STATUS_INVALID_PARAMS); |
| 4614 | } |
| 4615 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4616 | hci_dev_lock(hdev); |
| 4617 | |
| 4618 | hci_smp_ltks_clear(hdev); |
| 4619 | |
| 4620 | for (i = 0; i < key_count; i++) { |
| 4621 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4622 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4623 | |
| 4624 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4625 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4626 | else |
| 4627 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4628 | |
| 4629 | if (key->master) |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4630 | type = SMP_LTK; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4631 | else |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4632 | type = SMP_LTK_SLAVE; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4633 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4634 | switch (key->type) { |
| 4635 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4636 | authenticated = 0x00; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4637 | break; |
| 4638 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4639 | authenticated = 0x01; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4640 | break; |
| 4641 | default: |
| 4642 | continue; |
| 4643 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4644 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4645 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4646 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4647 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4648 | } |
| 4649 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4650 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4651 | NULL, 0); |
| 4652 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4653 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4654 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4655 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4656 | } |
| 4657 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4658 | struct cmd_conn_lookup { |
| 4659 | struct hci_conn *conn; |
| 4660 | bool valid_tx_power; |
| 4661 | u8 mgmt_status; |
| 4662 | }; |
| 4663 | |
| 4664 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 4665 | { |
| 4666 | struct cmd_conn_lookup *match = data; |
| 4667 | struct mgmt_cp_get_conn_info *cp; |
| 4668 | struct mgmt_rp_get_conn_info rp; |
| 4669 | struct hci_conn *conn = cmd->user_data; |
| 4670 | |
| 4671 | if (conn != match->conn) |
| 4672 | return; |
| 4673 | |
| 4674 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 4675 | |
| 4676 | memset(&rp, 0, sizeof(rp)); |
| 4677 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4678 | rp.addr.type = cp->addr.type; |
| 4679 | |
| 4680 | if (!match->mgmt_status) { |
| 4681 | rp.rssi = conn->rssi; |
| 4682 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4683 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4684 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4685 | rp.max_tx_power = conn->max_tx_power; |
| 4686 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4687 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4688 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 4689 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4690 | } |
| 4691 | |
| 4692 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4693 | match->mgmt_status, &rp, sizeof(rp)); |
| 4694 | |
| 4695 | hci_conn_drop(conn); |
| 4696 | |
| 4697 | mgmt_pending_remove(cmd); |
| 4698 | } |
| 4699 | |
| 4700 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 4701 | { |
| 4702 | struct hci_cp_read_rssi *cp; |
| 4703 | struct hci_conn *conn; |
| 4704 | struct cmd_conn_lookup match; |
| 4705 | u16 handle; |
| 4706 | |
| 4707 | BT_DBG("status 0x%02x", status); |
| 4708 | |
| 4709 | hci_dev_lock(hdev); |
| 4710 | |
| 4711 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4712 | * otherwise we assume it's not valid. At the moment we assume that |
| 4713 | * either both or none of current and max values are valid to keep code |
| 4714 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4715 | */ |
| 4716 | match.valid_tx_power = !status; |
| 4717 | |
| 4718 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4719 | * Level so we check which one was last sent to retrieve connection |
| 4720 | * handle. Both commands have handle as first parameter so it's safe to |
| 4721 | * cast data on the same command struct. |
| 4722 | * |
| 4723 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4724 | * In other case we simply override error to indicate success as we |
| 4725 | * already remembered if TX power value is actually valid. |
| 4726 | */ |
| 4727 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4728 | if (!cp) { |
| 4729 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 4730 | status = 0; |
| 4731 | } |
| 4732 | |
| 4733 | if (!cp) { |
| 4734 | BT_ERR("invalid sent_cmd in response"); |
| 4735 | goto unlock; |
| 4736 | } |
| 4737 | |
| 4738 | handle = __le16_to_cpu(cp->handle); |
| 4739 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4740 | if (!conn) { |
| 4741 | BT_ERR("unknown handle (%d) in response", handle); |
| 4742 | goto unlock; |
| 4743 | } |
| 4744 | |
| 4745 | match.conn = conn; |
| 4746 | match.mgmt_status = mgmt_status(status); |
| 4747 | |
| 4748 | /* Cache refresh is complete, now reply for mgmt request for given |
| 4749 | * connection only. |
| 4750 | */ |
| 4751 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 4752 | get_conn_info_complete, &match); |
| 4753 | |
| 4754 | unlock: |
| 4755 | hci_dev_unlock(hdev); |
| 4756 | } |
| 4757 | |
| 4758 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4759 | u16 len) |
| 4760 | { |
| 4761 | struct mgmt_cp_get_conn_info *cp = data; |
| 4762 | struct mgmt_rp_get_conn_info rp; |
| 4763 | struct hci_conn *conn; |
| 4764 | unsigned long conn_info_age; |
| 4765 | int err = 0; |
| 4766 | |
| 4767 | BT_DBG("%s", hdev->name); |
| 4768 | |
| 4769 | memset(&rp, 0, sizeof(rp)); |
| 4770 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4771 | rp.addr.type = cp->addr.type; |
| 4772 | |
| 4773 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 4774 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4775 | MGMT_STATUS_INVALID_PARAMS, |
| 4776 | &rp, sizeof(rp)); |
| 4777 | |
| 4778 | hci_dev_lock(hdev); |
| 4779 | |
| 4780 | if (!hdev_is_powered(hdev)) { |
| 4781 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4782 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4783 | goto unlock; |
| 4784 | } |
| 4785 | |
| 4786 | if (cp->addr.type == BDADDR_BREDR) |
| 4787 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4788 | &cp->addr.bdaddr); |
| 4789 | else |
| 4790 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 4791 | |
| 4792 | if (!conn || conn->state != BT_CONNECTED) { |
| 4793 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4794 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 4795 | goto unlock; |
| 4796 | } |
| 4797 | |
| 4798 | /* To avoid client trying to guess when to poll again for information we |
| 4799 | * calculate conn info age as random value between min/max set in hdev. |
| 4800 | */ |
| 4801 | conn_info_age = hdev->conn_info_min_age + |
| 4802 | prandom_u32_max(hdev->conn_info_max_age - |
| 4803 | hdev->conn_info_min_age); |
| 4804 | |
| 4805 | /* Query controller to refresh cached values if they are too old or were |
| 4806 | * never read. |
| 4807 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 4808 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 4809 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4810 | !conn->conn_info_timestamp) { |
| 4811 | struct hci_request req; |
| 4812 | struct hci_cp_read_tx_power req_txp_cp; |
| 4813 | struct hci_cp_read_rssi req_rssi_cp; |
| 4814 | struct pending_cmd *cmd; |
| 4815 | |
| 4816 | hci_req_init(&req, hdev); |
| 4817 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 4818 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 4819 | &req_rssi_cp); |
| 4820 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 4821 | /* For LE links TX power does not change thus we don't need to |
| 4822 | * query for it once value is known. |
| 4823 | */ |
| 4824 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 4825 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 4826 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4827 | req_txp_cp.type = 0x00; |
| 4828 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4829 | sizeof(req_txp_cp), &req_txp_cp); |
| 4830 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4831 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4832 | /* Max TX power needs to be read only once per connection */ |
| 4833 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 4834 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4835 | req_txp_cp.type = 0x01; |
| 4836 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4837 | sizeof(req_txp_cp), &req_txp_cp); |
| 4838 | } |
| 4839 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4840 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 4841 | if (err < 0) |
| 4842 | goto unlock; |
| 4843 | |
| 4844 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 4845 | data, len); |
| 4846 | if (!cmd) { |
| 4847 | err = -ENOMEM; |
| 4848 | goto unlock; |
| 4849 | } |
| 4850 | |
| 4851 | hci_conn_hold(conn); |
| 4852 | cmd->user_data = conn; |
| 4853 | |
| 4854 | conn->conn_info_timestamp = jiffies; |
| 4855 | } else { |
| 4856 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 4857 | rp.rssi = conn->rssi; |
| 4858 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4859 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4860 | |
| 4861 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4862 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 4863 | } |
| 4864 | |
| 4865 | unlock: |
| 4866 | hci_dev_unlock(hdev); |
| 4867 | return err; |
| 4868 | } |
| 4869 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 4870 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status) |
| 4871 | { |
| 4872 | struct mgmt_cp_get_clock_info *cp; |
| 4873 | struct mgmt_rp_get_clock_info rp; |
| 4874 | struct hci_cp_read_clock *hci_cp; |
| 4875 | struct pending_cmd *cmd; |
| 4876 | struct hci_conn *conn; |
| 4877 | |
| 4878 | BT_DBG("%s status %u", hdev->name, status); |
| 4879 | |
| 4880 | hci_dev_lock(hdev); |
| 4881 | |
| 4882 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 4883 | if (!hci_cp) |
| 4884 | goto unlock; |
| 4885 | |
| 4886 | if (hci_cp->which) { |
| 4887 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 4888 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4889 | } else { |
| 4890 | conn = NULL; |
| 4891 | } |
| 4892 | |
| 4893 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 4894 | if (!cmd) |
| 4895 | goto unlock; |
| 4896 | |
| 4897 | cp = cmd->param; |
| 4898 | |
| 4899 | memset(&rp, 0, sizeof(rp)); |
| 4900 | memcpy(&rp.addr, &cp->addr, sizeof(rp.addr)); |
| 4901 | |
| 4902 | if (status) |
| 4903 | goto send_rsp; |
| 4904 | |
| 4905 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 4906 | |
| 4907 | if (conn) { |
| 4908 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 4909 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 4910 | } |
| 4911 | |
| 4912 | send_rsp: |
| 4913 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 4914 | &rp, sizeof(rp)); |
| 4915 | mgmt_pending_remove(cmd); |
| 4916 | if (conn) |
| 4917 | hci_conn_drop(conn); |
| 4918 | |
| 4919 | unlock: |
| 4920 | hci_dev_unlock(hdev); |
| 4921 | } |
| 4922 | |
| 4923 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4924 | u16 len) |
| 4925 | { |
| 4926 | struct mgmt_cp_get_clock_info *cp = data; |
| 4927 | struct mgmt_rp_get_clock_info rp; |
| 4928 | struct hci_cp_read_clock hci_cp; |
| 4929 | struct pending_cmd *cmd; |
| 4930 | struct hci_request req; |
| 4931 | struct hci_conn *conn; |
| 4932 | int err; |
| 4933 | |
| 4934 | BT_DBG("%s", hdev->name); |
| 4935 | |
| 4936 | memset(&rp, 0, sizeof(rp)); |
| 4937 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4938 | rp.addr.type = cp->addr.type; |
| 4939 | |
| 4940 | if (cp->addr.type != BDADDR_BREDR) |
| 4941 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 4942 | MGMT_STATUS_INVALID_PARAMS, |
| 4943 | &rp, sizeof(rp)); |
| 4944 | |
| 4945 | hci_dev_lock(hdev); |
| 4946 | |
| 4947 | if (!hdev_is_powered(hdev)) { |
| 4948 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 4949 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4950 | goto unlock; |
| 4951 | } |
| 4952 | |
| 4953 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 4954 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4955 | &cp->addr.bdaddr); |
| 4956 | if (!conn || conn->state != BT_CONNECTED) { |
| 4957 | err = cmd_complete(sk, hdev->id, |
| 4958 | MGMT_OP_GET_CLOCK_INFO, |
| 4959 | MGMT_STATUS_NOT_CONNECTED, |
| 4960 | &rp, sizeof(rp)); |
| 4961 | goto unlock; |
| 4962 | } |
| 4963 | } else { |
| 4964 | conn = NULL; |
| 4965 | } |
| 4966 | |
| 4967 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 4968 | if (!cmd) { |
| 4969 | err = -ENOMEM; |
| 4970 | goto unlock; |
| 4971 | } |
| 4972 | |
| 4973 | hci_req_init(&req, hdev); |
| 4974 | |
| 4975 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 4976 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 4977 | |
| 4978 | if (conn) { |
| 4979 | hci_conn_hold(conn); |
| 4980 | cmd->user_data = conn; |
| 4981 | |
| 4982 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 4983 | hci_cp.which = 0x01; /* Piconet clock */ |
| 4984 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 4985 | } |
| 4986 | |
| 4987 | err = hci_req_run(&req, get_clock_info_complete); |
| 4988 | if (err < 0) |
| 4989 | mgmt_pending_remove(cmd); |
| 4990 | |
| 4991 | unlock: |
| 4992 | hci_dev_unlock(hdev); |
| 4993 | return err; |
| 4994 | } |
| 4995 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 4996 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 4997 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 4998 | { |
| 4999 | struct mgmt_ev_device_added ev; |
| 5000 | |
| 5001 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5002 | ev.addr.type = type; |
| 5003 | ev.action = action; |
| 5004 | |
| 5005 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5006 | } |
| 5007 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5008 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5009 | void *data, u16 len) |
| 5010 | { |
| 5011 | struct mgmt_cp_add_device *cp = data; |
| 5012 | u8 auto_conn, addr_type; |
| 5013 | int err; |
| 5014 | |
| 5015 | BT_DBG("%s", hdev->name); |
| 5016 | |
| 5017 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5018 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
| 5019 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5020 | MGMT_STATUS_INVALID_PARAMS, |
| 5021 | &cp->addr, sizeof(cp->addr)); |
| 5022 | |
| 5023 | if (cp->action != 0x00 && cp->action != 0x01) |
| 5024 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5025 | MGMT_STATUS_INVALID_PARAMS, |
| 5026 | &cp->addr, sizeof(cp->addr)); |
| 5027 | |
| 5028 | hci_dev_lock(hdev); |
| 5029 | |
| 5030 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5031 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5032 | else |
| 5033 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5034 | |
| 5035 | if (cp->action) |
| 5036 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
| 5037 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5038 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5039 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5040 | /* If the connection parameters don't exist for this device, |
| 5041 | * they will be created and configured with defaults. |
| 5042 | */ |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5043 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
| 5044 | auto_conn) < 0) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5045 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5046 | MGMT_STATUS_FAILED, |
| 5047 | &cp->addr, sizeof(cp->addr)); |
| 5048 | goto unlock; |
| 5049 | } |
| 5050 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5051 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5052 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5053 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5054 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5055 | |
| 5056 | unlock: |
| 5057 | hci_dev_unlock(hdev); |
| 5058 | return err; |
| 5059 | } |
| 5060 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5061 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5062 | bdaddr_t *bdaddr, u8 type) |
| 5063 | { |
| 5064 | struct mgmt_ev_device_removed ev; |
| 5065 | |
| 5066 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5067 | ev.addr.type = type; |
| 5068 | |
| 5069 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5070 | } |
| 5071 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5072 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5073 | void *data, u16 len) |
| 5074 | { |
| 5075 | struct mgmt_cp_remove_device *cp = data; |
| 5076 | int err; |
| 5077 | |
| 5078 | BT_DBG("%s", hdev->name); |
| 5079 | |
| 5080 | hci_dev_lock(hdev); |
| 5081 | |
| 5082 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5083 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5084 | u8 addr_type; |
| 5085 | |
| 5086 | if (!bdaddr_type_is_le(cp->addr.type)) { |
| 5087 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5088 | MGMT_STATUS_INVALID_PARAMS, |
| 5089 | &cp->addr, sizeof(cp->addr)); |
| 5090 | goto unlock; |
| 5091 | } |
| 5092 | |
| 5093 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5094 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5095 | else |
| 5096 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5097 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5098 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5099 | addr_type); |
| 5100 | if (!params) { |
| 5101 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5102 | MGMT_STATUS_INVALID_PARAMS, |
| 5103 | &cp->addr, sizeof(cp->addr)); |
| 5104 | goto unlock; |
| 5105 | } |
| 5106 | |
| 5107 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED) { |
| 5108 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5109 | MGMT_STATUS_INVALID_PARAMS, |
| 5110 | &cp->addr, sizeof(cp->addr)); |
| 5111 | goto unlock; |
| 5112 | } |
| 5113 | |
| 5114 | hci_pend_le_conn_del(hdev, &cp->addr.bdaddr, addr_type); |
| 5115 | list_del(¶ms->list); |
| 5116 | kfree(params); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5117 | |
| 5118 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5119 | } else { |
| 5120 | if (cp->addr.type) { |
| 5121 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5122 | MGMT_STATUS_INVALID_PARAMS, |
| 5123 | &cp->addr, sizeof(cp->addr)); |
| 5124 | goto unlock; |
| 5125 | } |
| 5126 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5127 | hci_conn_params_clear_enabled(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5128 | } |
| 5129 | |
| 5130 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5131 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5132 | |
| 5133 | unlock: |
| 5134 | hci_dev_unlock(hdev); |
| 5135 | return err; |
| 5136 | } |
| 5137 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame^] | 5138 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5139 | u16 len) |
| 5140 | { |
| 5141 | struct mgmt_cp_load_conn_param *cp = data; |
| 5142 | u16 param_count, expected_len; |
| 5143 | int i; |
| 5144 | |
| 5145 | if (!lmp_le_capable(hdev)) |
| 5146 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5147 | MGMT_STATUS_NOT_SUPPORTED); |
| 5148 | |
| 5149 | param_count = __le16_to_cpu(cp->param_count); |
| 5150 | |
| 5151 | expected_len = sizeof(*cp) + param_count * |
| 5152 | sizeof(struct mgmt_conn_param); |
| 5153 | if (expected_len != len) { |
| 5154 | BT_ERR("load_conn_param: expected %u bytes, got %u bytes", |
| 5155 | expected_len, len); |
| 5156 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5157 | MGMT_STATUS_INVALID_PARAMS); |
| 5158 | } |
| 5159 | |
| 5160 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 5161 | |
| 5162 | hci_dev_lock(hdev); |
| 5163 | |
| 5164 | hci_conn_params_clear_disabled(hdev); |
| 5165 | |
| 5166 | for (i = 0; i < param_count; i++) { |
| 5167 | struct mgmt_conn_param *param = &cp->params[i]; |
| 5168 | struct hci_conn_params *hci_param; |
| 5169 | u16 min, max, latency, timeout; |
| 5170 | u8 addr_type; |
| 5171 | |
| 5172 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 5173 | param->addr.type); |
| 5174 | |
| 5175 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 5176 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5177 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 5178 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5179 | } else { |
| 5180 | BT_ERR("Ignoring invalid connection parameters"); |
| 5181 | continue; |
| 5182 | } |
| 5183 | |
| 5184 | min = le16_to_cpu(param->min_interval); |
| 5185 | max = le16_to_cpu(param->max_interval); |
| 5186 | latency = le16_to_cpu(param->latency); |
| 5187 | timeout = le16_to_cpu(param->timeout); |
| 5188 | |
| 5189 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 5190 | min, max, latency, timeout); |
| 5191 | |
| 5192 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
| 5193 | BT_ERR("Ignoring invalid connection parameters"); |
| 5194 | continue; |
| 5195 | } |
| 5196 | |
| 5197 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 5198 | addr_type); |
| 5199 | if (!hci_param) { |
| 5200 | BT_ERR("Failed to add connection parameters"); |
| 5201 | continue; |
| 5202 | } |
| 5203 | |
| 5204 | hci_param->conn_min_interval = min; |
| 5205 | hci_param->conn_max_interval = max; |
| 5206 | hci_param->conn_latency = latency; |
| 5207 | hci_param->supervision_timeout = timeout; |
| 5208 | } |
| 5209 | |
| 5210 | hci_dev_unlock(hdev); |
| 5211 | |
| 5212 | return cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, NULL, 0); |
| 5213 | } |
| 5214 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5215 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5216 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 5217 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5218 | bool var_len; |
| 5219 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5220 | } mgmt_handlers[] = { |
| 5221 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5222 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 5223 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 5224 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 5225 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 5226 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 5227 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 5228 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 5229 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 5230 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 5231 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 5232 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 5233 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 5234 | { set_le, false, MGMT_SETTING_SIZE }, |
| 5235 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 5236 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 5237 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 5238 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 5239 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 5240 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 5241 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 5242 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 5243 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 5244 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 5245 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 5246 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 5247 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 5248 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 5249 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 5250 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 5251 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 5252 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 5253 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5254 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5255 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 5256 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 5257 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 5258 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 5259 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 5260 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5261 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5262 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5263 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5264 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5265 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5266 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5267 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5268 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5269 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5270 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5271 | { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE }, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5272 | { add_device, false, MGMT_ADD_DEVICE_SIZE }, |
| 5273 | { remove_device, false, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame^] | 5274 | { load_conn_param, true, MGMT_LOAD_CONN_PARAM_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5275 | }; |
| 5276 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5277 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 5278 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5279 | void *buf; |
| 5280 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5281 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 5282 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5283 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5284 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5285 | int err; |
| 5286 | |
| 5287 | BT_DBG("got %zu bytes", msglen); |
| 5288 | |
| 5289 | if (msglen < sizeof(*hdr)) |
| 5290 | return -EINVAL; |
| 5291 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 5292 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5293 | if (!buf) |
| 5294 | return -ENOMEM; |
| 5295 | |
| 5296 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 5297 | err = -EFAULT; |
| 5298 | goto done; |
| 5299 | } |
| 5300 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5301 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5302 | opcode = __le16_to_cpu(hdr->opcode); |
| 5303 | index = __le16_to_cpu(hdr->index); |
| 5304 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5305 | |
| 5306 | if (len != msglen - sizeof(*hdr)) { |
| 5307 | err = -EINVAL; |
| 5308 | goto done; |
| 5309 | } |
| 5310 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5311 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5312 | hdev = hci_dev_get(index); |
| 5313 | if (!hdev) { |
| 5314 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5315 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5316 | goto done; |
| 5317 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5318 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 5319 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 5320 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) || |
| 5321 | test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5322 | err = cmd_status(sk, index, opcode, |
| 5323 | MGMT_STATUS_INVALID_INDEX); |
| 5324 | goto done; |
| 5325 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5326 | } |
| 5327 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5328 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5329 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5330 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5331 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5332 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5333 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5334 | } |
| 5335 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5336 | if ((hdev && opcode < MGMT_OP_READ_INFO) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5337 | (!hdev && opcode >= MGMT_OP_READ_INFO)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5338 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5339 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5340 | goto done; |
| 5341 | } |
| 5342 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5343 | handler = &mgmt_handlers[opcode]; |
| 5344 | |
| 5345 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5346 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5347 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5348 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5349 | goto done; |
| 5350 | } |
| 5351 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5352 | if (hdev) |
| 5353 | mgmt_init_hdev(sk, hdev); |
| 5354 | |
| 5355 | cp = buf + sizeof(*hdr); |
| 5356 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5357 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 5358 | if (err < 0) |
| 5359 | goto done; |
| 5360 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5361 | err = msglen; |
| 5362 | |
| 5363 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5364 | if (hdev) |
| 5365 | hci_dev_put(hdev); |
| 5366 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5367 | kfree(buf); |
| 5368 | return err; |
| 5369 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5370 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5371 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5372 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5373 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5374 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5375 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5376 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5377 | } |
| 5378 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5379 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5380 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 5381 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5382 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5383 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5384 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5385 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5386 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5387 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5388 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5389 | } |
| 5390 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5391 | /* This function requires the caller holds hdev->lock */ |
| 5392 | static void restart_le_auto_conns(struct hci_dev *hdev) |
| 5393 | { |
| 5394 | struct hci_conn_params *p; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5395 | bool added = false; |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5396 | |
| 5397 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5398 | if (p->auto_connect == HCI_AUTO_CONN_ALWAYS) { |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5399 | hci_pend_le_conn_add(hdev, &p->addr, p->addr_type); |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5400 | added = true; |
| 5401 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5402 | } |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5403 | |
| 5404 | /* Calling hci_pend_le_conn_add will actually already trigger |
| 5405 | * background scanning when needed. So no need to trigger it |
| 5406 | * just another time. |
| 5407 | * |
| 5408 | * This check is here to avoid an unneeded restart of the |
| 5409 | * passive scanning. Since this is during the controller |
| 5410 | * power up phase the duplicate filtering is not an issue. |
| 5411 | */ |
| 5412 | if (added) |
| 5413 | return; |
| 5414 | |
| 5415 | hci_update_background_scan(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5416 | } |
| 5417 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5418 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 5419 | { |
| 5420 | struct cmd_lookup match = { NULL, hdev }; |
| 5421 | |
| 5422 | BT_DBG("status 0x%02x", status); |
| 5423 | |
| 5424 | hci_dev_lock(hdev); |
| 5425 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5426 | restart_le_auto_conns(hdev); |
| 5427 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5428 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5429 | |
| 5430 | new_settings(hdev, match.sk); |
| 5431 | |
| 5432 | hci_dev_unlock(hdev); |
| 5433 | |
| 5434 | if (match.sk) |
| 5435 | sock_put(match.sk); |
| 5436 | } |
| 5437 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5438 | static int powered_update_hci(struct hci_dev *hdev) |
| 5439 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5440 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5441 | u8 link_sec; |
| 5442 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5443 | hci_req_init(&req, hdev); |
| 5444 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5445 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 5446 | !lmp_host_ssp_capable(hdev)) { |
| 5447 | u8 ssp = 1; |
| 5448 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5449 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5450 | } |
| 5451 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 5452 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 5453 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5454 | struct hci_cp_write_le_host_supported cp; |
| 5455 | |
| 5456 | cp.le = 1; |
| 5457 | cp.simul = lmp_le_br_capable(hdev); |
| 5458 | |
| 5459 | /* Check first if we already have the right |
| 5460 | * host state (host features set) |
| 5461 | */ |
| 5462 | if (cp.le != lmp_host_le_capable(hdev) || |
| 5463 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5464 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 5465 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5466 | } |
| 5467 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5468 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5469 | /* Make sure the controller has a good default for |
| 5470 | * advertising data. This also applies to the case |
| 5471 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 5472 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5473 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 5474 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5475 | update_scan_rsp_data(&req); |
| 5476 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5477 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 5478 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5479 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 5480 | } |
| 5481 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5482 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 5483 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5484 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 5485 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5486 | |
| 5487 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5488 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 5489 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5490 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5491 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5492 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5493 | } |
| 5494 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5495 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5496 | } |
| 5497 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5498 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5499 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 5500 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5501 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 5502 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5503 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5504 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5505 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 5506 | return 0; |
| 5507 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5508 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5509 | if (powered_update_hci(hdev) == 0) |
| 5510 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 5511 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5512 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 5513 | &match); |
| 5514 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5515 | } |
| 5516 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5517 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5518 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 5519 | |
| 5520 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 5521 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 5522 | zero_cod, sizeof(zero_cod), NULL); |
| 5523 | |
| 5524 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5525 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5526 | |
| 5527 | if (match.sk) |
| 5528 | sock_put(match.sk); |
| 5529 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5530 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5531 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5532 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5533 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5534 | { |
| 5535 | struct pending_cmd *cmd; |
| 5536 | u8 status; |
| 5537 | |
| 5538 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5539 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5540 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5541 | |
| 5542 | if (err == -ERFKILL) |
| 5543 | status = MGMT_STATUS_RFKILLED; |
| 5544 | else |
| 5545 | status = MGMT_STATUS_FAILED; |
| 5546 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5547 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5548 | |
| 5549 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5550 | } |
| 5551 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5552 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 5553 | { |
| 5554 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5555 | |
| 5556 | hci_dev_lock(hdev); |
| 5557 | |
| 5558 | /* When discoverable timeout triggers, then just make sure |
| 5559 | * the limited discoverable flag is cleared. Even in the case |
| 5560 | * of a timeout triggered from general discoverable, it is |
| 5561 | * safe to unconditionally clear the flag. |
| 5562 | */ |
| 5563 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5564 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5565 | |
| 5566 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 5567 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 5568 | u8 scan = SCAN_PAGE; |
| 5569 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 5570 | sizeof(scan), &scan); |
| 5571 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5572 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5573 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5574 | hci_req_run(&req, NULL); |
| 5575 | |
| 5576 | hdev->discov_timeout = 0; |
| 5577 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5578 | new_settings(hdev, NULL); |
| 5579 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5580 | hci_dev_unlock(hdev); |
| 5581 | } |
| 5582 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5583 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5584 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5585 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5586 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5587 | /* Nothing needed here if there's a pending command since that |
| 5588 | * commands request completion callback takes care of everything |
| 5589 | * necessary. |
| 5590 | */ |
| 5591 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5592 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5593 | |
Johan Hedberg | bd10799 | 2014-02-24 14:52:19 +0200 | [diff] [blame] | 5594 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5595 | if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5596 | return; |
| 5597 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5598 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5599 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5600 | } else { |
| 5601 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5602 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5603 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5604 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5605 | if (changed) { |
| 5606 | struct hci_request req; |
| 5607 | |
| 5608 | /* In case this change in discoverable was triggered by |
| 5609 | * a disabling of connectable there could be a need to |
| 5610 | * update the advertising flags. |
| 5611 | */ |
| 5612 | hci_req_init(&req, hdev); |
| 5613 | update_adv_data(&req); |
| 5614 | hci_req_run(&req, NULL); |
| 5615 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5616 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5617 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5618 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5619 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5620 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5621 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5622 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5623 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5624 | /* Nothing needed here if there's a pending command since that |
| 5625 | * commands request completion callback takes care of everything |
| 5626 | * necessary. |
| 5627 | */ |
| 5628 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5629 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5630 | |
Johan Hedberg | ce3f24c | 2014-02-24 14:52:20 +0200 | [diff] [blame] | 5631 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5632 | if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5633 | return; |
| 5634 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5635 | if (connectable) |
| 5636 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 5637 | else |
| 5638 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5639 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5640 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5641 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5642 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5643 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5644 | void mgmt_advertising(struct hci_dev *hdev, u8 advertising) |
| 5645 | { |
Johan Hedberg | 7c4cfab | 2014-02-24 14:52:21 +0200 | [diff] [blame] | 5646 | /* Powering off may stop advertising - don't let that interfere */ |
| 5647 | if (!advertising && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5648 | return; |
| 5649 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5650 | if (advertising) |
| 5651 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5652 | else |
| 5653 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5654 | } |
| 5655 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 5656 | 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] | 5657 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5658 | u8 mgmt_err = mgmt_status(status); |
| 5659 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5660 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5661 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5662 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5663 | |
| 5664 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5665 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5666 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5667 | } |
| 5668 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5669 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 5670 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5671 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 5672 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5673 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5674 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5675 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5676 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 5677 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5678 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5679 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 5680 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5681 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5682 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5683 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5684 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5685 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5686 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 5687 | { |
| 5688 | if (ltk->authenticated) |
| 5689 | return MGMT_LTK_AUTHENTICATED; |
| 5690 | |
| 5691 | return MGMT_LTK_UNAUTHENTICATED; |
| 5692 | } |
| 5693 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5694 | 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] | 5695 | { |
| 5696 | struct mgmt_ev_new_long_term_key ev; |
| 5697 | |
| 5698 | memset(&ev, 0, sizeof(ev)); |
| 5699 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 5700 | /* Devices using resolvable or non-resolvable random addresses |
| 5701 | * without providing an indentity resolving key don't require |
| 5702 | * to store long term keys. Their addresses will change the |
| 5703 | * next time around. |
| 5704 | * |
| 5705 | * Only when a remote device provides an identity address |
| 5706 | * make sure the long term key is stored. If the remote |
| 5707 | * identity is known, the long term keys are internally |
| 5708 | * mapped to the identity address. So allow static random |
| 5709 | * and public addresses here. |
| 5710 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5711 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5712 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5713 | ev.store_hint = 0x00; |
| 5714 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5715 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5716 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5717 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5718 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5719 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5720 | ev.key.enc_size = key->enc_size; |
| 5721 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 5722 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5723 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 5724 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5725 | ev.key.master = 1; |
| 5726 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5727 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 5728 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 5729 | 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] | 5730 | } |
| 5731 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5732 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 5733 | { |
| 5734 | struct mgmt_ev_new_irk ev; |
| 5735 | |
| 5736 | memset(&ev, 0, sizeof(ev)); |
| 5737 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 5738 | /* For identity resolving keys from devices that are already |
| 5739 | * using a public address or static random address, do not |
| 5740 | * ask for storing this key. The identity resolving key really |
| 5741 | * is only mandatory for devices using resovlable random |
| 5742 | * addresses. |
| 5743 | * |
| 5744 | * Storing all identity resolving keys has the downside that |
| 5745 | * they will be also loaded on next boot of they system. More |
| 5746 | * identity resolving keys, means more time during scanning is |
| 5747 | * needed to actually resolve these addresses. |
| 5748 | */ |
| 5749 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 5750 | ev.store_hint = 0x01; |
| 5751 | else |
| 5752 | ev.store_hint = 0x00; |
| 5753 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5754 | bacpy(&ev.rpa, &irk->rpa); |
| 5755 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 5756 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 5757 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 5758 | |
| 5759 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 5760 | } |
| 5761 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5762 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 5763 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5764 | { |
| 5765 | struct mgmt_ev_new_csrk ev; |
| 5766 | |
| 5767 | memset(&ev, 0, sizeof(ev)); |
| 5768 | |
| 5769 | /* Devices using resolvable or non-resolvable random addresses |
| 5770 | * without providing an indentity resolving key don't require |
| 5771 | * to store signature resolving keys. Their addresses will change |
| 5772 | * the next time around. |
| 5773 | * |
| 5774 | * Only when a remote device provides an identity address |
| 5775 | * make sure the signature resolving key is stored. So allow |
| 5776 | * static random and public addresses here. |
| 5777 | */ |
| 5778 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5779 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5780 | ev.store_hint = 0x00; |
| 5781 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5782 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5783 | |
| 5784 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 5785 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 5786 | ev.key.master = csrk->master; |
| 5787 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 5788 | |
| 5789 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 5790 | } |
| 5791 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5792 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5793 | u8 bdaddr_type, u16 min_interval, u16 max_interval, |
| 5794 | u16 latency, u16 timeout) |
| 5795 | { |
| 5796 | struct mgmt_ev_new_conn_param ev; |
| 5797 | |
| 5798 | memset(&ev, 0, sizeof(ev)); |
| 5799 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5800 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
| 5801 | ev.store_hint = 0x00; |
| 5802 | ev.min_interval = cpu_to_le16(min_interval); |
| 5803 | ev.max_interval = cpu_to_le16(max_interval); |
| 5804 | ev.latency = cpu_to_le16(latency); |
| 5805 | ev.timeout = cpu_to_le16(timeout); |
| 5806 | |
| 5807 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 5808 | } |
| 5809 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 5810 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 5811 | u8 data_len) |
| 5812 | { |
| 5813 | eir[eir_len++] = sizeof(type) + data_len; |
| 5814 | eir[eir_len++] = type; |
| 5815 | memcpy(&eir[eir_len], data, data_len); |
| 5816 | eir_len += data_len; |
| 5817 | |
| 5818 | return eir_len; |
| 5819 | } |
| 5820 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5821 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5822 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 5823 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5824 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5825 | char buf[512]; |
| 5826 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 5827 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5828 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5829 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5830 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5831 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 5832 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 5833 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5834 | if (name_len > 0) |
| 5835 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5836 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5837 | |
| 5838 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 5839 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5840 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5841 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5842 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5843 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5844 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 5845 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5846 | } |
| 5847 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5848 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 5849 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 5850 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5851 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 5852 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5853 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5854 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5855 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5856 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5857 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5858 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5859 | |
| 5860 | *sk = cmd->sk; |
| 5861 | sock_hold(*sk); |
| 5862 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5863 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5864 | } |
| 5865 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5866 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5867 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5868 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5869 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 5870 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5871 | |
| 5872 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5873 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5874 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5875 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5876 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 5877 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5878 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5879 | |
| 5880 | mgmt_pending_remove(cmd); |
| 5881 | } |
| 5882 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5883 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5884 | u8 link_type, u8 addr_type, u8 reason, |
| 5885 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5886 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5887 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5888 | struct pending_cmd *power_off; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5889 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5890 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5891 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5892 | if (power_off) { |
| 5893 | struct mgmt_mode *cp = power_off->param; |
| 5894 | |
| 5895 | /* The connection is still in hci_conn_hash so test for 1 |
| 5896 | * instead of 0 to know if this is the last one. |
| 5897 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5898 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5899 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5900 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5901 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5902 | } |
| 5903 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5904 | if (!mgmt_connected) |
| 5905 | return; |
| 5906 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 5907 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 5908 | return; |
| 5909 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5910 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5911 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5912 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5913 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 5914 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5915 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5916 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5917 | |
| 5918 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 5919 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5920 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5921 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5922 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5923 | } |
| 5924 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5925 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5926 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5927 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5928 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 5929 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5930 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5931 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5932 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 5933 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 5934 | hdev); |
| 5935 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5936 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5937 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5938 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5939 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5940 | cp = cmd->param; |
| 5941 | |
| 5942 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 5943 | return; |
| 5944 | |
| 5945 | if (cp->addr.type != bdaddr_type) |
| 5946 | return; |
| 5947 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5948 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5949 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 5950 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5951 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 5952 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5953 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5954 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5955 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5956 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5957 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5958 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5959 | { |
| 5960 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5961 | struct pending_cmd *power_off; |
| 5962 | |
| 5963 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5964 | if (power_off) { |
| 5965 | struct mgmt_mode *cp = power_off->param; |
| 5966 | |
| 5967 | /* The connection is still in hci_conn_hash so test for 1 |
| 5968 | * instead of 0 to know if this is the last one. |
| 5969 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5970 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5971 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5972 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5973 | } |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5974 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5975 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 5976 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5977 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5978 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5979 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5980 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5981 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5982 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 5983 | 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] | 5984 | { |
| 5985 | struct mgmt_ev_pin_code_request ev; |
| 5986 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5987 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5988 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 5989 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5990 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 5991 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5992 | } |
| 5993 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5994 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5995 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5996 | { |
| 5997 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5998 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5999 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6000 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6001 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6002 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6003 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6004 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6005 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6006 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6007 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 6008 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6009 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6010 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6011 | } |
| 6012 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6013 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6014 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6015 | { |
| 6016 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6017 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6018 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6019 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6020 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6021 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6022 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6023 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6024 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6025 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6026 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 6027 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6028 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6029 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6030 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6031 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6032 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6033 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6034 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6035 | { |
| 6036 | struct mgmt_ev_user_confirm_request ev; |
| 6037 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6038 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6039 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6040 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6041 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 6042 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6043 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6044 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6045 | 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] | 6046 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6047 | } |
| 6048 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6049 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 6050 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6051 | { |
| 6052 | struct mgmt_ev_user_passkey_request ev; |
| 6053 | |
| 6054 | BT_DBG("%s", hdev->name); |
| 6055 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6056 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6057 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6058 | |
| 6059 | 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] | 6060 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6061 | } |
| 6062 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 6063 | 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] | 6064 | u8 link_type, u8 addr_type, u8 status, |
| 6065 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6066 | { |
| 6067 | struct pending_cmd *cmd; |
| 6068 | struct mgmt_rp_user_confirm_reply rp; |
| 6069 | int err; |
| 6070 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6071 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6072 | if (!cmd) |
| 6073 | return -ENOENT; |
| 6074 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6075 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6076 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6077 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6078 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6079 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6080 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6081 | |
| 6082 | return err; |
| 6083 | } |
| 6084 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6085 | 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] | 6086 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6087 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6088 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6089 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6090 | } |
| 6091 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6092 | 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] | 6093 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6094 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6095 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6096 | status, |
| 6097 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6098 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6099 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6100 | 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] | 6101 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6102 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6103 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6104 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6105 | } |
| 6106 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6107 | 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] | 6108 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6109 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6110 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6111 | status, |
| 6112 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6113 | } |
| 6114 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 6115 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6116 | u8 link_type, u8 addr_type, u32 passkey, |
| 6117 | u8 entered) |
| 6118 | { |
| 6119 | struct mgmt_ev_passkey_notify ev; |
| 6120 | |
| 6121 | BT_DBG("%s", hdev->name); |
| 6122 | |
| 6123 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6124 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6125 | ev.passkey = __cpu_to_le32(passkey); |
| 6126 | ev.entered = entered; |
| 6127 | |
| 6128 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 6129 | } |
| 6130 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6131 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6132 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6133 | { |
| 6134 | struct mgmt_ev_auth_failed ev; |
| 6135 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 6136 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6137 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6138 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6139 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6140 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6141 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6142 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6143 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6144 | { |
| 6145 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6146 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6147 | |
| 6148 | if (status) { |
| 6149 | u8 mgmt_err = mgmt_status(status); |
| 6150 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6151 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6152 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6153 | } |
| 6154 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6155 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 6156 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 6157 | &hdev->dev_flags); |
| 6158 | else |
| 6159 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 6160 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6161 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6162 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6163 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6164 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6165 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6166 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6167 | |
| 6168 | if (match.sk) |
| 6169 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6170 | } |
| 6171 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6172 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6173 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6174 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6175 | struct hci_cp_write_eir cp; |
| 6176 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 6177 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6178 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6179 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 6180 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 6181 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6182 | memset(&cp, 0, sizeof(cp)); |
| 6183 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6184 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6185 | } |
| 6186 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6187 | 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] | 6188 | { |
| 6189 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6190 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6191 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6192 | |
| 6193 | if (status) { |
| 6194 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6195 | |
| 6196 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6197 | &hdev->dev_flags)) { |
| 6198 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6199 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6200 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6201 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6202 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 6203 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6204 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6205 | } |
| 6206 | |
| 6207 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6208 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6209 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6210 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 6211 | if (!changed) |
| 6212 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 6213 | &hdev->dev_flags); |
| 6214 | else |
| 6215 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6216 | } |
| 6217 | |
| 6218 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 6219 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6220 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6221 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6222 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 6223 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6224 | sock_put(match.sk); |
| 6225 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6226 | hci_req_init(&req, hdev); |
| 6227 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6228 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 6229 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 6230 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6231 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6232 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6233 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6234 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6235 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6236 | |
| 6237 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6238 | } |
| 6239 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6240 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 6241 | { |
| 6242 | struct cmd_lookup match = { NULL, hdev }; |
| 6243 | bool changed = false; |
| 6244 | |
| 6245 | if (status) { |
| 6246 | u8 mgmt_err = mgmt_status(status); |
| 6247 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6248 | if (enable) { |
| 6249 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 6250 | &hdev->dev_flags)) |
| 6251 | new_settings(hdev, NULL); |
| 6252 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6253 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6254 | |
| 6255 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6256 | cmd_status_rsp, &mgmt_err); |
| 6257 | return; |
| 6258 | } |
| 6259 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6260 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6261 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6262 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6263 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6264 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6265 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6266 | |
| 6267 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6268 | settings_rsp, &match); |
| 6269 | |
| 6270 | if (changed) |
| 6271 | new_settings(hdev, match.sk); |
| 6272 | |
| 6273 | if (match.sk) |
| 6274 | sock_put(match.sk); |
| 6275 | } |
| 6276 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6277 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6278 | { |
| 6279 | struct cmd_lookup *match = data; |
| 6280 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6281 | if (match->sk == NULL) { |
| 6282 | match->sk = cmd->sk; |
| 6283 | sock_hold(match->sk); |
| 6284 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6285 | } |
| 6286 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6287 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 6288 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6289 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6290 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6291 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6292 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 6293 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 6294 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6295 | |
| 6296 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6297 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 6298 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6299 | |
| 6300 | if (match.sk) |
| 6301 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6302 | } |
| 6303 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6304 | 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] | 6305 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6306 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6307 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6308 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6309 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6310 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6311 | |
| 6312 | memset(&ev, 0, sizeof(ev)); |
| 6313 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6314 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6315 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6316 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6317 | if (!cmd) { |
| 6318 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6319 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6320 | /* If this is a HCI command related to powering on the |
| 6321 | * HCI dev don't send any mgmt signals. |
| 6322 | */ |
| 6323 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6324 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6325 | } |
| 6326 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6327 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 6328 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6329 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6330 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6331 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 6332 | u8 *randomizer192, u8 *hash256, |
| 6333 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6334 | { |
| 6335 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6336 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6337 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6338 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6339 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6340 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6341 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6342 | |
| 6343 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6344 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 6345 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6346 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6347 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 6348 | hash256 && randomizer256) { |
| 6349 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6350 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6351 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 6352 | memcpy(rp.randomizer192, randomizer192, |
| 6353 | sizeof(rp.randomizer192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6354 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6355 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
| 6356 | memcpy(rp.randomizer256, randomizer256, |
| 6357 | sizeof(rp.randomizer256)); |
| 6358 | |
| 6359 | cmd_complete(cmd->sk, hdev->id, |
| 6360 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6361 | &rp, sizeof(rp)); |
| 6362 | } else { |
| 6363 | struct mgmt_rp_read_local_oob_data rp; |
| 6364 | |
| 6365 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 6366 | memcpy(rp.randomizer, randomizer192, |
| 6367 | sizeof(rp.randomizer)); |
| 6368 | |
| 6369 | cmd_complete(cmd->sk, hdev->id, |
| 6370 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6371 | &rp, sizeof(rp)); |
| 6372 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6373 | } |
| 6374 | |
| 6375 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6376 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6377 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6378 | void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 6379 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 6380 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6381 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6382 | char buf[512]; |
| 6383 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6384 | struct smp_irk *irk; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6385 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6386 | |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6387 | if (!hci_discovery_active(hdev)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6388 | return; |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6389 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6390 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 6391 | * are for the potential CoD field. |
| 6392 | */ |
| 6393 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6394 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 6395 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6396 | memset(buf, 0, sizeof(buf)); |
| 6397 | |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6398 | irk = hci_get_irk(hdev, bdaddr, addr_type); |
| 6399 | if (irk) { |
| 6400 | bacpy(&ev->addr.bdaddr, &irk->bdaddr); |
| 6401 | ev->addr.type = link_to_bdaddr(link_type, irk->addr_type); |
| 6402 | } else { |
| 6403 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 6404 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
| 6405 | } |
| 6406 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6407 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 6408 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6409 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6410 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6411 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6412 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6413 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 6414 | 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] | 6415 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6416 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6417 | if (scan_rsp_len > 0) |
| 6418 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
| 6419 | |
| 6420 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 6421 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 6422 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6423 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6424 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6425 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6426 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6427 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6428 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6429 | struct mgmt_ev_device_found *ev; |
| 6430 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 6431 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6432 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6433 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6434 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6435 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6436 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6437 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6438 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6439 | ev->rssi = rssi; |
| 6440 | |
| 6441 | 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] | 6442 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6443 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6444 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6445 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6446 | 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] | 6447 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6448 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6449 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6450 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6451 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6452 | struct pending_cmd *cmd; |
| 6453 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 6454 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 6455 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6456 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6457 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6458 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6459 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6460 | |
| 6461 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 6462 | u8 type = hdev->discovery.type; |
| 6463 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6464 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 6465 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6466 | mgmt_pending_remove(cmd); |
| 6467 | } |
| 6468 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6469 | memset(&ev, 0, sizeof(ev)); |
| 6470 | ev.type = hdev->discovery.type; |
| 6471 | ev.discovering = discovering; |
| 6472 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6473 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6474 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6475 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6476 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 6477 | { |
| 6478 | BT_DBG("%s status %u", hdev->name, status); |
| 6479 | |
| 6480 | /* Clear the advertising mgmt setting if we failed to re-enable it */ |
| 6481 | if (status) { |
| 6482 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6483 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6484 | } |
| 6485 | } |
| 6486 | |
| 6487 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 6488 | { |
| 6489 | struct hci_request req; |
| 6490 | |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 6491 | if (hci_conn_num(hdev, LE_LINK) > 0) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6492 | return; |
| 6493 | |
| 6494 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6495 | return; |
| 6496 | |
| 6497 | hci_req_init(&req, hdev); |
| 6498 | enable_advertising(&req); |
| 6499 | |
| 6500 | /* If this fails we have no option but to let user space know |
| 6501 | * that we've disabled advertising. |
| 6502 | */ |
| 6503 | if (hci_req_run(&req, adv_enable_complete) < 0) { |
| 6504 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6505 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6506 | } |
| 6507 | } |