blob: fb2e764c62113d8f08768e6dd118b081f7be0531 [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
Marcel Holtmannbeb1c212015-03-10 14:04:52 -070041#define MGMT_REVISION 9
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,
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200105};
106
107static const u16 mgmt_events[] = {
108 MGMT_EV_CONTROLLER_ERROR,
109 MGMT_EV_INDEX_ADDED,
110 MGMT_EV_INDEX_REMOVED,
111 MGMT_EV_NEW_SETTINGS,
112 MGMT_EV_CLASS_OF_DEV_CHANGED,
113 MGMT_EV_LOCAL_NAME_CHANGED,
114 MGMT_EV_NEW_LINK_KEY,
115 MGMT_EV_NEW_LONG_TERM_KEY,
116 MGMT_EV_DEVICE_CONNECTED,
117 MGMT_EV_DEVICE_DISCONNECTED,
118 MGMT_EV_CONNECT_FAILED,
119 MGMT_EV_PIN_CODE_REQUEST,
120 MGMT_EV_USER_CONFIRM_REQUEST,
121 MGMT_EV_USER_PASSKEY_REQUEST,
122 MGMT_EV_AUTH_FAILED,
123 MGMT_EV_DEVICE_FOUND,
124 MGMT_EV_DISCOVERING,
125 MGMT_EV_DEVICE_BLOCKED,
126 MGMT_EV_DEVICE_UNBLOCKED,
127 MGMT_EV_DEVICE_UNPAIRED,
Johan Hedberg92a25252012-09-06 18:39:26 +0300128 MGMT_EV_PASSKEY_NOTIFY,
Marcel Holtmann1b60ef22014-02-21 21:35:30 -0800129 MGMT_EV_NEW_IRK,
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700130 MGMT_EV_NEW_CSRK,
Marcel Holtmann8afef092014-06-29 22:28:34 +0200131 MGMT_EV_DEVICE_ADDED,
132 MGMT_EV_DEVICE_REMOVED,
Andre Guedesffb5a8272014-07-01 18:10:11 -0300133 MGMT_EV_NEW_CONN_PARAM,
Marcel Holtmann0602a8a2014-07-02 21:30:54 +0200134 MGMT_EV_UNCONF_INDEX_ADDED,
Marcel Holtmannedd38962014-07-02 21:30:55 +0200135 MGMT_EV_UNCONF_INDEX_REMOVED,
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200136 MGMT_EV_NEW_CONFIG_OPTIONS,
Marcel Holtmannced85542015-03-14 19:27:56 -0700137 MGMT_EV_EXT_INDEX_ADDED,
138 MGMT_EV_EXT_INDEX_REMOVED,
Marcel Holtmann72000df2015-03-16 16:11:21 -0700139 MGMT_EV_LOCAL_OOB_DATA_UPDATED,
Arman Uguray24b4f382015-03-23 15:57:12 -0700140 MGMT_EV_ADVERTISING_ADDED,
141 MGMT_EV_ADVERTISING_REMOVED,
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200142};
143
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700144static const u16 mgmt_untrusted_commands[] = {
145 MGMT_OP_READ_INDEX_LIST,
146 MGMT_OP_READ_INFO,
147 MGMT_OP_READ_UNCONF_INDEX_LIST,
148 MGMT_OP_READ_CONFIG_INFO,
149 MGMT_OP_READ_EXT_INDEX_LIST,
150};
151
152static const u16 mgmt_untrusted_events[] = {
153 MGMT_EV_INDEX_ADDED,
154 MGMT_EV_INDEX_REMOVED,
155 MGMT_EV_NEW_SETTINGS,
156 MGMT_EV_CLASS_OF_DEV_CHANGED,
157 MGMT_EV_LOCAL_NAME_CHANGED,
158 MGMT_EV_UNCONF_INDEX_ADDED,
159 MGMT_EV_UNCONF_INDEX_REMOVED,
160 MGMT_EV_NEW_CONFIG_OPTIONS,
161 MGMT_EV_EXT_INDEX_ADDED,
162 MGMT_EV_EXT_INDEX_REMOVED,
163};
164
Marcel Holtmann17b02e62012-03-01 14:32:37 -0800165#define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)
Johan Hedberg7d785252011-12-15 00:47:39 +0200166
Johan Hedbergd25b78e2015-01-27 12:55:52 +0200167#define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
168 "\x00\x00\x00\x00\x00\x00\x00\x00"
169
Johan Hedbergca69b792011-11-11 18:10:00 +0200170/* HCI to MGMT error code conversion table */
171static u8 mgmt_status_table[] = {
172 MGMT_STATUS_SUCCESS,
173 MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */
174 MGMT_STATUS_NOT_CONNECTED, /* No Connection */
175 MGMT_STATUS_FAILED, /* Hardware Failure */
176 MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */
177 MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */
Johan Hedbergeadd6632014-01-13 17:15:53 +0200178 MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */
Johan Hedbergca69b792011-11-11 18:10:00 +0200179 MGMT_STATUS_NO_RESOURCES, /* Memory Full */
180 MGMT_STATUS_TIMEOUT, /* Connection Timeout */
181 MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */
182 MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */
183 MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */
184 MGMT_STATUS_BUSY, /* Command Disallowed */
185 MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */
186 MGMT_STATUS_REJECTED, /* Rejected Security */
187 MGMT_STATUS_REJECTED, /* Rejected Personal */
188 MGMT_STATUS_TIMEOUT, /* Host Timeout */
189 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */
190 MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */
191 MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */
192 MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */
193 MGMT_STATUS_DISCONNECTED, /* OE Power Off */
194 MGMT_STATUS_DISCONNECTED, /* Connection Terminated */
195 MGMT_STATUS_BUSY, /* Repeated Attempts */
196 MGMT_STATUS_REJECTED, /* Pairing Not Allowed */
197 MGMT_STATUS_FAILED, /* Unknown LMP PDU */
198 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */
199 MGMT_STATUS_REJECTED, /* SCO Offset Rejected */
200 MGMT_STATUS_REJECTED, /* SCO Interval Rejected */
201 MGMT_STATUS_REJECTED, /* Air Mode Rejected */
202 MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */
203 MGMT_STATUS_FAILED, /* Unspecified Error */
204 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */
205 MGMT_STATUS_FAILED, /* Role Change Not Allowed */
206 MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */
207 MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */
208 MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */
209 MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */
210 MGMT_STATUS_FAILED, /* Unit Link Key Used */
211 MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */
212 MGMT_STATUS_TIMEOUT, /* Instant Passed */
213 MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */
214 MGMT_STATUS_FAILED, /* Transaction Collision */
215 MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */
216 MGMT_STATUS_REJECTED, /* QoS Rejected */
217 MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */
218 MGMT_STATUS_REJECTED, /* Insufficient Security */
219 MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */
220 MGMT_STATUS_BUSY, /* Role Switch Pending */
221 MGMT_STATUS_FAILED, /* Slot Violation */
222 MGMT_STATUS_FAILED, /* Role Switch Failed */
223 MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */
224 MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */
225 MGMT_STATUS_BUSY, /* Host Busy Pairing */
226 MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */
227 MGMT_STATUS_BUSY, /* Controller Busy */
228 MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */
229 MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */
230 MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */
231 MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */
232 MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */
233};
234
235static u8 mgmt_status(u8 hci_status)
236{
237 if (hci_status < ARRAY_SIZE(mgmt_status_table))
238 return mgmt_status_table[hci_status];
239
240 return MGMT_STATUS_FAILED;
241}
242
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700243static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data,
244 u16 len, int flag)
Marcel Holtmannf9207332015-03-14 19:27:55 -0700245{
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700246 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
247 flag, NULL);
Marcel Holtmannf9207332015-03-14 19:27:55 -0700248}
249
Marcel Holtmann72000df2015-03-16 16:11:21 -0700250static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data,
251 u16 len, int flag, struct sock *skip_sk)
252{
253 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
254 flag, skip_sk);
255}
256
Marcel Holtmannf6b77122015-03-14 19:28:05 -0700257static int mgmt_generic_event(u16 event, struct hci_dev *hdev, void *data,
258 u16 len, struct sock *skip_sk)
259{
260 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
261 HCI_MGMT_GENERIC_EVENTS, 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
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300271static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
272 u16 data_len)
Johan Hedberga38528f2011-01-22 06:46:43 +0200273{
274 struct mgmt_rp_read_version rp;
275
276 BT_DBG("sock %p", sk);
277
278 rp.version = MGMT_VERSION;
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700279 rp.revision = cpu_to_le16(MGMT_REVISION);
Johan Hedberga38528f2011-01-22 06:46:43 +0200280
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200281 return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0,
282 &rp, sizeof(rp));
Johan Hedberga38528f2011-01-22 06:46:43 +0200283}
284
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300285static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
286 u16 data_len)
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200287{
288 struct mgmt_rp_read_commands *rp;
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700289 u16 num_commands, num_events;
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200290 size_t rp_size;
291 int i, err;
292
293 BT_DBG("sock %p", sk);
294
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700295 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
296 num_commands = ARRAY_SIZE(mgmt_commands);
297 num_events = ARRAY_SIZE(mgmt_events);
298 } else {
299 num_commands = ARRAY_SIZE(mgmt_untrusted_commands);
300 num_events = ARRAY_SIZE(mgmt_untrusted_events);
301 }
302
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200303 rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16));
304
305 rp = kmalloc(rp_size, GFP_KERNEL);
306 if (!rp)
307 return -ENOMEM;
308
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700309 rp->num_commands = cpu_to_le16(num_commands);
310 rp->num_events = cpu_to_le16(num_events);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200311
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700312 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
313 __le16 *opcode = rp->opcodes;
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200314
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700315 for (i = 0; i < num_commands; i++, opcode++)
316 put_unaligned_le16(mgmt_commands[i], opcode);
317
318 for (i = 0; i < num_events; i++, opcode++)
319 put_unaligned_le16(mgmt_events[i], opcode);
320 } else {
321 __le16 *opcode = rp->opcodes;
322
323 for (i = 0; i < num_commands; i++, opcode++)
324 put_unaligned_le16(mgmt_untrusted_commands[i], opcode);
325
326 for (i = 0; i < num_events; i++, opcode++)
327 put_unaligned_le16(mgmt_untrusted_events[i], opcode);
328 }
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200329
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200330 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0,
331 rp, rp_size);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200332 kfree(rp);
333
334 return err;
335}
336
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300337static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
338 u16 data_len)
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200339{
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200340 struct mgmt_rp_read_index_list *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200341 struct hci_dev *d;
Johan Hedberga38528f2011-01-22 06:46:43 +0200342 size_t rp_len;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200343 u16 count;
Johan Hedberg476e44c2012-10-19 20:10:46 +0300344 int err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200345
346 BT_DBG("sock %p", sk);
347
348 read_lock(&hci_dev_list_lock);
349
350 count = 0;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +0300351 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200352 if (d->dev_type == HCI_BREDR &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700353 !hci_dev_test_flag(d, HCI_UNCONFIGURED))
Marcel Holtmann1514b892013-10-06 08:25:01 -0700354 count++;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200355 }
356
Johan Hedberga38528f2011-01-22 06:46:43 +0200357 rp_len = sizeof(*rp) + (2 * count);
358 rp = kmalloc(rp_len, GFP_ATOMIC);
359 if (!rp) {
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100360 read_unlock(&hci_dev_list_lock);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200361 return -ENOMEM;
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100362 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200363
Johan Hedberg476e44c2012-10-19 20:10:46 +0300364 count = 0;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200365 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700366 if (hci_dev_test_flag(d, HCI_SETUP) ||
367 hci_dev_test_flag(d, HCI_CONFIG) ||
368 hci_dev_test_flag(d, HCI_USER_CHANNEL))
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200369 continue;
370
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200371 /* Devices marked as raw-only are neither configured
372 * nor unconfigured controllers.
373 */
374 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
Marcel Holtmann0736cfa2013-08-26 21:40:51 -0700375 continue;
376
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200377 if (d->dev_type == HCI_BREDR &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700378 !hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
Marcel Holtmann1514b892013-10-06 08:25:01 -0700379 rp->index[count++] = cpu_to_le16(d->id);
380 BT_DBG("Added hci%u", d->id);
381 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200382 }
383
Johan Hedberg476e44c2012-10-19 20:10:46 +0300384 rp->num_controllers = cpu_to_le16(count);
385 rp_len = sizeof(*rp) + (2 * count);
386
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200387 read_unlock(&hci_dev_list_lock);
388
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200389 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST,
390 0, rp, rp_len);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200391
Johan Hedberga38528f2011-01-22 06:46:43 +0200392 kfree(rp);
393
394 return err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200395}
396
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200397static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
398 void *data, u16 data_len)
399{
400 struct mgmt_rp_read_unconf_index_list *rp;
401 struct hci_dev *d;
402 size_t rp_len;
403 u16 count;
404 int err;
405
406 BT_DBG("sock %p", sk);
407
408 read_lock(&hci_dev_list_lock);
409
410 count = 0;
411 list_for_each_entry(d, &hci_dev_list, list) {
412 if (d->dev_type == HCI_BREDR &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700413 hci_dev_test_flag(d, HCI_UNCONFIGURED))
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200414 count++;
415 }
416
417 rp_len = sizeof(*rp) + (2 * count);
418 rp = kmalloc(rp_len, GFP_ATOMIC);
419 if (!rp) {
420 read_unlock(&hci_dev_list_lock);
421 return -ENOMEM;
422 }
423
424 count = 0;
425 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700426 if (hci_dev_test_flag(d, HCI_SETUP) ||
427 hci_dev_test_flag(d, HCI_CONFIG) ||
428 hci_dev_test_flag(d, HCI_USER_CHANNEL))
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200429 continue;
430
431 /* Devices marked as raw-only are neither configured
432 * nor unconfigured controllers.
433 */
434 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
435 continue;
436
437 if (d->dev_type == HCI_BREDR &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700438 hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200439 rp->index[count++] = cpu_to_le16(d->id);
440 BT_DBG("Added hci%u", d->id);
441 }
442 }
443
444 rp->num_controllers = cpu_to_le16(count);
445 rp_len = sizeof(*rp) + (2 * count);
446
447 read_unlock(&hci_dev_list_lock);
448
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200449 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
450 MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len);
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200451
452 kfree(rp);
453
454 return err;
455}
456
Marcel Holtmann96f14742015-03-14 19:27:57 -0700457static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev,
458 void *data, u16 data_len)
459{
460 struct mgmt_rp_read_ext_index_list *rp;
461 struct hci_dev *d;
462 size_t rp_len;
463 u16 count;
464 int err;
465
466 BT_DBG("sock %p", sk);
467
468 read_lock(&hci_dev_list_lock);
469
470 count = 0;
471 list_for_each_entry(d, &hci_dev_list, list) {
472 if (d->dev_type == HCI_BREDR || d->dev_type == HCI_AMP)
473 count++;
474 }
475
476 rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
477 rp = kmalloc(rp_len, GFP_ATOMIC);
478 if (!rp) {
479 read_unlock(&hci_dev_list_lock);
480 return -ENOMEM;
481 }
482
483 count = 0;
484 list_for_each_entry(d, &hci_dev_list, list) {
485 if (hci_dev_test_flag(d, HCI_SETUP) ||
486 hci_dev_test_flag(d, HCI_CONFIG) ||
487 hci_dev_test_flag(d, HCI_USER_CHANNEL))
488 continue;
489
490 /* Devices marked as raw-only are neither configured
491 * nor unconfigured controllers.
492 */
493 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
494 continue;
495
496 if (d->dev_type == HCI_BREDR) {
497 if (hci_dev_test_flag(d, HCI_UNCONFIGURED))
498 rp->entry[count].type = 0x01;
499 else
500 rp->entry[count].type = 0x00;
501 } else if (d->dev_type == HCI_AMP) {
502 rp->entry[count].type = 0x02;
503 } else {
504 continue;
505 }
506
507 rp->entry[count].bus = d->bus;
508 rp->entry[count++].index = cpu_to_le16(d->id);
509 BT_DBG("Added hci%u", d->id);
510 }
511
512 rp->num_controllers = cpu_to_le16(count);
513 rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
514
515 read_unlock(&hci_dev_list_lock);
516
517 /* If this command is called at least once, then all the
518 * default index and unconfigured index events are disabled
519 * and from now on only extended index events are used.
520 */
521 hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS);
522 hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS);
523 hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
524
525 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
526 MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, rp_len);
527
528 kfree(rp);
529
530 return err;
531}
532
Marcel Holtmanndbece372014-07-04 18:11:55 +0200533static bool is_configured(struct hci_dev *hdev)
534{
535 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700536 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
Marcel Holtmanndbece372014-07-04 18:11:55 +0200537 return false;
538
539 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
540 !bacmp(&hdev->public_addr, BDADDR_ANY))
541 return false;
542
543 return true;
544}
545
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200546static __le32 get_missing_options(struct hci_dev *hdev)
547{
548 u32 options = 0;
549
Marcel Holtmanndbece372014-07-04 18:11:55 +0200550 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 Holtmanneb1904f2014-07-04 17:23:33 +0200552 options |= MGMT_OPTION_EXTERNAL_CONFIG;
553
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200554 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
555 !bacmp(&hdev->public_addr, BDADDR_ANY))
556 options |= MGMT_OPTION_PUBLIC_ADDRESS;
557
558 return cpu_to_le32(options);
559}
560
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200561static int new_options(struct hci_dev *hdev, struct sock *skip)
562{
563 __le32 options = get_missing_options(hdev);
564
Marcel Holtmannf6b77122015-03-14 19:28:05 -0700565 return mgmt_generic_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options,
566 sizeof(options), skip);
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200567}
568
Marcel Holtmanndbece372014-07-04 18:11:55 +0200569static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
570{
571 __le32 options = get_missing_options(hdev);
572
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200573 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options,
574 sizeof(options));
Marcel Holtmanndbece372014-07-04 18:11:55 +0200575}
576
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200577static int read_config_info(struct sock *sk, struct hci_dev *hdev,
578 void *data, u16 data_len)
579{
580 struct mgmt_rp_read_config_info rp;
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200581 u32 options = 0;
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200582
583 BT_DBG("sock %p %s", sk, hdev->name);
584
585 hci_dev_lock(hdev);
586
587 memset(&rp, 0, sizeof(rp));
588 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200589
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200590 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
591 options |= MGMT_OPTION_EXTERNAL_CONFIG;
592
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200593 if (hdev->set_bdaddr)
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200594 options |= MGMT_OPTION_PUBLIC_ADDRESS;
595
596 rp.supported_options = cpu_to_le32(options);
597 rp.missing_options = get_missing_options(hdev);
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200598
599 hci_dev_unlock(hdev);
600
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200601 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0,
602 &rp, sizeof(rp));
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200603}
604
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200605static u32 get_supported_settings(struct hci_dev *hdev)
Johan Hedberg03811012010-12-08 00:21:06 +0200606{
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200607 u32 settings = 0;
Johan Hedberg03811012010-12-08 00:21:06 +0200608
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200609 settings |= MGMT_SETTING_POWERED;
Johan Hedbergb2939472014-07-30 09:22:23 +0300610 settings |= MGMT_SETTING_BONDABLE;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800611 settings |= MGMT_SETTING_DEBUG_KEYS;
Johan Hedberg3742abf2014-07-08 16:07:34 +0300612 settings |= MGMT_SETTING_CONNECTABLE;
613 settings |= MGMT_SETTING_DISCOVERABLE;
Johan Hedberg03811012010-12-08 00:21:06 +0200614
Andre Guedesed3fa312012-07-24 15:03:46 -0300615 if (lmp_bredr_capable(hdev)) {
Johan Hedberg1a47aee2013-03-15 17:07:06 -0500616 if (hdev->hci_ver >= BLUETOOTH_VER_1_2)
617 settings |= MGMT_SETTING_FAST_CONNECTABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200618 settings |= MGMT_SETTING_BREDR;
619 settings |= MGMT_SETTING_LINK_SECURITY;
Marcel Holtmanna82974c2013-10-11 09:48:47 -0700620
621 if (lmp_ssp_capable(hdev)) {
622 settings |= MGMT_SETTING_SSP;
623 settings |= MGMT_SETTING_HS;
624 }
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800625
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -0800626 if (lmp_sc_capable(hdev))
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800627 settings |= MGMT_SETTING_SECURE_CONN;
Marcel Holtmann848566b2013-10-01 22:59:22 -0700628 }
Marcel Holtmannd7b7e792012-02-20 21:47:49 +0100629
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300630 if (lmp_le_capable(hdev)) {
Marcel Holtmann9d428202012-05-03 07:12:31 +0200631 settings |= MGMT_SETTING_LE;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300632 settings |= MGMT_SETTING_ADVERTISING;
Johan Hedberga3209692014-05-26 11:23:35 +0300633 settings |= MGMT_SETTING_SECURE_CONN;
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200634 settings |= MGMT_SETTING_PRIVACY;
Marcel Holtmann93690c22015-03-06 10:11:21 -0800635 settings |= MGMT_SETTING_STATIC_ADDRESS;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300636 }
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200637
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200638 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
639 hdev->set_bdaddr)
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200640 settings |= MGMT_SETTING_CONFIGURATION;
641
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200642 return settings;
643}
Johan Hedbergebc99fe2011-01-04 11:54:26 +0200644
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200645static u32 get_current_settings(struct hci_dev *hdev)
646{
647 u32 settings = 0;
Johan Hedbergdc4fe302011-03-16 14:29:36 +0200648
Johan Hedbergf1f0eb02012-02-21 17:15:41 +0200649 if (hdev_is_powered(hdev))
Marcel Holtmannf0d4b782012-02-21 12:14:25 +0100650 settings |= MGMT_SETTING_POWERED;
651
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700652 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200653 settings |= MGMT_SETTING_CONNECTABLE;
654
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700655 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
Johan Hedberg1a4d3c42013-03-15 17:07:08 -0500656 settings |= MGMT_SETTING_FAST_CONNECTABLE;
657
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700658 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200659 settings |= MGMT_SETTING_DISCOVERABLE;
660
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700661 if (hci_dev_test_flag(hdev, HCI_BONDABLE))
Johan Hedbergb2939472014-07-30 09:22:23 +0300662 settings |= MGMT_SETTING_BONDABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200663
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700664 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200665 settings |= MGMT_SETTING_BREDR;
666
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700667 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200668 settings |= MGMT_SETTING_LE;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200669
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700670 if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200671 settings |= MGMT_SETTING_LINK_SECURITY;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200672
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700673 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200674 settings |= MGMT_SETTING_SSP;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200675
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700676 if (hci_dev_test_flag(hdev, HCI_HS_ENABLED))
Johan Hedberg6d80dfd2012-02-20 23:50:38 +0200677 settings |= MGMT_SETTING_HS;
678
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700679 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300680 settings |= MGMT_SETTING_ADVERTISING;
681
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700682 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800683 settings |= MGMT_SETTING_SECURE_CONN;
684
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700685 if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS))
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800686 settings |= MGMT_SETTING_DEBUG_KEYS;
687
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700688 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200689 settings |= MGMT_SETTING_PRIVACY;
690
Marcel Holtmann93690c22015-03-06 10:11:21 -0800691 /* The current setting for static address has two purposes. The
692 * first is to indicate if the static address will be used and
693 * the second is to indicate if it is actually set.
694 *
695 * This means if the static address is not configured, this flag
Marcel Holtmann08dc0e92015-03-25 18:32:13 -0700696 * will never be set. If the address is configured, then if the
Marcel Holtmann93690c22015-03-06 10:11:21 -0800697 * address is actually used decides if the flag is set or not.
698 *
699 * For single mode LE only controllers and dual-mode controllers
700 * with BR/EDR disabled, the existence of the static address will
701 * be evaluated.
702 */
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -0700703 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700704 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
Marcel Holtmann93690c22015-03-06 10:11:21 -0800705 !bacmp(&hdev->bdaddr, BDADDR_ANY)) {
706 if (bacmp(&hdev->static_addr, BDADDR_ANY))
707 settings |= MGMT_SETTING_STATIC_ADDRESS;
708 }
709
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200710 return settings;
Johan Hedbergc542a062011-01-26 13:11:03 +0200711}
712
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300713#define PNP_INFO_SVCLASS_ID 0x1200
714
Johan Hedberg213202e2013-01-27 00:31:33 +0200715static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
716{
717 u8 *ptr = data, *uuids_start = NULL;
718 struct bt_uuid *uuid;
719
720 if (len < 4)
721 return ptr;
722
723 list_for_each_entry(uuid, &hdev->uuids, list) {
724 u16 uuid16;
725
726 if (uuid->size != 16)
727 continue;
728
729 uuid16 = get_unaligned_le16(&uuid->uuid[12]);
730 if (uuid16 < 0x1100)
731 continue;
732
733 if (uuid16 == PNP_INFO_SVCLASS_ID)
734 continue;
735
736 if (!uuids_start) {
737 uuids_start = ptr;
738 uuids_start[0] = 1;
739 uuids_start[1] = EIR_UUID16_ALL;
740 ptr += 2;
741 }
742
743 /* Stop if not enough space to put next UUID */
744 if ((ptr - data) + sizeof(u16) > len) {
745 uuids_start[1] = EIR_UUID16_SOME;
746 break;
747 }
748
749 *ptr++ = (uuid16 & 0x00ff);
750 *ptr++ = (uuid16 & 0xff00) >> 8;
751 uuids_start[0] += sizeof(uuid16);
752 }
753
754 return ptr;
755}
756
Johan Hedbergcdf19632013-01-27 00:31:34 +0200757static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
758{
759 u8 *ptr = data, *uuids_start = NULL;
760 struct bt_uuid *uuid;
761
762 if (len < 6)
763 return ptr;
764
765 list_for_each_entry(uuid, &hdev->uuids, list) {
766 if (uuid->size != 32)
767 continue;
768
769 if (!uuids_start) {
770 uuids_start = ptr;
771 uuids_start[0] = 1;
772 uuids_start[1] = EIR_UUID32_ALL;
773 ptr += 2;
774 }
775
776 /* Stop if not enough space to put next UUID */
777 if ((ptr - data) + sizeof(u32) > len) {
778 uuids_start[1] = EIR_UUID32_SOME;
779 break;
780 }
781
782 memcpy(ptr, &uuid->uuid[12], sizeof(u32));
783 ptr += sizeof(u32);
784 uuids_start[0] += sizeof(u32);
785 }
786
787 return ptr;
788}
789
Johan Hedbergc00d5752013-01-27 00:31:35 +0200790static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
791{
792 u8 *ptr = data, *uuids_start = NULL;
793 struct bt_uuid *uuid;
794
795 if (len < 18)
796 return ptr;
797
798 list_for_each_entry(uuid, &hdev->uuids, list) {
799 if (uuid->size != 128)
800 continue;
801
802 if (!uuids_start) {
803 uuids_start = ptr;
804 uuids_start[0] = 1;
805 uuids_start[1] = EIR_UUID128_ALL;
806 ptr += 2;
807 }
808
809 /* Stop if not enough space to put next UUID */
810 if ((ptr - data) + 16 > len) {
811 uuids_start[1] = EIR_UUID128_SOME;
812 break;
813 }
814
815 memcpy(ptr, uuid->uuid, 16);
816 ptr += 16;
817 uuids_start[0] += 16;
818 }
819
820 return ptr;
821}
822
Johan Hedberg333ae952015-03-17 13:48:47 +0200823static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev)
824{
825 return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev);
826}
827
Johan Hedberg333ae952015-03-17 13:48:47 +0200828static struct mgmt_pending_cmd *pending_find_data(u16 opcode,
829 struct hci_dev *hdev,
830 const void *data)
831{
832 return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data);
833}
834
Arman Uguray4117ed72015-03-23 15:57:14 -0700835static u8 create_default_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700836{
Marcel Holtmann7a5f4992013-10-16 00:16:49 -0700837 u8 ad_len = 0;
838 size_t name_len;
839
840 name_len = strlen(hdev->dev_name);
841 if (name_len > 0) {
842 size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2;
843
844 if (name_len > max_len) {
845 name_len = max_len;
846 ptr[1] = EIR_NAME_SHORT;
847 } else
848 ptr[1] = EIR_NAME_COMPLETE;
849
850 ptr[0] = name_len + 1;
851
852 memcpy(ptr + 2, hdev->dev_name, name_len);
853
854 ad_len += (name_len + 2);
855 ptr += (name_len + 2);
856 }
857
858 return ad_len;
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700859}
860
Arman Uguray4117ed72015-03-23 15:57:14 -0700861static u8 create_instance_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
862{
863 /* TODO: Set the appropriate entries based on advertising instance flags
864 * here once flags other than 0 are supported.
865 */
866 memcpy(ptr, hdev->adv_instance.scan_rsp_data,
867 hdev->adv_instance.scan_rsp_len);
868
869 return hdev->adv_instance.scan_rsp_len;
870}
871
872static void update_scan_rsp_data_for_instance(struct hci_request *req,
873 u8 instance)
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700874{
875 struct hci_dev *hdev = req->hdev;
876 struct hci_cp_le_set_scan_rsp_data cp;
877 u8 len;
878
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700879 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700880 return;
881
882 memset(&cp, 0, sizeof(cp));
883
Arman Uguray4117ed72015-03-23 15:57:14 -0700884 if (instance)
885 len = create_instance_scan_rsp_data(hdev, cp.data);
886 else
887 len = create_default_scan_rsp_data(hdev, cp.data);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700888
Johan Hedbergeb438b52013-10-16 15:31:07 +0300889 if (hdev->scan_rsp_data_len == len &&
Arman Uguray4117ed72015-03-23 15:57:14 -0700890 !memcmp(cp.data, hdev->scan_rsp_data, len))
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700891 return;
892
Johan Hedbergeb438b52013-10-16 15:31:07 +0300893 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
894 hdev->scan_rsp_data_len = len;
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700895
896 cp.length = len;
897
898 hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp);
899}
900
Arman Uguray4117ed72015-03-23 15:57:14 -0700901static void update_scan_rsp_data(struct hci_request *req)
902{
903 struct hci_dev *hdev = req->hdev;
904 u8 instance;
905
906 /* The "Set Advertising" setting supersedes the "Add Advertising"
907 * setting. Here we set the scan response data based on which
908 * setting was set. When neither apply, default to the global settings,
909 * represented by instance "0".
910 */
911 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
912 !hci_dev_test_flag(hdev, HCI_ADVERTISING))
913 instance = 0x01;
914 else
915 instance = 0x00;
916
917 update_scan_rsp_data_for_instance(req, instance);
918}
919
Johan Hedberg9a43e252013-10-20 19:00:07 +0300920static u8 get_adv_discov_flags(struct hci_dev *hdev)
921{
Johan Hedberg3b0602c2015-03-06 21:08:55 +0200922 struct mgmt_pending_cmd *cmd;
Johan Hedberg9a43e252013-10-20 19:00:07 +0300923
924 /* If there's a pending mgmt command the flags will not yet have
925 * their final values, so check for this first.
926 */
Johan Hedberg333ae952015-03-17 13:48:47 +0200927 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg9a43e252013-10-20 19:00:07 +0300928 if (cmd) {
929 struct mgmt_mode *cp = cmd->param;
930 if (cp->val == 0x01)
931 return LE_AD_GENERAL;
932 else if (cp->val == 0x02)
933 return LE_AD_LIMITED;
934 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700935 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300936 return LE_AD_LIMITED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700937 else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300938 return LE_AD_GENERAL;
939 }
940
941 return 0;
942}
943
Arman Uguraye7a685d2015-03-25 18:53:40 -0700944static u8 get_current_adv_instance(struct hci_dev *hdev)
Arman Uguray24b4f382015-03-23 15:57:12 -0700945{
Arman Uguray24b4f382015-03-23 15:57:12 -0700946 /* The "Set Advertising" setting supersedes the "Add Advertising"
947 * setting. Here we set the advertising data based on which
948 * setting was set. When neither apply, default to the global settings,
949 * represented by instance "0".
950 */
951 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
952 !hci_dev_test_flag(hdev, HCI_ADVERTISING))
Arman Uguraye7a685d2015-03-25 18:53:40 -0700953 return 0x01;
954
955 return 0x00;
956}
957
958static bool get_connectable(struct hci_dev *hdev)
959{
960 struct mgmt_pending_cmd *cmd;
961
962 /* If there's a pending mgmt command the flag will not yet have
963 * it's final value, so check for this first.
964 */
965 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
966 if (cmd) {
967 struct mgmt_mode *cp = cmd->param;
968
969 return cp->val;
970 }
971
972 return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
973}
974
975static u32 get_adv_instance_flags(struct hci_dev *hdev, u8 instance)
976{
977 u32 flags;
978
979 if (instance > 0x01)
980 return 0;
981
Arman Ugurayfdf51782015-03-25 18:53:46 -0700982 if (instance == 0x01)
Arman Uguraye7a685d2015-03-25 18:53:40 -0700983 return hdev->adv_instance.flags;
984
Arman Ugurayfdf51782015-03-25 18:53:46 -0700985 /* Instance 0 always manages the "Tx Power" and "Flags" fields */
986 flags = MGMT_ADV_FLAG_TX_POWER | MGMT_ADV_FLAG_MANAGED_FLAGS;
Arman Uguraye7a685d2015-03-25 18:53:40 -0700987
988 /* For instance 0, assemble the flags from global settings */
989 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE) ||
990 get_connectable(hdev))
991 flags |= MGMT_ADV_FLAG_CONNECTABLE;
992
Arman Uguraye7a685d2015-03-25 18:53:40 -0700993 return flags;
994}
995
Arman Ugurayfdf51782015-03-25 18:53:46 -0700996static u8 create_instance_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
997{
998 u8 ad_len = 0, flags = 0;
999 u32 instance_flags = get_adv_instance_flags(hdev, instance);
1000
1001 /* The Add Advertising command allows userspace to set both the general
1002 * and limited discoverable flags.
1003 */
1004 if (instance_flags & MGMT_ADV_FLAG_DISCOV)
1005 flags |= LE_AD_GENERAL;
1006
1007 if (instance_flags & MGMT_ADV_FLAG_LIMITED_DISCOV)
1008 flags |= LE_AD_LIMITED;
1009
1010 if (flags || (instance_flags & MGMT_ADV_FLAG_MANAGED_FLAGS)) {
1011 /* If a discovery flag wasn't provided, simply use the global
1012 * settings.
1013 */
1014 if (!flags)
1015 flags |= get_adv_discov_flags(hdev);
1016
1017 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1018 flags |= LE_AD_NO_BREDR;
1019
1020 /* If flags would still be empty, then there is no need to
1021 * include the "Flags" AD field".
1022 */
1023 if (flags) {
1024 ptr[0] = 0x02;
1025 ptr[1] = EIR_FLAGS;
1026 ptr[2] = flags;
1027
1028 ad_len += 3;
1029 ptr += 3;
1030 }
1031 }
1032
1033 /* Provide Tx Power only if we can provide a valid value for it */
1034 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID &&
1035 (instance_flags & MGMT_ADV_FLAG_TX_POWER)) {
1036 ptr[0] = 0x02;
1037 ptr[1] = EIR_TX_POWER;
1038 ptr[2] = (u8)hdev->adv_tx_power;
1039
1040 ad_len += 3;
1041 ptr += 3;
1042 }
1043
1044 if (instance) {
1045 memcpy(ptr, hdev->adv_instance.adv_data,
1046 hdev->adv_instance.adv_data_len);
1047 ad_len += hdev->adv_instance.adv_data_len;
1048 }
1049
1050 return ad_len;
1051}
1052
1053static void update_adv_data_for_instance(struct hci_request *req, u8 instance)
1054{
1055 struct hci_dev *hdev = req->hdev;
1056 struct hci_cp_le_set_adv_data cp;
1057 u8 len;
1058
1059 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1060 return;
1061
1062 memset(&cp, 0, sizeof(cp));
1063
1064 len = create_instance_adv_data(hdev, instance, cp.data);
1065
1066 /* There's nothing to do if the data hasn't changed */
1067 if (hdev->adv_data_len == len &&
1068 memcmp(cp.data, hdev->adv_data, len) == 0)
1069 return;
1070
1071 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1072 hdev->adv_data_len = len;
1073
1074 cp.length = len;
1075
1076 hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
1077}
1078
Arman Uguraye7a685d2015-03-25 18:53:40 -07001079static void update_adv_data(struct hci_request *req)
1080{
1081 struct hci_dev *hdev = req->hdev;
1082 u8 instance = get_current_adv_instance(hdev);
Arman Uguray24b4f382015-03-23 15:57:12 -07001083
1084 update_adv_data_for_instance(req, instance);
1085}
1086
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001087int mgmt_update_adv_data(struct hci_dev *hdev)
1088{
1089 struct hci_request req;
1090
1091 hci_req_init(&req, hdev);
1092 update_adv_data(&req);
1093
1094 return hci_req_run(&req, NULL);
1095}
1096
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001097static void create_eir(struct hci_dev *hdev, u8 *data)
1098{
1099 u8 *ptr = data;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001100 size_t name_len;
1101
1102 name_len = strlen(hdev->dev_name);
1103
1104 if (name_len > 0) {
1105 /* EIR Data type */
1106 if (name_len > 48) {
1107 name_len = 48;
1108 ptr[1] = EIR_NAME_SHORT;
1109 } else
1110 ptr[1] = EIR_NAME_COMPLETE;
1111
1112 /* EIR Data length */
1113 ptr[0] = name_len + 1;
1114
1115 memcpy(ptr + 2, hdev->dev_name, name_len);
1116
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001117 ptr += (name_len + 2);
1118 }
1119
Johan Hedbergbbaf4442012-11-08 01:22:59 +01001120 if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) {
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -07001121 ptr[0] = 2;
1122 ptr[1] = EIR_TX_POWER;
1123 ptr[2] = (u8) hdev->inq_tx_power;
1124
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -07001125 ptr += 3;
1126 }
1127
Marcel Holtmann2b9be132012-03-11 19:32:12 -07001128 if (hdev->devid_source > 0) {
1129 ptr[0] = 9;
1130 ptr[1] = EIR_DEVICE_ID;
1131
1132 put_unaligned_le16(hdev->devid_source, ptr + 2);
1133 put_unaligned_le16(hdev->devid_vendor, ptr + 4);
1134 put_unaligned_le16(hdev->devid_product, ptr + 6);
1135 put_unaligned_le16(hdev->devid_version, ptr + 8);
1136
Marcel Holtmann2b9be132012-03-11 19:32:12 -07001137 ptr += 10;
1138 }
1139
Johan Hedberg213202e2013-01-27 00:31:33 +02001140 ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedbergcdf19632013-01-27 00:31:34 +02001141 ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedbergc00d5752013-01-27 00:31:35 +02001142 ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001143}
1144
Johan Hedberg890ea892013-03-15 17:06:52 -05001145static void update_eir(struct hci_request *req)
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001146{
Johan Hedberg890ea892013-03-15 17:06:52 -05001147 struct hci_dev *hdev = req->hdev;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001148 struct hci_cp_write_eir cp;
1149
Johan Hedberg504c8dc2012-02-23 13:30:41 +02001150 if (!hdev_is_powered(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05001151 return;
Johan Hedberg7770c4a2012-02-22 22:06:38 +02001152
Johan Hedberg976eb202012-10-24 21:12:01 +03001153 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05001154 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001155
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001156 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberg890ea892013-03-15 17:06:52 -05001157 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001158
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001159 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg890ea892013-03-15 17:06:52 -05001160 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001161
1162 memset(&cp, 0, sizeof(cp));
1163
1164 create_eir(hdev, cp.data);
1165
1166 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
Johan Hedberg890ea892013-03-15 17:06:52 -05001167 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001168
1169 memcpy(hdev->eir, cp.data, sizeof(cp.data));
1170
Johan Hedberg890ea892013-03-15 17:06:52 -05001171 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001172}
1173
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001174static u8 get_service_classes(struct hci_dev *hdev)
1175{
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -03001176 struct bt_uuid *uuid;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001177 u8 val = 0;
1178
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -03001179 list_for_each_entry(uuid, &hdev->uuids, list)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001180 val |= uuid->svc_hint;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001181
1182 return val;
1183}
1184
Johan Hedberg890ea892013-03-15 17:06:52 -05001185static void update_class(struct hci_request *req)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001186{
Johan Hedberg890ea892013-03-15 17:06:52 -05001187 struct hci_dev *hdev = req->hdev;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001188 u8 cod[3];
1189
1190 BT_DBG("%s", hdev->name);
1191
Johan Hedberg504c8dc2012-02-23 13:30:41 +02001192 if (!hdev_is_powered(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05001193 return;
Johan Hedberg7770c4a2012-02-22 22:06:38 +02001194
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001195 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedbergf87ea1d2013-10-19 23:38:17 +03001196 return;
1197
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001198 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg890ea892013-03-15 17:06:52 -05001199 return;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001200
1201 cod[0] = hdev->minor_class;
1202 cod[1] = hdev->major_class;
1203 cod[2] = get_service_classes(hdev);
1204
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001205 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
Marcel Holtmann6acd7db2013-10-15 06:33:53 -07001206 cod[1] |= 0x20;
1207
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001208 if (memcmp(cod, hdev->dev_class, 3) == 0)
Johan Hedberg890ea892013-03-15 17:06:52 -05001209 return;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001210
Johan Hedberg890ea892013-03-15 17:06:52 -05001211 hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001212}
1213
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001214static void disable_advertising(struct hci_request *req)
1215{
1216 u8 enable = 0x00;
1217
1218 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1219}
1220
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001221static void enable_advertising(struct hci_request *req)
1222{
1223 struct hci_dev *hdev = req->hdev;
1224 struct hci_cp_le_set_adv_param cp;
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001225 u8 own_addr_type, enable = 0x01;
Johan Hedberga4858cb2014-02-25 19:56:31 +02001226 bool connectable;
Arman Uguraye7a685d2015-03-25 18:53:40 -07001227 u8 instance;
1228 u32 flags;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001229
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001230 if (hci_conn_num(hdev, LE_LINK) > 0)
1231 return;
1232
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001233 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001234 disable_advertising(req);
1235
Johan Hedberg5ce194c2014-07-08 15:07:49 +03001236 /* Clear the HCI_LE_ADV bit temporarily so that the
Johan Hedberg8d972502014-02-28 12:54:14 +02001237 * hci_update_random_address knows that it's safe to go ahead
1238 * and write a new random address. The flag will be set back on
1239 * as soon as the SET_ADV_ENABLE HCI command completes.
1240 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001241 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Johan Hedberg8d972502014-02-28 12:54:14 +02001242
Arman Uguraye7a685d2015-03-25 18:53:40 -07001243 instance = get_current_adv_instance(hdev);
1244 flags = get_adv_instance_flags(hdev, instance);
1245 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE);
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001246
Johan Hedberga4858cb2014-02-25 19:56:31 +02001247 /* Set require_privacy to true only when non-connectable
1248 * advertising is used. In that case it is fine to use a
1249 * non-resolvable private address.
1250 */
1251 if (hci_update_random_address(req, !connectable, &own_addr_type) < 0)
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001252 return;
1253
Marcel Holtmann41c90c12014-02-23 20:25:55 -08001254 memset(&cp, 0, sizeof(cp));
Georg Lukas628531c2014-07-26 13:59:57 +02001255 cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval);
1256 cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval);
Johan Hedberga4858cb2014-02-25 19:56:31 +02001257 cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND;
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001258 cp.own_address_type = own_addr_type;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001259 cp.channel_map = hdev->le_adv_channel_map;
1260
1261 hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
1262
1263 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1264}
1265
Johan Hedberg7d785252011-12-15 00:47:39 +02001266static void service_cache_off(struct work_struct *work)
1267{
1268 struct hci_dev *hdev = container_of(work, struct hci_dev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001269 service_cache.work);
Johan Hedberg890ea892013-03-15 17:06:52 -05001270 struct hci_request req;
Johan Hedberg7d785252011-12-15 00:47:39 +02001271
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001272 if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg7d785252011-12-15 00:47:39 +02001273 return;
1274
Johan Hedberg890ea892013-03-15 17:06:52 -05001275 hci_req_init(&req, hdev);
1276
Johan Hedberg7d785252011-12-15 00:47:39 +02001277 hci_dev_lock(hdev);
1278
Johan Hedberg890ea892013-03-15 17:06:52 -05001279 update_eir(&req);
1280 update_class(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02001281
1282 hci_dev_unlock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -05001283
1284 hci_req_run(&req, NULL);
Johan Hedberg7d785252011-12-15 00:47:39 +02001285}
1286
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001287static void rpa_expired(struct work_struct *work)
1288{
1289 struct hci_dev *hdev = container_of(work, struct hci_dev,
1290 rpa_expired.work);
1291 struct hci_request req;
1292
1293 BT_DBG("");
1294
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001295 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001296
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001297 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001298 return;
1299
1300 /* The generation of a new RPA and programming it into the
1301 * controller happens in the enable_advertising() function.
1302 */
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001303 hci_req_init(&req, hdev);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001304 enable_advertising(&req);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001305 hci_req_run(&req, NULL);
1306}
1307
Johan Hedberg6a919082012-02-28 06:17:26 +02001308static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
Johan Hedberg7d785252011-12-15 00:47:39 +02001309{
Marcel Holtmann238be782015-03-13 02:11:06 -07001310 if (hci_dev_test_and_set_flag(hdev, HCI_MGMT))
Johan Hedberg6a919082012-02-28 06:17:26 +02001311 return;
1312
Johan Hedberg4f87da82012-03-02 19:55:56 +02001313 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001314 INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
Johan Hedberg7d785252011-12-15 00:47:39 +02001315
Johan Hedberg4f87da82012-03-02 19:55:56 +02001316 /* Non-mgmt controlled devices get this bit set
1317 * implicitly so that pairing works for them, however
1318 * for mgmt we require user-space to explicitly enable
1319 * it
1320 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001321 hci_dev_clear_flag(hdev, HCI_BONDABLE);
Johan Hedberg7d785252011-12-15 00:47:39 +02001322}
1323
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02001324static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001325 void *data, u16 data_len)
Johan Hedberg03811012010-12-08 00:21:06 +02001326{
1327 struct mgmt_rp_read_info rp;
Johan Hedberg03811012010-12-08 00:21:06 +02001328
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001329 BT_DBG("sock %p %s", sk, hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001330
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001331 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001332
Johan Hedberg03811012010-12-08 00:21:06 +02001333 memset(&rp, 0, sizeof(rp));
1334
Johan Hedberg03811012010-12-08 00:21:06 +02001335 bacpy(&rp.bdaddr, &hdev->bdaddr);
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001336
1337 rp.version = hdev->hci_ver;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02001338 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001339
1340 rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
1341 rp.current_settings = cpu_to_le32(get_current_settings(hdev));
1342
1343 memcpy(rp.dev_class, hdev->dev_class, 3);
Johan Hedberg03811012010-12-08 00:21:06 +02001344
1345 memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
Johan Hedberg27fcc362012-02-22 21:46:22 +02001346 memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
Johan Hedberg03811012010-12-08 00:21:06 +02001347
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001348 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001349
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001350 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
1351 sizeof(rp));
Johan Hedberg03811012010-12-08 00:21:06 +02001352}
1353
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001354static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
Johan Hedberg86805702011-11-11 16:18:52 +02001355{
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001356 __le32 settings = cpu_to_le32(get_current_settings(hdev));
Johan Hedberg86805702011-11-11 16:18:52 +02001357
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001358 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings,
1359 sizeof(settings));
Johan Hedberg86805702011-11-11 16:18:52 +02001360}
1361
Marcel Holtmann1904a852015-01-11 13:50:44 -08001362static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg8b064a32014-02-24 14:52:22 +02001363{
1364 BT_DBG("%s status 0x%02x", hdev->name, status);
1365
Johan Hedberga3172b72014-02-28 09:33:44 +02001366 if (hci_conn_count(hdev) == 0) {
1367 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001368 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberga3172b72014-02-28 09:33:44 +02001369 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001370}
1371
Johan Hedberg23a48092014-07-08 16:05:06 +03001372static bool hci_stop_discovery(struct hci_request *req)
Johan Hedberg21a60d32014-06-10 14:05:58 +03001373{
1374 struct hci_dev *hdev = req->hdev;
1375 struct hci_cp_remote_name_req_cancel cp;
1376 struct inquiry_entry *e;
1377
1378 switch (hdev->discovery.state) {
1379 case DISCOVERY_FINDING:
Jakub Pawlowski07d23342015-03-17 09:04:14 -07001380 if (test_bit(HCI_INQUIRY, &hdev->flags))
Johan Hedberg21a60d32014-06-10 14:05:58 +03001381 hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL);
Jakub Pawlowski07d23342015-03-17 09:04:14 -07001382
1383 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
Johan Hedberg21a60d32014-06-10 14:05:58 +03001384 cancel_delayed_work(&hdev->le_scan_disable);
1385 hci_req_add_le_scan_disable(req);
1386 }
1387
Johan Hedberg23a48092014-07-08 16:05:06 +03001388 return true;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001389
1390 case DISCOVERY_RESOLVING:
1391 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
1392 NAME_PENDING);
1393 if (!e)
Johan Hedberg23a48092014-07-08 16:05:06 +03001394 break;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001395
1396 bacpy(&cp.bdaddr, &e->data.bdaddr);
1397 hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp),
1398 &cp);
1399
Johan Hedberg23a48092014-07-08 16:05:06 +03001400 return true;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001401
1402 default:
1403 /* Passive scanning */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001404 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
Johan Hedberg21a60d32014-06-10 14:05:58 +03001405 hci_req_add_le_scan_disable(req);
Johan Hedberg23a48092014-07-08 16:05:06 +03001406 return true;
1407 }
1408
Johan Hedberg21a60d32014-06-10 14:05:58 +03001409 break;
1410 }
Johan Hedberg23a48092014-07-08 16:05:06 +03001411
1412 return false;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001413}
1414
Arman Uguray912098a2015-03-23 15:57:15 -07001415static void advertising_added(struct sock *sk, struct hci_dev *hdev,
1416 u8 instance)
1417{
1418 struct mgmt_ev_advertising_added ev;
1419
1420 ev.instance = instance;
1421
1422 mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk);
1423}
1424
1425static void advertising_removed(struct sock *sk, struct hci_dev *hdev,
1426 u8 instance)
1427{
1428 struct mgmt_ev_advertising_removed ev;
1429
1430 ev.instance = instance;
1431
1432 mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk);
1433}
1434
1435static void clear_adv_instance(struct hci_dev *hdev)
1436{
1437 struct hci_request req;
1438
1439 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
1440 return;
1441
1442 if (hdev->adv_instance.timeout)
1443 cancel_delayed_work(&hdev->adv_instance.timeout_exp);
1444
1445 memset(&hdev->adv_instance, 0, sizeof(hdev->adv_instance));
1446 advertising_removed(NULL, hdev, 1);
1447 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
1448
1449 if (!hdev_is_powered(hdev) ||
1450 hci_dev_test_flag(hdev, HCI_ADVERTISING))
1451 return;
1452
1453 hci_req_init(&req, hdev);
1454 disable_advertising(&req);
1455 hci_req_run(&req, NULL);
1456}
1457
Johan Hedberg8b064a32014-02-24 14:52:22 +02001458static int clean_up_hci_state(struct hci_dev *hdev)
1459{
1460 struct hci_request req;
1461 struct hci_conn *conn;
Johan Hedberg23a48092014-07-08 16:05:06 +03001462 bool discov_stopped;
1463 int err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001464
1465 hci_req_init(&req, hdev);
1466
1467 if (test_bit(HCI_ISCAN, &hdev->flags) ||
1468 test_bit(HCI_PSCAN, &hdev->flags)) {
1469 u8 scan = 0x00;
1470 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1471 }
1472
Arman Uguray912098a2015-03-23 15:57:15 -07001473 if (hdev->adv_instance.timeout)
1474 clear_adv_instance(hdev);
1475
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001476 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedberg8b064a32014-02-24 14:52:22 +02001477 disable_advertising(&req);
1478
Johan Hedberg23a48092014-07-08 16:05:06 +03001479 discov_stopped = hci_stop_discovery(&req);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001480
1481 list_for_each_entry(conn, &hdev->conn_hash.list, list) {
1482 struct hci_cp_disconnect dc;
Johan Hedbergc9910d02014-02-27 14:35:12 +02001483 struct hci_cp_reject_conn_req rej;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001484
Johan Hedbergc9910d02014-02-27 14:35:12 +02001485 switch (conn->state) {
1486 case BT_CONNECTED:
1487 case BT_CONFIG:
1488 dc.handle = cpu_to_le16(conn->handle);
1489 dc.reason = 0x15; /* Terminated due to Power Off */
1490 hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc);
1491 break;
1492 case BT_CONNECT:
1493 if (conn->type == LE_LINK)
1494 hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL,
1495 0, NULL);
1496 else if (conn->type == ACL_LINK)
1497 hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL,
1498 6, &conn->dst);
1499 break;
1500 case BT_CONNECT2:
1501 bacpy(&rej.bdaddr, &conn->dst);
1502 rej.reason = 0x15; /* Terminated due to Power Off */
1503 if (conn->type == ACL_LINK)
1504 hci_req_add(&req, HCI_OP_REJECT_CONN_REQ,
1505 sizeof(rej), &rej);
1506 else if (conn->type == SCO_LINK)
1507 hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ,
1508 sizeof(rej), &rej);
1509 break;
1510 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001511 }
1512
Johan Hedberg23a48092014-07-08 16:05:06 +03001513 err = hci_req_run(&req, clean_up_hci_complete);
1514 if (!err && discov_stopped)
1515 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
1516
1517 return err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001518}
1519
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001520static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001521 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001522{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001523 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001524 struct mgmt_pending_cmd *cmd;
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001525 int err;
Johan Hedberg03811012010-12-08 00:21:06 +02001526
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001527 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001528
Johan Hedberga7e80f22013-01-09 16:05:19 +02001529 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001530 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1531 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001532
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001533 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001534
Johan Hedberg333ae952015-03-17 13:48:47 +02001535 if (pending_find(MGMT_OP_SET_POWERED, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001536 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1537 MGMT_STATUS_BUSY);
Johan Hedberg87b95ba2013-09-25 13:26:06 +03001538 goto failed;
1539 }
1540
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001541 if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
Marcel Holtmannf0d4b782012-02-21 12:14:25 +01001542 cancel_delayed_work(&hdev->power_off);
1543
1544 if (cp->val) {
Johan Hedberga1d70452013-01-09 15:29:40 +02001545 mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev,
1546 data, len);
1547 err = mgmt_powered(hdev, 1);
Marcel Holtmannf0d4b782012-02-21 12:14:25 +01001548 goto failed;
1549 }
1550 }
1551
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001552 if (!!cp->val == hdev_is_powered(hdev)) {
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001553 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001554 goto failed;
1555 }
1556
Johan Hedberg03811012010-12-08 00:21:06 +02001557 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
1558 if (!cmd) {
1559 err = -ENOMEM;
1560 goto failed;
1561 }
1562
Johan Hedberg8b064a32014-02-24 14:52:22 +02001563 if (cp->val) {
Johan Hedberg19202572013-01-14 22:33:51 +02001564 queue_work(hdev->req_workqueue, &hdev->power_on);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001565 err = 0;
1566 } else {
1567 /* Disconnect connections, stop scans, etc */
1568 err = clean_up_hci_state(hdev);
Johan Hedberga3172b72014-02-28 09:33:44 +02001569 if (!err)
1570 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
1571 HCI_POWER_OFF_TIMEOUT);
Johan Hedberg03811012010-12-08 00:21:06 +02001572
Johan Hedberg8b064a32014-02-24 14:52:22 +02001573 /* ENODATA means there were no HCI commands queued */
1574 if (err == -ENODATA) {
Johan Hedberga3172b72014-02-28 09:33:44 +02001575 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001576 queue_work(hdev->req_workqueue, &hdev->power_off.work);
1577 err = 0;
1578 }
1579 }
Johan Hedberg03811012010-12-08 00:21:06 +02001580
1581failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001582 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001583 return err;
1584}
1585
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001586static int new_settings(struct hci_dev *hdev, struct sock *skip)
1587{
Marcel Holtmannf6b77122015-03-14 19:28:05 -07001588 __le32 ev = cpu_to_le32(get_current_settings(hdev));
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001589
Marcel Holtmannf6b77122015-03-14 19:28:05 -07001590 return mgmt_generic_event(MGMT_EV_NEW_SETTINGS, hdev, &ev,
1591 sizeof(ev), skip);
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001592}
1593
Johan Hedberg91a668b2014-07-09 13:28:26 +03001594int mgmt_new_settings(struct hci_dev *hdev)
1595{
1596 return new_settings(hdev, NULL);
1597}
1598
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001599struct cmd_lookup {
1600 struct sock *sk;
1601 struct hci_dev *hdev;
1602 u8 mgmt_status;
1603};
1604
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001605static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001606{
1607 struct cmd_lookup *match = data;
1608
1609 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
1610
1611 list_del(&cmd->list);
1612
1613 if (match->sk == NULL) {
1614 match->sk = cmd->sk;
1615 sock_hold(match->sk);
1616 }
1617
1618 mgmt_pending_free(cmd);
1619}
1620
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001621static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001622{
1623 u8 *status = data;
1624
Johan Hedberga69e8372015-03-06 21:08:53 +02001625 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001626 mgmt_pending_remove(cmd);
1627}
1628
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001629static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg1b9b5ee2014-12-05 13:36:00 +02001630{
1631 if (cmd->cmd_complete) {
1632 u8 *status = data;
1633
1634 cmd->cmd_complete(cmd, *status);
1635 mgmt_pending_remove(cmd);
1636
1637 return;
1638 }
1639
1640 cmd_status_rsp(cmd, data);
1641}
1642
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001643static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedbergf5818c22014-12-05 13:36:02 +02001644{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001645 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1646 cmd->param, cmd->param_len);
Johan Hedbergf5818c22014-12-05 13:36:02 +02001647}
1648
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001649static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001650{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001651 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1652 cmd->param, sizeof(struct mgmt_addr_info));
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001653}
1654
Johan Hedberge6fe7982013-10-02 15:45:22 +03001655static u8 mgmt_bredr_support(struct hci_dev *hdev)
1656{
1657 if (!lmp_bredr_capable(hdev))
1658 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001659 else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001660 return MGMT_STATUS_REJECTED;
1661 else
1662 return MGMT_STATUS_SUCCESS;
1663}
1664
1665static u8 mgmt_le_support(struct hci_dev *hdev)
1666{
1667 if (!lmp_le_capable(hdev))
1668 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001669 else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001670 return MGMT_STATUS_REJECTED;
1671 else
1672 return MGMT_STATUS_SUCCESS;
1673}
1674
Marcel Holtmann1904a852015-01-11 13:50:44 -08001675static void set_discoverable_complete(struct hci_dev *hdev, u8 status,
1676 u16 opcode)
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001677{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001678 struct mgmt_pending_cmd *cmd;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001679 struct mgmt_mode *cp;
Marcel Holtmann970ba522013-10-15 06:33:57 -07001680 struct hci_request req;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001681 bool changed;
1682
1683 BT_DBG("status 0x%02x", status);
1684
1685 hci_dev_lock(hdev);
1686
Johan Hedberg333ae952015-03-17 13:48:47 +02001687 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001688 if (!cmd)
1689 goto unlock;
1690
1691 if (status) {
1692 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001693 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001694 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001695 goto remove_cmd;
1696 }
1697
1698 cp = cmd->param;
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001699 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001700 changed = !hci_dev_test_and_set_flag(hdev, HCI_DISCOVERABLE);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001701
1702 if (hdev->discov_timeout > 0) {
1703 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1704 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
1705 to);
1706 }
1707 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001708 changed = hci_dev_test_and_clear_flag(hdev, HCI_DISCOVERABLE);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001709 }
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001710
1711 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1712
1713 if (changed)
1714 new_settings(hdev, cmd->sk);
1715
Marcel Holtmann970ba522013-10-15 06:33:57 -07001716 /* When the discoverable mode gets changed, make sure
1717 * that class of device has the limited discoverable
Johan Hedberg432df052014-08-01 11:13:31 +03001718 * bit correctly set. Also update page scan based on whitelist
1719 * entries.
Marcel Holtmann970ba522013-10-15 06:33:57 -07001720 */
1721 hci_req_init(&req, hdev);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02001722 __hci_update_page_scan(&req);
Marcel Holtmann970ba522013-10-15 06:33:57 -07001723 update_class(&req);
1724 hci_req_run(&req, NULL);
1725
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001726remove_cmd:
1727 mgmt_pending_remove(cmd);
1728
1729unlock:
1730 hci_dev_unlock(hdev);
1731}
1732
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001733static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001734 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001735{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001736 struct mgmt_cp_set_discoverable *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001737 struct mgmt_pending_cmd *cmd;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001738 struct hci_request req;
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001739 u16 timeout;
Johan Hedberg9a43e252013-10-20 19:00:07 +03001740 u8 scan;
Johan Hedberg03811012010-12-08 00:21:06 +02001741 int err;
Johan Hedberge41d8b42010-12-13 21:07:03 +02001742
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001743 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001744
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001745 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1746 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001747 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1748 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001749
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001750 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02001751 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1752 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001753
Marcel Holtmann1f350c82012-03-12 20:31:08 -07001754 timeout = __le16_to_cpu(cp->timeout);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001755
1756 /* Disabling discoverable requires that no timeout is set,
1757 * and enabling limited discoverable requires a timeout.
1758 */
1759 if ((cp->val == 0x00 && timeout > 0) ||
1760 (cp->val == 0x02 && timeout == 0))
Johan Hedberga69e8372015-03-06 21:08:53 +02001761 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1762 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001763
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001764 hci_dev_lock(hdev);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001765
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001766 if (!hdev_is_powered(hdev) && timeout > 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001767 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1768 MGMT_STATUS_NOT_POWERED);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001769 goto failed;
1770 }
1771
Johan Hedberg333ae952015-03-17 13:48:47 +02001772 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1773 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001774 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1775 MGMT_STATUS_BUSY);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001776 goto failed;
1777 }
1778
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001779 if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001780 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1781 MGMT_STATUS_REJECTED);
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001782 goto failed;
1783 }
1784
1785 if (!hdev_is_powered(hdev)) {
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001786 bool changed = false;
1787
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001788 /* Setting limited discoverable when powered off is
1789 * not a valid operation since it requires a timeout
1790 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1791 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001792 if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07001793 hci_dev_change_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001794 changed = true;
1795 }
1796
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001797 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001798 if (err < 0)
1799 goto failed;
1800
1801 if (changed)
1802 err = new_settings(hdev, sk);
1803
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001804 goto failed;
1805 }
1806
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001807 /* If the current mode is the same, then just update the timeout
1808 * value with the new value. And if only the timeout gets updated,
1809 * then no need for any HCI transactions.
1810 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001811 if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1812 (cp->val == 0x02) == hci_dev_test_flag(hdev,
1813 HCI_LIMITED_DISCOVERABLE)) {
Marcel Holtmann36261542013-10-15 08:28:51 -07001814 cancel_delayed_work(&hdev->discov_off);
1815 hdev->discov_timeout = timeout;
Marcel Holtmann955638e2012-02-22 18:21:00 +01001816
Marcel Holtmann36261542013-10-15 08:28:51 -07001817 if (cp->val && hdev->discov_timeout > 0) {
1818 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001819 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
Marcel Holtmann36261542013-10-15 08:28:51 -07001820 to);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001821 }
1822
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001823 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001824 goto failed;
1825 }
1826
1827 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
1828 if (!cmd) {
1829 err = -ENOMEM;
1830 goto failed;
1831 }
1832
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001833 /* Cancel any potential discoverable timeout that might be
1834 * still active and store new timeout value. The arming of
1835 * the timeout happens in the complete handler.
1836 */
1837 cancel_delayed_work(&hdev->discov_off);
1838 hdev->discov_timeout = timeout;
1839
Johan Hedbergb456f872013-10-19 23:38:22 +03001840 /* Limited discoverable mode */
1841 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001842 hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001843 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001844 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001845
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001846 hci_req_init(&req, hdev);
1847
Johan Hedberg9a43e252013-10-20 19:00:07 +03001848 /* The procedure for LE-only controllers is much simpler - just
1849 * update the advertising data.
1850 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001851 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberg9a43e252013-10-20 19:00:07 +03001852 goto update_ad;
1853
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001854 scan = SCAN_PAGE;
1855
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001856 if (cp->val) {
1857 struct hci_cp_write_current_iac_lap hci_cp;
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001858
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001859 if (cp->val == 0x02) {
1860 /* Limited discoverable mode */
Marcel Holtmann33337dc2013-10-23 08:28:01 -07001861 hci_cp.num_iac = min_t(u8, hdev->num_iac, 2);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001862 hci_cp.iac_lap[0] = 0x00; /* LIAC */
1863 hci_cp.iac_lap[1] = 0x8b;
1864 hci_cp.iac_lap[2] = 0x9e;
1865 hci_cp.iac_lap[3] = 0x33; /* GIAC */
1866 hci_cp.iac_lap[4] = 0x8b;
1867 hci_cp.iac_lap[5] = 0x9e;
1868 } else {
1869 /* General discoverable mode */
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001870 hci_cp.num_iac = 1;
1871 hci_cp.iac_lap[0] = 0x33; /* GIAC */
1872 hci_cp.iac_lap[1] = 0x8b;
1873 hci_cp.iac_lap[2] = 0x9e;
1874 }
1875
1876 hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP,
1877 (hci_cp.num_iac * 3) + 1, &hci_cp);
1878
1879 scan |= SCAN_INQUIRY;
1880 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001881 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001882 }
1883
1884 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001885
Johan Hedberg9a43e252013-10-20 19:00:07 +03001886update_ad:
1887 update_adv_data(&req);
1888
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001889 err = hci_req_run(&req, set_discoverable_complete);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001890 if (err < 0)
1891 mgmt_pending_remove(cmd);
1892
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001893failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001894 hci_dev_unlock(hdev);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001895 return err;
1896}
1897
Johan Hedberg406d7802013-03-15 17:07:09 -05001898static void write_fast_connectable(struct hci_request *req, bool enable)
1899{
Johan Hedbergbd98b992013-03-15 17:07:13 -05001900 struct hci_dev *hdev = req->hdev;
Johan Hedberg406d7802013-03-15 17:07:09 -05001901 struct hci_cp_write_page_scan_activity acp;
1902 u8 type;
1903
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001904 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberg547003b2013-10-21 16:51:53 +03001905 return;
1906
Johan Hedberg4c01f8b2013-03-15 17:07:14 -05001907 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
1908 return;
1909
Johan Hedberg406d7802013-03-15 17:07:09 -05001910 if (enable) {
1911 type = PAGE_SCAN_TYPE_INTERLACED;
1912
1913 /* 160 msec page scan interval */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001914 acp.interval = cpu_to_le16(0x0100);
Johan Hedberg406d7802013-03-15 17:07:09 -05001915 } else {
1916 type = PAGE_SCAN_TYPE_STANDARD; /* default */
1917
1918 /* default 1.28 sec page scan */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001919 acp.interval = cpu_to_le16(0x0800);
Johan Hedberg406d7802013-03-15 17:07:09 -05001920 }
1921
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001922 acp.window = cpu_to_le16(0x0012);
Johan Hedberg406d7802013-03-15 17:07:09 -05001923
Johan Hedbergbd98b992013-03-15 17:07:13 -05001924 if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval ||
1925 __cpu_to_le16(hdev->page_scan_window) != acp.window)
1926 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
1927 sizeof(acp), &acp);
1928
1929 if (hdev->page_scan_type != type)
1930 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type);
Johan Hedberg406d7802013-03-15 17:07:09 -05001931}
1932
Marcel Holtmann1904a852015-01-11 13:50:44 -08001933static void set_connectable_complete(struct hci_dev *hdev, u8 status,
1934 u16 opcode)
Johan Hedberg2b76f452013-03-15 17:07:04 -05001935{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001936 struct mgmt_pending_cmd *cmd;
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001937 struct mgmt_mode *cp;
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001938 bool conn_changed, discov_changed;
Johan Hedberg2b76f452013-03-15 17:07:04 -05001939
1940 BT_DBG("status 0x%02x", status);
1941
1942 hci_dev_lock(hdev);
1943
Johan Hedberg333ae952015-03-17 13:48:47 +02001944 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
Johan Hedberg2b76f452013-03-15 17:07:04 -05001945 if (!cmd)
1946 goto unlock;
1947
Johan Hedberg37438c12013-10-14 16:20:05 +03001948 if (status) {
1949 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001950 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg37438c12013-10-14 16:20:05 +03001951 goto remove_cmd;
1952 }
1953
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001954 cp = cmd->param;
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001955 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001956 conn_changed = !hci_dev_test_and_set_flag(hdev,
1957 HCI_CONNECTABLE);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001958 discov_changed = false;
1959 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001960 conn_changed = hci_dev_test_and_clear_flag(hdev,
1961 HCI_CONNECTABLE);
1962 discov_changed = hci_dev_test_and_clear_flag(hdev,
1963 HCI_DISCOVERABLE);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001964 }
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001965
Johan Hedberg2b76f452013-03-15 17:07:04 -05001966 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
1967
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001968 if (conn_changed || discov_changed) {
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001969 new_settings(hdev, cmd->sk);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02001970 hci_update_page_scan(hdev);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001971 if (discov_changed)
1972 mgmt_update_adv_data(hdev);
Johan Hedberg2b7be332014-07-07 14:40:22 +03001973 hci_update_background_scan(hdev);
1974 }
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001975
Johan Hedberg37438c12013-10-14 16:20:05 +03001976remove_cmd:
Johan Hedberg2b76f452013-03-15 17:07:04 -05001977 mgmt_pending_remove(cmd);
1978
1979unlock:
1980 hci_dev_unlock(hdev);
1981}
1982
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001983static int set_connectable_update_settings(struct hci_dev *hdev,
1984 struct sock *sk, u8 val)
1985{
1986 bool changed = false;
1987 int err;
1988
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001989 if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE))
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001990 changed = true;
1991
1992 if (val) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001993 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001994 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001995 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
1996 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001997 }
1998
1999 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
2000 if (err < 0)
2001 return err;
2002
Johan Hedberg562064e2014-07-08 16:35:34 +03002003 if (changed) {
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02002004 hci_update_page_scan(hdev);
Johan Hedberg562064e2014-07-08 16:35:34 +03002005 hci_update_background_scan(hdev);
Johan Hedberge8ba3a12013-10-19 23:38:18 +03002006 return new_settings(hdev, sk);
Johan Hedberg562064e2014-07-08 16:35:34 +03002007 }
Johan Hedberge8ba3a12013-10-19 23:38:18 +03002008
2009 return 0;
2010}
2011
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002012static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002013 u16 len)
Johan Hedbergf7b64e692010-12-13 21:07:06 +02002014{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002015 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002016 struct mgmt_pending_cmd *cmd;
Johan Hedberg2b76f452013-03-15 17:07:04 -05002017 struct hci_request req;
Johan Hedberg1987fdc2013-10-14 21:15:24 +03002018 u8 scan;
Johan Hedbergf7b64e692010-12-13 21:07:06 +02002019 int err;
Johan Hedberg03811012010-12-08 00:21:06 +02002020
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002021 BT_DBG("request for %s", hdev->name);
Johan Hedberge41d8b42010-12-13 21:07:03 +02002022
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002023 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
2024 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02002025 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
2026 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03002027
Johan Hedberga7e80f22013-01-09 16:05:19 +02002028 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002029 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
2030 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002031
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002032 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002033
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002034 if (!hdev_is_powered(hdev)) {
Johan Hedberge8ba3a12013-10-19 23:38:18 +03002035 err = set_connectable_update_settings(hdev, sk, cp->val);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002036 goto failed;
2037 }
2038
Johan Hedberg333ae952015-03-17 13:48:47 +02002039 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
2040 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002041 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
2042 MGMT_STATUS_BUSY);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002043 goto failed;
2044 }
2045
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002046 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
2047 if (!cmd) {
2048 err = -ENOMEM;
2049 goto failed;
2050 }
2051
Johan Hedberg2b76f452013-03-15 17:07:04 -05002052 hci_req_init(&req, hdev);
2053
Johan Hedberg9a43e252013-10-20 19:00:07 +03002054 /* If BR/EDR is not enabled and we disable advertising as a
2055 * by-product of disabling connectable, we need to update the
2056 * advertising flags.
2057 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002058 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg9a43e252013-10-20 19:00:07 +03002059 if (!cp->val) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002060 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2061 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberg9a43e252013-10-20 19:00:07 +03002062 }
2063 update_adv_data(&req);
2064 } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) {
Johan Hedberg9b742462013-10-14 16:20:03 +03002065 if (cp->val) {
2066 scan = SCAN_PAGE;
2067 } else {
Johan Hedberg3bd27242014-07-28 20:53:58 +03002068 /* If we don't have any whitelist entries just
2069 * disable all scanning. If there are entries
2070 * and we had both page and inquiry scanning
2071 * enabled then fall back to only page scanning.
2072 * Otherwise no changes are needed.
2073 */
2074 if (list_empty(&hdev->whitelist))
2075 scan = SCAN_DISABLED;
2076 else if (test_bit(HCI_ISCAN, &hdev->flags))
2077 scan = SCAN_PAGE;
2078 else
2079 goto no_scan_update;
Johan Hedberg9b742462013-10-14 16:20:03 +03002080
2081 if (test_bit(HCI_ISCAN, &hdev->flags) &&
Marcel Holtmann8d6083f2013-10-14 16:38:45 -07002082 hdev->discov_timeout > 0)
Johan Hedberg9b742462013-10-14 16:20:03 +03002083 cancel_delayed_work(&hdev->discov_off);
2084 }
2085
2086 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
2087 }
Johan Hedberg2b76f452013-03-15 17:07:04 -05002088
Johan Hedberg3bd27242014-07-28 20:53:58 +03002089no_scan_update:
Johan Hedberge8b12022014-07-10 10:51:27 +03002090 /* Update the advertising parameters if necessary */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002091 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedberg1987fdc2013-10-14 21:15:24 +03002092 enable_advertising(&req);
Johan Hedberg1987fdc2013-10-14 21:15:24 +03002093
Johan Hedberg2b76f452013-03-15 17:07:04 -05002094 err = hci_req_run(&req, set_connectable_complete);
Johan Hedberg9b742462013-10-14 16:20:03 +03002095 if (err < 0) {
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002096 mgmt_pending_remove(cmd);
Johan Hedberg9b742462013-10-14 16:20:03 +03002097 if (err == -ENODATA)
Johan Hedberga81070b2013-10-19 23:38:19 +03002098 err = set_connectable_update_settings(hdev, sk,
2099 cp->val);
Johan Hedberg9b742462013-10-14 16:20:03 +03002100 goto failed;
2101 }
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002102
2103failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002104 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002105 return err;
2106}
2107
Johan Hedbergb2939472014-07-30 09:22:23 +03002108static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002109 u16 len)
Johan Hedberg73f22f62010-12-29 16:00:25 +02002110{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002111 struct mgmt_mode *cp = data;
Marcel Holtmann55594352013-10-06 16:11:57 -07002112 bool changed;
Johan Hedberg73f22f62010-12-29 16:00:25 +02002113 int err;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002114
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002115 BT_DBG("request for %s", hdev->name);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002116
Johan Hedberga7e80f22013-01-09 16:05:19 +02002117 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002118 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE,
2119 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002120
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002121 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002122
2123 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07002124 changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002125 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002126 changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002127
Johan Hedbergb2939472014-07-30 09:22:23 +03002128 err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002129 if (err < 0)
Marcel Holtmann55594352013-10-06 16:11:57 -07002130 goto unlock;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002131
Marcel Holtmann55594352013-10-06 16:11:57 -07002132 if (changed)
2133 err = new_settings(hdev, sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002134
Marcel Holtmann55594352013-10-06 16:11:57 -07002135unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002136 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002137 return err;
2138}
Johan Hedberg72a734e2010-12-30 00:38:22 +02002139
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002140static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
2141 u16 len)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002142{
2143 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002144 struct mgmt_pending_cmd *cmd;
Johan Hedberge6fe7982013-10-02 15:45:22 +03002145 u8 val, status;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002146 int err;
2147
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002148 BT_DBG("request for %s", hdev->name);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002149
Johan Hedberge6fe7982013-10-02 15:45:22 +03002150 status = mgmt_bredr_support(hdev);
2151 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02002152 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2153 status);
Johan Hedberg33c525c2012-10-24 21:11:58 +03002154
Johan Hedberga7e80f22013-01-09 16:05:19 +02002155 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002156 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2157 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002158
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002159 hci_dev_lock(hdev);
2160
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002161 if (!hdev_is_powered(hdev)) {
Johan Hedberg47990ea2012-02-22 11:58:37 +02002162 bool changed = false;
2163
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002164 if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07002165 hci_dev_change_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02002166 changed = true;
2167 }
2168
2169 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
2170 if (err < 0)
2171 goto failed;
2172
2173 if (changed)
2174 err = new_settings(hdev, sk);
2175
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002176 goto failed;
2177 }
2178
Johan Hedberg333ae952015-03-17 13:48:47 +02002179 if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002180 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2181 MGMT_STATUS_BUSY);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002182 goto failed;
2183 }
2184
2185 val = !!cp->val;
2186
2187 if (test_bit(HCI_AUTH, &hdev->flags) == val) {
2188 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
2189 goto failed;
2190 }
2191
2192 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
2193 if (!cmd) {
2194 err = -ENOMEM;
2195 goto failed;
2196 }
2197
2198 err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
2199 if (err < 0) {
2200 mgmt_pending_remove(cmd);
2201 goto failed;
2202 }
2203
2204failed:
2205 hci_dev_unlock(hdev);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002206 return err;
2207}
2208
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002209static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002210{
2211 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002212 struct mgmt_pending_cmd *cmd;
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07002213 u8 status;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002214 int err;
2215
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002216 BT_DBG("request for %s", hdev->name);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002217
Marcel Holtmanncdba5282013-10-02 21:31:52 -07002218 status = mgmt_bredr_support(hdev);
2219 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02002220 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
Marcel Holtmanncdba5282013-10-02 21:31:52 -07002221
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002222 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002223 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2224 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002225
Johan Hedberga7e80f22013-01-09 16:05:19 +02002226 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002227 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2228 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002229
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002230 hci_dev_lock(hdev);
Johan Hedberg6c8f12c2012-02-22 16:35:26 +02002231
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002232 if (!hdev_is_powered(hdev)) {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002233 bool changed;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02002234
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002235 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07002236 changed = !hci_dev_test_and_set_flag(hdev,
2237 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002238 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002239 changed = hci_dev_test_and_clear_flag(hdev,
2240 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002241 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002242 changed = hci_dev_test_and_clear_flag(hdev,
2243 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002244 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002245 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02002246 }
2247
2248 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2249 if (err < 0)
2250 goto failed;
2251
2252 if (changed)
2253 err = new_settings(hdev, sk);
2254
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002255 goto failed;
2256 }
2257
Johan Hedberg333ae952015-03-17 13:48:47 +02002258 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002259 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2260 MGMT_STATUS_BUSY);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002261 goto failed;
2262 }
2263
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002264 if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002265 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2266 goto failed;
2267 }
2268
2269 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
2270 if (!cmd) {
2271 err = -ENOMEM;
2272 goto failed;
2273 }
2274
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002275 if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03002276 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2277 sizeof(cp->val), &cp->val);
2278
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07002279 err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002280 if (err < 0) {
2281 mgmt_pending_remove(cmd);
2282 goto failed;
2283 }
2284
2285failed:
2286 hci_dev_unlock(hdev);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002287 return err;
2288}
2289
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002290static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002291{
2292 struct mgmt_mode *cp = data;
Marcel Holtmannee392692013-10-01 22:59:23 -07002293 bool changed;
Johan Hedberge6fe7982013-10-02 15:45:22 +03002294 u8 status;
Marcel Holtmannee392692013-10-01 22:59:23 -07002295 int err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002296
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002297 BT_DBG("request for %s", hdev->name);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002298
Johan Hedberge6fe7982013-10-02 15:45:22 +03002299 status = mgmt_bredr_support(hdev);
2300 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02002301 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002302
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002303 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002304 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2305 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002306
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002307 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02002308 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2309 MGMT_STATUS_REJECTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002310
Johan Hedberga7e80f22013-01-09 16:05:19 +02002311 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002312 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2313 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002314
Marcel Holtmannee392692013-10-01 22:59:23 -07002315 hci_dev_lock(hdev);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002316
Johan Hedberg333ae952015-03-17 13:48:47 +02002317 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002318 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2319 MGMT_STATUS_BUSY);
Johan Hedberga2cb01d2015-02-19 17:38:07 +02002320 goto unlock;
2321 }
2322
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002323 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07002324 changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002325 } else {
2326 if (hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002327 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2328 MGMT_STATUS_REJECTED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002329 goto unlock;
2330 }
2331
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002332 changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002333 }
Marcel Holtmannee392692013-10-01 22:59:23 -07002334
2335 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
2336 if (err < 0)
2337 goto unlock;
2338
2339 if (changed)
2340 err = new_settings(hdev, sk);
2341
2342unlock:
2343 hci_dev_unlock(hdev);
2344 return err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002345}
2346
Marcel Holtmann1904a852015-01-11 13:50:44 -08002347static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002348{
2349 struct cmd_lookup match = { NULL, hdev };
2350
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302351 hci_dev_lock(hdev);
2352
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002353 if (status) {
2354 u8 mgmt_err = mgmt_status(status);
2355
2356 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
2357 &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302358 goto unlock;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002359 }
2360
2361 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
2362
2363 new_settings(hdev, match.sk);
2364
2365 if (match.sk)
2366 sock_put(match.sk);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002367
2368 /* Make sure the controller has a good default for
2369 * advertising data. Restrict the update to when LE
2370 * has actually been enabled. During power on, the
2371 * update in powered_update_hci will take care of it.
2372 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002373 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002374 struct hci_request req;
2375
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002376 hci_req_init(&req, hdev);
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07002377 update_adv_data(&req);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07002378 update_scan_rsp_data(&req);
Johan Hedberg2cf22212014-12-19 22:26:00 +02002379 __hci_update_background_scan(&req);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002380 hci_req_run(&req, NULL);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002381 }
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302382
2383unlock:
2384 hci_dev_unlock(hdev);
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002385}
2386
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002387static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg06199cf2012-02-22 16:37:11 +02002388{
2389 struct mgmt_mode *cp = data;
2390 struct hci_cp_write_le_host_supported hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002391 struct mgmt_pending_cmd *cmd;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002392 struct hci_request req;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002393 int err;
Johan Hedberg0b60eba2012-02-28 00:57:24 +02002394 u8 val, enabled;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002395
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002396 BT_DBG("request for %s", hdev->name);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002397
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002398 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002399 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2400 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg1de028c2012-02-29 19:55:35 -08002401
Johan Hedberga7e80f22013-01-09 16:05:19 +02002402 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002403 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2404 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002405
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07002406 /* Bluetooth single mode LE only controllers or dual-mode
2407 * controllers configured as LE only devices, do not allow
2408 * switching LE off. These have either LE enabled explicitly
2409 * or BR/EDR has been previously switched off.
2410 *
2411 * When trying to enable an already enabled LE, then gracefully
2412 * send a positive response. Trying to disable it however will
2413 * result into rejection.
2414 */
2415 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
2416 if (cp->val == 0x01)
2417 return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2418
Johan Hedberga69e8372015-03-06 21:08:53 +02002419 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2420 MGMT_STATUS_REJECTED);
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07002421 }
Johan Hedbergc73eee92013-04-19 18:35:21 +03002422
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002423 hci_dev_lock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002424
2425 val = !!cp->val;
Gustavo Padovanffa88e02012-11-23 16:50:51 -02002426 enabled = lmp_host_le_capable(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002427
Johan Hedberg0b60eba2012-02-28 00:57:24 +02002428 if (!hdev_is_powered(hdev) || val == enabled) {
Johan Hedberg06199cf2012-02-22 16:37:11 +02002429 bool changed = false;
2430
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002431 if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07002432 hci_dev_change_flag(hdev, HCI_LE_ENABLED);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002433 changed = true;
2434 }
2435
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002436 if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002437 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03002438 changed = true;
2439 }
2440
Johan Hedberg06199cf2012-02-22 16:37:11 +02002441 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2442 if (err < 0)
Johan Hedberg1de028c2012-02-29 19:55:35 -08002443 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002444
2445 if (changed)
2446 err = new_settings(hdev, sk);
2447
Johan Hedberg1de028c2012-02-29 19:55:35 -08002448 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002449 }
2450
Johan Hedberg333ae952015-03-17 13:48:47 +02002451 if (pending_find(MGMT_OP_SET_LE, hdev) ||
2452 pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002453 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2454 MGMT_STATUS_BUSY);
Johan Hedberg1de028c2012-02-29 19:55:35 -08002455 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002456 }
2457
2458 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
2459 if (!cmd) {
2460 err = -ENOMEM;
Johan Hedberg1de028c2012-02-29 19:55:35 -08002461 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002462 }
2463
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002464 hci_req_init(&req, hdev);
2465
Johan Hedberg06199cf2012-02-22 16:37:11 +02002466 memset(&hci_cp, 0, sizeof(hci_cp));
2467
2468 if (val) {
2469 hci_cp.le = val;
Marcel Holtmann32226e42014-07-24 20:04:16 +02002470 hci_cp.simul = 0x00;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002471 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002472 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002473 disable_advertising(&req);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002474 }
2475
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002476 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp),
2477 &hci_cp);
2478
2479 err = hci_req_run(&req, le_enable_complete);
Syam Sidhardhan0c01bc42012-04-12 20:33:21 +05302480 if (err < 0)
Johan Hedberg06199cf2012-02-22 16:37:11 +02002481 mgmt_pending_remove(cmd);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002482
Johan Hedberg1de028c2012-02-29 19:55:35 -08002483unlock:
2484 hci_dev_unlock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002485 return err;
2486}
2487
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002488/* This is a helper function to test for pending mgmt commands that can
2489 * cause CoD or EIR HCI commands. We can only allow one such pending
2490 * mgmt command at a time since otherwise we cannot easily track what
2491 * the current values are, will be, and based on that calculate if a new
2492 * HCI command needs to be sent and if yes with what value.
2493 */
2494static bool pending_eir_or_class(struct hci_dev *hdev)
2495{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002496 struct mgmt_pending_cmd *cmd;
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002497
2498 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
2499 switch (cmd->opcode) {
2500 case MGMT_OP_ADD_UUID:
2501 case MGMT_OP_REMOVE_UUID:
2502 case MGMT_OP_SET_DEV_CLASS:
2503 case MGMT_OP_SET_POWERED:
2504 return true;
2505 }
2506 }
2507
2508 return false;
2509}
2510
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002511static const u8 bluetooth_base_uuid[] = {
2512 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
2513 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2514};
2515
2516static u8 get_uuid_size(const u8 *uuid)
2517{
2518 u32 val;
2519
2520 if (memcmp(uuid, bluetooth_base_uuid, 12))
2521 return 128;
2522
2523 val = get_unaligned_le32(&uuid[12]);
2524 if (val > 0xffff)
2525 return 32;
2526
2527 return 16;
2528}
2529
Johan Hedberg92da6092013-03-15 17:06:55 -05002530static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status)
2531{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002532 struct mgmt_pending_cmd *cmd;
Johan Hedberg92da6092013-03-15 17:06:55 -05002533
2534 hci_dev_lock(hdev);
2535
Johan Hedberg333ae952015-03-17 13:48:47 +02002536 cmd = pending_find(mgmt_op, hdev);
Johan Hedberg92da6092013-03-15 17:06:55 -05002537 if (!cmd)
2538 goto unlock;
2539
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002540 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
2541 mgmt_status(status), hdev->dev_class, 3);
Johan Hedberg92da6092013-03-15 17:06:55 -05002542
2543 mgmt_pending_remove(cmd);
2544
2545unlock:
2546 hci_dev_unlock(hdev);
2547}
2548
Marcel Holtmann1904a852015-01-11 13:50:44 -08002549static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002550{
2551 BT_DBG("status 0x%02x", status);
2552
2553 mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status);
2554}
2555
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002556static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002557{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002558 struct mgmt_cp_add_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002559 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002560 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002561 struct bt_uuid *uuid;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002562 int err;
2563
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002564 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002565
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002566 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002567
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002568 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002569 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
2570 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002571 goto failed;
2572 }
2573
Andre Guedes92c4c202012-06-07 19:05:44 -03002574 uuid = kmalloc(sizeof(*uuid), GFP_KERNEL);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002575 if (!uuid) {
2576 err = -ENOMEM;
2577 goto failed;
2578 }
2579
2580 memcpy(uuid->uuid, cp->uuid, 16);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002581 uuid->svc_hint = cp->svc_hint;
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002582 uuid->size = get_uuid_size(cp->uuid);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002583
Johan Hedbergde66aa62013-01-27 00:31:27 +02002584 list_add_tail(&uuid->list, &hdev->uuids);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002585
Johan Hedberg890ea892013-03-15 17:06:52 -05002586 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002587
Johan Hedberg890ea892013-03-15 17:06:52 -05002588 update_class(&req);
2589 update_eir(&req);
2590
Johan Hedberg92da6092013-03-15 17:06:55 -05002591 err = hci_req_run(&req, add_uuid_complete);
2592 if (err < 0) {
2593 if (err != -ENODATA)
2594 goto failed;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002595
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002596 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
2597 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002598 goto failed;
2599 }
2600
2601 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002602 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002603 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002604 goto failed;
2605 }
2606
2607 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002608
2609failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002610 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002611 return err;
2612}
2613
Johan Hedberg24b78d02012-02-23 23:24:30 +02002614static bool enable_service_cache(struct hci_dev *hdev)
2615{
2616 if (!hdev_is_powered(hdev))
2617 return false;
2618
Marcel Holtmann238be782015-03-13 02:11:06 -07002619 if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg46818ed2013-01-14 22:33:52 +02002620 queue_delayed_work(hdev->workqueue, &hdev->service_cache,
2621 CACHE_TIMEOUT);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002622 return true;
2623 }
2624
2625 return false;
2626}
2627
Marcel Holtmann1904a852015-01-11 13:50:44 -08002628static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002629{
2630 BT_DBG("status 0x%02x", status);
2631
2632 mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status);
2633}
2634
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002635static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002636 u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002637{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002638 struct mgmt_cp_remove_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002639 struct mgmt_pending_cmd *cmd;
Johan Hedberg056341c2013-01-27 00:31:30 +02002640 struct bt_uuid *match, *tmp;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002641 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 -05002642 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002643 int err, found;
2644
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002645 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002646
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002647 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002648
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002649 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002650 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2651 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002652 goto unlock;
2653 }
2654
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002655 if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
Johan Hedberg35f74982014-02-18 17:14:32 +02002656 hci_uuids_clear(hdev);
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002657
Johan Hedberg24b78d02012-02-23 23:24:30 +02002658 if (enable_service_cache(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002659 err = mgmt_cmd_complete(sk, hdev->id,
2660 MGMT_OP_REMOVE_UUID,
2661 0, hdev->dev_class, 3);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002662 goto unlock;
2663 }
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002664
Johan Hedberg9246a862012-02-23 21:33:16 +02002665 goto update_class;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002666 }
2667
2668 found = 0;
2669
Johan Hedberg056341c2013-01-27 00:31:30 +02002670 list_for_each_entry_safe(match, tmp, &hdev->uuids, list) {
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002671 if (memcmp(match->uuid, cp->uuid, 16) != 0)
2672 continue;
2673
2674 list_del(&match->list);
Johan Hedberg482049f2012-11-08 10:25:26 +01002675 kfree(match);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002676 found++;
2677 }
2678
2679 if (found == 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002680 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2681 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002682 goto unlock;
2683 }
2684
Johan Hedberg9246a862012-02-23 21:33:16 +02002685update_class:
Johan Hedberg890ea892013-03-15 17:06:52 -05002686 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002687
Johan Hedberg890ea892013-03-15 17:06:52 -05002688 update_class(&req);
2689 update_eir(&req);
2690
Johan Hedberg92da6092013-03-15 17:06:55 -05002691 err = hci_req_run(&req, remove_uuid_complete);
2692 if (err < 0) {
2693 if (err != -ENODATA)
2694 goto unlock;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002695
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002696 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
2697 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002698 goto unlock;
2699 }
2700
2701 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002702 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002703 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002704 goto unlock;
2705 }
2706
2707 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002708
2709unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002710 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002711 return err;
2712}
2713
Marcel Holtmann1904a852015-01-11 13:50:44 -08002714static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002715{
2716 BT_DBG("status 0x%02x", status);
2717
2718 mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status);
2719}
2720
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002721static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002722 u16 len)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002723{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002724 struct mgmt_cp_set_dev_class *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002725 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002726 struct hci_request req;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002727 int err;
2728
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002729 BT_DBG("request for %s", hdev->name);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002730
Marcel Holtmann6203fc92013-10-02 23:37:29 -07002731 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002732 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2733 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002734
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002735 hci_dev_lock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002736
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002737 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002738 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2739 MGMT_STATUS_BUSY);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002740 goto unlock;
2741 }
2742
2743 if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002744 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2745 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002746 goto unlock;
2747 }
2748
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002749 hdev->major_class = cp->major;
2750 hdev->minor_class = cp->minor;
2751
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002752 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002753 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2754 hdev->dev_class, 3);
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002755 goto unlock;
2756 }
2757
Johan Hedberg890ea892013-03-15 17:06:52 -05002758 hci_req_init(&req, hdev);
2759
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002760 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg7d785252011-12-15 00:47:39 +02002761 hci_dev_unlock(hdev);
2762 cancel_delayed_work_sync(&hdev->service_cache);
2763 hci_dev_lock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -05002764 update_eir(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02002765 }
Johan Hedberg14c0b602011-12-15 00:47:37 +02002766
Johan Hedberg890ea892013-03-15 17:06:52 -05002767 update_class(&req);
2768
Johan Hedberg92da6092013-03-15 17:06:55 -05002769 err = hci_req_run(&req, set_class_complete);
2770 if (err < 0) {
2771 if (err != -ENODATA)
2772 goto unlock;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002773
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002774 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2775 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002776 goto unlock;
2777 }
2778
2779 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002780 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002781 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002782 goto unlock;
2783 }
2784
2785 err = 0;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002786
Johan Hedbergb5235a62012-02-21 14:32:24 +02002787unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002788 hci_dev_unlock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002789 return err;
2790}
2791
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002792static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002793 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002794{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002795 struct mgmt_cp_load_link_keys *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03002796 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
2797 sizeof(struct mgmt_link_key_info));
Szymon Janc4e51eae2011-02-25 19:05:48 +01002798 u16 key_count, expected_len;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002799 bool changed;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002800 int i;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002801
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002802 BT_DBG("request for %s", hdev->name);
2803
2804 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002805 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2806 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002807
Marcel Holtmann1f350c82012-03-12 20:31:08 -07002808 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002809 if (key_count > max_key_count) {
2810 BT_ERR("load_link_keys: too big key_count value %u",
2811 key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02002812 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2813 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002814 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002815
Johan Hedberg86742e12011-11-07 23:13:38 +02002816 expected_len = sizeof(*cp) + key_count *
2817 sizeof(struct mgmt_link_key_info);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002818 if (expected_len != len) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002819 BT_ERR("load_link_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02002820 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02002821 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2822 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002823 }
2824
Johan Hedberg4ae14302013-01-20 14:27:13 +02002825 if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002826 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2827 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ae14302013-01-20 14:27:13 +02002828
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002829 BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002830 key_count);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002831
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002832 for (i = 0; i < key_count; i++) {
2833 struct mgmt_link_key_info *key = &cp->keys[i];
2834
Marcel Holtmann8e991132014-01-10 02:07:25 -08002835 if (key->addr.type != BDADDR_BREDR || key->type > 0x08)
Johan Hedberga69e8372015-03-06 21:08:53 +02002836 return mgmt_cmd_status(sk, hdev->id,
2837 MGMT_OP_LOAD_LINK_KEYS,
2838 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002839 }
2840
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002841 hci_dev_lock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002842
2843 hci_link_keys_clear(hdev);
2844
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002845 if (cp->debug_keys)
Marcel Holtmann238be782015-03-13 02:11:06 -07002846 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002847 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002848 changed = hci_dev_test_and_clear_flag(hdev,
2849 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002850
2851 if (changed)
2852 new_settings(hdev, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002853
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002854 for (i = 0; i < key_count; i++) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002855 struct mgmt_link_key_info *key = &cp->keys[i];
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002856
Johan Hedberg58e92932014-06-24 14:00:26 +03002857 /* Always ignore debug keys and require a new pairing if
2858 * the user wants to use them.
2859 */
2860 if (key->type == HCI_LK_DEBUG_COMBINATION)
2861 continue;
2862
Johan Hedberg7652ff62014-06-24 13:15:49 +03002863 hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val,
2864 key->type, key->pin_len, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002865 }
2866
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002867 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
Johan Hedberg0e5f8752011-11-11 16:18:54 +02002868
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002869 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002870
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002871 return 0;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002872}
2873
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002874static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002875 u8 addr_type, struct sock *skip_sk)
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002876{
2877 struct mgmt_ev_device_unpaired ev;
2878
2879 bacpy(&ev.addr.bdaddr, bdaddr);
2880 ev.addr.type = addr_type;
2881
2882 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002883 skip_sk);
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002884}
2885
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002886static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002887 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002888{
Johan Hedberg124f6e32012-02-09 13:50:12 +02002889 struct mgmt_cp_unpair_device *cp = data;
2890 struct mgmt_rp_unpair_device rp;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002891 struct hci_cp_disconnect dc;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002892 struct mgmt_pending_cmd *cmd;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002893 struct hci_conn *conn;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002894 int err;
2895
Johan Hedberga8a1d192011-11-10 15:54:38 +02002896 memset(&rp, 0, sizeof(rp));
Johan Hedberg124f6e32012-02-09 13:50:12 +02002897 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2898 rp.addr.type = cp->addr.type;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002899
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002900 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002901 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2902 MGMT_STATUS_INVALID_PARAMS,
2903 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002904
Johan Hedberg118da702013-01-20 14:27:20 +02002905 if (cp->disconnect != 0x00 && cp->disconnect != 0x01)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002906 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2907 MGMT_STATUS_INVALID_PARAMS,
2908 &rp, sizeof(rp));
Johan Hedberg118da702013-01-20 14:27:20 +02002909
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002910 hci_dev_lock(hdev);
2911
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002912 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002913 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2914 MGMT_STATUS_NOT_POWERED, &rp,
2915 sizeof(rp));
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002916 goto unlock;
2917 }
2918
Johan Hedberge0b2b272014-02-18 17:14:31 +02002919 if (cp->addr.type == BDADDR_BREDR) {
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002920 /* If disconnection is requested, then look up the
2921 * connection. If the remote device is connected, it
2922 * will be later used to terminate the link.
2923 *
2924 * Setting it to NULL explicitly will cause no
2925 * termination of the link.
2926 */
2927 if (cp->disconnect)
2928 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2929 &cp->addr.bdaddr);
2930 else
2931 conn = NULL;
2932
Johan Hedberg124f6e32012-02-09 13:50:12 +02002933 err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
Johan Hedberge0b2b272014-02-18 17:14:31 +02002934 } else {
2935 u8 addr_type;
2936
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002937 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK,
2938 &cp->addr.bdaddr);
2939 if (conn) {
2940 /* Defer clearing up the connection parameters
2941 * until closing to give a chance of keeping
2942 * them if a repairing happens.
2943 */
2944 set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
2945
2946 /* If disconnection is not requested, then
2947 * clear the connection variable so that the
2948 * link is not terminated.
2949 */
2950 if (!cp->disconnect)
2951 conn = NULL;
2952 }
2953
Johan Hedberge0b2b272014-02-18 17:14:31 +02002954 if (cp->addr.type == BDADDR_LE_PUBLIC)
2955 addr_type = ADDR_LE_DEV_PUBLIC;
2956 else
2957 addr_type = ADDR_LE_DEV_RANDOM;
2958
Johan Hedberga7ec7332014-02-18 17:14:35 +02002959 hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type);
2960
Johan Hedberge0b2b272014-02-18 17:14:31 +02002961 err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type);
2962 }
Vinicius Costa Gomesb0dbfb42012-02-02 21:08:03 -03002963
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002964 if (err < 0) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002965 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2966 MGMT_STATUS_NOT_PAIRED, &rp,
2967 sizeof(rp));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002968 goto unlock;
2969 }
2970
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002971 /* If the connection variable is set, then termination of the
2972 * link is requested.
2973 */
Johan Hedberga8a1d192011-11-10 15:54:38 +02002974 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002975 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
2976 &rp, sizeof(rp));
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002977 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002978 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002979 }
2980
Johan Hedberg124f6e32012-02-09 13:50:12 +02002981 cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002982 sizeof(*cp));
Johan Hedberga8a1d192011-11-10 15:54:38 +02002983 if (!cmd) {
2984 err = -ENOMEM;
2985 goto unlock;
2986 }
2987
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02002988 cmd->cmd_complete = addr_cmd_complete;
2989
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02002990 dc.handle = cpu_to_le16(conn->handle);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002991 dc.reason = 0x13; /* Remote User Terminated Connection */
2992 err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc);
2993 if (err < 0)
2994 mgmt_pending_remove(cmd);
2995
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002996unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002997 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002998 return err;
2999}
3000
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003001static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003002 u16 len)
Johan Hedberg8962ee72011-01-20 12:40:27 +02003003{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003004 struct mgmt_cp_disconnect *cp = data;
Johan Hedberg06a63b12013-01-20 14:27:21 +02003005 struct mgmt_rp_disconnect rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003006 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02003007 struct hci_conn *conn;
Johan Hedberg8962ee72011-01-20 12:40:27 +02003008 int err;
3009
3010 BT_DBG("");
3011
Johan Hedberg06a63b12013-01-20 14:27:21 +02003012 memset(&rp, 0, sizeof(rp));
3013 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3014 rp.addr.type = cp->addr.type;
3015
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003016 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003017 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3018 MGMT_STATUS_INVALID_PARAMS,
3019 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003020
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003021 hci_dev_lock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003022
3023 if (!test_bit(HCI_UP, &hdev->flags)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003024 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3025 MGMT_STATUS_NOT_POWERED, &rp,
3026 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02003027 goto failed;
3028 }
3029
Johan Hedberg333ae952015-03-17 13:48:47 +02003030 if (pending_find(MGMT_OP_DISCONNECT, hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003031 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3032 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02003033 goto failed;
3034 }
3035
Andre Guedes591f47f2012-04-24 21:02:49 -03003036 if (cp->addr.type == BDADDR_BREDR)
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003037 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
3038 &cp->addr.bdaddr);
Johan Hedberg88c3df12012-02-09 14:27:38 +02003039 else
3040 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
Vinicius Costa Gomes365227e2011-05-06 18:41:44 -03003041
Vishal Agarwalf9607272012-06-13 05:32:43 +05303042 if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003043 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3044 MGMT_STATUS_NOT_CONNECTED, &rp,
3045 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02003046 goto failed;
3047 }
3048
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003049 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03003050 if (!cmd) {
3051 err = -ENOMEM;
Johan Hedberg8962ee72011-01-20 12:40:27 +02003052 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03003053 }
Johan Hedberg8962ee72011-01-20 12:40:27 +02003054
Johan Hedbergf5818c22014-12-05 13:36:02 +02003055 cmd->cmd_complete = generic_cmd_complete;
3056
Johan Hedberge3f2f922014-08-18 20:33:33 +03003057 err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003058 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03003059 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003060
3061failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003062 hci_dev_unlock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003063 return err;
3064}
3065
Andre Guedes57c14772012-04-24 21:02:50 -03003066static u8 link_to_bdaddr(u8 link_type, u8 addr_type)
Johan Hedberg4c659c32011-11-07 23:13:39 +02003067{
3068 switch (link_type) {
3069 case LE_LINK:
Johan Hedberg48264f02011-11-09 13:58:58 +02003070 switch (addr_type) {
3071 case ADDR_LE_DEV_PUBLIC:
Andre Guedes591f47f2012-04-24 21:02:49 -03003072 return BDADDR_LE_PUBLIC;
Andre Guedes0ed09142012-04-03 08:46:54 -03003073
Johan Hedberg48264f02011-11-09 13:58:58 +02003074 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03003075 /* Fallback to LE Random address type */
Andre Guedes591f47f2012-04-24 21:02:49 -03003076 return BDADDR_LE_RANDOM;
Johan Hedberg48264f02011-11-09 13:58:58 +02003077 }
Andre Guedes0ed09142012-04-03 08:46:54 -03003078
Johan Hedberg4c659c32011-11-07 23:13:39 +02003079 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03003080 /* Fallback to BR/EDR type */
Andre Guedes591f47f2012-04-24 21:02:49 -03003081 return BDADDR_BREDR;
Johan Hedberg4c659c32011-11-07 23:13:39 +02003082 }
3083}
3084
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003085static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
3086 u16 data_len)
Johan Hedberg2784eb42011-01-21 13:56:35 +02003087{
Johan Hedberg2784eb42011-01-21 13:56:35 +02003088 struct mgmt_rp_get_connections *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02003089 struct hci_conn *c;
Johan Hedberga38528f2011-01-22 06:46:43 +02003090 size_t rp_len;
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003091 int err;
3092 u16 i;
Johan Hedberg2784eb42011-01-21 13:56:35 +02003093
3094 BT_DBG("");
3095
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003096 hci_dev_lock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02003097
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003098 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003099 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
3100 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003101 goto unlock;
3102 }
3103
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003104 i = 0;
Johan Hedbergb644ba32012-01-17 21:48:47 +02003105 list_for_each_entry(c, &hdev->conn_hash.list, list) {
3106 if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003107 i++;
Johan Hedberg2784eb42011-01-21 13:56:35 +02003108 }
3109
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003110 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Andre Guedes92c4c202012-06-07 19:05:44 -03003111 rp = kmalloc(rp_len, GFP_KERNEL);
Johan Hedberga38528f2011-01-22 06:46:43 +02003112 if (!rp) {
Johan Hedberg2784eb42011-01-21 13:56:35 +02003113 err = -ENOMEM;
3114 goto unlock;
3115 }
3116
Johan Hedberg2784eb42011-01-21 13:56:35 +02003117 i = 0;
Johan Hedberg4c659c32011-11-07 23:13:39 +02003118 list_for_each_entry(c, &hdev->conn_hash.list, list) {
Johan Hedbergb644ba32012-01-17 21:48:47 +02003119 if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
3120 continue;
Johan Hedberg4c659c32011-11-07 23:13:39 +02003121 bacpy(&rp->addr[i].bdaddr, &c->dst);
Andre Guedes57c14772012-04-24 21:02:50 -03003122 rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type);
Andre Guedes0ed09142012-04-03 08:46:54 -03003123 if (c->type == SCO_LINK || c->type == ESCO_LINK)
Johan Hedberg4c659c32011-11-07 23:13:39 +02003124 continue;
3125 i++;
3126 }
3127
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02003128 rp->conn_count = cpu_to_le16(i);
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003129
Johan Hedberg4c659c32011-11-07 23:13:39 +02003130 /* Recalculate length in case of filtered SCO connections, etc */
3131 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Johan Hedberg2784eb42011-01-21 13:56:35 +02003132
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003133 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
3134 rp_len);
Johan Hedberg2784eb42011-01-21 13:56:35 +02003135
Johan Hedberga38528f2011-01-22 06:46:43 +02003136 kfree(rp);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003137
3138unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003139 hci_dev_unlock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02003140 return err;
3141}
3142
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003143static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003144 struct mgmt_cp_pin_code_neg_reply *cp)
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003145{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003146 struct mgmt_pending_cmd *cmd;
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003147 int err;
3148
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003149 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003150 sizeof(*cp));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003151 if (!cmd)
3152 return -ENOMEM;
3153
Johan Hedbergd8457692012-02-17 14:24:57 +02003154 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003155 sizeof(cp->addr.bdaddr), &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003156 if (err < 0)
3157 mgmt_pending_remove(cmd);
3158
3159 return err;
3160}
3161
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003162static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003163 u16 len)
Johan Hedberg980e1a52011-01-22 06:10:07 +02003164{
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003165 struct hci_conn *conn;
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003166 struct mgmt_cp_pin_code_reply *cp = data;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003167 struct hci_cp_pin_code_reply reply;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003168 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003169 int err;
3170
3171 BT_DBG("");
3172
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003173 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003174
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003175 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003176 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3177 MGMT_STATUS_NOT_POWERED);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003178 goto failed;
3179 }
3180
Johan Hedbergd8457692012-02-17 14:24:57 +02003181 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003182 if (!conn) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003183 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3184 MGMT_STATUS_NOT_CONNECTED);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003185 goto failed;
3186 }
3187
3188 if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) {
Johan Hedbergd8457692012-02-17 14:24:57 +02003189 struct mgmt_cp_pin_code_neg_reply ncp;
3190
3191 memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003192
3193 BT_ERR("PIN code is not 16 bytes long");
3194
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003195 err = send_pin_code_neg_reply(sk, hdev, &ncp);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003196 if (err >= 0)
Johan Hedberga69e8372015-03-06 21:08:53 +02003197 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3198 MGMT_STATUS_INVALID_PARAMS);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003199
3200 goto failed;
3201 }
3202
Gustavo F. Padovan00abfe42012-03-01 00:37:10 -03003203 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03003204 if (!cmd) {
3205 err = -ENOMEM;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003206 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03003207 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02003208
Johan Hedberg7776d1d2014-12-05 13:36:03 +02003209 cmd->cmd_complete = addr_cmd_complete;
3210
Johan Hedbergd8457692012-02-17 14:24:57 +02003211 bacpy(&reply.bdaddr, &cp->addr.bdaddr);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003212 reply.pin_len = cp->pin_len;
Waldemar Rymarkiewicz24718ca2011-06-01 17:28:47 +02003213 memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code));
Johan Hedberg980e1a52011-01-22 06:10:07 +02003214
3215 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
3216 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03003217 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003218
3219failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003220 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003221 return err;
3222}
3223
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003224static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
3225 u16 len)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003226{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003227 struct mgmt_cp_set_io_capability *cp = data;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003228
3229 BT_DBG("");
3230
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003231 if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003232 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY,
3233 MGMT_STATUS_INVALID_PARAMS, NULL, 0);
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003234
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003235 hci_dev_lock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003236
3237 hdev->io_capability = cp->io_capability;
3238
3239 BT_DBG("%s IO capability set to 0x%02x", hdev->name,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003240 hdev->io_capability);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003241
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003242 hci_dev_unlock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003243
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003244 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0,
3245 NULL, 0);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003246}
3247
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003248static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003249{
3250 struct hci_dev *hdev = conn->hdev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003251 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003252
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003253 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
Johan Hedberge9a416b2011-02-19 12:05:56 -03003254 if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
3255 continue;
3256
Johan Hedberge9a416b2011-02-19 12:05:56 -03003257 if (cmd->user_data != conn)
3258 continue;
3259
3260 return cmd;
3261 }
3262
3263 return NULL;
3264}
3265
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003266static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003267{
3268 struct mgmt_rp_pair_device rp;
3269 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9df74652014-12-19 22:26:03 +02003270 int err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003271
Johan Hedberg61b1a7f2014-03-20 12:54:16 +02003272 bacpy(&rp.addr.bdaddr, &conn->dst);
3273 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003274
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003275 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE,
3276 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003277
3278 /* So we don't get further callbacks for this connection */
3279 conn->connect_cfm_cb = NULL;
3280 conn->security_cfm_cb = NULL;
3281 conn->disconn_cfm_cb = NULL;
3282
David Herrmann76a68ba2013-04-06 20:28:37 +02003283 hci_conn_drop(conn);
Alfonso Acosta89cbb062014-10-11 21:44:47 +00003284
3285 /* The device is paired so there is no need to remove
3286 * its connection parameters anymore.
3287 */
3288 clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
Johan Hedberg15013ae2014-12-11 21:45:44 +02003289
3290 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02003291
3292 return err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003293}
3294
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003295void mgmt_smp_complete(struct hci_conn *conn, bool complete)
3296{
3297 u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003298 struct mgmt_pending_cmd *cmd;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003299
3300 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003301 if (cmd) {
Johan Hedberg04ab2742014-12-05 13:36:04 +02003302 cmd->cmd_complete(cmd, status);
Johan Hedberga511b352014-12-11 21:45:45 +02003303 mgmt_pending_remove(cmd);
3304 }
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003305}
3306
Johan Hedberge9a416b2011-02-19 12:05:56 -03003307static void pairing_complete_cb(struct hci_conn *conn, u8 status)
3308{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003309 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003310
3311 BT_DBG("status %u", status);
3312
Johan Hedberg56e5cb82011-11-08 20:40:16 +02003313 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003314 if (!cmd) {
Johan Hedberg56e5cb82011-11-08 20:40:16 +02003315 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02003316 return;
3317 }
3318
3319 cmd->cmd_complete(cmd, mgmt_status(status));
3320 mgmt_pending_remove(cmd);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003321}
3322
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003323static void le_pairing_complete_cb(struct hci_conn *conn, u8 status)
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303324{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003325 struct mgmt_pending_cmd *cmd;
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303326
3327 BT_DBG("status %u", status);
3328
3329 if (!status)
3330 return;
3331
3332 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003333 if (!cmd) {
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303334 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02003335 return;
3336 }
3337
3338 cmd->cmd_complete(cmd, mgmt_status(status));
3339 mgmt_pending_remove(cmd);
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303340}
3341
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003342static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003343 u16 len)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003344{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003345 struct mgmt_cp_pair_device *cp = data;
Johan Hedberg1425acb2011-11-11 00:07:35 +02003346 struct mgmt_rp_pair_device rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003347 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003348 u8 sec_level, auth_type;
3349 struct hci_conn *conn;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003350 int err;
3351
3352 BT_DBG("");
3353
Szymon Jancf950a30e2013-01-18 12:48:07 +01003354 memset(&rp, 0, sizeof(rp));
3355 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3356 rp.addr.type = cp->addr.type;
3357
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003358 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003359 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3360 MGMT_STATUS_INVALID_PARAMS,
3361 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003362
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003363 if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003364 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3365 MGMT_STATUS_INVALID_PARAMS,
3366 &rp, sizeof(rp));
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003367
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003368 hci_dev_lock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003369
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003370 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003371 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3372 MGMT_STATUS_NOT_POWERED, &rp,
3373 sizeof(rp));
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003374 goto unlock;
3375 }
3376
Johan Hedberg55e76b32015-03-10 22:34:40 +02003377 if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) {
3378 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3379 MGMT_STATUS_ALREADY_PAIRED, &rp,
3380 sizeof(rp));
3381 goto unlock;
3382 }
3383
Vinicius Costa Gomesc908df32011-09-02 14:51:22 -03003384 sec_level = BT_SECURITY_MEDIUM;
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003385 auth_type = HCI_AT_DEDICATED_BONDING;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003386
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003387 if (cp->addr.type == BDADDR_BREDR) {
Andre Guedes04a6c582014-02-26 20:21:44 -03003388 conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
3389 auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003390 } else {
3391 u8 addr_type;
3392
3393 /* Convert from L2CAP channel address type to HCI address type
3394 */
3395 if (cp->addr.type == BDADDR_LE_PUBLIC)
3396 addr_type = ADDR_LE_DEV_PUBLIC;
3397 else
3398 addr_type = ADDR_LE_DEV_RANDOM;
3399
Marcel Holtmann7c264b12014-06-30 12:34:40 +02003400 /* When pairing a new device, it is expected to remember
3401 * this device for future connections. Adding the connection
3402 * parameter information ahead of time allows tracking
3403 * of the slave preferred values and will speed up any
3404 * further connection establishment.
3405 *
3406 * If connection parameters already exist, then they
3407 * will be kept and this function does nothing.
3408 */
3409 hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type);
3410
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003411 conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type,
Johan Hedberge804d252014-07-16 11:42:28 +03003412 sec_level, HCI_LE_CONN_TIMEOUT,
3413 HCI_ROLE_MASTER);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003414 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003415
Ville Tervo30e76272011-02-22 16:10:53 -03003416 if (IS_ERR(conn)) {
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02003417 int status;
3418
3419 if (PTR_ERR(conn) == -EBUSY)
3420 status = MGMT_STATUS_BUSY;
Lukasz Rymanowskifaa81032015-02-11 12:31:42 +01003421 else if (PTR_ERR(conn) == -EOPNOTSUPP)
3422 status = MGMT_STATUS_NOT_SUPPORTED;
3423 else if (PTR_ERR(conn) == -ECONNREFUSED)
3424 status = MGMT_STATUS_REJECTED;
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02003425 else
3426 status = MGMT_STATUS_CONNECT_FAILED;
3427
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003428 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3429 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003430 goto unlock;
3431 }
3432
3433 if (conn->connect_cfm_cb) {
David Herrmann76a68ba2013-04-06 20:28:37 +02003434 hci_conn_drop(conn);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003435 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3436 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003437 goto unlock;
3438 }
3439
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003440 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003441 if (!cmd) {
3442 err = -ENOMEM;
David Herrmann76a68ba2013-04-06 20:28:37 +02003443 hci_conn_drop(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003444 goto unlock;
3445 }
3446
Johan Hedberg04ab2742014-12-05 13:36:04 +02003447 cmd->cmd_complete = pairing_complete;
3448
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003449 /* For LE, just connecting isn't a proof that the pairing finished */
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003450 if (cp->addr.type == BDADDR_BREDR) {
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003451 conn->connect_cfm_cb = pairing_complete_cb;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003452 conn->security_cfm_cb = pairing_complete_cb;
3453 conn->disconn_cfm_cb = pairing_complete_cb;
3454 } else {
3455 conn->connect_cfm_cb = le_pairing_complete_cb;
3456 conn->security_cfm_cb = le_pairing_complete_cb;
3457 conn->disconn_cfm_cb = le_pairing_complete_cb;
3458 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003459
Johan Hedberge9a416b2011-02-19 12:05:56 -03003460 conn->io_capability = cp->io_cap;
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03003461 cmd->user_data = hci_conn_get(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003462
Johan Hedberg6f78fd42014-07-30 08:35:48 +03003463 if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) &&
Johan Hedberga511b352014-12-11 21:45:45 +02003464 hci_conn_security(conn, sec_level, auth_type, true)) {
3465 cmd->cmd_complete(cmd, 0);
3466 mgmt_pending_remove(cmd);
3467 }
Johan Hedberge9a416b2011-02-19 12:05:56 -03003468
3469 err = 0;
3470
3471unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003472 hci_dev_unlock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003473 return err;
3474}
3475
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003476static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3477 u16 len)
Johan Hedberg28424702012-02-02 04:02:29 +02003478{
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003479 struct mgmt_addr_info *addr = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003480 struct mgmt_pending_cmd *cmd;
Johan Hedberg28424702012-02-02 04:02:29 +02003481 struct hci_conn *conn;
3482 int err;
3483
3484 BT_DBG("");
3485
Johan Hedberg28424702012-02-02 04:02:29 +02003486 hci_dev_lock(hdev);
3487
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003488 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003489 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3490 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003491 goto unlock;
3492 }
3493
Johan Hedberg333ae952015-03-17 13:48:47 +02003494 cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02003495 if (!cmd) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003496 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3497 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003498 goto unlock;
3499 }
3500
3501 conn = cmd->user_data;
3502
3503 if (bacmp(&addr->bdaddr, &conn->dst) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003504 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3505 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003506 goto unlock;
3507 }
3508
Johan Hedberga511b352014-12-11 21:45:45 +02003509 cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED);
3510 mgmt_pending_remove(cmd);
Johan Hedberg28424702012-02-02 04:02:29 +02003511
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003512 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
3513 addr, sizeof(*addr));
Johan Hedberg28424702012-02-02 04:02:29 +02003514unlock:
3515 hci_dev_unlock(hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02003516 return err;
3517}
3518
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003519static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
Johan Hedberg1707c602013-03-15 17:07:15 -05003520 struct mgmt_addr_info *addr, u16 mgmt_op,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003521 u16 hci_op, __le32 passkey)
Johan Hedberga5c29682011-02-19 12:05:57 -03003522{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003523 struct mgmt_pending_cmd *cmd;
Brian Gix0df4c182011-11-16 13:53:13 -08003524 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03003525 int err;
3526
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003527 hci_dev_lock(hdev);
Johan Hedberg08ba5382011-03-16 14:29:34 +02003528
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003529 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003530 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3531 MGMT_STATUS_NOT_POWERED, addr,
3532 sizeof(*addr));
Brian Gix0df4c182011-11-16 13:53:13 -08003533 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003534 }
3535
Johan Hedberg1707c602013-03-15 17:07:15 -05003536 if (addr->type == BDADDR_BREDR)
3537 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr);
Johan Hedberg272d90d2012-02-09 15:26:12 +02003538 else
Johan Hedberg1707c602013-03-15 17:07:15 -05003539 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr);
Brian Gix47c15e22011-11-16 13:53:14 -08003540
Johan Hedberg272d90d2012-02-09 15:26:12 +02003541 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003542 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3543 MGMT_STATUS_NOT_CONNECTED, addr,
3544 sizeof(*addr));
Johan Hedberg272d90d2012-02-09 15:26:12 +02003545 goto done;
3546 }
3547
Johan Hedberg1707c602013-03-15 17:07:15 -05003548 if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
Brian Gix5fe57d92011-12-21 16:12:13 -08003549 err = smp_user_confirm_reply(conn, mgmt_op, passkey);
Brian Gix5fe57d92011-12-21 16:12:13 -08003550 if (!err)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003551 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3552 MGMT_STATUS_SUCCESS, addr,
3553 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003554 else
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003555 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3556 MGMT_STATUS_FAILED, addr,
3557 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003558
Brian Gix47c15e22011-11-16 13:53:14 -08003559 goto done;
3560 }
3561
Johan Hedberg1707c602013-03-15 17:07:15 -05003562 cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr));
Johan Hedberga5c29682011-02-19 12:05:57 -03003563 if (!cmd) {
3564 err = -ENOMEM;
Brian Gix0df4c182011-11-16 13:53:13 -08003565 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003566 }
3567
Johan Hedberg7776d1d2014-12-05 13:36:03 +02003568 cmd->cmd_complete = addr_cmd_complete;
3569
Brian Gix0df4c182011-11-16 13:53:13 -08003570 /* Continue with pairing via HCI */
Brian Gix604086b2011-11-23 08:28:33 -08003571 if (hci_op == HCI_OP_USER_PASSKEY_REPLY) {
3572 struct hci_cp_user_passkey_reply cp;
3573
Johan Hedberg1707c602013-03-15 17:07:15 -05003574 bacpy(&cp.bdaddr, &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003575 cp.passkey = passkey;
3576 err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
3577 } else
Johan Hedberg1707c602013-03-15 17:07:15 -05003578 err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr),
3579 &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003580
Johan Hedberga664b5b2011-02-19 12:06:02 -03003581 if (err < 0)
3582 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03003583
Brian Gix0df4c182011-11-16 13:53:13 -08003584done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003585 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03003586 return err;
3587}
3588
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303589static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
3590 void *data, u16 len)
3591{
3592 struct mgmt_cp_pin_code_neg_reply *cp = data;
3593
3594 BT_DBG("");
3595
Johan Hedberg1707c602013-03-15 17:07:15 -05003596 return user_pairing_resp(sk, hdev, &cp->addr,
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303597 MGMT_OP_PIN_CODE_NEG_REPLY,
3598 HCI_OP_PIN_CODE_NEG_REPLY, 0);
3599}
3600
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003601static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3602 u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003603{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003604 struct mgmt_cp_user_confirm_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003605
3606 BT_DBG("");
3607
3608 if (len != sizeof(*cp))
Johan Hedberga69e8372015-03-06 21:08:53 +02003609 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
3610 MGMT_STATUS_INVALID_PARAMS);
Brian Gix0df4c182011-11-16 13:53:13 -08003611
Johan Hedberg1707c602013-03-15 17:07:15 -05003612 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003613 MGMT_OP_USER_CONFIRM_REPLY,
3614 HCI_OP_USER_CONFIRM_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003615}
3616
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003617static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003618 void *data, u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003619{
Johan Hedbergc9c26592011-12-15 00:47:41 +02003620 struct mgmt_cp_user_confirm_neg_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003621
3622 BT_DBG("");
3623
Johan Hedberg1707c602013-03-15 17:07:15 -05003624 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003625 MGMT_OP_USER_CONFIRM_NEG_REPLY,
3626 HCI_OP_USER_CONFIRM_NEG_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003627}
3628
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003629static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3630 u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003631{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003632 struct mgmt_cp_user_passkey_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003633
3634 BT_DBG("");
3635
Johan Hedberg1707c602013-03-15 17:07:15 -05003636 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003637 MGMT_OP_USER_PASSKEY_REPLY,
3638 HCI_OP_USER_PASSKEY_REPLY, cp->passkey);
Brian Gix604086b2011-11-23 08:28:33 -08003639}
3640
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003641static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003642 void *data, u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003643{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003644 struct mgmt_cp_user_passkey_neg_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003645
3646 BT_DBG("");
3647
Johan Hedberg1707c602013-03-15 17:07:15 -05003648 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003649 MGMT_OP_USER_PASSKEY_NEG_REPLY,
3650 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
Brian Gix604086b2011-11-23 08:28:33 -08003651}
3652
Johan Hedberg13928972013-03-15 17:07:00 -05003653static void update_name(struct hci_request *req)
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003654{
Johan Hedberg13928972013-03-15 17:07:00 -05003655 struct hci_dev *hdev = req->hdev;
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003656 struct hci_cp_write_local_name cp;
3657
Johan Hedberg13928972013-03-15 17:07:00 -05003658 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003659
Johan Hedberg890ea892013-03-15 17:06:52 -05003660 hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp);
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003661}
3662
Marcel Holtmann1904a852015-01-11 13:50:44 -08003663static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg13928972013-03-15 17:07:00 -05003664{
3665 struct mgmt_cp_set_local_name *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003666 struct mgmt_pending_cmd *cmd;
Johan Hedberg13928972013-03-15 17:07:00 -05003667
3668 BT_DBG("status 0x%02x", status);
3669
3670 hci_dev_lock(hdev);
3671
Johan Hedberg333ae952015-03-17 13:48:47 +02003672 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05003673 if (!cmd)
3674 goto unlock;
3675
3676 cp = cmd->param;
3677
3678 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02003679 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
3680 mgmt_status(status));
Johan Hedberg13928972013-03-15 17:07:00 -05003681 else
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003682 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3683 cp, sizeof(*cp));
Johan Hedberg13928972013-03-15 17:07:00 -05003684
3685 mgmt_pending_remove(cmd);
3686
3687unlock:
3688 hci_dev_unlock(hdev);
3689}
3690
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003691static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003692 u16 len)
Johan Hedbergb312b1612011-03-16 14:29:37 +02003693{
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003694 struct mgmt_cp_set_local_name *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003695 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05003696 struct hci_request req;
Johan Hedbergb312b1612011-03-16 14:29:37 +02003697 int err;
3698
3699 BT_DBG("");
3700
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003701 hci_dev_lock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003702
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003703 /* If the old values are the same as the new ones just return a
3704 * direct command complete event.
3705 */
3706 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
3707 !memcmp(hdev->short_name, cp->short_name,
3708 sizeof(hdev->short_name))) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003709 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3710 data, len);
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003711 goto failed;
3712 }
3713
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003714 memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003715
Johan Hedbergb5235a62012-02-21 14:32:24 +02003716 if (!hdev_is_powered(hdev)) {
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003717 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003718
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003719 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3720 data, len);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003721 if (err < 0)
3722 goto failed;
3723
Marcel Holtmannf6b77122015-03-14 19:28:05 -07003724 err = mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev,
3725 data, len, sk);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003726
Johan Hedbergb5235a62012-02-21 14:32:24 +02003727 goto failed;
3728 }
3729
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003730 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003731 if (!cmd) {
3732 err = -ENOMEM;
3733 goto failed;
3734 }
3735
Johan Hedberg13928972013-03-15 17:07:00 -05003736 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3737
Johan Hedberg890ea892013-03-15 17:06:52 -05003738 hci_req_init(&req, hdev);
Johan Hedberg3f985052013-03-15 17:07:02 -05003739
3740 if (lmp_bredr_capable(hdev)) {
3741 update_name(&req);
3742 update_eir(&req);
3743 }
3744
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003745 /* The name is stored in the scan response data and so
3746 * no need to udpate the advertising data here.
3747 */
Johan Hedberg3f985052013-03-15 17:07:02 -05003748 if (lmp_le_capable(hdev))
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003749 update_scan_rsp_data(&req);
Johan Hedberg3f985052013-03-15 17:07:02 -05003750
Johan Hedberg13928972013-03-15 17:07:00 -05003751 err = hci_req_run(&req, set_name_complete);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003752 if (err < 0)
3753 mgmt_pending_remove(cmd);
3754
3755failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003756 hci_dev_unlock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003757 return err;
3758}
3759
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003760static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003761 void *data, u16 data_len)
Szymon Jancc35938b2011-03-22 13:12:21 +01003762{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003763 struct mgmt_pending_cmd *cmd;
Szymon Jancc35938b2011-03-22 13:12:21 +01003764 int err;
3765
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003766 BT_DBG("%s", hdev->name);
Szymon Jancc35938b2011-03-22 13:12:21 +01003767
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003768 hci_dev_lock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003769
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003770 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003771 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3772 MGMT_STATUS_NOT_POWERED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003773 goto unlock;
3774 }
3775
Andre Guedes9a1a1992012-07-24 15:03:48 -03003776 if (!lmp_ssp_capable(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003777 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3778 MGMT_STATUS_NOT_SUPPORTED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003779 goto unlock;
3780 }
3781
Johan Hedberg333ae952015-03-17 13:48:47 +02003782 if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003783 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3784 MGMT_STATUS_BUSY);
Szymon Jancc35938b2011-03-22 13:12:21 +01003785 goto unlock;
3786 }
3787
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003788 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
Szymon Jancc35938b2011-03-22 13:12:21 +01003789 if (!cmd) {
3790 err = -ENOMEM;
3791 goto unlock;
3792 }
3793
Johan Hedberg710f11c2014-05-26 11:21:22 +03003794 if (bredr_sc_enabled(hdev))
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08003795 err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA,
3796 0, NULL);
3797 else
3798 err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
3799
Szymon Jancc35938b2011-03-22 13:12:21 +01003800 if (err < 0)
3801 mgmt_pending_remove(cmd);
3802
3803unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003804 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003805 return err;
3806}
3807
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003808static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003809 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003810{
Johan Hedberg5d57e792015-01-23 10:10:38 +02003811 struct mgmt_addr_info *addr = data;
Szymon Janc2763eda2011-03-22 13:12:22 +01003812 int err;
3813
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003814 BT_DBG("%s ", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003815
Johan Hedberg5d57e792015-01-23 10:10:38 +02003816 if (!bdaddr_type_is_valid(addr->type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003817 return mgmt_cmd_complete(sk, hdev->id,
3818 MGMT_OP_ADD_REMOTE_OOB_DATA,
3819 MGMT_STATUS_INVALID_PARAMS,
3820 addr, sizeof(*addr));
Johan Hedberg5d57e792015-01-23 10:10:38 +02003821
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003822 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003823
Marcel Holtmannec109112014-01-10 02:07:30 -08003824 if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) {
3825 struct mgmt_cp_add_remote_oob_data *cp = data;
3826 u8 status;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003827
Johan Hedbergc19a4952014-11-17 20:52:19 +02003828 if (cp->addr.type != BDADDR_BREDR) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003829 err = mgmt_cmd_complete(sk, hdev->id,
3830 MGMT_OP_ADD_REMOTE_OOB_DATA,
3831 MGMT_STATUS_INVALID_PARAMS,
3832 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02003833 goto unlock;
3834 }
3835
Marcel Holtmannec109112014-01-10 02:07:30 -08003836 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg6928a922014-10-26 20:46:09 +01003837 cp->addr.type, cp->hash,
3838 cp->rand, NULL, NULL);
Marcel Holtmannec109112014-01-10 02:07:30 -08003839 if (err < 0)
3840 status = MGMT_STATUS_FAILED;
3841 else
3842 status = MGMT_STATUS_SUCCESS;
3843
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003844 err = mgmt_cmd_complete(sk, hdev->id,
3845 MGMT_OP_ADD_REMOTE_OOB_DATA, status,
3846 &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08003847 } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) {
3848 struct mgmt_cp_add_remote_oob_ext_data *cp = data;
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003849 u8 *rand192, *hash192, *rand256, *hash256;
Marcel Holtmannec109112014-01-10 02:07:30 -08003850 u8 status;
3851
Johan Hedberg86df9202014-10-26 20:52:27 +01003852 if (bdaddr_type_is_le(cp->addr.type)) {
Johan Hedbergd25b78e2015-01-27 12:55:52 +02003853 /* Enforce zero-valued 192-bit parameters as
3854 * long as legacy SMP OOB isn't implemented.
3855 */
3856 if (memcmp(cp->rand192, ZERO_KEY, 16) ||
3857 memcmp(cp->hash192, ZERO_KEY, 16)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003858 err = mgmt_cmd_complete(sk, hdev->id,
3859 MGMT_OP_ADD_REMOTE_OOB_DATA,
3860 MGMT_STATUS_INVALID_PARAMS,
3861 addr, sizeof(*addr));
Johan Hedbergd25b78e2015-01-27 12:55:52 +02003862 goto unlock;
3863 }
3864
Johan Hedberg86df9202014-10-26 20:52:27 +01003865 rand192 = NULL;
3866 hash192 = NULL;
3867 } else {
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003868 /* In case one of the P-192 values is set to zero,
3869 * then just disable OOB data for P-192.
3870 */
3871 if (!memcmp(cp->rand192, ZERO_KEY, 16) ||
3872 !memcmp(cp->hash192, ZERO_KEY, 16)) {
3873 rand192 = NULL;
3874 hash192 = NULL;
3875 } else {
3876 rand192 = cp->rand192;
3877 hash192 = cp->hash192;
3878 }
3879 }
3880
3881 /* In case one of the P-256 values is set to zero, then just
3882 * disable OOB data for P-256.
3883 */
3884 if (!memcmp(cp->rand256, ZERO_KEY, 16) ||
3885 !memcmp(cp->hash256, ZERO_KEY, 16)) {
3886 rand256 = NULL;
3887 hash256 = NULL;
3888 } else {
3889 rand256 = cp->rand256;
3890 hash256 = cp->hash256;
Johan Hedberg86df9202014-10-26 20:52:27 +01003891 }
3892
Johan Hedberg81328d5c2014-10-26 20:33:47 +01003893 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg86df9202014-10-26 20:52:27 +01003894 cp->addr.type, hash192, rand192,
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003895 hash256, rand256);
Marcel Holtmannec109112014-01-10 02:07:30 -08003896 if (err < 0)
3897 status = MGMT_STATUS_FAILED;
3898 else
3899 status = MGMT_STATUS_SUCCESS;
3900
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003901 err = mgmt_cmd_complete(sk, hdev->id,
3902 MGMT_OP_ADD_REMOTE_OOB_DATA,
3903 status, &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08003904 } else {
3905 BT_ERR("add_remote_oob_data: invalid length of %u bytes", len);
Johan Hedberga69e8372015-03-06 21:08:53 +02003906 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3907 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannec109112014-01-10 02:07:30 -08003908 }
Szymon Janc2763eda2011-03-22 13:12:22 +01003909
Johan Hedbergc19a4952014-11-17 20:52:19 +02003910unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003911 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003912 return err;
3913}
3914
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003915static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003916 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003917{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003918 struct mgmt_cp_remove_remote_oob_data *cp = data;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003919 u8 status;
Szymon Janc2763eda2011-03-22 13:12:22 +01003920 int err;
3921
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003922 BT_DBG("%s", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003923
Johan Hedbergc19a4952014-11-17 20:52:19 +02003924 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003925 return mgmt_cmd_complete(sk, hdev->id,
3926 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
3927 MGMT_STATUS_INVALID_PARAMS,
3928 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02003929
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003930 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003931
Johan Hedbergeedbd582014-11-15 09:34:23 +02003932 if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
3933 hci_remote_oob_data_clear(hdev);
3934 status = MGMT_STATUS_SUCCESS;
3935 goto done;
3936 }
3937
Johan Hedberg6928a922014-10-26 20:46:09 +01003938 err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type);
Szymon Janc2763eda2011-03-22 13:12:22 +01003939 if (err < 0)
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003940 status = MGMT_STATUS_INVALID_PARAMS;
Szymon Janc2763eda2011-03-22 13:12:22 +01003941 else
Szymon Janca6785be2012-12-13 15:11:21 +01003942 status = MGMT_STATUS_SUCCESS;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003943
Johan Hedbergeedbd582014-11-15 09:34:23 +02003944done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003945 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
3946 status, &cp->addr, sizeof(cp->addr));
Szymon Janc2763eda2011-03-22 13:12:22 +01003947
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003948 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003949 return err;
3950}
3951
Jakub Pawlowski812abb12015-03-17 09:04:13 -07003952static bool trigger_bredr_inquiry(struct hci_request *req, u8 *status)
3953{
3954 struct hci_dev *hdev = req->hdev;
3955 struct hci_cp_inquiry cp;
3956 /* General inquiry access code (GIAC) */
3957 u8 lap[3] = { 0x33, 0x8b, 0x9e };
3958
3959 *status = mgmt_bredr_support(hdev);
3960 if (*status)
3961 return false;
3962
3963 if (hci_dev_test_flag(hdev, HCI_INQUIRY)) {
3964 *status = MGMT_STATUS_BUSY;
3965 return false;
3966 }
3967
3968 hci_inquiry_cache_flush(hdev);
3969
3970 memset(&cp, 0, sizeof(cp));
3971 memcpy(&cp.lap, lap, sizeof(cp.lap));
3972 cp.length = DISCOV_BREDR_INQUIRY_LEN;
3973
3974 hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp);
3975
3976 return true;
3977}
3978
3979static bool trigger_le_scan(struct hci_request *req, u16 interval, u8 *status)
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003980{
Marcel Holtmann80190442014-12-04 11:36:36 +01003981 struct hci_dev *hdev = req->hdev;
3982 struct hci_cp_le_set_scan_param param_cp;
3983 struct hci_cp_le_set_scan_enable enable_cp;
Marcel Holtmann80190442014-12-04 11:36:36 +01003984 u8 own_addr_type;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003985 int err;
3986
Jakub Pawlowski812abb12015-03-17 09:04:13 -07003987 *status = mgmt_le_support(hdev);
3988 if (*status)
3989 return false;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003990
Jakub Pawlowski812abb12015-03-17 09:04:13 -07003991 if (hci_dev_test_flag(hdev, HCI_LE_ADV)) {
3992 /* Don't let discovery abort an outgoing connection attempt
3993 * that's using directed advertising.
3994 */
3995 if (hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT)) {
3996 *status = MGMT_STATUS_REJECTED;
Marcel Holtmann80190442014-12-04 11:36:36 +01003997 return false;
3998 }
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003999
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004000 disable_advertising(req);
4001 }
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004002
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004003 /* If controller is scanning, it means the background scanning is
4004 * running. Thus, we should temporarily stop it in order to set the
4005 * discovery scanning parameters.
4006 */
4007 if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
4008 hci_req_add_le_scan_disable(req);
4009
4010 /* All active scans will be done with either a resolvable private
4011 * address (when privacy feature has been enabled) or non-resolvable
4012 * private address.
4013 */
4014 err = hci_update_random_address(req, true, &own_addr_type);
4015 if (err < 0) {
4016 *status = MGMT_STATUS_FAILED;
4017 return false;
4018 }
4019
4020 memset(&param_cp, 0, sizeof(param_cp));
4021 param_cp.type = LE_SCAN_ACTIVE;
4022 param_cp.interval = cpu_to_le16(interval);
4023 param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN);
4024 param_cp.own_address_type = own_addr_type;
4025
4026 hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
4027 &param_cp);
4028
4029 memset(&enable_cp, 0, sizeof(enable_cp));
4030 enable_cp.enable = LE_SCAN_ENABLE;
4031 enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
4032
4033 hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
4034 &enable_cp);
4035
4036 return true;
4037}
4038
4039static bool trigger_discovery(struct hci_request *req, u8 *status)
4040{
4041 struct hci_dev *hdev = req->hdev;
4042
4043 switch (hdev->discovery.type) {
4044 case DISCOV_TYPE_BREDR:
4045 if (!trigger_bredr_inquiry(req, status))
4046 return false;
Marcel Holtmann80190442014-12-04 11:36:36 +01004047 break;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004048
Marcel Holtmann80190442014-12-04 11:36:36 +01004049 case DISCOV_TYPE_INTERLEAVED:
Jakub Pawlowski07d23342015-03-17 09:04:14 -07004050 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
4051 &hdev->quirks)) {
4052 /* During simultaneous discovery, we double LE scan
4053 * interval. We must leave some time for the controller
4054 * to do BR/EDR inquiry.
4055 */
4056 if (!trigger_le_scan(req, DISCOV_LE_SCAN_INT * 2,
4057 status))
4058 return false;
4059
4060 if (!trigger_bredr_inquiry(req, status))
4061 return false;
4062
4063 return true;
4064 }
4065
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004066 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Marcel Holtmann80190442014-12-04 11:36:36 +01004067 *status = MGMT_STATUS_NOT_SUPPORTED;
4068 return false;
4069 }
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004070 /* fall through */
Marcel Holtmann80190442014-12-04 11:36:36 +01004071
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004072 case DISCOV_TYPE_LE:
4073 if (!trigger_le_scan(req, DISCOV_LE_SCAN_INT, status))
Marcel Holtmann80190442014-12-04 11:36:36 +01004074 return false;
Marcel Holtmann80190442014-12-04 11:36:36 +01004075 break;
4076
4077 default:
4078 *status = MGMT_STATUS_INVALID_PARAMS;
4079 return false;
4080 }
4081
4082 return true;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004083}
4084
Marcel Holtmann1904a852015-01-11 13:50:44 -08004085static void start_discovery_complete(struct hci_dev *hdev, u8 status,
4086 u16 opcode)
Andre Guedes7c307722013-04-30 15:29:28 -03004087{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004088 struct mgmt_pending_cmd *cmd;
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004089 unsigned long timeout;
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01004090
Andre Guedes7c307722013-04-30 15:29:28 -03004091 BT_DBG("status %d", status);
4092
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004093 hci_dev_lock(hdev);
4094
Johan Hedberg333ae952015-03-17 13:48:47 +02004095 cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004096 if (!cmd)
Johan Hedberg333ae952015-03-17 13:48:47 +02004097 cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004098
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004099 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02004100 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004101 mgmt_pending_remove(cmd);
Andre Guedes7c307722013-04-30 15:29:28 -03004102 }
4103
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004104 if (status) {
4105 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4106 goto unlock;
4107 }
4108
Andre Guedes7c307722013-04-30 15:29:28 -03004109 hci_discovery_set_state(hdev, DISCOVERY_FINDING);
Andre Guedes7c307722013-04-30 15:29:28 -03004110
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08004111 /* If the scan involves LE scan, pick proper timeout to schedule
4112 * hdev->le_scan_disable that will stop it.
4113 */
Andre Guedes7c307722013-04-30 15:29:28 -03004114 switch (hdev->discovery.type) {
4115 case DISCOV_TYPE_LE:
Lukasz Rymanowski3d5a76f2014-03-27 20:55:21 +01004116 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
Andre Guedes7c307722013-04-30 15:29:28 -03004117 break;
Andre Guedes7c307722013-04-30 15:29:28 -03004118 case DISCOV_TYPE_INTERLEAVED:
Jakub Pawlowski07d23342015-03-17 09:04:14 -07004119 /* When running simultaneous discovery, the LE scanning time
4120 * should occupy the whole discovery time sine BR/EDR inquiry
4121 * and LE scanning are scheduled by the controller.
4122 *
4123 * For interleaving discovery in comparison, BR/EDR inquiry
4124 * and LE scanning are done sequentially with separate
4125 * timeouts.
4126 */
4127 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
4128 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
4129 else
4130 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
Andre Guedes7c307722013-04-30 15:29:28 -03004131 break;
Andre Guedes7c307722013-04-30 15:29:28 -03004132 case DISCOV_TYPE_BREDR:
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004133 timeout = 0;
Andre Guedes7c307722013-04-30 15:29:28 -03004134 break;
Andre Guedes7c307722013-04-30 15:29:28 -03004135 default:
4136 BT_ERR("Invalid discovery type %d", hdev->discovery.type);
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004137 timeout = 0;
4138 break;
Andre Guedes7c307722013-04-30 15:29:28 -03004139 }
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01004140
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08004141 if (timeout) {
4142 /* When service discovery is used and the controller has
4143 * a strict duplicate filter, it is important to remember
4144 * the start and duration of the scan. This is required
4145 * for restarting scanning during the discovery phase.
4146 */
4147 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER,
4148 &hdev->quirks) &&
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08004149 hdev->discovery.result_filtering) {
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08004150 hdev->discovery.scan_start = jiffies;
4151 hdev->discovery.scan_duration = timeout;
4152 }
4153
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004154 queue_delayed_work(hdev->workqueue,
4155 &hdev->le_scan_disable, timeout);
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08004156 }
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01004157
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004158unlock:
4159 hci_dev_unlock(hdev);
Andre Guedes7c307722013-04-30 15:29:28 -03004160}
4161
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004162static int start_discovery(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004163 void *data, u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04004164{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004165 struct mgmt_cp_start_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004166 struct mgmt_pending_cmd *cmd;
Andre Guedes7c307722013-04-30 15:29:28 -03004167 struct hci_request req;
Marcel Holtmann80190442014-12-04 11:36:36 +01004168 u8 status;
Johan Hedberg14a53662011-04-27 10:29:56 -04004169 int err;
4170
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004171 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04004172
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004173 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004174
Johan Hedberg4b34ee782012-02-21 14:13:02 +02004175 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004176 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4177 MGMT_STATUS_NOT_POWERED,
4178 &cp->type, sizeof(cp->type));
Johan Hedbergbd2d1332011-11-07 23:13:37 +02004179 goto failed;
4180 }
4181
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004182 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004183 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004184 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4185 MGMT_STATUS_BUSY, &cp->type,
4186 sizeof(cp->type));
Andre Guedes642be6c2012-03-21 00:03:37 -03004187 goto failed;
4188 }
4189
Johan Hedberg2922a942014-12-05 13:36:06 +02004190 cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, data, len);
Johan Hedberg14a53662011-04-27 10:29:56 -04004191 if (!cmd) {
4192 err = -ENOMEM;
4193 goto failed;
4194 }
4195
Johan Hedberg2922a942014-12-05 13:36:06 +02004196 cmd->cmd_complete = generic_cmd_complete;
4197
Marcel Holtmann22078802014-12-05 11:45:22 +01004198 /* Clear the discovery filter first to free any previously
4199 * allocated memory for the UUID list.
4200 */
4201 hci_discovery_filter_clear(hdev);
4202
Andre Guedes4aab14e2012-02-17 20:39:36 -03004203 hdev->discovery.type = cp->type;
Marcel Holtmannda25cf62014-12-05 13:03:35 +01004204 hdev->discovery.report_invalid_rssi = false;
Andre Guedes4aab14e2012-02-17 20:39:36 -03004205
Andre Guedes7c307722013-04-30 15:29:28 -03004206 hci_req_init(&req, hdev);
4207
Marcel Holtmann80190442014-12-04 11:36:36 +01004208 if (!trigger_discovery(&req, &status)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004209 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4210 status, &cp->type, sizeof(cp->type));
Johan Hedberg04106752013-01-10 14:54:09 +02004211 mgmt_pending_remove(cmd);
4212 goto failed;
Andre Guedesf39799f2012-02-17 20:39:35 -03004213 }
Andre Guedes3fd24152012-02-03 17:48:01 -03004214
Andre Guedes7c307722013-04-30 15:29:28 -03004215 err = hci_req_run(&req, start_discovery_complete);
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004216 if (err < 0) {
Johan Hedberg14a53662011-04-27 10:29:56 -04004217 mgmt_pending_remove(cmd);
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004218 goto failed;
4219 }
4220
4221 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
Johan Hedberg14a53662011-04-27 10:29:56 -04004222
4223failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004224 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004225 return err;
4226}
4227
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004228static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd,
4229 u8 status)
Andre Guedes1183fdc2013-04-30 15:29:35 -03004230{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004231 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
4232 cmd->param, 1);
Johan Hedberg2922a942014-12-05 13:36:06 +02004233}
4234
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004235static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
4236 void *data, u16 len)
4237{
4238 struct mgmt_cp_start_service_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004239 struct mgmt_pending_cmd *cmd;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004240 struct hci_request req;
4241 const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16);
4242 u16 uuid_count, expected_len;
4243 u8 status;
Andre Guedes1183fdc2013-04-30 15:29:35 -03004244 int err;
4245
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004246 BT_DBG("%s", hdev->name);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004247
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004248 hci_dev_lock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004249
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004250 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004251 err = mgmt_cmd_complete(sk, hdev->id,
4252 MGMT_OP_START_SERVICE_DISCOVERY,
4253 MGMT_STATUS_NOT_POWERED,
4254 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004255 goto failed;
4256 }
4257
4258 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004259 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004260 err = mgmt_cmd_complete(sk, hdev->id,
4261 MGMT_OP_START_SERVICE_DISCOVERY,
4262 MGMT_STATUS_BUSY, &cp->type,
4263 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004264 goto failed;
4265 }
4266
4267 uuid_count = __le16_to_cpu(cp->uuid_count);
4268 if (uuid_count > max_uuid_count) {
4269 BT_ERR("service_discovery: too big uuid_count value %u",
4270 uuid_count);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004271 err = mgmt_cmd_complete(sk, hdev->id,
4272 MGMT_OP_START_SERVICE_DISCOVERY,
4273 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4274 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004275 goto failed;
4276 }
4277
4278 expected_len = sizeof(*cp) + uuid_count * 16;
4279 if (expected_len != len) {
4280 BT_ERR("service_discovery: expected %u bytes, got %u bytes",
4281 expected_len, len);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004282 err = mgmt_cmd_complete(sk, hdev->id,
4283 MGMT_OP_START_SERVICE_DISCOVERY,
4284 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4285 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004286 goto failed;
4287 }
4288
4289 cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY,
Johan Hedberg2922a942014-12-05 13:36:06 +02004290 hdev, data, len);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004291 if (!cmd) {
4292 err = -ENOMEM;
4293 goto failed;
4294 }
4295
Johan Hedberg2922a942014-12-05 13:36:06 +02004296 cmd->cmd_complete = service_discovery_cmd_complete;
4297
Marcel Holtmann22078802014-12-05 11:45:22 +01004298 /* Clear the discovery filter first to free any previously
4299 * allocated memory for the UUID list.
4300 */
4301 hci_discovery_filter_clear(hdev);
4302
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08004303 hdev->discovery.result_filtering = true;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004304 hdev->discovery.type = cp->type;
4305 hdev->discovery.rssi = cp->rssi;
4306 hdev->discovery.uuid_count = uuid_count;
4307
4308 if (uuid_count > 0) {
4309 hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16,
4310 GFP_KERNEL);
4311 if (!hdev->discovery.uuids) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004312 err = mgmt_cmd_complete(sk, hdev->id,
4313 MGMT_OP_START_SERVICE_DISCOVERY,
4314 MGMT_STATUS_FAILED,
4315 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004316 mgmt_pending_remove(cmd);
4317 goto failed;
4318 }
4319 }
4320
4321 hci_req_init(&req, hdev);
4322
4323 if (!trigger_discovery(&req, &status)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004324 err = mgmt_cmd_complete(sk, hdev->id,
4325 MGMT_OP_START_SERVICE_DISCOVERY,
4326 status, &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004327 mgmt_pending_remove(cmd);
4328 goto failed;
4329 }
4330
4331 err = hci_req_run(&req, start_discovery_complete);
4332 if (err < 0) {
4333 mgmt_pending_remove(cmd);
4334 goto failed;
4335 }
4336
4337 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
4338
4339failed:
4340 hci_dev_unlock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004341 return err;
4342}
4343
Marcel Holtmann1904a852015-01-11 13:50:44 -08004344static void stop_discovery_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Andre Guedes0e05bba2013-04-30 15:29:33 -03004345{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004346 struct mgmt_pending_cmd *cmd;
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004347
Andre Guedes0e05bba2013-04-30 15:29:33 -03004348 BT_DBG("status %d", status);
4349
4350 hci_dev_lock(hdev);
4351
Johan Hedberg333ae952015-03-17 13:48:47 +02004352 cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004353 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02004354 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004355 mgmt_pending_remove(cmd);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004356 }
4357
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004358 if (!status)
4359 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004360
Andre Guedes0e05bba2013-04-30 15:29:33 -03004361 hci_dev_unlock(hdev);
4362}
4363
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004364static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004365 u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04004366{
Johan Hedbergd9306502012-02-20 23:25:18 +02004367 struct mgmt_cp_stop_discovery *mgmt_cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004368 struct mgmt_pending_cmd *cmd;
Andre Guedes0e05bba2013-04-30 15:29:33 -03004369 struct hci_request req;
Johan Hedberg14a53662011-04-27 10:29:56 -04004370 int err;
4371
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004372 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04004373
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004374 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004375
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004376 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004377 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4378 MGMT_STATUS_REJECTED, &mgmt_cp->type,
4379 sizeof(mgmt_cp->type));
Johan Hedbergd9306502012-02-20 23:25:18 +02004380 goto unlock;
4381 }
4382
4383 if (hdev->discovery.type != mgmt_cp->type) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004384 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4385 MGMT_STATUS_INVALID_PARAMS,
4386 &mgmt_cp->type, sizeof(mgmt_cp->type));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004387 goto unlock;
Johan Hedbergff9ef572012-01-04 14:23:45 +02004388 }
4389
Johan Hedberg2922a942014-12-05 13:36:06 +02004390 cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len);
Johan Hedberg14a53662011-04-27 10:29:56 -04004391 if (!cmd) {
4392 err = -ENOMEM;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004393 goto unlock;
Johan Hedberg14a53662011-04-27 10:29:56 -04004394 }
4395
Johan Hedberg2922a942014-12-05 13:36:06 +02004396 cmd->cmd_complete = generic_cmd_complete;
4397
Andre Guedes0e05bba2013-04-30 15:29:33 -03004398 hci_req_init(&req, hdev);
4399
Johan Hedberg21a60d32014-06-10 14:05:58 +03004400 hci_stop_discovery(&req);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004401
Johan Hedberg21a60d32014-06-10 14:05:58 +03004402 err = hci_req_run(&req, stop_discovery_complete);
4403 if (!err) {
4404 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004405 goto unlock;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004406 }
4407
Johan Hedberg21a60d32014-06-10 14:05:58 +03004408 mgmt_pending_remove(cmd);
4409
4410 /* If no HCI commands were sent we're done */
4411 if (err == -ENODATA) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004412 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0,
4413 &mgmt_cp->type, sizeof(mgmt_cp->type));
Johan Hedberg21a60d32014-06-10 14:05:58 +03004414 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4415 }
Johan Hedberg14a53662011-04-27 10:29:56 -04004416
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004417unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004418 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004419 return err;
4420}
4421
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004422static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004423 u16 len)
Johan Hedberg561aafb2012-01-04 13:31:59 +02004424{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004425 struct mgmt_cp_confirm_name *cp = data;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004426 struct inquiry_entry *e;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004427 int err;
4428
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004429 BT_DBG("%s", hdev->name);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004430
Johan Hedberg561aafb2012-01-04 13:31:59 +02004431 hci_dev_lock(hdev);
4432
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004433 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004434 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4435 MGMT_STATUS_FAILED, &cp->addr,
4436 sizeof(cp->addr));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004437 goto failed;
4438 }
4439
Johan Hedberga198e7b2012-02-17 14:27:06 +02004440 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004441 if (!e) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004442 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4443 MGMT_STATUS_INVALID_PARAMS, &cp->addr,
4444 sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02004445 goto failed;
4446 }
4447
4448 if (cp->name_known) {
4449 e->name_state = NAME_KNOWN;
4450 list_del(&e->list);
4451 } else {
4452 e->name_state = NAME_NEEDED;
Johan Hedberga3d4e202012-01-09 00:53:02 +02004453 hci_inquiry_cache_update_resolve(hdev, e);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004454 }
4455
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004456 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0,
4457 &cp->addr, sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02004458
4459failed:
4460 hci_dev_unlock(hdev);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004461 return err;
4462}
4463
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004464static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004465 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03004466{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004467 struct mgmt_cp_block_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004468 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03004469 int err;
4470
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004471 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03004472
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004473 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004474 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
4475 MGMT_STATUS_INVALID_PARAMS,
4476 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004477
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004478 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03004479
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004480 err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr,
4481 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004482 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004483 status = MGMT_STATUS_FAILED;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004484 goto done;
4485 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004486
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004487 mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4488 sk);
4489 status = MGMT_STATUS_SUCCESS;
4490
4491done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004492 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
4493 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03004494
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004495 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03004496
4497 return err;
4498}
4499
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004500static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004501 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03004502{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004503 struct mgmt_cp_unblock_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004504 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03004505 int err;
4506
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004507 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03004508
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004509 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004510 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
4511 MGMT_STATUS_INVALID_PARAMS,
4512 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004513
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004514 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03004515
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004516 err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr,
4517 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004518 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004519 status = MGMT_STATUS_INVALID_PARAMS;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004520 goto done;
4521 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004522
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004523 mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4524 sk);
4525 status = MGMT_STATUS_SUCCESS;
4526
4527done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004528 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
4529 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03004530
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004531 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03004532
4533 return err;
4534}
4535
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004536static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
4537 u16 len)
4538{
4539 struct mgmt_cp_set_device_id *cp = data;
Johan Hedberg890ea892013-03-15 17:06:52 -05004540 struct hci_request req;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004541 int err;
Szymon Jancc72d4b82012-03-16 16:02:57 +01004542 __u16 source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004543
4544 BT_DBG("%s", hdev->name);
4545
Szymon Jancc72d4b82012-03-16 16:02:57 +01004546 source = __le16_to_cpu(cp->source);
4547
4548 if (source > 0x0002)
Johan Hedberga69e8372015-03-06 21:08:53 +02004549 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
4550 MGMT_STATUS_INVALID_PARAMS);
Szymon Jancc72d4b82012-03-16 16:02:57 +01004551
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004552 hci_dev_lock(hdev);
4553
Szymon Jancc72d4b82012-03-16 16:02:57 +01004554 hdev->devid_source = source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004555 hdev->devid_vendor = __le16_to_cpu(cp->vendor);
4556 hdev->devid_product = __le16_to_cpu(cp->product);
4557 hdev->devid_version = __le16_to_cpu(cp->version);
4558
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004559 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0,
4560 NULL, 0);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004561
Johan Hedberg890ea892013-03-15 17:06:52 -05004562 hci_req_init(&req, hdev);
4563 update_eir(&req);
4564 hci_req_run(&req, NULL);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004565
4566 hci_dev_unlock(hdev);
4567
4568 return err;
4569}
4570
Arman Uguray24b4f382015-03-23 15:57:12 -07004571static void enable_advertising_instance(struct hci_dev *hdev, u8 status,
4572 u16 opcode)
4573{
4574 BT_DBG("status %d", status);
4575}
4576
Marcel Holtmann1904a852015-01-11 13:50:44 -08004577static void set_advertising_complete(struct hci_dev *hdev, u8 status,
4578 u16 opcode)
Johan Hedberg4375f102013-09-25 13:26:10 +03004579{
4580 struct cmd_lookup match = { NULL, hdev };
Arman Uguray24b4f382015-03-23 15:57:12 -07004581 struct hci_request req;
Johan Hedberg4375f102013-09-25 13:26:10 +03004582
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304583 hci_dev_lock(hdev);
4584
Johan Hedberg4375f102013-09-25 13:26:10 +03004585 if (status) {
4586 u8 mgmt_err = mgmt_status(status);
4587
4588 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev,
4589 cmd_status_rsp, &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304590 goto unlock;
Johan Hedberg4375f102013-09-25 13:26:10 +03004591 }
4592
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004593 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004594 hci_dev_set_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03004595 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004596 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03004597
Johan Hedberg4375f102013-09-25 13:26:10 +03004598 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
4599 &match);
4600
4601 new_settings(hdev, match.sk);
4602
4603 if (match.sk)
4604 sock_put(match.sk);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304605
Arman Uguray24b4f382015-03-23 15:57:12 -07004606 /* If "Set Advertising" was just disabled and instance advertising was
4607 * set up earlier, then enable the advertising instance.
4608 */
4609 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
4610 !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
4611 goto unlock;
4612
4613 hci_req_init(&req, hdev);
4614
4615 update_adv_data(&req);
4616 enable_advertising(&req);
4617
4618 if (hci_req_run(&req, enable_advertising_instance) < 0)
4619 BT_ERR("Failed to re-configure advertising");
4620
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304621unlock:
4622 hci_dev_unlock(hdev);
Johan Hedberg4375f102013-09-25 13:26:10 +03004623}
4624
Marcel Holtmann21b51872013-10-10 09:47:53 -07004625static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
4626 u16 len)
Johan Hedberg4375f102013-09-25 13:26:10 +03004627{
4628 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004629 struct mgmt_pending_cmd *cmd;
Johan Hedberg4375f102013-09-25 13:26:10 +03004630 struct hci_request req;
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004631 u8 val, status;
Johan Hedberg4375f102013-09-25 13:26:10 +03004632 int err;
4633
4634 BT_DBG("request for %s", hdev->name);
4635
Johan Hedberge6fe7982013-10-02 15:45:22 +03004636 status = mgmt_le_support(hdev);
4637 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02004638 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4639 status);
Johan Hedberg4375f102013-09-25 13:26:10 +03004640
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004641 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02004642 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4643 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4375f102013-09-25 13:26:10 +03004644
4645 hci_dev_lock(hdev);
4646
4647 val = !!cp->val;
Johan Hedberg4375f102013-09-25 13:26:10 +03004648
Johan Hedbergf74ca9b2013-10-08 15:52:18 +02004649 /* The following conditions are ones which mean that we should
4650 * not do any HCI communication but directly send a mgmt
4651 * response to user space (after toggling the flag if
4652 * necessary).
4653 */
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004654 if (!hdev_is_powered(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004655 (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
4656 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) ||
Johan Hedberge8bb6b92014-07-08 15:07:53 +03004657 hci_conn_num(hdev, LE_LINK) > 0 ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004658 (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Johan Hedberge8bb6b92014-07-08 15:07:53 +03004659 hdev->le_scan_type == LE_SCAN_ACTIVE)) {
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004660 bool changed;
Johan Hedberg4375f102013-09-25 13:26:10 +03004661
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004662 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07004663 changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004664 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004665 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004666 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004667 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004668 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07004669 changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004670 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Johan Hedberg4375f102013-09-25 13:26:10 +03004671 }
4672
4673 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
4674 if (err < 0)
4675 goto unlock;
4676
4677 if (changed)
4678 err = new_settings(hdev, sk);
4679
4680 goto unlock;
4681 }
4682
Johan Hedberg333ae952015-03-17 13:48:47 +02004683 if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
4684 pending_find(MGMT_OP_SET_LE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004685 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4686 MGMT_STATUS_BUSY);
Johan Hedberg4375f102013-09-25 13:26:10 +03004687 goto unlock;
4688 }
4689
4690 cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
4691 if (!cmd) {
4692 err = -ENOMEM;
4693 goto unlock;
4694 }
4695
4696 hci_req_init(&req, hdev);
4697
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004698 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004699 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004700 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004701 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004702
Arman Uguray24b4f382015-03-23 15:57:12 -07004703 if (val) {
4704 /* Switch to instance "0" for the Set Advertising setting. */
4705 update_adv_data_for_instance(&req, 0);
Arman Uguray4117ed72015-03-23 15:57:14 -07004706 update_scan_rsp_data_for_instance(&req, 0);
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07004707 enable_advertising(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07004708 } else {
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07004709 disable_advertising(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07004710 }
Johan Hedberg4375f102013-09-25 13:26:10 +03004711
4712 err = hci_req_run(&req, set_advertising_complete);
4713 if (err < 0)
4714 mgmt_pending_remove(cmd);
4715
4716unlock:
4717 hci_dev_unlock(hdev);
4718 return err;
4719}
4720
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004721static int set_static_address(struct sock *sk, struct hci_dev *hdev,
4722 void *data, u16 len)
4723{
4724 struct mgmt_cp_set_static_address *cp = data;
4725 int err;
4726
4727 BT_DBG("%s", hdev->name);
4728
Marcel Holtmann62af4442013-10-02 22:10:32 -07004729 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004730 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4731 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004732
4733 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004734 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4735 MGMT_STATUS_REJECTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004736
4737 if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
4738 if (!bacmp(&cp->bdaddr, BDADDR_NONE))
Johan Hedberga69e8372015-03-06 21:08:53 +02004739 return mgmt_cmd_status(sk, hdev->id,
4740 MGMT_OP_SET_STATIC_ADDRESS,
4741 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004742
4743 /* Two most significant bits shall be set */
4744 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
Johan Hedberga69e8372015-03-06 21:08:53 +02004745 return mgmt_cmd_status(sk, hdev->id,
4746 MGMT_OP_SET_STATIC_ADDRESS,
4747 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004748 }
4749
4750 hci_dev_lock(hdev);
4751
4752 bacpy(&hdev->static_addr, &cp->bdaddr);
4753
Marcel Holtmann93690c22015-03-06 10:11:21 -08004754 err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev);
4755 if (err < 0)
4756 goto unlock;
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004757
Marcel Holtmann93690c22015-03-06 10:11:21 -08004758 err = new_settings(hdev, sk);
4759
4760unlock:
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004761 hci_dev_unlock(hdev);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004762 return err;
4763}
4764
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004765static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
4766 void *data, u16 len)
4767{
4768 struct mgmt_cp_set_scan_params *cp = data;
4769 __u16 interval, window;
4770 int err;
4771
4772 BT_DBG("%s", hdev->name);
4773
4774 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004775 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4776 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004777
4778 interval = __le16_to_cpu(cp->interval);
4779
4780 if (interval < 0x0004 || interval > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02004781 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4782 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004783
4784 window = __le16_to_cpu(cp->window);
4785
4786 if (window < 0x0004 || window > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02004787 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4788 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004789
Marcel Holtmann899e1072013-10-14 09:55:32 -07004790 if (window > interval)
Johan Hedberga69e8372015-03-06 21:08:53 +02004791 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4792 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann899e1072013-10-14 09:55:32 -07004793
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004794 hci_dev_lock(hdev);
4795
4796 hdev->le_scan_interval = interval;
4797 hdev->le_scan_window = window;
4798
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004799 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0,
4800 NULL, 0);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004801
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03004802 /* If background scan is running, restart it so new parameters are
4803 * loaded.
4804 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004805 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03004806 hdev->discovery.state == DISCOVERY_STOPPED) {
4807 struct hci_request req;
4808
4809 hci_req_init(&req, hdev);
4810
4811 hci_req_add_le_scan_disable(&req);
4812 hci_req_add_le_passive_scan(&req);
4813
4814 hci_req_run(&req, NULL);
4815 }
4816
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004817 hci_dev_unlock(hdev);
4818
4819 return err;
4820}
4821
Marcel Holtmann1904a852015-01-11 13:50:44 -08004822static void fast_connectable_complete(struct hci_dev *hdev, u8 status,
4823 u16 opcode)
Johan Hedberg33e38b32013-03-15 17:07:05 -05004824{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004825 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004826
4827 BT_DBG("status 0x%02x", status);
4828
4829 hci_dev_lock(hdev);
4830
Johan Hedberg333ae952015-03-17 13:48:47 +02004831 cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004832 if (!cmd)
4833 goto unlock;
4834
4835 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004836 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4837 mgmt_status(status));
Johan Hedberg33e38b32013-03-15 17:07:05 -05004838 } else {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004839 struct mgmt_mode *cp = cmd->param;
4840
4841 if (cp->val)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004842 hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004843 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004844 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004845
Johan Hedberg33e38b32013-03-15 17:07:05 -05004846 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
4847 new_settings(hdev, cmd->sk);
4848 }
4849
4850 mgmt_pending_remove(cmd);
4851
4852unlock:
4853 hci_dev_unlock(hdev);
4854}
4855
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004856static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004857 void *data, u16 len)
Antti Julkuf6422ec2011-06-22 13:11:56 +03004858{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004859 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004860 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004861 struct hci_request req;
Antti Julkuf6422ec2011-06-22 13:11:56 +03004862 int err;
4863
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004864 BT_DBG("%s", hdev->name);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004865
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004866 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
Johan Hedberg56f87902013-10-02 13:43:13 +03004867 hdev->hci_ver < BLUETOOTH_VER_1_2)
Johan Hedberga69e8372015-03-06 21:08:53 +02004868 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4869 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03004870
Johan Hedberga7e80f22013-01-09 16:05:19 +02004871 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02004872 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4873 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02004874
Antti Julkuf6422ec2011-06-22 13:11:56 +03004875 hci_dev_lock(hdev);
4876
Johan Hedberg333ae952015-03-17 13:48:47 +02004877 if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004878 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4879 MGMT_STATUS_BUSY);
Johan Hedberg05cbf292013-03-15 17:07:07 -05004880 goto unlock;
4881 }
4882
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004883 if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004884 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4885 hdev);
4886 goto unlock;
4887 }
4888
Johan Hedberg406ef2a2015-03-10 20:14:27 +02004889 if (!hdev_is_powered(hdev)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07004890 hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg406ef2a2015-03-10 20:14:27 +02004891 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4892 hdev);
4893 new_settings(hdev, sk);
4894 goto unlock;
4895 }
4896
Johan Hedberg33e38b32013-03-15 17:07:05 -05004897 cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev,
4898 data, len);
4899 if (!cmd) {
4900 err = -ENOMEM;
4901 goto unlock;
4902 }
4903
4904 hci_req_init(&req, hdev);
4905
Johan Hedberg406d7802013-03-15 17:07:09 -05004906 write_fast_connectable(&req, cp->val);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004907
4908 err = hci_req_run(&req, fast_connectable_complete);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004909 if (err < 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004910 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4911 MGMT_STATUS_FAILED);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004912 mgmt_pending_remove(cmd);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004913 }
4914
Johan Hedberg33e38b32013-03-15 17:07:05 -05004915unlock:
Antti Julkuf6422ec2011-06-22 13:11:56 +03004916 hci_dev_unlock(hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004917
Antti Julkuf6422ec2011-06-22 13:11:56 +03004918 return err;
4919}
4920
Marcel Holtmann1904a852015-01-11 13:50:44 -08004921static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg0663ca22013-10-02 13:43:14 +03004922{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004923 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03004924
4925 BT_DBG("status 0x%02x", status);
4926
4927 hci_dev_lock(hdev);
4928
Johan Hedberg333ae952015-03-17 13:48:47 +02004929 cmd = pending_find(MGMT_OP_SET_BREDR, hdev);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004930 if (!cmd)
4931 goto unlock;
4932
4933 if (status) {
4934 u8 mgmt_err = mgmt_status(status);
4935
4936 /* We need to restore the flag if related HCI commands
4937 * failed.
4938 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004939 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004940
Johan Hedberga69e8372015-03-06 21:08:53 +02004941 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004942 } else {
4943 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
4944 new_settings(hdev, cmd->sk);
4945 }
4946
4947 mgmt_pending_remove(cmd);
4948
4949unlock:
4950 hci_dev_unlock(hdev);
4951}
4952
4953static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
4954{
4955 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004956 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03004957 struct hci_request req;
4958 int err;
4959
4960 BT_DBG("request for %s", hdev->name);
4961
4962 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004963 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4964 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004965
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004966 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02004967 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4968 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004969
4970 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02004971 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4972 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004973
4974 hci_dev_lock(hdev);
4975
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004976 if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg0663ca22013-10-02 13:43:14 +03004977 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4978 goto unlock;
4979 }
4980
4981 if (!hdev_is_powered(hdev)) {
4982 if (!cp->val) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004983 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
4984 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
4985 hci_dev_clear_flag(hdev, HCI_LINK_SECURITY);
4986 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
4987 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004988 }
4989
Marcel Holtmannce05d602015-03-13 02:11:03 -07004990 hci_dev_change_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004991
4992 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4993 if (err < 0)
4994 goto unlock;
4995
4996 err = new_settings(hdev, sk);
4997 goto unlock;
4998 }
4999
5000 /* Reject disabling when powered on */
5001 if (!cp->val) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005002 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5003 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005004 goto unlock;
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08005005 } else {
5006 /* When configuring a dual-mode controller to operate
5007 * with LE only and using a static address, then switching
5008 * BR/EDR back on is not allowed.
5009 *
5010 * Dual-mode controllers shall operate with the public
5011 * address as its identity address for BR/EDR and LE. So
5012 * reject the attempt to create an invalid configuration.
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08005013 *
5014 * The same restrictions applies when secure connections
5015 * has been enabled. For BR/EDR this is a controller feature
5016 * while for LE it is a host stack feature. This means that
5017 * switching BR/EDR back on when secure connections has been
5018 * enabled is not a supported transaction.
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08005019 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005020 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08005021 (bacmp(&hdev->static_addr, BDADDR_ANY) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005022 hci_dev_test_flag(hdev, HCI_SC_ENABLED))) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005023 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5024 MGMT_STATUS_REJECTED);
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08005025 goto unlock;
5026 }
Johan Hedberg0663ca22013-10-02 13:43:14 +03005027 }
5028
Johan Hedberg333ae952015-03-17 13:48:47 +02005029 if (pending_find(MGMT_OP_SET_BREDR, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005030 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5031 MGMT_STATUS_BUSY);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005032 goto unlock;
5033 }
5034
5035 cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len);
5036 if (!cmd) {
5037 err = -ENOMEM;
5038 goto unlock;
5039 }
5040
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07005041 /* We need to flip the bit already here so that update_adv_data
Johan Hedberg0663ca22013-10-02 13:43:14 +03005042 * generates the correct flags.
5043 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005044 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005045
5046 hci_req_init(&req, hdev);
Johan Hedbergaa8af462013-10-14 21:15:26 +03005047
Johan Hedberg432df052014-08-01 11:13:31 +03005048 write_fast_connectable(&req, false);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02005049 __hci_update_page_scan(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03005050
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07005051 /* Since only the advertising data flags will change, there
5052 * is no need to update the scan response data.
5053 */
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07005054 update_adv_data(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03005055
Johan Hedberg0663ca22013-10-02 13:43:14 +03005056 err = hci_req_run(&req, set_bredr_complete);
5057 if (err < 0)
5058 mgmt_pending_remove(cmd);
5059
5060unlock:
5061 hci_dev_unlock(hdev);
5062 return err;
5063}
5064
Johan Hedberga1443f52015-01-23 15:42:46 +02005065static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
5066{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005067 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02005068 struct mgmt_mode *cp;
5069
5070 BT_DBG("%s status %u", hdev->name, status);
5071
5072 hci_dev_lock(hdev);
5073
Johan Hedberg333ae952015-03-17 13:48:47 +02005074 cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev);
Johan Hedberga1443f52015-01-23 15:42:46 +02005075 if (!cmd)
5076 goto unlock;
5077
5078 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005079 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
5080 mgmt_status(status));
Johan Hedberga1443f52015-01-23 15:42:46 +02005081 goto remove;
5082 }
5083
5084 cp = cmd->param;
5085
5086 switch (cp->val) {
5087 case 0x00:
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005088 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
5089 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02005090 break;
5091 case 0x01:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005092 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005093 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02005094 break;
5095 case 0x02:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005096 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
5097 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02005098 break;
5099 }
5100
5101 send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev);
5102 new_settings(hdev, cmd->sk);
5103
5104remove:
5105 mgmt_pending_remove(cmd);
5106unlock:
5107 hci_dev_unlock(hdev);
5108}
5109
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005110static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
5111 void *data, u16 len)
5112{
5113 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005114 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02005115 struct hci_request req;
Johan Hedberga3209692014-05-26 11:23:35 +03005116 u8 val;
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005117 int err;
5118
5119 BT_DBG("request for %s", hdev->name);
5120
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08005121 if (!lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005122 !hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02005123 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5124 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005125
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005126 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Johan Hedberg59200282015-01-28 19:56:00 +02005127 lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005128 !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02005129 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5130 MGMT_STATUS_REJECTED);
Marcel Holtmanned93ec62015-01-22 11:15:22 -08005131
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005132 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02005133 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005134 MGMT_STATUS_INVALID_PARAMS);
5135
5136 hci_dev_lock(hdev);
5137
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08005138 if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005139 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005140 bool changed;
5141
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005142 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07005143 changed = !hci_dev_test_and_set_flag(hdev,
5144 HCI_SC_ENABLED);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005145 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005146 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005147 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005148 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005149 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005150 changed = hci_dev_test_and_clear_flag(hdev,
5151 HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005152 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005153 }
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005154
5155 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
5156 if (err < 0)
5157 goto failed;
5158
5159 if (changed)
5160 err = new_settings(hdev, sk);
5161
5162 goto failed;
5163 }
5164
Johan Hedberg333ae952015-03-17 13:48:47 +02005165 if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005166 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5167 MGMT_STATUS_BUSY);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005168 goto failed;
5169 }
5170
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005171 val = !!cp->val;
5172
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005173 if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
5174 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005175 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
5176 goto failed;
5177 }
5178
5179 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len);
5180 if (!cmd) {
5181 err = -ENOMEM;
5182 goto failed;
5183 }
5184
Johan Hedberga1443f52015-01-23 15:42:46 +02005185 hci_req_init(&req, hdev);
5186 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val);
5187 err = hci_req_run(&req, sc_enable_complete);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005188 if (err < 0) {
5189 mgmt_pending_remove(cmd);
5190 goto failed;
5191 }
5192
5193failed:
5194 hci_dev_unlock(hdev);
5195 return err;
5196}
5197
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005198static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
5199 void *data, u16 len)
5200{
5201 struct mgmt_mode *cp = data;
Johan Hedbergb97109792014-06-24 14:00:28 +03005202 bool changed, use_changed;
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005203 int err;
5204
5205 BT_DBG("request for %s", hdev->name);
5206
Johan Hedbergb97109792014-06-24 14:00:28 +03005207 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02005208 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
5209 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005210
5211 hci_dev_lock(hdev);
5212
5213 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07005214 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005215 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005216 changed = hci_dev_test_and_clear_flag(hdev,
5217 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005218
Johan Hedbergb97109792014-06-24 14:00:28 +03005219 if (cp->val == 0x02)
Marcel Holtmann238be782015-03-13 02:11:06 -07005220 use_changed = !hci_dev_test_and_set_flag(hdev,
5221 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03005222 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005223 use_changed = hci_dev_test_and_clear_flag(hdev,
5224 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03005225
5226 if (hdev_is_powered(hdev) && use_changed &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005227 hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergb97109792014-06-24 14:00:28 +03005228 u8 mode = (cp->val == 0x02) ? 0x01 : 0x00;
5229 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
5230 sizeof(mode), &mode);
5231 }
5232
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005233 err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev);
5234 if (err < 0)
5235 goto unlock;
5236
5237 if (changed)
5238 err = new_settings(hdev, sk);
5239
5240unlock:
5241 hci_dev_unlock(hdev);
5242 return err;
5243}
5244
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005245static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5246 u16 len)
5247{
5248 struct mgmt_cp_set_privacy *cp = cp_data;
5249 bool changed;
5250 int err;
5251
5252 BT_DBG("request for %s", hdev->name);
5253
5254 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005255 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5256 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005257
5258 if (cp->privacy != 0x00 && cp->privacy != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02005259 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5260 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005261
5262 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005263 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5264 MGMT_STATUS_REJECTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005265
5266 hci_dev_lock(hdev);
5267
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02005268 /* If user space supports this command it is also expected to
5269 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
5270 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005271 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02005272
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005273 if (cp->privacy) {
Marcel Holtmann238be782015-03-13 02:11:06 -07005274 changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005275 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005276 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005277 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005278 changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005279 memset(hdev->irk, 0, sizeof(hdev->irk));
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005280 hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005281 }
5282
5283 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
5284 if (err < 0)
5285 goto unlock;
5286
5287 if (changed)
5288 err = new_settings(hdev, sk);
5289
5290unlock:
5291 hci_dev_unlock(hdev);
5292 return err;
5293}
5294
Johan Hedberg41edf162014-02-18 10:19:35 +02005295static bool irk_is_valid(struct mgmt_irk_info *irk)
5296{
5297 switch (irk->addr.type) {
5298 case BDADDR_LE_PUBLIC:
5299 return true;
5300
5301 case BDADDR_LE_RANDOM:
5302 /* Two most significant bits shall be set */
5303 if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0)
5304 return false;
5305 return true;
5306 }
5307
5308 return false;
5309}
5310
5311static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5312 u16 len)
5313{
5314 struct mgmt_cp_load_irks *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005315 const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) /
5316 sizeof(struct mgmt_irk_info));
Johan Hedberg41edf162014-02-18 10:19:35 +02005317 u16 irk_count, expected_len;
5318 int i, err;
5319
5320 BT_DBG("request for %s", hdev->name);
5321
5322 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005323 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5324 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg41edf162014-02-18 10:19:35 +02005325
5326 irk_count = __le16_to_cpu(cp->irk_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005327 if (irk_count > max_irk_count) {
5328 BT_ERR("load_irks: too big irk_count value %u", irk_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02005329 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5330 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005331 }
Johan Hedberg41edf162014-02-18 10:19:35 +02005332
5333 expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info);
5334 if (expected_len != len) {
5335 BT_ERR("load_irks: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02005336 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02005337 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5338 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02005339 }
5340
5341 BT_DBG("%s irk_count %u", hdev->name, irk_count);
5342
5343 for (i = 0; i < irk_count; i++) {
5344 struct mgmt_irk_info *key = &cp->irks[i];
5345
5346 if (!irk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02005347 return mgmt_cmd_status(sk, hdev->id,
5348 MGMT_OP_LOAD_IRKS,
5349 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02005350 }
5351
5352 hci_dev_lock(hdev);
5353
5354 hci_smp_irks_clear(hdev);
5355
5356 for (i = 0; i < irk_count; i++) {
5357 struct mgmt_irk_info *irk = &cp->irks[i];
5358 u8 addr_type;
5359
5360 if (irk->addr.type == BDADDR_LE_PUBLIC)
5361 addr_type = ADDR_LE_DEV_PUBLIC;
5362 else
5363 addr_type = ADDR_LE_DEV_RANDOM;
5364
5365 hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val,
5366 BDADDR_ANY);
5367 }
5368
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005369 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedberg41edf162014-02-18 10:19:35 +02005370
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005371 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
Johan Hedberg41edf162014-02-18 10:19:35 +02005372
5373 hci_dev_unlock(hdev);
5374
5375 return err;
5376}
5377
Johan Hedberg3f706b72013-01-20 14:27:16 +02005378static bool ltk_is_valid(struct mgmt_ltk_info *key)
5379{
5380 if (key->master != 0x00 && key->master != 0x01)
5381 return false;
Marcel Holtmann490cb0b2014-02-16 12:59:05 -08005382
5383 switch (key->addr.type) {
5384 case BDADDR_LE_PUBLIC:
5385 return true;
5386
5387 case BDADDR_LE_RANDOM:
5388 /* Two most significant bits shall be set */
5389 if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0)
5390 return false;
5391 return true;
5392 }
5393
5394 return false;
Johan Hedberg3f706b72013-01-20 14:27:16 +02005395}
5396
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005397static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005398 void *cp_data, u16 len)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005399{
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005400 struct mgmt_cp_load_long_term_keys *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005401 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
5402 sizeof(struct mgmt_ltk_info));
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005403 u16 key_count, expected_len;
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005404 int i, err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005405
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07005406 BT_DBG("request for %s", hdev->name);
5407
5408 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005409 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5410 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07005411
Marcel Holtmann1f350c82012-03-12 20:31:08 -07005412 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005413 if (key_count > max_key_count) {
5414 BT_ERR("load_ltks: too big key_count value %u", key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02005415 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5416 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005417 }
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005418
5419 expected_len = sizeof(*cp) + key_count *
5420 sizeof(struct mgmt_ltk_info);
5421 if (expected_len != len) {
5422 BT_ERR("load_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02005423 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02005424 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5425 MGMT_STATUS_INVALID_PARAMS);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005426 }
5427
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005428 BT_DBG("%s key_count %u", hdev->name, key_count);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005429
Johan Hedberg54ad6d82013-01-20 14:27:15 +02005430 for (i = 0; i < key_count; i++) {
5431 struct mgmt_ltk_info *key = &cp->keys[i];
5432
Johan Hedberg3f706b72013-01-20 14:27:16 +02005433 if (!ltk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02005434 return mgmt_cmd_status(sk, hdev->id,
5435 MGMT_OP_LOAD_LONG_TERM_KEYS,
5436 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg54ad6d82013-01-20 14:27:15 +02005437 }
5438
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005439 hci_dev_lock(hdev);
5440
5441 hci_smp_ltks_clear(hdev);
5442
5443 for (i = 0; i < key_count; i++) {
5444 struct mgmt_ltk_info *key = &cp->keys[i];
Johan Hedbergd7b25452014-05-23 13:19:53 +03005445 u8 type, addr_type, authenticated;
Marcel Holtmann79d95a12013-10-13 03:57:38 -07005446
5447 if (key->addr.type == BDADDR_LE_PUBLIC)
5448 addr_type = ADDR_LE_DEV_PUBLIC;
5449 else
5450 addr_type = ADDR_LE_DEV_RANDOM;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005451
Johan Hedberg61b43352014-05-29 19:36:53 +03005452 switch (key->type) {
5453 case MGMT_LTK_UNAUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03005454 authenticated = 0x00;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005455 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03005456 break;
5457 case MGMT_LTK_AUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03005458 authenticated = 0x01;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005459 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03005460 break;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005461 case MGMT_LTK_P256_UNAUTH:
5462 authenticated = 0x00;
5463 type = SMP_LTK_P256;
5464 break;
5465 case MGMT_LTK_P256_AUTH:
5466 authenticated = 0x01;
5467 type = SMP_LTK_P256;
5468 break;
5469 case MGMT_LTK_P256_DEBUG:
5470 authenticated = 0x00;
5471 type = SMP_LTK_P256_DEBUG;
Johan Hedberg61b43352014-05-29 19:36:53 +03005472 default:
5473 continue;
5474 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03005475
Johan Hedberg35d70272014-02-19 14:57:47 +02005476 hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type,
Johan Hedbergd7b25452014-05-23 13:19:53 +03005477 authenticated, key->val, key->enc_size, key->ediv,
Johan Hedberg35d70272014-02-19 14:57:47 +02005478 key->rand);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005479 }
5480
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005481 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005482 NULL, 0);
5483
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005484 hci_dev_unlock(hdev);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005485
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005486 return err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005487}
5488
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005489static int conn_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005490{
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005491 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005492 struct mgmt_rp_get_conn_info rp;
Johan Hedberg9df74652014-12-19 22:26:03 +02005493 int err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005494
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005495 memcpy(&rp.addr, cmd->param, sizeof(rp.addr));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005496
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005497 if (status == MGMT_STATUS_SUCCESS) {
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005498 rp.rssi = conn->rssi;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005499 rp.tx_power = conn->tx_power;
5500 rp.max_tx_power = conn->max_tx_power;
5501 } else {
5502 rp.rssi = HCI_RSSI_INVALID;
5503 rp.tx_power = HCI_TX_POWER_INVALID;
5504 rp.max_tx_power = HCI_TX_POWER_INVALID;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005505 }
5506
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005507 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO,
5508 status, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005509
5510 hci_conn_drop(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005511 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02005512
5513 return err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005514}
5515
Marcel Holtmann1904a852015-01-11 13:50:44 -08005516static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status,
5517 u16 opcode)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005518{
5519 struct hci_cp_read_rssi *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005520 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005521 struct hci_conn *conn;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005522 u16 handle;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005523 u8 status;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005524
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005525 BT_DBG("status 0x%02x", hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005526
5527 hci_dev_lock(hdev);
5528
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005529 /* Commands sent in request are either Read RSSI or Read Transmit Power
5530 * Level so we check which one was last sent to retrieve connection
5531 * handle. Both commands have handle as first parameter so it's safe to
5532 * cast data on the same command struct.
5533 *
5534 * First command sent is always Read RSSI and we fail only if it fails.
5535 * In other case we simply override error to indicate success as we
5536 * already remembered if TX power value is actually valid.
5537 */
5538 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI);
5539 if (!cp) {
5540 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005541 status = MGMT_STATUS_SUCCESS;
5542 } else {
5543 status = mgmt_status(hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005544 }
5545
5546 if (!cp) {
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005547 BT_ERR("invalid sent_cmd in conn_info response");
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005548 goto unlock;
5549 }
5550
5551 handle = __le16_to_cpu(cp->handle);
5552 conn = hci_conn_hash_lookup_handle(hdev, handle);
5553 if (!conn) {
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005554 BT_ERR("unknown handle (%d) in conn_info response", handle);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005555 goto unlock;
5556 }
5557
Johan Hedberg333ae952015-03-17 13:48:47 +02005558 cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005559 if (!cmd)
5560 goto unlock;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005561
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005562 cmd->cmd_complete(cmd, status);
5563 mgmt_pending_remove(cmd);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005564
5565unlock:
5566 hci_dev_unlock(hdev);
5567}
5568
5569static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
5570 u16 len)
5571{
5572 struct mgmt_cp_get_conn_info *cp = data;
5573 struct mgmt_rp_get_conn_info rp;
5574 struct hci_conn *conn;
5575 unsigned long conn_info_age;
5576 int err = 0;
5577
5578 BT_DBG("%s", hdev->name);
5579
5580 memset(&rp, 0, sizeof(rp));
5581 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5582 rp.addr.type = cp->addr.type;
5583
5584 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005585 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5586 MGMT_STATUS_INVALID_PARAMS,
5587 &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005588
5589 hci_dev_lock(hdev);
5590
5591 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005592 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5593 MGMT_STATUS_NOT_POWERED, &rp,
5594 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005595 goto unlock;
5596 }
5597
5598 if (cp->addr.type == BDADDR_BREDR)
5599 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5600 &cp->addr.bdaddr);
5601 else
5602 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
5603
5604 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005605 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5606 MGMT_STATUS_NOT_CONNECTED, &rp,
5607 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005608 goto unlock;
5609 }
5610
Johan Hedberg333ae952015-03-17 13:48:47 +02005611 if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005612 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5613 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005614 goto unlock;
5615 }
5616
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005617 /* To avoid client trying to guess when to poll again for information we
5618 * calculate conn info age as random value between min/max set in hdev.
5619 */
5620 conn_info_age = hdev->conn_info_min_age +
5621 prandom_u32_max(hdev->conn_info_max_age -
5622 hdev->conn_info_min_age);
5623
5624 /* Query controller to refresh cached values if they are too old or were
5625 * never read.
5626 */
Andrzej Kaczmarekf4e2dd52014-05-16 16:48:57 +02005627 if (time_after(jiffies, conn->conn_info_timestamp +
5628 msecs_to_jiffies(conn_info_age)) ||
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005629 !conn->conn_info_timestamp) {
5630 struct hci_request req;
5631 struct hci_cp_read_tx_power req_txp_cp;
5632 struct hci_cp_read_rssi req_rssi_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005633 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005634
5635 hci_req_init(&req, hdev);
5636 req_rssi_cp.handle = cpu_to_le16(conn->handle);
5637 hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp),
5638 &req_rssi_cp);
5639
Andrzej Kaczmarekf7faab02014-05-14 13:43:04 +02005640 /* For LE links TX power does not change thus we don't need to
5641 * query for it once value is known.
5642 */
5643 if (!bdaddr_type_is_le(cp->addr.type) ||
5644 conn->tx_power == HCI_TX_POWER_INVALID) {
5645 req_txp_cp.handle = cpu_to_le16(conn->handle);
5646 req_txp_cp.type = 0x00;
5647 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5648 sizeof(req_txp_cp), &req_txp_cp);
5649 }
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005650
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005651 /* Max TX power needs to be read only once per connection */
5652 if (conn->max_tx_power == HCI_TX_POWER_INVALID) {
5653 req_txp_cp.handle = cpu_to_le16(conn->handle);
5654 req_txp_cp.type = 0x01;
5655 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5656 sizeof(req_txp_cp), &req_txp_cp);
5657 }
5658
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005659 err = hci_req_run(&req, conn_info_refresh_complete);
5660 if (err < 0)
5661 goto unlock;
5662
5663 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev,
5664 data, len);
5665 if (!cmd) {
5666 err = -ENOMEM;
5667 goto unlock;
5668 }
5669
5670 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005671 cmd->user_data = hci_conn_get(conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005672 cmd->cmd_complete = conn_info_cmd_complete;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005673
5674 conn->conn_info_timestamp = jiffies;
5675 } else {
5676 /* Cache is valid, just reply with values cached in hci_conn */
5677 rp.rssi = conn->rssi;
5678 rp.tx_power = conn->tx_power;
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005679 rp.max_tx_power = conn->max_tx_power;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005680
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005681 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5682 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005683 }
5684
5685unlock:
5686 hci_dev_unlock(hdev);
5687 return err;
5688}
5689
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005690static int clock_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg69487372014-12-05 13:36:07 +02005691{
5692 struct hci_conn *conn = cmd->user_data;
5693 struct mgmt_rp_get_clock_info rp;
5694 struct hci_dev *hdev;
Johan Hedberg9df74652014-12-19 22:26:03 +02005695 int err;
Johan Hedberg69487372014-12-05 13:36:07 +02005696
5697 memset(&rp, 0, sizeof(rp));
5698 memcpy(&rp.addr, &cmd->param, sizeof(rp.addr));
5699
5700 if (status)
5701 goto complete;
5702
5703 hdev = hci_dev_get(cmd->index);
5704 if (hdev) {
5705 rp.local_clock = cpu_to_le32(hdev->clock);
5706 hci_dev_put(hdev);
5707 }
5708
5709 if (conn) {
5710 rp.piconet_clock = cpu_to_le32(conn->clock);
5711 rp.accuracy = cpu_to_le16(conn->clock_accuracy);
5712 }
5713
5714complete:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005715 err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp,
5716 sizeof(rp));
Johan Hedberg69487372014-12-05 13:36:07 +02005717
5718 if (conn) {
5719 hci_conn_drop(conn);
5720 hci_conn_put(conn);
5721 }
Johan Hedberg9df74652014-12-19 22:26:03 +02005722
5723 return err;
Johan Hedberg69487372014-12-05 13:36:07 +02005724}
5725
Marcel Holtmann1904a852015-01-11 13:50:44 -08005726static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg95868422014-06-28 17:54:07 +03005727{
Johan Hedberg95868422014-06-28 17:54:07 +03005728 struct hci_cp_read_clock *hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005729 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03005730 struct hci_conn *conn;
5731
5732 BT_DBG("%s status %u", hdev->name, status);
5733
5734 hci_dev_lock(hdev);
5735
5736 hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
5737 if (!hci_cp)
5738 goto unlock;
5739
5740 if (hci_cp->which) {
5741 u16 handle = __le16_to_cpu(hci_cp->handle);
5742 conn = hci_conn_hash_lookup_handle(hdev, handle);
5743 } else {
5744 conn = NULL;
5745 }
5746
Johan Hedberg333ae952015-03-17 13:48:47 +02005747 cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn);
Johan Hedberg95868422014-06-28 17:54:07 +03005748 if (!cmd)
5749 goto unlock;
5750
Johan Hedberg69487372014-12-05 13:36:07 +02005751 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberg95868422014-06-28 17:54:07 +03005752 mgmt_pending_remove(cmd);
Johan Hedberg95868422014-06-28 17:54:07 +03005753
5754unlock:
5755 hci_dev_unlock(hdev);
5756}
5757
5758static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
5759 u16 len)
5760{
5761 struct mgmt_cp_get_clock_info *cp = data;
5762 struct mgmt_rp_get_clock_info rp;
5763 struct hci_cp_read_clock hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005764 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03005765 struct hci_request req;
5766 struct hci_conn *conn;
5767 int err;
5768
5769 BT_DBG("%s", hdev->name);
5770
5771 memset(&rp, 0, sizeof(rp));
5772 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5773 rp.addr.type = cp->addr.type;
5774
5775 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005776 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5777 MGMT_STATUS_INVALID_PARAMS,
5778 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005779
5780 hci_dev_lock(hdev);
5781
5782 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005783 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5784 MGMT_STATUS_NOT_POWERED, &rp,
5785 sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005786 goto unlock;
5787 }
5788
5789 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
5790 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5791 &cp->addr.bdaddr);
5792 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005793 err = mgmt_cmd_complete(sk, hdev->id,
5794 MGMT_OP_GET_CLOCK_INFO,
5795 MGMT_STATUS_NOT_CONNECTED,
5796 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005797 goto unlock;
5798 }
5799 } else {
5800 conn = NULL;
5801 }
5802
5803 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len);
5804 if (!cmd) {
5805 err = -ENOMEM;
5806 goto unlock;
5807 }
5808
Johan Hedberg69487372014-12-05 13:36:07 +02005809 cmd->cmd_complete = clock_info_cmd_complete;
5810
Johan Hedberg95868422014-06-28 17:54:07 +03005811 hci_req_init(&req, hdev);
5812
5813 memset(&hci_cp, 0, sizeof(hci_cp));
5814 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5815
5816 if (conn) {
5817 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005818 cmd->user_data = hci_conn_get(conn);
Johan Hedberg95868422014-06-28 17:54:07 +03005819
5820 hci_cp.handle = cpu_to_le16(conn->handle);
5821 hci_cp.which = 0x01; /* Piconet clock */
5822 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5823 }
5824
5825 err = hci_req_run(&req, get_clock_info_complete);
5826 if (err < 0)
5827 mgmt_pending_remove(cmd);
5828
5829unlock:
5830 hci_dev_unlock(hdev);
5831 return err;
5832}
5833
Johan Hedberg5a154e62014-12-19 22:26:02 +02005834static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
5835{
5836 struct hci_conn *conn;
5837
5838 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
5839 if (!conn)
5840 return false;
5841
5842 if (conn->dst_type != type)
5843 return false;
5844
5845 if (conn->state != BT_CONNECTED)
5846 return false;
5847
5848 return true;
5849}
5850
5851/* This function requires the caller holds hdev->lock */
5852static int hci_conn_params_set(struct hci_request *req, bdaddr_t *addr,
5853 u8 addr_type, u8 auto_connect)
5854{
5855 struct hci_dev *hdev = req->hdev;
5856 struct hci_conn_params *params;
5857
5858 params = hci_conn_params_add(hdev, addr, addr_type);
5859 if (!params)
5860 return -EIO;
5861
5862 if (params->auto_connect == auto_connect)
5863 return 0;
5864
5865 list_del_init(&params->action);
5866
5867 switch (auto_connect) {
5868 case HCI_AUTO_CONN_DISABLED:
5869 case HCI_AUTO_CONN_LINK_LOSS:
5870 __hci_update_background_scan(req);
5871 break;
5872 case HCI_AUTO_CONN_REPORT:
5873 list_add(&params->action, &hdev->pend_le_reports);
5874 __hci_update_background_scan(req);
5875 break;
5876 case HCI_AUTO_CONN_DIRECT:
5877 case HCI_AUTO_CONN_ALWAYS:
5878 if (!is_connected(hdev, addr, addr_type)) {
5879 list_add(&params->action, &hdev->pend_le_conns);
5880 __hci_update_background_scan(req);
5881 }
5882 break;
5883 }
5884
5885 params->auto_connect = auto_connect;
5886
5887 BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
5888 auto_connect);
5889
5890 return 0;
5891}
5892
Marcel Holtmann8afef092014-06-29 22:28:34 +02005893static void device_added(struct sock *sk, struct hci_dev *hdev,
5894 bdaddr_t *bdaddr, u8 type, u8 action)
5895{
5896 struct mgmt_ev_device_added ev;
5897
5898 bacpy(&ev.addr.bdaddr, bdaddr);
5899 ev.addr.type = type;
5900 ev.action = action;
5901
5902 mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk);
5903}
5904
Marcel Holtmann1904a852015-01-11 13:50:44 -08005905static void add_device_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg5a154e62014-12-19 22:26:02 +02005906{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005907 struct mgmt_pending_cmd *cmd;
Johan Hedberg5a154e62014-12-19 22:26:02 +02005908
5909 BT_DBG("status 0x%02x", status);
5910
5911 hci_dev_lock(hdev);
5912
Johan Hedberg333ae952015-03-17 13:48:47 +02005913 cmd = pending_find(MGMT_OP_ADD_DEVICE, hdev);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005914 if (!cmd)
5915 goto unlock;
5916
5917 cmd->cmd_complete(cmd, mgmt_status(status));
5918 mgmt_pending_remove(cmd);
5919
5920unlock:
5921 hci_dev_unlock(hdev);
5922}
5923
Marcel Holtmann2faade52014-06-29 19:44:03 +02005924static int add_device(struct sock *sk, struct hci_dev *hdev,
5925 void *data, u16 len)
5926{
5927 struct mgmt_cp_add_device *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005928 struct mgmt_pending_cmd *cmd;
Johan Hedberg5a154e62014-12-19 22:26:02 +02005929 struct hci_request req;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005930 u8 auto_conn, addr_type;
5931 int err;
5932
5933 BT_DBG("%s", hdev->name);
5934
Johan Hedberg66593582014-07-09 12:59:14 +03005935 if (!bdaddr_type_is_valid(cp->addr.type) ||
Marcel Holtmann2faade52014-06-29 19:44:03 +02005936 !bacmp(&cp->addr.bdaddr, BDADDR_ANY))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005937 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5938 MGMT_STATUS_INVALID_PARAMS,
5939 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005940
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005941 if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005942 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5943 MGMT_STATUS_INVALID_PARAMS,
5944 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005945
Johan Hedberg5a154e62014-12-19 22:26:02 +02005946 hci_req_init(&req, hdev);
5947
Marcel Holtmann2faade52014-06-29 19:44:03 +02005948 hci_dev_lock(hdev);
5949
Johan Hedberg5a154e62014-12-19 22:26:02 +02005950 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_DEVICE, hdev, data, len);
5951 if (!cmd) {
5952 err = -ENOMEM;
5953 goto unlock;
5954 }
5955
5956 cmd->cmd_complete = addr_cmd_complete;
5957
Johan Hedberg66593582014-07-09 12:59:14 +03005958 if (cp->addr.type == BDADDR_BREDR) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005959 /* Only incoming connections action is supported for now */
Johan Hedberg66593582014-07-09 12:59:14 +03005960 if (cp->action != 0x01) {
Johan Hedberg9df74652014-12-19 22:26:03 +02005961 err = cmd->cmd_complete(cmd,
5962 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005963 mgmt_pending_remove(cmd);
Johan Hedberg66593582014-07-09 12:59:14 +03005964 goto unlock;
5965 }
5966
5967 err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr,
5968 cp->addr.type);
5969 if (err)
5970 goto unlock;
Johan Hedberga3974072014-07-09 12:59:15 +03005971
Johan Hedberg5a154e62014-12-19 22:26:02 +02005972 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03005973
Johan Hedberg66593582014-07-09 12:59:14 +03005974 goto added;
5975 }
5976
Marcel Holtmann2faade52014-06-29 19:44:03 +02005977 if (cp->addr.type == BDADDR_LE_PUBLIC)
5978 addr_type = ADDR_LE_DEV_PUBLIC;
5979 else
5980 addr_type = ADDR_LE_DEV_RANDOM;
5981
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005982 if (cp->action == 0x02)
Marcel Holtmann2faade52014-06-29 19:44:03 +02005983 auto_conn = HCI_AUTO_CONN_ALWAYS;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005984 else if (cp->action == 0x01)
5985 auto_conn = HCI_AUTO_CONN_DIRECT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005986 else
Johan Hedberga3451d22014-07-02 17:37:27 +03005987 auto_conn = HCI_AUTO_CONN_REPORT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005988
Marcel Holtmannbf5b3c82014-06-30 12:34:39 +02005989 /* If the connection parameters don't exist for this device,
5990 * they will be created and configured with defaults.
5991 */
Johan Hedberg5a154e62014-12-19 22:26:02 +02005992 if (hci_conn_params_set(&req, &cp->addr.bdaddr, addr_type,
Marcel Holtmannd06b50c2014-07-01 12:11:06 +02005993 auto_conn) < 0) {
Johan Hedberg9df74652014-12-19 22:26:03 +02005994 err = cmd->cmd_complete(cmd, MGMT_STATUS_FAILED);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005995 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005996 goto unlock;
5997 }
5998
Johan Hedberg66593582014-07-09 12:59:14 +03005999added:
Marcel Holtmann8afef092014-06-29 22:28:34 +02006000 device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
6001
Johan Hedberg5a154e62014-12-19 22:26:02 +02006002 err = hci_req_run(&req, add_device_complete);
6003 if (err < 0) {
6004 /* ENODATA means no HCI commands were needed (e.g. if
6005 * the adapter is powered off).
6006 */
Johan Hedberg9df74652014-12-19 22:26:03 +02006007 if (err == -ENODATA)
6008 err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS);
Johan Hedberg5a154e62014-12-19 22:26:02 +02006009 mgmt_pending_remove(cmd);
6010 }
Marcel Holtmann2faade52014-06-29 19:44:03 +02006011
6012unlock:
6013 hci_dev_unlock(hdev);
6014 return err;
6015}
6016
Marcel Holtmann8afef092014-06-29 22:28:34 +02006017static void device_removed(struct sock *sk, struct hci_dev *hdev,
6018 bdaddr_t *bdaddr, u8 type)
6019{
6020 struct mgmt_ev_device_removed ev;
6021
6022 bacpy(&ev.addr.bdaddr, bdaddr);
6023 ev.addr.type = type;
6024
6025 mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk);
6026}
6027
Marcel Holtmann1904a852015-01-11 13:50:44 -08006028static void remove_device_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006029{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006030 struct mgmt_pending_cmd *cmd;
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006031
6032 BT_DBG("status 0x%02x", status);
6033
6034 hci_dev_lock(hdev);
6035
Johan Hedberg333ae952015-03-17 13:48:47 +02006036 cmd = pending_find(MGMT_OP_REMOVE_DEVICE, hdev);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006037 if (!cmd)
6038 goto unlock;
6039
6040 cmd->cmd_complete(cmd, mgmt_status(status));
6041 mgmt_pending_remove(cmd);
6042
6043unlock:
6044 hci_dev_unlock(hdev);
6045}
6046
Marcel Holtmann2faade52014-06-29 19:44:03 +02006047static int remove_device(struct sock *sk, struct hci_dev *hdev,
6048 void *data, u16 len)
6049{
6050 struct mgmt_cp_remove_device *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006051 struct mgmt_pending_cmd *cmd;
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006052 struct hci_request req;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006053 int err;
6054
6055 BT_DBG("%s", hdev->name);
6056
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006057 hci_req_init(&req, hdev);
6058
Marcel Holtmann2faade52014-06-29 19:44:03 +02006059 hci_dev_lock(hdev);
6060
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006061 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_DEVICE, hdev, data, len);
6062 if (!cmd) {
6063 err = -ENOMEM;
6064 goto unlock;
6065 }
6066
6067 cmd->cmd_complete = addr_cmd_complete;
6068
Marcel Holtmann2faade52014-06-29 19:44:03 +02006069 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
Johan Hedbergc71593d2014-07-02 17:37:28 +03006070 struct hci_conn_params *params;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006071 u8 addr_type;
6072
Johan Hedberg66593582014-07-09 12:59:14 +03006073 if (!bdaddr_type_is_valid(cp->addr.type)) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006074 err = cmd->cmd_complete(cmd,
6075 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006076 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006077 goto unlock;
6078 }
6079
Johan Hedberg66593582014-07-09 12:59:14 +03006080 if (cp->addr.type == BDADDR_BREDR) {
6081 err = hci_bdaddr_list_del(&hdev->whitelist,
6082 &cp->addr.bdaddr,
6083 cp->addr.type);
6084 if (err) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006085 err = cmd->cmd_complete(cmd,
6086 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006087 mgmt_pending_remove(cmd);
Johan Hedberg66593582014-07-09 12:59:14 +03006088 goto unlock;
6089 }
6090
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006091 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03006092
Johan Hedberg66593582014-07-09 12:59:14 +03006093 device_removed(sk, hdev, &cp->addr.bdaddr,
6094 cp->addr.type);
6095 goto complete;
6096 }
6097
Marcel Holtmann2faade52014-06-29 19:44:03 +02006098 if (cp->addr.type == BDADDR_LE_PUBLIC)
6099 addr_type = ADDR_LE_DEV_PUBLIC;
6100 else
6101 addr_type = ADDR_LE_DEV_RANDOM;
6102
Johan Hedbergc71593d2014-07-02 17:37:28 +03006103 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
6104 addr_type);
6105 if (!params) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006106 err = cmd->cmd_complete(cmd,
6107 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006108 mgmt_pending_remove(cmd);
Johan Hedbergc71593d2014-07-02 17:37:28 +03006109 goto unlock;
6110 }
6111
6112 if (params->auto_connect == HCI_AUTO_CONN_DISABLED) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006113 err = cmd->cmd_complete(cmd,
6114 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006115 mgmt_pending_remove(cmd);
Johan Hedbergc71593d2014-07-02 17:37:28 +03006116 goto unlock;
6117 }
6118
Johan Hedbergd1dbf122014-07-04 16:17:23 +03006119 list_del(&params->action);
Johan Hedbergc71593d2014-07-02 17:37:28 +03006120 list_del(&params->list);
6121 kfree(params);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006122 __hci_update_background_scan(&req);
Marcel Holtmann8afef092014-06-29 22:28:34 +02006123
6124 device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006125 } else {
Johan Hedberg19de0822014-07-06 13:06:51 +03006126 struct hci_conn_params *p, *tmp;
Johan Hedberg66593582014-07-09 12:59:14 +03006127 struct bdaddr_list *b, *btmp;
Johan Hedberg19de0822014-07-06 13:06:51 +03006128
Marcel Holtmann2faade52014-06-29 19:44:03 +02006129 if (cp->addr.type) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006130 err = cmd->cmd_complete(cmd,
6131 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006132 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006133 goto unlock;
6134 }
6135
Johan Hedberg66593582014-07-09 12:59:14 +03006136 list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) {
6137 device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type);
6138 list_del(&b->list);
6139 kfree(b);
6140 }
6141
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006142 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03006143
Johan Hedberg19de0822014-07-06 13:06:51 +03006144 list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) {
6145 if (p->auto_connect == HCI_AUTO_CONN_DISABLED)
6146 continue;
6147 device_removed(sk, hdev, &p->addr, p->addr_type);
6148 list_del(&p->action);
6149 list_del(&p->list);
6150 kfree(p);
6151 }
6152
6153 BT_DBG("All LE connection parameters were removed");
6154
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006155 __hci_update_background_scan(&req);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006156 }
6157
Johan Hedberg66593582014-07-09 12:59:14 +03006158complete:
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006159 err = hci_req_run(&req, remove_device_complete);
6160 if (err < 0) {
6161 /* ENODATA means no HCI commands were needed (e.g. if
6162 * the adapter is powered off).
6163 */
Johan Hedberg9df74652014-12-19 22:26:03 +02006164 if (err == -ENODATA)
6165 err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006166 mgmt_pending_remove(cmd);
6167 }
Marcel Holtmann2faade52014-06-29 19:44:03 +02006168
6169unlock:
6170 hci_dev_unlock(hdev);
6171 return err;
6172}
6173
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006174static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
6175 u16 len)
6176{
6177 struct mgmt_cp_load_conn_param *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03006178 const u16 max_param_count = ((U16_MAX - sizeof(*cp)) /
6179 sizeof(struct mgmt_conn_param));
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006180 u16 param_count, expected_len;
6181 int i;
6182
6183 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006184 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6185 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006186
6187 param_count = __le16_to_cpu(cp->param_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03006188 if (param_count > max_param_count) {
6189 BT_ERR("load_conn_param: too big param_count value %u",
6190 param_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02006191 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6192 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03006193 }
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006194
6195 expected_len = sizeof(*cp) + param_count *
6196 sizeof(struct mgmt_conn_param);
6197 if (expected_len != len) {
6198 BT_ERR("load_conn_param: expected %u bytes, got %u bytes",
6199 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02006200 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6201 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006202 }
6203
6204 BT_DBG("%s param_count %u", hdev->name, param_count);
6205
6206 hci_dev_lock(hdev);
6207
6208 hci_conn_params_clear_disabled(hdev);
6209
6210 for (i = 0; i < param_count; i++) {
6211 struct mgmt_conn_param *param = &cp->params[i];
6212 struct hci_conn_params *hci_param;
6213 u16 min, max, latency, timeout;
6214 u8 addr_type;
6215
6216 BT_DBG("Adding %pMR (type %u)", &param->addr.bdaddr,
6217 param->addr.type);
6218
6219 if (param->addr.type == BDADDR_LE_PUBLIC) {
6220 addr_type = ADDR_LE_DEV_PUBLIC;
6221 } else if (param->addr.type == BDADDR_LE_RANDOM) {
6222 addr_type = ADDR_LE_DEV_RANDOM;
6223 } else {
6224 BT_ERR("Ignoring invalid connection parameters");
6225 continue;
6226 }
6227
6228 min = le16_to_cpu(param->min_interval);
6229 max = le16_to_cpu(param->max_interval);
6230 latency = le16_to_cpu(param->latency);
6231 timeout = le16_to_cpu(param->timeout);
6232
6233 BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x",
6234 min, max, latency, timeout);
6235
6236 if (hci_check_conn_params(min, max, latency, timeout) < 0) {
6237 BT_ERR("Ignoring invalid connection parameters");
6238 continue;
6239 }
6240
6241 hci_param = hci_conn_params_add(hdev, &param->addr.bdaddr,
6242 addr_type);
6243 if (!hci_param) {
6244 BT_ERR("Failed to add connection parameters");
6245 continue;
6246 }
6247
6248 hci_param->conn_min_interval = min;
6249 hci_param->conn_max_interval = max;
6250 hci_param->conn_latency = latency;
6251 hci_param->supervision_timeout = timeout;
6252 }
6253
6254 hci_dev_unlock(hdev);
6255
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006256 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0,
6257 NULL, 0);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006258}
6259
Marcel Holtmanndbece372014-07-04 18:11:55 +02006260static int set_external_config(struct sock *sk, struct hci_dev *hdev,
6261 void *data, u16 len)
6262{
6263 struct mgmt_cp_set_external_config *cp = data;
6264 bool changed;
6265 int err;
6266
6267 BT_DBG("%s", hdev->name);
6268
6269 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006270 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6271 MGMT_STATUS_REJECTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006272
6273 if (cp->config != 0x00 && cp->config != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02006274 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6275 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006276
6277 if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
Johan Hedberga69e8372015-03-06 21:08:53 +02006278 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6279 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006280
6281 hci_dev_lock(hdev);
6282
6283 if (cp->config)
Marcel Holtmann238be782015-03-13 02:11:06 -07006284 changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006285 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07006286 changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006287
6288 err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
6289 if (err < 0)
6290 goto unlock;
6291
6292 if (!changed)
6293 goto unlock;
6294
Marcel Holtmannf4537c02014-07-04 19:06:23 +02006295 err = new_options(hdev, sk);
6296
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006297 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) {
Marcel Holtmanndbece372014-07-04 18:11:55 +02006298 mgmt_index_removed(hdev);
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02006299
Marcel Holtmann516018a2015-03-13 02:11:04 -07006300 if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006301 hci_dev_set_flag(hdev, HCI_CONFIG);
6302 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02006303
6304 queue_work(hdev->req_workqueue, &hdev->power_on);
6305 } else {
Marcel Holtmann5ea234d2014-07-06 12:11:16 +02006306 set_bit(HCI_RAW, &hdev->flags);
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02006307 mgmt_index_added(hdev);
6308 }
Marcel Holtmanndbece372014-07-04 18:11:55 +02006309 }
6310
6311unlock:
6312 hci_dev_unlock(hdev);
6313 return err;
6314}
6315
Marcel Holtmann9713c172014-07-06 12:11:15 +02006316static int set_public_address(struct sock *sk, struct hci_dev *hdev,
6317 void *data, u16 len)
6318{
6319 struct mgmt_cp_set_public_address *cp = data;
6320 bool changed;
6321 int err;
6322
6323 BT_DBG("%s", hdev->name);
6324
6325 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006326 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6327 MGMT_STATUS_REJECTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006328
6329 if (!bacmp(&cp->bdaddr, BDADDR_ANY))
Johan Hedberga69e8372015-03-06 21:08:53 +02006330 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6331 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006332
6333 if (!hdev->set_bdaddr)
Johan Hedberga69e8372015-03-06 21:08:53 +02006334 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6335 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006336
6337 hci_dev_lock(hdev);
6338
6339 changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
6340 bacpy(&hdev->public_addr, &cp->bdaddr);
6341
6342 err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev);
6343 if (err < 0)
6344 goto unlock;
6345
6346 if (!changed)
6347 goto unlock;
6348
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006349 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
Marcel Holtmann9713c172014-07-06 12:11:15 +02006350 err = new_options(hdev, sk);
6351
6352 if (is_configured(hdev)) {
6353 mgmt_index_removed(hdev);
6354
Marcel Holtmanna358dc12015-03-13 02:11:02 -07006355 hci_dev_clear_flag(hdev, HCI_UNCONFIGURED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006356
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006357 hci_dev_set_flag(hdev, HCI_CONFIG);
6358 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006359
6360 queue_work(hdev->req_workqueue, &hdev->power_on);
6361 }
6362
6363unlock:
6364 hci_dev_unlock(hdev);
6365 return err;
6366}
6367
Marcel Holtmannbea41602015-03-14 22:43:17 -07006368static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
6369 u8 data_len)
6370{
6371 eir[eir_len++] = sizeof(type) + data_len;
6372 eir[eir_len++] = type;
6373 memcpy(&eir[eir_len], data, data_len);
6374 eir_len += data_len;
6375
6376 return eir_len;
6377}
6378
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006379static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
6380 void *data, u16 data_len)
6381{
6382 struct mgmt_cp_read_local_oob_ext_data *cp = data;
6383 struct mgmt_rp_read_local_oob_ext_data *rp;
6384 size_t rp_len;
6385 u16 eir_len;
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006386 u8 status, flags, role, addr[7], hash[16], rand[16];
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006387 int err;
6388
6389 BT_DBG("%s", hdev->name);
6390
6391 if (!hdev_is_powered(hdev))
6392 return mgmt_cmd_complete(sk, hdev->id,
6393 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
6394 MGMT_STATUS_NOT_POWERED,
6395 &cp->type, sizeof(cp->type));
6396
6397 switch (cp->type) {
6398 case BIT(BDADDR_BREDR):
6399 status = mgmt_bredr_support(hdev);
6400 if (status)
6401 return mgmt_cmd_complete(sk, hdev->id,
6402 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
6403 status, &cp->type,
6404 sizeof(cp->type));
6405 eir_len = 5;
6406 break;
6407 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
6408 status = mgmt_le_support(hdev);
6409 if (status)
6410 return mgmt_cmd_complete(sk, hdev->id,
6411 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
6412 status, &cp->type,
6413 sizeof(cp->type));
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006414 eir_len = 9 + 3 + 18 + 18 + 3;
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006415 break;
6416 default:
6417 return mgmt_cmd_complete(sk, hdev->id,
6418 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
6419 MGMT_STATUS_INVALID_PARAMS,
6420 &cp->type, sizeof(cp->type));
6421 }
6422
6423 hci_dev_lock(hdev);
6424
6425 rp_len = sizeof(*rp) + eir_len;
6426 rp = kmalloc(rp_len, GFP_ATOMIC);
6427 if (!rp) {
6428 hci_dev_unlock(hdev);
6429 return -ENOMEM;
6430 }
6431
6432 eir_len = 0;
6433 switch (cp->type) {
6434 case BIT(BDADDR_BREDR):
6435 eir_len = eir_append_data(rp->eir, eir_len, EIR_CLASS_OF_DEV,
6436 hdev->dev_class, 3);
6437 break;
6438 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
Marcel Holtmann5082a592015-03-16 12:39:00 -07006439 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
6440 smp_generate_oob(hdev, hash, rand) < 0) {
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006441 hci_dev_unlock(hdev);
6442 err = mgmt_cmd_complete(sk, hdev->id,
Marcel Holtmann5082a592015-03-16 12:39:00 -07006443 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
6444 MGMT_STATUS_FAILED,
6445 &cp->type, sizeof(cp->type));
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006446 goto done;
6447 }
6448
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006449 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
6450 memcpy(addr, &hdev->rpa, 6);
6451 addr[6] = 0x01;
6452 } else if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
6453 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
6454 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
6455 bacmp(&hdev->static_addr, BDADDR_ANY))) {
6456 memcpy(addr, &hdev->static_addr, 6);
6457 addr[6] = 0x01;
6458 } else {
6459 memcpy(addr, &hdev->bdaddr, 6);
6460 addr[6] = 0x00;
6461 }
6462
6463 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR,
6464 addr, sizeof(addr));
6465
6466 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
6467 role = 0x02;
6468 else
6469 role = 0x01;
6470
6471 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE,
6472 &role, sizeof(role));
6473
Marcel Holtmann5082a592015-03-16 12:39:00 -07006474 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) {
6475 eir_len = eir_append_data(rp->eir, eir_len,
6476 EIR_LE_SC_CONFIRM,
6477 hash, sizeof(hash));
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006478
Marcel Holtmann5082a592015-03-16 12:39:00 -07006479 eir_len = eir_append_data(rp->eir, eir_len,
6480 EIR_LE_SC_RANDOM,
6481 rand, sizeof(rand));
6482 }
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006483
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006484 flags = get_adv_discov_flags(hdev);
6485
6486 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
6487 flags |= LE_AD_NO_BREDR;
6488
6489 eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS,
6490 &flags, sizeof(flags));
6491 break;
6492 }
6493
6494 rp->type = cp->type;
6495 rp->eir_len = cpu_to_le16(eir_len);
6496
6497 hci_dev_unlock(hdev);
6498
Marcel Holtmann72000df2015-03-16 16:11:21 -07006499 hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS);
6500
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006501 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
Marcel Holtmann5425f982015-03-16 16:05:44 -07006502 MGMT_STATUS_SUCCESS, rp, sizeof(*rp) + eir_len);
Marcel Holtmann72000df2015-03-16 16:11:21 -07006503 if (err < 0)
6504 goto done;
6505
6506 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
6507 rp, sizeof(*rp) + eir_len,
6508 HCI_MGMT_OOB_DATA_EVENTS, sk);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006509
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006510done:
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006511 kfree(rp);
6512
6513 return err;
6514}
6515
Arman Uguray089fa8c2015-03-25 18:53:45 -07006516static u32 get_supported_adv_flags(struct hci_dev *hdev)
6517{
6518 u32 flags = 0;
6519
6520 flags |= MGMT_ADV_FLAG_CONNECTABLE;
6521 flags |= MGMT_ADV_FLAG_DISCOV;
6522 flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
6523 flags |= MGMT_ADV_FLAG_MANAGED_FLAGS;
6524
6525 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID)
6526 flags |= MGMT_ADV_FLAG_TX_POWER;
6527
6528 return flags;
6529}
6530
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006531static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
6532 void *data, u16 data_len)
6533{
6534 struct mgmt_rp_read_adv_features *rp;
6535 size_t rp_len;
6536 int err;
Arman Uguray24b4f382015-03-23 15:57:12 -07006537 bool instance;
Arman Uguray089fa8c2015-03-25 18:53:45 -07006538 u32 supported_flags;
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006539
6540 BT_DBG("%s", hdev->name);
6541
Arman Uguray089fa8c2015-03-25 18:53:45 -07006542 if (!lmp_le_capable(hdev))
6543 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
6544 MGMT_STATUS_REJECTED);
6545
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006546 hci_dev_lock(hdev);
6547
6548 rp_len = sizeof(*rp);
Arman Uguray24b4f382015-03-23 15:57:12 -07006549
6550 /* Currently only one instance is supported, so just add 1 to the
6551 * response length.
6552 */
6553 instance = hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE);
6554 if (instance)
6555 rp_len++;
6556
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006557 rp = kmalloc(rp_len, GFP_ATOMIC);
6558 if (!rp) {
6559 hci_dev_unlock(hdev);
6560 return -ENOMEM;
6561 }
6562
Arman Uguray089fa8c2015-03-25 18:53:45 -07006563 supported_flags = get_supported_adv_flags(hdev);
6564
6565 rp->supported_flags = cpu_to_le32(supported_flags);
Marcel Holtmanndc5d82a2015-03-19 17:22:25 -07006566 rp->max_adv_data_len = HCI_MAX_AD_LENGTH;
6567 rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH;
Arman Uguray24b4f382015-03-23 15:57:12 -07006568 rp->max_instances = 1;
6569
6570 /* Currently only one instance is supported, so simply return the
6571 * current instance number.
6572 */
6573 if (instance) {
6574 rp->num_instances = 1;
6575 rp->instance[0] = 1;
6576 } else {
6577 rp->num_instances = 0;
6578 }
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006579
6580 hci_dev_unlock(hdev);
6581
6582 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
6583 MGMT_STATUS_SUCCESS, rp, rp_len);
6584
6585 kfree(rp);
6586
6587 return err;
6588}
6589
Arman Uguray4117ed72015-03-23 15:57:14 -07006590static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data,
Arman Ugurayb44133f2015-03-25 18:53:41 -07006591 u8 len, bool is_adv_data)
Arman Uguray24b4f382015-03-23 15:57:12 -07006592{
Arman Uguray4117ed72015-03-23 15:57:14 -07006593 u8 max_len = HCI_MAX_AD_LENGTH;
Arman Uguray24b4f382015-03-23 15:57:12 -07006594 int i, cur_len;
Arman Ugurayb44133f2015-03-25 18:53:41 -07006595 bool flags_managed = false;
Arman Uguray5507e352015-03-25 18:53:44 -07006596 bool tx_power_managed = false;
Arman Uguray67e0c0c2015-03-25 18:53:43 -07006597 u32 flags_params = MGMT_ADV_FLAG_DISCOV | MGMT_ADV_FLAG_LIMITED_DISCOV |
6598 MGMT_ADV_FLAG_MANAGED_FLAGS;
Arman Uguray24b4f382015-03-23 15:57:12 -07006599
Arman Uguray807ec772015-03-25 18:53:42 -07006600 if (is_adv_data && (adv_flags & flags_params)) {
Arman Ugurayb44133f2015-03-25 18:53:41 -07006601 flags_managed = true;
6602 max_len -= 3;
6603 }
Arman Uguray24b4f382015-03-23 15:57:12 -07006604
Arman Uguray5507e352015-03-25 18:53:44 -07006605 if (is_adv_data && (adv_flags & MGMT_ADV_FLAG_TX_POWER)) {
6606 tx_power_managed = true;
6607 max_len -= 3;
6608 }
6609
Arman Uguray4117ed72015-03-23 15:57:14 -07006610 if (len > max_len)
Arman Uguray24b4f382015-03-23 15:57:12 -07006611 return false;
6612
Arman Uguray4117ed72015-03-23 15:57:14 -07006613 /* Make sure that the data is correctly formatted. */
6614 for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) {
6615 cur_len = data[i];
Arman Uguray24b4f382015-03-23 15:57:12 -07006616
Arman Ugurayb44133f2015-03-25 18:53:41 -07006617 if (flags_managed && data[i + 1] == EIR_FLAGS)
6618 return false;
6619
Arman Uguray5507e352015-03-25 18:53:44 -07006620 if (tx_power_managed && data[i + 1] == EIR_TX_POWER)
6621 return false;
6622
Arman Uguray24b4f382015-03-23 15:57:12 -07006623 /* If the current field length would exceed the total data
6624 * length, then it's invalid.
6625 */
Arman Uguray4117ed72015-03-23 15:57:14 -07006626 if (i + cur_len >= len)
Arman Uguray24b4f382015-03-23 15:57:12 -07006627 return false;
6628 }
6629
6630 return true;
6631}
6632
Arman Uguray24b4f382015-03-23 15:57:12 -07006633static void add_advertising_complete(struct hci_dev *hdev, u8 status,
6634 u16 opcode)
6635{
6636 struct mgmt_pending_cmd *cmd;
6637 struct mgmt_rp_add_advertising rp;
6638
6639 BT_DBG("status %d", status);
6640
6641 hci_dev_lock(hdev);
6642
6643 cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev);
6644
6645 if (status) {
6646 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
6647 memset(&hdev->adv_instance, 0, sizeof(hdev->adv_instance));
6648 advertising_removed(cmd ? cmd->sk : NULL, hdev, 1);
6649 }
6650
6651 if (!cmd)
6652 goto unlock;
6653
6654 rp.instance = 0x01;
6655
6656 if (status)
6657 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
6658 mgmt_status(status));
6659 else
6660 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
6661 mgmt_status(status), &rp, sizeof(rp));
6662
6663 mgmt_pending_remove(cmd);
6664
6665unlock:
6666 hci_dev_unlock(hdev);
6667}
6668
Arman Uguray912098a2015-03-23 15:57:15 -07006669static void adv_timeout_expired(struct work_struct *work)
6670{
6671 struct hci_dev *hdev = container_of(work, struct hci_dev,
6672 adv_instance.timeout_exp.work);
6673
6674 hdev->adv_instance.timeout = 0;
6675
6676 hci_dev_lock(hdev);
6677 clear_adv_instance(hdev);
6678 hci_dev_unlock(hdev);
6679}
6680
Arman Uguray24b4f382015-03-23 15:57:12 -07006681static int add_advertising(struct sock *sk, struct hci_dev *hdev,
6682 void *data, u16 data_len)
6683{
6684 struct mgmt_cp_add_advertising *cp = data;
6685 struct mgmt_rp_add_advertising rp;
6686 u32 flags;
Arman Uguray089fa8c2015-03-25 18:53:45 -07006687 u32 supported_flags;
Arman Uguray24b4f382015-03-23 15:57:12 -07006688 u8 status;
Arman Uguray912098a2015-03-23 15:57:15 -07006689 u16 timeout;
Arman Uguray24b4f382015-03-23 15:57:12 -07006690 int err;
6691 struct mgmt_pending_cmd *cmd;
6692 struct hci_request req;
6693
6694 BT_DBG("%s", hdev->name);
6695
6696 status = mgmt_le_support(hdev);
6697 if (status)
6698 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6699 status);
6700
6701 flags = __le32_to_cpu(cp->flags);
Arman Uguray912098a2015-03-23 15:57:15 -07006702 timeout = __le16_to_cpu(cp->timeout);
Arman Uguray24b4f382015-03-23 15:57:12 -07006703
Arman Uguray089fa8c2015-03-25 18:53:45 -07006704 /* The current implementation only supports adding one instance and only
6705 * a subset of the specified flags.
6706 */
6707 supported_flags = get_supported_adv_flags(hdev);
6708 if (cp->instance != 0x01 || (flags & ~supported_flags))
Arman Uguray24b4f382015-03-23 15:57:12 -07006709 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6710 MGMT_STATUS_INVALID_PARAMS);
6711
6712 hci_dev_lock(hdev);
6713
Arman Uguray912098a2015-03-23 15:57:15 -07006714 if (timeout && !hdev_is_powered(hdev)) {
6715 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6716 MGMT_STATUS_REJECTED);
6717 goto unlock;
6718 }
6719
Arman Uguray24b4f382015-03-23 15:57:12 -07006720 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
Arman Ugurayda9293352015-03-23 15:57:13 -07006721 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
Arman Uguray24b4f382015-03-23 15:57:12 -07006722 pending_find(MGMT_OP_SET_LE, hdev)) {
6723 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6724 MGMT_STATUS_BUSY);
6725 goto unlock;
6726 }
6727
Arman Ugurayb44133f2015-03-25 18:53:41 -07006728 if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) ||
Arman Uguray4117ed72015-03-23 15:57:14 -07006729 !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len,
Arman Ugurayb44133f2015-03-25 18:53:41 -07006730 cp->scan_rsp_len, false)) {
Arman Uguray24b4f382015-03-23 15:57:12 -07006731 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6732 MGMT_STATUS_INVALID_PARAMS);
6733 goto unlock;
6734 }
6735
Arman Uguray912098a2015-03-23 15:57:15 -07006736 INIT_DELAYED_WORK(&hdev->adv_instance.timeout_exp, adv_timeout_expired);
6737
Arman Uguray24b4f382015-03-23 15:57:12 -07006738 hdev->adv_instance.flags = flags;
6739 hdev->adv_instance.adv_data_len = cp->adv_data_len;
6740 hdev->adv_instance.scan_rsp_len = cp->scan_rsp_len;
6741
6742 if (cp->adv_data_len)
6743 memcpy(hdev->adv_instance.adv_data, cp->data, cp->adv_data_len);
6744
6745 if (cp->scan_rsp_len)
6746 memcpy(hdev->adv_instance.scan_rsp_data,
6747 cp->data + cp->adv_data_len, cp->scan_rsp_len);
6748
Arman Uguray912098a2015-03-23 15:57:15 -07006749 if (hdev->adv_instance.timeout)
6750 cancel_delayed_work(&hdev->adv_instance.timeout_exp);
6751
6752 hdev->adv_instance.timeout = timeout;
6753
6754 if (timeout)
6755 queue_delayed_work(hdev->workqueue,
6756 &hdev->adv_instance.timeout_exp,
6757 msecs_to_jiffies(timeout * 1000));
6758
Arman Uguray24b4f382015-03-23 15:57:12 -07006759 if (!hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING_INSTANCE))
6760 advertising_added(sk, hdev, 1);
6761
6762 /* If the HCI_ADVERTISING flag is set or the device isn't powered then
6763 * we have no HCI communication to make. Simply return.
6764 */
6765 if (!hdev_is_powered(hdev) ||
6766 hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
6767 rp.instance = 0x01;
6768 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6769 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
6770 goto unlock;
6771 }
6772
6773 /* We're good to go, update advertising data, parameters, and start
6774 * advertising.
6775 */
6776 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data,
6777 data_len);
6778 if (!cmd) {
6779 err = -ENOMEM;
6780 goto unlock;
6781 }
6782
6783 hci_req_init(&req, hdev);
6784
6785 update_adv_data(&req);
Arman Uguray4117ed72015-03-23 15:57:14 -07006786 update_scan_rsp_data(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07006787 enable_advertising(&req);
6788
6789 err = hci_req_run(&req, add_advertising_complete);
6790 if (err < 0)
6791 mgmt_pending_remove(cmd);
6792
6793unlock:
6794 hci_dev_unlock(hdev);
6795
6796 return err;
6797}
6798
Arman Ugurayda9293352015-03-23 15:57:13 -07006799static void remove_advertising_complete(struct hci_dev *hdev, u8 status,
6800 u16 opcode)
6801{
6802 struct mgmt_pending_cmd *cmd;
6803 struct mgmt_rp_remove_advertising rp;
6804
6805 BT_DBG("status %d", status);
6806
6807 hci_dev_lock(hdev);
6808
6809 /* A failure status here only means that we failed to disable
6810 * advertising. Otherwise, the advertising instance has been removed,
6811 * so report success.
6812 */
6813 cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev);
6814 if (!cmd)
6815 goto unlock;
6816
6817 rp.instance = 1;
6818
6819 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS,
6820 &rp, sizeof(rp));
6821 mgmt_pending_remove(cmd);
6822
6823unlock:
6824 hci_dev_unlock(hdev);
6825}
6826
6827static int remove_advertising(struct sock *sk, struct hci_dev *hdev,
6828 void *data, u16 data_len)
6829{
6830 struct mgmt_cp_remove_advertising *cp = data;
6831 struct mgmt_rp_remove_advertising rp;
6832 int err;
6833 struct mgmt_pending_cmd *cmd;
6834 struct hci_request req;
6835
6836 BT_DBG("%s", hdev->name);
6837
6838 /* The current implementation only allows modifying instance no 1. A
6839 * value of 0 indicates that all instances should be cleared.
6840 */
6841 if (cp->instance > 1)
6842 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
6843 MGMT_STATUS_INVALID_PARAMS);
6844
6845 hci_dev_lock(hdev);
6846
6847 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
6848 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
6849 pending_find(MGMT_OP_SET_LE, hdev)) {
6850 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
6851 MGMT_STATUS_BUSY);
6852 goto unlock;
6853 }
6854
6855 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE)) {
6856 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
6857 MGMT_STATUS_INVALID_PARAMS);
6858 goto unlock;
6859 }
6860
Arman Uguray912098a2015-03-23 15:57:15 -07006861 if (hdev->adv_instance.timeout)
6862 cancel_delayed_work(&hdev->adv_instance.timeout_exp);
6863
Arman Ugurayda9293352015-03-23 15:57:13 -07006864 memset(&hdev->adv_instance, 0, sizeof(hdev->adv_instance));
6865
6866 advertising_removed(sk, hdev, 1);
6867
6868 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
6869
6870 /* If the HCI_ADVERTISING flag is set or the device isn't powered then
6871 * we have no HCI communication to make. Simply return.
6872 */
6873 if (!hdev_is_powered(hdev) ||
6874 hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
6875 rp.instance = 1;
6876 err = mgmt_cmd_complete(sk, hdev->id,
6877 MGMT_OP_REMOVE_ADVERTISING,
6878 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
6879 goto unlock;
6880 }
6881
6882 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data,
6883 data_len);
6884 if (!cmd) {
6885 err = -ENOMEM;
6886 goto unlock;
6887 }
6888
6889 hci_req_init(&req, hdev);
6890 disable_advertising(&req);
6891
6892 err = hci_req_run(&req, remove_advertising_complete);
6893 if (err < 0)
6894 mgmt_pending_remove(cmd);
6895
6896unlock:
6897 hci_dev_unlock(hdev);
6898
6899 return err;
6900}
6901
Johan Hedberg6d785aa32015-03-06 21:08:51 +02006902static const struct hci_mgmt_handler mgmt_handlers[] = {
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02006903 { NULL }, /* 0x0000 (no command) */
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006904 { read_version, MGMT_READ_VERSION_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006905 HCI_MGMT_NO_HDEV |
6906 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006907 { read_commands, MGMT_READ_COMMANDS_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006908 HCI_MGMT_NO_HDEV |
6909 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006910 { read_index_list, MGMT_READ_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006911 HCI_MGMT_NO_HDEV |
6912 HCI_MGMT_UNTRUSTED },
6913 { read_controller_info, MGMT_READ_INFO_SIZE,
6914 HCI_MGMT_UNTRUSTED },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006915 { set_powered, MGMT_SETTING_SIZE },
6916 { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE },
6917 { set_connectable, MGMT_SETTING_SIZE },
6918 { set_fast_connectable, MGMT_SETTING_SIZE },
6919 { set_bondable, MGMT_SETTING_SIZE },
6920 { set_link_security, MGMT_SETTING_SIZE },
6921 { set_ssp, MGMT_SETTING_SIZE },
6922 { set_hs, MGMT_SETTING_SIZE },
6923 { set_le, MGMT_SETTING_SIZE },
6924 { set_dev_class, MGMT_SET_DEV_CLASS_SIZE },
6925 { set_local_name, MGMT_SET_LOCAL_NAME_SIZE },
6926 { add_uuid, MGMT_ADD_UUID_SIZE },
6927 { remove_uuid, MGMT_REMOVE_UUID_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006928 { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE,
6929 HCI_MGMT_VAR_LEN },
6930 { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE,
6931 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006932 { disconnect, MGMT_DISCONNECT_SIZE },
6933 { get_connections, MGMT_GET_CONNECTIONS_SIZE },
6934 { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE },
6935 { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE },
6936 { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE },
6937 { pair_device, MGMT_PAIR_DEVICE_SIZE },
6938 { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE },
6939 { unpair_device, MGMT_UNPAIR_DEVICE_SIZE },
6940 { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE },
6941 { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
6942 { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE },
6943 { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006944 { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE },
6945 { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE,
6946 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006947 { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
6948 { start_discovery, MGMT_START_DISCOVERY_SIZE },
6949 { stop_discovery, MGMT_STOP_DISCOVERY_SIZE },
6950 { confirm_name, MGMT_CONFIRM_NAME_SIZE },
6951 { block_device, MGMT_BLOCK_DEVICE_SIZE },
6952 { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE },
6953 { set_device_id, MGMT_SET_DEVICE_ID_SIZE },
6954 { set_advertising, MGMT_SETTING_SIZE },
6955 { set_bredr, MGMT_SETTING_SIZE },
6956 { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE },
6957 { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE },
6958 { set_secure_conn, MGMT_SETTING_SIZE },
6959 { set_debug_keys, MGMT_SETTING_SIZE },
6960 { set_privacy, MGMT_SET_PRIVACY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006961 { load_irks, MGMT_LOAD_IRKS_SIZE,
6962 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006963 { get_conn_info, MGMT_GET_CONN_INFO_SIZE },
6964 { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE },
6965 { add_device, MGMT_ADD_DEVICE_SIZE },
6966 { remove_device, MGMT_REMOVE_DEVICE_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006967 { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE,
6968 HCI_MGMT_VAR_LEN },
6969 { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006970 HCI_MGMT_NO_HDEV |
6971 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006972 { read_config_info, MGMT_READ_CONFIG_INFO_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006973 HCI_MGMT_UNCONFIGURED |
6974 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006975 { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE,
6976 HCI_MGMT_UNCONFIGURED },
6977 { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE,
6978 HCI_MGMT_UNCONFIGURED },
6979 { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE,
6980 HCI_MGMT_VAR_LEN },
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006981 { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE },
Marcel Holtmann96f14742015-03-14 19:27:57 -07006982 { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006983 HCI_MGMT_NO_HDEV |
6984 HCI_MGMT_UNTRUSTED },
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006985 { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE },
Arman Uguray24b4f382015-03-23 15:57:12 -07006986 { add_advertising, MGMT_ADD_ADVERTISING_SIZE,
6987 HCI_MGMT_VAR_LEN },
Arman Ugurayda9293352015-03-23 15:57:13 -07006988 { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE },
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02006989};
6990
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07006991void mgmt_index_added(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02006992{
Marcel Holtmannced85542015-03-14 19:27:56 -07006993 struct mgmt_ev_ext_index ev;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +03006994
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02006995 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
6996 return;
6997
Marcel Holtmannf9207332015-03-14 19:27:55 -07006998 switch (hdev->dev_type) {
6999 case HCI_BREDR:
7000 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
7001 mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev,
7002 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007003 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007004 } else {
7005 mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0,
7006 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007007 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007008 }
7009 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07007010 case HCI_AMP:
7011 ev.type = 0x02;
7012 break;
7013 default:
7014 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007015 }
Marcel Holtmannced85542015-03-14 19:27:56 -07007016
7017 ev.bus = hdev->bus;
7018
7019 mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev),
7020 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergc71e97b2010-12-13 21:07:07 +02007021}
7022
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07007023void mgmt_index_removed(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02007024{
Marcel Holtmannced85542015-03-14 19:27:56 -07007025 struct mgmt_ev_ext_index ev;
Johan Hedberg5f159032012-03-02 03:13:19 +02007026 u8 status = MGMT_STATUS_INVALID_INDEX;
Johan Hedbergb24752f2011-11-03 14:40:33 +02007027
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02007028 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
7029 return;
7030
Marcel Holtmannf9207332015-03-14 19:27:55 -07007031 switch (hdev->dev_type) {
7032 case HCI_BREDR:
7033 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedbergb24752f2011-11-03 14:40:33 +02007034
Marcel Holtmannf9207332015-03-14 19:27:55 -07007035 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
7036 mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev,
7037 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007038 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007039 } else {
7040 mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0,
7041 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007042 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007043 }
7044 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07007045 case HCI_AMP:
7046 ev.type = 0x02;
7047 break;
7048 default:
7049 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007050 }
Marcel Holtmannced85542015-03-14 19:27:56 -07007051
7052 ev.bus = hdev->bus;
7053
7054 mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev),
7055 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02007056}
7057
Andre Guedes6046dc32014-02-26 20:21:51 -03007058/* This function requires the caller holds hdev->lock */
Johan Hedberg2cf22212014-12-19 22:26:00 +02007059static void restart_le_actions(struct hci_request *req)
Andre Guedes6046dc32014-02-26 20:21:51 -03007060{
Johan Hedberg2cf22212014-12-19 22:26:00 +02007061 struct hci_dev *hdev = req->hdev;
Andre Guedes6046dc32014-02-26 20:21:51 -03007062 struct hci_conn_params *p;
7063
7064 list_for_each_entry(p, &hdev->le_conn_params, list) {
Johan Hedbergd7347f32014-07-04 12:37:23 +03007065 /* Needed for AUTO_OFF case where might not "really"
7066 * have been powered off.
7067 */
7068 list_del_init(&p->action);
7069
7070 switch (p->auto_connect) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02007071 case HCI_AUTO_CONN_DIRECT:
Johan Hedbergd7347f32014-07-04 12:37:23 +03007072 case HCI_AUTO_CONN_ALWAYS:
7073 list_add(&p->action, &hdev->pend_le_conns);
7074 break;
7075 case HCI_AUTO_CONN_REPORT:
7076 list_add(&p->action, &hdev->pend_le_reports);
7077 break;
7078 default:
7079 break;
Marcel Holtmannc83ed192014-07-01 19:28:24 +02007080 }
Andre Guedes6046dc32014-02-26 20:21:51 -03007081 }
Marcel Holtmannc83ed192014-07-01 19:28:24 +02007082
Johan Hedberg2cf22212014-12-19 22:26:00 +02007083 __hci_update_background_scan(req);
Andre Guedes6046dc32014-02-26 20:21:51 -03007084}
7085
Marcel Holtmann1904a852015-01-11 13:50:44 -08007086static void powered_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg229ab392013-03-15 17:06:53 -05007087{
7088 struct cmd_lookup match = { NULL, hdev };
7089
7090 BT_DBG("status 0x%02x", status);
7091
Marcel Holtmann162a3ba2015-01-14 15:43:11 -08007092 if (!status) {
7093 /* Register the available SMP channels (BR/EDR and LE) only
7094 * when successfully powering on the controller. This late
7095 * registration is required so that LE SMP can clearly
7096 * decide if the public address or static address is used.
7097 */
7098 smp_register(hdev);
7099 }
7100
Johan Hedberg229ab392013-03-15 17:06:53 -05007101 hci_dev_lock(hdev);
7102
7103 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
7104
7105 new_settings(hdev, match.sk);
7106
7107 hci_dev_unlock(hdev);
7108
7109 if (match.sk)
7110 sock_put(match.sk);
7111}
7112
Johan Hedberg70da6242013-03-15 17:06:51 -05007113static int powered_update_hci(struct hci_dev *hdev)
7114{
Johan Hedberg890ea892013-03-15 17:06:52 -05007115 struct hci_request req;
Johan Hedberg70da6242013-03-15 17:06:51 -05007116 u8 link_sec;
7117
Johan Hedberg890ea892013-03-15 17:06:52 -05007118 hci_req_init(&req, hdev);
7119
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007120 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
Johan Hedberg70da6242013-03-15 17:06:51 -05007121 !lmp_host_ssp_capable(hdev)) {
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08007122 u8 mode = 0x01;
Johan Hedberg70da6242013-03-15 17:06:51 -05007123
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08007124 hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode);
Johan Hedberg70da6242013-03-15 17:06:51 -05007125
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08007126 if (bredr_sc_enabled(hdev) && !lmp_host_sc_capable(hdev)) {
7127 u8 support = 0x01;
7128
7129 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT,
7130 sizeof(support), &support);
7131 }
Johan Hedbergec6f99b2014-12-12 13:30:11 +02007132 }
7133
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007134 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
Johan Hedbergc73eee92013-04-19 18:35:21 +03007135 lmp_bredr_capable(hdev)) {
Johan Hedberg70da6242013-03-15 17:06:51 -05007136 struct hci_cp_write_le_host_supported cp;
7137
Marcel Holtmann32226e42014-07-24 20:04:16 +02007138 cp.le = 0x01;
7139 cp.simul = 0x00;
Johan Hedberg70da6242013-03-15 17:06:51 -05007140
7141 /* Check first if we already have the right
7142 * host state (host features set)
7143 */
7144 if (cp.le != lmp_host_le_capable(hdev) ||
7145 cp.simul != lmp_host_le_br_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05007146 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED,
7147 sizeof(cp), &cp);
Johan Hedberg70da6242013-03-15 17:06:51 -05007148 }
7149
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07007150 if (lmp_le_capable(hdev)) {
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07007151 /* Make sure the controller has a good default for
7152 * advertising data. This also applies to the case
7153 * where BR/EDR was toggled during the AUTO_OFF phase.
7154 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007155 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07007156 update_adv_data(&req);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07007157 update_scan_rsp_data(&req);
7158 }
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07007159
Arman Uguray24b4f382015-03-23 15:57:12 -07007160 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
7161 hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07007162 enable_advertising(&req);
Johan Hedberg2cf22212014-12-19 22:26:00 +02007163
7164 restart_le_actions(&req);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03007165 }
7166
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007167 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg70da6242013-03-15 17:06:51 -05007168 if (link_sec != test_bit(HCI_AUTH, &hdev->flags))
Johan Hedberg890ea892013-03-15 17:06:52 -05007169 hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE,
7170 sizeof(link_sec), &link_sec);
Johan Hedberg70da6242013-03-15 17:06:51 -05007171
7172 if (lmp_bredr_capable(hdev)) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007173 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
Johan Hedberg406ef2a2015-03-10 20:14:27 +02007174 write_fast_connectable(&req, true);
7175 else
7176 write_fast_connectable(&req, false);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02007177 __hci_update_page_scan(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05007178 update_class(&req);
Johan Hedberg13928972013-03-15 17:07:00 -05007179 update_name(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05007180 update_eir(&req);
Johan Hedberg70da6242013-03-15 17:06:51 -05007181 }
7182
Johan Hedberg229ab392013-03-15 17:06:53 -05007183 return hci_req_run(&req, powered_complete);
Johan Hedberg70da6242013-03-15 17:06:51 -05007184}
7185
Johan Hedberg744cf192011-11-08 20:40:14 +02007186int mgmt_powered(struct hci_dev *hdev, u8 powered)
Johan Hedberg5add6af2010-12-16 10:00:37 +02007187{
Johan Hedberg76a7f3a2012-02-17 00:34:40 +02007188 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg98459042014-12-12 11:15:21 +02007189 u8 status, zero_cod[] = { 0, 0, 0 };
Johan Hedberg7bb895d2012-02-17 01:20:00 +02007190 int err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02007191
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007192 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg5e5282b2012-02-21 16:01:30 +02007193 return 0;
7194
Johan Hedberg5e5282b2012-02-21 16:01:30 +02007195 if (powered) {
Johan Hedberg229ab392013-03-15 17:06:53 -05007196 if (powered_update_hci(hdev) == 0)
7197 return 0;
Johan Hedbergfe038882013-01-16 16:15:34 +02007198
Johan Hedberg229ab392013-03-15 17:06:53 -05007199 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp,
7200 &match);
7201 goto new_settings;
Johan Hedbergb24752f2011-11-03 14:40:33 +02007202 }
7203
Johan Hedberg229ab392013-03-15 17:06:53 -05007204 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
Johan Hedberg98459042014-12-12 11:15:21 +02007205
7206 /* If the power off is because of hdev unregistration let
7207 * use the appropriate INVALID_INDEX status. Otherwise use
7208 * NOT_POWERED. We cover both scenarios here since later in
7209 * mgmt_index_removed() any hci_conn callbacks will have already
7210 * been triggered, potentially causing misleading DISCONNECTED
7211 * status responses.
7212 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007213 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
Johan Hedberg98459042014-12-12 11:15:21 +02007214 status = MGMT_STATUS_INVALID_INDEX;
7215 else
7216 status = MGMT_STATUS_NOT_POWERED;
7217
7218 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedberg229ab392013-03-15 17:06:53 -05007219
7220 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0)
Marcel Holtmannf6b77122015-03-14 19:28:05 -07007221 mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
7222 zero_cod, sizeof(zero_cod), NULL);
Johan Hedberg229ab392013-03-15 17:06:53 -05007223
7224new_settings:
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02007225 err = new_settings(hdev, match.sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02007226
7227 if (match.sk)
7228 sock_put(match.sk);
7229
Johan Hedberg7bb895d2012-02-17 01:20:00 +02007230 return err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02007231}
Johan Hedberg73f22f62010-12-29 16:00:25 +02007232
Marcel Holtmann3eec7052013-10-06 23:55:46 -07007233void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
Johan Hedberg96570ff2013-05-29 09:51:29 +03007234{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007235 struct mgmt_pending_cmd *cmd;
Johan Hedberg96570ff2013-05-29 09:51:29 +03007236 u8 status;
7237
Johan Hedberg333ae952015-03-17 13:48:47 +02007238 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg96570ff2013-05-29 09:51:29 +03007239 if (!cmd)
Marcel Holtmann3eec7052013-10-06 23:55:46 -07007240 return;
Johan Hedberg96570ff2013-05-29 09:51:29 +03007241
7242 if (err == -ERFKILL)
7243 status = MGMT_STATUS_RFKILLED;
7244 else
7245 status = MGMT_STATUS_FAILED;
7246
Johan Hedberga69e8372015-03-06 21:08:53 +02007247 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
Johan Hedberg96570ff2013-05-29 09:51:29 +03007248
7249 mgmt_pending_remove(cmd);
Johan Hedberg96570ff2013-05-29 09:51:29 +03007250}
7251
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007252void mgmt_discoverable_timeout(struct hci_dev *hdev)
7253{
7254 struct hci_request req;
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007255
7256 hci_dev_lock(hdev);
7257
7258 /* When discoverable timeout triggers, then just make sure
7259 * the limited discoverable flag is cleared. Even in the case
7260 * of a timeout triggered from general discoverable, it is
7261 * safe to unconditionally clear the flag.
7262 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07007263 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
7264 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007265
7266 hci_req_init(&req, hdev);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007267 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg4b580612013-10-19 23:38:21 +03007268 u8 scan = SCAN_PAGE;
7269 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE,
7270 sizeof(scan), &scan);
7271 }
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007272 update_class(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07007273
7274 /* Advertising instances don't use the global discoverable setting, so
7275 * only update AD if advertising was enabled using Set Advertising.
7276 */
7277 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
7278 update_adv_data(&req);
7279
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007280 hci_req_run(&req, NULL);
7281
7282 hdev->discov_timeout = 0;
7283
Johan Hedberg9a43e252013-10-20 19:00:07 +03007284 new_settings(hdev, NULL);
7285
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007286 hci_dev_unlock(hdev);
7287}
7288
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07007289void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
7290 bool persistent)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007291{
Johan Hedberg86742e12011-11-07 23:13:38 +02007292 struct mgmt_ev_new_link_key ev;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007293
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007294 memset(&ev, 0, sizeof(ev));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007295
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007296 ev.store_hint = persistent;
Johan Hedbergd753fdc2012-02-17 14:06:34 +02007297 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03007298 ev.key.addr.type = BDADDR_BREDR;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007299 ev.key.type = key->type;
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03007300 memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007301 ev.key.pin_len = key->pin_len;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007302
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07007303 mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007304}
Johan Hedbergf7520542011-01-20 12:34:39 +02007305
Johan Hedbergd7b25452014-05-23 13:19:53 +03007306static u8 mgmt_ltk_type(struct smp_ltk *ltk)
7307{
Johan Hedberg23fb8de2014-05-23 13:15:37 +03007308 switch (ltk->type) {
7309 case SMP_LTK:
7310 case SMP_LTK_SLAVE:
7311 if (ltk->authenticated)
7312 return MGMT_LTK_AUTHENTICATED;
7313 return MGMT_LTK_UNAUTHENTICATED;
7314 case SMP_LTK_P256:
7315 if (ltk->authenticated)
7316 return MGMT_LTK_P256_AUTH;
7317 return MGMT_LTK_P256_UNAUTH;
7318 case SMP_LTK_P256_DEBUG:
7319 return MGMT_LTK_P256_DEBUG;
7320 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03007321
7322 return MGMT_LTK_UNAUTHENTICATED;
7323}
7324
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007325void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007326{
7327 struct mgmt_ev_new_long_term_key ev;
7328
7329 memset(&ev, 0, sizeof(ev));
7330
Marcel Holtmann5192d302014-02-19 17:11:58 -08007331 /* Devices using resolvable or non-resolvable random addresses
7332 * without providing an indentity resolving key don't require
7333 * to store long term keys. Their addresses will change the
7334 * next time around.
7335 *
7336 * Only when a remote device provides an identity address
7337 * make sure the long term key is stored. If the remote
7338 * identity is known, the long term keys are internally
7339 * mapped to the identity address. So allow static random
7340 * and public addresses here.
7341 */
Johan Hedbergba74b662014-02-19 14:57:45 +02007342 if (key->bdaddr_type == ADDR_LE_DEV_RANDOM &&
7343 (key->bdaddr.b[5] & 0xc0) != 0xc0)
7344 ev.store_hint = 0x00;
7345 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007346 ev.store_hint = persistent;
Johan Hedbergba74b662014-02-19 14:57:45 +02007347
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007348 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007349 ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
Johan Hedbergd7b25452014-05-23 13:19:53 +03007350 ev.key.type = mgmt_ltk_type(key);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007351 ev.key.enc_size = key->enc_size;
7352 ev.key.ediv = key->ediv;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08007353 ev.key.rand = key->rand;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007354
Johan Hedberg2ceba532014-06-16 19:25:16 +03007355 if (key->type == SMP_LTK)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007356 ev.key.master = 1;
7357
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007358 memcpy(ev.key.val, key->val, sizeof(key->val));
7359
Marcel Holtmann083368f2013-10-15 14:26:29 -07007360 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007361}
7362
Johan Hedberg95fbac82014-02-19 15:18:31 +02007363void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk)
7364{
7365 struct mgmt_ev_new_irk ev;
7366
7367 memset(&ev, 0, sizeof(ev));
7368
Marcel Holtmannbab6d1e2014-02-19 11:51:54 -08007369 /* For identity resolving keys from devices that are already
7370 * using a public address or static random address, do not
7371 * ask for storing this key. The identity resolving key really
7372 * is only mandatory for devices using resovlable random
7373 * addresses.
7374 *
7375 * Storing all identity resolving keys has the downside that
7376 * they will be also loaded on next boot of they system. More
7377 * identity resolving keys, means more time during scanning is
7378 * needed to actually resolve these addresses.
7379 */
7380 if (bacmp(&irk->rpa, BDADDR_ANY))
7381 ev.store_hint = 0x01;
7382 else
7383 ev.store_hint = 0x00;
7384
Johan Hedberg95fbac82014-02-19 15:18:31 +02007385 bacpy(&ev.rpa, &irk->rpa);
7386 bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr);
7387 ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type);
7388 memcpy(ev.irk.val, irk->val, sizeof(irk->val));
7389
7390 mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
7391}
7392
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007393void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
7394 bool persistent)
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007395{
7396 struct mgmt_ev_new_csrk ev;
7397
7398 memset(&ev, 0, sizeof(ev));
7399
7400 /* Devices using resolvable or non-resolvable random addresses
7401 * without providing an indentity resolving key don't require
7402 * to store signature resolving keys. Their addresses will change
7403 * the next time around.
7404 *
7405 * Only when a remote device provides an identity address
7406 * make sure the signature resolving key is stored. So allow
7407 * static random and public addresses here.
7408 */
7409 if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM &&
7410 (csrk->bdaddr.b[5] & 0xc0) != 0xc0)
7411 ev.store_hint = 0x00;
7412 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007413 ev.store_hint = persistent;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007414
7415 bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
7416 ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
Johan Hedberg4cd39282015-02-27 10:11:13 +02007417 ev.key.type = csrk->type;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007418 memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
7419
7420 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
7421}
7422
Andre Guedesffb5a8272014-07-01 18:10:11 -03007423void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedbergf4869e22014-07-02 17:37:32 +03007424 u8 bdaddr_type, u8 store_hint, u16 min_interval,
7425 u16 max_interval, u16 latency, u16 timeout)
Andre Guedesffb5a8272014-07-01 18:10:11 -03007426{
7427 struct mgmt_ev_new_conn_param ev;
7428
Johan Hedbergc103aea2014-07-02 17:37:34 +03007429 if (!hci_is_identity_address(bdaddr, bdaddr_type))
7430 return;
7431
Andre Guedesffb5a8272014-07-01 18:10:11 -03007432 memset(&ev, 0, sizeof(ev));
7433 bacpy(&ev.addr.bdaddr, bdaddr);
7434 ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type);
Johan Hedbergf4869e22014-07-02 17:37:32 +03007435 ev.store_hint = store_hint;
Andre Guedesffb5a8272014-07-01 18:10:11 -03007436 ev.min_interval = cpu_to_le16(min_interval);
7437 ev.max_interval = cpu_to_le16(max_interval);
7438 ev.latency = cpu_to_le16(latency);
7439 ev.timeout = cpu_to_le16(timeout);
7440
7441 mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL);
7442}
7443
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00007444void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
7445 u32 flags, u8 *name, u8 name_len)
Johan Hedbergf7520542011-01-20 12:34:39 +02007446{
Johan Hedbergb644ba32012-01-17 21:48:47 +02007447 char buf[512];
7448 struct mgmt_ev_device_connected *ev = (void *) buf;
7449 u16 eir_len = 0;
Johan Hedbergf7520542011-01-20 12:34:39 +02007450
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00007451 bacpy(&ev->addr.bdaddr, &conn->dst);
7452 ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedbergf7520542011-01-20 12:34:39 +02007453
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02007454 ev->flags = __cpu_to_le32(flags);
Johan Hedberg08c79b62012-02-23 22:31:51 +02007455
Alfonso Acostafd45ada2014-10-07 08:44:11 +00007456 /* We must ensure that the EIR Data fields are ordered and
7457 * unique. Keep it simple for now and avoid the problem by not
7458 * adding any BR/EDR data to the LE adv.
7459 */
7460 if (conn->le_adv_data_len > 0) {
7461 memcpy(&ev->eir[eir_len],
7462 conn->le_adv_data, conn->le_adv_data_len);
7463 eir_len = conn->le_adv_data_len;
7464 } else {
7465 if (name_len > 0)
7466 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE,
7467 name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007468
Alfonso Acostaddbea5c2014-10-07 08:44:12 +00007469 if (memcmp(conn->dev_class, "\0\0\0", 3) != 0)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00007470 eir_len = eir_append_data(ev->eir, eir_len,
7471 EIR_CLASS_OF_DEV,
7472 conn->dev_class, 3);
7473 }
Johan Hedbergb644ba32012-01-17 21:48:47 +02007474
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02007475 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007476
Marcel Holtmannecd90ae2013-10-06 23:55:49 -07007477 mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf,
7478 sizeof(*ev) + eir_len, NULL);
Johan Hedbergf7520542011-01-20 12:34:39 +02007479}
7480
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007481static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg8962ee72011-01-20 12:40:27 +02007482{
Johan Hedberg8962ee72011-01-20 12:40:27 +02007483 struct sock **sk = data;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007484
Johan Hedbergf5818c22014-12-05 13:36:02 +02007485 cmd->cmd_complete(cmd, 0);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007486
7487 *sk = cmd->sk;
7488 sock_hold(*sk);
7489
Johan Hedberga664b5b2011-02-19 12:06:02 -03007490 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007491}
7492
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007493static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberga8a1d192011-11-10 15:54:38 +02007494{
Johan Hedbergb1078ad2012-02-09 17:21:16 +02007495 struct hci_dev *hdev = data;
Johan Hedberg124f6e32012-02-09 13:50:12 +02007496 struct mgmt_cp_unpair_device *cp = cmd->param;
Johan Hedberga8a1d192011-11-10 15:54:38 +02007497
Johan Hedbergb1078ad2012-02-09 17:21:16 +02007498 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
7499
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02007500 cmd->cmd_complete(cmd, 0);
Johan Hedberga8a1d192011-11-10 15:54:38 +02007501 mgmt_pending_remove(cmd);
7502}
7503
Johan Hedberg84c61d92014-08-01 11:13:30 +03007504bool mgmt_powering_down(struct hci_dev *hdev)
7505{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007506 struct mgmt_pending_cmd *cmd;
Johan Hedberg84c61d92014-08-01 11:13:30 +03007507 struct mgmt_mode *cp;
7508
Johan Hedberg333ae952015-03-17 13:48:47 +02007509 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg84c61d92014-08-01 11:13:30 +03007510 if (!cmd)
7511 return false;
7512
7513 cp = cmd->param;
7514 if (!cp->val)
7515 return true;
7516
7517 return false;
7518}
7519
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07007520void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02007521 u8 link_type, u8 addr_type, u8 reason,
7522 bool mgmt_connected)
Johan Hedbergf7520542011-01-20 12:34:39 +02007523{
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02007524 struct mgmt_ev_device_disconnected ev;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007525 struct sock *sk = NULL;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007526
Johan Hedberg84c61d92014-08-01 11:13:30 +03007527 /* The connection is still in hci_conn_hash so test for 1
7528 * instead of 0 to know if this is the last one.
7529 */
7530 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
7531 cancel_delayed_work(&hdev->power_off);
7532 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberg8b064a32014-02-24 14:52:22 +02007533 }
7534
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02007535 if (!mgmt_connected)
7536 return;
7537
Andre Guedes57eb7762013-10-30 19:01:41 -03007538 if (link_type != ACL_LINK && link_type != LE_LINK)
7539 return;
7540
Johan Hedberg744cf192011-11-08 20:40:14 +02007541 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
Johan Hedbergf7520542011-01-20 12:34:39 +02007542
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02007543 bacpy(&ev.addr.bdaddr, bdaddr);
7544 ev.addr.type = link_to_bdaddr(link_type, addr_type);
7545 ev.reason = reason;
Johan Hedbergf7520542011-01-20 12:34:39 +02007546
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07007547 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007548
7549 if (sk)
Szymon Jancd97dcb62012-03-16 16:02:56 +01007550 sock_put(sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007551
Johan Hedberg124f6e32012-02-09 13:50:12 +02007552 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007553 hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007554}
7555
Marcel Holtmann78929242013-10-06 23:55:47 -07007556void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
7557 u8 link_type, u8 addr_type, u8 status)
Johan Hedberg8962ee72011-01-20 12:40:27 +02007558{
Andre Guedes3655bba2013-10-30 19:01:40 -03007559 u8 bdaddr_type = link_to_bdaddr(link_type, addr_type);
7560 struct mgmt_cp_disconnect *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007561 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007562
Jefferson Delfes36a75f12012-09-18 13:36:54 -04007563 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
7564 hdev);
7565
Johan Hedberg333ae952015-03-17 13:48:47 +02007566 cmd = pending_find(MGMT_OP_DISCONNECT, hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007567 if (!cmd)
Marcel Holtmann78929242013-10-06 23:55:47 -07007568 return;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007569
Andre Guedes3655bba2013-10-30 19:01:40 -03007570 cp = cmd->param;
7571
7572 if (bacmp(bdaddr, &cp->addr.bdaddr))
7573 return;
7574
7575 if (cp->addr.type != bdaddr_type)
7576 return;
7577
Johan Hedbergf5818c22014-12-05 13:36:02 +02007578 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007579 mgmt_pending_remove(cmd);
Johan Hedbergf7520542011-01-20 12:34:39 +02007580}
Johan Hedberg17d5c042011-01-22 06:09:08 +02007581
Marcel Holtmann445608d2013-10-06 23:55:48 -07007582void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
7583 u8 addr_type, u8 status)
Johan Hedberg17d5c042011-01-22 06:09:08 +02007584{
7585 struct mgmt_ev_connect_failed ev;
Johan Hedbergc9910d02014-02-27 14:35:12 +02007586
Johan Hedberg84c61d92014-08-01 11:13:30 +03007587 /* The connection is still in hci_conn_hash so test for 1
7588 * instead of 0 to know if this is the last one.
7589 */
7590 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
7591 cancel_delayed_work(&hdev->power_off);
7592 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedbergc9910d02014-02-27 14:35:12 +02007593 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02007594
Johan Hedberg4c659c32011-11-07 23:13:39 +02007595 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007596 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergca69b792011-11-11 18:10:00 +02007597 ev.status = mgmt_status(status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02007598
Marcel Holtmann445608d2013-10-06 23:55:48 -07007599 mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg17d5c042011-01-22 06:09:08 +02007600}
Johan Hedberg980e1a52011-01-22 06:10:07 +02007601
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07007602void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007603{
7604 struct mgmt_ev_pin_code_request ev;
7605
Johan Hedbergd8457692012-02-17 14:24:57 +02007606 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03007607 ev.addr.type = BDADDR_BREDR;
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02007608 ev.secure = secure;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007609
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07007610 mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007611}
7612
Marcel Holtmanne669cf82013-10-15 14:26:21 -07007613void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
7614 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007615{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007616 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007617
Johan Hedberg333ae952015-03-17 13:48:47 +02007618 cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007619 if (!cmd)
Marcel Holtmanne669cf82013-10-15 14:26:21 -07007620 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007621
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007622 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007623 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007624}
7625
Marcel Holtmann3eb38522013-10-15 14:26:22 -07007626void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
7627 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007628{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007629 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007630
Johan Hedberg333ae952015-03-17 13:48:47 +02007631 cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007632 if (!cmd)
Marcel Holtmann3eb38522013-10-15 14:26:22 -07007633 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007634
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007635 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007636 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007637}
Johan Hedberga5c29682011-02-19 12:05:57 -03007638
Johan Hedberg744cf192011-11-08 20:40:14 +02007639int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg39adbff2014-03-20 08:18:14 +02007640 u8 link_type, u8 addr_type, u32 value,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007641 u8 confirm_hint)
Johan Hedberga5c29682011-02-19 12:05:57 -03007642{
7643 struct mgmt_ev_user_confirm_request ev;
7644
Johan Hedberg744cf192011-11-08 20:40:14 +02007645 BT_DBG("%s", hdev->name);
Johan Hedberga5c29682011-02-19 12:05:57 -03007646
Johan Hedberg272d90d2012-02-09 15:26:12 +02007647 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007648 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberg55bc1a32011-04-28 11:28:56 -07007649 ev.confirm_hint = confirm_hint;
Johan Hedberg39adbff2014-03-20 08:18:14 +02007650 ev.value = cpu_to_le32(value);
Johan Hedberga5c29682011-02-19 12:05:57 -03007651
Johan Hedberg744cf192011-11-08 20:40:14 +02007652 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007653 NULL);
Johan Hedberga5c29682011-02-19 12:05:57 -03007654}
7655
Johan Hedberg272d90d2012-02-09 15:26:12 +02007656int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03007657 u8 link_type, u8 addr_type)
Brian Gix604086b2011-11-23 08:28:33 -08007658{
7659 struct mgmt_ev_user_passkey_request ev;
7660
7661 BT_DBG("%s", hdev->name);
7662
Johan Hedberg272d90d2012-02-09 15:26:12 +02007663 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007664 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Brian Gix604086b2011-11-23 08:28:33 -08007665
7666 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007667 NULL);
Brian Gix604086b2011-11-23 08:28:33 -08007668}
7669
Brian Gix0df4c182011-11-16 13:53:13 -08007670static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03007671 u8 link_type, u8 addr_type, u8 status,
7672 u8 opcode)
Johan Hedberga5c29682011-02-19 12:05:57 -03007673{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007674 struct mgmt_pending_cmd *cmd;
Johan Hedberga5c29682011-02-19 12:05:57 -03007675
Johan Hedberg333ae952015-03-17 13:48:47 +02007676 cmd = pending_find(opcode, hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03007677 if (!cmd)
7678 return -ENOENT;
7679
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007680 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007681 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03007682
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007683 return 0;
Johan Hedberga5c29682011-02-19 12:05:57 -03007684}
7685
Johan Hedberg744cf192011-11-08 20:40:14 +02007686int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007687 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03007688{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007689 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007690 status, MGMT_OP_USER_CONFIRM_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03007691}
7692
Johan Hedberg272d90d2012-02-09 15:26:12 +02007693int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007694 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03007695{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007696 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03007697 status,
7698 MGMT_OP_USER_CONFIRM_NEG_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03007699}
Johan Hedberg2a611692011-02-19 12:06:00 -03007700
Brian Gix604086b2011-11-23 08:28:33 -08007701int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007702 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08007703{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007704 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007705 status, MGMT_OP_USER_PASSKEY_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08007706}
7707
Johan Hedberg272d90d2012-02-09 15:26:12 +02007708int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007709 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08007710{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007711 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03007712 status,
7713 MGMT_OP_USER_PASSKEY_NEG_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08007714}
7715
Johan Hedberg92a25252012-09-06 18:39:26 +03007716int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
7717 u8 link_type, u8 addr_type, u32 passkey,
7718 u8 entered)
7719{
7720 struct mgmt_ev_passkey_notify ev;
7721
7722 BT_DBG("%s", hdev->name);
7723
7724 bacpy(&ev.addr.bdaddr, bdaddr);
7725 ev.addr.type = link_to_bdaddr(link_type, addr_type);
7726 ev.passkey = __cpu_to_le32(passkey);
7727 ev.entered = entered;
7728
7729 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
7730}
7731
Johan Hedberge1e930f2014-09-08 17:09:49 -07007732void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status)
Johan Hedberg2a611692011-02-19 12:06:00 -03007733{
7734 struct mgmt_ev_auth_failed ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007735 struct mgmt_pending_cmd *cmd;
Johan Hedberge1e930f2014-09-08 17:09:49 -07007736 u8 status = mgmt_status(hci_status);
Johan Hedberg2a611692011-02-19 12:06:00 -03007737
Johan Hedberge1e930f2014-09-08 17:09:49 -07007738 bacpy(&ev.addr.bdaddr, &conn->dst);
7739 ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
7740 ev.status = status;
Johan Hedberg2a611692011-02-19 12:06:00 -03007741
Johan Hedberge1e930f2014-09-08 17:09:49 -07007742 cmd = find_pairing(conn);
7743
7744 mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev),
7745 cmd ? cmd->sk : NULL);
7746
Johan Hedberga511b352014-12-11 21:45:45 +02007747 if (cmd) {
7748 cmd->cmd_complete(cmd, status);
7749 mgmt_pending_remove(cmd);
7750 }
Johan Hedberg2a611692011-02-19 12:06:00 -03007751}
Johan Hedbergb312b1612011-03-16 14:29:37 +02007752
Marcel Holtmann464996a2013-10-15 14:26:24 -07007753void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007754{
7755 struct cmd_lookup match = { NULL, hdev };
Marcel Holtmann464996a2013-10-15 14:26:24 -07007756 bool changed;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007757
7758 if (status) {
7759 u8 mgmt_err = mgmt_status(status);
7760 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007761 cmd_status_rsp, &mgmt_err);
Marcel Holtmann464996a2013-10-15 14:26:24 -07007762 return;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007763 }
7764
Marcel Holtmann464996a2013-10-15 14:26:24 -07007765 if (test_bit(HCI_AUTH, &hdev->flags))
Marcel Holtmann238be782015-03-13 02:11:06 -07007766 changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY);
Marcel Holtmann464996a2013-10-15 14:26:24 -07007767 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007768 changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02007769
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007770 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007771 &match);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007772
Johan Hedberg47990ea2012-02-22 11:58:37 +02007773 if (changed)
Marcel Holtmann464996a2013-10-15 14:26:24 -07007774 new_settings(hdev, match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007775
7776 if (match.sk)
7777 sock_put(match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007778}
7779
Johan Hedberg890ea892013-03-15 17:06:52 -05007780static void clear_eir(struct hci_request *req)
Johan Hedbergcacaf522012-02-21 00:52:42 +02007781{
Johan Hedberg890ea892013-03-15 17:06:52 -05007782 struct hci_dev *hdev = req->hdev;
Johan Hedbergcacaf522012-02-21 00:52:42 +02007783 struct hci_cp_write_eir cp;
7784
Johan Hedberg976eb202012-10-24 21:12:01 +03007785 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05007786 return;
Johan Hedbergcacaf522012-02-21 00:52:42 +02007787
Johan Hedbergc80da272012-02-22 15:38:48 +02007788 memset(hdev->eir, 0, sizeof(hdev->eir));
7789
Johan Hedbergcacaf522012-02-21 00:52:42 +02007790 memset(&cp, 0, sizeof(cp));
7791
Johan Hedberg890ea892013-03-15 17:06:52 -05007792 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedbergcacaf522012-02-21 00:52:42 +02007793}
7794
Marcel Holtmann3e248562013-10-15 14:26:25 -07007795void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007796{
7797 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg890ea892013-03-15 17:06:52 -05007798 struct hci_request req;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007799 bool changed = false;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007800
7801 if (status) {
7802 u8 mgmt_err = mgmt_status(status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007803
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007804 if (enable && hci_dev_test_and_clear_flag(hdev,
7805 HCI_SSP_ENABLED)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07007806 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmann3e248562013-10-15 14:26:25 -07007807 new_settings(hdev, NULL);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007808 }
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007809
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007810 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
7811 &mgmt_err);
Marcel Holtmann3e248562013-10-15 14:26:25 -07007812 return;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007813 }
7814
7815 if (enable) {
Marcel Holtmann238be782015-03-13 02:11:06 -07007816 changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007817 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007818 changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007819 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007820 changed = hci_dev_test_and_clear_flag(hdev,
7821 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007822 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07007823 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007824 }
7825
7826 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
7827
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007828 if (changed)
Marcel Holtmann3e248562013-10-15 14:26:25 -07007829 new_settings(hdev, match.sk);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007830
Johan Hedberg5fc6ebb2012-02-22 15:10:59 +02007831 if (match.sk)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007832 sock_put(match.sk);
7833
Johan Hedberg890ea892013-03-15 17:06:52 -05007834 hci_req_init(&req, hdev);
7835
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007836 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
7837 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03007838 hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE,
7839 sizeof(enable), &enable);
Johan Hedberg890ea892013-03-15 17:06:52 -05007840 update_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03007841 } else {
Johan Hedberg890ea892013-03-15 17:06:52 -05007842 clear_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03007843 }
Johan Hedberg890ea892013-03-15 17:06:52 -05007844
7845 hci_req_run(&req, NULL);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007846}
7847
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007848static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg90e70452012-02-23 23:09:40 +02007849{
7850 struct cmd_lookup *match = data;
7851
Johan Hedberg90e70452012-02-23 23:09:40 +02007852 if (match->sk == NULL) {
7853 match->sk = cmd->sk;
7854 sock_hold(match->sk);
7855 }
Johan Hedberg90e70452012-02-23 23:09:40 +02007856}
7857
Marcel Holtmann4e1b0242013-10-15 14:26:26 -07007858void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
7859 u8 status)
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007860{
Johan Hedberg90e70452012-02-23 23:09:40 +02007861 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007862
Johan Hedberg92da6092013-03-15 17:06:55 -05007863 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
7864 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
7865 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
Johan Hedberg90e70452012-02-23 23:09:40 +02007866
7867 if (!status)
Marcel Holtmannf6b77122015-03-14 19:28:05 -07007868 mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
7869 dev_class, 3, NULL);
Johan Hedberg90e70452012-02-23 23:09:40 +02007870
7871 if (match.sk)
7872 sock_put(match.sk);
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007873}
7874
Marcel Holtmann7667da32013-10-15 14:26:27 -07007875void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
Johan Hedbergb312b1612011-03-16 14:29:37 +02007876{
Johan Hedbergb312b1612011-03-16 14:29:37 +02007877 struct mgmt_cp_set_local_name ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007878 struct mgmt_pending_cmd *cmd;
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02007879
Johan Hedberg13928972013-03-15 17:07:00 -05007880 if (status)
Marcel Holtmann7667da32013-10-15 14:26:27 -07007881 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02007882
7883 memset(&ev, 0, sizeof(ev));
7884 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02007885 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
Johan Hedbergb312b1612011-03-16 14:29:37 +02007886
Johan Hedberg333ae952015-03-17 13:48:47 +02007887 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05007888 if (!cmd) {
7889 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
Johan Hedbergb312b1612011-03-16 14:29:37 +02007890
Johan Hedberg13928972013-03-15 17:07:00 -05007891 /* If this is a HCI command related to powering on the
7892 * HCI dev don't send any mgmt signals.
7893 */
Johan Hedberg333ae952015-03-17 13:48:47 +02007894 if (pending_find(MGMT_OP_SET_POWERED, hdev))
Marcel Holtmann7667da32013-10-15 14:26:27 -07007895 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02007896 }
7897
Marcel Holtmannf6b77122015-03-14 19:28:05 -07007898 mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
7899 cmd ? cmd->sk : NULL);
Johan Hedbergb312b1612011-03-16 14:29:37 +02007900}
Szymon Jancc35938b2011-03-22 13:12:21 +01007901
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08007902void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192,
Johan Hedberg38da1702014-11-17 20:52:20 +02007903 u8 *rand192, u8 *hash256, u8 *rand256,
7904 u8 status)
Szymon Jancc35938b2011-03-22 13:12:21 +01007905{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007906 struct mgmt_pending_cmd *cmd;
Szymon Jancc35938b2011-03-22 13:12:21 +01007907
Johan Hedberg744cf192011-11-08 20:40:14 +02007908 BT_DBG("%s status %u", hdev->name, status);
Szymon Jancc35938b2011-03-22 13:12:21 +01007909
Johan Hedberg333ae952015-03-17 13:48:47 +02007910 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01007911 if (!cmd)
Marcel Holtmann3edaf092013-10-15 14:26:28 -07007912 return;
Szymon Jancc35938b2011-03-22 13:12:21 +01007913
7914 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02007915 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
7916 mgmt_status(status));
Szymon Jancc35938b2011-03-22 13:12:21 +01007917 } else {
Johan Hedberg66f096f2015-02-02 13:23:42 +02007918 struct mgmt_rp_read_local_oob_data rp;
7919 size_t rp_size = sizeof(rp);
7920
7921 memcpy(rp.hash192, hash192, sizeof(rp.hash192));
7922 memcpy(rp.rand192, rand192, sizeof(rp.rand192));
7923
Johan Hedberg710f11c2014-05-26 11:21:22 +03007924 if (bredr_sc_enabled(hdev) && hash256 && rand256) {
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08007925 memcpy(rp.hash256, hash256, sizeof(rp.hash256));
Johan Hedberg38da1702014-11-17 20:52:20 +02007926 memcpy(rp.rand256, rand256, sizeof(rp.rand256));
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08007927 } else {
Johan Hedberg66f096f2015-02-02 13:23:42 +02007928 rp_size -= sizeof(rp.hash256) + sizeof(rp.rand256);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08007929 }
Johan Hedberg66f096f2015-02-02 13:23:42 +02007930
Johan Hedberg2a1afb52015-03-06 21:08:54 +02007931 mgmt_cmd_complete(cmd->sk, hdev->id,
7932 MGMT_OP_READ_LOCAL_OOB_DATA, 0,
7933 &rp, rp_size);
Szymon Jancc35938b2011-03-22 13:12:21 +01007934 }
7935
7936 mgmt_pending_remove(cmd);
Szymon Jancc35938b2011-03-22 13:12:21 +01007937}
Johan Hedberge17acd42011-03-30 23:57:16 +03007938
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007939static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16])
7940{
7941 int i;
7942
7943 for (i = 0; i < uuid_count; i++) {
7944 if (!memcmp(uuid, uuids[i], 16))
7945 return true;
7946 }
7947
7948 return false;
7949}
7950
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007951static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16])
7952{
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007953 u16 parsed = 0;
7954
7955 while (parsed < eir_len) {
7956 u8 field_len = eir[0];
7957 u8 uuid[16];
7958 int i;
7959
7960 if (field_len == 0)
7961 break;
7962
7963 if (eir_len - parsed < field_len + 1)
7964 break;
7965
7966 switch (eir[1]) {
7967 case EIR_UUID16_ALL:
7968 case EIR_UUID16_SOME:
7969 for (i = 0; i + 3 <= field_len; i += 2) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02007970 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007971 uuid[13] = eir[i + 3];
7972 uuid[12] = eir[i + 2];
7973 if (has_uuid(uuid, uuid_count, uuids))
7974 return true;
7975 }
7976 break;
7977 case EIR_UUID32_ALL:
7978 case EIR_UUID32_SOME:
7979 for (i = 0; i + 5 <= field_len; i += 4) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02007980 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007981 uuid[15] = eir[i + 5];
7982 uuid[14] = eir[i + 4];
7983 uuid[13] = eir[i + 3];
7984 uuid[12] = eir[i + 2];
7985 if (has_uuid(uuid, uuid_count, uuids))
7986 return true;
7987 }
7988 break;
7989 case EIR_UUID128_ALL:
7990 case EIR_UUID128_SOME:
7991 for (i = 0; i + 17 <= field_len; i += 16) {
7992 memcpy(uuid, eir + i + 2, 16);
7993 if (has_uuid(uuid, uuid_count, uuids))
7994 return true;
7995 }
7996 break;
7997 }
7998
7999 parsed += field_len + 1;
8000 eir += field_len + 1;
8001 }
8002
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01008003 return false;
8004}
8005
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08008006static void restart_le_scan(struct hci_dev *hdev)
8007{
8008 /* If controller is not scanning we are done. */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07008009 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08008010 return;
8011
8012 if (time_after(jiffies + DISCOV_LE_RESTART_DELAY,
8013 hdev->discovery.scan_start +
8014 hdev->discovery.scan_duration))
8015 return;
8016
8017 queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart,
8018 DISCOV_LE_RESTART_DELAY);
8019}
8020
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008021static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir,
8022 u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
8023{
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008024 /* If a RSSI threshold has been specified, and
8025 * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with
8026 * a RSSI smaller than the RSSI threshold will be dropped. If the quirk
8027 * is set, let it through for further processing, as we might need to
8028 * restart the scan.
8029 *
8030 * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry,
8031 * the results are also dropped.
8032 */
8033 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
8034 (rssi == HCI_RSSI_INVALID ||
8035 (rssi < hdev->discovery.rssi &&
8036 !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))
8037 return false;
8038
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008039 if (hdev->discovery.uuid_count != 0) {
8040 /* If a list of UUIDs is provided in filter, results with no
8041 * matching UUID should be dropped.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008042 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008043 if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count,
8044 hdev->discovery.uuids) &&
8045 !eir_has_uuids(scan_rsp, scan_rsp_len,
8046 hdev->discovery.uuid_count,
8047 hdev->discovery.uuids))
8048 return false;
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008049 }
8050
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008051 /* If duplicate filtering does not report RSSI changes, then restart
8052 * scanning to ensure updated result with updated RSSI values.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008053 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008054 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) {
8055 restart_le_scan(hdev);
8056
8057 /* Validate RSSI value against the RSSI threshold once more. */
8058 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
8059 rssi < hdev->discovery.rssi)
8060 return false;
8061 }
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008062
8063 return true;
8064}
8065
Marcel Holtmann901801b2013-10-06 23:55:51 -07008066void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
Marcel Holtmannaf589252014-07-01 14:11:20 +02008067 u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
8068 u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
Johan Hedberge17acd42011-03-30 23:57:16 +03008069{
Johan Hedberge319d2e2012-01-15 19:51:59 +02008070 char buf[512];
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008071 struct mgmt_ev_device_found *ev = (void *)buf;
Johan Hedberg1dc06092012-01-15 21:01:23 +02008072 size_t ev_size;
Johan Hedberge17acd42011-03-30 23:57:16 +03008073
Johan Hedberg75ce2082014-07-02 22:42:01 +03008074 /* Don't send events for a non-kernel initiated discovery. With
8075 * LE one exception is if we have pend_le_reports > 0 in which
8076 * case we're doing passive scanning and want these events.
8077 */
8078 if (!hci_discovery_active(hdev)) {
8079 if (link_type == ACL_LINK)
8080 return;
Johan Hedberg66f84552014-07-04 12:37:18 +03008081 if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports))
Johan Hedberg75ce2082014-07-02 22:42:01 +03008082 return;
8083 }
Andre Guedes12602d02013-04-30 15:29:40 -03008084
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08008085 if (hdev->discovery.result_filtering) {
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008086 /* We are using service discovery */
8087 if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp,
8088 scan_rsp_len))
8089 return;
8090 }
Marcel Holtmannbda157a2014-12-05 10:55:56 +01008091
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02008092 /* Make sure that the buffer is big enough. The 5 extra bytes
8093 * are for the potential CoD field.
8094 */
8095 if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf))
Marcel Holtmann901801b2013-10-06 23:55:51 -07008096 return;
Andre Guedes7d262f82012-01-10 18:20:49 -03008097
Johan Hedberg1dc06092012-01-15 21:01:23 +02008098 memset(buf, 0, sizeof(buf));
8099
Marcel Holtmannda25cf62014-12-05 13:03:35 +01008100 /* In case of device discovery with BR/EDR devices (pre 1.2), the
8101 * RSSI value was reported as 0 when not available. This behavior
8102 * is kept when using device discovery. This is required for full
8103 * backwards compatibility with the API.
8104 *
8105 * However when using service discovery, the value 127 will be
8106 * returned when the RSSI is not available.
8107 */
Szymon Janc91200e92015-01-22 16:57:05 +01008108 if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi &&
8109 link_type == ACL_LINK)
Marcel Holtmannefb25132014-12-05 13:03:34 +01008110 rssi = 0;
8111
Johan Hedberg841c5642014-07-07 12:45:54 +03008112 bacpy(&ev->addr.bdaddr, bdaddr);
8113 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberge319d2e2012-01-15 19:51:59 +02008114 ev->rssi = rssi;
Marcel Holtmannaf589252014-07-01 14:11:20 +02008115 ev->flags = cpu_to_le32(flags);
Johan Hedberge17acd42011-03-30 23:57:16 +03008116
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008117 if (eir_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01008118 /* Copy EIR or advertising data into event */
Johan Hedberge319d2e2012-01-15 19:51:59 +02008119 memcpy(ev->eir, eir, eir_len);
Johan Hedberge17acd42011-03-30 23:57:16 +03008120
Johan Hedberg1dc06092012-01-15 21:01:23 +02008121 if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV))
8122 eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03008123 dev_class, 3);
Johan Hedberg1dc06092012-01-15 21:01:23 +02008124
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008125 if (scan_rsp_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01008126 /* Append scan response data to event */
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02008127 memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len);
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08008128
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02008129 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
8130 ev_size = sizeof(*ev) + eir_len + scan_rsp_len;
Andre Guedesf8523592011-09-09 18:56:26 -03008131
Marcel Holtmann901801b2013-10-06 23:55:51 -07008132 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL);
Johan Hedberge17acd42011-03-30 23:57:16 +03008133}
Johan Hedberga88a9652011-03-30 13:18:12 +03008134
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07008135void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
8136 u8 addr_type, s8 rssi, u8 *name, u8 name_len)
Johan Hedberga88a9652011-03-30 13:18:12 +03008137{
Johan Hedbergb644ba32012-01-17 21:48:47 +02008138 struct mgmt_ev_device_found *ev;
8139 char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2];
8140 u16 eir_len;
Johan Hedberga88a9652011-03-30 13:18:12 +03008141
Johan Hedbergb644ba32012-01-17 21:48:47 +02008142 ev = (struct mgmt_ev_device_found *) buf;
Johan Hedberga88a9652011-03-30 13:18:12 +03008143
Johan Hedbergb644ba32012-01-17 21:48:47 +02008144 memset(buf, 0, sizeof(buf));
Johan Hedberga88a9652011-03-30 13:18:12 +03008145
Johan Hedbergb644ba32012-01-17 21:48:47 +02008146 bacpy(&ev->addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03008147 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergb644ba32012-01-17 21:48:47 +02008148 ev->rssi = rssi;
8149
8150 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03008151 name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02008152
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02008153 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02008154
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07008155 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL);
Johan Hedberga88a9652011-03-30 13:18:12 +03008156}
Johan Hedberg314b2382011-04-27 10:29:57 -04008157
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07008158void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
Johan Hedberg314b2382011-04-27 10:29:57 -04008159{
Johan Hedbergf963e8e2012-02-20 23:30:44 +02008160 struct mgmt_ev_discovering ev;
Johan Hedberg164a6e72011-11-01 17:06:44 +02008161
Andre Guedes343fb142011-11-22 17:14:19 -03008162 BT_DBG("%s discovering %u", hdev->name, discovering);
8163
Johan Hedbergf963e8e2012-02-20 23:30:44 +02008164 memset(&ev, 0, sizeof(ev));
8165 ev.type = hdev->discovery.type;
8166 ev.discovering = discovering;
8167
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07008168 mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg314b2382011-04-27 10:29:57 -04008169}
Antti Julku5e762442011-08-25 16:48:02 +03008170
Marcel Holtmann1904a852015-01-11 13:50:44 -08008171static void adv_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Marcel Holtmann5976e602013-10-06 04:08:14 -07008172{
8173 BT_DBG("%s status %u", hdev->name, status);
Marcel Holtmann5976e602013-10-06 04:08:14 -07008174}
8175
8176void mgmt_reenable_advertising(struct hci_dev *hdev)
8177{
8178 struct hci_request req;
8179
Arman Uguray24b4f382015-03-23 15:57:12 -07008180 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
8181 !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
Marcel Holtmann5976e602013-10-06 04:08:14 -07008182 return;
8183
8184 hci_req_init(&req, hdev);
8185 enable_advertising(&req);
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03008186 hci_req_run(&req, adv_enable_complete);
Marcel Holtmann5976e602013-10-06 04:08:14 -07008187}
Johan Hedberg6d785aa32015-03-06 21:08:51 +02008188
8189static struct hci_mgmt_chan chan = {
8190 .channel = HCI_CHANNEL_CONTROL,
8191 .handler_count = ARRAY_SIZE(mgmt_handlers),
8192 .handlers = mgmt_handlers,
Johan Hedberg88b94ce2015-03-17 13:48:49 +02008193 .hdev_init = mgmt_init_hdev,
Johan Hedberg6d785aa32015-03-06 21:08:51 +02008194};
8195
8196int mgmt_init(void)
8197{
8198 return hci_mgmt_chan_register(&chan);
8199}
8200
8201void mgmt_exit(void)
8202{
8203 hci_mgmt_chan_unregister(&chan);
8204}