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 | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static const u16 mgmt_events[] = { |
| 94 | MGMT_EV_CONTROLLER_ERROR, |
| 95 | MGMT_EV_INDEX_ADDED, |
| 96 | MGMT_EV_INDEX_REMOVED, |
| 97 | MGMT_EV_NEW_SETTINGS, |
| 98 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 99 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 100 | MGMT_EV_NEW_LINK_KEY, |
| 101 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 102 | MGMT_EV_DEVICE_CONNECTED, |
| 103 | MGMT_EV_DEVICE_DISCONNECTED, |
| 104 | MGMT_EV_CONNECT_FAILED, |
| 105 | MGMT_EV_PIN_CODE_REQUEST, |
| 106 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 107 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 108 | MGMT_EV_AUTH_FAILED, |
| 109 | MGMT_EV_DEVICE_FOUND, |
| 110 | MGMT_EV_DISCOVERING, |
| 111 | MGMT_EV_DEVICE_BLOCKED, |
| 112 | MGMT_EV_DEVICE_UNBLOCKED, |
| 113 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 114 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 115 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 116 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 117 | MGMT_EV_DEVICE_ADDED, |
| 118 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 119 | MGMT_EV_NEW_CONN_PARAM, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 120 | }; |
| 121 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 122 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 123 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 124 | #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \ |
| 125 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 126 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 127 | struct pending_cmd { |
| 128 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 129 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 130 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 131 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 132 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 133 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 134 | }; |
| 135 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 136 | /* HCI to MGMT error code conversion table */ |
| 137 | static u8 mgmt_status_table[] = { |
| 138 | MGMT_STATUS_SUCCESS, |
| 139 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 140 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 141 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 142 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 143 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 144 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 145 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 146 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 147 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 148 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 149 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 150 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 151 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 152 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 153 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 154 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 155 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 156 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 157 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 158 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 159 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 160 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 161 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 162 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 163 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 164 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 165 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 166 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 167 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 168 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 169 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 170 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 171 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 172 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 173 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 174 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 175 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 176 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 177 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 178 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 179 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 180 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 181 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 182 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 183 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 184 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 185 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 186 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 187 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 188 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 189 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 190 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 191 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 192 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 193 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 194 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 195 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 196 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 197 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 198 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 199 | }; |
| 200 | |
| 201 | static u8 mgmt_status(u8 hci_status) |
| 202 | { |
| 203 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 204 | return mgmt_status_table[hci_status]; |
| 205 | |
| 206 | return MGMT_STATUS_FAILED; |
| 207 | } |
| 208 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 209 | 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] | 210 | { |
| 211 | struct sk_buff *skb; |
| 212 | struct mgmt_hdr *hdr; |
| 213 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 214 | int err; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 215 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 216 | 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] | 217 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 218 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 219 | if (!skb) |
| 220 | return -ENOMEM; |
| 221 | |
| 222 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 223 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 224 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 225 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 226 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 227 | |
| 228 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 229 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 230 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 231 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 232 | err = sock_queue_rcv_skb(sk, skb); |
| 233 | if (err < 0) |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 234 | kfree_skb(skb); |
| 235 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 236 | return err; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 239 | 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] | 240 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 241 | { |
| 242 | struct sk_buff *skb; |
| 243 | struct mgmt_hdr *hdr; |
| 244 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 245 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 246 | |
| 247 | BT_DBG("sock %p", sk); |
| 248 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 249 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 250 | if (!skb) |
| 251 | return -ENOMEM; |
| 252 | |
| 253 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 254 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 255 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 256 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 257 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 258 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 259 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 260 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 261 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 262 | |
| 263 | if (rp) |
| 264 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 265 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 266 | err = sock_queue_rcv_skb(sk, skb); |
| 267 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 268 | kfree_skb(skb); |
| 269 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 270 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 273 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 274 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 275 | { |
| 276 | struct mgmt_rp_read_version rp; |
| 277 | |
| 278 | BT_DBG("sock %p", sk); |
| 279 | |
| 280 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 281 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 282 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 283 | 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] | 284 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 287 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 288 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 289 | { |
| 290 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 291 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 292 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 293 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 294 | size_t rp_size; |
| 295 | int i, err; |
| 296 | |
| 297 | BT_DBG("sock %p", sk); |
| 298 | |
| 299 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 300 | |
| 301 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 302 | if (!rp) |
| 303 | return -ENOMEM; |
| 304 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 305 | rp->num_commands = cpu_to_le16(num_commands); |
| 306 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 307 | |
| 308 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 309 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 310 | |
| 311 | for (i = 0; i < num_events; i++, opcode++) |
| 312 | put_unaligned_le16(mgmt_events[i], opcode); |
| 313 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 314 | 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] | 315 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 316 | kfree(rp); |
| 317 | |
| 318 | return err; |
| 319 | } |
| 320 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 321 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 322 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 323 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 324 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 325 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 326 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 327 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 328 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 329 | |
| 330 | BT_DBG("sock %p", sk); |
| 331 | |
| 332 | read_lock(&hci_dev_list_lock); |
| 333 | |
| 334 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 335 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 336 | if (d->dev_type == HCI_BREDR) |
| 337 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 338 | } |
| 339 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 340 | rp_len = sizeof(*rp) + (2 * count); |
| 341 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 342 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 343 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 344 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 345 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 346 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 347 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 348 | list_for_each_entry(d, &hci_dev_list, list) { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 349 | if (test_bit(HCI_SETUP, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 350 | continue; |
| 351 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 352 | if (test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 353 | continue; |
| 354 | |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 355 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 356 | continue; |
| 357 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 358 | if (d->dev_type == HCI_BREDR) { |
| 359 | rp->index[count++] = cpu_to_le16(d->id); |
| 360 | BT_DBG("Added hci%u", d->id); |
| 361 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 362 | } |
| 363 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 364 | rp->num_controllers = cpu_to_le16(count); |
| 365 | rp_len = sizeof(*rp) + (2 * count); |
| 366 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 367 | read_unlock(&hci_dev_list_lock); |
| 368 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 369 | 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] | 370 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 371 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 372 | kfree(rp); |
| 373 | |
| 374 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 375 | } |
| 376 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 377 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 378 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 379 | u32 settings = 0; |
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 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 382 | settings |= MGMT_SETTING_PAIRABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 383 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 384 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 385 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 386 | settings |= MGMT_SETTING_CONNECTABLE; |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 387 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 388 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 389 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 390 | settings |= MGMT_SETTING_BREDR; |
| 391 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 392 | |
| 393 | if (lmp_ssp_capable(hdev)) { |
| 394 | settings |= MGMT_SETTING_SSP; |
| 395 | settings |= MGMT_SETTING_HS; |
| 396 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 397 | |
Marcel Holtmann | 5afeac14 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 398 | if (lmp_sc_capable(hdev) || |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 399 | test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 400 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 401 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 402 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 403 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 404 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 405 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 406 | settings |= MGMT_SETTING_PRIVACY; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 407 | } |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 408 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 409 | return settings; |
| 410 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 411 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 412 | static u32 get_current_settings(struct hci_dev *hdev) |
| 413 | { |
| 414 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 415 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 416 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 417 | settings |= MGMT_SETTING_POWERED; |
| 418 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 419 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 420 | settings |= MGMT_SETTING_CONNECTABLE; |
| 421 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 422 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 423 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 424 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 425 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 426 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 427 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 428 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 429 | settings |= MGMT_SETTING_PAIRABLE; |
| 430 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 431 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 432 | settings |= MGMT_SETTING_BREDR; |
| 433 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 434 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 435 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 436 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 437 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 438 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 439 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 440 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 441 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 442 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 443 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 444 | settings |= MGMT_SETTING_HS; |
| 445 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 446 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 447 | settings |= MGMT_SETTING_ADVERTISING; |
| 448 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 449 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 450 | settings |= MGMT_SETTING_SECURE_CONN; |
| 451 | |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 452 | if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 453 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 454 | |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 455 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 456 | settings |= MGMT_SETTING_PRIVACY; |
| 457 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 458 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 459 | } |
| 460 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 461 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 462 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 463 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 464 | { |
| 465 | u8 *ptr = data, *uuids_start = NULL; |
| 466 | struct bt_uuid *uuid; |
| 467 | |
| 468 | if (len < 4) |
| 469 | return ptr; |
| 470 | |
| 471 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 472 | u16 uuid16; |
| 473 | |
| 474 | if (uuid->size != 16) |
| 475 | continue; |
| 476 | |
| 477 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 478 | if (uuid16 < 0x1100) |
| 479 | continue; |
| 480 | |
| 481 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 482 | continue; |
| 483 | |
| 484 | if (!uuids_start) { |
| 485 | uuids_start = ptr; |
| 486 | uuids_start[0] = 1; |
| 487 | uuids_start[1] = EIR_UUID16_ALL; |
| 488 | ptr += 2; |
| 489 | } |
| 490 | |
| 491 | /* Stop if not enough space to put next UUID */ |
| 492 | if ((ptr - data) + sizeof(u16) > len) { |
| 493 | uuids_start[1] = EIR_UUID16_SOME; |
| 494 | break; |
| 495 | } |
| 496 | |
| 497 | *ptr++ = (uuid16 & 0x00ff); |
| 498 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 499 | uuids_start[0] += sizeof(uuid16); |
| 500 | } |
| 501 | |
| 502 | return ptr; |
| 503 | } |
| 504 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 505 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 506 | { |
| 507 | u8 *ptr = data, *uuids_start = NULL; |
| 508 | struct bt_uuid *uuid; |
| 509 | |
| 510 | if (len < 6) |
| 511 | return ptr; |
| 512 | |
| 513 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 514 | if (uuid->size != 32) |
| 515 | continue; |
| 516 | |
| 517 | if (!uuids_start) { |
| 518 | uuids_start = ptr; |
| 519 | uuids_start[0] = 1; |
| 520 | uuids_start[1] = EIR_UUID32_ALL; |
| 521 | ptr += 2; |
| 522 | } |
| 523 | |
| 524 | /* Stop if not enough space to put next UUID */ |
| 525 | if ((ptr - data) + sizeof(u32) > len) { |
| 526 | uuids_start[1] = EIR_UUID32_SOME; |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 531 | ptr += sizeof(u32); |
| 532 | uuids_start[0] += sizeof(u32); |
| 533 | } |
| 534 | |
| 535 | return ptr; |
| 536 | } |
| 537 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 538 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 539 | { |
| 540 | u8 *ptr = data, *uuids_start = NULL; |
| 541 | struct bt_uuid *uuid; |
| 542 | |
| 543 | if (len < 18) |
| 544 | return ptr; |
| 545 | |
| 546 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 547 | if (uuid->size != 128) |
| 548 | continue; |
| 549 | |
| 550 | if (!uuids_start) { |
| 551 | uuids_start = ptr; |
| 552 | uuids_start[0] = 1; |
| 553 | uuids_start[1] = EIR_UUID128_ALL; |
| 554 | ptr += 2; |
| 555 | } |
| 556 | |
| 557 | /* Stop if not enough space to put next UUID */ |
| 558 | if ((ptr - data) + 16 > len) { |
| 559 | uuids_start[1] = EIR_UUID128_SOME; |
| 560 | break; |
| 561 | } |
| 562 | |
| 563 | memcpy(ptr, uuid->uuid, 16); |
| 564 | ptr += 16; |
| 565 | uuids_start[0] += 16; |
| 566 | } |
| 567 | |
| 568 | return ptr; |
| 569 | } |
| 570 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 571 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 572 | { |
| 573 | struct pending_cmd *cmd; |
| 574 | |
| 575 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 576 | if (cmd->opcode == opcode) |
| 577 | return cmd; |
| 578 | } |
| 579 | |
| 580 | return NULL; |
| 581 | } |
| 582 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 583 | static struct pending_cmd *mgmt_pending_find_data(u16 opcode, |
| 584 | struct hci_dev *hdev, |
| 585 | const void *data) |
| 586 | { |
| 587 | struct pending_cmd *cmd; |
| 588 | |
| 589 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 590 | if (cmd->user_data != data) |
| 591 | continue; |
| 592 | if (cmd->opcode == opcode) |
| 593 | return cmd; |
| 594 | } |
| 595 | |
| 596 | return NULL; |
| 597 | } |
| 598 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 599 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 600 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 601 | u8 ad_len = 0; |
| 602 | size_t name_len; |
| 603 | |
| 604 | name_len = strlen(hdev->dev_name); |
| 605 | if (name_len > 0) { |
| 606 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 607 | |
| 608 | if (name_len > max_len) { |
| 609 | name_len = max_len; |
| 610 | ptr[1] = EIR_NAME_SHORT; |
| 611 | } else |
| 612 | ptr[1] = EIR_NAME_COMPLETE; |
| 613 | |
| 614 | ptr[0] = name_len + 1; |
| 615 | |
| 616 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 617 | |
| 618 | ad_len += (name_len + 2); |
| 619 | ptr += (name_len + 2); |
| 620 | } |
| 621 | |
| 622 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | static void update_scan_rsp_data(struct hci_request *req) |
| 626 | { |
| 627 | struct hci_dev *hdev = req->hdev; |
| 628 | struct hci_cp_le_set_scan_rsp_data cp; |
| 629 | u8 len; |
| 630 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 631 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 632 | return; |
| 633 | |
| 634 | memset(&cp, 0, sizeof(cp)); |
| 635 | |
| 636 | len = create_scan_rsp_data(hdev, cp.data); |
| 637 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 638 | if (hdev->scan_rsp_data_len == len && |
| 639 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 640 | return; |
| 641 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 642 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 643 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 644 | |
| 645 | cp.length = len; |
| 646 | |
| 647 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 648 | } |
| 649 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 650 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 651 | { |
| 652 | struct pending_cmd *cmd; |
| 653 | |
| 654 | /* If there's a pending mgmt command the flags will not yet have |
| 655 | * their final values, so check for this first. |
| 656 | */ |
| 657 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 658 | if (cmd) { |
| 659 | struct mgmt_mode *cp = cmd->param; |
| 660 | if (cp->val == 0x01) |
| 661 | return LE_AD_GENERAL; |
| 662 | else if (cp->val == 0x02) |
| 663 | return LE_AD_LIMITED; |
| 664 | } else { |
| 665 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 666 | return LE_AD_LIMITED; |
| 667 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 668 | return LE_AD_GENERAL; |
| 669 | } |
| 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 674 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 675 | { |
| 676 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 677 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 678 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 679 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 680 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 681 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 682 | |
| 683 | if (flags) { |
| 684 | BT_DBG("adv flags 0x%02x", flags); |
| 685 | |
| 686 | ptr[0] = 2; |
| 687 | ptr[1] = EIR_FLAGS; |
| 688 | ptr[2] = flags; |
| 689 | |
| 690 | ad_len += 3; |
| 691 | ptr += 3; |
| 692 | } |
| 693 | |
| 694 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 695 | ptr[0] = 2; |
| 696 | ptr[1] = EIR_TX_POWER; |
| 697 | ptr[2] = (u8) hdev->adv_tx_power; |
| 698 | |
| 699 | ad_len += 3; |
| 700 | ptr += 3; |
| 701 | } |
| 702 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 703 | return ad_len; |
| 704 | } |
| 705 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 706 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 707 | { |
| 708 | struct hci_dev *hdev = req->hdev; |
| 709 | struct hci_cp_le_set_adv_data cp; |
| 710 | u8 len; |
| 711 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 712 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 713 | return; |
| 714 | |
| 715 | memset(&cp, 0, sizeof(cp)); |
| 716 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 717 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 718 | |
| 719 | if (hdev->adv_data_len == len && |
| 720 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 721 | return; |
| 722 | |
| 723 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 724 | hdev->adv_data_len = len; |
| 725 | |
| 726 | cp.length = len; |
| 727 | |
| 728 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 729 | } |
| 730 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 731 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 732 | { |
| 733 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 734 | size_t name_len; |
| 735 | |
| 736 | name_len = strlen(hdev->dev_name); |
| 737 | |
| 738 | if (name_len > 0) { |
| 739 | /* EIR Data type */ |
| 740 | if (name_len > 48) { |
| 741 | name_len = 48; |
| 742 | ptr[1] = EIR_NAME_SHORT; |
| 743 | } else |
| 744 | ptr[1] = EIR_NAME_COMPLETE; |
| 745 | |
| 746 | /* EIR Data length */ |
| 747 | ptr[0] = name_len + 1; |
| 748 | |
| 749 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 750 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 751 | ptr += (name_len + 2); |
| 752 | } |
| 753 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 754 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 755 | ptr[0] = 2; |
| 756 | ptr[1] = EIR_TX_POWER; |
| 757 | ptr[2] = (u8) hdev->inq_tx_power; |
| 758 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 759 | ptr += 3; |
| 760 | } |
| 761 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 762 | if (hdev->devid_source > 0) { |
| 763 | ptr[0] = 9; |
| 764 | ptr[1] = EIR_DEVICE_ID; |
| 765 | |
| 766 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 767 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 768 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 769 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 770 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 771 | ptr += 10; |
| 772 | } |
| 773 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 774 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 775 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 776 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 777 | } |
| 778 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 779 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 780 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 781 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 782 | struct hci_cp_write_eir cp; |
| 783 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 784 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 785 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 786 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 787 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 788 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 789 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 790 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 791 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 792 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 793 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 794 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 795 | |
| 796 | memset(&cp, 0, sizeof(cp)); |
| 797 | |
| 798 | create_eir(hdev, cp.data); |
| 799 | |
| 800 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 801 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 802 | |
| 803 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 804 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 805 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 806 | } |
| 807 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 808 | static u8 get_service_classes(struct hci_dev *hdev) |
| 809 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 810 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 811 | u8 val = 0; |
| 812 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 813 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 814 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 815 | |
| 816 | return val; |
| 817 | } |
| 818 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 819 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 820 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 821 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 822 | u8 cod[3]; |
| 823 | |
| 824 | BT_DBG("%s", hdev->name); |
| 825 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 826 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 827 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 828 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 829 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 830 | return; |
| 831 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 832 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 833 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 834 | |
| 835 | cod[0] = hdev->minor_class; |
| 836 | cod[1] = hdev->major_class; |
| 837 | cod[2] = get_service_classes(hdev); |
| 838 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 839 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 840 | cod[1] |= 0x20; |
| 841 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 842 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 843 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 844 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 845 | 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] | 846 | } |
| 847 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 848 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 849 | { |
| 850 | struct pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 851 | |
| 852 | /* If there's a pending mgmt command the flag will not yet have |
| 853 | * it's final value, so check for this first. |
| 854 | */ |
| 855 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 856 | if (cmd) { |
| 857 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 858 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 859 | } |
| 860 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 861 | return test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | static void enable_advertising(struct hci_request *req) |
| 865 | { |
| 866 | struct hci_dev *hdev = req->hdev; |
| 867 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 868 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 869 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 870 | |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 871 | /* Clear the HCI_ADVERTISING bit temporarily so that the |
| 872 | * hci_update_random_address knows that it's safe to go ahead |
| 873 | * and write a new random address. The flag will be set back on |
| 874 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 875 | */ |
| 876 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 877 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 878 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 879 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 880 | /* Set require_privacy to true only when non-connectable |
| 881 | * advertising is used. In that case it is fine to use a |
| 882 | * non-resolvable private address. |
| 883 | */ |
| 884 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 885 | return; |
| 886 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 887 | memset(&cp, 0, sizeof(cp)); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 888 | cp.min_interval = cpu_to_le16(0x0800); |
| 889 | cp.max_interval = cpu_to_le16(0x0800); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 890 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 891 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 892 | cp.channel_map = hdev->le_adv_channel_map; |
| 893 | |
| 894 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 895 | |
| 896 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 897 | } |
| 898 | |
| 899 | static void disable_advertising(struct hci_request *req) |
| 900 | { |
| 901 | u8 enable = 0x00; |
| 902 | |
| 903 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 904 | } |
| 905 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 906 | static void service_cache_off(struct work_struct *work) |
| 907 | { |
| 908 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 909 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 910 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 911 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 912 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 913 | return; |
| 914 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 915 | hci_req_init(&req, hdev); |
| 916 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 917 | hci_dev_lock(hdev); |
| 918 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 919 | update_eir(&req); |
| 920 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 921 | |
| 922 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 923 | |
| 924 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 925 | } |
| 926 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 927 | static void rpa_expired(struct work_struct *work) |
| 928 | { |
| 929 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 930 | rpa_expired.work); |
| 931 | struct hci_request req; |
| 932 | |
| 933 | BT_DBG(""); |
| 934 | |
| 935 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 936 | |
| 937 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags) || |
| 938 | hci_conn_num(hdev, LE_LINK) > 0) |
| 939 | return; |
| 940 | |
| 941 | /* The generation of a new RPA and programming it into the |
| 942 | * controller happens in the enable_advertising() function. |
| 943 | */ |
| 944 | |
| 945 | hci_req_init(&req, hdev); |
| 946 | |
| 947 | disable_advertising(&req); |
| 948 | enable_advertising(&req); |
| 949 | |
| 950 | hci_req_run(&req, NULL); |
| 951 | } |
| 952 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 953 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 954 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 955 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 956 | return; |
| 957 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 958 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 959 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 960 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 961 | /* Non-mgmt controlled devices get this bit set |
| 962 | * implicitly so that pairing works for them, however |
| 963 | * for mgmt we require user-space to explicitly enable |
| 964 | * it |
| 965 | */ |
| 966 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 967 | } |
| 968 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 969 | 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] | 970 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 971 | { |
| 972 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 973 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 974 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 975 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 976 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 977 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 978 | memset(&rp, 0, sizeof(rp)); |
| 979 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 980 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 981 | |
| 982 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 983 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 984 | |
| 985 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 986 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 987 | |
| 988 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 989 | |
| 990 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 991 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 992 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 993 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 994 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 995 | 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] | 996 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 1000 | { |
| 1001 | sock_put(cmd->sk); |
| 1002 | kfree(cmd->param); |
| 1003 | kfree(cmd); |
| 1004 | } |
| 1005 | |
| 1006 | 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] | 1007 | struct hci_dev *hdev, void *data, |
| 1008 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1009 | { |
| 1010 | struct pending_cmd *cmd; |
| 1011 | |
Johan Hedberg | fca2001 | 2014-06-28 17:54:05 +0300 | [diff] [blame] | 1012 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1013 | if (!cmd) |
| 1014 | return NULL; |
| 1015 | |
| 1016 | cmd->opcode = opcode; |
| 1017 | cmd->index = hdev->id; |
| 1018 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 1019 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1020 | if (!cmd->param) { |
| 1021 | kfree(cmd); |
| 1022 | return NULL; |
| 1023 | } |
| 1024 | |
| 1025 | if (data) |
| 1026 | memcpy(cmd->param, data, len); |
| 1027 | |
| 1028 | cmd->sk = sk; |
| 1029 | sock_hold(sk); |
| 1030 | |
| 1031 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1032 | |
| 1033 | return cmd; |
| 1034 | } |
| 1035 | |
| 1036 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1037 | void (*cb)(struct pending_cmd *cmd, |
| 1038 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1039 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1040 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1041 | struct pending_cmd *cmd, *tmp; |
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 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1044 | if (opcode > 0 && cmd->opcode != opcode) |
| 1045 | continue; |
| 1046 | |
| 1047 | cb(cmd, data); |
| 1048 | } |
| 1049 | } |
| 1050 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1051 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 1052 | { |
| 1053 | list_del(&cmd->list); |
| 1054 | mgmt_pending_free(cmd); |
| 1055 | } |
| 1056 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1057 | 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] | 1058 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1059 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1060 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1061 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1062 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1063 | } |
| 1064 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1065 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status) |
| 1066 | { |
| 1067 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1068 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1069 | if (hci_conn_count(hdev) == 0) { |
| 1070 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1071 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1072 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1073 | } |
| 1074 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1075 | static void hci_stop_discovery(struct hci_request *req) |
| 1076 | { |
| 1077 | struct hci_dev *hdev = req->hdev; |
| 1078 | struct hci_cp_remote_name_req_cancel cp; |
| 1079 | struct inquiry_entry *e; |
| 1080 | |
| 1081 | switch (hdev->discovery.state) { |
| 1082 | case DISCOVERY_FINDING: |
| 1083 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 1084 | hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 1085 | } else { |
| 1086 | cancel_delayed_work(&hdev->le_scan_disable); |
| 1087 | hci_req_add_le_scan_disable(req); |
| 1088 | } |
| 1089 | |
| 1090 | break; |
| 1091 | |
| 1092 | case DISCOVERY_RESOLVING: |
| 1093 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 1094 | NAME_PENDING); |
| 1095 | if (!e) |
| 1096 | return; |
| 1097 | |
| 1098 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 1099 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 1100 | &cp); |
| 1101 | |
| 1102 | break; |
| 1103 | |
| 1104 | default: |
| 1105 | /* Passive scanning */ |
| 1106 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 1107 | hci_req_add_le_scan_disable(req); |
| 1108 | break; |
| 1109 | } |
| 1110 | } |
| 1111 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1112 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1113 | { |
| 1114 | struct hci_request req; |
| 1115 | struct hci_conn *conn; |
| 1116 | |
| 1117 | hci_req_init(&req, hdev); |
| 1118 | |
| 1119 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1120 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1121 | u8 scan = 0x00; |
| 1122 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1123 | } |
| 1124 | |
| 1125 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 1126 | disable_advertising(&req); |
| 1127 | |
Johan Hedberg | f8680f1 | 2014-06-10 14:05:59 +0300 | [diff] [blame] | 1128 | hci_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1129 | |
| 1130 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1131 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1132 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1133 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1134 | switch (conn->state) { |
| 1135 | case BT_CONNECTED: |
| 1136 | case BT_CONFIG: |
| 1137 | dc.handle = cpu_to_le16(conn->handle); |
| 1138 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1139 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1140 | break; |
| 1141 | case BT_CONNECT: |
| 1142 | if (conn->type == LE_LINK) |
| 1143 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1144 | 0, NULL); |
| 1145 | else if (conn->type == ACL_LINK) |
| 1146 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1147 | 6, &conn->dst); |
| 1148 | break; |
| 1149 | case BT_CONNECT2: |
| 1150 | bacpy(&rej.bdaddr, &conn->dst); |
| 1151 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1152 | if (conn->type == ACL_LINK) |
| 1153 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1154 | sizeof(rej), &rej); |
| 1155 | else if (conn->type == SCO_LINK) |
| 1156 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1157 | sizeof(rej), &rej); |
| 1158 | break; |
| 1159 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | return hci_req_run(&req, clean_up_hci_complete); |
| 1163 | } |
| 1164 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1165 | 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] | 1166 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1167 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1168 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1169 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1170 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1171 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1172 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1173 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1174 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1175 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1176 | MGMT_STATUS_INVALID_PARAMS); |
| 1177 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1178 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1179 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1180 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 1181 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1182 | MGMT_STATUS_BUSY); |
| 1183 | goto failed; |
| 1184 | } |
| 1185 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1186 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1187 | cancel_delayed_work(&hdev->power_off); |
| 1188 | |
| 1189 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1190 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1191 | data, len); |
| 1192 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1193 | goto failed; |
| 1194 | } |
| 1195 | } |
| 1196 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1197 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1198 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1199 | goto failed; |
| 1200 | } |
| 1201 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1202 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1203 | if (!cmd) { |
| 1204 | err = -ENOMEM; |
| 1205 | goto failed; |
| 1206 | } |
| 1207 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1208 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1209 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1210 | err = 0; |
| 1211 | } else { |
| 1212 | /* Disconnect connections, stop scans, etc */ |
| 1213 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1214 | if (!err) |
| 1215 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1216 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1217 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1218 | /* ENODATA means there were no HCI commands queued */ |
| 1219 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1220 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1221 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1222 | err = 0; |
| 1223 | } |
| 1224 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1225 | |
| 1226 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1227 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1228 | return err; |
| 1229 | } |
| 1230 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1231 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 1232 | struct sock *skip_sk) |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1233 | { |
| 1234 | struct sk_buff *skb; |
| 1235 | struct mgmt_hdr *hdr; |
| 1236 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 1237 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1238 | if (!skb) |
| 1239 | return -ENOMEM; |
| 1240 | |
| 1241 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 1242 | hdr->opcode = cpu_to_le16(event); |
| 1243 | if (hdev) |
| 1244 | hdr->index = cpu_to_le16(hdev->id); |
| 1245 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1246 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1247 | hdr->len = cpu_to_le16(data_len); |
| 1248 | |
| 1249 | if (data) |
| 1250 | memcpy(skb_put(skb, data_len), data, data_len); |
| 1251 | |
Marcel Holtmann | 97e0bde | 2012-02-22 13:49:28 +0100 | [diff] [blame] | 1252 | /* Time stamp */ |
| 1253 | __net_timestamp(skb); |
| 1254 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1255 | hci_send_to_control(skb, skip_sk); |
| 1256 | kfree_skb(skb); |
| 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1262 | { |
| 1263 | __le32 ev; |
| 1264 | |
| 1265 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1266 | |
| 1267 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1268 | } |
| 1269 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1270 | struct cmd_lookup { |
| 1271 | struct sock *sk; |
| 1272 | struct hci_dev *hdev; |
| 1273 | u8 mgmt_status; |
| 1274 | }; |
| 1275 | |
| 1276 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1277 | { |
| 1278 | struct cmd_lookup *match = data; |
| 1279 | |
| 1280 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1281 | |
| 1282 | list_del(&cmd->list); |
| 1283 | |
| 1284 | if (match->sk == NULL) { |
| 1285 | match->sk = cmd->sk; |
| 1286 | sock_hold(match->sk); |
| 1287 | } |
| 1288 | |
| 1289 | mgmt_pending_free(cmd); |
| 1290 | } |
| 1291 | |
| 1292 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1293 | { |
| 1294 | u8 *status = data; |
| 1295 | |
| 1296 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1297 | mgmt_pending_remove(cmd); |
| 1298 | } |
| 1299 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1300 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1301 | { |
| 1302 | if (!lmp_bredr_capable(hdev)) |
| 1303 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1304 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1305 | return MGMT_STATUS_REJECTED; |
| 1306 | else |
| 1307 | return MGMT_STATUS_SUCCESS; |
| 1308 | } |
| 1309 | |
| 1310 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1311 | { |
| 1312 | if (!lmp_le_capable(hdev)) |
| 1313 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1314 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1315 | return MGMT_STATUS_REJECTED; |
| 1316 | else |
| 1317 | return MGMT_STATUS_SUCCESS; |
| 1318 | } |
| 1319 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1320 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1321 | { |
| 1322 | struct pending_cmd *cmd; |
| 1323 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1324 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1325 | bool changed; |
| 1326 | |
| 1327 | BT_DBG("status 0x%02x", status); |
| 1328 | |
| 1329 | hci_dev_lock(hdev); |
| 1330 | |
| 1331 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1332 | if (!cmd) |
| 1333 | goto unlock; |
| 1334 | |
| 1335 | if (status) { |
| 1336 | u8 mgmt_err = mgmt_status(status); |
| 1337 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1338 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1339 | goto remove_cmd; |
| 1340 | } |
| 1341 | |
| 1342 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1343 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1344 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1345 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1346 | |
| 1347 | if (hdev->discov_timeout > 0) { |
| 1348 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1349 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1350 | to); |
| 1351 | } |
| 1352 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1353 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1354 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1355 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1356 | |
| 1357 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1358 | |
| 1359 | if (changed) |
| 1360 | new_settings(hdev, cmd->sk); |
| 1361 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1362 | /* When the discoverable mode gets changed, make sure |
| 1363 | * that class of device has the limited discoverable |
| 1364 | * bit correctly set. |
| 1365 | */ |
| 1366 | hci_req_init(&req, hdev); |
| 1367 | update_class(&req); |
| 1368 | hci_req_run(&req, NULL); |
| 1369 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1370 | remove_cmd: |
| 1371 | mgmt_pending_remove(cmd); |
| 1372 | |
| 1373 | unlock: |
| 1374 | hci_dev_unlock(hdev); |
| 1375 | } |
| 1376 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1377 | 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] | 1378 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1379 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1380 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1381 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1382 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1383 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1384 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1385 | int err; |
| 1386 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1387 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1388 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1389 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1390 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1391 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1392 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1393 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1394 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1395 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1396 | MGMT_STATUS_INVALID_PARAMS); |
| 1397 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1398 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1399 | |
| 1400 | /* Disabling discoverable requires that no timeout is set, |
| 1401 | * and enabling limited discoverable requires a timeout. |
| 1402 | */ |
| 1403 | if ((cp->val == 0x00 && timeout > 0) || |
| 1404 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1405 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1406 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1407 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1408 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1409 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1410 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1411 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1412 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1413 | goto failed; |
| 1414 | } |
| 1415 | |
| 1416 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1417 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1418 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1419 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1420 | goto failed; |
| 1421 | } |
| 1422 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1423 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1424 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1425 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1426 | goto failed; |
| 1427 | } |
| 1428 | |
| 1429 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1430 | bool changed = false; |
| 1431 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1432 | /* Setting limited discoverable when powered off is |
| 1433 | * not a valid operation since it requires a timeout |
| 1434 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1435 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1436 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1437 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1438 | changed = true; |
| 1439 | } |
| 1440 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1441 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1442 | if (err < 0) |
| 1443 | goto failed; |
| 1444 | |
| 1445 | if (changed) |
| 1446 | err = new_settings(hdev, sk); |
| 1447 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1448 | goto failed; |
| 1449 | } |
| 1450 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1451 | /* If the current mode is the same, then just update the timeout |
| 1452 | * value with the new value. And if only the timeout gets updated, |
| 1453 | * then no need for any HCI transactions. |
| 1454 | */ |
| 1455 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1456 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1457 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1458 | cancel_delayed_work(&hdev->discov_off); |
| 1459 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1460 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1461 | if (cp->val && hdev->discov_timeout > 0) { |
| 1462 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1463 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1464 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1465 | } |
| 1466 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1467 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1468 | goto failed; |
| 1469 | } |
| 1470 | |
| 1471 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1472 | if (!cmd) { |
| 1473 | err = -ENOMEM; |
| 1474 | goto failed; |
| 1475 | } |
| 1476 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1477 | /* Cancel any potential discoverable timeout that might be |
| 1478 | * still active and store new timeout value. The arming of |
| 1479 | * the timeout happens in the complete handler. |
| 1480 | */ |
| 1481 | cancel_delayed_work(&hdev->discov_off); |
| 1482 | hdev->discov_timeout = timeout; |
| 1483 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1484 | /* Limited discoverable mode */ |
| 1485 | if (cp->val == 0x02) |
| 1486 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1487 | else |
| 1488 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1489 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1490 | hci_req_init(&req, hdev); |
| 1491 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1492 | /* The procedure for LE-only controllers is much simpler - just |
| 1493 | * update the advertising data. |
| 1494 | */ |
| 1495 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1496 | goto update_ad; |
| 1497 | |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1498 | scan = SCAN_PAGE; |
| 1499 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1500 | if (cp->val) { |
| 1501 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1502 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1503 | if (cp->val == 0x02) { |
| 1504 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1505 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1506 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1507 | hci_cp.iac_lap[1] = 0x8b; |
| 1508 | hci_cp.iac_lap[2] = 0x9e; |
| 1509 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1510 | hci_cp.iac_lap[4] = 0x8b; |
| 1511 | hci_cp.iac_lap[5] = 0x9e; |
| 1512 | } else { |
| 1513 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1514 | hci_cp.num_iac = 1; |
| 1515 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1516 | hci_cp.iac_lap[1] = 0x8b; |
| 1517 | hci_cp.iac_lap[2] = 0x9e; |
| 1518 | } |
| 1519 | |
| 1520 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1521 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1522 | |
| 1523 | scan |= SCAN_INQUIRY; |
| 1524 | } else { |
| 1525 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1526 | } |
| 1527 | |
| 1528 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1529 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1530 | update_ad: |
| 1531 | update_adv_data(&req); |
| 1532 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1533 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1534 | if (err < 0) |
| 1535 | mgmt_pending_remove(cmd); |
| 1536 | |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1537 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1538 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1539 | return err; |
| 1540 | } |
| 1541 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1542 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1543 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1544 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1545 | struct hci_cp_write_page_scan_activity acp; |
| 1546 | u8 type; |
| 1547 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1548 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1549 | return; |
| 1550 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1551 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1552 | return; |
| 1553 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1554 | if (enable) { |
| 1555 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1556 | |
| 1557 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1558 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1559 | } else { |
| 1560 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1561 | |
| 1562 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1563 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1564 | } |
| 1565 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1566 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1567 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1568 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1569 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1570 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1571 | sizeof(acp), &acp); |
| 1572 | |
| 1573 | if (hdev->page_scan_type != type) |
| 1574 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1575 | } |
| 1576 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1577 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1578 | { |
| 1579 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1580 | struct mgmt_mode *cp; |
| 1581 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1582 | |
| 1583 | BT_DBG("status 0x%02x", status); |
| 1584 | |
| 1585 | hci_dev_lock(hdev); |
| 1586 | |
| 1587 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1588 | if (!cmd) |
| 1589 | goto unlock; |
| 1590 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1591 | if (status) { |
| 1592 | u8 mgmt_err = mgmt_status(status); |
| 1593 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1594 | goto remove_cmd; |
| 1595 | } |
| 1596 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1597 | cp = cmd->param; |
| 1598 | if (cp->val) |
| 1599 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1600 | else |
| 1601 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1602 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1603 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1604 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1605 | if (changed) |
| 1606 | new_settings(hdev, cmd->sk); |
| 1607 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1608 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1609 | mgmt_pending_remove(cmd); |
| 1610 | |
| 1611 | unlock: |
| 1612 | hci_dev_unlock(hdev); |
| 1613 | } |
| 1614 | |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1615 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1616 | struct sock *sk, u8 val) |
| 1617 | { |
| 1618 | bool changed = false; |
| 1619 | int err; |
| 1620 | |
| 1621 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1622 | changed = true; |
| 1623 | |
| 1624 | if (val) { |
| 1625 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1626 | } else { |
| 1627 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1628 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1629 | } |
| 1630 | |
| 1631 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1632 | if (err < 0) |
| 1633 | return err; |
| 1634 | |
| 1635 | if (changed) |
| 1636 | return new_settings(hdev, sk); |
| 1637 | |
| 1638 | return 0; |
| 1639 | } |
| 1640 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1641 | 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] | 1642 | u16 len) |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1643 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1644 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1645 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1646 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1647 | u8 scan; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1648 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1649 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1650 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1651 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1652 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1653 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1654 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1655 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1656 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1657 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1658 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1659 | MGMT_STATUS_INVALID_PARAMS); |
| 1660 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1661 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1662 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1663 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1664 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1665 | goto failed; |
| 1666 | } |
| 1667 | |
| 1668 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1669 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1670 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1671 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1672 | goto failed; |
| 1673 | } |
| 1674 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1675 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1676 | if (!cmd) { |
| 1677 | err = -ENOMEM; |
| 1678 | goto failed; |
| 1679 | } |
| 1680 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1681 | hci_req_init(&req, hdev); |
| 1682 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1683 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1684 | * by-product of disabling connectable, we need to update the |
| 1685 | * advertising flags. |
| 1686 | */ |
| 1687 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1688 | if (!cp->val) { |
| 1689 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1690 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1691 | } |
| 1692 | update_adv_data(&req); |
| 1693 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1694 | if (cp->val) { |
| 1695 | scan = SCAN_PAGE; |
| 1696 | } else { |
| 1697 | scan = 0; |
| 1698 | |
| 1699 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1700 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1701 | cancel_delayed_work(&hdev->discov_off); |
| 1702 | } |
| 1703 | |
| 1704 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1705 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1706 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1707 | /* If we're going from non-connectable to connectable or |
| 1708 | * vice-versa when fast connectable is enabled ensure that fast |
| 1709 | * connectable gets disabled. write_fast_connectable won't do |
| 1710 | * anything if the page scan parameters are already what they |
| 1711 | * should be. |
| 1712 | */ |
| 1713 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1714 | write_fast_connectable(&req, false); |
| 1715 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1716 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) && |
| 1717 | hci_conn_num(hdev, LE_LINK) == 0) { |
| 1718 | disable_advertising(&req); |
| 1719 | enable_advertising(&req); |
| 1720 | } |
| 1721 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1722 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1723 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1724 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1725 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1726 | err = set_connectable_update_settings(hdev, sk, |
| 1727 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1728 | goto failed; |
| 1729 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1730 | |
| 1731 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1732 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1733 | return err; |
| 1734 | } |
| 1735 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1736 | 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] | 1737 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1738 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1739 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1740 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1741 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1742 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1743 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1744 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1745 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1746 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1747 | MGMT_STATUS_INVALID_PARAMS); |
| 1748 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1749 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1750 | |
| 1751 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1752 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1753 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1754 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1755 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1756 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1757 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1758 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1759 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1760 | if (changed) |
| 1761 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1762 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1763 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1764 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1765 | return err; |
| 1766 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1767 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1768 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1769 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1770 | { |
| 1771 | struct mgmt_mode *cp = data; |
| 1772 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1773 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1774 | int err; |
| 1775 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1776 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1777 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1778 | status = mgmt_bredr_support(hdev); |
| 1779 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1780 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1781 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1782 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1783 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1784 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1785 | MGMT_STATUS_INVALID_PARAMS); |
| 1786 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1787 | hci_dev_lock(hdev); |
| 1788 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1789 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1790 | bool changed = false; |
| 1791 | |
| 1792 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1793 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1794 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1795 | changed = true; |
| 1796 | } |
| 1797 | |
| 1798 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1799 | if (err < 0) |
| 1800 | goto failed; |
| 1801 | |
| 1802 | if (changed) |
| 1803 | err = new_settings(hdev, sk); |
| 1804 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1805 | goto failed; |
| 1806 | } |
| 1807 | |
| 1808 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1809 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1810 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1811 | goto failed; |
| 1812 | } |
| 1813 | |
| 1814 | val = !!cp->val; |
| 1815 | |
| 1816 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1817 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1818 | goto failed; |
| 1819 | } |
| 1820 | |
| 1821 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1822 | if (!cmd) { |
| 1823 | err = -ENOMEM; |
| 1824 | goto failed; |
| 1825 | } |
| 1826 | |
| 1827 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1828 | if (err < 0) { |
| 1829 | mgmt_pending_remove(cmd); |
| 1830 | goto failed; |
| 1831 | } |
| 1832 | |
| 1833 | failed: |
| 1834 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1835 | return err; |
| 1836 | } |
| 1837 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1838 | 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] | 1839 | { |
| 1840 | struct mgmt_mode *cp = data; |
| 1841 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1842 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1843 | int err; |
| 1844 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1845 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1846 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1847 | status = mgmt_bredr_support(hdev); |
| 1848 | if (status) |
| 1849 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 1850 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1851 | if (!lmp_ssp_capable(hdev)) |
| 1852 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1853 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1854 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1855 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1856 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1857 | MGMT_STATUS_INVALID_PARAMS); |
| 1858 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1859 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1860 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1861 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1862 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1863 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1864 | if (cp->val) { |
| 1865 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 1866 | &hdev->dev_flags); |
| 1867 | } else { |
| 1868 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 1869 | &hdev->dev_flags); |
| 1870 | if (!changed) |
| 1871 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 1872 | &hdev->dev_flags); |
| 1873 | else |
| 1874 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1878 | if (err < 0) |
| 1879 | goto failed; |
| 1880 | |
| 1881 | if (changed) |
| 1882 | err = new_settings(hdev, sk); |
| 1883 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1884 | goto failed; |
| 1885 | } |
| 1886 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1887 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 1888 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 1889 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1890 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1891 | goto failed; |
| 1892 | } |
| 1893 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1894 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1895 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1896 | goto failed; |
| 1897 | } |
| 1898 | |
| 1899 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1900 | if (!cmd) { |
| 1901 | err = -ENOMEM; |
| 1902 | goto failed; |
| 1903 | } |
| 1904 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1905 | if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 1906 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1907 | sizeof(cp->val), &cp->val); |
| 1908 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1909 | 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] | 1910 | if (err < 0) { |
| 1911 | mgmt_pending_remove(cmd); |
| 1912 | goto failed; |
| 1913 | } |
| 1914 | |
| 1915 | failed: |
| 1916 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1917 | return err; |
| 1918 | } |
| 1919 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1920 | 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] | 1921 | { |
| 1922 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1923 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1924 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1925 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1926 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1927 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1928 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1929 | status = mgmt_bredr_support(hdev); |
| 1930 | if (status) |
| 1931 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1932 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1933 | if (!lmp_ssp_capable(hdev)) |
| 1934 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1935 | MGMT_STATUS_NOT_SUPPORTED); |
| 1936 | |
| 1937 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 1938 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1939 | MGMT_STATUS_REJECTED); |
| 1940 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1941 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1942 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1943 | MGMT_STATUS_INVALID_PARAMS); |
| 1944 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1945 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1946 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1947 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1948 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1949 | } else { |
| 1950 | if (hdev_is_powered(hdev)) { |
| 1951 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1952 | MGMT_STATUS_REJECTED); |
| 1953 | goto unlock; |
| 1954 | } |
| 1955 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1956 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1957 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1958 | |
| 1959 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1960 | if (err < 0) |
| 1961 | goto unlock; |
| 1962 | |
| 1963 | if (changed) |
| 1964 | err = new_settings(hdev, sk); |
| 1965 | |
| 1966 | unlock: |
| 1967 | hci_dev_unlock(hdev); |
| 1968 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1969 | } |
| 1970 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1971 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 1972 | { |
| 1973 | struct cmd_lookup match = { NULL, hdev }; |
| 1974 | |
| 1975 | if (status) { |
| 1976 | u8 mgmt_err = mgmt_status(status); |
| 1977 | |
| 1978 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1979 | &mgmt_err); |
| 1980 | return; |
| 1981 | } |
| 1982 | |
| 1983 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1984 | |
| 1985 | new_settings(hdev, match.sk); |
| 1986 | |
| 1987 | if (match.sk) |
| 1988 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1989 | |
| 1990 | /* Make sure the controller has a good default for |
| 1991 | * advertising data. Restrict the update to when LE |
| 1992 | * has actually been enabled. During power on, the |
| 1993 | * update in powered_update_hci will take care of it. |
| 1994 | */ |
| 1995 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 1996 | struct hci_request req; |
| 1997 | |
| 1998 | hci_dev_lock(hdev); |
| 1999 | |
| 2000 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 2001 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 2002 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2003 | hci_req_run(&req, NULL); |
| 2004 | |
| 2005 | hci_dev_unlock(hdev); |
| 2006 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2007 | } |
| 2008 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2009 | 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] | 2010 | { |
| 2011 | struct mgmt_mode *cp = data; |
| 2012 | struct hci_cp_write_le_host_supported hci_cp; |
| 2013 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2014 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2015 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2016 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2017 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2018 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2019 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2020 | if (!lmp_le_capable(hdev)) |
| 2021 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2022 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2023 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2024 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2025 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2026 | MGMT_STATUS_INVALID_PARAMS); |
| 2027 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2028 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2029 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2030 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2031 | MGMT_STATUS_REJECTED); |
| 2032 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2033 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2034 | |
| 2035 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2036 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2037 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2038 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2039 | bool changed = false; |
| 2040 | |
| 2041 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2042 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 2043 | changed = true; |
| 2044 | } |
| 2045 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 2046 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 2047 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2048 | changed = true; |
| 2049 | } |
| 2050 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2051 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2052 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2053 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2054 | |
| 2055 | if (changed) |
| 2056 | err = new_settings(hdev, sk); |
| 2057 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2058 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2059 | } |
| 2060 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2061 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2062 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2063 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2064 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2065 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2069 | if (!cmd) { |
| 2070 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2071 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2072 | } |
| 2073 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2074 | hci_req_init(&req, hdev); |
| 2075 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2076 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2077 | |
| 2078 | if (val) { |
| 2079 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2080 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2081 | } else { |
| 2082 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 2083 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2084 | } |
| 2085 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2086 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2087 | &hci_cp); |
| 2088 | |
| 2089 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2090 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2091 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2092 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2093 | unlock: |
| 2094 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2095 | return err; |
| 2096 | } |
| 2097 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2098 | /* This is a helper function to test for pending mgmt commands that can |
| 2099 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2100 | * mgmt command at a time since otherwise we cannot easily track what |
| 2101 | * the current values are, will be, and based on that calculate if a new |
| 2102 | * HCI command needs to be sent and if yes with what value. |
| 2103 | */ |
| 2104 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2105 | { |
| 2106 | struct pending_cmd *cmd; |
| 2107 | |
| 2108 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2109 | switch (cmd->opcode) { |
| 2110 | case MGMT_OP_ADD_UUID: |
| 2111 | case MGMT_OP_REMOVE_UUID: |
| 2112 | case MGMT_OP_SET_DEV_CLASS: |
| 2113 | case MGMT_OP_SET_POWERED: |
| 2114 | return true; |
| 2115 | } |
| 2116 | } |
| 2117 | |
| 2118 | return false; |
| 2119 | } |
| 2120 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2121 | static const u8 bluetooth_base_uuid[] = { |
| 2122 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2123 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2124 | }; |
| 2125 | |
| 2126 | static u8 get_uuid_size(const u8 *uuid) |
| 2127 | { |
| 2128 | u32 val; |
| 2129 | |
| 2130 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2131 | return 128; |
| 2132 | |
| 2133 | val = get_unaligned_le32(&uuid[12]); |
| 2134 | if (val > 0xffff) |
| 2135 | return 32; |
| 2136 | |
| 2137 | return 16; |
| 2138 | } |
| 2139 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2140 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2141 | { |
| 2142 | struct pending_cmd *cmd; |
| 2143 | |
| 2144 | hci_dev_lock(hdev); |
| 2145 | |
| 2146 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2147 | if (!cmd) |
| 2148 | goto unlock; |
| 2149 | |
| 2150 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 2151 | hdev->dev_class, 3); |
| 2152 | |
| 2153 | mgmt_pending_remove(cmd); |
| 2154 | |
| 2155 | unlock: |
| 2156 | hci_dev_unlock(hdev); |
| 2157 | } |
| 2158 | |
| 2159 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2160 | { |
| 2161 | BT_DBG("status 0x%02x", status); |
| 2162 | |
| 2163 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2164 | } |
| 2165 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2166 | 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] | 2167 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2168 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2169 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2170 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2171 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2172 | int err; |
| 2173 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2174 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2175 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2176 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2177 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2178 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2179 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2180 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2181 | goto failed; |
| 2182 | } |
| 2183 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2184 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2185 | if (!uuid) { |
| 2186 | err = -ENOMEM; |
| 2187 | goto failed; |
| 2188 | } |
| 2189 | |
| 2190 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2191 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2192 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2193 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2194 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2195 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2196 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2197 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2198 | update_class(&req); |
| 2199 | update_eir(&req); |
| 2200 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2201 | err = hci_req_run(&req, add_uuid_complete); |
| 2202 | if (err < 0) { |
| 2203 | if (err != -ENODATA) |
| 2204 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2205 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2206 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2207 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2208 | goto failed; |
| 2209 | } |
| 2210 | |
| 2211 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2212 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2213 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2214 | goto failed; |
| 2215 | } |
| 2216 | |
| 2217 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2218 | |
| 2219 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2220 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2221 | return err; |
| 2222 | } |
| 2223 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2224 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2225 | { |
| 2226 | if (!hdev_is_powered(hdev)) |
| 2227 | return false; |
| 2228 | |
| 2229 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2230 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2231 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2232 | return true; |
| 2233 | } |
| 2234 | |
| 2235 | return false; |
| 2236 | } |
| 2237 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2238 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2239 | { |
| 2240 | BT_DBG("status 0x%02x", status); |
| 2241 | |
| 2242 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2243 | } |
| 2244 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2245 | 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] | 2246 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2247 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2248 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2249 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2250 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2251 | 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] | 2252 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2253 | int err, found; |
| 2254 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2255 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2256 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2257 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2258 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2259 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2260 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2261 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2262 | goto unlock; |
| 2263 | } |
| 2264 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2265 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2266 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2267 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2268 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2269 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2270 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2271 | goto unlock; |
| 2272 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2273 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2274 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | found = 0; |
| 2278 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2279 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2280 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2281 | continue; |
| 2282 | |
| 2283 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2284 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2285 | found++; |
| 2286 | } |
| 2287 | |
| 2288 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2289 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2290 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2291 | goto unlock; |
| 2292 | } |
| 2293 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2294 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2295 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2296 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2297 | update_class(&req); |
| 2298 | update_eir(&req); |
| 2299 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2300 | err = hci_req_run(&req, remove_uuid_complete); |
| 2301 | if (err < 0) { |
| 2302 | if (err != -ENODATA) |
| 2303 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2304 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2305 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2306 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2307 | goto unlock; |
| 2308 | } |
| 2309 | |
| 2310 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2311 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2312 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2313 | goto unlock; |
| 2314 | } |
| 2315 | |
| 2316 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2317 | |
| 2318 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2319 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2320 | return err; |
| 2321 | } |
| 2322 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2323 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2324 | { |
| 2325 | BT_DBG("status 0x%02x", status); |
| 2326 | |
| 2327 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2328 | } |
| 2329 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2330 | 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] | 2331 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2332 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2333 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2334 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2335 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2336 | int err; |
| 2337 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2338 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2339 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2340 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2341 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2342 | MGMT_STATUS_NOT_SUPPORTED); |
| 2343 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2344 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2345 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2346 | if (pending_eir_or_class(hdev)) { |
| 2347 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2348 | MGMT_STATUS_BUSY); |
| 2349 | goto unlock; |
| 2350 | } |
| 2351 | |
| 2352 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2353 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2354 | MGMT_STATUS_INVALID_PARAMS); |
| 2355 | goto unlock; |
| 2356 | } |
| 2357 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2358 | hdev->major_class = cp->major; |
| 2359 | hdev->minor_class = cp->minor; |
| 2360 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2361 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2362 | 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] | 2363 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2364 | goto unlock; |
| 2365 | } |
| 2366 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2367 | hci_req_init(&req, hdev); |
| 2368 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2369 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2370 | hci_dev_unlock(hdev); |
| 2371 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2372 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2373 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2374 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2375 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2376 | update_class(&req); |
| 2377 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2378 | err = hci_req_run(&req, set_class_complete); |
| 2379 | if (err < 0) { |
| 2380 | if (err != -ENODATA) |
| 2381 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2382 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2383 | 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] | 2384 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2385 | goto unlock; |
| 2386 | } |
| 2387 | |
| 2388 | 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] | 2389 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2390 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2391 | goto unlock; |
| 2392 | } |
| 2393 | |
| 2394 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2395 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2396 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2397 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2398 | return err; |
| 2399 | } |
| 2400 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2401 | 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] | 2402 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2403 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2404 | struct mgmt_cp_load_link_keys *cp = data; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2405 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2406 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2407 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2408 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2409 | BT_DBG("request for %s", hdev->name); |
| 2410 | |
| 2411 | if (!lmp_bredr_capable(hdev)) |
| 2412 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2413 | MGMT_STATUS_NOT_SUPPORTED); |
| 2414 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2415 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2416 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2417 | expected_len = sizeof(*cp) + key_count * |
| 2418 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2419 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2420 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2421 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2422 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2423 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2424 | } |
| 2425 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2426 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2427 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2428 | MGMT_STATUS_INVALID_PARAMS); |
| 2429 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2430 | 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] | 2431 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2432 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2433 | for (i = 0; i < key_count; i++) { |
| 2434 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2435 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2436 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2437 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2438 | MGMT_STATUS_INVALID_PARAMS); |
| 2439 | } |
| 2440 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2441 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2442 | |
| 2443 | hci_link_keys_clear(hdev); |
| 2444 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2445 | if (cp->debug_keys) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2446 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 2447 | &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2448 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2449 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 2450 | &hdev->dev_flags); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2451 | |
| 2452 | if (changed) |
| 2453 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2454 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2455 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2456 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2457 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2458 | /* Always ignore debug keys and require a new pairing if |
| 2459 | * the user wants to use them. |
| 2460 | */ |
| 2461 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2462 | continue; |
| 2463 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2464 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2465 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2466 | } |
| 2467 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2468 | 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] | 2469 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2470 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2471 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2472 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2473 | } |
| 2474 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2475 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2476 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2477 | { |
| 2478 | struct mgmt_ev_device_unpaired ev; |
| 2479 | |
| 2480 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2481 | ev.addr.type = addr_type; |
| 2482 | |
| 2483 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2484 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2485 | } |
| 2486 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2487 | 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] | 2488 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2489 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2490 | struct mgmt_cp_unpair_device *cp = data; |
| 2491 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2492 | struct hci_cp_disconnect dc; |
| 2493 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2494 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2495 | int err; |
| 2496 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2497 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2498 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2499 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2500 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2501 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2502 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2503 | MGMT_STATUS_INVALID_PARAMS, |
| 2504 | &rp, sizeof(rp)); |
| 2505 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2506 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2507 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2508 | MGMT_STATUS_INVALID_PARAMS, |
| 2509 | &rp, sizeof(rp)); |
| 2510 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2511 | hci_dev_lock(hdev); |
| 2512 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2513 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2514 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2515 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2516 | goto unlock; |
| 2517 | } |
| 2518 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2519 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2520 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2521 | } else { |
| 2522 | u8 addr_type; |
| 2523 | |
| 2524 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2525 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2526 | else |
| 2527 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2528 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2529 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2530 | |
Andre Guedes | a9b0a04 | 2014-02-26 20:21:52 -0300 | [diff] [blame] | 2531 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2532 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2533 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2534 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2535 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2536 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2537 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2538 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2539 | goto unlock; |
| 2540 | } |
| 2541 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2542 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2543 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2544 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2545 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2546 | else |
| 2547 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2548 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2549 | } else { |
| 2550 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2551 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2552 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2553 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2554 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2555 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2556 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2557 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2558 | } |
| 2559 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2560 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2561 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2562 | if (!cmd) { |
| 2563 | err = -ENOMEM; |
| 2564 | goto unlock; |
| 2565 | } |
| 2566 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2567 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2568 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2569 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2570 | if (err < 0) |
| 2571 | mgmt_pending_remove(cmd); |
| 2572 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2573 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2574 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2575 | return err; |
| 2576 | } |
| 2577 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2578 | 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] | 2579 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2580 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2581 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2582 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2583 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2584 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2585 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2586 | int err; |
| 2587 | |
| 2588 | BT_DBG(""); |
| 2589 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2590 | memset(&rp, 0, sizeof(rp)); |
| 2591 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2592 | rp.addr.type = cp->addr.type; |
| 2593 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2594 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2595 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2596 | MGMT_STATUS_INVALID_PARAMS, |
| 2597 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2598 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2599 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2600 | |
| 2601 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2602 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2603 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2604 | goto failed; |
| 2605 | } |
| 2606 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2607 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2608 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2609 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2610 | goto failed; |
| 2611 | } |
| 2612 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2613 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2614 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2615 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2616 | else |
| 2617 | 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] | 2618 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2619 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2620 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2621 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2622 | goto failed; |
| 2623 | } |
| 2624 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2625 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2626 | if (!cmd) { |
| 2627 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2628 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2629 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2630 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2631 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2632 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2633 | |
| 2634 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2635 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2636 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2637 | |
| 2638 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2639 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2640 | return err; |
| 2641 | } |
| 2642 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2643 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2644 | { |
| 2645 | switch (link_type) { |
| 2646 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2647 | switch (addr_type) { |
| 2648 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2649 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2650 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2651 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2652 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2653 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2654 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2655 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2656 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2657 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2658 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2659 | } |
| 2660 | } |
| 2661 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2662 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2663 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2664 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2665 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2666 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2667 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2668 | int err; |
| 2669 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2670 | |
| 2671 | BT_DBG(""); |
| 2672 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2673 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2674 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2675 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2676 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2677 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2678 | goto unlock; |
| 2679 | } |
| 2680 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2681 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2682 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2683 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2684 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2685 | } |
| 2686 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2687 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2688 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2689 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2690 | err = -ENOMEM; |
| 2691 | goto unlock; |
| 2692 | } |
| 2693 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2694 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2695 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2696 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2697 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2698 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2699 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2700 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2701 | continue; |
| 2702 | i++; |
| 2703 | } |
| 2704 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2705 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2706 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2707 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2708 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2709 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2710 | 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] | 2711 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2712 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2713 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2714 | |
| 2715 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2716 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2717 | return err; |
| 2718 | } |
| 2719 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2720 | 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] | 2721 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2722 | { |
| 2723 | struct pending_cmd *cmd; |
| 2724 | int err; |
| 2725 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2726 | 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] | 2727 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2728 | if (!cmd) |
| 2729 | return -ENOMEM; |
| 2730 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2731 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2732 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2733 | if (err < 0) |
| 2734 | mgmt_pending_remove(cmd); |
| 2735 | |
| 2736 | return err; |
| 2737 | } |
| 2738 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2739 | 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] | 2740 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2741 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2742 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2743 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2744 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2745 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2746 | int err; |
| 2747 | |
| 2748 | BT_DBG(""); |
| 2749 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2750 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2751 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2752 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2753 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2754 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2755 | goto failed; |
| 2756 | } |
| 2757 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2758 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2759 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2760 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2761 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2762 | goto failed; |
| 2763 | } |
| 2764 | |
| 2765 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2766 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2767 | |
| 2768 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2769 | |
| 2770 | BT_ERR("PIN code is not 16 bytes long"); |
| 2771 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2772 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2773 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2774 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2775 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2776 | |
| 2777 | goto failed; |
| 2778 | } |
| 2779 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2780 | 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] | 2781 | if (!cmd) { |
| 2782 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2783 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2784 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2785 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2786 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2787 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2788 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2789 | |
| 2790 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2791 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2792 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2793 | |
| 2794 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2795 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2796 | return err; |
| 2797 | } |
| 2798 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2799 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2800 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2801 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2802 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2803 | |
| 2804 | BT_DBG(""); |
| 2805 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2806 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
| 2807 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2808 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
| 2809 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2810 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2811 | |
| 2812 | hdev->io_capability = cp->io_capability; |
| 2813 | |
| 2814 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2815 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2816 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2817 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2818 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2819 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2820 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2821 | } |
| 2822 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2823 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2824 | { |
| 2825 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2826 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2827 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2828 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2829 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2830 | continue; |
| 2831 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2832 | if (cmd->user_data != conn) |
| 2833 | continue; |
| 2834 | |
| 2835 | return cmd; |
| 2836 | } |
| 2837 | |
| 2838 | return NULL; |
| 2839 | } |
| 2840 | |
| 2841 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 2842 | { |
| 2843 | struct mgmt_rp_pair_device rp; |
| 2844 | struct hci_conn *conn = cmd->user_data; |
| 2845 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2846 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2847 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2848 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 2849 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2850 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2851 | |
| 2852 | /* So we don't get further callbacks for this connection */ |
| 2853 | conn->connect_cfm_cb = NULL; |
| 2854 | conn->security_cfm_cb = NULL; |
| 2855 | conn->disconn_cfm_cb = NULL; |
| 2856 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2857 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2858 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2859 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2860 | } |
| 2861 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2862 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2863 | { |
| 2864 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 2865 | struct pending_cmd *cmd; |
| 2866 | |
| 2867 | cmd = find_pairing(conn); |
| 2868 | if (cmd) |
| 2869 | pairing_complete(cmd, status); |
| 2870 | } |
| 2871 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2872 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2873 | { |
| 2874 | struct pending_cmd *cmd; |
| 2875 | |
| 2876 | BT_DBG("status %u", status); |
| 2877 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2878 | cmd = find_pairing(conn); |
| 2879 | if (!cmd) |
| 2880 | BT_DBG("Unable to find a pending command"); |
| 2881 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 2882 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2883 | } |
| 2884 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2885 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2886 | { |
| 2887 | struct pending_cmd *cmd; |
| 2888 | |
| 2889 | BT_DBG("status %u", status); |
| 2890 | |
| 2891 | if (!status) |
| 2892 | return; |
| 2893 | |
| 2894 | cmd = find_pairing(conn); |
| 2895 | if (!cmd) |
| 2896 | BT_DBG("Unable to find a pending command"); |
| 2897 | else |
| 2898 | pairing_complete(cmd, mgmt_status(status)); |
| 2899 | } |
| 2900 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2901 | 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] | 2902 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2903 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2904 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2905 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2906 | struct pending_cmd *cmd; |
| 2907 | u8 sec_level, auth_type; |
| 2908 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2909 | int err; |
| 2910 | |
| 2911 | BT_DBG(""); |
| 2912 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2913 | memset(&rp, 0, sizeof(rp)); |
| 2914 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2915 | rp.addr.type = cp->addr.type; |
| 2916 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2917 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2918 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2919 | MGMT_STATUS_INVALID_PARAMS, |
| 2920 | &rp, sizeof(rp)); |
| 2921 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2922 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
| 2923 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2924 | MGMT_STATUS_INVALID_PARAMS, |
| 2925 | &rp, sizeof(rp)); |
| 2926 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2927 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2928 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2929 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2930 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2931 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2932 | goto unlock; |
| 2933 | } |
| 2934 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2935 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2936 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2937 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2938 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2939 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 2940 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2941 | } else { |
| 2942 | u8 addr_type; |
| 2943 | |
| 2944 | /* Convert from L2CAP channel address type to HCI address type |
| 2945 | */ |
| 2946 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2947 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2948 | else |
| 2949 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2950 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2951 | /* When pairing a new device, it is expected to remember |
| 2952 | * this device for future connections. Adding the connection |
| 2953 | * parameter information ahead of time allows tracking |
| 2954 | * of the slave preferred values and will speed up any |
| 2955 | * further connection establishment. |
| 2956 | * |
| 2957 | * If connection parameters already exist, then they |
| 2958 | * will be kept and this function does nothing. |
| 2959 | */ |
| 2960 | hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 2961 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2962 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2963 | sec_level, auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2964 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2965 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2966 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2967 | int status; |
| 2968 | |
| 2969 | if (PTR_ERR(conn) == -EBUSY) |
| 2970 | status = MGMT_STATUS_BUSY; |
| 2971 | else |
| 2972 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2973 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2974 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2975 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2976 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2977 | goto unlock; |
| 2978 | } |
| 2979 | |
| 2980 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2981 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2982 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2983 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2984 | goto unlock; |
| 2985 | } |
| 2986 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2987 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2988 | if (!cmd) { |
| 2989 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2990 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2991 | goto unlock; |
| 2992 | } |
| 2993 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2994 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2995 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2996 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2997 | conn->security_cfm_cb = pairing_complete_cb; |
| 2998 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 2999 | } else { |
| 3000 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3001 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3002 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3003 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3004 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3005 | conn->io_capability = cp->io_cap; |
| 3006 | cmd->user_data = conn; |
| 3007 | |
| 3008 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3009 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3010 | pairing_complete(cmd, 0); |
| 3011 | |
| 3012 | err = 0; |
| 3013 | |
| 3014 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3015 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3016 | return err; |
| 3017 | } |
| 3018 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3019 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3020 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3021 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3022 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3023 | struct pending_cmd *cmd; |
| 3024 | struct hci_conn *conn; |
| 3025 | int err; |
| 3026 | |
| 3027 | BT_DBG(""); |
| 3028 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3029 | hci_dev_lock(hdev); |
| 3030 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3031 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3032 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3033 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3034 | goto unlock; |
| 3035 | } |
| 3036 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3037 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3038 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3039 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3040 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3041 | goto unlock; |
| 3042 | } |
| 3043 | |
| 3044 | conn = cmd->user_data; |
| 3045 | |
| 3046 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3047 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3048 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3049 | goto unlock; |
| 3050 | } |
| 3051 | |
| 3052 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3053 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3054 | 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] | 3055 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3056 | unlock: |
| 3057 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3058 | return err; |
| 3059 | } |
| 3060 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3061 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3062 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3063 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3064 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3065 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3066 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3067 | int err; |
| 3068 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3069 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3070 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3071 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3072 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3073 | MGMT_STATUS_NOT_POWERED, addr, |
| 3074 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3075 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3076 | } |
| 3077 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3078 | if (addr->type == BDADDR_BREDR) |
| 3079 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3080 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3081 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3082 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3083 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3084 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3085 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3086 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3087 | goto done; |
| 3088 | } |
| 3089 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3090 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3091 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3092 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3093 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3094 | MGMT_STATUS_SUCCESS, addr, |
| 3095 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3096 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3097 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3098 | MGMT_STATUS_FAILED, addr, |
| 3099 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3100 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3101 | goto done; |
| 3102 | } |
| 3103 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3104 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3105 | if (!cmd) { |
| 3106 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3107 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3108 | } |
| 3109 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3110 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3111 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3112 | struct hci_cp_user_passkey_reply cp; |
| 3113 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3114 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3115 | cp.passkey = passkey; |
| 3116 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3117 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3118 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3119 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3120 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3121 | if (err < 0) |
| 3122 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3123 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3124 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3125 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3126 | return err; |
| 3127 | } |
| 3128 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3129 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3130 | void *data, u16 len) |
| 3131 | { |
| 3132 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3133 | |
| 3134 | BT_DBG(""); |
| 3135 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3136 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3137 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3138 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3139 | } |
| 3140 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3141 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3142 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3143 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3144 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3145 | |
| 3146 | BT_DBG(""); |
| 3147 | |
| 3148 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3149 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3150 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3151 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3152 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3153 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3154 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3155 | } |
| 3156 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3157 | 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] | 3158 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3159 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3160 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3161 | |
| 3162 | BT_DBG(""); |
| 3163 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3164 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3165 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3166 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3167 | } |
| 3168 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3169 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3170 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3171 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3172 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3173 | |
| 3174 | BT_DBG(""); |
| 3175 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3176 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3177 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3178 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3179 | } |
| 3180 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3181 | 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] | 3182 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3183 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3184 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3185 | |
| 3186 | BT_DBG(""); |
| 3187 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3188 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3189 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3190 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3191 | } |
| 3192 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3193 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3194 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3195 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3196 | struct hci_cp_write_local_name cp; |
| 3197 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3198 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3199 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3200 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3201 | } |
| 3202 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3203 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3204 | { |
| 3205 | struct mgmt_cp_set_local_name *cp; |
| 3206 | struct pending_cmd *cmd; |
| 3207 | |
| 3208 | BT_DBG("status 0x%02x", status); |
| 3209 | |
| 3210 | hci_dev_lock(hdev); |
| 3211 | |
| 3212 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3213 | if (!cmd) |
| 3214 | goto unlock; |
| 3215 | |
| 3216 | cp = cmd->param; |
| 3217 | |
| 3218 | if (status) |
| 3219 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3220 | mgmt_status(status)); |
| 3221 | else |
| 3222 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3223 | cp, sizeof(*cp)); |
| 3224 | |
| 3225 | mgmt_pending_remove(cmd); |
| 3226 | |
| 3227 | unlock: |
| 3228 | hci_dev_unlock(hdev); |
| 3229 | } |
| 3230 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3231 | 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] | 3232 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3233 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3234 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3235 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3236 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3237 | int err; |
| 3238 | |
| 3239 | BT_DBG(""); |
| 3240 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3241 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3242 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3243 | /* If the old values are the same as the new ones just return a |
| 3244 | * direct command complete event. |
| 3245 | */ |
| 3246 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3247 | !memcmp(hdev->short_name, cp->short_name, |
| 3248 | sizeof(hdev->short_name))) { |
| 3249 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3250 | data, len); |
| 3251 | goto failed; |
| 3252 | } |
| 3253 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3254 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3255 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3256 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3257 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3258 | |
| 3259 | 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] | 3260 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3261 | if (err < 0) |
| 3262 | goto failed; |
| 3263 | |
| 3264 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3265 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3266 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3267 | goto failed; |
| 3268 | } |
| 3269 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3270 | 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] | 3271 | if (!cmd) { |
| 3272 | err = -ENOMEM; |
| 3273 | goto failed; |
| 3274 | } |
| 3275 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3276 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3277 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3278 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3279 | |
| 3280 | if (lmp_bredr_capable(hdev)) { |
| 3281 | update_name(&req); |
| 3282 | update_eir(&req); |
| 3283 | } |
| 3284 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3285 | /* The name is stored in the scan response data and so |
| 3286 | * no need to udpate the advertising data here. |
| 3287 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3288 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3289 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3290 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3291 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3292 | if (err < 0) |
| 3293 | mgmt_pending_remove(cmd); |
| 3294 | |
| 3295 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3296 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3297 | return err; |
| 3298 | } |
| 3299 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3300 | 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] | 3301 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3302 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3303 | struct pending_cmd *cmd; |
| 3304 | int err; |
| 3305 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3306 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3307 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3308 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3309 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3310 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3311 | 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] | 3312 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3313 | goto unlock; |
| 3314 | } |
| 3315 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3316 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3317 | 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] | 3318 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3319 | goto unlock; |
| 3320 | } |
| 3321 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3322 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3323 | 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] | 3324 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3325 | goto unlock; |
| 3326 | } |
| 3327 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3328 | 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] | 3329 | if (!cmd) { |
| 3330 | err = -ENOMEM; |
| 3331 | goto unlock; |
| 3332 | } |
| 3333 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3334 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3335 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3336 | 0, NULL); |
| 3337 | else |
| 3338 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3339 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3340 | if (err < 0) |
| 3341 | mgmt_pending_remove(cmd); |
| 3342 | |
| 3343 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3344 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3345 | return err; |
| 3346 | } |
| 3347 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3348 | 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] | 3349 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3350 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3351 | int err; |
| 3352 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3353 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3354 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3355 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3356 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3357 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3358 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3359 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3360 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3361 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3362 | cp->hash, cp->randomizer); |
| 3363 | if (err < 0) |
| 3364 | status = MGMT_STATUS_FAILED; |
| 3365 | else |
| 3366 | status = MGMT_STATUS_SUCCESS; |
| 3367 | |
| 3368 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3369 | status, &cp->addr, sizeof(cp->addr)); |
| 3370 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3371 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3372 | u8 status; |
| 3373 | |
| 3374 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3375 | cp->hash192, |
| 3376 | cp->randomizer192, |
| 3377 | cp->hash256, |
| 3378 | cp->randomizer256); |
| 3379 | if (err < 0) |
| 3380 | status = MGMT_STATUS_FAILED; |
| 3381 | else |
| 3382 | status = MGMT_STATUS_SUCCESS; |
| 3383 | |
| 3384 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3385 | status, &cp->addr, sizeof(cp->addr)); |
| 3386 | } else { |
| 3387 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3388 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3389 | MGMT_STATUS_INVALID_PARAMS); |
| 3390 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3391 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3392 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3393 | return err; |
| 3394 | } |
| 3395 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3396 | 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] | 3397 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3398 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3399 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3400 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3401 | int err; |
| 3402 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3403 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3404 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3405 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3406 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3407 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3408 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3409 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3410 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3411 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3412 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3413 | 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] | 3414 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3415 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3416 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3417 | return err; |
| 3418 | } |
| 3419 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3420 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3421 | { |
| 3422 | struct pending_cmd *cmd; |
| 3423 | u8 type; |
| 3424 | int err; |
| 3425 | |
| 3426 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3427 | |
| 3428 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3429 | if (!cmd) |
| 3430 | return -ENOENT; |
| 3431 | |
| 3432 | type = hdev->discovery.type; |
| 3433 | |
| 3434 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3435 | &type, sizeof(type)); |
| 3436 | mgmt_pending_remove(cmd); |
| 3437 | |
| 3438 | return err; |
| 3439 | } |
| 3440 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3441 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3442 | { |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3443 | unsigned long timeout = 0; |
| 3444 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3445 | BT_DBG("status %d", status); |
| 3446 | |
| 3447 | if (status) { |
| 3448 | hci_dev_lock(hdev); |
| 3449 | mgmt_start_discovery_failed(hdev, status); |
| 3450 | hci_dev_unlock(hdev); |
| 3451 | return; |
| 3452 | } |
| 3453 | |
| 3454 | hci_dev_lock(hdev); |
| 3455 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3456 | hci_dev_unlock(hdev); |
| 3457 | |
| 3458 | switch (hdev->discovery.type) { |
| 3459 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3460 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3461 | break; |
| 3462 | |
| 3463 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3464 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3465 | break; |
| 3466 | |
| 3467 | case DISCOV_TYPE_BREDR: |
| 3468 | break; |
| 3469 | |
| 3470 | default: |
| 3471 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3472 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3473 | |
| 3474 | if (!timeout) |
| 3475 | return; |
| 3476 | |
| 3477 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3478 | } |
| 3479 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3480 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3481 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3482 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3483 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3484 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3485 | struct hci_cp_le_set_scan_param param_cp; |
| 3486 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3487 | struct hci_cp_inquiry inq_cp; |
| 3488 | struct hci_request req; |
| 3489 | /* General inquiry access code (GIAC) */ |
| 3490 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3491 | u8 status, own_addr_type; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3492 | int err; |
| 3493 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3494 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3495 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3496 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3497 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3498 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3499 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3500 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3501 | goto failed; |
| 3502 | } |
| 3503 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3504 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3505 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3506 | MGMT_STATUS_BUSY); |
| 3507 | goto failed; |
| 3508 | } |
| 3509 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3510 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3511 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3512 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3513 | goto failed; |
| 3514 | } |
| 3515 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3516 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3517 | if (!cmd) { |
| 3518 | err = -ENOMEM; |
| 3519 | goto failed; |
| 3520 | } |
| 3521 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3522 | hdev->discovery.type = cp->type; |
| 3523 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3524 | hci_req_init(&req, hdev); |
| 3525 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3526 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3527 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3528 | status = mgmt_bredr_support(hdev); |
| 3529 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3530 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3531 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3532 | mgmt_pending_remove(cmd); |
| 3533 | goto failed; |
| 3534 | } |
| 3535 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3536 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3537 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3538 | MGMT_STATUS_BUSY); |
| 3539 | mgmt_pending_remove(cmd); |
| 3540 | goto failed; |
| 3541 | } |
| 3542 | |
| 3543 | hci_inquiry_cache_flush(hdev); |
| 3544 | |
| 3545 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3546 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3547 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3548 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3549 | break; |
| 3550 | |
| 3551 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3552 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3553 | status = mgmt_le_support(hdev); |
| 3554 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3555 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3556 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3557 | mgmt_pending_remove(cmd); |
| 3558 | goto failed; |
| 3559 | } |
| 3560 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3561 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3562 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3563 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3564 | MGMT_STATUS_NOT_SUPPORTED); |
| 3565 | mgmt_pending_remove(cmd); |
| 3566 | goto failed; |
| 3567 | } |
| 3568 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3569 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3570 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3571 | MGMT_STATUS_REJECTED); |
| 3572 | mgmt_pending_remove(cmd); |
| 3573 | goto failed; |
| 3574 | } |
| 3575 | |
Andre Guedes | c54c386 | 2014-02-26 20:21:50 -0300 | [diff] [blame] | 3576 | /* If controller is scanning, it means the background scanning |
| 3577 | * is running. Thus, we should temporarily stop it in order to |
| 3578 | * set the discovery scanning parameters. |
| 3579 | */ |
| 3580 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3581 | hci_req_add_le_scan_disable(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3582 | |
| 3583 | memset(¶m_cp, 0, sizeof(param_cp)); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3584 | |
Marcel Holtmann | 94b1fc9 | 2014-02-23 20:25:54 -0800 | [diff] [blame] | 3585 | /* All active scans will be done with either a resolvable |
| 3586 | * private address (when privacy feature has been enabled) |
| 3587 | * or unresolvable private address. |
| 3588 | */ |
| 3589 | err = hci_update_random_address(&req, true, &own_addr_type); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3590 | if (err < 0) { |
| 3591 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3592 | MGMT_STATUS_FAILED); |
| 3593 | mgmt_pending_remove(cmd); |
| 3594 | goto failed; |
| 3595 | } |
| 3596 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3597 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3598 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3599 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3600 | param_cp.own_address_type = own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3601 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3602 | ¶m_cp); |
| 3603 | |
| 3604 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3605 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3606 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3607 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3608 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3609 | break; |
| 3610 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3611 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3612 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3613 | MGMT_STATUS_INVALID_PARAMS); |
| 3614 | mgmt_pending_remove(cmd); |
| 3615 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3616 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3617 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3618 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3619 | if (err < 0) |
| 3620 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3621 | else |
| 3622 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3623 | |
| 3624 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3625 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3626 | return err; |
| 3627 | } |
| 3628 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3629 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3630 | { |
| 3631 | struct pending_cmd *cmd; |
| 3632 | int err; |
| 3633 | |
| 3634 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3635 | if (!cmd) |
| 3636 | return -ENOENT; |
| 3637 | |
| 3638 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3639 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3640 | mgmt_pending_remove(cmd); |
| 3641 | |
| 3642 | return err; |
| 3643 | } |
| 3644 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3645 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3646 | { |
| 3647 | BT_DBG("status %d", status); |
| 3648 | |
| 3649 | hci_dev_lock(hdev); |
| 3650 | |
| 3651 | if (status) { |
| 3652 | mgmt_stop_discovery_failed(hdev, status); |
| 3653 | goto unlock; |
| 3654 | } |
| 3655 | |
| 3656 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3657 | |
| 3658 | unlock: |
| 3659 | hci_dev_unlock(hdev); |
| 3660 | } |
| 3661 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3662 | 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] | 3663 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3664 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3665 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3666 | struct pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3667 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3668 | int err; |
| 3669 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3670 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3671 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3672 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3673 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3674 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3675 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3676 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3677 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3678 | goto unlock; |
| 3679 | } |
| 3680 | |
| 3681 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3682 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3683 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3684 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3685 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3686 | } |
| 3687 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3688 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3689 | if (!cmd) { |
| 3690 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3691 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3692 | } |
| 3693 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3694 | hci_req_init(&req, hdev); |
| 3695 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3696 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3697 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3698 | err = hci_req_run(&req, stop_discovery_complete); |
| 3699 | if (!err) { |
| 3700 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3701 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3702 | } |
| 3703 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3704 | mgmt_pending_remove(cmd); |
| 3705 | |
| 3706 | /* If no HCI commands were sent we're done */ |
| 3707 | if (err == -ENODATA) { |
| 3708 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 3709 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 3710 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3711 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3712 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3713 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3714 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3715 | return err; |
| 3716 | } |
| 3717 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3718 | 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] | 3719 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3720 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3721 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3722 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3723 | int err; |
| 3724 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3725 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3726 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3727 | hci_dev_lock(hdev); |
| 3728 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3729 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3730 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3731 | MGMT_STATUS_FAILED, &cp->addr, |
| 3732 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3733 | goto failed; |
| 3734 | } |
| 3735 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3736 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3737 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3738 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3739 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3740 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3741 | goto failed; |
| 3742 | } |
| 3743 | |
| 3744 | if (cp->name_known) { |
| 3745 | e->name_state = NAME_KNOWN; |
| 3746 | list_del(&e->list); |
| 3747 | } else { |
| 3748 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3749 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3750 | } |
| 3751 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3752 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3753 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3754 | |
| 3755 | failed: |
| 3756 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3757 | return err; |
| 3758 | } |
| 3759 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3760 | 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] | 3761 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3762 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3763 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3764 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3765 | int err; |
| 3766 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3767 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3768 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3769 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3770 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3771 | MGMT_STATUS_INVALID_PARAMS, |
| 3772 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3773 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3774 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3775 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3776 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3777 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3778 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3779 | goto done; |
| 3780 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3781 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3782 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3783 | sk); |
| 3784 | status = MGMT_STATUS_SUCCESS; |
| 3785 | |
| 3786 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3787 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3788 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3789 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3790 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3791 | |
| 3792 | return err; |
| 3793 | } |
| 3794 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3795 | 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] | 3796 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3797 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3798 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3799 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3800 | int err; |
| 3801 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3802 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3803 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3804 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3805 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3806 | MGMT_STATUS_INVALID_PARAMS, |
| 3807 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3808 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3809 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3810 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3811 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3812 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3813 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3814 | goto done; |
| 3815 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3816 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3817 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3818 | sk); |
| 3819 | status = MGMT_STATUS_SUCCESS; |
| 3820 | |
| 3821 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3822 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3823 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3824 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3825 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3826 | |
| 3827 | return err; |
| 3828 | } |
| 3829 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3830 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3831 | u16 len) |
| 3832 | { |
| 3833 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3834 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3835 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3836 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3837 | |
| 3838 | BT_DBG("%s", hdev->name); |
| 3839 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3840 | source = __le16_to_cpu(cp->source); |
| 3841 | |
| 3842 | if (source > 0x0002) |
| 3843 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3844 | MGMT_STATUS_INVALID_PARAMS); |
| 3845 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3846 | hci_dev_lock(hdev); |
| 3847 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3848 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3849 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 3850 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 3851 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 3852 | |
| 3853 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 3854 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3855 | hci_req_init(&req, hdev); |
| 3856 | update_eir(&req); |
| 3857 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3858 | |
| 3859 | hci_dev_unlock(hdev); |
| 3860 | |
| 3861 | return err; |
| 3862 | } |
| 3863 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3864 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 3865 | { |
| 3866 | struct cmd_lookup match = { NULL, hdev }; |
| 3867 | |
| 3868 | if (status) { |
| 3869 | u8 mgmt_err = mgmt_status(status); |
| 3870 | |
| 3871 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 3872 | cmd_status_rsp, &mgmt_err); |
| 3873 | return; |
| 3874 | } |
| 3875 | |
| 3876 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 3877 | &match); |
| 3878 | |
| 3879 | new_settings(hdev, match.sk); |
| 3880 | |
| 3881 | if (match.sk) |
| 3882 | sock_put(match.sk); |
| 3883 | } |
| 3884 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 3885 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3886 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3887 | { |
| 3888 | struct mgmt_mode *cp = data; |
| 3889 | struct pending_cmd *cmd; |
| 3890 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3891 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3892 | int err; |
| 3893 | |
| 3894 | BT_DBG("request for %s", hdev->name); |
| 3895 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3896 | status = mgmt_le_support(hdev); |
| 3897 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3898 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3899 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3900 | |
| 3901 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3902 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3903 | MGMT_STATUS_INVALID_PARAMS); |
| 3904 | |
| 3905 | hci_dev_lock(hdev); |
| 3906 | |
| 3907 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3908 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3909 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 3910 | /* The following conditions are ones which mean that we should |
| 3911 | * not do any HCI communication but directly send a mgmt |
| 3912 | * response to user space (after toggling the flag if |
| 3913 | * necessary). |
| 3914 | */ |
| 3915 | if (!hdev_is_powered(hdev) || val == enabled || |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 3916 | hci_conn_num(hdev, LE_LINK) > 0) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3917 | bool changed = false; |
| 3918 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3919 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 3920 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3921 | changed = true; |
| 3922 | } |
| 3923 | |
| 3924 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 3925 | if (err < 0) |
| 3926 | goto unlock; |
| 3927 | |
| 3928 | if (changed) |
| 3929 | err = new_settings(hdev, sk); |
| 3930 | |
| 3931 | goto unlock; |
| 3932 | } |
| 3933 | |
| 3934 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 3935 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 3936 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3937 | MGMT_STATUS_BUSY); |
| 3938 | goto unlock; |
| 3939 | } |
| 3940 | |
| 3941 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 3942 | if (!cmd) { |
| 3943 | err = -ENOMEM; |
| 3944 | goto unlock; |
| 3945 | } |
| 3946 | |
| 3947 | hci_req_init(&req, hdev); |
| 3948 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 3949 | if (val) |
| 3950 | enable_advertising(&req); |
| 3951 | else |
| 3952 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3953 | |
| 3954 | err = hci_req_run(&req, set_advertising_complete); |
| 3955 | if (err < 0) |
| 3956 | mgmt_pending_remove(cmd); |
| 3957 | |
| 3958 | unlock: |
| 3959 | hci_dev_unlock(hdev); |
| 3960 | return err; |
| 3961 | } |
| 3962 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3963 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 3964 | void *data, u16 len) |
| 3965 | { |
| 3966 | struct mgmt_cp_set_static_address *cp = data; |
| 3967 | int err; |
| 3968 | |
| 3969 | BT_DBG("%s", hdev->name); |
| 3970 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3971 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3972 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3973 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3974 | |
| 3975 | if (hdev_is_powered(hdev)) |
| 3976 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 3977 | MGMT_STATUS_REJECTED); |
| 3978 | |
| 3979 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 3980 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 3981 | return cmd_status(sk, hdev->id, |
| 3982 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3983 | MGMT_STATUS_INVALID_PARAMS); |
| 3984 | |
| 3985 | /* Two most significant bits shall be set */ |
| 3986 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 3987 | return cmd_status(sk, hdev->id, |
| 3988 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3989 | MGMT_STATUS_INVALID_PARAMS); |
| 3990 | } |
| 3991 | |
| 3992 | hci_dev_lock(hdev); |
| 3993 | |
| 3994 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 3995 | |
| 3996 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 3997 | |
| 3998 | hci_dev_unlock(hdev); |
| 3999 | |
| 4000 | return err; |
| 4001 | } |
| 4002 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4003 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4004 | void *data, u16 len) |
| 4005 | { |
| 4006 | struct mgmt_cp_set_scan_params *cp = data; |
| 4007 | __u16 interval, window; |
| 4008 | int err; |
| 4009 | |
| 4010 | BT_DBG("%s", hdev->name); |
| 4011 | |
| 4012 | if (!lmp_le_capable(hdev)) |
| 4013 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4014 | MGMT_STATUS_NOT_SUPPORTED); |
| 4015 | |
| 4016 | interval = __le16_to_cpu(cp->interval); |
| 4017 | |
| 4018 | if (interval < 0x0004 || interval > 0x4000) |
| 4019 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4020 | MGMT_STATUS_INVALID_PARAMS); |
| 4021 | |
| 4022 | window = __le16_to_cpu(cp->window); |
| 4023 | |
| 4024 | if (window < 0x0004 || window > 0x4000) |
| 4025 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4026 | MGMT_STATUS_INVALID_PARAMS); |
| 4027 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4028 | if (window > interval) |
| 4029 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4030 | MGMT_STATUS_INVALID_PARAMS); |
| 4031 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4032 | hci_dev_lock(hdev); |
| 4033 | |
| 4034 | hdev->le_scan_interval = interval; |
| 4035 | hdev->le_scan_window = window; |
| 4036 | |
| 4037 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 4038 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4039 | /* If background scan is running, restart it so new parameters are |
| 4040 | * loaded. |
| 4041 | */ |
| 4042 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4043 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4044 | struct hci_request req; |
| 4045 | |
| 4046 | hci_req_init(&req, hdev); |
| 4047 | |
| 4048 | hci_req_add_le_scan_disable(&req); |
| 4049 | hci_req_add_le_passive_scan(&req); |
| 4050 | |
| 4051 | hci_req_run(&req, NULL); |
| 4052 | } |
| 4053 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4054 | hci_dev_unlock(hdev); |
| 4055 | |
| 4056 | return err; |
| 4057 | } |
| 4058 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4059 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 4060 | { |
| 4061 | struct pending_cmd *cmd; |
| 4062 | |
| 4063 | BT_DBG("status 0x%02x", status); |
| 4064 | |
| 4065 | hci_dev_lock(hdev); |
| 4066 | |
| 4067 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4068 | if (!cmd) |
| 4069 | goto unlock; |
| 4070 | |
| 4071 | if (status) { |
| 4072 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4073 | mgmt_status(status)); |
| 4074 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4075 | struct mgmt_mode *cp = cmd->param; |
| 4076 | |
| 4077 | if (cp->val) |
| 4078 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4079 | else |
| 4080 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4081 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4082 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4083 | new_settings(hdev, cmd->sk); |
| 4084 | } |
| 4085 | |
| 4086 | mgmt_pending_remove(cmd); |
| 4087 | |
| 4088 | unlock: |
| 4089 | hci_dev_unlock(hdev); |
| 4090 | } |
| 4091 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4092 | 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] | 4093 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4094 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4095 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4096 | struct pending_cmd *cmd; |
| 4097 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4098 | int err; |
| 4099 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4100 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4101 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4102 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4103 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4104 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4105 | MGMT_STATUS_NOT_SUPPORTED); |
| 4106 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4107 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4108 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4109 | MGMT_STATUS_INVALID_PARAMS); |
| 4110 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4111 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4112 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4113 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4114 | |
| 4115 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4116 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4117 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4118 | |
| 4119 | hci_dev_lock(hdev); |
| 4120 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4121 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4122 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4123 | MGMT_STATUS_BUSY); |
| 4124 | goto unlock; |
| 4125 | } |
| 4126 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4127 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4128 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4129 | hdev); |
| 4130 | goto unlock; |
| 4131 | } |
| 4132 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4133 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4134 | data, len); |
| 4135 | if (!cmd) { |
| 4136 | err = -ENOMEM; |
| 4137 | goto unlock; |
| 4138 | } |
| 4139 | |
| 4140 | hci_req_init(&req, hdev); |
| 4141 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4142 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4143 | |
| 4144 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4145 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4146 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4147 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4148 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4149 | } |
| 4150 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4151 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4152 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4153 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4154 | return err; |
| 4155 | } |
| 4156 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 4157 | static void set_bredr_scan(struct hci_request *req) |
| 4158 | { |
| 4159 | struct hci_dev *hdev = req->hdev; |
| 4160 | u8 scan = 0; |
| 4161 | |
| 4162 | /* Ensure that fast connectable is disabled. This function will |
| 4163 | * not do anything if the page scan parameters are already what |
| 4164 | * they should be. |
| 4165 | */ |
| 4166 | write_fast_connectable(req, false); |
| 4167 | |
| 4168 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4169 | scan |= SCAN_PAGE; |
| 4170 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 4171 | scan |= SCAN_INQUIRY; |
| 4172 | |
| 4173 | if (scan) |
| 4174 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 4175 | } |
| 4176 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4177 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4178 | { |
| 4179 | struct pending_cmd *cmd; |
| 4180 | |
| 4181 | BT_DBG("status 0x%02x", status); |
| 4182 | |
| 4183 | hci_dev_lock(hdev); |
| 4184 | |
| 4185 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4186 | if (!cmd) |
| 4187 | goto unlock; |
| 4188 | |
| 4189 | if (status) { |
| 4190 | u8 mgmt_err = mgmt_status(status); |
| 4191 | |
| 4192 | /* We need to restore the flag if related HCI commands |
| 4193 | * failed. |
| 4194 | */ |
| 4195 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4196 | |
| 4197 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4198 | } else { |
| 4199 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4200 | new_settings(hdev, cmd->sk); |
| 4201 | } |
| 4202 | |
| 4203 | mgmt_pending_remove(cmd); |
| 4204 | |
| 4205 | unlock: |
| 4206 | hci_dev_unlock(hdev); |
| 4207 | } |
| 4208 | |
| 4209 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4210 | { |
| 4211 | struct mgmt_mode *cp = data; |
| 4212 | struct pending_cmd *cmd; |
| 4213 | struct hci_request req; |
| 4214 | int err; |
| 4215 | |
| 4216 | BT_DBG("request for %s", hdev->name); |
| 4217 | |
| 4218 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4219 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4220 | MGMT_STATUS_NOT_SUPPORTED); |
| 4221 | |
| 4222 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4223 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4224 | MGMT_STATUS_REJECTED); |
| 4225 | |
| 4226 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4227 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4228 | MGMT_STATUS_INVALID_PARAMS); |
| 4229 | |
| 4230 | hci_dev_lock(hdev); |
| 4231 | |
| 4232 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4233 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4234 | goto unlock; |
| 4235 | } |
| 4236 | |
| 4237 | if (!hdev_is_powered(hdev)) { |
| 4238 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4239 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4240 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4241 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4242 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4243 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4244 | } |
| 4245 | |
| 4246 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4247 | |
| 4248 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4249 | if (err < 0) |
| 4250 | goto unlock; |
| 4251 | |
| 4252 | err = new_settings(hdev, sk); |
| 4253 | goto unlock; |
| 4254 | } |
| 4255 | |
| 4256 | /* Reject disabling when powered on */ |
| 4257 | if (!cp->val) { |
| 4258 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4259 | MGMT_STATUS_REJECTED); |
| 4260 | goto unlock; |
| 4261 | } |
| 4262 | |
| 4263 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4264 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4265 | MGMT_STATUS_BUSY); |
| 4266 | goto unlock; |
| 4267 | } |
| 4268 | |
| 4269 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4270 | if (!cmd) { |
| 4271 | err = -ENOMEM; |
| 4272 | goto unlock; |
| 4273 | } |
| 4274 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4275 | /* 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] | 4276 | * generates the correct flags. |
| 4277 | */ |
| 4278 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4279 | |
| 4280 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4281 | |
| 4282 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4283 | set_bredr_scan(&req); |
| 4284 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4285 | /* Since only the advertising data flags will change, there |
| 4286 | * is no need to update the scan response data. |
| 4287 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4288 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4289 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4290 | err = hci_req_run(&req, set_bredr_complete); |
| 4291 | if (err < 0) |
| 4292 | mgmt_pending_remove(cmd); |
| 4293 | |
| 4294 | unlock: |
| 4295 | hci_dev_unlock(hdev); |
| 4296 | return err; |
| 4297 | } |
| 4298 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4299 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4300 | void *data, u16 len) |
| 4301 | { |
| 4302 | struct mgmt_mode *cp = data; |
| 4303 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4304 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4305 | int err; |
| 4306 | |
| 4307 | BT_DBG("request for %s", hdev->name); |
| 4308 | |
| 4309 | status = mgmt_bredr_support(hdev); |
| 4310 | if (status) |
| 4311 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4312 | status); |
| 4313 | |
Marcel Holtmann | 5afeac14 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4314 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 4315 | !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4316 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4317 | MGMT_STATUS_NOT_SUPPORTED); |
| 4318 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4319 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4320 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4321 | MGMT_STATUS_INVALID_PARAMS); |
| 4322 | |
| 4323 | hci_dev_lock(hdev); |
| 4324 | |
| 4325 | if (!hdev_is_powered(hdev)) { |
| 4326 | bool changed; |
| 4327 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4328 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4329 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4330 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4331 | if (cp->val == 0x02) |
| 4332 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4333 | else |
| 4334 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4335 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4336 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4337 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4338 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4339 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4340 | |
| 4341 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4342 | if (err < 0) |
| 4343 | goto failed; |
| 4344 | |
| 4345 | if (changed) |
| 4346 | err = new_settings(hdev, sk); |
| 4347 | |
| 4348 | goto failed; |
| 4349 | } |
| 4350 | |
| 4351 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4352 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4353 | MGMT_STATUS_BUSY); |
| 4354 | goto failed; |
| 4355 | } |
| 4356 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4357 | val = !!cp->val; |
| 4358 | |
| 4359 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4360 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4361 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4362 | goto failed; |
| 4363 | } |
| 4364 | |
| 4365 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4366 | if (!cmd) { |
| 4367 | err = -ENOMEM; |
| 4368 | goto failed; |
| 4369 | } |
| 4370 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4371 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4372 | if (err < 0) { |
| 4373 | mgmt_pending_remove(cmd); |
| 4374 | goto failed; |
| 4375 | } |
| 4376 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4377 | if (cp->val == 0x02) |
| 4378 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4379 | else |
| 4380 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4381 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4382 | failed: |
| 4383 | hci_dev_unlock(hdev); |
| 4384 | return err; |
| 4385 | } |
| 4386 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4387 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4388 | void *data, u16 len) |
| 4389 | { |
| 4390 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4391 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4392 | int err; |
| 4393 | |
| 4394 | BT_DBG("request for %s", hdev->name); |
| 4395 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4396 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4397 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4398 | MGMT_STATUS_INVALID_PARAMS); |
| 4399 | |
| 4400 | hci_dev_lock(hdev); |
| 4401 | |
| 4402 | if (cp->val) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4403 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 4404 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4405 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4406 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 4407 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4408 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4409 | if (cp->val == 0x02) |
| 4410 | use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, |
| 4411 | &hdev->dev_flags); |
| 4412 | else |
| 4413 | use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, |
| 4414 | &hdev->dev_flags); |
| 4415 | |
| 4416 | if (hdev_is_powered(hdev) && use_changed && |
| 4417 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 4418 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4419 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4420 | sizeof(mode), &mode); |
| 4421 | } |
| 4422 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4423 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4424 | if (err < 0) |
| 4425 | goto unlock; |
| 4426 | |
| 4427 | if (changed) |
| 4428 | err = new_settings(hdev, sk); |
| 4429 | |
| 4430 | unlock: |
| 4431 | hci_dev_unlock(hdev); |
| 4432 | return err; |
| 4433 | } |
| 4434 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4435 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4436 | u16 len) |
| 4437 | { |
| 4438 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4439 | bool changed; |
| 4440 | int err; |
| 4441 | |
| 4442 | BT_DBG("request for %s", hdev->name); |
| 4443 | |
| 4444 | if (!lmp_le_capable(hdev)) |
| 4445 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4446 | MGMT_STATUS_NOT_SUPPORTED); |
| 4447 | |
| 4448 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4449 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4450 | MGMT_STATUS_INVALID_PARAMS); |
| 4451 | |
| 4452 | if (hdev_is_powered(hdev)) |
| 4453 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4454 | MGMT_STATUS_REJECTED); |
| 4455 | |
| 4456 | hci_dev_lock(hdev); |
| 4457 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4458 | /* If user space supports this command it is also expected to |
| 4459 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4460 | */ |
| 4461 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4462 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4463 | if (cp->privacy) { |
| 4464 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4465 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4466 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4467 | } else { |
| 4468 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4469 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4470 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4471 | } |
| 4472 | |
| 4473 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4474 | if (err < 0) |
| 4475 | goto unlock; |
| 4476 | |
| 4477 | if (changed) |
| 4478 | err = new_settings(hdev, sk); |
| 4479 | |
| 4480 | unlock: |
| 4481 | hci_dev_unlock(hdev); |
| 4482 | return err; |
| 4483 | } |
| 4484 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4485 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4486 | { |
| 4487 | switch (irk->addr.type) { |
| 4488 | case BDADDR_LE_PUBLIC: |
| 4489 | return true; |
| 4490 | |
| 4491 | case BDADDR_LE_RANDOM: |
| 4492 | /* Two most significant bits shall be set */ |
| 4493 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4494 | return false; |
| 4495 | return true; |
| 4496 | } |
| 4497 | |
| 4498 | return false; |
| 4499 | } |
| 4500 | |
| 4501 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4502 | u16 len) |
| 4503 | { |
| 4504 | struct mgmt_cp_load_irks *cp = cp_data; |
| 4505 | u16 irk_count, expected_len; |
| 4506 | int i, err; |
| 4507 | |
| 4508 | BT_DBG("request for %s", hdev->name); |
| 4509 | |
| 4510 | if (!lmp_le_capable(hdev)) |
| 4511 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4512 | MGMT_STATUS_NOT_SUPPORTED); |
| 4513 | |
| 4514 | irk_count = __le16_to_cpu(cp->irk_count); |
| 4515 | |
| 4516 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4517 | if (expected_len != len) { |
| 4518 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4519 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4520 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4521 | MGMT_STATUS_INVALID_PARAMS); |
| 4522 | } |
| 4523 | |
| 4524 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4525 | |
| 4526 | for (i = 0; i < irk_count; i++) { |
| 4527 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4528 | |
| 4529 | if (!irk_is_valid(key)) |
| 4530 | return cmd_status(sk, hdev->id, |
| 4531 | MGMT_OP_LOAD_IRKS, |
| 4532 | MGMT_STATUS_INVALID_PARAMS); |
| 4533 | } |
| 4534 | |
| 4535 | hci_dev_lock(hdev); |
| 4536 | |
| 4537 | hci_smp_irks_clear(hdev); |
| 4538 | |
| 4539 | for (i = 0; i < irk_count; i++) { |
| 4540 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4541 | u8 addr_type; |
| 4542 | |
| 4543 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4544 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4545 | else |
| 4546 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4547 | |
| 4548 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4549 | BDADDR_ANY); |
| 4550 | } |
| 4551 | |
| 4552 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4553 | |
| 4554 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4555 | |
| 4556 | hci_dev_unlock(hdev); |
| 4557 | |
| 4558 | return err; |
| 4559 | } |
| 4560 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4561 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4562 | { |
| 4563 | if (key->master != 0x00 && key->master != 0x01) |
| 4564 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4565 | |
| 4566 | switch (key->addr.type) { |
| 4567 | case BDADDR_LE_PUBLIC: |
| 4568 | return true; |
| 4569 | |
| 4570 | case BDADDR_LE_RANDOM: |
| 4571 | /* Two most significant bits shall be set */ |
| 4572 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4573 | return false; |
| 4574 | return true; |
| 4575 | } |
| 4576 | |
| 4577 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4578 | } |
| 4579 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4580 | 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] | 4581 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4582 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4583 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
| 4584 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4585 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4586 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4587 | BT_DBG("request for %s", hdev->name); |
| 4588 | |
| 4589 | if (!lmp_le_capable(hdev)) |
| 4590 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4591 | MGMT_STATUS_NOT_SUPPORTED); |
| 4592 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4593 | key_count = __le16_to_cpu(cp->key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4594 | |
| 4595 | expected_len = sizeof(*cp) + key_count * |
| 4596 | sizeof(struct mgmt_ltk_info); |
| 4597 | if (expected_len != len) { |
| 4598 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4599 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4600 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4601 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4602 | } |
| 4603 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4604 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4605 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4606 | for (i = 0; i < key_count; i++) { |
| 4607 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4608 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4609 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4610 | return cmd_status(sk, hdev->id, |
| 4611 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4612 | MGMT_STATUS_INVALID_PARAMS); |
| 4613 | } |
| 4614 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4615 | hci_dev_lock(hdev); |
| 4616 | |
| 4617 | hci_smp_ltks_clear(hdev); |
| 4618 | |
| 4619 | for (i = 0; i < key_count; i++) { |
| 4620 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4621 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4622 | |
| 4623 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4624 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4625 | else |
| 4626 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4627 | |
| 4628 | if (key->master) |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4629 | type = SMP_LTK; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4630 | else |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4631 | type = SMP_LTK_SLAVE; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4632 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4633 | switch (key->type) { |
| 4634 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4635 | authenticated = 0x00; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4636 | break; |
| 4637 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4638 | authenticated = 0x01; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4639 | break; |
| 4640 | default: |
| 4641 | continue; |
| 4642 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4643 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4644 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4645 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4646 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4647 | } |
| 4648 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4649 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4650 | NULL, 0); |
| 4651 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4652 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4653 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4654 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4655 | } |
| 4656 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4657 | struct cmd_conn_lookup { |
| 4658 | struct hci_conn *conn; |
| 4659 | bool valid_tx_power; |
| 4660 | u8 mgmt_status; |
| 4661 | }; |
| 4662 | |
| 4663 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 4664 | { |
| 4665 | struct cmd_conn_lookup *match = data; |
| 4666 | struct mgmt_cp_get_conn_info *cp; |
| 4667 | struct mgmt_rp_get_conn_info rp; |
| 4668 | struct hci_conn *conn = cmd->user_data; |
| 4669 | |
| 4670 | if (conn != match->conn) |
| 4671 | return; |
| 4672 | |
| 4673 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 4674 | |
| 4675 | memset(&rp, 0, sizeof(rp)); |
| 4676 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4677 | rp.addr.type = cp->addr.type; |
| 4678 | |
| 4679 | if (!match->mgmt_status) { |
| 4680 | rp.rssi = conn->rssi; |
| 4681 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4682 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4683 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4684 | rp.max_tx_power = conn->max_tx_power; |
| 4685 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4686 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4687 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 4688 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4689 | } |
| 4690 | |
| 4691 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4692 | match->mgmt_status, &rp, sizeof(rp)); |
| 4693 | |
| 4694 | hci_conn_drop(conn); |
| 4695 | |
| 4696 | mgmt_pending_remove(cmd); |
| 4697 | } |
| 4698 | |
| 4699 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 4700 | { |
| 4701 | struct hci_cp_read_rssi *cp; |
| 4702 | struct hci_conn *conn; |
| 4703 | struct cmd_conn_lookup match; |
| 4704 | u16 handle; |
| 4705 | |
| 4706 | BT_DBG("status 0x%02x", status); |
| 4707 | |
| 4708 | hci_dev_lock(hdev); |
| 4709 | |
| 4710 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4711 | * otherwise we assume it's not valid. At the moment we assume that |
| 4712 | * either both or none of current and max values are valid to keep code |
| 4713 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4714 | */ |
| 4715 | match.valid_tx_power = !status; |
| 4716 | |
| 4717 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4718 | * Level so we check which one was last sent to retrieve connection |
| 4719 | * handle. Both commands have handle as first parameter so it's safe to |
| 4720 | * cast data on the same command struct. |
| 4721 | * |
| 4722 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4723 | * In other case we simply override error to indicate success as we |
| 4724 | * already remembered if TX power value is actually valid. |
| 4725 | */ |
| 4726 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4727 | if (!cp) { |
| 4728 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 4729 | status = 0; |
| 4730 | } |
| 4731 | |
| 4732 | if (!cp) { |
| 4733 | BT_ERR("invalid sent_cmd in response"); |
| 4734 | goto unlock; |
| 4735 | } |
| 4736 | |
| 4737 | handle = __le16_to_cpu(cp->handle); |
| 4738 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4739 | if (!conn) { |
| 4740 | BT_ERR("unknown handle (%d) in response", handle); |
| 4741 | goto unlock; |
| 4742 | } |
| 4743 | |
| 4744 | match.conn = conn; |
| 4745 | match.mgmt_status = mgmt_status(status); |
| 4746 | |
| 4747 | /* Cache refresh is complete, now reply for mgmt request for given |
| 4748 | * connection only. |
| 4749 | */ |
| 4750 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 4751 | get_conn_info_complete, &match); |
| 4752 | |
| 4753 | unlock: |
| 4754 | hci_dev_unlock(hdev); |
| 4755 | } |
| 4756 | |
| 4757 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4758 | u16 len) |
| 4759 | { |
| 4760 | struct mgmt_cp_get_conn_info *cp = data; |
| 4761 | struct mgmt_rp_get_conn_info rp; |
| 4762 | struct hci_conn *conn; |
| 4763 | unsigned long conn_info_age; |
| 4764 | int err = 0; |
| 4765 | |
| 4766 | BT_DBG("%s", hdev->name); |
| 4767 | |
| 4768 | memset(&rp, 0, sizeof(rp)); |
| 4769 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4770 | rp.addr.type = cp->addr.type; |
| 4771 | |
| 4772 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 4773 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4774 | MGMT_STATUS_INVALID_PARAMS, |
| 4775 | &rp, sizeof(rp)); |
| 4776 | |
| 4777 | hci_dev_lock(hdev); |
| 4778 | |
| 4779 | if (!hdev_is_powered(hdev)) { |
| 4780 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4781 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4782 | goto unlock; |
| 4783 | } |
| 4784 | |
| 4785 | if (cp->addr.type == BDADDR_BREDR) |
| 4786 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4787 | &cp->addr.bdaddr); |
| 4788 | else |
| 4789 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 4790 | |
| 4791 | if (!conn || conn->state != BT_CONNECTED) { |
| 4792 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4793 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 4794 | goto unlock; |
| 4795 | } |
| 4796 | |
| 4797 | /* To avoid client trying to guess when to poll again for information we |
| 4798 | * calculate conn info age as random value between min/max set in hdev. |
| 4799 | */ |
| 4800 | conn_info_age = hdev->conn_info_min_age + |
| 4801 | prandom_u32_max(hdev->conn_info_max_age - |
| 4802 | hdev->conn_info_min_age); |
| 4803 | |
| 4804 | /* Query controller to refresh cached values if they are too old or were |
| 4805 | * never read. |
| 4806 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 4807 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 4808 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4809 | !conn->conn_info_timestamp) { |
| 4810 | struct hci_request req; |
| 4811 | struct hci_cp_read_tx_power req_txp_cp; |
| 4812 | struct hci_cp_read_rssi req_rssi_cp; |
| 4813 | struct pending_cmd *cmd; |
| 4814 | |
| 4815 | hci_req_init(&req, hdev); |
| 4816 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 4817 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 4818 | &req_rssi_cp); |
| 4819 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 4820 | /* For LE links TX power does not change thus we don't need to |
| 4821 | * query for it once value is known. |
| 4822 | */ |
| 4823 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 4824 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 4825 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4826 | req_txp_cp.type = 0x00; |
| 4827 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4828 | sizeof(req_txp_cp), &req_txp_cp); |
| 4829 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4830 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4831 | /* Max TX power needs to be read only once per connection */ |
| 4832 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 4833 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4834 | req_txp_cp.type = 0x01; |
| 4835 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4836 | sizeof(req_txp_cp), &req_txp_cp); |
| 4837 | } |
| 4838 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4839 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 4840 | if (err < 0) |
| 4841 | goto unlock; |
| 4842 | |
| 4843 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 4844 | data, len); |
| 4845 | if (!cmd) { |
| 4846 | err = -ENOMEM; |
| 4847 | goto unlock; |
| 4848 | } |
| 4849 | |
| 4850 | hci_conn_hold(conn); |
| 4851 | cmd->user_data = conn; |
| 4852 | |
| 4853 | conn->conn_info_timestamp = jiffies; |
| 4854 | } else { |
| 4855 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 4856 | rp.rssi = conn->rssi; |
| 4857 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4858 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4859 | |
| 4860 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4861 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 4862 | } |
| 4863 | |
| 4864 | unlock: |
| 4865 | hci_dev_unlock(hdev); |
| 4866 | return err; |
| 4867 | } |
| 4868 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 4869 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status) |
| 4870 | { |
| 4871 | struct mgmt_cp_get_clock_info *cp; |
| 4872 | struct mgmt_rp_get_clock_info rp; |
| 4873 | struct hci_cp_read_clock *hci_cp; |
| 4874 | struct pending_cmd *cmd; |
| 4875 | struct hci_conn *conn; |
| 4876 | |
| 4877 | BT_DBG("%s status %u", hdev->name, status); |
| 4878 | |
| 4879 | hci_dev_lock(hdev); |
| 4880 | |
| 4881 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 4882 | if (!hci_cp) |
| 4883 | goto unlock; |
| 4884 | |
| 4885 | if (hci_cp->which) { |
| 4886 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 4887 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4888 | } else { |
| 4889 | conn = NULL; |
| 4890 | } |
| 4891 | |
| 4892 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 4893 | if (!cmd) |
| 4894 | goto unlock; |
| 4895 | |
| 4896 | cp = cmd->param; |
| 4897 | |
| 4898 | memset(&rp, 0, sizeof(rp)); |
| 4899 | memcpy(&rp.addr, &cp->addr, sizeof(rp.addr)); |
| 4900 | |
| 4901 | if (status) |
| 4902 | goto send_rsp; |
| 4903 | |
| 4904 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 4905 | |
| 4906 | if (conn) { |
| 4907 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 4908 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 4909 | } |
| 4910 | |
| 4911 | send_rsp: |
| 4912 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 4913 | &rp, sizeof(rp)); |
| 4914 | mgmt_pending_remove(cmd); |
| 4915 | if (conn) |
| 4916 | hci_conn_drop(conn); |
| 4917 | |
| 4918 | unlock: |
| 4919 | hci_dev_unlock(hdev); |
| 4920 | } |
| 4921 | |
| 4922 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4923 | u16 len) |
| 4924 | { |
| 4925 | struct mgmt_cp_get_clock_info *cp = data; |
| 4926 | struct mgmt_rp_get_clock_info rp; |
| 4927 | struct hci_cp_read_clock hci_cp; |
| 4928 | struct pending_cmd *cmd; |
| 4929 | struct hci_request req; |
| 4930 | struct hci_conn *conn; |
| 4931 | int err; |
| 4932 | |
| 4933 | BT_DBG("%s", hdev->name); |
| 4934 | |
| 4935 | memset(&rp, 0, sizeof(rp)); |
| 4936 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4937 | rp.addr.type = cp->addr.type; |
| 4938 | |
| 4939 | if (cp->addr.type != BDADDR_BREDR) |
| 4940 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 4941 | MGMT_STATUS_INVALID_PARAMS, |
| 4942 | &rp, sizeof(rp)); |
| 4943 | |
| 4944 | hci_dev_lock(hdev); |
| 4945 | |
| 4946 | if (!hdev_is_powered(hdev)) { |
| 4947 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 4948 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4949 | goto unlock; |
| 4950 | } |
| 4951 | |
| 4952 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 4953 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4954 | &cp->addr.bdaddr); |
| 4955 | if (!conn || conn->state != BT_CONNECTED) { |
| 4956 | err = cmd_complete(sk, hdev->id, |
| 4957 | MGMT_OP_GET_CLOCK_INFO, |
| 4958 | MGMT_STATUS_NOT_CONNECTED, |
| 4959 | &rp, sizeof(rp)); |
| 4960 | goto unlock; |
| 4961 | } |
| 4962 | } else { |
| 4963 | conn = NULL; |
| 4964 | } |
| 4965 | |
| 4966 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 4967 | if (!cmd) { |
| 4968 | err = -ENOMEM; |
| 4969 | goto unlock; |
| 4970 | } |
| 4971 | |
| 4972 | hci_req_init(&req, hdev); |
| 4973 | |
| 4974 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 4975 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 4976 | |
| 4977 | if (conn) { |
| 4978 | hci_conn_hold(conn); |
| 4979 | cmd->user_data = conn; |
| 4980 | |
| 4981 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 4982 | hci_cp.which = 0x01; /* Piconet clock */ |
| 4983 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 4984 | } |
| 4985 | |
| 4986 | err = hci_req_run(&req, get_clock_info_complete); |
| 4987 | if (err < 0) |
| 4988 | mgmt_pending_remove(cmd); |
| 4989 | |
| 4990 | unlock: |
| 4991 | hci_dev_unlock(hdev); |
| 4992 | return err; |
| 4993 | } |
| 4994 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 4995 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 4996 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 4997 | { |
| 4998 | struct mgmt_ev_device_added ev; |
| 4999 | |
| 5000 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5001 | ev.addr.type = type; |
| 5002 | ev.action = action; |
| 5003 | |
| 5004 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5005 | } |
| 5006 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5007 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5008 | void *data, u16 len) |
| 5009 | { |
| 5010 | struct mgmt_cp_add_device *cp = data; |
| 5011 | u8 auto_conn, addr_type; |
| 5012 | int err; |
| 5013 | |
| 5014 | BT_DBG("%s", hdev->name); |
| 5015 | |
| 5016 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5017 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
| 5018 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5019 | MGMT_STATUS_INVALID_PARAMS, |
| 5020 | &cp->addr, sizeof(cp->addr)); |
| 5021 | |
| 5022 | if (cp->action != 0x00 && cp->action != 0x01) |
| 5023 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5024 | MGMT_STATUS_INVALID_PARAMS, |
| 5025 | &cp->addr, sizeof(cp->addr)); |
| 5026 | |
| 5027 | hci_dev_lock(hdev); |
| 5028 | |
| 5029 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5030 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5031 | else |
| 5032 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5033 | |
| 5034 | if (cp->action) |
| 5035 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
| 5036 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5037 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5038 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5039 | /* If the connection parameters don't exist for this device, |
| 5040 | * they will be created and configured with defaults. |
| 5041 | */ |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5042 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
| 5043 | auto_conn) < 0) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5044 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5045 | MGMT_STATUS_FAILED, |
| 5046 | &cp->addr, sizeof(cp->addr)); |
| 5047 | goto unlock; |
| 5048 | } |
| 5049 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5050 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5051 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5052 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5053 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5054 | |
| 5055 | unlock: |
| 5056 | hci_dev_unlock(hdev); |
| 5057 | return err; |
| 5058 | } |
| 5059 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5060 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5061 | bdaddr_t *bdaddr, u8 type) |
| 5062 | { |
| 5063 | struct mgmt_ev_device_removed ev; |
| 5064 | |
| 5065 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5066 | ev.addr.type = type; |
| 5067 | |
| 5068 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5069 | } |
| 5070 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5071 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5072 | void *data, u16 len) |
| 5073 | { |
| 5074 | struct mgmt_cp_remove_device *cp = data; |
| 5075 | int err; |
| 5076 | |
| 5077 | BT_DBG("%s", hdev->name); |
| 5078 | |
| 5079 | hci_dev_lock(hdev); |
| 5080 | |
| 5081 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame^] | 5082 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5083 | u8 addr_type; |
| 5084 | |
| 5085 | if (!bdaddr_type_is_le(cp->addr.type)) { |
| 5086 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5087 | MGMT_STATUS_INVALID_PARAMS, |
| 5088 | &cp->addr, sizeof(cp->addr)); |
| 5089 | goto unlock; |
| 5090 | } |
| 5091 | |
| 5092 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5093 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5094 | else |
| 5095 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5096 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame^] | 5097 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5098 | addr_type); |
| 5099 | if (!params) { |
| 5100 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5101 | MGMT_STATUS_INVALID_PARAMS, |
| 5102 | &cp->addr, sizeof(cp->addr)); |
| 5103 | goto unlock; |
| 5104 | } |
| 5105 | |
| 5106 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED) { |
| 5107 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5108 | MGMT_STATUS_INVALID_PARAMS, |
| 5109 | &cp->addr, sizeof(cp->addr)); |
| 5110 | goto unlock; |
| 5111 | } |
| 5112 | |
| 5113 | hci_pend_le_conn_del(hdev, &cp->addr.bdaddr, addr_type); |
| 5114 | list_del(¶ms->list); |
| 5115 | kfree(params); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5116 | |
| 5117 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5118 | } else { |
| 5119 | if (cp->addr.type) { |
| 5120 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5121 | MGMT_STATUS_INVALID_PARAMS, |
| 5122 | &cp->addr, sizeof(cp->addr)); |
| 5123 | goto unlock; |
| 5124 | } |
| 5125 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame^] | 5126 | hci_conn_params_clear_enabled(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5127 | } |
| 5128 | |
| 5129 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5130 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5131 | |
| 5132 | unlock: |
| 5133 | hci_dev_unlock(hdev); |
| 5134 | return err; |
| 5135 | } |
| 5136 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5137 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5138 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 5139 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5140 | bool var_len; |
| 5141 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5142 | } mgmt_handlers[] = { |
| 5143 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5144 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 5145 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 5146 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 5147 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 5148 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 5149 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 5150 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 5151 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 5152 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 5153 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 5154 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 5155 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 5156 | { set_le, false, MGMT_SETTING_SIZE }, |
| 5157 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 5158 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 5159 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 5160 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 5161 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 5162 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 5163 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 5164 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 5165 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 5166 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 5167 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 5168 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 5169 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 5170 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 5171 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 5172 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 5173 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 5174 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 5175 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5176 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5177 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 5178 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 5179 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 5180 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 5181 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 5182 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5183 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5184 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5185 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5186 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5187 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5188 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5189 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5190 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5191 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5192 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5193 | { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE }, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5194 | { add_device, false, MGMT_ADD_DEVICE_SIZE }, |
| 5195 | { remove_device, false, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5196 | }; |
| 5197 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5198 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 5199 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5200 | void *buf; |
| 5201 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5202 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 5203 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5204 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5205 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5206 | int err; |
| 5207 | |
| 5208 | BT_DBG("got %zu bytes", msglen); |
| 5209 | |
| 5210 | if (msglen < sizeof(*hdr)) |
| 5211 | return -EINVAL; |
| 5212 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 5213 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5214 | if (!buf) |
| 5215 | return -ENOMEM; |
| 5216 | |
| 5217 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 5218 | err = -EFAULT; |
| 5219 | goto done; |
| 5220 | } |
| 5221 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5222 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5223 | opcode = __le16_to_cpu(hdr->opcode); |
| 5224 | index = __le16_to_cpu(hdr->index); |
| 5225 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5226 | |
| 5227 | if (len != msglen - sizeof(*hdr)) { |
| 5228 | err = -EINVAL; |
| 5229 | goto done; |
| 5230 | } |
| 5231 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5232 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5233 | hdev = hci_dev_get(index); |
| 5234 | if (!hdev) { |
| 5235 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5236 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5237 | goto done; |
| 5238 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5239 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 5240 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 5241 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) || |
| 5242 | test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5243 | err = cmd_status(sk, index, opcode, |
| 5244 | MGMT_STATUS_INVALID_INDEX); |
| 5245 | goto done; |
| 5246 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5247 | } |
| 5248 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5249 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5250 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5251 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5252 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5253 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5254 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5255 | } |
| 5256 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5257 | if ((hdev && opcode < MGMT_OP_READ_INFO) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5258 | (!hdev && opcode >= MGMT_OP_READ_INFO)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5259 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5260 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5261 | goto done; |
| 5262 | } |
| 5263 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5264 | handler = &mgmt_handlers[opcode]; |
| 5265 | |
| 5266 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5267 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5268 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5269 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5270 | goto done; |
| 5271 | } |
| 5272 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5273 | if (hdev) |
| 5274 | mgmt_init_hdev(sk, hdev); |
| 5275 | |
| 5276 | cp = buf + sizeof(*hdr); |
| 5277 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5278 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 5279 | if (err < 0) |
| 5280 | goto done; |
| 5281 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5282 | err = msglen; |
| 5283 | |
| 5284 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5285 | if (hdev) |
| 5286 | hci_dev_put(hdev); |
| 5287 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5288 | kfree(buf); |
| 5289 | return err; |
| 5290 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5291 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5292 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5293 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5294 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5295 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5296 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5297 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5298 | } |
| 5299 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5300 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5301 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 5302 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5303 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5304 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5305 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5306 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5307 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5308 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5309 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5310 | } |
| 5311 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5312 | /* This function requires the caller holds hdev->lock */ |
| 5313 | static void restart_le_auto_conns(struct hci_dev *hdev) |
| 5314 | { |
| 5315 | struct hci_conn_params *p; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5316 | bool added = false; |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5317 | |
| 5318 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5319 | if (p->auto_connect == HCI_AUTO_CONN_ALWAYS) { |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5320 | hci_pend_le_conn_add(hdev, &p->addr, p->addr_type); |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5321 | added = true; |
| 5322 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5323 | } |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5324 | |
| 5325 | /* Calling hci_pend_le_conn_add will actually already trigger |
| 5326 | * background scanning when needed. So no need to trigger it |
| 5327 | * just another time. |
| 5328 | * |
| 5329 | * This check is here to avoid an unneeded restart of the |
| 5330 | * passive scanning. Since this is during the controller |
| 5331 | * power up phase the duplicate filtering is not an issue. |
| 5332 | */ |
| 5333 | if (added) |
| 5334 | return; |
| 5335 | |
| 5336 | hci_update_background_scan(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5337 | } |
| 5338 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5339 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 5340 | { |
| 5341 | struct cmd_lookup match = { NULL, hdev }; |
| 5342 | |
| 5343 | BT_DBG("status 0x%02x", status); |
| 5344 | |
| 5345 | hci_dev_lock(hdev); |
| 5346 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5347 | restart_le_auto_conns(hdev); |
| 5348 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5349 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5350 | |
| 5351 | new_settings(hdev, match.sk); |
| 5352 | |
| 5353 | hci_dev_unlock(hdev); |
| 5354 | |
| 5355 | if (match.sk) |
| 5356 | sock_put(match.sk); |
| 5357 | } |
| 5358 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5359 | static int powered_update_hci(struct hci_dev *hdev) |
| 5360 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5361 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5362 | u8 link_sec; |
| 5363 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5364 | hci_req_init(&req, hdev); |
| 5365 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5366 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 5367 | !lmp_host_ssp_capable(hdev)) { |
| 5368 | u8 ssp = 1; |
| 5369 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5370 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5371 | } |
| 5372 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 5373 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 5374 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5375 | struct hci_cp_write_le_host_supported cp; |
| 5376 | |
| 5377 | cp.le = 1; |
| 5378 | cp.simul = lmp_le_br_capable(hdev); |
| 5379 | |
| 5380 | /* Check first if we already have the right |
| 5381 | * host state (host features set) |
| 5382 | */ |
| 5383 | if (cp.le != lmp_host_le_capable(hdev) || |
| 5384 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5385 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 5386 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5387 | } |
| 5388 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5389 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5390 | /* Make sure the controller has a good default for |
| 5391 | * advertising data. This also applies to the case |
| 5392 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 5393 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5394 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 5395 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5396 | update_scan_rsp_data(&req); |
| 5397 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5398 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 5399 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5400 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 5401 | } |
| 5402 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5403 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 5404 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5405 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 5406 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5407 | |
| 5408 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5409 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 5410 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5411 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5412 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5413 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5414 | } |
| 5415 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5416 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5417 | } |
| 5418 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5419 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5420 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 5421 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5422 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 5423 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5424 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5425 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5426 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 5427 | return 0; |
| 5428 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5429 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5430 | if (powered_update_hci(hdev) == 0) |
| 5431 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 5432 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5433 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 5434 | &match); |
| 5435 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5436 | } |
| 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 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 5440 | |
| 5441 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 5442 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 5443 | zero_cod, sizeof(zero_cod), NULL); |
| 5444 | |
| 5445 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5446 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5447 | |
| 5448 | if (match.sk) |
| 5449 | sock_put(match.sk); |
| 5450 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5451 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5452 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5453 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5454 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5455 | { |
| 5456 | struct pending_cmd *cmd; |
| 5457 | u8 status; |
| 5458 | |
| 5459 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5460 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5461 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5462 | |
| 5463 | if (err == -ERFKILL) |
| 5464 | status = MGMT_STATUS_RFKILLED; |
| 5465 | else |
| 5466 | status = MGMT_STATUS_FAILED; |
| 5467 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5468 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5469 | |
| 5470 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5471 | } |
| 5472 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5473 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 5474 | { |
| 5475 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5476 | |
| 5477 | hci_dev_lock(hdev); |
| 5478 | |
| 5479 | /* When discoverable timeout triggers, then just make sure |
| 5480 | * the limited discoverable flag is cleared. Even in the case |
| 5481 | * of a timeout triggered from general discoverable, it is |
| 5482 | * safe to unconditionally clear the flag. |
| 5483 | */ |
| 5484 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5485 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5486 | |
| 5487 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 5488 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 5489 | u8 scan = SCAN_PAGE; |
| 5490 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 5491 | sizeof(scan), &scan); |
| 5492 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5493 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5494 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5495 | hci_req_run(&req, NULL); |
| 5496 | |
| 5497 | hdev->discov_timeout = 0; |
| 5498 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5499 | new_settings(hdev, NULL); |
| 5500 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5501 | hci_dev_unlock(hdev); |
| 5502 | } |
| 5503 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5504 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5505 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5506 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5507 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5508 | /* Nothing needed here if there's a pending command since that |
| 5509 | * commands request completion callback takes care of everything |
| 5510 | * necessary. |
| 5511 | */ |
| 5512 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5513 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5514 | |
Johan Hedberg | bd10799 | 2014-02-24 14:52:19 +0200 | [diff] [blame] | 5515 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5516 | if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5517 | return; |
| 5518 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5519 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5520 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5521 | } else { |
| 5522 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5523 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5524 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5525 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5526 | if (changed) { |
| 5527 | struct hci_request req; |
| 5528 | |
| 5529 | /* In case this change in discoverable was triggered by |
| 5530 | * a disabling of connectable there could be a need to |
| 5531 | * update the advertising flags. |
| 5532 | */ |
| 5533 | hci_req_init(&req, hdev); |
| 5534 | update_adv_data(&req); |
| 5535 | hci_req_run(&req, NULL); |
| 5536 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5537 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5538 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5539 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5540 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5541 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5542 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5543 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5544 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5545 | /* Nothing needed here if there's a pending command since that |
| 5546 | * commands request completion callback takes care of everything |
| 5547 | * necessary. |
| 5548 | */ |
| 5549 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5550 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5551 | |
Johan Hedberg | ce3f24c | 2014-02-24 14:52:20 +0200 | [diff] [blame] | 5552 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5553 | if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5554 | return; |
| 5555 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5556 | if (connectable) |
| 5557 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 5558 | else |
| 5559 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5560 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5561 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5562 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5563 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5564 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5565 | void mgmt_advertising(struct hci_dev *hdev, u8 advertising) |
| 5566 | { |
Johan Hedberg | 7c4cfab | 2014-02-24 14:52:21 +0200 | [diff] [blame] | 5567 | /* Powering off may stop advertising - don't let that interfere */ |
| 5568 | if (!advertising && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5569 | return; |
| 5570 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5571 | if (advertising) |
| 5572 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5573 | else |
| 5574 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5575 | } |
| 5576 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 5577 | 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] | 5578 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5579 | u8 mgmt_err = mgmt_status(status); |
| 5580 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5581 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5582 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5583 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5584 | |
| 5585 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5586 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5587 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5588 | } |
| 5589 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5590 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 5591 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5592 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 5593 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5594 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5595 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5596 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5597 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 5598 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5599 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5600 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 5601 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5602 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5603 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5604 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5605 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5606 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5607 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 5608 | { |
| 5609 | if (ltk->authenticated) |
| 5610 | return MGMT_LTK_AUTHENTICATED; |
| 5611 | |
| 5612 | return MGMT_LTK_UNAUTHENTICATED; |
| 5613 | } |
| 5614 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5615 | 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] | 5616 | { |
| 5617 | struct mgmt_ev_new_long_term_key ev; |
| 5618 | |
| 5619 | memset(&ev, 0, sizeof(ev)); |
| 5620 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 5621 | /* Devices using resolvable or non-resolvable random addresses |
| 5622 | * without providing an indentity resolving key don't require |
| 5623 | * to store long term keys. Their addresses will change the |
| 5624 | * next time around. |
| 5625 | * |
| 5626 | * Only when a remote device provides an identity address |
| 5627 | * make sure the long term key is stored. If the remote |
| 5628 | * identity is known, the long term keys are internally |
| 5629 | * mapped to the identity address. So allow static random |
| 5630 | * and public addresses here. |
| 5631 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5632 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5633 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5634 | ev.store_hint = 0x00; |
| 5635 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5636 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5637 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5638 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5639 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5640 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5641 | ev.key.enc_size = key->enc_size; |
| 5642 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 5643 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5644 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 5645 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5646 | ev.key.master = 1; |
| 5647 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5648 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 5649 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 5650 | 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] | 5651 | } |
| 5652 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5653 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 5654 | { |
| 5655 | struct mgmt_ev_new_irk ev; |
| 5656 | |
| 5657 | memset(&ev, 0, sizeof(ev)); |
| 5658 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 5659 | /* For identity resolving keys from devices that are already |
| 5660 | * using a public address or static random address, do not |
| 5661 | * ask for storing this key. The identity resolving key really |
| 5662 | * is only mandatory for devices using resovlable random |
| 5663 | * addresses. |
| 5664 | * |
| 5665 | * Storing all identity resolving keys has the downside that |
| 5666 | * they will be also loaded on next boot of they system. More |
| 5667 | * identity resolving keys, means more time during scanning is |
| 5668 | * needed to actually resolve these addresses. |
| 5669 | */ |
| 5670 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 5671 | ev.store_hint = 0x01; |
| 5672 | else |
| 5673 | ev.store_hint = 0x00; |
| 5674 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5675 | bacpy(&ev.rpa, &irk->rpa); |
| 5676 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 5677 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 5678 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 5679 | |
| 5680 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 5681 | } |
| 5682 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5683 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 5684 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5685 | { |
| 5686 | struct mgmt_ev_new_csrk ev; |
| 5687 | |
| 5688 | memset(&ev, 0, sizeof(ev)); |
| 5689 | |
| 5690 | /* Devices using resolvable or non-resolvable random addresses |
| 5691 | * without providing an indentity resolving key don't require |
| 5692 | * to store signature resolving keys. Their addresses will change |
| 5693 | * the next time around. |
| 5694 | * |
| 5695 | * Only when a remote device provides an identity address |
| 5696 | * make sure the signature resolving key is stored. So allow |
| 5697 | * static random and public addresses here. |
| 5698 | */ |
| 5699 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5700 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5701 | ev.store_hint = 0x00; |
| 5702 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5703 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5704 | |
| 5705 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 5706 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 5707 | ev.key.master = csrk->master; |
| 5708 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 5709 | |
| 5710 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 5711 | } |
| 5712 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5713 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5714 | u8 bdaddr_type, u16 min_interval, u16 max_interval, |
| 5715 | u16 latency, u16 timeout) |
| 5716 | { |
| 5717 | struct mgmt_ev_new_conn_param ev; |
| 5718 | |
| 5719 | memset(&ev, 0, sizeof(ev)); |
| 5720 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5721 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
| 5722 | ev.store_hint = 0x00; |
| 5723 | ev.min_interval = cpu_to_le16(min_interval); |
| 5724 | ev.max_interval = cpu_to_le16(max_interval); |
| 5725 | ev.latency = cpu_to_le16(latency); |
| 5726 | ev.timeout = cpu_to_le16(timeout); |
| 5727 | |
| 5728 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 5729 | } |
| 5730 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 5731 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 5732 | u8 data_len) |
| 5733 | { |
| 5734 | eir[eir_len++] = sizeof(type) + data_len; |
| 5735 | eir[eir_len++] = type; |
| 5736 | memcpy(&eir[eir_len], data, data_len); |
| 5737 | eir_len += data_len; |
| 5738 | |
| 5739 | return eir_len; |
| 5740 | } |
| 5741 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5742 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5743 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 5744 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5745 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5746 | char buf[512]; |
| 5747 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 5748 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5749 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5750 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5751 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5752 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 5753 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 5754 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5755 | if (name_len > 0) |
| 5756 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5757 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5758 | |
| 5759 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 5760 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5761 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5762 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5763 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5764 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5765 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 5766 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5767 | } |
| 5768 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5769 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 5770 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 5771 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5772 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 5773 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5774 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5775 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5776 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5777 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5778 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5779 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5780 | |
| 5781 | *sk = cmd->sk; |
| 5782 | sock_hold(*sk); |
| 5783 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5784 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5785 | } |
| 5786 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5787 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5788 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5789 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5790 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 5791 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5792 | |
| 5793 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5794 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5795 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5796 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5797 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 5798 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5799 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5800 | |
| 5801 | mgmt_pending_remove(cmd); |
| 5802 | } |
| 5803 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5804 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5805 | u8 link_type, u8 addr_type, u8 reason, |
| 5806 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5807 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5808 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5809 | struct pending_cmd *power_off; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5810 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5811 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5812 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5813 | if (power_off) { |
| 5814 | struct mgmt_mode *cp = power_off->param; |
| 5815 | |
| 5816 | /* The connection is still in hci_conn_hash so test for 1 |
| 5817 | * instead of 0 to know if this is the last one. |
| 5818 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5819 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5820 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5821 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5822 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5823 | } |
| 5824 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5825 | if (!mgmt_connected) |
| 5826 | return; |
| 5827 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 5828 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 5829 | return; |
| 5830 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5831 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5832 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5833 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5834 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 5835 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5836 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5837 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5838 | |
| 5839 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 5840 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5841 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5842 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5843 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5844 | } |
| 5845 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5846 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5847 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5848 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5849 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 5850 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5851 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5852 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5853 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 5854 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 5855 | hdev); |
| 5856 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5857 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5858 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5859 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5860 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5861 | cp = cmd->param; |
| 5862 | |
| 5863 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 5864 | return; |
| 5865 | |
| 5866 | if (cp->addr.type != bdaddr_type) |
| 5867 | return; |
| 5868 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5869 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5870 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 5871 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5872 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 5873 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5874 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5875 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5876 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5877 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5878 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5879 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5880 | { |
| 5881 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5882 | struct pending_cmd *power_off; |
| 5883 | |
| 5884 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5885 | if (power_off) { |
| 5886 | struct mgmt_mode *cp = power_off->param; |
| 5887 | |
| 5888 | /* The connection is still in hci_conn_hash so test for 1 |
| 5889 | * instead of 0 to know if this is the last one. |
| 5890 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5891 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5892 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5893 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5894 | } |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5895 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5896 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 5897 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5898 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5899 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5900 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5901 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5902 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5903 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 5904 | 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] | 5905 | { |
| 5906 | struct mgmt_ev_pin_code_request ev; |
| 5907 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5908 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5909 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 5910 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5911 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 5912 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5913 | } |
| 5914 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5915 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5916 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5917 | { |
| 5918 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5919 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5920 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5921 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5922 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5923 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5924 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5925 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5926 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5927 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5928 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 5929 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5930 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5931 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5932 | } |
| 5933 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5934 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5935 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5936 | { |
| 5937 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5938 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5939 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5940 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5941 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5942 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5943 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5944 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5945 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5946 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5947 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 5948 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5949 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5950 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5951 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5952 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5953 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 5954 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5955 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5956 | { |
| 5957 | struct mgmt_ev_user_confirm_request ev; |
| 5958 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5959 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5960 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5961 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5962 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 5963 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 5964 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5965 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5966 | 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] | 5967 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5968 | } |
| 5969 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5970 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5971 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5972 | { |
| 5973 | struct mgmt_ev_user_passkey_request ev; |
| 5974 | |
| 5975 | BT_DBG("%s", hdev->name); |
| 5976 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5977 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5978 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5979 | |
| 5980 | 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] | 5981 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5982 | } |
| 5983 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 5984 | 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] | 5985 | u8 link_type, u8 addr_type, u8 status, |
| 5986 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5987 | { |
| 5988 | struct pending_cmd *cmd; |
| 5989 | struct mgmt_rp_user_confirm_reply rp; |
| 5990 | int err; |
| 5991 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5992 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5993 | if (!cmd) |
| 5994 | return -ENOENT; |
| 5995 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5996 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5997 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5998 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5999 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6000 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6001 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6002 | |
| 6003 | return err; |
| 6004 | } |
| 6005 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6006 | 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] | 6007 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6008 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6009 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6010 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6011 | } |
| 6012 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6013 | 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] | 6014 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6015 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6016 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6017 | status, |
| 6018 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6019 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6020 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6021 | 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] | 6022 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6023 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6024 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6025 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6026 | } |
| 6027 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6028 | 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] | 6029 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6030 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6031 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6032 | status, |
| 6033 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6034 | } |
| 6035 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 6036 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6037 | u8 link_type, u8 addr_type, u32 passkey, |
| 6038 | u8 entered) |
| 6039 | { |
| 6040 | struct mgmt_ev_passkey_notify ev; |
| 6041 | |
| 6042 | BT_DBG("%s", hdev->name); |
| 6043 | |
| 6044 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6045 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6046 | ev.passkey = __cpu_to_le32(passkey); |
| 6047 | ev.entered = entered; |
| 6048 | |
| 6049 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 6050 | } |
| 6051 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6052 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6053 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6054 | { |
| 6055 | struct mgmt_ev_auth_failed ev; |
| 6056 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 6057 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6058 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6059 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6060 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6061 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6062 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6063 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6064 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6065 | { |
| 6066 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6067 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6068 | |
| 6069 | if (status) { |
| 6070 | u8 mgmt_err = mgmt_status(status); |
| 6071 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6072 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6073 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6074 | } |
| 6075 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6076 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 6077 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 6078 | &hdev->dev_flags); |
| 6079 | else |
| 6080 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 6081 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6082 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6083 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6084 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6085 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6086 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6087 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6088 | |
| 6089 | if (match.sk) |
| 6090 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6091 | } |
| 6092 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6093 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6094 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6095 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6096 | struct hci_cp_write_eir cp; |
| 6097 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 6098 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6099 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6100 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 6101 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 6102 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6103 | memset(&cp, 0, sizeof(cp)); |
| 6104 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6105 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6106 | } |
| 6107 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6108 | 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] | 6109 | { |
| 6110 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6111 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6112 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6113 | |
| 6114 | if (status) { |
| 6115 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6116 | |
| 6117 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6118 | &hdev->dev_flags)) { |
| 6119 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6120 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6121 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6122 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6123 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 6124 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6125 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6126 | } |
| 6127 | |
| 6128 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6129 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6130 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6131 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 6132 | if (!changed) |
| 6133 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 6134 | &hdev->dev_flags); |
| 6135 | else |
| 6136 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6137 | } |
| 6138 | |
| 6139 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 6140 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6141 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6142 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6143 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 6144 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6145 | sock_put(match.sk); |
| 6146 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6147 | hci_req_init(&req, hdev); |
| 6148 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6149 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 6150 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 6151 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6152 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6153 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6154 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6155 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6156 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6157 | |
| 6158 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6159 | } |
| 6160 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6161 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 6162 | { |
| 6163 | struct cmd_lookup match = { NULL, hdev }; |
| 6164 | bool changed = false; |
| 6165 | |
| 6166 | if (status) { |
| 6167 | u8 mgmt_err = mgmt_status(status); |
| 6168 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6169 | if (enable) { |
| 6170 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 6171 | &hdev->dev_flags)) |
| 6172 | new_settings(hdev, NULL); |
| 6173 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6174 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6175 | |
| 6176 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6177 | cmd_status_rsp, &mgmt_err); |
| 6178 | return; |
| 6179 | } |
| 6180 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6181 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6182 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6183 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6184 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6185 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6186 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6187 | |
| 6188 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6189 | settings_rsp, &match); |
| 6190 | |
| 6191 | if (changed) |
| 6192 | new_settings(hdev, match.sk); |
| 6193 | |
| 6194 | if (match.sk) |
| 6195 | sock_put(match.sk); |
| 6196 | } |
| 6197 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6198 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6199 | { |
| 6200 | struct cmd_lookup *match = data; |
| 6201 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6202 | if (match->sk == NULL) { |
| 6203 | match->sk = cmd->sk; |
| 6204 | sock_hold(match->sk); |
| 6205 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6206 | } |
| 6207 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6208 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 6209 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6210 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6211 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6212 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6213 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 6214 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 6215 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6216 | |
| 6217 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6218 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 6219 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6220 | |
| 6221 | if (match.sk) |
| 6222 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6223 | } |
| 6224 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6225 | 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] | 6226 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6227 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6228 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6229 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6230 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6231 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6232 | |
| 6233 | memset(&ev, 0, sizeof(ev)); |
| 6234 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6235 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6236 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6237 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6238 | if (!cmd) { |
| 6239 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6240 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6241 | /* If this is a HCI command related to powering on the |
| 6242 | * HCI dev don't send any mgmt signals. |
| 6243 | */ |
| 6244 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6245 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6246 | } |
| 6247 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6248 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 6249 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6250 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6251 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6252 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 6253 | u8 *randomizer192, u8 *hash256, |
| 6254 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6255 | { |
| 6256 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6257 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6258 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6259 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6260 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6261 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6262 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6263 | |
| 6264 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6265 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 6266 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6267 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6268 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 6269 | hash256 && randomizer256) { |
| 6270 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6271 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6272 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 6273 | memcpy(rp.randomizer192, randomizer192, |
| 6274 | sizeof(rp.randomizer192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6275 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6276 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
| 6277 | memcpy(rp.randomizer256, randomizer256, |
| 6278 | sizeof(rp.randomizer256)); |
| 6279 | |
| 6280 | cmd_complete(cmd->sk, hdev->id, |
| 6281 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6282 | &rp, sizeof(rp)); |
| 6283 | } else { |
| 6284 | struct mgmt_rp_read_local_oob_data rp; |
| 6285 | |
| 6286 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 6287 | memcpy(rp.randomizer, randomizer192, |
| 6288 | sizeof(rp.randomizer)); |
| 6289 | |
| 6290 | cmd_complete(cmd->sk, hdev->id, |
| 6291 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6292 | &rp, sizeof(rp)); |
| 6293 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6294 | } |
| 6295 | |
| 6296 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6297 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6298 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6299 | 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] | 6300 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 6301 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6302 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6303 | char buf[512]; |
| 6304 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6305 | struct smp_irk *irk; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6306 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6307 | |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6308 | if (!hci_discovery_active(hdev)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6309 | return; |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6310 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6311 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 6312 | * are for the potential CoD field. |
| 6313 | */ |
| 6314 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6315 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 6316 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6317 | memset(buf, 0, sizeof(buf)); |
| 6318 | |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6319 | irk = hci_get_irk(hdev, bdaddr, addr_type); |
| 6320 | if (irk) { |
| 6321 | bacpy(&ev->addr.bdaddr, &irk->bdaddr); |
| 6322 | ev->addr.type = link_to_bdaddr(link_type, irk->addr_type); |
| 6323 | } else { |
| 6324 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 6325 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
| 6326 | } |
| 6327 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6328 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 6329 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6330 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6331 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6332 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6333 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6334 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 6335 | 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] | 6336 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6337 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6338 | if (scan_rsp_len > 0) |
| 6339 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
| 6340 | |
| 6341 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 6342 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 6343 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6344 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6345 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6346 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6347 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6348 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6349 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6350 | struct mgmt_ev_device_found *ev; |
| 6351 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 6352 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6353 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6354 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6355 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6356 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6357 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6358 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6359 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6360 | ev->rssi = rssi; |
| 6361 | |
| 6362 | 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] | 6363 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6364 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6365 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6366 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6367 | 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] | 6368 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6369 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6370 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6371 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6372 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6373 | struct pending_cmd *cmd; |
| 6374 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 6375 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 6376 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6377 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6378 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6379 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6380 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6381 | |
| 6382 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 6383 | u8 type = hdev->discovery.type; |
| 6384 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6385 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 6386 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6387 | mgmt_pending_remove(cmd); |
| 6388 | } |
| 6389 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6390 | memset(&ev, 0, sizeof(ev)); |
| 6391 | ev.type = hdev->discovery.type; |
| 6392 | ev.discovering = discovering; |
| 6393 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6394 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6395 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6396 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6397 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 6398 | { |
| 6399 | BT_DBG("%s status %u", hdev->name, status); |
| 6400 | |
| 6401 | /* Clear the advertising mgmt setting if we failed to re-enable it */ |
| 6402 | if (status) { |
| 6403 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6404 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6405 | } |
| 6406 | } |
| 6407 | |
| 6408 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 6409 | { |
| 6410 | struct hci_request req; |
| 6411 | |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 6412 | if (hci_conn_num(hdev, LE_LINK) > 0) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6413 | return; |
| 6414 | |
| 6415 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6416 | return; |
| 6417 | |
| 6418 | hci_req_init(&req, hdev); |
| 6419 | enable_advertising(&req); |
| 6420 | |
| 6421 | /* If this fails we have no option but to let user space know |
| 6422 | * that we've disabled advertising. |
| 6423 | */ |
| 6424 | if (hci_req_run(&req, adv_enable_complete) < 0) { |
| 6425 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6426 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6427 | } |
| 6428 | } |