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, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame^] | 92 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | static const u16 mgmt_events[] = { |
| 96 | MGMT_EV_CONTROLLER_ERROR, |
| 97 | MGMT_EV_INDEX_ADDED, |
| 98 | MGMT_EV_INDEX_REMOVED, |
| 99 | MGMT_EV_NEW_SETTINGS, |
| 100 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 101 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 102 | MGMT_EV_NEW_LINK_KEY, |
| 103 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 104 | MGMT_EV_DEVICE_CONNECTED, |
| 105 | MGMT_EV_DEVICE_DISCONNECTED, |
| 106 | MGMT_EV_CONNECT_FAILED, |
| 107 | MGMT_EV_PIN_CODE_REQUEST, |
| 108 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 109 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 110 | MGMT_EV_AUTH_FAILED, |
| 111 | MGMT_EV_DEVICE_FOUND, |
| 112 | MGMT_EV_DISCOVERING, |
| 113 | MGMT_EV_DEVICE_BLOCKED, |
| 114 | MGMT_EV_DEVICE_UNBLOCKED, |
| 115 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 116 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 117 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 118 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 119 | MGMT_EV_DEVICE_ADDED, |
| 120 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 121 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 122 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896 | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 123 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 124 | }; |
| 125 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 126 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 127 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 128 | #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \ |
| 129 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 130 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 131 | struct pending_cmd { |
| 132 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 133 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 134 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 135 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 136 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 137 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 138 | }; |
| 139 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 140 | /* HCI to MGMT error code conversion table */ |
| 141 | static u8 mgmt_status_table[] = { |
| 142 | MGMT_STATUS_SUCCESS, |
| 143 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 144 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 145 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 146 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 147 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 148 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 149 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 150 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 151 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 152 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 153 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 154 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 155 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 156 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 157 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 158 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 159 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 160 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 161 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 162 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 163 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 164 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 165 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 166 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 167 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 168 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 169 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 170 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 171 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 172 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 173 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 174 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 175 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 176 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 177 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 178 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 179 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 180 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 181 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 182 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 183 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 184 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 185 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 186 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 187 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 188 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 189 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 190 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 191 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 192 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 193 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 194 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 195 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 196 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 197 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 198 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 199 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 200 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 201 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 202 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 203 | }; |
| 204 | |
| 205 | static u8 mgmt_status(u8 hci_status) |
| 206 | { |
| 207 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 208 | return mgmt_status_table[hci_status]; |
| 209 | |
| 210 | return MGMT_STATUS_FAILED; |
| 211 | } |
| 212 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 213 | 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] | 214 | { |
| 215 | struct sk_buff *skb; |
| 216 | struct mgmt_hdr *hdr; |
| 217 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 218 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 219 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 220 | 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] | 221 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 222 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 223 | if (!skb) |
| 224 | return -ENOMEM; |
| 225 | |
| 226 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 227 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 228 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 229 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 230 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 231 | |
| 232 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 233 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 234 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 235 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 236 | err = sock_queue_rcv_skb(sk, skb); |
| 237 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 238 | kfree_skb(skb); |
| 239 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 240 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 243 | 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] | 244 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 245 | { |
| 246 | struct sk_buff *skb; |
| 247 | struct mgmt_hdr *hdr; |
| 248 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 249 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 250 | |
| 251 | BT_DBG("sock %p", sk); |
| 252 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 253 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 254 | if (!skb) |
| 255 | return -ENOMEM; |
| 256 | |
| 257 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 258 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 259 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 260 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 261 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 262 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 263 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 264 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 265 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 266 | |
| 267 | if (rp) |
| 268 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 269 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 270 | err = sock_queue_rcv_skb(sk, skb); |
| 271 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 272 | kfree_skb(skb); |
| 273 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 274 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 277 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 278 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 279 | { |
| 280 | struct mgmt_rp_read_version rp; |
| 281 | |
| 282 | BT_DBG("sock %p", sk); |
| 283 | |
| 284 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 285 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 286 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 287 | 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] | 288 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 289 | } |
| 290 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 291 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 292 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 293 | { |
| 294 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 295 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 296 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 297 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 298 | size_t rp_size; |
| 299 | int i, err; |
| 300 | |
| 301 | BT_DBG("sock %p", sk); |
| 302 | |
| 303 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 304 | |
| 305 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 306 | if (!rp) |
| 307 | return -ENOMEM; |
| 308 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 309 | rp->num_commands = cpu_to_le16(num_commands); |
| 310 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 311 | |
| 312 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 313 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 314 | |
| 315 | for (i = 0; i < num_events; i++, opcode++) |
| 316 | put_unaligned_le16(mgmt_events[i], opcode); |
| 317 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 318 | 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] | 319 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 320 | kfree(rp); |
| 321 | |
| 322 | return err; |
| 323 | } |
| 324 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 325 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 326 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 327 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 328 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 329 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 330 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 331 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 332 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 333 | |
| 334 | BT_DBG("sock %p", sk); |
| 335 | |
| 336 | read_lock(&hci_dev_list_lock); |
| 337 | |
| 338 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 339 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame^] | 340 | if (d->dev_type == HCI_BREDR && |
| 341 | !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 342 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 343 | } |
| 344 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 345 | rp_len = sizeof(*rp) + (2 * count); |
| 346 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 347 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 348 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 349 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 350 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 351 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 352 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 353 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame^] | 354 | if (test_bit(HCI_SETUP, &d->dev_flags) || |
| 355 | test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 356 | continue; |
| 357 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame^] | 358 | /* Devices marked as raw-only are neither configured |
| 359 | * nor unconfigured controllers. |
| 360 | */ |
| 361 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 362 | continue; |
| 363 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame^] | 364 | if (d->dev_type == HCI_BREDR && |
| 365 | !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 366 | rp->index[count++] = cpu_to_le16(d->id); |
| 367 | BT_DBG("Added hci%u", d->id); |
| 368 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 371 | rp->num_controllers = cpu_to_le16(count); |
| 372 | rp_len = sizeof(*rp) + (2 * count); |
| 373 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 374 | read_unlock(&hci_dev_list_lock); |
| 375 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 376 | 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] | 377 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 378 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 379 | kfree(rp); |
| 380 | |
| 381 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 382 | } |
| 383 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame^] | 384 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 385 | void *data, u16 data_len) |
| 386 | { |
| 387 | struct mgmt_rp_read_unconf_index_list *rp; |
| 388 | struct hci_dev *d; |
| 389 | size_t rp_len; |
| 390 | u16 count; |
| 391 | int err; |
| 392 | |
| 393 | BT_DBG("sock %p", sk); |
| 394 | |
| 395 | read_lock(&hci_dev_list_lock); |
| 396 | |
| 397 | count = 0; |
| 398 | list_for_each_entry(d, &hci_dev_list, list) { |
| 399 | if (d->dev_type == HCI_BREDR && |
| 400 | test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
| 401 | count++; |
| 402 | } |
| 403 | |
| 404 | rp_len = sizeof(*rp) + (2 * count); |
| 405 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 406 | if (!rp) { |
| 407 | read_unlock(&hci_dev_list_lock); |
| 408 | return -ENOMEM; |
| 409 | } |
| 410 | |
| 411 | count = 0; |
| 412 | list_for_each_entry(d, &hci_dev_list, list) { |
| 413 | if (test_bit(HCI_SETUP, &d->dev_flags) || |
| 414 | test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 415 | continue; |
| 416 | |
| 417 | /* Devices marked as raw-only are neither configured |
| 418 | * nor unconfigured controllers. |
| 419 | */ |
| 420 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 421 | continue; |
| 422 | |
| 423 | if (d->dev_type == HCI_BREDR && |
| 424 | test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { |
| 425 | rp->index[count++] = cpu_to_le16(d->id); |
| 426 | BT_DBG("Added hci%u", d->id); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | rp->num_controllers = cpu_to_le16(count); |
| 431 | rp_len = sizeof(*rp) + (2 * count); |
| 432 | |
| 433 | read_unlock(&hci_dev_list_lock); |
| 434 | |
| 435 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_UNCONF_INDEX_LIST, |
| 436 | 0, rp, rp_len); |
| 437 | |
| 438 | kfree(rp); |
| 439 | |
| 440 | return err; |
| 441 | } |
| 442 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 443 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 444 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 445 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 446 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 447 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 448 | settings |= MGMT_SETTING_PAIRABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 449 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 450 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 451 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 452 | settings |= MGMT_SETTING_CONNECTABLE; |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 453 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 454 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 455 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 456 | settings |= MGMT_SETTING_BREDR; |
| 457 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 458 | |
| 459 | if (lmp_ssp_capable(hdev)) { |
| 460 | settings |= MGMT_SETTING_SSP; |
| 461 | settings |= MGMT_SETTING_HS; |
| 462 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 463 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 464 | if (lmp_sc_capable(hdev) || |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 465 | test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 466 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 467 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 468 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 469 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 470 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 471 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 472 | settings |= MGMT_SETTING_PRIVACY; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 473 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 474 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 475 | return settings; |
| 476 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 477 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 478 | static u32 get_current_settings(struct hci_dev *hdev) |
| 479 | { |
| 480 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 481 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 482 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 483 | settings |= MGMT_SETTING_POWERED; |
| 484 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 485 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 486 | settings |= MGMT_SETTING_CONNECTABLE; |
| 487 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 488 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 489 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 490 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 491 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 492 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 493 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 494 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 495 | settings |= MGMT_SETTING_PAIRABLE; |
| 496 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 497 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 498 | settings |= MGMT_SETTING_BREDR; |
| 499 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 500 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 501 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 502 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 503 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 504 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 505 | |
Johan Hedberg | 84bde9d6c | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 506 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 507 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 508 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 509 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 510 | settings |= MGMT_SETTING_HS; |
| 511 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 512 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 513 | settings |= MGMT_SETTING_ADVERTISING; |
| 514 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 515 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 516 | settings |= MGMT_SETTING_SECURE_CONN; |
| 517 | |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 518 | if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 519 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 520 | |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 521 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 522 | settings |= MGMT_SETTING_PRIVACY; |
| 523 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 524 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 525 | } |
| 526 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 527 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 528 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 529 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 530 | { |
| 531 | u8 *ptr = data, *uuids_start = NULL; |
| 532 | struct bt_uuid *uuid; |
| 533 | |
| 534 | if (len < 4) |
| 535 | return ptr; |
| 536 | |
| 537 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 538 | u16 uuid16; |
| 539 | |
| 540 | if (uuid->size != 16) |
| 541 | continue; |
| 542 | |
| 543 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 544 | if (uuid16 < 0x1100) |
| 545 | continue; |
| 546 | |
| 547 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 548 | continue; |
| 549 | |
| 550 | if (!uuids_start) { |
| 551 | uuids_start = ptr; |
| 552 | uuids_start[0] = 1; |
| 553 | uuids_start[1] = EIR_UUID16_ALL; |
| 554 | ptr += 2; |
| 555 | } |
| 556 | |
| 557 | /* Stop if not enough space to put next UUID */ |
| 558 | if ((ptr - data) + sizeof(u16) > len) { |
| 559 | uuids_start[1] = EIR_UUID16_SOME; |
| 560 | break; |
| 561 | } |
| 562 | |
| 563 | *ptr++ = (uuid16 & 0x00ff); |
| 564 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 565 | uuids_start[0] += sizeof(uuid16); |
| 566 | } |
| 567 | |
| 568 | return ptr; |
| 569 | } |
| 570 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 571 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 572 | { |
| 573 | u8 *ptr = data, *uuids_start = NULL; |
| 574 | struct bt_uuid *uuid; |
| 575 | |
| 576 | if (len < 6) |
| 577 | return ptr; |
| 578 | |
| 579 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 580 | if (uuid->size != 32) |
| 581 | continue; |
| 582 | |
| 583 | if (!uuids_start) { |
| 584 | uuids_start = ptr; |
| 585 | uuids_start[0] = 1; |
| 586 | uuids_start[1] = EIR_UUID32_ALL; |
| 587 | ptr += 2; |
| 588 | } |
| 589 | |
| 590 | /* Stop if not enough space to put next UUID */ |
| 591 | if ((ptr - data) + sizeof(u32) > len) { |
| 592 | uuids_start[1] = EIR_UUID32_SOME; |
| 593 | break; |
| 594 | } |
| 595 | |
| 596 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 597 | ptr += sizeof(u32); |
| 598 | uuids_start[0] += sizeof(u32); |
| 599 | } |
| 600 | |
| 601 | return ptr; |
| 602 | } |
| 603 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 604 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 605 | { |
| 606 | u8 *ptr = data, *uuids_start = NULL; |
| 607 | struct bt_uuid *uuid; |
| 608 | |
| 609 | if (len < 18) |
| 610 | return ptr; |
| 611 | |
| 612 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 613 | if (uuid->size != 128) |
| 614 | continue; |
| 615 | |
| 616 | if (!uuids_start) { |
| 617 | uuids_start = ptr; |
| 618 | uuids_start[0] = 1; |
| 619 | uuids_start[1] = EIR_UUID128_ALL; |
| 620 | ptr += 2; |
| 621 | } |
| 622 | |
| 623 | /* Stop if not enough space to put next UUID */ |
| 624 | if ((ptr - data) + 16 > len) { |
| 625 | uuids_start[1] = EIR_UUID128_SOME; |
| 626 | break; |
| 627 | } |
| 628 | |
| 629 | memcpy(ptr, uuid->uuid, 16); |
| 630 | ptr += 16; |
| 631 | uuids_start[0] += 16; |
| 632 | } |
| 633 | |
| 634 | return ptr; |
| 635 | } |
| 636 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 637 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 638 | { |
| 639 | struct pending_cmd *cmd; |
| 640 | |
| 641 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 642 | if (cmd->opcode == opcode) |
| 643 | return cmd; |
| 644 | } |
| 645 | |
| 646 | return NULL; |
| 647 | } |
| 648 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 649 | static struct pending_cmd *mgmt_pending_find_data(u16 opcode, |
| 650 | struct hci_dev *hdev, |
| 651 | const void *data) |
| 652 | { |
| 653 | struct pending_cmd *cmd; |
| 654 | |
| 655 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 656 | if (cmd->user_data != data) |
| 657 | continue; |
| 658 | if (cmd->opcode == opcode) |
| 659 | return cmd; |
| 660 | } |
| 661 | |
| 662 | return NULL; |
| 663 | } |
| 664 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 665 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 666 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 667 | u8 ad_len = 0; |
| 668 | size_t name_len; |
| 669 | |
| 670 | name_len = strlen(hdev->dev_name); |
| 671 | if (name_len > 0) { |
| 672 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 673 | |
| 674 | if (name_len > max_len) { |
| 675 | name_len = max_len; |
| 676 | ptr[1] = EIR_NAME_SHORT; |
| 677 | } else |
| 678 | ptr[1] = EIR_NAME_COMPLETE; |
| 679 | |
| 680 | ptr[0] = name_len + 1; |
| 681 | |
| 682 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 683 | |
| 684 | ad_len += (name_len + 2); |
| 685 | ptr += (name_len + 2); |
| 686 | } |
| 687 | |
| 688 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | static void update_scan_rsp_data(struct hci_request *req) |
| 692 | { |
| 693 | struct hci_dev *hdev = req->hdev; |
| 694 | struct hci_cp_le_set_scan_rsp_data cp; |
| 695 | u8 len; |
| 696 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 697 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 698 | return; |
| 699 | |
| 700 | memset(&cp, 0, sizeof(cp)); |
| 701 | |
| 702 | len = create_scan_rsp_data(hdev, cp.data); |
| 703 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 704 | if (hdev->scan_rsp_data_len == len && |
| 705 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 706 | return; |
| 707 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 708 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 709 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 710 | |
| 711 | cp.length = len; |
| 712 | |
| 713 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 714 | } |
| 715 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 716 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 717 | { |
| 718 | struct pending_cmd *cmd; |
| 719 | |
| 720 | /* If there's a pending mgmt command the flags will not yet have |
| 721 | * their final values, so check for this first. |
| 722 | */ |
| 723 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 724 | if (cmd) { |
| 725 | struct mgmt_mode *cp = cmd->param; |
| 726 | if (cp->val == 0x01) |
| 727 | return LE_AD_GENERAL; |
| 728 | else if (cp->val == 0x02) |
| 729 | return LE_AD_LIMITED; |
| 730 | } else { |
| 731 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 732 | return LE_AD_LIMITED; |
| 733 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 734 | return LE_AD_GENERAL; |
| 735 | } |
| 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 740 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 741 | { |
| 742 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 743 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 744 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 745 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 746 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 747 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 748 | |
| 749 | if (flags) { |
| 750 | BT_DBG("adv flags 0x%02x", flags); |
| 751 | |
| 752 | ptr[0] = 2; |
| 753 | ptr[1] = EIR_FLAGS; |
| 754 | ptr[2] = flags; |
| 755 | |
| 756 | ad_len += 3; |
| 757 | ptr += 3; |
| 758 | } |
| 759 | |
| 760 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 761 | ptr[0] = 2; |
| 762 | ptr[1] = EIR_TX_POWER; |
| 763 | ptr[2] = (u8) hdev->adv_tx_power; |
| 764 | |
| 765 | ad_len += 3; |
| 766 | ptr += 3; |
| 767 | } |
| 768 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 769 | return ad_len; |
| 770 | } |
| 771 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 772 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 773 | { |
| 774 | struct hci_dev *hdev = req->hdev; |
| 775 | struct hci_cp_le_set_adv_data cp; |
| 776 | u8 len; |
| 777 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 778 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 779 | return; |
| 780 | |
| 781 | memset(&cp, 0, sizeof(cp)); |
| 782 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 783 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 784 | |
| 785 | if (hdev->adv_data_len == len && |
| 786 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 787 | return; |
| 788 | |
| 789 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 790 | hdev->adv_data_len = len; |
| 791 | |
| 792 | cp.length = len; |
| 793 | |
| 794 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 795 | } |
| 796 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 797 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 798 | { |
| 799 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 800 | size_t name_len; |
| 801 | |
| 802 | name_len = strlen(hdev->dev_name); |
| 803 | |
| 804 | if (name_len > 0) { |
| 805 | /* EIR Data type */ |
| 806 | if (name_len > 48) { |
| 807 | name_len = 48; |
| 808 | ptr[1] = EIR_NAME_SHORT; |
| 809 | } else |
| 810 | ptr[1] = EIR_NAME_COMPLETE; |
| 811 | |
| 812 | /* EIR Data length */ |
| 813 | ptr[0] = name_len + 1; |
| 814 | |
| 815 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 816 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 817 | ptr += (name_len + 2); |
| 818 | } |
| 819 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 820 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 821 | ptr[0] = 2; |
| 822 | ptr[1] = EIR_TX_POWER; |
| 823 | ptr[2] = (u8) hdev->inq_tx_power; |
| 824 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 825 | ptr += 3; |
| 826 | } |
| 827 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 828 | if (hdev->devid_source > 0) { |
| 829 | ptr[0] = 9; |
| 830 | ptr[1] = EIR_DEVICE_ID; |
| 831 | |
| 832 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 833 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 834 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 835 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 836 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 837 | ptr += 10; |
| 838 | } |
| 839 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 840 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 841 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 842 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 843 | } |
| 844 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 845 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 846 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 847 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 848 | struct hci_cp_write_eir cp; |
| 849 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 850 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 851 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 852 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 853 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 854 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 855 | |
Johan Hedberg | 84bde9d6c | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 856 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 857 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 858 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 859 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 860 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 861 | |
| 862 | memset(&cp, 0, sizeof(cp)); |
| 863 | |
| 864 | create_eir(hdev, cp.data); |
| 865 | |
| 866 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 867 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 868 | |
| 869 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 870 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 871 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 872 | } |
| 873 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 874 | static u8 get_service_classes(struct hci_dev *hdev) |
| 875 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 876 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 877 | u8 val = 0; |
| 878 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 879 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 880 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 881 | |
| 882 | return val; |
| 883 | } |
| 884 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 885 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 886 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 887 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 888 | u8 cod[3]; |
| 889 | |
| 890 | BT_DBG("%s", hdev->name); |
| 891 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 892 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 893 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 894 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 895 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 896 | return; |
| 897 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 898 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 899 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 900 | |
| 901 | cod[0] = hdev->minor_class; |
| 902 | cod[1] = hdev->major_class; |
| 903 | cod[2] = get_service_classes(hdev); |
| 904 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 905 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 906 | cod[1] |= 0x20; |
| 907 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 908 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 909 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 910 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 911 | 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] | 912 | } |
| 913 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 914 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 915 | { |
| 916 | struct pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 917 | |
| 918 | /* If there's a pending mgmt command the flag will not yet have |
| 919 | * it's final value, so check for this first. |
| 920 | */ |
| 921 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 922 | if (cmd) { |
| 923 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 924 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 925 | } |
| 926 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 927 | return test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | static void enable_advertising(struct hci_request *req) |
| 931 | { |
| 932 | struct hci_dev *hdev = req->hdev; |
| 933 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 934 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 935 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 936 | |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 937 | /* Clear the HCI_ADVERTISING bit temporarily so that the |
| 938 | * hci_update_random_address knows that it's safe to go ahead |
| 939 | * and write a new random address. The flag will be set back on |
| 940 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 941 | */ |
| 942 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 943 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 944 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 945 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 946 | /* Set require_privacy to true only when non-connectable |
| 947 | * advertising is used. In that case it is fine to use a |
| 948 | * non-resolvable private address. |
| 949 | */ |
| 950 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 951 | return; |
| 952 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 953 | memset(&cp, 0, sizeof(cp)); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 954 | cp.min_interval = cpu_to_le16(0x0800); |
| 955 | cp.max_interval = cpu_to_le16(0x0800); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 956 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 957 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 958 | cp.channel_map = hdev->le_adv_channel_map; |
| 959 | |
| 960 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 961 | |
| 962 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 963 | } |
| 964 | |
| 965 | static void disable_advertising(struct hci_request *req) |
| 966 | { |
| 967 | u8 enable = 0x00; |
| 968 | |
| 969 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 970 | } |
| 971 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 972 | static void service_cache_off(struct work_struct *work) |
| 973 | { |
| 974 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 975 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 976 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 977 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 978 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 979 | return; |
| 980 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 981 | hci_req_init(&req, hdev); |
| 982 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 983 | hci_dev_lock(hdev); |
| 984 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 985 | update_eir(&req); |
| 986 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 987 | |
| 988 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 989 | |
| 990 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 991 | } |
| 992 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 993 | static void rpa_expired(struct work_struct *work) |
| 994 | { |
| 995 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 996 | rpa_expired.work); |
| 997 | struct hci_request req; |
| 998 | |
| 999 | BT_DBG(""); |
| 1000 | |
| 1001 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 1002 | |
| 1003 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags) || |
| 1004 | hci_conn_num(hdev, LE_LINK) > 0) |
| 1005 | return; |
| 1006 | |
| 1007 | /* The generation of a new RPA and programming it into the |
| 1008 | * controller happens in the enable_advertising() function. |
| 1009 | */ |
| 1010 | |
| 1011 | hci_req_init(&req, hdev); |
| 1012 | |
| 1013 | disable_advertising(&req); |
| 1014 | enable_advertising(&req); |
| 1015 | |
| 1016 | hci_req_run(&req, NULL); |
| 1017 | } |
| 1018 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1019 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1020 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1021 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1022 | return; |
| 1023 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1024 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1025 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1026 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1027 | /* Non-mgmt controlled devices get this bit set |
| 1028 | * implicitly so that pairing works for them, however |
| 1029 | * for mgmt we require user-space to explicitly enable |
| 1030 | * it |
| 1031 | */ |
| 1032 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1033 | } |
| 1034 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1035 | 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] | 1036 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1037 | { |
| 1038 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1039 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1040 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1041 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1042 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1043 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1044 | memset(&rp, 0, sizeof(rp)); |
| 1045 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1046 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1047 | |
| 1048 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1049 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1050 | |
| 1051 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1052 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1053 | |
| 1054 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1055 | |
| 1056 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1057 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1058 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1059 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1060 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1061 | 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] | 1062 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 1066 | { |
| 1067 | sock_put(cmd->sk); |
| 1068 | kfree(cmd->param); |
| 1069 | kfree(cmd); |
| 1070 | } |
| 1071 | |
| 1072 | 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] | 1073 | struct hci_dev *hdev, void *data, |
| 1074 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1075 | { |
| 1076 | struct pending_cmd *cmd; |
| 1077 | |
Johan Hedberg | fca2001 | 2014-06-28 17:54:05 +0300 | [diff] [blame] | 1078 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1079 | if (!cmd) |
| 1080 | return NULL; |
| 1081 | |
| 1082 | cmd->opcode = opcode; |
| 1083 | cmd->index = hdev->id; |
| 1084 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 1085 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1086 | if (!cmd->param) { |
| 1087 | kfree(cmd); |
| 1088 | return NULL; |
| 1089 | } |
| 1090 | |
| 1091 | if (data) |
| 1092 | memcpy(cmd->param, data, len); |
| 1093 | |
| 1094 | cmd->sk = sk; |
| 1095 | sock_hold(sk); |
| 1096 | |
| 1097 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1098 | |
| 1099 | return cmd; |
| 1100 | } |
| 1101 | |
| 1102 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1103 | void (*cb)(struct pending_cmd *cmd, |
| 1104 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1105 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1106 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1107 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1108 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1109 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1110 | if (opcode > 0 && cmd->opcode != opcode) |
| 1111 | continue; |
| 1112 | |
| 1113 | cb(cmd, data); |
| 1114 | } |
| 1115 | } |
| 1116 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1117 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 1118 | { |
| 1119 | list_del(&cmd->list); |
| 1120 | mgmt_pending_free(cmd); |
| 1121 | } |
| 1122 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1123 | 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] | 1124 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1125 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1126 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1127 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1128 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1129 | } |
| 1130 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1131 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status) |
| 1132 | { |
| 1133 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1134 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1135 | if (hci_conn_count(hdev) == 0) { |
| 1136 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1137 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1138 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1139 | } |
| 1140 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1141 | static void hci_stop_discovery(struct hci_request *req) |
| 1142 | { |
| 1143 | struct hci_dev *hdev = req->hdev; |
| 1144 | struct hci_cp_remote_name_req_cancel cp; |
| 1145 | struct inquiry_entry *e; |
| 1146 | |
| 1147 | switch (hdev->discovery.state) { |
| 1148 | case DISCOVERY_FINDING: |
| 1149 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 1150 | hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 1151 | } else { |
| 1152 | cancel_delayed_work(&hdev->le_scan_disable); |
| 1153 | hci_req_add_le_scan_disable(req); |
| 1154 | } |
| 1155 | |
| 1156 | break; |
| 1157 | |
| 1158 | case DISCOVERY_RESOLVING: |
| 1159 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 1160 | NAME_PENDING); |
| 1161 | if (!e) |
| 1162 | return; |
| 1163 | |
| 1164 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 1165 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 1166 | &cp); |
| 1167 | |
| 1168 | break; |
| 1169 | |
| 1170 | default: |
| 1171 | /* Passive scanning */ |
| 1172 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 1173 | hci_req_add_le_scan_disable(req); |
| 1174 | break; |
| 1175 | } |
| 1176 | } |
| 1177 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1178 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1179 | { |
| 1180 | struct hci_request req; |
| 1181 | struct hci_conn *conn; |
| 1182 | |
| 1183 | hci_req_init(&req, hdev); |
| 1184 | |
| 1185 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1186 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1187 | u8 scan = 0x00; |
| 1188 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1189 | } |
| 1190 | |
| 1191 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 1192 | disable_advertising(&req); |
| 1193 | |
Johan Hedberg | f8680f1 | 2014-06-10 14:05:59 +0300 | [diff] [blame] | 1194 | hci_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1195 | |
| 1196 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1197 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1198 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1199 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1200 | switch (conn->state) { |
| 1201 | case BT_CONNECTED: |
| 1202 | case BT_CONFIG: |
| 1203 | dc.handle = cpu_to_le16(conn->handle); |
| 1204 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1205 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1206 | break; |
| 1207 | case BT_CONNECT: |
| 1208 | if (conn->type == LE_LINK) |
| 1209 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1210 | 0, NULL); |
| 1211 | else if (conn->type == ACL_LINK) |
| 1212 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1213 | 6, &conn->dst); |
| 1214 | break; |
| 1215 | case BT_CONNECT2: |
| 1216 | bacpy(&rej.bdaddr, &conn->dst); |
| 1217 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1218 | if (conn->type == ACL_LINK) |
| 1219 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1220 | sizeof(rej), &rej); |
| 1221 | else if (conn->type == SCO_LINK) |
| 1222 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1223 | sizeof(rej), &rej); |
| 1224 | break; |
| 1225 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | return hci_req_run(&req, clean_up_hci_complete); |
| 1229 | } |
| 1230 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1231 | 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] | 1232 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1233 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1234 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1235 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1236 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1237 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1238 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1239 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1240 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1241 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1242 | MGMT_STATUS_INVALID_PARAMS); |
| 1243 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1244 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1245 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1246 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 1247 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1248 | MGMT_STATUS_BUSY); |
| 1249 | goto failed; |
| 1250 | } |
| 1251 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1252 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1253 | cancel_delayed_work(&hdev->power_off); |
| 1254 | |
| 1255 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1256 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1257 | data, len); |
| 1258 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1259 | goto failed; |
| 1260 | } |
| 1261 | } |
| 1262 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1263 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1264 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1265 | goto failed; |
| 1266 | } |
| 1267 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1268 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1269 | if (!cmd) { |
| 1270 | err = -ENOMEM; |
| 1271 | goto failed; |
| 1272 | } |
| 1273 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1274 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1275 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1276 | err = 0; |
| 1277 | } else { |
| 1278 | /* Disconnect connections, stop scans, etc */ |
| 1279 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1280 | if (!err) |
| 1281 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1282 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1283 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1284 | /* ENODATA means there were no HCI commands queued */ |
| 1285 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1286 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1287 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1288 | err = 0; |
| 1289 | } |
| 1290 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1291 | |
| 1292 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1293 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1294 | return err; |
| 1295 | } |
| 1296 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1297 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 1298 | struct sock *skip_sk) |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1299 | { |
| 1300 | struct sk_buff *skb; |
| 1301 | struct mgmt_hdr *hdr; |
| 1302 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 1303 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1304 | if (!skb) |
| 1305 | return -ENOMEM; |
| 1306 | |
| 1307 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 1308 | hdr->opcode = cpu_to_le16(event); |
| 1309 | if (hdev) |
| 1310 | hdr->index = cpu_to_le16(hdev->id); |
| 1311 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1312 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1313 | hdr->len = cpu_to_le16(data_len); |
| 1314 | |
| 1315 | if (data) |
| 1316 | memcpy(skb_put(skb, data_len), data, data_len); |
| 1317 | |
Marcel Holtmann | 97e0bde | 2012-02-22 13:49:28 +0100 | [diff] [blame] | 1318 | /* Time stamp */ |
| 1319 | __net_timestamp(skb); |
| 1320 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1321 | hci_send_to_control(skb, skip_sk); |
| 1322 | kfree_skb(skb); |
| 1323 | |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
| 1327 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1328 | { |
| 1329 | __le32 ev; |
| 1330 | |
| 1331 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1332 | |
| 1333 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1334 | } |
| 1335 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1336 | struct cmd_lookup { |
| 1337 | struct sock *sk; |
| 1338 | struct hci_dev *hdev; |
| 1339 | u8 mgmt_status; |
| 1340 | }; |
| 1341 | |
| 1342 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1343 | { |
| 1344 | struct cmd_lookup *match = data; |
| 1345 | |
| 1346 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1347 | |
| 1348 | list_del(&cmd->list); |
| 1349 | |
| 1350 | if (match->sk == NULL) { |
| 1351 | match->sk = cmd->sk; |
| 1352 | sock_hold(match->sk); |
| 1353 | } |
| 1354 | |
| 1355 | mgmt_pending_free(cmd); |
| 1356 | } |
| 1357 | |
| 1358 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1359 | { |
| 1360 | u8 *status = data; |
| 1361 | |
| 1362 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1363 | mgmt_pending_remove(cmd); |
| 1364 | } |
| 1365 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1366 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1367 | { |
| 1368 | if (!lmp_bredr_capable(hdev)) |
| 1369 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1370 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1371 | return MGMT_STATUS_REJECTED; |
| 1372 | else |
| 1373 | return MGMT_STATUS_SUCCESS; |
| 1374 | } |
| 1375 | |
| 1376 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1377 | { |
| 1378 | if (!lmp_le_capable(hdev)) |
| 1379 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1380 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1381 | return MGMT_STATUS_REJECTED; |
| 1382 | else |
| 1383 | return MGMT_STATUS_SUCCESS; |
| 1384 | } |
| 1385 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1386 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1387 | { |
| 1388 | struct pending_cmd *cmd; |
| 1389 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1390 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1391 | bool changed; |
| 1392 | |
| 1393 | BT_DBG("status 0x%02x", status); |
| 1394 | |
| 1395 | hci_dev_lock(hdev); |
| 1396 | |
| 1397 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1398 | if (!cmd) |
| 1399 | goto unlock; |
| 1400 | |
| 1401 | if (status) { |
| 1402 | u8 mgmt_err = mgmt_status(status); |
| 1403 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1404 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1405 | goto remove_cmd; |
| 1406 | } |
| 1407 | |
| 1408 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1409 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1410 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1411 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1412 | |
| 1413 | if (hdev->discov_timeout > 0) { |
| 1414 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1415 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1416 | to); |
| 1417 | } |
| 1418 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1419 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1420 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1421 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1422 | |
| 1423 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1424 | |
| 1425 | if (changed) |
| 1426 | new_settings(hdev, cmd->sk); |
| 1427 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1428 | /* When the discoverable mode gets changed, make sure |
| 1429 | * that class of device has the limited discoverable |
| 1430 | * bit correctly set. |
| 1431 | */ |
| 1432 | hci_req_init(&req, hdev); |
| 1433 | update_class(&req); |
| 1434 | hci_req_run(&req, NULL); |
| 1435 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1436 | remove_cmd: |
| 1437 | mgmt_pending_remove(cmd); |
| 1438 | |
| 1439 | unlock: |
| 1440 | hci_dev_unlock(hdev); |
| 1441 | } |
| 1442 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1443 | 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] | 1444 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1445 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1446 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1447 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1448 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1449 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1450 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1451 | int err; |
| 1452 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1453 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1454 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1455 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1456 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1457 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1458 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1459 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1460 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1461 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1462 | MGMT_STATUS_INVALID_PARAMS); |
| 1463 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1464 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1465 | |
| 1466 | /* Disabling discoverable requires that no timeout is set, |
| 1467 | * and enabling limited discoverable requires a timeout. |
| 1468 | */ |
| 1469 | if ((cp->val == 0x00 && timeout > 0) || |
| 1470 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1471 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1472 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1473 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1474 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1475 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1476 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1477 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1478 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1479 | goto failed; |
| 1480 | } |
| 1481 | |
| 1482 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1483 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1484 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1485 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1486 | goto failed; |
| 1487 | } |
| 1488 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1489 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1490 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1491 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1492 | goto failed; |
| 1493 | } |
| 1494 | |
| 1495 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1496 | bool changed = false; |
| 1497 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1498 | /* Setting limited discoverable when powered off is |
| 1499 | * not a valid operation since it requires a timeout |
| 1500 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1501 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1502 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1503 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1504 | changed = true; |
| 1505 | } |
| 1506 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1507 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1508 | if (err < 0) |
| 1509 | goto failed; |
| 1510 | |
| 1511 | if (changed) |
| 1512 | err = new_settings(hdev, sk); |
| 1513 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1514 | goto failed; |
| 1515 | } |
| 1516 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1517 | /* If the current mode is the same, then just update the timeout |
| 1518 | * value with the new value. And if only the timeout gets updated, |
| 1519 | * then no need for any HCI transactions. |
| 1520 | */ |
| 1521 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1522 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1523 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1524 | cancel_delayed_work(&hdev->discov_off); |
| 1525 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1526 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1527 | if (cp->val && hdev->discov_timeout > 0) { |
| 1528 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1529 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1530 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1531 | } |
| 1532 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1533 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1534 | goto failed; |
| 1535 | } |
| 1536 | |
| 1537 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1538 | if (!cmd) { |
| 1539 | err = -ENOMEM; |
| 1540 | goto failed; |
| 1541 | } |
| 1542 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1543 | /* Cancel any potential discoverable timeout that might be |
| 1544 | * still active and store new timeout value. The arming of |
| 1545 | * the timeout happens in the complete handler. |
| 1546 | */ |
| 1547 | cancel_delayed_work(&hdev->discov_off); |
| 1548 | hdev->discov_timeout = timeout; |
| 1549 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1550 | /* Limited discoverable mode */ |
| 1551 | if (cp->val == 0x02) |
| 1552 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1553 | else |
| 1554 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1555 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1556 | hci_req_init(&req, hdev); |
| 1557 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1558 | /* The procedure for LE-only controllers is much simpler - just |
| 1559 | * update the advertising data. |
| 1560 | */ |
| 1561 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1562 | goto update_ad; |
| 1563 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1564 | scan = SCAN_PAGE; |
| 1565 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1566 | if (cp->val) { |
| 1567 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1568 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1569 | if (cp->val == 0x02) { |
| 1570 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1571 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1572 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1573 | hci_cp.iac_lap[1] = 0x8b; |
| 1574 | hci_cp.iac_lap[2] = 0x9e; |
| 1575 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1576 | hci_cp.iac_lap[4] = 0x8b; |
| 1577 | hci_cp.iac_lap[5] = 0x9e; |
| 1578 | } else { |
| 1579 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1580 | hci_cp.num_iac = 1; |
| 1581 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1582 | hci_cp.iac_lap[1] = 0x8b; |
| 1583 | hci_cp.iac_lap[2] = 0x9e; |
| 1584 | } |
| 1585 | |
| 1586 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1587 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1588 | |
| 1589 | scan |= SCAN_INQUIRY; |
| 1590 | } else { |
| 1591 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1592 | } |
| 1593 | |
| 1594 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1595 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1596 | update_ad: |
| 1597 | update_adv_data(&req); |
| 1598 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1599 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1600 | if (err < 0) |
| 1601 | mgmt_pending_remove(cmd); |
| 1602 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1603 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1604 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1605 | return err; |
| 1606 | } |
| 1607 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1608 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1609 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1610 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1611 | struct hci_cp_write_page_scan_activity acp; |
| 1612 | u8 type; |
| 1613 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1614 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1615 | return; |
| 1616 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1617 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1618 | return; |
| 1619 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1620 | if (enable) { |
| 1621 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1622 | |
| 1623 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1624 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1625 | } else { |
| 1626 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1627 | |
| 1628 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1629 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1630 | } |
| 1631 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1632 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1633 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1634 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1635 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1636 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1637 | sizeof(acp), &acp); |
| 1638 | |
| 1639 | if (hdev->page_scan_type != type) |
| 1640 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1641 | } |
| 1642 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1643 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1644 | { |
| 1645 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1646 | struct mgmt_mode *cp; |
| 1647 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1648 | |
| 1649 | BT_DBG("status 0x%02x", status); |
| 1650 | |
| 1651 | hci_dev_lock(hdev); |
| 1652 | |
| 1653 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1654 | if (!cmd) |
| 1655 | goto unlock; |
| 1656 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1657 | if (status) { |
| 1658 | u8 mgmt_err = mgmt_status(status); |
| 1659 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1660 | goto remove_cmd; |
| 1661 | } |
| 1662 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1663 | cp = cmd->param; |
| 1664 | if (cp->val) |
| 1665 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1666 | else |
| 1667 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1668 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1669 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1670 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1671 | if (changed) |
| 1672 | new_settings(hdev, cmd->sk); |
| 1673 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1674 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1675 | mgmt_pending_remove(cmd); |
| 1676 | |
| 1677 | unlock: |
| 1678 | hci_dev_unlock(hdev); |
| 1679 | } |
| 1680 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1681 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1682 | struct sock *sk, u8 val) |
| 1683 | { |
| 1684 | bool changed = false; |
| 1685 | int err; |
| 1686 | |
| 1687 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1688 | changed = true; |
| 1689 | |
| 1690 | if (val) { |
| 1691 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1692 | } else { |
| 1693 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1694 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1695 | } |
| 1696 | |
| 1697 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1698 | if (err < 0) |
| 1699 | return err; |
| 1700 | |
| 1701 | if (changed) |
| 1702 | return new_settings(hdev, sk); |
| 1703 | |
| 1704 | return 0; |
| 1705 | } |
| 1706 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1707 | 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] | 1708 | u16 len) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1709 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1710 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1711 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1712 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1713 | u8 scan; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1714 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1715 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1716 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1717 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1718 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1719 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1720 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1721 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1722 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1723 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1724 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1725 | MGMT_STATUS_INVALID_PARAMS); |
| 1726 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1727 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1728 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1729 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1730 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1731 | goto failed; |
| 1732 | } |
| 1733 | |
| 1734 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1735 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1736 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1737 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1738 | goto failed; |
| 1739 | } |
| 1740 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1741 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1742 | if (!cmd) { |
| 1743 | err = -ENOMEM; |
| 1744 | goto failed; |
| 1745 | } |
| 1746 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1747 | hci_req_init(&req, hdev); |
| 1748 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1749 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1750 | * by-product of disabling connectable, we need to update the |
| 1751 | * advertising flags. |
| 1752 | */ |
| 1753 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1754 | if (!cp->val) { |
| 1755 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1756 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1757 | } |
| 1758 | update_adv_data(&req); |
| 1759 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1760 | if (cp->val) { |
| 1761 | scan = SCAN_PAGE; |
| 1762 | } else { |
| 1763 | scan = 0; |
| 1764 | |
| 1765 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1766 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1767 | cancel_delayed_work(&hdev->discov_off); |
| 1768 | } |
| 1769 | |
| 1770 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1771 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1772 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1773 | /* If we're going from non-connectable to connectable or |
| 1774 | * vice-versa when fast connectable is enabled ensure that fast |
| 1775 | * connectable gets disabled. write_fast_connectable won't do |
| 1776 | * anything if the page scan parameters are already what they |
| 1777 | * should be. |
| 1778 | */ |
| 1779 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1780 | write_fast_connectable(&req, false); |
| 1781 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1782 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) && |
| 1783 | hci_conn_num(hdev, LE_LINK) == 0) { |
| 1784 | disable_advertising(&req); |
| 1785 | enable_advertising(&req); |
| 1786 | } |
| 1787 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1788 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1789 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1790 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1791 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1792 | err = set_connectable_update_settings(hdev, sk, |
| 1793 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1794 | goto failed; |
| 1795 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1796 | |
| 1797 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1798 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1799 | return err; |
| 1800 | } |
| 1801 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1802 | 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] | 1803 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1804 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1805 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1806 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1807 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1808 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1809 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1810 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1811 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1812 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1813 | MGMT_STATUS_INVALID_PARAMS); |
| 1814 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1815 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1816 | |
| 1817 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1818 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1819 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1820 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1821 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1822 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1823 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1824 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1825 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1826 | if (changed) |
| 1827 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1828 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1829 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1830 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1831 | return err; |
| 1832 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1833 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1834 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1835 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1836 | { |
| 1837 | struct mgmt_mode *cp = data; |
| 1838 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1839 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1840 | int err; |
| 1841 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1842 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1843 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1844 | status = mgmt_bredr_support(hdev); |
| 1845 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1846 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1847 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1848 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1849 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1850 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1851 | MGMT_STATUS_INVALID_PARAMS); |
| 1852 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1853 | hci_dev_lock(hdev); |
| 1854 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1855 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1856 | bool changed = false; |
| 1857 | |
| 1858 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1859 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1860 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1861 | changed = true; |
| 1862 | } |
| 1863 | |
| 1864 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1865 | if (err < 0) |
| 1866 | goto failed; |
| 1867 | |
| 1868 | if (changed) |
| 1869 | err = new_settings(hdev, sk); |
| 1870 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1871 | goto failed; |
| 1872 | } |
| 1873 | |
| 1874 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1875 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1876 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1877 | goto failed; |
| 1878 | } |
| 1879 | |
| 1880 | val = !!cp->val; |
| 1881 | |
| 1882 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1883 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1884 | goto failed; |
| 1885 | } |
| 1886 | |
| 1887 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1888 | if (!cmd) { |
| 1889 | err = -ENOMEM; |
| 1890 | goto failed; |
| 1891 | } |
| 1892 | |
| 1893 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1894 | if (err < 0) { |
| 1895 | mgmt_pending_remove(cmd); |
| 1896 | goto failed; |
| 1897 | } |
| 1898 | |
| 1899 | failed: |
| 1900 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1901 | return err; |
| 1902 | } |
| 1903 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1904 | 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] | 1905 | { |
| 1906 | struct mgmt_mode *cp = data; |
| 1907 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1908 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1909 | int err; |
| 1910 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1911 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1912 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1913 | status = mgmt_bredr_support(hdev); |
| 1914 | if (status) |
| 1915 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 1916 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1917 | if (!lmp_ssp_capable(hdev)) |
| 1918 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1919 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1920 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1921 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1922 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1923 | MGMT_STATUS_INVALID_PARAMS); |
| 1924 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1925 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1926 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1927 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1928 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1929 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1930 | if (cp->val) { |
| 1931 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 1932 | &hdev->dev_flags); |
| 1933 | } else { |
| 1934 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 1935 | &hdev->dev_flags); |
| 1936 | if (!changed) |
| 1937 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 1938 | &hdev->dev_flags); |
| 1939 | else |
| 1940 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1944 | if (err < 0) |
| 1945 | goto failed; |
| 1946 | |
| 1947 | if (changed) |
| 1948 | err = new_settings(hdev, sk); |
| 1949 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1950 | goto failed; |
| 1951 | } |
| 1952 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1953 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 1954 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 1955 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1956 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1957 | goto failed; |
| 1958 | } |
| 1959 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1960 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1961 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1962 | goto failed; |
| 1963 | } |
| 1964 | |
| 1965 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1966 | if (!cmd) { |
| 1967 | err = -ENOMEM; |
| 1968 | goto failed; |
| 1969 | } |
| 1970 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1971 | if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 1972 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1973 | sizeof(cp->val), &cp->val); |
| 1974 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1975 | 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] | 1976 | if (err < 0) { |
| 1977 | mgmt_pending_remove(cmd); |
| 1978 | goto failed; |
| 1979 | } |
| 1980 | |
| 1981 | failed: |
| 1982 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1983 | return err; |
| 1984 | } |
| 1985 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1986 | 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] | 1987 | { |
| 1988 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1989 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1990 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1991 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1992 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1993 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1994 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1995 | status = mgmt_bredr_support(hdev); |
| 1996 | if (status) |
| 1997 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1998 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1999 | if (!lmp_ssp_capable(hdev)) |
| 2000 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2001 | MGMT_STATUS_NOT_SUPPORTED); |
| 2002 | |
| 2003 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 2004 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2005 | MGMT_STATUS_REJECTED); |
| 2006 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2007 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2008 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2009 | MGMT_STATUS_INVALID_PARAMS); |
| 2010 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2011 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2012 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2013 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2014 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2015 | } else { |
| 2016 | if (hdev_is_powered(hdev)) { |
| 2017 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2018 | MGMT_STATUS_REJECTED); |
| 2019 | goto unlock; |
| 2020 | } |
| 2021 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2022 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2023 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2024 | |
| 2025 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 2026 | if (err < 0) |
| 2027 | goto unlock; |
| 2028 | |
| 2029 | if (changed) |
| 2030 | err = new_settings(hdev, sk); |
| 2031 | |
| 2032 | unlock: |
| 2033 | hci_dev_unlock(hdev); |
| 2034 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2035 | } |
| 2036 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2037 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 2038 | { |
| 2039 | struct cmd_lookup match = { NULL, hdev }; |
| 2040 | |
| 2041 | if (status) { |
| 2042 | u8 mgmt_err = mgmt_status(status); |
| 2043 | |
| 2044 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 2045 | &mgmt_err); |
| 2046 | return; |
| 2047 | } |
| 2048 | |
| 2049 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 2050 | |
| 2051 | new_settings(hdev, match.sk); |
| 2052 | |
| 2053 | if (match.sk) |
| 2054 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2055 | |
| 2056 | /* Make sure the controller has a good default for |
| 2057 | * advertising data. Restrict the update to when LE |
| 2058 | * has actually been enabled. During power on, the |
| 2059 | * update in powered_update_hci will take care of it. |
| 2060 | */ |
| 2061 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2062 | struct hci_request req; |
| 2063 | |
| 2064 | hci_dev_lock(hdev); |
| 2065 | |
| 2066 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 2067 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 2068 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2069 | hci_req_run(&req, NULL); |
| 2070 | |
| 2071 | hci_dev_unlock(hdev); |
| 2072 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2073 | } |
| 2074 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2075 | 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] | 2076 | { |
| 2077 | struct mgmt_mode *cp = data; |
| 2078 | struct hci_cp_write_le_host_supported hci_cp; |
| 2079 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2080 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2081 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2082 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2083 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2084 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2085 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2086 | if (!lmp_le_capable(hdev)) |
| 2087 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2088 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2089 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2090 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2091 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2092 | MGMT_STATUS_INVALID_PARAMS); |
| 2093 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2094 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2095 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2096 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2097 | MGMT_STATUS_REJECTED); |
| 2098 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2099 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2100 | |
| 2101 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2102 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2103 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2104 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2105 | bool changed = false; |
| 2106 | |
| 2107 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2108 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 2109 | changed = true; |
| 2110 | } |
| 2111 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 2112 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 2113 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2114 | changed = true; |
| 2115 | } |
| 2116 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2117 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2118 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2119 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2120 | |
| 2121 | if (changed) |
| 2122 | err = new_settings(hdev, sk); |
| 2123 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2124 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2125 | } |
| 2126 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2127 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2128 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2129 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2130 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2131 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2135 | if (!cmd) { |
| 2136 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2137 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2138 | } |
| 2139 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2140 | hci_req_init(&req, hdev); |
| 2141 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2142 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2143 | |
| 2144 | if (val) { |
| 2145 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2146 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2147 | } else { |
| 2148 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 2149 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2150 | } |
| 2151 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2152 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2153 | &hci_cp); |
| 2154 | |
| 2155 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2156 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2157 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2158 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2159 | unlock: |
| 2160 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2161 | return err; |
| 2162 | } |
| 2163 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2164 | /* This is a helper function to test for pending mgmt commands that can |
| 2165 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2166 | * mgmt command at a time since otherwise we cannot easily track what |
| 2167 | * the current values are, will be, and based on that calculate if a new |
| 2168 | * HCI command needs to be sent and if yes with what value. |
| 2169 | */ |
| 2170 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2171 | { |
| 2172 | struct pending_cmd *cmd; |
| 2173 | |
| 2174 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2175 | switch (cmd->opcode) { |
| 2176 | case MGMT_OP_ADD_UUID: |
| 2177 | case MGMT_OP_REMOVE_UUID: |
| 2178 | case MGMT_OP_SET_DEV_CLASS: |
| 2179 | case MGMT_OP_SET_POWERED: |
| 2180 | return true; |
| 2181 | } |
| 2182 | } |
| 2183 | |
| 2184 | return false; |
| 2185 | } |
| 2186 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2187 | static const u8 bluetooth_base_uuid[] = { |
| 2188 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2189 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2190 | }; |
| 2191 | |
| 2192 | static u8 get_uuid_size(const u8 *uuid) |
| 2193 | { |
| 2194 | u32 val; |
| 2195 | |
| 2196 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2197 | return 128; |
| 2198 | |
| 2199 | val = get_unaligned_le32(&uuid[12]); |
| 2200 | if (val > 0xffff) |
| 2201 | return 32; |
| 2202 | |
| 2203 | return 16; |
| 2204 | } |
| 2205 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2206 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2207 | { |
| 2208 | struct pending_cmd *cmd; |
| 2209 | |
| 2210 | hci_dev_lock(hdev); |
| 2211 | |
| 2212 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2213 | if (!cmd) |
| 2214 | goto unlock; |
| 2215 | |
| 2216 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 2217 | hdev->dev_class, 3); |
| 2218 | |
| 2219 | mgmt_pending_remove(cmd); |
| 2220 | |
| 2221 | unlock: |
| 2222 | hci_dev_unlock(hdev); |
| 2223 | } |
| 2224 | |
| 2225 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2226 | { |
| 2227 | BT_DBG("status 0x%02x", status); |
| 2228 | |
| 2229 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2230 | } |
| 2231 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2232 | 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] | 2233 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2234 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2235 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2236 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2237 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2238 | int err; |
| 2239 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2240 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2241 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2242 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2243 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2244 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2245 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2246 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2247 | goto failed; |
| 2248 | } |
| 2249 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2250 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2251 | if (!uuid) { |
| 2252 | err = -ENOMEM; |
| 2253 | goto failed; |
| 2254 | } |
| 2255 | |
| 2256 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2257 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2258 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2259 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2260 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2261 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2262 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2263 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2264 | update_class(&req); |
| 2265 | update_eir(&req); |
| 2266 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2267 | err = hci_req_run(&req, add_uuid_complete); |
| 2268 | if (err < 0) { |
| 2269 | if (err != -ENODATA) |
| 2270 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2271 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2272 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2273 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2274 | goto failed; |
| 2275 | } |
| 2276 | |
| 2277 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2278 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2279 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2280 | goto failed; |
| 2281 | } |
| 2282 | |
| 2283 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2284 | |
| 2285 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2286 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2287 | return err; |
| 2288 | } |
| 2289 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2290 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2291 | { |
| 2292 | if (!hdev_is_powered(hdev)) |
| 2293 | return false; |
| 2294 | |
| 2295 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2296 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2297 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2298 | return true; |
| 2299 | } |
| 2300 | |
| 2301 | return false; |
| 2302 | } |
| 2303 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2304 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2305 | { |
| 2306 | BT_DBG("status 0x%02x", status); |
| 2307 | |
| 2308 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2309 | } |
| 2310 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2311 | 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] | 2312 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2313 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2314 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2315 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2316 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2317 | 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] | 2318 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2319 | int err, found; |
| 2320 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2321 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2322 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2323 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2324 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2325 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2326 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2327 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2328 | goto unlock; |
| 2329 | } |
| 2330 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2331 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2332 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2333 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2334 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2335 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2336 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2337 | goto unlock; |
| 2338 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2339 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2340 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2341 | } |
| 2342 | |
| 2343 | found = 0; |
| 2344 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2345 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2346 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2347 | continue; |
| 2348 | |
| 2349 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2350 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2351 | found++; |
| 2352 | } |
| 2353 | |
| 2354 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2355 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2356 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2357 | goto unlock; |
| 2358 | } |
| 2359 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2360 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2361 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2362 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2363 | update_class(&req); |
| 2364 | update_eir(&req); |
| 2365 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2366 | err = hci_req_run(&req, remove_uuid_complete); |
| 2367 | if (err < 0) { |
| 2368 | if (err != -ENODATA) |
| 2369 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2370 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2371 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2372 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2373 | goto unlock; |
| 2374 | } |
| 2375 | |
| 2376 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2377 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2378 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2379 | goto unlock; |
| 2380 | } |
| 2381 | |
| 2382 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2383 | |
| 2384 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2385 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2386 | return err; |
| 2387 | } |
| 2388 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2389 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2390 | { |
| 2391 | BT_DBG("status 0x%02x", status); |
| 2392 | |
| 2393 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2394 | } |
| 2395 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2396 | 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] | 2397 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2398 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2399 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2400 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2401 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2402 | int err; |
| 2403 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2404 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2405 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2406 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2407 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2408 | MGMT_STATUS_NOT_SUPPORTED); |
| 2409 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2410 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2411 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2412 | if (pending_eir_or_class(hdev)) { |
| 2413 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2414 | MGMT_STATUS_BUSY); |
| 2415 | goto unlock; |
| 2416 | } |
| 2417 | |
| 2418 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2419 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2420 | MGMT_STATUS_INVALID_PARAMS); |
| 2421 | goto unlock; |
| 2422 | } |
| 2423 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2424 | hdev->major_class = cp->major; |
| 2425 | hdev->minor_class = cp->minor; |
| 2426 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2427 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2428 | 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] | 2429 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2430 | goto unlock; |
| 2431 | } |
| 2432 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2433 | hci_req_init(&req, hdev); |
| 2434 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2435 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2436 | hci_dev_unlock(hdev); |
| 2437 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2438 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2439 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2440 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2441 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2442 | update_class(&req); |
| 2443 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2444 | err = hci_req_run(&req, set_class_complete); |
| 2445 | if (err < 0) { |
| 2446 | if (err != -ENODATA) |
| 2447 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2448 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2449 | 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] | 2450 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2451 | goto unlock; |
| 2452 | } |
| 2453 | |
| 2454 | 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] | 2455 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2456 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2457 | goto unlock; |
| 2458 | } |
| 2459 | |
| 2460 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2461 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2462 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2463 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2464 | return err; |
| 2465 | } |
| 2466 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2467 | 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] | 2468 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2469 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2470 | struct mgmt_cp_load_link_keys *cp = data; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2471 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2472 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2473 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2474 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2475 | BT_DBG("request for %s", hdev->name); |
| 2476 | |
| 2477 | if (!lmp_bredr_capable(hdev)) |
| 2478 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2479 | MGMT_STATUS_NOT_SUPPORTED); |
| 2480 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2481 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2482 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2483 | expected_len = sizeof(*cp) + key_count * |
| 2484 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2485 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2486 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2487 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2488 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2489 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2490 | } |
| 2491 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2492 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2493 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2494 | MGMT_STATUS_INVALID_PARAMS); |
| 2495 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2496 | 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] | 2497 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2498 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2499 | for (i = 0; i < key_count; i++) { |
| 2500 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2501 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2502 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2503 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2504 | MGMT_STATUS_INVALID_PARAMS); |
| 2505 | } |
| 2506 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2507 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2508 | |
| 2509 | hci_link_keys_clear(hdev); |
| 2510 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2511 | if (cp->debug_keys) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2512 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 2513 | &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2514 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2515 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 2516 | &hdev->dev_flags); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2517 | |
| 2518 | if (changed) |
| 2519 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2520 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2521 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2522 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2523 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2524 | /* Always ignore debug keys and require a new pairing if |
| 2525 | * the user wants to use them. |
| 2526 | */ |
| 2527 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2528 | continue; |
| 2529 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2530 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2531 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2532 | } |
| 2533 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2534 | 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] | 2535 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2536 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2537 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2538 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2539 | } |
| 2540 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2541 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2542 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2543 | { |
| 2544 | struct mgmt_ev_device_unpaired ev; |
| 2545 | |
| 2546 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2547 | ev.addr.type = addr_type; |
| 2548 | |
| 2549 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2550 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2551 | } |
| 2552 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2553 | 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] | 2554 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2555 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2556 | struct mgmt_cp_unpair_device *cp = data; |
| 2557 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2558 | struct hci_cp_disconnect dc; |
| 2559 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2560 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2561 | int err; |
| 2562 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2563 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2564 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2565 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2566 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2567 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2568 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2569 | MGMT_STATUS_INVALID_PARAMS, |
| 2570 | &rp, sizeof(rp)); |
| 2571 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2572 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2573 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2574 | MGMT_STATUS_INVALID_PARAMS, |
| 2575 | &rp, sizeof(rp)); |
| 2576 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2577 | hci_dev_lock(hdev); |
| 2578 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2579 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2580 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2581 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2582 | goto unlock; |
| 2583 | } |
| 2584 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2585 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2586 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2587 | } else { |
| 2588 | u8 addr_type; |
| 2589 | |
| 2590 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2591 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2592 | else |
| 2593 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2594 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2595 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2596 | |
Andre Guedes | a9b0a04 | 2014-02-26 20:21:52 -0300 | [diff] [blame] | 2597 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2598 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2599 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2600 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2601 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2602 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2603 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2604 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2605 | goto unlock; |
| 2606 | } |
| 2607 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2608 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2609 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2610 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2611 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2612 | else |
| 2613 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2614 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2615 | } else { |
| 2616 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2617 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2618 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2619 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2620 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2621 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2622 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2623 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2624 | } |
| 2625 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2626 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2627 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2628 | if (!cmd) { |
| 2629 | err = -ENOMEM; |
| 2630 | goto unlock; |
| 2631 | } |
| 2632 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2633 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2634 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2635 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2636 | if (err < 0) |
| 2637 | mgmt_pending_remove(cmd); |
| 2638 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2639 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2640 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2641 | return err; |
| 2642 | } |
| 2643 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2644 | 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] | 2645 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2646 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2647 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2648 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2649 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2650 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2651 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2652 | int err; |
| 2653 | |
| 2654 | BT_DBG(""); |
| 2655 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2656 | memset(&rp, 0, sizeof(rp)); |
| 2657 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2658 | rp.addr.type = cp->addr.type; |
| 2659 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2660 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2661 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2662 | MGMT_STATUS_INVALID_PARAMS, |
| 2663 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2664 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2665 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2666 | |
| 2667 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2668 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2669 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2670 | goto failed; |
| 2671 | } |
| 2672 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2673 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2674 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2675 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2676 | goto failed; |
| 2677 | } |
| 2678 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2679 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2680 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2681 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2682 | else |
| 2683 | 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] | 2684 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2685 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2686 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2687 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2688 | goto failed; |
| 2689 | } |
| 2690 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2691 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2692 | if (!cmd) { |
| 2693 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2694 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2695 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2696 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2697 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2698 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2699 | |
| 2700 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2701 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2702 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2703 | |
| 2704 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2705 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2706 | return err; |
| 2707 | } |
| 2708 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2709 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2710 | { |
| 2711 | switch (link_type) { |
| 2712 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2713 | switch (addr_type) { |
| 2714 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2715 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2716 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2717 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2718 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2719 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2720 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2721 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2722 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2723 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2724 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2725 | } |
| 2726 | } |
| 2727 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2728 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2729 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2730 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2731 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2732 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2733 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2734 | int err; |
| 2735 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2736 | |
| 2737 | BT_DBG(""); |
| 2738 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2739 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2740 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2741 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2742 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2743 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2744 | goto unlock; |
| 2745 | } |
| 2746 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2747 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2748 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2749 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2750 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2751 | } |
| 2752 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2753 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2754 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2755 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2756 | err = -ENOMEM; |
| 2757 | goto unlock; |
| 2758 | } |
| 2759 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2760 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2761 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2762 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2763 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2764 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2765 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2766 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2767 | continue; |
| 2768 | i++; |
| 2769 | } |
| 2770 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2771 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2772 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2773 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2774 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2775 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2776 | 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] | 2777 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2778 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2779 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2780 | |
| 2781 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2782 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2783 | return err; |
| 2784 | } |
| 2785 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2786 | 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] | 2787 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2788 | { |
| 2789 | struct pending_cmd *cmd; |
| 2790 | int err; |
| 2791 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2792 | 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] | 2793 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2794 | if (!cmd) |
| 2795 | return -ENOMEM; |
| 2796 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2797 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2798 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2799 | if (err < 0) |
| 2800 | mgmt_pending_remove(cmd); |
| 2801 | |
| 2802 | return err; |
| 2803 | } |
| 2804 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2805 | 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] | 2806 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2807 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2808 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2809 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2810 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2811 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2812 | int err; |
| 2813 | |
| 2814 | BT_DBG(""); |
| 2815 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2816 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2817 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2818 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2819 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2820 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2821 | goto failed; |
| 2822 | } |
| 2823 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2824 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2825 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2826 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2827 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2828 | goto failed; |
| 2829 | } |
| 2830 | |
| 2831 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2832 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2833 | |
| 2834 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2835 | |
| 2836 | BT_ERR("PIN code is not 16 bytes long"); |
| 2837 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2838 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2839 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2840 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2841 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2842 | |
| 2843 | goto failed; |
| 2844 | } |
| 2845 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2846 | 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] | 2847 | if (!cmd) { |
| 2848 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2849 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2850 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2851 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2852 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2853 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2854 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2855 | |
| 2856 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2857 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2858 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2859 | |
| 2860 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2861 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2862 | return err; |
| 2863 | } |
| 2864 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2865 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2866 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2867 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2868 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2869 | |
| 2870 | BT_DBG(""); |
| 2871 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2872 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
| 2873 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2874 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
| 2875 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2876 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2877 | |
| 2878 | hdev->io_capability = cp->io_capability; |
| 2879 | |
| 2880 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2881 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2882 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2883 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2884 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2885 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2886 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2887 | } |
| 2888 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2889 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2890 | { |
| 2891 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2892 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2893 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2894 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2895 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2896 | continue; |
| 2897 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2898 | if (cmd->user_data != conn) |
| 2899 | continue; |
| 2900 | |
| 2901 | return cmd; |
| 2902 | } |
| 2903 | |
| 2904 | return NULL; |
| 2905 | } |
| 2906 | |
| 2907 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 2908 | { |
| 2909 | struct mgmt_rp_pair_device rp; |
| 2910 | struct hci_conn *conn = cmd->user_data; |
| 2911 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2912 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2913 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2914 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 2915 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2916 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2917 | |
| 2918 | /* So we don't get further callbacks for this connection */ |
| 2919 | conn->connect_cfm_cb = NULL; |
| 2920 | conn->security_cfm_cb = NULL; |
| 2921 | conn->disconn_cfm_cb = NULL; |
| 2922 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2923 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2924 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2925 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2926 | } |
| 2927 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2928 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2929 | { |
| 2930 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 2931 | struct pending_cmd *cmd; |
| 2932 | |
| 2933 | cmd = find_pairing(conn); |
| 2934 | if (cmd) |
| 2935 | pairing_complete(cmd, status); |
| 2936 | } |
| 2937 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2938 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2939 | { |
| 2940 | struct pending_cmd *cmd; |
| 2941 | |
| 2942 | BT_DBG("status %u", status); |
| 2943 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2944 | cmd = find_pairing(conn); |
| 2945 | if (!cmd) |
| 2946 | BT_DBG("Unable to find a pending command"); |
| 2947 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 2948 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2949 | } |
| 2950 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2951 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2952 | { |
| 2953 | struct pending_cmd *cmd; |
| 2954 | |
| 2955 | BT_DBG("status %u", status); |
| 2956 | |
| 2957 | if (!status) |
| 2958 | return; |
| 2959 | |
| 2960 | cmd = find_pairing(conn); |
| 2961 | if (!cmd) |
| 2962 | BT_DBG("Unable to find a pending command"); |
| 2963 | else |
| 2964 | pairing_complete(cmd, mgmt_status(status)); |
| 2965 | } |
| 2966 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2967 | 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] | 2968 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2969 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2970 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2971 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2972 | struct pending_cmd *cmd; |
| 2973 | u8 sec_level, auth_type; |
| 2974 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2975 | int err; |
| 2976 | |
| 2977 | BT_DBG(""); |
| 2978 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2979 | memset(&rp, 0, sizeof(rp)); |
| 2980 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2981 | rp.addr.type = cp->addr.type; |
| 2982 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2983 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2984 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2985 | MGMT_STATUS_INVALID_PARAMS, |
| 2986 | &rp, sizeof(rp)); |
| 2987 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2988 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
| 2989 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2990 | MGMT_STATUS_INVALID_PARAMS, |
| 2991 | &rp, sizeof(rp)); |
| 2992 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2993 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2994 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2995 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2996 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2997 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2998 | goto unlock; |
| 2999 | } |
| 3000 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 3001 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 3002 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3003 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3004 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 3005 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 3006 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3007 | } else { |
| 3008 | u8 addr_type; |
| 3009 | |
| 3010 | /* Convert from L2CAP channel address type to HCI address type |
| 3011 | */ |
| 3012 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 3013 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 3014 | else |
| 3015 | addr_type = ADDR_LE_DEV_RANDOM; |
| 3016 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 3017 | /* When pairing a new device, it is expected to remember |
| 3018 | * this device for future connections. Adding the connection |
| 3019 | * parameter information ahead of time allows tracking |
| 3020 | * of the slave preferred values and will speed up any |
| 3021 | * further connection establishment. |
| 3022 | * |
| 3023 | * If connection parameters already exist, then they |
| 3024 | * will be kept and this function does nothing. |
| 3025 | */ |
| 3026 | hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 3027 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3028 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 3029 | sec_level, auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3030 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3031 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 3032 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3033 | int status; |
| 3034 | |
| 3035 | if (PTR_ERR(conn) == -EBUSY) |
| 3036 | status = MGMT_STATUS_BUSY; |
| 3037 | else |
| 3038 | status = MGMT_STATUS_CONNECT_FAILED; |
| 3039 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3040 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3041 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3042 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3043 | goto unlock; |
| 3044 | } |
| 3045 | |
| 3046 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3047 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3048 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3049 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3050 | goto unlock; |
| 3051 | } |
| 3052 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3053 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3054 | if (!cmd) { |
| 3055 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3056 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3057 | goto unlock; |
| 3058 | } |
| 3059 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3060 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3061 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3062 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3063 | conn->security_cfm_cb = pairing_complete_cb; |
| 3064 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3065 | } else { |
| 3066 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3067 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3068 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3069 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3070 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3071 | conn->io_capability = cp->io_cap; |
| 3072 | cmd->user_data = conn; |
| 3073 | |
| 3074 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3075 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3076 | pairing_complete(cmd, 0); |
| 3077 | |
| 3078 | err = 0; |
| 3079 | |
| 3080 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3081 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3082 | return err; |
| 3083 | } |
| 3084 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3085 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3086 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3087 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3088 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3089 | struct pending_cmd *cmd; |
| 3090 | struct hci_conn *conn; |
| 3091 | int err; |
| 3092 | |
| 3093 | BT_DBG(""); |
| 3094 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3095 | hci_dev_lock(hdev); |
| 3096 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3097 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3098 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3099 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3100 | goto unlock; |
| 3101 | } |
| 3102 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3103 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3104 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3105 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3106 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3107 | goto unlock; |
| 3108 | } |
| 3109 | |
| 3110 | conn = cmd->user_data; |
| 3111 | |
| 3112 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3113 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3114 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3115 | goto unlock; |
| 3116 | } |
| 3117 | |
| 3118 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3119 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3120 | 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] | 3121 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3122 | unlock: |
| 3123 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3124 | return err; |
| 3125 | } |
| 3126 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3127 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3128 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3129 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3130 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3131 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3132 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3133 | int err; |
| 3134 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3135 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3136 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3137 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3138 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3139 | MGMT_STATUS_NOT_POWERED, addr, |
| 3140 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3141 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3142 | } |
| 3143 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3144 | if (addr->type == BDADDR_BREDR) |
| 3145 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3146 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3147 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3148 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3149 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3150 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3151 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3152 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3153 | goto done; |
| 3154 | } |
| 3155 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3156 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3157 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3158 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3159 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3160 | MGMT_STATUS_SUCCESS, addr, |
| 3161 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3162 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3163 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3164 | MGMT_STATUS_FAILED, addr, |
| 3165 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3166 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3167 | goto done; |
| 3168 | } |
| 3169 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3170 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3171 | if (!cmd) { |
| 3172 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3173 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3174 | } |
| 3175 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3176 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3177 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3178 | struct hci_cp_user_passkey_reply cp; |
| 3179 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3180 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3181 | cp.passkey = passkey; |
| 3182 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3183 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3184 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3185 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3186 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3187 | if (err < 0) |
| 3188 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3189 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3190 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3191 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3192 | return err; |
| 3193 | } |
| 3194 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3195 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3196 | void *data, u16 len) |
| 3197 | { |
| 3198 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3199 | |
| 3200 | BT_DBG(""); |
| 3201 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3202 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3203 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3204 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3205 | } |
| 3206 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3207 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3208 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3209 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3210 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3211 | |
| 3212 | BT_DBG(""); |
| 3213 | |
| 3214 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3215 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3216 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3217 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3218 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3219 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3220 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3221 | } |
| 3222 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3223 | 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] | 3224 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3225 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3226 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3227 | |
| 3228 | BT_DBG(""); |
| 3229 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3230 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3231 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3232 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3233 | } |
| 3234 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3235 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3236 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3237 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3238 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3239 | |
| 3240 | BT_DBG(""); |
| 3241 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3242 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3243 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3244 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3245 | } |
| 3246 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3247 | 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] | 3248 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3249 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3250 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3251 | |
| 3252 | BT_DBG(""); |
| 3253 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3254 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3255 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3256 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3257 | } |
| 3258 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3259 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3260 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3261 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3262 | struct hci_cp_write_local_name cp; |
| 3263 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3264 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3265 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3266 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3267 | } |
| 3268 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3269 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3270 | { |
| 3271 | struct mgmt_cp_set_local_name *cp; |
| 3272 | struct pending_cmd *cmd; |
| 3273 | |
| 3274 | BT_DBG("status 0x%02x", status); |
| 3275 | |
| 3276 | hci_dev_lock(hdev); |
| 3277 | |
| 3278 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3279 | if (!cmd) |
| 3280 | goto unlock; |
| 3281 | |
| 3282 | cp = cmd->param; |
| 3283 | |
| 3284 | if (status) |
| 3285 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3286 | mgmt_status(status)); |
| 3287 | else |
| 3288 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3289 | cp, sizeof(*cp)); |
| 3290 | |
| 3291 | mgmt_pending_remove(cmd); |
| 3292 | |
| 3293 | unlock: |
| 3294 | hci_dev_unlock(hdev); |
| 3295 | } |
| 3296 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3297 | 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] | 3298 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3299 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3300 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3301 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3302 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3303 | int err; |
| 3304 | |
| 3305 | BT_DBG(""); |
| 3306 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3307 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3308 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3309 | /* If the old values are the same as the new ones just return a |
| 3310 | * direct command complete event. |
| 3311 | */ |
| 3312 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3313 | !memcmp(hdev->short_name, cp->short_name, |
| 3314 | sizeof(hdev->short_name))) { |
| 3315 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3316 | data, len); |
| 3317 | goto failed; |
| 3318 | } |
| 3319 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3320 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3321 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3322 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3323 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3324 | |
| 3325 | 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] | 3326 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3327 | if (err < 0) |
| 3328 | goto failed; |
| 3329 | |
| 3330 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3331 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3332 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3333 | goto failed; |
| 3334 | } |
| 3335 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3336 | 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] | 3337 | if (!cmd) { |
| 3338 | err = -ENOMEM; |
| 3339 | goto failed; |
| 3340 | } |
| 3341 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3342 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3343 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3344 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3345 | |
| 3346 | if (lmp_bredr_capable(hdev)) { |
| 3347 | update_name(&req); |
| 3348 | update_eir(&req); |
| 3349 | } |
| 3350 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3351 | /* The name is stored in the scan response data and so |
| 3352 | * no need to udpate the advertising data here. |
| 3353 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3354 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3355 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3356 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3357 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3358 | if (err < 0) |
| 3359 | mgmt_pending_remove(cmd); |
| 3360 | |
| 3361 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3362 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3363 | return err; |
| 3364 | } |
| 3365 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3366 | 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] | 3367 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3368 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3369 | struct pending_cmd *cmd; |
| 3370 | int err; |
| 3371 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3372 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3373 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3374 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3375 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3376 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3377 | 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] | 3378 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3379 | goto unlock; |
| 3380 | } |
| 3381 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3382 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3383 | 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] | 3384 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3385 | goto unlock; |
| 3386 | } |
| 3387 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3388 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3389 | 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] | 3390 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3391 | goto unlock; |
| 3392 | } |
| 3393 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3394 | 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] | 3395 | if (!cmd) { |
| 3396 | err = -ENOMEM; |
| 3397 | goto unlock; |
| 3398 | } |
| 3399 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3400 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3401 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3402 | 0, NULL); |
| 3403 | else |
| 3404 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3405 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3406 | if (err < 0) |
| 3407 | mgmt_pending_remove(cmd); |
| 3408 | |
| 3409 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3410 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3411 | return err; |
| 3412 | } |
| 3413 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3414 | 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] | 3415 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3416 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3417 | int err; |
| 3418 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3419 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3420 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3421 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3422 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3423 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3424 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3425 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3426 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3427 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3428 | cp->hash, cp->randomizer); |
| 3429 | if (err < 0) |
| 3430 | status = MGMT_STATUS_FAILED; |
| 3431 | else |
| 3432 | status = MGMT_STATUS_SUCCESS; |
| 3433 | |
| 3434 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3435 | status, &cp->addr, sizeof(cp->addr)); |
| 3436 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3437 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3438 | u8 status; |
| 3439 | |
| 3440 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3441 | cp->hash192, |
| 3442 | cp->randomizer192, |
| 3443 | cp->hash256, |
| 3444 | cp->randomizer256); |
| 3445 | if (err < 0) |
| 3446 | status = MGMT_STATUS_FAILED; |
| 3447 | else |
| 3448 | status = MGMT_STATUS_SUCCESS; |
| 3449 | |
| 3450 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3451 | status, &cp->addr, sizeof(cp->addr)); |
| 3452 | } else { |
| 3453 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3454 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3455 | MGMT_STATUS_INVALID_PARAMS); |
| 3456 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3457 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3458 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3459 | return err; |
| 3460 | } |
| 3461 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3462 | 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] | 3463 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3464 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3465 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3466 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3467 | int err; |
| 3468 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3469 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3470 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3471 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3472 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3473 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3474 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3475 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3476 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3477 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3478 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3479 | 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] | 3480 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3481 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3482 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3483 | return err; |
| 3484 | } |
| 3485 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3486 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3487 | { |
| 3488 | struct pending_cmd *cmd; |
| 3489 | u8 type; |
| 3490 | int err; |
| 3491 | |
| 3492 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3493 | |
| 3494 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3495 | if (!cmd) |
| 3496 | return -ENOENT; |
| 3497 | |
| 3498 | type = hdev->discovery.type; |
| 3499 | |
| 3500 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3501 | &type, sizeof(type)); |
| 3502 | mgmt_pending_remove(cmd); |
| 3503 | |
| 3504 | return err; |
| 3505 | } |
| 3506 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3507 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3508 | { |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3509 | unsigned long timeout = 0; |
| 3510 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3511 | BT_DBG("status %d", status); |
| 3512 | |
| 3513 | if (status) { |
| 3514 | hci_dev_lock(hdev); |
| 3515 | mgmt_start_discovery_failed(hdev, status); |
| 3516 | hci_dev_unlock(hdev); |
| 3517 | return; |
| 3518 | } |
| 3519 | |
| 3520 | hci_dev_lock(hdev); |
| 3521 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3522 | hci_dev_unlock(hdev); |
| 3523 | |
| 3524 | switch (hdev->discovery.type) { |
| 3525 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3526 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3527 | break; |
| 3528 | |
| 3529 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3530 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3531 | break; |
| 3532 | |
| 3533 | case DISCOV_TYPE_BREDR: |
| 3534 | break; |
| 3535 | |
| 3536 | default: |
| 3537 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3538 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3539 | |
| 3540 | if (!timeout) |
| 3541 | return; |
| 3542 | |
| 3543 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3544 | } |
| 3545 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3546 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3547 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3548 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3549 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3550 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3551 | struct hci_cp_le_set_scan_param param_cp; |
| 3552 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3553 | struct hci_cp_inquiry inq_cp; |
| 3554 | struct hci_request req; |
| 3555 | /* General inquiry access code (GIAC) */ |
| 3556 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3557 | u8 status, own_addr_type; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3558 | int err; |
| 3559 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3560 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3561 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3562 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3563 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3564 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3565 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3566 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3567 | goto failed; |
| 3568 | } |
| 3569 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3570 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3571 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3572 | MGMT_STATUS_BUSY); |
| 3573 | goto failed; |
| 3574 | } |
| 3575 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3576 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3577 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3578 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3579 | goto failed; |
| 3580 | } |
| 3581 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3582 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3583 | if (!cmd) { |
| 3584 | err = -ENOMEM; |
| 3585 | goto failed; |
| 3586 | } |
| 3587 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3588 | hdev->discovery.type = cp->type; |
| 3589 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3590 | hci_req_init(&req, hdev); |
| 3591 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3592 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3593 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3594 | status = mgmt_bredr_support(hdev); |
| 3595 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3596 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3597 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3598 | mgmt_pending_remove(cmd); |
| 3599 | goto failed; |
| 3600 | } |
| 3601 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3602 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3603 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3604 | MGMT_STATUS_BUSY); |
| 3605 | mgmt_pending_remove(cmd); |
| 3606 | goto failed; |
| 3607 | } |
| 3608 | |
| 3609 | hci_inquiry_cache_flush(hdev); |
| 3610 | |
| 3611 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3612 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3613 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3614 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3615 | break; |
| 3616 | |
| 3617 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3618 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3619 | status = mgmt_le_support(hdev); |
| 3620 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3621 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3622 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3623 | mgmt_pending_remove(cmd); |
| 3624 | goto failed; |
| 3625 | } |
| 3626 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3627 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3628 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3629 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3630 | MGMT_STATUS_NOT_SUPPORTED); |
| 3631 | mgmt_pending_remove(cmd); |
| 3632 | goto failed; |
| 3633 | } |
| 3634 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3635 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3636 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3637 | MGMT_STATUS_REJECTED); |
| 3638 | mgmt_pending_remove(cmd); |
| 3639 | goto failed; |
| 3640 | } |
| 3641 | |
Andre Guedes | c54c386 | 2014-02-26 20:21:50 -0300 | [diff] [blame] | 3642 | /* If controller is scanning, it means the background scanning |
| 3643 | * is running. Thus, we should temporarily stop it in order to |
| 3644 | * set the discovery scanning parameters. |
| 3645 | */ |
| 3646 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3647 | hci_req_add_le_scan_disable(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3648 | |
| 3649 | memset(¶m_cp, 0, sizeof(param_cp)); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3650 | |
Marcel Holtmann | 94b1fc9 | 2014-02-23 20:25:54 -0800 | [diff] [blame] | 3651 | /* All active scans will be done with either a resolvable |
| 3652 | * private address (when privacy feature has been enabled) |
| 3653 | * or unresolvable private address. |
| 3654 | */ |
| 3655 | err = hci_update_random_address(&req, true, &own_addr_type); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3656 | if (err < 0) { |
| 3657 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3658 | MGMT_STATUS_FAILED); |
| 3659 | mgmt_pending_remove(cmd); |
| 3660 | goto failed; |
| 3661 | } |
| 3662 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3663 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3664 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3665 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3666 | param_cp.own_address_type = own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3667 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3668 | ¶m_cp); |
| 3669 | |
| 3670 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3671 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3672 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3673 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3674 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3675 | break; |
| 3676 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3677 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3678 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3679 | MGMT_STATUS_INVALID_PARAMS); |
| 3680 | mgmt_pending_remove(cmd); |
| 3681 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3682 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3683 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3684 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3685 | if (err < 0) |
| 3686 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3687 | else |
| 3688 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3689 | |
| 3690 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3691 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3692 | return err; |
| 3693 | } |
| 3694 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3695 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3696 | { |
| 3697 | struct pending_cmd *cmd; |
| 3698 | int err; |
| 3699 | |
| 3700 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3701 | if (!cmd) |
| 3702 | return -ENOENT; |
| 3703 | |
| 3704 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3705 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3706 | mgmt_pending_remove(cmd); |
| 3707 | |
| 3708 | return err; |
| 3709 | } |
| 3710 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3711 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3712 | { |
| 3713 | BT_DBG("status %d", status); |
| 3714 | |
| 3715 | hci_dev_lock(hdev); |
| 3716 | |
| 3717 | if (status) { |
| 3718 | mgmt_stop_discovery_failed(hdev, status); |
| 3719 | goto unlock; |
| 3720 | } |
| 3721 | |
| 3722 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3723 | |
| 3724 | unlock: |
| 3725 | hci_dev_unlock(hdev); |
| 3726 | } |
| 3727 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3728 | 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] | 3729 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3730 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3731 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3732 | struct pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3733 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3734 | int err; |
| 3735 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3736 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3737 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3738 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3739 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3740 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3741 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3742 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3743 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3744 | goto unlock; |
| 3745 | } |
| 3746 | |
| 3747 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3748 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3749 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3750 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3751 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3752 | } |
| 3753 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3754 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3755 | if (!cmd) { |
| 3756 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3757 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3758 | } |
| 3759 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3760 | hci_req_init(&req, hdev); |
| 3761 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3762 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3763 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3764 | err = hci_req_run(&req, stop_discovery_complete); |
| 3765 | if (!err) { |
| 3766 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3767 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3768 | } |
| 3769 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3770 | mgmt_pending_remove(cmd); |
| 3771 | |
| 3772 | /* If no HCI commands were sent we're done */ |
| 3773 | if (err == -ENODATA) { |
| 3774 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 3775 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 3776 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3777 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3778 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3779 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3780 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3781 | return err; |
| 3782 | } |
| 3783 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3784 | 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] | 3785 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3786 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3787 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3788 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3789 | int err; |
| 3790 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3791 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3792 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3793 | hci_dev_lock(hdev); |
| 3794 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3795 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3796 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3797 | MGMT_STATUS_FAILED, &cp->addr, |
| 3798 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3799 | goto failed; |
| 3800 | } |
| 3801 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3802 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3803 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3804 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3805 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3806 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3807 | goto failed; |
| 3808 | } |
| 3809 | |
| 3810 | if (cp->name_known) { |
| 3811 | e->name_state = NAME_KNOWN; |
| 3812 | list_del(&e->list); |
| 3813 | } else { |
| 3814 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3815 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3816 | } |
| 3817 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3818 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3819 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3820 | |
| 3821 | failed: |
| 3822 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3823 | return err; |
| 3824 | } |
| 3825 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3826 | 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] | 3827 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3828 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3829 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3830 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3831 | int err; |
| 3832 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3833 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3834 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3835 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3836 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3837 | MGMT_STATUS_INVALID_PARAMS, |
| 3838 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3839 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3840 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3841 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3842 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3843 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3844 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3845 | goto done; |
| 3846 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3847 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3848 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3849 | sk); |
| 3850 | status = MGMT_STATUS_SUCCESS; |
| 3851 | |
| 3852 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3853 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3854 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3855 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3856 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3857 | |
| 3858 | return err; |
| 3859 | } |
| 3860 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3861 | 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] | 3862 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3863 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3864 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3865 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3866 | int err; |
| 3867 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3868 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3869 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3870 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3871 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3872 | MGMT_STATUS_INVALID_PARAMS, |
| 3873 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3874 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3875 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3876 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3877 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3878 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3879 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3880 | goto done; |
| 3881 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3882 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3883 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3884 | sk); |
| 3885 | status = MGMT_STATUS_SUCCESS; |
| 3886 | |
| 3887 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3888 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3889 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3890 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3891 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3892 | |
| 3893 | return err; |
| 3894 | } |
| 3895 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3896 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3897 | u16 len) |
| 3898 | { |
| 3899 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3900 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3901 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3902 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3903 | |
| 3904 | BT_DBG("%s", hdev->name); |
| 3905 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3906 | source = __le16_to_cpu(cp->source); |
| 3907 | |
| 3908 | if (source > 0x0002) |
| 3909 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3910 | MGMT_STATUS_INVALID_PARAMS); |
| 3911 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3912 | hci_dev_lock(hdev); |
| 3913 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3914 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3915 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 3916 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 3917 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 3918 | |
| 3919 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 3920 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3921 | hci_req_init(&req, hdev); |
| 3922 | update_eir(&req); |
| 3923 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3924 | |
| 3925 | hci_dev_unlock(hdev); |
| 3926 | |
| 3927 | return err; |
| 3928 | } |
| 3929 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3930 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 3931 | { |
| 3932 | struct cmd_lookup match = { NULL, hdev }; |
| 3933 | |
| 3934 | if (status) { |
| 3935 | u8 mgmt_err = mgmt_status(status); |
| 3936 | |
| 3937 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 3938 | cmd_status_rsp, &mgmt_err); |
| 3939 | return; |
| 3940 | } |
| 3941 | |
| 3942 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 3943 | &match); |
| 3944 | |
| 3945 | new_settings(hdev, match.sk); |
| 3946 | |
| 3947 | if (match.sk) |
| 3948 | sock_put(match.sk); |
| 3949 | } |
| 3950 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 3951 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3952 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3953 | { |
| 3954 | struct mgmt_mode *cp = data; |
| 3955 | struct pending_cmd *cmd; |
| 3956 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3957 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3958 | int err; |
| 3959 | |
| 3960 | BT_DBG("request for %s", hdev->name); |
| 3961 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3962 | status = mgmt_le_support(hdev); |
| 3963 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3964 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3965 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3966 | |
| 3967 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3968 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3969 | MGMT_STATUS_INVALID_PARAMS); |
| 3970 | |
| 3971 | hci_dev_lock(hdev); |
| 3972 | |
| 3973 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3974 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3975 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 3976 | /* The following conditions are ones which mean that we should |
| 3977 | * not do any HCI communication but directly send a mgmt |
| 3978 | * response to user space (after toggling the flag if |
| 3979 | * necessary). |
| 3980 | */ |
| 3981 | if (!hdev_is_powered(hdev) || val == enabled || |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 3982 | hci_conn_num(hdev, LE_LINK) > 0) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3983 | bool changed = false; |
| 3984 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3985 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 3986 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3987 | changed = true; |
| 3988 | } |
| 3989 | |
| 3990 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 3991 | if (err < 0) |
| 3992 | goto unlock; |
| 3993 | |
| 3994 | if (changed) |
| 3995 | err = new_settings(hdev, sk); |
| 3996 | |
| 3997 | goto unlock; |
| 3998 | } |
| 3999 | |
| 4000 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 4001 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 4002 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4003 | MGMT_STATUS_BUSY); |
| 4004 | goto unlock; |
| 4005 | } |
| 4006 | |
| 4007 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 4008 | if (!cmd) { |
| 4009 | err = -ENOMEM; |
| 4010 | goto unlock; |
| 4011 | } |
| 4012 | |
| 4013 | hci_req_init(&req, hdev); |
| 4014 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 4015 | if (val) |
| 4016 | enable_advertising(&req); |
| 4017 | else |
| 4018 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4019 | |
| 4020 | err = hci_req_run(&req, set_advertising_complete); |
| 4021 | if (err < 0) |
| 4022 | mgmt_pending_remove(cmd); |
| 4023 | |
| 4024 | unlock: |
| 4025 | hci_dev_unlock(hdev); |
| 4026 | return err; |
| 4027 | } |
| 4028 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4029 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 4030 | void *data, u16 len) |
| 4031 | { |
| 4032 | struct mgmt_cp_set_static_address *cp = data; |
| 4033 | int err; |
| 4034 | |
| 4035 | BT_DBG("%s", hdev->name); |
| 4036 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4037 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4038 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4039 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4040 | |
| 4041 | if (hdev_is_powered(hdev)) |
| 4042 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4043 | MGMT_STATUS_REJECTED); |
| 4044 | |
| 4045 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 4046 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 4047 | return cmd_status(sk, hdev->id, |
| 4048 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4049 | MGMT_STATUS_INVALID_PARAMS); |
| 4050 | |
| 4051 | /* Two most significant bits shall be set */ |
| 4052 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 4053 | return cmd_status(sk, hdev->id, |
| 4054 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4055 | MGMT_STATUS_INVALID_PARAMS); |
| 4056 | } |
| 4057 | |
| 4058 | hci_dev_lock(hdev); |
| 4059 | |
| 4060 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 4061 | |
| 4062 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 4063 | |
| 4064 | hci_dev_unlock(hdev); |
| 4065 | |
| 4066 | return err; |
| 4067 | } |
| 4068 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4069 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4070 | void *data, u16 len) |
| 4071 | { |
| 4072 | struct mgmt_cp_set_scan_params *cp = data; |
| 4073 | __u16 interval, window; |
| 4074 | int err; |
| 4075 | |
| 4076 | BT_DBG("%s", hdev->name); |
| 4077 | |
| 4078 | if (!lmp_le_capable(hdev)) |
| 4079 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4080 | MGMT_STATUS_NOT_SUPPORTED); |
| 4081 | |
| 4082 | interval = __le16_to_cpu(cp->interval); |
| 4083 | |
| 4084 | if (interval < 0x0004 || interval > 0x4000) |
| 4085 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4086 | MGMT_STATUS_INVALID_PARAMS); |
| 4087 | |
| 4088 | window = __le16_to_cpu(cp->window); |
| 4089 | |
| 4090 | if (window < 0x0004 || window > 0x4000) |
| 4091 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4092 | MGMT_STATUS_INVALID_PARAMS); |
| 4093 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4094 | if (window > interval) |
| 4095 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4096 | MGMT_STATUS_INVALID_PARAMS); |
| 4097 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4098 | hci_dev_lock(hdev); |
| 4099 | |
| 4100 | hdev->le_scan_interval = interval; |
| 4101 | hdev->le_scan_window = window; |
| 4102 | |
| 4103 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 4104 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4105 | /* If background scan is running, restart it so new parameters are |
| 4106 | * loaded. |
| 4107 | */ |
| 4108 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4109 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4110 | struct hci_request req; |
| 4111 | |
| 4112 | hci_req_init(&req, hdev); |
| 4113 | |
| 4114 | hci_req_add_le_scan_disable(&req); |
| 4115 | hci_req_add_le_passive_scan(&req); |
| 4116 | |
| 4117 | hci_req_run(&req, NULL); |
| 4118 | } |
| 4119 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4120 | hci_dev_unlock(hdev); |
| 4121 | |
| 4122 | return err; |
| 4123 | } |
| 4124 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4125 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 4126 | { |
| 4127 | struct pending_cmd *cmd; |
| 4128 | |
| 4129 | BT_DBG("status 0x%02x", status); |
| 4130 | |
| 4131 | hci_dev_lock(hdev); |
| 4132 | |
| 4133 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4134 | if (!cmd) |
| 4135 | goto unlock; |
| 4136 | |
| 4137 | if (status) { |
| 4138 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4139 | mgmt_status(status)); |
| 4140 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4141 | struct mgmt_mode *cp = cmd->param; |
| 4142 | |
| 4143 | if (cp->val) |
| 4144 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4145 | else |
| 4146 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4147 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4148 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4149 | new_settings(hdev, cmd->sk); |
| 4150 | } |
| 4151 | |
| 4152 | mgmt_pending_remove(cmd); |
| 4153 | |
| 4154 | unlock: |
| 4155 | hci_dev_unlock(hdev); |
| 4156 | } |
| 4157 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4158 | 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] | 4159 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4160 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4161 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4162 | struct pending_cmd *cmd; |
| 4163 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4164 | int err; |
| 4165 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4166 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4167 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4168 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4169 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4170 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4171 | MGMT_STATUS_NOT_SUPPORTED); |
| 4172 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4173 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4174 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4175 | MGMT_STATUS_INVALID_PARAMS); |
| 4176 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4177 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4178 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4179 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4180 | |
| 4181 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4182 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4183 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4184 | |
| 4185 | hci_dev_lock(hdev); |
| 4186 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4187 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4188 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4189 | MGMT_STATUS_BUSY); |
| 4190 | goto unlock; |
| 4191 | } |
| 4192 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4193 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4194 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4195 | hdev); |
| 4196 | goto unlock; |
| 4197 | } |
| 4198 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4199 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4200 | data, len); |
| 4201 | if (!cmd) { |
| 4202 | err = -ENOMEM; |
| 4203 | goto unlock; |
| 4204 | } |
| 4205 | |
| 4206 | hci_req_init(&req, hdev); |
| 4207 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4208 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4209 | |
| 4210 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4211 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4212 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4213 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4214 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4215 | } |
| 4216 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4217 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4218 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4219 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4220 | return err; |
| 4221 | } |
| 4222 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 4223 | static void set_bredr_scan(struct hci_request *req) |
| 4224 | { |
| 4225 | struct hci_dev *hdev = req->hdev; |
| 4226 | u8 scan = 0; |
| 4227 | |
| 4228 | /* Ensure that fast connectable is disabled. This function will |
| 4229 | * not do anything if the page scan parameters are already what |
| 4230 | * they should be. |
| 4231 | */ |
| 4232 | write_fast_connectable(req, false); |
| 4233 | |
| 4234 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4235 | scan |= SCAN_PAGE; |
| 4236 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 4237 | scan |= SCAN_INQUIRY; |
| 4238 | |
| 4239 | if (scan) |
| 4240 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 4241 | } |
| 4242 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4243 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4244 | { |
| 4245 | struct pending_cmd *cmd; |
| 4246 | |
| 4247 | BT_DBG("status 0x%02x", status); |
| 4248 | |
| 4249 | hci_dev_lock(hdev); |
| 4250 | |
| 4251 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4252 | if (!cmd) |
| 4253 | goto unlock; |
| 4254 | |
| 4255 | if (status) { |
| 4256 | u8 mgmt_err = mgmt_status(status); |
| 4257 | |
| 4258 | /* We need to restore the flag if related HCI commands |
| 4259 | * failed. |
| 4260 | */ |
| 4261 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4262 | |
| 4263 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4264 | } else { |
| 4265 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4266 | new_settings(hdev, cmd->sk); |
| 4267 | } |
| 4268 | |
| 4269 | mgmt_pending_remove(cmd); |
| 4270 | |
| 4271 | unlock: |
| 4272 | hci_dev_unlock(hdev); |
| 4273 | } |
| 4274 | |
| 4275 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4276 | { |
| 4277 | struct mgmt_mode *cp = data; |
| 4278 | struct pending_cmd *cmd; |
| 4279 | struct hci_request req; |
| 4280 | int err; |
| 4281 | |
| 4282 | BT_DBG("request for %s", hdev->name); |
| 4283 | |
| 4284 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4285 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4286 | MGMT_STATUS_NOT_SUPPORTED); |
| 4287 | |
| 4288 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4289 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4290 | MGMT_STATUS_REJECTED); |
| 4291 | |
| 4292 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4293 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4294 | MGMT_STATUS_INVALID_PARAMS); |
| 4295 | |
| 4296 | hci_dev_lock(hdev); |
| 4297 | |
| 4298 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4299 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4300 | goto unlock; |
| 4301 | } |
| 4302 | |
| 4303 | if (!hdev_is_powered(hdev)) { |
| 4304 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4305 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4306 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4307 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4308 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4309 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4310 | } |
| 4311 | |
| 4312 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4313 | |
| 4314 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4315 | if (err < 0) |
| 4316 | goto unlock; |
| 4317 | |
| 4318 | err = new_settings(hdev, sk); |
| 4319 | goto unlock; |
| 4320 | } |
| 4321 | |
| 4322 | /* Reject disabling when powered on */ |
| 4323 | if (!cp->val) { |
| 4324 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4325 | MGMT_STATUS_REJECTED); |
| 4326 | goto unlock; |
| 4327 | } |
| 4328 | |
| 4329 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4330 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4331 | MGMT_STATUS_BUSY); |
| 4332 | goto unlock; |
| 4333 | } |
| 4334 | |
| 4335 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4336 | if (!cmd) { |
| 4337 | err = -ENOMEM; |
| 4338 | goto unlock; |
| 4339 | } |
| 4340 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4341 | /* 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] | 4342 | * generates the correct flags. |
| 4343 | */ |
| 4344 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4345 | |
| 4346 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4347 | |
| 4348 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4349 | set_bredr_scan(&req); |
| 4350 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4351 | /* Since only the advertising data flags will change, there |
| 4352 | * is no need to update the scan response data. |
| 4353 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4354 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4355 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4356 | err = hci_req_run(&req, set_bredr_complete); |
| 4357 | if (err < 0) |
| 4358 | mgmt_pending_remove(cmd); |
| 4359 | |
| 4360 | unlock: |
| 4361 | hci_dev_unlock(hdev); |
| 4362 | return err; |
| 4363 | } |
| 4364 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4365 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4366 | void *data, u16 len) |
| 4367 | { |
| 4368 | struct mgmt_mode *cp = data; |
| 4369 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4370 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4371 | int err; |
| 4372 | |
| 4373 | BT_DBG("request for %s", hdev->name); |
| 4374 | |
| 4375 | status = mgmt_bredr_support(hdev); |
| 4376 | if (status) |
| 4377 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4378 | status); |
| 4379 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4380 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 4381 | !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4382 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4383 | MGMT_STATUS_NOT_SUPPORTED); |
| 4384 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4385 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4386 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4387 | MGMT_STATUS_INVALID_PARAMS); |
| 4388 | |
| 4389 | hci_dev_lock(hdev); |
| 4390 | |
| 4391 | if (!hdev_is_powered(hdev)) { |
| 4392 | bool changed; |
| 4393 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4394 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4395 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4396 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4397 | if (cp->val == 0x02) |
| 4398 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4399 | else |
| 4400 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4401 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4402 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4403 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4404 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4405 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4406 | |
| 4407 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4408 | if (err < 0) |
| 4409 | goto failed; |
| 4410 | |
| 4411 | if (changed) |
| 4412 | err = new_settings(hdev, sk); |
| 4413 | |
| 4414 | goto failed; |
| 4415 | } |
| 4416 | |
| 4417 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4418 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4419 | MGMT_STATUS_BUSY); |
| 4420 | goto failed; |
| 4421 | } |
| 4422 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4423 | val = !!cp->val; |
| 4424 | |
| 4425 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4426 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4427 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4428 | goto failed; |
| 4429 | } |
| 4430 | |
| 4431 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4432 | if (!cmd) { |
| 4433 | err = -ENOMEM; |
| 4434 | goto failed; |
| 4435 | } |
| 4436 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4437 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4438 | if (err < 0) { |
| 4439 | mgmt_pending_remove(cmd); |
| 4440 | goto failed; |
| 4441 | } |
| 4442 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4443 | if (cp->val == 0x02) |
| 4444 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4445 | else |
| 4446 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4447 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4448 | failed: |
| 4449 | hci_dev_unlock(hdev); |
| 4450 | return err; |
| 4451 | } |
| 4452 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4453 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4454 | void *data, u16 len) |
| 4455 | { |
| 4456 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4457 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4458 | int err; |
| 4459 | |
| 4460 | BT_DBG("request for %s", hdev->name); |
| 4461 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4462 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4463 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4464 | MGMT_STATUS_INVALID_PARAMS); |
| 4465 | |
| 4466 | hci_dev_lock(hdev); |
| 4467 | |
| 4468 | if (cp->val) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4469 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 4470 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4471 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4472 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 4473 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4474 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4475 | if (cp->val == 0x02) |
| 4476 | use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, |
| 4477 | &hdev->dev_flags); |
| 4478 | else |
| 4479 | use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, |
| 4480 | &hdev->dev_flags); |
| 4481 | |
| 4482 | if (hdev_is_powered(hdev) && use_changed && |
| 4483 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 4484 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4485 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4486 | sizeof(mode), &mode); |
| 4487 | } |
| 4488 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4489 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4490 | if (err < 0) |
| 4491 | goto unlock; |
| 4492 | |
| 4493 | if (changed) |
| 4494 | err = new_settings(hdev, sk); |
| 4495 | |
| 4496 | unlock: |
| 4497 | hci_dev_unlock(hdev); |
| 4498 | return err; |
| 4499 | } |
| 4500 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4501 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4502 | u16 len) |
| 4503 | { |
| 4504 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4505 | bool changed; |
| 4506 | int err; |
| 4507 | |
| 4508 | BT_DBG("request for %s", hdev->name); |
| 4509 | |
| 4510 | if (!lmp_le_capable(hdev)) |
| 4511 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4512 | MGMT_STATUS_NOT_SUPPORTED); |
| 4513 | |
| 4514 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4515 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4516 | MGMT_STATUS_INVALID_PARAMS); |
| 4517 | |
| 4518 | if (hdev_is_powered(hdev)) |
| 4519 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4520 | MGMT_STATUS_REJECTED); |
| 4521 | |
| 4522 | hci_dev_lock(hdev); |
| 4523 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4524 | /* If user space supports this command it is also expected to |
| 4525 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4526 | */ |
| 4527 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4528 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4529 | if (cp->privacy) { |
| 4530 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4531 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4532 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4533 | } else { |
| 4534 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4535 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4536 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4537 | } |
| 4538 | |
| 4539 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4540 | if (err < 0) |
| 4541 | goto unlock; |
| 4542 | |
| 4543 | if (changed) |
| 4544 | err = new_settings(hdev, sk); |
| 4545 | |
| 4546 | unlock: |
| 4547 | hci_dev_unlock(hdev); |
| 4548 | return err; |
| 4549 | } |
| 4550 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4551 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4552 | { |
| 4553 | switch (irk->addr.type) { |
| 4554 | case BDADDR_LE_PUBLIC: |
| 4555 | return true; |
| 4556 | |
| 4557 | case BDADDR_LE_RANDOM: |
| 4558 | /* Two most significant bits shall be set */ |
| 4559 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4560 | return false; |
| 4561 | return true; |
| 4562 | } |
| 4563 | |
| 4564 | return false; |
| 4565 | } |
| 4566 | |
| 4567 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4568 | u16 len) |
| 4569 | { |
| 4570 | struct mgmt_cp_load_irks *cp = cp_data; |
| 4571 | u16 irk_count, expected_len; |
| 4572 | int i, err; |
| 4573 | |
| 4574 | BT_DBG("request for %s", hdev->name); |
| 4575 | |
| 4576 | if (!lmp_le_capable(hdev)) |
| 4577 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4578 | MGMT_STATUS_NOT_SUPPORTED); |
| 4579 | |
| 4580 | irk_count = __le16_to_cpu(cp->irk_count); |
| 4581 | |
| 4582 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4583 | if (expected_len != len) { |
| 4584 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4585 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4586 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4587 | MGMT_STATUS_INVALID_PARAMS); |
| 4588 | } |
| 4589 | |
| 4590 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4591 | |
| 4592 | for (i = 0; i < irk_count; i++) { |
| 4593 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4594 | |
| 4595 | if (!irk_is_valid(key)) |
| 4596 | return cmd_status(sk, hdev->id, |
| 4597 | MGMT_OP_LOAD_IRKS, |
| 4598 | MGMT_STATUS_INVALID_PARAMS); |
| 4599 | } |
| 4600 | |
| 4601 | hci_dev_lock(hdev); |
| 4602 | |
| 4603 | hci_smp_irks_clear(hdev); |
| 4604 | |
| 4605 | for (i = 0; i < irk_count; i++) { |
| 4606 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4607 | u8 addr_type; |
| 4608 | |
| 4609 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4610 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4611 | else |
| 4612 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4613 | |
| 4614 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4615 | BDADDR_ANY); |
| 4616 | } |
| 4617 | |
| 4618 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4619 | |
| 4620 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4621 | |
| 4622 | hci_dev_unlock(hdev); |
| 4623 | |
| 4624 | return err; |
| 4625 | } |
| 4626 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4627 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4628 | { |
| 4629 | if (key->master != 0x00 && key->master != 0x01) |
| 4630 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4631 | |
| 4632 | switch (key->addr.type) { |
| 4633 | case BDADDR_LE_PUBLIC: |
| 4634 | return true; |
| 4635 | |
| 4636 | case BDADDR_LE_RANDOM: |
| 4637 | /* Two most significant bits shall be set */ |
| 4638 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4639 | return false; |
| 4640 | return true; |
| 4641 | } |
| 4642 | |
| 4643 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4644 | } |
| 4645 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4646 | 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] | 4647 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4648 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4649 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
| 4650 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4651 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4652 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4653 | BT_DBG("request for %s", hdev->name); |
| 4654 | |
| 4655 | if (!lmp_le_capable(hdev)) |
| 4656 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4657 | MGMT_STATUS_NOT_SUPPORTED); |
| 4658 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4659 | key_count = __le16_to_cpu(cp->key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4660 | |
| 4661 | expected_len = sizeof(*cp) + key_count * |
| 4662 | sizeof(struct mgmt_ltk_info); |
| 4663 | if (expected_len != len) { |
| 4664 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4665 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4666 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4667 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4668 | } |
| 4669 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4670 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4671 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4672 | for (i = 0; i < key_count; i++) { |
| 4673 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4674 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4675 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4676 | return cmd_status(sk, hdev->id, |
| 4677 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4678 | MGMT_STATUS_INVALID_PARAMS); |
| 4679 | } |
| 4680 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4681 | hci_dev_lock(hdev); |
| 4682 | |
| 4683 | hci_smp_ltks_clear(hdev); |
| 4684 | |
| 4685 | for (i = 0; i < key_count; i++) { |
| 4686 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4687 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4688 | |
| 4689 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4690 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4691 | else |
| 4692 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4693 | |
| 4694 | if (key->master) |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4695 | type = SMP_LTK; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4696 | else |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4697 | type = SMP_LTK_SLAVE; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4698 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4699 | switch (key->type) { |
| 4700 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4701 | authenticated = 0x00; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4702 | break; |
| 4703 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4704 | authenticated = 0x01; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4705 | break; |
| 4706 | default: |
| 4707 | continue; |
| 4708 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4709 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4710 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4711 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4712 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4713 | } |
| 4714 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4715 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4716 | NULL, 0); |
| 4717 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4718 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4719 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4720 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4721 | } |
| 4722 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4723 | struct cmd_conn_lookup { |
| 4724 | struct hci_conn *conn; |
| 4725 | bool valid_tx_power; |
| 4726 | u8 mgmt_status; |
| 4727 | }; |
| 4728 | |
| 4729 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 4730 | { |
| 4731 | struct cmd_conn_lookup *match = data; |
| 4732 | struct mgmt_cp_get_conn_info *cp; |
| 4733 | struct mgmt_rp_get_conn_info rp; |
| 4734 | struct hci_conn *conn = cmd->user_data; |
| 4735 | |
| 4736 | if (conn != match->conn) |
| 4737 | return; |
| 4738 | |
| 4739 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 4740 | |
| 4741 | memset(&rp, 0, sizeof(rp)); |
| 4742 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4743 | rp.addr.type = cp->addr.type; |
| 4744 | |
| 4745 | if (!match->mgmt_status) { |
| 4746 | rp.rssi = conn->rssi; |
| 4747 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4748 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4749 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4750 | rp.max_tx_power = conn->max_tx_power; |
| 4751 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4752 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4753 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 4754 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4758 | match->mgmt_status, &rp, sizeof(rp)); |
| 4759 | |
| 4760 | hci_conn_drop(conn); |
| 4761 | |
| 4762 | mgmt_pending_remove(cmd); |
| 4763 | } |
| 4764 | |
| 4765 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 4766 | { |
| 4767 | struct hci_cp_read_rssi *cp; |
| 4768 | struct hci_conn *conn; |
| 4769 | struct cmd_conn_lookup match; |
| 4770 | u16 handle; |
| 4771 | |
| 4772 | BT_DBG("status 0x%02x", status); |
| 4773 | |
| 4774 | hci_dev_lock(hdev); |
| 4775 | |
| 4776 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4777 | * otherwise we assume it's not valid. At the moment we assume that |
| 4778 | * either both or none of current and max values are valid to keep code |
| 4779 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4780 | */ |
| 4781 | match.valid_tx_power = !status; |
| 4782 | |
| 4783 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4784 | * Level so we check which one was last sent to retrieve connection |
| 4785 | * handle. Both commands have handle as first parameter so it's safe to |
| 4786 | * cast data on the same command struct. |
| 4787 | * |
| 4788 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4789 | * In other case we simply override error to indicate success as we |
| 4790 | * already remembered if TX power value is actually valid. |
| 4791 | */ |
| 4792 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4793 | if (!cp) { |
| 4794 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 4795 | status = 0; |
| 4796 | } |
| 4797 | |
| 4798 | if (!cp) { |
| 4799 | BT_ERR("invalid sent_cmd in response"); |
| 4800 | goto unlock; |
| 4801 | } |
| 4802 | |
| 4803 | handle = __le16_to_cpu(cp->handle); |
| 4804 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4805 | if (!conn) { |
| 4806 | BT_ERR("unknown handle (%d) in response", handle); |
| 4807 | goto unlock; |
| 4808 | } |
| 4809 | |
| 4810 | match.conn = conn; |
| 4811 | match.mgmt_status = mgmt_status(status); |
| 4812 | |
| 4813 | /* Cache refresh is complete, now reply for mgmt request for given |
| 4814 | * connection only. |
| 4815 | */ |
| 4816 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 4817 | get_conn_info_complete, &match); |
| 4818 | |
| 4819 | unlock: |
| 4820 | hci_dev_unlock(hdev); |
| 4821 | } |
| 4822 | |
| 4823 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4824 | u16 len) |
| 4825 | { |
| 4826 | struct mgmt_cp_get_conn_info *cp = data; |
| 4827 | struct mgmt_rp_get_conn_info rp; |
| 4828 | struct hci_conn *conn; |
| 4829 | unsigned long conn_info_age; |
| 4830 | int err = 0; |
| 4831 | |
| 4832 | BT_DBG("%s", hdev->name); |
| 4833 | |
| 4834 | memset(&rp, 0, sizeof(rp)); |
| 4835 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4836 | rp.addr.type = cp->addr.type; |
| 4837 | |
| 4838 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 4839 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4840 | MGMT_STATUS_INVALID_PARAMS, |
| 4841 | &rp, sizeof(rp)); |
| 4842 | |
| 4843 | hci_dev_lock(hdev); |
| 4844 | |
| 4845 | if (!hdev_is_powered(hdev)) { |
| 4846 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4847 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4848 | goto unlock; |
| 4849 | } |
| 4850 | |
| 4851 | if (cp->addr.type == BDADDR_BREDR) |
| 4852 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4853 | &cp->addr.bdaddr); |
| 4854 | else |
| 4855 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 4856 | |
| 4857 | if (!conn || conn->state != BT_CONNECTED) { |
| 4858 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4859 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 4860 | goto unlock; |
| 4861 | } |
| 4862 | |
| 4863 | /* To avoid client trying to guess when to poll again for information we |
| 4864 | * calculate conn info age as random value between min/max set in hdev. |
| 4865 | */ |
| 4866 | conn_info_age = hdev->conn_info_min_age + |
| 4867 | prandom_u32_max(hdev->conn_info_max_age - |
| 4868 | hdev->conn_info_min_age); |
| 4869 | |
| 4870 | /* Query controller to refresh cached values if they are too old or were |
| 4871 | * never read. |
| 4872 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 4873 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 4874 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4875 | !conn->conn_info_timestamp) { |
| 4876 | struct hci_request req; |
| 4877 | struct hci_cp_read_tx_power req_txp_cp; |
| 4878 | struct hci_cp_read_rssi req_rssi_cp; |
| 4879 | struct pending_cmd *cmd; |
| 4880 | |
| 4881 | hci_req_init(&req, hdev); |
| 4882 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 4883 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 4884 | &req_rssi_cp); |
| 4885 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 4886 | /* For LE links TX power does not change thus we don't need to |
| 4887 | * query for it once value is known. |
| 4888 | */ |
| 4889 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 4890 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 4891 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4892 | req_txp_cp.type = 0x00; |
| 4893 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4894 | sizeof(req_txp_cp), &req_txp_cp); |
| 4895 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4896 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4897 | /* Max TX power needs to be read only once per connection */ |
| 4898 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 4899 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4900 | req_txp_cp.type = 0x01; |
| 4901 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4902 | sizeof(req_txp_cp), &req_txp_cp); |
| 4903 | } |
| 4904 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4905 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 4906 | if (err < 0) |
| 4907 | goto unlock; |
| 4908 | |
| 4909 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 4910 | data, len); |
| 4911 | if (!cmd) { |
| 4912 | err = -ENOMEM; |
| 4913 | goto unlock; |
| 4914 | } |
| 4915 | |
| 4916 | hci_conn_hold(conn); |
| 4917 | cmd->user_data = conn; |
| 4918 | |
| 4919 | conn->conn_info_timestamp = jiffies; |
| 4920 | } else { |
| 4921 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 4922 | rp.rssi = conn->rssi; |
| 4923 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4924 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4925 | |
| 4926 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4927 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 4928 | } |
| 4929 | |
| 4930 | unlock: |
| 4931 | hci_dev_unlock(hdev); |
| 4932 | return err; |
| 4933 | } |
| 4934 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 4935 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status) |
| 4936 | { |
| 4937 | struct mgmt_cp_get_clock_info *cp; |
| 4938 | struct mgmt_rp_get_clock_info rp; |
| 4939 | struct hci_cp_read_clock *hci_cp; |
| 4940 | struct pending_cmd *cmd; |
| 4941 | struct hci_conn *conn; |
| 4942 | |
| 4943 | BT_DBG("%s status %u", hdev->name, status); |
| 4944 | |
| 4945 | hci_dev_lock(hdev); |
| 4946 | |
| 4947 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 4948 | if (!hci_cp) |
| 4949 | goto unlock; |
| 4950 | |
| 4951 | if (hci_cp->which) { |
| 4952 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 4953 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4954 | } else { |
| 4955 | conn = NULL; |
| 4956 | } |
| 4957 | |
| 4958 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 4959 | if (!cmd) |
| 4960 | goto unlock; |
| 4961 | |
| 4962 | cp = cmd->param; |
| 4963 | |
| 4964 | memset(&rp, 0, sizeof(rp)); |
| 4965 | memcpy(&rp.addr, &cp->addr, sizeof(rp.addr)); |
| 4966 | |
| 4967 | if (status) |
| 4968 | goto send_rsp; |
| 4969 | |
| 4970 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 4971 | |
| 4972 | if (conn) { |
| 4973 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 4974 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 4975 | } |
| 4976 | |
| 4977 | send_rsp: |
| 4978 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 4979 | &rp, sizeof(rp)); |
| 4980 | mgmt_pending_remove(cmd); |
| 4981 | if (conn) |
| 4982 | hci_conn_drop(conn); |
| 4983 | |
| 4984 | unlock: |
| 4985 | hci_dev_unlock(hdev); |
| 4986 | } |
| 4987 | |
| 4988 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4989 | u16 len) |
| 4990 | { |
| 4991 | struct mgmt_cp_get_clock_info *cp = data; |
| 4992 | struct mgmt_rp_get_clock_info rp; |
| 4993 | struct hci_cp_read_clock hci_cp; |
| 4994 | struct pending_cmd *cmd; |
| 4995 | struct hci_request req; |
| 4996 | struct hci_conn *conn; |
| 4997 | int err; |
| 4998 | |
| 4999 | BT_DBG("%s", hdev->name); |
| 5000 | |
| 5001 | memset(&rp, 0, sizeof(rp)); |
| 5002 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5003 | rp.addr.type = cp->addr.type; |
| 5004 | |
| 5005 | if (cp->addr.type != BDADDR_BREDR) |
| 5006 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5007 | MGMT_STATUS_INVALID_PARAMS, |
| 5008 | &rp, sizeof(rp)); |
| 5009 | |
| 5010 | hci_dev_lock(hdev); |
| 5011 | |
| 5012 | if (!hdev_is_powered(hdev)) { |
| 5013 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5014 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 5015 | goto unlock; |
| 5016 | } |
| 5017 | |
| 5018 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5019 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5020 | &cp->addr.bdaddr); |
| 5021 | if (!conn || conn->state != BT_CONNECTED) { |
| 5022 | err = cmd_complete(sk, hdev->id, |
| 5023 | MGMT_OP_GET_CLOCK_INFO, |
| 5024 | MGMT_STATUS_NOT_CONNECTED, |
| 5025 | &rp, sizeof(rp)); |
| 5026 | goto unlock; |
| 5027 | } |
| 5028 | } else { |
| 5029 | conn = NULL; |
| 5030 | } |
| 5031 | |
| 5032 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 5033 | if (!cmd) { |
| 5034 | err = -ENOMEM; |
| 5035 | goto unlock; |
| 5036 | } |
| 5037 | |
| 5038 | hci_req_init(&req, hdev); |
| 5039 | |
| 5040 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 5041 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5042 | |
| 5043 | if (conn) { |
| 5044 | hci_conn_hold(conn); |
| 5045 | cmd->user_data = conn; |
| 5046 | |
| 5047 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 5048 | hci_cp.which = 0x01; /* Piconet clock */ |
| 5049 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5050 | } |
| 5051 | |
| 5052 | err = hci_req_run(&req, get_clock_info_complete); |
| 5053 | if (err < 0) |
| 5054 | mgmt_pending_remove(cmd); |
| 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_added(struct sock *sk, struct hci_dev *hdev, |
| 5062 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 5063 | { |
| 5064 | struct mgmt_ev_device_added ev; |
| 5065 | |
| 5066 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5067 | ev.addr.type = type; |
| 5068 | ev.action = action; |
| 5069 | |
| 5070 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5071 | } |
| 5072 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5073 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5074 | void *data, u16 len) |
| 5075 | { |
| 5076 | struct mgmt_cp_add_device *cp = data; |
| 5077 | u8 auto_conn, addr_type; |
| 5078 | int err; |
| 5079 | |
| 5080 | BT_DBG("%s", hdev->name); |
| 5081 | |
| 5082 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5083 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
| 5084 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5085 | MGMT_STATUS_INVALID_PARAMS, |
| 5086 | &cp->addr, sizeof(cp->addr)); |
| 5087 | |
| 5088 | if (cp->action != 0x00 && cp->action != 0x01) |
| 5089 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5090 | MGMT_STATUS_INVALID_PARAMS, |
| 5091 | &cp->addr, sizeof(cp->addr)); |
| 5092 | |
| 5093 | hci_dev_lock(hdev); |
| 5094 | |
| 5095 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5096 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5097 | else |
| 5098 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5099 | |
| 5100 | if (cp->action) |
| 5101 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
| 5102 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5103 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5104 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5105 | /* If the connection parameters don't exist for this device, |
| 5106 | * they will be created and configured with defaults. |
| 5107 | */ |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5108 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
| 5109 | auto_conn) < 0) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5110 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5111 | MGMT_STATUS_FAILED, |
| 5112 | &cp->addr, sizeof(cp->addr)); |
| 5113 | goto unlock; |
| 5114 | } |
| 5115 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5116 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5117 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5118 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5119 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5120 | |
| 5121 | unlock: |
| 5122 | hci_dev_unlock(hdev); |
| 5123 | return err; |
| 5124 | } |
| 5125 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5126 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5127 | bdaddr_t *bdaddr, u8 type) |
| 5128 | { |
| 5129 | struct mgmt_ev_device_removed ev; |
| 5130 | |
| 5131 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5132 | ev.addr.type = type; |
| 5133 | |
| 5134 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5135 | } |
| 5136 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5137 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5138 | void *data, u16 len) |
| 5139 | { |
| 5140 | struct mgmt_cp_remove_device *cp = data; |
| 5141 | int err; |
| 5142 | |
| 5143 | BT_DBG("%s", hdev->name); |
| 5144 | |
| 5145 | hci_dev_lock(hdev); |
| 5146 | |
| 5147 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5148 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5149 | u8 addr_type; |
| 5150 | |
| 5151 | if (!bdaddr_type_is_le(cp->addr.type)) { |
| 5152 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5153 | MGMT_STATUS_INVALID_PARAMS, |
| 5154 | &cp->addr, sizeof(cp->addr)); |
| 5155 | goto unlock; |
| 5156 | } |
| 5157 | |
| 5158 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5159 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5160 | else |
| 5161 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5162 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5163 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5164 | addr_type); |
| 5165 | if (!params) { |
| 5166 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5167 | MGMT_STATUS_INVALID_PARAMS, |
| 5168 | &cp->addr, sizeof(cp->addr)); |
| 5169 | goto unlock; |
| 5170 | } |
| 5171 | |
| 5172 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED) { |
| 5173 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5174 | MGMT_STATUS_INVALID_PARAMS, |
| 5175 | &cp->addr, sizeof(cp->addr)); |
| 5176 | goto unlock; |
| 5177 | } |
| 5178 | |
| 5179 | hci_pend_le_conn_del(hdev, &cp->addr.bdaddr, addr_type); |
| 5180 | list_del(¶ms->list); |
| 5181 | kfree(params); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5182 | |
| 5183 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5184 | } else { |
| 5185 | if (cp->addr.type) { |
| 5186 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5187 | MGMT_STATUS_INVALID_PARAMS, |
| 5188 | &cp->addr, sizeof(cp->addr)); |
| 5189 | goto unlock; |
| 5190 | } |
| 5191 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5192 | hci_conn_params_clear_enabled(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5193 | } |
| 5194 | |
| 5195 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5196 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5197 | |
| 5198 | unlock: |
| 5199 | hci_dev_unlock(hdev); |
| 5200 | return err; |
| 5201 | } |
| 5202 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5203 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5204 | u16 len) |
| 5205 | { |
| 5206 | struct mgmt_cp_load_conn_param *cp = data; |
| 5207 | u16 param_count, expected_len; |
| 5208 | int i; |
| 5209 | |
| 5210 | if (!lmp_le_capable(hdev)) |
| 5211 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5212 | MGMT_STATUS_NOT_SUPPORTED); |
| 5213 | |
| 5214 | param_count = __le16_to_cpu(cp->param_count); |
| 5215 | |
| 5216 | expected_len = sizeof(*cp) + param_count * |
| 5217 | sizeof(struct mgmt_conn_param); |
| 5218 | if (expected_len != len) { |
| 5219 | BT_ERR("load_conn_param: expected %u bytes, got %u bytes", |
| 5220 | expected_len, len); |
| 5221 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5222 | MGMT_STATUS_INVALID_PARAMS); |
| 5223 | } |
| 5224 | |
| 5225 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 5226 | |
| 5227 | hci_dev_lock(hdev); |
| 5228 | |
| 5229 | hci_conn_params_clear_disabled(hdev); |
| 5230 | |
| 5231 | for (i = 0; i < param_count; i++) { |
| 5232 | struct mgmt_conn_param *param = &cp->params[i]; |
| 5233 | struct hci_conn_params *hci_param; |
| 5234 | u16 min, max, latency, timeout; |
| 5235 | u8 addr_type; |
| 5236 | |
| 5237 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 5238 | param->addr.type); |
| 5239 | |
| 5240 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 5241 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5242 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 5243 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5244 | } else { |
| 5245 | BT_ERR("Ignoring invalid connection parameters"); |
| 5246 | continue; |
| 5247 | } |
| 5248 | |
| 5249 | min = le16_to_cpu(param->min_interval); |
| 5250 | max = le16_to_cpu(param->max_interval); |
| 5251 | latency = le16_to_cpu(param->latency); |
| 5252 | timeout = le16_to_cpu(param->timeout); |
| 5253 | |
| 5254 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 5255 | min, max, latency, timeout); |
| 5256 | |
| 5257 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
| 5258 | BT_ERR("Ignoring invalid connection parameters"); |
| 5259 | continue; |
| 5260 | } |
| 5261 | |
| 5262 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 5263 | addr_type); |
| 5264 | if (!hci_param) { |
| 5265 | BT_ERR("Failed to add connection parameters"); |
| 5266 | continue; |
| 5267 | } |
| 5268 | |
| 5269 | hci_param->conn_min_interval = min; |
| 5270 | hci_param->conn_max_interval = max; |
| 5271 | hci_param->conn_latency = latency; |
| 5272 | hci_param->supervision_timeout = timeout; |
| 5273 | } |
| 5274 | |
| 5275 | hci_dev_unlock(hdev); |
| 5276 | |
| 5277 | return cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, NULL, 0); |
| 5278 | } |
| 5279 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5280 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5281 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 5282 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5283 | bool var_len; |
| 5284 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5285 | } mgmt_handlers[] = { |
| 5286 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5287 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 5288 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 5289 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 5290 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 5291 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 5292 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 5293 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 5294 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 5295 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 5296 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 5297 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 5298 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 5299 | { set_le, false, MGMT_SETTING_SIZE }, |
| 5300 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 5301 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 5302 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 5303 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 5304 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 5305 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 5306 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 5307 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 5308 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 5309 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 5310 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 5311 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 5312 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 5313 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 5314 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 5315 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 5316 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 5317 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 5318 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5319 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5320 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 5321 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 5322 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 5323 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 5324 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 5325 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5326 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5327 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5328 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5329 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5330 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5331 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5332 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5333 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5334 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5335 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5336 | { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE }, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5337 | { add_device, false, MGMT_ADD_DEVICE_SIZE }, |
| 5338 | { remove_device, false, MGMT_REMOVE_DEVICE_SIZE }, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame^] | 5339 | { load_conn_param, true, MGMT_LOAD_CONN_PARAM_SIZE }, |
| 5340 | { read_unconf_index_list, false, MGMT_READ_UNCONF_INDEX_LIST_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5341 | }; |
| 5342 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5343 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 5344 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5345 | void *buf; |
| 5346 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5347 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 5348 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5349 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5350 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5351 | int err; |
| 5352 | |
| 5353 | BT_DBG("got %zu bytes", msglen); |
| 5354 | |
| 5355 | if (msglen < sizeof(*hdr)) |
| 5356 | return -EINVAL; |
| 5357 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 5358 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5359 | if (!buf) |
| 5360 | return -ENOMEM; |
| 5361 | |
| 5362 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 5363 | err = -EFAULT; |
| 5364 | goto done; |
| 5365 | } |
| 5366 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5367 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5368 | opcode = __le16_to_cpu(hdr->opcode); |
| 5369 | index = __le16_to_cpu(hdr->index); |
| 5370 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5371 | |
| 5372 | if (len != msglen - sizeof(*hdr)) { |
| 5373 | err = -EINVAL; |
| 5374 | goto done; |
| 5375 | } |
| 5376 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5377 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5378 | hdev = hci_dev_get(index); |
| 5379 | if (!hdev) { |
| 5380 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5381 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5382 | goto done; |
| 5383 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5384 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 5385 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 5386 | test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) || |
| 5387 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5388 | err = cmd_status(sk, index, opcode, |
| 5389 | MGMT_STATUS_INVALID_INDEX); |
| 5390 | goto done; |
| 5391 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5392 | } |
| 5393 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5394 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5395 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5396 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5397 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5398 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5399 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5400 | } |
| 5401 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame^] | 5402 | if (hdev && (opcode <= MGMT_OP_READ_INDEX_LIST || |
| 5403 | opcode == MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
| 5404 | err = cmd_status(sk, index, opcode, |
| 5405 | MGMT_STATUS_INVALID_INDEX); |
| 5406 | goto done; |
| 5407 | } |
| 5408 | |
| 5409 | if (!hdev && (opcode > MGMT_OP_READ_INDEX_LIST && |
| 5410 | opcode != MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5411 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5412 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5413 | goto done; |
| 5414 | } |
| 5415 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5416 | handler = &mgmt_handlers[opcode]; |
| 5417 | |
| 5418 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5419 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5420 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5421 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5422 | goto done; |
| 5423 | } |
| 5424 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5425 | if (hdev) |
| 5426 | mgmt_init_hdev(sk, hdev); |
| 5427 | |
| 5428 | cp = buf + sizeof(*hdr); |
| 5429 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5430 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 5431 | if (err < 0) |
| 5432 | goto done; |
| 5433 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5434 | err = msglen; |
| 5435 | |
| 5436 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5437 | if (hdev) |
| 5438 | hci_dev_put(hdev); |
| 5439 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5440 | kfree(buf); |
| 5441 | return err; |
| 5442 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5443 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5444 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5445 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5446 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5447 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5448 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5449 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5450 | return; |
| 5451 | |
| 5452 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5453 | mgmt_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, NULL, 0, NULL); |
| 5454 | else |
| 5455 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5456 | } |
| 5457 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5458 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5459 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 5460 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5461 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5462 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5463 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5464 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5465 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5466 | return; |
| 5467 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5468 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5469 | |
Marcel Holtmann | edd3896 | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 5470 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5471 | mgmt_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, NULL, 0, NULL); |
| 5472 | else |
| 5473 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5474 | } |
| 5475 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5476 | /* This function requires the caller holds hdev->lock */ |
| 5477 | static void restart_le_auto_conns(struct hci_dev *hdev) |
| 5478 | { |
| 5479 | struct hci_conn_params *p; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5480 | bool added = false; |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5481 | |
| 5482 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5483 | if (p->auto_connect == HCI_AUTO_CONN_ALWAYS) { |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5484 | hci_pend_le_conn_add(hdev, &p->addr, p->addr_type); |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5485 | added = true; |
| 5486 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5487 | } |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5488 | |
| 5489 | /* Calling hci_pend_le_conn_add will actually already trigger |
| 5490 | * background scanning when needed. So no need to trigger it |
| 5491 | * just another time. |
| 5492 | * |
| 5493 | * This check is here to avoid an unneeded restart of the |
| 5494 | * passive scanning. Since this is during the controller |
| 5495 | * power up phase the duplicate filtering is not an issue. |
| 5496 | */ |
| 5497 | if (added) |
| 5498 | return; |
| 5499 | |
| 5500 | hci_update_background_scan(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5501 | } |
| 5502 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5503 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 5504 | { |
| 5505 | struct cmd_lookup match = { NULL, hdev }; |
| 5506 | |
| 5507 | BT_DBG("status 0x%02x", status); |
| 5508 | |
| 5509 | hci_dev_lock(hdev); |
| 5510 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5511 | restart_le_auto_conns(hdev); |
| 5512 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5513 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5514 | |
| 5515 | new_settings(hdev, match.sk); |
| 5516 | |
| 5517 | hci_dev_unlock(hdev); |
| 5518 | |
| 5519 | if (match.sk) |
| 5520 | sock_put(match.sk); |
| 5521 | } |
| 5522 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5523 | static int powered_update_hci(struct hci_dev *hdev) |
| 5524 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5525 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5526 | u8 link_sec; |
| 5527 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5528 | hci_req_init(&req, hdev); |
| 5529 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5530 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 5531 | !lmp_host_ssp_capable(hdev)) { |
| 5532 | u8 ssp = 1; |
| 5533 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5534 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5535 | } |
| 5536 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 5537 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 5538 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5539 | struct hci_cp_write_le_host_supported cp; |
| 5540 | |
| 5541 | cp.le = 1; |
| 5542 | cp.simul = lmp_le_br_capable(hdev); |
| 5543 | |
| 5544 | /* Check first if we already have the right |
| 5545 | * host state (host features set) |
| 5546 | */ |
| 5547 | if (cp.le != lmp_host_le_capable(hdev) || |
| 5548 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5549 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 5550 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5551 | } |
| 5552 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5553 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5554 | /* Make sure the controller has a good default for |
| 5555 | * advertising data. This also applies to the case |
| 5556 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 5557 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5558 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 5559 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5560 | update_scan_rsp_data(&req); |
| 5561 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5562 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 5563 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5564 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 5565 | } |
| 5566 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5567 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 5568 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5569 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 5570 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5571 | |
| 5572 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5573 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 5574 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5575 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5576 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5577 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5578 | } |
| 5579 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5580 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5581 | } |
| 5582 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5583 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5584 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 5585 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5586 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 5587 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5588 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5589 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5590 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 5591 | return 0; |
| 5592 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5593 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5594 | if (powered_update_hci(hdev) == 0) |
| 5595 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 5596 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5597 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 5598 | &match); |
| 5599 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5600 | } |
| 5601 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5602 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5603 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 5604 | |
| 5605 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 5606 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 5607 | zero_cod, sizeof(zero_cod), NULL); |
| 5608 | |
| 5609 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5610 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5611 | |
| 5612 | if (match.sk) |
| 5613 | sock_put(match.sk); |
| 5614 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5615 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5616 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5617 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5618 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5619 | { |
| 5620 | struct pending_cmd *cmd; |
| 5621 | u8 status; |
| 5622 | |
| 5623 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5624 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5625 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5626 | |
| 5627 | if (err == -ERFKILL) |
| 5628 | status = MGMT_STATUS_RFKILLED; |
| 5629 | else |
| 5630 | status = MGMT_STATUS_FAILED; |
| 5631 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5632 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5633 | |
| 5634 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5635 | } |
| 5636 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5637 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 5638 | { |
| 5639 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5640 | |
| 5641 | hci_dev_lock(hdev); |
| 5642 | |
| 5643 | /* When discoverable timeout triggers, then just make sure |
| 5644 | * the limited discoverable flag is cleared. Even in the case |
| 5645 | * of a timeout triggered from general discoverable, it is |
| 5646 | * safe to unconditionally clear the flag. |
| 5647 | */ |
| 5648 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5649 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5650 | |
| 5651 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 5652 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 5653 | u8 scan = SCAN_PAGE; |
| 5654 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 5655 | sizeof(scan), &scan); |
| 5656 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5657 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5658 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5659 | hci_req_run(&req, NULL); |
| 5660 | |
| 5661 | hdev->discov_timeout = 0; |
| 5662 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5663 | new_settings(hdev, NULL); |
| 5664 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5665 | hci_dev_unlock(hdev); |
| 5666 | } |
| 5667 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5668 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5669 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5670 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5671 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5672 | /* Nothing needed here if there's a pending command since that |
| 5673 | * commands request completion callback takes care of everything |
| 5674 | * necessary. |
| 5675 | */ |
| 5676 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5677 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5678 | |
Johan Hedberg | bd10799 | 2014-02-24 14:52:19 +0200 | [diff] [blame] | 5679 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5680 | if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5681 | return; |
| 5682 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5683 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5684 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5685 | } else { |
| 5686 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5687 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5688 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5689 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5690 | if (changed) { |
| 5691 | struct hci_request req; |
| 5692 | |
| 5693 | /* In case this change in discoverable was triggered by |
| 5694 | * a disabling of connectable there could be a need to |
| 5695 | * update the advertising flags. |
| 5696 | */ |
| 5697 | hci_req_init(&req, hdev); |
| 5698 | update_adv_data(&req); |
| 5699 | hci_req_run(&req, NULL); |
| 5700 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5701 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5702 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5703 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5704 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5705 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5706 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5707 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5708 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5709 | /* Nothing needed here if there's a pending command since that |
| 5710 | * commands request completion callback takes care of everything |
| 5711 | * necessary. |
| 5712 | */ |
| 5713 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5714 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5715 | |
Johan Hedberg | ce3f24c | 2014-02-24 14:52:20 +0200 | [diff] [blame] | 5716 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5717 | if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5718 | return; |
| 5719 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5720 | if (connectable) |
| 5721 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 5722 | else |
| 5723 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5724 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5725 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5726 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5727 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5728 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5729 | void mgmt_advertising(struct hci_dev *hdev, u8 advertising) |
| 5730 | { |
Johan Hedberg | 7c4cfab | 2014-02-24 14:52:21 +0200 | [diff] [blame] | 5731 | /* Powering off may stop advertising - don't let that interfere */ |
| 5732 | if (!advertising && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5733 | return; |
| 5734 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5735 | if (advertising) |
| 5736 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5737 | else |
| 5738 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5739 | } |
| 5740 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 5741 | 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] | 5742 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5743 | u8 mgmt_err = mgmt_status(status); |
| 5744 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5745 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5746 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5747 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5748 | |
| 5749 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5750 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5751 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5752 | } |
| 5753 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5754 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 5755 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5756 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 5757 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5758 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5759 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5760 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5761 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 5762 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5763 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5764 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 5765 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5766 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5767 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5768 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5769 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5770 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5771 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 5772 | { |
| 5773 | if (ltk->authenticated) |
| 5774 | return MGMT_LTK_AUTHENTICATED; |
| 5775 | |
| 5776 | return MGMT_LTK_UNAUTHENTICATED; |
| 5777 | } |
| 5778 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5779 | 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] | 5780 | { |
| 5781 | struct mgmt_ev_new_long_term_key ev; |
| 5782 | |
| 5783 | memset(&ev, 0, sizeof(ev)); |
| 5784 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 5785 | /* Devices using resolvable or non-resolvable random addresses |
| 5786 | * without providing an indentity resolving key don't require |
| 5787 | * to store long term keys. Their addresses will change the |
| 5788 | * next time around. |
| 5789 | * |
| 5790 | * Only when a remote device provides an identity address |
| 5791 | * make sure the long term key is stored. If the remote |
| 5792 | * identity is known, the long term keys are internally |
| 5793 | * mapped to the identity address. So allow static random |
| 5794 | * and public addresses here. |
| 5795 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5796 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5797 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5798 | ev.store_hint = 0x00; |
| 5799 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5800 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5801 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5802 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5803 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5804 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5805 | ev.key.enc_size = key->enc_size; |
| 5806 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 5807 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5808 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 5809 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5810 | ev.key.master = 1; |
| 5811 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5812 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 5813 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 5814 | 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] | 5815 | } |
| 5816 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5817 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 5818 | { |
| 5819 | struct mgmt_ev_new_irk ev; |
| 5820 | |
| 5821 | memset(&ev, 0, sizeof(ev)); |
| 5822 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 5823 | /* For identity resolving keys from devices that are already |
| 5824 | * using a public address or static random address, do not |
| 5825 | * ask for storing this key. The identity resolving key really |
| 5826 | * is only mandatory for devices using resovlable random |
| 5827 | * addresses. |
| 5828 | * |
| 5829 | * Storing all identity resolving keys has the downside that |
| 5830 | * they will be also loaded on next boot of they system. More |
| 5831 | * identity resolving keys, means more time during scanning is |
| 5832 | * needed to actually resolve these addresses. |
| 5833 | */ |
| 5834 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 5835 | ev.store_hint = 0x01; |
| 5836 | else |
| 5837 | ev.store_hint = 0x00; |
| 5838 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5839 | bacpy(&ev.rpa, &irk->rpa); |
| 5840 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 5841 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 5842 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 5843 | |
| 5844 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 5845 | } |
| 5846 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5847 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 5848 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5849 | { |
| 5850 | struct mgmt_ev_new_csrk ev; |
| 5851 | |
| 5852 | memset(&ev, 0, sizeof(ev)); |
| 5853 | |
| 5854 | /* Devices using resolvable or non-resolvable random addresses |
| 5855 | * without providing an indentity resolving key don't require |
| 5856 | * to store signature resolving keys. Their addresses will change |
| 5857 | * the next time around. |
| 5858 | * |
| 5859 | * Only when a remote device provides an identity address |
| 5860 | * make sure the signature resolving key is stored. So allow |
| 5861 | * static random and public addresses here. |
| 5862 | */ |
| 5863 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5864 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5865 | ev.store_hint = 0x00; |
| 5866 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5867 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5868 | |
| 5869 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 5870 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 5871 | ev.key.master = csrk->master; |
| 5872 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 5873 | |
| 5874 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 5875 | } |
| 5876 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5877 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 5878 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 5879 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5880 | { |
| 5881 | struct mgmt_ev_new_conn_param ev; |
| 5882 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 5883 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 5884 | return; |
| 5885 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5886 | memset(&ev, 0, sizeof(ev)); |
| 5887 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5888 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 5889 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5890 | ev.min_interval = cpu_to_le16(min_interval); |
| 5891 | ev.max_interval = cpu_to_le16(max_interval); |
| 5892 | ev.latency = cpu_to_le16(latency); |
| 5893 | ev.timeout = cpu_to_le16(timeout); |
| 5894 | |
| 5895 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 5896 | } |
| 5897 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 5898 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 5899 | u8 data_len) |
| 5900 | { |
| 5901 | eir[eir_len++] = sizeof(type) + data_len; |
| 5902 | eir[eir_len++] = type; |
| 5903 | memcpy(&eir[eir_len], data, data_len); |
| 5904 | eir_len += data_len; |
| 5905 | |
| 5906 | return eir_len; |
| 5907 | } |
| 5908 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5909 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5910 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 5911 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5912 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5913 | char buf[512]; |
| 5914 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 5915 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5916 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5917 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5918 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5919 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 5920 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 5921 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5922 | if (name_len > 0) |
| 5923 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5924 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5925 | |
| 5926 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 5927 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5928 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5929 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5930 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5931 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5932 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 5933 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5934 | } |
| 5935 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5936 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 5937 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 5938 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5939 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 5940 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5941 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5942 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5943 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5944 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5945 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5946 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5947 | |
| 5948 | *sk = cmd->sk; |
| 5949 | sock_hold(*sk); |
| 5950 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5951 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5952 | } |
| 5953 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5954 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5955 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5956 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5957 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 5958 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5959 | |
| 5960 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5961 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5962 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5963 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5964 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 5965 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5966 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5967 | |
| 5968 | mgmt_pending_remove(cmd); |
| 5969 | } |
| 5970 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5971 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5972 | u8 link_type, u8 addr_type, u8 reason, |
| 5973 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5974 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5975 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5976 | struct pending_cmd *power_off; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5977 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5978 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5979 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5980 | if (power_off) { |
| 5981 | struct mgmt_mode *cp = power_off->param; |
| 5982 | |
| 5983 | /* The connection is still in hci_conn_hash so test for 1 |
| 5984 | * instead of 0 to know if this is the last one. |
| 5985 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5986 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5987 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5988 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5989 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5990 | } |
| 5991 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5992 | if (!mgmt_connected) |
| 5993 | return; |
| 5994 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 5995 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 5996 | return; |
| 5997 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5998 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5999 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6000 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6001 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6002 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6003 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6004 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6005 | |
| 6006 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 6007 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6008 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6009 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6010 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6011 | } |
| 6012 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6013 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6014 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6015 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6016 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 6017 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6018 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6019 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6020 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 6021 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 6022 | hdev); |
| 6023 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6024 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6025 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6026 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6027 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6028 | cp = cmd->param; |
| 6029 | |
| 6030 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 6031 | return; |
| 6032 | |
| 6033 | if (cp->addr.type != bdaddr_type) |
| 6034 | return; |
| 6035 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6036 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6037 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 6038 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6039 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 6040 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6041 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6042 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6043 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6044 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6045 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6046 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6047 | { |
| 6048 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6049 | struct pending_cmd *power_off; |
| 6050 | |
| 6051 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6052 | if (power_off) { |
| 6053 | struct mgmt_mode *cp = power_off->param; |
| 6054 | |
| 6055 | /* The connection is still in hci_conn_hash so test for 1 |
| 6056 | * instead of 0 to know if this is the last one. |
| 6057 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6058 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 6059 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6060 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6061 | } |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6062 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6063 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 6064 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6065 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6066 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6067 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6068 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6069 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6070 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6071 | 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] | 6072 | { |
| 6073 | struct mgmt_ev_pin_code_request ev; |
| 6074 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6075 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6076 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 6077 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6078 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6079 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6080 | } |
| 6081 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6082 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6083 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6084 | { |
| 6085 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6086 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6087 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6088 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6089 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6090 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6091 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6092 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6093 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6094 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6095 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 6096 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6097 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6098 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6099 | } |
| 6100 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6101 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6102 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6103 | { |
| 6104 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6105 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6106 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6107 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6108 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6109 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6110 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6111 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6112 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6113 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6114 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 6115 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6116 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6117 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6118 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6119 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6120 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6121 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6122 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6123 | { |
| 6124 | struct mgmt_ev_user_confirm_request ev; |
| 6125 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6126 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6127 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6128 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6129 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 6130 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6131 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6132 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6133 | 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] | 6134 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6135 | } |
| 6136 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6137 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 6138 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6139 | { |
| 6140 | struct mgmt_ev_user_passkey_request ev; |
| 6141 | |
| 6142 | BT_DBG("%s", hdev->name); |
| 6143 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6144 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6145 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6146 | |
| 6147 | 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] | 6148 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6149 | } |
| 6150 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 6151 | 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] | 6152 | u8 link_type, u8 addr_type, u8 status, |
| 6153 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6154 | { |
| 6155 | struct pending_cmd *cmd; |
| 6156 | struct mgmt_rp_user_confirm_reply rp; |
| 6157 | int err; |
| 6158 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6159 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6160 | if (!cmd) |
| 6161 | return -ENOENT; |
| 6162 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6163 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6164 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6165 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6166 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6167 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6168 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6169 | |
| 6170 | return err; |
| 6171 | } |
| 6172 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6173 | 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] | 6174 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6175 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6176 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6177 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6178 | } |
| 6179 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6180 | 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] | 6181 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6182 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6183 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6184 | status, |
| 6185 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6186 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6187 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6188 | 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] | 6189 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6190 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6191 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6192 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6193 | } |
| 6194 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6195 | 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] | 6196 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6197 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6198 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6199 | status, |
| 6200 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6201 | } |
| 6202 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 6203 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6204 | u8 link_type, u8 addr_type, u32 passkey, |
| 6205 | u8 entered) |
| 6206 | { |
| 6207 | struct mgmt_ev_passkey_notify ev; |
| 6208 | |
| 6209 | BT_DBG("%s", hdev->name); |
| 6210 | |
| 6211 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6212 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6213 | ev.passkey = __cpu_to_le32(passkey); |
| 6214 | ev.entered = entered; |
| 6215 | |
| 6216 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 6217 | } |
| 6218 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6219 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6220 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6221 | { |
| 6222 | struct mgmt_ev_auth_failed ev; |
| 6223 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 6224 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6225 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6226 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6227 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6228 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6229 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6230 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6231 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6232 | { |
| 6233 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6234 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6235 | |
| 6236 | if (status) { |
| 6237 | u8 mgmt_err = mgmt_status(status); |
| 6238 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6239 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6240 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6241 | } |
| 6242 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6243 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 6244 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 6245 | &hdev->dev_flags); |
| 6246 | else |
| 6247 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 6248 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6249 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6250 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6251 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6252 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6253 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6254 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6255 | |
| 6256 | if (match.sk) |
| 6257 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6258 | } |
| 6259 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6260 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6261 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6262 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6263 | struct hci_cp_write_eir cp; |
| 6264 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 6265 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6266 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6267 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 6268 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 6269 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6270 | memset(&cp, 0, sizeof(cp)); |
| 6271 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6272 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6273 | } |
| 6274 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6275 | 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] | 6276 | { |
| 6277 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6278 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6279 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6280 | |
| 6281 | if (status) { |
| 6282 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6283 | |
| 6284 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6285 | &hdev->dev_flags)) { |
| 6286 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6287 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6288 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6289 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6290 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 6291 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6292 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6293 | } |
| 6294 | |
| 6295 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6296 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6297 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6298 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 6299 | if (!changed) |
| 6300 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 6301 | &hdev->dev_flags); |
| 6302 | else |
| 6303 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6304 | } |
| 6305 | |
| 6306 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 6307 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6308 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6309 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6310 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 6311 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6312 | sock_put(match.sk); |
| 6313 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6314 | hci_req_init(&req, hdev); |
| 6315 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6316 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 6317 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 6318 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6319 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6320 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6321 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6322 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6323 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6324 | |
| 6325 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6326 | } |
| 6327 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6328 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 6329 | { |
| 6330 | struct cmd_lookup match = { NULL, hdev }; |
| 6331 | bool changed = false; |
| 6332 | |
| 6333 | if (status) { |
| 6334 | u8 mgmt_err = mgmt_status(status); |
| 6335 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6336 | if (enable) { |
| 6337 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 6338 | &hdev->dev_flags)) |
| 6339 | new_settings(hdev, NULL); |
| 6340 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6341 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6342 | |
| 6343 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6344 | cmd_status_rsp, &mgmt_err); |
| 6345 | return; |
| 6346 | } |
| 6347 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6348 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6349 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6350 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6351 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6352 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6353 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6354 | |
| 6355 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6356 | settings_rsp, &match); |
| 6357 | |
| 6358 | if (changed) |
| 6359 | new_settings(hdev, match.sk); |
| 6360 | |
| 6361 | if (match.sk) |
| 6362 | sock_put(match.sk); |
| 6363 | } |
| 6364 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6365 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6366 | { |
| 6367 | struct cmd_lookup *match = data; |
| 6368 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6369 | if (match->sk == NULL) { |
| 6370 | match->sk = cmd->sk; |
| 6371 | sock_hold(match->sk); |
| 6372 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6373 | } |
| 6374 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6375 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 6376 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6377 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6378 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6379 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6380 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 6381 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 6382 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6383 | |
| 6384 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6385 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 6386 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6387 | |
| 6388 | if (match.sk) |
| 6389 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6390 | } |
| 6391 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6392 | 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] | 6393 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6394 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6395 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6396 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6397 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6398 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6399 | |
| 6400 | memset(&ev, 0, sizeof(ev)); |
| 6401 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6402 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6403 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6404 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6405 | if (!cmd) { |
| 6406 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6407 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6408 | /* If this is a HCI command related to powering on the |
| 6409 | * HCI dev don't send any mgmt signals. |
| 6410 | */ |
| 6411 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6412 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6413 | } |
| 6414 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6415 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 6416 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6417 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6418 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6419 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 6420 | u8 *randomizer192, u8 *hash256, |
| 6421 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6422 | { |
| 6423 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6424 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6425 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6426 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6427 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6428 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6429 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6430 | |
| 6431 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6432 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 6433 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6434 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6435 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 6436 | hash256 && randomizer256) { |
| 6437 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6438 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6439 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 6440 | memcpy(rp.randomizer192, randomizer192, |
| 6441 | sizeof(rp.randomizer192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6442 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6443 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
| 6444 | memcpy(rp.randomizer256, randomizer256, |
| 6445 | sizeof(rp.randomizer256)); |
| 6446 | |
| 6447 | cmd_complete(cmd->sk, hdev->id, |
| 6448 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6449 | &rp, sizeof(rp)); |
| 6450 | } else { |
| 6451 | struct mgmt_rp_read_local_oob_data rp; |
| 6452 | |
| 6453 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 6454 | memcpy(rp.randomizer, randomizer192, |
| 6455 | sizeof(rp.randomizer)); |
| 6456 | |
| 6457 | cmd_complete(cmd->sk, hdev->id, |
| 6458 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6459 | &rp, sizeof(rp)); |
| 6460 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6461 | } |
| 6462 | |
| 6463 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6464 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6465 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6466 | 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] | 6467 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 6468 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6469 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6470 | char buf[512]; |
| 6471 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6472 | struct smp_irk *irk; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6473 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6474 | |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6475 | if (!hci_discovery_active(hdev)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6476 | return; |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6477 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6478 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 6479 | * are for the potential CoD field. |
| 6480 | */ |
| 6481 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6482 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 6483 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6484 | memset(buf, 0, sizeof(buf)); |
| 6485 | |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6486 | irk = hci_get_irk(hdev, bdaddr, addr_type); |
| 6487 | if (irk) { |
| 6488 | bacpy(&ev->addr.bdaddr, &irk->bdaddr); |
| 6489 | ev->addr.type = link_to_bdaddr(link_type, irk->addr_type); |
| 6490 | } else { |
| 6491 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 6492 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
| 6493 | } |
| 6494 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6495 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 6496 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6497 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6498 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6499 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6500 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6501 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 6502 | 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] | 6503 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6504 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6505 | if (scan_rsp_len > 0) |
| 6506 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
| 6507 | |
| 6508 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 6509 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 6510 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6511 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6512 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6513 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6514 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6515 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6516 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6517 | struct mgmt_ev_device_found *ev; |
| 6518 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 6519 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6520 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6521 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6522 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6523 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6524 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6525 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6526 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6527 | ev->rssi = rssi; |
| 6528 | |
| 6529 | 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] | 6530 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6531 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6532 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6533 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6534 | 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] | 6535 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6536 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6537 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6538 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6539 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6540 | struct pending_cmd *cmd; |
| 6541 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 6542 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 6543 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6544 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6545 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6546 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6547 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6548 | |
| 6549 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 6550 | u8 type = hdev->discovery.type; |
| 6551 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6552 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 6553 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6554 | mgmt_pending_remove(cmd); |
| 6555 | } |
| 6556 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6557 | memset(&ev, 0, sizeof(ev)); |
| 6558 | ev.type = hdev->discovery.type; |
| 6559 | ev.discovering = discovering; |
| 6560 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6561 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6562 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6563 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6564 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 6565 | { |
| 6566 | BT_DBG("%s status %u", hdev->name, status); |
| 6567 | |
| 6568 | /* Clear the advertising mgmt setting if we failed to re-enable it */ |
| 6569 | if (status) { |
| 6570 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6571 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6572 | } |
| 6573 | } |
| 6574 | |
| 6575 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 6576 | { |
| 6577 | struct hci_request req; |
| 6578 | |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 6579 | if (hci_conn_num(hdev, LE_LINK) > 0) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6580 | return; |
| 6581 | |
| 6582 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6583 | return; |
| 6584 | |
| 6585 | hci_req_init(&req, hdev); |
| 6586 | enable_advertising(&req); |
| 6587 | |
| 6588 | /* If this fails we have no option but to let user space know |
| 6589 | * that we've disabled advertising. |
| 6590 | */ |
| 6591 | if (hci_req_run(&req, adv_enable_complete) < 0) { |
| 6592 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6593 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6594 | } |
| 6595 | } |