blob: 63f42f45a96a3517a9a6b9ccc615c60be15b5429 [file] [log] [blame]
Johan Hedberg03811012010-12-08 00:21:06 +02001/*
2 BlueZ - Bluetooth protocol stack for Linux
Johan Hedbergea585ab2012-02-17 14:50:39 +02003
Johan Hedberg03811012010-12-08 00:21:06 +02004 Copyright (C) 2010 Nokia Corporation
Johan Hedbergea585ab2012-02-17 14:50:39 +02005 Copyright (C) 2011-2012 Intel Corporation
Johan Hedberg03811012010-12-08 00:21:06 +02006
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 Gortmaker3a9a2312011-05-27 09:12:25 -040027#include <linux/module.h>
Johan Hedberg03811012010-12-08 00:21:06 +020028#include <asm/unaligned.h>
29
30#include <net/bluetooth/bluetooth.h>
31#include <net/bluetooth/hci_core.h>
Johan Hedberg71290692015-02-20 13:26:23 +020032#include <net/bluetooth/hci_sock.h>
Johan Hedberg4bc58f52014-05-20 09:45:47 +030033#include <net/bluetooth/l2cap.h>
Johan Hedberg03811012010-12-08 00:21:06 +020034#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070035
Johan Hedberg0857dd32014-12-19 13:40:20 +020036#include "hci_request.h"
Marcel Holtmannac4b7232013-10-10 14:54:16 -070037#include "smp.h"
Johan Hedberga380b6c2015-03-17 13:48:48 +020038#include "mgmt_util.h"
Johan Hedberg03811012010-12-08 00:21:06 +020039
Johan Hedberg2da9c552012-02-17 14:39:28 +020040#define MGMT_VERSION 1
Szymon Janc33102302016-09-18 12:50:07 +020041#define MGMT_REVISION 14
Johan Hedberg02d98122010-12-13 21:07:04 +020042
Johan Hedberge70bb2e2012-02-13 16:59:33 +020043static const u16 mgmt_commands[] = {
44 MGMT_OP_READ_INDEX_LIST,
45 MGMT_OP_READ_INFO,
46 MGMT_OP_SET_POWERED,
47 MGMT_OP_SET_DISCOVERABLE,
48 MGMT_OP_SET_CONNECTABLE,
49 MGMT_OP_SET_FAST_CONNECTABLE,
Johan Hedbergb2939472014-07-30 09:22:23 +030050 MGMT_OP_SET_BONDABLE,
Johan Hedberge70bb2e2012-02-13 16:59:33 +020051 MGMT_OP_SET_LINK_SECURITY,
52 MGMT_OP_SET_SSP,
53 MGMT_OP_SET_HS,
54 MGMT_OP_SET_LE,
55 MGMT_OP_SET_DEV_CLASS,
56 MGMT_OP_SET_LOCAL_NAME,
57 MGMT_OP_ADD_UUID,
58 MGMT_OP_REMOVE_UUID,
59 MGMT_OP_LOAD_LINK_KEYS,
60 MGMT_OP_LOAD_LONG_TERM_KEYS,
61 MGMT_OP_DISCONNECT,
62 MGMT_OP_GET_CONNECTIONS,
63 MGMT_OP_PIN_CODE_REPLY,
64 MGMT_OP_PIN_CODE_NEG_REPLY,
65 MGMT_OP_SET_IO_CAPABILITY,
66 MGMT_OP_PAIR_DEVICE,
67 MGMT_OP_CANCEL_PAIR_DEVICE,
68 MGMT_OP_UNPAIR_DEVICE,
69 MGMT_OP_USER_CONFIRM_REPLY,
70 MGMT_OP_USER_CONFIRM_NEG_REPLY,
71 MGMT_OP_USER_PASSKEY_REPLY,
72 MGMT_OP_USER_PASSKEY_NEG_REPLY,
73 MGMT_OP_READ_LOCAL_OOB_DATA,
74 MGMT_OP_ADD_REMOTE_OOB_DATA,
75 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
76 MGMT_OP_START_DISCOVERY,
77 MGMT_OP_STOP_DISCOVERY,
78 MGMT_OP_CONFIRM_NAME,
79 MGMT_OP_BLOCK_DEVICE,
80 MGMT_OP_UNBLOCK_DEVICE,
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -070081 MGMT_OP_SET_DEVICE_ID,
Johan Hedberg4375f102013-09-25 13:26:10 +030082 MGMT_OP_SET_ADVERTISING,
Johan Hedberg0663ca22013-10-02 13:43:14 +030083 MGMT_OP_SET_BREDR,
Marcel Holtmannd13eafc2013-10-02 04:41:30 -070084 MGMT_OP_SET_STATIC_ADDRESS,
Marcel Holtmann7f72134e2013-10-11 14:44:58 -070085 MGMT_OP_SET_SCAN_PARAMS,
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -080086 MGMT_OP_SET_SECURE_CONN,
Marcel Holtmann4e39ac82014-01-31 11:55:22 -080087 MGMT_OP_SET_DEBUG_KEYS,
Johan Hedberg62b04cd2014-02-23 19:42:27 +020088 MGMT_OP_SET_PRIVACY,
Johan Hedberg41edf162014-02-18 10:19:35 +020089 MGMT_OP_LOAD_IRKS,
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +020090 MGMT_OP_GET_CONN_INFO,
Johan Hedberg95868422014-06-28 17:54:07 +030091 MGMT_OP_GET_CLOCK_INFO,
Marcel Holtmann2faade52014-06-29 19:44:03 +020092 MGMT_OP_ADD_DEVICE,
93 MGMT_OP_REMOVE_DEVICE,
Johan Hedberga26f3dc2014-07-02 17:37:29 +030094 MGMT_OP_LOAD_CONN_PARAM,
Marcel Holtmann73d1df22014-07-02 22:10:52 +020095 MGMT_OP_READ_UNCONF_INDEX_LIST,
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +020096 MGMT_OP_READ_CONFIG_INFO,
Marcel Holtmanndbece372014-07-04 18:11:55 +020097 MGMT_OP_SET_EXTERNAL_CONFIG,
Marcel Holtmann9713c172014-07-06 12:11:15 +020098 MGMT_OP_SET_PUBLIC_ADDRESS,
Jakub Pawlowski66ea9422014-12-05 10:55:59 +010099 MGMT_OP_START_SERVICE_DISCOVERY,
Marcel Holtmann4f0f1552015-03-14 22:43:19 -0700100 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
Marcel Holtmann96f14742015-03-14 19:27:57 -0700101 MGMT_OP_READ_EXT_INDEX_LIST,
Marcel Holtmannd3d53052015-03-14 20:53:25 -0700102 MGMT_OP_READ_ADV_FEATURES,
Arman Uguray24b4f382015-03-23 15:57:12 -0700103 MGMT_OP_ADD_ADVERTISING,
Arman Ugurayda9293352015-03-23 15:57:13 -0700104 MGMT_OP_REMOVE_ADVERTISING,
Marcel Holtmann40b25fe2015-11-19 16:16:43 +0100105 MGMT_OP_GET_ADV_SIZE_INFO,
Johan Hedberg78b781c2016-01-05 13:19:32 +0200106 MGMT_OP_START_LIMITED_DISCOVERY,
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200107 MGMT_OP_READ_EXT_INFO,
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +0200108 MGMT_OP_SET_APPEARANCE,
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200109};
110
111static const u16 mgmt_events[] = {
112 MGMT_EV_CONTROLLER_ERROR,
113 MGMT_EV_INDEX_ADDED,
114 MGMT_EV_INDEX_REMOVED,
115 MGMT_EV_NEW_SETTINGS,
116 MGMT_EV_CLASS_OF_DEV_CHANGED,
117 MGMT_EV_LOCAL_NAME_CHANGED,
118 MGMT_EV_NEW_LINK_KEY,
119 MGMT_EV_NEW_LONG_TERM_KEY,
120 MGMT_EV_DEVICE_CONNECTED,
121 MGMT_EV_DEVICE_DISCONNECTED,
122 MGMT_EV_CONNECT_FAILED,
123 MGMT_EV_PIN_CODE_REQUEST,
124 MGMT_EV_USER_CONFIRM_REQUEST,
125 MGMT_EV_USER_PASSKEY_REQUEST,
126 MGMT_EV_AUTH_FAILED,
127 MGMT_EV_DEVICE_FOUND,
128 MGMT_EV_DISCOVERING,
129 MGMT_EV_DEVICE_BLOCKED,
130 MGMT_EV_DEVICE_UNBLOCKED,
131 MGMT_EV_DEVICE_UNPAIRED,
Johan Hedberg92a25252012-09-06 18:39:26 +0300132 MGMT_EV_PASSKEY_NOTIFY,
Marcel Holtmann1b60ef22014-02-21 21:35:30 -0800133 MGMT_EV_NEW_IRK,
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700134 MGMT_EV_NEW_CSRK,
Marcel Holtmann8afef092014-06-29 22:28:34 +0200135 MGMT_EV_DEVICE_ADDED,
136 MGMT_EV_DEVICE_REMOVED,
Andre Guedesffb5a8272014-07-01 18:10:11 -0300137 MGMT_EV_NEW_CONN_PARAM,
Marcel Holtmann0602a8a2014-07-02 21:30:54 +0200138 MGMT_EV_UNCONF_INDEX_ADDED,
Marcel Holtmannedd38962014-07-02 21:30:55 +0200139 MGMT_EV_UNCONF_INDEX_REMOVED,
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200140 MGMT_EV_NEW_CONFIG_OPTIONS,
Marcel Holtmannced85542015-03-14 19:27:56 -0700141 MGMT_EV_EXT_INDEX_ADDED,
142 MGMT_EV_EXT_INDEX_REMOVED,
Marcel Holtmann72000df2015-03-16 16:11:21 -0700143 MGMT_EV_LOCAL_OOB_DATA_UPDATED,
Arman Uguray24b4f382015-03-23 15:57:12 -0700144 MGMT_EV_ADVERTISING_ADDED,
145 MGMT_EV_ADVERTISING_REMOVED,
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200146 MGMT_EV_EXT_INFO_CHANGED,
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200147};
148
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700149static const u16 mgmt_untrusted_commands[] = {
150 MGMT_OP_READ_INDEX_LIST,
151 MGMT_OP_READ_INFO,
152 MGMT_OP_READ_UNCONF_INDEX_LIST,
153 MGMT_OP_READ_CONFIG_INFO,
154 MGMT_OP_READ_EXT_INDEX_LIST,
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200155 MGMT_OP_READ_EXT_INFO,
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700156};
157
158static const u16 mgmt_untrusted_events[] = {
159 MGMT_EV_INDEX_ADDED,
160 MGMT_EV_INDEX_REMOVED,
161 MGMT_EV_NEW_SETTINGS,
162 MGMT_EV_CLASS_OF_DEV_CHANGED,
163 MGMT_EV_LOCAL_NAME_CHANGED,
164 MGMT_EV_UNCONF_INDEX_ADDED,
165 MGMT_EV_UNCONF_INDEX_REMOVED,
166 MGMT_EV_NEW_CONFIG_OPTIONS,
167 MGMT_EV_EXT_INDEX_ADDED,
168 MGMT_EV_EXT_INDEX_REMOVED,
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200169 MGMT_EV_EXT_INFO_CHANGED,
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700170};
171
Marcel Holtmann17b02e62012-03-01 14:32:37 -0800172#define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)
Johan Hedberg7d785252011-12-15 00:47:39 +0200173
Johan Hedbergd25b78e2015-01-27 12:55:52 +0200174#define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
175 "\x00\x00\x00\x00\x00\x00\x00\x00"
176
Johan Hedbergca69b792011-11-11 18:10:00 +0200177/* HCI to MGMT error code conversion table */
178static u8 mgmt_status_table[] = {
179 MGMT_STATUS_SUCCESS,
180 MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */
181 MGMT_STATUS_NOT_CONNECTED, /* No Connection */
182 MGMT_STATUS_FAILED, /* Hardware Failure */
183 MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */
184 MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */
Johan Hedbergeadd6632014-01-13 17:15:53 +0200185 MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */
Johan Hedbergca69b792011-11-11 18:10:00 +0200186 MGMT_STATUS_NO_RESOURCES, /* Memory Full */
187 MGMT_STATUS_TIMEOUT, /* Connection Timeout */
188 MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */
189 MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */
190 MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */
191 MGMT_STATUS_BUSY, /* Command Disallowed */
192 MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */
193 MGMT_STATUS_REJECTED, /* Rejected Security */
194 MGMT_STATUS_REJECTED, /* Rejected Personal */
195 MGMT_STATUS_TIMEOUT, /* Host Timeout */
196 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */
197 MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */
198 MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */
199 MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */
200 MGMT_STATUS_DISCONNECTED, /* OE Power Off */
201 MGMT_STATUS_DISCONNECTED, /* Connection Terminated */
202 MGMT_STATUS_BUSY, /* Repeated Attempts */
203 MGMT_STATUS_REJECTED, /* Pairing Not Allowed */
204 MGMT_STATUS_FAILED, /* Unknown LMP PDU */
205 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */
206 MGMT_STATUS_REJECTED, /* SCO Offset Rejected */
207 MGMT_STATUS_REJECTED, /* SCO Interval Rejected */
208 MGMT_STATUS_REJECTED, /* Air Mode Rejected */
209 MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */
210 MGMT_STATUS_FAILED, /* Unspecified Error */
211 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */
212 MGMT_STATUS_FAILED, /* Role Change Not Allowed */
213 MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */
214 MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */
215 MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */
216 MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */
217 MGMT_STATUS_FAILED, /* Unit Link Key Used */
218 MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */
219 MGMT_STATUS_TIMEOUT, /* Instant Passed */
220 MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */
221 MGMT_STATUS_FAILED, /* Transaction Collision */
222 MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */
223 MGMT_STATUS_REJECTED, /* QoS Rejected */
224 MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */
225 MGMT_STATUS_REJECTED, /* Insufficient Security */
226 MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */
227 MGMT_STATUS_BUSY, /* Role Switch Pending */
228 MGMT_STATUS_FAILED, /* Slot Violation */
229 MGMT_STATUS_FAILED, /* Role Switch Failed */
230 MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */
231 MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */
232 MGMT_STATUS_BUSY, /* Host Busy Pairing */
233 MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */
234 MGMT_STATUS_BUSY, /* Controller Busy */
235 MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */
236 MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */
237 MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */
238 MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */
239 MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */
240};
241
242static u8 mgmt_status(u8 hci_status)
243{
244 if (hci_status < ARRAY_SIZE(mgmt_status_table))
245 return mgmt_status_table[hci_status];
246
247 return MGMT_STATUS_FAILED;
248}
249
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700250static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data,
251 u16 len, int flag)
Marcel Holtmannf9207332015-03-14 19:27:55 -0700252{
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700253 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
254 flag, NULL);
Marcel Holtmannf9207332015-03-14 19:27:55 -0700255}
256
Marcel Holtmann72000df2015-03-16 16:11:21 -0700257static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data,
258 u16 len, int flag, struct sock *skip_sk)
259{
260 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
261 flag, skip_sk);
262}
263
Johan Hedberg7a00ff42015-03-06 21:08:56 +0200264static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len,
265 struct sock *skip_sk)
266{
267 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700268 HCI_SOCK_TRUSTED, skip_sk);
Johan Hedberg7a00ff42015-03-06 21:08:56 +0200269}
270
Johan Hedberg85813a72015-10-21 18:02:59 +0300271static u8 le_addr_type(u8 mgmt_addr_type)
272{
273 if (mgmt_addr_type == BDADDR_LE_PUBLIC)
274 return ADDR_LE_DEV_PUBLIC;
275 else
276 return ADDR_LE_DEV_RANDOM;
277}
278
Marcel Holtmann03c979c2016-08-27 20:23:39 +0200279void mgmt_fill_version_info(void *ver)
280{
281 struct mgmt_rp_read_version *rp = ver;
282
283 rp->version = MGMT_VERSION;
284 rp->revision = cpu_to_le16(MGMT_REVISION);
285}
286
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300287static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
288 u16 data_len)
Johan Hedberga38528f2011-01-22 06:46:43 +0200289{
290 struct mgmt_rp_read_version rp;
291
292 BT_DBG("sock %p", sk);
293
Marcel Holtmann03c979c2016-08-27 20:23:39 +0200294 mgmt_fill_version_info(&rp);
Johan Hedberga38528f2011-01-22 06:46:43 +0200295
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200296 return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0,
297 &rp, sizeof(rp));
Johan Hedberga38528f2011-01-22 06:46:43 +0200298}
299
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300300static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
301 u16 data_len)
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200302{
303 struct mgmt_rp_read_commands *rp;
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700304 u16 num_commands, num_events;
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200305 size_t rp_size;
306 int i, err;
307
308 BT_DBG("sock %p", sk);
309
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700310 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
311 num_commands = ARRAY_SIZE(mgmt_commands);
312 num_events = ARRAY_SIZE(mgmt_events);
313 } else {
314 num_commands = ARRAY_SIZE(mgmt_untrusted_commands);
315 num_events = ARRAY_SIZE(mgmt_untrusted_events);
316 }
317
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200318 rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16));
319
320 rp = kmalloc(rp_size, GFP_KERNEL);
321 if (!rp)
322 return -ENOMEM;
323
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700324 rp->num_commands = cpu_to_le16(num_commands);
325 rp->num_events = cpu_to_le16(num_events);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200326
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700327 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
328 __le16 *opcode = rp->opcodes;
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200329
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700330 for (i = 0; i < num_commands; i++, opcode++)
331 put_unaligned_le16(mgmt_commands[i], opcode);
332
333 for (i = 0; i < num_events; i++, opcode++)
334 put_unaligned_le16(mgmt_events[i], opcode);
335 } else {
336 __le16 *opcode = rp->opcodes;
337
338 for (i = 0; i < num_commands; i++, opcode++)
339 put_unaligned_le16(mgmt_untrusted_commands[i], opcode);
340
341 for (i = 0; i < num_events; i++, opcode++)
342 put_unaligned_le16(mgmt_untrusted_events[i], opcode);
343 }
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200344
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200345 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0,
346 rp, rp_size);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200347 kfree(rp);
348
349 return err;
350}
351
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300352static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
353 u16 data_len)
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200354{
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200355 struct mgmt_rp_read_index_list *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200356 struct hci_dev *d;
Johan Hedberga38528f2011-01-22 06:46:43 +0200357 size_t rp_len;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200358 u16 count;
Johan Hedberg476e44c2012-10-19 20:10:46 +0300359 int err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200360
361 BT_DBG("sock %p", sk);
362
363 read_lock(&hci_dev_list_lock);
364
365 count = 0;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +0300366 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200367 if (d->dev_type == HCI_PRIMARY &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700368 !hci_dev_test_flag(d, HCI_UNCONFIGURED))
Marcel Holtmann1514b892013-10-06 08:25:01 -0700369 count++;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200370 }
371
Johan Hedberga38528f2011-01-22 06:46:43 +0200372 rp_len = sizeof(*rp) + (2 * count);
373 rp = kmalloc(rp_len, GFP_ATOMIC);
374 if (!rp) {
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100375 read_unlock(&hci_dev_list_lock);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200376 return -ENOMEM;
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100377 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200378
Johan Hedberg476e44c2012-10-19 20:10:46 +0300379 count = 0;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200380 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700381 if (hci_dev_test_flag(d, HCI_SETUP) ||
382 hci_dev_test_flag(d, HCI_CONFIG) ||
383 hci_dev_test_flag(d, HCI_USER_CHANNEL))
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200384 continue;
385
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200386 /* Devices marked as raw-only are neither configured
387 * nor unconfigured controllers.
388 */
389 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
Marcel Holtmann0736cfa2013-08-26 21:40:51 -0700390 continue;
391
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200392 if (d->dev_type == HCI_PRIMARY &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700393 !hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
Marcel Holtmann1514b892013-10-06 08:25:01 -0700394 rp->index[count++] = cpu_to_le16(d->id);
395 BT_DBG("Added hci%u", d->id);
396 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200397 }
398
Johan Hedberg476e44c2012-10-19 20:10:46 +0300399 rp->num_controllers = cpu_to_le16(count);
400 rp_len = sizeof(*rp) + (2 * count);
401
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200402 read_unlock(&hci_dev_list_lock);
403
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200404 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST,
405 0, rp, rp_len);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200406
Johan Hedberga38528f2011-01-22 06:46:43 +0200407 kfree(rp);
408
409 return err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200410}
411
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200412static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
413 void *data, u16 data_len)
414{
415 struct mgmt_rp_read_unconf_index_list *rp;
416 struct hci_dev *d;
417 size_t rp_len;
418 u16 count;
419 int err;
420
421 BT_DBG("sock %p", sk);
422
423 read_lock(&hci_dev_list_lock);
424
425 count = 0;
426 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200427 if (d->dev_type == HCI_PRIMARY &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700428 hci_dev_test_flag(d, HCI_UNCONFIGURED))
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200429 count++;
430 }
431
432 rp_len = sizeof(*rp) + (2 * count);
433 rp = kmalloc(rp_len, GFP_ATOMIC);
434 if (!rp) {
435 read_unlock(&hci_dev_list_lock);
436 return -ENOMEM;
437 }
438
439 count = 0;
440 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700441 if (hci_dev_test_flag(d, HCI_SETUP) ||
442 hci_dev_test_flag(d, HCI_CONFIG) ||
443 hci_dev_test_flag(d, HCI_USER_CHANNEL))
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200444 continue;
445
446 /* Devices marked as raw-only are neither configured
447 * nor unconfigured controllers.
448 */
449 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
450 continue;
451
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200452 if (d->dev_type == HCI_PRIMARY &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700453 hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200454 rp->index[count++] = cpu_to_le16(d->id);
455 BT_DBG("Added hci%u", d->id);
456 }
457 }
458
459 rp->num_controllers = cpu_to_le16(count);
460 rp_len = sizeof(*rp) + (2 * count);
461
462 read_unlock(&hci_dev_list_lock);
463
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200464 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
465 MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len);
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200466
467 kfree(rp);
468
469 return err;
470}
471
Marcel Holtmann96f14742015-03-14 19:27:57 -0700472static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev,
473 void *data, u16 data_len)
474{
475 struct mgmt_rp_read_ext_index_list *rp;
476 struct hci_dev *d;
477 size_t rp_len;
478 u16 count;
479 int err;
480
481 BT_DBG("sock %p", sk);
482
483 read_lock(&hci_dev_list_lock);
484
485 count = 0;
486 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200487 if (d->dev_type == HCI_PRIMARY || d->dev_type == HCI_AMP)
Marcel Holtmann96f14742015-03-14 19:27:57 -0700488 count++;
489 }
490
491 rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
492 rp = kmalloc(rp_len, GFP_ATOMIC);
493 if (!rp) {
494 read_unlock(&hci_dev_list_lock);
495 return -ENOMEM;
496 }
497
498 count = 0;
499 list_for_each_entry(d, &hci_dev_list, list) {
500 if (hci_dev_test_flag(d, HCI_SETUP) ||
501 hci_dev_test_flag(d, HCI_CONFIG) ||
502 hci_dev_test_flag(d, HCI_USER_CHANNEL))
503 continue;
504
505 /* Devices marked as raw-only are neither configured
506 * nor unconfigured controllers.
507 */
508 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
509 continue;
510
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200511 if (d->dev_type == HCI_PRIMARY) {
Marcel Holtmann96f14742015-03-14 19:27:57 -0700512 if (hci_dev_test_flag(d, HCI_UNCONFIGURED))
513 rp->entry[count].type = 0x01;
514 else
515 rp->entry[count].type = 0x00;
516 } else if (d->dev_type == HCI_AMP) {
517 rp->entry[count].type = 0x02;
518 } else {
519 continue;
520 }
521
522 rp->entry[count].bus = d->bus;
523 rp->entry[count++].index = cpu_to_le16(d->id);
524 BT_DBG("Added hci%u", d->id);
525 }
526
527 rp->num_controllers = cpu_to_le16(count);
528 rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
529
530 read_unlock(&hci_dev_list_lock);
531
532 /* If this command is called at least once, then all the
533 * default index and unconfigured index events are disabled
534 * and from now on only extended index events are used.
535 */
536 hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS);
537 hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS);
538 hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
539
540 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
541 MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, rp_len);
542
543 kfree(rp);
544
545 return err;
546}
547
Marcel Holtmanndbece372014-07-04 18:11:55 +0200548static bool is_configured(struct hci_dev *hdev)
549{
550 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700551 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
Marcel Holtmanndbece372014-07-04 18:11:55 +0200552 return false;
553
554 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
555 !bacmp(&hdev->public_addr, BDADDR_ANY))
556 return false;
557
558 return true;
559}
560
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200561static __le32 get_missing_options(struct hci_dev *hdev)
562{
563 u32 options = 0;
564
Marcel Holtmanndbece372014-07-04 18:11:55 +0200565 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700566 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200567 options |= MGMT_OPTION_EXTERNAL_CONFIG;
568
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200569 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
570 !bacmp(&hdev->public_addr, BDADDR_ANY))
571 options |= MGMT_OPTION_PUBLIC_ADDRESS;
572
573 return cpu_to_le32(options);
574}
575
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200576static int new_options(struct hci_dev *hdev, struct sock *skip)
577{
578 __le32 options = get_missing_options(hdev);
579
Marcel Holtmann5504c3a2016-08-29 06:19:46 +0200580 return mgmt_limited_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options,
581 sizeof(options), HCI_MGMT_OPTION_EVENTS, skip);
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200582}
583
Marcel Holtmanndbece372014-07-04 18:11:55 +0200584static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
585{
586 __le32 options = get_missing_options(hdev);
587
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200588 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options,
589 sizeof(options));
Marcel Holtmanndbece372014-07-04 18:11:55 +0200590}
591
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200592static int read_config_info(struct sock *sk, struct hci_dev *hdev,
593 void *data, u16 data_len)
594{
595 struct mgmt_rp_read_config_info rp;
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200596 u32 options = 0;
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200597
598 BT_DBG("sock %p %s", sk, hdev->name);
599
600 hci_dev_lock(hdev);
601
602 memset(&rp, 0, sizeof(rp));
603 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200604
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200605 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
606 options |= MGMT_OPTION_EXTERNAL_CONFIG;
607
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200608 if (hdev->set_bdaddr)
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200609 options |= MGMT_OPTION_PUBLIC_ADDRESS;
610
611 rp.supported_options = cpu_to_le32(options);
612 rp.missing_options = get_missing_options(hdev);
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200613
614 hci_dev_unlock(hdev);
615
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200616 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0,
617 &rp, sizeof(rp));
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200618}
619
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200620static u32 get_supported_settings(struct hci_dev *hdev)
Johan Hedberg03811012010-12-08 00:21:06 +0200621{
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200622 u32 settings = 0;
Johan Hedberg03811012010-12-08 00:21:06 +0200623
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200624 settings |= MGMT_SETTING_POWERED;
Johan Hedbergb2939472014-07-30 09:22:23 +0300625 settings |= MGMT_SETTING_BONDABLE;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800626 settings |= MGMT_SETTING_DEBUG_KEYS;
Johan Hedberg3742abf2014-07-08 16:07:34 +0300627 settings |= MGMT_SETTING_CONNECTABLE;
628 settings |= MGMT_SETTING_DISCOVERABLE;
Johan Hedberg03811012010-12-08 00:21:06 +0200629
Andre Guedesed3fa312012-07-24 15:03:46 -0300630 if (lmp_bredr_capable(hdev)) {
Johan Hedberg1a47aee2013-03-15 17:07:06 -0500631 if (hdev->hci_ver >= BLUETOOTH_VER_1_2)
632 settings |= MGMT_SETTING_FAST_CONNECTABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200633 settings |= MGMT_SETTING_BREDR;
634 settings |= MGMT_SETTING_LINK_SECURITY;
Marcel Holtmanna82974c2013-10-11 09:48:47 -0700635
636 if (lmp_ssp_capable(hdev)) {
637 settings |= MGMT_SETTING_SSP;
638 settings |= MGMT_SETTING_HS;
639 }
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800640
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -0800641 if (lmp_sc_capable(hdev))
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800642 settings |= MGMT_SETTING_SECURE_CONN;
Marcel Holtmann848566b2013-10-01 22:59:22 -0700643 }
Marcel Holtmannd7b7e792012-02-20 21:47:49 +0100644
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300645 if (lmp_le_capable(hdev)) {
Marcel Holtmann9d428202012-05-03 07:12:31 +0200646 settings |= MGMT_SETTING_LE;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300647 settings |= MGMT_SETTING_ADVERTISING;
Johan Hedberga3209692014-05-26 11:23:35 +0300648 settings |= MGMT_SETTING_SECURE_CONN;
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200649 settings |= MGMT_SETTING_PRIVACY;
Marcel Holtmann93690c22015-03-06 10:11:21 -0800650 settings |= MGMT_SETTING_STATIC_ADDRESS;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300651 }
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200652
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200653 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
654 hdev->set_bdaddr)
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200655 settings |= MGMT_SETTING_CONFIGURATION;
656
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200657 return settings;
658}
Johan Hedbergebc99fe2011-01-04 11:54:26 +0200659
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200660static u32 get_current_settings(struct hci_dev *hdev)
661{
662 u32 settings = 0;
Johan Hedbergdc4fe302011-03-16 14:29:36 +0200663
Johan Hedbergf1f0eb02012-02-21 17:15:41 +0200664 if (hdev_is_powered(hdev))
Marcel Holtmannf0d4b782012-02-21 12:14:25 +0100665 settings |= MGMT_SETTING_POWERED;
666
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700667 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200668 settings |= MGMT_SETTING_CONNECTABLE;
669
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700670 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
Johan Hedberg1a4d3c42013-03-15 17:07:08 -0500671 settings |= MGMT_SETTING_FAST_CONNECTABLE;
672
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700673 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200674 settings |= MGMT_SETTING_DISCOVERABLE;
675
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700676 if (hci_dev_test_flag(hdev, HCI_BONDABLE))
Johan Hedbergb2939472014-07-30 09:22:23 +0300677 settings |= MGMT_SETTING_BONDABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200678
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700679 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200680 settings |= MGMT_SETTING_BREDR;
681
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700682 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200683 settings |= MGMT_SETTING_LE;
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200684
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700685 if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200686 settings |= MGMT_SETTING_LINK_SECURITY;
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200687
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700688 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200689 settings |= MGMT_SETTING_SSP;
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200690
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700691 if (hci_dev_test_flag(hdev, HCI_HS_ENABLED))
Johan Hedberg6d80dfd2012-02-20 23:50:38 +0200692 settings |= MGMT_SETTING_HS;
693
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700694 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300695 settings |= MGMT_SETTING_ADVERTISING;
696
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700697 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800698 settings |= MGMT_SETTING_SECURE_CONN;
699
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700700 if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS))
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800701 settings |= MGMT_SETTING_DEBUG_KEYS;
702
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700703 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200704 settings |= MGMT_SETTING_PRIVACY;
705
Marcel Holtmann93690c22015-03-06 10:11:21 -0800706 /* The current setting for static address has two purposes. The
707 * first is to indicate if the static address will be used and
708 * the second is to indicate if it is actually set.
709 *
710 * This means if the static address is not configured, this flag
Marcel Holtmann08dc0e92015-03-25 18:32:13 -0700711 * will never be set. If the address is configured, then if the
Marcel Holtmann93690c22015-03-06 10:11:21 -0800712 * address is actually used decides if the flag is set or not.
713 *
714 * For single mode LE only controllers and dual-mode controllers
715 * with BR/EDR disabled, the existence of the static address will
716 * be evaluated.
717 */
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -0700718 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700719 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
Marcel Holtmann93690c22015-03-06 10:11:21 -0800720 !bacmp(&hdev->bdaddr, BDADDR_ANY)) {
721 if (bacmp(&hdev->static_addr, BDADDR_ANY))
722 settings |= MGMT_SETTING_STATIC_ADDRESS;
723 }
724
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200725 return settings;
Johan Hedbergc542a062011-01-26 13:11:03 +0200726}
727
Johan Hedberg333ae952015-03-17 13:48:47 +0200728static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev)
729{
730 return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev);
731}
732
Johan Hedberg333ae952015-03-17 13:48:47 +0200733static struct mgmt_pending_cmd *pending_find_data(u16 opcode,
734 struct hci_dev *hdev,
735 const void *data)
736{
737 return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data);
738}
739
Johan Hedbergf2252572015-11-18 12:49:20 +0200740u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev)
Johan Hedberg9a43e252013-10-20 19:00:07 +0300741{
Johan Hedberg3b0602c2015-03-06 21:08:55 +0200742 struct mgmt_pending_cmd *cmd;
Johan Hedberg9a43e252013-10-20 19:00:07 +0300743
744 /* If there's a pending mgmt command the flags will not yet have
745 * their final values, so check for this first.
746 */
Johan Hedberg333ae952015-03-17 13:48:47 +0200747 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg9a43e252013-10-20 19:00:07 +0300748 if (cmd) {
749 struct mgmt_mode *cp = cmd->param;
750 if (cp->val == 0x01)
751 return LE_AD_GENERAL;
752 else if (cp->val == 0x02)
753 return LE_AD_LIMITED;
754 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700755 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300756 return LE_AD_LIMITED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700757 else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300758 return LE_AD_GENERAL;
759 }
760
761 return 0;
762}
763
Johan Hedbergf2252572015-11-18 12:49:20 +0200764bool mgmt_get_connectable(struct hci_dev *hdev)
Arman Uguraye7a685d2015-03-25 18:53:40 -0700765{
766 struct mgmt_pending_cmd *cmd;
767
768 /* If there's a pending mgmt command the flag will not yet have
769 * it's final value, so check for this first.
770 */
771 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
772 if (cmd) {
773 struct mgmt_mode *cp = cmd->param;
774
775 return cp->val;
776 }
777
778 return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
779}
780
Johan Hedberg7d785252011-12-15 00:47:39 +0200781static void service_cache_off(struct work_struct *work)
782{
783 struct hci_dev *hdev = container_of(work, struct hci_dev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300784 service_cache.work);
Johan Hedberg890ea892013-03-15 17:06:52 -0500785 struct hci_request req;
Johan Hedberg7d785252011-12-15 00:47:39 +0200786
Marcel Holtmanna69d8922015-03-13 02:11:05 -0700787 if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg7d785252011-12-15 00:47:39 +0200788 return;
789
Johan Hedberg890ea892013-03-15 17:06:52 -0500790 hci_req_init(&req, hdev);
791
Johan Hedberg7d785252011-12-15 00:47:39 +0200792 hci_dev_lock(hdev);
793
Johan Hedbergb1a89172015-11-25 16:15:42 +0200794 __hci_req_update_eir(&req);
Johan Hedberg14bf5ea2015-11-22 19:00:22 +0200795 __hci_req_update_class(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +0200796
797 hci_dev_unlock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -0500798
799 hci_req_run(&req, NULL);
Johan Hedberg7d785252011-12-15 00:47:39 +0200800}
801
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200802static void rpa_expired(struct work_struct *work)
803{
804 struct hci_dev *hdev = container_of(work, struct hci_dev,
805 rpa_expired.work);
806 struct hci_request req;
807
808 BT_DBG("");
809
Marcel Holtmanna1536da2015-03-13 02:11:01 -0700810 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200811
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700812 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200813 return;
814
815 /* The generation of a new RPA and programming it into the
Johan Hedbergf2252572015-11-18 12:49:20 +0200816 * controller happens in the hci_req_enable_advertising()
817 * function.
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200818 */
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200819 hci_req_init(&req, hdev);
Johan Hedbergf2252572015-11-18 12:49:20 +0200820 __hci_req_enable_advertising(&req);
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200821 hci_req_run(&req, NULL);
822}
823
Johan Hedberg6a919082012-02-28 06:17:26 +0200824static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
Johan Hedberg7d785252011-12-15 00:47:39 +0200825{
Marcel Holtmann238be782015-03-13 02:11:06 -0700826 if (hci_dev_test_and_set_flag(hdev, HCI_MGMT))
Johan Hedberg6a919082012-02-28 06:17:26 +0200827 return;
828
Johan Hedberg4f87da82012-03-02 19:55:56 +0200829 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200830 INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
Johan Hedberg7d785252011-12-15 00:47:39 +0200831
Johan Hedberg4f87da82012-03-02 19:55:56 +0200832 /* Non-mgmt controlled devices get this bit set
833 * implicitly so that pairing works for them, however
834 * for mgmt we require user-space to explicitly enable
835 * it
836 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -0700837 hci_dev_clear_flag(hdev, HCI_BONDABLE);
Johan Hedberg7d785252011-12-15 00:47:39 +0200838}
839
Johan Hedberg0f4e68c2012-02-28 17:18:30 +0200840static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300841 void *data, u16 data_len)
Johan Hedberg03811012010-12-08 00:21:06 +0200842{
843 struct mgmt_rp_read_info rp;
Johan Hedberg03811012010-12-08 00:21:06 +0200844
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200845 BT_DBG("sock %p %s", sk, hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +0200846
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300847 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +0200848
Johan Hedberg03811012010-12-08 00:21:06 +0200849 memset(&rp, 0, sizeof(rp));
850
Johan Hedberg03811012010-12-08 00:21:06 +0200851 bacpy(&rp.bdaddr, &hdev->bdaddr);
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200852
853 rp.version = hdev->hci_ver;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200854 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200855
856 rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
857 rp.current_settings = cpu_to_le32(get_current_settings(hdev));
858
859 memcpy(rp.dev_class, hdev->dev_class, 3);
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200860
861 memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
Johan Hedberg27fcc362012-02-22 21:46:22 +0200862 memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200863
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300864 hci_dev_unlock(hdev);
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200865
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200866 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
867 sizeof(rp));
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200868}
869
MichaƂ Narajowski8a0c9f42016-09-01 16:46:24 +0200870static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
871 u8 data_len)
872{
873 eir[eir_len++] = sizeof(type) + data_len;
874 eir[eir_len++] = type;
875 memcpy(&eir[eir_len], data, data_len);
876 eir_len += data_len;
877
878 return eir_len;
879}
880
Szymon Janc6a9e90b2016-09-19 20:25:54 +0200881static inline u16 eir_append_le16(u8 *eir, u16 eir_len, u8 type, u16 data)
882{
883 eir[eir_len++] = sizeof(type) + sizeof(data);
884 eir[eir_len++] = type;
885 put_unaligned_le16(data, &eir[eir_len]);
886 eir_len += sizeof(data);
887
888 return eir_len;
889}
890
MichaƂ Narajowskicde7a862016-09-19 20:25:53 +0200891static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir)
892{
893 u16 eir_len = 0;
894 size_t name_len;
895
896 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
897 eir_len = eir_append_data(eir, eir_len, EIR_CLASS_OF_DEV,
898 hdev->dev_class, 3);
899
Szymon Janc6a9e90b2016-09-19 20:25:54 +0200900 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
901 eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE,
902 hdev->appearance);
903
MichaƂ Narajowskicde7a862016-09-19 20:25:53 +0200904 name_len = strlen(hdev->dev_name);
905 eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE,
906 hdev->dev_name, name_len);
907
908 name_len = strlen(hdev->short_name);
909 eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT,
910 hdev->short_name, name_len);
911
912 return eir_len;
913}
914
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200915static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev,
916 void *data, u16 data_len)
917{
Szymon Janc7d5c11d2016-09-19 20:25:52 +0200918 char buf[512];
919 struct mgmt_rp_read_ext_info *rp = (void *)buf;
MichaƂ Narajowskicde7a862016-09-19 20:25:53 +0200920 u16 eir_len;
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200921
922 BT_DBG("sock %p %s", sk, hdev->name);
923
Szymon Janc7d5c11d2016-09-19 20:25:52 +0200924 memset(&buf, 0, sizeof(buf));
925
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200926 hci_dev_lock(hdev);
927
MichaƂ Narajowski8a0c9f42016-09-01 16:46:24 +0200928 bacpy(&rp->bdaddr, &hdev->bdaddr);
929
930 rp->version = hdev->hci_ver;
931 rp->manufacturer = cpu_to_le16(hdev->manufacturer);
932
933 rp->supported_settings = cpu_to_le32(get_supported_settings(hdev));
934 rp->current_settings = cpu_to_le32(get_current_settings(hdev));
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200935
Szymon Janc7d5c11d2016-09-19 20:25:52 +0200936
MichaƂ Narajowskicde7a862016-09-19 20:25:53 +0200937 eir_len = append_eir_data_to_buf(hdev, rp->eir);
Szymon Janc7d5c11d2016-09-19 20:25:52 +0200938 rp->eir_len = cpu_to_le16(eir_len);
939
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200940 hci_dev_unlock(hdev);
941
942 /* If this command is called at least once, then the events
943 * for class of device and local name changes are disabled
944 * and only the new extended controller information event
945 * is used.
946 */
947 hci_sock_set_flag(sk, HCI_MGMT_EXT_INFO_EVENTS);
948 hci_sock_clear_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS);
949 hci_sock_clear_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS);
950
MichaƂ Narajowski8a0c9f42016-09-01 16:46:24 +0200951 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp,
952 sizeof(*rp) + eir_len);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200953}
954
955static int ext_info_changed(struct hci_dev *hdev, struct sock *skip)
956{
MichaƂ Narajowski5e9fae42016-09-19 20:25:55 +0200957 char buf[512];
958 struct mgmt_ev_ext_info_changed *ev = (void *)buf;
959 u16 eir_len;
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200960
MichaƂ Narajowski5e9fae42016-09-19 20:25:55 +0200961 memset(buf, 0, sizeof(buf));
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200962
MichaƂ Narajowski5e9fae42016-09-19 20:25:55 +0200963 eir_len = append_eir_data_to_buf(hdev, ev->eir);
964 ev->eir_len = cpu_to_le16(eir_len);
965
966 return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev,
967 sizeof(*ev) + eir_len,
968 HCI_MGMT_EXT_INFO_EVENTS, skip);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200969}
970
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200971static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
Johan Hedberg86805702011-11-11 16:18:52 +0200972{
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200973 __le32 settings = cpu_to_le32(get_current_settings(hdev));
Johan Hedberg86805702011-11-11 16:18:52 +0200974
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200975 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings,
976 sizeof(settings));
Johan Hedberg86805702011-11-11 16:18:52 +0200977}
978
Marcel Holtmann1904a852015-01-11 13:50:44 -0800979static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg8b064a32014-02-24 14:52:22 +0200980{
981 BT_DBG("%s status 0x%02x", hdev->name, status);
982
Johan Hedberga3172b72014-02-28 09:33:44 +0200983 if (hci_conn_count(hdev) == 0) {
984 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +0200985 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberga3172b72014-02-28 09:33:44 +0200986 }
Johan Hedberg8b064a32014-02-24 14:52:22 +0200987}
988
Johan Hedbergf2252572015-11-18 12:49:20 +0200989void mgmt_advertising_added(struct sock *sk, struct hci_dev *hdev, u8 instance)
Arman Uguray912098a2015-03-23 15:57:15 -0700990{
991 struct mgmt_ev_advertising_added ev;
992
993 ev.instance = instance;
994
995 mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk);
996}
997
Johan Hedbergf2252572015-11-18 12:49:20 +0200998void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev,
999 u8 instance)
Arman Uguray912098a2015-03-23 15:57:15 -07001000{
1001 struct mgmt_ev_advertising_removed ev;
1002
1003 ev.instance = instance;
1004
1005 mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk);
1006}
1007
Florian Grandel7816b822015-06-18 03:16:45 +02001008static void cancel_adv_timeout(struct hci_dev *hdev)
1009{
1010 if (hdev->adv_instance_timeout) {
1011 hdev->adv_instance_timeout = 0;
1012 cancel_delayed_work(&hdev->adv_instance_expire);
1013 }
1014}
1015
Johan Hedberg8b064a32014-02-24 14:52:22 +02001016static int clean_up_hci_state(struct hci_dev *hdev)
1017{
1018 struct hci_request req;
1019 struct hci_conn *conn;
Johan Hedberg23a48092014-07-08 16:05:06 +03001020 bool discov_stopped;
1021 int err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001022
1023 hci_req_init(&req, hdev);
1024
1025 if (test_bit(HCI_ISCAN, &hdev->flags) ||
1026 test_bit(HCI_PSCAN, &hdev->flags)) {
1027 u8 scan = 0x00;
1028 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1029 }
1030
Johan Hedberg37d3a1f2016-08-28 20:53:34 +03001031 hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, false);
Arman Uguray912098a2015-03-23 15:57:15 -07001032
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001033 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedbergf2252572015-11-18 12:49:20 +02001034 __hci_req_disable_advertising(&req);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001035
Johan Hedberg2154d3f2015-11-11 08:30:45 +02001036 discov_stopped = hci_req_stop_discovery(&req);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001037
1038 list_for_each_entry(conn, &hdev->conn_hash.list, list) {
Johan Hedberg89e0ccc2015-10-22 10:49:38 +03001039 /* 0x15 == Terminated due to Power Off */
1040 __hci_abort_conn(&req, conn, 0x15);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001041 }
1042
Johan Hedberg23a48092014-07-08 16:05:06 +03001043 err = hci_req_run(&req, clean_up_hci_complete);
1044 if (!err && discov_stopped)
1045 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
1046
1047 return err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001048}
1049
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001050static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001051 u16 len)
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001052{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001053 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001054 struct mgmt_pending_cmd *cmd;
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001055 int err;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001056
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001057 BT_DBG("request for %s", hdev->name);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001058
Johan Hedberga7e80f22013-01-09 16:05:19 +02001059 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001060 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1061 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001062
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001063 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001064
Johan Hedberg333ae952015-03-17 13:48:47 +02001065 if (pending_find(MGMT_OP_SET_POWERED, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001066 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1067 MGMT_STATUS_BUSY);
Johan Hedberg87b95ba2013-09-25 13:26:06 +03001068 goto failed;
1069 }
1070
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001071 if (!!cp->val == hdev_is_powered(hdev)) {
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001072 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001073 goto failed;
1074 }
1075
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001076 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
1077 if (!cmd) {
1078 err = -ENOMEM;
1079 goto failed;
1080 }
1081
Johan Hedberg8b064a32014-02-24 14:52:22 +02001082 if (cp->val) {
Johan Hedberg19202572013-01-14 22:33:51 +02001083 queue_work(hdev->req_workqueue, &hdev->power_on);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001084 err = 0;
1085 } else {
1086 /* Disconnect connections, stop scans, etc */
1087 err = clean_up_hci_state(hdev);
Johan Hedberga3172b72014-02-28 09:33:44 +02001088 if (!err)
1089 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
1090 HCI_POWER_OFF_TIMEOUT);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001091
Johan Hedberg8b064a32014-02-24 14:52:22 +02001092 /* ENODATA means there were no HCI commands queued */
1093 if (err == -ENODATA) {
Johan Hedberga3172b72014-02-28 09:33:44 +02001094 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001095 queue_work(hdev->req_workqueue, &hdev->power_off.work);
1096 err = 0;
1097 }
1098 }
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001099
1100failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001101 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001102 return err;
1103}
1104
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001105static int new_settings(struct hci_dev *hdev, struct sock *skip)
1106{
Marcel Holtmannf6b77122015-03-14 19:28:05 -07001107 __le32 ev = cpu_to_le32(get_current_settings(hdev));
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001108
Marcel Holtmann5504c3a2016-08-29 06:19:46 +02001109 return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev,
1110 sizeof(ev), HCI_MGMT_SETTING_EVENTS, skip);
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001111}
1112
Johan Hedberg91a668b2014-07-09 13:28:26 +03001113int mgmt_new_settings(struct hci_dev *hdev)
1114{
1115 return new_settings(hdev, NULL);
1116}
1117
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001118struct cmd_lookup {
1119 struct sock *sk;
1120 struct hci_dev *hdev;
1121 u8 mgmt_status;
1122};
1123
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001124static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001125{
1126 struct cmd_lookup *match = data;
1127
1128 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
1129
1130 list_del(&cmd->list);
1131
1132 if (match->sk == NULL) {
1133 match->sk = cmd->sk;
1134 sock_hold(match->sk);
1135 }
1136
1137 mgmt_pending_free(cmd);
1138}
1139
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001140static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001141{
1142 u8 *status = data;
1143
Johan Hedberga69e8372015-03-06 21:08:53 +02001144 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001145 mgmt_pending_remove(cmd);
1146}
1147
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001148static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg1b9b5ee2014-12-05 13:36:00 +02001149{
1150 if (cmd->cmd_complete) {
1151 u8 *status = data;
1152
1153 cmd->cmd_complete(cmd, *status);
1154 mgmt_pending_remove(cmd);
1155
1156 return;
1157 }
1158
1159 cmd_status_rsp(cmd, data);
1160}
1161
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001162static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedbergf5818c22014-12-05 13:36:02 +02001163{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001164 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1165 cmd->param, cmd->param_len);
Johan Hedbergf5818c22014-12-05 13:36:02 +02001166}
1167
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001168static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001169{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001170 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1171 cmd->param, sizeof(struct mgmt_addr_info));
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001172}
1173
Johan Hedberge6fe7982013-10-02 15:45:22 +03001174static u8 mgmt_bredr_support(struct hci_dev *hdev)
1175{
1176 if (!lmp_bredr_capable(hdev))
1177 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001178 else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001179 return MGMT_STATUS_REJECTED;
1180 else
1181 return MGMT_STATUS_SUCCESS;
1182}
1183
1184static u8 mgmt_le_support(struct hci_dev *hdev)
1185{
1186 if (!lmp_le_capable(hdev))
1187 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001188 else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001189 return MGMT_STATUS_REJECTED;
1190 else
1191 return MGMT_STATUS_SUCCESS;
1192}
1193
Johan Hedbergaed1a882015-11-22 17:24:44 +03001194void mgmt_set_discoverable_complete(struct hci_dev *hdev, u8 status)
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001195{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001196 struct mgmt_pending_cmd *cmd;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001197
1198 BT_DBG("status 0x%02x", status);
1199
1200 hci_dev_lock(hdev);
1201
Johan Hedberg333ae952015-03-17 13:48:47 +02001202 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001203 if (!cmd)
1204 goto unlock;
1205
1206 if (status) {
1207 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001208 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001209 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001210 goto remove_cmd;
1211 }
1212
Johan Hedbergaed1a882015-11-22 17:24:44 +03001213 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1214 hdev->discov_timeout > 0) {
1215 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1216 queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001217 }
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001218
1219 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergaed1a882015-11-22 17:24:44 +03001220 new_settings(hdev, cmd->sk);
Marcel Holtmann970ba522013-10-15 06:33:57 -07001221
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001222remove_cmd:
1223 mgmt_pending_remove(cmd);
1224
1225unlock:
1226 hci_dev_unlock(hdev);
1227}
1228
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001229static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001230 u16 len)
Johan Hedberg73f22f62010-12-29 16:00:25 +02001231{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001232 struct mgmt_cp_set_discoverable *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001233 struct mgmt_pending_cmd *cmd;
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001234 u16 timeout;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001235 int err;
1236
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001237 BT_DBG("request for %s", hdev->name);
Johan Hedberg73f22f62010-12-29 16:00:25 +02001238
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001239 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1240 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001241 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1242 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001243
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001244 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02001245 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1246 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001247
Marcel Holtmann1f350c82012-03-12 20:31:08 -07001248 timeout = __le16_to_cpu(cp->timeout);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001249
1250 /* Disabling discoverable requires that no timeout is set,
1251 * and enabling limited discoverable requires a timeout.
1252 */
1253 if ((cp->val == 0x00 && timeout > 0) ||
1254 (cp->val == 0x02 && timeout == 0))
Johan Hedberga69e8372015-03-06 21:08:53 +02001255 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1256 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001257
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001258 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001259
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001260 if (!hdev_is_powered(hdev) && timeout > 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001261 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1262 MGMT_STATUS_NOT_POWERED);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001263 goto failed;
1264 }
1265
Johan Hedberg333ae952015-03-17 13:48:47 +02001266 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1267 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001268 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1269 MGMT_STATUS_BUSY);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001270 goto failed;
1271 }
1272
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001273 if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001274 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1275 MGMT_STATUS_REJECTED);
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001276 goto failed;
1277 }
1278
1279 if (!hdev_is_powered(hdev)) {
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001280 bool changed = false;
1281
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001282 /* Setting limited discoverable when powered off is
1283 * not a valid operation since it requires a timeout
1284 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1285 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001286 if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07001287 hci_dev_change_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001288 changed = true;
1289 }
1290
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001291 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001292 if (err < 0)
1293 goto failed;
1294
1295 if (changed)
1296 err = new_settings(hdev, sk);
1297
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001298 goto failed;
1299 }
1300
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001301 /* If the current mode is the same, then just update the timeout
1302 * value with the new value. And if only the timeout gets updated,
1303 * then no need for any HCI transactions.
1304 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001305 if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1306 (cp->val == 0x02) == hci_dev_test_flag(hdev,
1307 HCI_LIMITED_DISCOVERABLE)) {
Marcel Holtmann36261542013-10-15 08:28:51 -07001308 cancel_delayed_work(&hdev->discov_off);
1309 hdev->discov_timeout = timeout;
Marcel Holtmann955638e2012-02-22 18:21:00 +01001310
Marcel Holtmann36261542013-10-15 08:28:51 -07001311 if (cp->val && hdev->discov_timeout > 0) {
1312 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
Johan Hedbergc366f552015-11-23 15:43:06 +02001313 queue_delayed_work(hdev->req_workqueue,
1314 &hdev->discov_off, to);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001315 }
1316
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001317 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001318 goto failed;
1319 }
1320
1321 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
1322 if (!cmd) {
1323 err = -ENOMEM;
1324 goto failed;
1325 }
1326
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001327 /* Cancel any potential discoverable timeout that might be
1328 * still active and store new timeout value. The arming of
1329 * the timeout happens in the complete handler.
1330 */
1331 cancel_delayed_work(&hdev->discov_off);
1332 hdev->discov_timeout = timeout;
1333
Johan Hedbergaed1a882015-11-22 17:24:44 +03001334 if (cp->val)
1335 hci_dev_set_flag(hdev, HCI_DISCOVERABLE);
1336 else
1337 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1338
Johan Hedbergb456f872013-10-19 23:38:22 +03001339 /* Limited discoverable mode */
1340 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001341 hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001342 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001343 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001344
Johan Hedbergaed1a882015-11-22 17:24:44 +03001345 queue_work(hdev->req_workqueue, &hdev->discoverable_update);
1346 err = 0;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001347
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001348failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001349 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001350 return err;
1351}
Johan Hedberg73f22f62010-12-29 16:00:25 +02001352
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001353void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status)
Johan Hedberg2b76f452013-03-15 17:07:04 -05001354{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001355 struct mgmt_pending_cmd *cmd;
Johan Hedberg2b76f452013-03-15 17:07:04 -05001356
1357 BT_DBG("status 0x%02x", status);
1358
1359 hci_dev_lock(hdev);
1360
Johan Hedberg333ae952015-03-17 13:48:47 +02001361 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
Johan Hedberg2b76f452013-03-15 17:07:04 -05001362 if (!cmd)
1363 goto unlock;
1364
Johan Hedberg37438c12013-10-14 16:20:05 +03001365 if (status) {
1366 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001367 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg37438c12013-10-14 16:20:05 +03001368 goto remove_cmd;
1369 }
1370
Johan Hedberg2b76f452013-03-15 17:07:04 -05001371 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001372 new_settings(hdev, cmd->sk);
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001373
Johan Hedberg37438c12013-10-14 16:20:05 +03001374remove_cmd:
Johan Hedberg2b76f452013-03-15 17:07:04 -05001375 mgmt_pending_remove(cmd);
1376
1377unlock:
1378 hci_dev_unlock(hdev);
1379}
1380
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001381static int set_connectable_update_settings(struct hci_dev *hdev,
1382 struct sock *sk, u8 val)
1383{
1384 bool changed = false;
1385 int err;
1386
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001387 if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE))
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001388 changed = true;
1389
1390 if (val) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001391 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001392 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001393 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
1394 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001395 }
1396
1397 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
1398 if (err < 0)
1399 return err;
1400
Johan Hedberg562064e2014-07-08 16:35:34 +03001401 if (changed) {
Johan Hedberg01b1cb82015-11-16 12:52:21 +02001402 hci_req_update_scan(hdev);
Johan Hedberg562064e2014-07-08 16:35:34 +03001403 hci_update_background_scan(hdev);
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001404 return new_settings(hdev, sk);
Johan Hedberg562064e2014-07-08 16:35:34 +03001405 }
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001406
1407 return 0;
1408}
1409
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001410static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001411 u16 len)
Johan Hedberg73f22f62010-12-29 16:00:25 +02001412{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001413 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001414 struct mgmt_pending_cmd *cmd;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001415 int err;
1416
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001417 BT_DBG("request for %s", hdev->name);
Johan Hedberg73f22f62010-12-29 16:00:25 +02001418
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001419 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1420 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001421 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1422 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001423
Johan Hedberga7e80f22013-01-09 16:05:19 +02001424 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001425 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1426 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001427
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001428 hci_dev_lock(hdev);
Johan Hedberg73f22f62010-12-29 16:00:25 +02001429
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001430 if (!hdev_is_powered(hdev)) {
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001431 err = set_connectable_update_settings(hdev, sk, cp->val);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001432 goto failed;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001433 }
1434
Johan Hedberg333ae952015-03-17 13:48:47 +02001435 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1436 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001437 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1438 MGMT_STATUS_BUSY);
Johan Hedberg73f22f62010-12-29 16:00:25 +02001439 goto failed;
1440 }
1441
Johan Hedberg73f22f62010-12-29 16:00:25 +02001442 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
1443 if (!cmd) {
1444 err = -ENOMEM;
Johan Hedberg72a734e2010-12-30 00:38:22 +02001445 goto failed;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001446 }
1447
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001448 if (cp->val) {
1449 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
1450 } else {
1451 if (hdev->discov_timeout > 0)
1452 cancel_delayed_work(&hdev->discov_off);
Johan Hedberg2b76f452013-03-15 17:07:04 -05001453
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001454 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1455 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1456 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
Johan Hedberg9b742462013-10-14 16:20:03 +03001457 }
Johan Hedberg2b76f452013-03-15 17:07:04 -05001458
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001459 queue_work(hdev->req_workqueue, &hdev->connectable_update);
1460 err = 0;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001461
1462failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001463 hci_dev_unlock(hdev);
Johan Hedberg72a734e2010-12-30 00:38:22 +02001464 return err;
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001465}
1466
Johan Hedbergb2939472014-07-30 09:22:23 +03001467static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001468 u16 len)
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001469{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001470 struct mgmt_mode *cp = data;
Marcel Holtmann55594352013-10-06 16:11:57 -07001471 bool changed;
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001472 int err;
1473
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001474 BT_DBG("request for %s", hdev->name);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001475
Johan Hedberga7e80f22013-01-09 16:05:19 +02001476 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001477 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE,
1478 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001479
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001480 hci_dev_lock(hdev);
Johan Hedberg053f0212011-01-26 13:07:10 +02001481
1482 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07001483 changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE);
Johan Hedberg053f0212011-01-26 13:07:10 +02001484 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001485 changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE);
Johan Hedberg053f0212011-01-26 13:07:10 +02001486
Johan Hedbergb2939472014-07-30 09:22:23 +03001487 err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev);
Johan Hedberg053f0212011-01-26 13:07:10 +02001488 if (err < 0)
Marcel Holtmann55594352013-10-06 16:11:57 -07001489 goto unlock;
Johan Hedberg053f0212011-01-26 13:07:10 +02001490
Johan Hedberg82a37ad2016-03-09 17:30:34 +02001491 if (changed) {
1492 /* In limited privacy mode the change of bondable mode
1493 * may affect the local advertising address.
1494 */
1495 if (hdev_is_powered(hdev) &&
1496 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1497 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1498 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
1499 queue_work(hdev->req_workqueue,
1500 &hdev->discoverable_update);
1501
Marcel Holtmann55594352013-10-06 16:11:57 -07001502 err = new_settings(hdev, sk);
Johan Hedberg82a37ad2016-03-09 17:30:34 +02001503 }
Johan Hedberg053f0212011-01-26 13:07:10 +02001504
Marcel Holtmann55594352013-10-06 16:11:57 -07001505unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001506 hci_dev_unlock(hdev);
Johan Hedberg053f0212011-01-26 13:07:10 +02001507 return err;
1508}
1509
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001510static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
1511 u16 len)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001512{
1513 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001514 struct mgmt_pending_cmd *cmd;
Johan Hedberge6fe7982013-10-02 15:45:22 +03001515 u8 val, status;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001516 int err;
1517
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001518 BT_DBG("request for %s", hdev->name);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001519
Johan Hedberge6fe7982013-10-02 15:45:22 +03001520 status = mgmt_bredr_support(hdev);
1521 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02001522 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1523 status);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001524
Johan Hedberga7e80f22013-01-09 16:05:19 +02001525 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001526 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1527 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001528
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001529 hci_dev_lock(hdev);
1530
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001531 if (!hdev_is_powered(hdev)) {
Johan Hedberg47990ea2012-02-22 11:58:37 +02001532 bool changed = false;
1533
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001534 if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07001535 hci_dev_change_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02001536 changed = true;
1537 }
1538
1539 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1540 if (err < 0)
1541 goto failed;
1542
1543 if (changed)
1544 err = new_settings(hdev, sk);
1545
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001546 goto failed;
1547 }
1548
Johan Hedberg333ae952015-03-17 13:48:47 +02001549 if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001550 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1551 MGMT_STATUS_BUSY);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001552 goto failed;
1553 }
1554
1555 val = !!cp->val;
1556
1557 if (test_bit(HCI_AUTH, &hdev->flags) == val) {
1558 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1559 goto failed;
1560 }
1561
1562 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
1563 if (!cmd) {
1564 err = -ENOMEM;
1565 goto failed;
1566 }
1567
1568 err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
1569 if (err < 0) {
1570 mgmt_pending_remove(cmd);
1571 goto failed;
1572 }
1573
1574failed:
1575 hci_dev_unlock(hdev);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001576 return err;
1577}
1578
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001579static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001580{
1581 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001582 struct mgmt_pending_cmd *cmd;
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07001583 u8 status;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001584 int err;
1585
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001586 BT_DBG("request for %s", hdev->name);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001587
Marcel Holtmanncdba5282013-10-02 21:31:52 -07001588 status = mgmt_bredr_support(hdev);
1589 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02001590 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
Marcel Holtmanncdba5282013-10-02 21:31:52 -07001591
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001592 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02001593 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1594 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001595
Johan Hedberga7e80f22013-01-09 16:05:19 +02001596 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001597 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1598 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001599
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001600 hci_dev_lock(hdev);
Johan Hedberg6c8f12c2012-02-22 16:35:26 +02001601
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001602 if (!hdev_is_powered(hdev)) {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001603 bool changed;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02001604
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001605 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001606 changed = !hci_dev_test_and_set_flag(hdev,
1607 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001608 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001609 changed = hci_dev_test_and_clear_flag(hdev,
1610 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001611 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001612 changed = hci_dev_test_and_clear_flag(hdev,
1613 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001614 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001615 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02001616 }
1617
1618 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
1619 if (err < 0)
1620 goto failed;
1621
1622 if (changed)
1623 err = new_settings(hdev, sk);
1624
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001625 goto failed;
1626 }
1627
Johan Hedberg333ae952015-03-17 13:48:47 +02001628 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001629 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1630 MGMT_STATUS_BUSY);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001631 goto failed;
1632 }
1633
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001634 if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001635 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
1636 goto failed;
1637 }
1638
1639 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
1640 if (!cmd) {
1641 err = -ENOMEM;
1642 goto failed;
1643 }
1644
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001645 if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03001646 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
1647 sizeof(cp->val), &cp->val);
1648
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07001649 err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001650 if (err < 0) {
1651 mgmt_pending_remove(cmd);
1652 goto failed;
1653 }
1654
1655failed:
1656 hci_dev_unlock(hdev);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001657 return err;
1658}
1659
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001660static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001661{
1662 struct mgmt_mode *cp = data;
Marcel Holtmannee392692013-10-01 22:59:23 -07001663 bool changed;
Johan Hedberge6fe7982013-10-02 15:45:22 +03001664 u8 status;
Marcel Holtmannee392692013-10-01 22:59:23 -07001665 int err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001666
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001667 BT_DBG("request for %s", hdev->name);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001668
Johan Hedberge6fe7982013-10-02 15:45:22 +03001669 status = mgmt_bredr_support(hdev);
1670 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02001671 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001672
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001673 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02001674 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1675 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001676
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001677 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001678 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1679 MGMT_STATUS_REJECTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001680
Johan Hedberga7e80f22013-01-09 16:05:19 +02001681 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001682 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1683 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001684
Marcel Holtmannee392692013-10-01 22:59:23 -07001685 hci_dev_lock(hdev);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001686
Johan Hedberg333ae952015-03-17 13:48:47 +02001687 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001688 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1689 MGMT_STATUS_BUSY);
Johan Hedberga2cb01d2015-02-19 17:38:07 +02001690 goto unlock;
1691 }
1692
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001693 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001694 changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001695 } else {
1696 if (hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001697 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1698 MGMT_STATUS_REJECTED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001699 goto unlock;
1700 }
1701
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001702 changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001703 }
Marcel Holtmannee392692013-10-01 22:59:23 -07001704
1705 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
1706 if (err < 0)
1707 goto unlock;
1708
1709 if (changed)
1710 err = new_settings(hdev, sk);
1711
1712unlock:
1713 hci_dev_unlock(hdev);
1714 return err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001715}
1716
Marcel Holtmann1904a852015-01-11 13:50:44 -08001717static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001718{
1719 struct cmd_lookup match = { NULL, hdev };
1720
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05301721 hci_dev_lock(hdev);
1722
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001723 if (status) {
1724 u8 mgmt_err = mgmt_status(status);
1725
1726 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
1727 &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05301728 goto unlock;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001729 }
1730
1731 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
1732
1733 new_settings(hdev, match.sk);
1734
1735 if (match.sk)
1736 sock_put(match.sk);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001737
1738 /* Make sure the controller has a good default for
1739 * advertising data. Restrict the update to when LE
1740 * has actually been enabled. During power on, the
1741 * update in powered_update_hci will take care of it.
1742 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001743 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001744 struct hci_request req;
1745
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001746 hci_req_init(&req, hdev);
Johan Hedbergcab054a2015-11-30 11:21:45 +02001747 __hci_req_update_adv_data(&req, 0x00);
1748 __hci_req_update_scan_rsp_data(&req, 0x00);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001749 hci_req_run(&req, NULL);
Johan Hedberg2e93e532015-11-11 08:11:17 +02001750 hci_update_background_scan(hdev);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001751 }
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05301752
1753unlock:
1754 hci_dev_unlock(hdev);
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001755}
1756
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001757static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg06199cf2012-02-22 16:37:11 +02001758{
1759 struct mgmt_mode *cp = data;
1760 struct hci_cp_write_le_host_supported hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001761 struct mgmt_pending_cmd *cmd;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001762 struct hci_request req;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001763 int err;
Johan Hedberg0b60eba2012-02-28 00:57:24 +02001764 u8 val, enabled;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001765
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001766 BT_DBG("request for %s", hdev->name);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001767
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001768 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02001769 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1770 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg1de028c2012-02-29 19:55:35 -08001771
Johan Hedberga7e80f22013-01-09 16:05:19 +02001772 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001773 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1774 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001775
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07001776 /* Bluetooth single mode LE only controllers or dual-mode
1777 * controllers configured as LE only devices, do not allow
1778 * switching LE off. These have either LE enabled explicitly
1779 * or BR/EDR has been previously switched off.
1780 *
1781 * When trying to enable an already enabled LE, then gracefully
1782 * send a positive response. Trying to disable it however will
1783 * result into rejection.
1784 */
1785 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
1786 if (cp->val == 0x01)
1787 return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
1788
Johan Hedberga69e8372015-03-06 21:08:53 +02001789 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1790 MGMT_STATUS_REJECTED);
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07001791 }
Johan Hedbergc73eee92013-04-19 18:35:21 +03001792
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001793 hci_dev_lock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001794
1795 val = !!cp->val;
Gustavo Padovanffa88e02012-11-23 16:50:51 -02001796 enabled = lmp_host_le_capable(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001797
Florian Grandel847818d2015-06-18 03:16:46 +02001798 if (!val)
Johan Hedberg37d3a1f2016-08-28 20:53:34 +03001799 hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, true);
Florian Grandel847818d2015-06-18 03:16:46 +02001800
Johan Hedberg0b60eba2012-02-28 00:57:24 +02001801 if (!hdev_is_powered(hdev) || val == enabled) {
Johan Hedberg06199cf2012-02-22 16:37:11 +02001802 bool changed = false;
1803
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001804 if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07001805 hci_dev_change_flag(hdev, HCI_LE_ENABLED);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001806 changed = true;
1807 }
1808
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001809 if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001810 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03001811 changed = true;
1812 }
1813
Johan Hedberg06199cf2012-02-22 16:37:11 +02001814 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
1815 if (err < 0)
Johan Hedberg1de028c2012-02-29 19:55:35 -08001816 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001817
1818 if (changed)
1819 err = new_settings(hdev, sk);
1820
Johan Hedberg1de028c2012-02-29 19:55:35 -08001821 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001822 }
1823
Johan Hedberg333ae952015-03-17 13:48:47 +02001824 if (pending_find(MGMT_OP_SET_LE, hdev) ||
1825 pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001826 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1827 MGMT_STATUS_BUSY);
Johan Hedberg1de028c2012-02-29 19:55:35 -08001828 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001829 }
1830
1831 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
1832 if (!cmd) {
1833 err = -ENOMEM;
Johan Hedberg1de028c2012-02-29 19:55:35 -08001834 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001835 }
1836
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001837 hci_req_init(&req, hdev);
1838
Johan Hedberg06199cf2012-02-22 16:37:11 +02001839 memset(&hci_cp, 0, sizeof(hci_cp));
1840
1841 if (val) {
1842 hci_cp.le = val;
Marcel Holtmann32226e42014-07-24 20:04:16 +02001843 hci_cp.simul = 0x00;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001844 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001845 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedbergf2252572015-11-18 12:49:20 +02001846 __hci_req_disable_advertising(&req);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001847 }
1848
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001849 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp),
1850 &hci_cp);
1851
1852 err = hci_req_run(&req, le_enable_complete);
Syam Sidhardhan0c01bc42012-04-12 20:33:21 +05301853 if (err < 0)
Johan Hedberg06199cf2012-02-22 16:37:11 +02001854 mgmt_pending_remove(cmd);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001855
Johan Hedberg1de028c2012-02-29 19:55:35 -08001856unlock:
1857 hci_dev_unlock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001858 return err;
1859}
1860
Johan Hedberg0cab9c82013-03-15 17:06:54 -05001861/* This is a helper function to test for pending mgmt commands that can
1862 * cause CoD or EIR HCI commands. We can only allow one such pending
1863 * mgmt command at a time since otherwise we cannot easily track what
1864 * the current values are, will be, and based on that calculate if a new
1865 * HCI command needs to be sent and if yes with what value.
1866 */
1867static bool pending_eir_or_class(struct hci_dev *hdev)
1868{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001869 struct mgmt_pending_cmd *cmd;
Johan Hedberg0cab9c82013-03-15 17:06:54 -05001870
1871 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
1872 switch (cmd->opcode) {
1873 case MGMT_OP_ADD_UUID:
1874 case MGMT_OP_REMOVE_UUID:
1875 case MGMT_OP_SET_DEV_CLASS:
1876 case MGMT_OP_SET_POWERED:
1877 return true;
1878 }
1879 }
1880
1881 return false;
1882}
1883
Johan Hedberg83be8ec2013-01-27 00:31:29 +02001884static const u8 bluetooth_base_uuid[] = {
1885 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
1886 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1887};
1888
1889static u8 get_uuid_size(const u8 *uuid)
1890{
1891 u32 val;
1892
1893 if (memcmp(uuid, bluetooth_base_uuid, 12))
1894 return 128;
1895
1896 val = get_unaligned_le32(&uuid[12]);
1897 if (val > 0xffff)
1898 return 32;
1899
1900 return 16;
1901}
1902
Johan Hedberg92da6092013-03-15 17:06:55 -05001903static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status)
1904{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001905 struct mgmt_pending_cmd *cmd;
Johan Hedberg92da6092013-03-15 17:06:55 -05001906
1907 hci_dev_lock(hdev);
1908
Johan Hedberg333ae952015-03-17 13:48:47 +02001909 cmd = pending_find(mgmt_op, hdev);
Johan Hedberg92da6092013-03-15 17:06:55 -05001910 if (!cmd)
1911 goto unlock;
1912
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001913 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
1914 mgmt_status(status), hdev->dev_class, 3);
Johan Hedberg92da6092013-03-15 17:06:55 -05001915
1916 mgmt_pending_remove(cmd);
1917
1918unlock:
1919 hci_dev_unlock(hdev);
1920}
1921
Marcel Holtmann1904a852015-01-11 13:50:44 -08001922static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05001923{
1924 BT_DBG("status 0x%02x", status);
1925
1926 mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status);
1927}
1928
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001929static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001930{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001931 struct mgmt_cp_add_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001932 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05001933 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001934 struct bt_uuid *uuid;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001935 int err;
1936
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001937 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001938
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001939 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001940
Johan Hedberg0cab9c82013-03-15 17:06:54 -05001941 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001942 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
1943 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02001944 goto failed;
1945 }
1946
Andre Guedes92c4c202012-06-07 19:05:44 -03001947 uuid = kmalloc(sizeof(*uuid), GFP_KERNEL);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001948 if (!uuid) {
1949 err = -ENOMEM;
1950 goto failed;
1951 }
1952
1953 memcpy(uuid->uuid, cp->uuid, 16);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001954 uuid->svc_hint = cp->svc_hint;
Johan Hedberg83be8ec2013-01-27 00:31:29 +02001955 uuid->size = get_uuid_size(cp->uuid);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001956
Johan Hedbergde66aa62013-01-27 00:31:27 +02001957 list_add_tail(&uuid->list, &hdev->uuids);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001958
Johan Hedberg890ea892013-03-15 17:06:52 -05001959 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001960
Johan Hedberg14bf5ea2015-11-22 19:00:22 +02001961 __hci_req_update_class(&req);
Johan Hedbergb1a89172015-11-25 16:15:42 +02001962 __hci_req_update_eir(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05001963
Johan Hedberg92da6092013-03-15 17:06:55 -05001964 err = hci_req_run(&req, add_uuid_complete);
1965 if (err < 0) {
1966 if (err != -ENODATA)
1967 goto failed;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001968
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001969 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
1970 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02001971 goto failed;
1972 }
1973
1974 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05001975 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02001976 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05001977 goto failed;
1978 }
1979
1980 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001981
1982failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001983 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001984 return err;
1985}
1986
Johan Hedberg24b78d02012-02-23 23:24:30 +02001987static bool enable_service_cache(struct hci_dev *hdev)
1988{
1989 if (!hdev_is_powered(hdev))
1990 return false;
1991
Marcel Holtmann238be782015-03-13 02:11:06 -07001992 if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg46818ed2013-01-14 22:33:52 +02001993 queue_delayed_work(hdev->workqueue, &hdev->service_cache,
1994 CACHE_TIMEOUT);
Johan Hedberg24b78d02012-02-23 23:24:30 +02001995 return true;
1996 }
1997
1998 return false;
1999}
2000
Marcel Holtmann1904a852015-01-11 13:50:44 -08002001static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002002{
2003 BT_DBG("status 0x%02x", status);
2004
2005 mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status);
2006}
2007
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002008static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002009 u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002010{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002011 struct mgmt_cp_remove_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002012 struct mgmt_pending_cmd *cmd;
Johan Hedberg056341c2013-01-27 00:31:30 +02002013 struct bt_uuid *match, *tmp;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002014 u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Johan Hedberg890ea892013-03-15 17:06:52 -05002015 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002016 int err, found;
2017
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002018 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002019
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002020 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002021
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002022 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002023 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2024 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002025 goto unlock;
2026 }
2027
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002028 if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
Johan Hedberg35f74982014-02-18 17:14:32 +02002029 hci_uuids_clear(hdev);
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002030
Johan Hedberg24b78d02012-02-23 23:24:30 +02002031 if (enable_service_cache(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002032 err = mgmt_cmd_complete(sk, hdev->id,
2033 MGMT_OP_REMOVE_UUID,
2034 0, hdev->dev_class, 3);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002035 goto unlock;
2036 }
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002037
Johan Hedberg9246a862012-02-23 21:33:16 +02002038 goto update_class;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002039 }
2040
2041 found = 0;
2042
Johan Hedberg056341c2013-01-27 00:31:30 +02002043 list_for_each_entry_safe(match, tmp, &hdev->uuids, list) {
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002044 if (memcmp(match->uuid, cp->uuid, 16) != 0)
2045 continue;
2046
2047 list_del(&match->list);
Johan Hedberg482049f2012-11-08 10:25:26 +01002048 kfree(match);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002049 found++;
2050 }
2051
2052 if (found == 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002053 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2054 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002055 goto unlock;
2056 }
2057
Johan Hedberg9246a862012-02-23 21:33:16 +02002058update_class:
Johan Hedberg890ea892013-03-15 17:06:52 -05002059 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002060
Johan Hedberg14bf5ea2015-11-22 19:00:22 +02002061 __hci_req_update_class(&req);
Johan Hedbergb1a89172015-11-25 16:15:42 +02002062 __hci_req_update_eir(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05002063
Johan Hedberg92da6092013-03-15 17:06:55 -05002064 err = hci_req_run(&req, remove_uuid_complete);
2065 if (err < 0) {
2066 if (err != -ENODATA)
2067 goto unlock;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002068
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002069 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
2070 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002071 goto unlock;
2072 }
2073
2074 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002075 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002076 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002077 goto unlock;
2078 }
2079
2080 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002081
2082unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002083 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002084 return err;
2085}
2086
Marcel Holtmann1904a852015-01-11 13:50:44 -08002087static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002088{
2089 BT_DBG("status 0x%02x", status);
2090
2091 mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status);
2092}
2093
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002094static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002095 u16 len)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002096{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002097 struct mgmt_cp_set_dev_class *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002098 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002099 struct hci_request req;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002100 int err;
2101
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002102 BT_DBG("request for %s", hdev->name);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002103
Marcel Holtmann6203fc92013-10-02 23:37:29 -07002104 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002105 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2106 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002107
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002108 hci_dev_lock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002109
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002110 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002111 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2112 MGMT_STATUS_BUSY);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002113 goto unlock;
2114 }
2115
2116 if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002117 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2118 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002119 goto unlock;
2120 }
2121
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002122 hdev->major_class = cp->major;
2123 hdev->minor_class = cp->minor;
2124
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002125 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002126 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2127 hdev->dev_class, 3);
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002128 goto unlock;
2129 }
2130
Johan Hedberg890ea892013-03-15 17:06:52 -05002131 hci_req_init(&req, hdev);
2132
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002133 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg7d785252011-12-15 00:47:39 +02002134 hci_dev_unlock(hdev);
2135 cancel_delayed_work_sync(&hdev->service_cache);
2136 hci_dev_lock(hdev);
Johan Hedbergb1a89172015-11-25 16:15:42 +02002137 __hci_req_update_eir(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02002138 }
Johan Hedberg14c0b602011-12-15 00:47:37 +02002139
Johan Hedberg14bf5ea2015-11-22 19:00:22 +02002140 __hci_req_update_class(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05002141
Johan Hedberg92da6092013-03-15 17:06:55 -05002142 err = hci_req_run(&req, set_class_complete);
2143 if (err < 0) {
2144 if (err != -ENODATA)
2145 goto unlock;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002146
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002147 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2148 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002149 goto unlock;
2150 }
2151
2152 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002153 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002154 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002155 goto unlock;
2156 }
2157
2158 err = 0;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002159
Johan Hedbergb5235a62012-02-21 14:32:24 +02002160unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002161 hci_dev_unlock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002162 return err;
2163}
2164
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002165static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002166 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002167{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002168 struct mgmt_cp_load_link_keys *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03002169 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
2170 sizeof(struct mgmt_link_key_info));
Szymon Janc4e51eae2011-02-25 19:05:48 +01002171 u16 key_count, expected_len;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002172 bool changed;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002173 int i;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002174
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002175 BT_DBG("request for %s", hdev->name);
2176
2177 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002178 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2179 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002180
Marcel Holtmann1f350c82012-03-12 20:31:08 -07002181 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002182 if (key_count > max_key_count) {
2183 BT_ERR("load_link_keys: too big key_count value %u",
2184 key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02002185 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2186 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002187 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002188
Johan Hedberg86742e12011-11-07 23:13:38 +02002189 expected_len = sizeof(*cp) + key_count *
2190 sizeof(struct mgmt_link_key_info);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002191 if (expected_len != len) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002192 BT_ERR("load_link_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02002193 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02002194 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2195 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002196 }
2197
Johan Hedberg4ae14302013-01-20 14:27:13 +02002198 if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002199 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2200 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ae14302013-01-20 14:27:13 +02002201
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002202 BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002203 key_count);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002204
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002205 for (i = 0; i < key_count; i++) {
2206 struct mgmt_link_key_info *key = &cp->keys[i];
2207
Marcel Holtmann8e991132014-01-10 02:07:25 -08002208 if (key->addr.type != BDADDR_BREDR || key->type > 0x08)
Johan Hedberga69e8372015-03-06 21:08:53 +02002209 return mgmt_cmd_status(sk, hdev->id,
2210 MGMT_OP_LOAD_LINK_KEYS,
2211 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002212 }
2213
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002214 hci_dev_lock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002215
2216 hci_link_keys_clear(hdev);
2217
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002218 if (cp->debug_keys)
Marcel Holtmann238be782015-03-13 02:11:06 -07002219 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002220 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002221 changed = hci_dev_test_and_clear_flag(hdev,
2222 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002223
2224 if (changed)
2225 new_settings(hdev, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002226
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002227 for (i = 0; i < key_count; i++) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002228 struct mgmt_link_key_info *key = &cp->keys[i];
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002229
Johan Hedberg58e92932014-06-24 14:00:26 +03002230 /* Always ignore debug keys and require a new pairing if
2231 * the user wants to use them.
2232 */
2233 if (key->type == HCI_LK_DEBUG_COMBINATION)
2234 continue;
2235
Johan Hedberg7652ff62014-06-24 13:15:49 +03002236 hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val,
2237 key->type, key->pin_len, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002238 }
2239
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002240 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
Johan Hedberg0e5f8752011-11-11 16:18:54 +02002241
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002242 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002243
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002244 return 0;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002245}
2246
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002247static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002248 u8 addr_type, struct sock *skip_sk)
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002249{
2250 struct mgmt_ev_device_unpaired ev;
2251
2252 bacpy(&ev.addr.bdaddr, bdaddr);
2253 ev.addr.type = addr_type;
2254
2255 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002256 skip_sk);
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002257}
2258
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002259static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002260 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002261{
Johan Hedberg124f6e32012-02-09 13:50:12 +02002262 struct mgmt_cp_unpair_device *cp = data;
2263 struct mgmt_rp_unpair_device rp;
Johan Hedbergfc643612015-10-22 09:38:31 +03002264 struct hci_conn_params *params;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002265 struct mgmt_pending_cmd *cmd;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002266 struct hci_conn *conn;
Johan Hedbergec182f02015-10-21 18:03:03 +03002267 u8 addr_type;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002268 int err;
2269
Johan Hedberga8a1d192011-11-10 15:54:38 +02002270 memset(&rp, 0, sizeof(rp));
Johan Hedberg124f6e32012-02-09 13:50:12 +02002271 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2272 rp.addr.type = cp->addr.type;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002273
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002274 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002275 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2276 MGMT_STATUS_INVALID_PARAMS,
2277 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002278
Johan Hedberg118da702013-01-20 14:27:20 +02002279 if (cp->disconnect != 0x00 && cp->disconnect != 0x01)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002280 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2281 MGMT_STATUS_INVALID_PARAMS,
2282 &rp, sizeof(rp));
Johan Hedberg118da702013-01-20 14:27:20 +02002283
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002284 hci_dev_lock(hdev);
2285
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002286 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002287 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2288 MGMT_STATUS_NOT_POWERED, &rp,
2289 sizeof(rp));
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002290 goto unlock;
2291 }
2292
Johan Hedberge0b2b272014-02-18 17:14:31 +02002293 if (cp->addr.type == BDADDR_BREDR) {
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002294 /* If disconnection is requested, then look up the
2295 * connection. If the remote device is connected, it
2296 * will be later used to terminate the link.
2297 *
2298 * Setting it to NULL explicitly will cause no
2299 * termination of the link.
2300 */
2301 if (cp->disconnect)
2302 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2303 &cp->addr.bdaddr);
2304 else
2305 conn = NULL;
2306
Johan Hedberg124f6e32012-02-09 13:50:12 +02002307 err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
Johan Hedbergec182f02015-10-21 18:03:03 +03002308 if (err < 0) {
2309 err = mgmt_cmd_complete(sk, hdev->id,
2310 MGMT_OP_UNPAIR_DEVICE,
2311 MGMT_STATUS_NOT_PAIRED, &rp,
2312 sizeof(rp));
2313 goto unlock;
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002314 }
2315
Johan Hedbergec182f02015-10-21 18:03:03 +03002316 goto done;
Johan Hedberge0b2b272014-02-18 17:14:31 +02002317 }
Vinicius Costa Gomesb0dbfb42012-02-02 21:08:03 -03002318
Johan Hedbergec182f02015-10-21 18:03:03 +03002319 /* LE address type */
2320 addr_type = le_addr_type(cp->addr.type);
2321
2322 hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type);
2323
2324 err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002325 if (err < 0) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002326 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2327 MGMT_STATUS_NOT_PAIRED, &rp,
2328 sizeof(rp));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002329 goto unlock;
2330 }
2331
Johan Hedbergec182f02015-10-21 18:03:03 +03002332 conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type);
2333 if (!conn) {
2334 hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type);
2335 goto done;
2336 }
2337
Johan Hedbergc81d5552015-10-22 09:38:35 +03002338 /* Abort any ongoing SMP pairing */
2339 smp_cancel_pairing(conn);
2340
Johan Hedbergec182f02015-10-21 18:03:03 +03002341 /* Defer clearing up the connection parameters until closing to
2342 * give a chance of keeping them if a repairing happens.
2343 */
2344 set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
2345
Johan Hedbergfc643612015-10-22 09:38:31 +03002346 /* Disable auto-connection parameters if present */
2347 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type);
2348 if (params) {
2349 if (params->explicit_connect)
2350 params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
2351 else
2352 params->auto_connect = HCI_AUTO_CONN_DISABLED;
2353 }
2354
Johan Hedbergec182f02015-10-21 18:03:03 +03002355 /* If disconnection is not requested, then clear the connection
2356 * variable so that the link is not terminated.
2357 */
2358 if (!cp->disconnect)
2359 conn = NULL;
2360
2361done:
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002362 /* If the connection variable is set, then termination of the
2363 * link is requested.
2364 */
Johan Hedberga8a1d192011-11-10 15:54:38 +02002365 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002366 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
2367 &rp, sizeof(rp));
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002368 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002369 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002370 }
2371
Johan Hedberg124f6e32012-02-09 13:50:12 +02002372 cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002373 sizeof(*cp));
Johan Hedberga8a1d192011-11-10 15:54:38 +02002374 if (!cmd) {
2375 err = -ENOMEM;
2376 goto unlock;
2377 }
2378
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02002379 cmd->cmd_complete = addr_cmd_complete;
2380
Johan Hedberg89e0ccc2015-10-22 10:49:38 +03002381 err = hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002382 if (err < 0)
2383 mgmt_pending_remove(cmd);
2384
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002385unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002386 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002387 return err;
2388}
2389
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002390static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002391 u16 len)
Johan Hedberg8962ee72011-01-20 12:40:27 +02002392{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002393 struct mgmt_cp_disconnect *cp = data;
Johan Hedberg06a63b12013-01-20 14:27:21 +02002394 struct mgmt_rp_disconnect rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002395 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002396 struct hci_conn *conn;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002397 int err;
2398
2399 BT_DBG("");
2400
Johan Hedberg06a63b12013-01-20 14:27:21 +02002401 memset(&rp, 0, sizeof(rp));
2402 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2403 rp.addr.type = cp->addr.type;
2404
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002405 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002406 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2407 MGMT_STATUS_INVALID_PARAMS,
2408 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002409
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002410 hci_dev_lock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002411
2412 if (!test_bit(HCI_UP, &hdev->flags)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002413 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2414 MGMT_STATUS_NOT_POWERED, &rp,
2415 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002416 goto failed;
2417 }
2418
Johan Hedberg333ae952015-03-17 13:48:47 +02002419 if (pending_find(MGMT_OP_DISCONNECT, hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002420 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2421 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002422 goto failed;
2423 }
2424
Andre Guedes591f47f2012-04-24 21:02:49 -03002425 if (cp->addr.type == BDADDR_BREDR)
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03002426 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2427 &cp->addr.bdaddr);
Johan Hedberg88c3df12012-02-09 14:27:38 +02002428 else
Johan Hedberg9d4c1cc2015-10-21 18:03:01 +03002429 conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr,
2430 le_addr_type(cp->addr.type));
Vinicius Costa Gomes365227e2011-05-06 18:41:44 -03002431
Vishal Agarwalf9607272012-06-13 05:32:43 +05302432 if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002433 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2434 MGMT_STATUS_NOT_CONNECTED, &rp,
2435 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002436 goto failed;
2437 }
2438
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002439 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03002440 if (!cmd) {
2441 err = -ENOMEM;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002442 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03002443 }
Johan Hedberg8962ee72011-01-20 12:40:27 +02002444
Johan Hedbergf5818c22014-12-05 13:36:02 +02002445 cmd->cmd_complete = generic_cmd_complete;
2446
Johan Hedberge3f2f922014-08-18 20:33:33 +03002447 err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002448 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03002449 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002450
2451failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002452 hci_dev_unlock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002453 return err;
2454}
2455
Andre Guedes57c14772012-04-24 21:02:50 -03002456static u8 link_to_bdaddr(u8 link_type, u8 addr_type)
Johan Hedberg4c659c32011-11-07 23:13:39 +02002457{
2458 switch (link_type) {
2459 case LE_LINK:
Johan Hedberg48264f02011-11-09 13:58:58 +02002460 switch (addr_type) {
2461 case ADDR_LE_DEV_PUBLIC:
Andre Guedes591f47f2012-04-24 21:02:49 -03002462 return BDADDR_LE_PUBLIC;
Andre Guedes0ed09142012-04-03 08:46:54 -03002463
Johan Hedberg48264f02011-11-09 13:58:58 +02002464 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03002465 /* Fallback to LE Random address type */
Andre Guedes591f47f2012-04-24 21:02:49 -03002466 return BDADDR_LE_RANDOM;
Johan Hedberg48264f02011-11-09 13:58:58 +02002467 }
Andre Guedes0ed09142012-04-03 08:46:54 -03002468
Johan Hedberg4c659c32011-11-07 23:13:39 +02002469 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03002470 /* Fallback to BR/EDR type */
Andre Guedes591f47f2012-04-24 21:02:49 -03002471 return BDADDR_BREDR;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002472 }
2473}
2474
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002475static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
2476 u16 data_len)
Johan Hedberg2784eb42011-01-21 13:56:35 +02002477{
Johan Hedberg2784eb42011-01-21 13:56:35 +02002478 struct mgmt_rp_get_connections *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002479 struct hci_conn *c;
Johan Hedberga38528f2011-01-22 06:46:43 +02002480 size_t rp_len;
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002481 int err;
2482 u16 i;
Johan Hedberg2784eb42011-01-21 13:56:35 +02002483
2484 BT_DBG("");
2485
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002486 hci_dev_lock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002487
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002488 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002489 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
2490 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002491 goto unlock;
2492 }
2493
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002494 i = 0;
Johan Hedbergb644ba32012-01-17 21:48:47 +02002495 list_for_each_entry(c, &hdev->conn_hash.list, list) {
2496 if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002497 i++;
Johan Hedberg2784eb42011-01-21 13:56:35 +02002498 }
2499
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002500 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Andre Guedes92c4c202012-06-07 19:05:44 -03002501 rp = kmalloc(rp_len, GFP_KERNEL);
Johan Hedberga38528f2011-01-22 06:46:43 +02002502 if (!rp) {
Johan Hedberg2784eb42011-01-21 13:56:35 +02002503 err = -ENOMEM;
2504 goto unlock;
2505 }
2506
Johan Hedberg2784eb42011-01-21 13:56:35 +02002507 i = 0;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002508 list_for_each_entry(c, &hdev->conn_hash.list, list) {
Johan Hedbergb644ba32012-01-17 21:48:47 +02002509 if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
2510 continue;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002511 bacpy(&rp->addr[i].bdaddr, &c->dst);
Andre Guedes57c14772012-04-24 21:02:50 -03002512 rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type);
Andre Guedes0ed09142012-04-03 08:46:54 -03002513 if (c->type == SCO_LINK || c->type == ESCO_LINK)
Johan Hedberg4c659c32011-11-07 23:13:39 +02002514 continue;
2515 i++;
2516 }
2517
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02002518 rp->conn_count = cpu_to_le16(i);
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002519
Johan Hedberg4c659c32011-11-07 23:13:39 +02002520 /* Recalculate length in case of filtered SCO connections, etc */
2521 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Johan Hedberg2784eb42011-01-21 13:56:35 +02002522
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002523 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
2524 rp_len);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002525
Johan Hedberga38528f2011-01-22 06:46:43 +02002526 kfree(rp);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002527
2528unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002529 hci_dev_unlock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002530 return err;
2531}
2532
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002533static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002534 struct mgmt_cp_pin_code_neg_reply *cp)
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002535{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002536 struct mgmt_pending_cmd *cmd;
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002537 int err;
2538
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002539 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002540 sizeof(*cp));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002541 if (!cmd)
2542 return -ENOMEM;
2543
Arek Lichwadd7e39b2016-09-22 14:08:05 +02002544 cmd->cmd_complete = addr_cmd_complete;
2545
Johan Hedbergd8457692012-02-17 14:24:57 +02002546 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002547 sizeof(cp->addr.bdaddr), &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002548 if (err < 0)
2549 mgmt_pending_remove(cmd);
2550
2551 return err;
2552}
2553
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002554static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002555 u16 len)
Johan Hedberg980e1a52011-01-22 06:10:07 +02002556{
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002557 struct hci_conn *conn;
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002558 struct mgmt_cp_pin_code_reply *cp = data;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002559 struct hci_cp_pin_code_reply reply;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002560 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002561 int err;
2562
2563 BT_DBG("");
2564
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002565 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002566
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002567 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002568 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
2569 MGMT_STATUS_NOT_POWERED);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002570 goto failed;
2571 }
2572
Johan Hedbergd8457692012-02-17 14:24:57 +02002573 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002574 if (!conn) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002575 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
2576 MGMT_STATUS_NOT_CONNECTED);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002577 goto failed;
2578 }
2579
2580 if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) {
Johan Hedbergd8457692012-02-17 14:24:57 +02002581 struct mgmt_cp_pin_code_neg_reply ncp;
2582
2583 memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002584
2585 BT_ERR("PIN code is not 16 bytes long");
2586
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002587 err = send_pin_code_neg_reply(sk, hdev, &ncp);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002588 if (err >= 0)
Johan Hedberga69e8372015-03-06 21:08:53 +02002589 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
2590 MGMT_STATUS_INVALID_PARAMS);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002591
2592 goto failed;
2593 }
2594
Gustavo F. Padovan00abfe42012-03-01 00:37:10 -03002595 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03002596 if (!cmd) {
2597 err = -ENOMEM;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002598 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03002599 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02002600
Johan Hedberg7776d1d2014-12-05 13:36:03 +02002601 cmd->cmd_complete = addr_cmd_complete;
2602
Johan Hedbergd8457692012-02-17 14:24:57 +02002603 bacpy(&reply.bdaddr, &cp->addr.bdaddr);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002604 reply.pin_len = cp->pin_len;
Waldemar Rymarkiewicz24718ca2011-06-01 17:28:47 +02002605 memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code));
Johan Hedberg980e1a52011-01-22 06:10:07 +02002606
2607 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
2608 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03002609 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002610
2611failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002612 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002613 return err;
2614}
2615
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002616static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
2617 u16 len)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002618{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002619 struct mgmt_cp_set_io_capability *cp = data;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002620
2621 BT_DBG("");
2622
Johan Hedberg4ec86d42014-06-17 15:14:48 +03002623 if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY)
Marcel Holtmann9db5c622016-08-29 06:31:57 +02002624 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY,
2625 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ec86d42014-06-17 15:14:48 +03002626
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002627 hci_dev_lock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002628
2629 hdev->io_capability = cp->io_capability;
2630
2631 BT_DBG("%s IO capability set to 0x%02x", hdev->name,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002632 hdev->io_capability);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002633
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002634 hci_dev_unlock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002635
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002636 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0,
2637 NULL, 0);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002638}
2639
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002640static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn)
Johan Hedberge9a416b2011-02-19 12:05:56 -03002641{
2642 struct hci_dev *hdev = conn->hdev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002643 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002644
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002645 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
Johan Hedberge9a416b2011-02-19 12:05:56 -03002646 if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
2647 continue;
2648
Johan Hedberge9a416b2011-02-19 12:05:56 -03002649 if (cmd->user_data != conn)
2650 continue;
2651
2652 return cmd;
2653 }
2654
2655 return NULL;
2656}
2657
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002658static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberge9a416b2011-02-19 12:05:56 -03002659{
2660 struct mgmt_rp_pair_device rp;
2661 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9df74652014-12-19 22:26:03 +02002662 int err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002663
Johan Hedberg61b1a7f2014-03-20 12:54:16 +02002664 bacpy(&rp.addr.bdaddr, &conn->dst);
2665 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002666
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002667 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE,
2668 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03002669
2670 /* So we don't get further callbacks for this connection */
2671 conn->connect_cfm_cb = NULL;
2672 conn->security_cfm_cb = NULL;
2673 conn->disconn_cfm_cb = NULL;
2674
David Herrmann76a68ba2013-04-06 20:28:37 +02002675 hci_conn_drop(conn);
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002676
2677 /* The device is paired so there is no need to remove
2678 * its connection parameters anymore.
2679 */
2680 clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
Johan Hedberg15013ae2014-12-11 21:45:44 +02002681
2682 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02002683
2684 return err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002685}
2686
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002687void mgmt_smp_complete(struct hci_conn *conn, bool complete)
2688{
2689 u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002690 struct mgmt_pending_cmd *cmd;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002691
2692 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02002693 if (cmd) {
Johan Hedberg04ab2742014-12-05 13:36:04 +02002694 cmd->cmd_complete(cmd, status);
Johan Hedberga511b352014-12-11 21:45:45 +02002695 mgmt_pending_remove(cmd);
2696 }
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002697}
2698
Johan Hedberge9a416b2011-02-19 12:05:56 -03002699static void pairing_complete_cb(struct hci_conn *conn, u8 status)
2700{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002701 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002702
2703 BT_DBG("status %u", status);
2704
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002705 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02002706 if (!cmd) {
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002707 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02002708 return;
2709 }
2710
2711 cmd->cmd_complete(cmd, mgmt_status(status));
2712 mgmt_pending_remove(cmd);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002713}
2714
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002715static void le_pairing_complete_cb(struct hci_conn *conn, u8 status)
Vishal Agarwal4c47d732012-06-07 20:27:35 +05302716{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002717 struct mgmt_pending_cmd *cmd;
Vishal Agarwal4c47d732012-06-07 20:27:35 +05302718
2719 BT_DBG("status %u", status);
2720
2721 if (!status)
2722 return;
2723
2724 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02002725 if (!cmd) {
Vishal Agarwal4c47d732012-06-07 20:27:35 +05302726 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02002727 return;
2728 }
2729
2730 cmd->cmd_complete(cmd, mgmt_status(status));
2731 mgmt_pending_remove(cmd);
Vishal Agarwal4c47d732012-06-07 20:27:35 +05302732}
2733
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002734static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002735 u16 len)
Johan Hedberge9a416b2011-02-19 12:05:56 -03002736{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002737 struct mgmt_cp_pair_device *cp = data;
Johan Hedberg1425acb2011-11-11 00:07:35 +02002738 struct mgmt_rp_pair_device rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002739 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002740 u8 sec_level, auth_type;
2741 struct hci_conn *conn;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002742 int err;
2743
2744 BT_DBG("");
2745
Szymon Jancf950a30e2013-01-18 12:48:07 +01002746 memset(&rp, 0, sizeof(rp));
2747 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2748 rp.addr.type = cp->addr.type;
2749
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002750 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002751 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2752 MGMT_STATUS_INVALID_PARAMS,
2753 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002754
Johan Hedberg4ec86d42014-06-17 15:14:48 +03002755 if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002756 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2757 MGMT_STATUS_INVALID_PARAMS,
2758 &rp, sizeof(rp));
Johan Hedberg4ec86d42014-06-17 15:14:48 +03002759
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002760 hci_dev_lock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002761
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002762 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002763 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2764 MGMT_STATUS_NOT_POWERED, &rp,
2765 sizeof(rp));
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002766 goto unlock;
2767 }
2768
Johan Hedberg55e76b32015-03-10 22:34:40 +02002769 if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) {
2770 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2771 MGMT_STATUS_ALREADY_PAIRED, &rp,
2772 sizeof(rp));
2773 goto unlock;
2774 }
2775
Vinicius Costa Gomesc908df32011-09-02 14:51:22 -03002776 sec_level = BT_SECURITY_MEDIUM;
Mikel Astiz6fd6b912014-04-08 14:21:32 +02002777 auth_type = HCI_AT_DEDICATED_BONDING;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002778
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002779 if (cp->addr.type == BDADDR_BREDR) {
Andre Guedes04a6c582014-02-26 20:21:44 -03002780 conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
2781 auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002782 } else {
Johan Hedberg85813a72015-10-21 18:02:59 +03002783 u8 addr_type = le_addr_type(cp->addr.type);
Jakub Pawlowski5157b8a2015-10-16 10:07:54 +03002784 struct hci_conn_params *p;
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002785
Marcel Holtmann7c264b12014-06-30 12:34:40 +02002786 /* When pairing a new device, it is expected to remember
2787 * this device for future connections. Adding the connection
2788 * parameter information ahead of time allows tracking
2789 * of the slave preferred values and will speed up any
2790 * further connection establishment.
2791 *
2792 * If connection parameters already exist, then they
2793 * will be kept and this function does nothing.
2794 */
Jakub Pawlowski5157b8a2015-10-16 10:07:54 +03002795 p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type);
2796
2797 if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT)
2798 p->auto_connect = HCI_AUTO_CONN_DISABLED;
Marcel Holtmann7c264b12014-06-30 12:34:40 +02002799
Jakub Pawlowskifa142222015-08-07 20:22:56 +02002800 conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr,
2801 addr_type, sec_level,
Johan Hedberg0ad06aa2015-11-11 14:44:57 +02002802 HCI_LE_CONN_TIMEOUT);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002803 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03002804
Ville Tervo30e76272011-02-22 16:10:53 -03002805 if (IS_ERR(conn)) {
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02002806 int status;
2807
2808 if (PTR_ERR(conn) == -EBUSY)
2809 status = MGMT_STATUS_BUSY;
Lukasz Rymanowskifaa81032015-02-11 12:31:42 +01002810 else if (PTR_ERR(conn) == -EOPNOTSUPP)
2811 status = MGMT_STATUS_NOT_SUPPORTED;
2812 else if (PTR_ERR(conn) == -ECONNREFUSED)
2813 status = MGMT_STATUS_REJECTED;
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02002814 else
2815 status = MGMT_STATUS_CONNECT_FAILED;
2816
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002817 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2818 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03002819 goto unlock;
2820 }
2821
2822 if (conn->connect_cfm_cb) {
David Herrmann76a68ba2013-04-06 20:28:37 +02002823 hci_conn_drop(conn);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002824 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2825 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03002826 goto unlock;
2827 }
2828
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002829 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002830 if (!cmd) {
2831 err = -ENOMEM;
David Herrmann76a68ba2013-04-06 20:28:37 +02002832 hci_conn_drop(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002833 goto unlock;
2834 }
2835
Johan Hedberg04ab2742014-12-05 13:36:04 +02002836 cmd->cmd_complete = pairing_complete;
2837
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03002838 /* For LE, just connecting isn't a proof that the pairing finished */
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002839 if (cp->addr.type == BDADDR_BREDR) {
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03002840 conn->connect_cfm_cb = pairing_complete_cb;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002841 conn->security_cfm_cb = pairing_complete_cb;
2842 conn->disconn_cfm_cb = pairing_complete_cb;
2843 } else {
2844 conn->connect_cfm_cb = le_pairing_complete_cb;
2845 conn->security_cfm_cb = le_pairing_complete_cb;
2846 conn->disconn_cfm_cb = le_pairing_complete_cb;
2847 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03002848
Johan Hedberge9a416b2011-02-19 12:05:56 -03002849 conn->io_capability = cp->io_cap;
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03002850 cmd->user_data = hci_conn_get(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002851
Johan Hedberg6f78fd42014-07-30 08:35:48 +03002852 if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) &&
Johan Hedberga511b352014-12-11 21:45:45 +02002853 hci_conn_security(conn, sec_level, auth_type, true)) {
2854 cmd->cmd_complete(cmd, 0);
2855 mgmt_pending_remove(cmd);
2856 }
Johan Hedberge9a416b2011-02-19 12:05:56 -03002857
2858 err = 0;
2859
2860unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002861 hci_dev_unlock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002862 return err;
2863}
2864
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002865static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
2866 u16 len)
Johan Hedberg28424702012-02-02 04:02:29 +02002867{
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02002868 struct mgmt_addr_info *addr = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002869 struct mgmt_pending_cmd *cmd;
Johan Hedberg28424702012-02-02 04:02:29 +02002870 struct hci_conn *conn;
2871 int err;
2872
2873 BT_DBG("");
2874
Johan Hedberg28424702012-02-02 04:02:29 +02002875 hci_dev_lock(hdev);
2876
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002877 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002878 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
2879 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002880 goto unlock;
2881 }
2882
Johan Hedberg333ae952015-03-17 13:48:47 +02002883 cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02002884 if (!cmd) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002885 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
2886 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02002887 goto unlock;
2888 }
2889
2890 conn = cmd->user_data;
2891
2892 if (bacmp(&addr->bdaddr, &conn->dst) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002893 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
2894 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02002895 goto unlock;
2896 }
2897
Johan Hedberga511b352014-12-11 21:45:45 +02002898 cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED);
2899 mgmt_pending_remove(cmd);
Johan Hedberg28424702012-02-02 04:02:29 +02002900
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002901 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
2902 addr, sizeof(*addr));
Johan Hedberg28424702012-02-02 04:02:29 +02002903unlock:
2904 hci_dev_unlock(hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02002905 return err;
2906}
2907
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002908static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
Johan Hedberg1707c602013-03-15 17:07:15 -05002909 struct mgmt_addr_info *addr, u16 mgmt_op,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002910 u16 hci_op, __le32 passkey)
Johan Hedberga5c29682011-02-19 12:05:57 -03002911{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002912 struct mgmt_pending_cmd *cmd;
Brian Gix0df4c182011-11-16 13:53:13 -08002913 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03002914 int err;
2915
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002916 hci_dev_lock(hdev);
Johan Hedberg08ba5382011-03-16 14:29:34 +02002917
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002918 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002919 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
2920 MGMT_STATUS_NOT_POWERED, addr,
2921 sizeof(*addr));
Brian Gix0df4c182011-11-16 13:53:13 -08002922 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03002923 }
2924
Johan Hedberg1707c602013-03-15 17:07:15 -05002925 if (addr->type == BDADDR_BREDR)
2926 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr);
Johan Hedberg272d90d2012-02-09 15:26:12 +02002927 else
Johan Hedberg9d4c1cc2015-10-21 18:03:01 +03002928 conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr,
2929 le_addr_type(addr->type));
Brian Gix47c15e22011-11-16 13:53:14 -08002930
Johan Hedberg272d90d2012-02-09 15:26:12 +02002931 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002932 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
2933 MGMT_STATUS_NOT_CONNECTED, addr,
2934 sizeof(*addr));
Johan Hedberg272d90d2012-02-09 15:26:12 +02002935 goto done;
2936 }
2937
Johan Hedberg1707c602013-03-15 17:07:15 -05002938 if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
Brian Gix5fe57d92011-12-21 16:12:13 -08002939 err = smp_user_confirm_reply(conn, mgmt_op, passkey);
Brian Gix5fe57d92011-12-21 16:12:13 -08002940 if (!err)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002941 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
2942 MGMT_STATUS_SUCCESS, addr,
2943 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08002944 else
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002945 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
2946 MGMT_STATUS_FAILED, addr,
2947 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08002948
Brian Gix47c15e22011-11-16 13:53:14 -08002949 goto done;
2950 }
2951
Johan Hedberg1707c602013-03-15 17:07:15 -05002952 cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr));
Johan Hedberga5c29682011-02-19 12:05:57 -03002953 if (!cmd) {
2954 err = -ENOMEM;
Brian Gix0df4c182011-11-16 13:53:13 -08002955 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03002956 }
2957
Johan Hedberg7776d1d2014-12-05 13:36:03 +02002958 cmd->cmd_complete = addr_cmd_complete;
2959
Brian Gix0df4c182011-11-16 13:53:13 -08002960 /* Continue with pairing via HCI */
Brian Gix604086b2011-11-23 08:28:33 -08002961 if (hci_op == HCI_OP_USER_PASSKEY_REPLY) {
2962 struct hci_cp_user_passkey_reply cp;
2963
Johan Hedberg1707c602013-03-15 17:07:15 -05002964 bacpy(&cp.bdaddr, &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08002965 cp.passkey = passkey;
2966 err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
2967 } else
Johan Hedberg1707c602013-03-15 17:07:15 -05002968 err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr),
2969 &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08002970
Johan Hedberga664b5b2011-02-19 12:06:02 -03002971 if (err < 0)
2972 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03002973
Brian Gix0df4c182011-11-16 13:53:13 -08002974done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002975 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03002976 return err;
2977}
2978
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05302979static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
2980 void *data, u16 len)
2981{
2982 struct mgmt_cp_pin_code_neg_reply *cp = data;
2983
2984 BT_DBG("");
2985
Johan Hedberg1707c602013-03-15 17:07:15 -05002986 return user_pairing_resp(sk, hdev, &cp->addr,
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05302987 MGMT_OP_PIN_CODE_NEG_REPLY,
2988 HCI_OP_PIN_CODE_NEG_REPLY, 0);
2989}
2990
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002991static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
2992 u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08002993{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002994 struct mgmt_cp_user_confirm_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08002995
2996 BT_DBG("");
2997
2998 if (len != sizeof(*cp))
Johan Hedberga69e8372015-03-06 21:08:53 +02002999 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
3000 MGMT_STATUS_INVALID_PARAMS);
Brian Gix0df4c182011-11-16 13:53:13 -08003001
Johan Hedberg1707c602013-03-15 17:07:15 -05003002 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003003 MGMT_OP_USER_CONFIRM_REPLY,
3004 HCI_OP_USER_CONFIRM_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003005}
3006
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003007static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003008 void *data, u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003009{
Johan Hedbergc9c26592011-12-15 00:47:41 +02003010 struct mgmt_cp_user_confirm_neg_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003011
3012 BT_DBG("");
3013
Johan Hedberg1707c602013-03-15 17:07:15 -05003014 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003015 MGMT_OP_USER_CONFIRM_NEG_REPLY,
3016 HCI_OP_USER_CONFIRM_NEG_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003017}
3018
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003019static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3020 u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003021{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003022 struct mgmt_cp_user_passkey_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003023
3024 BT_DBG("");
3025
Johan Hedberg1707c602013-03-15 17:07:15 -05003026 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003027 MGMT_OP_USER_PASSKEY_REPLY,
3028 HCI_OP_USER_PASSKEY_REPLY, cp->passkey);
Brian Gix604086b2011-11-23 08:28:33 -08003029}
3030
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003031static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003032 void *data, u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003033{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003034 struct mgmt_cp_user_passkey_neg_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003035
3036 BT_DBG("");
3037
Johan Hedberg1707c602013-03-15 17:07:15 -05003038 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003039 MGMT_OP_USER_PASSKEY_NEG_REPLY,
3040 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
Brian Gix604086b2011-11-23 08:28:33 -08003041}
3042
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02003043static void adv_expire(struct hci_dev *hdev, u32 flags)
3044{
3045 struct adv_info *adv_instance;
3046 struct hci_request req;
3047 int err;
3048
3049 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
3050 if (!adv_instance)
3051 return;
3052
3053 /* stop if current instance doesn't need to be changed */
3054 if (!(adv_instance->flags & flags))
3055 return;
3056
3057 cancel_adv_timeout(hdev);
3058
3059 adv_instance = hci_get_next_instance(hdev, adv_instance->instance);
3060 if (!adv_instance)
3061 return;
3062
3063 hci_req_init(&req, hdev);
3064 err = __hci_req_schedule_adv_instance(&req, adv_instance->instance,
3065 true);
3066 if (err)
3067 return;
3068
3069 hci_req_run(&req, NULL);
3070}
3071
Marcel Holtmann1904a852015-01-11 13:50:44 -08003072static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg13928972013-03-15 17:07:00 -05003073{
3074 struct mgmt_cp_set_local_name *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003075 struct mgmt_pending_cmd *cmd;
Johan Hedberg13928972013-03-15 17:07:00 -05003076
3077 BT_DBG("status 0x%02x", status);
3078
3079 hci_dev_lock(hdev);
3080
Johan Hedberg333ae952015-03-17 13:48:47 +02003081 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05003082 if (!cmd)
3083 goto unlock;
3084
3085 cp = cmd->param;
3086
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02003087 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003088 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
3089 mgmt_status(status));
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02003090 } else {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003091 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3092 cp, sizeof(*cp));
Johan Hedberg13928972013-03-15 17:07:00 -05003093
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02003094 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
3095 adv_expire(hdev, MGMT_ADV_FLAG_LOCAL_NAME);
3096 }
3097
Johan Hedberg13928972013-03-15 17:07:00 -05003098 mgmt_pending_remove(cmd);
3099
3100unlock:
3101 hci_dev_unlock(hdev);
3102}
3103
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003104static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003105 u16 len)
Johan Hedbergb312b1612011-03-16 14:29:37 +02003106{
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003107 struct mgmt_cp_set_local_name *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003108 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05003109 struct hci_request req;
Johan Hedbergb312b1612011-03-16 14:29:37 +02003110 int err;
3111
3112 BT_DBG("");
3113
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003114 hci_dev_lock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003115
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003116 /* If the old values are the same as the new ones just return a
3117 * direct command complete event.
3118 */
3119 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
3120 !memcmp(hdev->short_name, cp->short_name,
3121 sizeof(hdev->short_name))) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003122 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3123 data, len);
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003124 goto failed;
3125 }
3126
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003127 memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003128
Johan Hedbergb5235a62012-02-21 14:32:24 +02003129 if (!hdev_is_powered(hdev)) {
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003130 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003131
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003132 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3133 data, len);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003134 if (err < 0)
3135 goto failed;
3136
Marcel Holtmann5504c3a2016-08-29 06:19:46 +02003137 err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data,
3138 len, HCI_MGMT_LOCAL_NAME_EVENTS, sk);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02003139 ext_info_changed(hdev, sk);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003140
Johan Hedbergb5235a62012-02-21 14:32:24 +02003141 goto failed;
3142 }
3143
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003144 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003145 if (!cmd) {
3146 err = -ENOMEM;
3147 goto failed;
3148 }
3149
Johan Hedberg13928972013-03-15 17:07:00 -05003150 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3151
Johan Hedberg890ea892013-03-15 17:06:52 -05003152 hci_req_init(&req, hdev);
Johan Hedberg3f985052013-03-15 17:07:02 -05003153
3154 if (lmp_bredr_capable(hdev)) {
Johan Hedberg00cf5042015-11-25 16:15:41 +02003155 __hci_req_update_name(&req);
Johan Hedbergb1a89172015-11-25 16:15:42 +02003156 __hci_req_update_eir(&req);
Johan Hedberg3f985052013-03-15 17:07:02 -05003157 }
3158
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003159 /* The name is stored in the scan response data and so
3160 * no need to udpate the advertising data here.
3161 */
Johan Hedberg3f985052013-03-15 17:07:02 -05003162 if (lmp_le_capable(hdev))
Johan Hedbergcab054a2015-11-30 11:21:45 +02003163 __hci_req_update_scan_rsp_data(&req, hdev->cur_adv_instance);
Johan Hedberg3f985052013-03-15 17:07:02 -05003164
Johan Hedberg13928972013-03-15 17:07:00 -05003165 err = hci_req_run(&req, set_name_complete);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003166 if (err < 0)
3167 mgmt_pending_remove(cmd);
3168
3169failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003170 hci_dev_unlock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003171 return err;
3172}
3173
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02003174static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data,
3175 u16 len)
3176{
3177 struct mgmt_cp_set_appearance *cp = data;
3178 u16 apperance;
3179 int err;
3180
3181 BT_DBG("");
3182
MichaƂ Narajowskiaf4168c2016-09-19 14:33:33 +02003183 if (!lmp_le_capable(hdev))
3184 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE,
3185 MGMT_STATUS_NOT_SUPPORTED);
3186
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02003187 apperance = le16_to_cpu(cp->appearance);
3188
3189 hci_dev_lock(hdev);
3190
3191 if (hdev->appearance != apperance) {
3192 hdev->appearance = apperance;
3193
3194 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
3195 adv_expire(hdev, MGMT_ADV_FLAG_APPEARANCE);
MichaƂ Narajowskie74317f2016-09-19 20:25:56 +02003196
3197 ext_info_changed(hdev, sk);
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02003198 }
3199
3200 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL,
3201 0);
3202
3203 hci_dev_unlock(hdev);
3204
3205 return err;
3206}
3207
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003208static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status,
3209 u16 opcode, struct sk_buff *skb)
3210{
3211 struct mgmt_rp_read_local_oob_data mgmt_rp;
3212 size_t rp_size = sizeof(mgmt_rp);
3213 struct mgmt_pending_cmd *cmd;
3214
3215 BT_DBG("%s status %u", hdev->name, status);
3216
3217 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
3218 if (!cmd)
3219 return;
3220
3221 if (status || !skb) {
3222 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3223 status ? mgmt_status(status) : MGMT_STATUS_FAILED);
3224 goto remove;
3225 }
3226
3227 memset(&mgmt_rp, 0, sizeof(mgmt_rp));
3228
3229 if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
3230 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
3231
3232 if (skb->len < sizeof(*rp)) {
3233 mgmt_cmd_status(cmd->sk, hdev->id,
3234 MGMT_OP_READ_LOCAL_OOB_DATA,
3235 MGMT_STATUS_FAILED);
3236 goto remove;
3237 }
3238
3239 memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash));
3240 memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand));
3241
3242 rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256);
3243 } else {
3244 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
3245
3246 if (skb->len < sizeof(*rp)) {
3247 mgmt_cmd_status(cmd->sk, hdev->id,
3248 MGMT_OP_READ_LOCAL_OOB_DATA,
3249 MGMT_STATUS_FAILED);
3250 goto remove;
3251 }
3252
3253 memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192));
3254 memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192));
3255
3256 memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256));
3257 memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256));
3258 }
3259
3260 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3261 MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size);
3262
3263remove:
3264 mgmt_pending_remove(cmd);
3265}
3266
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003267static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003268 void *data, u16 data_len)
Szymon Jancc35938b2011-03-22 13:12:21 +01003269{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003270 struct mgmt_pending_cmd *cmd;
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003271 struct hci_request req;
Szymon Jancc35938b2011-03-22 13:12:21 +01003272 int err;
3273
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003274 BT_DBG("%s", hdev->name);
Szymon Jancc35938b2011-03-22 13:12:21 +01003275
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003276 hci_dev_lock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003277
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003278 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003279 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3280 MGMT_STATUS_NOT_POWERED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003281 goto unlock;
3282 }
3283
Andre Guedes9a1a1992012-07-24 15:03:48 -03003284 if (!lmp_ssp_capable(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003285 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3286 MGMT_STATUS_NOT_SUPPORTED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003287 goto unlock;
3288 }
3289
Johan Hedberg333ae952015-03-17 13:48:47 +02003290 if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003291 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3292 MGMT_STATUS_BUSY);
Szymon Jancc35938b2011-03-22 13:12:21 +01003293 goto unlock;
3294 }
3295
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003296 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
Szymon Jancc35938b2011-03-22 13:12:21 +01003297 if (!cmd) {
3298 err = -ENOMEM;
3299 goto unlock;
3300 }
3301
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003302 hci_req_init(&req, hdev);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08003303
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003304 if (bredr_sc_enabled(hdev))
3305 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
3306 else
3307 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
3308
3309 err = hci_req_run_skb(&req, read_local_oob_data_complete);
Szymon Jancc35938b2011-03-22 13:12:21 +01003310 if (err < 0)
3311 mgmt_pending_remove(cmd);
3312
3313unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003314 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003315 return err;
3316}
3317
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003318static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003319 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003320{
Johan Hedberg5d57e792015-01-23 10:10:38 +02003321 struct mgmt_addr_info *addr = data;
Szymon Janc2763eda2011-03-22 13:12:22 +01003322 int err;
3323
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003324 BT_DBG("%s ", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003325
Johan Hedberg5d57e792015-01-23 10:10:38 +02003326 if (!bdaddr_type_is_valid(addr->type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003327 return mgmt_cmd_complete(sk, hdev->id,
3328 MGMT_OP_ADD_REMOTE_OOB_DATA,
3329 MGMT_STATUS_INVALID_PARAMS,
3330 addr, sizeof(*addr));
Johan Hedberg5d57e792015-01-23 10:10:38 +02003331
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003332 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003333
Marcel Holtmannec109112014-01-10 02:07:30 -08003334 if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) {
3335 struct mgmt_cp_add_remote_oob_data *cp = data;
3336 u8 status;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003337
Johan Hedbergc19a4952014-11-17 20:52:19 +02003338 if (cp->addr.type != BDADDR_BREDR) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003339 err = mgmt_cmd_complete(sk, hdev->id,
3340 MGMT_OP_ADD_REMOTE_OOB_DATA,
3341 MGMT_STATUS_INVALID_PARAMS,
3342 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02003343 goto unlock;
3344 }
3345
Marcel Holtmannec109112014-01-10 02:07:30 -08003346 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg6928a922014-10-26 20:46:09 +01003347 cp->addr.type, cp->hash,
3348 cp->rand, NULL, NULL);
Marcel Holtmannec109112014-01-10 02:07:30 -08003349 if (err < 0)
3350 status = MGMT_STATUS_FAILED;
3351 else
3352 status = MGMT_STATUS_SUCCESS;
3353
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003354 err = mgmt_cmd_complete(sk, hdev->id,
3355 MGMT_OP_ADD_REMOTE_OOB_DATA, status,
3356 &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08003357 } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) {
3358 struct mgmt_cp_add_remote_oob_ext_data *cp = data;
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003359 u8 *rand192, *hash192, *rand256, *hash256;
Marcel Holtmannec109112014-01-10 02:07:30 -08003360 u8 status;
3361
Johan Hedberg86df9202014-10-26 20:52:27 +01003362 if (bdaddr_type_is_le(cp->addr.type)) {
Johan Hedbergd25b78e2015-01-27 12:55:52 +02003363 /* Enforce zero-valued 192-bit parameters as
3364 * long as legacy SMP OOB isn't implemented.
3365 */
3366 if (memcmp(cp->rand192, ZERO_KEY, 16) ||
3367 memcmp(cp->hash192, ZERO_KEY, 16)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003368 err = mgmt_cmd_complete(sk, hdev->id,
3369 MGMT_OP_ADD_REMOTE_OOB_DATA,
3370 MGMT_STATUS_INVALID_PARAMS,
3371 addr, sizeof(*addr));
Johan Hedbergd25b78e2015-01-27 12:55:52 +02003372 goto unlock;
3373 }
3374
Johan Hedberg86df9202014-10-26 20:52:27 +01003375 rand192 = NULL;
3376 hash192 = NULL;
3377 } else {
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003378 /* In case one of the P-192 values is set to zero,
3379 * then just disable OOB data for P-192.
3380 */
3381 if (!memcmp(cp->rand192, ZERO_KEY, 16) ||
3382 !memcmp(cp->hash192, ZERO_KEY, 16)) {
3383 rand192 = NULL;
3384 hash192 = NULL;
3385 } else {
3386 rand192 = cp->rand192;
3387 hash192 = cp->hash192;
3388 }
3389 }
3390
3391 /* In case one of the P-256 values is set to zero, then just
3392 * disable OOB data for P-256.
3393 */
3394 if (!memcmp(cp->rand256, ZERO_KEY, 16) ||
3395 !memcmp(cp->hash256, ZERO_KEY, 16)) {
3396 rand256 = NULL;
3397 hash256 = NULL;
3398 } else {
3399 rand256 = cp->rand256;
3400 hash256 = cp->hash256;
Johan Hedberg86df9202014-10-26 20:52:27 +01003401 }
3402
Johan Hedberg81328d52014-10-26 20:33:47 +01003403 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg86df9202014-10-26 20:52:27 +01003404 cp->addr.type, hash192, rand192,
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003405 hash256, rand256);
Marcel Holtmannec109112014-01-10 02:07:30 -08003406 if (err < 0)
3407 status = MGMT_STATUS_FAILED;
3408 else
3409 status = MGMT_STATUS_SUCCESS;
3410
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003411 err = mgmt_cmd_complete(sk, hdev->id,
3412 MGMT_OP_ADD_REMOTE_OOB_DATA,
3413 status, &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08003414 } else {
3415 BT_ERR("add_remote_oob_data: invalid length of %u bytes", len);
Johan Hedberga69e8372015-03-06 21:08:53 +02003416 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3417 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannec109112014-01-10 02:07:30 -08003418 }
Szymon Janc2763eda2011-03-22 13:12:22 +01003419
Johan Hedbergc19a4952014-11-17 20:52:19 +02003420unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003421 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003422 return err;
3423}
3424
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003425static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003426 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003427{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003428 struct mgmt_cp_remove_remote_oob_data *cp = data;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003429 u8 status;
Szymon Janc2763eda2011-03-22 13:12:22 +01003430 int err;
3431
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003432 BT_DBG("%s", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003433
Johan Hedbergc19a4952014-11-17 20:52:19 +02003434 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003435 return mgmt_cmd_complete(sk, hdev->id,
3436 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
3437 MGMT_STATUS_INVALID_PARAMS,
3438 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02003439
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003440 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003441
Johan Hedbergeedbd582014-11-15 09:34:23 +02003442 if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
3443 hci_remote_oob_data_clear(hdev);
3444 status = MGMT_STATUS_SUCCESS;
3445 goto done;
3446 }
3447
Johan Hedberg6928a922014-10-26 20:46:09 +01003448 err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type);
Szymon Janc2763eda2011-03-22 13:12:22 +01003449 if (err < 0)
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003450 status = MGMT_STATUS_INVALID_PARAMS;
Szymon Janc2763eda2011-03-22 13:12:22 +01003451 else
Szymon Janca6785be2012-12-13 15:11:21 +01003452 status = MGMT_STATUS_SUCCESS;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003453
Johan Hedbergeedbd582014-11-15 09:34:23 +02003454done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003455 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
3456 status, &cp->addr, sizeof(cp->addr));
Szymon Janc2763eda2011-03-22 13:12:22 +01003457
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003458 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003459 return err;
3460}
3461
Johan Hedberge68f0722015-11-11 08:30:30 +02003462void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status)
Andre Guedes7c307722013-04-30 15:29:28 -03003463{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003464 struct mgmt_pending_cmd *cmd;
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01003465
Andre Guedes7c307722013-04-30 15:29:28 -03003466 BT_DBG("status %d", status);
3467
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003468 hci_dev_lock(hdev);
3469
Johan Hedberg333ae952015-03-17 13:48:47 +02003470 cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003471 if (!cmd)
Johan Hedberg333ae952015-03-17 13:48:47 +02003472 cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003473
Johan Hedberg78b781c2016-01-05 13:19:32 +02003474 if (!cmd)
3475 cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev);
3476
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003477 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02003478 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003479 mgmt_pending_remove(cmd);
Andre Guedes7c307722013-04-30 15:29:28 -03003480 }
3481
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003482 hci_dev_unlock(hdev);
Andre Guedes7c307722013-04-30 15:29:28 -03003483}
3484
Johan Hedberg591752a2015-11-11 08:11:24 +02003485static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type,
3486 uint8_t *mgmt_status)
3487{
3488 switch (type) {
3489 case DISCOV_TYPE_LE:
3490 *mgmt_status = mgmt_le_support(hdev);
3491 if (*mgmt_status)
3492 return false;
3493 break;
3494 case DISCOV_TYPE_INTERLEAVED:
3495 *mgmt_status = mgmt_le_support(hdev);
3496 if (*mgmt_status)
3497 return false;
3498 /* Intentional fall-through */
3499 case DISCOV_TYPE_BREDR:
3500 *mgmt_status = mgmt_bredr_support(hdev);
3501 if (*mgmt_status)
3502 return false;
3503 break;
3504 default:
3505 *mgmt_status = MGMT_STATUS_INVALID_PARAMS;
3506 return false;
3507 }
3508
3509 return true;
3510}
3511
Johan Hedberg78b781c2016-01-05 13:19:32 +02003512static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev,
3513 u16 op, void *data, u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04003514{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003515 struct mgmt_cp_start_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003516 struct mgmt_pending_cmd *cmd;
Marcel Holtmann80190442014-12-04 11:36:36 +01003517 u8 status;
Johan Hedberg14a53662011-04-27 10:29:56 -04003518 int err;
3519
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003520 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04003521
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003522 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04003523
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003524 if (!hdev_is_powered(hdev)) {
Johan Hedberg78b781c2016-01-05 13:19:32 +02003525 err = mgmt_cmd_complete(sk, hdev->id, op,
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003526 MGMT_STATUS_NOT_POWERED,
3527 &cp->type, sizeof(cp->type));
Johan Hedbergbd2d1332011-11-07 23:13:37 +02003528 goto failed;
3529 }
3530
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01003531 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003532 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg78b781c2016-01-05 13:19:32 +02003533 err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY,
3534 &cp->type, sizeof(cp->type));
Andre Guedes642be6c2012-03-21 00:03:37 -03003535 goto failed;
3536 }
3537
Johan Hedberg591752a2015-11-11 08:11:24 +02003538 if (!discovery_type_is_valid(hdev, cp->type, &status)) {
Johan Hedberg78b781c2016-01-05 13:19:32 +02003539 err = mgmt_cmd_complete(sk, hdev->id, op, status,
3540 &cp->type, sizeof(cp->type));
Johan Hedberg591752a2015-11-11 08:11:24 +02003541 goto failed;
3542 }
3543
Marcel Holtmann22078802014-12-05 11:45:22 +01003544 /* Clear the discovery filter first to free any previously
3545 * allocated memory for the UUID list.
3546 */
3547 hci_discovery_filter_clear(hdev);
3548
Andre Guedes4aab14e2012-02-17 20:39:36 -03003549 hdev->discovery.type = cp->type;
Marcel Holtmannda25cf62014-12-05 13:03:35 +01003550 hdev->discovery.report_invalid_rssi = false;
Johan Hedberg78b781c2016-01-05 13:19:32 +02003551 if (op == MGMT_OP_START_LIMITED_DISCOVERY)
3552 hdev->discovery.limited = true;
3553 else
3554 hdev->discovery.limited = false;
Andre Guedes4aab14e2012-02-17 20:39:36 -03003555
Johan Hedberg78b781c2016-01-05 13:19:32 +02003556 cmd = mgmt_pending_add(sk, op, hdev, data, len);
Johan Hedberge68f0722015-11-11 08:30:30 +02003557 if (!cmd) {
3558 err = -ENOMEM;
Johan Hedberg04106752013-01-10 14:54:09 +02003559 goto failed;
Andre Guedesf39799f2012-02-17 20:39:35 -03003560 }
Andre Guedes3fd24152012-02-03 17:48:01 -03003561
Johan Hedberge68f0722015-11-11 08:30:30 +02003562 cmd->cmd_complete = generic_cmd_complete;
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01003563
3564 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
Johan Hedberge68f0722015-11-11 08:30:30 +02003565 queue_work(hdev->req_workqueue, &hdev->discov_update);
3566 err = 0;
Johan Hedberg14a53662011-04-27 10:29:56 -04003567
3568failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003569 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04003570 return err;
3571}
3572
Johan Hedberg78b781c2016-01-05 13:19:32 +02003573static int start_discovery(struct sock *sk, struct hci_dev *hdev,
3574 void *data, u16 len)
3575{
3576 return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY,
3577 data, len);
3578}
3579
3580static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev,
3581 void *data, u16 len)
3582{
3583 return start_discovery_internal(sk, hdev,
3584 MGMT_OP_START_LIMITED_DISCOVERY,
3585 data, len);
3586}
3587
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003588static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd,
3589 u8 status)
Andre Guedes1183fdc2013-04-30 15:29:35 -03003590{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003591 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
3592 cmd->param, 1);
Johan Hedberg2922a942014-12-05 13:36:06 +02003593}
3594
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003595static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
3596 void *data, u16 len)
3597{
3598 struct mgmt_cp_start_service_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003599 struct mgmt_pending_cmd *cmd;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003600 const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16);
3601 u16 uuid_count, expected_len;
3602 u8 status;
Andre Guedes1183fdc2013-04-30 15:29:35 -03003603 int err;
3604
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003605 BT_DBG("%s", hdev->name);
Andre Guedes1183fdc2013-04-30 15:29:35 -03003606
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003607 hci_dev_lock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03003608
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003609 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003610 err = mgmt_cmd_complete(sk, hdev->id,
3611 MGMT_OP_START_SERVICE_DISCOVERY,
3612 MGMT_STATUS_NOT_POWERED,
3613 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003614 goto failed;
3615 }
3616
3617 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003618 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003619 err = mgmt_cmd_complete(sk, hdev->id,
3620 MGMT_OP_START_SERVICE_DISCOVERY,
3621 MGMT_STATUS_BUSY, &cp->type,
3622 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003623 goto failed;
3624 }
3625
3626 uuid_count = __le16_to_cpu(cp->uuid_count);
3627 if (uuid_count > max_uuid_count) {
3628 BT_ERR("service_discovery: too big uuid_count value %u",
3629 uuid_count);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003630 err = mgmt_cmd_complete(sk, hdev->id,
3631 MGMT_OP_START_SERVICE_DISCOVERY,
3632 MGMT_STATUS_INVALID_PARAMS, &cp->type,
3633 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003634 goto failed;
3635 }
3636
3637 expected_len = sizeof(*cp) + uuid_count * 16;
3638 if (expected_len != len) {
3639 BT_ERR("service_discovery: expected %u bytes, got %u bytes",
3640 expected_len, len);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003641 err = mgmt_cmd_complete(sk, hdev->id,
3642 MGMT_OP_START_SERVICE_DISCOVERY,
3643 MGMT_STATUS_INVALID_PARAMS, &cp->type,
3644 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003645 goto failed;
3646 }
3647
Johan Hedberg591752a2015-11-11 08:11:24 +02003648 if (!discovery_type_is_valid(hdev, cp->type, &status)) {
3649 err = mgmt_cmd_complete(sk, hdev->id,
3650 MGMT_OP_START_SERVICE_DISCOVERY,
3651 status, &cp->type, sizeof(cp->type));
3652 goto failed;
3653 }
3654
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003655 cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY,
Johan Hedberg2922a942014-12-05 13:36:06 +02003656 hdev, data, len);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003657 if (!cmd) {
3658 err = -ENOMEM;
3659 goto failed;
3660 }
3661
Johan Hedberg2922a942014-12-05 13:36:06 +02003662 cmd->cmd_complete = service_discovery_cmd_complete;
3663
Marcel Holtmann22078802014-12-05 11:45:22 +01003664 /* Clear the discovery filter first to free any previously
3665 * allocated memory for the UUID list.
3666 */
3667 hci_discovery_filter_clear(hdev);
3668
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08003669 hdev->discovery.result_filtering = true;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003670 hdev->discovery.type = cp->type;
3671 hdev->discovery.rssi = cp->rssi;
3672 hdev->discovery.uuid_count = uuid_count;
3673
3674 if (uuid_count > 0) {
3675 hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16,
3676 GFP_KERNEL);
3677 if (!hdev->discovery.uuids) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003678 err = mgmt_cmd_complete(sk, hdev->id,
3679 MGMT_OP_START_SERVICE_DISCOVERY,
3680 MGMT_STATUS_FAILED,
3681 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003682 mgmt_pending_remove(cmd);
3683 goto failed;
3684 }
3685 }
3686
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003687 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
Johan Hedberge68f0722015-11-11 08:30:30 +02003688 queue_work(hdev->req_workqueue, &hdev->discov_update);
3689 err = 0;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003690
3691failed:
3692 hci_dev_unlock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03003693 return err;
3694}
3695
Johan Hedberg2154d3f2015-11-11 08:30:45 +02003696void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status)
Andre Guedes0e05bba2013-04-30 15:29:33 -03003697{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003698 struct mgmt_pending_cmd *cmd;
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003699
Andre Guedes0e05bba2013-04-30 15:29:33 -03003700 BT_DBG("status %d", status);
3701
3702 hci_dev_lock(hdev);
3703
Johan Hedberg333ae952015-03-17 13:48:47 +02003704 cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003705 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02003706 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003707 mgmt_pending_remove(cmd);
Andre Guedes0e05bba2013-04-30 15:29:33 -03003708 }
3709
Andre Guedes0e05bba2013-04-30 15:29:33 -03003710 hci_dev_unlock(hdev);
3711}
3712
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003713static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003714 u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04003715{
Johan Hedbergd9306502012-02-20 23:25:18 +02003716 struct mgmt_cp_stop_discovery *mgmt_cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003717 struct mgmt_pending_cmd *cmd;
Johan Hedberg14a53662011-04-27 10:29:56 -04003718 int err;
3719
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003720 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04003721
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003722 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04003723
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003724 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003725 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
3726 MGMT_STATUS_REJECTED, &mgmt_cp->type,
3727 sizeof(mgmt_cp->type));
Johan Hedbergd9306502012-02-20 23:25:18 +02003728 goto unlock;
3729 }
3730
3731 if (hdev->discovery.type != mgmt_cp->type) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003732 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
3733 MGMT_STATUS_INVALID_PARAMS,
3734 &mgmt_cp->type, sizeof(mgmt_cp->type));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003735 goto unlock;
Johan Hedbergff9ef572012-01-04 14:23:45 +02003736 }
3737
Johan Hedberg2922a942014-12-05 13:36:06 +02003738 cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len);
Johan Hedberg14a53662011-04-27 10:29:56 -04003739 if (!cmd) {
3740 err = -ENOMEM;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003741 goto unlock;
Johan Hedberg14a53662011-04-27 10:29:56 -04003742 }
3743
Johan Hedberg2922a942014-12-05 13:36:06 +02003744 cmd->cmd_complete = generic_cmd_complete;
3745
Johan Hedberg2154d3f2015-11-11 08:30:45 +02003746 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
3747 queue_work(hdev->req_workqueue, &hdev->discov_update);
3748 err = 0;
Johan Hedberg14a53662011-04-27 10:29:56 -04003749
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003750unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003751 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04003752 return err;
3753}
3754
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003755static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003756 u16 len)
Johan Hedberg561aafb2012-01-04 13:31:59 +02003757{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003758 struct mgmt_cp_confirm_name *cp = data;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003759 struct inquiry_entry *e;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003760 int err;
3761
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003762 BT_DBG("%s", hdev->name);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003763
Johan Hedberg561aafb2012-01-04 13:31:59 +02003764 hci_dev_lock(hdev);
3765
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003766 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003767 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
3768 MGMT_STATUS_FAILED, &cp->addr,
3769 sizeof(cp->addr));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003770 goto failed;
3771 }
3772
Johan Hedberga198e7b2012-02-17 14:27:06 +02003773 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003774 if (!e) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003775 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
3776 MGMT_STATUS_INVALID_PARAMS, &cp->addr,
3777 sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02003778 goto failed;
3779 }
3780
3781 if (cp->name_known) {
3782 e->name_state = NAME_KNOWN;
3783 list_del(&e->list);
3784 } else {
3785 e->name_state = NAME_NEEDED;
Johan Hedberga3d4e202012-01-09 00:53:02 +02003786 hci_inquiry_cache_update_resolve(hdev, e);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003787 }
3788
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003789 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0,
3790 &cp->addr, sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02003791
3792failed:
3793 hci_dev_unlock(hdev);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003794 return err;
3795}
3796
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003797static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003798 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03003799{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003800 struct mgmt_cp_block_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003801 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03003802 int err;
3803
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003804 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03003805
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003806 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003807 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
3808 MGMT_STATUS_INVALID_PARAMS,
3809 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003810
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003811 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03003812
Johan Hedbergdcc36c12014-07-09 12:59:13 +03003813 err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr,
3814 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03003815 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003816 status = MGMT_STATUS_FAILED;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03003817 goto done;
3818 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003819
Johan Hedberg2a8357f2014-07-01 22:09:47 +03003820 mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr),
3821 sk);
3822 status = MGMT_STATUS_SUCCESS;
3823
3824done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003825 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
3826 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03003827
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003828 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03003829
3830 return err;
3831}
3832
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003833static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003834 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03003835{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003836 struct mgmt_cp_unblock_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003837 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03003838 int err;
3839
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003840 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03003841
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003842 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003843 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
3844 MGMT_STATUS_INVALID_PARAMS,
3845 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003846
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003847 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03003848
Johan Hedbergdcc36c12014-07-09 12:59:13 +03003849 err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr,
3850 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03003851 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003852 status = MGMT_STATUS_INVALID_PARAMS;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03003853 goto done;
3854 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003855
Johan Hedberg2a8357f2014-07-01 22:09:47 +03003856 mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr),
3857 sk);
3858 status = MGMT_STATUS_SUCCESS;
3859
3860done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003861 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
3862 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03003863
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003864 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03003865
3866 return err;
3867}
3868
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003869static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
3870 u16 len)
3871{
3872 struct mgmt_cp_set_device_id *cp = data;
Johan Hedberg890ea892013-03-15 17:06:52 -05003873 struct hci_request req;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003874 int err;
Szymon Jancc72d4b82012-03-16 16:02:57 +01003875 __u16 source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003876
3877 BT_DBG("%s", hdev->name);
3878
Szymon Jancc72d4b82012-03-16 16:02:57 +01003879 source = __le16_to_cpu(cp->source);
3880
3881 if (source > 0x0002)
Johan Hedberga69e8372015-03-06 21:08:53 +02003882 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
3883 MGMT_STATUS_INVALID_PARAMS);
Szymon Jancc72d4b82012-03-16 16:02:57 +01003884
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003885 hci_dev_lock(hdev);
3886
Szymon Jancc72d4b82012-03-16 16:02:57 +01003887 hdev->devid_source = source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003888 hdev->devid_vendor = __le16_to_cpu(cp->vendor);
3889 hdev->devid_product = __le16_to_cpu(cp->product);
3890 hdev->devid_version = __le16_to_cpu(cp->version);
3891
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003892 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0,
3893 NULL, 0);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003894
Johan Hedberg890ea892013-03-15 17:06:52 -05003895 hci_req_init(&req, hdev);
Johan Hedbergb1a89172015-11-25 16:15:42 +02003896 __hci_req_update_eir(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05003897 hci_req_run(&req, NULL);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003898
3899 hci_dev_unlock(hdev);
3900
3901 return err;
3902}
3903
Arman Uguray24b4f382015-03-23 15:57:12 -07003904static void enable_advertising_instance(struct hci_dev *hdev, u8 status,
3905 u16 opcode)
3906{
3907 BT_DBG("status %d", status);
3908}
3909
Marcel Holtmann1904a852015-01-11 13:50:44 -08003910static void set_advertising_complete(struct hci_dev *hdev, u8 status,
3911 u16 opcode)
Johan Hedberg4375f102013-09-25 13:26:10 +03003912{
3913 struct cmd_lookup match = { NULL, hdev };
Arman Uguray24b4f382015-03-23 15:57:12 -07003914 struct hci_request req;
Florian Grandel7816b822015-06-18 03:16:45 +02003915 u8 instance;
3916 struct adv_info *adv_instance;
3917 int err;
Johan Hedberg4375f102013-09-25 13:26:10 +03003918
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05303919 hci_dev_lock(hdev);
3920
Johan Hedberg4375f102013-09-25 13:26:10 +03003921 if (status) {
3922 u8 mgmt_err = mgmt_status(status);
3923
3924 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev,
3925 cmd_status_rsp, &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05303926 goto unlock;
Johan Hedberg4375f102013-09-25 13:26:10 +03003927 }
3928
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003929 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Marcel Holtmanna1536da2015-03-13 02:11:01 -07003930 hci_dev_set_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03003931 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07003932 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03003933
Johan Hedberg4375f102013-09-25 13:26:10 +03003934 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
3935 &match);
3936
3937 new_settings(hdev, match.sk);
3938
3939 if (match.sk)
3940 sock_put(match.sk);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05303941
Arman Uguray24b4f382015-03-23 15:57:12 -07003942 /* If "Set Advertising" was just disabled and instance advertising was
Florian Grandel7816b822015-06-18 03:16:45 +02003943 * set up earlier, then re-enable multi-instance advertising.
Arman Uguray24b4f382015-03-23 15:57:12 -07003944 */
3945 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
Florian Grandel7816b822015-06-18 03:16:45 +02003946 list_empty(&hdev->adv_instances))
Arman Uguray24b4f382015-03-23 15:57:12 -07003947 goto unlock;
3948
Florian Grandel7816b822015-06-18 03:16:45 +02003949 instance = hdev->cur_adv_instance;
3950 if (!instance) {
3951 adv_instance = list_first_entry_or_null(&hdev->adv_instances,
3952 struct adv_info, list);
3953 if (!adv_instance)
3954 goto unlock;
3955
3956 instance = adv_instance->instance;
3957 }
3958
Arman Uguray24b4f382015-03-23 15:57:12 -07003959 hci_req_init(&req, hdev);
3960
Johan Hedbergf2252572015-11-18 12:49:20 +02003961 err = __hci_req_schedule_adv_instance(&req, instance, true);
Arman Uguray24b4f382015-03-23 15:57:12 -07003962
Florian Grandel7816b822015-06-18 03:16:45 +02003963 if (!err)
3964 err = hci_req_run(&req, enable_advertising_instance);
3965
3966 if (err)
Arman Uguray24b4f382015-03-23 15:57:12 -07003967 BT_ERR("Failed to re-configure advertising");
3968
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05303969unlock:
3970 hci_dev_unlock(hdev);
Johan Hedberg4375f102013-09-25 13:26:10 +03003971}
3972
Marcel Holtmann21b51872013-10-10 09:47:53 -07003973static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
3974 u16 len)
Johan Hedberg4375f102013-09-25 13:26:10 +03003975{
3976 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003977 struct mgmt_pending_cmd *cmd;
Johan Hedberg4375f102013-09-25 13:26:10 +03003978 struct hci_request req;
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07003979 u8 val, status;
Johan Hedberg4375f102013-09-25 13:26:10 +03003980 int err;
3981
3982 BT_DBG("request for %s", hdev->name);
3983
Johan Hedberge6fe7982013-10-02 15:45:22 +03003984 status = mgmt_le_support(hdev);
3985 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02003986 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
3987 status);
Johan Hedberg4375f102013-09-25 13:26:10 +03003988
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07003989 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02003990 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
3991 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4375f102013-09-25 13:26:10 +03003992
3993 hci_dev_lock(hdev);
3994
3995 val = !!cp->val;
Johan Hedberg4375f102013-09-25 13:26:10 +03003996
Johan Hedbergf74ca9b2013-10-08 15:52:18 +02003997 /* The following conditions are ones which mean that we should
3998 * not do any HCI communication but directly send a mgmt
3999 * response to user space (after toggling the flag if
4000 * necessary).
4001 */
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004002 if (!hdev_is_powered(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004003 (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
4004 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) ||
Johan Hedberge8bb6b92014-07-08 15:07:53 +03004005 hci_conn_num(hdev, LE_LINK) > 0 ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004006 (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Johan Hedberge8bb6b92014-07-08 15:07:53 +03004007 hdev->le_scan_type == LE_SCAN_ACTIVE)) {
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004008 bool changed;
Johan Hedberg4375f102013-09-25 13:26:10 +03004009
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004010 if (cp->val) {
Johan Hedbergcab054a2015-11-30 11:21:45 +02004011 hdev->cur_adv_instance = 0x00;
Marcel Holtmann238be782015-03-13 02:11:06 -07004012 changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004013 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004014 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004015 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004016 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004017 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07004018 changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004019 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Johan Hedberg4375f102013-09-25 13:26:10 +03004020 }
4021
4022 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
4023 if (err < 0)
4024 goto unlock;
4025
4026 if (changed)
4027 err = new_settings(hdev, sk);
4028
4029 goto unlock;
4030 }
4031
Johan Hedberg333ae952015-03-17 13:48:47 +02004032 if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
4033 pending_find(MGMT_OP_SET_LE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004034 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4035 MGMT_STATUS_BUSY);
Johan Hedberg4375f102013-09-25 13:26:10 +03004036 goto unlock;
4037 }
4038
4039 cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
4040 if (!cmd) {
4041 err = -ENOMEM;
4042 goto unlock;
4043 }
4044
4045 hci_req_init(&req, hdev);
4046
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004047 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004048 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004049 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004050 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004051
Florian Grandel7816b822015-06-18 03:16:45 +02004052 cancel_adv_timeout(hdev);
4053
Arman Uguray24b4f382015-03-23 15:57:12 -07004054 if (val) {
Florian Grandel7816b822015-06-18 03:16:45 +02004055 /* Switch to instance "0" for the Set Advertising setting.
4056 * We cannot use update_[adv|scan_rsp]_data() here as the
4057 * HCI_ADVERTISING flag is not yet set.
4058 */
Johan Hedbergcab054a2015-11-30 11:21:45 +02004059 hdev->cur_adv_instance = 0x00;
Johan Hedbergf2252572015-11-18 12:49:20 +02004060 __hci_req_update_adv_data(&req, 0x00);
4061 __hci_req_update_scan_rsp_data(&req, 0x00);
4062 __hci_req_enable_advertising(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07004063 } else {
Johan Hedbergf2252572015-11-18 12:49:20 +02004064 __hci_req_disable_advertising(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07004065 }
Johan Hedberg4375f102013-09-25 13:26:10 +03004066
4067 err = hci_req_run(&req, set_advertising_complete);
4068 if (err < 0)
4069 mgmt_pending_remove(cmd);
4070
4071unlock:
4072 hci_dev_unlock(hdev);
4073 return err;
4074}
4075
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004076static int set_static_address(struct sock *sk, struct hci_dev *hdev,
4077 void *data, u16 len)
4078{
4079 struct mgmt_cp_set_static_address *cp = data;
4080 int err;
4081
4082 BT_DBG("%s", hdev->name);
4083
Marcel Holtmann62af4442013-10-02 22:10:32 -07004084 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004085 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4086 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004087
4088 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004089 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4090 MGMT_STATUS_REJECTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004091
4092 if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
4093 if (!bacmp(&cp->bdaddr, BDADDR_NONE))
Johan Hedberga69e8372015-03-06 21:08:53 +02004094 return mgmt_cmd_status(sk, hdev->id,
4095 MGMT_OP_SET_STATIC_ADDRESS,
4096 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004097
4098 /* Two most significant bits shall be set */
4099 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
Johan Hedberga69e8372015-03-06 21:08:53 +02004100 return mgmt_cmd_status(sk, hdev->id,
4101 MGMT_OP_SET_STATIC_ADDRESS,
4102 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004103 }
4104
4105 hci_dev_lock(hdev);
4106
4107 bacpy(&hdev->static_addr, &cp->bdaddr);
4108
Marcel Holtmann93690c22015-03-06 10:11:21 -08004109 err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev);
4110 if (err < 0)
4111 goto unlock;
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004112
Marcel Holtmann93690c22015-03-06 10:11:21 -08004113 err = new_settings(hdev, sk);
4114
4115unlock:
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004116 hci_dev_unlock(hdev);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004117 return err;
4118}
4119
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004120static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
4121 void *data, u16 len)
4122{
4123 struct mgmt_cp_set_scan_params *cp = data;
4124 __u16 interval, window;
4125 int err;
4126
4127 BT_DBG("%s", hdev->name);
4128
4129 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004130 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4131 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004132
4133 interval = __le16_to_cpu(cp->interval);
4134
4135 if (interval < 0x0004 || interval > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02004136 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4137 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004138
4139 window = __le16_to_cpu(cp->window);
4140
4141 if (window < 0x0004 || window > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02004142 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4143 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004144
Marcel Holtmann899e1072013-10-14 09:55:32 -07004145 if (window > interval)
Johan Hedberga69e8372015-03-06 21:08:53 +02004146 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4147 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann899e1072013-10-14 09:55:32 -07004148
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004149 hci_dev_lock(hdev);
4150
4151 hdev->le_scan_interval = interval;
4152 hdev->le_scan_window = window;
4153
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004154 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0,
4155 NULL, 0);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004156
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03004157 /* If background scan is running, restart it so new parameters are
4158 * loaded.
4159 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004160 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03004161 hdev->discovery.state == DISCOVERY_STOPPED) {
4162 struct hci_request req;
4163
4164 hci_req_init(&req, hdev);
4165
4166 hci_req_add_le_scan_disable(&req);
4167 hci_req_add_le_passive_scan(&req);
4168
4169 hci_req_run(&req, NULL);
4170 }
4171
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004172 hci_dev_unlock(hdev);
4173
4174 return err;
4175}
4176
Marcel Holtmann1904a852015-01-11 13:50:44 -08004177static void fast_connectable_complete(struct hci_dev *hdev, u8 status,
4178 u16 opcode)
Johan Hedberg33e38b32013-03-15 17:07:05 -05004179{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004180 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004181
4182 BT_DBG("status 0x%02x", status);
4183
4184 hci_dev_lock(hdev);
4185
Johan Hedberg333ae952015-03-17 13:48:47 +02004186 cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004187 if (!cmd)
4188 goto unlock;
4189
4190 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004191 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4192 mgmt_status(status));
Johan Hedberg33e38b32013-03-15 17:07:05 -05004193 } else {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004194 struct mgmt_mode *cp = cmd->param;
4195
4196 if (cp->val)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004197 hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004198 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004199 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004200
Johan Hedberg33e38b32013-03-15 17:07:05 -05004201 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
4202 new_settings(hdev, cmd->sk);
4203 }
4204
4205 mgmt_pending_remove(cmd);
4206
4207unlock:
4208 hci_dev_unlock(hdev);
4209}
4210
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004211static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004212 void *data, u16 len)
Antti Julkuf6422ec2011-06-22 13:11:56 +03004213{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004214 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004215 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004216 struct hci_request req;
Antti Julkuf6422ec2011-06-22 13:11:56 +03004217 int err;
4218
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004219 BT_DBG("%s", hdev->name);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004220
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004221 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
Johan Hedberg56f87902013-10-02 13:43:13 +03004222 hdev->hci_ver < BLUETOOTH_VER_1_2)
Johan Hedberga69e8372015-03-06 21:08:53 +02004223 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4224 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03004225
Johan Hedberga7e80f22013-01-09 16:05:19 +02004226 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02004227 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4228 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02004229
Antti Julkuf6422ec2011-06-22 13:11:56 +03004230 hci_dev_lock(hdev);
4231
Johan Hedberg333ae952015-03-17 13:48:47 +02004232 if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004233 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4234 MGMT_STATUS_BUSY);
Johan Hedberg05cbf292013-03-15 17:07:07 -05004235 goto unlock;
4236 }
4237
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004238 if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004239 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4240 hdev);
4241 goto unlock;
4242 }
4243
Johan Hedberg406ef2a2015-03-10 20:14:27 +02004244 if (!hdev_is_powered(hdev)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07004245 hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg406ef2a2015-03-10 20:14:27 +02004246 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4247 hdev);
4248 new_settings(hdev, sk);
4249 goto unlock;
4250 }
4251
Johan Hedberg33e38b32013-03-15 17:07:05 -05004252 cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev,
4253 data, len);
4254 if (!cmd) {
4255 err = -ENOMEM;
4256 goto unlock;
4257 }
4258
4259 hci_req_init(&req, hdev);
4260
Johan Hedbergbf943cb2015-11-25 16:15:43 +02004261 __hci_req_write_fast_connectable(&req, cp->val);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004262
4263 err = hci_req_run(&req, fast_connectable_complete);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004264 if (err < 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004265 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4266 MGMT_STATUS_FAILED);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004267 mgmt_pending_remove(cmd);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004268 }
4269
Johan Hedberg33e38b32013-03-15 17:07:05 -05004270unlock:
Antti Julkuf6422ec2011-06-22 13:11:56 +03004271 hci_dev_unlock(hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004272
Antti Julkuf6422ec2011-06-22 13:11:56 +03004273 return err;
4274}
4275
Marcel Holtmann1904a852015-01-11 13:50:44 -08004276static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg0663ca22013-10-02 13:43:14 +03004277{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004278 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03004279
4280 BT_DBG("status 0x%02x", status);
4281
4282 hci_dev_lock(hdev);
4283
Johan Hedberg333ae952015-03-17 13:48:47 +02004284 cmd = pending_find(MGMT_OP_SET_BREDR, hdev);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004285 if (!cmd)
4286 goto unlock;
4287
4288 if (status) {
4289 u8 mgmt_err = mgmt_status(status);
4290
4291 /* We need to restore the flag if related HCI commands
4292 * failed.
4293 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004294 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004295
Johan Hedberga69e8372015-03-06 21:08:53 +02004296 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004297 } else {
4298 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
4299 new_settings(hdev, cmd->sk);
4300 }
4301
4302 mgmt_pending_remove(cmd);
4303
4304unlock:
4305 hci_dev_unlock(hdev);
4306}
4307
4308static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
4309{
4310 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004311 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03004312 struct hci_request req;
4313 int err;
4314
4315 BT_DBG("request for %s", hdev->name);
4316
4317 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004318 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4319 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004320
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004321 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02004322 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4323 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004324
4325 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02004326 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4327 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004328
4329 hci_dev_lock(hdev);
4330
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004331 if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg0663ca22013-10-02 13:43:14 +03004332 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4333 goto unlock;
4334 }
4335
4336 if (!hdev_is_powered(hdev)) {
4337 if (!cp->val) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004338 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
4339 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
4340 hci_dev_clear_flag(hdev, HCI_LINK_SECURITY);
4341 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
4342 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004343 }
4344
Marcel Holtmannce05d602015-03-13 02:11:03 -07004345 hci_dev_change_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004346
4347 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4348 if (err < 0)
4349 goto unlock;
4350
4351 err = new_settings(hdev, sk);
4352 goto unlock;
4353 }
4354
4355 /* Reject disabling when powered on */
4356 if (!cp->val) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004357 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4358 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004359 goto unlock;
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08004360 } else {
4361 /* When configuring a dual-mode controller to operate
4362 * with LE only and using a static address, then switching
4363 * BR/EDR back on is not allowed.
4364 *
4365 * Dual-mode controllers shall operate with the public
4366 * address as its identity address for BR/EDR and LE. So
4367 * reject the attempt to create an invalid configuration.
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08004368 *
4369 * The same restrictions applies when secure connections
4370 * has been enabled. For BR/EDR this is a controller feature
4371 * while for LE it is a host stack feature. This means that
4372 * switching BR/EDR back on when secure connections has been
4373 * enabled is not a supported transaction.
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08004374 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004375 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08004376 (bacmp(&hdev->static_addr, BDADDR_ANY) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004377 hci_dev_test_flag(hdev, HCI_SC_ENABLED))) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004378 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4379 MGMT_STATUS_REJECTED);
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08004380 goto unlock;
4381 }
Johan Hedberg0663ca22013-10-02 13:43:14 +03004382 }
4383
Johan Hedberg333ae952015-03-17 13:48:47 +02004384 if (pending_find(MGMT_OP_SET_BREDR, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004385 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4386 MGMT_STATUS_BUSY);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004387 goto unlock;
4388 }
4389
4390 cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len);
4391 if (!cmd) {
4392 err = -ENOMEM;
4393 goto unlock;
4394 }
4395
Johan Hedbergf2252572015-11-18 12:49:20 +02004396 /* We need to flip the bit already here so that
4397 * hci_req_update_adv_data generates the correct flags.
Johan Hedberg0663ca22013-10-02 13:43:14 +03004398 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004399 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004400
4401 hci_req_init(&req, hdev);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004402
Johan Hedbergbf943cb2015-11-25 16:15:43 +02004403 __hci_req_write_fast_connectable(&req, false);
Johan Hedberg01b1cb82015-11-16 12:52:21 +02004404 __hci_req_update_scan(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004405
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07004406 /* Since only the advertising data flags will change, there
4407 * is no need to update the scan response data.
4408 */
Johan Hedbergcab054a2015-11-30 11:21:45 +02004409 __hci_req_update_adv_data(&req, hdev->cur_adv_instance);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004410
Johan Hedberg0663ca22013-10-02 13:43:14 +03004411 err = hci_req_run(&req, set_bredr_complete);
4412 if (err < 0)
4413 mgmt_pending_remove(cmd);
4414
4415unlock:
4416 hci_dev_unlock(hdev);
4417 return err;
4418}
4419
Johan Hedberga1443f52015-01-23 15:42:46 +02004420static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
4421{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004422 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02004423 struct mgmt_mode *cp;
4424
4425 BT_DBG("%s status %u", hdev->name, status);
4426
4427 hci_dev_lock(hdev);
4428
Johan Hedberg333ae952015-03-17 13:48:47 +02004429 cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev);
Johan Hedberga1443f52015-01-23 15:42:46 +02004430 if (!cmd)
4431 goto unlock;
4432
4433 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004434 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
4435 mgmt_status(status));
Johan Hedberga1443f52015-01-23 15:42:46 +02004436 goto remove;
4437 }
4438
4439 cp = cmd->param;
4440
4441 switch (cp->val) {
4442 case 0x00:
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004443 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
4444 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02004445 break;
4446 case 0x01:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004447 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004448 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02004449 break;
4450 case 0x02:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004451 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
4452 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02004453 break;
4454 }
4455
4456 send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev);
4457 new_settings(hdev, cmd->sk);
4458
4459remove:
4460 mgmt_pending_remove(cmd);
4461unlock:
4462 hci_dev_unlock(hdev);
4463}
4464
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004465static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
4466 void *data, u16 len)
4467{
4468 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004469 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02004470 struct hci_request req;
Johan Hedberga3209692014-05-26 11:23:35 +03004471 u8 val;
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004472 int err;
4473
4474 BT_DBG("request for %s", hdev->name);
4475
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08004476 if (!lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004477 !hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02004478 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4479 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004480
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004481 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Johan Hedberg59200282015-01-28 19:56:00 +02004482 lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004483 !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02004484 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4485 MGMT_STATUS_REJECTED);
Marcel Holtmanned93ec62015-01-22 11:15:22 -08004486
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004487 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02004488 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004489 MGMT_STATUS_INVALID_PARAMS);
4490
4491 hci_dev_lock(hdev);
4492
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08004493 if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004494 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004495 bool changed;
4496
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004497 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07004498 changed = !hci_dev_test_and_set_flag(hdev,
4499 HCI_SC_ENABLED);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004500 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004501 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004502 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004503 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004504 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07004505 changed = hci_dev_test_and_clear_flag(hdev,
4506 HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004507 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004508 }
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004509
4510 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
4511 if (err < 0)
4512 goto failed;
4513
4514 if (changed)
4515 err = new_settings(hdev, sk);
4516
4517 goto failed;
4518 }
4519
Johan Hedberg333ae952015-03-17 13:48:47 +02004520 if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004521 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4522 MGMT_STATUS_BUSY);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004523 goto failed;
4524 }
4525
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004526 val = !!cp->val;
4527
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004528 if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
4529 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004530 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
4531 goto failed;
4532 }
4533
4534 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len);
4535 if (!cmd) {
4536 err = -ENOMEM;
4537 goto failed;
4538 }
4539
Johan Hedberga1443f52015-01-23 15:42:46 +02004540 hci_req_init(&req, hdev);
4541 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val);
4542 err = hci_req_run(&req, sc_enable_complete);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004543 if (err < 0) {
4544 mgmt_pending_remove(cmd);
4545 goto failed;
4546 }
4547
4548failed:
4549 hci_dev_unlock(hdev);
4550 return err;
4551}
4552
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004553static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
4554 void *data, u16 len)
4555{
4556 struct mgmt_mode *cp = data;
Johan Hedbergb97109792014-06-24 14:00:28 +03004557 bool changed, use_changed;
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004558 int err;
4559
4560 BT_DBG("request for %s", hdev->name);
4561
Johan Hedbergb97109792014-06-24 14:00:28 +03004562 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02004563 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
4564 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004565
4566 hci_dev_lock(hdev);
4567
4568 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07004569 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004570 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07004571 changed = hci_dev_test_and_clear_flag(hdev,
4572 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004573
Johan Hedbergb97109792014-06-24 14:00:28 +03004574 if (cp->val == 0x02)
Marcel Holtmann238be782015-03-13 02:11:06 -07004575 use_changed = !hci_dev_test_and_set_flag(hdev,
4576 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03004577 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07004578 use_changed = hci_dev_test_and_clear_flag(hdev,
4579 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03004580
4581 if (hdev_is_powered(hdev) && use_changed &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004582 hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergb97109792014-06-24 14:00:28 +03004583 u8 mode = (cp->val == 0x02) ? 0x01 : 0x00;
4584 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
4585 sizeof(mode), &mode);
4586 }
4587
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004588 err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev);
4589 if (err < 0)
4590 goto unlock;
4591
4592 if (changed)
4593 err = new_settings(hdev, sk);
4594
4595unlock:
4596 hci_dev_unlock(hdev);
4597 return err;
4598}
4599
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004600static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
4601 u16 len)
4602{
4603 struct mgmt_cp_set_privacy *cp = cp_data;
4604 bool changed;
4605 int err;
4606
4607 BT_DBG("request for %s", hdev->name);
4608
4609 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004610 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
4611 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004612
Johan Hedberg82a37ad2016-03-09 17:30:34 +02004613 if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02004614 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
4615 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004616
4617 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004618 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
4619 MGMT_STATUS_REJECTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004620
4621 hci_dev_lock(hdev);
4622
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02004623 /* If user space supports this command it is also expected to
4624 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
4625 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004626 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02004627
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004628 if (cp->privacy) {
Marcel Holtmann238be782015-03-13 02:11:06 -07004629 changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004630 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004631 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedberg82a37ad2016-03-09 17:30:34 +02004632 if (cp->privacy == 0x02)
4633 hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY);
4634 else
4635 hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004636 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07004637 changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004638 memset(hdev->irk, 0, sizeof(hdev->irk));
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004639 hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedberg82a37ad2016-03-09 17:30:34 +02004640 hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004641 }
4642
4643 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
4644 if (err < 0)
4645 goto unlock;
4646
4647 if (changed)
4648 err = new_settings(hdev, sk);
4649
4650unlock:
4651 hci_dev_unlock(hdev);
4652 return err;
4653}
4654
Johan Hedberg41edf162014-02-18 10:19:35 +02004655static bool irk_is_valid(struct mgmt_irk_info *irk)
4656{
4657 switch (irk->addr.type) {
4658 case BDADDR_LE_PUBLIC:
4659 return true;
4660
4661 case BDADDR_LE_RANDOM:
4662 /* Two most significant bits shall be set */
4663 if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0)
4664 return false;
4665 return true;
4666 }
4667
4668 return false;
4669}
4670
4671static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
4672 u16 len)
4673{
4674 struct mgmt_cp_load_irks *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03004675 const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) /
4676 sizeof(struct mgmt_irk_info));
Johan Hedberg41edf162014-02-18 10:19:35 +02004677 u16 irk_count, expected_len;
4678 int i, err;
4679
4680 BT_DBG("request for %s", hdev->name);
4681
4682 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004683 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
4684 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg41edf162014-02-18 10:19:35 +02004685
4686 irk_count = __le16_to_cpu(cp->irk_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03004687 if (irk_count > max_irk_count) {
4688 BT_ERR("load_irks: too big irk_count value %u", irk_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02004689 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
4690 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03004691 }
Johan Hedberg41edf162014-02-18 10:19:35 +02004692
4693 expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info);
4694 if (expected_len != len) {
4695 BT_ERR("load_irks: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02004696 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02004697 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
4698 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02004699 }
4700
4701 BT_DBG("%s irk_count %u", hdev->name, irk_count);
4702
4703 for (i = 0; i < irk_count; i++) {
4704 struct mgmt_irk_info *key = &cp->irks[i];
4705
4706 if (!irk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02004707 return mgmt_cmd_status(sk, hdev->id,
4708 MGMT_OP_LOAD_IRKS,
4709 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02004710 }
4711
4712 hci_dev_lock(hdev);
4713
4714 hci_smp_irks_clear(hdev);
4715
4716 for (i = 0; i < irk_count; i++) {
4717 struct mgmt_irk_info *irk = &cp->irks[i];
Johan Hedberg41edf162014-02-18 10:19:35 +02004718
Johan Hedberg85813a72015-10-21 18:02:59 +03004719 hci_add_irk(hdev, &irk->addr.bdaddr,
4720 le_addr_type(irk->addr.type), irk->val,
Johan Hedberg41edf162014-02-18 10:19:35 +02004721 BDADDR_ANY);
4722 }
4723
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004724 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedberg41edf162014-02-18 10:19:35 +02004725
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004726 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
Johan Hedberg41edf162014-02-18 10:19:35 +02004727
4728 hci_dev_unlock(hdev);
4729
4730 return err;
4731}
4732
Johan Hedberg3f706b72013-01-20 14:27:16 +02004733static bool ltk_is_valid(struct mgmt_ltk_info *key)
4734{
4735 if (key->master != 0x00 && key->master != 0x01)
4736 return false;
Marcel Holtmann490cb0b2014-02-16 12:59:05 -08004737
4738 switch (key->addr.type) {
4739 case BDADDR_LE_PUBLIC:
4740 return true;
4741
4742 case BDADDR_LE_RANDOM:
4743 /* Two most significant bits shall be set */
4744 if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0)
4745 return false;
4746 return true;
4747 }
4748
4749 return false;
Johan Hedberg3f706b72013-01-20 14:27:16 +02004750}
4751
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004752static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004753 void *cp_data, u16 len)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004754{
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004755 struct mgmt_cp_load_long_term_keys *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03004756 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
4757 sizeof(struct mgmt_ltk_info));
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004758 u16 key_count, expected_len;
Johan Hedberg715a5bf2013-01-09 15:29:34 +02004759 int i, err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004760
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07004761 BT_DBG("request for %s", hdev->name);
4762
4763 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004764 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
4765 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07004766
Marcel Holtmann1f350c82012-03-12 20:31:08 -07004767 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03004768 if (key_count > max_key_count) {
4769 BT_ERR("load_ltks: too big key_count value %u", key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02004770 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
4771 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03004772 }
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004773
4774 expected_len = sizeof(*cp) + key_count *
4775 sizeof(struct mgmt_ltk_info);
4776 if (expected_len != len) {
4777 BT_ERR("load_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02004778 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02004779 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
4780 MGMT_STATUS_INVALID_PARAMS);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004781 }
4782
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004783 BT_DBG("%s key_count %u", hdev->name, key_count);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004784
Johan Hedberg54ad6d82013-01-20 14:27:15 +02004785 for (i = 0; i < key_count; i++) {
4786 struct mgmt_ltk_info *key = &cp->keys[i];
4787
Johan Hedberg3f706b72013-01-20 14:27:16 +02004788 if (!ltk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02004789 return mgmt_cmd_status(sk, hdev->id,
4790 MGMT_OP_LOAD_LONG_TERM_KEYS,
4791 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg54ad6d82013-01-20 14:27:15 +02004792 }
4793
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004794 hci_dev_lock(hdev);
4795
4796 hci_smp_ltks_clear(hdev);
4797
4798 for (i = 0; i < key_count; i++) {
4799 struct mgmt_ltk_info *key = &cp->keys[i];
Johan Hedberg85813a72015-10-21 18:02:59 +03004800 u8 type, authenticated;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004801
Johan Hedberg61b43352014-05-29 19:36:53 +03004802 switch (key->type) {
4803 case MGMT_LTK_UNAUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03004804 authenticated = 0x00;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03004805 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03004806 break;
4807 case MGMT_LTK_AUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03004808 authenticated = 0x01;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03004809 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03004810 break;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03004811 case MGMT_LTK_P256_UNAUTH:
4812 authenticated = 0x00;
4813 type = SMP_LTK_P256;
4814 break;
4815 case MGMT_LTK_P256_AUTH:
4816 authenticated = 0x01;
4817 type = SMP_LTK_P256;
4818 break;
4819 case MGMT_LTK_P256_DEBUG:
4820 authenticated = 0x00;
4821 type = SMP_LTK_P256_DEBUG;
Johan Hedberg61b43352014-05-29 19:36:53 +03004822 default:
4823 continue;
4824 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03004825
Johan Hedberg85813a72015-10-21 18:02:59 +03004826 hci_add_ltk(hdev, &key->addr.bdaddr,
4827 le_addr_type(key->addr.type), type, authenticated,
4828 key->val, key->enc_size, key->ediv, key->rand);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004829 }
4830
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004831 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
Johan Hedberg715a5bf2013-01-09 15:29:34 +02004832 NULL, 0);
4833
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004834 hci_dev_unlock(hdev);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004835
Johan Hedberg715a5bf2013-01-09 15:29:34 +02004836 return err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004837}
4838
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004839static int conn_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004840{
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004841 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004842 struct mgmt_rp_get_conn_info rp;
Johan Hedberg9df74652014-12-19 22:26:03 +02004843 int err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004844
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004845 memcpy(&rp.addr, cmd->param, sizeof(rp.addr));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004846
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004847 if (status == MGMT_STATUS_SUCCESS) {
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004848 rp.rssi = conn->rssi;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004849 rp.tx_power = conn->tx_power;
4850 rp.max_tx_power = conn->max_tx_power;
4851 } else {
4852 rp.rssi = HCI_RSSI_INVALID;
4853 rp.tx_power = HCI_TX_POWER_INVALID;
4854 rp.max_tx_power = HCI_TX_POWER_INVALID;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004855 }
4856
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004857 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO,
4858 status, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004859
4860 hci_conn_drop(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03004861 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02004862
4863 return err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004864}
4865
Marcel Holtmann1904a852015-01-11 13:50:44 -08004866static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status,
4867 u16 opcode)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004868{
4869 struct hci_cp_read_rssi *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004870 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004871 struct hci_conn *conn;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004872 u16 handle;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004873 u8 status;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004874
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004875 BT_DBG("status 0x%02x", hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004876
4877 hci_dev_lock(hdev);
4878
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004879 /* Commands sent in request are either Read RSSI or Read Transmit Power
4880 * Level so we check which one was last sent to retrieve connection
4881 * handle. Both commands have handle as first parameter so it's safe to
4882 * cast data on the same command struct.
4883 *
4884 * First command sent is always Read RSSI and we fail only if it fails.
4885 * In other case we simply override error to indicate success as we
4886 * already remembered if TX power value is actually valid.
4887 */
4888 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI);
4889 if (!cp) {
4890 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004891 status = MGMT_STATUS_SUCCESS;
4892 } else {
4893 status = mgmt_status(hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004894 }
4895
4896 if (!cp) {
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004897 BT_ERR("invalid sent_cmd in conn_info response");
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004898 goto unlock;
4899 }
4900
4901 handle = __le16_to_cpu(cp->handle);
4902 conn = hci_conn_hash_lookup_handle(hdev, handle);
4903 if (!conn) {
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004904 BT_ERR("unknown handle (%d) in conn_info response", handle);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004905 goto unlock;
4906 }
4907
Johan Hedberg333ae952015-03-17 13:48:47 +02004908 cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004909 if (!cmd)
4910 goto unlock;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004911
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004912 cmd->cmd_complete(cmd, status);
4913 mgmt_pending_remove(cmd);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004914
4915unlock:
4916 hci_dev_unlock(hdev);
4917}
4918
4919static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
4920 u16 len)
4921{
4922 struct mgmt_cp_get_conn_info *cp = data;
4923 struct mgmt_rp_get_conn_info rp;
4924 struct hci_conn *conn;
4925 unsigned long conn_info_age;
4926 int err = 0;
4927
4928 BT_DBG("%s", hdev->name);
4929
4930 memset(&rp, 0, sizeof(rp));
4931 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
4932 rp.addr.type = cp->addr.type;
4933
4934 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004935 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
4936 MGMT_STATUS_INVALID_PARAMS,
4937 &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004938
4939 hci_dev_lock(hdev);
4940
4941 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004942 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
4943 MGMT_STATUS_NOT_POWERED, &rp,
4944 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004945 goto unlock;
4946 }
4947
4948 if (cp->addr.type == BDADDR_BREDR)
4949 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
4950 &cp->addr.bdaddr);
4951 else
4952 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
4953
4954 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004955 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
4956 MGMT_STATUS_NOT_CONNECTED, &rp,
4957 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004958 goto unlock;
4959 }
4960
Johan Hedberg333ae952015-03-17 13:48:47 +02004961 if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004962 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
4963 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004964 goto unlock;
4965 }
4966
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004967 /* To avoid client trying to guess when to poll again for information we
4968 * calculate conn info age as random value between min/max set in hdev.
4969 */
4970 conn_info_age = hdev->conn_info_min_age +
4971 prandom_u32_max(hdev->conn_info_max_age -
4972 hdev->conn_info_min_age);
4973
4974 /* Query controller to refresh cached values if they are too old or were
4975 * never read.
4976 */
Andrzej Kaczmarekf4e2dd52014-05-16 16:48:57 +02004977 if (time_after(jiffies, conn->conn_info_timestamp +
4978 msecs_to_jiffies(conn_info_age)) ||
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004979 !conn->conn_info_timestamp) {
4980 struct hci_request req;
4981 struct hci_cp_read_tx_power req_txp_cp;
4982 struct hci_cp_read_rssi req_rssi_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004983 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004984
4985 hci_req_init(&req, hdev);
4986 req_rssi_cp.handle = cpu_to_le16(conn->handle);
4987 hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp),
4988 &req_rssi_cp);
4989
Andrzej Kaczmarekf7faab02014-05-14 13:43:04 +02004990 /* For LE links TX power does not change thus we don't need to
4991 * query for it once value is known.
4992 */
4993 if (!bdaddr_type_is_le(cp->addr.type) ||
4994 conn->tx_power == HCI_TX_POWER_INVALID) {
4995 req_txp_cp.handle = cpu_to_le16(conn->handle);
4996 req_txp_cp.type = 0x00;
4997 hci_req_add(&req, HCI_OP_READ_TX_POWER,
4998 sizeof(req_txp_cp), &req_txp_cp);
4999 }
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005000
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005001 /* Max TX power needs to be read only once per connection */
5002 if (conn->max_tx_power == HCI_TX_POWER_INVALID) {
5003 req_txp_cp.handle = cpu_to_le16(conn->handle);
5004 req_txp_cp.type = 0x01;
5005 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5006 sizeof(req_txp_cp), &req_txp_cp);
5007 }
5008
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005009 err = hci_req_run(&req, conn_info_refresh_complete);
5010 if (err < 0)
5011 goto unlock;
5012
5013 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev,
5014 data, len);
5015 if (!cmd) {
5016 err = -ENOMEM;
5017 goto unlock;
5018 }
5019
5020 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005021 cmd->user_data = hci_conn_get(conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005022 cmd->cmd_complete = conn_info_cmd_complete;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005023
5024 conn->conn_info_timestamp = jiffies;
5025 } else {
5026 /* Cache is valid, just reply with values cached in hci_conn */
5027 rp.rssi = conn->rssi;
5028 rp.tx_power = conn->tx_power;
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005029 rp.max_tx_power = conn->max_tx_power;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005030
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005031 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5032 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005033 }
5034
5035unlock:
5036 hci_dev_unlock(hdev);
5037 return err;
5038}
5039
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005040static int clock_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg69487372014-12-05 13:36:07 +02005041{
5042 struct hci_conn *conn = cmd->user_data;
5043 struct mgmt_rp_get_clock_info rp;
5044 struct hci_dev *hdev;
Johan Hedberg9df74652014-12-19 22:26:03 +02005045 int err;
Johan Hedberg69487372014-12-05 13:36:07 +02005046
5047 memset(&rp, 0, sizeof(rp));
Marcel Holtmann56f787c2016-08-29 06:19:47 +02005048 memcpy(&rp.addr, cmd->param, sizeof(rp.addr));
Johan Hedberg69487372014-12-05 13:36:07 +02005049
5050 if (status)
5051 goto complete;
5052
5053 hdev = hci_dev_get(cmd->index);
5054 if (hdev) {
5055 rp.local_clock = cpu_to_le32(hdev->clock);
5056 hci_dev_put(hdev);
5057 }
5058
5059 if (conn) {
5060 rp.piconet_clock = cpu_to_le32(conn->clock);
5061 rp.accuracy = cpu_to_le16(conn->clock_accuracy);
5062 }
5063
5064complete:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005065 err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp,
5066 sizeof(rp));
Johan Hedberg69487372014-12-05 13:36:07 +02005067
5068 if (conn) {
5069 hci_conn_drop(conn);
5070 hci_conn_put(conn);
5071 }
Johan Hedberg9df74652014-12-19 22:26:03 +02005072
5073 return err;
Johan Hedberg69487372014-12-05 13:36:07 +02005074}
5075
Marcel Holtmann1904a852015-01-11 13:50:44 -08005076static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg95868422014-06-28 17:54:07 +03005077{
Johan Hedberg95868422014-06-28 17:54:07 +03005078 struct hci_cp_read_clock *hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005079 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03005080 struct hci_conn *conn;
5081
5082 BT_DBG("%s status %u", hdev->name, status);
5083
5084 hci_dev_lock(hdev);
5085
5086 hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
5087 if (!hci_cp)
5088 goto unlock;
5089
5090 if (hci_cp->which) {
5091 u16 handle = __le16_to_cpu(hci_cp->handle);
5092 conn = hci_conn_hash_lookup_handle(hdev, handle);
5093 } else {
5094 conn = NULL;
5095 }
5096
Johan Hedberg333ae952015-03-17 13:48:47 +02005097 cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn);
Johan Hedberg95868422014-06-28 17:54:07 +03005098 if (!cmd)
5099 goto unlock;
5100
Johan Hedberg69487372014-12-05 13:36:07 +02005101 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberg95868422014-06-28 17:54:07 +03005102 mgmt_pending_remove(cmd);
Johan Hedberg95868422014-06-28 17:54:07 +03005103
5104unlock:
5105 hci_dev_unlock(hdev);
5106}
5107
5108static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
5109 u16 len)
5110{
5111 struct mgmt_cp_get_clock_info *cp = data;
5112 struct mgmt_rp_get_clock_info rp;
5113 struct hci_cp_read_clock hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005114 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03005115 struct hci_request req;
5116 struct hci_conn *conn;
5117 int err;
5118
5119 BT_DBG("%s", hdev->name);
5120
5121 memset(&rp, 0, sizeof(rp));
5122 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5123 rp.addr.type = cp->addr.type;
5124
5125 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005126 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5127 MGMT_STATUS_INVALID_PARAMS,
5128 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005129
5130 hci_dev_lock(hdev);
5131
5132 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005133 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5134 MGMT_STATUS_NOT_POWERED, &rp,
5135 sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005136 goto unlock;
5137 }
5138
5139 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
5140 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5141 &cp->addr.bdaddr);
5142 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005143 err = mgmt_cmd_complete(sk, hdev->id,
5144 MGMT_OP_GET_CLOCK_INFO,
5145 MGMT_STATUS_NOT_CONNECTED,
5146 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005147 goto unlock;
5148 }
5149 } else {
5150 conn = NULL;
5151 }
5152
5153 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len);
5154 if (!cmd) {
5155 err = -ENOMEM;
5156 goto unlock;
5157 }
5158
Johan Hedberg69487372014-12-05 13:36:07 +02005159 cmd->cmd_complete = clock_info_cmd_complete;
5160
Johan Hedberg95868422014-06-28 17:54:07 +03005161 hci_req_init(&req, hdev);
5162
5163 memset(&hci_cp, 0, sizeof(hci_cp));
5164 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5165
5166 if (conn) {
5167 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005168 cmd->user_data = hci_conn_get(conn);
Johan Hedberg95868422014-06-28 17:54:07 +03005169
5170 hci_cp.handle = cpu_to_le16(conn->handle);
5171 hci_cp.which = 0x01; /* Piconet clock */
5172 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5173 }
5174
5175 err = hci_req_run(&req, get_clock_info_complete);
5176 if (err < 0)
5177 mgmt_pending_remove(cmd);
5178
5179unlock:
5180 hci_dev_unlock(hdev);
5181 return err;
5182}
5183
Johan Hedberg5a154e62014-12-19 22:26:02 +02005184static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
5185{
5186 struct hci_conn *conn;
5187
5188 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
5189 if (!conn)
5190 return false;
5191
5192 if (conn->dst_type != type)
5193 return false;
5194
5195 if (conn->state != BT_CONNECTED)
5196 return false;
5197
5198 return true;
5199}
5200
5201/* This function requires the caller holds hdev->lock */
Johan Hedberg51d7a942015-11-11 08:11:18 +02005202static int hci_conn_params_set(struct hci_dev *hdev, bdaddr_t *addr,
Johan Hedberg5a154e62014-12-19 22:26:02 +02005203 u8 addr_type, u8 auto_connect)
5204{
Johan Hedberg5a154e62014-12-19 22:26:02 +02005205 struct hci_conn_params *params;
5206
5207 params = hci_conn_params_add(hdev, addr, addr_type);
5208 if (!params)
5209 return -EIO;
5210
5211 if (params->auto_connect == auto_connect)
5212 return 0;
5213
5214 list_del_init(&params->action);
5215
5216 switch (auto_connect) {
5217 case HCI_AUTO_CONN_DISABLED:
5218 case HCI_AUTO_CONN_LINK_LOSS:
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005219 /* If auto connect is being disabled when we're trying to
5220 * connect to device, keep connecting.
5221 */
5222 if (params->explicit_connect)
5223 list_add(&params->action, &hdev->pend_le_conns);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005224 break;
5225 case HCI_AUTO_CONN_REPORT:
Johan Hedberg49c50922015-10-16 10:07:51 +03005226 if (params->explicit_connect)
5227 list_add(&params->action, &hdev->pend_le_conns);
5228 else
5229 list_add(&params->action, &hdev->pend_le_reports);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005230 break;
5231 case HCI_AUTO_CONN_DIRECT:
5232 case HCI_AUTO_CONN_ALWAYS:
Johan Hedberg51d7a942015-11-11 08:11:18 +02005233 if (!is_connected(hdev, addr, addr_type))
Johan Hedberg5a154e62014-12-19 22:26:02 +02005234 list_add(&params->action, &hdev->pend_le_conns);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005235 break;
5236 }
5237
5238 params->auto_connect = auto_connect;
5239
5240 BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
5241 auto_connect);
5242
5243 return 0;
5244}
5245
Marcel Holtmann8afef092014-06-29 22:28:34 +02005246static void device_added(struct sock *sk, struct hci_dev *hdev,
5247 bdaddr_t *bdaddr, u8 type, u8 action)
5248{
5249 struct mgmt_ev_device_added ev;
5250
5251 bacpy(&ev.addr.bdaddr, bdaddr);
5252 ev.addr.type = type;
5253 ev.action = action;
5254
5255 mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk);
5256}
5257
Marcel Holtmann2faade52014-06-29 19:44:03 +02005258static int add_device(struct sock *sk, struct hci_dev *hdev,
5259 void *data, u16 len)
5260{
5261 struct mgmt_cp_add_device *cp = data;
5262 u8 auto_conn, addr_type;
5263 int err;
5264
5265 BT_DBG("%s", hdev->name);
5266
Johan Hedberg66593582014-07-09 12:59:14 +03005267 if (!bdaddr_type_is_valid(cp->addr.type) ||
Marcel Holtmann2faade52014-06-29 19:44:03 +02005268 !bacmp(&cp->addr.bdaddr, BDADDR_ANY))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005269 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5270 MGMT_STATUS_INVALID_PARAMS,
5271 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005272
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005273 if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005274 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5275 MGMT_STATUS_INVALID_PARAMS,
5276 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005277
5278 hci_dev_lock(hdev);
5279
Johan Hedberg66593582014-07-09 12:59:14 +03005280 if (cp->addr.type == BDADDR_BREDR) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005281 /* Only incoming connections action is supported for now */
Johan Hedberg66593582014-07-09 12:59:14 +03005282 if (cp->action != 0x01) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005283 err = mgmt_cmd_complete(sk, hdev->id,
5284 MGMT_OP_ADD_DEVICE,
5285 MGMT_STATUS_INVALID_PARAMS,
5286 &cp->addr, sizeof(cp->addr));
Johan Hedberg66593582014-07-09 12:59:14 +03005287 goto unlock;
5288 }
5289
5290 err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr,
5291 cp->addr.type);
5292 if (err)
5293 goto unlock;
Johan Hedberga3974072014-07-09 12:59:15 +03005294
Johan Hedberg01b1cb82015-11-16 12:52:21 +02005295 hci_req_update_scan(hdev);
Johan Hedberga3974072014-07-09 12:59:15 +03005296
Johan Hedberg66593582014-07-09 12:59:14 +03005297 goto added;
5298 }
5299
Johan Hedberg85813a72015-10-21 18:02:59 +03005300 addr_type = le_addr_type(cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005301
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005302 if (cp->action == 0x02)
Marcel Holtmann2faade52014-06-29 19:44:03 +02005303 auto_conn = HCI_AUTO_CONN_ALWAYS;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005304 else if (cp->action == 0x01)
5305 auto_conn = HCI_AUTO_CONN_DIRECT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005306 else
Johan Hedberga3451d22014-07-02 17:37:27 +03005307 auto_conn = HCI_AUTO_CONN_REPORT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005308
Jakub Pawlowski9a0a8a82015-07-20 13:12:49 +02005309 /* Kernel internally uses conn_params with resolvable private
5310 * address, but Add Device allows only identity addresses.
5311 * Make sure it is enforced before calling
5312 * hci_conn_params_lookup.
5313 */
5314 if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005315 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5316 MGMT_STATUS_INVALID_PARAMS,
5317 &cp->addr, sizeof(cp->addr));
Jakub Pawlowski9a0a8a82015-07-20 13:12:49 +02005318 goto unlock;
5319 }
5320
Marcel Holtmannbf5b3c82014-06-30 12:34:39 +02005321 /* If the connection parameters don't exist for this device,
5322 * they will be created and configured with defaults.
5323 */
Johan Hedberg51d7a942015-11-11 08:11:18 +02005324 if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type,
Marcel Holtmannd06b50c2014-07-01 12:11:06 +02005325 auto_conn) < 0) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005326 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5327 MGMT_STATUS_FAILED, &cp->addr,
5328 sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005329 goto unlock;
5330 }
5331
Johan Hedberg51d7a942015-11-11 08:11:18 +02005332 hci_update_background_scan(hdev);
5333
Johan Hedberg66593582014-07-09 12:59:14 +03005334added:
Marcel Holtmann8afef092014-06-29 22:28:34 +02005335 device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
5336
Johan Hedberg51d7a942015-11-11 08:11:18 +02005337 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5338 MGMT_STATUS_SUCCESS, &cp->addr,
5339 sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005340
5341unlock:
5342 hci_dev_unlock(hdev);
5343 return err;
5344}
5345
Marcel Holtmann8afef092014-06-29 22:28:34 +02005346static void device_removed(struct sock *sk, struct hci_dev *hdev,
5347 bdaddr_t *bdaddr, u8 type)
5348{
5349 struct mgmt_ev_device_removed ev;
5350
5351 bacpy(&ev.addr.bdaddr, bdaddr);
5352 ev.addr.type = type;
5353
5354 mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk);
5355}
5356
Marcel Holtmann2faade52014-06-29 19:44:03 +02005357static int remove_device(struct sock *sk, struct hci_dev *hdev,
5358 void *data, u16 len)
5359{
5360 struct mgmt_cp_remove_device *cp = data;
5361 int err;
5362
5363 BT_DBG("%s", hdev->name);
5364
5365 hci_dev_lock(hdev);
5366
5367 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
Johan Hedbergc71593d2014-07-02 17:37:28 +03005368 struct hci_conn_params *params;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005369 u8 addr_type;
5370
Johan Hedberg66593582014-07-09 12:59:14 +03005371 if (!bdaddr_type_is_valid(cp->addr.type)) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005372 err = mgmt_cmd_complete(sk, hdev->id,
5373 MGMT_OP_REMOVE_DEVICE,
5374 MGMT_STATUS_INVALID_PARAMS,
5375 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005376 goto unlock;
5377 }
5378
Johan Hedberg66593582014-07-09 12:59:14 +03005379 if (cp->addr.type == BDADDR_BREDR) {
5380 err = hci_bdaddr_list_del(&hdev->whitelist,
5381 &cp->addr.bdaddr,
5382 cp->addr.type);
5383 if (err) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005384 err = mgmt_cmd_complete(sk, hdev->id,
5385 MGMT_OP_REMOVE_DEVICE,
5386 MGMT_STATUS_INVALID_PARAMS,
5387 &cp->addr,
5388 sizeof(cp->addr));
Johan Hedberg66593582014-07-09 12:59:14 +03005389 goto unlock;
5390 }
5391
Johan Hedberg01b1cb82015-11-16 12:52:21 +02005392 hci_req_update_scan(hdev);
Johan Hedberga3974072014-07-09 12:59:15 +03005393
Johan Hedberg66593582014-07-09 12:59:14 +03005394 device_removed(sk, hdev, &cp->addr.bdaddr,
5395 cp->addr.type);
5396 goto complete;
5397 }
5398
Johan Hedberg85813a72015-10-21 18:02:59 +03005399 addr_type = le_addr_type(cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005400
Jakub Pawlowski9a0a8a82015-07-20 13:12:49 +02005401 /* Kernel internally uses conn_params with resolvable private
5402 * address, but Remove Device allows only identity addresses.
5403 * Make sure it is enforced before calling
5404 * hci_conn_params_lookup.
5405 */
5406 if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005407 err = mgmt_cmd_complete(sk, hdev->id,
5408 MGMT_OP_REMOVE_DEVICE,
5409 MGMT_STATUS_INVALID_PARAMS,
5410 &cp->addr, sizeof(cp->addr));
Jakub Pawlowski9a0a8a82015-07-20 13:12:49 +02005411 goto unlock;
5412 }
5413
Johan Hedbergc71593d2014-07-02 17:37:28 +03005414 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
5415 addr_type);
5416 if (!params) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005417 err = mgmt_cmd_complete(sk, hdev->id,
5418 MGMT_OP_REMOVE_DEVICE,
5419 MGMT_STATUS_INVALID_PARAMS,
5420 &cp->addr, sizeof(cp->addr));
Johan Hedbergc71593d2014-07-02 17:37:28 +03005421 goto unlock;
5422 }
5423
Johan Hedberg679d2b62015-10-16 10:07:52 +03005424 if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
5425 params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005426 err = mgmt_cmd_complete(sk, hdev->id,
5427 MGMT_OP_REMOVE_DEVICE,
5428 MGMT_STATUS_INVALID_PARAMS,
5429 &cp->addr, sizeof(cp->addr));
Johan Hedbergc71593d2014-07-02 17:37:28 +03005430 goto unlock;
5431 }
5432
Johan Hedbergd1dbf122014-07-04 16:17:23 +03005433 list_del(&params->action);
Johan Hedbergc71593d2014-07-02 17:37:28 +03005434 list_del(&params->list);
5435 kfree(params);
Johan Hedberg51d7a942015-11-11 08:11:18 +02005436 hci_update_background_scan(hdev);
Marcel Holtmann8afef092014-06-29 22:28:34 +02005437
5438 device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005439 } else {
Johan Hedberg19de0822014-07-06 13:06:51 +03005440 struct hci_conn_params *p, *tmp;
Johan Hedberg66593582014-07-09 12:59:14 +03005441 struct bdaddr_list *b, *btmp;
Johan Hedberg19de0822014-07-06 13:06:51 +03005442
Marcel Holtmann2faade52014-06-29 19:44:03 +02005443 if (cp->addr.type) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005444 err = mgmt_cmd_complete(sk, hdev->id,
5445 MGMT_OP_REMOVE_DEVICE,
5446 MGMT_STATUS_INVALID_PARAMS,
5447 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005448 goto unlock;
5449 }
5450
Johan Hedberg66593582014-07-09 12:59:14 +03005451 list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) {
5452 device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type);
5453 list_del(&b->list);
5454 kfree(b);
5455 }
5456
Johan Hedberg01b1cb82015-11-16 12:52:21 +02005457 hci_req_update_scan(hdev);
Johan Hedberga3974072014-07-09 12:59:15 +03005458
Johan Hedberg19de0822014-07-06 13:06:51 +03005459 list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) {
5460 if (p->auto_connect == HCI_AUTO_CONN_DISABLED)
5461 continue;
5462 device_removed(sk, hdev, &p->addr, p->addr_type);
Johan Hedberg679d2b62015-10-16 10:07:52 +03005463 if (p->explicit_connect) {
5464 p->auto_connect = HCI_AUTO_CONN_EXPLICIT;
5465 continue;
5466 }
Johan Hedberg19de0822014-07-06 13:06:51 +03005467 list_del(&p->action);
5468 list_del(&p->list);
5469 kfree(p);
5470 }
5471
5472 BT_DBG("All LE connection parameters were removed");
5473
Johan Hedberg51d7a942015-11-11 08:11:18 +02005474 hci_update_background_scan(hdev);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005475 }
5476
Johan Hedberg66593582014-07-09 12:59:14 +03005477complete:
Johan Hedberg51d7a942015-11-11 08:11:18 +02005478 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE,
5479 MGMT_STATUS_SUCCESS, &cp->addr,
5480 sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005481unlock:
5482 hci_dev_unlock(hdev);
5483 return err;
5484}
5485
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005486static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
5487 u16 len)
5488{
5489 struct mgmt_cp_load_conn_param *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005490 const u16 max_param_count = ((U16_MAX - sizeof(*cp)) /
5491 sizeof(struct mgmt_conn_param));
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005492 u16 param_count, expected_len;
5493 int i;
5494
5495 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005496 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
5497 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005498
5499 param_count = __le16_to_cpu(cp->param_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005500 if (param_count > max_param_count) {
5501 BT_ERR("load_conn_param: too big param_count value %u",
5502 param_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02005503 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
5504 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005505 }
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005506
5507 expected_len = sizeof(*cp) + param_count *
5508 sizeof(struct mgmt_conn_param);
5509 if (expected_len != len) {
5510 BT_ERR("load_conn_param: expected %u bytes, got %u bytes",
5511 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02005512 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
5513 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005514 }
5515
5516 BT_DBG("%s param_count %u", hdev->name, param_count);
5517
5518 hci_dev_lock(hdev);
5519
5520 hci_conn_params_clear_disabled(hdev);
5521
5522 for (i = 0; i < param_count; i++) {
5523 struct mgmt_conn_param *param = &cp->params[i];
5524 struct hci_conn_params *hci_param;
5525 u16 min, max, latency, timeout;
5526 u8 addr_type;
5527
5528 BT_DBG("Adding %pMR (type %u)", &param->addr.bdaddr,
5529 param->addr.type);
5530
5531 if (param->addr.type == BDADDR_LE_PUBLIC) {
5532 addr_type = ADDR_LE_DEV_PUBLIC;
5533 } else if (param->addr.type == BDADDR_LE_RANDOM) {
5534 addr_type = ADDR_LE_DEV_RANDOM;
5535 } else {
5536 BT_ERR("Ignoring invalid connection parameters");
5537 continue;
5538 }
5539
5540 min = le16_to_cpu(param->min_interval);
5541 max = le16_to_cpu(param->max_interval);
5542 latency = le16_to_cpu(param->latency);
5543 timeout = le16_to_cpu(param->timeout);
5544
5545 BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x",
5546 min, max, latency, timeout);
5547
5548 if (hci_check_conn_params(min, max, latency, timeout) < 0) {
5549 BT_ERR("Ignoring invalid connection parameters");
5550 continue;
5551 }
5552
5553 hci_param = hci_conn_params_add(hdev, &param->addr.bdaddr,
5554 addr_type);
5555 if (!hci_param) {
5556 BT_ERR("Failed to add connection parameters");
5557 continue;
5558 }
5559
5560 hci_param->conn_min_interval = min;
5561 hci_param->conn_max_interval = max;
5562 hci_param->conn_latency = latency;
5563 hci_param->supervision_timeout = timeout;
5564 }
5565
5566 hci_dev_unlock(hdev);
5567
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005568 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0,
5569 NULL, 0);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005570}
5571
Marcel Holtmanndbece372014-07-04 18:11:55 +02005572static int set_external_config(struct sock *sk, struct hci_dev *hdev,
5573 void *data, u16 len)
5574{
5575 struct mgmt_cp_set_external_config *cp = data;
5576 bool changed;
5577 int err;
5578
5579 BT_DBG("%s", hdev->name);
5580
5581 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005582 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
5583 MGMT_STATUS_REJECTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02005584
5585 if (cp->config != 0x00 && cp->config != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02005586 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
5587 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmanndbece372014-07-04 18:11:55 +02005588
5589 if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
Johan Hedberga69e8372015-03-06 21:08:53 +02005590 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
5591 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02005592
5593 hci_dev_lock(hdev);
5594
5595 if (cp->config)
Marcel Holtmann238be782015-03-13 02:11:06 -07005596 changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02005597 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005598 changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02005599
5600 err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
5601 if (err < 0)
5602 goto unlock;
5603
5604 if (!changed)
5605 goto unlock;
5606
Marcel Holtmannf4537c02014-07-04 19:06:23 +02005607 err = new_options(hdev, sk);
5608
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005609 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) {
Marcel Holtmanndbece372014-07-04 18:11:55 +02005610 mgmt_index_removed(hdev);
Marcel Holtmannd603b762014-07-06 12:11:14 +02005611
Marcel Holtmann516018a2015-03-13 02:11:04 -07005612 if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005613 hci_dev_set_flag(hdev, HCI_CONFIG);
5614 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmannd603b762014-07-06 12:11:14 +02005615
5616 queue_work(hdev->req_workqueue, &hdev->power_on);
5617 } else {
Marcel Holtmann5ea234d2014-07-06 12:11:16 +02005618 set_bit(HCI_RAW, &hdev->flags);
Marcel Holtmannd603b762014-07-06 12:11:14 +02005619 mgmt_index_added(hdev);
5620 }
Marcel Holtmanndbece372014-07-04 18:11:55 +02005621 }
5622
5623unlock:
5624 hci_dev_unlock(hdev);
5625 return err;
5626}
5627
Marcel Holtmann9713c172014-07-06 12:11:15 +02005628static int set_public_address(struct sock *sk, struct hci_dev *hdev,
5629 void *data, u16 len)
5630{
5631 struct mgmt_cp_set_public_address *cp = data;
5632 bool changed;
5633 int err;
5634
5635 BT_DBG("%s", hdev->name);
5636
5637 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005638 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
5639 MGMT_STATUS_REJECTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02005640
5641 if (!bacmp(&cp->bdaddr, BDADDR_ANY))
Johan Hedberga69e8372015-03-06 21:08:53 +02005642 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
5643 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann9713c172014-07-06 12:11:15 +02005644
5645 if (!hdev->set_bdaddr)
Johan Hedberga69e8372015-03-06 21:08:53 +02005646 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
5647 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02005648
5649 hci_dev_lock(hdev);
5650
5651 changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
5652 bacpy(&hdev->public_addr, &cp->bdaddr);
5653
5654 err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev);
5655 if (err < 0)
5656 goto unlock;
5657
5658 if (!changed)
5659 goto unlock;
5660
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005661 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
Marcel Holtmann9713c172014-07-06 12:11:15 +02005662 err = new_options(hdev, sk);
5663
5664 if (is_configured(hdev)) {
5665 mgmt_index_removed(hdev);
5666
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005667 hci_dev_clear_flag(hdev, HCI_UNCONFIGURED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02005668
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005669 hci_dev_set_flag(hdev, HCI_CONFIG);
5670 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmann9713c172014-07-06 12:11:15 +02005671
5672 queue_work(hdev->req_workqueue, &hdev->power_on);
5673 }
5674
5675unlock:
5676 hci_dev_unlock(hdev);
5677 return err;
5678}
5679
Johan Hedberg40f66c02015-04-07 21:52:22 +03005680static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status,
5681 u16 opcode, struct sk_buff *skb)
5682{
5683 const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp;
5684 struct mgmt_rp_read_local_oob_ext_data *mgmt_rp;
5685 u8 *h192, *r192, *h256, *r256;
5686 struct mgmt_pending_cmd *cmd;
5687 u16 eir_len;
5688 int err;
5689
5690 BT_DBG("%s status %u", hdev->name, status);
5691
5692 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev);
5693 if (!cmd)
5694 return;
5695
5696 mgmt_cp = cmd->param;
5697
5698 if (status) {
5699 status = mgmt_status(status);
5700 eir_len = 0;
5701
5702 h192 = NULL;
5703 r192 = NULL;
5704 h256 = NULL;
5705 r256 = NULL;
5706 } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
5707 struct hci_rp_read_local_oob_data *rp;
5708
5709 if (skb->len != sizeof(*rp)) {
5710 status = MGMT_STATUS_FAILED;
5711 eir_len = 0;
5712 } else {
5713 status = MGMT_STATUS_SUCCESS;
5714 rp = (void *)skb->data;
5715
5716 eir_len = 5 + 18 + 18;
5717 h192 = rp->hash;
5718 r192 = rp->rand;
5719 h256 = NULL;
5720 r256 = NULL;
5721 }
5722 } else {
5723 struct hci_rp_read_local_oob_ext_data *rp;
5724
5725 if (skb->len != sizeof(*rp)) {
5726 status = MGMT_STATUS_FAILED;
5727 eir_len = 0;
5728 } else {
5729 status = MGMT_STATUS_SUCCESS;
5730 rp = (void *)skb->data;
5731
5732 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
5733 eir_len = 5 + 18 + 18;
5734 h192 = NULL;
5735 r192 = NULL;
5736 } else {
5737 eir_len = 5 + 18 + 18 + 18 + 18;
5738 h192 = rp->hash192;
5739 r192 = rp->rand192;
5740 }
5741
5742 h256 = rp->hash256;
5743 r256 = rp->rand256;
5744 }
5745 }
5746
5747 mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL);
5748 if (!mgmt_rp)
5749 goto done;
5750
5751 if (status)
5752 goto send_rsp;
5753
5754 eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV,
5755 hdev->dev_class, 3);
5756
5757 if (h192 && r192) {
5758 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
5759 EIR_SSP_HASH_C192, h192, 16);
5760 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
5761 EIR_SSP_RAND_R192, r192, 16);
5762 }
5763
5764 if (h256 && r256) {
5765 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
5766 EIR_SSP_HASH_C256, h256, 16);
5767 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
5768 EIR_SSP_RAND_R256, r256, 16);
5769 }
5770
5771send_rsp:
5772 mgmt_rp->type = mgmt_cp->type;
5773 mgmt_rp->eir_len = cpu_to_le16(eir_len);
5774
5775 err = mgmt_cmd_complete(cmd->sk, hdev->id,
5776 MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status,
5777 mgmt_rp, sizeof(*mgmt_rp) + eir_len);
5778 if (err < 0 || status)
5779 goto done;
5780
5781 hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS);
5782
5783 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
5784 mgmt_rp, sizeof(*mgmt_rp) + eir_len,
5785 HCI_MGMT_OOB_DATA_EVENTS, cmd->sk);
5786done:
5787 kfree(mgmt_rp);
5788 mgmt_pending_remove(cmd);
5789}
5790
5791static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk,
5792 struct mgmt_cp_read_local_oob_ext_data *cp)
5793{
5794 struct mgmt_pending_cmd *cmd;
5795 struct hci_request req;
5796 int err;
5797
5798 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev,
5799 cp, sizeof(*cp));
5800 if (!cmd)
5801 return -ENOMEM;
5802
5803 hci_req_init(&req, hdev);
5804
5805 if (bredr_sc_enabled(hdev))
5806 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
5807 else
5808 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
5809
5810 err = hci_req_run_skb(&req, read_local_oob_ext_data_complete);
5811 if (err < 0) {
5812 mgmt_pending_remove(cmd);
5813 return err;
5814 }
5815
5816 return 0;
5817}
5818
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005819static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
5820 void *data, u16 data_len)
5821{
5822 struct mgmt_cp_read_local_oob_ext_data *cp = data;
5823 struct mgmt_rp_read_local_oob_ext_data *rp;
5824 size_t rp_len;
5825 u16 eir_len;
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07005826 u8 status, flags, role, addr[7], hash[16], rand[16];
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005827 int err;
5828
5829 BT_DBG("%s", hdev->name);
5830
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07005831 if (hdev_is_powered(hdev)) {
5832 switch (cp->type) {
5833 case BIT(BDADDR_BREDR):
5834 status = mgmt_bredr_support(hdev);
5835 if (status)
5836 eir_len = 0;
5837 else
5838 eir_len = 5;
5839 break;
5840 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
5841 status = mgmt_le_support(hdev);
5842 if (status)
5843 eir_len = 0;
5844 else
5845 eir_len = 9 + 3 + 18 + 18 + 3;
5846 break;
5847 default:
5848 status = MGMT_STATUS_INVALID_PARAMS;
5849 eir_len = 0;
5850 break;
5851 }
5852 } else {
5853 status = MGMT_STATUS_NOT_POWERED;
5854 eir_len = 0;
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005855 }
5856
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005857 rp_len = sizeof(*rp) + eir_len;
5858 rp = kmalloc(rp_len, GFP_ATOMIC);
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07005859 if (!rp)
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005860 return -ENOMEM;
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07005861
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07005862 if (status)
5863 goto complete;
5864
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07005865 hci_dev_lock(hdev);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005866
5867 eir_len = 0;
5868 switch (cp->type) {
5869 case BIT(BDADDR_BREDR):
Johan Hedberg40f66c02015-04-07 21:52:22 +03005870 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
5871 err = read_local_ssp_oob_req(hdev, sk, cp);
5872 hci_dev_unlock(hdev);
5873 if (!err)
5874 goto done;
5875
5876 status = MGMT_STATUS_FAILED;
5877 goto complete;
5878 } else {
5879 eir_len = eir_append_data(rp->eir, eir_len,
5880 EIR_CLASS_OF_DEV,
5881 hdev->dev_class, 3);
5882 }
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005883 break;
5884 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
Marcel Holtmann5082a592015-03-16 12:39:00 -07005885 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
5886 smp_generate_oob(hdev, hash, rand) < 0) {
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07005887 hci_dev_unlock(hdev);
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07005888 status = MGMT_STATUS_FAILED;
5889 goto complete;
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07005890 }
5891
Marcel Holtmanne2135682015-04-02 12:00:58 -07005892 /* This should return the active RPA, but since the RPA
5893 * is only programmed on demand, it is really hard to fill
5894 * this in at the moment. For now disallow retrieving
5895 * local out-of-band data when privacy is in use.
5896 *
5897 * Returning the identity address will not help here since
5898 * pairing happens before the identity resolving key is
5899 * known and thus the connection establishment happens
5900 * based on the RPA and not the identity address.
5901 */
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005902 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
Marcel Holtmanne2135682015-04-02 12:00:58 -07005903 hci_dev_unlock(hdev);
5904 status = MGMT_STATUS_REJECTED;
5905 goto complete;
5906 }
5907
5908 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
5909 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
5910 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
5911 bacmp(&hdev->static_addr, BDADDR_ANY))) {
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005912 memcpy(addr, &hdev->static_addr, 6);
5913 addr[6] = 0x01;
5914 } else {
5915 memcpy(addr, &hdev->bdaddr, 6);
5916 addr[6] = 0x00;
5917 }
5918
5919 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR,
5920 addr, sizeof(addr));
5921
5922 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
5923 role = 0x02;
5924 else
5925 role = 0x01;
5926
5927 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE,
5928 &role, sizeof(role));
5929
Marcel Holtmann5082a592015-03-16 12:39:00 -07005930 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) {
5931 eir_len = eir_append_data(rp->eir, eir_len,
5932 EIR_LE_SC_CONFIRM,
5933 hash, sizeof(hash));
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07005934
Marcel Holtmann5082a592015-03-16 12:39:00 -07005935 eir_len = eir_append_data(rp->eir, eir_len,
5936 EIR_LE_SC_RANDOM,
5937 rand, sizeof(rand));
5938 }
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07005939
Johan Hedbergf2252572015-11-18 12:49:20 +02005940 flags = mgmt_get_adv_discov_flags(hdev);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005941
5942 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5943 flags |= LE_AD_NO_BREDR;
5944
5945 eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS,
5946 &flags, sizeof(flags));
5947 break;
5948 }
5949
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005950 hci_dev_unlock(hdev);
5951
Marcel Holtmann72000df2015-03-16 16:11:21 -07005952 hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS);
5953
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07005954 status = MGMT_STATUS_SUCCESS;
5955
5956complete:
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07005957 rp->type = cp->type;
5958 rp->eir_len = cpu_to_le16(eir_len);
5959
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005960 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07005961 status, rp, sizeof(*rp) + eir_len);
5962 if (err < 0 || status)
Marcel Holtmann72000df2015-03-16 16:11:21 -07005963 goto done;
5964
5965 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
5966 rp, sizeof(*rp) + eir_len,
5967 HCI_MGMT_OOB_DATA_EVENTS, sk);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005968
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07005969done:
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005970 kfree(rp);
5971
5972 return err;
5973}
5974
Arman Uguray089fa8c2015-03-25 18:53:45 -07005975static u32 get_supported_adv_flags(struct hci_dev *hdev)
5976{
5977 u32 flags = 0;
5978
5979 flags |= MGMT_ADV_FLAG_CONNECTABLE;
5980 flags |= MGMT_ADV_FLAG_DISCOV;
5981 flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
5982 flags |= MGMT_ADV_FLAG_MANAGED_FLAGS;
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02005983 flags |= MGMT_ADV_FLAG_APPEARANCE;
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02005984 flags |= MGMT_ADV_FLAG_LOCAL_NAME;
Arman Uguray089fa8c2015-03-25 18:53:45 -07005985
5986 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID)
5987 flags |= MGMT_ADV_FLAG_TX_POWER;
5988
5989 return flags;
5990}
5991
Marcel Holtmannd3d53052015-03-14 20:53:25 -07005992static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
5993 void *data, u16 data_len)
5994{
5995 struct mgmt_rp_read_adv_features *rp;
5996 size_t rp_len;
Johan Hedberg02c04af2015-11-26 12:15:58 +02005997 int err;
Florian Grandel286e0c82015-06-18 03:16:38 +02005998 struct adv_info *adv_instance;
Arman Uguray089fa8c2015-03-25 18:53:45 -07005999 u32 supported_flags;
Johan Hedberg02c04af2015-11-26 12:15:58 +02006000 u8 *instance;
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006001
6002 BT_DBG("%s", hdev->name);
6003
Arman Uguray089fa8c2015-03-25 18:53:45 -07006004 if (!lmp_le_capable(hdev))
6005 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
6006 MGMT_STATUS_REJECTED);
6007
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006008 hci_dev_lock(hdev);
6009
Johan Hedberg02c04af2015-11-26 12:15:58 +02006010 rp_len = sizeof(*rp) + hdev->adv_instance_cnt;
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006011 rp = kmalloc(rp_len, GFP_ATOMIC);
6012 if (!rp) {
6013 hci_dev_unlock(hdev);
6014 return -ENOMEM;
6015 }
6016
Arman Uguray089fa8c2015-03-25 18:53:45 -07006017 supported_flags = get_supported_adv_flags(hdev);
6018
6019 rp->supported_flags = cpu_to_le32(supported_flags);
Marcel Holtmanndc5d82a2015-03-19 17:22:25 -07006020 rp->max_adv_data_len = HCI_MAX_AD_LENGTH;
6021 rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH;
Florian Grandeld2609b32015-06-18 03:16:34 +02006022 rp->max_instances = HCI_MAX_ADV_INSTANCES;
Johan Hedberg02c04af2015-11-26 12:15:58 +02006023 rp->num_instances = hdev->adv_instance_cnt;
Arman Uguray24b4f382015-03-23 15:57:12 -07006024
Johan Hedberg02c04af2015-11-26 12:15:58 +02006025 instance = rp->instance;
6026 list_for_each_entry(adv_instance, &hdev->adv_instances, list) {
6027 *instance = adv_instance->instance;
6028 instance++;
Arman Uguray24b4f382015-03-23 15:57:12 -07006029 }
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006030
6031 hci_dev_unlock(hdev);
6032
6033 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
6034 MGMT_STATUS_SUCCESS, rp, rp_len);
6035
6036 kfree(rp);
6037
6038 return err;
6039}
6040
Szymon Janc2bb368702016-09-18 12:50:05 +02006041static u8 tlv_data_max_len(u32 adv_flags, bool is_adv_data)
Arman Uguray24b4f382015-03-23 15:57:12 -07006042{
Arman Uguray4117ed72015-03-23 15:57:14 -07006043 u8 max_len = HCI_MAX_AD_LENGTH;
Arman Uguray24b4f382015-03-23 15:57:12 -07006044
Marcel Holtmann31a32482015-11-19 16:16:42 +01006045 if (is_adv_data) {
6046 if (adv_flags & (MGMT_ADV_FLAG_DISCOV |
6047 MGMT_ADV_FLAG_LIMITED_DISCOV |
Szymon Janc2bb368702016-09-18 12:50:05 +02006048 MGMT_ADV_FLAG_MANAGED_FLAGS))
Marcel Holtmann31a32482015-11-19 16:16:42 +01006049 max_len -= 3;
Arman Uguray24b4f382015-03-23 15:57:12 -07006050
Szymon Janc2bb368702016-09-18 12:50:05 +02006051 if (adv_flags & MGMT_ADV_FLAG_TX_POWER)
Marcel Holtmann31a32482015-11-19 16:16:42 +01006052 max_len -= 3;
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02006053 } else {
6054 /* at least 1 byte of name should fit in */
6055 if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME)
6056 max_len -= 3;
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02006057
Szymon Janc2bb368702016-09-18 12:50:05 +02006058 if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE))
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02006059 max_len -= 4;
Arman Uguray5507e352015-03-25 18:53:44 -07006060 }
6061
Szymon Janc2bb368702016-09-18 12:50:05 +02006062 return max_len;
6063}
6064
6065static bool flags_managed(u32 adv_flags)
6066{
6067 return adv_flags & (MGMT_ADV_FLAG_DISCOV |
6068 MGMT_ADV_FLAG_LIMITED_DISCOV |
6069 MGMT_ADV_FLAG_MANAGED_FLAGS);
6070}
6071
6072static bool tx_power_managed(u32 adv_flags)
6073{
6074 return adv_flags & MGMT_ADV_FLAG_TX_POWER;
6075}
6076
6077static bool name_managed(u32 adv_flags)
6078{
6079 return adv_flags & MGMT_ADV_FLAG_LOCAL_NAME;
6080}
6081
6082static bool appearance_managed(u32 adv_flags)
6083{
6084 return adv_flags & MGMT_ADV_FLAG_APPEARANCE;
6085}
6086
6087static bool tlv_data_is_valid(u32 adv_flags, u8 *data, u8 len, bool is_adv_data)
6088{
6089 int i, cur_len;
6090 u8 max_len;
6091
6092 max_len = tlv_data_max_len(adv_flags, is_adv_data);
6093
Arman Uguray4117ed72015-03-23 15:57:14 -07006094 if (len > max_len)
Arman Uguray24b4f382015-03-23 15:57:12 -07006095 return false;
6096
Arman Uguray4117ed72015-03-23 15:57:14 -07006097 /* Make sure that the data is correctly formatted. */
6098 for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) {
6099 cur_len = data[i];
Arman Uguray24b4f382015-03-23 15:57:12 -07006100
Szymon Janc9c9db782016-09-18 12:50:06 +02006101 if (data[i + 1] == EIR_FLAGS &&
6102 (!is_adv_data || flags_managed(adv_flags)))
Arman Ugurayb44133f2015-03-25 18:53:41 -07006103 return false;
6104
Szymon Janc2bb368702016-09-18 12:50:05 +02006105 if (data[i + 1] == EIR_TX_POWER && tx_power_managed(adv_flags))
6106 return false;
6107
6108 if (data[i + 1] == EIR_NAME_COMPLETE && name_managed(adv_flags))
6109 return false;
6110
6111 if (data[i + 1] == EIR_NAME_SHORT && name_managed(adv_flags))
6112 return false;
6113
6114 if (data[i + 1] == EIR_APPEARANCE &&
6115 appearance_managed(adv_flags))
Arman Uguray5507e352015-03-25 18:53:44 -07006116 return false;
6117
Arman Uguray24b4f382015-03-23 15:57:12 -07006118 /* If the current field length would exceed the total data
6119 * length, then it's invalid.
6120 */
Arman Uguray4117ed72015-03-23 15:57:14 -07006121 if (i + cur_len >= len)
Arman Uguray24b4f382015-03-23 15:57:12 -07006122 return false;
6123 }
6124
6125 return true;
6126}
6127
Arman Uguray24b4f382015-03-23 15:57:12 -07006128static void add_advertising_complete(struct hci_dev *hdev, u8 status,
6129 u16 opcode)
6130{
6131 struct mgmt_pending_cmd *cmd;
Florian Grandelfffd38b2015-06-18 03:16:47 +02006132 struct mgmt_cp_add_advertising *cp;
Arman Uguray24b4f382015-03-23 15:57:12 -07006133 struct mgmt_rp_add_advertising rp;
Florian Grandelfffd38b2015-06-18 03:16:47 +02006134 struct adv_info *adv_instance, *n;
6135 u8 instance;
Arman Uguray24b4f382015-03-23 15:57:12 -07006136
6137 BT_DBG("status %d", status);
6138
6139 hci_dev_lock(hdev);
6140
6141 cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev);
6142
Florian Grandelfffd38b2015-06-18 03:16:47 +02006143 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) {
6144 if (!adv_instance->pending)
6145 continue;
6146
6147 if (!status) {
6148 adv_instance->pending = false;
6149 continue;
6150 }
6151
6152 instance = adv_instance->instance;
6153
6154 if (hdev->cur_adv_instance == instance)
6155 cancel_adv_timeout(hdev);
6156
6157 hci_remove_adv_instance(hdev, instance);
Johan Hedbergf2252572015-11-18 12:49:20 +02006158 mgmt_advertising_removed(cmd ? cmd->sk : NULL, hdev, instance);
Arman Uguray24b4f382015-03-23 15:57:12 -07006159 }
6160
6161 if (!cmd)
6162 goto unlock;
6163
Florian Grandelfffd38b2015-06-18 03:16:47 +02006164 cp = cmd->param;
6165 rp.instance = cp->instance;
Arman Uguray24b4f382015-03-23 15:57:12 -07006166
6167 if (status)
6168 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
6169 mgmt_status(status));
6170 else
6171 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
6172 mgmt_status(status), &rp, sizeof(rp));
6173
6174 mgmt_pending_remove(cmd);
6175
6176unlock:
6177 hci_dev_unlock(hdev);
6178}
6179
6180static int add_advertising(struct sock *sk, struct hci_dev *hdev,
6181 void *data, u16 data_len)
6182{
6183 struct mgmt_cp_add_advertising *cp = data;
6184 struct mgmt_rp_add_advertising rp;
6185 u32 flags;
Arman Uguray089fa8c2015-03-25 18:53:45 -07006186 u32 supported_flags;
Arman Uguray24b4f382015-03-23 15:57:12 -07006187 u8 status;
Florian Grandelfffd38b2015-06-18 03:16:47 +02006188 u16 timeout, duration;
6189 unsigned int prev_instance_cnt = hdev->adv_instance_cnt;
6190 u8 schedule_instance = 0;
6191 struct adv_info *next_instance;
Arman Uguray24b4f382015-03-23 15:57:12 -07006192 int err;
6193 struct mgmt_pending_cmd *cmd;
6194 struct hci_request req;
6195
6196 BT_DBG("%s", hdev->name);
6197
6198 status = mgmt_le_support(hdev);
6199 if (status)
6200 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6201 status);
6202
Marcel Holtmannceff86a2015-11-19 16:16:41 +01006203 if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES)
6204 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6205 MGMT_STATUS_INVALID_PARAMS);
6206
Johan Hedberg6a0e7802016-03-11 09:56:33 +02006207 if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len)
6208 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6209 MGMT_STATUS_INVALID_PARAMS);
6210
Arman Uguray24b4f382015-03-23 15:57:12 -07006211 flags = __le32_to_cpu(cp->flags);
Arman Uguray912098a2015-03-23 15:57:15 -07006212 timeout = __le16_to_cpu(cp->timeout);
Florian Grandelfffd38b2015-06-18 03:16:47 +02006213 duration = __le16_to_cpu(cp->duration);
Arman Uguray24b4f382015-03-23 15:57:12 -07006214
Florian Grandelfffd38b2015-06-18 03:16:47 +02006215 /* The current implementation only supports a subset of the specified
6216 * flags.
Arman Uguray089fa8c2015-03-25 18:53:45 -07006217 */
6218 supported_flags = get_supported_adv_flags(hdev);
Florian Grandelfffd38b2015-06-18 03:16:47 +02006219 if (flags & ~supported_flags)
Arman Uguray24b4f382015-03-23 15:57:12 -07006220 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6221 MGMT_STATUS_INVALID_PARAMS);
6222
6223 hci_dev_lock(hdev);
6224
Arman Uguray912098a2015-03-23 15:57:15 -07006225 if (timeout && !hdev_is_powered(hdev)) {
6226 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6227 MGMT_STATUS_REJECTED);
6228 goto unlock;
6229 }
6230
Arman Uguray24b4f382015-03-23 15:57:12 -07006231 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
Arman Ugurayda9293352015-03-23 15:57:13 -07006232 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
Arman Uguray24b4f382015-03-23 15:57:12 -07006233 pending_find(MGMT_OP_SET_LE, hdev)) {
6234 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6235 MGMT_STATUS_BUSY);
6236 goto unlock;
6237 }
6238
Szymon Janc5e2c59e2016-09-18 12:50:04 +02006239 if (!tlv_data_is_valid(flags, cp->data, cp->adv_data_len, true) ||
6240 !tlv_data_is_valid(flags, cp->data + cp->adv_data_len,
Arman Ugurayb44133f2015-03-25 18:53:41 -07006241 cp->scan_rsp_len, false)) {
Arman Uguray24b4f382015-03-23 15:57:12 -07006242 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6243 MGMT_STATUS_INVALID_PARAMS);
6244 goto unlock;
6245 }
6246
Florian Grandelfffd38b2015-06-18 03:16:47 +02006247 err = hci_add_adv_instance(hdev, cp->instance, flags,
6248 cp->adv_data_len, cp->data,
6249 cp->scan_rsp_len,
6250 cp->data + cp->adv_data_len,
6251 timeout, duration);
6252 if (err < 0) {
6253 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6254 MGMT_STATUS_FAILED);
6255 goto unlock;
6256 }
Arman Uguray24b4f382015-03-23 15:57:12 -07006257
Florian Grandelfffd38b2015-06-18 03:16:47 +02006258 /* Only trigger an advertising added event if a new instance was
6259 * actually added.
6260 */
6261 if (hdev->adv_instance_cnt > prev_instance_cnt)
Johan Hedbergf2252572015-11-18 12:49:20 +02006262 mgmt_advertising_added(sk, hdev, cp->instance);
Arman Uguray24b4f382015-03-23 15:57:12 -07006263
Florian Grandelfffd38b2015-06-18 03:16:47 +02006264 if (hdev->cur_adv_instance == cp->instance) {
6265 /* If the currently advertised instance is being changed then
6266 * cancel the current advertising and schedule the next
6267 * instance. If there is only one instance then the overridden
6268 * advertising data will be visible right away.
6269 */
6270 cancel_adv_timeout(hdev);
Arman Uguray912098a2015-03-23 15:57:15 -07006271
Florian Grandelfffd38b2015-06-18 03:16:47 +02006272 next_instance = hci_get_next_instance(hdev, cp->instance);
6273 if (next_instance)
6274 schedule_instance = next_instance->instance;
6275 } else if (!hdev->adv_instance_timeout) {
6276 /* Immediately advertise the new instance if no other
6277 * instance is currently being advertised.
6278 */
6279 schedule_instance = cp->instance;
6280 }
Arman Uguray912098a2015-03-23 15:57:15 -07006281
Florian Grandelfffd38b2015-06-18 03:16:47 +02006282 /* If the HCI_ADVERTISING flag is set or the device isn't powered or
6283 * there is no instance to be advertised then we have no HCI
6284 * communication to make. Simply return.
Arman Uguray24b4f382015-03-23 15:57:12 -07006285 */
6286 if (!hdev_is_powered(hdev) ||
Florian Grandelfffd38b2015-06-18 03:16:47 +02006287 hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
6288 !schedule_instance) {
6289 rp.instance = cp->instance;
Arman Uguray24b4f382015-03-23 15:57:12 -07006290 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6291 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
6292 goto unlock;
6293 }
6294
6295 /* We're good to go, update advertising data, parameters, and start
6296 * advertising.
6297 */
6298 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data,
6299 data_len);
6300 if (!cmd) {
6301 err = -ENOMEM;
6302 goto unlock;
6303 }
6304
6305 hci_req_init(&req, hdev);
6306
Johan Hedbergf2252572015-11-18 12:49:20 +02006307 err = __hci_req_schedule_adv_instance(&req, schedule_instance, true);
Arman Uguray24b4f382015-03-23 15:57:12 -07006308
Florian Grandelfffd38b2015-06-18 03:16:47 +02006309 if (!err)
6310 err = hci_req_run(&req, add_advertising_complete);
6311
Arman Uguray24b4f382015-03-23 15:57:12 -07006312 if (err < 0)
6313 mgmt_pending_remove(cmd);
6314
6315unlock:
6316 hci_dev_unlock(hdev);
6317
6318 return err;
6319}
6320
Arman Ugurayda9293352015-03-23 15:57:13 -07006321static void remove_advertising_complete(struct hci_dev *hdev, u8 status,
6322 u16 opcode)
6323{
6324 struct mgmt_pending_cmd *cmd;
Florian Grandel01948332015-06-18 03:16:48 +02006325 struct mgmt_cp_remove_advertising *cp;
Arman Ugurayda9293352015-03-23 15:57:13 -07006326 struct mgmt_rp_remove_advertising rp;
6327
6328 BT_DBG("status %d", status);
6329
6330 hci_dev_lock(hdev);
6331
6332 /* A failure status here only means that we failed to disable
6333 * advertising. Otherwise, the advertising instance has been removed,
6334 * so report success.
6335 */
6336 cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev);
6337 if (!cmd)
6338 goto unlock;
6339
Florian Grandel01948332015-06-18 03:16:48 +02006340 cp = cmd->param;
6341 rp.instance = cp->instance;
Arman Ugurayda9293352015-03-23 15:57:13 -07006342
6343 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS,
6344 &rp, sizeof(rp));
6345 mgmt_pending_remove(cmd);
6346
6347unlock:
6348 hci_dev_unlock(hdev);
6349}
6350
6351static int remove_advertising(struct sock *sk, struct hci_dev *hdev,
6352 void *data, u16 data_len)
6353{
6354 struct mgmt_cp_remove_advertising *cp = data;
6355 struct mgmt_rp_remove_advertising rp;
Arman Ugurayda9293352015-03-23 15:57:13 -07006356 struct mgmt_pending_cmd *cmd;
6357 struct hci_request req;
Johan Hedberg952497b2015-06-18 21:05:31 +03006358 int err;
Arman Ugurayda9293352015-03-23 15:57:13 -07006359
6360 BT_DBG("%s", hdev->name);
6361
Arman Ugurayda9293352015-03-23 15:57:13 -07006362 hci_dev_lock(hdev);
6363
Johan Hedberg952497b2015-06-18 21:05:31 +03006364 if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) {
Florian Grandel01948332015-06-18 03:16:48 +02006365 err = mgmt_cmd_status(sk, hdev->id,
6366 MGMT_OP_REMOVE_ADVERTISING,
6367 MGMT_STATUS_INVALID_PARAMS);
6368 goto unlock;
6369 }
6370
Arman Ugurayda9293352015-03-23 15:57:13 -07006371 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
6372 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
6373 pending_find(MGMT_OP_SET_LE, hdev)) {
6374 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
6375 MGMT_STATUS_BUSY);
6376 goto unlock;
6377 }
6378
Johan Hedberg17fd08f2015-11-26 12:15:59 +02006379 if (list_empty(&hdev->adv_instances)) {
Arman Ugurayda9293352015-03-23 15:57:13 -07006380 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
6381 MGMT_STATUS_INVALID_PARAMS);
6382 goto unlock;
6383 }
6384
Florian Grandel01948332015-06-18 03:16:48 +02006385 hci_req_init(&req, hdev);
Arman Uguray912098a2015-03-23 15:57:15 -07006386
Johan Hedberg37d3a1f2016-08-28 20:53:34 +03006387 hci_req_clear_adv_instance(hdev, sk, &req, cp->instance, true);
Arman Ugurayda9293352015-03-23 15:57:13 -07006388
Florian Grandel01948332015-06-18 03:16:48 +02006389 if (list_empty(&hdev->adv_instances))
Johan Hedbergf2252572015-11-18 12:49:20 +02006390 __hci_req_disable_advertising(&req);
Arman Ugurayda9293352015-03-23 15:57:13 -07006391
Florian Grandel01948332015-06-18 03:16:48 +02006392 /* If no HCI commands have been collected so far or the HCI_ADVERTISING
6393 * flag is set or the device isn't powered then we have no HCI
6394 * communication to make. Simply return.
Arman Ugurayda9293352015-03-23 15:57:13 -07006395 */
Florian Grandel01948332015-06-18 03:16:48 +02006396 if (skb_queue_empty(&req.cmd_q) ||
6397 !hdev_is_powered(hdev) ||
Arman Ugurayda9293352015-03-23 15:57:13 -07006398 hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
Florian Grandel01948332015-06-18 03:16:48 +02006399 rp.instance = cp->instance;
Arman Ugurayda9293352015-03-23 15:57:13 -07006400 err = mgmt_cmd_complete(sk, hdev->id,
6401 MGMT_OP_REMOVE_ADVERTISING,
6402 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
6403 goto unlock;
6404 }
6405
6406 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data,
6407 data_len);
6408 if (!cmd) {
6409 err = -ENOMEM;
6410 goto unlock;
6411 }
6412
Arman Ugurayda9293352015-03-23 15:57:13 -07006413 err = hci_req_run(&req, remove_advertising_complete);
6414 if (err < 0)
6415 mgmt_pending_remove(cmd);
6416
6417unlock:
6418 hci_dev_unlock(hdev);
6419
6420 return err;
6421}
6422
Marcel Holtmann40b25fe2015-11-19 16:16:43 +01006423static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev,
6424 void *data, u16 data_len)
6425{
6426 struct mgmt_cp_get_adv_size_info *cp = data;
6427 struct mgmt_rp_get_adv_size_info rp;
6428 u32 flags, supported_flags;
6429 int err;
6430
6431 BT_DBG("%s", hdev->name);
6432
6433 if (!lmp_le_capable(hdev))
6434 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
6435 MGMT_STATUS_REJECTED);
6436
6437 if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES)
6438 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
6439 MGMT_STATUS_INVALID_PARAMS);
6440
6441 flags = __le32_to_cpu(cp->flags);
6442
6443 /* The current implementation only supports a subset of the specified
6444 * flags.
6445 */
6446 supported_flags = get_supported_adv_flags(hdev);
6447 if (flags & ~supported_flags)
6448 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
6449 MGMT_STATUS_INVALID_PARAMS);
6450
6451 rp.instance = cp->instance;
6452 rp.flags = cp->flags;
6453 rp.max_adv_data_len = tlv_data_max_len(flags, true);
6454 rp.max_scan_rsp_len = tlv_data_max_len(flags, false);
6455
6456 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
6457 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
6458
6459 return err;
6460}
6461
Johan Hedberg6d785aa32015-03-06 21:08:51 +02006462static const struct hci_mgmt_handler mgmt_handlers[] = {
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02006463 { NULL }, /* 0x0000 (no command) */
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006464 { read_version, MGMT_READ_VERSION_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006465 HCI_MGMT_NO_HDEV |
6466 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006467 { read_commands, MGMT_READ_COMMANDS_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006468 HCI_MGMT_NO_HDEV |
6469 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006470 { read_index_list, MGMT_READ_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006471 HCI_MGMT_NO_HDEV |
6472 HCI_MGMT_UNTRUSTED },
6473 { read_controller_info, MGMT_READ_INFO_SIZE,
6474 HCI_MGMT_UNTRUSTED },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006475 { set_powered, MGMT_SETTING_SIZE },
6476 { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE },
6477 { set_connectable, MGMT_SETTING_SIZE },
6478 { set_fast_connectable, MGMT_SETTING_SIZE },
6479 { set_bondable, MGMT_SETTING_SIZE },
6480 { set_link_security, MGMT_SETTING_SIZE },
6481 { set_ssp, MGMT_SETTING_SIZE },
6482 { set_hs, MGMT_SETTING_SIZE },
6483 { set_le, MGMT_SETTING_SIZE },
6484 { set_dev_class, MGMT_SET_DEV_CLASS_SIZE },
6485 { set_local_name, MGMT_SET_LOCAL_NAME_SIZE },
6486 { add_uuid, MGMT_ADD_UUID_SIZE },
6487 { remove_uuid, MGMT_REMOVE_UUID_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006488 { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE,
6489 HCI_MGMT_VAR_LEN },
6490 { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE,
6491 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006492 { disconnect, MGMT_DISCONNECT_SIZE },
6493 { get_connections, MGMT_GET_CONNECTIONS_SIZE },
6494 { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE },
6495 { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE },
6496 { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE },
6497 { pair_device, MGMT_PAIR_DEVICE_SIZE },
6498 { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE },
6499 { unpair_device, MGMT_UNPAIR_DEVICE_SIZE },
6500 { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE },
6501 { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
6502 { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE },
6503 { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006504 { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE },
6505 { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE,
6506 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006507 { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
6508 { start_discovery, MGMT_START_DISCOVERY_SIZE },
6509 { stop_discovery, MGMT_STOP_DISCOVERY_SIZE },
6510 { confirm_name, MGMT_CONFIRM_NAME_SIZE },
6511 { block_device, MGMT_BLOCK_DEVICE_SIZE },
6512 { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE },
6513 { set_device_id, MGMT_SET_DEVICE_ID_SIZE },
6514 { set_advertising, MGMT_SETTING_SIZE },
6515 { set_bredr, MGMT_SETTING_SIZE },
6516 { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE },
6517 { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE },
6518 { set_secure_conn, MGMT_SETTING_SIZE },
6519 { set_debug_keys, MGMT_SETTING_SIZE },
6520 { set_privacy, MGMT_SET_PRIVACY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006521 { load_irks, MGMT_LOAD_IRKS_SIZE,
6522 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006523 { get_conn_info, MGMT_GET_CONN_INFO_SIZE },
6524 { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE },
6525 { add_device, MGMT_ADD_DEVICE_SIZE },
6526 { remove_device, MGMT_REMOVE_DEVICE_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006527 { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE,
6528 HCI_MGMT_VAR_LEN },
6529 { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006530 HCI_MGMT_NO_HDEV |
6531 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006532 { read_config_info, MGMT_READ_CONFIG_INFO_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006533 HCI_MGMT_UNCONFIGURED |
6534 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006535 { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE,
6536 HCI_MGMT_UNCONFIGURED },
6537 { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE,
6538 HCI_MGMT_UNCONFIGURED },
6539 { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE,
6540 HCI_MGMT_VAR_LEN },
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006541 { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE },
Marcel Holtmann96f14742015-03-14 19:27:57 -07006542 { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006543 HCI_MGMT_NO_HDEV |
6544 HCI_MGMT_UNTRUSTED },
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006545 { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE },
Arman Uguray24b4f382015-03-23 15:57:12 -07006546 { add_advertising, MGMT_ADD_ADVERTISING_SIZE,
6547 HCI_MGMT_VAR_LEN },
Arman Ugurayda9293352015-03-23 15:57:13 -07006548 { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE },
Marcel Holtmann40b25fe2015-11-19 16:16:43 +01006549 { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE },
Johan Hedberg78b781c2016-01-05 13:19:32 +02006550 { start_limited_discovery, MGMT_START_DISCOVERY_SIZE },
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02006551 { read_ext_controller_info,MGMT_READ_EXT_INFO_SIZE,
6552 HCI_MGMT_UNTRUSTED },
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02006553 { set_appearance, MGMT_SET_APPEARANCE_SIZE },
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02006554};
6555
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07006556void mgmt_index_added(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02006557{
Marcel Holtmannced85542015-03-14 19:27:56 -07006558 struct mgmt_ev_ext_index ev;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +03006559
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02006560 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
6561 return;
6562
Marcel Holtmannf9207332015-03-14 19:27:55 -07006563 switch (hdev->dev_type) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +02006564 case HCI_PRIMARY:
Marcel Holtmannf9207332015-03-14 19:27:55 -07006565 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
6566 mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev,
6567 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07006568 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006569 } else {
6570 mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0,
6571 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07006572 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006573 }
6574 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07006575 case HCI_AMP:
6576 ev.type = 0x02;
6577 break;
6578 default:
6579 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006580 }
Marcel Holtmannced85542015-03-14 19:27:56 -07006581
6582 ev.bus = hdev->bus;
6583
6584 mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev),
6585 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergc71e97b2010-12-13 21:07:07 +02006586}
6587
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07006588void mgmt_index_removed(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02006589{
Marcel Holtmannced85542015-03-14 19:27:56 -07006590 struct mgmt_ev_ext_index ev;
Johan Hedberg5f159032012-03-02 03:13:19 +02006591 u8 status = MGMT_STATUS_INVALID_INDEX;
Johan Hedbergb24752f2011-11-03 14:40:33 +02006592
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02006593 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
6594 return;
6595
Marcel Holtmannf9207332015-03-14 19:27:55 -07006596 switch (hdev->dev_type) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +02006597 case HCI_PRIMARY:
Marcel Holtmannf9207332015-03-14 19:27:55 -07006598 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedbergb24752f2011-11-03 14:40:33 +02006599
Marcel Holtmannf9207332015-03-14 19:27:55 -07006600 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
6601 mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev,
6602 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07006603 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006604 } else {
6605 mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0,
6606 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07006607 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006608 }
6609 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07006610 case HCI_AMP:
6611 ev.type = 0x02;
6612 break;
6613 default:
6614 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006615 }
Marcel Holtmannced85542015-03-14 19:27:56 -07006616
6617 ev.bus = hdev->bus;
6618
6619 mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev),
6620 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02006621}
6622
Andre Guedes6046dc32014-02-26 20:21:51 -03006623/* This function requires the caller holds hdev->lock */
Johan Hedbergaf02dd42015-11-11 08:11:21 +02006624static void restart_le_actions(struct hci_dev *hdev)
Andre Guedes6046dc32014-02-26 20:21:51 -03006625{
6626 struct hci_conn_params *p;
6627
6628 list_for_each_entry(p, &hdev->le_conn_params, list) {
Johan Hedbergd7347f32014-07-04 12:37:23 +03006629 /* Needed for AUTO_OFF case where might not "really"
6630 * have been powered off.
6631 */
6632 list_del_init(&p->action);
6633
6634 switch (p->auto_connect) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006635 case HCI_AUTO_CONN_DIRECT:
Johan Hedbergd7347f32014-07-04 12:37:23 +03006636 case HCI_AUTO_CONN_ALWAYS:
6637 list_add(&p->action, &hdev->pend_le_conns);
6638 break;
6639 case HCI_AUTO_CONN_REPORT:
6640 list_add(&p->action, &hdev->pend_le_reports);
6641 break;
6642 default:
6643 break;
Marcel Holtmannc83ed192014-07-01 19:28:24 +02006644 }
Andre Guedes6046dc32014-02-26 20:21:51 -03006645 }
6646}
6647
Johan Hedberg2ff13892015-11-25 16:15:44 +02006648void mgmt_power_on(struct hci_dev *hdev, int err)
Johan Hedberg229ab392013-03-15 17:06:53 -05006649{
6650 struct cmd_lookup match = { NULL, hdev };
6651
Johan Hedberg2ff13892015-11-25 16:15:44 +02006652 BT_DBG("err %d", err);
Johan Hedberg229ab392013-03-15 17:06:53 -05006653
Johan Hedberg2ff13892015-11-25 16:15:44 +02006654 hci_dev_lock(hdev);
6655
6656 if (!err) {
Johan Hedbergaf02dd42015-11-11 08:11:21 +02006657 restart_le_actions(hdev);
6658 hci_update_background_scan(hdev);
Marcel Holtmann162a3ba2015-01-14 15:43:11 -08006659 }
6660
Johan Hedberg229ab392013-03-15 17:06:53 -05006661 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
6662
6663 new_settings(hdev, match.sk);
6664
Johan Hedberg229ab392013-03-15 17:06:53 -05006665 if (match.sk)
6666 sock_put(match.sk);
Johan Hedberg2ff13892015-11-25 16:15:44 +02006667
6668 hci_dev_unlock(hdev);
Johan Hedberg229ab392013-03-15 17:06:53 -05006669}
6670
Johan Hedberg2ff13892015-11-25 16:15:44 +02006671void __mgmt_power_off(struct hci_dev *hdev)
Johan Hedberg5add6af2010-12-16 10:00:37 +02006672{
Johan Hedberg76a7f3a2012-02-17 00:34:40 +02006673 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg98459042014-12-12 11:15:21 +02006674 u8 status, zero_cod[] = { 0, 0, 0 };
Johan Hedbergb24752f2011-11-03 14:40:33 +02006675
Johan Hedberg229ab392013-03-15 17:06:53 -05006676 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
Johan Hedberg98459042014-12-12 11:15:21 +02006677
6678 /* If the power off is because of hdev unregistration let
6679 * use the appropriate INVALID_INDEX status. Otherwise use
6680 * NOT_POWERED. We cover both scenarios here since later in
6681 * mgmt_index_removed() any hci_conn callbacks will have already
6682 * been triggered, potentially causing misleading DISCONNECTED
6683 * status responses.
6684 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006685 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
Johan Hedberg98459042014-12-12 11:15:21 +02006686 status = MGMT_STATUS_INVALID_INDEX;
6687 else
6688 status = MGMT_STATUS_NOT_POWERED;
6689
6690 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedberg229ab392013-03-15 17:06:53 -05006691
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02006692 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) {
Marcel Holtmann5504c3a2016-08-29 06:19:46 +02006693 mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
6694 zero_cod, sizeof(zero_cod),
6695 HCI_MGMT_DEV_CLASS_EVENTS, NULL);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02006696 ext_info_changed(hdev, NULL);
6697 }
Johan Hedberg229ab392013-03-15 17:06:53 -05006698
Johan Hedberg2ff13892015-11-25 16:15:44 +02006699 new_settings(hdev, match.sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02006700
6701 if (match.sk)
6702 sock_put(match.sk);
Johan Hedberg5add6af2010-12-16 10:00:37 +02006703}
Johan Hedberg73f22f62010-12-29 16:00:25 +02006704
Marcel Holtmann3eec7052013-10-06 23:55:46 -07006705void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
Johan Hedberg96570ff2013-05-29 09:51:29 +03006706{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006707 struct mgmt_pending_cmd *cmd;
Johan Hedberg96570ff2013-05-29 09:51:29 +03006708 u8 status;
6709
Johan Hedberg333ae952015-03-17 13:48:47 +02006710 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg96570ff2013-05-29 09:51:29 +03006711 if (!cmd)
Marcel Holtmann3eec7052013-10-06 23:55:46 -07006712 return;
Johan Hedberg96570ff2013-05-29 09:51:29 +03006713
6714 if (err == -ERFKILL)
6715 status = MGMT_STATUS_RFKILLED;
6716 else
6717 status = MGMT_STATUS_FAILED;
6718
Johan Hedberga69e8372015-03-06 21:08:53 +02006719 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
Johan Hedberg96570ff2013-05-29 09:51:29 +03006720
6721 mgmt_pending_remove(cmd);
Johan Hedberg96570ff2013-05-29 09:51:29 +03006722}
6723
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07006724void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
6725 bool persistent)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006726{
Johan Hedberg86742e12011-11-07 23:13:38 +02006727 struct mgmt_ev_new_link_key ev;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006728
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006729 memset(&ev, 0, sizeof(ev));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006730
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006731 ev.store_hint = persistent;
Johan Hedbergd753fdc2012-02-17 14:06:34 +02006732 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03006733 ev.key.addr.type = BDADDR_BREDR;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006734 ev.key.type = key->type;
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03006735 memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006736 ev.key.pin_len = key->pin_len;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006737
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07006738 mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006739}
Johan Hedbergf7520542011-01-20 12:34:39 +02006740
Johan Hedbergd7b25452014-05-23 13:19:53 +03006741static u8 mgmt_ltk_type(struct smp_ltk *ltk)
6742{
Johan Hedberg23fb8de2014-05-23 13:15:37 +03006743 switch (ltk->type) {
6744 case SMP_LTK:
6745 case SMP_LTK_SLAVE:
6746 if (ltk->authenticated)
6747 return MGMT_LTK_AUTHENTICATED;
6748 return MGMT_LTK_UNAUTHENTICATED;
6749 case SMP_LTK_P256:
6750 if (ltk->authenticated)
6751 return MGMT_LTK_P256_AUTH;
6752 return MGMT_LTK_P256_UNAUTH;
6753 case SMP_LTK_P256_DEBUG:
6754 return MGMT_LTK_P256_DEBUG;
6755 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03006756
6757 return MGMT_LTK_UNAUTHENTICATED;
6758}
6759
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07006760void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006761{
6762 struct mgmt_ev_new_long_term_key ev;
6763
6764 memset(&ev, 0, sizeof(ev));
6765
Marcel Holtmann5192d302014-02-19 17:11:58 -08006766 /* Devices using resolvable or non-resolvable random addresses
Florian Grandelf72186d2015-05-26 03:31:09 +02006767 * without providing an identity resolving key don't require
Marcel Holtmann5192d302014-02-19 17:11:58 -08006768 * to store long term keys. Their addresses will change the
6769 * next time around.
6770 *
6771 * Only when a remote device provides an identity address
6772 * make sure the long term key is stored. If the remote
6773 * identity is known, the long term keys are internally
6774 * mapped to the identity address. So allow static random
6775 * and public addresses here.
6776 */
Johan Hedbergba74b662014-02-19 14:57:45 +02006777 if (key->bdaddr_type == ADDR_LE_DEV_RANDOM &&
6778 (key->bdaddr.b[5] & 0xc0) != 0xc0)
6779 ev.store_hint = 0x00;
6780 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07006781 ev.store_hint = persistent;
Johan Hedbergba74b662014-02-19 14:57:45 +02006782
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006783 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03006784 ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
Johan Hedbergd7b25452014-05-23 13:19:53 +03006785 ev.key.type = mgmt_ltk_type(key);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006786 ev.key.enc_size = key->enc_size;
6787 ev.key.ediv = key->ediv;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08006788 ev.key.rand = key->rand;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006789
Johan Hedberg2ceba532014-06-16 19:25:16 +03006790 if (key->type == SMP_LTK)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006791 ev.key.master = 1;
6792
Johan Hedberg1fc62c52015-06-10 11:11:20 +03006793 /* Make sure we copy only the significant bytes based on the
6794 * encryption key size, and set the rest of the value to zeroes.
6795 */
Jakub Pawlowskicb922052015-08-05 23:16:29 +02006796 memcpy(ev.key.val, key->val, key->enc_size);
Johan Hedberg1fc62c52015-06-10 11:11:20 +03006797 memset(ev.key.val + key->enc_size, 0,
6798 sizeof(ev.key.val) - key->enc_size);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006799
Marcel Holtmann083368f2013-10-15 14:26:29 -07006800 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006801}
6802
Johan Hedbergcad20c22015-10-12 13:36:19 +02006803void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent)
Johan Hedberg95fbac82014-02-19 15:18:31 +02006804{
6805 struct mgmt_ev_new_irk ev;
6806
6807 memset(&ev, 0, sizeof(ev));
6808
Johan Hedbergcad20c22015-10-12 13:36:19 +02006809 ev.store_hint = persistent;
Marcel Holtmannbab6d1e2014-02-19 11:51:54 -08006810
Johan Hedberg95fbac82014-02-19 15:18:31 +02006811 bacpy(&ev.rpa, &irk->rpa);
6812 bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr);
6813 ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type);
6814 memcpy(ev.irk.val, irk->val, sizeof(irk->val));
6815
6816 mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
6817}
6818
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07006819void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
6820 bool persistent)
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07006821{
6822 struct mgmt_ev_new_csrk ev;
6823
6824 memset(&ev, 0, sizeof(ev));
6825
6826 /* Devices using resolvable or non-resolvable random addresses
Florian Grandelf72186d2015-05-26 03:31:09 +02006827 * without providing an identity resolving key don't require
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07006828 * to store signature resolving keys. Their addresses will change
6829 * the next time around.
6830 *
6831 * Only when a remote device provides an identity address
6832 * make sure the signature resolving key is stored. So allow
6833 * static random and public addresses here.
6834 */
6835 if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM &&
6836 (csrk->bdaddr.b[5] & 0xc0) != 0xc0)
6837 ev.store_hint = 0x00;
6838 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07006839 ev.store_hint = persistent;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07006840
6841 bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
6842 ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
Johan Hedberg4cd39282015-02-27 10:11:13 +02006843 ev.key.type = csrk->type;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07006844 memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
6845
6846 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
6847}
6848
Andre Guedesffb5a8272014-07-01 18:10:11 -03006849void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedbergf4869e22014-07-02 17:37:32 +03006850 u8 bdaddr_type, u8 store_hint, u16 min_interval,
6851 u16 max_interval, u16 latency, u16 timeout)
Andre Guedesffb5a8272014-07-01 18:10:11 -03006852{
6853 struct mgmt_ev_new_conn_param ev;
6854
Johan Hedbergc103aea2014-07-02 17:37:34 +03006855 if (!hci_is_identity_address(bdaddr, bdaddr_type))
6856 return;
6857
Andre Guedesffb5a8272014-07-01 18:10:11 -03006858 memset(&ev, 0, sizeof(ev));
6859 bacpy(&ev.addr.bdaddr, bdaddr);
6860 ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type);
Johan Hedbergf4869e22014-07-02 17:37:32 +03006861 ev.store_hint = store_hint;
Andre Guedesffb5a8272014-07-01 18:10:11 -03006862 ev.min_interval = cpu_to_le16(min_interval);
6863 ev.max_interval = cpu_to_le16(max_interval);
6864 ev.latency = cpu_to_le16(latency);
6865 ev.timeout = cpu_to_le16(timeout);
6866
6867 mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL);
6868}
6869
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00006870void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
6871 u32 flags, u8 *name, u8 name_len)
Johan Hedbergf7520542011-01-20 12:34:39 +02006872{
Johan Hedbergb644ba32012-01-17 21:48:47 +02006873 char buf[512];
6874 struct mgmt_ev_device_connected *ev = (void *) buf;
6875 u16 eir_len = 0;
Johan Hedbergf7520542011-01-20 12:34:39 +02006876
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00006877 bacpy(&ev->addr.bdaddr, &conn->dst);
6878 ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedbergf7520542011-01-20 12:34:39 +02006879
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02006880 ev->flags = __cpu_to_le32(flags);
Johan Hedberg08c79b62012-02-23 22:31:51 +02006881
Alfonso Acostafd45ada2014-10-07 08:44:11 +00006882 /* We must ensure that the EIR Data fields are ordered and
6883 * unique. Keep it simple for now and avoid the problem by not
6884 * adding any BR/EDR data to the LE adv.
6885 */
6886 if (conn->le_adv_data_len > 0) {
6887 memcpy(&ev->eir[eir_len],
6888 conn->le_adv_data, conn->le_adv_data_len);
6889 eir_len = conn->le_adv_data_len;
6890 } else {
6891 if (name_len > 0)
6892 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE,
6893 name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02006894
Alfonso Acostaddbea5c2014-10-07 08:44:12 +00006895 if (memcmp(conn->dev_class, "\0\0\0", 3) != 0)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00006896 eir_len = eir_append_data(ev->eir, eir_len,
6897 EIR_CLASS_OF_DEV,
6898 conn->dev_class, 3);
6899 }
Johan Hedbergb644ba32012-01-17 21:48:47 +02006900
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02006901 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02006902
Marcel Holtmannecd90ae2013-10-06 23:55:49 -07006903 mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf,
6904 sizeof(*ev) + eir_len, NULL);
Johan Hedbergf7520542011-01-20 12:34:39 +02006905}
6906
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006907static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg8962ee72011-01-20 12:40:27 +02006908{
Johan Hedberg8962ee72011-01-20 12:40:27 +02006909 struct sock **sk = data;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006910
Johan Hedbergf5818c22014-12-05 13:36:02 +02006911 cmd->cmd_complete(cmd, 0);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006912
6913 *sk = cmd->sk;
6914 sock_hold(*sk);
6915
Johan Hedberga664b5b2011-02-19 12:06:02 -03006916 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006917}
6918
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006919static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberga8a1d192011-11-10 15:54:38 +02006920{
Johan Hedbergb1078ad2012-02-09 17:21:16 +02006921 struct hci_dev *hdev = data;
Johan Hedberg124f6e32012-02-09 13:50:12 +02006922 struct mgmt_cp_unpair_device *cp = cmd->param;
Johan Hedberga8a1d192011-11-10 15:54:38 +02006923
Johan Hedbergb1078ad2012-02-09 17:21:16 +02006924 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
6925
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02006926 cmd->cmd_complete(cmd, 0);
Johan Hedberga8a1d192011-11-10 15:54:38 +02006927 mgmt_pending_remove(cmd);
6928}
6929
Johan Hedberg84c61d92014-08-01 11:13:30 +03006930bool mgmt_powering_down(struct hci_dev *hdev)
6931{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006932 struct mgmt_pending_cmd *cmd;
Johan Hedberg84c61d92014-08-01 11:13:30 +03006933 struct mgmt_mode *cp;
6934
Johan Hedberg333ae952015-03-17 13:48:47 +02006935 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg84c61d92014-08-01 11:13:30 +03006936 if (!cmd)
6937 return false;
6938
6939 cp = cmd->param;
6940 if (!cp->val)
6941 return true;
6942
6943 return false;
6944}
6945
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07006946void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02006947 u8 link_type, u8 addr_type, u8 reason,
6948 bool mgmt_connected)
Johan Hedbergf7520542011-01-20 12:34:39 +02006949{
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02006950 struct mgmt_ev_device_disconnected ev;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006951 struct sock *sk = NULL;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006952
Johan Hedberg84c61d92014-08-01 11:13:30 +03006953 /* The connection is still in hci_conn_hash so test for 1
6954 * instead of 0 to know if this is the last one.
6955 */
6956 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
6957 cancel_delayed_work(&hdev->power_off);
6958 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberg8b064a32014-02-24 14:52:22 +02006959 }
6960
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02006961 if (!mgmt_connected)
6962 return;
6963
Andre Guedes57eb7762013-10-30 19:01:41 -03006964 if (link_type != ACL_LINK && link_type != LE_LINK)
6965 return;
6966
Johan Hedberg744cf192011-11-08 20:40:14 +02006967 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
Johan Hedbergf7520542011-01-20 12:34:39 +02006968
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02006969 bacpy(&ev.addr.bdaddr, bdaddr);
6970 ev.addr.type = link_to_bdaddr(link_type, addr_type);
6971 ev.reason = reason;
Johan Hedbergf7520542011-01-20 12:34:39 +02006972
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07006973 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006974
6975 if (sk)
Szymon Jancd97dcb62012-03-16 16:02:56 +01006976 sock_put(sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006977
Johan Hedberg124f6e32012-02-09 13:50:12 +02006978 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006979 hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006980}
6981
Marcel Holtmann78929242013-10-06 23:55:47 -07006982void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
6983 u8 link_type, u8 addr_type, u8 status)
Johan Hedberg8962ee72011-01-20 12:40:27 +02006984{
Andre Guedes3655bba2013-10-30 19:01:40 -03006985 u8 bdaddr_type = link_to_bdaddr(link_type, addr_type);
6986 struct mgmt_cp_disconnect *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006987 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006988
Jefferson Delfes36a75f12012-09-18 13:36:54 -04006989 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
6990 hdev);
6991
Johan Hedberg333ae952015-03-17 13:48:47 +02006992 cmd = pending_find(MGMT_OP_DISCONNECT, hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006993 if (!cmd)
Marcel Holtmann78929242013-10-06 23:55:47 -07006994 return;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006995
Andre Guedes3655bba2013-10-30 19:01:40 -03006996 cp = cmd->param;
6997
6998 if (bacmp(bdaddr, &cp->addr.bdaddr))
6999 return;
7000
7001 if (cp->addr.type != bdaddr_type)
7002 return;
7003
Johan Hedbergf5818c22014-12-05 13:36:02 +02007004 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007005 mgmt_pending_remove(cmd);
Johan Hedbergf7520542011-01-20 12:34:39 +02007006}
Johan Hedberg17d5c042011-01-22 06:09:08 +02007007
Marcel Holtmann445608d2013-10-06 23:55:48 -07007008void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
7009 u8 addr_type, u8 status)
Johan Hedberg17d5c042011-01-22 06:09:08 +02007010{
7011 struct mgmt_ev_connect_failed ev;
Johan Hedbergc9910d02014-02-27 14:35:12 +02007012
Johan Hedberg84c61d92014-08-01 11:13:30 +03007013 /* The connection is still in hci_conn_hash so test for 1
7014 * instead of 0 to know if this is the last one.
7015 */
7016 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
7017 cancel_delayed_work(&hdev->power_off);
7018 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedbergc9910d02014-02-27 14:35:12 +02007019 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02007020
Johan Hedberg4c659c32011-11-07 23:13:39 +02007021 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007022 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergca69b792011-11-11 18:10:00 +02007023 ev.status = mgmt_status(status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02007024
Marcel Holtmann445608d2013-10-06 23:55:48 -07007025 mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg17d5c042011-01-22 06:09:08 +02007026}
Johan Hedberg980e1a52011-01-22 06:10:07 +02007027
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07007028void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007029{
7030 struct mgmt_ev_pin_code_request ev;
7031
Johan Hedbergd8457692012-02-17 14:24:57 +02007032 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03007033 ev.addr.type = BDADDR_BREDR;
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02007034 ev.secure = secure;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007035
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07007036 mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007037}
7038
Marcel Holtmanne669cf82013-10-15 14:26:21 -07007039void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
7040 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007041{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007042 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007043
Johan Hedberg333ae952015-03-17 13:48:47 +02007044 cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007045 if (!cmd)
Marcel Holtmanne669cf82013-10-15 14:26:21 -07007046 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007047
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007048 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007049 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007050}
7051
Marcel Holtmann3eb38522013-10-15 14:26:22 -07007052void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
7053 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007054{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007055 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007056
Johan Hedberg333ae952015-03-17 13:48:47 +02007057 cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007058 if (!cmd)
Marcel Holtmann3eb38522013-10-15 14:26:22 -07007059 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007060
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007061 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007062 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007063}
Johan Hedberga5c29682011-02-19 12:05:57 -03007064
Johan Hedberg744cf192011-11-08 20:40:14 +02007065int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg39adbff2014-03-20 08:18:14 +02007066 u8 link_type, u8 addr_type, u32 value,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007067 u8 confirm_hint)
Johan Hedberga5c29682011-02-19 12:05:57 -03007068{
7069 struct mgmt_ev_user_confirm_request ev;
7070
Johan Hedberg744cf192011-11-08 20:40:14 +02007071 BT_DBG("%s", hdev->name);
Johan Hedberga5c29682011-02-19 12:05:57 -03007072
Johan Hedberg272d90d2012-02-09 15:26:12 +02007073 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007074 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberg55bc1a32011-04-28 11:28:56 -07007075 ev.confirm_hint = confirm_hint;
Johan Hedberg39adbff2014-03-20 08:18:14 +02007076 ev.value = cpu_to_le32(value);
Johan Hedberga5c29682011-02-19 12:05:57 -03007077
Johan Hedberg744cf192011-11-08 20:40:14 +02007078 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007079 NULL);
Johan Hedberga5c29682011-02-19 12:05:57 -03007080}
7081
Johan Hedberg272d90d2012-02-09 15:26:12 +02007082int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03007083 u8 link_type, u8 addr_type)
Brian Gix604086b2011-11-23 08:28:33 -08007084{
7085 struct mgmt_ev_user_passkey_request ev;
7086
7087 BT_DBG("%s", hdev->name);
7088
Johan Hedberg272d90d2012-02-09 15:26:12 +02007089 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007090 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Brian Gix604086b2011-11-23 08:28:33 -08007091
7092 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007093 NULL);
Brian Gix604086b2011-11-23 08:28:33 -08007094}
7095
Brian Gix0df4c182011-11-16 13:53:13 -08007096static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03007097 u8 link_type, u8 addr_type, u8 status,
7098 u8 opcode)
Johan Hedberga5c29682011-02-19 12:05:57 -03007099{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007100 struct mgmt_pending_cmd *cmd;
Johan Hedberga5c29682011-02-19 12:05:57 -03007101
Johan Hedberg333ae952015-03-17 13:48:47 +02007102 cmd = pending_find(opcode, hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03007103 if (!cmd)
7104 return -ENOENT;
7105
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007106 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007107 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03007108
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007109 return 0;
Johan Hedberga5c29682011-02-19 12:05:57 -03007110}
7111
Johan Hedberg744cf192011-11-08 20:40:14 +02007112int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007113 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03007114{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007115 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007116 status, MGMT_OP_USER_CONFIRM_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03007117}
7118
Johan Hedberg272d90d2012-02-09 15:26:12 +02007119int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007120 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03007121{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007122 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03007123 status,
7124 MGMT_OP_USER_CONFIRM_NEG_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03007125}
Johan Hedberg2a611692011-02-19 12:06:00 -03007126
Brian Gix604086b2011-11-23 08:28:33 -08007127int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007128 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08007129{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007130 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007131 status, MGMT_OP_USER_PASSKEY_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08007132}
7133
Johan Hedberg272d90d2012-02-09 15:26:12 +02007134int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007135 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08007136{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007137 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03007138 status,
7139 MGMT_OP_USER_PASSKEY_NEG_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08007140}
7141
Johan Hedberg92a25252012-09-06 18:39:26 +03007142int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
7143 u8 link_type, u8 addr_type, u32 passkey,
7144 u8 entered)
7145{
7146 struct mgmt_ev_passkey_notify ev;
7147
7148 BT_DBG("%s", hdev->name);
7149
7150 bacpy(&ev.addr.bdaddr, bdaddr);
7151 ev.addr.type = link_to_bdaddr(link_type, addr_type);
7152 ev.passkey = __cpu_to_le32(passkey);
7153 ev.entered = entered;
7154
7155 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
7156}
7157
Johan Hedberge1e930f2014-09-08 17:09:49 -07007158void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status)
Johan Hedberg2a611692011-02-19 12:06:00 -03007159{
7160 struct mgmt_ev_auth_failed ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007161 struct mgmt_pending_cmd *cmd;
Johan Hedberge1e930f2014-09-08 17:09:49 -07007162 u8 status = mgmt_status(hci_status);
Johan Hedberg2a611692011-02-19 12:06:00 -03007163
Johan Hedberge1e930f2014-09-08 17:09:49 -07007164 bacpy(&ev.addr.bdaddr, &conn->dst);
7165 ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
7166 ev.status = status;
Johan Hedberg2a611692011-02-19 12:06:00 -03007167
Johan Hedberge1e930f2014-09-08 17:09:49 -07007168 cmd = find_pairing(conn);
7169
7170 mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev),
7171 cmd ? cmd->sk : NULL);
7172
Johan Hedberga511b352014-12-11 21:45:45 +02007173 if (cmd) {
7174 cmd->cmd_complete(cmd, status);
7175 mgmt_pending_remove(cmd);
7176 }
Johan Hedberg2a611692011-02-19 12:06:00 -03007177}
Johan Hedbergb312b1612011-03-16 14:29:37 +02007178
Marcel Holtmann464996a2013-10-15 14:26:24 -07007179void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007180{
7181 struct cmd_lookup match = { NULL, hdev };
Marcel Holtmann464996a2013-10-15 14:26:24 -07007182 bool changed;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007183
7184 if (status) {
7185 u8 mgmt_err = mgmt_status(status);
7186 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007187 cmd_status_rsp, &mgmt_err);
Marcel Holtmann464996a2013-10-15 14:26:24 -07007188 return;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007189 }
7190
Marcel Holtmann464996a2013-10-15 14:26:24 -07007191 if (test_bit(HCI_AUTH, &hdev->flags))
Marcel Holtmann238be782015-03-13 02:11:06 -07007192 changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY);
Marcel Holtmann464996a2013-10-15 14:26:24 -07007193 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007194 changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02007195
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007196 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007197 &match);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007198
Johan Hedberg47990ea2012-02-22 11:58:37 +02007199 if (changed)
Marcel Holtmann464996a2013-10-15 14:26:24 -07007200 new_settings(hdev, match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007201
7202 if (match.sk)
7203 sock_put(match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007204}
7205
Johan Hedberg890ea892013-03-15 17:06:52 -05007206static void clear_eir(struct hci_request *req)
Johan Hedbergcacaf522012-02-21 00:52:42 +02007207{
Johan Hedberg890ea892013-03-15 17:06:52 -05007208 struct hci_dev *hdev = req->hdev;
Johan Hedbergcacaf522012-02-21 00:52:42 +02007209 struct hci_cp_write_eir cp;
7210
Johan Hedberg976eb202012-10-24 21:12:01 +03007211 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05007212 return;
Johan Hedbergcacaf522012-02-21 00:52:42 +02007213
Johan Hedbergc80da272012-02-22 15:38:48 +02007214 memset(hdev->eir, 0, sizeof(hdev->eir));
7215
Johan Hedbergcacaf522012-02-21 00:52:42 +02007216 memset(&cp, 0, sizeof(cp));
7217
Johan Hedberg890ea892013-03-15 17:06:52 -05007218 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedbergcacaf522012-02-21 00:52:42 +02007219}
7220
Marcel Holtmann3e248562013-10-15 14:26:25 -07007221void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007222{
7223 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg890ea892013-03-15 17:06:52 -05007224 struct hci_request req;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007225 bool changed = false;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007226
7227 if (status) {
7228 u8 mgmt_err = mgmt_status(status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007229
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007230 if (enable && hci_dev_test_and_clear_flag(hdev,
7231 HCI_SSP_ENABLED)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07007232 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmann3e248562013-10-15 14:26:25 -07007233 new_settings(hdev, NULL);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007234 }
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007235
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007236 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
7237 &mgmt_err);
Marcel Holtmann3e248562013-10-15 14:26:25 -07007238 return;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007239 }
7240
7241 if (enable) {
Marcel Holtmann238be782015-03-13 02:11:06 -07007242 changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007243 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007244 changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007245 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007246 changed = hci_dev_test_and_clear_flag(hdev,
7247 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007248 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07007249 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007250 }
7251
7252 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
7253
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007254 if (changed)
Marcel Holtmann3e248562013-10-15 14:26:25 -07007255 new_settings(hdev, match.sk);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007256
Johan Hedberg5fc6ebb2012-02-22 15:10:59 +02007257 if (match.sk)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007258 sock_put(match.sk);
7259
Johan Hedberg890ea892013-03-15 17:06:52 -05007260 hci_req_init(&req, hdev);
7261
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007262 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
7263 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03007264 hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE,
7265 sizeof(enable), &enable);
Johan Hedbergb1a89172015-11-25 16:15:42 +02007266 __hci_req_update_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03007267 } else {
Johan Hedberg890ea892013-03-15 17:06:52 -05007268 clear_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03007269 }
Johan Hedberg890ea892013-03-15 17:06:52 -05007270
7271 hci_req_run(&req, NULL);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007272}
7273
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007274static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg90e70452012-02-23 23:09:40 +02007275{
7276 struct cmd_lookup *match = data;
7277
Johan Hedberg90e70452012-02-23 23:09:40 +02007278 if (match->sk == NULL) {
7279 match->sk = cmd->sk;
7280 sock_hold(match->sk);
7281 }
Johan Hedberg90e70452012-02-23 23:09:40 +02007282}
7283
Marcel Holtmann4e1b0242013-10-15 14:26:26 -07007284void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
7285 u8 status)
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007286{
Johan Hedberg90e70452012-02-23 23:09:40 +02007287 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007288
Johan Hedberg92da6092013-03-15 17:06:55 -05007289 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
7290 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
7291 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
Johan Hedberg90e70452012-02-23 23:09:40 +02007292
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02007293 if (!status) {
Marcel Holtmann5504c3a2016-08-29 06:19:46 +02007294 mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class,
7295 3, HCI_MGMT_DEV_CLASS_EVENTS, NULL);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02007296 ext_info_changed(hdev, NULL);
7297 }
Johan Hedberg90e70452012-02-23 23:09:40 +02007298
7299 if (match.sk)
7300 sock_put(match.sk);
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007301}
7302
Marcel Holtmann7667da32013-10-15 14:26:27 -07007303void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
Johan Hedbergb312b1612011-03-16 14:29:37 +02007304{
Johan Hedbergb312b1612011-03-16 14:29:37 +02007305 struct mgmt_cp_set_local_name ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007306 struct mgmt_pending_cmd *cmd;
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02007307
Johan Hedberg13928972013-03-15 17:07:00 -05007308 if (status)
Marcel Holtmann7667da32013-10-15 14:26:27 -07007309 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02007310
7311 memset(&ev, 0, sizeof(ev));
7312 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02007313 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
Johan Hedbergb312b1612011-03-16 14:29:37 +02007314
Johan Hedberg333ae952015-03-17 13:48:47 +02007315 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05007316 if (!cmd) {
7317 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
Johan Hedbergb312b1612011-03-16 14:29:37 +02007318
Johan Hedberg13928972013-03-15 17:07:00 -05007319 /* If this is a HCI command related to powering on the
7320 * HCI dev don't send any mgmt signals.
7321 */
Johan Hedberg333ae952015-03-17 13:48:47 +02007322 if (pending_find(MGMT_OP_SET_POWERED, hdev))
Marcel Holtmann7667da32013-10-15 14:26:27 -07007323 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02007324 }
7325
Marcel Holtmann5504c3a2016-08-29 06:19:46 +02007326 mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
7327 HCI_MGMT_LOCAL_NAME_EVENTS, cmd ? cmd->sk : NULL);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02007328 ext_info_changed(hdev, cmd ? cmd->sk : NULL);
Johan Hedbergb312b1612011-03-16 14:29:37 +02007329}
Szymon Jancc35938b2011-03-22 13:12:21 +01007330
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007331static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16])
7332{
7333 int i;
7334
7335 for (i = 0; i < uuid_count; i++) {
7336 if (!memcmp(uuid, uuids[i], 16))
7337 return true;
7338 }
7339
7340 return false;
7341}
7342
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007343static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16])
7344{
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007345 u16 parsed = 0;
7346
7347 while (parsed < eir_len) {
7348 u8 field_len = eir[0];
7349 u8 uuid[16];
7350 int i;
7351
7352 if (field_len == 0)
7353 break;
7354
7355 if (eir_len - parsed < field_len + 1)
7356 break;
7357
7358 switch (eir[1]) {
7359 case EIR_UUID16_ALL:
7360 case EIR_UUID16_SOME:
7361 for (i = 0; i + 3 <= field_len; i += 2) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02007362 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007363 uuid[13] = eir[i + 3];
7364 uuid[12] = eir[i + 2];
7365 if (has_uuid(uuid, uuid_count, uuids))
7366 return true;
7367 }
7368 break;
7369 case EIR_UUID32_ALL:
7370 case EIR_UUID32_SOME:
7371 for (i = 0; i + 5 <= field_len; i += 4) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02007372 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007373 uuid[15] = eir[i + 5];
7374 uuid[14] = eir[i + 4];
7375 uuid[13] = eir[i + 3];
7376 uuid[12] = eir[i + 2];
7377 if (has_uuid(uuid, uuid_count, uuids))
7378 return true;
7379 }
7380 break;
7381 case EIR_UUID128_ALL:
7382 case EIR_UUID128_SOME:
7383 for (i = 0; i + 17 <= field_len; i += 16) {
7384 memcpy(uuid, eir + i + 2, 16);
7385 if (has_uuid(uuid, uuid_count, uuids))
7386 return true;
7387 }
7388 break;
7389 }
7390
7391 parsed += field_len + 1;
7392 eir += field_len + 1;
7393 }
7394
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007395 return false;
7396}
7397
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007398static void restart_le_scan(struct hci_dev *hdev)
7399{
7400 /* If controller is not scanning we are done. */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007401 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007402 return;
7403
7404 if (time_after(jiffies + DISCOV_LE_RESTART_DELAY,
7405 hdev->discovery.scan_start +
7406 hdev->discovery.scan_duration))
7407 return;
7408
Johan Hedberg7c1fbed2015-11-11 08:11:23 +02007409 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart,
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007410 DISCOV_LE_RESTART_DELAY);
7411}
7412
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007413static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir,
7414 u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
7415{
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007416 /* If a RSSI threshold has been specified, and
7417 * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with
7418 * a RSSI smaller than the RSSI threshold will be dropped. If the quirk
7419 * is set, let it through for further processing, as we might need to
7420 * restart the scan.
7421 *
7422 * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry,
7423 * the results are also dropped.
7424 */
7425 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
7426 (rssi == HCI_RSSI_INVALID ||
7427 (rssi < hdev->discovery.rssi &&
7428 !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))
7429 return false;
7430
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08007431 if (hdev->discovery.uuid_count != 0) {
7432 /* If a list of UUIDs is provided in filter, results with no
7433 * matching UUID should be dropped.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007434 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08007435 if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count,
7436 hdev->discovery.uuids) &&
7437 !eir_has_uuids(scan_rsp, scan_rsp_len,
7438 hdev->discovery.uuid_count,
7439 hdev->discovery.uuids))
7440 return false;
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007441 }
7442
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08007443 /* If duplicate filtering does not report RSSI changes, then restart
7444 * scanning to ensure updated result with updated RSSI values.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007445 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08007446 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) {
7447 restart_le_scan(hdev);
7448
7449 /* Validate RSSI value against the RSSI threshold once more. */
7450 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
7451 rssi < hdev->discovery.rssi)
7452 return false;
7453 }
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007454
7455 return true;
7456}
7457
Marcel Holtmann901801b2013-10-06 23:55:51 -07007458void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
Marcel Holtmannaf589252014-07-01 14:11:20 +02007459 u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
7460 u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
Johan Hedberge17acd42011-03-30 23:57:16 +03007461{
Johan Hedberge319d2e2012-01-15 19:51:59 +02007462 char buf[512];
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007463 struct mgmt_ev_device_found *ev = (void *)buf;
Johan Hedberg1dc06092012-01-15 21:01:23 +02007464 size_t ev_size;
Johan Hedberge17acd42011-03-30 23:57:16 +03007465
Johan Hedberg75ce2082014-07-02 22:42:01 +03007466 /* Don't send events for a non-kernel initiated discovery. With
7467 * LE one exception is if we have pend_le_reports > 0 in which
7468 * case we're doing passive scanning and want these events.
7469 */
7470 if (!hci_discovery_active(hdev)) {
7471 if (link_type == ACL_LINK)
7472 return;
Johan Hedberg66f84552014-07-04 12:37:18 +03007473 if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports))
Johan Hedberg75ce2082014-07-02 22:42:01 +03007474 return;
7475 }
Andre Guedes12602d02013-04-30 15:29:40 -03007476
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08007477 if (hdev->discovery.result_filtering) {
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007478 /* We are using service discovery */
7479 if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp,
7480 scan_rsp_len))
7481 return;
7482 }
Marcel Holtmannbda157a2014-12-05 10:55:56 +01007483
Johan Hedberg78b781c2016-01-05 13:19:32 +02007484 if (hdev->discovery.limited) {
7485 /* Check for limited discoverable bit */
7486 if (dev_class) {
7487 if (!(dev_class[1] & 0x20))
7488 return;
7489 } else {
7490 u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL);
7491 if (!flags || !(flags[0] & LE_AD_LIMITED))
7492 return;
7493 }
7494 }
7495
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02007496 /* Make sure that the buffer is big enough. The 5 extra bytes
7497 * are for the potential CoD field.
7498 */
7499 if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf))
Marcel Holtmann901801b2013-10-06 23:55:51 -07007500 return;
Andre Guedes7d262f82012-01-10 18:20:49 -03007501
Johan Hedberg1dc06092012-01-15 21:01:23 +02007502 memset(buf, 0, sizeof(buf));
7503
Marcel Holtmannda25cf62014-12-05 13:03:35 +01007504 /* In case of device discovery with BR/EDR devices (pre 1.2), the
7505 * RSSI value was reported as 0 when not available. This behavior
7506 * is kept when using device discovery. This is required for full
7507 * backwards compatibility with the API.
7508 *
7509 * However when using service discovery, the value 127 will be
7510 * returned when the RSSI is not available.
7511 */
Szymon Janc91200e92015-01-22 16:57:05 +01007512 if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi &&
7513 link_type == ACL_LINK)
Marcel Holtmannefb25132014-12-05 13:03:34 +01007514 rssi = 0;
7515
Johan Hedberg841c5642014-07-07 12:45:54 +03007516 bacpy(&ev->addr.bdaddr, bdaddr);
7517 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberge319d2e2012-01-15 19:51:59 +02007518 ev->rssi = rssi;
Marcel Holtmannaf589252014-07-01 14:11:20 +02007519 ev->flags = cpu_to_le32(flags);
Johan Hedberge17acd42011-03-30 23:57:16 +03007520
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007521 if (eir_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007522 /* Copy EIR or advertising data into event */
Johan Hedberge319d2e2012-01-15 19:51:59 +02007523 memcpy(ev->eir, eir, eir_len);
Johan Hedberge17acd42011-03-30 23:57:16 +03007524
Johan Hedberg0d3b7f62016-01-05 13:19:31 +02007525 if (dev_class && !eir_get_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
7526 NULL))
Johan Hedberg1dc06092012-01-15 21:01:23 +02007527 eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007528 dev_class, 3);
Johan Hedberg1dc06092012-01-15 21:01:23 +02007529
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007530 if (scan_rsp_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007531 /* Append scan response data to event */
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02007532 memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len);
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007533
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02007534 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
7535 ev_size = sizeof(*ev) + eir_len + scan_rsp_len;
Andre Guedesf8523592011-09-09 18:56:26 -03007536
Marcel Holtmann901801b2013-10-06 23:55:51 -07007537 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL);
Johan Hedberge17acd42011-03-30 23:57:16 +03007538}
Johan Hedberga88a9652011-03-30 13:18:12 +03007539
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07007540void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
7541 u8 addr_type, s8 rssi, u8 *name, u8 name_len)
Johan Hedberga88a9652011-03-30 13:18:12 +03007542{
Johan Hedbergb644ba32012-01-17 21:48:47 +02007543 struct mgmt_ev_device_found *ev;
7544 char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2];
7545 u16 eir_len;
Johan Hedberga88a9652011-03-30 13:18:12 +03007546
Johan Hedbergb644ba32012-01-17 21:48:47 +02007547 ev = (struct mgmt_ev_device_found *) buf;
Johan Hedberga88a9652011-03-30 13:18:12 +03007548
Johan Hedbergb644ba32012-01-17 21:48:47 +02007549 memset(buf, 0, sizeof(buf));
Johan Hedberga88a9652011-03-30 13:18:12 +03007550
Johan Hedbergb644ba32012-01-17 21:48:47 +02007551 bacpy(&ev->addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007552 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007553 ev->rssi = rssi;
7554
7555 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007556 name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007557
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02007558 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007559
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07007560 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL);
Johan Hedberga88a9652011-03-30 13:18:12 +03007561}
Johan Hedberg314b2382011-04-27 10:29:57 -04007562
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07007563void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
Johan Hedberg314b2382011-04-27 10:29:57 -04007564{
Johan Hedbergf963e8e2012-02-20 23:30:44 +02007565 struct mgmt_ev_discovering ev;
Johan Hedberg164a6e72011-11-01 17:06:44 +02007566
Andre Guedes343fb142011-11-22 17:14:19 -03007567 BT_DBG("%s discovering %u", hdev->name, discovering);
7568
Johan Hedbergf963e8e2012-02-20 23:30:44 +02007569 memset(&ev, 0, sizeof(ev));
7570 ev.type = hdev->discovery.type;
7571 ev.discovering = discovering;
7572
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07007573 mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg314b2382011-04-27 10:29:57 -04007574}
Antti Julku5e762442011-08-25 16:48:02 +03007575
Johan Hedberg6d785aa32015-03-06 21:08:51 +02007576static struct hci_mgmt_chan chan = {
7577 .channel = HCI_CHANNEL_CONTROL,
7578 .handler_count = ARRAY_SIZE(mgmt_handlers),
7579 .handlers = mgmt_handlers,
Johan Hedberg88b94ce2015-03-17 13:48:49 +02007580 .hdev_init = mgmt_init_hdev,
Johan Hedberg6d785aa32015-03-06 21:08:51 +02007581};
7582
7583int mgmt_init(void)
7584{
7585 return hci_mgmt_chan_register(&chan);
7586}
7587
7588void mgmt_exit(void)
7589{
7590 hci_mgmt_chan_unregister(&chan);
7591}