Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
Johan Hedberg | ea585ab | 2012-02-17 14:50:39 +0200 | [diff] [blame] | 3 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4 | Copyright (C) 2010 Nokia Corporation |
Johan Hedberg | ea585ab | 2012-02-17 14:50:39 +0200 | [diff] [blame] | 5 | Copyright (C) 2011-2012 Intel Corporation |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License version 2 as |
| 9 | published by the Free Software Foundation; |
| 10 | |
| 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 12 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 14 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 15 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | |
| 20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 22 | SOFTWARE IS DISCLAIMED. |
| 23 | */ |
| 24 | |
| 25 | /* Bluetooth HCI Management interface */ |
| 26 | |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
| 29 | |
| 30 | #include <net/bluetooth/bluetooth.h> |
| 31 | #include <net/bluetooth/hci_core.h> |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 32 | #include <net/bluetooth/l2cap.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 33 | #include <net/bluetooth/mgmt.h> |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 34 | |
| 35 | #include "smp.h" |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 36 | |
Johan Hedberg | 2da9c55 | 2012-02-17 14:39:28 +0200 | [diff] [blame] | 37 | #define MGMT_VERSION 1 |
Marcel Holtmann | 730f091 | 2014-06-28 12:36:10 +0200 | [diff] [blame] | 38 | #define MGMT_REVISION 7 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 39 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 40 | static const u16 mgmt_commands[] = { |
| 41 | MGMT_OP_READ_INDEX_LIST, |
| 42 | MGMT_OP_READ_INFO, |
| 43 | MGMT_OP_SET_POWERED, |
| 44 | MGMT_OP_SET_DISCOVERABLE, |
| 45 | MGMT_OP_SET_CONNECTABLE, |
| 46 | MGMT_OP_SET_FAST_CONNECTABLE, |
| 47 | MGMT_OP_SET_PAIRABLE, |
| 48 | MGMT_OP_SET_LINK_SECURITY, |
| 49 | MGMT_OP_SET_SSP, |
| 50 | MGMT_OP_SET_HS, |
| 51 | MGMT_OP_SET_LE, |
| 52 | MGMT_OP_SET_DEV_CLASS, |
| 53 | MGMT_OP_SET_LOCAL_NAME, |
| 54 | MGMT_OP_ADD_UUID, |
| 55 | MGMT_OP_REMOVE_UUID, |
| 56 | MGMT_OP_LOAD_LINK_KEYS, |
| 57 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 58 | MGMT_OP_DISCONNECT, |
| 59 | MGMT_OP_GET_CONNECTIONS, |
| 60 | MGMT_OP_PIN_CODE_REPLY, |
| 61 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 62 | MGMT_OP_SET_IO_CAPABILITY, |
| 63 | MGMT_OP_PAIR_DEVICE, |
| 64 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 65 | MGMT_OP_UNPAIR_DEVICE, |
| 66 | MGMT_OP_USER_CONFIRM_REPLY, |
| 67 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 68 | MGMT_OP_USER_PASSKEY_REPLY, |
| 69 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 70 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 71 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 72 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 73 | MGMT_OP_START_DISCOVERY, |
| 74 | MGMT_OP_STOP_DISCOVERY, |
| 75 | MGMT_OP_CONFIRM_NAME, |
| 76 | MGMT_OP_BLOCK_DEVICE, |
| 77 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 78 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 79 | MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 80 | MGMT_OP_SET_BREDR, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 81 | MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 7f72134e | 2013-10-11 14:44:58 -0700 | [diff] [blame] | 82 | MGMT_OP_SET_SCAN_PARAMS, |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 83 | MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 84 | MGMT_OP_SET_DEBUG_KEYS, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 85 | MGMT_OP_SET_PRIVACY, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 86 | MGMT_OP_LOAD_IRKS, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 87 | MGMT_OP_GET_CONN_INFO, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 88 | MGMT_OP_GET_CLOCK_INFO, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 89 | MGMT_OP_ADD_DEVICE, |
| 90 | MGMT_OP_REMOVE_DEVICE, |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 91 | MGMT_OP_LOAD_CONN_PARAM, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | static const u16 mgmt_events[] = { |
| 95 | MGMT_EV_CONTROLLER_ERROR, |
| 96 | MGMT_EV_INDEX_ADDED, |
| 97 | MGMT_EV_INDEX_REMOVED, |
| 98 | MGMT_EV_NEW_SETTINGS, |
| 99 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 100 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 101 | MGMT_EV_NEW_LINK_KEY, |
| 102 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 103 | MGMT_EV_DEVICE_CONNECTED, |
| 104 | MGMT_EV_DEVICE_DISCONNECTED, |
| 105 | MGMT_EV_CONNECT_FAILED, |
| 106 | MGMT_EV_PIN_CODE_REQUEST, |
| 107 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 108 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 109 | MGMT_EV_AUTH_FAILED, |
| 110 | MGMT_EV_DEVICE_FOUND, |
| 111 | MGMT_EV_DISCOVERING, |
| 112 | MGMT_EV_DEVICE_BLOCKED, |
| 113 | MGMT_EV_DEVICE_UNBLOCKED, |
| 114 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 115 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 116 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 117 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 118 | MGMT_EV_DEVICE_ADDED, |
| 119 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 120 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 121 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896 | 2014-07-02 21:30:55 +0200 | [diff] [blame^] | 122 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 123 | }; |
| 124 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 125 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 126 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 127 | #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \ |
| 128 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 129 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 130 | struct pending_cmd { |
| 131 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 132 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 133 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 134 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 135 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 136 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 137 | }; |
| 138 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 139 | /* HCI to MGMT error code conversion table */ |
| 140 | static u8 mgmt_status_table[] = { |
| 141 | MGMT_STATUS_SUCCESS, |
| 142 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 143 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 144 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 145 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 146 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 147 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 148 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 149 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 150 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 151 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 152 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 153 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 154 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 155 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 156 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 157 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 158 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 159 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 160 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 161 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 162 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 163 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 164 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 165 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 166 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 167 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 168 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 169 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 170 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 171 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 172 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 173 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 174 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 175 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 176 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 177 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 178 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 179 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 180 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 181 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 182 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 183 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 184 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 185 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 186 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 187 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 188 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 189 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 190 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 191 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 192 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 193 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 194 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 195 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 196 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 197 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 198 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 199 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 200 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 201 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 202 | }; |
| 203 | |
| 204 | static u8 mgmt_status(u8 hci_status) |
| 205 | { |
| 206 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 207 | return mgmt_status_table[hci_status]; |
| 208 | |
| 209 | return MGMT_STATUS_FAILED; |
| 210 | } |
| 211 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 212 | 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] | 213 | { |
| 214 | struct sk_buff *skb; |
| 215 | struct mgmt_hdr *hdr; |
| 216 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 217 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 218 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 219 | 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] | 220 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 221 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 222 | if (!skb) |
| 223 | return -ENOMEM; |
| 224 | |
| 225 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 226 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 227 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 228 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 229 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 230 | |
| 231 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 232 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 233 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 234 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 235 | err = sock_queue_rcv_skb(sk, skb); |
| 236 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 237 | kfree_skb(skb); |
| 238 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 239 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 242 | 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] | 243 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 244 | { |
| 245 | struct sk_buff *skb; |
| 246 | struct mgmt_hdr *hdr; |
| 247 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 248 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 249 | |
| 250 | BT_DBG("sock %p", sk); |
| 251 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 252 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 253 | if (!skb) |
| 254 | return -ENOMEM; |
| 255 | |
| 256 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 257 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 258 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 259 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 260 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 261 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 262 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 263 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 264 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 265 | |
| 266 | if (rp) |
| 267 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 268 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 269 | err = sock_queue_rcv_skb(sk, skb); |
| 270 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 271 | kfree_skb(skb); |
| 272 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 273 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 276 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 277 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 278 | { |
| 279 | struct mgmt_rp_read_version rp; |
| 280 | |
| 281 | BT_DBG("sock %p", sk); |
| 282 | |
| 283 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 284 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 285 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 286 | 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] | 287 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 288 | } |
| 289 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 290 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 291 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 292 | { |
| 293 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 294 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 295 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 296 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 297 | size_t rp_size; |
| 298 | int i, err; |
| 299 | |
| 300 | BT_DBG("sock %p", sk); |
| 301 | |
| 302 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 303 | |
| 304 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 305 | if (!rp) |
| 306 | return -ENOMEM; |
| 307 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 308 | rp->num_commands = cpu_to_le16(num_commands); |
| 309 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 310 | |
| 311 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 312 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 313 | |
| 314 | for (i = 0; i < num_events; i++, opcode++) |
| 315 | put_unaligned_le16(mgmt_events[i], opcode); |
| 316 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 317 | 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] | 318 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 319 | kfree(rp); |
| 320 | |
| 321 | return err; |
| 322 | } |
| 323 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 324 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 325 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 326 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 327 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 328 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 329 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 330 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 331 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 332 | |
| 333 | BT_DBG("sock %p", sk); |
| 334 | |
| 335 | read_lock(&hci_dev_list_lock); |
| 336 | |
| 337 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 338 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 339 | if (d->dev_type == HCI_BREDR) |
| 340 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 341 | } |
| 342 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 343 | rp_len = sizeof(*rp) + (2 * count); |
| 344 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 345 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 346 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 347 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 348 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 349 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 350 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 351 | list_for_each_entry(d, &hci_dev_list, list) { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 352 | if (test_bit(HCI_SETUP, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 353 | continue; |
| 354 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 355 | if (test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 356 | continue; |
| 357 | |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 358 | if (test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 359 | continue; |
| 360 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 361 | if (d->dev_type == HCI_BREDR) { |
| 362 | rp->index[count++] = cpu_to_le16(d->id); |
| 363 | BT_DBG("Added hci%u", d->id); |
| 364 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 367 | rp->num_controllers = cpu_to_le16(count); |
| 368 | rp_len = sizeof(*rp) + (2 * count); |
| 369 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 370 | read_unlock(&hci_dev_list_lock); |
| 371 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 372 | 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] | 373 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 374 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 375 | kfree(rp); |
| 376 | |
| 377 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 378 | } |
| 379 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 380 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 381 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 382 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 383 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 384 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 385 | settings |= MGMT_SETTING_PAIRABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 386 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 387 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 388 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 389 | settings |= MGMT_SETTING_CONNECTABLE; |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 390 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 391 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 392 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 393 | settings |= MGMT_SETTING_BREDR; |
| 394 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 395 | |
| 396 | if (lmp_ssp_capable(hdev)) { |
| 397 | settings |= MGMT_SETTING_SSP; |
| 398 | settings |= MGMT_SETTING_HS; |
| 399 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 400 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 401 | if (lmp_sc_capable(hdev) || |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 402 | test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 403 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 404 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 405 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 406 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 407 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 408 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 409 | settings |= MGMT_SETTING_PRIVACY; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 410 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 411 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 412 | return settings; |
| 413 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 414 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 415 | static u32 get_current_settings(struct hci_dev *hdev) |
| 416 | { |
| 417 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 418 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 419 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 420 | settings |= MGMT_SETTING_POWERED; |
| 421 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 422 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 423 | settings |= MGMT_SETTING_CONNECTABLE; |
| 424 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 425 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 426 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 427 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 428 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 429 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 430 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 431 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 432 | settings |= MGMT_SETTING_PAIRABLE; |
| 433 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 434 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 435 | settings |= MGMT_SETTING_BREDR; |
| 436 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 437 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 438 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 439 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 440 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 441 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 442 | |
Johan Hedberg | 84bde9d6c | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 443 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 444 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 445 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 446 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 447 | settings |= MGMT_SETTING_HS; |
| 448 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 449 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 450 | settings |= MGMT_SETTING_ADVERTISING; |
| 451 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 452 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 453 | settings |= MGMT_SETTING_SECURE_CONN; |
| 454 | |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 455 | if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 456 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 457 | |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 458 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 459 | settings |= MGMT_SETTING_PRIVACY; |
| 460 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 461 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 462 | } |
| 463 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 464 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 465 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 466 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 467 | { |
| 468 | u8 *ptr = data, *uuids_start = NULL; |
| 469 | struct bt_uuid *uuid; |
| 470 | |
| 471 | if (len < 4) |
| 472 | return ptr; |
| 473 | |
| 474 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 475 | u16 uuid16; |
| 476 | |
| 477 | if (uuid->size != 16) |
| 478 | continue; |
| 479 | |
| 480 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 481 | if (uuid16 < 0x1100) |
| 482 | continue; |
| 483 | |
| 484 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 485 | continue; |
| 486 | |
| 487 | if (!uuids_start) { |
| 488 | uuids_start = ptr; |
| 489 | uuids_start[0] = 1; |
| 490 | uuids_start[1] = EIR_UUID16_ALL; |
| 491 | ptr += 2; |
| 492 | } |
| 493 | |
| 494 | /* Stop if not enough space to put next UUID */ |
| 495 | if ((ptr - data) + sizeof(u16) > len) { |
| 496 | uuids_start[1] = EIR_UUID16_SOME; |
| 497 | break; |
| 498 | } |
| 499 | |
| 500 | *ptr++ = (uuid16 & 0x00ff); |
| 501 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 502 | uuids_start[0] += sizeof(uuid16); |
| 503 | } |
| 504 | |
| 505 | return ptr; |
| 506 | } |
| 507 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 508 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 509 | { |
| 510 | u8 *ptr = data, *uuids_start = NULL; |
| 511 | struct bt_uuid *uuid; |
| 512 | |
| 513 | if (len < 6) |
| 514 | return ptr; |
| 515 | |
| 516 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 517 | if (uuid->size != 32) |
| 518 | continue; |
| 519 | |
| 520 | if (!uuids_start) { |
| 521 | uuids_start = ptr; |
| 522 | uuids_start[0] = 1; |
| 523 | uuids_start[1] = EIR_UUID32_ALL; |
| 524 | ptr += 2; |
| 525 | } |
| 526 | |
| 527 | /* Stop if not enough space to put next UUID */ |
| 528 | if ((ptr - data) + sizeof(u32) > len) { |
| 529 | uuids_start[1] = EIR_UUID32_SOME; |
| 530 | break; |
| 531 | } |
| 532 | |
| 533 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 534 | ptr += sizeof(u32); |
| 535 | uuids_start[0] += sizeof(u32); |
| 536 | } |
| 537 | |
| 538 | return ptr; |
| 539 | } |
| 540 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 541 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 542 | { |
| 543 | u8 *ptr = data, *uuids_start = NULL; |
| 544 | struct bt_uuid *uuid; |
| 545 | |
| 546 | if (len < 18) |
| 547 | return ptr; |
| 548 | |
| 549 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 550 | if (uuid->size != 128) |
| 551 | continue; |
| 552 | |
| 553 | if (!uuids_start) { |
| 554 | uuids_start = ptr; |
| 555 | uuids_start[0] = 1; |
| 556 | uuids_start[1] = EIR_UUID128_ALL; |
| 557 | ptr += 2; |
| 558 | } |
| 559 | |
| 560 | /* Stop if not enough space to put next UUID */ |
| 561 | if ((ptr - data) + 16 > len) { |
| 562 | uuids_start[1] = EIR_UUID128_SOME; |
| 563 | break; |
| 564 | } |
| 565 | |
| 566 | memcpy(ptr, uuid->uuid, 16); |
| 567 | ptr += 16; |
| 568 | uuids_start[0] += 16; |
| 569 | } |
| 570 | |
| 571 | return ptr; |
| 572 | } |
| 573 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 574 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 575 | { |
| 576 | struct pending_cmd *cmd; |
| 577 | |
| 578 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 579 | if (cmd->opcode == opcode) |
| 580 | return cmd; |
| 581 | } |
| 582 | |
| 583 | return NULL; |
| 584 | } |
| 585 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 586 | static struct pending_cmd *mgmt_pending_find_data(u16 opcode, |
| 587 | struct hci_dev *hdev, |
| 588 | const void *data) |
| 589 | { |
| 590 | struct pending_cmd *cmd; |
| 591 | |
| 592 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 593 | if (cmd->user_data != data) |
| 594 | continue; |
| 595 | if (cmd->opcode == opcode) |
| 596 | return cmd; |
| 597 | } |
| 598 | |
| 599 | return NULL; |
| 600 | } |
| 601 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 602 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 603 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 604 | u8 ad_len = 0; |
| 605 | size_t name_len; |
| 606 | |
| 607 | name_len = strlen(hdev->dev_name); |
| 608 | if (name_len > 0) { |
| 609 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 610 | |
| 611 | if (name_len > max_len) { |
| 612 | name_len = max_len; |
| 613 | ptr[1] = EIR_NAME_SHORT; |
| 614 | } else |
| 615 | ptr[1] = EIR_NAME_COMPLETE; |
| 616 | |
| 617 | ptr[0] = name_len + 1; |
| 618 | |
| 619 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 620 | |
| 621 | ad_len += (name_len + 2); |
| 622 | ptr += (name_len + 2); |
| 623 | } |
| 624 | |
| 625 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | static void update_scan_rsp_data(struct hci_request *req) |
| 629 | { |
| 630 | struct hci_dev *hdev = req->hdev; |
| 631 | struct hci_cp_le_set_scan_rsp_data cp; |
| 632 | u8 len; |
| 633 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 634 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 635 | return; |
| 636 | |
| 637 | memset(&cp, 0, sizeof(cp)); |
| 638 | |
| 639 | len = create_scan_rsp_data(hdev, cp.data); |
| 640 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 641 | if (hdev->scan_rsp_data_len == len && |
| 642 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 643 | return; |
| 644 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 645 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 646 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 647 | |
| 648 | cp.length = len; |
| 649 | |
| 650 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 651 | } |
| 652 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 653 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 654 | { |
| 655 | struct pending_cmd *cmd; |
| 656 | |
| 657 | /* If there's a pending mgmt command the flags will not yet have |
| 658 | * their final values, so check for this first. |
| 659 | */ |
| 660 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 661 | if (cmd) { |
| 662 | struct mgmt_mode *cp = cmd->param; |
| 663 | if (cp->val == 0x01) |
| 664 | return LE_AD_GENERAL; |
| 665 | else if (cp->val == 0x02) |
| 666 | return LE_AD_LIMITED; |
| 667 | } else { |
| 668 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 669 | return LE_AD_LIMITED; |
| 670 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 671 | return LE_AD_GENERAL; |
| 672 | } |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 677 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 678 | { |
| 679 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 680 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 681 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 682 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 683 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 684 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 685 | |
| 686 | if (flags) { |
| 687 | BT_DBG("adv flags 0x%02x", flags); |
| 688 | |
| 689 | ptr[0] = 2; |
| 690 | ptr[1] = EIR_FLAGS; |
| 691 | ptr[2] = flags; |
| 692 | |
| 693 | ad_len += 3; |
| 694 | ptr += 3; |
| 695 | } |
| 696 | |
| 697 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 698 | ptr[0] = 2; |
| 699 | ptr[1] = EIR_TX_POWER; |
| 700 | ptr[2] = (u8) hdev->adv_tx_power; |
| 701 | |
| 702 | ad_len += 3; |
| 703 | ptr += 3; |
| 704 | } |
| 705 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 706 | return ad_len; |
| 707 | } |
| 708 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 709 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 710 | { |
| 711 | struct hci_dev *hdev = req->hdev; |
| 712 | struct hci_cp_le_set_adv_data cp; |
| 713 | u8 len; |
| 714 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 715 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 716 | return; |
| 717 | |
| 718 | memset(&cp, 0, sizeof(cp)); |
| 719 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 720 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 721 | |
| 722 | if (hdev->adv_data_len == len && |
| 723 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 724 | return; |
| 725 | |
| 726 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 727 | hdev->adv_data_len = len; |
| 728 | |
| 729 | cp.length = len; |
| 730 | |
| 731 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 732 | } |
| 733 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 734 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 735 | { |
| 736 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 737 | size_t name_len; |
| 738 | |
| 739 | name_len = strlen(hdev->dev_name); |
| 740 | |
| 741 | if (name_len > 0) { |
| 742 | /* EIR Data type */ |
| 743 | if (name_len > 48) { |
| 744 | name_len = 48; |
| 745 | ptr[1] = EIR_NAME_SHORT; |
| 746 | } else |
| 747 | ptr[1] = EIR_NAME_COMPLETE; |
| 748 | |
| 749 | /* EIR Data length */ |
| 750 | ptr[0] = name_len + 1; |
| 751 | |
| 752 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 753 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 754 | ptr += (name_len + 2); |
| 755 | } |
| 756 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 757 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 758 | ptr[0] = 2; |
| 759 | ptr[1] = EIR_TX_POWER; |
| 760 | ptr[2] = (u8) hdev->inq_tx_power; |
| 761 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 762 | ptr += 3; |
| 763 | } |
| 764 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 765 | if (hdev->devid_source > 0) { |
| 766 | ptr[0] = 9; |
| 767 | ptr[1] = EIR_DEVICE_ID; |
| 768 | |
| 769 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 770 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 771 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 772 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 773 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 774 | ptr += 10; |
| 775 | } |
| 776 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 777 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 778 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 779 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 780 | } |
| 781 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 782 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 783 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 784 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 785 | struct hci_cp_write_eir cp; |
| 786 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 787 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 788 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 789 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 790 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 791 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 792 | |
Johan Hedberg | 84bde9d6c | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 793 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 794 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 795 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 796 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 797 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 798 | |
| 799 | memset(&cp, 0, sizeof(cp)); |
| 800 | |
| 801 | create_eir(hdev, cp.data); |
| 802 | |
| 803 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 804 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 805 | |
| 806 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 807 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 808 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 809 | } |
| 810 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 811 | static u8 get_service_classes(struct hci_dev *hdev) |
| 812 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 813 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 814 | u8 val = 0; |
| 815 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 816 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 817 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 818 | |
| 819 | return val; |
| 820 | } |
| 821 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 822 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 823 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 824 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 825 | u8 cod[3]; |
| 826 | |
| 827 | BT_DBG("%s", hdev->name); |
| 828 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 829 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 830 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 831 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 832 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 833 | return; |
| 834 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 835 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 836 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 837 | |
| 838 | cod[0] = hdev->minor_class; |
| 839 | cod[1] = hdev->major_class; |
| 840 | cod[2] = get_service_classes(hdev); |
| 841 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 842 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 843 | cod[1] |= 0x20; |
| 844 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 845 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 846 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 847 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 848 | 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] | 849 | } |
| 850 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 851 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 852 | { |
| 853 | struct pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 854 | |
| 855 | /* If there's a pending mgmt command the flag will not yet have |
| 856 | * it's final value, so check for this first. |
| 857 | */ |
| 858 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 859 | if (cmd) { |
| 860 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 861 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 862 | } |
| 863 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 864 | return test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | static void enable_advertising(struct hci_request *req) |
| 868 | { |
| 869 | struct hci_dev *hdev = req->hdev; |
| 870 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 871 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 872 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 873 | |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 874 | /* Clear the HCI_ADVERTISING bit temporarily so that the |
| 875 | * hci_update_random_address knows that it's safe to go ahead |
| 876 | * and write a new random address. The flag will be set back on |
| 877 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 878 | */ |
| 879 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 880 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 881 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 882 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 883 | /* Set require_privacy to true only when non-connectable |
| 884 | * advertising is used. In that case it is fine to use a |
| 885 | * non-resolvable private address. |
| 886 | */ |
| 887 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 888 | return; |
| 889 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 890 | memset(&cp, 0, sizeof(cp)); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 891 | cp.min_interval = cpu_to_le16(0x0800); |
| 892 | cp.max_interval = cpu_to_le16(0x0800); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 893 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 894 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 895 | cp.channel_map = hdev->le_adv_channel_map; |
| 896 | |
| 897 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 898 | |
| 899 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 900 | } |
| 901 | |
| 902 | static void disable_advertising(struct hci_request *req) |
| 903 | { |
| 904 | u8 enable = 0x00; |
| 905 | |
| 906 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 907 | } |
| 908 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 909 | static void service_cache_off(struct work_struct *work) |
| 910 | { |
| 911 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 912 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 913 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 914 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 915 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 916 | return; |
| 917 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 918 | hci_req_init(&req, hdev); |
| 919 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 920 | hci_dev_lock(hdev); |
| 921 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 922 | update_eir(&req); |
| 923 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 924 | |
| 925 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 926 | |
| 927 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 928 | } |
| 929 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 930 | static void rpa_expired(struct work_struct *work) |
| 931 | { |
| 932 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 933 | rpa_expired.work); |
| 934 | struct hci_request req; |
| 935 | |
| 936 | BT_DBG(""); |
| 937 | |
| 938 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 939 | |
| 940 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags) || |
| 941 | hci_conn_num(hdev, LE_LINK) > 0) |
| 942 | return; |
| 943 | |
| 944 | /* The generation of a new RPA and programming it into the |
| 945 | * controller happens in the enable_advertising() function. |
| 946 | */ |
| 947 | |
| 948 | hci_req_init(&req, hdev); |
| 949 | |
| 950 | disable_advertising(&req); |
| 951 | enable_advertising(&req); |
| 952 | |
| 953 | hci_req_run(&req, NULL); |
| 954 | } |
| 955 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 956 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 957 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 958 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 959 | return; |
| 960 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 961 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 962 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 963 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 964 | /* Non-mgmt controlled devices get this bit set |
| 965 | * implicitly so that pairing works for them, however |
| 966 | * for mgmt we require user-space to explicitly enable |
| 967 | * it |
| 968 | */ |
| 969 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 970 | } |
| 971 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 972 | 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] | 973 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 974 | { |
| 975 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 976 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 977 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 978 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 979 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 980 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 981 | memset(&rp, 0, sizeof(rp)); |
| 982 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 983 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 984 | |
| 985 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 986 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 987 | |
| 988 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 989 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 990 | |
| 991 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 992 | |
| 993 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 994 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 995 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 996 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 997 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 998 | 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] | 999 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 1003 | { |
| 1004 | sock_put(cmd->sk); |
| 1005 | kfree(cmd->param); |
| 1006 | kfree(cmd); |
| 1007 | } |
| 1008 | |
| 1009 | 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] | 1010 | struct hci_dev *hdev, void *data, |
| 1011 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1012 | { |
| 1013 | struct pending_cmd *cmd; |
| 1014 | |
Johan Hedberg | fca2001 | 2014-06-28 17:54:05 +0300 | [diff] [blame] | 1015 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1016 | if (!cmd) |
| 1017 | return NULL; |
| 1018 | |
| 1019 | cmd->opcode = opcode; |
| 1020 | cmd->index = hdev->id; |
| 1021 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 1022 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1023 | if (!cmd->param) { |
| 1024 | kfree(cmd); |
| 1025 | return NULL; |
| 1026 | } |
| 1027 | |
| 1028 | if (data) |
| 1029 | memcpy(cmd->param, data, len); |
| 1030 | |
| 1031 | cmd->sk = sk; |
| 1032 | sock_hold(sk); |
| 1033 | |
| 1034 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1035 | |
| 1036 | return cmd; |
| 1037 | } |
| 1038 | |
| 1039 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1040 | void (*cb)(struct pending_cmd *cmd, |
| 1041 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1042 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1043 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1044 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1045 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1046 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1047 | if (opcode > 0 && cmd->opcode != opcode) |
| 1048 | continue; |
| 1049 | |
| 1050 | cb(cmd, data); |
| 1051 | } |
| 1052 | } |
| 1053 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1054 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 1055 | { |
| 1056 | list_del(&cmd->list); |
| 1057 | mgmt_pending_free(cmd); |
| 1058 | } |
| 1059 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1060 | 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] | 1061 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1062 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1063 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1064 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1065 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1066 | } |
| 1067 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1068 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status) |
| 1069 | { |
| 1070 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1071 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1072 | if (hci_conn_count(hdev) == 0) { |
| 1073 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1074 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1075 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1076 | } |
| 1077 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1078 | static void hci_stop_discovery(struct hci_request *req) |
| 1079 | { |
| 1080 | struct hci_dev *hdev = req->hdev; |
| 1081 | struct hci_cp_remote_name_req_cancel cp; |
| 1082 | struct inquiry_entry *e; |
| 1083 | |
| 1084 | switch (hdev->discovery.state) { |
| 1085 | case DISCOVERY_FINDING: |
| 1086 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 1087 | hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 1088 | } else { |
| 1089 | cancel_delayed_work(&hdev->le_scan_disable); |
| 1090 | hci_req_add_le_scan_disable(req); |
| 1091 | } |
| 1092 | |
| 1093 | break; |
| 1094 | |
| 1095 | case DISCOVERY_RESOLVING: |
| 1096 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 1097 | NAME_PENDING); |
| 1098 | if (!e) |
| 1099 | return; |
| 1100 | |
| 1101 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 1102 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 1103 | &cp); |
| 1104 | |
| 1105 | break; |
| 1106 | |
| 1107 | default: |
| 1108 | /* Passive scanning */ |
| 1109 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 1110 | hci_req_add_le_scan_disable(req); |
| 1111 | break; |
| 1112 | } |
| 1113 | } |
| 1114 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1115 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1116 | { |
| 1117 | struct hci_request req; |
| 1118 | struct hci_conn *conn; |
| 1119 | |
| 1120 | hci_req_init(&req, hdev); |
| 1121 | |
| 1122 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1123 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1124 | u8 scan = 0x00; |
| 1125 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1126 | } |
| 1127 | |
| 1128 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 1129 | disable_advertising(&req); |
| 1130 | |
Johan Hedberg | f8680f1 | 2014-06-10 14:05:59 +0300 | [diff] [blame] | 1131 | hci_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1132 | |
| 1133 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1134 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1135 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1136 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1137 | switch (conn->state) { |
| 1138 | case BT_CONNECTED: |
| 1139 | case BT_CONFIG: |
| 1140 | dc.handle = cpu_to_le16(conn->handle); |
| 1141 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1142 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1143 | break; |
| 1144 | case BT_CONNECT: |
| 1145 | if (conn->type == LE_LINK) |
| 1146 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1147 | 0, NULL); |
| 1148 | else if (conn->type == ACL_LINK) |
| 1149 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1150 | 6, &conn->dst); |
| 1151 | break; |
| 1152 | case BT_CONNECT2: |
| 1153 | bacpy(&rej.bdaddr, &conn->dst); |
| 1154 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1155 | if (conn->type == ACL_LINK) |
| 1156 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1157 | sizeof(rej), &rej); |
| 1158 | else if (conn->type == SCO_LINK) |
| 1159 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1160 | sizeof(rej), &rej); |
| 1161 | break; |
| 1162 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | return hci_req_run(&req, clean_up_hci_complete); |
| 1166 | } |
| 1167 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1168 | 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] | 1169 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1170 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1171 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1172 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1173 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1174 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1175 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1176 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1177 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1178 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1179 | MGMT_STATUS_INVALID_PARAMS); |
| 1180 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1181 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1182 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1183 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 1184 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1185 | MGMT_STATUS_BUSY); |
| 1186 | goto failed; |
| 1187 | } |
| 1188 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1189 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1190 | cancel_delayed_work(&hdev->power_off); |
| 1191 | |
| 1192 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1193 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1194 | data, len); |
| 1195 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1196 | goto failed; |
| 1197 | } |
| 1198 | } |
| 1199 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1200 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1201 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1202 | goto failed; |
| 1203 | } |
| 1204 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1205 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1206 | if (!cmd) { |
| 1207 | err = -ENOMEM; |
| 1208 | goto failed; |
| 1209 | } |
| 1210 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1211 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1212 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1213 | err = 0; |
| 1214 | } else { |
| 1215 | /* Disconnect connections, stop scans, etc */ |
| 1216 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1217 | if (!err) |
| 1218 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1219 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1220 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1221 | /* ENODATA means there were no HCI commands queued */ |
| 1222 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1223 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1224 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1225 | err = 0; |
| 1226 | } |
| 1227 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1228 | |
| 1229 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1230 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1231 | return err; |
| 1232 | } |
| 1233 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1234 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 1235 | struct sock *skip_sk) |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1236 | { |
| 1237 | struct sk_buff *skb; |
| 1238 | struct mgmt_hdr *hdr; |
| 1239 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 1240 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1241 | if (!skb) |
| 1242 | return -ENOMEM; |
| 1243 | |
| 1244 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 1245 | hdr->opcode = cpu_to_le16(event); |
| 1246 | if (hdev) |
| 1247 | hdr->index = cpu_to_le16(hdev->id); |
| 1248 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1249 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1250 | hdr->len = cpu_to_le16(data_len); |
| 1251 | |
| 1252 | if (data) |
| 1253 | memcpy(skb_put(skb, data_len), data, data_len); |
| 1254 | |
Marcel Holtmann | 97e0bde | 2012-02-22 13:49:28 +0100 | [diff] [blame] | 1255 | /* Time stamp */ |
| 1256 | __net_timestamp(skb); |
| 1257 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1258 | hci_send_to_control(skb, skip_sk); |
| 1259 | kfree_skb(skb); |
| 1260 | |
| 1261 | return 0; |
| 1262 | } |
| 1263 | |
| 1264 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1265 | { |
| 1266 | __le32 ev; |
| 1267 | |
| 1268 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1269 | |
| 1270 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1271 | } |
| 1272 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1273 | struct cmd_lookup { |
| 1274 | struct sock *sk; |
| 1275 | struct hci_dev *hdev; |
| 1276 | u8 mgmt_status; |
| 1277 | }; |
| 1278 | |
| 1279 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1280 | { |
| 1281 | struct cmd_lookup *match = data; |
| 1282 | |
| 1283 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1284 | |
| 1285 | list_del(&cmd->list); |
| 1286 | |
| 1287 | if (match->sk == NULL) { |
| 1288 | match->sk = cmd->sk; |
| 1289 | sock_hold(match->sk); |
| 1290 | } |
| 1291 | |
| 1292 | mgmt_pending_free(cmd); |
| 1293 | } |
| 1294 | |
| 1295 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1296 | { |
| 1297 | u8 *status = data; |
| 1298 | |
| 1299 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1300 | mgmt_pending_remove(cmd); |
| 1301 | } |
| 1302 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1303 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1304 | { |
| 1305 | if (!lmp_bredr_capable(hdev)) |
| 1306 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1307 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1308 | return MGMT_STATUS_REJECTED; |
| 1309 | else |
| 1310 | return MGMT_STATUS_SUCCESS; |
| 1311 | } |
| 1312 | |
| 1313 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1314 | { |
| 1315 | if (!lmp_le_capable(hdev)) |
| 1316 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1317 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1318 | return MGMT_STATUS_REJECTED; |
| 1319 | else |
| 1320 | return MGMT_STATUS_SUCCESS; |
| 1321 | } |
| 1322 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1323 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1324 | { |
| 1325 | struct pending_cmd *cmd; |
| 1326 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1327 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1328 | bool changed; |
| 1329 | |
| 1330 | BT_DBG("status 0x%02x", status); |
| 1331 | |
| 1332 | hci_dev_lock(hdev); |
| 1333 | |
| 1334 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1335 | if (!cmd) |
| 1336 | goto unlock; |
| 1337 | |
| 1338 | if (status) { |
| 1339 | u8 mgmt_err = mgmt_status(status); |
| 1340 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1341 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1342 | goto remove_cmd; |
| 1343 | } |
| 1344 | |
| 1345 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1346 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1347 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1348 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1349 | |
| 1350 | if (hdev->discov_timeout > 0) { |
| 1351 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1352 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1353 | to); |
| 1354 | } |
| 1355 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1356 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1357 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1358 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1359 | |
| 1360 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1361 | |
| 1362 | if (changed) |
| 1363 | new_settings(hdev, cmd->sk); |
| 1364 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1365 | /* When the discoverable mode gets changed, make sure |
| 1366 | * that class of device has the limited discoverable |
| 1367 | * bit correctly set. |
| 1368 | */ |
| 1369 | hci_req_init(&req, hdev); |
| 1370 | update_class(&req); |
| 1371 | hci_req_run(&req, NULL); |
| 1372 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1373 | remove_cmd: |
| 1374 | mgmt_pending_remove(cmd); |
| 1375 | |
| 1376 | unlock: |
| 1377 | hci_dev_unlock(hdev); |
| 1378 | } |
| 1379 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1380 | 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] | 1381 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1382 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1383 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1384 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1385 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1386 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1387 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1388 | int err; |
| 1389 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1390 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1391 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1392 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1393 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1394 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1395 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1396 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1397 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1398 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1399 | MGMT_STATUS_INVALID_PARAMS); |
| 1400 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1401 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1402 | |
| 1403 | /* Disabling discoverable requires that no timeout is set, |
| 1404 | * and enabling limited discoverable requires a timeout. |
| 1405 | */ |
| 1406 | if ((cp->val == 0x00 && timeout > 0) || |
| 1407 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1408 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1409 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1410 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1411 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1412 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1413 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1414 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1415 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1416 | goto failed; |
| 1417 | } |
| 1418 | |
| 1419 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1420 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1421 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1422 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1423 | goto failed; |
| 1424 | } |
| 1425 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1426 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1427 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1428 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1429 | goto failed; |
| 1430 | } |
| 1431 | |
| 1432 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1433 | bool changed = false; |
| 1434 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1435 | /* Setting limited discoverable when powered off is |
| 1436 | * not a valid operation since it requires a timeout |
| 1437 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1438 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1439 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1440 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1441 | changed = true; |
| 1442 | } |
| 1443 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1444 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1445 | if (err < 0) |
| 1446 | goto failed; |
| 1447 | |
| 1448 | if (changed) |
| 1449 | err = new_settings(hdev, sk); |
| 1450 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1451 | goto failed; |
| 1452 | } |
| 1453 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1454 | /* If the current mode is the same, then just update the timeout |
| 1455 | * value with the new value. And if only the timeout gets updated, |
| 1456 | * then no need for any HCI transactions. |
| 1457 | */ |
| 1458 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1459 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1460 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1461 | cancel_delayed_work(&hdev->discov_off); |
| 1462 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1463 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1464 | if (cp->val && hdev->discov_timeout > 0) { |
| 1465 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1466 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1467 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1468 | } |
| 1469 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1470 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1471 | goto failed; |
| 1472 | } |
| 1473 | |
| 1474 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1475 | if (!cmd) { |
| 1476 | err = -ENOMEM; |
| 1477 | goto failed; |
| 1478 | } |
| 1479 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1480 | /* Cancel any potential discoverable timeout that might be |
| 1481 | * still active and store new timeout value. The arming of |
| 1482 | * the timeout happens in the complete handler. |
| 1483 | */ |
| 1484 | cancel_delayed_work(&hdev->discov_off); |
| 1485 | hdev->discov_timeout = timeout; |
| 1486 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1487 | /* Limited discoverable mode */ |
| 1488 | if (cp->val == 0x02) |
| 1489 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1490 | else |
| 1491 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1492 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1493 | hci_req_init(&req, hdev); |
| 1494 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1495 | /* The procedure for LE-only controllers is much simpler - just |
| 1496 | * update the advertising data. |
| 1497 | */ |
| 1498 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1499 | goto update_ad; |
| 1500 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1501 | scan = SCAN_PAGE; |
| 1502 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1503 | if (cp->val) { |
| 1504 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1505 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1506 | if (cp->val == 0x02) { |
| 1507 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1508 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1509 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1510 | hci_cp.iac_lap[1] = 0x8b; |
| 1511 | hci_cp.iac_lap[2] = 0x9e; |
| 1512 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1513 | hci_cp.iac_lap[4] = 0x8b; |
| 1514 | hci_cp.iac_lap[5] = 0x9e; |
| 1515 | } else { |
| 1516 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1517 | hci_cp.num_iac = 1; |
| 1518 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1519 | hci_cp.iac_lap[1] = 0x8b; |
| 1520 | hci_cp.iac_lap[2] = 0x9e; |
| 1521 | } |
| 1522 | |
| 1523 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1524 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1525 | |
| 1526 | scan |= SCAN_INQUIRY; |
| 1527 | } else { |
| 1528 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1529 | } |
| 1530 | |
| 1531 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1532 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1533 | update_ad: |
| 1534 | update_adv_data(&req); |
| 1535 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1536 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1537 | if (err < 0) |
| 1538 | mgmt_pending_remove(cmd); |
| 1539 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1540 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1541 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1542 | return err; |
| 1543 | } |
| 1544 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1545 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1546 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1547 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1548 | struct hci_cp_write_page_scan_activity acp; |
| 1549 | u8 type; |
| 1550 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1551 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1552 | return; |
| 1553 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1554 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1555 | return; |
| 1556 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1557 | if (enable) { |
| 1558 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1559 | |
| 1560 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1561 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1562 | } else { |
| 1563 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1564 | |
| 1565 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1566 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1567 | } |
| 1568 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1569 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1570 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1571 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1572 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1573 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1574 | sizeof(acp), &acp); |
| 1575 | |
| 1576 | if (hdev->page_scan_type != type) |
| 1577 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1578 | } |
| 1579 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1580 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1581 | { |
| 1582 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1583 | struct mgmt_mode *cp; |
| 1584 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1585 | |
| 1586 | BT_DBG("status 0x%02x", status); |
| 1587 | |
| 1588 | hci_dev_lock(hdev); |
| 1589 | |
| 1590 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1591 | if (!cmd) |
| 1592 | goto unlock; |
| 1593 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1594 | if (status) { |
| 1595 | u8 mgmt_err = mgmt_status(status); |
| 1596 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1597 | goto remove_cmd; |
| 1598 | } |
| 1599 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1600 | cp = cmd->param; |
| 1601 | if (cp->val) |
| 1602 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1603 | else |
| 1604 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1605 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1606 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1607 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1608 | if (changed) |
| 1609 | new_settings(hdev, cmd->sk); |
| 1610 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1611 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1612 | mgmt_pending_remove(cmd); |
| 1613 | |
| 1614 | unlock: |
| 1615 | hci_dev_unlock(hdev); |
| 1616 | } |
| 1617 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1618 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1619 | struct sock *sk, u8 val) |
| 1620 | { |
| 1621 | bool changed = false; |
| 1622 | int err; |
| 1623 | |
| 1624 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1625 | changed = true; |
| 1626 | |
| 1627 | if (val) { |
| 1628 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1629 | } else { |
| 1630 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1631 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1632 | } |
| 1633 | |
| 1634 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1635 | if (err < 0) |
| 1636 | return err; |
| 1637 | |
| 1638 | if (changed) |
| 1639 | return new_settings(hdev, sk); |
| 1640 | |
| 1641 | return 0; |
| 1642 | } |
| 1643 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1644 | 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] | 1645 | u16 len) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1646 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1647 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1648 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1649 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1650 | u8 scan; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1651 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1652 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1653 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1654 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1655 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1656 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1657 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1658 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1659 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1660 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1661 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1662 | MGMT_STATUS_INVALID_PARAMS); |
| 1663 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1664 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1665 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1666 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1667 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1668 | goto failed; |
| 1669 | } |
| 1670 | |
| 1671 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1672 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1673 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1674 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1675 | goto failed; |
| 1676 | } |
| 1677 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1678 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1679 | if (!cmd) { |
| 1680 | err = -ENOMEM; |
| 1681 | goto failed; |
| 1682 | } |
| 1683 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1684 | hci_req_init(&req, hdev); |
| 1685 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1686 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1687 | * by-product of disabling connectable, we need to update the |
| 1688 | * advertising flags. |
| 1689 | */ |
| 1690 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1691 | if (!cp->val) { |
| 1692 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1693 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1694 | } |
| 1695 | update_adv_data(&req); |
| 1696 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1697 | if (cp->val) { |
| 1698 | scan = SCAN_PAGE; |
| 1699 | } else { |
| 1700 | scan = 0; |
| 1701 | |
| 1702 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1703 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1704 | cancel_delayed_work(&hdev->discov_off); |
| 1705 | } |
| 1706 | |
| 1707 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1708 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1709 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1710 | /* If we're going from non-connectable to connectable or |
| 1711 | * vice-versa when fast connectable is enabled ensure that fast |
| 1712 | * connectable gets disabled. write_fast_connectable won't do |
| 1713 | * anything if the page scan parameters are already what they |
| 1714 | * should be. |
| 1715 | */ |
| 1716 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1717 | write_fast_connectable(&req, false); |
| 1718 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1719 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) && |
| 1720 | hci_conn_num(hdev, LE_LINK) == 0) { |
| 1721 | disable_advertising(&req); |
| 1722 | enable_advertising(&req); |
| 1723 | } |
| 1724 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1725 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1726 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1727 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1728 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1729 | err = set_connectable_update_settings(hdev, sk, |
| 1730 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1731 | goto failed; |
| 1732 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1733 | |
| 1734 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1735 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1736 | return err; |
| 1737 | } |
| 1738 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1739 | 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] | 1740 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1741 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1742 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1743 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1744 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1745 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1746 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1747 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1748 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1749 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1750 | MGMT_STATUS_INVALID_PARAMS); |
| 1751 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1752 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1753 | |
| 1754 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1755 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1756 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1757 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1758 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1759 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1760 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1761 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1762 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1763 | if (changed) |
| 1764 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1765 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1766 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1767 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1768 | return err; |
| 1769 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1770 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1771 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1772 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1773 | { |
| 1774 | struct mgmt_mode *cp = data; |
| 1775 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1776 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1777 | int err; |
| 1778 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1779 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1780 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1781 | status = mgmt_bredr_support(hdev); |
| 1782 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1783 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1784 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1785 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1786 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1787 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1788 | MGMT_STATUS_INVALID_PARAMS); |
| 1789 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1790 | hci_dev_lock(hdev); |
| 1791 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1792 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1793 | bool changed = false; |
| 1794 | |
| 1795 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1796 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1797 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1798 | changed = true; |
| 1799 | } |
| 1800 | |
| 1801 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1802 | if (err < 0) |
| 1803 | goto failed; |
| 1804 | |
| 1805 | if (changed) |
| 1806 | err = new_settings(hdev, sk); |
| 1807 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1808 | goto failed; |
| 1809 | } |
| 1810 | |
| 1811 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1812 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1813 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1814 | goto failed; |
| 1815 | } |
| 1816 | |
| 1817 | val = !!cp->val; |
| 1818 | |
| 1819 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1820 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1821 | goto failed; |
| 1822 | } |
| 1823 | |
| 1824 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1825 | if (!cmd) { |
| 1826 | err = -ENOMEM; |
| 1827 | goto failed; |
| 1828 | } |
| 1829 | |
| 1830 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1831 | if (err < 0) { |
| 1832 | mgmt_pending_remove(cmd); |
| 1833 | goto failed; |
| 1834 | } |
| 1835 | |
| 1836 | failed: |
| 1837 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1838 | return err; |
| 1839 | } |
| 1840 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1841 | 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] | 1842 | { |
| 1843 | struct mgmt_mode *cp = data; |
| 1844 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1845 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1846 | int err; |
| 1847 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1848 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1849 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1850 | status = mgmt_bredr_support(hdev); |
| 1851 | if (status) |
| 1852 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 1853 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1854 | if (!lmp_ssp_capable(hdev)) |
| 1855 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1856 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1857 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1858 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1859 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1860 | MGMT_STATUS_INVALID_PARAMS); |
| 1861 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1862 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1863 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1864 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1865 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1866 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1867 | if (cp->val) { |
| 1868 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 1869 | &hdev->dev_flags); |
| 1870 | } else { |
| 1871 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 1872 | &hdev->dev_flags); |
| 1873 | if (!changed) |
| 1874 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 1875 | &hdev->dev_flags); |
| 1876 | else |
| 1877 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1881 | if (err < 0) |
| 1882 | goto failed; |
| 1883 | |
| 1884 | if (changed) |
| 1885 | err = new_settings(hdev, sk); |
| 1886 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1887 | goto failed; |
| 1888 | } |
| 1889 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1890 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 1891 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 1892 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1893 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1894 | goto failed; |
| 1895 | } |
| 1896 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1897 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1898 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1899 | goto failed; |
| 1900 | } |
| 1901 | |
| 1902 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1903 | if (!cmd) { |
| 1904 | err = -ENOMEM; |
| 1905 | goto failed; |
| 1906 | } |
| 1907 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1908 | if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 1909 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1910 | sizeof(cp->val), &cp->val); |
| 1911 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1912 | 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] | 1913 | if (err < 0) { |
| 1914 | mgmt_pending_remove(cmd); |
| 1915 | goto failed; |
| 1916 | } |
| 1917 | |
| 1918 | failed: |
| 1919 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1920 | return err; |
| 1921 | } |
| 1922 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1923 | 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] | 1924 | { |
| 1925 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1926 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1927 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1928 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1929 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1930 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1931 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1932 | status = mgmt_bredr_support(hdev); |
| 1933 | if (status) |
| 1934 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1935 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1936 | if (!lmp_ssp_capable(hdev)) |
| 1937 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1938 | MGMT_STATUS_NOT_SUPPORTED); |
| 1939 | |
| 1940 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 1941 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1942 | MGMT_STATUS_REJECTED); |
| 1943 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1944 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1945 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1946 | MGMT_STATUS_INVALID_PARAMS); |
| 1947 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1948 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1949 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1950 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1951 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1952 | } else { |
| 1953 | if (hdev_is_powered(hdev)) { |
| 1954 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1955 | MGMT_STATUS_REJECTED); |
| 1956 | goto unlock; |
| 1957 | } |
| 1958 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1959 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1960 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1961 | |
| 1962 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1963 | if (err < 0) |
| 1964 | goto unlock; |
| 1965 | |
| 1966 | if (changed) |
| 1967 | err = new_settings(hdev, sk); |
| 1968 | |
| 1969 | unlock: |
| 1970 | hci_dev_unlock(hdev); |
| 1971 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1972 | } |
| 1973 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1974 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 1975 | { |
| 1976 | struct cmd_lookup match = { NULL, hdev }; |
| 1977 | |
| 1978 | if (status) { |
| 1979 | u8 mgmt_err = mgmt_status(status); |
| 1980 | |
| 1981 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1982 | &mgmt_err); |
| 1983 | return; |
| 1984 | } |
| 1985 | |
| 1986 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1987 | |
| 1988 | new_settings(hdev, match.sk); |
| 1989 | |
| 1990 | if (match.sk) |
| 1991 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1992 | |
| 1993 | /* Make sure the controller has a good default for |
| 1994 | * advertising data. Restrict the update to when LE |
| 1995 | * has actually been enabled. During power on, the |
| 1996 | * update in powered_update_hci will take care of it. |
| 1997 | */ |
| 1998 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 1999 | struct hci_request req; |
| 2000 | |
| 2001 | hci_dev_lock(hdev); |
| 2002 | |
| 2003 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 2004 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 2005 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2006 | hci_req_run(&req, NULL); |
| 2007 | |
| 2008 | hci_dev_unlock(hdev); |
| 2009 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2010 | } |
| 2011 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2012 | 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] | 2013 | { |
| 2014 | struct mgmt_mode *cp = data; |
| 2015 | struct hci_cp_write_le_host_supported hci_cp; |
| 2016 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2017 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2018 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2019 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2020 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2021 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2022 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2023 | if (!lmp_le_capable(hdev)) |
| 2024 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2025 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2026 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2027 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2028 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2029 | MGMT_STATUS_INVALID_PARAMS); |
| 2030 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2031 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2032 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2033 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2034 | MGMT_STATUS_REJECTED); |
| 2035 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2036 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2037 | |
| 2038 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2039 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2040 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2041 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2042 | bool changed = false; |
| 2043 | |
| 2044 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2045 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 2046 | changed = true; |
| 2047 | } |
| 2048 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 2049 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 2050 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2051 | changed = true; |
| 2052 | } |
| 2053 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2054 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2055 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2056 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2057 | |
| 2058 | if (changed) |
| 2059 | err = new_settings(hdev, sk); |
| 2060 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2061 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2062 | } |
| 2063 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2064 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2065 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2066 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2067 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2068 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2072 | if (!cmd) { |
| 2073 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2074 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2075 | } |
| 2076 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2077 | hci_req_init(&req, hdev); |
| 2078 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2079 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2080 | |
| 2081 | if (val) { |
| 2082 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2083 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2084 | } else { |
| 2085 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 2086 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2087 | } |
| 2088 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2089 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2090 | &hci_cp); |
| 2091 | |
| 2092 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2093 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2094 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2095 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2096 | unlock: |
| 2097 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2098 | return err; |
| 2099 | } |
| 2100 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2101 | /* This is a helper function to test for pending mgmt commands that can |
| 2102 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2103 | * mgmt command at a time since otherwise we cannot easily track what |
| 2104 | * the current values are, will be, and based on that calculate if a new |
| 2105 | * HCI command needs to be sent and if yes with what value. |
| 2106 | */ |
| 2107 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2108 | { |
| 2109 | struct pending_cmd *cmd; |
| 2110 | |
| 2111 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2112 | switch (cmd->opcode) { |
| 2113 | case MGMT_OP_ADD_UUID: |
| 2114 | case MGMT_OP_REMOVE_UUID: |
| 2115 | case MGMT_OP_SET_DEV_CLASS: |
| 2116 | case MGMT_OP_SET_POWERED: |
| 2117 | return true; |
| 2118 | } |
| 2119 | } |
| 2120 | |
| 2121 | return false; |
| 2122 | } |
| 2123 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2124 | static const u8 bluetooth_base_uuid[] = { |
| 2125 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2126 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2127 | }; |
| 2128 | |
| 2129 | static u8 get_uuid_size(const u8 *uuid) |
| 2130 | { |
| 2131 | u32 val; |
| 2132 | |
| 2133 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2134 | return 128; |
| 2135 | |
| 2136 | val = get_unaligned_le32(&uuid[12]); |
| 2137 | if (val > 0xffff) |
| 2138 | return 32; |
| 2139 | |
| 2140 | return 16; |
| 2141 | } |
| 2142 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2143 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2144 | { |
| 2145 | struct pending_cmd *cmd; |
| 2146 | |
| 2147 | hci_dev_lock(hdev); |
| 2148 | |
| 2149 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2150 | if (!cmd) |
| 2151 | goto unlock; |
| 2152 | |
| 2153 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 2154 | hdev->dev_class, 3); |
| 2155 | |
| 2156 | mgmt_pending_remove(cmd); |
| 2157 | |
| 2158 | unlock: |
| 2159 | hci_dev_unlock(hdev); |
| 2160 | } |
| 2161 | |
| 2162 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2163 | { |
| 2164 | BT_DBG("status 0x%02x", status); |
| 2165 | |
| 2166 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2167 | } |
| 2168 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2169 | 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] | 2170 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2171 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2172 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2173 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2174 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2175 | int err; |
| 2176 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2177 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2178 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2179 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2180 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2181 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2182 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2183 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2184 | goto failed; |
| 2185 | } |
| 2186 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2187 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2188 | if (!uuid) { |
| 2189 | err = -ENOMEM; |
| 2190 | goto failed; |
| 2191 | } |
| 2192 | |
| 2193 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2194 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2195 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2196 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2197 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2198 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2199 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2200 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2201 | update_class(&req); |
| 2202 | update_eir(&req); |
| 2203 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2204 | err = hci_req_run(&req, add_uuid_complete); |
| 2205 | if (err < 0) { |
| 2206 | if (err != -ENODATA) |
| 2207 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2208 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2209 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2210 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2211 | goto failed; |
| 2212 | } |
| 2213 | |
| 2214 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2215 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2216 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2217 | goto failed; |
| 2218 | } |
| 2219 | |
| 2220 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2221 | |
| 2222 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2223 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2224 | return err; |
| 2225 | } |
| 2226 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2227 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2228 | { |
| 2229 | if (!hdev_is_powered(hdev)) |
| 2230 | return false; |
| 2231 | |
| 2232 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2233 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2234 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2235 | return true; |
| 2236 | } |
| 2237 | |
| 2238 | return false; |
| 2239 | } |
| 2240 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2241 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2242 | { |
| 2243 | BT_DBG("status 0x%02x", status); |
| 2244 | |
| 2245 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2246 | } |
| 2247 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2248 | 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] | 2249 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2250 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2251 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2252 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2253 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2254 | 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] | 2255 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2256 | int err, found; |
| 2257 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2258 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2259 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2260 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2261 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2262 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2263 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2264 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2265 | goto unlock; |
| 2266 | } |
| 2267 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2268 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2269 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2270 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2271 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2272 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2273 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2274 | goto unlock; |
| 2275 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2276 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2277 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | found = 0; |
| 2281 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2282 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2283 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2284 | continue; |
| 2285 | |
| 2286 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2287 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2288 | found++; |
| 2289 | } |
| 2290 | |
| 2291 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2292 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2293 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2294 | goto unlock; |
| 2295 | } |
| 2296 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2297 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2298 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2299 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2300 | update_class(&req); |
| 2301 | update_eir(&req); |
| 2302 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2303 | err = hci_req_run(&req, remove_uuid_complete); |
| 2304 | if (err < 0) { |
| 2305 | if (err != -ENODATA) |
| 2306 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2307 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2308 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2309 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2310 | goto unlock; |
| 2311 | } |
| 2312 | |
| 2313 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2314 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2315 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2316 | goto unlock; |
| 2317 | } |
| 2318 | |
| 2319 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2320 | |
| 2321 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2322 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2323 | return err; |
| 2324 | } |
| 2325 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2326 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2327 | { |
| 2328 | BT_DBG("status 0x%02x", status); |
| 2329 | |
| 2330 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2331 | } |
| 2332 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2333 | 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] | 2334 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2335 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2336 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2337 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2338 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2339 | int err; |
| 2340 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2341 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2342 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2343 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2344 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2345 | MGMT_STATUS_NOT_SUPPORTED); |
| 2346 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2347 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2348 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2349 | if (pending_eir_or_class(hdev)) { |
| 2350 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2351 | MGMT_STATUS_BUSY); |
| 2352 | goto unlock; |
| 2353 | } |
| 2354 | |
| 2355 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2356 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2357 | MGMT_STATUS_INVALID_PARAMS); |
| 2358 | goto unlock; |
| 2359 | } |
| 2360 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2361 | hdev->major_class = cp->major; |
| 2362 | hdev->minor_class = cp->minor; |
| 2363 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2364 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2365 | 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] | 2366 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2367 | goto unlock; |
| 2368 | } |
| 2369 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2370 | hci_req_init(&req, hdev); |
| 2371 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2372 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2373 | hci_dev_unlock(hdev); |
| 2374 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2375 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2376 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2377 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2378 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2379 | update_class(&req); |
| 2380 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2381 | err = hci_req_run(&req, set_class_complete); |
| 2382 | if (err < 0) { |
| 2383 | if (err != -ENODATA) |
| 2384 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2385 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2386 | 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] | 2387 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2388 | goto unlock; |
| 2389 | } |
| 2390 | |
| 2391 | 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] | 2392 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2393 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2394 | goto unlock; |
| 2395 | } |
| 2396 | |
| 2397 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2398 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2399 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2400 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2401 | return err; |
| 2402 | } |
| 2403 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2404 | 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] | 2405 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2406 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2407 | struct mgmt_cp_load_link_keys *cp = data; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2408 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2409 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2410 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2411 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2412 | BT_DBG("request for %s", hdev->name); |
| 2413 | |
| 2414 | if (!lmp_bredr_capable(hdev)) |
| 2415 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2416 | MGMT_STATUS_NOT_SUPPORTED); |
| 2417 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2418 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2419 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2420 | expected_len = sizeof(*cp) + key_count * |
| 2421 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2422 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2423 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2424 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2425 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2426 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2427 | } |
| 2428 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2429 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2430 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2431 | MGMT_STATUS_INVALID_PARAMS); |
| 2432 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2433 | 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] | 2434 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2435 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2436 | for (i = 0; i < key_count; i++) { |
| 2437 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2438 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2439 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2440 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2441 | MGMT_STATUS_INVALID_PARAMS); |
| 2442 | } |
| 2443 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2444 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2445 | |
| 2446 | hci_link_keys_clear(hdev); |
| 2447 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2448 | if (cp->debug_keys) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2449 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 2450 | &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2451 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2452 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 2453 | &hdev->dev_flags); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2454 | |
| 2455 | if (changed) |
| 2456 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2457 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2458 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2459 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2460 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2461 | /* Always ignore debug keys and require a new pairing if |
| 2462 | * the user wants to use them. |
| 2463 | */ |
| 2464 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2465 | continue; |
| 2466 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2467 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2468 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2469 | } |
| 2470 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2471 | 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] | 2472 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2473 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2474 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2475 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2476 | } |
| 2477 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2478 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2479 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2480 | { |
| 2481 | struct mgmt_ev_device_unpaired ev; |
| 2482 | |
| 2483 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2484 | ev.addr.type = addr_type; |
| 2485 | |
| 2486 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2487 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2488 | } |
| 2489 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2490 | 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] | 2491 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2492 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2493 | struct mgmt_cp_unpair_device *cp = data; |
| 2494 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2495 | struct hci_cp_disconnect dc; |
| 2496 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2497 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2498 | int err; |
| 2499 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2500 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2501 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2502 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2503 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2504 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2505 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2506 | MGMT_STATUS_INVALID_PARAMS, |
| 2507 | &rp, sizeof(rp)); |
| 2508 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2509 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2510 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2511 | MGMT_STATUS_INVALID_PARAMS, |
| 2512 | &rp, sizeof(rp)); |
| 2513 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2514 | hci_dev_lock(hdev); |
| 2515 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2516 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2517 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2518 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2519 | goto unlock; |
| 2520 | } |
| 2521 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2522 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2523 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2524 | } else { |
| 2525 | u8 addr_type; |
| 2526 | |
| 2527 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2528 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2529 | else |
| 2530 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2531 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2532 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2533 | |
Andre Guedes | a9b0a04 | 2014-02-26 20:21:52 -0300 | [diff] [blame] | 2534 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2535 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2536 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2537 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2538 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2539 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2540 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2541 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2542 | goto unlock; |
| 2543 | } |
| 2544 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2545 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2546 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2547 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2548 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2549 | else |
| 2550 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2551 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2552 | } else { |
| 2553 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2554 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2555 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2556 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2557 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2558 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2559 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2560 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2561 | } |
| 2562 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2563 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2564 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2565 | if (!cmd) { |
| 2566 | err = -ENOMEM; |
| 2567 | goto unlock; |
| 2568 | } |
| 2569 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2570 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2571 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2572 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2573 | if (err < 0) |
| 2574 | mgmt_pending_remove(cmd); |
| 2575 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2576 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2577 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2578 | return err; |
| 2579 | } |
| 2580 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2581 | 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] | 2582 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2583 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2584 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2585 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2586 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2587 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2588 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2589 | int err; |
| 2590 | |
| 2591 | BT_DBG(""); |
| 2592 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2593 | memset(&rp, 0, sizeof(rp)); |
| 2594 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2595 | rp.addr.type = cp->addr.type; |
| 2596 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2597 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2598 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2599 | MGMT_STATUS_INVALID_PARAMS, |
| 2600 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2601 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2602 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2603 | |
| 2604 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2605 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2606 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2607 | goto failed; |
| 2608 | } |
| 2609 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2610 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2611 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2612 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2613 | goto failed; |
| 2614 | } |
| 2615 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2616 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2617 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2618 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2619 | else |
| 2620 | 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] | 2621 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2622 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2623 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2624 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2625 | goto failed; |
| 2626 | } |
| 2627 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2628 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2629 | if (!cmd) { |
| 2630 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2631 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2632 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2633 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2634 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2635 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2636 | |
| 2637 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2638 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2639 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2640 | |
| 2641 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2642 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2643 | return err; |
| 2644 | } |
| 2645 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2646 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2647 | { |
| 2648 | switch (link_type) { |
| 2649 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2650 | switch (addr_type) { |
| 2651 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2652 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2653 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2654 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2655 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2656 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2657 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2658 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2659 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2660 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2661 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2662 | } |
| 2663 | } |
| 2664 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2665 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2666 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2667 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2668 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2669 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2670 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2671 | int err; |
| 2672 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2673 | |
| 2674 | BT_DBG(""); |
| 2675 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2676 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2677 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2678 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2679 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2680 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2681 | goto unlock; |
| 2682 | } |
| 2683 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2684 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2685 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2686 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2687 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2688 | } |
| 2689 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2690 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2691 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2692 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2693 | err = -ENOMEM; |
| 2694 | goto unlock; |
| 2695 | } |
| 2696 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2697 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2698 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2699 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2700 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2701 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2702 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2703 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2704 | continue; |
| 2705 | i++; |
| 2706 | } |
| 2707 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2708 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2709 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2710 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2711 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2712 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2713 | 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] | 2714 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2715 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2716 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2717 | |
| 2718 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2719 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2720 | return err; |
| 2721 | } |
| 2722 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2723 | 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] | 2724 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2725 | { |
| 2726 | struct pending_cmd *cmd; |
| 2727 | int err; |
| 2728 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2729 | 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] | 2730 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2731 | if (!cmd) |
| 2732 | return -ENOMEM; |
| 2733 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2734 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2735 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2736 | if (err < 0) |
| 2737 | mgmt_pending_remove(cmd); |
| 2738 | |
| 2739 | return err; |
| 2740 | } |
| 2741 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2742 | 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] | 2743 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2744 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2745 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2746 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2747 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2748 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2749 | int err; |
| 2750 | |
| 2751 | BT_DBG(""); |
| 2752 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2753 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2754 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2755 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2756 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2757 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2758 | goto failed; |
| 2759 | } |
| 2760 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2761 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2762 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2763 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2764 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2765 | goto failed; |
| 2766 | } |
| 2767 | |
| 2768 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2769 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2770 | |
| 2771 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2772 | |
| 2773 | BT_ERR("PIN code is not 16 bytes long"); |
| 2774 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2775 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2776 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2777 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2778 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2779 | |
| 2780 | goto failed; |
| 2781 | } |
| 2782 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2783 | 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] | 2784 | if (!cmd) { |
| 2785 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2786 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2787 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2788 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2789 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2790 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2791 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2792 | |
| 2793 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2794 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2795 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2796 | |
| 2797 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2798 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2799 | return err; |
| 2800 | } |
| 2801 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2802 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2803 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2804 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2805 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2806 | |
| 2807 | BT_DBG(""); |
| 2808 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2809 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
| 2810 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2811 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
| 2812 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2813 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2814 | |
| 2815 | hdev->io_capability = cp->io_capability; |
| 2816 | |
| 2817 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2818 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2819 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2820 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2821 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2822 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2823 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2824 | } |
| 2825 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2826 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2827 | { |
| 2828 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2829 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2830 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2831 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2832 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2833 | continue; |
| 2834 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2835 | if (cmd->user_data != conn) |
| 2836 | continue; |
| 2837 | |
| 2838 | return cmd; |
| 2839 | } |
| 2840 | |
| 2841 | return NULL; |
| 2842 | } |
| 2843 | |
| 2844 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 2845 | { |
| 2846 | struct mgmt_rp_pair_device rp; |
| 2847 | struct hci_conn *conn = cmd->user_data; |
| 2848 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2849 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2850 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2851 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 2852 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2853 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2854 | |
| 2855 | /* So we don't get further callbacks for this connection */ |
| 2856 | conn->connect_cfm_cb = NULL; |
| 2857 | conn->security_cfm_cb = NULL; |
| 2858 | conn->disconn_cfm_cb = NULL; |
| 2859 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2860 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2861 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2862 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2863 | } |
| 2864 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2865 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2866 | { |
| 2867 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 2868 | struct pending_cmd *cmd; |
| 2869 | |
| 2870 | cmd = find_pairing(conn); |
| 2871 | if (cmd) |
| 2872 | pairing_complete(cmd, status); |
| 2873 | } |
| 2874 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2875 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2876 | { |
| 2877 | struct pending_cmd *cmd; |
| 2878 | |
| 2879 | BT_DBG("status %u", status); |
| 2880 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2881 | cmd = find_pairing(conn); |
| 2882 | if (!cmd) |
| 2883 | BT_DBG("Unable to find a pending command"); |
| 2884 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 2885 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2886 | } |
| 2887 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2888 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2889 | { |
| 2890 | struct pending_cmd *cmd; |
| 2891 | |
| 2892 | BT_DBG("status %u", status); |
| 2893 | |
| 2894 | if (!status) |
| 2895 | return; |
| 2896 | |
| 2897 | cmd = find_pairing(conn); |
| 2898 | if (!cmd) |
| 2899 | BT_DBG("Unable to find a pending command"); |
| 2900 | else |
| 2901 | pairing_complete(cmd, mgmt_status(status)); |
| 2902 | } |
| 2903 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2904 | 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] | 2905 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2906 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2907 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2908 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2909 | struct pending_cmd *cmd; |
| 2910 | u8 sec_level, auth_type; |
| 2911 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2912 | int err; |
| 2913 | |
| 2914 | BT_DBG(""); |
| 2915 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2916 | memset(&rp, 0, sizeof(rp)); |
| 2917 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2918 | rp.addr.type = cp->addr.type; |
| 2919 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2920 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2921 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2922 | MGMT_STATUS_INVALID_PARAMS, |
| 2923 | &rp, sizeof(rp)); |
| 2924 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2925 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
| 2926 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2927 | MGMT_STATUS_INVALID_PARAMS, |
| 2928 | &rp, sizeof(rp)); |
| 2929 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2930 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2931 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2932 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2933 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2934 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2935 | goto unlock; |
| 2936 | } |
| 2937 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2938 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2939 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2940 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2941 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2942 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 2943 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2944 | } else { |
| 2945 | u8 addr_type; |
| 2946 | |
| 2947 | /* Convert from L2CAP channel address type to HCI address type |
| 2948 | */ |
| 2949 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2950 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2951 | else |
| 2952 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2953 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2954 | /* When pairing a new device, it is expected to remember |
| 2955 | * this device for future connections. Adding the connection |
| 2956 | * parameter information ahead of time allows tracking |
| 2957 | * of the slave preferred values and will speed up any |
| 2958 | * further connection establishment. |
| 2959 | * |
| 2960 | * If connection parameters already exist, then they |
| 2961 | * will be kept and this function does nothing. |
| 2962 | */ |
| 2963 | hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 2964 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2965 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2966 | sec_level, auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2967 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2968 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2969 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2970 | int status; |
| 2971 | |
| 2972 | if (PTR_ERR(conn) == -EBUSY) |
| 2973 | status = MGMT_STATUS_BUSY; |
| 2974 | else |
| 2975 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2976 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2977 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2978 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2979 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2980 | goto unlock; |
| 2981 | } |
| 2982 | |
| 2983 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2984 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2985 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2986 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2987 | goto unlock; |
| 2988 | } |
| 2989 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2990 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2991 | if (!cmd) { |
| 2992 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2993 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2994 | goto unlock; |
| 2995 | } |
| 2996 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2997 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2998 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2999 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3000 | conn->security_cfm_cb = pairing_complete_cb; |
| 3001 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3002 | } else { |
| 3003 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3004 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3005 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3006 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3007 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3008 | conn->io_capability = cp->io_cap; |
| 3009 | cmd->user_data = conn; |
| 3010 | |
| 3011 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3012 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3013 | pairing_complete(cmd, 0); |
| 3014 | |
| 3015 | err = 0; |
| 3016 | |
| 3017 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3018 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3019 | return err; |
| 3020 | } |
| 3021 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3022 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3023 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3024 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3025 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3026 | struct pending_cmd *cmd; |
| 3027 | struct hci_conn *conn; |
| 3028 | int err; |
| 3029 | |
| 3030 | BT_DBG(""); |
| 3031 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3032 | hci_dev_lock(hdev); |
| 3033 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3034 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3035 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3036 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3037 | goto unlock; |
| 3038 | } |
| 3039 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3040 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3041 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3042 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3043 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3044 | goto unlock; |
| 3045 | } |
| 3046 | |
| 3047 | conn = cmd->user_data; |
| 3048 | |
| 3049 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3050 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3051 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3052 | goto unlock; |
| 3053 | } |
| 3054 | |
| 3055 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3056 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3057 | 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] | 3058 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3059 | unlock: |
| 3060 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3061 | return err; |
| 3062 | } |
| 3063 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3064 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3065 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3066 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3067 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3068 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3069 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3070 | int err; |
| 3071 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3072 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3073 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3074 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3075 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3076 | MGMT_STATUS_NOT_POWERED, addr, |
| 3077 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3078 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3079 | } |
| 3080 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3081 | if (addr->type == BDADDR_BREDR) |
| 3082 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3083 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3084 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3085 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3086 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3087 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3088 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3089 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3090 | goto done; |
| 3091 | } |
| 3092 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3093 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3094 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3095 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3096 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3097 | MGMT_STATUS_SUCCESS, addr, |
| 3098 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3099 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3100 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3101 | MGMT_STATUS_FAILED, addr, |
| 3102 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3103 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3104 | goto done; |
| 3105 | } |
| 3106 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3107 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3108 | if (!cmd) { |
| 3109 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3110 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3111 | } |
| 3112 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3113 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3114 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3115 | struct hci_cp_user_passkey_reply cp; |
| 3116 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3117 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3118 | cp.passkey = passkey; |
| 3119 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3120 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3121 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3122 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3123 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3124 | if (err < 0) |
| 3125 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3126 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3127 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3128 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3129 | return err; |
| 3130 | } |
| 3131 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3132 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3133 | void *data, u16 len) |
| 3134 | { |
| 3135 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3136 | |
| 3137 | BT_DBG(""); |
| 3138 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3139 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3140 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3141 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3142 | } |
| 3143 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3144 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3145 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3146 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3147 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3148 | |
| 3149 | BT_DBG(""); |
| 3150 | |
| 3151 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3152 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3153 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3154 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3155 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3156 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3157 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3158 | } |
| 3159 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3160 | 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] | 3161 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3162 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3163 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3164 | |
| 3165 | BT_DBG(""); |
| 3166 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3167 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3168 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3169 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3170 | } |
| 3171 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3172 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3173 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3174 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3175 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3176 | |
| 3177 | BT_DBG(""); |
| 3178 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3179 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3180 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3181 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3182 | } |
| 3183 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3184 | 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] | 3185 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3186 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3187 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3188 | |
| 3189 | BT_DBG(""); |
| 3190 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3191 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3192 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3193 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3194 | } |
| 3195 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3196 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3197 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3198 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3199 | struct hci_cp_write_local_name cp; |
| 3200 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3201 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3202 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3203 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3204 | } |
| 3205 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3206 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3207 | { |
| 3208 | struct mgmt_cp_set_local_name *cp; |
| 3209 | struct pending_cmd *cmd; |
| 3210 | |
| 3211 | BT_DBG("status 0x%02x", status); |
| 3212 | |
| 3213 | hci_dev_lock(hdev); |
| 3214 | |
| 3215 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3216 | if (!cmd) |
| 3217 | goto unlock; |
| 3218 | |
| 3219 | cp = cmd->param; |
| 3220 | |
| 3221 | if (status) |
| 3222 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3223 | mgmt_status(status)); |
| 3224 | else |
| 3225 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3226 | cp, sizeof(*cp)); |
| 3227 | |
| 3228 | mgmt_pending_remove(cmd); |
| 3229 | |
| 3230 | unlock: |
| 3231 | hci_dev_unlock(hdev); |
| 3232 | } |
| 3233 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3234 | 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] | 3235 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3236 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3237 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3238 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3239 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3240 | int err; |
| 3241 | |
| 3242 | BT_DBG(""); |
| 3243 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3244 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3245 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3246 | /* If the old values are the same as the new ones just return a |
| 3247 | * direct command complete event. |
| 3248 | */ |
| 3249 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3250 | !memcmp(hdev->short_name, cp->short_name, |
| 3251 | sizeof(hdev->short_name))) { |
| 3252 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3253 | data, len); |
| 3254 | goto failed; |
| 3255 | } |
| 3256 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3257 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3258 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3259 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3260 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3261 | |
| 3262 | 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] | 3263 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3264 | if (err < 0) |
| 3265 | goto failed; |
| 3266 | |
| 3267 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3268 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3269 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3270 | goto failed; |
| 3271 | } |
| 3272 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3273 | 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] | 3274 | if (!cmd) { |
| 3275 | err = -ENOMEM; |
| 3276 | goto failed; |
| 3277 | } |
| 3278 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3279 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3280 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3281 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3282 | |
| 3283 | if (lmp_bredr_capable(hdev)) { |
| 3284 | update_name(&req); |
| 3285 | update_eir(&req); |
| 3286 | } |
| 3287 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3288 | /* The name is stored in the scan response data and so |
| 3289 | * no need to udpate the advertising data here. |
| 3290 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3291 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3292 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3293 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3294 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3295 | if (err < 0) |
| 3296 | mgmt_pending_remove(cmd); |
| 3297 | |
| 3298 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3299 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3300 | return err; |
| 3301 | } |
| 3302 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3303 | 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] | 3304 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3305 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3306 | struct pending_cmd *cmd; |
| 3307 | int err; |
| 3308 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3309 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3310 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3311 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3312 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3313 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3314 | 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] | 3315 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3316 | goto unlock; |
| 3317 | } |
| 3318 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3319 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3320 | 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] | 3321 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3322 | goto unlock; |
| 3323 | } |
| 3324 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3325 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3326 | 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] | 3327 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3328 | goto unlock; |
| 3329 | } |
| 3330 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3331 | 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] | 3332 | if (!cmd) { |
| 3333 | err = -ENOMEM; |
| 3334 | goto unlock; |
| 3335 | } |
| 3336 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3337 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3338 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3339 | 0, NULL); |
| 3340 | else |
| 3341 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3342 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3343 | if (err < 0) |
| 3344 | mgmt_pending_remove(cmd); |
| 3345 | |
| 3346 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3347 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3348 | return err; |
| 3349 | } |
| 3350 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3351 | 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] | 3352 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3353 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3354 | int err; |
| 3355 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3356 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3357 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3358 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3359 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3360 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3361 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3362 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3363 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3364 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3365 | cp->hash, cp->randomizer); |
| 3366 | if (err < 0) |
| 3367 | status = MGMT_STATUS_FAILED; |
| 3368 | else |
| 3369 | status = MGMT_STATUS_SUCCESS; |
| 3370 | |
| 3371 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3372 | status, &cp->addr, sizeof(cp->addr)); |
| 3373 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3374 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3375 | u8 status; |
| 3376 | |
| 3377 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3378 | cp->hash192, |
| 3379 | cp->randomizer192, |
| 3380 | cp->hash256, |
| 3381 | cp->randomizer256); |
| 3382 | if (err < 0) |
| 3383 | status = MGMT_STATUS_FAILED; |
| 3384 | else |
| 3385 | status = MGMT_STATUS_SUCCESS; |
| 3386 | |
| 3387 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3388 | status, &cp->addr, sizeof(cp->addr)); |
| 3389 | } else { |
| 3390 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3391 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3392 | MGMT_STATUS_INVALID_PARAMS); |
| 3393 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3394 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3395 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3396 | return err; |
| 3397 | } |
| 3398 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3399 | 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] | 3400 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3401 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3402 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3403 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3404 | int err; |
| 3405 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3406 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3407 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3408 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3409 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3410 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3411 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3412 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3413 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3414 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3415 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3416 | 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] | 3417 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3418 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3419 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3420 | return err; |
| 3421 | } |
| 3422 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3423 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3424 | { |
| 3425 | struct pending_cmd *cmd; |
| 3426 | u8 type; |
| 3427 | int err; |
| 3428 | |
| 3429 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3430 | |
| 3431 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3432 | if (!cmd) |
| 3433 | return -ENOENT; |
| 3434 | |
| 3435 | type = hdev->discovery.type; |
| 3436 | |
| 3437 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3438 | &type, sizeof(type)); |
| 3439 | mgmt_pending_remove(cmd); |
| 3440 | |
| 3441 | return err; |
| 3442 | } |
| 3443 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3444 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3445 | { |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3446 | unsigned long timeout = 0; |
| 3447 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3448 | BT_DBG("status %d", status); |
| 3449 | |
| 3450 | if (status) { |
| 3451 | hci_dev_lock(hdev); |
| 3452 | mgmt_start_discovery_failed(hdev, status); |
| 3453 | hci_dev_unlock(hdev); |
| 3454 | return; |
| 3455 | } |
| 3456 | |
| 3457 | hci_dev_lock(hdev); |
| 3458 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3459 | hci_dev_unlock(hdev); |
| 3460 | |
| 3461 | switch (hdev->discovery.type) { |
| 3462 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3463 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3464 | break; |
| 3465 | |
| 3466 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3467 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3468 | break; |
| 3469 | |
| 3470 | case DISCOV_TYPE_BREDR: |
| 3471 | break; |
| 3472 | |
| 3473 | default: |
| 3474 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3475 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3476 | |
| 3477 | if (!timeout) |
| 3478 | return; |
| 3479 | |
| 3480 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3481 | } |
| 3482 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3483 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3484 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3485 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3486 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3487 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3488 | struct hci_cp_le_set_scan_param param_cp; |
| 3489 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3490 | struct hci_cp_inquiry inq_cp; |
| 3491 | struct hci_request req; |
| 3492 | /* General inquiry access code (GIAC) */ |
| 3493 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3494 | u8 status, own_addr_type; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3495 | int err; |
| 3496 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3497 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3498 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3499 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3500 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3501 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3502 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3503 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3504 | goto failed; |
| 3505 | } |
| 3506 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3507 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3508 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3509 | MGMT_STATUS_BUSY); |
| 3510 | goto failed; |
| 3511 | } |
| 3512 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3513 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3514 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3515 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3516 | goto failed; |
| 3517 | } |
| 3518 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3519 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3520 | if (!cmd) { |
| 3521 | err = -ENOMEM; |
| 3522 | goto failed; |
| 3523 | } |
| 3524 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3525 | hdev->discovery.type = cp->type; |
| 3526 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3527 | hci_req_init(&req, hdev); |
| 3528 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3529 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3530 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3531 | status = mgmt_bredr_support(hdev); |
| 3532 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3533 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3534 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3535 | mgmt_pending_remove(cmd); |
| 3536 | goto failed; |
| 3537 | } |
| 3538 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3539 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3540 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3541 | MGMT_STATUS_BUSY); |
| 3542 | mgmt_pending_remove(cmd); |
| 3543 | goto failed; |
| 3544 | } |
| 3545 | |
| 3546 | hci_inquiry_cache_flush(hdev); |
| 3547 | |
| 3548 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3549 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3550 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3551 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3552 | break; |
| 3553 | |
| 3554 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3555 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3556 | status = mgmt_le_support(hdev); |
| 3557 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3558 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3559 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3560 | mgmt_pending_remove(cmd); |
| 3561 | goto failed; |
| 3562 | } |
| 3563 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3564 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3565 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3566 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3567 | MGMT_STATUS_NOT_SUPPORTED); |
| 3568 | mgmt_pending_remove(cmd); |
| 3569 | goto failed; |
| 3570 | } |
| 3571 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3572 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3573 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3574 | MGMT_STATUS_REJECTED); |
| 3575 | mgmt_pending_remove(cmd); |
| 3576 | goto failed; |
| 3577 | } |
| 3578 | |
Andre Guedes | c54c386 | 2014-02-26 20:21:50 -0300 | [diff] [blame] | 3579 | /* If controller is scanning, it means the background scanning |
| 3580 | * is running. Thus, we should temporarily stop it in order to |
| 3581 | * set the discovery scanning parameters. |
| 3582 | */ |
| 3583 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3584 | hci_req_add_le_scan_disable(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3585 | |
| 3586 | memset(¶m_cp, 0, sizeof(param_cp)); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3587 | |
Marcel Holtmann | 94b1fc9 | 2014-02-23 20:25:54 -0800 | [diff] [blame] | 3588 | /* All active scans will be done with either a resolvable |
| 3589 | * private address (when privacy feature has been enabled) |
| 3590 | * or unresolvable private address. |
| 3591 | */ |
| 3592 | err = hci_update_random_address(&req, true, &own_addr_type); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3593 | if (err < 0) { |
| 3594 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3595 | MGMT_STATUS_FAILED); |
| 3596 | mgmt_pending_remove(cmd); |
| 3597 | goto failed; |
| 3598 | } |
| 3599 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3600 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3601 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3602 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3603 | param_cp.own_address_type = own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3604 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3605 | ¶m_cp); |
| 3606 | |
| 3607 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3608 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3609 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3610 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3611 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3612 | break; |
| 3613 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3614 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3615 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3616 | MGMT_STATUS_INVALID_PARAMS); |
| 3617 | mgmt_pending_remove(cmd); |
| 3618 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3619 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3620 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3621 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3622 | if (err < 0) |
| 3623 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3624 | else |
| 3625 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3626 | |
| 3627 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3628 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3629 | return err; |
| 3630 | } |
| 3631 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3632 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3633 | { |
| 3634 | struct pending_cmd *cmd; |
| 3635 | int err; |
| 3636 | |
| 3637 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3638 | if (!cmd) |
| 3639 | return -ENOENT; |
| 3640 | |
| 3641 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3642 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3643 | mgmt_pending_remove(cmd); |
| 3644 | |
| 3645 | return err; |
| 3646 | } |
| 3647 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3648 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3649 | { |
| 3650 | BT_DBG("status %d", status); |
| 3651 | |
| 3652 | hci_dev_lock(hdev); |
| 3653 | |
| 3654 | if (status) { |
| 3655 | mgmt_stop_discovery_failed(hdev, status); |
| 3656 | goto unlock; |
| 3657 | } |
| 3658 | |
| 3659 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3660 | |
| 3661 | unlock: |
| 3662 | hci_dev_unlock(hdev); |
| 3663 | } |
| 3664 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3665 | 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] | 3666 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3667 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3668 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3669 | struct pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3670 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3671 | int err; |
| 3672 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3673 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3674 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3675 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3676 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3677 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3678 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3679 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3680 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3681 | goto unlock; |
| 3682 | } |
| 3683 | |
| 3684 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3685 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3686 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3687 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3688 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3689 | } |
| 3690 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3691 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3692 | if (!cmd) { |
| 3693 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3694 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3695 | } |
| 3696 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3697 | hci_req_init(&req, hdev); |
| 3698 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3699 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3700 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3701 | err = hci_req_run(&req, stop_discovery_complete); |
| 3702 | if (!err) { |
| 3703 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3704 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3705 | } |
| 3706 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3707 | mgmt_pending_remove(cmd); |
| 3708 | |
| 3709 | /* If no HCI commands were sent we're done */ |
| 3710 | if (err == -ENODATA) { |
| 3711 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 3712 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 3713 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3714 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3715 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3716 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3717 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3718 | return err; |
| 3719 | } |
| 3720 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3721 | 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] | 3722 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3723 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3724 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3725 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3726 | int err; |
| 3727 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3728 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3729 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3730 | hci_dev_lock(hdev); |
| 3731 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3732 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3733 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3734 | MGMT_STATUS_FAILED, &cp->addr, |
| 3735 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3736 | goto failed; |
| 3737 | } |
| 3738 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3739 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3740 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3741 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3742 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3743 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3744 | goto failed; |
| 3745 | } |
| 3746 | |
| 3747 | if (cp->name_known) { |
| 3748 | e->name_state = NAME_KNOWN; |
| 3749 | list_del(&e->list); |
| 3750 | } else { |
| 3751 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3752 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3753 | } |
| 3754 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3755 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3756 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3757 | |
| 3758 | failed: |
| 3759 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3760 | return err; |
| 3761 | } |
| 3762 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3763 | 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] | 3764 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3765 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3766 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3767 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3768 | int err; |
| 3769 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3770 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3771 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3772 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3773 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3774 | MGMT_STATUS_INVALID_PARAMS, |
| 3775 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3776 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3777 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3778 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3779 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3780 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3781 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3782 | goto done; |
| 3783 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3784 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3785 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3786 | sk); |
| 3787 | status = MGMT_STATUS_SUCCESS; |
| 3788 | |
| 3789 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3790 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3791 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3792 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3793 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3794 | |
| 3795 | return err; |
| 3796 | } |
| 3797 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3798 | 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] | 3799 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3800 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3801 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3802 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3803 | int err; |
| 3804 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3805 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3806 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3807 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3808 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3809 | MGMT_STATUS_INVALID_PARAMS, |
| 3810 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3811 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3812 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3813 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3814 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3815 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3816 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3817 | goto done; |
| 3818 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3819 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3820 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3821 | sk); |
| 3822 | status = MGMT_STATUS_SUCCESS; |
| 3823 | |
| 3824 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3825 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3826 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3827 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3828 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3829 | |
| 3830 | return err; |
| 3831 | } |
| 3832 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3833 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3834 | u16 len) |
| 3835 | { |
| 3836 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3837 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3838 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3839 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3840 | |
| 3841 | BT_DBG("%s", hdev->name); |
| 3842 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3843 | source = __le16_to_cpu(cp->source); |
| 3844 | |
| 3845 | if (source > 0x0002) |
| 3846 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3847 | MGMT_STATUS_INVALID_PARAMS); |
| 3848 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3849 | hci_dev_lock(hdev); |
| 3850 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3851 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3852 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 3853 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 3854 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 3855 | |
| 3856 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 3857 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3858 | hci_req_init(&req, hdev); |
| 3859 | update_eir(&req); |
| 3860 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3861 | |
| 3862 | hci_dev_unlock(hdev); |
| 3863 | |
| 3864 | return err; |
| 3865 | } |
| 3866 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3867 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 3868 | { |
| 3869 | struct cmd_lookup match = { NULL, hdev }; |
| 3870 | |
| 3871 | if (status) { |
| 3872 | u8 mgmt_err = mgmt_status(status); |
| 3873 | |
| 3874 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 3875 | cmd_status_rsp, &mgmt_err); |
| 3876 | return; |
| 3877 | } |
| 3878 | |
| 3879 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 3880 | &match); |
| 3881 | |
| 3882 | new_settings(hdev, match.sk); |
| 3883 | |
| 3884 | if (match.sk) |
| 3885 | sock_put(match.sk); |
| 3886 | } |
| 3887 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 3888 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3889 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3890 | { |
| 3891 | struct mgmt_mode *cp = data; |
| 3892 | struct pending_cmd *cmd; |
| 3893 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3894 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3895 | int err; |
| 3896 | |
| 3897 | BT_DBG("request for %s", hdev->name); |
| 3898 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3899 | status = mgmt_le_support(hdev); |
| 3900 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3901 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3902 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3903 | |
| 3904 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3905 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3906 | MGMT_STATUS_INVALID_PARAMS); |
| 3907 | |
| 3908 | hci_dev_lock(hdev); |
| 3909 | |
| 3910 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3911 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3912 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 3913 | /* The following conditions are ones which mean that we should |
| 3914 | * not do any HCI communication but directly send a mgmt |
| 3915 | * response to user space (after toggling the flag if |
| 3916 | * necessary). |
| 3917 | */ |
| 3918 | if (!hdev_is_powered(hdev) || val == enabled || |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 3919 | hci_conn_num(hdev, LE_LINK) > 0) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3920 | bool changed = false; |
| 3921 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3922 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 3923 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3924 | changed = true; |
| 3925 | } |
| 3926 | |
| 3927 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 3928 | if (err < 0) |
| 3929 | goto unlock; |
| 3930 | |
| 3931 | if (changed) |
| 3932 | err = new_settings(hdev, sk); |
| 3933 | |
| 3934 | goto unlock; |
| 3935 | } |
| 3936 | |
| 3937 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 3938 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 3939 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3940 | MGMT_STATUS_BUSY); |
| 3941 | goto unlock; |
| 3942 | } |
| 3943 | |
| 3944 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 3945 | if (!cmd) { |
| 3946 | err = -ENOMEM; |
| 3947 | goto unlock; |
| 3948 | } |
| 3949 | |
| 3950 | hci_req_init(&req, hdev); |
| 3951 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 3952 | if (val) |
| 3953 | enable_advertising(&req); |
| 3954 | else |
| 3955 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3956 | |
| 3957 | err = hci_req_run(&req, set_advertising_complete); |
| 3958 | if (err < 0) |
| 3959 | mgmt_pending_remove(cmd); |
| 3960 | |
| 3961 | unlock: |
| 3962 | hci_dev_unlock(hdev); |
| 3963 | return err; |
| 3964 | } |
| 3965 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3966 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 3967 | void *data, u16 len) |
| 3968 | { |
| 3969 | struct mgmt_cp_set_static_address *cp = data; |
| 3970 | int err; |
| 3971 | |
| 3972 | BT_DBG("%s", hdev->name); |
| 3973 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3974 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3975 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3976 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3977 | |
| 3978 | if (hdev_is_powered(hdev)) |
| 3979 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 3980 | MGMT_STATUS_REJECTED); |
| 3981 | |
| 3982 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 3983 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 3984 | return cmd_status(sk, hdev->id, |
| 3985 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3986 | MGMT_STATUS_INVALID_PARAMS); |
| 3987 | |
| 3988 | /* Two most significant bits shall be set */ |
| 3989 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 3990 | return cmd_status(sk, hdev->id, |
| 3991 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3992 | MGMT_STATUS_INVALID_PARAMS); |
| 3993 | } |
| 3994 | |
| 3995 | hci_dev_lock(hdev); |
| 3996 | |
| 3997 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 3998 | |
| 3999 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 4000 | |
| 4001 | hci_dev_unlock(hdev); |
| 4002 | |
| 4003 | return err; |
| 4004 | } |
| 4005 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4006 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4007 | void *data, u16 len) |
| 4008 | { |
| 4009 | struct mgmt_cp_set_scan_params *cp = data; |
| 4010 | __u16 interval, window; |
| 4011 | int err; |
| 4012 | |
| 4013 | BT_DBG("%s", hdev->name); |
| 4014 | |
| 4015 | if (!lmp_le_capable(hdev)) |
| 4016 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4017 | MGMT_STATUS_NOT_SUPPORTED); |
| 4018 | |
| 4019 | interval = __le16_to_cpu(cp->interval); |
| 4020 | |
| 4021 | if (interval < 0x0004 || interval > 0x4000) |
| 4022 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4023 | MGMT_STATUS_INVALID_PARAMS); |
| 4024 | |
| 4025 | window = __le16_to_cpu(cp->window); |
| 4026 | |
| 4027 | if (window < 0x0004 || window > 0x4000) |
| 4028 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4029 | MGMT_STATUS_INVALID_PARAMS); |
| 4030 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4031 | if (window > interval) |
| 4032 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4033 | MGMT_STATUS_INVALID_PARAMS); |
| 4034 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4035 | hci_dev_lock(hdev); |
| 4036 | |
| 4037 | hdev->le_scan_interval = interval; |
| 4038 | hdev->le_scan_window = window; |
| 4039 | |
| 4040 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 4041 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4042 | /* If background scan is running, restart it so new parameters are |
| 4043 | * loaded. |
| 4044 | */ |
| 4045 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4046 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4047 | struct hci_request req; |
| 4048 | |
| 4049 | hci_req_init(&req, hdev); |
| 4050 | |
| 4051 | hci_req_add_le_scan_disable(&req); |
| 4052 | hci_req_add_le_passive_scan(&req); |
| 4053 | |
| 4054 | hci_req_run(&req, NULL); |
| 4055 | } |
| 4056 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4057 | hci_dev_unlock(hdev); |
| 4058 | |
| 4059 | return err; |
| 4060 | } |
| 4061 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4062 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 4063 | { |
| 4064 | struct pending_cmd *cmd; |
| 4065 | |
| 4066 | BT_DBG("status 0x%02x", status); |
| 4067 | |
| 4068 | hci_dev_lock(hdev); |
| 4069 | |
| 4070 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4071 | if (!cmd) |
| 4072 | goto unlock; |
| 4073 | |
| 4074 | if (status) { |
| 4075 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4076 | mgmt_status(status)); |
| 4077 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4078 | struct mgmt_mode *cp = cmd->param; |
| 4079 | |
| 4080 | if (cp->val) |
| 4081 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4082 | else |
| 4083 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4084 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4085 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4086 | new_settings(hdev, cmd->sk); |
| 4087 | } |
| 4088 | |
| 4089 | mgmt_pending_remove(cmd); |
| 4090 | |
| 4091 | unlock: |
| 4092 | hci_dev_unlock(hdev); |
| 4093 | } |
| 4094 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4095 | 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] | 4096 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4097 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4098 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4099 | struct pending_cmd *cmd; |
| 4100 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4101 | int err; |
| 4102 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4103 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4104 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4105 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4106 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4107 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4108 | MGMT_STATUS_NOT_SUPPORTED); |
| 4109 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4110 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4111 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4112 | MGMT_STATUS_INVALID_PARAMS); |
| 4113 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4114 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4115 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4116 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4117 | |
| 4118 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4119 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4120 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4121 | |
| 4122 | hci_dev_lock(hdev); |
| 4123 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4124 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4125 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4126 | MGMT_STATUS_BUSY); |
| 4127 | goto unlock; |
| 4128 | } |
| 4129 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4130 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4131 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4132 | hdev); |
| 4133 | goto unlock; |
| 4134 | } |
| 4135 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4136 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4137 | data, len); |
| 4138 | if (!cmd) { |
| 4139 | err = -ENOMEM; |
| 4140 | goto unlock; |
| 4141 | } |
| 4142 | |
| 4143 | hci_req_init(&req, hdev); |
| 4144 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4145 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4146 | |
| 4147 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4148 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4149 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4150 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4151 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4152 | } |
| 4153 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4154 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4155 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4156 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4157 | return err; |
| 4158 | } |
| 4159 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 4160 | static void set_bredr_scan(struct hci_request *req) |
| 4161 | { |
| 4162 | struct hci_dev *hdev = req->hdev; |
| 4163 | u8 scan = 0; |
| 4164 | |
| 4165 | /* Ensure that fast connectable is disabled. This function will |
| 4166 | * not do anything if the page scan parameters are already what |
| 4167 | * they should be. |
| 4168 | */ |
| 4169 | write_fast_connectable(req, false); |
| 4170 | |
| 4171 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4172 | scan |= SCAN_PAGE; |
| 4173 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 4174 | scan |= SCAN_INQUIRY; |
| 4175 | |
| 4176 | if (scan) |
| 4177 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 4178 | } |
| 4179 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4180 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4181 | { |
| 4182 | struct pending_cmd *cmd; |
| 4183 | |
| 4184 | BT_DBG("status 0x%02x", status); |
| 4185 | |
| 4186 | hci_dev_lock(hdev); |
| 4187 | |
| 4188 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4189 | if (!cmd) |
| 4190 | goto unlock; |
| 4191 | |
| 4192 | if (status) { |
| 4193 | u8 mgmt_err = mgmt_status(status); |
| 4194 | |
| 4195 | /* We need to restore the flag if related HCI commands |
| 4196 | * failed. |
| 4197 | */ |
| 4198 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4199 | |
| 4200 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4201 | } else { |
| 4202 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4203 | new_settings(hdev, cmd->sk); |
| 4204 | } |
| 4205 | |
| 4206 | mgmt_pending_remove(cmd); |
| 4207 | |
| 4208 | unlock: |
| 4209 | hci_dev_unlock(hdev); |
| 4210 | } |
| 4211 | |
| 4212 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4213 | { |
| 4214 | struct mgmt_mode *cp = data; |
| 4215 | struct pending_cmd *cmd; |
| 4216 | struct hci_request req; |
| 4217 | int err; |
| 4218 | |
| 4219 | BT_DBG("request for %s", hdev->name); |
| 4220 | |
| 4221 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4222 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4223 | MGMT_STATUS_NOT_SUPPORTED); |
| 4224 | |
| 4225 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4226 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4227 | MGMT_STATUS_REJECTED); |
| 4228 | |
| 4229 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4230 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4231 | MGMT_STATUS_INVALID_PARAMS); |
| 4232 | |
| 4233 | hci_dev_lock(hdev); |
| 4234 | |
| 4235 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4236 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4237 | goto unlock; |
| 4238 | } |
| 4239 | |
| 4240 | if (!hdev_is_powered(hdev)) { |
| 4241 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4242 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4243 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4244 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4245 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4246 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4247 | } |
| 4248 | |
| 4249 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4250 | |
| 4251 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4252 | if (err < 0) |
| 4253 | goto unlock; |
| 4254 | |
| 4255 | err = new_settings(hdev, sk); |
| 4256 | goto unlock; |
| 4257 | } |
| 4258 | |
| 4259 | /* Reject disabling when powered on */ |
| 4260 | if (!cp->val) { |
| 4261 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4262 | MGMT_STATUS_REJECTED); |
| 4263 | goto unlock; |
| 4264 | } |
| 4265 | |
| 4266 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4267 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4268 | MGMT_STATUS_BUSY); |
| 4269 | goto unlock; |
| 4270 | } |
| 4271 | |
| 4272 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4273 | if (!cmd) { |
| 4274 | err = -ENOMEM; |
| 4275 | goto unlock; |
| 4276 | } |
| 4277 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4278 | /* 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] | 4279 | * generates the correct flags. |
| 4280 | */ |
| 4281 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4282 | |
| 4283 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4284 | |
| 4285 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4286 | set_bredr_scan(&req); |
| 4287 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4288 | /* Since only the advertising data flags will change, there |
| 4289 | * is no need to update the scan response data. |
| 4290 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4291 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4292 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4293 | err = hci_req_run(&req, set_bredr_complete); |
| 4294 | if (err < 0) |
| 4295 | mgmt_pending_remove(cmd); |
| 4296 | |
| 4297 | unlock: |
| 4298 | hci_dev_unlock(hdev); |
| 4299 | return err; |
| 4300 | } |
| 4301 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4302 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4303 | void *data, u16 len) |
| 4304 | { |
| 4305 | struct mgmt_mode *cp = data; |
| 4306 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4307 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4308 | int err; |
| 4309 | |
| 4310 | BT_DBG("request for %s", hdev->name); |
| 4311 | |
| 4312 | status = mgmt_bredr_support(hdev); |
| 4313 | if (status) |
| 4314 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4315 | status); |
| 4316 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4317 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 4318 | !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4319 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4320 | MGMT_STATUS_NOT_SUPPORTED); |
| 4321 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4322 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4323 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4324 | MGMT_STATUS_INVALID_PARAMS); |
| 4325 | |
| 4326 | hci_dev_lock(hdev); |
| 4327 | |
| 4328 | if (!hdev_is_powered(hdev)) { |
| 4329 | bool changed; |
| 4330 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4331 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4332 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4333 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4334 | if (cp->val == 0x02) |
| 4335 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4336 | else |
| 4337 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4338 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4339 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4340 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4341 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4342 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4343 | |
| 4344 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4345 | if (err < 0) |
| 4346 | goto failed; |
| 4347 | |
| 4348 | if (changed) |
| 4349 | err = new_settings(hdev, sk); |
| 4350 | |
| 4351 | goto failed; |
| 4352 | } |
| 4353 | |
| 4354 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4355 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4356 | MGMT_STATUS_BUSY); |
| 4357 | goto failed; |
| 4358 | } |
| 4359 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4360 | val = !!cp->val; |
| 4361 | |
| 4362 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4363 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4364 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4365 | goto failed; |
| 4366 | } |
| 4367 | |
| 4368 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4369 | if (!cmd) { |
| 4370 | err = -ENOMEM; |
| 4371 | goto failed; |
| 4372 | } |
| 4373 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4374 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4375 | if (err < 0) { |
| 4376 | mgmt_pending_remove(cmd); |
| 4377 | goto failed; |
| 4378 | } |
| 4379 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4380 | if (cp->val == 0x02) |
| 4381 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4382 | else |
| 4383 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4384 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4385 | failed: |
| 4386 | hci_dev_unlock(hdev); |
| 4387 | return err; |
| 4388 | } |
| 4389 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4390 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4391 | void *data, u16 len) |
| 4392 | { |
| 4393 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4394 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4395 | int err; |
| 4396 | |
| 4397 | BT_DBG("request for %s", hdev->name); |
| 4398 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4399 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4400 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4401 | MGMT_STATUS_INVALID_PARAMS); |
| 4402 | |
| 4403 | hci_dev_lock(hdev); |
| 4404 | |
| 4405 | if (cp->val) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4406 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 4407 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4408 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4409 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 4410 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4411 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4412 | if (cp->val == 0x02) |
| 4413 | use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, |
| 4414 | &hdev->dev_flags); |
| 4415 | else |
| 4416 | use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, |
| 4417 | &hdev->dev_flags); |
| 4418 | |
| 4419 | if (hdev_is_powered(hdev) && use_changed && |
| 4420 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 4421 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4422 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4423 | sizeof(mode), &mode); |
| 4424 | } |
| 4425 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4426 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4427 | if (err < 0) |
| 4428 | goto unlock; |
| 4429 | |
| 4430 | if (changed) |
| 4431 | err = new_settings(hdev, sk); |
| 4432 | |
| 4433 | unlock: |
| 4434 | hci_dev_unlock(hdev); |
| 4435 | return err; |
| 4436 | } |
| 4437 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4438 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4439 | u16 len) |
| 4440 | { |
| 4441 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4442 | bool changed; |
| 4443 | int err; |
| 4444 | |
| 4445 | BT_DBG("request for %s", hdev->name); |
| 4446 | |
| 4447 | if (!lmp_le_capable(hdev)) |
| 4448 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4449 | MGMT_STATUS_NOT_SUPPORTED); |
| 4450 | |
| 4451 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4452 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4453 | MGMT_STATUS_INVALID_PARAMS); |
| 4454 | |
| 4455 | if (hdev_is_powered(hdev)) |
| 4456 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4457 | MGMT_STATUS_REJECTED); |
| 4458 | |
| 4459 | hci_dev_lock(hdev); |
| 4460 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4461 | /* If user space supports this command it is also expected to |
| 4462 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4463 | */ |
| 4464 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4465 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4466 | if (cp->privacy) { |
| 4467 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4468 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4469 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4470 | } else { |
| 4471 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4472 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4473 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4474 | } |
| 4475 | |
| 4476 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4477 | if (err < 0) |
| 4478 | goto unlock; |
| 4479 | |
| 4480 | if (changed) |
| 4481 | err = new_settings(hdev, sk); |
| 4482 | |
| 4483 | unlock: |
| 4484 | hci_dev_unlock(hdev); |
| 4485 | return err; |
| 4486 | } |
| 4487 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4488 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4489 | { |
| 4490 | switch (irk->addr.type) { |
| 4491 | case BDADDR_LE_PUBLIC: |
| 4492 | return true; |
| 4493 | |
| 4494 | case BDADDR_LE_RANDOM: |
| 4495 | /* Two most significant bits shall be set */ |
| 4496 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4497 | return false; |
| 4498 | return true; |
| 4499 | } |
| 4500 | |
| 4501 | return false; |
| 4502 | } |
| 4503 | |
| 4504 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4505 | u16 len) |
| 4506 | { |
| 4507 | struct mgmt_cp_load_irks *cp = cp_data; |
| 4508 | u16 irk_count, expected_len; |
| 4509 | int i, err; |
| 4510 | |
| 4511 | BT_DBG("request for %s", hdev->name); |
| 4512 | |
| 4513 | if (!lmp_le_capable(hdev)) |
| 4514 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4515 | MGMT_STATUS_NOT_SUPPORTED); |
| 4516 | |
| 4517 | irk_count = __le16_to_cpu(cp->irk_count); |
| 4518 | |
| 4519 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4520 | if (expected_len != len) { |
| 4521 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4522 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4523 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4524 | MGMT_STATUS_INVALID_PARAMS); |
| 4525 | } |
| 4526 | |
| 4527 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4528 | |
| 4529 | for (i = 0; i < irk_count; i++) { |
| 4530 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4531 | |
| 4532 | if (!irk_is_valid(key)) |
| 4533 | return cmd_status(sk, hdev->id, |
| 4534 | MGMT_OP_LOAD_IRKS, |
| 4535 | MGMT_STATUS_INVALID_PARAMS); |
| 4536 | } |
| 4537 | |
| 4538 | hci_dev_lock(hdev); |
| 4539 | |
| 4540 | hci_smp_irks_clear(hdev); |
| 4541 | |
| 4542 | for (i = 0; i < irk_count; i++) { |
| 4543 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4544 | u8 addr_type; |
| 4545 | |
| 4546 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4547 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4548 | else |
| 4549 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4550 | |
| 4551 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4552 | BDADDR_ANY); |
| 4553 | } |
| 4554 | |
| 4555 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4556 | |
| 4557 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4558 | |
| 4559 | hci_dev_unlock(hdev); |
| 4560 | |
| 4561 | return err; |
| 4562 | } |
| 4563 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4564 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4565 | { |
| 4566 | if (key->master != 0x00 && key->master != 0x01) |
| 4567 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4568 | |
| 4569 | switch (key->addr.type) { |
| 4570 | case BDADDR_LE_PUBLIC: |
| 4571 | return true; |
| 4572 | |
| 4573 | case BDADDR_LE_RANDOM: |
| 4574 | /* Two most significant bits shall be set */ |
| 4575 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4576 | return false; |
| 4577 | return true; |
| 4578 | } |
| 4579 | |
| 4580 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4581 | } |
| 4582 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4583 | 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] | 4584 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4585 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4586 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
| 4587 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4588 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4589 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4590 | BT_DBG("request for %s", hdev->name); |
| 4591 | |
| 4592 | if (!lmp_le_capable(hdev)) |
| 4593 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4594 | MGMT_STATUS_NOT_SUPPORTED); |
| 4595 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4596 | key_count = __le16_to_cpu(cp->key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4597 | |
| 4598 | expected_len = sizeof(*cp) + key_count * |
| 4599 | sizeof(struct mgmt_ltk_info); |
| 4600 | if (expected_len != len) { |
| 4601 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4602 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4603 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4604 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4605 | } |
| 4606 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4607 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4608 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4609 | for (i = 0; i < key_count; i++) { |
| 4610 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4611 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4612 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4613 | return cmd_status(sk, hdev->id, |
| 4614 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4615 | MGMT_STATUS_INVALID_PARAMS); |
| 4616 | } |
| 4617 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4618 | hci_dev_lock(hdev); |
| 4619 | |
| 4620 | hci_smp_ltks_clear(hdev); |
| 4621 | |
| 4622 | for (i = 0; i < key_count; i++) { |
| 4623 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4624 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4625 | |
| 4626 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4627 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4628 | else |
| 4629 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4630 | |
| 4631 | if (key->master) |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4632 | type = SMP_LTK; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4633 | else |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4634 | type = SMP_LTK_SLAVE; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4635 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4636 | switch (key->type) { |
| 4637 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4638 | authenticated = 0x00; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4639 | break; |
| 4640 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4641 | authenticated = 0x01; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4642 | break; |
| 4643 | default: |
| 4644 | continue; |
| 4645 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4646 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4647 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4648 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4649 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4650 | } |
| 4651 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4652 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4653 | NULL, 0); |
| 4654 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4655 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4656 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4657 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4658 | } |
| 4659 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4660 | struct cmd_conn_lookup { |
| 4661 | struct hci_conn *conn; |
| 4662 | bool valid_tx_power; |
| 4663 | u8 mgmt_status; |
| 4664 | }; |
| 4665 | |
| 4666 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 4667 | { |
| 4668 | struct cmd_conn_lookup *match = data; |
| 4669 | struct mgmt_cp_get_conn_info *cp; |
| 4670 | struct mgmt_rp_get_conn_info rp; |
| 4671 | struct hci_conn *conn = cmd->user_data; |
| 4672 | |
| 4673 | if (conn != match->conn) |
| 4674 | return; |
| 4675 | |
| 4676 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 4677 | |
| 4678 | memset(&rp, 0, sizeof(rp)); |
| 4679 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4680 | rp.addr.type = cp->addr.type; |
| 4681 | |
| 4682 | if (!match->mgmt_status) { |
| 4683 | rp.rssi = conn->rssi; |
| 4684 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4685 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4686 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4687 | rp.max_tx_power = conn->max_tx_power; |
| 4688 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4689 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4690 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 4691 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4695 | match->mgmt_status, &rp, sizeof(rp)); |
| 4696 | |
| 4697 | hci_conn_drop(conn); |
| 4698 | |
| 4699 | mgmt_pending_remove(cmd); |
| 4700 | } |
| 4701 | |
| 4702 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 4703 | { |
| 4704 | struct hci_cp_read_rssi *cp; |
| 4705 | struct hci_conn *conn; |
| 4706 | struct cmd_conn_lookup match; |
| 4707 | u16 handle; |
| 4708 | |
| 4709 | BT_DBG("status 0x%02x", status); |
| 4710 | |
| 4711 | hci_dev_lock(hdev); |
| 4712 | |
| 4713 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4714 | * otherwise we assume it's not valid. At the moment we assume that |
| 4715 | * either both or none of current and max values are valid to keep code |
| 4716 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4717 | */ |
| 4718 | match.valid_tx_power = !status; |
| 4719 | |
| 4720 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4721 | * Level so we check which one was last sent to retrieve connection |
| 4722 | * handle. Both commands have handle as first parameter so it's safe to |
| 4723 | * cast data on the same command struct. |
| 4724 | * |
| 4725 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4726 | * In other case we simply override error to indicate success as we |
| 4727 | * already remembered if TX power value is actually valid. |
| 4728 | */ |
| 4729 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4730 | if (!cp) { |
| 4731 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 4732 | status = 0; |
| 4733 | } |
| 4734 | |
| 4735 | if (!cp) { |
| 4736 | BT_ERR("invalid sent_cmd in response"); |
| 4737 | goto unlock; |
| 4738 | } |
| 4739 | |
| 4740 | handle = __le16_to_cpu(cp->handle); |
| 4741 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4742 | if (!conn) { |
| 4743 | BT_ERR("unknown handle (%d) in response", handle); |
| 4744 | goto unlock; |
| 4745 | } |
| 4746 | |
| 4747 | match.conn = conn; |
| 4748 | match.mgmt_status = mgmt_status(status); |
| 4749 | |
| 4750 | /* Cache refresh is complete, now reply for mgmt request for given |
| 4751 | * connection only. |
| 4752 | */ |
| 4753 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 4754 | get_conn_info_complete, &match); |
| 4755 | |
| 4756 | unlock: |
| 4757 | hci_dev_unlock(hdev); |
| 4758 | } |
| 4759 | |
| 4760 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4761 | u16 len) |
| 4762 | { |
| 4763 | struct mgmt_cp_get_conn_info *cp = data; |
| 4764 | struct mgmt_rp_get_conn_info rp; |
| 4765 | struct hci_conn *conn; |
| 4766 | unsigned long conn_info_age; |
| 4767 | int err = 0; |
| 4768 | |
| 4769 | BT_DBG("%s", hdev->name); |
| 4770 | |
| 4771 | memset(&rp, 0, sizeof(rp)); |
| 4772 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4773 | rp.addr.type = cp->addr.type; |
| 4774 | |
| 4775 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 4776 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4777 | MGMT_STATUS_INVALID_PARAMS, |
| 4778 | &rp, sizeof(rp)); |
| 4779 | |
| 4780 | hci_dev_lock(hdev); |
| 4781 | |
| 4782 | if (!hdev_is_powered(hdev)) { |
| 4783 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4784 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4785 | goto unlock; |
| 4786 | } |
| 4787 | |
| 4788 | if (cp->addr.type == BDADDR_BREDR) |
| 4789 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4790 | &cp->addr.bdaddr); |
| 4791 | else |
| 4792 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 4793 | |
| 4794 | if (!conn || conn->state != BT_CONNECTED) { |
| 4795 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4796 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 4797 | goto unlock; |
| 4798 | } |
| 4799 | |
| 4800 | /* To avoid client trying to guess when to poll again for information we |
| 4801 | * calculate conn info age as random value between min/max set in hdev. |
| 4802 | */ |
| 4803 | conn_info_age = hdev->conn_info_min_age + |
| 4804 | prandom_u32_max(hdev->conn_info_max_age - |
| 4805 | hdev->conn_info_min_age); |
| 4806 | |
| 4807 | /* Query controller to refresh cached values if they are too old or were |
| 4808 | * never read. |
| 4809 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 4810 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 4811 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4812 | !conn->conn_info_timestamp) { |
| 4813 | struct hci_request req; |
| 4814 | struct hci_cp_read_tx_power req_txp_cp; |
| 4815 | struct hci_cp_read_rssi req_rssi_cp; |
| 4816 | struct pending_cmd *cmd; |
| 4817 | |
| 4818 | hci_req_init(&req, hdev); |
| 4819 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 4820 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 4821 | &req_rssi_cp); |
| 4822 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 4823 | /* For LE links TX power does not change thus we don't need to |
| 4824 | * query for it once value is known. |
| 4825 | */ |
| 4826 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 4827 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 4828 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4829 | req_txp_cp.type = 0x00; |
| 4830 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4831 | sizeof(req_txp_cp), &req_txp_cp); |
| 4832 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4833 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4834 | /* Max TX power needs to be read only once per connection */ |
| 4835 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 4836 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4837 | req_txp_cp.type = 0x01; |
| 4838 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4839 | sizeof(req_txp_cp), &req_txp_cp); |
| 4840 | } |
| 4841 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4842 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 4843 | if (err < 0) |
| 4844 | goto unlock; |
| 4845 | |
| 4846 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 4847 | data, len); |
| 4848 | if (!cmd) { |
| 4849 | err = -ENOMEM; |
| 4850 | goto unlock; |
| 4851 | } |
| 4852 | |
| 4853 | hci_conn_hold(conn); |
| 4854 | cmd->user_data = conn; |
| 4855 | |
| 4856 | conn->conn_info_timestamp = jiffies; |
| 4857 | } else { |
| 4858 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 4859 | rp.rssi = conn->rssi; |
| 4860 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4861 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4862 | |
| 4863 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4864 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 4865 | } |
| 4866 | |
| 4867 | unlock: |
| 4868 | hci_dev_unlock(hdev); |
| 4869 | return err; |
| 4870 | } |
| 4871 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 4872 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status) |
| 4873 | { |
| 4874 | struct mgmt_cp_get_clock_info *cp; |
| 4875 | struct mgmt_rp_get_clock_info rp; |
| 4876 | struct hci_cp_read_clock *hci_cp; |
| 4877 | struct pending_cmd *cmd; |
| 4878 | struct hci_conn *conn; |
| 4879 | |
| 4880 | BT_DBG("%s status %u", hdev->name, status); |
| 4881 | |
| 4882 | hci_dev_lock(hdev); |
| 4883 | |
| 4884 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 4885 | if (!hci_cp) |
| 4886 | goto unlock; |
| 4887 | |
| 4888 | if (hci_cp->which) { |
| 4889 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 4890 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4891 | } else { |
| 4892 | conn = NULL; |
| 4893 | } |
| 4894 | |
| 4895 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 4896 | if (!cmd) |
| 4897 | goto unlock; |
| 4898 | |
| 4899 | cp = cmd->param; |
| 4900 | |
| 4901 | memset(&rp, 0, sizeof(rp)); |
| 4902 | memcpy(&rp.addr, &cp->addr, sizeof(rp.addr)); |
| 4903 | |
| 4904 | if (status) |
| 4905 | goto send_rsp; |
| 4906 | |
| 4907 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 4908 | |
| 4909 | if (conn) { |
| 4910 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 4911 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 4912 | } |
| 4913 | |
| 4914 | send_rsp: |
| 4915 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 4916 | &rp, sizeof(rp)); |
| 4917 | mgmt_pending_remove(cmd); |
| 4918 | if (conn) |
| 4919 | hci_conn_drop(conn); |
| 4920 | |
| 4921 | unlock: |
| 4922 | hci_dev_unlock(hdev); |
| 4923 | } |
| 4924 | |
| 4925 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4926 | u16 len) |
| 4927 | { |
| 4928 | struct mgmt_cp_get_clock_info *cp = data; |
| 4929 | struct mgmt_rp_get_clock_info rp; |
| 4930 | struct hci_cp_read_clock hci_cp; |
| 4931 | struct pending_cmd *cmd; |
| 4932 | struct hci_request req; |
| 4933 | struct hci_conn *conn; |
| 4934 | int err; |
| 4935 | |
| 4936 | BT_DBG("%s", hdev->name); |
| 4937 | |
| 4938 | memset(&rp, 0, sizeof(rp)); |
| 4939 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4940 | rp.addr.type = cp->addr.type; |
| 4941 | |
| 4942 | if (cp->addr.type != BDADDR_BREDR) |
| 4943 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 4944 | MGMT_STATUS_INVALID_PARAMS, |
| 4945 | &rp, sizeof(rp)); |
| 4946 | |
| 4947 | hci_dev_lock(hdev); |
| 4948 | |
| 4949 | if (!hdev_is_powered(hdev)) { |
| 4950 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 4951 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4952 | goto unlock; |
| 4953 | } |
| 4954 | |
| 4955 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 4956 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4957 | &cp->addr.bdaddr); |
| 4958 | if (!conn || conn->state != BT_CONNECTED) { |
| 4959 | err = cmd_complete(sk, hdev->id, |
| 4960 | MGMT_OP_GET_CLOCK_INFO, |
| 4961 | MGMT_STATUS_NOT_CONNECTED, |
| 4962 | &rp, sizeof(rp)); |
| 4963 | goto unlock; |
| 4964 | } |
| 4965 | } else { |
| 4966 | conn = NULL; |
| 4967 | } |
| 4968 | |
| 4969 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 4970 | if (!cmd) { |
| 4971 | err = -ENOMEM; |
| 4972 | goto unlock; |
| 4973 | } |
| 4974 | |
| 4975 | hci_req_init(&req, hdev); |
| 4976 | |
| 4977 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 4978 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 4979 | |
| 4980 | if (conn) { |
| 4981 | hci_conn_hold(conn); |
| 4982 | cmd->user_data = conn; |
| 4983 | |
| 4984 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 4985 | hci_cp.which = 0x01; /* Piconet clock */ |
| 4986 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 4987 | } |
| 4988 | |
| 4989 | err = hci_req_run(&req, get_clock_info_complete); |
| 4990 | if (err < 0) |
| 4991 | mgmt_pending_remove(cmd); |
| 4992 | |
| 4993 | unlock: |
| 4994 | hci_dev_unlock(hdev); |
| 4995 | return err; |
| 4996 | } |
| 4997 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 4998 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 4999 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 5000 | { |
| 5001 | struct mgmt_ev_device_added ev; |
| 5002 | |
| 5003 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5004 | ev.addr.type = type; |
| 5005 | ev.action = action; |
| 5006 | |
| 5007 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5008 | } |
| 5009 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5010 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5011 | void *data, u16 len) |
| 5012 | { |
| 5013 | struct mgmt_cp_add_device *cp = data; |
| 5014 | u8 auto_conn, addr_type; |
| 5015 | int err; |
| 5016 | |
| 5017 | BT_DBG("%s", hdev->name); |
| 5018 | |
| 5019 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5020 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
| 5021 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5022 | MGMT_STATUS_INVALID_PARAMS, |
| 5023 | &cp->addr, sizeof(cp->addr)); |
| 5024 | |
| 5025 | if (cp->action != 0x00 && cp->action != 0x01) |
| 5026 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5027 | MGMT_STATUS_INVALID_PARAMS, |
| 5028 | &cp->addr, sizeof(cp->addr)); |
| 5029 | |
| 5030 | hci_dev_lock(hdev); |
| 5031 | |
| 5032 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5033 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5034 | else |
| 5035 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5036 | |
| 5037 | if (cp->action) |
| 5038 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
| 5039 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5040 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5041 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5042 | /* If the connection parameters don't exist for this device, |
| 5043 | * they will be created and configured with defaults. |
| 5044 | */ |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5045 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
| 5046 | auto_conn) < 0) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5047 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5048 | MGMT_STATUS_FAILED, |
| 5049 | &cp->addr, sizeof(cp->addr)); |
| 5050 | goto unlock; |
| 5051 | } |
| 5052 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5053 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5054 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5055 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5056 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5057 | |
| 5058 | unlock: |
| 5059 | hci_dev_unlock(hdev); |
| 5060 | return err; |
| 5061 | } |
| 5062 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5063 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5064 | bdaddr_t *bdaddr, u8 type) |
| 5065 | { |
| 5066 | struct mgmt_ev_device_removed ev; |
| 5067 | |
| 5068 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5069 | ev.addr.type = type; |
| 5070 | |
| 5071 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5072 | } |
| 5073 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5074 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5075 | void *data, u16 len) |
| 5076 | { |
| 5077 | struct mgmt_cp_remove_device *cp = data; |
| 5078 | int err; |
| 5079 | |
| 5080 | BT_DBG("%s", hdev->name); |
| 5081 | |
| 5082 | hci_dev_lock(hdev); |
| 5083 | |
| 5084 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5085 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5086 | u8 addr_type; |
| 5087 | |
| 5088 | if (!bdaddr_type_is_le(cp->addr.type)) { |
| 5089 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5090 | MGMT_STATUS_INVALID_PARAMS, |
| 5091 | &cp->addr, sizeof(cp->addr)); |
| 5092 | goto unlock; |
| 5093 | } |
| 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 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5100 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5101 | addr_type); |
| 5102 | if (!params) { |
| 5103 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5104 | MGMT_STATUS_INVALID_PARAMS, |
| 5105 | &cp->addr, sizeof(cp->addr)); |
| 5106 | goto unlock; |
| 5107 | } |
| 5108 | |
| 5109 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED) { |
| 5110 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5111 | MGMT_STATUS_INVALID_PARAMS, |
| 5112 | &cp->addr, sizeof(cp->addr)); |
| 5113 | goto unlock; |
| 5114 | } |
| 5115 | |
| 5116 | hci_pend_le_conn_del(hdev, &cp->addr.bdaddr, addr_type); |
| 5117 | list_del(¶ms->list); |
| 5118 | kfree(params); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5119 | |
| 5120 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5121 | } else { |
| 5122 | if (cp->addr.type) { |
| 5123 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5124 | MGMT_STATUS_INVALID_PARAMS, |
| 5125 | &cp->addr, sizeof(cp->addr)); |
| 5126 | goto unlock; |
| 5127 | } |
| 5128 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5129 | hci_conn_params_clear_enabled(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5130 | } |
| 5131 | |
| 5132 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5133 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5134 | |
| 5135 | unlock: |
| 5136 | hci_dev_unlock(hdev); |
| 5137 | return err; |
| 5138 | } |
| 5139 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5140 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5141 | u16 len) |
| 5142 | { |
| 5143 | struct mgmt_cp_load_conn_param *cp = data; |
| 5144 | u16 param_count, expected_len; |
| 5145 | int i; |
| 5146 | |
| 5147 | if (!lmp_le_capable(hdev)) |
| 5148 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5149 | MGMT_STATUS_NOT_SUPPORTED); |
| 5150 | |
| 5151 | param_count = __le16_to_cpu(cp->param_count); |
| 5152 | |
| 5153 | expected_len = sizeof(*cp) + param_count * |
| 5154 | sizeof(struct mgmt_conn_param); |
| 5155 | if (expected_len != len) { |
| 5156 | BT_ERR("load_conn_param: expected %u bytes, got %u bytes", |
| 5157 | expected_len, len); |
| 5158 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5159 | MGMT_STATUS_INVALID_PARAMS); |
| 5160 | } |
| 5161 | |
| 5162 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 5163 | |
| 5164 | hci_dev_lock(hdev); |
| 5165 | |
| 5166 | hci_conn_params_clear_disabled(hdev); |
| 5167 | |
| 5168 | for (i = 0; i < param_count; i++) { |
| 5169 | struct mgmt_conn_param *param = &cp->params[i]; |
| 5170 | struct hci_conn_params *hci_param; |
| 5171 | u16 min, max, latency, timeout; |
| 5172 | u8 addr_type; |
| 5173 | |
| 5174 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 5175 | param->addr.type); |
| 5176 | |
| 5177 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 5178 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5179 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 5180 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5181 | } else { |
| 5182 | BT_ERR("Ignoring invalid connection parameters"); |
| 5183 | continue; |
| 5184 | } |
| 5185 | |
| 5186 | min = le16_to_cpu(param->min_interval); |
| 5187 | max = le16_to_cpu(param->max_interval); |
| 5188 | latency = le16_to_cpu(param->latency); |
| 5189 | timeout = le16_to_cpu(param->timeout); |
| 5190 | |
| 5191 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 5192 | min, max, latency, timeout); |
| 5193 | |
| 5194 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
| 5195 | BT_ERR("Ignoring invalid connection parameters"); |
| 5196 | continue; |
| 5197 | } |
| 5198 | |
| 5199 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 5200 | addr_type); |
| 5201 | if (!hci_param) { |
| 5202 | BT_ERR("Failed to add connection parameters"); |
| 5203 | continue; |
| 5204 | } |
| 5205 | |
| 5206 | hci_param->conn_min_interval = min; |
| 5207 | hci_param->conn_max_interval = max; |
| 5208 | hci_param->conn_latency = latency; |
| 5209 | hci_param->supervision_timeout = timeout; |
| 5210 | } |
| 5211 | |
| 5212 | hci_dev_unlock(hdev); |
| 5213 | |
| 5214 | return cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, NULL, 0); |
| 5215 | } |
| 5216 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5217 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5218 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 5219 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5220 | bool var_len; |
| 5221 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5222 | } mgmt_handlers[] = { |
| 5223 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5224 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 5225 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 5226 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 5227 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 5228 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 5229 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 5230 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 5231 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 5232 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 5233 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 5234 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 5235 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 5236 | { set_le, false, MGMT_SETTING_SIZE }, |
| 5237 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 5238 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 5239 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 5240 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 5241 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 5242 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 5243 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 5244 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 5245 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 5246 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 5247 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 5248 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 5249 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 5250 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 5251 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 5252 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 5253 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 5254 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 5255 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5256 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5257 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 5258 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 5259 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 5260 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 5261 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 5262 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5263 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5264 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5265 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5266 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5267 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5268 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5269 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5270 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5271 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5272 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5273 | { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE }, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5274 | { add_device, false, MGMT_ADD_DEVICE_SIZE }, |
| 5275 | { remove_device, false, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5276 | { load_conn_param, true, MGMT_LOAD_CONN_PARAM_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5277 | }; |
| 5278 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5279 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 5280 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5281 | void *buf; |
| 5282 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5283 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 5284 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5285 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5286 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5287 | int err; |
| 5288 | |
| 5289 | BT_DBG("got %zu bytes", msglen); |
| 5290 | |
| 5291 | if (msglen < sizeof(*hdr)) |
| 5292 | return -EINVAL; |
| 5293 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 5294 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5295 | if (!buf) |
| 5296 | return -ENOMEM; |
| 5297 | |
| 5298 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 5299 | err = -EFAULT; |
| 5300 | goto done; |
| 5301 | } |
| 5302 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5303 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5304 | opcode = __le16_to_cpu(hdr->opcode); |
| 5305 | index = __le16_to_cpu(hdr->index); |
| 5306 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5307 | |
| 5308 | if (len != msglen - sizeof(*hdr)) { |
| 5309 | err = -EINVAL; |
| 5310 | goto done; |
| 5311 | } |
| 5312 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5313 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5314 | hdev = hci_dev_get(index); |
| 5315 | if (!hdev) { |
| 5316 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5317 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5318 | goto done; |
| 5319 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5320 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 5321 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 5322 | test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) || |
| 5323 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5324 | err = cmd_status(sk, index, opcode, |
| 5325 | MGMT_STATUS_INVALID_INDEX); |
| 5326 | goto done; |
| 5327 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5328 | } |
| 5329 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5330 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5331 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5332 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5333 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5334 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5335 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5336 | } |
| 5337 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5338 | if ((hdev && opcode < MGMT_OP_READ_INFO) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5339 | (!hdev && opcode >= MGMT_OP_READ_INFO)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5340 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5341 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5342 | goto done; |
| 5343 | } |
| 5344 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5345 | handler = &mgmt_handlers[opcode]; |
| 5346 | |
| 5347 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5348 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5349 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5350 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5351 | goto done; |
| 5352 | } |
| 5353 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5354 | if (hdev) |
| 5355 | mgmt_init_hdev(sk, hdev); |
| 5356 | |
| 5357 | cp = buf + sizeof(*hdr); |
| 5358 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5359 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 5360 | if (err < 0) |
| 5361 | goto done; |
| 5362 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5363 | err = msglen; |
| 5364 | |
| 5365 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5366 | if (hdev) |
| 5367 | hci_dev_put(hdev); |
| 5368 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5369 | kfree(buf); |
| 5370 | return err; |
| 5371 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5372 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5373 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5374 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5375 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5376 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5377 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5378 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5379 | return; |
| 5380 | |
| 5381 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5382 | mgmt_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, NULL, 0, NULL); |
| 5383 | else |
| 5384 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5385 | } |
| 5386 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5387 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5388 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 5389 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5390 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5391 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5392 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5393 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5394 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5395 | return; |
| 5396 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5397 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5398 | |
Marcel Holtmann | edd3896 | 2014-07-02 21:30:55 +0200 | [diff] [blame^] | 5399 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5400 | mgmt_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, NULL, 0, NULL); |
| 5401 | else |
| 5402 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5403 | } |
| 5404 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5405 | /* This function requires the caller holds hdev->lock */ |
| 5406 | static void restart_le_auto_conns(struct hci_dev *hdev) |
| 5407 | { |
| 5408 | struct hci_conn_params *p; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5409 | bool added = false; |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5410 | |
| 5411 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5412 | if (p->auto_connect == HCI_AUTO_CONN_ALWAYS) { |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5413 | hci_pend_le_conn_add(hdev, &p->addr, p->addr_type); |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5414 | added = true; |
| 5415 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5416 | } |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5417 | |
| 5418 | /* Calling hci_pend_le_conn_add will actually already trigger |
| 5419 | * background scanning when needed. So no need to trigger it |
| 5420 | * just another time. |
| 5421 | * |
| 5422 | * This check is here to avoid an unneeded restart of the |
| 5423 | * passive scanning. Since this is during the controller |
| 5424 | * power up phase the duplicate filtering is not an issue. |
| 5425 | */ |
| 5426 | if (added) |
| 5427 | return; |
| 5428 | |
| 5429 | hci_update_background_scan(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5430 | } |
| 5431 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5432 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 5433 | { |
| 5434 | struct cmd_lookup match = { NULL, hdev }; |
| 5435 | |
| 5436 | BT_DBG("status 0x%02x", status); |
| 5437 | |
| 5438 | hci_dev_lock(hdev); |
| 5439 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5440 | restart_le_auto_conns(hdev); |
| 5441 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5442 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5443 | |
| 5444 | new_settings(hdev, match.sk); |
| 5445 | |
| 5446 | hci_dev_unlock(hdev); |
| 5447 | |
| 5448 | if (match.sk) |
| 5449 | sock_put(match.sk); |
| 5450 | } |
| 5451 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5452 | static int powered_update_hci(struct hci_dev *hdev) |
| 5453 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5454 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5455 | u8 link_sec; |
| 5456 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5457 | hci_req_init(&req, hdev); |
| 5458 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5459 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 5460 | !lmp_host_ssp_capable(hdev)) { |
| 5461 | u8 ssp = 1; |
| 5462 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5463 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5464 | } |
| 5465 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 5466 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 5467 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5468 | struct hci_cp_write_le_host_supported cp; |
| 5469 | |
| 5470 | cp.le = 1; |
| 5471 | cp.simul = lmp_le_br_capable(hdev); |
| 5472 | |
| 5473 | /* Check first if we already have the right |
| 5474 | * host state (host features set) |
| 5475 | */ |
| 5476 | if (cp.le != lmp_host_le_capable(hdev) || |
| 5477 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5478 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 5479 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5480 | } |
| 5481 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5482 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5483 | /* Make sure the controller has a good default for |
| 5484 | * advertising data. This also applies to the case |
| 5485 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 5486 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5487 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 5488 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5489 | update_scan_rsp_data(&req); |
| 5490 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5491 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 5492 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5493 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 5494 | } |
| 5495 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5496 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 5497 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5498 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 5499 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5500 | |
| 5501 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5502 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 5503 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5504 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5505 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5506 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5507 | } |
| 5508 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5509 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5510 | } |
| 5511 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5512 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5513 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 5514 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5515 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 5516 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5517 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5518 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5519 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 5520 | return 0; |
| 5521 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5522 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5523 | if (powered_update_hci(hdev) == 0) |
| 5524 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 5525 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5526 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 5527 | &match); |
| 5528 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5529 | } |
| 5530 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5531 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5532 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 5533 | |
| 5534 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 5535 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 5536 | zero_cod, sizeof(zero_cod), NULL); |
| 5537 | |
| 5538 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5539 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5540 | |
| 5541 | if (match.sk) |
| 5542 | sock_put(match.sk); |
| 5543 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5544 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5545 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5546 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5547 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5548 | { |
| 5549 | struct pending_cmd *cmd; |
| 5550 | u8 status; |
| 5551 | |
| 5552 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5553 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5554 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5555 | |
| 5556 | if (err == -ERFKILL) |
| 5557 | status = MGMT_STATUS_RFKILLED; |
| 5558 | else |
| 5559 | status = MGMT_STATUS_FAILED; |
| 5560 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5561 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5562 | |
| 5563 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5564 | } |
| 5565 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5566 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 5567 | { |
| 5568 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5569 | |
| 5570 | hci_dev_lock(hdev); |
| 5571 | |
| 5572 | /* When discoverable timeout triggers, then just make sure |
| 5573 | * the limited discoverable flag is cleared. Even in the case |
| 5574 | * of a timeout triggered from general discoverable, it is |
| 5575 | * safe to unconditionally clear the flag. |
| 5576 | */ |
| 5577 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5578 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5579 | |
| 5580 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 5581 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 5582 | u8 scan = SCAN_PAGE; |
| 5583 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 5584 | sizeof(scan), &scan); |
| 5585 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5586 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5587 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5588 | hci_req_run(&req, NULL); |
| 5589 | |
| 5590 | hdev->discov_timeout = 0; |
| 5591 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5592 | new_settings(hdev, NULL); |
| 5593 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5594 | hci_dev_unlock(hdev); |
| 5595 | } |
| 5596 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5597 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5598 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5599 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5600 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5601 | /* Nothing needed here if there's a pending command since that |
| 5602 | * commands request completion callback takes care of everything |
| 5603 | * necessary. |
| 5604 | */ |
| 5605 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5606 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5607 | |
Johan Hedberg | bd10799 | 2014-02-24 14:52:19 +0200 | [diff] [blame] | 5608 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5609 | if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5610 | return; |
| 5611 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5612 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5613 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5614 | } else { |
| 5615 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5616 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5617 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5618 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5619 | if (changed) { |
| 5620 | struct hci_request req; |
| 5621 | |
| 5622 | /* In case this change in discoverable was triggered by |
| 5623 | * a disabling of connectable there could be a need to |
| 5624 | * update the advertising flags. |
| 5625 | */ |
| 5626 | hci_req_init(&req, hdev); |
| 5627 | update_adv_data(&req); |
| 5628 | hci_req_run(&req, NULL); |
| 5629 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5630 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5631 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5632 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5633 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5634 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5635 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5636 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5637 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5638 | /* Nothing needed here if there's a pending command since that |
| 5639 | * commands request completion callback takes care of everything |
| 5640 | * necessary. |
| 5641 | */ |
| 5642 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5643 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5644 | |
Johan Hedberg | ce3f24c | 2014-02-24 14:52:20 +0200 | [diff] [blame] | 5645 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5646 | if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5647 | return; |
| 5648 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5649 | if (connectable) |
| 5650 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 5651 | else |
| 5652 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5653 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5654 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5655 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5656 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5657 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5658 | void mgmt_advertising(struct hci_dev *hdev, u8 advertising) |
| 5659 | { |
Johan Hedberg | 7c4cfab | 2014-02-24 14:52:21 +0200 | [diff] [blame] | 5660 | /* Powering off may stop advertising - don't let that interfere */ |
| 5661 | if (!advertising && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5662 | return; |
| 5663 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5664 | if (advertising) |
| 5665 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5666 | else |
| 5667 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5668 | } |
| 5669 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 5670 | 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] | 5671 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5672 | u8 mgmt_err = mgmt_status(status); |
| 5673 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5674 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5675 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5676 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5677 | |
| 5678 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5679 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5680 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5681 | } |
| 5682 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5683 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 5684 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5685 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 5686 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5687 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5688 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5689 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5690 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 5691 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5692 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5693 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 5694 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5695 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5696 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5697 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5698 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5699 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5700 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 5701 | { |
| 5702 | if (ltk->authenticated) |
| 5703 | return MGMT_LTK_AUTHENTICATED; |
| 5704 | |
| 5705 | return MGMT_LTK_UNAUTHENTICATED; |
| 5706 | } |
| 5707 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5708 | 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] | 5709 | { |
| 5710 | struct mgmt_ev_new_long_term_key ev; |
| 5711 | |
| 5712 | memset(&ev, 0, sizeof(ev)); |
| 5713 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 5714 | /* Devices using resolvable or non-resolvable random addresses |
| 5715 | * without providing an indentity resolving key don't require |
| 5716 | * to store long term keys. Their addresses will change the |
| 5717 | * next time around. |
| 5718 | * |
| 5719 | * Only when a remote device provides an identity address |
| 5720 | * make sure the long term key is stored. If the remote |
| 5721 | * identity is known, the long term keys are internally |
| 5722 | * mapped to the identity address. So allow static random |
| 5723 | * and public addresses here. |
| 5724 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5725 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5726 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5727 | ev.store_hint = 0x00; |
| 5728 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5729 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5730 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5731 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5732 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5733 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5734 | ev.key.enc_size = key->enc_size; |
| 5735 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 5736 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5737 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 5738 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5739 | ev.key.master = 1; |
| 5740 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5741 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 5742 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 5743 | 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] | 5744 | } |
| 5745 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5746 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 5747 | { |
| 5748 | struct mgmt_ev_new_irk ev; |
| 5749 | |
| 5750 | memset(&ev, 0, sizeof(ev)); |
| 5751 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 5752 | /* For identity resolving keys from devices that are already |
| 5753 | * using a public address or static random address, do not |
| 5754 | * ask for storing this key. The identity resolving key really |
| 5755 | * is only mandatory for devices using resovlable random |
| 5756 | * addresses. |
| 5757 | * |
| 5758 | * Storing all identity resolving keys has the downside that |
| 5759 | * they will be also loaded on next boot of they system. More |
| 5760 | * identity resolving keys, means more time during scanning is |
| 5761 | * needed to actually resolve these addresses. |
| 5762 | */ |
| 5763 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 5764 | ev.store_hint = 0x01; |
| 5765 | else |
| 5766 | ev.store_hint = 0x00; |
| 5767 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5768 | bacpy(&ev.rpa, &irk->rpa); |
| 5769 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 5770 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 5771 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 5772 | |
| 5773 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 5774 | } |
| 5775 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5776 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 5777 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5778 | { |
| 5779 | struct mgmt_ev_new_csrk ev; |
| 5780 | |
| 5781 | memset(&ev, 0, sizeof(ev)); |
| 5782 | |
| 5783 | /* Devices using resolvable or non-resolvable random addresses |
| 5784 | * without providing an indentity resolving key don't require |
| 5785 | * to store signature resolving keys. Their addresses will change |
| 5786 | * the next time around. |
| 5787 | * |
| 5788 | * Only when a remote device provides an identity address |
| 5789 | * make sure the signature resolving key is stored. So allow |
| 5790 | * static random and public addresses here. |
| 5791 | */ |
| 5792 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5793 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5794 | ev.store_hint = 0x00; |
| 5795 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5796 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5797 | |
| 5798 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 5799 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 5800 | ev.key.master = csrk->master; |
| 5801 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 5802 | |
| 5803 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 5804 | } |
| 5805 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5806 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 5807 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 5808 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5809 | { |
| 5810 | struct mgmt_ev_new_conn_param ev; |
| 5811 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 5812 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 5813 | return; |
| 5814 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5815 | memset(&ev, 0, sizeof(ev)); |
| 5816 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5817 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 5818 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5819 | ev.min_interval = cpu_to_le16(min_interval); |
| 5820 | ev.max_interval = cpu_to_le16(max_interval); |
| 5821 | ev.latency = cpu_to_le16(latency); |
| 5822 | ev.timeout = cpu_to_le16(timeout); |
| 5823 | |
| 5824 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 5825 | } |
| 5826 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 5827 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 5828 | u8 data_len) |
| 5829 | { |
| 5830 | eir[eir_len++] = sizeof(type) + data_len; |
| 5831 | eir[eir_len++] = type; |
| 5832 | memcpy(&eir[eir_len], data, data_len); |
| 5833 | eir_len += data_len; |
| 5834 | |
| 5835 | return eir_len; |
| 5836 | } |
| 5837 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5838 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5839 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 5840 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5841 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5842 | char buf[512]; |
| 5843 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 5844 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5845 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5846 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5847 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5848 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 5849 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 5850 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5851 | if (name_len > 0) |
| 5852 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5853 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5854 | |
| 5855 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 5856 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5857 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5858 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5859 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5860 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5861 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 5862 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5863 | } |
| 5864 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5865 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 5866 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 5867 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5868 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 5869 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5870 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5871 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5872 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5873 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5874 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5875 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5876 | |
| 5877 | *sk = cmd->sk; |
| 5878 | sock_hold(*sk); |
| 5879 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5880 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5881 | } |
| 5882 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5883 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5884 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5885 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5886 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 5887 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5888 | |
| 5889 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5890 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5891 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5892 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5893 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 5894 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5895 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5896 | |
| 5897 | mgmt_pending_remove(cmd); |
| 5898 | } |
| 5899 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5900 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5901 | u8 link_type, u8 addr_type, u8 reason, |
| 5902 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5903 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5904 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5905 | struct pending_cmd *power_off; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5906 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5907 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5908 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5909 | if (power_off) { |
| 5910 | struct mgmt_mode *cp = power_off->param; |
| 5911 | |
| 5912 | /* The connection is still in hci_conn_hash so test for 1 |
| 5913 | * instead of 0 to know if this is the last one. |
| 5914 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5915 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5916 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5917 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5918 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5919 | } |
| 5920 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5921 | if (!mgmt_connected) |
| 5922 | return; |
| 5923 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 5924 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 5925 | return; |
| 5926 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5927 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5928 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5929 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5930 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 5931 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5932 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5933 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5934 | |
| 5935 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 5936 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5937 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5938 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5939 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5940 | } |
| 5941 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5942 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5943 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5944 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5945 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 5946 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5947 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5948 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5949 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 5950 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 5951 | hdev); |
| 5952 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5953 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5954 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5955 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5956 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5957 | cp = cmd->param; |
| 5958 | |
| 5959 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 5960 | return; |
| 5961 | |
| 5962 | if (cp->addr.type != bdaddr_type) |
| 5963 | return; |
| 5964 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5965 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5966 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 5967 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5968 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 5969 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5970 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5971 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5972 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5973 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5974 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5975 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5976 | { |
| 5977 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5978 | struct pending_cmd *power_off; |
| 5979 | |
| 5980 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5981 | if (power_off) { |
| 5982 | struct mgmt_mode *cp = power_off->param; |
| 5983 | |
| 5984 | /* The connection is still in hci_conn_hash so test for 1 |
| 5985 | * instead of 0 to know if this is the last one. |
| 5986 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5987 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5988 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5989 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5990 | } |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5991 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5992 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 5993 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5994 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5995 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5996 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5997 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5998 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5999 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6000 | 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] | 6001 | { |
| 6002 | struct mgmt_ev_pin_code_request ev; |
| 6003 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6004 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6005 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 6006 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6007 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6008 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6009 | } |
| 6010 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6011 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6012 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6013 | { |
| 6014 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6015 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6016 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6017 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6018 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6019 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6020 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6021 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6022 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6023 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6024 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 6025 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6026 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6027 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6028 | } |
| 6029 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6030 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6031 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6032 | { |
| 6033 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6034 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6035 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6036 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6037 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6038 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6039 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6040 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6041 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6042 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6043 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 6044 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6045 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6046 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6047 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6048 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6049 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6050 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6051 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6052 | { |
| 6053 | struct mgmt_ev_user_confirm_request ev; |
| 6054 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6055 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6056 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6057 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6058 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 6059 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6060 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6061 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6062 | 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] | 6063 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6064 | } |
| 6065 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6066 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 6067 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6068 | { |
| 6069 | struct mgmt_ev_user_passkey_request ev; |
| 6070 | |
| 6071 | BT_DBG("%s", hdev->name); |
| 6072 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6073 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6074 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6075 | |
| 6076 | 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] | 6077 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6078 | } |
| 6079 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 6080 | 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] | 6081 | u8 link_type, u8 addr_type, u8 status, |
| 6082 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6083 | { |
| 6084 | struct pending_cmd *cmd; |
| 6085 | struct mgmt_rp_user_confirm_reply rp; |
| 6086 | int err; |
| 6087 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6088 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6089 | if (!cmd) |
| 6090 | return -ENOENT; |
| 6091 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6092 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6093 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6094 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6095 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6096 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6097 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6098 | |
| 6099 | return err; |
| 6100 | } |
| 6101 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6102 | 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] | 6103 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6104 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6105 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6106 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6107 | } |
| 6108 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6109 | 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] | 6110 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6111 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6112 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6113 | status, |
| 6114 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6115 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6116 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6117 | 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] | 6118 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6119 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6120 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6121 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6122 | } |
| 6123 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6124 | 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] | 6125 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6126 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6127 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6128 | status, |
| 6129 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6130 | } |
| 6131 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 6132 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6133 | u8 link_type, u8 addr_type, u32 passkey, |
| 6134 | u8 entered) |
| 6135 | { |
| 6136 | struct mgmt_ev_passkey_notify ev; |
| 6137 | |
| 6138 | BT_DBG("%s", hdev->name); |
| 6139 | |
| 6140 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6141 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6142 | ev.passkey = __cpu_to_le32(passkey); |
| 6143 | ev.entered = entered; |
| 6144 | |
| 6145 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 6146 | } |
| 6147 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6148 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6149 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6150 | { |
| 6151 | struct mgmt_ev_auth_failed ev; |
| 6152 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 6153 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6154 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6155 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6156 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6157 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6158 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6159 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6160 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6161 | { |
| 6162 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6163 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6164 | |
| 6165 | if (status) { |
| 6166 | u8 mgmt_err = mgmt_status(status); |
| 6167 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6168 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6169 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6170 | } |
| 6171 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6172 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 6173 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 6174 | &hdev->dev_flags); |
| 6175 | else |
| 6176 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 6177 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6178 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6179 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6180 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6181 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6182 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6183 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6184 | |
| 6185 | if (match.sk) |
| 6186 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6187 | } |
| 6188 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6189 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6190 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6191 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6192 | struct hci_cp_write_eir cp; |
| 6193 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 6194 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6195 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6196 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 6197 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 6198 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6199 | memset(&cp, 0, sizeof(cp)); |
| 6200 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6201 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6202 | } |
| 6203 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6204 | 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] | 6205 | { |
| 6206 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6207 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6208 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6209 | |
| 6210 | if (status) { |
| 6211 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6212 | |
| 6213 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6214 | &hdev->dev_flags)) { |
| 6215 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6216 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6217 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6218 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6219 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 6220 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6221 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6222 | } |
| 6223 | |
| 6224 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6225 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6226 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6227 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 6228 | if (!changed) |
| 6229 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 6230 | &hdev->dev_flags); |
| 6231 | else |
| 6232 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6233 | } |
| 6234 | |
| 6235 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 6236 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6237 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6238 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6239 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 6240 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6241 | sock_put(match.sk); |
| 6242 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6243 | hci_req_init(&req, hdev); |
| 6244 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6245 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 6246 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 6247 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6248 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6249 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6250 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6251 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6252 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6253 | |
| 6254 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6255 | } |
| 6256 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6257 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 6258 | { |
| 6259 | struct cmd_lookup match = { NULL, hdev }; |
| 6260 | bool changed = false; |
| 6261 | |
| 6262 | if (status) { |
| 6263 | u8 mgmt_err = mgmt_status(status); |
| 6264 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6265 | if (enable) { |
| 6266 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 6267 | &hdev->dev_flags)) |
| 6268 | new_settings(hdev, NULL); |
| 6269 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6270 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6271 | |
| 6272 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6273 | cmd_status_rsp, &mgmt_err); |
| 6274 | return; |
| 6275 | } |
| 6276 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6277 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6278 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6279 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6280 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6281 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6282 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6283 | |
| 6284 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6285 | settings_rsp, &match); |
| 6286 | |
| 6287 | if (changed) |
| 6288 | new_settings(hdev, match.sk); |
| 6289 | |
| 6290 | if (match.sk) |
| 6291 | sock_put(match.sk); |
| 6292 | } |
| 6293 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6294 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6295 | { |
| 6296 | struct cmd_lookup *match = data; |
| 6297 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6298 | if (match->sk == NULL) { |
| 6299 | match->sk = cmd->sk; |
| 6300 | sock_hold(match->sk); |
| 6301 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6302 | } |
| 6303 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6304 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 6305 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6306 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6307 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6308 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6309 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 6310 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 6311 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6312 | |
| 6313 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6314 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 6315 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6316 | |
| 6317 | if (match.sk) |
| 6318 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6319 | } |
| 6320 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6321 | 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] | 6322 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6323 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6324 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6325 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6326 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6327 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6328 | |
| 6329 | memset(&ev, 0, sizeof(ev)); |
| 6330 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6331 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6332 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6333 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6334 | if (!cmd) { |
| 6335 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6336 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6337 | /* If this is a HCI command related to powering on the |
| 6338 | * HCI dev don't send any mgmt signals. |
| 6339 | */ |
| 6340 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6341 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6342 | } |
| 6343 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6344 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 6345 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6346 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6347 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6348 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 6349 | u8 *randomizer192, u8 *hash256, |
| 6350 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6351 | { |
| 6352 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6353 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6354 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6355 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6356 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6357 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6358 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6359 | |
| 6360 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6361 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 6362 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6363 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6364 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 6365 | hash256 && randomizer256) { |
| 6366 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6367 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6368 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 6369 | memcpy(rp.randomizer192, randomizer192, |
| 6370 | sizeof(rp.randomizer192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6371 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6372 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
| 6373 | memcpy(rp.randomizer256, randomizer256, |
| 6374 | sizeof(rp.randomizer256)); |
| 6375 | |
| 6376 | cmd_complete(cmd->sk, hdev->id, |
| 6377 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6378 | &rp, sizeof(rp)); |
| 6379 | } else { |
| 6380 | struct mgmt_rp_read_local_oob_data rp; |
| 6381 | |
| 6382 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 6383 | memcpy(rp.randomizer, randomizer192, |
| 6384 | sizeof(rp.randomizer)); |
| 6385 | |
| 6386 | cmd_complete(cmd->sk, hdev->id, |
| 6387 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6388 | &rp, sizeof(rp)); |
| 6389 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6390 | } |
| 6391 | |
| 6392 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6393 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6394 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6395 | 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] | 6396 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 6397 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6398 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6399 | char buf[512]; |
| 6400 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6401 | struct smp_irk *irk; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6402 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6403 | |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6404 | if (!hci_discovery_active(hdev)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6405 | return; |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6406 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6407 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 6408 | * are for the potential CoD field. |
| 6409 | */ |
| 6410 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6411 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 6412 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6413 | memset(buf, 0, sizeof(buf)); |
| 6414 | |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6415 | irk = hci_get_irk(hdev, bdaddr, addr_type); |
| 6416 | if (irk) { |
| 6417 | bacpy(&ev->addr.bdaddr, &irk->bdaddr); |
| 6418 | ev->addr.type = link_to_bdaddr(link_type, irk->addr_type); |
| 6419 | } else { |
| 6420 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 6421 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
| 6422 | } |
| 6423 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6424 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 6425 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6426 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6427 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6428 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6429 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6430 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 6431 | 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] | 6432 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6433 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6434 | if (scan_rsp_len > 0) |
| 6435 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
| 6436 | |
| 6437 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 6438 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 6439 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6440 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6441 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6442 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6443 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6444 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6445 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6446 | struct mgmt_ev_device_found *ev; |
| 6447 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 6448 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6449 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6450 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6451 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6452 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6453 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6454 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6455 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6456 | ev->rssi = rssi; |
| 6457 | |
| 6458 | 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] | 6459 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6460 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6461 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6462 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6463 | 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] | 6464 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6465 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6466 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6467 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6468 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6469 | struct pending_cmd *cmd; |
| 6470 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 6471 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 6472 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6473 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6474 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6475 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6476 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6477 | |
| 6478 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 6479 | u8 type = hdev->discovery.type; |
| 6480 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6481 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 6482 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6483 | mgmt_pending_remove(cmd); |
| 6484 | } |
| 6485 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6486 | memset(&ev, 0, sizeof(ev)); |
| 6487 | ev.type = hdev->discovery.type; |
| 6488 | ev.discovering = discovering; |
| 6489 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6490 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6491 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6492 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6493 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 6494 | { |
| 6495 | BT_DBG("%s status %u", hdev->name, status); |
| 6496 | |
| 6497 | /* Clear the advertising mgmt setting if we failed to re-enable it */ |
| 6498 | if (status) { |
| 6499 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6500 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6501 | } |
| 6502 | } |
| 6503 | |
| 6504 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 6505 | { |
| 6506 | struct hci_request req; |
| 6507 | |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 6508 | if (hci_conn_num(hdev, LE_LINK) > 0) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6509 | return; |
| 6510 | |
| 6511 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6512 | return; |
| 6513 | |
| 6514 | hci_req_init(&req, hdev); |
| 6515 | enable_advertising(&req); |
| 6516 | |
| 6517 | /* If this fails we have no option but to let user space know |
| 6518 | * that we've disabled advertising. |
| 6519 | */ |
| 6520 | if (hci_req_run(&req, adv_enable_complete) < 0) { |
| 6521 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6522 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6523 | } |
| 6524 | } |