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, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 122 | }; |
| 123 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 124 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 125 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 126 | #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \ |
| 127 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 128 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 129 | struct pending_cmd { |
| 130 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 131 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 132 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 133 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 134 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 135 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 136 | }; |
| 137 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 138 | /* HCI to MGMT error code conversion table */ |
| 139 | static u8 mgmt_status_table[] = { |
| 140 | MGMT_STATUS_SUCCESS, |
| 141 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 142 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 143 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 144 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 145 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 146 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 147 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 148 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 149 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 150 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 151 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 152 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 153 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 154 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 155 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 156 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 157 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 158 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 159 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 160 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 161 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 162 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 163 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 164 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 165 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 166 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 167 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 168 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 169 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 170 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 171 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 172 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 173 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 174 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 175 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 176 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 177 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 178 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 179 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 180 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 181 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 182 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 183 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 184 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 185 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 186 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 187 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 188 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 189 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 190 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 191 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 192 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 193 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 194 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 195 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 196 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 197 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 198 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 199 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 200 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 201 | }; |
| 202 | |
| 203 | static u8 mgmt_status(u8 hci_status) |
| 204 | { |
| 205 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 206 | return mgmt_status_table[hci_status]; |
| 207 | |
| 208 | return MGMT_STATUS_FAILED; |
| 209 | } |
| 210 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 211 | static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status) |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 212 | { |
| 213 | struct sk_buff *skb; |
| 214 | struct mgmt_hdr *hdr; |
| 215 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 216 | int err; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 217 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 218 | BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 219 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 220 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 221 | if (!skb) |
| 222 | return -ENOMEM; |
| 223 | |
| 224 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 225 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 226 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 227 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 228 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 229 | |
| 230 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 231 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 232 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 233 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 234 | err = sock_queue_rcv_skb(sk, skb); |
| 235 | if (err < 0) |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 236 | kfree_skb(skb); |
| 237 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 238 | return err; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 241 | 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] | 242 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 243 | { |
| 244 | struct sk_buff *skb; |
| 245 | struct mgmt_hdr *hdr; |
| 246 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 247 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 248 | |
| 249 | BT_DBG("sock %p", sk); |
| 250 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 251 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 252 | if (!skb) |
| 253 | return -ENOMEM; |
| 254 | |
| 255 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 256 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 257 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 258 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 259 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 260 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 261 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 262 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 263 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 264 | |
| 265 | if (rp) |
| 266 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 267 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 268 | err = sock_queue_rcv_skb(sk, skb); |
| 269 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 270 | kfree_skb(skb); |
| 271 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 272 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 275 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 276 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 277 | { |
| 278 | struct mgmt_rp_read_version rp; |
| 279 | |
| 280 | BT_DBG("sock %p", sk); |
| 281 | |
| 282 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 283 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 284 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 285 | 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] | 286 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 287 | } |
| 288 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 289 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 290 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 291 | { |
| 292 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 293 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 294 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 295 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 296 | size_t rp_size; |
| 297 | int i, err; |
| 298 | |
| 299 | BT_DBG("sock %p", sk); |
| 300 | |
| 301 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 302 | |
| 303 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 304 | if (!rp) |
| 305 | return -ENOMEM; |
| 306 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 307 | rp->num_commands = cpu_to_le16(num_commands); |
| 308 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 309 | |
| 310 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 311 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 312 | |
| 313 | for (i = 0; i < num_events; i++, opcode++) |
| 314 | put_unaligned_le16(mgmt_events[i], opcode); |
| 315 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 316 | 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] | 317 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 318 | kfree(rp); |
| 319 | |
| 320 | return err; |
| 321 | } |
| 322 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 323 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 324 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 325 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 326 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 327 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 328 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 329 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 330 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 331 | |
| 332 | BT_DBG("sock %p", sk); |
| 333 | |
| 334 | read_lock(&hci_dev_list_lock); |
| 335 | |
| 336 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 337 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 338 | if (d->dev_type == HCI_BREDR) |
| 339 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 340 | } |
| 341 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 342 | rp_len = sizeof(*rp) + (2 * count); |
| 343 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 344 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 345 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 346 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 347 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 348 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 349 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 350 | list_for_each_entry(d, &hci_dev_list, list) { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 351 | if (test_bit(HCI_SETUP, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 352 | continue; |
| 353 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 354 | if (test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 355 | continue; |
| 356 | |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 357 | if (test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 358 | continue; |
| 359 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 360 | if (d->dev_type == HCI_BREDR) { |
| 361 | rp->index[count++] = cpu_to_le16(d->id); |
| 362 | BT_DBG("Added hci%u", d->id); |
| 363 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 364 | } |
| 365 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 366 | rp->num_controllers = cpu_to_le16(count); |
| 367 | rp_len = sizeof(*rp) + (2 * count); |
| 368 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 369 | read_unlock(&hci_dev_list_lock); |
| 370 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 371 | 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] | 372 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 373 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 374 | kfree(rp); |
| 375 | |
| 376 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 377 | } |
| 378 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 379 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 380 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 381 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 382 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 383 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 384 | settings |= MGMT_SETTING_PAIRABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 385 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 386 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 387 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 388 | settings |= MGMT_SETTING_CONNECTABLE; |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 389 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 390 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 391 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 392 | settings |= MGMT_SETTING_BREDR; |
| 393 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 394 | |
| 395 | if (lmp_ssp_capable(hdev)) { |
| 396 | settings |= MGMT_SETTING_SSP; |
| 397 | settings |= MGMT_SETTING_HS; |
| 398 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 399 | |
Marcel Holtmann | 5afeac14 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 400 | if (lmp_sc_capable(hdev) || |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 401 | test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 402 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 403 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 404 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 405 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 406 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 407 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 408 | settings |= MGMT_SETTING_PRIVACY; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 409 | } |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 410 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 411 | return settings; |
| 412 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 413 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 414 | static u32 get_current_settings(struct hci_dev *hdev) |
| 415 | { |
| 416 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 417 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 418 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 419 | settings |= MGMT_SETTING_POWERED; |
| 420 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 421 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 422 | settings |= MGMT_SETTING_CONNECTABLE; |
| 423 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 424 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 425 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 426 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 427 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 428 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 429 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 430 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 431 | settings |= MGMT_SETTING_PAIRABLE; |
| 432 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 433 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 434 | settings |= MGMT_SETTING_BREDR; |
| 435 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 436 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 437 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 438 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 439 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 440 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 441 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 442 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 443 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 444 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 445 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 446 | settings |= MGMT_SETTING_HS; |
| 447 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 448 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 449 | settings |= MGMT_SETTING_ADVERTISING; |
| 450 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 451 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 452 | settings |= MGMT_SETTING_SECURE_CONN; |
| 453 | |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 454 | if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 455 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 456 | |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 457 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 458 | settings |= MGMT_SETTING_PRIVACY; |
| 459 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 460 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 461 | } |
| 462 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 463 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 464 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 465 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 466 | { |
| 467 | u8 *ptr = data, *uuids_start = NULL; |
| 468 | struct bt_uuid *uuid; |
| 469 | |
| 470 | if (len < 4) |
| 471 | return ptr; |
| 472 | |
| 473 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 474 | u16 uuid16; |
| 475 | |
| 476 | if (uuid->size != 16) |
| 477 | continue; |
| 478 | |
| 479 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 480 | if (uuid16 < 0x1100) |
| 481 | continue; |
| 482 | |
| 483 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 484 | continue; |
| 485 | |
| 486 | if (!uuids_start) { |
| 487 | uuids_start = ptr; |
| 488 | uuids_start[0] = 1; |
| 489 | uuids_start[1] = EIR_UUID16_ALL; |
| 490 | ptr += 2; |
| 491 | } |
| 492 | |
| 493 | /* Stop if not enough space to put next UUID */ |
| 494 | if ((ptr - data) + sizeof(u16) > len) { |
| 495 | uuids_start[1] = EIR_UUID16_SOME; |
| 496 | break; |
| 497 | } |
| 498 | |
| 499 | *ptr++ = (uuid16 & 0x00ff); |
| 500 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 501 | uuids_start[0] += sizeof(uuid16); |
| 502 | } |
| 503 | |
| 504 | return ptr; |
| 505 | } |
| 506 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 507 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 508 | { |
| 509 | u8 *ptr = data, *uuids_start = NULL; |
| 510 | struct bt_uuid *uuid; |
| 511 | |
| 512 | if (len < 6) |
| 513 | return ptr; |
| 514 | |
| 515 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 516 | if (uuid->size != 32) |
| 517 | continue; |
| 518 | |
| 519 | if (!uuids_start) { |
| 520 | uuids_start = ptr; |
| 521 | uuids_start[0] = 1; |
| 522 | uuids_start[1] = EIR_UUID32_ALL; |
| 523 | ptr += 2; |
| 524 | } |
| 525 | |
| 526 | /* Stop if not enough space to put next UUID */ |
| 527 | if ((ptr - data) + sizeof(u32) > len) { |
| 528 | uuids_start[1] = EIR_UUID32_SOME; |
| 529 | break; |
| 530 | } |
| 531 | |
| 532 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 533 | ptr += sizeof(u32); |
| 534 | uuids_start[0] += sizeof(u32); |
| 535 | } |
| 536 | |
| 537 | return ptr; |
| 538 | } |
| 539 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 540 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 541 | { |
| 542 | u8 *ptr = data, *uuids_start = NULL; |
| 543 | struct bt_uuid *uuid; |
| 544 | |
| 545 | if (len < 18) |
| 546 | return ptr; |
| 547 | |
| 548 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 549 | if (uuid->size != 128) |
| 550 | continue; |
| 551 | |
| 552 | if (!uuids_start) { |
| 553 | uuids_start = ptr; |
| 554 | uuids_start[0] = 1; |
| 555 | uuids_start[1] = EIR_UUID128_ALL; |
| 556 | ptr += 2; |
| 557 | } |
| 558 | |
| 559 | /* Stop if not enough space to put next UUID */ |
| 560 | if ((ptr - data) + 16 > len) { |
| 561 | uuids_start[1] = EIR_UUID128_SOME; |
| 562 | break; |
| 563 | } |
| 564 | |
| 565 | memcpy(ptr, uuid->uuid, 16); |
| 566 | ptr += 16; |
| 567 | uuids_start[0] += 16; |
| 568 | } |
| 569 | |
| 570 | return ptr; |
| 571 | } |
| 572 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 573 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 574 | { |
| 575 | struct pending_cmd *cmd; |
| 576 | |
| 577 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 578 | if (cmd->opcode == opcode) |
| 579 | return cmd; |
| 580 | } |
| 581 | |
| 582 | return NULL; |
| 583 | } |
| 584 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 585 | static struct pending_cmd *mgmt_pending_find_data(u16 opcode, |
| 586 | struct hci_dev *hdev, |
| 587 | const void *data) |
| 588 | { |
| 589 | struct pending_cmd *cmd; |
| 590 | |
| 591 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 592 | if (cmd->user_data != data) |
| 593 | continue; |
| 594 | if (cmd->opcode == opcode) |
| 595 | return cmd; |
| 596 | } |
| 597 | |
| 598 | return NULL; |
| 599 | } |
| 600 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 601 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 602 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 603 | u8 ad_len = 0; |
| 604 | size_t name_len; |
| 605 | |
| 606 | name_len = strlen(hdev->dev_name); |
| 607 | if (name_len > 0) { |
| 608 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 609 | |
| 610 | if (name_len > max_len) { |
| 611 | name_len = max_len; |
| 612 | ptr[1] = EIR_NAME_SHORT; |
| 613 | } else |
| 614 | ptr[1] = EIR_NAME_COMPLETE; |
| 615 | |
| 616 | ptr[0] = name_len + 1; |
| 617 | |
| 618 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 619 | |
| 620 | ad_len += (name_len + 2); |
| 621 | ptr += (name_len + 2); |
| 622 | } |
| 623 | |
| 624 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static void update_scan_rsp_data(struct hci_request *req) |
| 628 | { |
| 629 | struct hci_dev *hdev = req->hdev; |
| 630 | struct hci_cp_le_set_scan_rsp_data cp; |
| 631 | u8 len; |
| 632 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 633 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 634 | return; |
| 635 | |
| 636 | memset(&cp, 0, sizeof(cp)); |
| 637 | |
| 638 | len = create_scan_rsp_data(hdev, cp.data); |
| 639 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 640 | if (hdev->scan_rsp_data_len == len && |
| 641 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 642 | return; |
| 643 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 644 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 645 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 646 | |
| 647 | cp.length = len; |
| 648 | |
| 649 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 650 | } |
| 651 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 652 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 653 | { |
| 654 | struct pending_cmd *cmd; |
| 655 | |
| 656 | /* If there's a pending mgmt command the flags will not yet have |
| 657 | * their final values, so check for this first. |
| 658 | */ |
| 659 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 660 | if (cmd) { |
| 661 | struct mgmt_mode *cp = cmd->param; |
| 662 | if (cp->val == 0x01) |
| 663 | return LE_AD_GENERAL; |
| 664 | else if (cp->val == 0x02) |
| 665 | return LE_AD_LIMITED; |
| 666 | } else { |
| 667 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 668 | return LE_AD_LIMITED; |
| 669 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 670 | return LE_AD_GENERAL; |
| 671 | } |
| 672 | |
| 673 | return 0; |
| 674 | } |
| 675 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 676 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 677 | { |
| 678 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 679 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 680 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 681 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 682 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 683 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 684 | |
| 685 | if (flags) { |
| 686 | BT_DBG("adv flags 0x%02x", flags); |
| 687 | |
| 688 | ptr[0] = 2; |
| 689 | ptr[1] = EIR_FLAGS; |
| 690 | ptr[2] = flags; |
| 691 | |
| 692 | ad_len += 3; |
| 693 | ptr += 3; |
| 694 | } |
| 695 | |
| 696 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 697 | ptr[0] = 2; |
| 698 | ptr[1] = EIR_TX_POWER; |
| 699 | ptr[2] = (u8) hdev->adv_tx_power; |
| 700 | |
| 701 | ad_len += 3; |
| 702 | ptr += 3; |
| 703 | } |
| 704 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 705 | return ad_len; |
| 706 | } |
| 707 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 708 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 709 | { |
| 710 | struct hci_dev *hdev = req->hdev; |
| 711 | struct hci_cp_le_set_adv_data cp; |
| 712 | u8 len; |
| 713 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 714 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 715 | return; |
| 716 | |
| 717 | memset(&cp, 0, sizeof(cp)); |
| 718 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 719 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 720 | |
| 721 | if (hdev->adv_data_len == len && |
| 722 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 723 | return; |
| 724 | |
| 725 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 726 | hdev->adv_data_len = len; |
| 727 | |
| 728 | cp.length = len; |
| 729 | |
| 730 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 731 | } |
| 732 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 733 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 734 | { |
| 735 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 736 | size_t name_len; |
| 737 | |
| 738 | name_len = strlen(hdev->dev_name); |
| 739 | |
| 740 | if (name_len > 0) { |
| 741 | /* EIR Data type */ |
| 742 | if (name_len > 48) { |
| 743 | name_len = 48; |
| 744 | ptr[1] = EIR_NAME_SHORT; |
| 745 | } else |
| 746 | ptr[1] = EIR_NAME_COMPLETE; |
| 747 | |
| 748 | /* EIR Data length */ |
| 749 | ptr[0] = name_len + 1; |
| 750 | |
| 751 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 752 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 753 | ptr += (name_len + 2); |
| 754 | } |
| 755 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 756 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 757 | ptr[0] = 2; |
| 758 | ptr[1] = EIR_TX_POWER; |
| 759 | ptr[2] = (u8) hdev->inq_tx_power; |
| 760 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 761 | ptr += 3; |
| 762 | } |
| 763 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 764 | if (hdev->devid_source > 0) { |
| 765 | ptr[0] = 9; |
| 766 | ptr[1] = EIR_DEVICE_ID; |
| 767 | |
| 768 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 769 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 770 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 771 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 772 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 773 | ptr += 10; |
| 774 | } |
| 775 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 776 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 777 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 778 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 779 | } |
| 780 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 781 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 782 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 783 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 784 | struct hci_cp_write_eir cp; |
| 785 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 786 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 787 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 788 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 789 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 790 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 791 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 792 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 793 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 794 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 795 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 796 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 797 | |
| 798 | memset(&cp, 0, sizeof(cp)); |
| 799 | |
| 800 | create_eir(hdev, cp.data); |
| 801 | |
| 802 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 803 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 804 | |
| 805 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 806 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 807 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 808 | } |
| 809 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 810 | static u8 get_service_classes(struct hci_dev *hdev) |
| 811 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 812 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 813 | u8 val = 0; |
| 814 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 815 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 816 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 817 | |
| 818 | return val; |
| 819 | } |
| 820 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 821 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 822 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 823 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 824 | u8 cod[3]; |
| 825 | |
| 826 | BT_DBG("%s", hdev->name); |
| 827 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 828 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 829 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 830 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 831 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 832 | return; |
| 833 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 834 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 835 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 836 | |
| 837 | cod[0] = hdev->minor_class; |
| 838 | cod[1] = hdev->major_class; |
| 839 | cod[2] = get_service_classes(hdev); |
| 840 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 841 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 842 | cod[1] |= 0x20; |
| 843 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 844 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 845 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 846 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 847 | 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] | 848 | } |
| 849 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 850 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 851 | { |
| 852 | struct pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 853 | |
| 854 | /* If there's a pending mgmt command the flag will not yet have |
| 855 | * it's final value, so check for this first. |
| 856 | */ |
| 857 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 858 | if (cmd) { |
| 859 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 860 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 861 | } |
| 862 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 863 | return test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | static void enable_advertising(struct hci_request *req) |
| 867 | { |
| 868 | struct hci_dev *hdev = req->hdev; |
| 869 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 870 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 871 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 872 | |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 873 | /* Clear the HCI_ADVERTISING bit temporarily so that the |
| 874 | * hci_update_random_address knows that it's safe to go ahead |
| 875 | * and write a new random address. The flag will be set back on |
| 876 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 877 | */ |
| 878 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 879 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 880 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 881 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 882 | /* Set require_privacy to true only when non-connectable |
| 883 | * advertising is used. In that case it is fine to use a |
| 884 | * non-resolvable private address. |
| 885 | */ |
| 886 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 887 | return; |
| 888 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 889 | memset(&cp, 0, sizeof(cp)); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 890 | cp.min_interval = cpu_to_le16(0x0800); |
| 891 | cp.max_interval = cpu_to_le16(0x0800); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 892 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 893 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 894 | cp.channel_map = hdev->le_adv_channel_map; |
| 895 | |
| 896 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 897 | |
| 898 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 899 | } |
| 900 | |
| 901 | static void disable_advertising(struct hci_request *req) |
| 902 | { |
| 903 | u8 enable = 0x00; |
| 904 | |
| 905 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 906 | } |
| 907 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 908 | static void service_cache_off(struct work_struct *work) |
| 909 | { |
| 910 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 911 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 912 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 913 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 914 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 915 | return; |
| 916 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 917 | hci_req_init(&req, hdev); |
| 918 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 919 | hci_dev_lock(hdev); |
| 920 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 921 | update_eir(&req); |
| 922 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 923 | |
| 924 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 925 | |
| 926 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 927 | } |
| 928 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 929 | static void rpa_expired(struct work_struct *work) |
| 930 | { |
| 931 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 932 | rpa_expired.work); |
| 933 | struct hci_request req; |
| 934 | |
| 935 | BT_DBG(""); |
| 936 | |
| 937 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 938 | |
| 939 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags) || |
| 940 | hci_conn_num(hdev, LE_LINK) > 0) |
| 941 | return; |
| 942 | |
| 943 | /* The generation of a new RPA and programming it into the |
| 944 | * controller happens in the enable_advertising() function. |
| 945 | */ |
| 946 | |
| 947 | hci_req_init(&req, hdev); |
| 948 | |
| 949 | disable_advertising(&req); |
| 950 | enable_advertising(&req); |
| 951 | |
| 952 | hci_req_run(&req, NULL); |
| 953 | } |
| 954 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 955 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 956 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 957 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 958 | return; |
| 959 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 960 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 961 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 962 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 963 | /* Non-mgmt controlled devices get this bit set |
| 964 | * implicitly so that pairing works for them, however |
| 965 | * for mgmt we require user-space to explicitly enable |
| 966 | * it |
| 967 | */ |
| 968 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 969 | } |
| 970 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 971 | 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] | 972 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 973 | { |
| 974 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 975 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 976 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 977 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 978 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 979 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 980 | memset(&rp, 0, sizeof(rp)); |
| 981 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 982 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 983 | |
| 984 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 985 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 986 | |
| 987 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 988 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 989 | |
| 990 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 991 | |
| 992 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 993 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 994 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 995 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 996 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 997 | 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] | 998 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 1002 | { |
| 1003 | sock_put(cmd->sk); |
| 1004 | kfree(cmd->param); |
| 1005 | kfree(cmd); |
| 1006 | } |
| 1007 | |
| 1008 | 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] | 1009 | struct hci_dev *hdev, void *data, |
| 1010 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1011 | { |
| 1012 | struct pending_cmd *cmd; |
| 1013 | |
Johan Hedberg | fca2001 | 2014-06-28 17:54:05 +0300 | [diff] [blame] | 1014 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1015 | if (!cmd) |
| 1016 | return NULL; |
| 1017 | |
| 1018 | cmd->opcode = opcode; |
| 1019 | cmd->index = hdev->id; |
| 1020 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 1021 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1022 | if (!cmd->param) { |
| 1023 | kfree(cmd); |
| 1024 | return NULL; |
| 1025 | } |
| 1026 | |
| 1027 | if (data) |
| 1028 | memcpy(cmd->param, data, len); |
| 1029 | |
| 1030 | cmd->sk = sk; |
| 1031 | sock_hold(sk); |
| 1032 | |
| 1033 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1034 | |
| 1035 | return cmd; |
| 1036 | } |
| 1037 | |
| 1038 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1039 | void (*cb)(struct pending_cmd *cmd, |
| 1040 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1041 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1042 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1043 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1044 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1045 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1046 | if (opcode > 0 && cmd->opcode != opcode) |
| 1047 | continue; |
| 1048 | |
| 1049 | cb(cmd, data); |
| 1050 | } |
| 1051 | } |
| 1052 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1053 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 1054 | { |
| 1055 | list_del(&cmd->list); |
| 1056 | mgmt_pending_free(cmd); |
| 1057 | } |
| 1058 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1059 | 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] | 1060 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1061 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1062 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1063 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1064 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1065 | } |
| 1066 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1067 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status) |
| 1068 | { |
| 1069 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1070 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1071 | if (hci_conn_count(hdev) == 0) { |
| 1072 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1073 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1074 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1075 | } |
| 1076 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1077 | static void hci_stop_discovery(struct hci_request *req) |
| 1078 | { |
| 1079 | struct hci_dev *hdev = req->hdev; |
| 1080 | struct hci_cp_remote_name_req_cancel cp; |
| 1081 | struct inquiry_entry *e; |
| 1082 | |
| 1083 | switch (hdev->discovery.state) { |
| 1084 | case DISCOVERY_FINDING: |
| 1085 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 1086 | hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 1087 | } else { |
| 1088 | cancel_delayed_work(&hdev->le_scan_disable); |
| 1089 | hci_req_add_le_scan_disable(req); |
| 1090 | } |
| 1091 | |
| 1092 | break; |
| 1093 | |
| 1094 | case DISCOVERY_RESOLVING: |
| 1095 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 1096 | NAME_PENDING); |
| 1097 | if (!e) |
| 1098 | return; |
| 1099 | |
| 1100 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 1101 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 1102 | &cp); |
| 1103 | |
| 1104 | break; |
| 1105 | |
| 1106 | default: |
| 1107 | /* Passive scanning */ |
| 1108 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 1109 | hci_req_add_le_scan_disable(req); |
| 1110 | break; |
| 1111 | } |
| 1112 | } |
| 1113 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1114 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1115 | { |
| 1116 | struct hci_request req; |
| 1117 | struct hci_conn *conn; |
| 1118 | |
| 1119 | hci_req_init(&req, hdev); |
| 1120 | |
| 1121 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1122 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1123 | u8 scan = 0x00; |
| 1124 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1125 | } |
| 1126 | |
| 1127 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 1128 | disable_advertising(&req); |
| 1129 | |
Johan Hedberg | f8680f1 | 2014-06-10 14:05:59 +0300 | [diff] [blame] | 1130 | hci_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1131 | |
| 1132 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1133 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1134 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1135 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1136 | switch (conn->state) { |
| 1137 | case BT_CONNECTED: |
| 1138 | case BT_CONFIG: |
| 1139 | dc.handle = cpu_to_le16(conn->handle); |
| 1140 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1141 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1142 | break; |
| 1143 | case BT_CONNECT: |
| 1144 | if (conn->type == LE_LINK) |
| 1145 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1146 | 0, NULL); |
| 1147 | else if (conn->type == ACL_LINK) |
| 1148 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1149 | 6, &conn->dst); |
| 1150 | break; |
| 1151 | case BT_CONNECT2: |
| 1152 | bacpy(&rej.bdaddr, &conn->dst); |
| 1153 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1154 | if (conn->type == ACL_LINK) |
| 1155 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1156 | sizeof(rej), &rej); |
| 1157 | else if (conn->type == SCO_LINK) |
| 1158 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1159 | sizeof(rej), &rej); |
| 1160 | break; |
| 1161 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | return hci_req_run(&req, clean_up_hci_complete); |
| 1165 | } |
| 1166 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1167 | 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] | 1168 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1169 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1170 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1171 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1172 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1173 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1174 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1175 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1176 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1177 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1178 | MGMT_STATUS_INVALID_PARAMS); |
| 1179 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1180 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1181 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1182 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 1183 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1184 | MGMT_STATUS_BUSY); |
| 1185 | goto failed; |
| 1186 | } |
| 1187 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1188 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1189 | cancel_delayed_work(&hdev->power_off); |
| 1190 | |
| 1191 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1192 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1193 | data, len); |
| 1194 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1195 | goto failed; |
| 1196 | } |
| 1197 | } |
| 1198 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1199 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1200 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1201 | goto failed; |
| 1202 | } |
| 1203 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1204 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1205 | if (!cmd) { |
| 1206 | err = -ENOMEM; |
| 1207 | goto failed; |
| 1208 | } |
| 1209 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1210 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1211 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1212 | err = 0; |
| 1213 | } else { |
| 1214 | /* Disconnect connections, stop scans, etc */ |
| 1215 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1216 | if (!err) |
| 1217 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1218 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1219 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1220 | /* ENODATA means there were no HCI commands queued */ |
| 1221 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1222 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1223 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1224 | err = 0; |
| 1225 | } |
| 1226 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1227 | |
| 1228 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1229 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1230 | return err; |
| 1231 | } |
| 1232 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1233 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 1234 | struct sock *skip_sk) |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1235 | { |
| 1236 | struct sk_buff *skb; |
| 1237 | struct mgmt_hdr *hdr; |
| 1238 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 1239 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1240 | if (!skb) |
| 1241 | return -ENOMEM; |
| 1242 | |
| 1243 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 1244 | hdr->opcode = cpu_to_le16(event); |
| 1245 | if (hdev) |
| 1246 | hdr->index = cpu_to_le16(hdev->id); |
| 1247 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1248 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1249 | hdr->len = cpu_to_le16(data_len); |
| 1250 | |
| 1251 | if (data) |
| 1252 | memcpy(skb_put(skb, data_len), data, data_len); |
| 1253 | |
Marcel Holtmann | 97e0bde | 2012-02-22 13:49:28 +0100 | [diff] [blame] | 1254 | /* Time stamp */ |
| 1255 | __net_timestamp(skb); |
| 1256 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1257 | hci_send_to_control(skb, skip_sk); |
| 1258 | kfree_skb(skb); |
| 1259 | |
| 1260 | return 0; |
| 1261 | } |
| 1262 | |
| 1263 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1264 | { |
| 1265 | __le32 ev; |
| 1266 | |
| 1267 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1268 | |
| 1269 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1270 | } |
| 1271 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1272 | struct cmd_lookup { |
| 1273 | struct sock *sk; |
| 1274 | struct hci_dev *hdev; |
| 1275 | u8 mgmt_status; |
| 1276 | }; |
| 1277 | |
| 1278 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1279 | { |
| 1280 | struct cmd_lookup *match = data; |
| 1281 | |
| 1282 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1283 | |
| 1284 | list_del(&cmd->list); |
| 1285 | |
| 1286 | if (match->sk == NULL) { |
| 1287 | match->sk = cmd->sk; |
| 1288 | sock_hold(match->sk); |
| 1289 | } |
| 1290 | |
| 1291 | mgmt_pending_free(cmd); |
| 1292 | } |
| 1293 | |
| 1294 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1295 | { |
| 1296 | u8 *status = data; |
| 1297 | |
| 1298 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1299 | mgmt_pending_remove(cmd); |
| 1300 | } |
| 1301 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1302 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1303 | { |
| 1304 | if (!lmp_bredr_capable(hdev)) |
| 1305 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1306 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1307 | return MGMT_STATUS_REJECTED; |
| 1308 | else |
| 1309 | return MGMT_STATUS_SUCCESS; |
| 1310 | } |
| 1311 | |
| 1312 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1313 | { |
| 1314 | if (!lmp_le_capable(hdev)) |
| 1315 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1316 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1317 | return MGMT_STATUS_REJECTED; |
| 1318 | else |
| 1319 | return MGMT_STATUS_SUCCESS; |
| 1320 | } |
| 1321 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1322 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1323 | { |
| 1324 | struct pending_cmd *cmd; |
| 1325 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1326 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1327 | bool changed; |
| 1328 | |
| 1329 | BT_DBG("status 0x%02x", status); |
| 1330 | |
| 1331 | hci_dev_lock(hdev); |
| 1332 | |
| 1333 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1334 | if (!cmd) |
| 1335 | goto unlock; |
| 1336 | |
| 1337 | if (status) { |
| 1338 | u8 mgmt_err = mgmt_status(status); |
| 1339 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1340 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1341 | goto remove_cmd; |
| 1342 | } |
| 1343 | |
| 1344 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1345 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1346 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1347 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1348 | |
| 1349 | if (hdev->discov_timeout > 0) { |
| 1350 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1351 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1352 | to); |
| 1353 | } |
| 1354 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1355 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1356 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1357 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1358 | |
| 1359 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1360 | |
| 1361 | if (changed) |
| 1362 | new_settings(hdev, cmd->sk); |
| 1363 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1364 | /* When the discoverable mode gets changed, make sure |
| 1365 | * that class of device has the limited discoverable |
| 1366 | * bit correctly set. |
| 1367 | */ |
| 1368 | hci_req_init(&req, hdev); |
| 1369 | update_class(&req); |
| 1370 | hci_req_run(&req, NULL); |
| 1371 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1372 | remove_cmd: |
| 1373 | mgmt_pending_remove(cmd); |
| 1374 | |
| 1375 | unlock: |
| 1376 | hci_dev_unlock(hdev); |
| 1377 | } |
| 1378 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1379 | 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] | 1380 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1381 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1382 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1383 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1384 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1385 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1386 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1387 | int err; |
| 1388 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1389 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1390 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1391 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1392 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1393 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1394 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1395 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1396 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1397 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1398 | MGMT_STATUS_INVALID_PARAMS); |
| 1399 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1400 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1401 | |
| 1402 | /* Disabling discoverable requires that no timeout is set, |
| 1403 | * and enabling limited discoverable requires a timeout. |
| 1404 | */ |
| 1405 | if ((cp->val == 0x00 && timeout > 0) || |
| 1406 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1407 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1408 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1409 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1410 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1411 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1412 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1413 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1414 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1415 | goto failed; |
| 1416 | } |
| 1417 | |
| 1418 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1419 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1420 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1421 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1422 | goto failed; |
| 1423 | } |
| 1424 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1425 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1426 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1427 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1428 | goto failed; |
| 1429 | } |
| 1430 | |
| 1431 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1432 | bool changed = false; |
| 1433 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1434 | /* Setting limited discoverable when powered off is |
| 1435 | * not a valid operation since it requires a timeout |
| 1436 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1437 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1438 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1439 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1440 | changed = true; |
| 1441 | } |
| 1442 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1443 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1444 | if (err < 0) |
| 1445 | goto failed; |
| 1446 | |
| 1447 | if (changed) |
| 1448 | err = new_settings(hdev, sk); |
| 1449 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1450 | goto failed; |
| 1451 | } |
| 1452 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1453 | /* If the current mode is the same, then just update the timeout |
| 1454 | * value with the new value. And if only the timeout gets updated, |
| 1455 | * then no need for any HCI transactions. |
| 1456 | */ |
| 1457 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1458 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1459 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1460 | cancel_delayed_work(&hdev->discov_off); |
| 1461 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1462 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1463 | if (cp->val && hdev->discov_timeout > 0) { |
| 1464 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1465 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1466 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1467 | } |
| 1468 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1469 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1470 | goto failed; |
| 1471 | } |
| 1472 | |
| 1473 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1474 | if (!cmd) { |
| 1475 | err = -ENOMEM; |
| 1476 | goto failed; |
| 1477 | } |
| 1478 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1479 | /* Cancel any potential discoverable timeout that might be |
| 1480 | * still active and store new timeout value. The arming of |
| 1481 | * the timeout happens in the complete handler. |
| 1482 | */ |
| 1483 | cancel_delayed_work(&hdev->discov_off); |
| 1484 | hdev->discov_timeout = timeout; |
| 1485 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1486 | /* Limited discoverable mode */ |
| 1487 | if (cp->val == 0x02) |
| 1488 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1489 | else |
| 1490 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1491 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1492 | hci_req_init(&req, hdev); |
| 1493 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1494 | /* The procedure for LE-only controllers is much simpler - just |
| 1495 | * update the advertising data. |
| 1496 | */ |
| 1497 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1498 | goto update_ad; |
| 1499 | |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1500 | scan = SCAN_PAGE; |
| 1501 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1502 | if (cp->val) { |
| 1503 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1504 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1505 | if (cp->val == 0x02) { |
| 1506 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1507 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1508 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1509 | hci_cp.iac_lap[1] = 0x8b; |
| 1510 | hci_cp.iac_lap[2] = 0x9e; |
| 1511 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1512 | hci_cp.iac_lap[4] = 0x8b; |
| 1513 | hci_cp.iac_lap[5] = 0x9e; |
| 1514 | } else { |
| 1515 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1516 | hci_cp.num_iac = 1; |
| 1517 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1518 | hci_cp.iac_lap[1] = 0x8b; |
| 1519 | hci_cp.iac_lap[2] = 0x9e; |
| 1520 | } |
| 1521 | |
| 1522 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1523 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1524 | |
| 1525 | scan |= SCAN_INQUIRY; |
| 1526 | } else { |
| 1527 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1528 | } |
| 1529 | |
| 1530 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1531 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1532 | update_ad: |
| 1533 | update_adv_data(&req); |
| 1534 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1535 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1536 | if (err < 0) |
| 1537 | mgmt_pending_remove(cmd); |
| 1538 | |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1539 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1540 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1541 | return err; |
| 1542 | } |
| 1543 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1544 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1545 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1546 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1547 | struct hci_cp_write_page_scan_activity acp; |
| 1548 | u8 type; |
| 1549 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1550 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1551 | return; |
| 1552 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1553 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1554 | return; |
| 1555 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1556 | if (enable) { |
| 1557 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1558 | |
| 1559 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1560 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1561 | } else { |
| 1562 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1563 | |
| 1564 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1565 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1566 | } |
| 1567 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1568 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1569 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1570 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1571 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1572 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1573 | sizeof(acp), &acp); |
| 1574 | |
| 1575 | if (hdev->page_scan_type != type) |
| 1576 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1577 | } |
| 1578 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1579 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1580 | { |
| 1581 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1582 | struct mgmt_mode *cp; |
| 1583 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1584 | |
| 1585 | BT_DBG("status 0x%02x", status); |
| 1586 | |
| 1587 | hci_dev_lock(hdev); |
| 1588 | |
| 1589 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1590 | if (!cmd) |
| 1591 | goto unlock; |
| 1592 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1593 | if (status) { |
| 1594 | u8 mgmt_err = mgmt_status(status); |
| 1595 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1596 | goto remove_cmd; |
| 1597 | } |
| 1598 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1599 | cp = cmd->param; |
| 1600 | if (cp->val) |
| 1601 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1602 | else |
| 1603 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1604 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1605 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1606 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1607 | if (changed) |
| 1608 | new_settings(hdev, cmd->sk); |
| 1609 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1610 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1611 | mgmt_pending_remove(cmd); |
| 1612 | |
| 1613 | unlock: |
| 1614 | hci_dev_unlock(hdev); |
| 1615 | } |
| 1616 | |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1617 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1618 | struct sock *sk, u8 val) |
| 1619 | { |
| 1620 | bool changed = false; |
| 1621 | int err; |
| 1622 | |
| 1623 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1624 | changed = true; |
| 1625 | |
| 1626 | if (val) { |
| 1627 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1628 | } else { |
| 1629 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1630 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1631 | } |
| 1632 | |
| 1633 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1634 | if (err < 0) |
| 1635 | return err; |
| 1636 | |
| 1637 | if (changed) |
| 1638 | return new_settings(hdev, sk); |
| 1639 | |
| 1640 | return 0; |
| 1641 | } |
| 1642 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1643 | 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] | 1644 | u16 len) |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1645 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1646 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1647 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1648 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1649 | u8 scan; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1650 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1651 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1652 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1653 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1654 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1655 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1656 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1657 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1658 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1659 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1660 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1661 | MGMT_STATUS_INVALID_PARAMS); |
| 1662 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1663 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1664 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1665 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1666 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1667 | goto failed; |
| 1668 | } |
| 1669 | |
| 1670 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1671 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1672 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1673 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1674 | goto failed; |
| 1675 | } |
| 1676 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1677 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1678 | if (!cmd) { |
| 1679 | err = -ENOMEM; |
| 1680 | goto failed; |
| 1681 | } |
| 1682 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1683 | hci_req_init(&req, hdev); |
| 1684 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1685 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1686 | * by-product of disabling connectable, we need to update the |
| 1687 | * advertising flags. |
| 1688 | */ |
| 1689 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1690 | if (!cp->val) { |
| 1691 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1692 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1693 | } |
| 1694 | update_adv_data(&req); |
| 1695 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1696 | if (cp->val) { |
| 1697 | scan = SCAN_PAGE; |
| 1698 | } else { |
| 1699 | scan = 0; |
| 1700 | |
| 1701 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1702 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1703 | cancel_delayed_work(&hdev->discov_off); |
| 1704 | } |
| 1705 | |
| 1706 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1707 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1708 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1709 | /* If we're going from non-connectable to connectable or |
| 1710 | * vice-versa when fast connectable is enabled ensure that fast |
| 1711 | * connectable gets disabled. write_fast_connectable won't do |
| 1712 | * anything if the page scan parameters are already what they |
| 1713 | * should be. |
| 1714 | */ |
| 1715 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1716 | write_fast_connectable(&req, false); |
| 1717 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1718 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) && |
| 1719 | hci_conn_num(hdev, LE_LINK) == 0) { |
| 1720 | disable_advertising(&req); |
| 1721 | enable_advertising(&req); |
| 1722 | } |
| 1723 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1724 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1725 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1726 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1727 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1728 | err = set_connectable_update_settings(hdev, sk, |
| 1729 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1730 | goto failed; |
| 1731 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1732 | |
| 1733 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1734 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1735 | return err; |
| 1736 | } |
| 1737 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1738 | 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] | 1739 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1740 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1741 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1742 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1743 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1744 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1745 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1746 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1747 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1748 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1749 | MGMT_STATUS_INVALID_PARAMS); |
| 1750 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1751 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1752 | |
| 1753 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1754 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1755 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1756 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1757 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1758 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1759 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1760 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1761 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1762 | if (changed) |
| 1763 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1764 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1765 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1766 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1767 | return err; |
| 1768 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1769 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1770 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1771 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1772 | { |
| 1773 | struct mgmt_mode *cp = data; |
| 1774 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1775 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1776 | int err; |
| 1777 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1778 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1779 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1780 | status = mgmt_bredr_support(hdev); |
| 1781 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1782 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1783 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1784 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1785 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1786 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1787 | MGMT_STATUS_INVALID_PARAMS); |
| 1788 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1789 | hci_dev_lock(hdev); |
| 1790 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1791 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1792 | bool changed = false; |
| 1793 | |
| 1794 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1795 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1796 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1797 | changed = true; |
| 1798 | } |
| 1799 | |
| 1800 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1801 | if (err < 0) |
| 1802 | goto failed; |
| 1803 | |
| 1804 | if (changed) |
| 1805 | err = new_settings(hdev, sk); |
| 1806 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1807 | goto failed; |
| 1808 | } |
| 1809 | |
| 1810 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1811 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1812 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1813 | goto failed; |
| 1814 | } |
| 1815 | |
| 1816 | val = !!cp->val; |
| 1817 | |
| 1818 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1819 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1820 | goto failed; |
| 1821 | } |
| 1822 | |
| 1823 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1824 | if (!cmd) { |
| 1825 | err = -ENOMEM; |
| 1826 | goto failed; |
| 1827 | } |
| 1828 | |
| 1829 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1830 | if (err < 0) { |
| 1831 | mgmt_pending_remove(cmd); |
| 1832 | goto failed; |
| 1833 | } |
| 1834 | |
| 1835 | failed: |
| 1836 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1837 | return err; |
| 1838 | } |
| 1839 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1840 | 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] | 1841 | { |
| 1842 | struct mgmt_mode *cp = data; |
| 1843 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1844 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1845 | int err; |
| 1846 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1847 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1848 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1849 | status = mgmt_bredr_support(hdev); |
| 1850 | if (status) |
| 1851 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 1852 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1853 | if (!lmp_ssp_capable(hdev)) |
| 1854 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1855 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1856 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1857 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1858 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1859 | MGMT_STATUS_INVALID_PARAMS); |
| 1860 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1861 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1862 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1863 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1864 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1865 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1866 | if (cp->val) { |
| 1867 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 1868 | &hdev->dev_flags); |
| 1869 | } else { |
| 1870 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 1871 | &hdev->dev_flags); |
| 1872 | if (!changed) |
| 1873 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 1874 | &hdev->dev_flags); |
| 1875 | else |
| 1876 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1880 | if (err < 0) |
| 1881 | goto failed; |
| 1882 | |
| 1883 | if (changed) |
| 1884 | err = new_settings(hdev, sk); |
| 1885 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1886 | goto failed; |
| 1887 | } |
| 1888 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1889 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 1890 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 1891 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1892 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1893 | goto failed; |
| 1894 | } |
| 1895 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1896 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1897 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1898 | goto failed; |
| 1899 | } |
| 1900 | |
| 1901 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1902 | if (!cmd) { |
| 1903 | err = -ENOMEM; |
| 1904 | goto failed; |
| 1905 | } |
| 1906 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1907 | if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 1908 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1909 | sizeof(cp->val), &cp->val); |
| 1910 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1911 | 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] | 1912 | if (err < 0) { |
| 1913 | mgmt_pending_remove(cmd); |
| 1914 | goto failed; |
| 1915 | } |
| 1916 | |
| 1917 | failed: |
| 1918 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1919 | return err; |
| 1920 | } |
| 1921 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1922 | 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] | 1923 | { |
| 1924 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1925 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1926 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1927 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1928 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1929 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1930 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1931 | status = mgmt_bredr_support(hdev); |
| 1932 | if (status) |
| 1933 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1934 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1935 | if (!lmp_ssp_capable(hdev)) |
| 1936 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1937 | MGMT_STATUS_NOT_SUPPORTED); |
| 1938 | |
| 1939 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 1940 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1941 | MGMT_STATUS_REJECTED); |
| 1942 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1943 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1944 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1945 | MGMT_STATUS_INVALID_PARAMS); |
| 1946 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1947 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1948 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1949 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1950 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1951 | } else { |
| 1952 | if (hdev_is_powered(hdev)) { |
| 1953 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1954 | MGMT_STATUS_REJECTED); |
| 1955 | goto unlock; |
| 1956 | } |
| 1957 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1958 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1959 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1960 | |
| 1961 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1962 | if (err < 0) |
| 1963 | goto unlock; |
| 1964 | |
| 1965 | if (changed) |
| 1966 | err = new_settings(hdev, sk); |
| 1967 | |
| 1968 | unlock: |
| 1969 | hci_dev_unlock(hdev); |
| 1970 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1971 | } |
| 1972 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1973 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 1974 | { |
| 1975 | struct cmd_lookup match = { NULL, hdev }; |
| 1976 | |
| 1977 | if (status) { |
| 1978 | u8 mgmt_err = mgmt_status(status); |
| 1979 | |
| 1980 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1981 | &mgmt_err); |
| 1982 | return; |
| 1983 | } |
| 1984 | |
| 1985 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1986 | |
| 1987 | new_settings(hdev, match.sk); |
| 1988 | |
| 1989 | if (match.sk) |
| 1990 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1991 | |
| 1992 | /* Make sure the controller has a good default for |
| 1993 | * advertising data. Restrict the update to when LE |
| 1994 | * has actually been enabled. During power on, the |
| 1995 | * update in powered_update_hci will take care of it. |
| 1996 | */ |
| 1997 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 1998 | struct hci_request req; |
| 1999 | |
| 2000 | hci_dev_lock(hdev); |
| 2001 | |
| 2002 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 2003 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 2004 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2005 | hci_req_run(&req, NULL); |
| 2006 | |
| 2007 | hci_dev_unlock(hdev); |
| 2008 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2009 | } |
| 2010 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2011 | 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] | 2012 | { |
| 2013 | struct mgmt_mode *cp = data; |
| 2014 | struct hci_cp_write_le_host_supported hci_cp; |
| 2015 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2016 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2017 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2018 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2019 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2020 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2021 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2022 | if (!lmp_le_capable(hdev)) |
| 2023 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2024 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2025 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2026 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2027 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2028 | MGMT_STATUS_INVALID_PARAMS); |
| 2029 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2030 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2031 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2032 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2033 | MGMT_STATUS_REJECTED); |
| 2034 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2035 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2036 | |
| 2037 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2038 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2039 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2040 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2041 | bool changed = false; |
| 2042 | |
| 2043 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2044 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 2045 | changed = true; |
| 2046 | } |
| 2047 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 2048 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 2049 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2050 | changed = true; |
| 2051 | } |
| 2052 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2053 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2054 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2055 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2056 | |
| 2057 | if (changed) |
| 2058 | err = new_settings(hdev, sk); |
| 2059 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2060 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2061 | } |
| 2062 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2063 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2064 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2065 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2066 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2067 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2071 | if (!cmd) { |
| 2072 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2073 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2074 | } |
| 2075 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2076 | hci_req_init(&req, hdev); |
| 2077 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2078 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2079 | |
| 2080 | if (val) { |
| 2081 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2082 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2083 | } else { |
| 2084 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 2085 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2086 | } |
| 2087 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2088 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2089 | &hci_cp); |
| 2090 | |
| 2091 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2092 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2093 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2094 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2095 | unlock: |
| 2096 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2097 | return err; |
| 2098 | } |
| 2099 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2100 | /* This is a helper function to test for pending mgmt commands that can |
| 2101 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2102 | * mgmt command at a time since otherwise we cannot easily track what |
| 2103 | * the current values are, will be, and based on that calculate if a new |
| 2104 | * HCI command needs to be sent and if yes with what value. |
| 2105 | */ |
| 2106 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2107 | { |
| 2108 | struct pending_cmd *cmd; |
| 2109 | |
| 2110 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2111 | switch (cmd->opcode) { |
| 2112 | case MGMT_OP_ADD_UUID: |
| 2113 | case MGMT_OP_REMOVE_UUID: |
| 2114 | case MGMT_OP_SET_DEV_CLASS: |
| 2115 | case MGMT_OP_SET_POWERED: |
| 2116 | return true; |
| 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | return false; |
| 2121 | } |
| 2122 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2123 | static const u8 bluetooth_base_uuid[] = { |
| 2124 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2125 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2126 | }; |
| 2127 | |
| 2128 | static u8 get_uuid_size(const u8 *uuid) |
| 2129 | { |
| 2130 | u32 val; |
| 2131 | |
| 2132 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2133 | return 128; |
| 2134 | |
| 2135 | val = get_unaligned_le32(&uuid[12]); |
| 2136 | if (val > 0xffff) |
| 2137 | return 32; |
| 2138 | |
| 2139 | return 16; |
| 2140 | } |
| 2141 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2142 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2143 | { |
| 2144 | struct pending_cmd *cmd; |
| 2145 | |
| 2146 | hci_dev_lock(hdev); |
| 2147 | |
| 2148 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2149 | if (!cmd) |
| 2150 | goto unlock; |
| 2151 | |
| 2152 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 2153 | hdev->dev_class, 3); |
| 2154 | |
| 2155 | mgmt_pending_remove(cmd); |
| 2156 | |
| 2157 | unlock: |
| 2158 | hci_dev_unlock(hdev); |
| 2159 | } |
| 2160 | |
| 2161 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2162 | { |
| 2163 | BT_DBG("status 0x%02x", status); |
| 2164 | |
| 2165 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2166 | } |
| 2167 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2168 | 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] | 2169 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2170 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2171 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2172 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2173 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2174 | int err; |
| 2175 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2176 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2177 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2178 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2179 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2180 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2181 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2182 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2183 | goto failed; |
| 2184 | } |
| 2185 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2186 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2187 | if (!uuid) { |
| 2188 | err = -ENOMEM; |
| 2189 | goto failed; |
| 2190 | } |
| 2191 | |
| 2192 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2193 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2194 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2195 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2196 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2197 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2198 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2199 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2200 | update_class(&req); |
| 2201 | update_eir(&req); |
| 2202 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2203 | err = hci_req_run(&req, add_uuid_complete); |
| 2204 | if (err < 0) { |
| 2205 | if (err != -ENODATA) |
| 2206 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2207 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2208 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2209 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2210 | goto failed; |
| 2211 | } |
| 2212 | |
| 2213 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2214 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2215 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2216 | goto failed; |
| 2217 | } |
| 2218 | |
| 2219 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2220 | |
| 2221 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2222 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2223 | return err; |
| 2224 | } |
| 2225 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2226 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2227 | { |
| 2228 | if (!hdev_is_powered(hdev)) |
| 2229 | return false; |
| 2230 | |
| 2231 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2232 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2233 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2234 | return true; |
| 2235 | } |
| 2236 | |
| 2237 | return false; |
| 2238 | } |
| 2239 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2240 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2241 | { |
| 2242 | BT_DBG("status 0x%02x", status); |
| 2243 | |
| 2244 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2245 | } |
| 2246 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2247 | 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] | 2248 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2249 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2250 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2251 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2252 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2253 | 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] | 2254 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2255 | int err, found; |
| 2256 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2257 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2258 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2259 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2260 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2261 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2262 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2263 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2264 | goto unlock; |
| 2265 | } |
| 2266 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2267 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2268 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2269 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2270 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2271 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2272 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2273 | goto unlock; |
| 2274 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2275 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2276 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | found = 0; |
| 2280 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2281 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2282 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2283 | continue; |
| 2284 | |
| 2285 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2286 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2287 | found++; |
| 2288 | } |
| 2289 | |
| 2290 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2291 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2292 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2293 | goto unlock; |
| 2294 | } |
| 2295 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2296 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2297 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2298 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2299 | update_class(&req); |
| 2300 | update_eir(&req); |
| 2301 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2302 | err = hci_req_run(&req, remove_uuid_complete); |
| 2303 | if (err < 0) { |
| 2304 | if (err != -ENODATA) |
| 2305 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2306 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2307 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2308 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2309 | goto unlock; |
| 2310 | } |
| 2311 | |
| 2312 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2313 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2314 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2315 | goto unlock; |
| 2316 | } |
| 2317 | |
| 2318 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2319 | |
| 2320 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2321 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2322 | return err; |
| 2323 | } |
| 2324 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2325 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2326 | { |
| 2327 | BT_DBG("status 0x%02x", status); |
| 2328 | |
| 2329 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2330 | } |
| 2331 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2332 | 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] | 2333 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2334 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2335 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2336 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2337 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2338 | int err; |
| 2339 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2340 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2341 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2342 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2343 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2344 | MGMT_STATUS_NOT_SUPPORTED); |
| 2345 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2346 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2347 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2348 | if (pending_eir_or_class(hdev)) { |
| 2349 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2350 | MGMT_STATUS_BUSY); |
| 2351 | goto unlock; |
| 2352 | } |
| 2353 | |
| 2354 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2355 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2356 | MGMT_STATUS_INVALID_PARAMS); |
| 2357 | goto unlock; |
| 2358 | } |
| 2359 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2360 | hdev->major_class = cp->major; |
| 2361 | hdev->minor_class = cp->minor; |
| 2362 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2363 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2364 | 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] | 2365 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2366 | goto unlock; |
| 2367 | } |
| 2368 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2369 | hci_req_init(&req, hdev); |
| 2370 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2371 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2372 | hci_dev_unlock(hdev); |
| 2373 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2374 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2375 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2376 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2377 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2378 | update_class(&req); |
| 2379 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2380 | err = hci_req_run(&req, set_class_complete); |
| 2381 | if (err < 0) { |
| 2382 | if (err != -ENODATA) |
| 2383 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2384 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2385 | 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] | 2386 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2387 | goto unlock; |
| 2388 | } |
| 2389 | |
| 2390 | 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] | 2391 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2392 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2393 | goto unlock; |
| 2394 | } |
| 2395 | |
| 2396 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2397 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2398 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2399 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2400 | return err; |
| 2401 | } |
| 2402 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2403 | 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] | 2404 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2405 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2406 | struct mgmt_cp_load_link_keys *cp = data; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2407 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2408 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2409 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2410 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2411 | BT_DBG("request for %s", hdev->name); |
| 2412 | |
| 2413 | if (!lmp_bredr_capable(hdev)) |
| 2414 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2415 | MGMT_STATUS_NOT_SUPPORTED); |
| 2416 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2417 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2418 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2419 | expected_len = sizeof(*cp) + key_count * |
| 2420 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2421 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2422 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2423 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2424 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2425 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2426 | } |
| 2427 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2428 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2429 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2430 | MGMT_STATUS_INVALID_PARAMS); |
| 2431 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2432 | 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] | 2433 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2434 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2435 | for (i = 0; i < key_count; i++) { |
| 2436 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2437 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2438 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2439 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2440 | MGMT_STATUS_INVALID_PARAMS); |
| 2441 | } |
| 2442 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2443 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2444 | |
| 2445 | hci_link_keys_clear(hdev); |
| 2446 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2447 | if (cp->debug_keys) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2448 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 2449 | &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2450 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2451 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 2452 | &hdev->dev_flags); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2453 | |
| 2454 | if (changed) |
| 2455 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2456 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2457 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2458 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2459 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2460 | /* Always ignore debug keys and require a new pairing if |
| 2461 | * the user wants to use them. |
| 2462 | */ |
| 2463 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2464 | continue; |
| 2465 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2466 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2467 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2468 | } |
| 2469 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2470 | 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] | 2471 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2472 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2473 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2474 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2475 | } |
| 2476 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2477 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2478 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2479 | { |
| 2480 | struct mgmt_ev_device_unpaired ev; |
| 2481 | |
| 2482 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2483 | ev.addr.type = addr_type; |
| 2484 | |
| 2485 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2486 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2487 | } |
| 2488 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2489 | 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] | 2490 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2491 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2492 | struct mgmt_cp_unpair_device *cp = data; |
| 2493 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2494 | struct hci_cp_disconnect dc; |
| 2495 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2496 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2497 | int err; |
| 2498 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2499 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2500 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2501 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2502 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2503 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2504 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2505 | MGMT_STATUS_INVALID_PARAMS, |
| 2506 | &rp, sizeof(rp)); |
| 2507 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2508 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2509 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2510 | MGMT_STATUS_INVALID_PARAMS, |
| 2511 | &rp, sizeof(rp)); |
| 2512 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2513 | hci_dev_lock(hdev); |
| 2514 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2515 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2516 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2517 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2518 | goto unlock; |
| 2519 | } |
| 2520 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2521 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2522 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2523 | } else { |
| 2524 | u8 addr_type; |
| 2525 | |
| 2526 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2527 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2528 | else |
| 2529 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2530 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2531 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2532 | |
Andre Guedes | a9b0a04 | 2014-02-26 20:21:52 -0300 | [diff] [blame] | 2533 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2534 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2535 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2536 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2537 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2538 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2539 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2540 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2541 | goto unlock; |
| 2542 | } |
| 2543 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2544 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2545 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2546 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2547 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2548 | else |
| 2549 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2550 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2551 | } else { |
| 2552 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2553 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2554 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2555 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2556 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2557 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2558 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2559 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2560 | } |
| 2561 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2562 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2563 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2564 | if (!cmd) { |
| 2565 | err = -ENOMEM; |
| 2566 | goto unlock; |
| 2567 | } |
| 2568 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2569 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2570 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2571 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2572 | if (err < 0) |
| 2573 | mgmt_pending_remove(cmd); |
| 2574 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2575 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2576 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2577 | return err; |
| 2578 | } |
| 2579 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2580 | 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] | 2581 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2582 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2583 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2584 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2585 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2586 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2587 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2588 | int err; |
| 2589 | |
| 2590 | BT_DBG(""); |
| 2591 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2592 | memset(&rp, 0, sizeof(rp)); |
| 2593 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2594 | rp.addr.type = cp->addr.type; |
| 2595 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2596 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2597 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2598 | MGMT_STATUS_INVALID_PARAMS, |
| 2599 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2600 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2601 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2602 | |
| 2603 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2604 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2605 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2606 | goto failed; |
| 2607 | } |
| 2608 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2609 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2610 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2611 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2612 | goto failed; |
| 2613 | } |
| 2614 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2615 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2616 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2617 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2618 | else |
| 2619 | 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] | 2620 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2621 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2622 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2623 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2624 | goto failed; |
| 2625 | } |
| 2626 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2627 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2628 | if (!cmd) { |
| 2629 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2630 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2631 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2632 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2633 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2634 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2635 | |
| 2636 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2637 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2638 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2639 | |
| 2640 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2641 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2642 | return err; |
| 2643 | } |
| 2644 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2645 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2646 | { |
| 2647 | switch (link_type) { |
| 2648 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2649 | switch (addr_type) { |
| 2650 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2651 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2652 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2653 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2654 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2655 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2656 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2657 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2658 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2659 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2660 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2661 | } |
| 2662 | } |
| 2663 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2664 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2665 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2666 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2667 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2668 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2669 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2670 | int err; |
| 2671 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2672 | |
| 2673 | BT_DBG(""); |
| 2674 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2675 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2676 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2677 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2678 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2679 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2680 | goto unlock; |
| 2681 | } |
| 2682 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2683 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2684 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2685 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2686 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2687 | } |
| 2688 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2689 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2690 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2691 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2692 | err = -ENOMEM; |
| 2693 | goto unlock; |
| 2694 | } |
| 2695 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2696 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2697 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2698 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2699 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2700 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2701 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2702 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2703 | continue; |
| 2704 | i++; |
| 2705 | } |
| 2706 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2707 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2708 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2709 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2710 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2711 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2712 | 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] | 2713 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2714 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2715 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2716 | |
| 2717 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2718 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2719 | return err; |
| 2720 | } |
| 2721 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2722 | 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] | 2723 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2724 | { |
| 2725 | struct pending_cmd *cmd; |
| 2726 | int err; |
| 2727 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2728 | 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] | 2729 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2730 | if (!cmd) |
| 2731 | return -ENOMEM; |
| 2732 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2733 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2734 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2735 | if (err < 0) |
| 2736 | mgmt_pending_remove(cmd); |
| 2737 | |
| 2738 | return err; |
| 2739 | } |
| 2740 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2741 | 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] | 2742 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2743 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2744 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2745 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2746 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2747 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2748 | int err; |
| 2749 | |
| 2750 | BT_DBG(""); |
| 2751 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2752 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2753 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2754 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2755 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2756 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2757 | goto failed; |
| 2758 | } |
| 2759 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2760 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2761 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2762 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2763 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2764 | goto failed; |
| 2765 | } |
| 2766 | |
| 2767 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2768 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2769 | |
| 2770 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2771 | |
| 2772 | BT_ERR("PIN code is not 16 bytes long"); |
| 2773 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2774 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2775 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2776 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2777 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2778 | |
| 2779 | goto failed; |
| 2780 | } |
| 2781 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2782 | 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] | 2783 | if (!cmd) { |
| 2784 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2785 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2786 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2787 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2788 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2789 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2790 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2791 | |
| 2792 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2793 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2794 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2795 | |
| 2796 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2797 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2798 | return err; |
| 2799 | } |
| 2800 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2801 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2802 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2803 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2804 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2805 | |
| 2806 | BT_DBG(""); |
| 2807 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2808 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
| 2809 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2810 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
| 2811 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2812 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2813 | |
| 2814 | hdev->io_capability = cp->io_capability; |
| 2815 | |
| 2816 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2817 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2818 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2819 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2820 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2821 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2822 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2823 | } |
| 2824 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2825 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2826 | { |
| 2827 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2828 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2829 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2830 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2831 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2832 | continue; |
| 2833 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2834 | if (cmd->user_data != conn) |
| 2835 | continue; |
| 2836 | |
| 2837 | return cmd; |
| 2838 | } |
| 2839 | |
| 2840 | return NULL; |
| 2841 | } |
| 2842 | |
| 2843 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 2844 | { |
| 2845 | struct mgmt_rp_pair_device rp; |
| 2846 | struct hci_conn *conn = cmd->user_data; |
| 2847 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2848 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2849 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2850 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 2851 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2852 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2853 | |
| 2854 | /* So we don't get further callbacks for this connection */ |
| 2855 | conn->connect_cfm_cb = NULL; |
| 2856 | conn->security_cfm_cb = NULL; |
| 2857 | conn->disconn_cfm_cb = NULL; |
| 2858 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2859 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2860 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2861 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2862 | } |
| 2863 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2864 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2865 | { |
| 2866 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 2867 | struct pending_cmd *cmd; |
| 2868 | |
| 2869 | cmd = find_pairing(conn); |
| 2870 | if (cmd) |
| 2871 | pairing_complete(cmd, status); |
| 2872 | } |
| 2873 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2874 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2875 | { |
| 2876 | struct pending_cmd *cmd; |
| 2877 | |
| 2878 | BT_DBG("status %u", status); |
| 2879 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2880 | cmd = find_pairing(conn); |
| 2881 | if (!cmd) |
| 2882 | BT_DBG("Unable to find a pending command"); |
| 2883 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 2884 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2885 | } |
| 2886 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2887 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2888 | { |
| 2889 | struct pending_cmd *cmd; |
| 2890 | |
| 2891 | BT_DBG("status %u", status); |
| 2892 | |
| 2893 | if (!status) |
| 2894 | return; |
| 2895 | |
| 2896 | cmd = find_pairing(conn); |
| 2897 | if (!cmd) |
| 2898 | BT_DBG("Unable to find a pending command"); |
| 2899 | else |
| 2900 | pairing_complete(cmd, mgmt_status(status)); |
| 2901 | } |
| 2902 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2903 | 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] | 2904 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2905 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2906 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2907 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2908 | struct pending_cmd *cmd; |
| 2909 | u8 sec_level, auth_type; |
| 2910 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2911 | int err; |
| 2912 | |
| 2913 | BT_DBG(""); |
| 2914 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2915 | memset(&rp, 0, sizeof(rp)); |
| 2916 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2917 | rp.addr.type = cp->addr.type; |
| 2918 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2919 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2920 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2921 | MGMT_STATUS_INVALID_PARAMS, |
| 2922 | &rp, sizeof(rp)); |
| 2923 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2924 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
| 2925 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2926 | MGMT_STATUS_INVALID_PARAMS, |
| 2927 | &rp, sizeof(rp)); |
| 2928 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2929 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2930 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2931 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2932 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2933 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2934 | goto unlock; |
| 2935 | } |
| 2936 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2937 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2938 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2939 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2940 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2941 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 2942 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2943 | } else { |
| 2944 | u8 addr_type; |
| 2945 | |
| 2946 | /* Convert from L2CAP channel address type to HCI address type |
| 2947 | */ |
| 2948 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2949 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2950 | else |
| 2951 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2952 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2953 | /* When pairing a new device, it is expected to remember |
| 2954 | * this device for future connections. Adding the connection |
| 2955 | * parameter information ahead of time allows tracking |
| 2956 | * of the slave preferred values and will speed up any |
| 2957 | * further connection establishment. |
| 2958 | * |
| 2959 | * If connection parameters already exist, then they |
| 2960 | * will be kept and this function does nothing. |
| 2961 | */ |
| 2962 | hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 2963 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2964 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2965 | sec_level, auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2966 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2967 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2968 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2969 | int status; |
| 2970 | |
| 2971 | if (PTR_ERR(conn) == -EBUSY) |
| 2972 | status = MGMT_STATUS_BUSY; |
| 2973 | else |
| 2974 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2975 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2976 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2977 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2978 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2979 | goto unlock; |
| 2980 | } |
| 2981 | |
| 2982 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2983 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2984 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2985 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2986 | goto unlock; |
| 2987 | } |
| 2988 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2989 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2990 | if (!cmd) { |
| 2991 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2992 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2993 | goto unlock; |
| 2994 | } |
| 2995 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2996 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2997 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2998 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2999 | conn->security_cfm_cb = pairing_complete_cb; |
| 3000 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3001 | } else { |
| 3002 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3003 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3004 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3005 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3006 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3007 | conn->io_capability = cp->io_cap; |
| 3008 | cmd->user_data = conn; |
| 3009 | |
| 3010 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3011 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3012 | pairing_complete(cmd, 0); |
| 3013 | |
| 3014 | err = 0; |
| 3015 | |
| 3016 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3017 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3018 | return err; |
| 3019 | } |
| 3020 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3021 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3022 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3023 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3024 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3025 | struct pending_cmd *cmd; |
| 3026 | struct hci_conn *conn; |
| 3027 | int err; |
| 3028 | |
| 3029 | BT_DBG(""); |
| 3030 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3031 | hci_dev_lock(hdev); |
| 3032 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3033 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3034 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3035 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3036 | goto unlock; |
| 3037 | } |
| 3038 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3039 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3040 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3041 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3042 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3043 | goto unlock; |
| 3044 | } |
| 3045 | |
| 3046 | conn = cmd->user_data; |
| 3047 | |
| 3048 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3049 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3050 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3051 | goto unlock; |
| 3052 | } |
| 3053 | |
| 3054 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3055 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3056 | 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] | 3057 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3058 | unlock: |
| 3059 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3060 | return err; |
| 3061 | } |
| 3062 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3063 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3064 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3065 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3066 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3067 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3068 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3069 | int err; |
| 3070 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3071 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3072 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3073 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3074 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3075 | MGMT_STATUS_NOT_POWERED, addr, |
| 3076 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3077 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3078 | } |
| 3079 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3080 | if (addr->type == BDADDR_BREDR) |
| 3081 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3082 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3083 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3084 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3085 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3086 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3087 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3088 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3089 | goto done; |
| 3090 | } |
| 3091 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3092 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3093 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3094 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3095 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3096 | MGMT_STATUS_SUCCESS, addr, |
| 3097 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3098 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3099 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3100 | MGMT_STATUS_FAILED, addr, |
| 3101 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3102 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3103 | goto done; |
| 3104 | } |
| 3105 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3106 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3107 | if (!cmd) { |
| 3108 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3109 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3110 | } |
| 3111 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3112 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3113 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3114 | struct hci_cp_user_passkey_reply cp; |
| 3115 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3116 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3117 | cp.passkey = passkey; |
| 3118 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3119 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3120 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3121 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3122 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3123 | if (err < 0) |
| 3124 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3125 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3126 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3127 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3128 | return err; |
| 3129 | } |
| 3130 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3131 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3132 | void *data, u16 len) |
| 3133 | { |
| 3134 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3135 | |
| 3136 | BT_DBG(""); |
| 3137 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3138 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3139 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3140 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3141 | } |
| 3142 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3143 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3144 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3145 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3146 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3147 | |
| 3148 | BT_DBG(""); |
| 3149 | |
| 3150 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3151 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3152 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3153 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3154 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3155 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3156 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3157 | } |
| 3158 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3159 | 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] | 3160 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3161 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3162 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3163 | |
| 3164 | BT_DBG(""); |
| 3165 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3166 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3167 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3168 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3169 | } |
| 3170 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3171 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3172 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3173 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3174 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3175 | |
| 3176 | BT_DBG(""); |
| 3177 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3178 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3179 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3180 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3181 | } |
| 3182 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3183 | 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] | 3184 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3185 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3186 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3187 | |
| 3188 | BT_DBG(""); |
| 3189 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3190 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3191 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3192 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3193 | } |
| 3194 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3195 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3196 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3197 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3198 | struct hci_cp_write_local_name cp; |
| 3199 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3200 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3201 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3202 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3203 | } |
| 3204 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3205 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3206 | { |
| 3207 | struct mgmt_cp_set_local_name *cp; |
| 3208 | struct pending_cmd *cmd; |
| 3209 | |
| 3210 | BT_DBG("status 0x%02x", status); |
| 3211 | |
| 3212 | hci_dev_lock(hdev); |
| 3213 | |
| 3214 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3215 | if (!cmd) |
| 3216 | goto unlock; |
| 3217 | |
| 3218 | cp = cmd->param; |
| 3219 | |
| 3220 | if (status) |
| 3221 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3222 | mgmt_status(status)); |
| 3223 | else |
| 3224 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3225 | cp, sizeof(*cp)); |
| 3226 | |
| 3227 | mgmt_pending_remove(cmd); |
| 3228 | |
| 3229 | unlock: |
| 3230 | hci_dev_unlock(hdev); |
| 3231 | } |
| 3232 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3233 | 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] | 3234 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3235 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3236 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3237 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3238 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3239 | int err; |
| 3240 | |
| 3241 | BT_DBG(""); |
| 3242 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3243 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3244 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3245 | /* If the old values are the same as the new ones just return a |
| 3246 | * direct command complete event. |
| 3247 | */ |
| 3248 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3249 | !memcmp(hdev->short_name, cp->short_name, |
| 3250 | sizeof(hdev->short_name))) { |
| 3251 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3252 | data, len); |
| 3253 | goto failed; |
| 3254 | } |
| 3255 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3256 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3257 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3258 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3259 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3260 | |
| 3261 | 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] | 3262 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3263 | if (err < 0) |
| 3264 | goto failed; |
| 3265 | |
| 3266 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3267 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3268 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3269 | goto failed; |
| 3270 | } |
| 3271 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3272 | 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] | 3273 | if (!cmd) { |
| 3274 | err = -ENOMEM; |
| 3275 | goto failed; |
| 3276 | } |
| 3277 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3278 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3279 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3280 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3281 | |
| 3282 | if (lmp_bredr_capable(hdev)) { |
| 3283 | update_name(&req); |
| 3284 | update_eir(&req); |
| 3285 | } |
| 3286 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3287 | /* The name is stored in the scan response data and so |
| 3288 | * no need to udpate the advertising data here. |
| 3289 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3290 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3291 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3292 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3293 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3294 | if (err < 0) |
| 3295 | mgmt_pending_remove(cmd); |
| 3296 | |
| 3297 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3298 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3299 | return err; |
| 3300 | } |
| 3301 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3302 | 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] | 3303 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3304 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3305 | struct pending_cmd *cmd; |
| 3306 | int err; |
| 3307 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3308 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3309 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3310 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3311 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3312 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3313 | 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] | 3314 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3315 | goto unlock; |
| 3316 | } |
| 3317 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3318 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3319 | 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] | 3320 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3321 | goto unlock; |
| 3322 | } |
| 3323 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3324 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3325 | 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] | 3326 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3327 | goto unlock; |
| 3328 | } |
| 3329 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3330 | 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] | 3331 | if (!cmd) { |
| 3332 | err = -ENOMEM; |
| 3333 | goto unlock; |
| 3334 | } |
| 3335 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3336 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3337 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3338 | 0, NULL); |
| 3339 | else |
| 3340 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3341 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3342 | if (err < 0) |
| 3343 | mgmt_pending_remove(cmd); |
| 3344 | |
| 3345 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3346 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3347 | return err; |
| 3348 | } |
| 3349 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3350 | 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] | 3351 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3352 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3353 | int err; |
| 3354 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3355 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3356 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3357 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3358 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3359 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3360 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3361 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3362 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3363 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3364 | cp->hash, cp->randomizer); |
| 3365 | if (err < 0) |
| 3366 | status = MGMT_STATUS_FAILED; |
| 3367 | else |
| 3368 | status = MGMT_STATUS_SUCCESS; |
| 3369 | |
| 3370 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3371 | status, &cp->addr, sizeof(cp->addr)); |
| 3372 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3373 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3374 | u8 status; |
| 3375 | |
| 3376 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3377 | cp->hash192, |
| 3378 | cp->randomizer192, |
| 3379 | cp->hash256, |
| 3380 | cp->randomizer256); |
| 3381 | if (err < 0) |
| 3382 | status = MGMT_STATUS_FAILED; |
| 3383 | else |
| 3384 | status = MGMT_STATUS_SUCCESS; |
| 3385 | |
| 3386 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3387 | status, &cp->addr, sizeof(cp->addr)); |
| 3388 | } else { |
| 3389 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3390 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3391 | MGMT_STATUS_INVALID_PARAMS); |
| 3392 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3393 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3394 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3395 | return err; |
| 3396 | } |
| 3397 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3398 | 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] | 3399 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3400 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3401 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3402 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3403 | int err; |
| 3404 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3405 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3406 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3407 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3408 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3409 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3410 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3411 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3412 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3413 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3414 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3415 | 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] | 3416 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3417 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3418 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3419 | return err; |
| 3420 | } |
| 3421 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3422 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3423 | { |
| 3424 | struct pending_cmd *cmd; |
| 3425 | u8 type; |
| 3426 | int err; |
| 3427 | |
| 3428 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3429 | |
| 3430 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3431 | if (!cmd) |
| 3432 | return -ENOENT; |
| 3433 | |
| 3434 | type = hdev->discovery.type; |
| 3435 | |
| 3436 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3437 | &type, sizeof(type)); |
| 3438 | mgmt_pending_remove(cmd); |
| 3439 | |
| 3440 | return err; |
| 3441 | } |
| 3442 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3443 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3444 | { |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3445 | unsigned long timeout = 0; |
| 3446 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3447 | BT_DBG("status %d", status); |
| 3448 | |
| 3449 | if (status) { |
| 3450 | hci_dev_lock(hdev); |
| 3451 | mgmt_start_discovery_failed(hdev, status); |
| 3452 | hci_dev_unlock(hdev); |
| 3453 | return; |
| 3454 | } |
| 3455 | |
| 3456 | hci_dev_lock(hdev); |
| 3457 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3458 | hci_dev_unlock(hdev); |
| 3459 | |
| 3460 | switch (hdev->discovery.type) { |
| 3461 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3462 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3463 | break; |
| 3464 | |
| 3465 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3466 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3467 | break; |
| 3468 | |
| 3469 | case DISCOV_TYPE_BREDR: |
| 3470 | break; |
| 3471 | |
| 3472 | default: |
| 3473 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3474 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3475 | |
| 3476 | if (!timeout) |
| 3477 | return; |
| 3478 | |
| 3479 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3480 | } |
| 3481 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3482 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3483 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3484 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3485 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3486 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3487 | struct hci_cp_le_set_scan_param param_cp; |
| 3488 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3489 | struct hci_cp_inquiry inq_cp; |
| 3490 | struct hci_request req; |
| 3491 | /* General inquiry access code (GIAC) */ |
| 3492 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3493 | u8 status, own_addr_type; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3494 | int err; |
| 3495 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3496 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3497 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3498 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3499 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3500 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3501 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3502 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3503 | goto failed; |
| 3504 | } |
| 3505 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3506 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3507 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3508 | MGMT_STATUS_BUSY); |
| 3509 | goto failed; |
| 3510 | } |
| 3511 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3512 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3513 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3514 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3515 | goto failed; |
| 3516 | } |
| 3517 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3518 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3519 | if (!cmd) { |
| 3520 | err = -ENOMEM; |
| 3521 | goto failed; |
| 3522 | } |
| 3523 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3524 | hdev->discovery.type = cp->type; |
| 3525 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3526 | hci_req_init(&req, hdev); |
| 3527 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3528 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3529 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3530 | status = mgmt_bredr_support(hdev); |
| 3531 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3532 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3533 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3534 | mgmt_pending_remove(cmd); |
| 3535 | goto failed; |
| 3536 | } |
| 3537 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3538 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3539 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3540 | MGMT_STATUS_BUSY); |
| 3541 | mgmt_pending_remove(cmd); |
| 3542 | goto failed; |
| 3543 | } |
| 3544 | |
| 3545 | hci_inquiry_cache_flush(hdev); |
| 3546 | |
| 3547 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3548 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3549 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3550 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3551 | break; |
| 3552 | |
| 3553 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3554 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3555 | status = mgmt_le_support(hdev); |
| 3556 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3557 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3558 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3559 | mgmt_pending_remove(cmd); |
| 3560 | goto failed; |
| 3561 | } |
| 3562 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3563 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3564 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3565 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3566 | MGMT_STATUS_NOT_SUPPORTED); |
| 3567 | mgmt_pending_remove(cmd); |
| 3568 | goto failed; |
| 3569 | } |
| 3570 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3571 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3572 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3573 | MGMT_STATUS_REJECTED); |
| 3574 | mgmt_pending_remove(cmd); |
| 3575 | goto failed; |
| 3576 | } |
| 3577 | |
Andre Guedes | c54c386 | 2014-02-26 20:21:50 -0300 | [diff] [blame] | 3578 | /* If controller is scanning, it means the background scanning |
| 3579 | * is running. Thus, we should temporarily stop it in order to |
| 3580 | * set the discovery scanning parameters. |
| 3581 | */ |
| 3582 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3583 | hci_req_add_le_scan_disable(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3584 | |
| 3585 | memset(¶m_cp, 0, sizeof(param_cp)); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3586 | |
Marcel Holtmann | 94b1fc9 | 2014-02-23 20:25:54 -0800 | [diff] [blame] | 3587 | /* All active scans will be done with either a resolvable |
| 3588 | * private address (when privacy feature has been enabled) |
| 3589 | * or unresolvable private address. |
| 3590 | */ |
| 3591 | err = hci_update_random_address(&req, true, &own_addr_type); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3592 | if (err < 0) { |
| 3593 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3594 | MGMT_STATUS_FAILED); |
| 3595 | mgmt_pending_remove(cmd); |
| 3596 | goto failed; |
| 3597 | } |
| 3598 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3599 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3600 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3601 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3602 | param_cp.own_address_type = own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3603 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3604 | ¶m_cp); |
| 3605 | |
| 3606 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3607 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3608 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3609 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3610 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3611 | break; |
| 3612 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3613 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3614 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3615 | MGMT_STATUS_INVALID_PARAMS); |
| 3616 | mgmt_pending_remove(cmd); |
| 3617 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3618 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3619 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3620 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3621 | if (err < 0) |
| 3622 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3623 | else |
| 3624 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3625 | |
| 3626 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3627 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3628 | return err; |
| 3629 | } |
| 3630 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3631 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3632 | { |
| 3633 | struct pending_cmd *cmd; |
| 3634 | int err; |
| 3635 | |
| 3636 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3637 | if (!cmd) |
| 3638 | return -ENOENT; |
| 3639 | |
| 3640 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3641 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3642 | mgmt_pending_remove(cmd); |
| 3643 | |
| 3644 | return err; |
| 3645 | } |
| 3646 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3647 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3648 | { |
| 3649 | BT_DBG("status %d", status); |
| 3650 | |
| 3651 | hci_dev_lock(hdev); |
| 3652 | |
| 3653 | if (status) { |
| 3654 | mgmt_stop_discovery_failed(hdev, status); |
| 3655 | goto unlock; |
| 3656 | } |
| 3657 | |
| 3658 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3659 | |
| 3660 | unlock: |
| 3661 | hci_dev_unlock(hdev); |
| 3662 | } |
| 3663 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3664 | 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] | 3665 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3666 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3667 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3668 | struct pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3669 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3670 | int err; |
| 3671 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3672 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3673 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3674 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3675 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3676 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3677 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3678 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3679 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3680 | goto unlock; |
| 3681 | } |
| 3682 | |
| 3683 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3684 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3685 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3686 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3687 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3688 | } |
| 3689 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3690 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3691 | if (!cmd) { |
| 3692 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3693 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3694 | } |
| 3695 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3696 | hci_req_init(&req, hdev); |
| 3697 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3698 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3699 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3700 | err = hci_req_run(&req, stop_discovery_complete); |
| 3701 | if (!err) { |
| 3702 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3703 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3704 | } |
| 3705 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3706 | mgmt_pending_remove(cmd); |
| 3707 | |
| 3708 | /* If no HCI commands were sent we're done */ |
| 3709 | if (err == -ENODATA) { |
| 3710 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 3711 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 3712 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3713 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3714 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3715 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3716 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3717 | return err; |
| 3718 | } |
| 3719 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3720 | 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] | 3721 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3722 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3723 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3724 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3725 | int err; |
| 3726 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3727 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3728 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3729 | hci_dev_lock(hdev); |
| 3730 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3731 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3732 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3733 | MGMT_STATUS_FAILED, &cp->addr, |
| 3734 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3735 | goto failed; |
| 3736 | } |
| 3737 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3738 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3739 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3740 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3741 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3742 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3743 | goto failed; |
| 3744 | } |
| 3745 | |
| 3746 | if (cp->name_known) { |
| 3747 | e->name_state = NAME_KNOWN; |
| 3748 | list_del(&e->list); |
| 3749 | } else { |
| 3750 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3751 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3752 | } |
| 3753 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3754 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3755 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3756 | |
| 3757 | failed: |
| 3758 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3759 | return err; |
| 3760 | } |
| 3761 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3762 | 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] | 3763 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3764 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3765 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3766 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3767 | int err; |
| 3768 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3769 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3770 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3771 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3772 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3773 | MGMT_STATUS_INVALID_PARAMS, |
| 3774 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3775 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3776 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3777 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3778 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3779 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3780 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3781 | goto done; |
| 3782 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3783 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3784 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3785 | sk); |
| 3786 | status = MGMT_STATUS_SUCCESS; |
| 3787 | |
| 3788 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3789 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3790 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3791 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3792 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3793 | |
| 3794 | return err; |
| 3795 | } |
| 3796 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3797 | 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] | 3798 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3799 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3800 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3801 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3802 | int err; |
| 3803 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3804 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3805 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3806 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3807 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3808 | MGMT_STATUS_INVALID_PARAMS, |
| 3809 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3810 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3811 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3812 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3813 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3814 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3815 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3816 | goto done; |
| 3817 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3818 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3819 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3820 | sk); |
| 3821 | status = MGMT_STATUS_SUCCESS; |
| 3822 | |
| 3823 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3824 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3825 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3826 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3827 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3828 | |
| 3829 | return err; |
| 3830 | } |
| 3831 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3832 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3833 | u16 len) |
| 3834 | { |
| 3835 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3836 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3837 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3838 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3839 | |
| 3840 | BT_DBG("%s", hdev->name); |
| 3841 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3842 | source = __le16_to_cpu(cp->source); |
| 3843 | |
| 3844 | if (source > 0x0002) |
| 3845 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3846 | MGMT_STATUS_INVALID_PARAMS); |
| 3847 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3848 | hci_dev_lock(hdev); |
| 3849 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3850 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3851 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 3852 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 3853 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 3854 | |
| 3855 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 3856 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3857 | hci_req_init(&req, hdev); |
| 3858 | update_eir(&req); |
| 3859 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3860 | |
| 3861 | hci_dev_unlock(hdev); |
| 3862 | |
| 3863 | return err; |
| 3864 | } |
| 3865 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3866 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 3867 | { |
| 3868 | struct cmd_lookup match = { NULL, hdev }; |
| 3869 | |
| 3870 | if (status) { |
| 3871 | u8 mgmt_err = mgmt_status(status); |
| 3872 | |
| 3873 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 3874 | cmd_status_rsp, &mgmt_err); |
| 3875 | return; |
| 3876 | } |
| 3877 | |
| 3878 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 3879 | &match); |
| 3880 | |
| 3881 | new_settings(hdev, match.sk); |
| 3882 | |
| 3883 | if (match.sk) |
| 3884 | sock_put(match.sk); |
| 3885 | } |
| 3886 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 3887 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3888 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3889 | { |
| 3890 | struct mgmt_mode *cp = data; |
| 3891 | struct pending_cmd *cmd; |
| 3892 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3893 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3894 | int err; |
| 3895 | |
| 3896 | BT_DBG("request for %s", hdev->name); |
| 3897 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3898 | status = mgmt_le_support(hdev); |
| 3899 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3900 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3901 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3902 | |
| 3903 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3904 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3905 | MGMT_STATUS_INVALID_PARAMS); |
| 3906 | |
| 3907 | hci_dev_lock(hdev); |
| 3908 | |
| 3909 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3910 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3911 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 3912 | /* The following conditions are ones which mean that we should |
| 3913 | * not do any HCI communication but directly send a mgmt |
| 3914 | * response to user space (after toggling the flag if |
| 3915 | * necessary). |
| 3916 | */ |
| 3917 | if (!hdev_is_powered(hdev) || val == enabled || |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 3918 | hci_conn_num(hdev, LE_LINK) > 0) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3919 | bool changed = false; |
| 3920 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3921 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 3922 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3923 | changed = true; |
| 3924 | } |
| 3925 | |
| 3926 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 3927 | if (err < 0) |
| 3928 | goto unlock; |
| 3929 | |
| 3930 | if (changed) |
| 3931 | err = new_settings(hdev, sk); |
| 3932 | |
| 3933 | goto unlock; |
| 3934 | } |
| 3935 | |
| 3936 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 3937 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 3938 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3939 | MGMT_STATUS_BUSY); |
| 3940 | goto unlock; |
| 3941 | } |
| 3942 | |
| 3943 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 3944 | if (!cmd) { |
| 3945 | err = -ENOMEM; |
| 3946 | goto unlock; |
| 3947 | } |
| 3948 | |
| 3949 | hci_req_init(&req, hdev); |
| 3950 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 3951 | if (val) |
| 3952 | enable_advertising(&req); |
| 3953 | else |
| 3954 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3955 | |
| 3956 | err = hci_req_run(&req, set_advertising_complete); |
| 3957 | if (err < 0) |
| 3958 | mgmt_pending_remove(cmd); |
| 3959 | |
| 3960 | unlock: |
| 3961 | hci_dev_unlock(hdev); |
| 3962 | return err; |
| 3963 | } |
| 3964 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3965 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 3966 | void *data, u16 len) |
| 3967 | { |
| 3968 | struct mgmt_cp_set_static_address *cp = data; |
| 3969 | int err; |
| 3970 | |
| 3971 | BT_DBG("%s", hdev->name); |
| 3972 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3973 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3974 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3975 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3976 | |
| 3977 | if (hdev_is_powered(hdev)) |
| 3978 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 3979 | MGMT_STATUS_REJECTED); |
| 3980 | |
| 3981 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 3982 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 3983 | return cmd_status(sk, hdev->id, |
| 3984 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3985 | MGMT_STATUS_INVALID_PARAMS); |
| 3986 | |
| 3987 | /* Two most significant bits shall be set */ |
| 3988 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 3989 | return cmd_status(sk, hdev->id, |
| 3990 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3991 | MGMT_STATUS_INVALID_PARAMS); |
| 3992 | } |
| 3993 | |
| 3994 | hci_dev_lock(hdev); |
| 3995 | |
| 3996 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 3997 | |
| 3998 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 3999 | |
| 4000 | hci_dev_unlock(hdev); |
| 4001 | |
| 4002 | return err; |
| 4003 | } |
| 4004 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4005 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4006 | void *data, u16 len) |
| 4007 | { |
| 4008 | struct mgmt_cp_set_scan_params *cp = data; |
| 4009 | __u16 interval, window; |
| 4010 | int err; |
| 4011 | |
| 4012 | BT_DBG("%s", hdev->name); |
| 4013 | |
| 4014 | if (!lmp_le_capable(hdev)) |
| 4015 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4016 | MGMT_STATUS_NOT_SUPPORTED); |
| 4017 | |
| 4018 | interval = __le16_to_cpu(cp->interval); |
| 4019 | |
| 4020 | if (interval < 0x0004 || interval > 0x4000) |
| 4021 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4022 | MGMT_STATUS_INVALID_PARAMS); |
| 4023 | |
| 4024 | window = __le16_to_cpu(cp->window); |
| 4025 | |
| 4026 | if (window < 0x0004 || window > 0x4000) |
| 4027 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4028 | MGMT_STATUS_INVALID_PARAMS); |
| 4029 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4030 | if (window > interval) |
| 4031 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4032 | MGMT_STATUS_INVALID_PARAMS); |
| 4033 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4034 | hci_dev_lock(hdev); |
| 4035 | |
| 4036 | hdev->le_scan_interval = interval; |
| 4037 | hdev->le_scan_window = window; |
| 4038 | |
| 4039 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 4040 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4041 | /* If background scan is running, restart it so new parameters are |
| 4042 | * loaded. |
| 4043 | */ |
| 4044 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4045 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4046 | struct hci_request req; |
| 4047 | |
| 4048 | hci_req_init(&req, hdev); |
| 4049 | |
| 4050 | hci_req_add_le_scan_disable(&req); |
| 4051 | hci_req_add_le_passive_scan(&req); |
| 4052 | |
| 4053 | hci_req_run(&req, NULL); |
| 4054 | } |
| 4055 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4056 | hci_dev_unlock(hdev); |
| 4057 | |
| 4058 | return err; |
| 4059 | } |
| 4060 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4061 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 4062 | { |
| 4063 | struct pending_cmd *cmd; |
| 4064 | |
| 4065 | BT_DBG("status 0x%02x", status); |
| 4066 | |
| 4067 | hci_dev_lock(hdev); |
| 4068 | |
| 4069 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4070 | if (!cmd) |
| 4071 | goto unlock; |
| 4072 | |
| 4073 | if (status) { |
| 4074 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4075 | mgmt_status(status)); |
| 4076 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4077 | struct mgmt_mode *cp = cmd->param; |
| 4078 | |
| 4079 | if (cp->val) |
| 4080 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4081 | else |
| 4082 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4083 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4084 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4085 | new_settings(hdev, cmd->sk); |
| 4086 | } |
| 4087 | |
| 4088 | mgmt_pending_remove(cmd); |
| 4089 | |
| 4090 | unlock: |
| 4091 | hci_dev_unlock(hdev); |
| 4092 | } |
| 4093 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4094 | 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] | 4095 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4096 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4097 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4098 | struct pending_cmd *cmd; |
| 4099 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4100 | int err; |
| 4101 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4102 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4103 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4104 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4105 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4106 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4107 | MGMT_STATUS_NOT_SUPPORTED); |
| 4108 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4109 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4110 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4111 | MGMT_STATUS_INVALID_PARAMS); |
| 4112 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4113 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4114 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4115 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4116 | |
| 4117 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4118 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4119 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4120 | |
| 4121 | hci_dev_lock(hdev); |
| 4122 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4123 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4124 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4125 | MGMT_STATUS_BUSY); |
| 4126 | goto unlock; |
| 4127 | } |
| 4128 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4129 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4130 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4131 | hdev); |
| 4132 | goto unlock; |
| 4133 | } |
| 4134 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4135 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4136 | data, len); |
| 4137 | if (!cmd) { |
| 4138 | err = -ENOMEM; |
| 4139 | goto unlock; |
| 4140 | } |
| 4141 | |
| 4142 | hci_req_init(&req, hdev); |
| 4143 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4144 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4145 | |
| 4146 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4147 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4148 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4149 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4150 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4151 | } |
| 4152 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4153 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4154 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4155 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4156 | return err; |
| 4157 | } |
| 4158 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 4159 | static void set_bredr_scan(struct hci_request *req) |
| 4160 | { |
| 4161 | struct hci_dev *hdev = req->hdev; |
| 4162 | u8 scan = 0; |
| 4163 | |
| 4164 | /* Ensure that fast connectable is disabled. This function will |
| 4165 | * not do anything if the page scan parameters are already what |
| 4166 | * they should be. |
| 4167 | */ |
| 4168 | write_fast_connectable(req, false); |
| 4169 | |
| 4170 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4171 | scan |= SCAN_PAGE; |
| 4172 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 4173 | scan |= SCAN_INQUIRY; |
| 4174 | |
| 4175 | if (scan) |
| 4176 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 4177 | } |
| 4178 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4179 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4180 | { |
| 4181 | struct pending_cmd *cmd; |
| 4182 | |
| 4183 | BT_DBG("status 0x%02x", status); |
| 4184 | |
| 4185 | hci_dev_lock(hdev); |
| 4186 | |
| 4187 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4188 | if (!cmd) |
| 4189 | goto unlock; |
| 4190 | |
| 4191 | if (status) { |
| 4192 | u8 mgmt_err = mgmt_status(status); |
| 4193 | |
| 4194 | /* We need to restore the flag if related HCI commands |
| 4195 | * failed. |
| 4196 | */ |
| 4197 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4198 | |
| 4199 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4200 | } else { |
| 4201 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4202 | new_settings(hdev, cmd->sk); |
| 4203 | } |
| 4204 | |
| 4205 | mgmt_pending_remove(cmd); |
| 4206 | |
| 4207 | unlock: |
| 4208 | hci_dev_unlock(hdev); |
| 4209 | } |
| 4210 | |
| 4211 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4212 | { |
| 4213 | struct mgmt_mode *cp = data; |
| 4214 | struct pending_cmd *cmd; |
| 4215 | struct hci_request req; |
| 4216 | int err; |
| 4217 | |
| 4218 | BT_DBG("request for %s", hdev->name); |
| 4219 | |
| 4220 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4221 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4222 | MGMT_STATUS_NOT_SUPPORTED); |
| 4223 | |
| 4224 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4225 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4226 | MGMT_STATUS_REJECTED); |
| 4227 | |
| 4228 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4229 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4230 | MGMT_STATUS_INVALID_PARAMS); |
| 4231 | |
| 4232 | hci_dev_lock(hdev); |
| 4233 | |
| 4234 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4235 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4236 | goto unlock; |
| 4237 | } |
| 4238 | |
| 4239 | if (!hdev_is_powered(hdev)) { |
| 4240 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4241 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4242 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4243 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4244 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4245 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4246 | } |
| 4247 | |
| 4248 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4249 | |
| 4250 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4251 | if (err < 0) |
| 4252 | goto unlock; |
| 4253 | |
| 4254 | err = new_settings(hdev, sk); |
| 4255 | goto unlock; |
| 4256 | } |
| 4257 | |
| 4258 | /* Reject disabling when powered on */ |
| 4259 | if (!cp->val) { |
| 4260 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4261 | MGMT_STATUS_REJECTED); |
| 4262 | goto unlock; |
| 4263 | } |
| 4264 | |
| 4265 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4266 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4267 | MGMT_STATUS_BUSY); |
| 4268 | goto unlock; |
| 4269 | } |
| 4270 | |
| 4271 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4272 | if (!cmd) { |
| 4273 | err = -ENOMEM; |
| 4274 | goto unlock; |
| 4275 | } |
| 4276 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4277 | /* 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] | 4278 | * generates the correct flags. |
| 4279 | */ |
| 4280 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4281 | |
| 4282 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4283 | |
| 4284 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4285 | set_bredr_scan(&req); |
| 4286 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4287 | /* Since only the advertising data flags will change, there |
| 4288 | * is no need to update the scan response data. |
| 4289 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4290 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4291 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4292 | err = hci_req_run(&req, set_bredr_complete); |
| 4293 | if (err < 0) |
| 4294 | mgmt_pending_remove(cmd); |
| 4295 | |
| 4296 | unlock: |
| 4297 | hci_dev_unlock(hdev); |
| 4298 | return err; |
| 4299 | } |
| 4300 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4301 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4302 | void *data, u16 len) |
| 4303 | { |
| 4304 | struct mgmt_mode *cp = data; |
| 4305 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4306 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4307 | int err; |
| 4308 | |
| 4309 | BT_DBG("request for %s", hdev->name); |
| 4310 | |
| 4311 | status = mgmt_bredr_support(hdev); |
| 4312 | if (status) |
| 4313 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4314 | status); |
| 4315 | |
Marcel Holtmann | 5afeac14 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4316 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 4317 | !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4318 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4319 | MGMT_STATUS_NOT_SUPPORTED); |
| 4320 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4321 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4322 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4323 | MGMT_STATUS_INVALID_PARAMS); |
| 4324 | |
| 4325 | hci_dev_lock(hdev); |
| 4326 | |
| 4327 | if (!hdev_is_powered(hdev)) { |
| 4328 | bool changed; |
| 4329 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4330 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4331 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4332 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4333 | if (cp->val == 0x02) |
| 4334 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4335 | else |
| 4336 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4337 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4338 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4339 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4340 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4341 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4342 | |
| 4343 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4344 | if (err < 0) |
| 4345 | goto failed; |
| 4346 | |
| 4347 | if (changed) |
| 4348 | err = new_settings(hdev, sk); |
| 4349 | |
| 4350 | goto failed; |
| 4351 | } |
| 4352 | |
| 4353 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4354 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4355 | MGMT_STATUS_BUSY); |
| 4356 | goto failed; |
| 4357 | } |
| 4358 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4359 | val = !!cp->val; |
| 4360 | |
| 4361 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4362 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4363 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4364 | goto failed; |
| 4365 | } |
| 4366 | |
| 4367 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4368 | if (!cmd) { |
| 4369 | err = -ENOMEM; |
| 4370 | goto failed; |
| 4371 | } |
| 4372 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4373 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4374 | if (err < 0) { |
| 4375 | mgmt_pending_remove(cmd); |
| 4376 | goto failed; |
| 4377 | } |
| 4378 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4379 | if (cp->val == 0x02) |
| 4380 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4381 | else |
| 4382 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4383 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4384 | failed: |
| 4385 | hci_dev_unlock(hdev); |
| 4386 | return err; |
| 4387 | } |
| 4388 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4389 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4390 | void *data, u16 len) |
| 4391 | { |
| 4392 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4393 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4394 | int err; |
| 4395 | |
| 4396 | BT_DBG("request for %s", hdev->name); |
| 4397 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4398 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4399 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4400 | MGMT_STATUS_INVALID_PARAMS); |
| 4401 | |
| 4402 | hci_dev_lock(hdev); |
| 4403 | |
| 4404 | if (cp->val) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4405 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 4406 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4407 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4408 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 4409 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4410 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4411 | if (cp->val == 0x02) |
| 4412 | use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, |
| 4413 | &hdev->dev_flags); |
| 4414 | else |
| 4415 | use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, |
| 4416 | &hdev->dev_flags); |
| 4417 | |
| 4418 | if (hdev_is_powered(hdev) && use_changed && |
| 4419 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 4420 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4421 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4422 | sizeof(mode), &mode); |
| 4423 | } |
| 4424 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4425 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4426 | if (err < 0) |
| 4427 | goto unlock; |
| 4428 | |
| 4429 | if (changed) |
| 4430 | err = new_settings(hdev, sk); |
| 4431 | |
| 4432 | unlock: |
| 4433 | hci_dev_unlock(hdev); |
| 4434 | return err; |
| 4435 | } |
| 4436 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4437 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4438 | u16 len) |
| 4439 | { |
| 4440 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4441 | bool changed; |
| 4442 | int err; |
| 4443 | |
| 4444 | BT_DBG("request for %s", hdev->name); |
| 4445 | |
| 4446 | if (!lmp_le_capable(hdev)) |
| 4447 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4448 | MGMT_STATUS_NOT_SUPPORTED); |
| 4449 | |
| 4450 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4451 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4452 | MGMT_STATUS_INVALID_PARAMS); |
| 4453 | |
| 4454 | if (hdev_is_powered(hdev)) |
| 4455 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4456 | MGMT_STATUS_REJECTED); |
| 4457 | |
| 4458 | hci_dev_lock(hdev); |
| 4459 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4460 | /* If user space supports this command it is also expected to |
| 4461 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4462 | */ |
| 4463 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4464 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4465 | if (cp->privacy) { |
| 4466 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4467 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4468 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4469 | } else { |
| 4470 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4471 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4472 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4473 | } |
| 4474 | |
| 4475 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4476 | if (err < 0) |
| 4477 | goto unlock; |
| 4478 | |
| 4479 | if (changed) |
| 4480 | err = new_settings(hdev, sk); |
| 4481 | |
| 4482 | unlock: |
| 4483 | hci_dev_unlock(hdev); |
| 4484 | return err; |
| 4485 | } |
| 4486 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4487 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4488 | { |
| 4489 | switch (irk->addr.type) { |
| 4490 | case BDADDR_LE_PUBLIC: |
| 4491 | return true; |
| 4492 | |
| 4493 | case BDADDR_LE_RANDOM: |
| 4494 | /* Two most significant bits shall be set */ |
| 4495 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4496 | return false; |
| 4497 | return true; |
| 4498 | } |
| 4499 | |
| 4500 | return false; |
| 4501 | } |
| 4502 | |
| 4503 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4504 | u16 len) |
| 4505 | { |
| 4506 | struct mgmt_cp_load_irks *cp = cp_data; |
| 4507 | u16 irk_count, expected_len; |
| 4508 | int i, err; |
| 4509 | |
| 4510 | BT_DBG("request for %s", hdev->name); |
| 4511 | |
| 4512 | if (!lmp_le_capable(hdev)) |
| 4513 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4514 | MGMT_STATUS_NOT_SUPPORTED); |
| 4515 | |
| 4516 | irk_count = __le16_to_cpu(cp->irk_count); |
| 4517 | |
| 4518 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4519 | if (expected_len != len) { |
| 4520 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4521 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4522 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4523 | MGMT_STATUS_INVALID_PARAMS); |
| 4524 | } |
| 4525 | |
| 4526 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4527 | |
| 4528 | for (i = 0; i < irk_count; i++) { |
| 4529 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4530 | |
| 4531 | if (!irk_is_valid(key)) |
| 4532 | return cmd_status(sk, hdev->id, |
| 4533 | MGMT_OP_LOAD_IRKS, |
| 4534 | MGMT_STATUS_INVALID_PARAMS); |
| 4535 | } |
| 4536 | |
| 4537 | hci_dev_lock(hdev); |
| 4538 | |
| 4539 | hci_smp_irks_clear(hdev); |
| 4540 | |
| 4541 | for (i = 0; i < irk_count; i++) { |
| 4542 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4543 | u8 addr_type; |
| 4544 | |
| 4545 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4546 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4547 | else |
| 4548 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4549 | |
| 4550 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4551 | BDADDR_ANY); |
| 4552 | } |
| 4553 | |
| 4554 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4555 | |
| 4556 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4557 | |
| 4558 | hci_dev_unlock(hdev); |
| 4559 | |
| 4560 | return err; |
| 4561 | } |
| 4562 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4563 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4564 | { |
| 4565 | if (key->master != 0x00 && key->master != 0x01) |
| 4566 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4567 | |
| 4568 | switch (key->addr.type) { |
| 4569 | case BDADDR_LE_PUBLIC: |
| 4570 | return true; |
| 4571 | |
| 4572 | case BDADDR_LE_RANDOM: |
| 4573 | /* Two most significant bits shall be set */ |
| 4574 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4575 | return false; |
| 4576 | return true; |
| 4577 | } |
| 4578 | |
| 4579 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4580 | } |
| 4581 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4582 | 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] | 4583 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4584 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4585 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
| 4586 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4587 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4588 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4589 | BT_DBG("request for %s", hdev->name); |
| 4590 | |
| 4591 | if (!lmp_le_capable(hdev)) |
| 4592 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4593 | MGMT_STATUS_NOT_SUPPORTED); |
| 4594 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4595 | key_count = __le16_to_cpu(cp->key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4596 | |
| 4597 | expected_len = sizeof(*cp) + key_count * |
| 4598 | sizeof(struct mgmt_ltk_info); |
| 4599 | if (expected_len != len) { |
| 4600 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4601 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4602 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4603 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4604 | } |
| 4605 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4606 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4607 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4608 | for (i = 0; i < key_count; i++) { |
| 4609 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4610 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4611 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4612 | return cmd_status(sk, hdev->id, |
| 4613 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4614 | MGMT_STATUS_INVALID_PARAMS); |
| 4615 | } |
| 4616 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4617 | hci_dev_lock(hdev); |
| 4618 | |
| 4619 | hci_smp_ltks_clear(hdev); |
| 4620 | |
| 4621 | for (i = 0; i < key_count; i++) { |
| 4622 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4623 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4624 | |
| 4625 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4626 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4627 | else |
| 4628 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4629 | |
| 4630 | if (key->master) |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4631 | type = SMP_LTK; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4632 | else |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4633 | type = SMP_LTK_SLAVE; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4634 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4635 | switch (key->type) { |
| 4636 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4637 | authenticated = 0x00; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4638 | break; |
| 4639 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4640 | authenticated = 0x01; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4641 | break; |
| 4642 | default: |
| 4643 | continue; |
| 4644 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4645 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4646 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4647 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4648 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4649 | } |
| 4650 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4651 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4652 | NULL, 0); |
| 4653 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4654 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4655 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4656 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4657 | } |
| 4658 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4659 | struct cmd_conn_lookup { |
| 4660 | struct hci_conn *conn; |
| 4661 | bool valid_tx_power; |
| 4662 | u8 mgmt_status; |
| 4663 | }; |
| 4664 | |
| 4665 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 4666 | { |
| 4667 | struct cmd_conn_lookup *match = data; |
| 4668 | struct mgmt_cp_get_conn_info *cp; |
| 4669 | struct mgmt_rp_get_conn_info rp; |
| 4670 | struct hci_conn *conn = cmd->user_data; |
| 4671 | |
| 4672 | if (conn != match->conn) |
| 4673 | return; |
| 4674 | |
| 4675 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 4676 | |
| 4677 | memset(&rp, 0, sizeof(rp)); |
| 4678 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4679 | rp.addr.type = cp->addr.type; |
| 4680 | |
| 4681 | if (!match->mgmt_status) { |
| 4682 | rp.rssi = conn->rssi; |
| 4683 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4684 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4685 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4686 | rp.max_tx_power = conn->max_tx_power; |
| 4687 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4688 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4689 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 4690 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4691 | } |
| 4692 | |
| 4693 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4694 | match->mgmt_status, &rp, sizeof(rp)); |
| 4695 | |
| 4696 | hci_conn_drop(conn); |
| 4697 | |
| 4698 | mgmt_pending_remove(cmd); |
| 4699 | } |
| 4700 | |
| 4701 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 4702 | { |
| 4703 | struct hci_cp_read_rssi *cp; |
| 4704 | struct hci_conn *conn; |
| 4705 | struct cmd_conn_lookup match; |
| 4706 | u16 handle; |
| 4707 | |
| 4708 | BT_DBG("status 0x%02x", status); |
| 4709 | |
| 4710 | hci_dev_lock(hdev); |
| 4711 | |
| 4712 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4713 | * otherwise we assume it's not valid. At the moment we assume that |
| 4714 | * either both or none of current and max values are valid to keep code |
| 4715 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4716 | */ |
| 4717 | match.valid_tx_power = !status; |
| 4718 | |
| 4719 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4720 | * Level so we check which one was last sent to retrieve connection |
| 4721 | * handle. Both commands have handle as first parameter so it's safe to |
| 4722 | * cast data on the same command struct. |
| 4723 | * |
| 4724 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4725 | * In other case we simply override error to indicate success as we |
| 4726 | * already remembered if TX power value is actually valid. |
| 4727 | */ |
| 4728 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4729 | if (!cp) { |
| 4730 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 4731 | status = 0; |
| 4732 | } |
| 4733 | |
| 4734 | if (!cp) { |
| 4735 | BT_ERR("invalid sent_cmd in response"); |
| 4736 | goto unlock; |
| 4737 | } |
| 4738 | |
| 4739 | handle = __le16_to_cpu(cp->handle); |
| 4740 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4741 | if (!conn) { |
| 4742 | BT_ERR("unknown handle (%d) in response", handle); |
| 4743 | goto unlock; |
| 4744 | } |
| 4745 | |
| 4746 | match.conn = conn; |
| 4747 | match.mgmt_status = mgmt_status(status); |
| 4748 | |
| 4749 | /* Cache refresh is complete, now reply for mgmt request for given |
| 4750 | * connection only. |
| 4751 | */ |
| 4752 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 4753 | get_conn_info_complete, &match); |
| 4754 | |
| 4755 | unlock: |
| 4756 | hci_dev_unlock(hdev); |
| 4757 | } |
| 4758 | |
| 4759 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4760 | u16 len) |
| 4761 | { |
| 4762 | struct mgmt_cp_get_conn_info *cp = data; |
| 4763 | struct mgmt_rp_get_conn_info rp; |
| 4764 | struct hci_conn *conn; |
| 4765 | unsigned long conn_info_age; |
| 4766 | int err = 0; |
| 4767 | |
| 4768 | BT_DBG("%s", hdev->name); |
| 4769 | |
| 4770 | memset(&rp, 0, sizeof(rp)); |
| 4771 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4772 | rp.addr.type = cp->addr.type; |
| 4773 | |
| 4774 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 4775 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4776 | MGMT_STATUS_INVALID_PARAMS, |
| 4777 | &rp, sizeof(rp)); |
| 4778 | |
| 4779 | hci_dev_lock(hdev); |
| 4780 | |
| 4781 | if (!hdev_is_powered(hdev)) { |
| 4782 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4783 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4784 | goto unlock; |
| 4785 | } |
| 4786 | |
| 4787 | if (cp->addr.type == BDADDR_BREDR) |
| 4788 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4789 | &cp->addr.bdaddr); |
| 4790 | else |
| 4791 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 4792 | |
| 4793 | if (!conn || conn->state != BT_CONNECTED) { |
| 4794 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4795 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 4796 | goto unlock; |
| 4797 | } |
| 4798 | |
| 4799 | /* To avoid client trying to guess when to poll again for information we |
| 4800 | * calculate conn info age as random value between min/max set in hdev. |
| 4801 | */ |
| 4802 | conn_info_age = hdev->conn_info_min_age + |
| 4803 | prandom_u32_max(hdev->conn_info_max_age - |
| 4804 | hdev->conn_info_min_age); |
| 4805 | |
| 4806 | /* Query controller to refresh cached values if they are too old or were |
| 4807 | * never read. |
| 4808 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 4809 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 4810 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4811 | !conn->conn_info_timestamp) { |
| 4812 | struct hci_request req; |
| 4813 | struct hci_cp_read_tx_power req_txp_cp; |
| 4814 | struct hci_cp_read_rssi req_rssi_cp; |
| 4815 | struct pending_cmd *cmd; |
| 4816 | |
| 4817 | hci_req_init(&req, hdev); |
| 4818 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 4819 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 4820 | &req_rssi_cp); |
| 4821 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 4822 | /* For LE links TX power does not change thus we don't need to |
| 4823 | * query for it once value is known. |
| 4824 | */ |
| 4825 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 4826 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 4827 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4828 | req_txp_cp.type = 0x00; |
| 4829 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4830 | sizeof(req_txp_cp), &req_txp_cp); |
| 4831 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4832 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4833 | /* Max TX power needs to be read only once per connection */ |
| 4834 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 4835 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4836 | req_txp_cp.type = 0x01; |
| 4837 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4838 | sizeof(req_txp_cp), &req_txp_cp); |
| 4839 | } |
| 4840 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4841 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 4842 | if (err < 0) |
| 4843 | goto unlock; |
| 4844 | |
| 4845 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 4846 | data, len); |
| 4847 | if (!cmd) { |
| 4848 | err = -ENOMEM; |
| 4849 | goto unlock; |
| 4850 | } |
| 4851 | |
| 4852 | hci_conn_hold(conn); |
| 4853 | cmd->user_data = conn; |
| 4854 | |
| 4855 | conn->conn_info_timestamp = jiffies; |
| 4856 | } else { |
| 4857 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 4858 | rp.rssi = conn->rssi; |
| 4859 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4860 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4861 | |
| 4862 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4863 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 4864 | } |
| 4865 | |
| 4866 | unlock: |
| 4867 | hci_dev_unlock(hdev); |
| 4868 | return err; |
| 4869 | } |
| 4870 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 4871 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status) |
| 4872 | { |
| 4873 | struct mgmt_cp_get_clock_info *cp; |
| 4874 | struct mgmt_rp_get_clock_info rp; |
| 4875 | struct hci_cp_read_clock *hci_cp; |
| 4876 | struct pending_cmd *cmd; |
| 4877 | struct hci_conn *conn; |
| 4878 | |
| 4879 | BT_DBG("%s status %u", hdev->name, status); |
| 4880 | |
| 4881 | hci_dev_lock(hdev); |
| 4882 | |
| 4883 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 4884 | if (!hci_cp) |
| 4885 | goto unlock; |
| 4886 | |
| 4887 | if (hci_cp->which) { |
| 4888 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 4889 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4890 | } else { |
| 4891 | conn = NULL; |
| 4892 | } |
| 4893 | |
| 4894 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 4895 | if (!cmd) |
| 4896 | goto unlock; |
| 4897 | |
| 4898 | cp = cmd->param; |
| 4899 | |
| 4900 | memset(&rp, 0, sizeof(rp)); |
| 4901 | memcpy(&rp.addr, &cp->addr, sizeof(rp.addr)); |
| 4902 | |
| 4903 | if (status) |
| 4904 | goto send_rsp; |
| 4905 | |
| 4906 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 4907 | |
| 4908 | if (conn) { |
| 4909 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 4910 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 4911 | } |
| 4912 | |
| 4913 | send_rsp: |
| 4914 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 4915 | &rp, sizeof(rp)); |
| 4916 | mgmt_pending_remove(cmd); |
| 4917 | if (conn) |
| 4918 | hci_conn_drop(conn); |
| 4919 | |
| 4920 | unlock: |
| 4921 | hci_dev_unlock(hdev); |
| 4922 | } |
| 4923 | |
| 4924 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4925 | u16 len) |
| 4926 | { |
| 4927 | struct mgmt_cp_get_clock_info *cp = data; |
| 4928 | struct mgmt_rp_get_clock_info rp; |
| 4929 | struct hci_cp_read_clock hci_cp; |
| 4930 | struct pending_cmd *cmd; |
| 4931 | struct hci_request req; |
| 4932 | struct hci_conn *conn; |
| 4933 | int err; |
| 4934 | |
| 4935 | BT_DBG("%s", hdev->name); |
| 4936 | |
| 4937 | memset(&rp, 0, sizeof(rp)); |
| 4938 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4939 | rp.addr.type = cp->addr.type; |
| 4940 | |
| 4941 | if (cp->addr.type != BDADDR_BREDR) |
| 4942 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 4943 | MGMT_STATUS_INVALID_PARAMS, |
| 4944 | &rp, sizeof(rp)); |
| 4945 | |
| 4946 | hci_dev_lock(hdev); |
| 4947 | |
| 4948 | if (!hdev_is_powered(hdev)) { |
| 4949 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 4950 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4951 | goto unlock; |
| 4952 | } |
| 4953 | |
| 4954 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 4955 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4956 | &cp->addr.bdaddr); |
| 4957 | if (!conn || conn->state != BT_CONNECTED) { |
| 4958 | err = cmd_complete(sk, hdev->id, |
| 4959 | MGMT_OP_GET_CLOCK_INFO, |
| 4960 | MGMT_STATUS_NOT_CONNECTED, |
| 4961 | &rp, sizeof(rp)); |
| 4962 | goto unlock; |
| 4963 | } |
| 4964 | } else { |
| 4965 | conn = NULL; |
| 4966 | } |
| 4967 | |
| 4968 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 4969 | if (!cmd) { |
| 4970 | err = -ENOMEM; |
| 4971 | goto unlock; |
| 4972 | } |
| 4973 | |
| 4974 | hci_req_init(&req, hdev); |
| 4975 | |
| 4976 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 4977 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 4978 | |
| 4979 | if (conn) { |
| 4980 | hci_conn_hold(conn); |
| 4981 | cmd->user_data = conn; |
| 4982 | |
| 4983 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 4984 | hci_cp.which = 0x01; /* Piconet clock */ |
| 4985 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 4986 | } |
| 4987 | |
| 4988 | err = hci_req_run(&req, get_clock_info_complete); |
| 4989 | if (err < 0) |
| 4990 | mgmt_pending_remove(cmd); |
| 4991 | |
| 4992 | unlock: |
| 4993 | hci_dev_unlock(hdev); |
| 4994 | return err; |
| 4995 | } |
| 4996 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 4997 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 4998 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 4999 | { |
| 5000 | struct mgmt_ev_device_added ev; |
| 5001 | |
| 5002 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5003 | ev.addr.type = type; |
| 5004 | ev.action = action; |
| 5005 | |
| 5006 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5007 | } |
| 5008 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5009 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5010 | void *data, u16 len) |
| 5011 | { |
| 5012 | struct mgmt_cp_add_device *cp = data; |
| 5013 | u8 auto_conn, addr_type; |
| 5014 | int err; |
| 5015 | |
| 5016 | BT_DBG("%s", hdev->name); |
| 5017 | |
| 5018 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5019 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
| 5020 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5021 | MGMT_STATUS_INVALID_PARAMS, |
| 5022 | &cp->addr, sizeof(cp->addr)); |
| 5023 | |
| 5024 | if (cp->action != 0x00 && cp->action != 0x01) |
| 5025 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5026 | MGMT_STATUS_INVALID_PARAMS, |
| 5027 | &cp->addr, sizeof(cp->addr)); |
| 5028 | |
| 5029 | hci_dev_lock(hdev); |
| 5030 | |
| 5031 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5032 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5033 | else |
| 5034 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5035 | |
| 5036 | if (cp->action) |
| 5037 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
| 5038 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5039 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5040 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5041 | /* If the connection parameters don't exist for this device, |
| 5042 | * they will be created and configured with defaults. |
| 5043 | */ |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5044 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
| 5045 | auto_conn) < 0) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5046 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5047 | MGMT_STATUS_FAILED, |
| 5048 | &cp->addr, sizeof(cp->addr)); |
| 5049 | goto unlock; |
| 5050 | } |
| 5051 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5052 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5053 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5054 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5055 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5056 | |
| 5057 | unlock: |
| 5058 | hci_dev_unlock(hdev); |
| 5059 | return err; |
| 5060 | } |
| 5061 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5062 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5063 | bdaddr_t *bdaddr, u8 type) |
| 5064 | { |
| 5065 | struct mgmt_ev_device_removed ev; |
| 5066 | |
| 5067 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5068 | ev.addr.type = type; |
| 5069 | |
| 5070 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5071 | } |
| 5072 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5073 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5074 | void *data, u16 len) |
| 5075 | { |
| 5076 | struct mgmt_cp_remove_device *cp = data; |
| 5077 | int err; |
| 5078 | |
| 5079 | BT_DBG("%s", hdev->name); |
| 5080 | |
| 5081 | hci_dev_lock(hdev); |
| 5082 | |
| 5083 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5084 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5085 | u8 addr_type; |
| 5086 | |
| 5087 | if (!bdaddr_type_is_le(cp->addr.type)) { |
| 5088 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5089 | MGMT_STATUS_INVALID_PARAMS, |
| 5090 | &cp->addr, sizeof(cp->addr)); |
| 5091 | goto unlock; |
| 5092 | } |
| 5093 | |
| 5094 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5095 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5096 | else |
| 5097 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5098 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5099 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5100 | addr_type); |
| 5101 | if (!params) { |
| 5102 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5103 | MGMT_STATUS_INVALID_PARAMS, |
| 5104 | &cp->addr, sizeof(cp->addr)); |
| 5105 | goto unlock; |
| 5106 | } |
| 5107 | |
| 5108 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED) { |
| 5109 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5110 | MGMT_STATUS_INVALID_PARAMS, |
| 5111 | &cp->addr, sizeof(cp->addr)); |
| 5112 | goto unlock; |
| 5113 | } |
| 5114 | |
| 5115 | hci_pend_le_conn_del(hdev, &cp->addr.bdaddr, addr_type); |
| 5116 | list_del(¶ms->list); |
| 5117 | kfree(params); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5118 | |
| 5119 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5120 | } else { |
| 5121 | if (cp->addr.type) { |
| 5122 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5123 | MGMT_STATUS_INVALID_PARAMS, |
| 5124 | &cp->addr, sizeof(cp->addr)); |
| 5125 | goto unlock; |
| 5126 | } |
| 5127 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5128 | hci_conn_params_clear_enabled(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5129 | } |
| 5130 | |
| 5131 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5132 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5133 | |
| 5134 | unlock: |
| 5135 | hci_dev_unlock(hdev); |
| 5136 | return err; |
| 5137 | } |
| 5138 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5139 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5140 | u16 len) |
| 5141 | { |
| 5142 | struct mgmt_cp_load_conn_param *cp = data; |
| 5143 | u16 param_count, expected_len; |
| 5144 | int i; |
| 5145 | |
| 5146 | if (!lmp_le_capable(hdev)) |
| 5147 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5148 | MGMT_STATUS_NOT_SUPPORTED); |
| 5149 | |
| 5150 | param_count = __le16_to_cpu(cp->param_count); |
| 5151 | |
| 5152 | expected_len = sizeof(*cp) + param_count * |
| 5153 | sizeof(struct mgmt_conn_param); |
| 5154 | if (expected_len != len) { |
| 5155 | BT_ERR("load_conn_param: expected %u bytes, got %u bytes", |
| 5156 | expected_len, len); |
| 5157 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5158 | MGMT_STATUS_INVALID_PARAMS); |
| 5159 | } |
| 5160 | |
| 5161 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 5162 | |
| 5163 | hci_dev_lock(hdev); |
| 5164 | |
| 5165 | hci_conn_params_clear_disabled(hdev); |
| 5166 | |
| 5167 | for (i = 0; i < param_count; i++) { |
| 5168 | struct mgmt_conn_param *param = &cp->params[i]; |
| 5169 | struct hci_conn_params *hci_param; |
| 5170 | u16 min, max, latency, timeout; |
| 5171 | u8 addr_type; |
| 5172 | |
| 5173 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 5174 | param->addr.type); |
| 5175 | |
| 5176 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 5177 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5178 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 5179 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5180 | } else { |
| 5181 | BT_ERR("Ignoring invalid connection parameters"); |
| 5182 | continue; |
| 5183 | } |
| 5184 | |
| 5185 | min = le16_to_cpu(param->min_interval); |
| 5186 | max = le16_to_cpu(param->max_interval); |
| 5187 | latency = le16_to_cpu(param->latency); |
| 5188 | timeout = le16_to_cpu(param->timeout); |
| 5189 | |
| 5190 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 5191 | min, max, latency, timeout); |
| 5192 | |
| 5193 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
| 5194 | BT_ERR("Ignoring invalid connection parameters"); |
| 5195 | continue; |
| 5196 | } |
| 5197 | |
| 5198 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 5199 | addr_type); |
| 5200 | if (!hci_param) { |
| 5201 | BT_ERR("Failed to add connection parameters"); |
| 5202 | continue; |
| 5203 | } |
| 5204 | |
| 5205 | hci_param->conn_min_interval = min; |
| 5206 | hci_param->conn_max_interval = max; |
| 5207 | hci_param->conn_latency = latency; |
| 5208 | hci_param->supervision_timeout = timeout; |
| 5209 | } |
| 5210 | |
| 5211 | hci_dev_unlock(hdev); |
| 5212 | |
| 5213 | return cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, NULL, 0); |
| 5214 | } |
| 5215 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5216 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5217 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 5218 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5219 | bool var_len; |
| 5220 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5221 | } mgmt_handlers[] = { |
| 5222 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5223 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 5224 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 5225 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 5226 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 5227 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 5228 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 5229 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 5230 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 5231 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 5232 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 5233 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 5234 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 5235 | { set_le, false, MGMT_SETTING_SIZE }, |
| 5236 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 5237 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 5238 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 5239 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 5240 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 5241 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 5242 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 5243 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 5244 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 5245 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 5246 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 5247 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 5248 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 5249 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 5250 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 5251 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 5252 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 5253 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 5254 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5255 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5256 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 5257 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 5258 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 5259 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 5260 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 5261 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5262 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5263 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5264 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5265 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5266 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5267 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5268 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5269 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5270 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5271 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5272 | { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE }, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5273 | { add_device, false, MGMT_ADD_DEVICE_SIZE }, |
| 5274 | { remove_device, false, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5275 | { load_conn_param, true, MGMT_LOAD_CONN_PARAM_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5276 | }; |
| 5277 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5278 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 5279 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5280 | void *buf; |
| 5281 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5282 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 5283 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5284 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5285 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5286 | int err; |
| 5287 | |
| 5288 | BT_DBG("got %zu bytes", msglen); |
| 5289 | |
| 5290 | if (msglen < sizeof(*hdr)) |
| 5291 | return -EINVAL; |
| 5292 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 5293 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5294 | if (!buf) |
| 5295 | return -ENOMEM; |
| 5296 | |
| 5297 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 5298 | err = -EFAULT; |
| 5299 | goto done; |
| 5300 | } |
| 5301 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5302 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5303 | opcode = __le16_to_cpu(hdr->opcode); |
| 5304 | index = __le16_to_cpu(hdr->index); |
| 5305 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5306 | |
| 5307 | if (len != msglen - sizeof(*hdr)) { |
| 5308 | err = -EINVAL; |
| 5309 | goto done; |
| 5310 | } |
| 5311 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5312 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5313 | hdev = hci_dev_get(index); |
| 5314 | if (!hdev) { |
| 5315 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5316 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5317 | goto done; |
| 5318 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5319 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 5320 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 5321 | test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) || |
| 5322 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5323 | err = cmd_status(sk, index, opcode, |
| 5324 | MGMT_STATUS_INVALID_INDEX); |
| 5325 | goto done; |
| 5326 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5327 | } |
| 5328 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5329 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5330 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5331 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5332 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5333 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5334 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5335 | } |
| 5336 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5337 | if ((hdev && opcode < MGMT_OP_READ_INFO) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5338 | (!hdev && opcode >= MGMT_OP_READ_INFO)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5339 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5340 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5341 | goto done; |
| 5342 | } |
| 5343 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5344 | handler = &mgmt_handlers[opcode]; |
| 5345 | |
| 5346 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5347 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5348 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5349 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5350 | goto done; |
| 5351 | } |
| 5352 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5353 | if (hdev) |
| 5354 | mgmt_init_hdev(sk, hdev); |
| 5355 | |
| 5356 | cp = buf + sizeof(*hdr); |
| 5357 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5358 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 5359 | if (err < 0) |
| 5360 | goto done; |
| 5361 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5362 | err = msglen; |
| 5363 | |
| 5364 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5365 | if (hdev) |
| 5366 | hci_dev_put(hdev); |
| 5367 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5368 | kfree(buf); |
| 5369 | return err; |
| 5370 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5371 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5372 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5373 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5374 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5375 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5376 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame^] | 5377 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5378 | return; |
| 5379 | |
| 5380 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5381 | mgmt_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, NULL, 0, NULL); |
| 5382 | else |
| 5383 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5384 | } |
| 5385 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5386 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5387 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 5388 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5389 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5390 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5391 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5392 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame^] | 5393 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5394 | return; |
| 5395 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5396 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5397 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5398 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5399 | } |
| 5400 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5401 | /* This function requires the caller holds hdev->lock */ |
| 5402 | static void restart_le_auto_conns(struct hci_dev *hdev) |
| 5403 | { |
| 5404 | struct hci_conn_params *p; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5405 | bool added = false; |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5406 | |
| 5407 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5408 | if (p->auto_connect == HCI_AUTO_CONN_ALWAYS) { |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5409 | hci_pend_le_conn_add(hdev, &p->addr, p->addr_type); |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5410 | added = true; |
| 5411 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5412 | } |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5413 | |
| 5414 | /* Calling hci_pend_le_conn_add will actually already trigger |
| 5415 | * background scanning when needed. So no need to trigger it |
| 5416 | * just another time. |
| 5417 | * |
| 5418 | * This check is here to avoid an unneeded restart of the |
| 5419 | * passive scanning. Since this is during the controller |
| 5420 | * power up phase the duplicate filtering is not an issue. |
| 5421 | */ |
| 5422 | if (added) |
| 5423 | return; |
| 5424 | |
| 5425 | hci_update_background_scan(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5426 | } |
| 5427 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5428 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 5429 | { |
| 5430 | struct cmd_lookup match = { NULL, hdev }; |
| 5431 | |
| 5432 | BT_DBG("status 0x%02x", status); |
| 5433 | |
| 5434 | hci_dev_lock(hdev); |
| 5435 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5436 | restart_le_auto_conns(hdev); |
| 5437 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5438 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5439 | |
| 5440 | new_settings(hdev, match.sk); |
| 5441 | |
| 5442 | hci_dev_unlock(hdev); |
| 5443 | |
| 5444 | if (match.sk) |
| 5445 | sock_put(match.sk); |
| 5446 | } |
| 5447 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5448 | static int powered_update_hci(struct hci_dev *hdev) |
| 5449 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5450 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5451 | u8 link_sec; |
| 5452 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5453 | hci_req_init(&req, hdev); |
| 5454 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5455 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 5456 | !lmp_host_ssp_capable(hdev)) { |
| 5457 | u8 ssp = 1; |
| 5458 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5459 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5460 | } |
| 5461 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 5462 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 5463 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5464 | struct hci_cp_write_le_host_supported cp; |
| 5465 | |
| 5466 | cp.le = 1; |
| 5467 | cp.simul = lmp_le_br_capable(hdev); |
| 5468 | |
| 5469 | /* Check first if we already have the right |
| 5470 | * host state (host features set) |
| 5471 | */ |
| 5472 | if (cp.le != lmp_host_le_capable(hdev) || |
| 5473 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5474 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 5475 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5476 | } |
| 5477 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5478 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5479 | /* Make sure the controller has a good default for |
| 5480 | * advertising data. This also applies to the case |
| 5481 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 5482 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5483 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 5484 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5485 | update_scan_rsp_data(&req); |
| 5486 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5487 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 5488 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5489 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 5490 | } |
| 5491 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5492 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 5493 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5494 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 5495 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5496 | |
| 5497 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5498 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 5499 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5500 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5501 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5502 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5503 | } |
| 5504 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5505 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5506 | } |
| 5507 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5508 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5509 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 5510 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5511 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 5512 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5513 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5514 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5515 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 5516 | return 0; |
| 5517 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5518 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5519 | if (powered_update_hci(hdev) == 0) |
| 5520 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 5521 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5522 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 5523 | &match); |
| 5524 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5525 | } |
| 5526 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5527 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5528 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 5529 | |
| 5530 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 5531 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 5532 | zero_cod, sizeof(zero_cod), NULL); |
| 5533 | |
| 5534 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5535 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5536 | |
| 5537 | if (match.sk) |
| 5538 | sock_put(match.sk); |
| 5539 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5540 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5541 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5542 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5543 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5544 | { |
| 5545 | struct pending_cmd *cmd; |
| 5546 | u8 status; |
| 5547 | |
| 5548 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5549 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5550 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5551 | |
| 5552 | if (err == -ERFKILL) |
| 5553 | status = MGMT_STATUS_RFKILLED; |
| 5554 | else |
| 5555 | status = MGMT_STATUS_FAILED; |
| 5556 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5557 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5558 | |
| 5559 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5560 | } |
| 5561 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5562 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 5563 | { |
| 5564 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5565 | |
| 5566 | hci_dev_lock(hdev); |
| 5567 | |
| 5568 | /* When discoverable timeout triggers, then just make sure |
| 5569 | * the limited discoverable flag is cleared. Even in the case |
| 5570 | * of a timeout triggered from general discoverable, it is |
| 5571 | * safe to unconditionally clear the flag. |
| 5572 | */ |
| 5573 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5574 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5575 | |
| 5576 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 5577 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 5578 | u8 scan = SCAN_PAGE; |
| 5579 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 5580 | sizeof(scan), &scan); |
| 5581 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5582 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5583 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5584 | hci_req_run(&req, NULL); |
| 5585 | |
| 5586 | hdev->discov_timeout = 0; |
| 5587 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5588 | new_settings(hdev, NULL); |
| 5589 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5590 | hci_dev_unlock(hdev); |
| 5591 | } |
| 5592 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5593 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5594 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5595 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5596 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5597 | /* Nothing needed here if there's a pending command since that |
| 5598 | * commands request completion callback takes care of everything |
| 5599 | * necessary. |
| 5600 | */ |
| 5601 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5602 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5603 | |
Johan Hedberg | bd10799 | 2014-02-24 14:52:19 +0200 | [diff] [blame] | 5604 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5605 | if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5606 | return; |
| 5607 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5608 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5609 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5610 | } else { |
| 5611 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5612 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5613 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5614 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5615 | if (changed) { |
| 5616 | struct hci_request req; |
| 5617 | |
| 5618 | /* In case this change in discoverable was triggered by |
| 5619 | * a disabling of connectable there could be a need to |
| 5620 | * update the advertising flags. |
| 5621 | */ |
| 5622 | hci_req_init(&req, hdev); |
| 5623 | update_adv_data(&req); |
| 5624 | hci_req_run(&req, NULL); |
| 5625 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5626 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5627 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5628 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5629 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5630 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5631 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5632 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5633 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5634 | /* Nothing needed here if there's a pending command since that |
| 5635 | * commands request completion callback takes care of everything |
| 5636 | * necessary. |
| 5637 | */ |
| 5638 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5639 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5640 | |
Johan Hedberg | ce3f24c | 2014-02-24 14:52:20 +0200 | [diff] [blame] | 5641 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5642 | if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5643 | return; |
| 5644 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5645 | if (connectable) |
| 5646 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 5647 | else |
| 5648 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5649 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5650 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5651 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5652 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5653 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5654 | void mgmt_advertising(struct hci_dev *hdev, u8 advertising) |
| 5655 | { |
Johan Hedberg | 7c4cfab | 2014-02-24 14:52:21 +0200 | [diff] [blame] | 5656 | /* Powering off may stop advertising - don't let that interfere */ |
| 5657 | if (!advertising && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5658 | return; |
| 5659 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5660 | if (advertising) |
| 5661 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5662 | else |
| 5663 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5664 | } |
| 5665 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 5666 | 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] | 5667 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5668 | u8 mgmt_err = mgmt_status(status); |
| 5669 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5670 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5671 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5672 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5673 | |
| 5674 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5675 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, 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 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5679 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 5680 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5681 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 5682 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5683 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5684 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5685 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5686 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 5687 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5688 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5689 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 5690 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5691 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5692 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5693 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5694 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5695 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5696 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 5697 | { |
| 5698 | if (ltk->authenticated) |
| 5699 | return MGMT_LTK_AUTHENTICATED; |
| 5700 | |
| 5701 | return MGMT_LTK_UNAUTHENTICATED; |
| 5702 | } |
| 5703 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5704 | 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] | 5705 | { |
| 5706 | struct mgmt_ev_new_long_term_key ev; |
| 5707 | |
| 5708 | memset(&ev, 0, sizeof(ev)); |
| 5709 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 5710 | /* Devices using resolvable or non-resolvable random addresses |
| 5711 | * without providing an indentity resolving key don't require |
| 5712 | * to store long term keys. Their addresses will change the |
| 5713 | * next time around. |
| 5714 | * |
| 5715 | * Only when a remote device provides an identity address |
| 5716 | * make sure the long term key is stored. If the remote |
| 5717 | * identity is known, the long term keys are internally |
| 5718 | * mapped to the identity address. So allow static random |
| 5719 | * and public addresses here. |
| 5720 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5721 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5722 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5723 | ev.store_hint = 0x00; |
| 5724 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5725 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5726 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5727 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5728 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5729 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5730 | ev.key.enc_size = key->enc_size; |
| 5731 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 5732 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5733 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 5734 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5735 | ev.key.master = 1; |
| 5736 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5737 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 5738 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 5739 | 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] | 5740 | } |
| 5741 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5742 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 5743 | { |
| 5744 | struct mgmt_ev_new_irk ev; |
| 5745 | |
| 5746 | memset(&ev, 0, sizeof(ev)); |
| 5747 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 5748 | /* For identity resolving keys from devices that are already |
| 5749 | * using a public address or static random address, do not |
| 5750 | * ask for storing this key. The identity resolving key really |
| 5751 | * is only mandatory for devices using resovlable random |
| 5752 | * addresses. |
| 5753 | * |
| 5754 | * Storing all identity resolving keys has the downside that |
| 5755 | * they will be also loaded on next boot of they system. More |
| 5756 | * identity resolving keys, means more time during scanning is |
| 5757 | * needed to actually resolve these addresses. |
| 5758 | */ |
| 5759 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 5760 | ev.store_hint = 0x01; |
| 5761 | else |
| 5762 | ev.store_hint = 0x00; |
| 5763 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5764 | bacpy(&ev.rpa, &irk->rpa); |
| 5765 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 5766 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 5767 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 5768 | |
| 5769 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 5770 | } |
| 5771 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5772 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 5773 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5774 | { |
| 5775 | struct mgmt_ev_new_csrk ev; |
| 5776 | |
| 5777 | memset(&ev, 0, sizeof(ev)); |
| 5778 | |
| 5779 | /* Devices using resolvable or non-resolvable random addresses |
| 5780 | * without providing an indentity resolving key don't require |
| 5781 | * to store signature resolving keys. Their addresses will change |
| 5782 | * the next time around. |
| 5783 | * |
| 5784 | * Only when a remote device provides an identity address |
| 5785 | * make sure the signature resolving key is stored. So allow |
| 5786 | * static random and public addresses here. |
| 5787 | */ |
| 5788 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5789 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5790 | ev.store_hint = 0x00; |
| 5791 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5792 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5793 | |
| 5794 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 5795 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 5796 | ev.key.master = csrk->master; |
| 5797 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 5798 | |
| 5799 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 5800 | } |
| 5801 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5802 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 5803 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 5804 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5805 | { |
| 5806 | struct mgmt_ev_new_conn_param ev; |
| 5807 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 5808 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 5809 | return; |
| 5810 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5811 | memset(&ev, 0, sizeof(ev)); |
| 5812 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5813 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 5814 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5815 | ev.min_interval = cpu_to_le16(min_interval); |
| 5816 | ev.max_interval = cpu_to_le16(max_interval); |
| 5817 | ev.latency = cpu_to_le16(latency); |
| 5818 | ev.timeout = cpu_to_le16(timeout); |
| 5819 | |
| 5820 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 5821 | } |
| 5822 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 5823 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 5824 | u8 data_len) |
| 5825 | { |
| 5826 | eir[eir_len++] = sizeof(type) + data_len; |
| 5827 | eir[eir_len++] = type; |
| 5828 | memcpy(&eir[eir_len], data, data_len); |
| 5829 | eir_len += data_len; |
| 5830 | |
| 5831 | return eir_len; |
| 5832 | } |
| 5833 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5834 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5835 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 5836 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5837 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5838 | char buf[512]; |
| 5839 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 5840 | u16 eir_len = 0; |
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 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5843 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5844 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 5845 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 5846 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5847 | if (name_len > 0) |
| 5848 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5849 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5850 | |
| 5851 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 5852 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5853 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5854 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5855 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5856 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5857 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 5858 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5859 | } |
| 5860 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5861 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 5862 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 5863 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5864 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 5865 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5866 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5867 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5868 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5869 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5870 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5871 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5872 | |
| 5873 | *sk = cmd->sk; |
| 5874 | sock_hold(*sk); |
| 5875 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5876 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5877 | } |
| 5878 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5879 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5880 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5881 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5882 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 5883 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5884 | |
| 5885 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5886 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5887 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5888 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5889 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 5890 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5891 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5892 | |
| 5893 | mgmt_pending_remove(cmd); |
| 5894 | } |
| 5895 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5896 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5897 | u8 link_type, u8 addr_type, u8 reason, |
| 5898 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5899 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5900 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5901 | struct pending_cmd *power_off; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5902 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5903 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5904 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5905 | if (power_off) { |
| 5906 | struct mgmt_mode *cp = power_off->param; |
| 5907 | |
| 5908 | /* The connection is still in hci_conn_hash so test for 1 |
| 5909 | * instead of 0 to know if this is the last one. |
| 5910 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5911 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5912 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5913 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5914 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5915 | } |
| 5916 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5917 | if (!mgmt_connected) |
| 5918 | return; |
| 5919 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 5920 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 5921 | return; |
| 5922 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5923 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5924 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5925 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5926 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 5927 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5928 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5929 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5930 | |
| 5931 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 5932 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5933 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5934 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5935 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5936 | } |
| 5937 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5938 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5939 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5940 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5941 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 5942 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5943 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5944 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5945 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 5946 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 5947 | hdev); |
| 5948 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5949 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5950 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5951 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5952 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5953 | cp = cmd->param; |
| 5954 | |
| 5955 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 5956 | return; |
| 5957 | |
| 5958 | if (cp->addr.type != bdaddr_type) |
| 5959 | return; |
| 5960 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5961 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5962 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 5963 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5964 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 5965 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5966 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5967 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5968 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5969 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5970 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5971 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5972 | { |
| 5973 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5974 | struct pending_cmd *power_off; |
| 5975 | |
| 5976 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5977 | if (power_off) { |
| 5978 | struct mgmt_mode *cp = power_off->param; |
| 5979 | |
| 5980 | /* The connection is still in hci_conn_hash so test for 1 |
| 5981 | * instead of 0 to know if this is the last one. |
| 5982 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5983 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5984 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5985 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5986 | } |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5987 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5988 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 5989 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5990 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5991 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5992 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5993 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5994 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5995 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 5996 | 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] | 5997 | { |
| 5998 | struct mgmt_ev_pin_code_request ev; |
| 5999 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6000 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6001 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 6002 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6003 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6004 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6005 | } |
| 6006 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6007 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6008 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6009 | { |
| 6010 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6011 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6012 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6013 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6014 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6015 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6016 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6017 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6018 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6019 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6020 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 6021 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6022 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6023 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6024 | } |
| 6025 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6026 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6027 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6028 | { |
| 6029 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6030 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6031 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6032 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6033 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6034 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6035 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6036 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6037 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6038 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6039 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 6040 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6041 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6042 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6043 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6044 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6045 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6046 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6047 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6048 | { |
| 6049 | struct mgmt_ev_user_confirm_request ev; |
| 6050 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6051 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6052 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6053 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6054 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 6055 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6056 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6057 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6058 | 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] | 6059 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6060 | } |
| 6061 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6062 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 6063 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6064 | { |
| 6065 | struct mgmt_ev_user_passkey_request ev; |
| 6066 | |
| 6067 | BT_DBG("%s", hdev->name); |
| 6068 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6069 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6070 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6071 | |
| 6072 | 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] | 6073 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6074 | } |
| 6075 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 6076 | 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] | 6077 | u8 link_type, u8 addr_type, u8 status, |
| 6078 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6079 | { |
| 6080 | struct pending_cmd *cmd; |
| 6081 | struct mgmt_rp_user_confirm_reply rp; |
| 6082 | int err; |
| 6083 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6084 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6085 | if (!cmd) |
| 6086 | return -ENOENT; |
| 6087 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6088 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6089 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6090 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6091 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6092 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6093 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6094 | |
| 6095 | return err; |
| 6096 | } |
| 6097 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6098 | 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] | 6099 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6100 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6101 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6102 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6103 | } |
| 6104 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6105 | 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] | 6106 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6107 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6108 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6109 | status, |
| 6110 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6111 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6112 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6113 | 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] | 6114 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6115 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6116 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6117 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6118 | } |
| 6119 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6120 | 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] | 6121 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6122 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6123 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6124 | status, |
| 6125 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6126 | } |
| 6127 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 6128 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6129 | u8 link_type, u8 addr_type, u32 passkey, |
| 6130 | u8 entered) |
| 6131 | { |
| 6132 | struct mgmt_ev_passkey_notify ev; |
| 6133 | |
| 6134 | BT_DBG("%s", hdev->name); |
| 6135 | |
| 6136 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6137 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6138 | ev.passkey = __cpu_to_le32(passkey); |
| 6139 | ev.entered = entered; |
| 6140 | |
| 6141 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 6142 | } |
| 6143 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6144 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6145 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6146 | { |
| 6147 | struct mgmt_ev_auth_failed ev; |
| 6148 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 6149 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6150 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6151 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6152 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6153 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6154 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6155 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6156 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6157 | { |
| 6158 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6159 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6160 | |
| 6161 | if (status) { |
| 6162 | u8 mgmt_err = mgmt_status(status); |
| 6163 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6164 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6165 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6166 | } |
| 6167 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6168 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 6169 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 6170 | &hdev->dev_flags); |
| 6171 | else |
| 6172 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 6173 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6174 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6175 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6176 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6177 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6178 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6179 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6180 | |
| 6181 | if (match.sk) |
| 6182 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6183 | } |
| 6184 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6185 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6186 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6187 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6188 | struct hci_cp_write_eir cp; |
| 6189 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 6190 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6191 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6192 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 6193 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 6194 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6195 | memset(&cp, 0, sizeof(cp)); |
| 6196 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6197 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6198 | } |
| 6199 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6200 | 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] | 6201 | { |
| 6202 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6203 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6204 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6205 | |
| 6206 | if (status) { |
| 6207 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6208 | |
| 6209 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6210 | &hdev->dev_flags)) { |
| 6211 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6212 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6213 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6214 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6215 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 6216 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6217 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6218 | } |
| 6219 | |
| 6220 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6221 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6222 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6223 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 6224 | if (!changed) |
| 6225 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 6226 | &hdev->dev_flags); |
| 6227 | else |
| 6228 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6229 | } |
| 6230 | |
| 6231 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 6232 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6233 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6234 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6235 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 6236 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6237 | sock_put(match.sk); |
| 6238 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6239 | hci_req_init(&req, hdev); |
| 6240 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6241 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 6242 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 6243 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6244 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6245 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6246 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6247 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6248 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6249 | |
| 6250 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6251 | } |
| 6252 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6253 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 6254 | { |
| 6255 | struct cmd_lookup match = { NULL, hdev }; |
| 6256 | bool changed = false; |
| 6257 | |
| 6258 | if (status) { |
| 6259 | u8 mgmt_err = mgmt_status(status); |
| 6260 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6261 | if (enable) { |
| 6262 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 6263 | &hdev->dev_flags)) |
| 6264 | new_settings(hdev, NULL); |
| 6265 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6266 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6267 | |
| 6268 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6269 | cmd_status_rsp, &mgmt_err); |
| 6270 | return; |
| 6271 | } |
| 6272 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6273 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6274 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6275 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6276 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6277 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6278 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6279 | |
| 6280 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6281 | settings_rsp, &match); |
| 6282 | |
| 6283 | if (changed) |
| 6284 | new_settings(hdev, match.sk); |
| 6285 | |
| 6286 | if (match.sk) |
| 6287 | sock_put(match.sk); |
| 6288 | } |
| 6289 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6290 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6291 | { |
| 6292 | struct cmd_lookup *match = data; |
| 6293 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6294 | if (match->sk == NULL) { |
| 6295 | match->sk = cmd->sk; |
| 6296 | sock_hold(match->sk); |
| 6297 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6298 | } |
| 6299 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6300 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 6301 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6302 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6303 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6304 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6305 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 6306 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 6307 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6308 | |
| 6309 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6310 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 6311 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6312 | |
| 6313 | if (match.sk) |
| 6314 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6315 | } |
| 6316 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6317 | 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] | 6318 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6319 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6320 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6321 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6322 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6323 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6324 | |
| 6325 | memset(&ev, 0, sizeof(ev)); |
| 6326 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6327 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6328 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6329 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6330 | if (!cmd) { |
| 6331 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6332 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6333 | /* If this is a HCI command related to powering on the |
| 6334 | * HCI dev don't send any mgmt signals. |
| 6335 | */ |
| 6336 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6337 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6338 | } |
| 6339 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6340 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 6341 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6342 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6343 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6344 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 6345 | u8 *randomizer192, u8 *hash256, |
| 6346 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6347 | { |
| 6348 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6349 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6350 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6351 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6352 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6353 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6354 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6355 | |
| 6356 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6357 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 6358 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6359 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6360 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 6361 | hash256 && randomizer256) { |
| 6362 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6363 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6364 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 6365 | memcpy(rp.randomizer192, randomizer192, |
| 6366 | sizeof(rp.randomizer192)); |
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.hash256, hash256, sizeof(rp.hash256)); |
| 6369 | memcpy(rp.randomizer256, randomizer256, |
| 6370 | sizeof(rp.randomizer256)); |
| 6371 | |
| 6372 | cmd_complete(cmd->sk, hdev->id, |
| 6373 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6374 | &rp, sizeof(rp)); |
| 6375 | } else { |
| 6376 | struct mgmt_rp_read_local_oob_data rp; |
| 6377 | |
| 6378 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 6379 | memcpy(rp.randomizer, randomizer192, |
| 6380 | sizeof(rp.randomizer)); |
| 6381 | |
| 6382 | cmd_complete(cmd->sk, hdev->id, |
| 6383 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6384 | &rp, sizeof(rp)); |
| 6385 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6386 | } |
| 6387 | |
| 6388 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6389 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6390 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6391 | 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] | 6392 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 6393 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6394 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6395 | char buf[512]; |
| 6396 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6397 | struct smp_irk *irk; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6398 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6399 | |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6400 | if (!hci_discovery_active(hdev)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6401 | return; |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6402 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6403 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 6404 | * are for the potential CoD field. |
| 6405 | */ |
| 6406 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6407 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 6408 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6409 | memset(buf, 0, sizeof(buf)); |
| 6410 | |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6411 | irk = hci_get_irk(hdev, bdaddr, addr_type); |
| 6412 | if (irk) { |
| 6413 | bacpy(&ev->addr.bdaddr, &irk->bdaddr); |
| 6414 | ev->addr.type = link_to_bdaddr(link_type, irk->addr_type); |
| 6415 | } else { |
| 6416 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 6417 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
| 6418 | } |
| 6419 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6420 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 6421 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6422 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6423 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6424 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6425 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6426 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 6427 | 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] | 6428 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6429 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6430 | if (scan_rsp_len > 0) |
| 6431 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
| 6432 | |
| 6433 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 6434 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 6435 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6436 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6437 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6438 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6439 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6440 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6441 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6442 | struct mgmt_ev_device_found *ev; |
| 6443 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 6444 | u16 eir_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 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6447 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6448 | memset(buf, 0, sizeof(buf)); |
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 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6451 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6452 | ev->rssi = rssi; |
| 6453 | |
| 6454 | 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] | 6455 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6456 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6457 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6458 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6459 | 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] | 6460 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6461 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6462 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6463 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6464 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6465 | struct pending_cmd *cmd; |
| 6466 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 6467 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 6468 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6469 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6470 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6471 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6472 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6473 | |
| 6474 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 6475 | u8 type = hdev->discovery.type; |
| 6476 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6477 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 6478 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6479 | mgmt_pending_remove(cmd); |
| 6480 | } |
| 6481 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6482 | memset(&ev, 0, sizeof(ev)); |
| 6483 | ev.type = hdev->discovery.type; |
| 6484 | ev.discovering = discovering; |
| 6485 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6486 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6487 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6488 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6489 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 6490 | { |
| 6491 | BT_DBG("%s status %u", hdev->name, status); |
| 6492 | |
| 6493 | /* Clear the advertising mgmt setting if we failed to re-enable it */ |
| 6494 | if (status) { |
| 6495 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6496 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6497 | } |
| 6498 | } |
| 6499 | |
| 6500 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 6501 | { |
| 6502 | struct hci_request req; |
| 6503 | |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 6504 | if (hci_conn_num(hdev, LE_LINK) > 0) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6505 | return; |
| 6506 | |
| 6507 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6508 | return; |
| 6509 | |
| 6510 | hci_req_init(&req, hdev); |
| 6511 | enable_advertising(&req); |
| 6512 | |
| 6513 | /* If this fails we have no option but to let user space know |
| 6514 | * that we've disabled advertising. |
| 6515 | */ |
| 6516 | if (hci_req_run(&req, adv_enable_complete) < 0) { |
| 6517 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6518 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6519 | } |
| 6520 | } |