blob: 7fabcb60da2d82e8d023a49ef69d751311f00add [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 Hedbergf7b64e62010-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 Hedbergf7b64e62010-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 Hedbergf7b64e62010-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 Hedbergf7b64e62010-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
Florian Grandel91aa9bb2015-06-18 03:16:36 +0200835static u8 get_current_adv_instance(struct hci_dev *hdev)
836{
837 /* The "Set Advertising" setting supersedes the "Add Advertising"
838 * setting. Here we set the advertising data based on which
839 * setting was set. When neither apply, default to the global settings,
840 * represented by instance "0".
841 */
842 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
843 !hci_dev_test_flag(hdev, HCI_ADVERTISING))
844 return 0x01;
845
846 return 0x00;
847}
848
Arman Uguray4117ed72015-03-23 15:57:14 -0700849static u8 create_default_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700850{
Marcel Holtmann7a5f4992013-10-16 00:16:49 -0700851 u8 ad_len = 0;
852 size_t name_len;
853
854 name_len = strlen(hdev->dev_name);
855 if (name_len > 0) {
856 size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2;
857
858 if (name_len > max_len) {
859 name_len = max_len;
860 ptr[1] = EIR_NAME_SHORT;
861 } else
862 ptr[1] = EIR_NAME_COMPLETE;
863
864 ptr[0] = name_len + 1;
865
866 memcpy(ptr + 2, hdev->dev_name, name_len);
867
868 ad_len += (name_len + 2);
869 ptr += (name_len + 2);
870 }
871
872 return ad_len;
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700873}
874
Arman Uguray4117ed72015-03-23 15:57:14 -0700875static u8 create_instance_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
876{
877 /* TODO: Set the appropriate entries based on advertising instance flags
878 * here once flags other than 0 are supported.
879 */
880 memcpy(ptr, hdev->adv_instance.scan_rsp_data,
881 hdev->adv_instance.scan_rsp_len);
882
883 return hdev->adv_instance.scan_rsp_len;
884}
885
Florian Grandelefae0022015-06-18 03:16:37 +0200886static void update_inst_scan_rsp_data(struct hci_request *req, u8 instance)
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700887{
888 struct hci_dev *hdev = req->hdev;
889 struct hci_cp_le_set_scan_rsp_data cp;
890 u8 len;
891
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700892 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700893 return;
894
895 memset(&cp, 0, sizeof(cp));
896
Arman Uguray4117ed72015-03-23 15:57:14 -0700897 if (instance)
898 len = create_instance_scan_rsp_data(hdev, cp.data);
899 else
900 len = create_default_scan_rsp_data(hdev, cp.data);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700901
Johan Hedbergeb438b52013-10-16 15:31:07 +0300902 if (hdev->scan_rsp_data_len == len &&
Arman Uguray4117ed72015-03-23 15:57:14 -0700903 !memcmp(cp.data, hdev->scan_rsp_data, len))
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700904 return;
905
Johan Hedbergeb438b52013-10-16 15:31:07 +0300906 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
907 hdev->scan_rsp_data_len = len;
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700908
909 cp.length = len;
910
911 hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp);
912}
913
Arman Uguray4117ed72015-03-23 15:57:14 -0700914static void update_scan_rsp_data(struct hci_request *req)
915{
Florian Grandelefae0022015-06-18 03:16:37 +0200916 update_inst_scan_rsp_data(req, get_current_adv_instance(req->hdev));
Arman Uguray4117ed72015-03-23 15:57:14 -0700917}
918
Johan Hedberg9a43e252013-10-20 19:00:07 +0300919static u8 get_adv_discov_flags(struct hci_dev *hdev)
920{
Johan Hedberg3b0602c2015-03-06 21:08:55 +0200921 struct mgmt_pending_cmd *cmd;
Johan Hedberg9a43e252013-10-20 19:00:07 +0300922
923 /* If there's a pending mgmt command the flags will not yet have
924 * their final values, so check for this first.
925 */
Johan Hedberg333ae952015-03-17 13:48:47 +0200926 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg9a43e252013-10-20 19:00:07 +0300927 if (cmd) {
928 struct mgmt_mode *cp = cmd->param;
929 if (cp->val == 0x01)
930 return LE_AD_GENERAL;
931 else if (cp->val == 0x02)
932 return LE_AD_LIMITED;
933 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700934 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300935 return LE_AD_LIMITED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700936 else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300937 return LE_AD_GENERAL;
938 }
939
940 return 0;
941}
942
Arman Uguraye7a685d2015-03-25 18:53:40 -0700943static bool get_connectable(struct hci_dev *hdev)
944{
945 struct mgmt_pending_cmd *cmd;
946
947 /* If there's a pending mgmt command the flag will not yet have
948 * it's final value, so check for this first.
949 */
950 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
951 if (cmd) {
952 struct mgmt_mode *cp = cmd->param;
953
954 return cp->val;
955 }
956
957 return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
958}
959
960static u32 get_adv_instance_flags(struct hci_dev *hdev, u8 instance)
961{
962 u32 flags;
963
964 if (instance > 0x01)
965 return 0;
966
Arman Ugurayfdf51782015-03-25 18:53:46 -0700967 if (instance == 0x01)
Arman Uguraye7a685d2015-03-25 18:53:40 -0700968 return hdev->adv_instance.flags;
969
Arman Ugurayfdf51782015-03-25 18:53:46 -0700970 /* Instance 0 always manages the "Tx Power" and "Flags" fields */
971 flags = MGMT_ADV_FLAG_TX_POWER | MGMT_ADV_FLAG_MANAGED_FLAGS;
Arman Uguraye7a685d2015-03-25 18:53:40 -0700972
Arman Ugurayfaccb952015-03-28 12:38:58 -0700973 /* For instance 0, the HCI_ADVERTISING_CONNECTABLE setting corresponds
974 * to the "connectable" instance flag.
975 */
976 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE))
Arman Uguraye7a685d2015-03-25 18:53:40 -0700977 flags |= MGMT_ADV_FLAG_CONNECTABLE;
978
Arman Uguraye7a685d2015-03-25 18:53:40 -0700979 return flags;
980}
981
Arman Ugurayc7d48832015-03-28 12:38:59 -0700982static u8 get_adv_instance_scan_rsp_len(struct hci_dev *hdev, u8 instance)
983{
984 /* Ignore instance 0 and other unsupported instances */
985 if (instance != 0x01)
986 return 0;
987
988 /* TODO: Take into account the "appearance" and "local-name" flags here.
989 * These are currently being ignored as they are not supported.
990 */
991 return hdev->adv_instance.scan_rsp_len;
992}
993
Arman Ugurayfdf51782015-03-25 18:53:46 -0700994static u8 create_instance_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
995{
996 u8 ad_len = 0, flags = 0;
997 u32 instance_flags = get_adv_instance_flags(hdev, instance);
998
999 /* The Add Advertising command allows userspace to set both the general
1000 * and limited discoverable flags.
1001 */
1002 if (instance_flags & MGMT_ADV_FLAG_DISCOV)
1003 flags |= LE_AD_GENERAL;
1004
1005 if (instance_flags & MGMT_ADV_FLAG_LIMITED_DISCOV)
1006 flags |= LE_AD_LIMITED;
1007
1008 if (flags || (instance_flags & MGMT_ADV_FLAG_MANAGED_FLAGS)) {
1009 /* If a discovery flag wasn't provided, simply use the global
1010 * settings.
1011 */
1012 if (!flags)
1013 flags |= get_adv_discov_flags(hdev);
1014
1015 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1016 flags |= LE_AD_NO_BREDR;
1017
1018 /* If flags would still be empty, then there is no need to
1019 * include the "Flags" AD field".
1020 */
1021 if (flags) {
1022 ptr[0] = 0x02;
1023 ptr[1] = EIR_FLAGS;
1024 ptr[2] = flags;
1025
1026 ad_len += 3;
1027 ptr += 3;
1028 }
1029 }
1030
Marcel Holtmann38c8af62015-04-03 13:23:12 -07001031 if (instance) {
1032 memcpy(ptr, hdev->adv_instance.adv_data,
1033 hdev->adv_instance.adv_data_len);
1034
1035 ad_len += hdev->adv_instance.adv_data_len;
1036 ptr += hdev->adv_instance.adv_data_len;
1037 }
1038
Arman Ugurayfdf51782015-03-25 18:53:46 -07001039 /* Provide Tx Power only if we can provide a valid value for it */
1040 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID &&
1041 (instance_flags & MGMT_ADV_FLAG_TX_POWER)) {
1042 ptr[0] = 0x02;
1043 ptr[1] = EIR_TX_POWER;
1044 ptr[2] = (u8)hdev->adv_tx_power;
1045
1046 ad_len += 3;
1047 ptr += 3;
1048 }
1049
Arman Ugurayfdf51782015-03-25 18:53:46 -07001050 return ad_len;
1051}
1052
Florian Grandelefae0022015-06-18 03:16:37 +02001053static void update_inst_adv_data(struct hci_request *req, u8 instance)
Arman Ugurayfdf51782015-03-25 18:53:46 -07001054{
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{
Florian Grandelefae0022015-06-18 03:16:37 +02001081 update_inst_adv_data(req, get_current_adv_instance(req->hdev));
Arman Uguray24b4f382015-03-23 15:57:12 -07001082}
1083
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001084int mgmt_update_adv_data(struct hci_dev *hdev)
1085{
1086 struct hci_request req;
1087
1088 hci_req_init(&req, hdev);
1089 update_adv_data(&req);
1090
1091 return hci_req_run(&req, NULL);
1092}
1093
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001094static void create_eir(struct hci_dev *hdev, u8 *data)
1095{
1096 u8 *ptr = data;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001097 size_t name_len;
1098
1099 name_len = strlen(hdev->dev_name);
1100
1101 if (name_len > 0) {
1102 /* EIR Data type */
1103 if (name_len > 48) {
1104 name_len = 48;
1105 ptr[1] = EIR_NAME_SHORT;
1106 } else
1107 ptr[1] = EIR_NAME_COMPLETE;
1108
1109 /* EIR Data length */
1110 ptr[0] = name_len + 1;
1111
1112 memcpy(ptr + 2, hdev->dev_name, name_len);
1113
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001114 ptr += (name_len + 2);
1115 }
1116
Johan Hedbergbbaf4442012-11-08 01:22:59 +01001117 if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) {
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -07001118 ptr[0] = 2;
1119 ptr[1] = EIR_TX_POWER;
1120 ptr[2] = (u8) hdev->inq_tx_power;
1121
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -07001122 ptr += 3;
1123 }
1124
Marcel Holtmann2b9be132012-03-11 19:32:12 -07001125 if (hdev->devid_source > 0) {
1126 ptr[0] = 9;
1127 ptr[1] = EIR_DEVICE_ID;
1128
1129 put_unaligned_le16(hdev->devid_source, ptr + 2);
1130 put_unaligned_le16(hdev->devid_vendor, ptr + 4);
1131 put_unaligned_le16(hdev->devid_product, ptr + 6);
1132 put_unaligned_le16(hdev->devid_version, ptr + 8);
1133
Marcel Holtmann2b9be132012-03-11 19:32:12 -07001134 ptr += 10;
1135 }
1136
Johan Hedberg213202e2013-01-27 00:31:33 +02001137 ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedbergcdf19632013-01-27 00:31:34 +02001138 ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedbergc00d5752013-01-27 00:31:35 +02001139 ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001140}
1141
Johan Hedberg890ea892013-03-15 17:06:52 -05001142static void update_eir(struct hci_request *req)
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001143{
Johan Hedberg890ea892013-03-15 17:06:52 -05001144 struct hci_dev *hdev = req->hdev;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001145 struct hci_cp_write_eir cp;
1146
Johan Hedberg504c8dc2012-02-23 13:30:41 +02001147 if (!hdev_is_powered(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05001148 return;
Johan Hedberg7770c4a2012-02-22 22:06:38 +02001149
Johan Hedberg976eb202012-10-24 21:12:01 +03001150 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05001151 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001152
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001153 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
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_SERVICE_CACHE))
Johan Hedberg890ea892013-03-15 17:06:52 -05001157 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001158
1159 memset(&cp, 0, sizeof(cp));
1160
1161 create_eir(hdev, cp.data);
1162
1163 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
Johan Hedberg890ea892013-03-15 17:06:52 -05001164 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001165
1166 memcpy(hdev->eir, cp.data, sizeof(cp.data));
1167
Johan Hedberg890ea892013-03-15 17:06:52 -05001168 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001169}
1170
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001171static u8 get_service_classes(struct hci_dev *hdev)
1172{
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -03001173 struct bt_uuid *uuid;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001174 u8 val = 0;
1175
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -03001176 list_for_each_entry(uuid, &hdev->uuids, list)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001177 val |= uuid->svc_hint;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001178
1179 return val;
1180}
1181
Johan Hedberg890ea892013-03-15 17:06:52 -05001182static void update_class(struct hci_request *req)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001183{
Johan Hedberg890ea892013-03-15 17:06:52 -05001184 struct hci_dev *hdev = req->hdev;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001185 u8 cod[3];
1186
1187 BT_DBG("%s", hdev->name);
1188
Johan Hedberg504c8dc2012-02-23 13:30:41 +02001189 if (!hdev_is_powered(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05001190 return;
Johan Hedberg7770c4a2012-02-22 22:06:38 +02001191
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001192 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedbergf87ea1d2013-10-19 23:38:17 +03001193 return;
1194
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001195 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg890ea892013-03-15 17:06:52 -05001196 return;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001197
1198 cod[0] = hdev->minor_class;
1199 cod[1] = hdev->major_class;
1200 cod[2] = get_service_classes(hdev);
1201
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001202 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
Marcel Holtmann6acd7db2013-10-15 06:33:53 -07001203 cod[1] |= 0x20;
1204
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001205 if (memcmp(cod, hdev->dev_class, 3) == 0)
Johan Hedberg890ea892013-03-15 17:06:52 -05001206 return;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001207
Johan Hedberg890ea892013-03-15 17:06:52 -05001208 hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001209}
1210
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001211static void disable_advertising(struct hci_request *req)
1212{
1213 u8 enable = 0x00;
1214
1215 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1216}
1217
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001218static void enable_advertising(struct hci_request *req)
1219{
1220 struct hci_dev *hdev = req->hdev;
1221 struct hci_cp_le_set_adv_param cp;
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001222 u8 own_addr_type, enable = 0x01;
Johan Hedberga4858cb2014-02-25 19:56:31 +02001223 bool connectable;
Arman Uguraye7a685d2015-03-25 18:53:40 -07001224 u8 instance;
1225 u32 flags;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001226
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001227 if (hci_conn_num(hdev, LE_LINK) > 0)
1228 return;
1229
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001230 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001231 disable_advertising(req);
1232
Johan Hedberg5ce194c2014-07-08 15:07:49 +03001233 /* Clear the HCI_LE_ADV bit temporarily so that the
Johan Hedberg8d972502014-02-28 12:54:14 +02001234 * hci_update_random_address knows that it's safe to go ahead
1235 * and write a new random address. The flag will be set back on
1236 * as soon as the SET_ADV_ENABLE HCI command completes.
1237 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001238 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Johan Hedberg8d972502014-02-28 12:54:14 +02001239
Arman Uguraye7a685d2015-03-25 18:53:40 -07001240 instance = get_current_adv_instance(hdev);
1241 flags = get_adv_instance_flags(hdev, instance);
Arman Ugurayfaccb952015-03-28 12:38:58 -07001242
1243 /* If the "connectable" instance flag was not set, then choose between
1244 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1245 */
1246 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1247 get_connectable(hdev);
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001248
Johan Hedberga4858cb2014-02-25 19:56:31 +02001249 /* Set require_privacy to true only when non-connectable
1250 * advertising is used. In that case it is fine to use a
1251 * non-resolvable private address.
1252 */
1253 if (hci_update_random_address(req, !connectable, &own_addr_type) < 0)
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001254 return;
1255
Marcel Holtmann41c90c12014-02-23 20:25:55 -08001256 memset(&cp, 0, sizeof(cp));
Georg Lukas628531c2014-07-26 13:59:57 +02001257 cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval);
1258 cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval);
Arman Ugurayc7d48832015-03-28 12:38:59 -07001259
1260 if (connectable)
1261 cp.type = LE_ADV_IND;
1262 else if (get_adv_instance_scan_rsp_len(hdev, instance))
1263 cp.type = LE_ADV_SCAN_IND;
1264 else
1265 cp.type = LE_ADV_NONCONN_IND;
1266
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001267 cp.own_address_type = own_addr_type;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001268 cp.channel_map = hdev->le_adv_channel_map;
1269
1270 hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
1271
1272 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1273}
1274
Johan Hedberg7d785252011-12-15 00:47:39 +02001275static void service_cache_off(struct work_struct *work)
1276{
1277 struct hci_dev *hdev = container_of(work, struct hci_dev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001278 service_cache.work);
Johan Hedberg890ea892013-03-15 17:06:52 -05001279 struct hci_request req;
Johan Hedberg7d785252011-12-15 00:47:39 +02001280
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001281 if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg7d785252011-12-15 00:47:39 +02001282 return;
1283
Johan Hedberg890ea892013-03-15 17:06:52 -05001284 hci_req_init(&req, hdev);
1285
Johan Hedberg7d785252011-12-15 00:47:39 +02001286 hci_dev_lock(hdev);
1287
Johan Hedberg890ea892013-03-15 17:06:52 -05001288 update_eir(&req);
1289 update_class(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02001290
1291 hci_dev_unlock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -05001292
1293 hci_req_run(&req, NULL);
Johan Hedberg7d785252011-12-15 00:47:39 +02001294}
1295
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001296static void rpa_expired(struct work_struct *work)
1297{
1298 struct hci_dev *hdev = container_of(work, struct hci_dev,
1299 rpa_expired.work);
1300 struct hci_request req;
1301
1302 BT_DBG("");
1303
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001304 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001305
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001306 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001307 return;
1308
1309 /* The generation of a new RPA and programming it into the
1310 * controller happens in the enable_advertising() function.
1311 */
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001312 hci_req_init(&req, hdev);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001313 enable_advertising(&req);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001314 hci_req_run(&req, NULL);
1315}
1316
Johan Hedberg6a919082012-02-28 06:17:26 +02001317static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
Johan Hedberg7d785252011-12-15 00:47:39 +02001318{
Marcel Holtmann238be782015-03-13 02:11:06 -07001319 if (hci_dev_test_and_set_flag(hdev, HCI_MGMT))
Johan Hedberg6a919082012-02-28 06:17:26 +02001320 return;
1321
Johan Hedberg4f87da82012-03-02 19:55:56 +02001322 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001323 INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
Johan Hedberg7d785252011-12-15 00:47:39 +02001324
Johan Hedberg4f87da82012-03-02 19:55:56 +02001325 /* Non-mgmt controlled devices get this bit set
1326 * implicitly so that pairing works for them, however
1327 * for mgmt we require user-space to explicitly enable
1328 * it
1329 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001330 hci_dev_clear_flag(hdev, HCI_BONDABLE);
Johan Hedberg7d785252011-12-15 00:47:39 +02001331}
1332
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02001333static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001334 void *data, u16 data_len)
Johan Hedberg03811012010-12-08 00:21:06 +02001335{
1336 struct mgmt_rp_read_info rp;
Johan Hedberg03811012010-12-08 00:21:06 +02001337
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001338 BT_DBG("sock %p %s", sk, hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001339
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001340 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001341
Johan Hedberg03811012010-12-08 00:21:06 +02001342 memset(&rp, 0, sizeof(rp));
1343
Johan Hedberg03811012010-12-08 00:21:06 +02001344 bacpy(&rp.bdaddr, &hdev->bdaddr);
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001345
1346 rp.version = hdev->hci_ver;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02001347 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001348
1349 rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
1350 rp.current_settings = cpu_to_le32(get_current_settings(hdev));
1351
1352 memcpy(rp.dev_class, hdev->dev_class, 3);
Johan Hedberg03811012010-12-08 00:21:06 +02001353
1354 memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
Johan Hedberg27fcc362012-02-22 21:46:22 +02001355 memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
Johan Hedberg03811012010-12-08 00:21:06 +02001356
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001357 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001358
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001359 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
1360 sizeof(rp));
Johan Hedberg03811012010-12-08 00:21:06 +02001361}
1362
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001363static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
Johan Hedberg86805702011-11-11 16:18:52 +02001364{
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001365 __le32 settings = cpu_to_le32(get_current_settings(hdev));
Johan Hedberg86805702011-11-11 16:18:52 +02001366
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001367 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings,
1368 sizeof(settings));
Johan Hedberg86805702011-11-11 16:18:52 +02001369}
1370
Marcel Holtmann1904a852015-01-11 13:50:44 -08001371static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg8b064a32014-02-24 14:52:22 +02001372{
1373 BT_DBG("%s status 0x%02x", hdev->name, status);
1374
Johan Hedberga3172b72014-02-28 09:33:44 +02001375 if (hci_conn_count(hdev) == 0) {
1376 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001377 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberga3172b72014-02-28 09:33:44 +02001378 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001379}
1380
Johan Hedberg23a48092014-07-08 16:05:06 +03001381static bool hci_stop_discovery(struct hci_request *req)
Johan Hedberg21a60d32014-06-10 14:05:58 +03001382{
1383 struct hci_dev *hdev = req->hdev;
1384 struct hci_cp_remote_name_req_cancel cp;
1385 struct inquiry_entry *e;
1386
1387 switch (hdev->discovery.state) {
1388 case DISCOVERY_FINDING:
Jakub Pawlowski07d23342015-03-17 09:04:14 -07001389 if (test_bit(HCI_INQUIRY, &hdev->flags))
Johan Hedberg21a60d32014-06-10 14:05:58 +03001390 hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL);
Jakub Pawlowski07d23342015-03-17 09:04:14 -07001391
1392 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
Johan Hedberg21a60d32014-06-10 14:05:58 +03001393 cancel_delayed_work(&hdev->le_scan_disable);
1394 hci_req_add_le_scan_disable(req);
1395 }
1396
Johan Hedberg23a48092014-07-08 16:05:06 +03001397 return true;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001398
1399 case DISCOVERY_RESOLVING:
1400 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
1401 NAME_PENDING);
1402 if (!e)
Johan Hedberg23a48092014-07-08 16:05:06 +03001403 break;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001404
1405 bacpy(&cp.bdaddr, &e->data.bdaddr);
1406 hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp),
1407 &cp);
1408
Johan Hedberg23a48092014-07-08 16:05:06 +03001409 return true;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001410
1411 default:
1412 /* Passive scanning */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001413 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
Johan Hedberg21a60d32014-06-10 14:05:58 +03001414 hci_req_add_le_scan_disable(req);
Johan Hedberg23a48092014-07-08 16:05:06 +03001415 return true;
1416 }
1417
Johan Hedberg21a60d32014-06-10 14:05:58 +03001418 break;
1419 }
Johan Hedberg23a48092014-07-08 16:05:06 +03001420
1421 return false;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001422}
1423
Arman Uguray912098a2015-03-23 15:57:15 -07001424static void advertising_added(struct sock *sk, struct hci_dev *hdev,
1425 u8 instance)
1426{
1427 struct mgmt_ev_advertising_added ev;
1428
1429 ev.instance = instance;
1430
1431 mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk);
1432}
1433
1434static void advertising_removed(struct sock *sk, struct hci_dev *hdev,
1435 u8 instance)
1436{
1437 struct mgmt_ev_advertising_removed ev;
1438
1439 ev.instance = instance;
1440
1441 mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk);
1442}
1443
1444static void clear_adv_instance(struct hci_dev *hdev)
1445{
1446 struct hci_request req;
1447
1448 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
1449 return;
1450
Florian Grandel5d900e42015-06-18 03:16:35 +02001451 if (hdev->adv_instance_timeout)
1452 cancel_delayed_work(&hdev->adv_instance_expire);
Arman Uguray912098a2015-03-23 15:57:15 -07001453
1454 memset(&hdev->adv_instance, 0, sizeof(hdev->adv_instance));
1455 advertising_removed(NULL, hdev, 1);
1456 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
1457
1458 if (!hdev_is_powered(hdev) ||
1459 hci_dev_test_flag(hdev, HCI_ADVERTISING))
1460 return;
1461
1462 hci_req_init(&req, hdev);
1463 disable_advertising(&req);
1464 hci_req_run(&req, NULL);
1465}
1466
Johan Hedberg8b064a32014-02-24 14:52:22 +02001467static int clean_up_hci_state(struct hci_dev *hdev)
1468{
1469 struct hci_request req;
1470 struct hci_conn *conn;
Johan Hedberg23a48092014-07-08 16:05:06 +03001471 bool discov_stopped;
1472 int err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001473
1474 hci_req_init(&req, hdev);
1475
1476 if (test_bit(HCI_ISCAN, &hdev->flags) ||
1477 test_bit(HCI_PSCAN, &hdev->flags)) {
1478 u8 scan = 0x00;
1479 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1480 }
1481
Florian Grandel5d900e42015-06-18 03:16:35 +02001482 if (hdev->adv_instance_timeout)
Arman Uguray912098a2015-03-23 15:57:15 -07001483 clear_adv_instance(hdev);
1484
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001485 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedberg8b064a32014-02-24 14:52:22 +02001486 disable_advertising(&req);
1487
Johan Hedberg23a48092014-07-08 16:05:06 +03001488 discov_stopped = hci_stop_discovery(&req);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001489
1490 list_for_each_entry(conn, &hdev->conn_hash.list, list) {
1491 struct hci_cp_disconnect dc;
Johan Hedbergc9910d02014-02-27 14:35:12 +02001492 struct hci_cp_reject_conn_req rej;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001493
Johan Hedbergc9910d02014-02-27 14:35:12 +02001494 switch (conn->state) {
1495 case BT_CONNECTED:
1496 case BT_CONFIG:
1497 dc.handle = cpu_to_le16(conn->handle);
1498 dc.reason = 0x15; /* Terminated due to Power Off */
1499 hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc);
1500 break;
1501 case BT_CONNECT:
1502 if (conn->type == LE_LINK)
1503 hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL,
1504 0, NULL);
1505 else if (conn->type == ACL_LINK)
1506 hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL,
1507 6, &conn->dst);
1508 break;
1509 case BT_CONNECT2:
1510 bacpy(&rej.bdaddr, &conn->dst);
1511 rej.reason = 0x15; /* Terminated due to Power Off */
1512 if (conn->type == ACL_LINK)
1513 hci_req_add(&req, HCI_OP_REJECT_CONN_REQ,
1514 sizeof(rej), &rej);
1515 else if (conn->type == SCO_LINK)
1516 hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ,
1517 sizeof(rej), &rej);
1518 break;
1519 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001520 }
1521
Johan Hedberg23a48092014-07-08 16:05:06 +03001522 err = hci_req_run(&req, clean_up_hci_complete);
1523 if (!err && discov_stopped)
1524 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
1525
1526 return err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001527}
1528
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001529static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001530 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001531{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001532 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001533 struct mgmt_pending_cmd *cmd;
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001534 int err;
Johan Hedberg03811012010-12-08 00:21:06 +02001535
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001536 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001537
Johan Hedberga7e80f22013-01-09 16:05:19 +02001538 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001539 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1540 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001541
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001542 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001543
Johan Hedberg333ae952015-03-17 13:48:47 +02001544 if (pending_find(MGMT_OP_SET_POWERED, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001545 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1546 MGMT_STATUS_BUSY);
Johan Hedberg87b95ba2013-09-25 13:26:06 +03001547 goto failed;
1548 }
1549
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001550 if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
Marcel Holtmannf0d4b782012-02-21 12:14:25 +01001551 cancel_delayed_work(&hdev->power_off);
1552
1553 if (cp->val) {
Johan Hedberga1d70452013-01-09 15:29:40 +02001554 mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev,
1555 data, len);
1556 err = mgmt_powered(hdev, 1);
Marcel Holtmannf0d4b782012-02-21 12:14:25 +01001557 goto failed;
1558 }
1559 }
1560
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001561 if (!!cp->val == hdev_is_powered(hdev)) {
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001562 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001563 goto failed;
1564 }
1565
Johan Hedberg03811012010-12-08 00:21:06 +02001566 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
1567 if (!cmd) {
1568 err = -ENOMEM;
1569 goto failed;
1570 }
1571
Johan Hedberg8b064a32014-02-24 14:52:22 +02001572 if (cp->val) {
Johan Hedberg19202572013-01-14 22:33:51 +02001573 queue_work(hdev->req_workqueue, &hdev->power_on);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001574 err = 0;
1575 } else {
1576 /* Disconnect connections, stop scans, etc */
1577 err = clean_up_hci_state(hdev);
Johan Hedberga3172b72014-02-28 09:33:44 +02001578 if (!err)
1579 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
1580 HCI_POWER_OFF_TIMEOUT);
Johan Hedberg03811012010-12-08 00:21:06 +02001581
Johan Hedberg8b064a32014-02-24 14:52:22 +02001582 /* ENODATA means there were no HCI commands queued */
1583 if (err == -ENODATA) {
Johan Hedberga3172b72014-02-28 09:33:44 +02001584 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001585 queue_work(hdev->req_workqueue, &hdev->power_off.work);
1586 err = 0;
1587 }
1588 }
Johan Hedberg03811012010-12-08 00:21:06 +02001589
1590failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001591 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001592 return err;
1593}
1594
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001595static int new_settings(struct hci_dev *hdev, struct sock *skip)
1596{
Marcel Holtmannf6b77122015-03-14 19:28:05 -07001597 __le32 ev = cpu_to_le32(get_current_settings(hdev));
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001598
Marcel Holtmannf6b77122015-03-14 19:28:05 -07001599 return mgmt_generic_event(MGMT_EV_NEW_SETTINGS, hdev, &ev,
1600 sizeof(ev), skip);
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001601}
1602
Johan Hedberg91a668b2014-07-09 13:28:26 +03001603int mgmt_new_settings(struct hci_dev *hdev)
1604{
1605 return new_settings(hdev, NULL);
1606}
1607
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001608struct cmd_lookup {
1609 struct sock *sk;
1610 struct hci_dev *hdev;
1611 u8 mgmt_status;
1612};
1613
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001614static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001615{
1616 struct cmd_lookup *match = data;
1617
1618 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
1619
1620 list_del(&cmd->list);
1621
1622 if (match->sk == NULL) {
1623 match->sk = cmd->sk;
1624 sock_hold(match->sk);
1625 }
1626
1627 mgmt_pending_free(cmd);
1628}
1629
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001630static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001631{
1632 u8 *status = data;
1633
Johan Hedberga69e8372015-03-06 21:08:53 +02001634 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001635 mgmt_pending_remove(cmd);
1636}
1637
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001638static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg1b9b5ee2014-12-05 13:36:00 +02001639{
1640 if (cmd->cmd_complete) {
1641 u8 *status = data;
1642
1643 cmd->cmd_complete(cmd, *status);
1644 mgmt_pending_remove(cmd);
1645
1646 return;
1647 }
1648
1649 cmd_status_rsp(cmd, data);
1650}
1651
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001652static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedbergf5818c22014-12-05 13:36:02 +02001653{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001654 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1655 cmd->param, cmd->param_len);
Johan Hedbergf5818c22014-12-05 13:36:02 +02001656}
1657
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001658static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001659{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001660 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1661 cmd->param, sizeof(struct mgmt_addr_info));
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001662}
1663
Johan Hedberge6fe7982013-10-02 15:45:22 +03001664static u8 mgmt_bredr_support(struct hci_dev *hdev)
1665{
1666 if (!lmp_bredr_capable(hdev))
1667 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001668 else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001669 return MGMT_STATUS_REJECTED;
1670 else
1671 return MGMT_STATUS_SUCCESS;
1672}
1673
1674static u8 mgmt_le_support(struct hci_dev *hdev)
1675{
1676 if (!lmp_le_capable(hdev))
1677 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001678 else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001679 return MGMT_STATUS_REJECTED;
1680 else
1681 return MGMT_STATUS_SUCCESS;
1682}
1683
Marcel Holtmann1904a852015-01-11 13:50:44 -08001684static void set_discoverable_complete(struct hci_dev *hdev, u8 status,
1685 u16 opcode)
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001686{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001687 struct mgmt_pending_cmd *cmd;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001688 struct mgmt_mode *cp;
Marcel Holtmann970ba522013-10-15 06:33:57 -07001689 struct hci_request req;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001690 bool changed;
1691
1692 BT_DBG("status 0x%02x", status);
1693
1694 hci_dev_lock(hdev);
1695
Johan Hedberg333ae952015-03-17 13:48:47 +02001696 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001697 if (!cmd)
1698 goto unlock;
1699
1700 if (status) {
1701 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001702 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001703 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001704 goto remove_cmd;
1705 }
1706
1707 cp = cmd->param;
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001708 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001709 changed = !hci_dev_test_and_set_flag(hdev, HCI_DISCOVERABLE);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001710
1711 if (hdev->discov_timeout > 0) {
1712 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1713 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
1714 to);
1715 }
1716 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001717 changed = hci_dev_test_and_clear_flag(hdev, HCI_DISCOVERABLE);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001718 }
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001719
1720 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1721
1722 if (changed)
1723 new_settings(hdev, cmd->sk);
1724
Marcel Holtmann970ba522013-10-15 06:33:57 -07001725 /* When the discoverable mode gets changed, make sure
1726 * that class of device has the limited discoverable
Johan Hedberg432df052014-08-01 11:13:31 +03001727 * bit correctly set. Also update page scan based on whitelist
1728 * entries.
Marcel Holtmann970ba522013-10-15 06:33:57 -07001729 */
1730 hci_req_init(&req, hdev);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02001731 __hci_update_page_scan(&req);
Marcel Holtmann970ba522013-10-15 06:33:57 -07001732 update_class(&req);
1733 hci_req_run(&req, NULL);
1734
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001735remove_cmd:
1736 mgmt_pending_remove(cmd);
1737
1738unlock:
1739 hci_dev_unlock(hdev);
1740}
1741
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001742static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001743 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001744{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001745 struct mgmt_cp_set_discoverable *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001746 struct mgmt_pending_cmd *cmd;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001747 struct hci_request req;
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001748 u16 timeout;
Johan Hedberg9a43e252013-10-20 19:00:07 +03001749 u8 scan;
Johan Hedberg03811012010-12-08 00:21:06 +02001750 int err;
Johan Hedberge41d8b42010-12-13 21:07:03 +02001751
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001752 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001753
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001754 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1755 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001756 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1757 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001758
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001759 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02001760 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1761 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001762
Marcel Holtmann1f350c82012-03-12 20:31:08 -07001763 timeout = __le16_to_cpu(cp->timeout);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001764
1765 /* Disabling discoverable requires that no timeout is set,
1766 * and enabling limited discoverable requires a timeout.
1767 */
1768 if ((cp->val == 0x00 && timeout > 0) ||
1769 (cp->val == 0x02 && timeout == 0))
Johan Hedberga69e8372015-03-06 21:08:53 +02001770 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1771 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001772
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001773 hci_dev_lock(hdev);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001774
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001775 if (!hdev_is_powered(hdev) && timeout > 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001776 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1777 MGMT_STATUS_NOT_POWERED);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001778 goto failed;
1779 }
1780
Johan Hedberg333ae952015-03-17 13:48:47 +02001781 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1782 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001783 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1784 MGMT_STATUS_BUSY);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001785 goto failed;
1786 }
1787
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001788 if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001789 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1790 MGMT_STATUS_REJECTED);
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001791 goto failed;
1792 }
1793
1794 if (!hdev_is_powered(hdev)) {
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001795 bool changed = false;
1796
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001797 /* Setting limited discoverable when powered off is
1798 * not a valid operation since it requires a timeout
1799 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1800 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001801 if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07001802 hci_dev_change_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001803 changed = true;
1804 }
1805
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001806 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001807 if (err < 0)
1808 goto failed;
1809
1810 if (changed)
1811 err = new_settings(hdev, sk);
1812
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001813 goto failed;
1814 }
1815
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001816 /* If the current mode is the same, then just update the timeout
1817 * value with the new value. And if only the timeout gets updated,
1818 * then no need for any HCI transactions.
1819 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001820 if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1821 (cp->val == 0x02) == hci_dev_test_flag(hdev,
1822 HCI_LIMITED_DISCOVERABLE)) {
Marcel Holtmann36261542013-10-15 08:28:51 -07001823 cancel_delayed_work(&hdev->discov_off);
1824 hdev->discov_timeout = timeout;
Marcel Holtmann955638e2012-02-22 18:21:00 +01001825
Marcel Holtmann36261542013-10-15 08:28:51 -07001826 if (cp->val && hdev->discov_timeout > 0) {
1827 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001828 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
Marcel Holtmann36261542013-10-15 08:28:51 -07001829 to);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001830 }
1831
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001832 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001833 goto failed;
1834 }
1835
1836 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
1837 if (!cmd) {
1838 err = -ENOMEM;
1839 goto failed;
1840 }
1841
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001842 /* Cancel any potential discoverable timeout that might be
1843 * still active and store new timeout value. The arming of
1844 * the timeout happens in the complete handler.
1845 */
1846 cancel_delayed_work(&hdev->discov_off);
1847 hdev->discov_timeout = timeout;
1848
Johan Hedbergb456f872013-10-19 23:38:22 +03001849 /* Limited discoverable mode */
1850 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001851 hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001852 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001853 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001854
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001855 hci_req_init(&req, hdev);
1856
Johan Hedberg9a43e252013-10-20 19:00:07 +03001857 /* The procedure for LE-only controllers is much simpler - just
1858 * update the advertising data.
1859 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001860 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberg9a43e252013-10-20 19:00:07 +03001861 goto update_ad;
1862
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001863 scan = SCAN_PAGE;
1864
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001865 if (cp->val) {
1866 struct hci_cp_write_current_iac_lap hci_cp;
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001867
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001868 if (cp->val == 0x02) {
1869 /* Limited discoverable mode */
Marcel Holtmann33337dc2013-10-23 08:28:01 -07001870 hci_cp.num_iac = min_t(u8, hdev->num_iac, 2);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001871 hci_cp.iac_lap[0] = 0x00; /* LIAC */
1872 hci_cp.iac_lap[1] = 0x8b;
1873 hci_cp.iac_lap[2] = 0x9e;
1874 hci_cp.iac_lap[3] = 0x33; /* GIAC */
1875 hci_cp.iac_lap[4] = 0x8b;
1876 hci_cp.iac_lap[5] = 0x9e;
1877 } else {
1878 /* General discoverable mode */
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001879 hci_cp.num_iac = 1;
1880 hci_cp.iac_lap[0] = 0x33; /* GIAC */
1881 hci_cp.iac_lap[1] = 0x8b;
1882 hci_cp.iac_lap[2] = 0x9e;
1883 }
1884
1885 hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP,
1886 (hci_cp.num_iac * 3) + 1, &hci_cp);
1887
1888 scan |= SCAN_INQUIRY;
1889 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001890 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001891 }
1892
1893 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001894
Johan Hedberg9a43e252013-10-20 19:00:07 +03001895update_ad:
1896 update_adv_data(&req);
1897
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001898 err = hci_req_run(&req, set_discoverable_complete);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001899 if (err < 0)
1900 mgmt_pending_remove(cmd);
1901
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001902failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001903 hci_dev_unlock(hdev);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001904 return err;
1905}
1906
Johan Hedberg406d7802013-03-15 17:07:09 -05001907static void write_fast_connectable(struct hci_request *req, bool enable)
1908{
Johan Hedbergbd98b992013-03-15 17:07:13 -05001909 struct hci_dev *hdev = req->hdev;
Johan Hedberg406d7802013-03-15 17:07:09 -05001910 struct hci_cp_write_page_scan_activity acp;
1911 u8 type;
1912
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001913 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberg547003b2013-10-21 16:51:53 +03001914 return;
1915
Johan Hedberg4c01f8b2013-03-15 17:07:14 -05001916 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
1917 return;
1918
Johan Hedberg406d7802013-03-15 17:07:09 -05001919 if (enable) {
1920 type = PAGE_SCAN_TYPE_INTERLACED;
1921
1922 /* 160 msec page scan interval */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001923 acp.interval = cpu_to_le16(0x0100);
Johan Hedberg406d7802013-03-15 17:07:09 -05001924 } else {
1925 type = PAGE_SCAN_TYPE_STANDARD; /* default */
1926
1927 /* default 1.28 sec page scan */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001928 acp.interval = cpu_to_le16(0x0800);
Johan Hedberg406d7802013-03-15 17:07:09 -05001929 }
1930
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001931 acp.window = cpu_to_le16(0x0012);
Johan Hedberg406d7802013-03-15 17:07:09 -05001932
Johan Hedbergbd98b992013-03-15 17:07:13 -05001933 if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval ||
1934 __cpu_to_le16(hdev->page_scan_window) != acp.window)
1935 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
1936 sizeof(acp), &acp);
1937
1938 if (hdev->page_scan_type != type)
1939 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type);
Johan Hedberg406d7802013-03-15 17:07:09 -05001940}
1941
Marcel Holtmann1904a852015-01-11 13:50:44 -08001942static void set_connectable_complete(struct hci_dev *hdev, u8 status,
1943 u16 opcode)
Johan Hedberg2b76f452013-03-15 17:07:04 -05001944{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001945 struct mgmt_pending_cmd *cmd;
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001946 struct mgmt_mode *cp;
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001947 bool conn_changed, discov_changed;
Johan Hedberg2b76f452013-03-15 17:07:04 -05001948
1949 BT_DBG("status 0x%02x", status);
1950
1951 hci_dev_lock(hdev);
1952
Johan Hedberg333ae952015-03-17 13:48:47 +02001953 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
Johan Hedberg2b76f452013-03-15 17:07:04 -05001954 if (!cmd)
1955 goto unlock;
1956
Johan Hedberg37438c12013-10-14 16:20:05 +03001957 if (status) {
1958 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001959 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg37438c12013-10-14 16:20:05 +03001960 goto remove_cmd;
1961 }
1962
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001963 cp = cmd->param;
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001964 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001965 conn_changed = !hci_dev_test_and_set_flag(hdev,
1966 HCI_CONNECTABLE);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001967 discov_changed = false;
1968 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001969 conn_changed = hci_dev_test_and_clear_flag(hdev,
1970 HCI_CONNECTABLE);
1971 discov_changed = hci_dev_test_and_clear_flag(hdev,
1972 HCI_DISCOVERABLE);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001973 }
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001974
Johan Hedberg2b76f452013-03-15 17:07:04 -05001975 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
1976
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001977 if (conn_changed || discov_changed) {
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001978 new_settings(hdev, cmd->sk);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02001979 hci_update_page_scan(hdev);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001980 if (discov_changed)
1981 mgmt_update_adv_data(hdev);
Johan Hedberg2b7be332014-07-07 14:40:22 +03001982 hci_update_background_scan(hdev);
1983 }
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001984
Johan Hedberg37438c12013-10-14 16:20:05 +03001985remove_cmd:
Johan Hedberg2b76f452013-03-15 17:07:04 -05001986 mgmt_pending_remove(cmd);
1987
1988unlock:
1989 hci_dev_unlock(hdev);
1990}
1991
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001992static int set_connectable_update_settings(struct hci_dev *hdev,
1993 struct sock *sk, u8 val)
1994{
1995 bool changed = false;
1996 int err;
1997
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001998 if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE))
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001999 changed = true;
2000
2001 if (val) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07002002 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
Johan Hedberge8ba3a12013-10-19 23:38:18 +03002003 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002004 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
2005 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberge8ba3a12013-10-19 23:38:18 +03002006 }
2007
2008 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
2009 if (err < 0)
2010 return err;
2011
Johan Hedberg562064e2014-07-08 16:35:34 +03002012 if (changed) {
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02002013 hci_update_page_scan(hdev);
Johan Hedberg562064e2014-07-08 16:35:34 +03002014 hci_update_background_scan(hdev);
Johan Hedberge8ba3a12013-10-19 23:38:18 +03002015 return new_settings(hdev, sk);
Johan Hedberg562064e2014-07-08 16:35:34 +03002016 }
Johan Hedberge8ba3a12013-10-19 23:38:18 +03002017
2018 return 0;
2019}
2020
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002021static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002022 u16 len)
Johan Hedbergf7b64e62010-12-13 21:07:06 +02002023{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002024 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002025 struct mgmt_pending_cmd *cmd;
Johan Hedberg2b76f452013-03-15 17:07:04 -05002026 struct hci_request req;
Johan Hedberg1987fdc2013-10-14 21:15:24 +03002027 u8 scan;
Johan Hedbergf7b64e62010-12-13 21:07:06 +02002028 int err;
Johan Hedberg03811012010-12-08 00:21:06 +02002029
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002030 BT_DBG("request for %s", hdev->name);
Johan Hedberge41d8b42010-12-13 21:07:03 +02002031
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002032 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
2033 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02002034 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
2035 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03002036
Johan Hedberga7e80f22013-01-09 16:05:19 +02002037 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002038 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
2039 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002040
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002041 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002042
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002043 if (!hdev_is_powered(hdev)) {
Johan Hedberge8ba3a12013-10-19 23:38:18 +03002044 err = set_connectable_update_settings(hdev, sk, cp->val);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002045 goto failed;
2046 }
2047
Johan Hedberg333ae952015-03-17 13:48:47 +02002048 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
2049 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002050 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
2051 MGMT_STATUS_BUSY);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002052 goto failed;
2053 }
2054
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002055 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
2056 if (!cmd) {
2057 err = -ENOMEM;
2058 goto failed;
2059 }
2060
Johan Hedberg2b76f452013-03-15 17:07:04 -05002061 hci_req_init(&req, hdev);
2062
Johan Hedberg9a43e252013-10-20 19:00:07 +03002063 /* If BR/EDR is not enabled and we disable advertising as a
2064 * by-product of disabling connectable, we need to update the
2065 * advertising flags.
2066 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002067 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg9a43e252013-10-20 19:00:07 +03002068 if (!cp->val) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002069 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2070 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberg9a43e252013-10-20 19:00:07 +03002071 }
2072 update_adv_data(&req);
2073 } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) {
Johan Hedberg9b742462013-10-14 16:20:03 +03002074 if (cp->val) {
2075 scan = SCAN_PAGE;
2076 } else {
Johan Hedberg3bd27242014-07-28 20:53:58 +03002077 /* If we don't have any whitelist entries just
2078 * disable all scanning. If there are entries
2079 * and we had both page and inquiry scanning
2080 * enabled then fall back to only page scanning.
2081 * Otherwise no changes are needed.
2082 */
2083 if (list_empty(&hdev->whitelist))
2084 scan = SCAN_DISABLED;
2085 else if (test_bit(HCI_ISCAN, &hdev->flags))
2086 scan = SCAN_PAGE;
2087 else
2088 goto no_scan_update;
Johan Hedberg9b742462013-10-14 16:20:03 +03002089
2090 if (test_bit(HCI_ISCAN, &hdev->flags) &&
Marcel Holtmann8d6083f2013-10-14 16:38:45 -07002091 hdev->discov_timeout > 0)
Johan Hedberg9b742462013-10-14 16:20:03 +03002092 cancel_delayed_work(&hdev->discov_off);
2093 }
2094
2095 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
2096 }
Johan Hedberg2b76f452013-03-15 17:07:04 -05002097
Johan Hedberg3bd27242014-07-28 20:53:58 +03002098no_scan_update:
Johan Hedberge8b12022014-07-10 10:51:27 +03002099 /* Update the advertising parameters if necessary */
Arman Uguray880897d2015-03-28 12:39:00 -07002100 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
2101 hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
Johan Hedberg1987fdc2013-10-14 21:15:24 +03002102 enable_advertising(&req);
Johan Hedberg1987fdc2013-10-14 21:15:24 +03002103
Johan Hedberg2b76f452013-03-15 17:07:04 -05002104 err = hci_req_run(&req, set_connectable_complete);
Johan Hedberg9b742462013-10-14 16:20:03 +03002105 if (err < 0) {
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002106 mgmt_pending_remove(cmd);
Johan Hedberg9b742462013-10-14 16:20:03 +03002107 if (err == -ENODATA)
Johan Hedberga81070b2013-10-19 23:38:19 +03002108 err = set_connectable_update_settings(hdev, sk,
2109 cp->val);
Johan Hedberg9b742462013-10-14 16:20:03 +03002110 goto failed;
2111 }
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002112
2113failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002114 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002115 return err;
2116}
2117
Johan Hedbergb2939472014-07-30 09:22:23 +03002118static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002119 u16 len)
Johan Hedberg73f22f62010-12-29 16:00:25 +02002120{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002121 struct mgmt_mode *cp = data;
Marcel Holtmann55594352013-10-06 16:11:57 -07002122 bool changed;
Johan Hedberg73f22f62010-12-29 16:00:25 +02002123 int err;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002124
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002125 BT_DBG("request for %s", hdev->name);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002126
Johan Hedberga7e80f22013-01-09 16:05:19 +02002127 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002128 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE,
2129 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002130
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002131 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002132
2133 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07002134 changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002135 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002136 changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002137
Johan Hedbergb2939472014-07-30 09:22:23 +03002138 err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002139 if (err < 0)
Marcel Holtmann55594352013-10-06 16:11:57 -07002140 goto unlock;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002141
Marcel Holtmann55594352013-10-06 16:11:57 -07002142 if (changed)
2143 err = new_settings(hdev, sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002144
Marcel Holtmann55594352013-10-06 16:11:57 -07002145unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002146 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002147 return err;
2148}
Johan Hedberg72a734e2010-12-30 00:38:22 +02002149
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002150static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
2151 u16 len)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002152{
2153 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002154 struct mgmt_pending_cmd *cmd;
Johan Hedberge6fe7982013-10-02 15:45:22 +03002155 u8 val, status;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002156 int err;
2157
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002158 BT_DBG("request for %s", hdev->name);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002159
Johan Hedberge6fe7982013-10-02 15:45:22 +03002160 status = mgmt_bredr_support(hdev);
2161 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02002162 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2163 status);
Johan Hedberg33c525c2012-10-24 21:11:58 +03002164
Johan Hedberga7e80f22013-01-09 16:05:19 +02002165 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002166 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2167 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002168
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002169 hci_dev_lock(hdev);
2170
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002171 if (!hdev_is_powered(hdev)) {
Johan Hedberg47990ea2012-02-22 11:58:37 +02002172 bool changed = false;
2173
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002174 if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07002175 hci_dev_change_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02002176 changed = true;
2177 }
2178
2179 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
2180 if (err < 0)
2181 goto failed;
2182
2183 if (changed)
2184 err = new_settings(hdev, sk);
2185
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002186 goto failed;
2187 }
2188
Johan Hedberg333ae952015-03-17 13:48:47 +02002189 if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002190 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2191 MGMT_STATUS_BUSY);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002192 goto failed;
2193 }
2194
2195 val = !!cp->val;
2196
2197 if (test_bit(HCI_AUTH, &hdev->flags) == val) {
2198 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
2199 goto failed;
2200 }
2201
2202 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
2203 if (!cmd) {
2204 err = -ENOMEM;
2205 goto failed;
2206 }
2207
2208 err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
2209 if (err < 0) {
2210 mgmt_pending_remove(cmd);
2211 goto failed;
2212 }
2213
2214failed:
2215 hci_dev_unlock(hdev);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002216 return err;
2217}
2218
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002219static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002220{
2221 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002222 struct mgmt_pending_cmd *cmd;
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07002223 u8 status;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002224 int err;
2225
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002226 BT_DBG("request for %s", hdev->name);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002227
Marcel Holtmanncdba5282013-10-02 21:31:52 -07002228 status = mgmt_bredr_support(hdev);
2229 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02002230 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
Marcel Holtmanncdba5282013-10-02 21:31:52 -07002231
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002232 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002233 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2234 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002235
Johan Hedberga7e80f22013-01-09 16:05:19 +02002236 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002237 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2238 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002239
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002240 hci_dev_lock(hdev);
Johan Hedberg6c8f12c2012-02-22 16:35:26 +02002241
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002242 if (!hdev_is_powered(hdev)) {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002243 bool changed;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02002244
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002245 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07002246 changed = !hci_dev_test_and_set_flag(hdev,
2247 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002248 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002249 changed = hci_dev_test_and_clear_flag(hdev,
2250 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002251 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002252 changed = hci_dev_test_and_clear_flag(hdev,
2253 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002254 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002255 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02002256 }
2257
2258 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2259 if (err < 0)
2260 goto failed;
2261
2262 if (changed)
2263 err = new_settings(hdev, sk);
2264
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002265 goto failed;
2266 }
2267
Johan Hedberg333ae952015-03-17 13:48:47 +02002268 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002269 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2270 MGMT_STATUS_BUSY);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002271 goto failed;
2272 }
2273
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002274 if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002275 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2276 goto failed;
2277 }
2278
2279 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
2280 if (!cmd) {
2281 err = -ENOMEM;
2282 goto failed;
2283 }
2284
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002285 if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03002286 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2287 sizeof(cp->val), &cp->val);
2288
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07002289 err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002290 if (err < 0) {
2291 mgmt_pending_remove(cmd);
2292 goto failed;
2293 }
2294
2295failed:
2296 hci_dev_unlock(hdev);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002297 return err;
2298}
2299
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002300static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002301{
2302 struct mgmt_mode *cp = data;
Marcel Holtmannee392692013-10-01 22:59:23 -07002303 bool changed;
Johan Hedberge6fe7982013-10-02 15:45:22 +03002304 u8 status;
Marcel Holtmannee392692013-10-01 22:59:23 -07002305 int err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002306
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002307 BT_DBG("request for %s", hdev->name);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002308
Johan Hedberge6fe7982013-10-02 15:45:22 +03002309 status = mgmt_bredr_support(hdev);
2310 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02002311 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002312
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002313 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002314 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2315 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002316
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002317 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02002318 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2319 MGMT_STATUS_REJECTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002320
Johan Hedberga7e80f22013-01-09 16:05:19 +02002321 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002322 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2323 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002324
Marcel Holtmannee392692013-10-01 22:59:23 -07002325 hci_dev_lock(hdev);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002326
Johan Hedberg333ae952015-03-17 13:48:47 +02002327 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002328 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2329 MGMT_STATUS_BUSY);
Johan Hedberga2cb01d2015-02-19 17:38:07 +02002330 goto unlock;
2331 }
2332
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002333 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07002334 changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002335 } else {
2336 if (hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002337 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2338 MGMT_STATUS_REJECTED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002339 goto unlock;
2340 }
2341
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002342 changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002343 }
Marcel Holtmannee392692013-10-01 22:59:23 -07002344
2345 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
2346 if (err < 0)
2347 goto unlock;
2348
2349 if (changed)
2350 err = new_settings(hdev, sk);
2351
2352unlock:
2353 hci_dev_unlock(hdev);
2354 return err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002355}
2356
Marcel Holtmann1904a852015-01-11 13:50:44 -08002357static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002358{
2359 struct cmd_lookup match = { NULL, hdev };
2360
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302361 hci_dev_lock(hdev);
2362
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002363 if (status) {
2364 u8 mgmt_err = mgmt_status(status);
2365
2366 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
2367 &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302368 goto unlock;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002369 }
2370
2371 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
2372
2373 new_settings(hdev, match.sk);
2374
2375 if (match.sk)
2376 sock_put(match.sk);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002377
2378 /* Make sure the controller has a good default for
2379 * advertising data. Restrict the update to when LE
2380 * has actually been enabled. During power on, the
2381 * update in powered_update_hci will take care of it.
2382 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002383 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002384 struct hci_request req;
2385
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002386 hci_req_init(&req, hdev);
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07002387 update_adv_data(&req);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07002388 update_scan_rsp_data(&req);
Johan Hedberg2cf22212014-12-19 22:26:00 +02002389 __hci_update_background_scan(&req);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002390 hci_req_run(&req, NULL);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002391 }
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302392
2393unlock:
2394 hci_dev_unlock(hdev);
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002395}
2396
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002397static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg06199cf2012-02-22 16:37:11 +02002398{
2399 struct mgmt_mode *cp = data;
2400 struct hci_cp_write_le_host_supported hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002401 struct mgmt_pending_cmd *cmd;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002402 struct hci_request req;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002403 int err;
Johan Hedberg0b60eba2012-02-28 00:57:24 +02002404 u8 val, enabled;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002405
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002406 BT_DBG("request for %s", hdev->name);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002407
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002408 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002409 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2410 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg1de028c2012-02-29 19:55:35 -08002411
Johan Hedberga7e80f22013-01-09 16:05:19 +02002412 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002413 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2414 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002415
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07002416 /* Bluetooth single mode LE only controllers or dual-mode
2417 * controllers configured as LE only devices, do not allow
2418 * switching LE off. These have either LE enabled explicitly
2419 * or BR/EDR has been previously switched off.
2420 *
2421 * When trying to enable an already enabled LE, then gracefully
2422 * send a positive response. Trying to disable it however will
2423 * result into rejection.
2424 */
2425 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
2426 if (cp->val == 0x01)
2427 return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2428
Johan Hedberga69e8372015-03-06 21:08:53 +02002429 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2430 MGMT_STATUS_REJECTED);
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07002431 }
Johan Hedbergc73eee92013-04-19 18:35:21 +03002432
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002433 hci_dev_lock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002434
2435 val = !!cp->val;
Gustavo Padovanffa88e02012-11-23 16:50:51 -02002436 enabled = lmp_host_le_capable(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002437
Johan Hedberg0b60eba2012-02-28 00:57:24 +02002438 if (!hdev_is_powered(hdev) || val == enabled) {
Johan Hedberg06199cf2012-02-22 16:37:11 +02002439 bool changed = false;
2440
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002441 if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07002442 hci_dev_change_flag(hdev, HCI_LE_ENABLED);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002443 changed = true;
2444 }
2445
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002446 if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002447 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03002448 changed = true;
2449 }
2450
Johan Hedberg06199cf2012-02-22 16:37:11 +02002451 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2452 if (err < 0)
Johan Hedberg1de028c2012-02-29 19:55:35 -08002453 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002454
2455 if (changed)
2456 err = new_settings(hdev, sk);
2457
Johan Hedberg1de028c2012-02-29 19:55:35 -08002458 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002459 }
2460
Johan Hedberg333ae952015-03-17 13:48:47 +02002461 if (pending_find(MGMT_OP_SET_LE, hdev) ||
2462 pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002463 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2464 MGMT_STATUS_BUSY);
Johan Hedberg1de028c2012-02-29 19:55:35 -08002465 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002466 }
2467
2468 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
2469 if (!cmd) {
2470 err = -ENOMEM;
Johan Hedberg1de028c2012-02-29 19:55:35 -08002471 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002472 }
2473
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002474 hci_req_init(&req, hdev);
2475
Johan Hedberg06199cf2012-02-22 16:37:11 +02002476 memset(&hci_cp, 0, sizeof(hci_cp));
2477
2478 if (val) {
2479 hci_cp.le = val;
Marcel Holtmann32226e42014-07-24 20:04:16 +02002480 hci_cp.simul = 0x00;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002481 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002482 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002483 disable_advertising(&req);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002484 }
2485
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002486 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp),
2487 &hci_cp);
2488
2489 err = hci_req_run(&req, le_enable_complete);
Syam Sidhardhan0c01bc42012-04-12 20:33:21 +05302490 if (err < 0)
Johan Hedberg06199cf2012-02-22 16:37:11 +02002491 mgmt_pending_remove(cmd);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002492
Johan Hedberg1de028c2012-02-29 19:55:35 -08002493unlock:
2494 hci_dev_unlock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002495 return err;
2496}
2497
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002498/* This is a helper function to test for pending mgmt commands that can
2499 * cause CoD or EIR HCI commands. We can only allow one such pending
2500 * mgmt command at a time since otherwise we cannot easily track what
2501 * the current values are, will be, and based on that calculate if a new
2502 * HCI command needs to be sent and if yes with what value.
2503 */
2504static bool pending_eir_or_class(struct hci_dev *hdev)
2505{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002506 struct mgmt_pending_cmd *cmd;
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002507
2508 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
2509 switch (cmd->opcode) {
2510 case MGMT_OP_ADD_UUID:
2511 case MGMT_OP_REMOVE_UUID:
2512 case MGMT_OP_SET_DEV_CLASS:
2513 case MGMT_OP_SET_POWERED:
2514 return true;
2515 }
2516 }
2517
2518 return false;
2519}
2520
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002521static const u8 bluetooth_base_uuid[] = {
2522 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
2523 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2524};
2525
2526static u8 get_uuid_size(const u8 *uuid)
2527{
2528 u32 val;
2529
2530 if (memcmp(uuid, bluetooth_base_uuid, 12))
2531 return 128;
2532
2533 val = get_unaligned_le32(&uuid[12]);
2534 if (val > 0xffff)
2535 return 32;
2536
2537 return 16;
2538}
2539
Johan Hedberg92da6092013-03-15 17:06:55 -05002540static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status)
2541{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002542 struct mgmt_pending_cmd *cmd;
Johan Hedberg92da6092013-03-15 17:06:55 -05002543
2544 hci_dev_lock(hdev);
2545
Johan Hedberg333ae952015-03-17 13:48:47 +02002546 cmd = pending_find(mgmt_op, hdev);
Johan Hedberg92da6092013-03-15 17:06:55 -05002547 if (!cmd)
2548 goto unlock;
2549
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002550 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
2551 mgmt_status(status), hdev->dev_class, 3);
Johan Hedberg92da6092013-03-15 17:06:55 -05002552
2553 mgmt_pending_remove(cmd);
2554
2555unlock:
2556 hci_dev_unlock(hdev);
2557}
2558
Marcel Holtmann1904a852015-01-11 13:50:44 -08002559static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002560{
2561 BT_DBG("status 0x%02x", status);
2562
2563 mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status);
2564}
2565
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002566static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002567{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002568 struct mgmt_cp_add_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002569 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002570 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002571 struct bt_uuid *uuid;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002572 int err;
2573
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002574 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002575
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002576 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002577
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002578 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002579 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
2580 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002581 goto failed;
2582 }
2583
Andre Guedes92c4c202012-06-07 19:05:44 -03002584 uuid = kmalloc(sizeof(*uuid), GFP_KERNEL);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002585 if (!uuid) {
2586 err = -ENOMEM;
2587 goto failed;
2588 }
2589
2590 memcpy(uuid->uuid, cp->uuid, 16);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002591 uuid->svc_hint = cp->svc_hint;
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002592 uuid->size = get_uuid_size(cp->uuid);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002593
Johan Hedbergde66aa62013-01-27 00:31:27 +02002594 list_add_tail(&uuid->list, &hdev->uuids);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002595
Johan Hedberg890ea892013-03-15 17:06:52 -05002596 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002597
Johan Hedberg890ea892013-03-15 17:06:52 -05002598 update_class(&req);
2599 update_eir(&req);
2600
Johan Hedberg92da6092013-03-15 17:06:55 -05002601 err = hci_req_run(&req, add_uuid_complete);
2602 if (err < 0) {
2603 if (err != -ENODATA)
2604 goto failed;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002605
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002606 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
2607 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002608 goto failed;
2609 }
2610
2611 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002612 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002613 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002614 goto failed;
2615 }
2616
2617 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002618
2619failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002620 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002621 return err;
2622}
2623
Johan Hedberg24b78d02012-02-23 23:24:30 +02002624static bool enable_service_cache(struct hci_dev *hdev)
2625{
2626 if (!hdev_is_powered(hdev))
2627 return false;
2628
Marcel Holtmann238be782015-03-13 02:11:06 -07002629 if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg46818ed2013-01-14 22:33:52 +02002630 queue_delayed_work(hdev->workqueue, &hdev->service_cache,
2631 CACHE_TIMEOUT);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002632 return true;
2633 }
2634
2635 return false;
2636}
2637
Marcel Holtmann1904a852015-01-11 13:50:44 -08002638static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002639{
2640 BT_DBG("status 0x%02x", status);
2641
2642 mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status);
2643}
2644
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002645static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002646 u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002647{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002648 struct mgmt_cp_remove_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002649 struct mgmt_pending_cmd *cmd;
Johan Hedberg056341c2013-01-27 00:31:30 +02002650 struct bt_uuid *match, *tmp;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002651 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 -05002652 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002653 int err, found;
2654
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002655 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002656
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002657 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002658
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002659 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002660 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2661 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002662 goto unlock;
2663 }
2664
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002665 if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
Johan Hedberg35f74982014-02-18 17:14:32 +02002666 hci_uuids_clear(hdev);
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002667
Johan Hedberg24b78d02012-02-23 23:24:30 +02002668 if (enable_service_cache(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002669 err = mgmt_cmd_complete(sk, hdev->id,
2670 MGMT_OP_REMOVE_UUID,
2671 0, hdev->dev_class, 3);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002672 goto unlock;
2673 }
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002674
Johan Hedberg9246a862012-02-23 21:33:16 +02002675 goto update_class;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002676 }
2677
2678 found = 0;
2679
Johan Hedberg056341c2013-01-27 00:31:30 +02002680 list_for_each_entry_safe(match, tmp, &hdev->uuids, list) {
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002681 if (memcmp(match->uuid, cp->uuid, 16) != 0)
2682 continue;
2683
2684 list_del(&match->list);
Johan Hedberg482049f2012-11-08 10:25:26 +01002685 kfree(match);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002686 found++;
2687 }
2688
2689 if (found == 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002690 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2691 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002692 goto unlock;
2693 }
2694
Johan Hedberg9246a862012-02-23 21:33:16 +02002695update_class:
Johan Hedberg890ea892013-03-15 17:06:52 -05002696 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002697
Johan Hedberg890ea892013-03-15 17:06:52 -05002698 update_class(&req);
2699 update_eir(&req);
2700
Johan Hedberg92da6092013-03-15 17:06:55 -05002701 err = hci_req_run(&req, remove_uuid_complete);
2702 if (err < 0) {
2703 if (err != -ENODATA)
2704 goto unlock;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002705
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002706 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
2707 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002708 goto unlock;
2709 }
2710
2711 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002712 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002713 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002714 goto unlock;
2715 }
2716
2717 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002718
2719unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002720 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002721 return err;
2722}
2723
Marcel Holtmann1904a852015-01-11 13:50:44 -08002724static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002725{
2726 BT_DBG("status 0x%02x", status);
2727
2728 mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status);
2729}
2730
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002731static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002732 u16 len)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002733{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002734 struct mgmt_cp_set_dev_class *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002735 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002736 struct hci_request req;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002737 int err;
2738
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002739 BT_DBG("request for %s", hdev->name);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002740
Marcel Holtmann6203fc92013-10-02 23:37:29 -07002741 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002742 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2743 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002744
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002745 hci_dev_lock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002746
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002747 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002748 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2749 MGMT_STATUS_BUSY);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002750 goto unlock;
2751 }
2752
2753 if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002754 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2755 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002756 goto unlock;
2757 }
2758
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002759 hdev->major_class = cp->major;
2760 hdev->minor_class = cp->minor;
2761
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002762 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002763 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2764 hdev->dev_class, 3);
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002765 goto unlock;
2766 }
2767
Johan Hedberg890ea892013-03-15 17:06:52 -05002768 hci_req_init(&req, hdev);
2769
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002770 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg7d785252011-12-15 00:47:39 +02002771 hci_dev_unlock(hdev);
2772 cancel_delayed_work_sync(&hdev->service_cache);
2773 hci_dev_lock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -05002774 update_eir(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02002775 }
Johan Hedberg14c0b602011-12-15 00:47:37 +02002776
Johan Hedberg890ea892013-03-15 17:06:52 -05002777 update_class(&req);
2778
Johan Hedberg92da6092013-03-15 17:06:55 -05002779 err = hci_req_run(&req, set_class_complete);
2780 if (err < 0) {
2781 if (err != -ENODATA)
2782 goto unlock;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002783
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002784 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2785 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002786 goto unlock;
2787 }
2788
2789 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002790 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002791 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002792 goto unlock;
2793 }
2794
2795 err = 0;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002796
Johan Hedbergb5235a62012-02-21 14:32:24 +02002797unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002798 hci_dev_unlock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002799 return err;
2800}
2801
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002802static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002803 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002804{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002805 struct mgmt_cp_load_link_keys *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03002806 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
2807 sizeof(struct mgmt_link_key_info));
Szymon Janc4e51eae2011-02-25 19:05:48 +01002808 u16 key_count, expected_len;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002809 bool changed;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002810 int i;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002811
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002812 BT_DBG("request for %s", hdev->name);
2813
2814 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002815 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2816 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002817
Marcel Holtmann1f350c82012-03-12 20:31:08 -07002818 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002819 if (key_count > max_key_count) {
2820 BT_ERR("load_link_keys: too big key_count value %u",
2821 key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02002822 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2823 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002824 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002825
Johan Hedberg86742e12011-11-07 23:13:38 +02002826 expected_len = sizeof(*cp) + key_count *
2827 sizeof(struct mgmt_link_key_info);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002828 if (expected_len != len) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002829 BT_ERR("load_link_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02002830 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02002831 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2832 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002833 }
2834
Johan Hedberg4ae14302013-01-20 14:27:13 +02002835 if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002836 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2837 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ae14302013-01-20 14:27:13 +02002838
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002839 BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002840 key_count);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002841
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002842 for (i = 0; i < key_count; i++) {
2843 struct mgmt_link_key_info *key = &cp->keys[i];
2844
Marcel Holtmann8e991132014-01-10 02:07:25 -08002845 if (key->addr.type != BDADDR_BREDR || key->type > 0x08)
Johan Hedberga69e8372015-03-06 21:08:53 +02002846 return mgmt_cmd_status(sk, hdev->id,
2847 MGMT_OP_LOAD_LINK_KEYS,
2848 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002849 }
2850
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002851 hci_dev_lock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002852
2853 hci_link_keys_clear(hdev);
2854
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002855 if (cp->debug_keys)
Marcel Holtmann238be782015-03-13 02:11:06 -07002856 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002857 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002858 changed = hci_dev_test_and_clear_flag(hdev,
2859 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002860
2861 if (changed)
2862 new_settings(hdev, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002863
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002864 for (i = 0; i < key_count; i++) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002865 struct mgmt_link_key_info *key = &cp->keys[i];
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002866
Johan Hedberg58e92932014-06-24 14:00:26 +03002867 /* Always ignore debug keys and require a new pairing if
2868 * the user wants to use them.
2869 */
2870 if (key->type == HCI_LK_DEBUG_COMBINATION)
2871 continue;
2872
Johan Hedberg7652ff62014-06-24 13:15:49 +03002873 hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val,
2874 key->type, key->pin_len, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002875 }
2876
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002877 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
Johan Hedberg0e5f8752011-11-11 16:18:54 +02002878
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002879 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002880
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002881 return 0;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002882}
2883
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002884static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002885 u8 addr_type, struct sock *skip_sk)
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002886{
2887 struct mgmt_ev_device_unpaired ev;
2888
2889 bacpy(&ev.addr.bdaddr, bdaddr);
2890 ev.addr.type = addr_type;
2891
2892 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002893 skip_sk);
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002894}
2895
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002896static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002897 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002898{
Johan Hedberg124f6e32012-02-09 13:50:12 +02002899 struct mgmt_cp_unpair_device *cp = data;
2900 struct mgmt_rp_unpair_device rp;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002901 struct hci_cp_disconnect dc;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002902 struct mgmt_pending_cmd *cmd;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002903 struct hci_conn *conn;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002904 int err;
2905
Johan Hedberga8a1d192011-11-10 15:54:38 +02002906 memset(&rp, 0, sizeof(rp));
Johan Hedberg124f6e32012-02-09 13:50:12 +02002907 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2908 rp.addr.type = cp->addr.type;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002909
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002910 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002911 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2912 MGMT_STATUS_INVALID_PARAMS,
2913 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002914
Johan Hedberg118da702013-01-20 14:27:20 +02002915 if (cp->disconnect != 0x00 && cp->disconnect != 0x01)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002916 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2917 MGMT_STATUS_INVALID_PARAMS,
2918 &rp, sizeof(rp));
Johan Hedberg118da702013-01-20 14:27:20 +02002919
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002920 hci_dev_lock(hdev);
2921
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002922 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002923 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2924 MGMT_STATUS_NOT_POWERED, &rp,
2925 sizeof(rp));
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002926 goto unlock;
2927 }
2928
Johan Hedberge0b2b272014-02-18 17:14:31 +02002929 if (cp->addr.type == BDADDR_BREDR) {
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002930 /* If disconnection is requested, then look up the
2931 * connection. If the remote device is connected, it
2932 * will be later used to terminate the link.
2933 *
2934 * Setting it to NULL explicitly will cause no
2935 * termination of the link.
2936 */
2937 if (cp->disconnect)
2938 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2939 &cp->addr.bdaddr);
2940 else
2941 conn = NULL;
2942
Johan Hedberg124f6e32012-02-09 13:50:12 +02002943 err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
Johan Hedberge0b2b272014-02-18 17:14:31 +02002944 } else {
2945 u8 addr_type;
2946
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002947 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK,
2948 &cp->addr.bdaddr);
2949 if (conn) {
2950 /* Defer clearing up the connection parameters
2951 * until closing to give a chance of keeping
2952 * them if a repairing happens.
2953 */
2954 set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
2955
2956 /* If disconnection is not requested, then
2957 * clear the connection variable so that the
2958 * link is not terminated.
2959 */
2960 if (!cp->disconnect)
2961 conn = NULL;
2962 }
2963
Johan Hedberge0b2b272014-02-18 17:14:31 +02002964 if (cp->addr.type == BDADDR_LE_PUBLIC)
2965 addr_type = ADDR_LE_DEV_PUBLIC;
2966 else
2967 addr_type = ADDR_LE_DEV_RANDOM;
2968
Johan Hedberga7ec7332014-02-18 17:14:35 +02002969 hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type);
2970
Johan Hedberge0b2b272014-02-18 17:14:31 +02002971 err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type);
2972 }
Vinicius Costa Gomesb0dbfb42012-02-02 21:08:03 -03002973
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002974 if (err < 0) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002975 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2976 MGMT_STATUS_NOT_PAIRED, &rp,
2977 sizeof(rp));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002978 goto unlock;
2979 }
2980
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002981 /* If the connection variable is set, then termination of the
2982 * link is requested.
2983 */
Johan Hedberga8a1d192011-11-10 15:54:38 +02002984 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002985 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
2986 &rp, sizeof(rp));
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002987 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002988 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002989 }
2990
Johan Hedberg124f6e32012-02-09 13:50:12 +02002991 cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002992 sizeof(*cp));
Johan Hedberga8a1d192011-11-10 15:54:38 +02002993 if (!cmd) {
2994 err = -ENOMEM;
2995 goto unlock;
2996 }
2997
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02002998 cmd->cmd_complete = addr_cmd_complete;
2999
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02003000 dc.handle = cpu_to_le16(conn->handle);
Johan Hedberga8a1d192011-11-10 15:54:38 +02003001 dc.reason = 0x13; /* Remote User Terminated Connection */
3002 err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc);
3003 if (err < 0)
3004 mgmt_pending_remove(cmd);
3005
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003006unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003007 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003008 return err;
3009}
3010
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003011static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003012 u16 len)
Johan Hedberg8962ee72011-01-20 12:40:27 +02003013{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003014 struct mgmt_cp_disconnect *cp = data;
Johan Hedberg06a63b12013-01-20 14:27:21 +02003015 struct mgmt_rp_disconnect rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003016 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02003017 struct hci_conn *conn;
Johan Hedberg8962ee72011-01-20 12:40:27 +02003018 int err;
3019
3020 BT_DBG("");
3021
Johan Hedberg06a63b12013-01-20 14:27:21 +02003022 memset(&rp, 0, sizeof(rp));
3023 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3024 rp.addr.type = cp->addr.type;
3025
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003026 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003027 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3028 MGMT_STATUS_INVALID_PARAMS,
3029 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003030
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003031 hci_dev_lock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003032
3033 if (!test_bit(HCI_UP, &hdev->flags)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003034 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3035 MGMT_STATUS_NOT_POWERED, &rp,
3036 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02003037 goto failed;
3038 }
3039
Johan Hedberg333ae952015-03-17 13:48:47 +02003040 if (pending_find(MGMT_OP_DISCONNECT, hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003041 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3042 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02003043 goto failed;
3044 }
3045
Andre Guedes591f47f2012-04-24 21:02:49 -03003046 if (cp->addr.type == BDADDR_BREDR)
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003047 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
3048 &cp->addr.bdaddr);
Johan Hedberg88c3df12012-02-09 14:27:38 +02003049 else
3050 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
Vinicius Costa Gomes365227e2011-05-06 18:41:44 -03003051
Vishal Agarwalf9607272012-06-13 05:32:43 +05303052 if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003053 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3054 MGMT_STATUS_NOT_CONNECTED, &rp,
3055 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02003056 goto failed;
3057 }
3058
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003059 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03003060 if (!cmd) {
3061 err = -ENOMEM;
Johan Hedberg8962ee72011-01-20 12:40:27 +02003062 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03003063 }
Johan Hedberg8962ee72011-01-20 12:40:27 +02003064
Johan Hedbergf5818c22014-12-05 13:36:02 +02003065 cmd->cmd_complete = generic_cmd_complete;
3066
Johan Hedberge3f2f922014-08-18 20:33:33 +03003067 err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003068 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03003069 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003070
3071failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003072 hci_dev_unlock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003073 return err;
3074}
3075
Andre Guedes57c14772012-04-24 21:02:50 -03003076static u8 link_to_bdaddr(u8 link_type, u8 addr_type)
Johan Hedberg4c659c32011-11-07 23:13:39 +02003077{
3078 switch (link_type) {
3079 case LE_LINK:
Johan Hedberg48264f02011-11-09 13:58:58 +02003080 switch (addr_type) {
3081 case ADDR_LE_DEV_PUBLIC:
Andre Guedes591f47f2012-04-24 21:02:49 -03003082 return BDADDR_LE_PUBLIC;
Andre Guedes0ed09142012-04-03 08:46:54 -03003083
Johan Hedberg48264f02011-11-09 13:58:58 +02003084 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03003085 /* Fallback to LE Random address type */
Andre Guedes591f47f2012-04-24 21:02:49 -03003086 return BDADDR_LE_RANDOM;
Johan Hedberg48264f02011-11-09 13:58:58 +02003087 }
Andre Guedes0ed09142012-04-03 08:46:54 -03003088
Johan Hedberg4c659c32011-11-07 23:13:39 +02003089 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03003090 /* Fallback to BR/EDR type */
Andre Guedes591f47f2012-04-24 21:02:49 -03003091 return BDADDR_BREDR;
Johan Hedberg4c659c32011-11-07 23:13:39 +02003092 }
3093}
3094
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003095static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
3096 u16 data_len)
Johan Hedberg2784eb42011-01-21 13:56:35 +02003097{
Johan Hedberg2784eb42011-01-21 13:56:35 +02003098 struct mgmt_rp_get_connections *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02003099 struct hci_conn *c;
Johan Hedberga38528f2011-01-22 06:46:43 +02003100 size_t rp_len;
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003101 int err;
3102 u16 i;
Johan Hedberg2784eb42011-01-21 13:56:35 +02003103
3104 BT_DBG("");
3105
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003106 hci_dev_lock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02003107
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003108 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003109 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
3110 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003111 goto unlock;
3112 }
3113
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003114 i = 0;
Johan Hedbergb644ba32012-01-17 21:48:47 +02003115 list_for_each_entry(c, &hdev->conn_hash.list, list) {
3116 if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003117 i++;
Johan Hedberg2784eb42011-01-21 13:56:35 +02003118 }
3119
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003120 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Andre Guedes92c4c202012-06-07 19:05:44 -03003121 rp = kmalloc(rp_len, GFP_KERNEL);
Johan Hedberga38528f2011-01-22 06:46:43 +02003122 if (!rp) {
Johan Hedberg2784eb42011-01-21 13:56:35 +02003123 err = -ENOMEM;
3124 goto unlock;
3125 }
3126
Johan Hedberg2784eb42011-01-21 13:56:35 +02003127 i = 0;
Johan Hedberg4c659c32011-11-07 23:13:39 +02003128 list_for_each_entry(c, &hdev->conn_hash.list, list) {
Johan Hedbergb644ba32012-01-17 21:48:47 +02003129 if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
3130 continue;
Johan Hedberg4c659c32011-11-07 23:13:39 +02003131 bacpy(&rp->addr[i].bdaddr, &c->dst);
Andre Guedes57c14772012-04-24 21:02:50 -03003132 rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type);
Andre Guedes0ed09142012-04-03 08:46:54 -03003133 if (c->type == SCO_LINK || c->type == ESCO_LINK)
Johan Hedberg4c659c32011-11-07 23:13:39 +02003134 continue;
3135 i++;
3136 }
3137
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02003138 rp->conn_count = cpu_to_le16(i);
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003139
Johan Hedberg4c659c32011-11-07 23:13:39 +02003140 /* Recalculate length in case of filtered SCO connections, etc */
3141 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Johan Hedberg2784eb42011-01-21 13:56:35 +02003142
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003143 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
3144 rp_len);
Johan Hedberg2784eb42011-01-21 13:56:35 +02003145
Johan Hedberga38528f2011-01-22 06:46:43 +02003146 kfree(rp);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003147
3148unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003149 hci_dev_unlock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02003150 return err;
3151}
3152
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003153static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003154 struct mgmt_cp_pin_code_neg_reply *cp)
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003155{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003156 struct mgmt_pending_cmd *cmd;
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003157 int err;
3158
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003159 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003160 sizeof(*cp));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003161 if (!cmd)
3162 return -ENOMEM;
3163
Johan Hedbergd8457692012-02-17 14:24:57 +02003164 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003165 sizeof(cp->addr.bdaddr), &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003166 if (err < 0)
3167 mgmt_pending_remove(cmd);
3168
3169 return err;
3170}
3171
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003172static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003173 u16 len)
Johan Hedberg980e1a52011-01-22 06:10:07 +02003174{
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003175 struct hci_conn *conn;
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003176 struct mgmt_cp_pin_code_reply *cp = data;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003177 struct hci_cp_pin_code_reply reply;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003178 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003179 int err;
3180
3181 BT_DBG("");
3182
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003183 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003184
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003185 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003186 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3187 MGMT_STATUS_NOT_POWERED);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003188 goto failed;
3189 }
3190
Johan Hedbergd8457692012-02-17 14:24:57 +02003191 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003192 if (!conn) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003193 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3194 MGMT_STATUS_NOT_CONNECTED);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003195 goto failed;
3196 }
3197
3198 if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) {
Johan Hedbergd8457692012-02-17 14:24:57 +02003199 struct mgmt_cp_pin_code_neg_reply ncp;
3200
3201 memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003202
3203 BT_ERR("PIN code is not 16 bytes long");
3204
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003205 err = send_pin_code_neg_reply(sk, hdev, &ncp);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003206 if (err >= 0)
Johan Hedberga69e8372015-03-06 21:08:53 +02003207 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3208 MGMT_STATUS_INVALID_PARAMS);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003209
3210 goto failed;
3211 }
3212
Gustavo F. Padovan00abfe42012-03-01 00:37:10 -03003213 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03003214 if (!cmd) {
3215 err = -ENOMEM;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003216 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03003217 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02003218
Johan Hedberg7776d1d2014-12-05 13:36:03 +02003219 cmd->cmd_complete = addr_cmd_complete;
3220
Johan Hedbergd8457692012-02-17 14:24:57 +02003221 bacpy(&reply.bdaddr, &cp->addr.bdaddr);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003222 reply.pin_len = cp->pin_len;
Waldemar Rymarkiewicz24718ca2011-06-01 17:28:47 +02003223 memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code));
Johan Hedberg980e1a52011-01-22 06:10:07 +02003224
3225 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
3226 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03003227 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003228
3229failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003230 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003231 return err;
3232}
3233
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003234static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
3235 u16 len)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003236{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003237 struct mgmt_cp_set_io_capability *cp = data;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003238
3239 BT_DBG("");
3240
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003241 if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003242 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY,
3243 MGMT_STATUS_INVALID_PARAMS, NULL, 0);
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003244
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003245 hci_dev_lock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003246
3247 hdev->io_capability = cp->io_capability;
3248
3249 BT_DBG("%s IO capability set to 0x%02x", hdev->name,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003250 hdev->io_capability);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003251
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003252 hci_dev_unlock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003253
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003254 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0,
3255 NULL, 0);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003256}
3257
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003258static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003259{
3260 struct hci_dev *hdev = conn->hdev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003261 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003262
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003263 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
Johan Hedberge9a416b2011-02-19 12:05:56 -03003264 if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
3265 continue;
3266
Johan Hedberge9a416b2011-02-19 12:05:56 -03003267 if (cmd->user_data != conn)
3268 continue;
3269
3270 return cmd;
3271 }
3272
3273 return NULL;
3274}
3275
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003276static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003277{
3278 struct mgmt_rp_pair_device rp;
3279 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9df74652014-12-19 22:26:03 +02003280 int err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003281
Johan Hedberg61b1a7f2014-03-20 12:54:16 +02003282 bacpy(&rp.addr.bdaddr, &conn->dst);
3283 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003284
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003285 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE,
3286 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003287
3288 /* So we don't get further callbacks for this connection */
3289 conn->connect_cfm_cb = NULL;
3290 conn->security_cfm_cb = NULL;
3291 conn->disconn_cfm_cb = NULL;
3292
David Herrmann76a68ba2013-04-06 20:28:37 +02003293 hci_conn_drop(conn);
Alfonso Acosta89cbb062014-10-11 21:44:47 +00003294
3295 /* The device is paired so there is no need to remove
3296 * its connection parameters anymore.
3297 */
3298 clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
Johan Hedberg15013ae2014-12-11 21:45:44 +02003299
3300 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02003301
3302 return err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003303}
3304
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003305void mgmt_smp_complete(struct hci_conn *conn, bool complete)
3306{
3307 u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003308 struct mgmt_pending_cmd *cmd;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003309
3310 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003311 if (cmd) {
Johan Hedberg04ab2742014-12-05 13:36:04 +02003312 cmd->cmd_complete(cmd, status);
Johan Hedberga511b352014-12-11 21:45:45 +02003313 mgmt_pending_remove(cmd);
3314 }
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003315}
3316
Johan Hedberge9a416b2011-02-19 12:05:56 -03003317static void pairing_complete_cb(struct hci_conn *conn, u8 status)
3318{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003319 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003320
3321 BT_DBG("status %u", status);
3322
Johan Hedberg56e5cb82011-11-08 20:40:16 +02003323 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003324 if (!cmd) {
Johan Hedberg56e5cb82011-11-08 20:40:16 +02003325 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02003326 return;
3327 }
3328
3329 cmd->cmd_complete(cmd, mgmt_status(status));
3330 mgmt_pending_remove(cmd);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003331}
3332
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003333static void le_pairing_complete_cb(struct hci_conn *conn, u8 status)
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303334{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003335 struct mgmt_pending_cmd *cmd;
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303336
3337 BT_DBG("status %u", status);
3338
3339 if (!status)
3340 return;
3341
3342 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003343 if (!cmd) {
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303344 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02003345 return;
3346 }
3347
3348 cmd->cmd_complete(cmd, mgmt_status(status));
3349 mgmt_pending_remove(cmd);
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303350}
3351
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003352static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003353 u16 len)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003354{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003355 struct mgmt_cp_pair_device *cp = data;
Johan Hedberg1425acb2011-11-11 00:07:35 +02003356 struct mgmt_rp_pair_device rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003357 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003358 u8 sec_level, auth_type;
3359 struct hci_conn *conn;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003360 int err;
3361
3362 BT_DBG("");
3363
Szymon Jancf950a30e2013-01-18 12:48:07 +01003364 memset(&rp, 0, sizeof(rp));
3365 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3366 rp.addr.type = cp->addr.type;
3367
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003368 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003369 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3370 MGMT_STATUS_INVALID_PARAMS,
3371 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003372
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003373 if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003374 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3375 MGMT_STATUS_INVALID_PARAMS,
3376 &rp, sizeof(rp));
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003377
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003378 hci_dev_lock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003379
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003380 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003381 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3382 MGMT_STATUS_NOT_POWERED, &rp,
3383 sizeof(rp));
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003384 goto unlock;
3385 }
3386
Johan Hedberg55e76b32015-03-10 22:34:40 +02003387 if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) {
3388 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3389 MGMT_STATUS_ALREADY_PAIRED, &rp,
3390 sizeof(rp));
3391 goto unlock;
3392 }
3393
Vinicius Costa Gomesc908df32011-09-02 14:51:22 -03003394 sec_level = BT_SECURITY_MEDIUM;
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003395 auth_type = HCI_AT_DEDICATED_BONDING;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003396
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003397 if (cp->addr.type == BDADDR_BREDR) {
Andre Guedes04a6c582014-02-26 20:21:44 -03003398 conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
3399 auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003400 } else {
3401 u8 addr_type;
3402
3403 /* Convert from L2CAP channel address type to HCI address type
3404 */
3405 if (cp->addr.type == BDADDR_LE_PUBLIC)
3406 addr_type = ADDR_LE_DEV_PUBLIC;
3407 else
3408 addr_type = ADDR_LE_DEV_RANDOM;
3409
Marcel Holtmann7c264b12014-06-30 12:34:40 +02003410 /* When pairing a new device, it is expected to remember
3411 * this device for future connections. Adding the connection
3412 * parameter information ahead of time allows tracking
3413 * of the slave preferred values and will speed up any
3414 * further connection establishment.
3415 *
3416 * If connection parameters already exist, then they
3417 * will be kept and this function does nothing.
3418 */
3419 hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type);
3420
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003421 conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type,
Johan Hedberge804d252014-07-16 11:42:28 +03003422 sec_level, HCI_LE_CONN_TIMEOUT,
3423 HCI_ROLE_MASTER);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003424 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003425
Ville Tervo30e76272011-02-22 16:10:53 -03003426 if (IS_ERR(conn)) {
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02003427 int status;
3428
3429 if (PTR_ERR(conn) == -EBUSY)
3430 status = MGMT_STATUS_BUSY;
Lukasz Rymanowskifaa81032015-02-11 12:31:42 +01003431 else if (PTR_ERR(conn) == -EOPNOTSUPP)
3432 status = MGMT_STATUS_NOT_SUPPORTED;
3433 else if (PTR_ERR(conn) == -ECONNREFUSED)
3434 status = MGMT_STATUS_REJECTED;
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02003435 else
3436 status = MGMT_STATUS_CONNECT_FAILED;
3437
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003438 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3439 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003440 goto unlock;
3441 }
3442
3443 if (conn->connect_cfm_cb) {
David Herrmann76a68ba2013-04-06 20:28:37 +02003444 hci_conn_drop(conn);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003445 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3446 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003447 goto unlock;
3448 }
3449
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003450 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003451 if (!cmd) {
3452 err = -ENOMEM;
David Herrmann76a68ba2013-04-06 20:28:37 +02003453 hci_conn_drop(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003454 goto unlock;
3455 }
3456
Johan Hedberg04ab2742014-12-05 13:36:04 +02003457 cmd->cmd_complete = pairing_complete;
3458
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003459 /* For LE, just connecting isn't a proof that the pairing finished */
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003460 if (cp->addr.type == BDADDR_BREDR) {
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003461 conn->connect_cfm_cb = pairing_complete_cb;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003462 conn->security_cfm_cb = pairing_complete_cb;
3463 conn->disconn_cfm_cb = pairing_complete_cb;
3464 } else {
3465 conn->connect_cfm_cb = le_pairing_complete_cb;
3466 conn->security_cfm_cb = le_pairing_complete_cb;
3467 conn->disconn_cfm_cb = le_pairing_complete_cb;
3468 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003469
Johan Hedberge9a416b2011-02-19 12:05:56 -03003470 conn->io_capability = cp->io_cap;
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03003471 cmd->user_data = hci_conn_get(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003472
Johan Hedberg6f78fd42014-07-30 08:35:48 +03003473 if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) &&
Johan Hedberga511b352014-12-11 21:45:45 +02003474 hci_conn_security(conn, sec_level, auth_type, true)) {
3475 cmd->cmd_complete(cmd, 0);
3476 mgmt_pending_remove(cmd);
3477 }
Johan Hedberge9a416b2011-02-19 12:05:56 -03003478
3479 err = 0;
3480
3481unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003482 hci_dev_unlock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003483 return err;
3484}
3485
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003486static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3487 u16 len)
Johan Hedberg28424702012-02-02 04:02:29 +02003488{
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003489 struct mgmt_addr_info *addr = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003490 struct mgmt_pending_cmd *cmd;
Johan Hedberg28424702012-02-02 04:02:29 +02003491 struct hci_conn *conn;
3492 int err;
3493
3494 BT_DBG("");
3495
Johan Hedberg28424702012-02-02 04:02:29 +02003496 hci_dev_lock(hdev);
3497
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003498 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003499 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3500 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003501 goto unlock;
3502 }
3503
Johan Hedberg333ae952015-03-17 13:48:47 +02003504 cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02003505 if (!cmd) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003506 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3507 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003508 goto unlock;
3509 }
3510
3511 conn = cmd->user_data;
3512
3513 if (bacmp(&addr->bdaddr, &conn->dst) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003514 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3515 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003516 goto unlock;
3517 }
3518
Johan Hedberga511b352014-12-11 21:45:45 +02003519 cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED);
3520 mgmt_pending_remove(cmd);
Johan Hedberg28424702012-02-02 04:02:29 +02003521
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003522 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
3523 addr, sizeof(*addr));
Johan Hedberg28424702012-02-02 04:02:29 +02003524unlock:
3525 hci_dev_unlock(hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02003526 return err;
3527}
3528
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003529static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
Johan Hedberg1707c602013-03-15 17:07:15 -05003530 struct mgmt_addr_info *addr, u16 mgmt_op,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003531 u16 hci_op, __le32 passkey)
Johan Hedberga5c29682011-02-19 12:05:57 -03003532{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003533 struct mgmt_pending_cmd *cmd;
Brian Gix0df4c182011-11-16 13:53:13 -08003534 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03003535 int err;
3536
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003537 hci_dev_lock(hdev);
Johan Hedberg08ba5382011-03-16 14:29:34 +02003538
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003539 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003540 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3541 MGMT_STATUS_NOT_POWERED, addr,
3542 sizeof(*addr));
Brian Gix0df4c182011-11-16 13:53:13 -08003543 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003544 }
3545
Johan Hedberg1707c602013-03-15 17:07:15 -05003546 if (addr->type == BDADDR_BREDR)
3547 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr);
Johan Hedberg272d90d2012-02-09 15:26:12 +02003548 else
Johan Hedberg1707c602013-03-15 17:07:15 -05003549 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr);
Brian Gix47c15e22011-11-16 13:53:14 -08003550
Johan Hedberg272d90d2012-02-09 15:26:12 +02003551 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003552 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3553 MGMT_STATUS_NOT_CONNECTED, addr,
3554 sizeof(*addr));
Johan Hedberg272d90d2012-02-09 15:26:12 +02003555 goto done;
3556 }
3557
Johan Hedberg1707c602013-03-15 17:07:15 -05003558 if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
Brian Gix5fe57d92011-12-21 16:12:13 -08003559 err = smp_user_confirm_reply(conn, mgmt_op, passkey);
Brian Gix5fe57d92011-12-21 16:12:13 -08003560 if (!err)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003561 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3562 MGMT_STATUS_SUCCESS, addr,
3563 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003564 else
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003565 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3566 MGMT_STATUS_FAILED, addr,
3567 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003568
Brian Gix47c15e22011-11-16 13:53:14 -08003569 goto done;
3570 }
3571
Johan Hedberg1707c602013-03-15 17:07:15 -05003572 cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr));
Johan Hedberga5c29682011-02-19 12:05:57 -03003573 if (!cmd) {
3574 err = -ENOMEM;
Brian Gix0df4c182011-11-16 13:53:13 -08003575 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003576 }
3577
Johan Hedberg7776d1d2014-12-05 13:36:03 +02003578 cmd->cmd_complete = addr_cmd_complete;
3579
Brian Gix0df4c182011-11-16 13:53:13 -08003580 /* Continue with pairing via HCI */
Brian Gix604086b2011-11-23 08:28:33 -08003581 if (hci_op == HCI_OP_USER_PASSKEY_REPLY) {
3582 struct hci_cp_user_passkey_reply cp;
3583
Johan Hedberg1707c602013-03-15 17:07:15 -05003584 bacpy(&cp.bdaddr, &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003585 cp.passkey = passkey;
3586 err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
3587 } else
Johan Hedberg1707c602013-03-15 17:07:15 -05003588 err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr),
3589 &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003590
Johan Hedberga664b5b2011-02-19 12:06:02 -03003591 if (err < 0)
3592 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03003593
Brian Gix0df4c182011-11-16 13:53:13 -08003594done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003595 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03003596 return err;
3597}
3598
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303599static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
3600 void *data, u16 len)
3601{
3602 struct mgmt_cp_pin_code_neg_reply *cp = data;
3603
3604 BT_DBG("");
3605
Johan Hedberg1707c602013-03-15 17:07:15 -05003606 return user_pairing_resp(sk, hdev, &cp->addr,
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303607 MGMT_OP_PIN_CODE_NEG_REPLY,
3608 HCI_OP_PIN_CODE_NEG_REPLY, 0);
3609}
3610
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003611static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3612 u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003613{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003614 struct mgmt_cp_user_confirm_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003615
3616 BT_DBG("");
3617
3618 if (len != sizeof(*cp))
Johan Hedberga69e8372015-03-06 21:08:53 +02003619 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
3620 MGMT_STATUS_INVALID_PARAMS);
Brian Gix0df4c182011-11-16 13:53:13 -08003621
Johan Hedberg1707c602013-03-15 17:07:15 -05003622 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003623 MGMT_OP_USER_CONFIRM_REPLY,
3624 HCI_OP_USER_CONFIRM_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003625}
3626
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003627static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003628 void *data, u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003629{
Johan Hedbergc9c26592011-12-15 00:47:41 +02003630 struct mgmt_cp_user_confirm_neg_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003631
3632 BT_DBG("");
3633
Johan Hedberg1707c602013-03-15 17:07:15 -05003634 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003635 MGMT_OP_USER_CONFIRM_NEG_REPLY,
3636 HCI_OP_USER_CONFIRM_NEG_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003637}
3638
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003639static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3640 u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003641{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003642 struct mgmt_cp_user_passkey_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003643
3644 BT_DBG("");
3645
Johan Hedberg1707c602013-03-15 17:07:15 -05003646 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003647 MGMT_OP_USER_PASSKEY_REPLY,
3648 HCI_OP_USER_PASSKEY_REPLY, cp->passkey);
Brian Gix604086b2011-11-23 08:28:33 -08003649}
3650
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003651static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003652 void *data, u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003653{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003654 struct mgmt_cp_user_passkey_neg_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003655
3656 BT_DBG("");
3657
Johan Hedberg1707c602013-03-15 17:07:15 -05003658 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003659 MGMT_OP_USER_PASSKEY_NEG_REPLY,
3660 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
Brian Gix604086b2011-11-23 08:28:33 -08003661}
3662
Johan Hedberg13928972013-03-15 17:07:00 -05003663static void update_name(struct hci_request *req)
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003664{
Johan Hedberg13928972013-03-15 17:07:00 -05003665 struct hci_dev *hdev = req->hdev;
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003666 struct hci_cp_write_local_name cp;
3667
Johan Hedberg13928972013-03-15 17:07:00 -05003668 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003669
Johan Hedberg890ea892013-03-15 17:06:52 -05003670 hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp);
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003671}
3672
Marcel Holtmann1904a852015-01-11 13:50:44 -08003673static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg13928972013-03-15 17:07:00 -05003674{
3675 struct mgmt_cp_set_local_name *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003676 struct mgmt_pending_cmd *cmd;
Johan Hedberg13928972013-03-15 17:07:00 -05003677
3678 BT_DBG("status 0x%02x", status);
3679
3680 hci_dev_lock(hdev);
3681
Johan Hedberg333ae952015-03-17 13:48:47 +02003682 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05003683 if (!cmd)
3684 goto unlock;
3685
3686 cp = cmd->param;
3687
3688 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02003689 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
3690 mgmt_status(status));
Johan Hedberg13928972013-03-15 17:07:00 -05003691 else
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003692 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3693 cp, sizeof(*cp));
Johan Hedberg13928972013-03-15 17:07:00 -05003694
3695 mgmt_pending_remove(cmd);
3696
3697unlock:
3698 hci_dev_unlock(hdev);
3699}
3700
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003701static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003702 u16 len)
Johan Hedbergb312b1612011-03-16 14:29:37 +02003703{
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003704 struct mgmt_cp_set_local_name *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003705 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05003706 struct hci_request req;
Johan Hedbergb312b1612011-03-16 14:29:37 +02003707 int err;
3708
3709 BT_DBG("");
3710
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003711 hci_dev_lock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003712
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003713 /* If the old values are the same as the new ones just return a
3714 * direct command complete event.
3715 */
3716 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
3717 !memcmp(hdev->short_name, cp->short_name,
3718 sizeof(hdev->short_name))) {
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 Hedbergb3f2ca92013-03-15 17:07:03 -05003721 goto failed;
3722 }
3723
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003724 memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003725
Johan Hedbergb5235a62012-02-21 14:32:24 +02003726 if (!hdev_is_powered(hdev)) {
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003727 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003728
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003729 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3730 data, len);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003731 if (err < 0)
3732 goto failed;
3733
Marcel Holtmannf6b77122015-03-14 19:28:05 -07003734 err = mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev,
3735 data, len, sk);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003736
Johan Hedbergb5235a62012-02-21 14:32:24 +02003737 goto failed;
3738 }
3739
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003740 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003741 if (!cmd) {
3742 err = -ENOMEM;
3743 goto failed;
3744 }
3745
Johan Hedberg13928972013-03-15 17:07:00 -05003746 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3747
Johan Hedberg890ea892013-03-15 17:06:52 -05003748 hci_req_init(&req, hdev);
Johan Hedberg3f985052013-03-15 17:07:02 -05003749
3750 if (lmp_bredr_capable(hdev)) {
3751 update_name(&req);
3752 update_eir(&req);
3753 }
3754
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003755 /* The name is stored in the scan response data and so
3756 * no need to udpate the advertising data here.
3757 */
Johan Hedberg3f985052013-03-15 17:07:02 -05003758 if (lmp_le_capable(hdev))
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003759 update_scan_rsp_data(&req);
Johan Hedberg3f985052013-03-15 17:07:02 -05003760
Johan Hedberg13928972013-03-15 17:07:00 -05003761 err = hci_req_run(&req, set_name_complete);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003762 if (err < 0)
3763 mgmt_pending_remove(cmd);
3764
3765failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003766 hci_dev_unlock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003767 return err;
3768}
3769
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003770static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status,
3771 u16 opcode, struct sk_buff *skb)
3772{
3773 struct mgmt_rp_read_local_oob_data mgmt_rp;
3774 size_t rp_size = sizeof(mgmt_rp);
3775 struct mgmt_pending_cmd *cmd;
3776
3777 BT_DBG("%s status %u", hdev->name, status);
3778
3779 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
3780 if (!cmd)
3781 return;
3782
3783 if (status || !skb) {
3784 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3785 status ? mgmt_status(status) : MGMT_STATUS_FAILED);
3786 goto remove;
3787 }
3788
3789 memset(&mgmt_rp, 0, sizeof(mgmt_rp));
3790
3791 if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
3792 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
3793
3794 if (skb->len < sizeof(*rp)) {
3795 mgmt_cmd_status(cmd->sk, hdev->id,
3796 MGMT_OP_READ_LOCAL_OOB_DATA,
3797 MGMT_STATUS_FAILED);
3798 goto remove;
3799 }
3800
3801 memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash));
3802 memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand));
3803
3804 rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256);
3805 } else {
3806 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
3807
3808 if (skb->len < sizeof(*rp)) {
3809 mgmt_cmd_status(cmd->sk, hdev->id,
3810 MGMT_OP_READ_LOCAL_OOB_DATA,
3811 MGMT_STATUS_FAILED);
3812 goto remove;
3813 }
3814
3815 memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192));
3816 memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192));
3817
3818 memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256));
3819 memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256));
3820 }
3821
3822 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3823 MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size);
3824
3825remove:
3826 mgmt_pending_remove(cmd);
3827}
3828
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003829static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003830 void *data, u16 data_len)
Szymon Jancc35938b2011-03-22 13:12:21 +01003831{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003832 struct mgmt_pending_cmd *cmd;
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003833 struct hci_request req;
Szymon Jancc35938b2011-03-22 13:12:21 +01003834 int err;
3835
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003836 BT_DBG("%s", hdev->name);
Szymon Jancc35938b2011-03-22 13:12:21 +01003837
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003838 hci_dev_lock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003839
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003840 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003841 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3842 MGMT_STATUS_NOT_POWERED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003843 goto unlock;
3844 }
3845
Andre Guedes9a1a1992012-07-24 15:03:48 -03003846 if (!lmp_ssp_capable(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003847 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3848 MGMT_STATUS_NOT_SUPPORTED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003849 goto unlock;
3850 }
3851
Johan Hedberg333ae952015-03-17 13:48:47 +02003852 if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003853 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3854 MGMT_STATUS_BUSY);
Szymon Jancc35938b2011-03-22 13:12:21 +01003855 goto unlock;
3856 }
3857
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003858 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
Szymon Jancc35938b2011-03-22 13:12:21 +01003859 if (!cmd) {
3860 err = -ENOMEM;
3861 goto unlock;
3862 }
3863
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003864 hci_req_init(&req, hdev);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08003865
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003866 if (bredr_sc_enabled(hdev))
3867 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
3868 else
3869 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
3870
3871 err = hci_req_run_skb(&req, read_local_oob_data_complete);
Szymon Jancc35938b2011-03-22 13:12:21 +01003872 if (err < 0)
3873 mgmt_pending_remove(cmd);
3874
3875unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003876 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003877 return err;
3878}
3879
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003880static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003881 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003882{
Johan Hedberg5d57e792015-01-23 10:10:38 +02003883 struct mgmt_addr_info *addr = data;
Szymon Janc2763eda2011-03-22 13:12:22 +01003884 int err;
3885
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003886 BT_DBG("%s ", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003887
Johan Hedberg5d57e792015-01-23 10:10:38 +02003888 if (!bdaddr_type_is_valid(addr->type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003889 return mgmt_cmd_complete(sk, hdev->id,
3890 MGMT_OP_ADD_REMOTE_OOB_DATA,
3891 MGMT_STATUS_INVALID_PARAMS,
3892 addr, sizeof(*addr));
Johan Hedberg5d57e792015-01-23 10:10:38 +02003893
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003894 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003895
Marcel Holtmannec109112014-01-10 02:07:30 -08003896 if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) {
3897 struct mgmt_cp_add_remote_oob_data *cp = data;
3898 u8 status;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003899
Johan Hedbergc19a4952014-11-17 20:52:19 +02003900 if (cp->addr.type != BDADDR_BREDR) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003901 err = mgmt_cmd_complete(sk, hdev->id,
3902 MGMT_OP_ADD_REMOTE_OOB_DATA,
3903 MGMT_STATUS_INVALID_PARAMS,
3904 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02003905 goto unlock;
3906 }
3907
Marcel Holtmannec109112014-01-10 02:07:30 -08003908 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg6928a922014-10-26 20:46:09 +01003909 cp->addr.type, cp->hash,
3910 cp->rand, NULL, NULL);
Marcel Holtmannec109112014-01-10 02:07:30 -08003911 if (err < 0)
3912 status = MGMT_STATUS_FAILED;
3913 else
3914 status = MGMT_STATUS_SUCCESS;
3915
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003916 err = mgmt_cmd_complete(sk, hdev->id,
3917 MGMT_OP_ADD_REMOTE_OOB_DATA, status,
3918 &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08003919 } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) {
3920 struct mgmt_cp_add_remote_oob_ext_data *cp = data;
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003921 u8 *rand192, *hash192, *rand256, *hash256;
Marcel Holtmannec109112014-01-10 02:07:30 -08003922 u8 status;
3923
Johan Hedberg86df9202014-10-26 20:52:27 +01003924 if (bdaddr_type_is_le(cp->addr.type)) {
Johan Hedbergd25b78e2015-01-27 12:55:52 +02003925 /* Enforce zero-valued 192-bit parameters as
3926 * long as legacy SMP OOB isn't implemented.
3927 */
3928 if (memcmp(cp->rand192, ZERO_KEY, 16) ||
3929 memcmp(cp->hash192, ZERO_KEY, 16)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003930 err = mgmt_cmd_complete(sk, hdev->id,
3931 MGMT_OP_ADD_REMOTE_OOB_DATA,
3932 MGMT_STATUS_INVALID_PARAMS,
3933 addr, sizeof(*addr));
Johan Hedbergd25b78e2015-01-27 12:55:52 +02003934 goto unlock;
3935 }
3936
Johan Hedberg86df9202014-10-26 20:52:27 +01003937 rand192 = NULL;
3938 hash192 = NULL;
3939 } else {
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003940 /* In case one of the P-192 values is set to zero,
3941 * then just disable OOB data for P-192.
3942 */
3943 if (!memcmp(cp->rand192, ZERO_KEY, 16) ||
3944 !memcmp(cp->hash192, ZERO_KEY, 16)) {
3945 rand192 = NULL;
3946 hash192 = NULL;
3947 } else {
3948 rand192 = cp->rand192;
3949 hash192 = cp->hash192;
3950 }
3951 }
3952
3953 /* In case one of the P-256 values is set to zero, then just
3954 * disable OOB data for P-256.
3955 */
3956 if (!memcmp(cp->rand256, ZERO_KEY, 16) ||
3957 !memcmp(cp->hash256, ZERO_KEY, 16)) {
3958 rand256 = NULL;
3959 hash256 = NULL;
3960 } else {
3961 rand256 = cp->rand256;
3962 hash256 = cp->hash256;
Johan Hedberg86df9202014-10-26 20:52:27 +01003963 }
3964
Johan Hedberg81328d52014-10-26 20:33:47 +01003965 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg86df9202014-10-26 20:52:27 +01003966 cp->addr.type, hash192, rand192,
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003967 hash256, rand256);
Marcel Holtmannec109112014-01-10 02:07:30 -08003968 if (err < 0)
3969 status = MGMT_STATUS_FAILED;
3970 else
3971 status = MGMT_STATUS_SUCCESS;
3972
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003973 err = mgmt_cmd_complete(sk, hdev->id,
3974 MGMT_OP_ADD_REMOTE_OOB_DATA,
3975 status, &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08003976 } else {
3977 BT_ERR("add_remote_oob_data: invalid length of %u bytes", len);
Johan Hedberga69e8372015-03-06 21:08:53 +02003978 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3979 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannec109112014-01-10 02:07:30 -08003980 }
Szymon Janc2763eda2011-03-22 13:12:22 +01003981
Johan Hedbergc19a4952014-11-17 20:52:19 +02003982unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003983 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003984 return err;
3985}
3986
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003987static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003988 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003989{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003990 struct mgmt_cp_remove_remote_oob_data *cp = data;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003991 u8 status;
Szymon Janc2763eda2011-03-22 13:12:22 +01003992 int err;
3993
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003994 BT_DBG("%s", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003995
Johan Hedbergc19a4952014-11-17 20:52:19 +02003996 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003997 return mgmt_cmd_complete(sk, hdev->id,
3998 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
3999 MGMT_STATUS_INVALID_PARAMS,
4000 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02004001
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004002 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01004003
Johan Hedbergeedbd582014-11-15 09:34:23 +02004004 if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
4005 hci_remote_oob_data_clear(hdev);
4006 status = MGMT_STATUS_SUCCESS;
4007 goto done;
4008 }
4009
Johan Hedberg6928a922014-10-26 20:46:09 +01004010 err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type);
Szymon Janc2763eda2011-03-22 13:12:22 +01004011 if (err < 0)
Johan Hedbergbf1e3542012-02-19 13:16:14 +02004012 status = MGMT_STATUS_INVALID_PARAMS;
Szymon Janc2763eda2011-03-22 13:12:22 +01004013 else
Szymon Janca6785be2012-12-13 15:11:21 +01004014 status = MGMT_STATUS_SUCCESS;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02004015
Johan Hedbergeedbd582014-11-15 09:34:23 +02004016done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004017 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
4018 status, &cp->addr, sizeof(cp->addr));
Szymon Janc2763eda2011-03-22 13:12:22 +01004019
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004020 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01004021 return err;
4022}
4023
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004024static bool trigger_bredr_inquiry(struct hci_request *req, u8 *status)
4025{
4026 struct hci_dev *hdev = req->hdev;
4027 struct hci_cp_inquiry cp;
4028 /* General inquiry access code (GIAC) */
4029 u8 lap[3] = { 0x33, 0x8b, 0x9e };
4030
4031 *status = mgmt_bredr_support(hdev);
4032 if (*status)
4033 return false;
4034
4035 if (hci_dev_test_flag(hdev, HCI_INQUIRY)) {
4036 *status = MGMT_STATUS_BUSY;
4037 return false;
4038 }
4039
4040 hci_inquiry_cache_flush(hdev);
4041
4042 memset(&cp, 0, sizeof(cp));
4043 memcpy(&cp.lap, lap, sizeof(cp.lap));
4044 cp.length = DISCOV_BREDR_INQUIRY_LEN;
4045
4046 hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp);
4047
4048 return true;
4049}
4050
4051static bool trigger_le_scan(struct hci_request *req, u16 interval, u8 *status)
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004052{
Marcel Holtmann80190442014-12-04 11:36:36 +01004053 struct hci_dev *hdev = req->hdev;
4054 struct hci_cp_le_set_scan_param param_cp;
4055 struct hci_cp_le_set_scan_enable enable_cp;
Marcel Holtmann80190442014-12-04 11:36:36 +01004056 u8 own_addr_type;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004057 int err;
4058
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004059 *status = mgmt_le_support(hdev);
4060 if (*status)
4061 return false;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004062
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004063 if (hci_dev_test_flag(hdev, HCI_LE_ADV)) {
4064 /* Don't let discovery abort an outgoing connection attempt
4065 * that's using directed advertising.
4066 */
4067 if (hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT)) {
4068 *status = MGMT_STATUS_REJECTED;
Marcel Holtmann80190442014-12-04 11:36:36 +01004069 return false;
4070 }
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004071
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004072 disable_advertising(req);
4073 }
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004074
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004075 /* If controller is scanning, it means the background scanning is
4076 * running. Thus, we should temporarily stop it in order to set the
4077 * discovery scanning parameters.
4078 */
4079 if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
4080 hci_req_add_le_scan_disable(req);
4081
4082 /* All active scans will be done with either a resolvable private
4083 * address (when privacy feature has been enabled) or non-resolvable
4084 * private address.
4085 */
4086 err = hci_update_random_address(req, true, &own_addr_type);
4087 if (err < 0) {
4088 *status = MGMT_STATUS_FAILED;
4089 return false;
4090 }
4091
4092 memset(&param_cp, 0, sizeof(param_cp));
4093 param_cp.type = LE_SCAN_ACTIVE;
4094 param_cp.interval = cpu_to_le16(interval);
4095 param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN);
4096 param_cp.own_address_type = own_addr_type;
4097
4098 hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
4099 &param_cp);
4100
4101 memset(&enable_cp, 0, sizeof(enable_cp));
4102 enable_cp.enable = LE_SCAN_ENABLE;
4103 enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
4104
4105 hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
4106 &enable_cp);
4107
4108 return true;
4109}
4110
4111static bool trigger_discovery(struct hci_request *req, u8 *status)
4112{
4113 struct hci_dev *hdev = req->hdev;
4114
4115 switch (hdev->discovery.type) {
4116 case DISCOV_TYPE_BREDR:
4117 if (!trigger_bredr_inquiry(req, status))
4118 return false;
Marcel Holtmann80190442014-12-04 11:36:36 +01004119 break;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004120
Marcel Holtmann80190442014-12-04 11:36:36 +01004121 case DISCOV_TYPE_INTERLEAVED:
Jakub Pawlowski07d23342015-03-17 09:04:14 -07004122 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
4123 &hdev->quirks)) {
4124 /* During simultaneous discovery, we double LE scan
4125 * interval. We must leave some time for the controller
4126 * to do BR/EDR inquiry.
4127 */
4128 if (!trigger_le_scan(req, DISCOV_LE_SCAN_INT * 2,
4129 status))
4130 return false;
4131
4132 if (!trigger_bredr_inquiry(req, status))
4133 return false;
4134
4135 return true;
4136 }
4137
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004138 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Marcel Holtmann80190442014-12-04 11:36:36 +01004139 *status = MGMT_STATUS_NOT_SUPPORTED;
4140 return false;
4141 }
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004142 /* fall through */
Marcel Holtmann80190442014-12-04 11:36:36 +01004143
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004144 case DISCOV_TYPE_LE:
4145 if (!trigger_le_scan(req, DISCOV_LE_SCAN_INT, status))
Marcel Holtmann80190442014-12-04 11:36:36 +01004146 return false;
Marcel Holtmann80190442014-12-04 11:36:36 +01004147 break;
4148
4149 default:
4150 *status = MGMT_STATUS_INVALID_PARAMS;
4151 return false;
4152 }
4153
4154 return true;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004155}
4156
Marcel Holtmann1904a852015-01-11 13:50:44 -08004157static void start_discovery_complete(struct hci_dev *hdev, u8 status,
4158 u16 opcode)
Andre Guedes7c307722013-04-30 15:29:28 -03004159{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004160 struct mgmt_pending_cmd *cmd;
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004161 unsigned long timeout;
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01004162
Andre Guedes7c307722013-04-30 15:29:28 -03004163 BT_DBG("status %d", status);
4164
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004165 hci_dev_lock(hdev);
4166
Johan Hedberg333ae952015-03-17 13:48:47 +02004167 cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004168 if (!cmd)
Johan Hedberg333ae952015-03-17 13:48:47 +02004169 cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004170
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004171 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02004172 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004173 mgmt_pending_remove(cmd);
Andre Guedes7c307722013-04-30 15:29:28 -03004174 }
4175
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004176 if (status) {
4177 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4178 goto unlock;
4179 }
4180
Andre Guedes7c307722013-04-30 15:29:28 -03004181 hci_discovery_set_state(hdev, DISCOVERY_FINDING);
Andre Guedes7c307722013-04-30 15:29:28 -03004182
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08004183 /* If the scan involves LE scan, pick proper timeout to schedule
4184 * hdev->le_scan_disable that will stop it.
4185 */
Andre Guedes7c307722013-04-30 15:29:28 -03004186 switch (hdev->discovery.type) {
4187 case DISCOV_TYPE_LE:
Lukasz Rymanowski3d5a76f2014-03-27 20:55:21 +01004188 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
Andre Guedes7c307722013-04-30 15:29:28 -03004189 break;
Andre Guedes7c307722013-04-30 15:29:28 -03004190 case DISCOV_TYPE_INTERLEAVED:
Jakub Pawlowski07d23342015-03-17 09:04:14 -07004191 /* When running simultaneous discovery, the LE scanning time
4192 * should occupy the whole discovery time sine BR/EDR inquiry
4193 * and LE scanning are scheduled by the controller.
4194 *
4195 * For interleaving discovery in comparison, BR/EDR inquiry
4196 * and LE scanning are done sequentially with separate
4197 * timeouts.
4198 */
4199 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
4200 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
4201 else
4202 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
Andre Guedes7c307722013-04-30 15:29:28 -03004203 break;
Andre Guedes7c307722013-04-30 15:29:28 -03004204 case DISCOV_TYPE_BREDR:
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004205 timeout = 0;
Andre Guedes7c307722013-04-30 15:29:28 -03004206 break;
Andre Guedes7c307722013-04-30 15:29:28 -03004207 default:
4208 BT_ERR("Invalid discovery type %d", hdev->discovery.type);
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004209 timeout = 0;
4210 break;
Andre Guedes7c307722013-04-30 15:29:28 -03004211 }
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01004212
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08004213 if (timeout) {
4214 /* When service discovery is used and the controller has
4215 * a strict duplicate filter, it is important to remember
4216 * the start and duration of the scan. This is required
4217 * for restarting scanning during the discovery phase.
4218 */
4219 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER,
4220 &hdev->quirks) &&
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08004221 hdev->discovery.result_filtering) {
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08004222 hdev->discovery.scan_start = jiffies;
4223 hdev->discovery.scan_duration = timeout;
4224 }
4225
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004226 queue_delayed_work(hdev->workqueue,
4227 &hdev->le_scan_disable, timeout);
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08004228 }
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01004229
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004230unlock:
4231 hci_dev_unlock(hdev);
Andre Guedes7c307722013-04-30 15:29:28 -03004232}
4233
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004234static int start_discovery(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004235 void *data, u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04004236{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004237 struct mgmt_cp_start_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004238 struct mgmt_pending_cmd *cmd;
Andre Guedes7c307722013-04-30 15:29:28 -03004239 struct hci_request req;
Marcel Holtmann80190442014-12-04 11:36:36 +01004240 u8 status;
Johan Hedberg14a53662011-04-27 10:29:56 -04004241 int err;
4242
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004243 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04004244
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004245 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004246
Johan Hedberg4b34ee782012-02-21 14:13:02 +02004247 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004248 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4249 MGMT_STATUS_NOT_POWERED,
4250 &cp->type, sizeof(cp->type));
Johan Hedbergbd2d1332011-11-07 23:13:37 +02004251 goto failed;
4252 }
4253
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004254 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004255 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004256 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4257 MGMT_STATUS_BUSY, &cp->type,
4258 sizeof(cp->type));
Andre Guedes642be6c2012-03-21 00:03:37 -03004259 goto failed;
4260 }
4261
Johan Hedberg2922a942014-12-05 13:36:06 +02004262 cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, data, len);
Johan Hedberg14a53662011-04-27 10:29:56 -04004263 if (!cmd) {
4264 err = -ENOMEM;
4265 goto failed;
4266 }
4267
Johan Hedberg2922a942014-12-05 13:36:06 +02004268 cmd->cmd_complete = generic_cmd_complete;
4269
Marcel Holtmann22078802014-12-05 11:45:22 +01004270 /* Clear the discovery filter first to free any previously
4271 * allocated memory for the UUID list.
4272 */
4273 hci_discovery_filter_clear(hdev);
4274
Andre Guedes4aab14e2012-02-17 20:39:36 -03004275 hdev->discovery.type = cp->type;
Marcel Holtmannda25cf62014-12-05 13:03:35 +01004276 hdev->discovery.report_invalid_rssi = false;
Andre Guedes4aab14e2012-02-17 20:39:36 -03004277
Andre Guedes7c307722013-04-30 15:29:28 -03004278 hci_req_init(&req, hdev);
4279
Marcel Holtmann80190442014-12-04 11:36:36 +01004280 if (!trigger_discovery(&req, &status)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004281 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4282 status, &cp->type, sizeof(cp->type));
Johan Hedberg04106752013-01-10 14:54:09 +02004283 mgmt_pending_remove(cmd);
4284 goto failed;
Andre Guedesf39799f2012-02-17 20:39:35 -03004285 }
Andre Guedes3fd24152012-02-03 17:48:01 -03004286
Andre Guedes7c307722013-04-30 15:29:28 -03004287 err = hci_req_run(&req, start_discovery_complete);
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004288 if (err < 0) {
Johan Hedberg14a53662011-04-27 10:29:56 -04004289 mgmt_pending_remove(cmd);
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004290 goto failed;
4291 }
4292
4293 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
Johan Hedberg14a53662011-04-27 10:29:56 -04004294
4295failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004296 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004297 return err;
4298}
4299
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004300static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd,
4301 u8 status)
Andre Guedes1183fdc2013-04-30 15:29:35 -03004302{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004303 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
4304 cmd->param, 1);
Johan Hedberg2922a942014-12-05 13:36:06 +02004305}
4306
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004307static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
4308 void *data, u16 len)
4309{
4310 struct mgmt_cp_start_service_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004311 struct mgmt_pending_cmd *cmd;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004312 struct hci_request req;
4313 const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16);
4314 u16 uuid_count, expected_len;
4315 u8 status;
Andre Guedes1183fdc2013-04-30 15:29:35 -03004316 int err;
4317
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004318 BT_DBG("%s", hdev->name);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004319
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004320 hci_dev_lock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004321
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004322 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004323 err = mgmt_cmd_complete(sk, hdev->id,
4324 MGMT_OP_START_SERVICE_DISCOVERY,
4325 MGMT_STATUS_NOT_POWERED,
4326 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004327 goto failed;
4328 }
4329
4330 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004331 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004332 err = mgmt_cmd_complete(sk, hdev->id,
4333 MGMT_OP_START_SERVICE_DISCOVERY,
4334 MGMT_STATUS_BUSY, &cp->type,
4335 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004336 goto failed;
4337 }
4338
4339 uuid_count = __le16_to_cpu(cp->uuid_count);
4340 if (uuid_count > max_uuid_count) {
4341 BT_ERR("service_discovery: too big uuid_count value %u",
4342 uuid_count);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004343 err = mgmt_cmd_complete(sk, hdev->id,
4344 MGMT_OP_START_SERVICE_DISCOVERY,
4345 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4346 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004347 goto failed;
4348 }
4349
4350 expected_len = sizeof(*cp) + uuid_count * 16;
4351 if (expected_len != len) {
4352 BT_ERR("service_discovery: expected %u bytes, got %u bytes",
4353 expected_len, len);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004354 err = mgmt_cmd_complete(sk, hdev->id,
4355 MGMT_OP_START_SERVICE_DISCOVERY,
4356 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4357 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004358 goto failed;
4359 }
4360
4361 cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY,
Johan Hedberg2922a942014-12-05 13:36:06 +02004362 hdev, data, len);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004363 if (!cmd) {
4364 err = -ENOMEM;
4365 goto failed;
4366 }
4367
Johan Hedberg2922a942014-12-05 13:36:06 +02004368 cmd->cmd_complete = service_discovery_cmd_complete;
4369
Marcel Holtmann22078802014-12-05 11:45:22 +01004370 /* Clear the discovery filter first to free any previously
4371 * allocated memory for the UUID list.
4372 */
4373 hci_discovery_filter_clear(hdev);
4374
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08004375 hdev->discovery.result_filtering = true;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004376 hdev->discovery.type = cp->type;
4377 hdev->discovery.rssi = cp->rssi;
4378 hdev->discovery.uuid_count = uuid_count;
4379
4380 if (uuid_count > 0) {
4381 hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16,
4382 GFP_KERNEL);
4383 if (!hdev->discovery.uuids) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004384 err = mgmt_cmd_complete(sk, hdev->id,
4385 MGMT_OP_START_SERVICE_DISCOVERY,
4386 MGMT_STATUS_FAILED,
4387 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004388 mgmt_pending_remove(cmd);
4389 goto failed;
4390 }
4391 }
4392
4393 hci_req_init(&req, hdev);
4394
4395 if (!trigger_discovery(&req, &status)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004396 err = mgmt_cmd_complete(sk, hdev->id,
4397 MGMT_OP_START_SERVICE_DISCOVERY,
4398 status, &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004399 mgmt_pending_remove(cmd);
4400 goto failed;
4401 }
4402
4403 err = hci_req_run(&req, start_discovery_complete);
4404 if (err < 0) {
4405 mgmt_pending_remove(cmd);
4406 goto failed;
4407 }
4408
4409 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
4410
4411failed:
4412 hci_dev_unlock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004413 return err;
4414}
4415
Marcel Holtmann1904a852015-01-11 13:50:44 -08004416static void stop_discovery_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Andre Guedes0e05bba2013-04-30 15:29:33 -03004417{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004418 struct mgmt_pending_cmd *cmd;
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004419
Andre Guedes0e05bba2013-04-30 15:29:33 -03004420 BT_DBG("status %d", status);
4421
4422 hci_dev_lock(hdev);
4423
Johan Hedberg333ae952015-03-17 13:48:47 +02004424 cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004425 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02004426 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004427 mgmt_pending_remove(cmd);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004428 }
4429
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004430 if (!status)
4431 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004432
Andre Guedes0e05bba2013-04-30 15:29:33 -03004433 hci_dev_unlock(hdev);
4434}
4435
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004436static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004437 u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04004438{
Johan Hedbergd9306502012-02-20 23:25:18 +02004439 struct mgmt_cp_stop_discovery *mgmt_cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004440 struct mgmt_pending_cmd *cmd;
Andre Guedes0e05bba2013-04-30 15:29:33 -03004441 struct hci_request req;
Johan Hedberg14a53662011-04-27 10:29:56 -04004442 int err;
4443
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004444 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04004445
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004446 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004447
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004448 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004449 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4450 MGMT_STATUS_REJECTED, &mgmt_cp->type,
4451 sizeof(mgmt_cp->type));
Johan Hedbergd9306502012-02-20 23:25:18 +02004452 goto unlock;
4453 }
4454
4455 if (hdev->discovery.type != mgmt_cp->type) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004456 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4457 MGMT_STATUS_INVALID_PARAMS,
4458 &mgmt_cp->type, sizeof(mgmt_cp->type));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004459 goto unlock;
Johan Hedbergff9ef572012-01-04 14:23:45 +02004460 }
4461
Johan Hedberg2922a942014-12-05 13:36:06 +02004462 cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len);
Johan Hedberg14a53662011-04-27 10:29:56 -04004463 if (!cmd) {
4464 err = -ENOMEM;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004465 goto unlock;
Johan Hedberg14a53662011-04-27 10:29:56 -04004466 }
4467
Johan Hedberg2922a942014-12-05 13:36:06 +02004468 cmd->cmd_complete = generic_cmd_complete;
4469
Andre Guedes0e05bba2013-04-30 15:29:33 -03004470 hci_req_init(&req, hdev);
4471
Johan Hedberg21a60d32014-06-10 14:05:58 +03004472 hci_stop_discovery(&req);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004473
Johan Hedberg21a60d32014-06-10 14:05:58 +03004474 err = hci_req_run(&req, stop_discovery_complete);
4475 if (!err) {
4476 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004477 goto unlock;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004478 }
4479
Johan Hedberg21a60d32014-06-10 14:05:58 +03004480 mgmt_pending_remove(cmd);
4481
4482 /* If no HCI commands were sent we're done */
4483 if (err == -ENODATA) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004484 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0,
4485 &mgmt_cp->type, sizeof(mgmt_cp->type));
Johan Hedberg21a60d32014-06-10 14:05:58 +03004486 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4487 }
Johan Hedberg14a53662011-04-27 10:29:56 -04004488
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004489unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004490 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004491 return err;
4492}
4493
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004494static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004495 u16 len)
Johan Hedberg561aafb2012-01-04 13:31:59 +02004496{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004497 struct mgmt_cp_confirm_name *cp = data;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004498 struct inquiry_entry *e;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004499 int err;
4500
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004501 BT_DBG("%s", hdev->name);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004502
Johan Hedberg561aafb2012-01-04 13:31:59 +02004503 hci_dev_lock(hdev);
4504
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004505 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004506 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4507 MGMT_STATUS_FAILED, &cp->addr,
4508 sizeof(cp->addr));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004509 goto failed;
4510 }
4511
Johan Hedberga198e7b2012-02-17 14:27:06 +02004512 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004513 if (!e) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004514 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4515 MGMT_STATUS_INVALID_PARAMS, &cp->addr,
4516 sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02004517 goto failed;
4518 }
4519
4520 if (cp->name_known) {
4521 e->name_state = NAME_KNOWN;
4522 list_del(&e->list);
4523 } else {
4524 e->name_state = NAME_NEEDED;
Johan Hedberga3d4e202012-01-09 00:53:02 +02004525 hci_inquiry_cache_update_resolve(hdev, e);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004526 }
4527
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004528 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0,
4529 &cp->addr, sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02004530
4531failed:
4532 hci_dev_unlock(hdev);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004533 return err;
4534}
4535
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004536static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004537 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03004538{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004539 struct mgmt_cp_block_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004540 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03004541 int err;
4542
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004543 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03004544
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004545 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004546 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
4547 MGMT_STATUS_INVALID_PARAMS,
4548 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004549
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004550 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03004551
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004552 err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr,
4553 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004554 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004555 status = MGMT_STATUS_FAILED;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004556 goto done;
4557 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004558
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004559 mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4560 sk);
4561 status = MGMT_STATUS_SUCCESS;
4562
4563done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004564 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
4565 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03004566
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004567 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03004568
4569 return err;
4570}
4571
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004572static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004573 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03004574{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004575 struct mgmt_cp_unblock_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004576 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03004577 int err;
4578
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004579 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03004580
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004581 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004582 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
4583 MGMT_STATUS_INVALID_PARAMS,
4584 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004585
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004586 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03004587
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004588 err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr,
4589 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004590 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004591 status = MGMT_STATUS_INVALID_PARAMS;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004592 goto done;
4593 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004594
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004595 mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4596 sk);
4597 status = MGMT_STATUS_SUCCESS;
4598
4599done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004600 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
4601 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03004602
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004603 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03004604
4605 return err;
4606}
4607
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004608static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
4609 u16 len)
4610{
4611 struct mgmt_cp_set_device_id *cp = data;
Johan Hedberg890ea892013-03-15 17:06:52 -05004612 struct hci_request req;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004613 int err;
Szymon Jancc72d4b82012-03-16 16:02:57 +01004614 __u16 source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004615
4616 BT_DBG("%s", hdev->name);
4617
Szymon Jancc72d4b82012-03-16 16:02:57 +01004618 source = __le16_to_cpu(cp->source);
4619
4620 if (source > 0x0002)
Johan Hedberga69e8372015-03-06 21:08:53 +02004621 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
4622 MGMT_STATUS_INVALID_PARAMS);
Szymon Jancc72d4b82012-03-16 16:02:57 +01004623
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004624 hci_dev_lock(hdev);
4625
Szymon Jancc72d4b82012-03-16 16:02:57 +01004626 hdev->devid_source = source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004627 hdev->devid_vendor = __le16_to_cpu(cp->vendor);
4628 hdev->devid_product = __le16_to_cpu(cp->product);
4629 hdev->devid_version = __le16_to_cpu(cp->version);
4630
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004631 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0,
4632 NULL, 0);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004633
Johan Hedberg890ea892013-03-15 17:06:52 -05004634 hci_req_init(&req, hdev);
4635 update_eir(&req);
4636 hci_req_run(&req, NULL);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004637
4638 hci_dev_unlock(hdev);
4639
4640 return err;
4641}
4642
Arman Uguray24b4f382015-03-23 15:57:12 -07004643static void enable_advertising_instance(struct hci_dev *hdev, u8 status,
4644 u16 opcode)
4645{
4646 BT_DBG("status %d", status);
4647}
4648
Marcel Holtmann1904a852015-01-11 13:50:44 -08004649static void set_advertising_complete(struct hci_dev *hdev, u8 status,
4650 u16 opcode)
Johan Hedberg4375f102013-09-25 13:26:10 +03004651{
4652 struct cmd_lookup match = { NULL, hdev };
Arman Uguray24b4f382015-03-23 15:57:12 -07004653 struct hci_request req;
Johan Hedberg4375f102013-09-25 13:26:10 +03004654
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304655 hci_dev_lock(hdev);
4656
Johan Hedberg4375f102013-09-25 13:26:10 +03004657 if (status) {
4658 u8 mgmt_err = mgmt_status(status);
4659
4660 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev,
4661 cmd_status_rsp, &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304662 goto unlock;
Johan Hedberg4375f102013-09-25 13:26:10 +03004663 }
4664
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004665 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004666 hci_dev_set_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03004667 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004668 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03004669
Johan Hedberg4375f102013-09-25 13:26:10 +03004670 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
4671 &match);
4672
4673 new_settings(hdev, match.sk);
4674
4675 if (match.sk)
4676 sock_put(match.sk);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304677
Arman Uguray24b4f382015-03-23 15:57:12 -07004678 /* If "Set Advertising" was just disabled and instance advertising was
4679 * set up earlier, then enable the advertising instance.
4680 */
4681 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
4682 !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
4683 goto unlock;
4684
4685 hci_req_init(&req, hdev);
4686
4687 update_adv_data(&req);
4688 enable_advertising(&req);
4689
4690 if (hci_req_run(&req, enable_advertising_instance) < 0)
4691 BT_ERR("Failed to re-configure advertising");
4692
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304693unlock:
4694 hci_dev_unlock(hdev);
Johan Hedberg4375f102013-09-25 13:26:10 +03004695}
4696
Marcel Holtmann21b51872013-10-10 09:47:53 -07004697static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
4698 u16 len)
Johan Hedberg4375f102013-09-25 13:26:10 +03004699{
4700 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004701 struct mgmt_pending_cmd *cmd;
Johan Hedberg4375f102013-09-25 13:26:10 +03004702 struct hci_request req;
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004703 u8 val, status;
Johan Hedberg4375f102013-09-25 13:26:10 +03004704 int err;
4705
4706 BT_DBG("request for %s", hdev->name);
4707
Johan Hedberge6fe7982013-10-02 15:45:22 +03004708 status = mgmt_le_support(hdev);
4709 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02004710 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4711 status);
Johan Hedberg4375f102013-09-25 13:26:10 +03004712
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004713 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02004714 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4715 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4375f102013-09-25 13:26:10 +03004716
4717 hci_dev_lock(hdev);
4718
4719 val = !!cp->val;
Johan Hedberg4375f102013-09-25 13:26:10 +03004720
Johan Hedbergf74ca9b2013-10-08 15:52:18 +02004721 /* The following conditions are ones which mean that we should
4722 * not do any HCI communication but directly send a mgmt
4723 * response to user space (after toggling the flag if
4724 * necessary).
4725 */
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004726 if (!hdev_is_powered(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004727 (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
4728 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) ||
Johan Hedberge8bb6b92014-07-08 15:07:53 +03004729 hci_conn_num(hdev, LE_LINK) > 0 ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004730 (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Johan Hedberge8bb6b92014-07-08 15:07:53 +03004731 hdev->le_scan_type == LE_SCAN_ACTIVE)) {
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004732 bool changed;
Johan Hedberg4375f102013-09-25 13:26:10 +03004733
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004734 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07004735 changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004736 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004737 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004738 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004739 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004740 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07004741 changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004742 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Johan Hedberg4375f102013-09-25 13:26:10 +03004743 }
4744
4745 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
4746 if (err < 0)
4747 goto unlock;
4748
4749 if (changed)
4750 err = new_settings(hdev, sk);
4751
4752 goto unlock;
4753 }
4754
Johan Hedberg333ae952015-03-17 13:48:47 +02004755 if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
4756 pending_find(MGMT_OP_SET_LE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004757 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4758 MGMT_STATUS_BUSY);
Johan Hedberg4375f102013-09-25 13:26:10 +03004759 goto unlock;
4760 }
4761
4762 cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
4763 if (!cmd) {
4764 err = -ENOMEM;
4765 goto unlock;
4766 }
4767
4768 hci_req_init(&req, hdev);
4769
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004770 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004771 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004772 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004773 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004774
Arman Uguray24b4f382015-03-23 15:57:12 -07004775 if (val) {
4776 /* Switch to instance "0" for the Set Advertising setting. */
Florian Grandelefae0022015-06-18 03:16:37 +02004777 update_inst_adv_data(&req, 0x00);
4778 update_inst_scan_rsp_data(&req, 0x00);
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07004779 enable_advertising(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07004780 } else {
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07004781 disable_advertising(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07004782 }
Johan Hedberg4375f102013-09-25 13:26:10 +03004783
4784 err = hci_req_run(&req, set_advertising_complete);
4785 if (err < 0)
4786 mgmt_pending_remove(cmd);
4787
4788unlock:
4789 hci_dev_unlock(hdev);
4790 return err;
4791}
4792
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004793static int set_static_address(struct sock *sk, struct hci_dev *hdev,
4794 void *data, u16 len)
4795{
4796 struct mgmt_cp_set_static_address *cp = data;
4797 int err;
4798
4799 BT_DBG("%s", hdev->name);
4800
Marcel Holtmann62af4442013-10-02 22:10:32 -07004801 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004802 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4803 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004804
4805 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004806 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4807 MGMT_STATUS_REJECTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004808
4809 if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
4810 if (!bacmp(&cp->bdaddr, BDADDR_NONE))
Johan Hedberga69e8372015-03-06 21:08:53 +02004811 return mgmt_cmd_status(sk, hdev->id,
4812 MGMT_OP_SET_STATIC_ADDRESS,
4813 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004814
4815 /* Two most significant bits shall be set */
4816 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
Johan Hedberga69e8372015-03-06 21:08:53 +02004817 return mgmt_cmd_status(sk, hdev->id,
4818 MGMT_OP_SET_STATIC_ADDRESS,
4819 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004820 }
4821
4822 hci_dev_lock(hdev);
4823
4824 bacpy(&hdev->static_addr, &cp->bdaddr);
4825
Marcel Holtmann93690c22015-03-06 10:11:21 -08004826 err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev);
4827 if (err < 0)
4828 goto unlock;
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004829
Marcel Holtmann93690c22015-03-06 10:11:21 -08004830 err = new_settings(hdev, sk);
4831
4832unlock:
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004833 hci_dev_unlock(hdev);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004834 return err;
4835}
4836
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004837static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
4838 void *data, u16 len)
4839{
4840 struct mgmt_cp_set_scan_params *cp = data;
4841 __u16 interval, window;
4842 int err;
4843
4844 BT_DBG("%s", hdev->name);
4845
4846 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004847 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4848 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004849
4850 interval = __le16_to_cpu(cp->interval);
4851
4852 if (interval < 0x0004 || interval > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02004853 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4854 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004855
4856 window = __le16_to_cpu(cp->window);
4857
4858 if (window < 0x0004 || window > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02004859 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4860 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004861
Marcel Holtmann899e1072013-10-14 09:55:32 -07004862 if (window > interval)
Johan Hedberga69e8372015-03-06 21:08:53 +02004863 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4864 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann899e1072013-10-14 09:55:32 -07004865
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004866 hci_dev_lock(hdev);
4867
4868 hdev->le_scan_interval = interval;
4869 hdev->le_scan_window = window;
4870
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004871 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0,
4872 NULL, 0);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004873
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03004874 /* If background scan is running, restart it so new parameters are
4875 * loaded.
4876 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004877 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03004878 hdev->discovery.state == DISCOVERY_STOPPED) {
4879 struct hci_request req;
4880
4881 hci_req_init(&req, hdev);
4882
4883 hci_req_add_le_scan_disable(&req);
4884 hci_req_add_le_passive_scan(&req);
4885
4886 hci_req_run(&req, NULL);
4887 }
4888
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004889 hci_dev_unlock(hdev);
4890
4891 return err;
4892}
4893
Marcel Holtmann1904a852015-01-11 13:50:44 -08004894static void fast_connectable_complete(struct hci_dev *hdev, u8 status,
4895 u16 opcode)
Johan Hedberg33e38b32013-03-15 17:07:05 -05004896{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004897 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004898
4899 BT_DBG("status 0x%02x", status);
4900
4901 hci_dev_lock(hdev);
4902
Johan Hedberg333ae952015-03-17 13:48:47 +02004903 cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004904 if (!cmd)
4905 goto unlock;
4906
4907 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004908 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4909 mgmt_status(status));
Johan Hedberg33e38b32013-03-15 17:07:05 -05004910 } else {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004911 struct mgmt_mode *cp = cmd->param;
4912
4913 if (cp->val)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004914 hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004915 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004916 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004917
Johan Hedberg33e38b32013-03-15 17:07:05 -05004918 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
4919 new_settings(hdev, cmd->sk);
4920 }
4921
4922 mgmt_pending_remove(cmd);
4923
4924unlock:
4925 hci_dev_unlock(hdev);
4926}
4927
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004928static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004929 void *data, u16 len)
Antti Julkuf6422ec2011-06-22 13:11:56 +03004930{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004931 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004932 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004933 struct hci_request req;
Antti Julkuf6422ec2011-06-22 13:11:56 +03004934 int err;
4935
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004936 BT_DBG("%s", hdev->name);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004937
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004938 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
Johan Hedberg56f87902013-10-02 13:43:13 +03004939 hdev->hci_ver < BLUETOOTH_VER_1_2)
Johan Hedberga69e8372015-03-06 21:08:53 +02004940 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4941 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03004942
Johan Hedberga7e80f22013-01-09 16:05:19 +02004943 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02004944 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4945 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02004946
Antti Julkuf6422ec2011-06-22 13:11:56 +03004947 hci_dev_lock(hdev);
4948
Johan Hedberg333ae952015-03-17 13:48:47 +02004949 if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004950 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4951 MGMT_STATUS_BUSY);
Johan Hedberg05cbf292013-03-15 17:07:07 -05004952 goto unlock;
4953 }
4954
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004955 if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004956 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4957 hdev);
4958 goto unlock;
4959 }
4960
Johan Hedberg406ef2a2015-03-10 20:14:27 +02004961 if (!hdev_is_powered(hdev)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07004962 hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg406ef2a2015-03-10 20:14:27 +02004963 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4964 hdev);
4965 new_settings(hdev, sk);
4966 goto unlock;
4967 }
4968
Johan Hedberg33e38b32013-03-15 17:07:05 -05004969 cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev,
4970 data, len);
4971 if (!cmd) {
4972 err = -ENOMEM;
4973 goto unlock;
4974 }
4975
4976 hci_req_init(&req, hdev);
4977
Johan Hedberg406d7802013-03-15 17:07:09 -05004978 write_fast_connectable(&req, cp->val);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004979
4980 err = hci_req_run(&req, fast_connectable_complete);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004981 if (err < 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004982 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4983 MGMT_STATUS_FAILED);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004984 mgmt_pending_remove(cmd);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004985 }
4986
Johan Hedberg33e38b32013-03-15 17:07:05 -05004987unlock:
Antti Julkuf6422ec2011-06-22 13:11:56 +03004988 hci_dev_unlock(hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004989
Antti Julkuf6422ec2011-06-22 13:11:56 +03004990 return err;
4991}
4992
Marcel Holtmann1904a852015-01-11 13:50:44 -08004993static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg0663ca22013-10-02 13:43:14 +03004994{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004995 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03004996
4997 BT_DBG("status 0x%02x", status);
4998
4999 hci_dev_lock(hdev);
5000
Johan Hedberg333ae952015-03-17 13:48:47 +02005001 cmd = pending_find(MGMT_OP_SET_BREDR, hdev);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005002 if (!cmd)
5003 goto unlock;
5004
5005 if (status) {
5006 u8 mgmt_err = mgmt_status(status);
5007
5008 /* We need to restore the flag if related HCI commands
5009 * failed.
5010 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005011 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005012
Johan Hedberga69e8372015-03-06 21:08:53 +02005013 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005014 } else {
5015 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
5016 new_settings(hdev, cmd->sk);
5017 }
5018
5019 mgmt_pending_remove(cmd);
5020
5021unlock:
5022 hci_dev_unlock(hdev);
5023}
5024
5025static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
5026{
5027 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005028 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03005029 struct hci_request req;
5030 int err;
5031
5032 BT_DBG("request for %s", hdev->name);
5033
5034 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005035 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5036 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005037
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005038 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02005039 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5040 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005041
5042 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02005043 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5044 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005045
5046 hci_dev_lock(hdev);
5047
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005048 if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg0663ca22013-10-02 13:43:14 +03005049 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
5050 goto unlock;
5051 }
5052
5053 if (!hdev_is_powered(hdev)) {
5054 if (!cp->val) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005055 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
5056 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
5057 hci_dev_clear_flag(hdev, HCI_LINK_SECURITY);
5058 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
5059 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005060 }
5061
Marcel Holtmannce05d602015-03-13 02:11:03 -07005062 hci_dev_change_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005063
5064 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
5065 if (err < 0)
5066 goto unlock;
5067
5068 err = new_settings(hdev, sk);
5069 goto unlock;
5070 }
5071
5072 /* Reject disabling when powered on */
5073 if (!cp->val) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005074 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5075 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005076 goto unlock;
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08005077 } else {
5078 /* When configuring a dual-mode controller to operate
5079 * with LE only and using a static address, then switching
5080 * BR/EDR back on is not allowed.
5081 *
5082 * Dual-mode controllers shall operate with the public
5083 * address as its identity address for BR/EDR and LE. So
5084 * reject the attempt to create an invalid configuration.
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08005085 *
5086 * The same restrictions applies when secure connections
5087 * has been enabled. For BR/EDR this is a controller feature
5088 * while for LE it is a host stack feature. This means that
5089 * switching BR/EDR back on when secure connections has been
5090 * enabled is not a supported transaction.
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08005091 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005092 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08005093 (bacmp(&hdev->static_addr, BDADDR_ANY) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005094 hci_dev_test_flag(hdev, HCI_SC_ENABLED))) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005095 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5096 MGMT_STATUS_REJECTED);
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08005097 goto unlock;
5098 }
Johan Hedberg0663ca22013-10-02 13:43:14 +03005099 }
5100
Johan Hedberg333ae952015-03-17 13:48:47 +02005101 if (pending_find(MGMT_OP_SET_BREDR, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005102 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5103 MGMT_STATUS_BUSY);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005104 goto unlock;
5105 }
5106
5107 cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len);
5108 if (!cmd) {
5109 err = -ENOMEM;
5110 goto unlock;
5111 }
5112
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07005113 /* We need to flip the bit already here so that update_adv_data
Johan Hedberg0663ca22013-10-02 13:43:14 +03005114 * generates the correct flags.
5115 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005116 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005117
5118 hci_req_init(&req, hdev);
Johan Hedbergaa8af462013-10-14 21:15:26 +03005119
Johan Hedberg432df052014-08-01 11:13:31 +03005120 write_fast_connectable(&req, false);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02005121 __hci_update_page_scan(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03005122
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07005123 /* Since only the advertising data flags will change, there
5124 * is no need to update the scan response data.
5125 */
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07005126 update_adv_data(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03005127
Johan Hedberg0663ca22013-10-02 13:43:14 +03005128 err = hci_req_run(&req, set_bredr_complete);
5129 if (err < 0)
5130 mgmt_pending_remove(cmd);
5131
5132unlock:
5133 hci_dev_unlock(hdev);
5134 return err;
5135}
5136
Johan Hedberga1443f52015-01-23 15:42:46 +02005137static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
5138{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005139 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02005140 struct mgmt_mode *cp;
5141
5142 BT_DBG("%s status %u", hdev->name, status);
5143
5144 hci_dev_lock(hdev);
5145
Johan Hedberg333ae952015-03-17 13:48:47 +02005146 cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev);
Johan Hedberga1443f52015-01-23 15:42:46 +02005147 if (!cmd)
5148 goto unlock;
5149
5150 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005151 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
5152 mgmt_status(status));
Johan Hedberga1443f52015-01-23 15:42:46 +02005153 goto remove;
5154 }
5155
5156 cp = cmd->param;
5157
5158 switch (cp->val) {
5159 case 0x00:
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005160 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
5161 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02005162 break;
5163 case 0x01:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005164 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005165 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02005166 break;
5167 case 0x02:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005168 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
5169 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02005170 break;
5171 }
5172
5173 send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev);
5174 new_settings(hdev, cmd->sk);
5175
5176remove:
5177 mgmt_pending_remove(cmd);
5178unlock:
5179 hci_dev_unlock(hdev);
5180}
5181
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005182static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
5183 void *data, u16 len)
5184{
5185 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005186 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02005187 struct hci_request req;
Johan Hedberga3209692014-05-26 11:23:35 +03005188 u8 val;
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005189 int err;
5190
5191 BT_DBG("request for %s", hdev->name);
5192
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08005193 if (!lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005194 !hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02005195 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5196 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005197
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005198 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Johan Hedberg59200282015-01-28 19:56:00 +02005199 lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005200 !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02005201 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5202 MGMT_STATUS_REJECTED);
Marcel Holtmanned93ec62015-01-22 11:15:22 -08005203
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005204 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02005205 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005206 MGMT_STATUS_INVALID_PARAMS);
5207
5208 hci_dev_lock(hdev);
5209
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08005210 if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005211 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005212 bool changed;
5213
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005214 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07005215 changed = !hci_dev_test_and_set_flag(hdev,
5216 HCI_SC_ENABLED);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005217 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005218 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005219 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005220 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005221 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005222 changed = hci_dev_test_and_clear_flag(hdev,
5223 HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005224 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005225 }
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005226
5227 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
5228 if (err < 0)
5229 goto failed;
5230
5231 if (changed)
5232 err = new_settings(hdev, sk);
5233
5234 goto failed;
5235 }
5236
Johan Hedberg333ae952015-03-17 13:48:47 +02005237 if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005238 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5239 MGMT_STATUS_BUSY);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005240 goto failed;
5241 }
5242
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005243 val = !!cp->val;
5244
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005245 if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
5246 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005247 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
5248 goto failed;
5249 }
5250
5251 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len);
5252 if (!cmd) {
5253 err = -ENOMEM;
5254 goto failed;
5255 }
5256
Johan Hedberga1443f52015-01-23 15:42:46 +02005257 hci_req_init(&req, hdev);
5258 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val);
5259 err = hci_req_run(&req, sc_enable_complete);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005260 if (err < 0) {
5261 mgmt_pending_remove(cmd);
5262 goto failed;
5263 }
5264
5265failed:
5266 hci_dev_unlock(hdev);
5267 return err;
5268}
5269
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005270static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
5271 void *data, u16 len)
5272{
5273 struct mgmt_mode *cp = data;
Johan Hedbergb97109792014-06-24 14:00:28 +03005274 bool changed, use_changed;
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005275 int err;
5276
5277 BT_DBG("request for %s", hdev->name);
5278
Johan Hedbergb97109792014-06-24 14:00:28 +03005279 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02005280 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
5281 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005282
5283 hci_dev_lock(hdev);
5284
5285 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07005286 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005287 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005288 changed = hci_dev_test_and_clear_flag(hdev,
5289 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005290
Johan Hedbergb97109792014-06-24 14:00:28 +03005291 if (cp->val == 0x02)
Marcel Holtmann238be782015-03-13 02:11:06 -07005292 use_changed = !hci_dev_test_and_set_flag(hdev,
5293 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03005294 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005295 use_changed = hci_dev_test_and_clear_flag(hdev,
5296 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03005297
5298 if (hdev_is_powered(hdev) && use_changed &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005299 hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergb97109792014-06-24 14:00:28 +03005300 u8 mode = (cp->val == 0x02) ? 0x01 : 0x00;
5301 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
5302 sizeof(mode), &mode);
5303 }
5304
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005305 err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev);
5306 if (err < 0)
5307 goto unlock;
5308
5309 if (changed)
5310 err = new_settings(hdev, sk);
5311
5312unlock:
5313 hci_dev_unlock(hdev);
5314 return err;
5315}
5316
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005317static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5318 u16 len)
5319{
5320 struct mgmt_cp_set_privacy *cp = cp_data;
5321 bool changed;
5322 int err;
5323
5324 BT_DBG("request for %s", hdev->name);
5325
5326 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005327 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5328 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005329
5330 if (cp->privacy != 0x00 && cp->privacy != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02005331 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5332 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005333
5334 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005335 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5336 MGMT_STATUS_REJECTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005337
5338 hci_dev_lock(hdev);
5339
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02005340 /* If user space supports this command it is also expected to
5341 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
5342 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005343 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02005344
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005345 if (cp->privacy) {
Marcel Holtmann238be782015-03-13 02:11:06 -07005346 changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005347 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005348 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005349 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005350 changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005351 memset(hdev->irk, 0, sizeof(hdev->irk));
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005352 hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005353 }
5354
5355 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
5356 if (err < 0)
5357 goto unlock;
5358
5359 if (changed)
5360 err = new_settings(hdev, sk);
5361
5362unlock:
5363 hci_dev_unlock(hdev);
5364 return err;
5365}
5366
Johan Hedberg41edf162014-02-18 10:19:35 +02005367static bool irk_is_valid(struct mgmt_irk_info *irk)
5368{
5369 switch (irk->addr.type) {
5370 case BDADDR_LE_PUBLIC:
5371 return true;
5372
5373 case BDADDR_LE_RANDOM:
5374 /* Two most significant bits shall be set */
5375 if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0)
5376 return false;
5377 return true;
5378 }
5379
5380 return false;
5381}
5382
5383static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5384 u16 len)
5385{
5386 struct mgmt_cp_load_irks *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005387 const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) /
5388 sizeof(struct mgmt_irk_info));
Johan Hedberg41edf162014-02-18 10:19:35 +02005389 u16 irk_count, expected_len;
5390 int i, err;
5391
5392 BT_DBG("request for %s", hdev->name);
5393
5394 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005395 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5396 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg41edf162014-02-18 10:19:35 +02005397
5398 irk_count = __le16_to_cpu(cp->irk_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005399 if (irk_count > max_irk_count) {
5400 BT_ERR("load_irks: too big irk_count value %u", irk_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02005401 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5402 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005403 }
Johan Hedberg41edf162014-02-18 10:19:35 +02005404
5405 expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info);
5406 if (expected_len != len) {
5407 BT_ERR("load_irks: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02005408 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02005409 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5410 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02005411 }
5412
5413 BT_DBG("%s irk_count %u", hdev->name, irk_count);
5414
5415 for (i = 0; i < irk_count; i++) {
5416 struct mgmt_irk_info *key = &cp->irks[i];
5417
5418 if (!irk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02005419 return mgmt_cmd_status(sk, hdev->id,
5420 MGMT_OP_LOAD_IRKS,
5421 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02005422 }
5423
5424 hci_dev_lock(hdev);
5425
5426 hci_smp_irks_clear(hdev);
5427
5428 for (i = 0; i < irk_count; i++) {
5429 struct mgmt_irk_info *irk = &cp->irks[i];
5430 u8 addr_type;
5431
5432 if (irk->addr.type == BDADDR_LE_PUBLIC)
5433 addr_type = ADDR_LE_DEV_PUBLIC;
5434 else
5435 addr_type = ADDR_LE_DEV_RANDOM;
5436
5437 hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val,
5438 BDADDR_ANY);
5439 }
5440
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005441 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedberg41edf162014-02-18 10:19:35 +02005442
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005443 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
Johan Hedberg41edf162014-02-18 10:19:35 +02005444
5445 hci_dev_unlock(hdev);
5446
5447 return err;
5448}
5449
Johan Hedberg3f706b72013-01-20 14:27:16 +02005450static bool ltk_is_valid(struct mgmt_ltk_info *key)
5451{
5452 if (key->master != 0x00 && key->master != 0x01)
5453 return false;
Marcel Holtmann490cb0b2014-02-16 12:59:05 -08005454
5455 switch (key->addr.type) {
5456 case BDADDR_LE_PUBLIC:
5457 return true;
5458
5459 case BDADDR_LE_RANDOM:
5460 /* Two most significant bits shall be set */
5461 if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0)
5462 return false;
5463 return true;
5464 }
5465
5466 return false;
Johan Hedberg3f706b72013-01-20 14:27:16 +02005467}
5468
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005469static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005470 void *cp_data, u16 len)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005471{
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005472 struct mgmt_cp_load_long_term_keys *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005473 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
5474 sizeof(struct mgmt_ltk_info));
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005475 u16 key_count, expected_len;
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005476 int i, err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005477
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07005478 BT_DBG("request for %s", hdev->name);
5479
5480 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005481 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5482 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07005483
Marcel Holtmann1f350c82012-03-12 20:31:08 -07005484 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005485 if (key_count > max_key_count) {
5486 BT_ERR("load_ltks: too big key_count value %u", key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02005487 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5488 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005489 }
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005490
5491 expected_len = sizeof(*cp) + key_count *
5492 sizeof(struct mgmt_ltk_info);
5493 if (expected_len != len) {
5494 BT_ERR("load_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02005495 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02005496 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5497 MGMT_STATUS_INVALID_PARAMS);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005498 }
5499
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005500 BT_DBG("%s key_count %u", hdev->name, key_count);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005501
Johan Hedberg54ad6d82013-01-20 14:27:15 +02005502 for (i = 0; i < key_count; i++) {
5503 struct mgmt_ltk_info *key = &cp->keys[i];
5504
Johan Hedberg3f706b72013-01-20 14:27:16 +02005505 if (!ltk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02005506 return mgmt_cmd_status(sk, hdev->id,
5507 MGMT_OP_LOAD_LONG_TERM_KEYS,
5508 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg54ad6d82013-01-20 14:27:15 +02005509 }
5510
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005511 hci_dev_lock(hdev);
5512
5513 hci_smp_ltks_clear(hdev);
5514
5515 for (i = 0; i < key_count; i++) {
5516 struct mgmt_ltk_info *key = &cp->keys[i];
Johan Hedbergd7b25452014-05-23 13:19:53 +03005517 u8 type, addr_type, authenticated;
Marcel Holtmann79d95a12013-10-13 03:57:38 -07005518
5519 if (key->addr.type == BDADDR_LE_PUBLIC)
5520 addr_type = ADDR_LE_DEV_PUBLIC;
5521 else
5522 addr_type = ADDR_LE_DEV_RANDOM;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005523
Johan Hedberg61b43352014-05-29 19:36:53 +03005524 switch (key->type) {
5525 case MGMT_LTK_UNAUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03005526 authenticated = 0x00;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005527 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03005528 break;
5529 case MGMT_LTK_AUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03005530 authenticated = 0x01;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005531 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03005532 break;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005533 case MGMT_LTK_P256_UNAUTH:
5534 authenticated = 0x00;
5535 type = SMP_LTK_P256;
5536 break;
5537 case MGMT_LTK_P256_AUTH:
5538 authenticated = 0x01;
5539 type = SMP_LTK_P256;
5540 break;
5541 case MGMT_LTK_P256_DEBUG:
5542 authenticated = 0x00;
5543 type = SMP_LTK_P256_DEBUG;
Johan Hedberg61b43352014-05-29 19:36:53 +03005544 default:
5545 continue;
5546 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03005547
Johan Hedberg35d70272014-02-19 14:57:47 +02005548 hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type,
Johan Hedbergd7b25452014-05-23 13:19:53 +03005549 authenticated, key->val, key->enc_size, key->ediv,
Johan Hedberg35d70272014-02-19 14:57:47 +02005550 key->rand);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005551 }
5552
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005553 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005554 NULL, 0);
5555
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005556 hci_dev_unlock(hdev);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005557
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005558 return err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005559}
5560
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005561static int conn_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005562{
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005563 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005564 struct mgmt_rp_get_conn_info rp;
Johan Hedberg9df74652014-12-19 22:26:03 +02005565 int err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005566
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005567 memcpy(&rp.addr, cmd->param, sizeof(rp.addr));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005568
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005569 if (status == MGMT_STATUS_SUCCESS) {
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005570 rp.rssi = conn->rssi;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005571 rp.tx_power = conn->tx_power;
5572 rp.max_tx_power = conn->max_tx_power;
5573 } else {
5574 rp.rssi = HCI_RSSI_INVALID;
5575 rp.tx_power = HCI_TX_POWER_INVALID;
5576 rp.max_tx_power = HCI_TX_POWER_INVALID;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005577 }
5578
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005579 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO,
5580 status, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005581
5582 hci_conn_drop(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005583 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02005584
5585 return err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005586}
5587
Marcel Holtmann1904a852015-01-11 13:50:44 -08005588static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status,
5589 u16 opcode)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005590{
5591 struct hci_cp_read_rssi *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005592 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005593 struct hci_conn *conn;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005594 u16 handle;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005595 u8 status;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005596
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005597 BT_DBG("status 0x%02x", hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005598
5599 hci_dev_lock(hdev);
5600
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005601 /* Commands sent in request are either Read RSSI or Read Transmit Power
5602 * Level so we check which one was last sent to retrieve connection
5603 * handle. Both commands have handle as first parameter so it's safe to
5604 * cast data on the same command struct.
5605 *
5606 * First command sent is always Read RSSI and we fail only if it fails.
5607 * In other case we simply override error to indicate success as we
5608 * already remembered if TX power value is actually valid.
5609 */
5610 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI);
5611 if (!cp) {
5612 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005613 status = MGMT_STATUS_SUCCESS;
5614 } else {
5615 status = mgmt_status(hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005616 }
5617
5618 if (!cp) {
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005619 BT_ERR("invalid sent_cmd in conn_info response");
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005620 goto unlock;
5621 }
5622
5623 handle = __le16_to_cpu(cp->handle);
5624 conn = hci_conn_hash_lookup_handle(hdev, handle);
5625 if (!conn) {
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005626 BT_ERR("unknown handle (%d) in conn_info response", handle);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005627 goto unlock;
5628 }
5629
Johan Hedberg333ae952015-03-17 13:48:47 +02005630 cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005631 if (!cmd)
5632 goto unlock;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005633
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005634 cmd->cmd_complete(cmd, status);
5635 mgmt_pending_remove(cmd);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005636
5637unlock:
5638 hci_dev_unlock(hdev);
5639}
5640
5641static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
5642 u16 len)
5643{
5644 struct mgmt_cp_get_conn_info *cp = data;
5645 struct mgmt_rp_get_conn_info rp;
5646 struct hci_conn *conn;
5647 unsigned long conn_info_age;
5648 int err = 0;
5649
5650 BT_DBG("%s", hdev->name);
5651
5652 memset(&rp, 0, sizeof(rp));
5653 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5654 rp.addr.type = cp->addr.type;
5655
5656 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005657 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5658 MGMT_STATUS_INVALID_PARAMS,
5659 &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005660
5661 hci_dev_lock(hdev);
5662
5663 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005664 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5665 MGMT_STATUS_NOT_POWERED, &rp,
5666 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005667 goto unlock;
5668 }
5669
5670 if (cp->addr.type == BDADDR_BREDR)
5671 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5672 &cp->addr.bdaddr);
5673 else
5674 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
5675
5676 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005677 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5678 MGMT_STATUS_NOT_CONNECTED, &rp,
5679 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005680 goto unlock;
5681 }
5682
Johan Hedberg333ae952015-03-17 13:48:47 +02005683 if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005684 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5685 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005686 goto unlock;
5687 }
5688
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005689 /* To avoid client trying to guess when to poll again for information we
5690 * calculate conn info age as random value between min/max set in hdev.
5691 */
5692 conn_info_age = hdev->conn_info_min_age +
5693 prandom_u32_max(hdev->conn_info_max_age -
5694 hdev->conn_info_min_age);
5695
5696 /* Query controller to refresh cached values if they are too old or were
5697 * never read.
5698 */
Andrzej Kaczmarekf4e2dd52014-05-16 16:48:57 +02005699 if (time_after(jiffies, conn->conn_info_timestamp +
5700 msecs_to_jiffies(conn_info_age)) ||
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005701 !conn->conn_info_timestamp) {
5702 struct hci_request req;
5703 struct hci_cp_read_tx_power req_txp_cp;
5704 struct hci_cp_read_rssi req_rssi_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005705 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005706
5707 hci_req_init(&req, hdev);
5708 req_rssi_cp.handle = cpu_to_le16(conn->handle);
5709 hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp),
5710 &req_rssi_cp);
5711
Andrzej Kaczmarekf7faab02014-05-14 13:43:04 +02005712 /* For LE links TX power does not change thus we don't need to
5713 * query for it once value is known.
5714 */
5715 if (!bdaddr_type_is_le(cp->addr.type) ||
5716 conn->tx_power == HCI_TX_POWER_INVALID) {
5717 req_txp_cp.handle = cpu_to_le16(conn->handle);
5718 req_txp_cp.type = 0x00;
5719 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5720 sizeof(req_txp_cp), &req_txp_cp);
5721 }
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005722
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005723 /* Max TX power needs to be read only once per connection */
5724 if (conn->max_tx_power == HCI_TX_POWER_INVALID) {
5725 req_txp_cp.handle = cpu_to_le16(conn->handle);
5726 req_txp_cp.type = 0x01;
5727 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5728 sizeof(req_txp_cp), &req_txp_cp);
5729 }
5730
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005731 err = hci_req_run(&req, conn_info_refresh_complete);
5732 if (err < 0)
5733 goto unlock;
5734
5735 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev,
5736 data, len);
5737 if (!cmd) {
5738 err = -ENOMEM;
5739 goto unlock;
5740 }
5741
5742 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005743 cmd->user_data = hci_conn_get(conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005744 cmd->cmd_complete = conn_info_cmd_complete;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005745
5746 conn->conn_info_timestamp = jiffies;
5747 } else {
5748 /* Cache is valid, just reply with values cached in hci_conn */
5749 rp.rssi = conn->rssi;
5750 rp.tx_power = conn->tx_power;
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005751 rp.max_tx_power = conn->max_tx_power;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005752
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005753 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5754 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005755 }
5756
5757unlock:
5758 hci_dev_unlock(hdev);
5759 return err;
5760}
5761
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005762static int clock_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg69487372014-12-05 13:36:07 +02005763{
5764 struct hci_conn *conn = cmd->user_data;
5765 struct mgmt_rp_get_clock_info rp;
5766 struct hci_dev *hdev;
Johan Hedberg9df74652014-12-19 22:26:03 +02005767 int err;
Johan Hedberg69487372014-12-05 13:36:07 +02005768
5769 memset(&rp, 0, sizeof(rp));
5770 memcpy(&rp.addr, &cmd->param, sizeof(rp.addr));
5771
5772 if (status)
5773 goto complete;
5774
5775 hdev = hci_dev_get(cmd->index);
5776 if (hdev) {
5777 rp.local_clock = cpu_to_le32(hdev->clock);
5778 hci_dev_put(hdev);
5779 }
5780
5781 if (conn) {
5782 rp.piconet_clock = cpu_to_le32(conn->clock);
5783 rp.accuracy = cpu_to_le16(conn->clock_accuracy);
5784 }
5785
5786complete:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005787 err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp,
5788 sizeof(rp));
Johan Hedberg69487372014-12-05 13:36:07 +02005789
5790 if (conn) {
5791 hci_conn_drop(conn);
5792 hci_conn_put(conn);
5793 }
Johan Hedberg9df74652014-12-19 22:26:03 +02005794
5795 return err;
Johan Hedberg69487372014-12-05 13:36:07 +02005796}
5797
Marcel Holtmann1904a852015-01-11 13:50:44 -08005798static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg95868422014-06-28 17:54:07 +03005799{
Johan Hedberg95868422014-06-28 17:54:07 +03005800 struct hci_cp_read_clock *hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005801 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03005802 struct hci_conn *conn;
5803
5804 BT_DBG("%s status %u", hdev->name, status);
5805
5806 hci_dev_lock(hdev);
5807
5808 hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
5809 if (!hci_cp)
5810 goto unlock;
5811
5812 if (hci_cp->which) {
5813 u16 handle = __le16_to_cpu(hci_cp->handle);
5814 conn = hci_conn_hash_lookup_handle(hdev, handle);
5815 } else {
5816 conn = NULL;
5817 }
5818
Johan Hedberg333ae952015-03-17 13:48:47 +02005819 cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn);
Johan Hedberg95868422014-06-28 17:54:07 +03005820 if (!cmd)
5821 goto unlock;
5822
Johan Hedberg69487372014-12-05 13:36:07 +02005823 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberg95868422014-06-28 17:54:07 +03005824 mgmt_pending_remove(cmd);
Johan Hedberg95868422014-06-28 17:54:07 +03005825
5826unlock:
5827 hci_dev_unlock(hdev);
5828}
5829
5830static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
5831 u16 len)
5832{
5833 struct mgmt_cp_get_clock_info *cp = data;
5834 struct mgmt_rp_get_clock_info rp;
5835 struct hci_cp_read_clock hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005836 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03005837 struct hci_request req;
5838 struct hci_conn *conn;
5839 int err;
5840
5841 BT_DBG("%s", hdev->name);
5842
5843 memset(&rp, 0, sizeof(rp));
5844 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5845 rp.addr.type = cp->addr.type;
5846
5847 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005848 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5849 MGMT_STATUS_INVALID_PARAMS,
5850 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005851
5852 hci_dev_lock(hdev);
5853
5854 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005855 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5856 MGMT_STATUS_NOT_POWERED, &rp,
5857 sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005858 goto unlock;
5859 }
5860
5861 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
5862 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5863 &cp->addr.bdaddr);
5864 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005865 err = mgmt_cmd_complete(sk, hdev->id,
5866 MGMT_OP_GET_CLOCK_INFO,
5867 MGMT_STATUS_NOT_CONNECTED,
5868 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005869 goto unlock;
5870 }
5871 } else {
5872 conn = NULL;
5873 }
5874
5875 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len);
5876 if (!cmd) {
5877 err = -ENOMEM;
5878 goto unlock;
5879 }
5880
Johan Hedberg69487372014-12-05 13:36:07 +02005881 cmd->cmd_complete = clock_info_cmd_complete;
5882
Johan Hedberg95868422014-06-28 17:54:07 +03005883 hci_req_init(&req, hdev);
5884
5885 memset(&hci_cp, 0, sizeof(hci_cp));
5886 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5887
5888 if (conn) {
5889 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005890 cmd->user_data = hci_conn_get(conn);
Johan Hedberg95868422014-06-28 17:54:07 +03005891
5892 hci_cp.handle = cpu_to_le16(conn->handle);
5893 hci_cp.which = 0x01; /* Piconet clock */
5894 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5895 }
5896
5897 err = hci_req_run(&req, get_clock_info_complete);
5898 if (err < 0)
5899 mgmt_pending_remove(cmd);
5900
5901unlock:
5902 hci_dev_unlock(hdev);
5903 return err;
5904}
5905
Johan Hedberg5a154e62014-12-19 22:26:02 +02005906static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
5907{
5908 struct hci_conn *conn;
5909
5910 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
5911 if (!conn)
5912 return false;
5913
5914 if (conn->dst_type != type)
5915 return false;
5916
5917 if (conn->state != BT_CONNECTED)
5918 return false;
5919
5920 return true;
5921}
5922
5923/* This function requires the caller holds hdev->lock */
5924static int hci_conn_params_set(struct hci_request *req, bdaddr_t *addr,
5925 u8 addr_type, u8 auto_connect)
5926{
5927 struct hci_dev *hdev = req->hdev;
5928 struct hci_conn_params *params;
5929
5930 params = hci_conn_params_add(hdev, addr, addr_type);
5931 if (!params)
5932 return -EIO;
5933
5934 if (params->auto_connect == auto_connect)
5935 return 0;
5936
5937 list_del_init(&params->action);
5938
5939 switch (auto_connect) {
5940 case HCI_AUTO_CONN_DISABLED:
5941 case HCI_AUTO_CONN_LINK_LOSS:
5942 __hci_update_background_scan(req);
5943 break;
5944 case HCI_AUTO_CONN_REPORT:
5945 list_add(&params->action, &hdev->pend_le_reports);
5946 __hci_update_background_scan(req);
5947 break;
5948 case HCI_AUTO_CONN_DIRECT:
5949 case HCI_AUTO_CONN_ALWAYS:
5950 if (!is_connected(hdev, addr, addr_type)) {
5951 list_add(&params->action, &hdev->pend_le_conns);
5952 __hci_update_background_scan(req);
5953 }
5954 break;
5955 }
5956
5957 params->auto_connect = auto_connect;
5958
5959 BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
5960 auto_connect);
5961
5962 return 0;
5963}
5964
Marcel Holtmann8afef092014-06-29 22:28:34 +02005965static void device_added(struct sock *sk, struct hci_dev *hdev,
5966 bdaddr_t *bdaddr, u8 type, u8 action)
5967{
5968 struct mgmt_ev_device_added ev;
5969
5970 bacpy(&ev.addr.bdaddr, bdaddr);
5971 ev.addr.type = type;
5972 ev.action = action;
5973
5974 mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk);
5975}
5976
Marcel Holtmann1904a852015-01-11 13:50:44 -08005977static void add_device_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg5a154e62014-12-19 22:26:02 +02005978{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005979 struct mgmt_pending_cmd *cmd;
Johan Hedberg5a154e62014-12-19 22:26:02 +02005980
5981 BT_DBG("status 0x%02x", status);
5982
5983 hci_dev_lock(hdev);
5984
Johan Hedberg333ae952015-03-17 13:48:47 +02005985 cmd = pending_find(MGMT_OP_ADD_DEVICE, hdev);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005986 if (!cmd)
5987 goto unlock;
5988
5989 cmd->cmd_complete(cmd, mgmt_status(status));
5990 mgmt_pending_remove(cmd);
5991
5992unlock:
5993 hci_dev_unlock(hdev);
5994}
5995
Marcel Holtmann2faade52014-06-29 19:44:03 +02005996static int add_device(struct sock *sk, struct hci_dev *hdev,
5997 void *data, u16 len)
5998{
5999 struct mgmt_cp_add_device *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006000 struct mgmt_pending_cmd *cmd;
Johan Hedberg5a154e62014-12-19 22:26:02 +02006001 struct hci_request req;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006002 u8 auto_conn, addr_type;
6003 int err;
6004
6005 BT_DBG("%s", hdev->name);
6006
Johan Hedberg66593582014-07-09 12:59:14 +03006007 if (!bdaddr_type_is_valid(cp->addr.type) ||
Marcel Holtmann2faade52014-06-29 19:44:03 +02006008 !bacmp(&cp->addr.bdaddr, BDADDR_ANY))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006009 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
6010 MGMT_STATUS_INVALID_PARAMS,
6011 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02006012
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006013 if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006014 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
6015 MGMT_STATUS_INVALID_PARAMS,
6016 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02006017
Johan Hedberg5a154e62014-12-19 22:26:02 +02006018 hci_req_init(&req, hdev);
6019
Marcel Holtmann2faade52014-06-29 19:44:03 +02006020 hci_dev_lock(hdev);
6021
Johan Hedberg5a154e62014-12-19 22:26:02 +02006022 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_DEVICE, hdev, data, len);
6023 if (!cmd) {
6024 err = -ENOMEM;
6025 goto unlock;
6026 }
6027
6028 cmd->cmd_complete = addr_cmd_complete;
6029
Johan Hedberg66593582014-07-09 12:59:14 +03006030 if (cp->addr.type == BDADDR_BREDR) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006031 /* Only incoming connections action is supported for now */
Johan Hedberg66593582014-07-09 12:59:14 +03006032 if (cp->action != 0x01) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006033 err = cmd->cmd_complete(cmd,
6034 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg5a154e62014-12-19 22:26:02 +02006035 mgmt_pending_remove(cmd);
Johan Hedberg66593582014-07-09 12:59:14 +03006036 goto unlock;
6037 }
6038
6039 err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr,
6040 cp->addr.type);
6041 if (err)
6042 goto unlock;
Johan Hedberga3974072014-07-09 12:59:15 +03006043
Johan Hedberg5a154e62014-12-19 22:26:02 +02006044 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03006045
Johan Hedberg66593582014-07-09 12:59:14 +03006046 goto added;
6047 }
6048
Marcel Holtmann2faade52014-06-29 19:44:03 +02006049 if (cp->addr.type == BDADDR_LE_PUBLIC)
6050 addr_type = ADDR_LE_DEV_PUBLIC;
6051 else
6052 addr_type = ADDR_LE_DEV_RANDOM;
6053
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006054 if (cp->action == 0x02)
Marcel Holtmann2faade52014-06-29 19:44:03 +02006055 auto_conn = HCI_AUTO_CONN_ALWAYS;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006056 else if (cp->action == 0x01)
6057 auto_conn = HCI_AUTO_CONN_DIRECT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006058 else
Johan Hedberga3451d22014-07-02 17:37:27 +03006059 auto_conn = HCI_AUTO_CONN_REPORT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006060
Marcel Holtmannbf5b3c82014-06-30 12:34:39 +02006061 /* If the connection parameters don't exist for this device,
6062 * they will be created and configured with defaults.
6063 */
Johan Hedberg5a154e62014-12-19 22:26:02 +02006064 if (hci_conn_params_set(&req, &cp->addr.bdaddr, addr_type,
Marcel Holtmannd06b50c2014-07-01 12:11:06 +02006065 auto_conn) < 0) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006066 err = cmd->cmd_complete(cmd, MGMT_STATUS_FAILED);
Johan Hedberg5a154e62014-12-19 22:26:02 +02006067 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006068 goto unlock;
6069 }
6070
Johan Hedberg66593582014-07-09 12:59:14 +03006071added:
Marcel Holtmann8afef092014-06-29 22:28:34 +02006072 device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
6073
Johan Hedberg5a154e62014-12-19 22:26:02 +02006074 err = hci_req_run(&req, add_device_complete);
6075 if (err < 0) {
6076 /* ENODATA means no HCI commands were needed (e.g. if
6077 * the adapter is powered off).
6078 */
Johan Hedberg9df74652014-12-19 22:26:03 +02006079 if (err == -ENODATA)
6080 err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS);
Johan Hedberg5a154e62014-12-19 22:26:02 +02006081 mgmt_pending_remove(cmd);
6082 }
Marcel Holtmann2faade52014-06-29 19:44:03 +02006083
6084unlock:
6085 hci_dev_unlock(hdev);
6086 return err;
6087}
6088
Marcel Holtmann8afef092014-06-29 22:28:34 +02006089static void device_removed(struct sock *sk, struct hci_dev *hdev,
6090 bdaddr_t *bdaddr, u8 type)
6091{
6092 struct mgmt_ev_device_removed ev;
6093
6094 bacpy(&ev.addr.bdaddr, bdaddr);
6095 ev.addr.type = type;
6096
6097 mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk);
6098}
6099
Marcel Holtmann1904a852015-01-11 13:50:44 -08006100static void remove_device_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006101{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006102 struct mgmt_pending_cmd *cmd;
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006103
6104 BT_DBG("status 0x%02x", status);
6105
6106 hci_dev_lock(hdev);
6107
Johan Hedberg333ae952015-03-17 13:48:47 +02006108 cmd = pending_find(MGMT_OP_REMOVE_DEVICE, hdev);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006109 if (!cmd)
6110 goto unlock;
6111
6112 cmd->cmd_complete(cmd, mgmt_status(status));
6113 mgmt_pending_remove(cmd);
6114
6115unlock:
6116 hci_dev_unlock(hdev);
6117}
6118
Marcel Holtmann2faade52014-06-29 19:44:03 +02006119static int remove_device(struct sock *sk, struct hci_dev *hdev,
6120 void *data, u16 len)
6121{
6122 struct mgmt_cp_remove_device *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006123 struct mgmt_pending_cmd *cmd;
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006124 struct hci_request req;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006125 int err;
6126
6127 BT_DBG("%s", hdev->name);
6128
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006129 hci_req_init(&req, hdev);
6130
Marcel Holtmann2faade52014-06-29 19:44:03 +02006131 hci_dev_lock(hdev);
6132
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006133 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_DEVICE, hdev, data, len);
6134 if (!cmd) {
6135 err = -ENOMEM;
6136 goto unlock;
6137 }
6138
6139 cmd->cmd_complete = addr_cmd_complete;
6140
Marcel Holtmann2faade52014-06-29 19:44:03 +02006141 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
Johan Hedbergc71593d2014-07-02 17:37:28 +03006142 struct hci_conn_params *params;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006143 u8 addr_type;
6144
Johan Hedberg66593582014-07-09 12:59:14 +03006145 if (!bdaddr_type_is_valid(cp->addr.type)) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006146 err = cmd->cmd_complete(cmd,
6147 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006148 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006149 goto unlock;
6150 }
6151
Johan Hedberg66593582014-07-09 12:59:14 +03006152 if (cp->addr.type == BDADDR_BREDR) {
6153 err = hci_bdaddr_list_del(&hdev->whitelist,
6154 &cp->addr.bdaddr,
6155 cp->addr.type);
6156 if (err) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006157 err = cmd->cmd_complete(cmd,
6158 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006159 mgmt_pending_remove(cmd);
Johan Hedberg66593582014-07-09 12:59:14 +03006160 goto unlock;
6161 }
6162
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006163 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03006164
Johan Hedberg66593582014-07-09 12:59:14 +03006165 device_removed(sk, hdev, &cp->addr.bdaddr,
6166 cp->addr.type);
6167 goto complete;
6168 }
6169
Marcel Holtmann2faade52014-06-29 19:44:03 +02006170 if (cp->addr.type == BDADDR_LE_PUBLIC)
6171 addr_type = ADDR_LE_DEV_PUBLIC;
6172 else
6173 addr_type = ADDR_LE_DEV_RANDOM;
6174
Johan Hedbergc71593d2014-07-02 17:37:28 +03006175 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
6176 addr_type);
6177 if (!params) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006178 err = cmd->cmd_complete(cmd,
6179 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006180 mgmt_pending_remove(cmd);
Johan Hedbergc71593d2014-07-02 17:37:28 +03006181 goto unlock;
6182 }
6183
6184 if (params->auto_connect == HCI_AUTO_CONN_DISABLED) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006185 err = cmd->cmd_complete(cmd,
6186 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006187 mgmt_pending_remove(cmd);
Johan Hedbergc71593d2014-07-02 17:37:28 +03006188 goto unlock;
6189 }
6190
Johan Hedbergd1dbf122014-07-04 16:17:23 +03006191 list_del(&params->action);
Johan Hedbergc71593d2014-07-02 17:37:28 +03006192 list_del(&params->list);
6193 kfree(params);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006194 __hci_update_background_scan(&req);
Marcel Holtmann8afef092014-06-29 22:28:34 +02006195
6196 device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006197 } else {
Johan Hedberg19de0822014-07-06 13:06:51 +03006198 struct hci_conn_params *p, *tmp;
Johan Hedberg66593582014-07-09 12:59:14 +03006199 struct bdaddr_list *b, *btmp;
Johan Hedberg19de0822014-07-06 13:06:51 +03006200
Marcel Holtmann2faade52014-06-29 19:44:03 +02006201 if (cp->addr.type) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006202 err = cmd->cmd_complete(cmd,
6203 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006204 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006205 goto unlock;
6206 }
6207
Johan Hedberg66593582014-07-09 12:59:14 +03006208 list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) {
6209 device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type);
6210 list_del(&b->list);
6211 kfree(b);
6212 }
6213
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006214 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03006215
Johan Hedberg19de0822014-07-06 13:06:51 +03006216 list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) {
6217 if (p->auto_connect == HCI_AUTO_CONN_DISABLED)
6218 continue;
6219 device_removed(sk, hdev, &p->addr, p->addr_type);
6220 list_del(&p->action);
6221 list_del(&p->list);
6222 kfree(p);
6223 }
6224
6225 BT_DBG("All LE connection parameters were removed");
6226
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006227 __hci_update_background_scan(&req);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006228 }
6229
Johan Hedberg66593582014-07-09 12:59:14 +03006230complete:
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006231 err = hci_req_run(&req, remove_device_complete);
6232 if (err < 0) {
6233 /* ENODATA means no HCI commands were needed (e.g. if
6234 * the adapter is powered off).
6235 */
Johan Hedberg9df74652014-12-19 22:26:03 +02006236 if (err == -ENODATA)
6237 err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006238 mgmt_pending_remove(cmd);
6239 }
Marcel Holtmann2faade52014-06-29 19:44:03 +02006240
6241unlock:
6242 hci_dev_unlock(hdev);
6243 return err;
6244}
6245
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006246static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
6247 u16 len)
6248{
6249 struct mgmt_cp_load_conn_param *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03006250 const u16 max_param_count = ((U16_MAX - sizeof(*cp)) /
6251 sizeof(struct mgmt_conn_param));
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006252 u16 param_count, expected_len;
6253 int i;
6254
6255 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006256 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6257 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006258
6259 param_count = __le16_to_cpu(cp->param_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03006260 if (param_count > max_param_count) {
6261 BT_ERR("load_conn_param: too big param_count value %u",
6262 param_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02006263 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6264 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03006265 }
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006266
6267 expected_len = sizeof(*cp) + param_count *
6268 sizeof(struct mgmt_conn_param);
6269 if (expected_len != len) {
6270 BT_ERR("load_conn_param: expected %u bytes, got %u bytes",
6271 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02006272 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6273 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006274 }
6275
6276 BT_DBG("%s param_count %u", hdev->name, param_count);
6277
6278 hci_dev_lock(hdev);
6279
6280 hci_conn_params_clear_disabled(hdev);
6281
6282 for (i = 0; i < param_count; i++) {
6283 struct mgmt_conn_param *param = &cp->params[i];
6284 struct hci_conn_params *hci_param;
6285 u16 min, max, latency, timeout;
6286 u8 addr_type;
6287
6288 BT_DBG("Adding %pMR (type %u)", &param->addr.bdaddr,
6289 param->addr.type);
6290
6291 if (param->addr.type == BDADDR_LE_PUBLIC) {
6292 addr_type = ADDR_LE_DEV_PUBLIC;
6293 } else if (param->addr.type == BDADDR_LE_RANDOM) {
6294 addr_type = ADDR_LE_DEV_RANDOM;
6295 } else {
6296 BT_ERR("Ignoring invalid connection parameters");
6297 continue;
6298 }
6299
6300 min = le16_to_cpu(param->min_interval);
6301 max = le16_to_cpu(param->max_interval);
6302 latency = le16_to_cpu(param->latency);
6303 timeout = le16_to_cpu(param->timeout);
6304
6305 BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x",
6306 min, max, latency, timeout);
6307
6308 if (hci_check_conn_params(min, max, latency, timeout) < 0) {
6309 BT_ERR("Ignoring invalid connection parameters");
6310 continue;
6311 }
6312
6313 hci_param = hci_conn_params_add(hdev, &param->addr.bdaddr,
6314 addr_type);
6315 if (!hci_param) {
6316 BT_ERR("Failed to add connection parameters");
6317 continue;
6318 }
6319
6320 hci_param->conn_min_interval = min;
6321 hci_param->conn_max_interval = max;
6322 hci_param->conn_latency = latency;
6323 hci_param->supervision_timeout = timeout;
6324 }
6325
6326 hci_dev_unlock(hdev);
6327
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006328 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0,
6329 NULL, 0);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006330}
6331
Marcel Holtmanndbece372014-07-04 18:11:55 +02006332static int set_external_config(struct sock *sk, struct hci_dev *hdev,
6333 void *data, u16 len)
6334{
6335 struct mgmt_cp_set_external_config *cp = data;
6336 bool changed;
6337 int err;
6338
6339 BT_DBG("%s", hdev->name);
6340
6341 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006342 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6343 MGMT_STATUS_REJECTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006344
6345 if (cp->config != 0x00 && cp->config != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02006346 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6347 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006348
6349 if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
Johan Hedberga69e8372015-03-06 21:08:53 +02006350 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6351 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006352
6353 hci_dev_lock(hdev);
6354
6355 if (cp->config)
Marcel Holtmann238be782015-03-13 02:11:06 -07006356 changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006357 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07006358 changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006359
6360 err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
6361 if (err < 0)
6362 goto unlock;
6363
6364 if (!changed)
6365 goto unlock;
6366
Marcel Holtmannf4537c02014-07-04 19:06:23 +02006367 err = new_options(hdev, sk);
6368
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006369 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) {
Marcel Holtmanndbece372014-07-04 18:11:55 +02006370 mgmt_index_removed(hdev);
Marcel Holtmannd603b762014-07-06 12:11:14 +02006371
Marcel Holtmann516018a2015-03-13 02:11:04 -07006372 if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006373 hci_dev_set_flag(hdev, HCI_CONFIG);
6374 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmannd603b762014-07-06 12:11:14 +02006375
6376 queue_work(hdev->req_workqueue, &hdev->power_on);
6377 } else {
Marcel Holtmann5ea234d2014-07-06 12:11:16 +02006378 set_bit(HCI_RAW, &hdev->flags);
Marcel Holtmannd603b762014-07-06 12:11:14 +02006379 mgmt_index_added(hdev);
6380 }
Marcel Holtmanndbece372014-07-04 18:11:55 +02006381 }
6382
6383unlock:
6384 hci_dev_unlock(hdev);
6385 return err;
6386}
6387
Marcel Holtmann9713c172014-07-06 12:11:15 +02006388static int set_public_address(struct sock *sk, struct hci_dev *hdev,
6389 void *data, u16 len)
6390{
6391 struct mgmt_cp_set_public_address *cp = data;
6392 bool changed;
6393 int err;
6394
6395 BT_DBG("%s", hdev->name);
6396
6397 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006398 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6399 MGMT_STATUS_REJECTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006400
6401 if (!bacmp(&cp->bdaddr, BDADDR_ANY))
Johan Hedberga69e8372015-03-06 21:08:53 +02006402 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6403 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006404
6405 if (!hdev->set_bdaddr)
Johan Hedberga69e8372015-03-06 21:08:53 +02006406 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6407 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006408
6409 hci_dev_lock(hdev);
6410
6411 changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
6412 bacpy(&hdev->public_addr, &cp->bdaddr);
6413
6414 err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev);
6415 if (err < 0)
6416 goto unlock;
6417
6418 if (!changed)
6419 goto unlock;
6420
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006421 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
Marcel Holtmann9713c172014-07-06 12:11:15 +02006422 err = new_options(hdev, sk);
6423
6424 if (is_configured(hdev)) {
6425 mgmt_index_removed(hdev);
6426
Marcel Holtmanna358dc12015-03-13 02:11:02 -07006427 hci_dev_clear_flag(hdev, HCI_UNCONFIGURED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006428
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006429 hci_dev_set_flag(hdev, HCI_CONFIG);
6430 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006431
6432 queue_work(hdev->req_workqueue, &hdev->power_on);
6433 }
6434
6435unlock:
6436 hci_dev_unlock(hdev);
6437 return err;
6438}
6439
Marcel Holtmannbea41602015-03-14 22:43:17 -07006440static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
6441 u8 data_len)
6442{
6443 eir[eir_len++] = sizeof(type) + data_len;
6444 eir[eir_len++] = type;
6445 memcpy(&eir[eir_len], data, data_len);
6446 eir_len += data_len;
6447
6448 return eir_len;
6449}
6450
Johan Hedberg40f66c02015-04-07 21:52:22 +03006451static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status,
6452 u16 opcode, struct sk_buff *skb)
6453{
6454 const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp;
6455 struct mgmt_rp_read_local_oob_ext_data *mgmt_rp;
6456 u8 *h192, *r192, *h256, *r256;
6457 struct mgmt_pending_cmd *cmd;
6458 u16 eir_len;
6459 int err;
6460
6461 BT_DBG("%s status %u", hdev->name, status);
6462
6463 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev);
6464 if (!cmd)
6465 return;
6466
6467 mgmt_cp = cmd->param;
6468
6469 if (status) {
6470 status = mgmt_status(status);
6471 eir_len = 0;
6472
6473 h192 = NULL;
6474 r192 = NULL;
6475 h256 = NULL;
6476 r256 = NULL;
6477 } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
6478 struct hci_rp_read_local_oob_data *rp;
6479
6480 if (skb->len != sizeof(*rp)) {
6481 status = MGMT_STATUS_FAILED;
6482 eir_len = 0;
6483 } else {
6484 status = MGMT_STATUS_SUCCESS;
6485 rp = (void *)skb->data;
6486
6487 eir_len = 5 + 18 + 18;
6488 h192 = rp->hash;
6489 r192 = rp->rand;
6490 h256 = NULL;
6491 r256 = NULL;
6492 }
6493 } else {
6494 struct hci_rp_read_local_oob_ext_data *rp;
6495
6496 if (skb->len != sizeof(*rp)) {
6497 status = MGMT_STATUS_FAILED;
6498 eir_len = 0;
6499 } else {
6500 status = MGMT_STATUS_SUCCESS;
6501 rp = (void *)skb->data;
6502
6503 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
6504 eir_len = 5 + 18 + 18;
6505 h192 = NULL;
6506 r192 = NULL;
6507 } else {
6508 eir_len = 5 + 18 + 18 + 18 + 18;
6509 h192 = rp->hash192;
6510 r192 = rp->rand192;
6511 }
6512
6513 h256 = rp->hash256;
6514 r256 = rp->rand256;
6515 }
6516 }
6517
6518 mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL);
6519 if (!mgmt_rp)
6520 goto done;
6521
6522 if (status)
6523 goto send_rsp;
6524
6525 eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV,
6526 hdev->dev_class, 3);
6527
6528 if (h192 && r192) {
6529 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6530 EIR_SSP_HASH_C192, h192, 16);
6531 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6532 EIR_SSP_RAND_R192, r192, 16);
6533 }
6534
6535 if (h256 && r256) {
6536 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6537 EIR_SSP_HASH_C256, h256, 16);
6538 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6539 EIR_SSP_RAND_R256, r256, 16);
6540 }
6541
6542send_rsp:
6543 mgmt_rp->type = mgmt_cp->type;
6544 mgmt_rp->eir_len = cpu_to_le16(eir_len);
6545
6546 err = mgmt_cmd_complete(cmd->sk, hdev->id,
6547 MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status,
6548 mgmt_rp, sizeof(*mgmt_rp) + eir_len);
6549 if (err < 0 || status)
6550 goto done;
6551
6552 hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS);
6553
6554 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
6555 mgmt_rp, sizeof(*mgmt_rp) + eir_len,
6556 HCI_MGMT_OOB_DATA_EVENTS, cmd->sk);
6557done:
6558 kfree(mgmt_rp);
6559 mgmt_pending_remove(cmd);
6560}
6561
6562static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk,
6563 struct mgmt_cp_read_local_oob_ext_data *cp)
6564{
6565 struct mgmt_pending_cmd *cmd;
6566 struct hci_request req;
6567 int err;
6568
6569 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev,
6570 cp, sizeof(*cp));
6571 if (!cmd)
6572 return -ENOMEM;
6573
6574 hci_req_init(&req, hdev);
6575
6576 if (bredr_sc_enabled(hdev))
6577 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
6578 else
6579 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
6580
6581 err = hci_req_run_skb(&req, read_local_oob_ext_data_complete);
6582 if (err < 0) {
6583 mgmt_pending_remove(cmd);
6584 return err;
6585 }
6586
6587 return 0;
6588}
6589
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006590static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
6591 void *data, u16 data_len)
6592{
6593 struct mgmt_cp_read_local_oob_ext_data *cp = data;
6594 struct mgmt_rp_read_local_oob_ext_data *rp;
6595 size_t rp_len;
6596 u16 eir_len;
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006597 u8 status, flags, role, addr[7], hash[16], rand[16];
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006598 int err;
6599
6600 BT_DBG("%s", hdev->name);
6601
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07006602 if (hdev_is_powered(hdev)) {
6603 switch (cp->type) {
6604 case BIT(BDADDR_BREDR):
6605 status = mgmt_bredr_support(hdev);
6606 if (status)
6607 eir_len = 0;
6608 else
6609 eir_len = 5;
6610 break;
6611 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
6612 status = mgmt_le_support(hdev);
6613 if (status)
6614 eir_len = 0;
6615 else
6616 eir_len = 9 + 3 + 18 + 18 + 3;
6617 break;
6618 default:
6619 status = MGMT_STATUS_INVALID_PARAMS;
6620 eir_len = 0;
6621 break;
6622 }
6623 } else {
6624 status = MGMT_STATUS_NOT_POWERED;
6625 eir_len = 0;
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006626 }
6627
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006628 rp_len = sizeof(*rp) + eir_len;
6629 rp = kmalloc(rp_len, GFP_ATOMIC);
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07006630 if (!rp)
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006631 return -ENOMEM;
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07006632
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07006633 if (status)
6634 goto complete;
6635
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07006636 hci_dev_lock(hdev);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006637
6638 eir_len = 0;
6639 switch (cp->type) {
6640 case BIT(BDADDR_BREDR):
Johan Hedberg40f66c02015-04-07 21:52:22 +03006641 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
6642 err = read_local_ssp_oob_req(hdev, sk, cp);
6643 hci_dev_unlock(hdev);
6644 if (!err)
6645 goto done;
6646
6647 status = MGMT_STATUS_FAILED;
6648 goto complete;
6649 } else {
6650 eir_len = eir_append_data(rp->eir, eir_len,
6651 EIR_CLASS_OF_DEV,
6652 hdev->dev_class, 3);
6653 }
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006654 break;
6655 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
Marcel Holtmann5082a592015-03-16 12:39:00 -07006656 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
6657 smp_generate_oob(hdev, hash, rand) < 0) {
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006658 hci_dev_unlock(hdev);
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07006659 status = MGMT_STATUS_FAILED;
6660 goto complete;
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006661 }
6662
Marcel Holtmanne2135682015-04-02 12:00:58 -07006663 /* This should return the active RPA, but since the RPA
6664 * is only programmed on demand, it is really hard to fill
6665 * this in at the moment. For now disallow retrieving
6666 * local out-of-band data when privacy is in use.
6667 *
6668 * Returning the identity address will not help here since
6669 * pairing happens before the identity resolving key is
6670 * known and thus the connection establishment happens
6671 * based on the RPA and not the identity address.
6672 */
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006673 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
Marcel Holtmanne2135682015-04-02 12:00:58 -07006674 hci_dev_unlock(hdev);
6675 status = MGMT_STATUS_REJECTED;
6676 goto complete;
6677 }
6678
6679 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
6680 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
6681 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
6682 bacmp(&hdev->static_addr, BDADDR_ANY))) {
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006683 memcpy(addr, &hdev->static_addr, 6);
6684 addr[6] = 0x01;
6685 } else {
6686 memcpy(addr, &hdev->bdaddr, 6);
6687 addr[6] = 0x00;
6688 }
6689
6690 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR,
6691 addr, sizeof(addr));
6692
6693 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
6694 role = 0x02;
6695 else
6696 role = 0x01;
6697
6698 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE,
6699 &role, sizeof(role));
6700
Marcel Holtmann5082a592015-03-16 12:39:00 -07006701 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) {
6702 eir_len = eir_append_data(rp->eir, eir_len,
6703 EIR_LE_SC_CONFIRM,
6704 hash, sizeof(hash));
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006705
Marcel Holtmann5082a592015-03-16 12:39:00 -07006706 eir_len = eir_append_data(rp->eir, eir_len,
6707 EIR_LE_SC_RANDOM,
6708 rand, sizeof(rand));
6709 }
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006710
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006711 flags = get_adv_discov_flags(hdev);
6712
6713 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
6714 flags |= LE_AD_NO_BREDR;
6715
6716 eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS,
6717 &flags, sizeof(flags));
6718 break;
6719 }
6720
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006721 hci_dev_unlock(hdev);
6722
Marcel Holtmann72000df2015-03-16 16:11:21 -07006723 hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS);
6724
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07006725 status = MGMT_STATUS_SUCCESS;
6726
6727complete:
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07006728 rp->type = cp->type;
6729 rp->eir_len = cpu_to_le16(eir_len);
6730
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006731 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07006732 status, rp, sizeof(*rp) + eir_len);
6733 if (err < 0 || status)
Marcel Holtmann72000df2015-03-16 16:11:21 -07006734 goto done;
6735
6736 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
6737 rp, sizeof(*rp) + eir_len,
6738 HCI_MGMT_OOB_DATA_EVENTS, sk);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006739
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006740done:
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006741 kfree(rp);
6742
6743 return err;
6744}
6745
Arman Uguray089fa8c2015-03-25 18:53:45 -07006746static u32 get_supported_adv_flags(struct hci_dev *hdev)
6747{
6748 u32 flags = 0;
6749
6750 flags |= MGMT_ADV_FLAG_CONNECTABLE;
6751 flags |= MGMT_ADV_FLAG_DISCOV;
6752 flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
6753 flags |= MGMT_ADV_FLAG_MANAGED_FLAGS;
6754
6755 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID)
6756 flags |= MGMT_ADV_FLAG_TX_POWER;
6757
6758 return flags;
6759}
6760
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006761static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
6762 void *data, u16 data_len)
6763{
6764 struct mgmt_rp_read_adv_features *rp;
6765 size_t rp_len;
6766 int err;
Arman Uguray24b4f382015-03-23 15:57:12 -07006767 bool instance;
Arman Uguray089fa8c2015-03-25 18:53:45 -07006768 u32 supported_flags;
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006769
6770 BT_DBG("%s", hdev->name);
6771
Arman Uguray089fa8c2015-03-25 18:53:45 -07006772 if (!lmp_le_capable(hdev))
6773 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
6774 MGMT_STATUS_REJECTED);
6775
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006776 hci_dev_lock(hdev);
6777
6778 rp_len = sizeof(*rp);
Arman Uguray24b4f382015-03-23 15:57:12 -07006779
6780 /* Currently only one instance is supported, so just add 1 to the
6781 * response length.
6782 */
6783 instance = hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE);
6784 if (instance)
6785 rp_len++;
6786
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006787 rp = kmalloc(rp_len, GFP_ATOMIC);
6788 if (!rp) {
6789 hci_dev_unlock(hdev);
6790 return -ENOMEM;
6791 }
6792
Arman Uguray089fa8c2015-03-25 18:53:45 -07006793 supported_flags = get_supported_adv_flags(hdev);
6794
6795 rp->supported_flags = cpu_to_le32(supported_flags);
Marcel Holtmanndc5d82a2015-03-19 17:22:25 -07006796 rp->max_adv_data_len = HCI_MAX_AD_LENGTH;
6797 rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH;
Florian Grandeld2609b32015-06-18 03:16:34 +02006798 rp->max_instances = HCI_MAX_ADV_INSTANCES;
Arman Uguray24b4f382015-03-23 15:57:12 -07006799
6800 /* Currently only one instance is supported, so simply return the
6801 * current instance number.
6802 */
6803 if (instance) {
6804 rp->num_instances = 1;
6805 rp->instance[0] = 1;
6806 } else {
6807 rp->num_instances = 0;
6808 }
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006809
6810 hci_dev_unlock(hdev);
6811
6812 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
6813 MGMT_STATUS_SUCCESS, rp, rp_len);
6814
6815 kfree(rp);
6816
6817 return err;
6818}
6819
Arman Uguray4117ed72015-03-23 15:57:14 -07006820static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data,
Arman Ugurayb44133f2015-03-25 18:53:41 -07006821 u8 len, bool is_adv_data)
Arman Uguray24b4f382015-03-23 15:57:12 -07006822{
Arman Uguray4117ed72015-03-23 15:57:14 -07006823 u8 max_len = HCI_MAX_AD_LENGTH;
Arman Uguray24b4f382015-03-23 15:57:12 -07006824 int i, cur_len;
Arman Ugurayb44133f2015-03-25 18:53:41 -07006825 bool flags_managed = false;
Arman Uguray5507e352015-03-25 18:53:44 -07006826 bool tx_power_managed = false;
Arman Uguray67e0c0c2015-03-25 18:53:43 -07006827 u32 flags_params = MGMT_ADV_FLAG_DISCOV | MGMT_ADV_FLAG_LIMITED_DISCOV |
6828 MGMT_ADV_FLAG_MANAGED_FLAGS;
Arman Uguray24b4f382015-03-23 15:57:12 -07006829
Arman Uguray807ec772015-03-25 18:53:42 -07006830 if (is_adv_data && (adv_flags & flags_params)) {
Arman Ugurayb44133f2015-03-25 18:53:41 -07006831 flags_managed = true;
6832 max_len -= 3;
6833 }
Arman Uguray24b4f382015-03-23 15:57:12 -07006834
Arman Uguray5507e352015-03-25 18:53:44 -07006835 if (is_adv_data && (adv_flags & MGMT_ADV_FLAG_TX_POWER)) {
6836 tx_power_managed = true;
6837 max_len -= 3;
6838 }
6839
Arman Uguray4117ed72015-03-23 15:57:14 -07006840 if (len > max_len)
Arman Uguray24b4f382015-03-23 15:57:12 -07006841 return false;
6842
Arman Uguray4117ed72015-03-23 15:57:14 -07006843 /* Make sure that the data is correctly formatted. */
6844 for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) {
6845 cur_len = data[i];
Arman Uguray24b4f382015-03-23 15:57:12 -07006846
Arman Ugurayb44133f2015-03-25 18:53:41 -07006847 if (flags_managed && data[i + 1] == EIR_FLAGS)
6848 return false;
6849
Arman Uguray5507e352015-03-25 18:53:44 -07006850 if (tx_power_managed && data[i + 1] == EIR_TX_POWER)
6851 return false;
6852
Arman Uguray24b4f382015-03-23 15:57:12 -07006853 /* If the current field length would exceed the total data
6854 * length, then it's invalid.
6855 */
Arman Uguray4117ed72015-03-23 15:57:14 -07006856 if (i + cur_len >= len)
Arman Uguray24b4f382015-03-23 15:57:12 -07006857 return false;
6858 }
6859
6860 return true;
6861}
6862
Arman Uguray24b4f382015-03-23 15:57:12 -07006863static void add_advertising_complete(struct hci_dev *hdev, u8 status,
6864 u16 opcode)
6865{
6866 struct mgmt_pending_cmd *cmd;
6867 struct mgmt_rp_add_advertising rp;
6868
6869 BT_DBG("status %d", status);
6870
6871 hci_dev_lock(hdev);
6872
6873 cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev);
6874
6875 if (status) {
6876 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
6877 memset(&hdev->adv_instance, 0, sizeof(hdev->adv_instance));
6878 advertising_removed(cmd ? cmd->sk : NULL, hdev, 1);
6879 }
6880
6881 if (!cmd)
6882 goto unlock;
6883
6884 rp.instance = 0x01;
6885
6886 if (status)
6887 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
6888 mgmt_status(status));
6889 else
6890 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
6891 mgmt_status(status), &rp, sizeof(rp));
6892
6893 mgmt_pending_remove(cmd);
6894
6895unlock:
6896 hci_dev_unlock(hdev);
6897}
6898
Florian Grandel5d900e42015-06-18 03:16:35 +02006899void mgmt_adv_timeout_expired(struct hci_dev *hdev)
Arman Uguray912098a2015-03-23 15:57:15 -07006900{
Florian Grandel5d900e42015-06-18 03:16:35 +02006901 hdev->adv_instance_timeout = 0;
Arman Uguray912098a2015-03-23 15:57:15 -07006902
6903 hci_dev_lock(hdev);
6904 clear_adv_instance(hdev);
6905 hci_dev_unlock(hdev);
6906}
6907
Arman Uguray24b4f382015-03-23 15:57:12 -07006908static int add_advertising(struct sock *sk, struct hci_dev *hdev,
6909 void *data, u16 data_len)
6910{
6911 struct mgmt_cp_add_advertising *cp = data;
6912 struct mgmt_rp_add_advertising rp;
6913 u32 flags;
Arman Uguray089fa8c2015-03-25 18:53:45 -07006914 u32 supported_flags;
Arman Uguray24b4f382015-03-23 15:57:12 -07006915 u8 status;
Arman Uguray912098a2015-03-23 15:57:15 -07006916 u16 timeout;
Arman Uguray24b4f382015-03-23 15:57:12 -07006917 int err;
6918 struct mgmt_pending_cmd *cmd;
6919 struct hci_request req;
6920
6921 BT_DBG("%s", hdev->name);
6922
6923 status = mgmt_le_support(hdev);
6924 if (status)
6925 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6926 status);
6927
6928 flags = __le32_to_cpu(cp->flags);
Arman Uguray912098a2015-03-23 15:57:15 -07006929 timeout = __le16_to_cpu(cp->timeout);
Arman Uguray24b4f382015-03-23 15:57:12 -07006930
Arman Uguray089fa8c2015-03-25 18:53:45 -07006931 /* The current implementation only supports adding one instance and only
6932 * a subset of the specified flags.
6933 */
6934 supported_flags = get_supported_adv_flags(hdev);
6935 if (cp->instance != 0x01 || (flags & ~supported_flags))
Arman Uguray24b4f382015-03-23 15:57:12 -07006936 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6937 MGMT_STATUS_INVALID_PARAMS);
6938
6939 hci_dev_lock(hdev);
6940
Arman Uguray912098a2015-03-23 15:57:15 -07006941 if (timeout && !hdev_is_powered(hdev)) {
6942 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6943 MGMT_STATUS_REJECTED);
6944 goto unlock;
6945 }
6946
Arman Uguray24b4f382015-03-23 15:57:12 -07006947 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
Arman Ugurayda9293352015-03-23 15:57:13 -07006948 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
Arman Uguray24b4f382015-03-23 15:57:12 -07006949 pending_find(MGMT_OP_SET_LE, hdev)) {
6950 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6951 MGMT_STATUS_BUSY);
6952 goto unlock;
6953 }
6954
Arman Ugurayb44133f2015-03-25 18:53:41 -07006955 if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) ||
Arman Uguray4117ed72015-03-23 15:57:14 -07006956 !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len,
Arman Ugurayb44133f2015-03-25 18:53:41 -07006957 cp->scan_rsp_len, false)) {
Arman Uguray24b4f382015-03-23 15:57:12 -07006958 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6959 MGMT_STATUS_INVALID_PARAMS);
6960 goto unlock;
6961 }
6962
6963 hdev->adv_instance.flags = flags;
6964 hdev->adv_instance.adv_data_len = cp->adv_data_len;
6965 hdev->adv_instance.scan_rsp_len = cp->scan_rsp_len;
6966
6967 if (cp->adv_data_len)
6968 memcpy(hdev->adv_instance.adv_data, cp->data, cp->adv_data_len);
6969
6970 if (cp->scan_rsp_len)
6971 memcpy(hdev->adv_instance.scan_rsp_data,
6972 cp->data + cp->adv_data_len, cp->scan_rsp_len);
6973
Florian Grandel5d900e42015-06-18 03:16:35 +02006974 if (hdev->adv_instance_timeout)
6975 cancel_delayed_work(&hdev->adv_instance_expire);
Arman Uguray912098a2015-03-23 15:57:15 -07006976
Florian Grandel5d900e42015-06-18 03:16:35 +02006977 hdev->adv_instance_timeout = timeout;
Arman Uguray912098a2015-03-23 15:57:15 -07006978
6979 if (timeout)
6980 queue_delayed_work(hdev->workqueue,
Florian Grandel5d900e42015-06-18 03:16:35 +02006981 &hdev->adv_instance_expire,
Arman Uguray912098a2015-03-23 15:57:15 -07006982 msecs_to_jiffies(timeout * 1000));
6983
Arman Uguray24b4f382015-03-23 15:57:12 -07006984 if (!hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING_INSTANCE))
6985 advertising_added(sk, hdev, 1);
6986
6987 /* If the HCI_ADVERTISING flag is set or the device isn't powered then
6988 * we have no HCI communication to make. Simply return.
6989 */
6990 if (!hdev_is_powered(hdev) ||
6991 hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
6992 rp.instance = 0x01;
6993 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6994 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
6995 goto unlock;
6996 }
6997
6998 /* We're good to go, update advertising data, parameters, and start
6999 * advertising.
7000 */
7001 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data,
7002 data_len);
7003 if (!cmd) {
7004 err = -ENOMEM;
7005 goto unlock;
7006 }
7007
7008 hci_req_init(&req, hdev);
7009
7010 update_adv_data(&req);
Arman Uguray4117ed72015-03-23 15:57:14 -07007011 update_scan_rsp_data(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07007012 enable_advertising(&req);
7013
7014 err = hci_req_run(&req, add_advertising_complete);
7015 if (err < 0)
7016 mgmt_pending_remove(cmd);
7017
7018unlock:
7019 hci_dev_unlock(hdev);
7020
7021 return err;
7022}
7023
Arman Ugurayda9293352015-03-23 15:57:13 -07007024static void remove_advertising_complete(struct hci_dev *hdev, u8 status,
7025 u16 opcode)
7026{
7027 struct mgmt_pending_cmd *cmd;
7028 struct mgmt_rp_remove_advertising rp;
7029
7030 BT_DBG("status %d", status);
7031
7032 hci_dev_lock(hdev);
7033
7034 /* A failure status here only means that we failed to disable
7035 * advertising. Otherwise, the advertising instance has been removed,
7036 * so report success.
7037 */
7038 cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev);
7039 if (!cmd)
7040 goto unlock;
7041
7042 rp.instance = 1;
7043
7044 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS,
7045 &rp, sizeof(rp));
7046 mgmt_pending_remove(cmd);
7047
7048unlock:
7049 hci_dev_unlock(hdev);
7050}
7051
7052static int remove_advertising(struct sock *sk, struct hci_dev *hdev,
7053 void *data, u16 data_len)
7054{
7055 struct mgmt_cp_remove_advertising *cp = data;
7056 struct mgmt_rp_remove_advertising rp;
7057 int err;
7058 struct mgmt_pending_cmd *cmd;
7059 struct hci_request req;
7060
7061 BT_DBG("%s", hdev->name);
7062
7063 /* The current implementation only allows modifying instance no 1. A
7064 * value of 0 indicates that all instances should be cleared.
7065 */
7066 if (cp->instance > 1)
7067 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
7068 MGMT_STATUS_INVALID_PARAMS);
7069
7070 hci_dev_lock(hdev);
7071
7072 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
7073 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
7074 pending_find(MGMT_OP_SET_LE, hdev)) {
7075 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
7076 MGMT_STATUS_BUSY);
7077 goto unlock;
7078 }
7079
7080 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE)) {
7081 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
7082 MGMT_STATUS_INVALID_PARAMS);
7083 goto unlock;
7084 }
7085
Florian Grandel5d900e42015-06-18 03:16:35 +02007086 if (hdev->adv_instance_timeout)
7087 cancel_delayed_work(&hdev->adv_instance_expire);
Arman Uguray912098a2015-03-23 15:57:15 -07007088
Arman Ugurayda9293352015-03-23 15:57:13 -07007089 memset(&hdev->adv_instance, 0, sizeof(hdev->adv_instance));
7090
7091 advertising_removed(sk, hdev, 1);
7092
7093 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
7094
7095 /* If the HCI_ADVERTISING flag is set or the device isn't powered then
7096 * we have no HCI communication to make. Simply return.
7097 */
7098 if (!hdev_is_powered(hdev) ||
7099 hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
7100 rp.instance = 1;
7101 err = mgmt_cmd_complete(sk, hdev->id,
7102 MGMT_OP_REMOVE_ADVERTISING,
7103 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
7104 goto unlock;
7105 }
7106
7107 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data,
7108 data_len);
7109 if (!cmd) {
7110 err = -ENOMEM;
7111 goto unlock;
7112 }
7113
7114 hci_req_init(&req, hdev);
7115 disable_advertising(&req);
7116
7117 err = hci_req_run(&req, remove_advertising_complete);
7118 if (err < 0)
7119 mgmt_pending_remove(cmd);
7120
7121unlock:
7122 hci_dev_unlock(hdev);
7123
7124 return err;
7125}
7126
Johan Hedberg6d785aa32015-03-06 21:08:51 +02007127static const struct hci_mgmt_handler mgmt_handlers[] = {
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02007128 { NULL }, /* 0x0000 (no command) */
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007129 { read_version, MGMT_READ_VERSION_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007130 HCI_MGMT_NO_HDEV |
7131 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007132 { read_commands, MGMT_READ_COMMANDS_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007133 HCI_MGMT_NO_HDEV |
7134 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007135 { read_index_list, MGMT_READ_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007136 HCI_MGMT_NO_HDEV |
7137 HCI_MGMT_UNTRUSTED },
7138 { read_controller_info, MGMT_READ_INFO_SIZE,
7139 HCI_MGMT_UNTRUSTED },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07007140 { set_powered, MGMT_SETTING_SIZE },
7141 { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE },
7142 { set_connectable, MGMT_SETTING_SIZE },
7143 { set_fast_connectable, MGMT_SETTING_SIZE },
7144 { set_bondable, MGMT_SETTING_SIZE },
7145 { set_link_security, MGMT_SETTING_SIZE },
7146 { set_ssp, MGMT_SETTING_SIZE },
7147 { set_hs, MGMT_SETTING_SIZE },
7148 { set_le, MGMT_SETTING_SIZE },
7149 { set_dev_class, MGMT_SET_DEV_CLASS_SIZE },
7150 { set_local_name, MGMT_SET_LOCAL_NAME_SIZE },
7151 { add_uuid, MGMT_ADD_UUID_SIZE },
7152 { remove_uuid, MGMT_REMOVE_UUID_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007153 { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE,
7154 HCI_MGMT_VAR_LEN },
7155 { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE,
7156 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07007157 { disconnect, MGMT_DISCONNECT_SIZE },
7158 { get_connections, MGMT_GET_CONNECTIONS_SIZE },
7159 { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE },
7160 { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE },
7161 { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE },
7162 { pair_device, MGMT_PAIR_DEVICE_SIZE },
7163 { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE },
7164 { unpair_device, MGMT_UNPAIR_DEVICE_SIZE },
7165 { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE },
7166 { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
7167 { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE },
7168 { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007169 { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE },
7170 { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE,
7171 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07007172 { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
7173 { start_discovery, MGMT_START_DISCOVERY_SIZE },
7174 { stop_discovery, MGMT_STOP_DISCOVERY_SIZE },
7175 { confirm_name, MGMT_CONFIRM_NAME_SIZE },
7176 { block_device, MGMT_BLOCK_DEVICE_SIZE },
7177 { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE },
7178 { set_device_id, MGMT_SET_DEVICE_ID_SIZE },
7179 { set_advertising, MGMT_SETTING_SIZE },
7180 { set_bredr, MGMT_SETTING_SIZE },
7181 { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE },
7182 { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE },
7183 { set_secure_conn, MGMT_SETTING_SIZE },
7184 { set_debug_keys, MGMT_SETTING_SIZE },
7185 { set_privacy, MGMT_SET_PRIVACY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007186 { load_irks, MGMT_LOAD_IRKS_SIZE,
7187 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07007188 { get_conn_info, MGMT_GET_CONN_INFO_SIZE },
7189 { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE },
7190 { add_device, MGMT_ADD_DEVICE_SIZE },
7191 { remove_device, MGMT_REMOVE_DEVICE_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007192 { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE,
7193 HCI_MGMT_VAR_LEN },
7194 { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007195 HCI_MGMT_NO_HDEV |
7196 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007197 { read_config_info, MGMT_READ_CONFIG_INFO_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007198 HCI_MGMT_UNCONFIGURED |
7199 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007200 { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE,
7201 HCI_MGMT_UNCONFIGURED },
7202 { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE,
7203 HCI_MGMT_UNCONFIGURED },
7204 { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE,
7205 HCI_MGMT_VAR_LEN },
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007206 { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE },
Marcel Holtmann96f14742015-03-14 19:27:57 -07007207 { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007208 HCI_MGMT_NO_HDEV |
7209 HCI_MGMT_UNTRUSTED },
Marcel Holtmannd3d53052015-03-14 20:53:25 -07007210 { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE },
Arman Uguray24b4f382015-03-23 15:57:12 -07007211 { add_advertising, MGMT_ADD_ADVERTISING_SIZE,
7212 HCI_MGMT_VAR_LEN },
Arman Ugurayda9293352015-03-23 15:57:13 -07007213 { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE },
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02007214};
7215
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07007216void mgmt_index_added(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02007217{
Marcel Holtmannced85542015-03-14 19:27:56 -07007218 struct mgmt_ev_ext_index ev;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +03007219
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02007220 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
7221 return;
7222
Marcel Holtmannf9207332015-03-14 19:27:55 -07007223 switch (hdev->dev_type) {
7224 case HCI_BREDR:
7225 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
7226 mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev,
7227 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007228 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007229 } else {
7230 mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0,
7231 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007232 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007233 }
7234 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07007235 case HCI_AMP:
7236 ev.type = 0x02;
7237 break;
7238 default:
7239 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007240 }
Marcel Holtmannced85542015-03-14 19:27:56 -07007241
7242 ev.bus = hdev->bus;
7243
7244 mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev),
7245 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergc71e97b2010-12-13 21:07:07 +02007246}
7247
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07007248void mgmt_index_removed(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02007249{
Marcel Holtmannced85542015-03-14 19:27:56 -07007250 struct mgmt_ev_ext_index ev;
Johan Hedberg5f159032012-03-02 03:13:19 +02007251 u8 status = MGMT_STATUS_INVALID_INDEX;
Johan Hedbergb24752f2011-11-03 14:40:33 +02007252
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02007253 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
7254 return;
7255
Marcel Holtmannf9207332015-03-14 19:27:55 -07007256 switch (hdev->dev_type) {
7257 case HCI_BREDR:
7258 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedbergb24752f2011-11-03 14:40:33 +02007259
Marcel Holtmannf9207332015-03-14 19:27:55 -07007260 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
7261 mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev,
7262 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007263 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007264 } else {
7265 mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0,
7266 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007267 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007268 }
7269 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07007270 case HCI_AMP:
7271 ev.type = 0x02;
7272 break;
7273 default:
7274 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007275 }
Marcel Holtmannced85542015-03-14 19:27:56 -07007276
7277 ev.bus = hdev->bus;
7278
7279 mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev),
7280 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02007281}
7282
Andre Guedes6046dc32014-02-26 20:21:51 -03007283/* This function requires the caller holds hdev->lock */
Johan Hedberg2cf22212014-12-19 22:26:00 +02007284static void restart_le_actions(struct hci_request *req)
Andre Guedes6046dc32014-02-26 20:21:51 -03007285{
Johan Hedberg2cf22212014-12-19 22:26:00 +02007286 struct hci_dev *hdev = req->hdev;
Andre Guedes6046dc32014-02-26 20:21:51 -03007287 struct hci_conn_params *p;
7288
7289 list_for_each_entry(p, &hdev->le_conn_params, list) {
Johan Hedbergd7347f32014-07-04 12:37:23 +03007290 /* Needed for AUTO_OFF case where might not "really"
7291 * have been powered off.
7292 */
7293 list_del_init(&p->action);
7294
7295 switch (p->auto_connect) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02007296 case HCI_AUTO_CONN_DIRECT:
Johan Hedbergd7347f32014-07-04 12:37:23 +03007297 case HCI_AUTO_CONN_ALWAYS:
7298 list_add(&p->action, &hdev->pend_le_conns);
7299 break;
7300 case HCI_AUTO_CONN_REPORT:
7301 list_add(&p->action, &hdev->pend_le_reports);
7302 break;
7303 default:
7304 break;
Marcel Holtmannc83ed192014-07-01 19:28:24 +02007305 }
Andre Guedes6046dc32014-02-26 20:21:51 -03007306 }
Marcel Holtmannc83ed192014-07-01 19:28:24 +02007307
Johan Hedberg2cf22212014-12-19 22:26:00 +02007308 __hci_update_background_scan(req);
Andre Guedes6046dc32014-02-26 20:21:51 -03007309}
7310
Marcel Holtmann1904a852015-01-11 13:50:44 -08007311static void powered_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg229ab392013-03-15 17:06:53 -05007312{
7313 struct cmd_lookup match = { NULL, hdev };
7314
7315 BT_DBG("status 0x%02x", status);
7316
Marcel Holtmann162a3ba2015-01-14 15:43:11 -08007317 if (!status) {
7318 /* Register the available SMP channels (BR/EDR and LE) only
7319 * when successfully powering on the controller. This late
7320 * registration is required so that LE SMP can clearly
7321 * decide if the public address or static address is used.
7322 */
7323 smp_register(hdev);
7324 }
7325
Johan Hedberg229ab392013-03-15 17:06:53 -05007326 hci_dev_lock(hdev);
7327
7328 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
7329
7330 new_settings(hdev, match.sk);
7331
7332 hci_dev_unlock(hdev);
7333
7334 if (match.sk)
7335 sock_put(match.sk);
7336}
7337
Johan Hedberg70da6242013-03-15 17:06:51 -05007338static int powered_update_hci(struct hci_dev *hdev)
7339{
Johan Hedberg890ea892013-03-15 17:06:52 -05007340 struct hci_request req;
Johan Hedberg70da6242013-03-15 17:06:51 -05007341 u8 link_sec;
7342
Johan Hedberg890ea892013-03-15 17:06:52 -05007343 hci_req_init(&req, hdev);
7344
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007345 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
Johan Hedberg70da6242013-03-15 17:06:51 -05007346 !lmp_host_ssp_capable(hdev)) {
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08007347 u8 mode = 0x01;
Johan Hedberg70da6242013-03-15 17:06:51 -05007348
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08007349 hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode);
Johan Hedberg70da6242013-03-15 17:06:51 -05007350
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08007351 if (bredr_sc_enabled(hdev) && !lmp_host_sc_capable(hdev)) {
7352 u8 support = 0x01;
7353
7354 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT,
7355 sizeof(support), &support);
7356 }
Johan Hedbergec6f99b2014-12-12 13:30:11 +02007357 }
7358
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007359 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
Johan Hedbergc73eee92013-04-19 18:35:21 +03007360 lmp_bredr_capable(hdev)) {
Johan Hedberg70da6242013-03-15 17:06:51 -05007361 struct hci_cp_write_le_host_supported cp;
7362
Marcel Holtmann32226e42014-07-24 20:04:16 +02007363 cp.le = 0x01;
7364 cp.simul = 0x00;
Johan Hedberg70da6242013-03-15 17:06:51 -05007365
7366 /* Check first if we already have the right
7367 * host state (host features set)
7368 */
7369 if (cp.le != lmp_host_le_capable(hdev) ||
7370 cp.simul != lmp_host_le_br_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05007371 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED,
7372 sizeof(cp), &cp);
Johan Hedberg70da6242013-03-15 17:06:51 -05007373 }
7374
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07007375 if (lmp_le_capable(hdev)) {
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07007376 /* Make sure the controller has a good default for
7377 * advertising data. This also applies to the case
7378 * where BR/EDR was toggled during the AUTO_OFF phase.
7379 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007380 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07007381 update_adv_data(&req);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07007382 update_scan_rsp_data(&req);
7383 }
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07007384
Arman Uguray24b4f382015-03-23 15:57:12 -07007385 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
7386 hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07007387 enable_advertising(&req);
Johan Hedberg2cf22212014-12-19 22:26:00 +02007388
7389 restart_le_actions(&req);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03007390 }
7391
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007392 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg70da6242013-03-15 17:06:51 -05007393 if (link_sec != test_bit(HCI_AUTH, &hdev->flags))
Johan Hedberg890ea892013-03-15 17:06:52 -05007394 hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE,
7395 sizeof(link_sec), &link_sec);
Johan Hedberg70da6242013-03-15 17:06:51 -05007396
7397 if (lmp_bredr_capable(hdev)) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007398 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
Johan Hedberg406ef2a2015-03-10 20:14:27 +02007399 write_fast_connectable(&req, true);
7400 else
7401 write_fast_connectable(&req, false);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02007402 __hci_update_page_scan(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05007403 update_class(&req);
Johan Hedberg13928972013-03-15 17:07:00 -05007404 update_name(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05007405 update_eir(&req);
Johan Hedberg70da6242013-03-15 17:06:51 -05007406 }
7407
Johan Hedberg229ab392013-03-15 17:06:53 -05007408 return hci_req_run(&req, powered_complete);
Johan Hedberg70da6242013-03-15 17:06:51 -05007409}
7410
Johan Hedberg744cf192011-11-08 20:40:14 +02007411int mgmt_powered(struct hci_dev *hdev, u8 powered)
Johan Hedberg5add6af2010-12-16 10:00:37 +02007412{
Johan Hedberg76a7f3a2012-02-17 00:34:40 +02007413 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg98459042014-12-12 11:15:21 +02007414 u8 status, zero_cod[] = { 0, 0, 0 };
Johan Hedberg7bb895d2012-02-17 01:20:00 +02007415 int err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02007416
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007417 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg5e5282b2012-02-21 16:01:30 +02007418 return 0;
7419
Johan Hedberg5e5282b2012-02-21 16:01:30 +02007420 if (powered) {
Johan Hedberg229ab392013-03-15 17:06:53 -05007421 if (powered_update_hci(hdev) == 0)
7422 return 0;
Johan Hedbergfe038882013-01-16 16:15:34 +02007423
Johan Hedberg229ab392013-03-15 17:06:53 -05007424 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp,
7425 &match);
7426 goto new_settings;
Johan Hedbergb24752f2011-11-03 14:40:33 +02007427 }
7428
Johan Hedberg229ab392013-03-15 17:06:53 -05007429 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
Johan Hedberg98459042014-12-12 11:15:21 +02007430
7431 /* If the power off is because of hdev unregistration let
7432 * use the appropriate INVALID_INDEX status. Otherwise use
7433 * NOT_POWERED. We cover both scenarios here since later in
7434 * mgmt_index_removed() any hci_conn callbacks will have already
7435 * been triggered, potentially causing misleading DISCONNECTED
7436 * status responses.
7437 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007438 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
Johan Hedberg98459042014-12-12 11:15:21 +02007439 status = MGMT_STATUS_INVALID_INDEX;
7440 else
7441 status = MGMT_STATUS_NOT_POWERED;
7442
7443 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedberg229ab392013-03-15 17:06:53 -05007444
7445 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0)
Marcel Holtmannf6b77122015-03-14 19:28:05 -07007446 mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
7447 zero_cod, sizeof(zero_cod), NULL);
Johan Hedberg229ab392013-03-15 17:06:53 -05007448
7449new_settings:
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02007450 err = new_settings(hdev, match.sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02007451
7452 if (match.sk)
7453 sock_put(match.sk);
7454
Johan Hedberg7bb895d2012-02-17 01:20:00 +02007455 return err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02007456}
Johan Hedberg73f22f62010-12-29 16:00:25 +02007457
Marcel Holtmann3eec7052013-10-06 23:55:46 -07007458void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
Johan Hedberg96570ff2013-05-29 09:51:29 +03007459{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007460 struct mgmt_pending_cmd *cmd;
Johan Hedberg96570ff2013-05-29 09:51:29 +03007461 u8 status;
7462
Johan Hedberg333ae952015-03-17 13:48:47 +02007463 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg96570ff2013-05-29 09:51:29 +03007464 if (!cmd)
Marcel Holtmann3eec7052013-10-06 23:55:46 -07007465 return;
Johan Hedberg96570ff2013-05-29 09:51:29 +03007466
7467 if (err == -ERFKILL)
7468 status = MGMT_STATUS_RFKILLED;
7469 else
7470 status = MGMT_STATUS_FAILED;
7471
Johan Hedberga69e8372015-03-06 21:08:53 +02007472 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
Johan Hedberg96570ff2013-05-29 09:51:29 +03007473
7474 mgmt_pending_remove(cmd);
Johan Hedberg96570ff2013-05-29 09:51:29 +03007475}
7476
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007477void mgmt_discoverable_timeout(struct hci_dev *hdev)
7478{
7479 struct hci_request req;
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007480
7481 hci_dev_lock(hdev);
7482
7483 /* When discoverable timeout triggers, then just make sure
7484 * the limited discoverable flag is cleared. Even in the case
7485 * of a timeout triggered from general discoverable, it is
7486 * safe to unconditionally clear the flag.
7487 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07007488 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
7489 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007490
7491 hci_req_init(&req, hdev);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007492 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg4b580612013-10-19 23:38:21 +03007493 u8 scan = SCAN_PAGE;
7494 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE,
7495 sizeof(scan), &scan);
7496 }
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007497 update_class(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07007498
7499 /* Advertising instances don't use the global discoverable setting, so
7500 * only update AD if advertising was enabled using Set Advertising.
7501 */
7502 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
7503 update_adv_data(&req);
7504
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007505 hci_req_run(&req, NULL);
7506
7507 hdev->discov_timeout = 0;
7508
Johan Hedberg9a43e252013-10-20 19:00:07 +03007509 new_settings(hdev, NULL);
7510
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007511 hci_dev_unlock(hdev);
7512}
7513
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07007514void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
7515 bool persistent)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007516{
Johan Hedberg86742e12011-11-07 23:13:38 +02007517 struct mgmt_ev_new_link_key ev;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007518
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007519 memset(&ev, 0, sizeof(ev));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007520
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007521 ev.store_hint = persistent;
Johan Hedbergd753fdc2012-02-17 14:06:34 +02007522 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03007523 ev.key.addr.type = BDADDR_BREDR;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007524 ev.key.type = key->type;
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03007525 memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007526 ev.key.pin_len = key->pin_len;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007527
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07007528 mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007529}
Johan Hedbergf7520542011-01-20 12:34:39 +02007530
Johan Hedbergd7b25452014-05-23 13:19:53 +03007531static u8 mgmt_ltk_type(struct smp_ltk *ltk)
7532{
Johan Hedberg23fb8de2014-05-23 13:15:37 +03007533 switch (ltk->type) {
7534 case SMP_LTK:
7535 case SMP_LTK_SLAVE:
7536 if (ltk->authenticated)
7537 return MGMT_LTK_AUTHENTICATED;
7538 return MGMT_LTK_UNAUTHENTICATED;
7539 case SMP_LTK_P256:
7540 if (ltk->authenticated)
7541 return MGMT_LTK_P256_AUTH;
7542 return MGMT_LTK_P256_UNAUTH;
7543 case SMP_LTK_P256_DEBUG:
7544 return MGMT_LTK_P256_DEBUG;
7545 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03007546
7547 return MGMT_LTK_UNAUTHENTICATED;
7548}
7549
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007550void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007551{
7552 struct mgmt_ev_new_long_term_key ev;
7553
7554 memset(&ev, 0, sizeof(ev));
7555
Marcel Holtmann5192d302014-02-19 17:11:58 -08007556 /* Devices using resolvable or non-resolvable random addresses
Florian Grandelf72186d2015-05-26 03:31:09 +02007557 * without providing an identity resolving key don't require
Marcel Holtmann5192d302014-02-19 17:11:58 -08007558 * to store long term keys. Their addresses will change the
7559 * next time around.
7560 *
7561 * Only when a remote device provides an identity address
7562 * make sure the long term key is stored. If the remote
7563 * identity is known, the long term keys are internally
7564 * mapped to the identity address. So allow static random
7565 * and public addresses here.
7566 */
Johan Hedbergba74b662014-02-19 14:57:45 +02007567 if (key->bdaddr_type == ADDR_LE_DEV_RANDOM &&
7568 (key->bdaddr.b[5] & 0xc0) != 0xc0)
7569 ev.store_hint = 0x00;
7570 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007571 ev.store_hint = persistent;
Johan Hedbergba74b662014-02-19 14:57:45 +02007572
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007573 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007574 ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
Johan Hedbergd7b25452014-05-23 13:19:53 +03007575 ev.key.type = mgmt_ltk_type(key);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007576 ev.key.enc_size = key->enc_size;
7577 ev.key.ediv = key->ediv;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08007578 ev.key.rand = key->rand;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007579
Johan Hedberg2ceba532014-06-16 19:25:16 +03007580 if (key->type == SMP_LTK)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007581 ev.key.master = 1;
7582
Johan Hedberg1fc62c52015-06-10 11:11:20 +03007583 /* Make sure we copy only the significant bytes based on the
7584 * encryption key size, and set the rest of the value to zeroes.
7585 */
7586 memcpy(ev.key.val, key->val, sizeof(key->enc_size));
7587 memset(ev.key.val + key->enc_size, 0,
7588 sizeof(ev.key.val) - key->enc_size);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007589
Marcel Holtmann083368f2013-10-15 14:26:29 -07007590 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007591}
7592
Johan Hedberg95fbac82014-02-19 15:18:31 +02007593void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk)
7594{
7595 struct mgmt_ev_new_irk ev;
7596
7597 memset(&ev, 0, sizeof(ev));
7598
Marcel Holtmannbab6d1e2014-02-19 11:51:54 -08007599 /* For identity resolving keys from devices that are already
7600 * using a public address or static random address, do not
7601 * ask for storing this key. The identity resolving key really
Florian Grandelf72186d2015-05-26 03:31:09 +02007602 * is only mandatory for devices using resolvable random
Marcel Holtmannbab6d1e2014-02-19 11:51:54 -08007603 * addresses.
7604 *
7605 * Storing all identity resolving keys has the downside that
7606 * they will be also loaded on next boot of they system. More
7607 * identity resolving keys, means more time during scanning is
7608 * needed to actually resolve these addresses.
7609 */
7610 if (bacmp(&irk->rpa, BDADDR_ANY))
7611 ev.store_hint = 0x01;
7612 else
7613 ev.store_hint = 0x00;
7614
Johan Hedberg95fbac82014-02-19 15:18:31 +02007615 bacpy(&ev.rpa, &irk->rpa);
7616 bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr);
7617 ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type);
7618 memcpy(ev.irk.val, irk->val, sizeof(irk->val));
7619
7620 mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
7621}
7622
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007623void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
7624 bool persistent)
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007625{
7626 struct mgmt_ev_new_csrk ev;
7627
7628 memset(&ev, 0, sizeof(ev));
7629
7630 /* Devices using resolvable or non-resolvable random addresses
Florian Grandelf72186d2015-05-26 03:31:09 +02007631 * without providing an identity resolving key don't require
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007632 * to store signature resolving keys. Their addresses will change
7633 * the next time around.
7634 *
7635 * Only when a remote device provides an identity address
7636 * make sure the signature resolving key is stored. So allow
7637 * static random and public addresses here.
7638 */
7639 if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM &&
7640 (csrk->bdaddr.b[5] & 0xc0) != 0xc0)
7641 ev.store_hint = 0x00;
7642 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007643 ev.store_hint = persistent;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007644
7645 bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
7646 ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
Johan Hedberg4cd39282015-02-27 10:11:13 +02007647 ev.key.type = csrk->type;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007648 memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
7649
7650 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
7651}
7652
Andre Guedesffb5a8272014-07-01 18:10:11 -03007653void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedbergf4869e22014-07-02 17:37:32 +03007654 u8 bdaddr_type, u8 store_hint, u16 min_interval,
7655 u16 max_interval, u16 latency, u16 timeout)
Andre Guedesffb5a8272014-07-01 18:10:11 -03007656{
7657 struct mgmt_ev_new_conn_param ev;
7658
Johan Hedbergc103aea2014-07-02 17:37:34 +03007659 if (!hci_is_identity_address(bdaddr, bdaddr_type))
7660 return;
7661
Andre Guedesffb5a8272014-07-01 18:10:11 -03007662 memset(&ev, 0, sizeof(ev));
7663 bacpy(&ev.addr.bdaddr, bdaddr);
7664 ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type);
Johan Hedbergf4869e22014-07-02 17:37:32 +03007665 ev.store_hint = store_hint;
Andre Guedesffb5a8272014-07-01 18:10:11 -03007666 ev.min_interval = cpu_to_le16(min_interval);
7667 ev.max_interval = cpu_to_le16(max_interval);
7668 ev.latency = cpu_to_le16(latency);
7669 ev.timeout = cpu_to_le16(timeout);
7670
7671 mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL);
7672}
7673
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00007674void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
7675 u32 flags, u8 *name, u8 name_len)
Johan Hedbergf7520542011-01-20 12:34:39 +02007676{
Johan Hedbergb644ba32012-01-17 21:48:47 +02007677 char buf[512];
7678 struct mgmt_ev_device_connected *ev = (void *) buf;
7679 u16 eir_len = 0;
Johan Hedbergf7520542011-01-20 12:34:39 +02007680
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00007681 bacpy(&ev->addr.bdaddr, &conn->dst);
7682 ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedbergf7520542011-01-20 12:34:39 +02007683
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02007684 ev->flags = __cpu_to_le32(flags);
Johan Hedberg08c79b62012-02-23 22:31:51 +02007685
Alfonso Acostafd45ada2014-10-07 08:44:11 +00007686 /* We must ensure that the EIR Data fields are ordered and
7687 * unique. Keep it simple for now and avoid the problem by not
7688 * adding any BR/EDR data to the LE adv.
7689 */
7690 if (conn->le_adv_data_len > 0) {
7691 memcpy(&ev->eir[eir_len],
7692 conn->le_adv_data, conn->le_adv_data_len);
7693 eir_len = conn->le_adv_data_len;
7694 } else {
7695 if (name_len > 0)
7696 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE,
7697 name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007698
Alfonso Acostaddbea5c2014-10-07 08:44:12 +00007699 if (memcmp(conn->dev_class, "\0\0\0", 3) != 0)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00007700 eir_len = eir_append_data(ev->eir, eir_len,
7701 EIR_CLASS_OF_DEV,
7702 conn->dev_class, 3);
7703 }
Johan Hedbergb644ba32012-01-17 21:48:47 +02007704
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02007705 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007706
Marcel Holtmannecd90ae2013-10-06 23:55:49 -07007707 mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf,
7708 sizeof(*ev) + eir_len, NULL);
Johan Hedbergf7520542011-01-20 12:34:39 +02007709}
7710
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007711static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg8962ee72011-01-20 12:40:27 +02007712{
Johan Hedberg8962ee72011-01-20 12:40:27 +02007713 struct sock **sk = data;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007714
Johan Hedbergf5818c22014-12-05 13:36:02 +02007715 cmd->cmd_complete(cmd, 0);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007716
7717 *sk = cmd->sk;
7718 sock_hold(*sk);
7719
Johan Hedberga664b5b2011-02-19 12:06:02 -03007720 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007721}
7722
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007723static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberga8a1d192011-11-10 15:54:38 +02007724{
Johan Hedbergb1078ad2012-02-09 17:21:16 +02007725 struct hci_dev *hdev = data;
Johan Hedberg124f6e32012-02-09 13:50:12 +02007726 struct mgmt_cp_unpair_device *cp = cmd->param;
Johan Hedberga8a1d192011-11-10 15:54:38 +02007727
Johan Hedbergb1078ad2012-02-09 17:21:16 +02007728 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
7729
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02007730 cmd->cmd_complete(cmd, 0);
Johan Hedberga8a1d192011-11-10 15:54:38 +02007731 mgmt_pending_remove(cmd);
7732}
7733
Johan Hedberg84c61d92014-08-01 11:13:30 +03007734bool mgmt_powering_down(struct hci_dev *hdev)
7735{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007736 struct mgmt_pending_cmd *cmd;
Johan Hedberg84c61d92014-08-01 11:13:30 +03007737 struct mgmt_mode *cp;
7738
Johan Hedberg333ae952015-03-17 13:48:47 +02007739 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg84c61d92014-08-01 11:13:30 +03007740 if (!cmd)
7741 return false;
7742
7743 cp = cmd->param;
7744 if (!cp->val)
7745 return true;
7746
7747 return false;
7748}
7749
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07007750void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02007751 u8 link_type, u8 addr_type, u8 reason,
7752 bool mgmt_connected)
Johan Hedbergf7520542011-01-20 12:34:39 +02007753{
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02007754 struct mgmt_ev_device_disconnected ev;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007755 struct sock *sk = NULL;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007756
Johan Hedberg84c61d92014-08-01 11:13:30 +03007757 /* The connection is still in hci_conn_hash so test for 1
7758 * instead of 0 to know if this is the last one.
7759 */
7760 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
7761 cancel_delayed_work(&hdev->power_off);
7762 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberg8b064a32014-02-24 14:52:22 +02007763 }
7764
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02007765 if (!mgmt_connected)
7766 return;
7767
Andre Guedes57eb7762013-10-30 19:01:41 -03007768 if (link_type != ACL_LINK && link_type != LE_LINK)
7769 return;
7770
Johan Hedberg744cf192011-11-08 20:40:14 +02007771 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
Johan Hedbergf7520542011-01-20 12:34:39 +02007772
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02007773 bacpy(&ev.addr.bdaddr, bdaddr);
7774 ev.addr.type = link_to_bdaddr(link_type, addr_type);
7775 ev.reason = reason;
Johan Hedbergf7520542011-01-20 12:34:39 +02007776
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07007777 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007778
7779 if (sk)
Szymon Jancd97dcb62012-03-16 16:02:56 +01007780 sock_put(sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007781
Johan Hedberg124f6e32012-02-09 13:50:12 +02007782 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007783 hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007784}
7785
Marcel Holtmann78929242013-10-06 23:55:47 -07007786void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
7787 u8 link_type, u8 addr_type, u8 status)
Johan Hedberg8962ee72011-01-20 12:40:27 +02007788{
Andre Guedes3655bba2013-10-30 19:01:40 -03007789 u8 bdaddr_type = link_to_bdaddr(link_type, addr_type);
7790 struct mgmt_cp_disconnect *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007791 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007792
Jefferson Delfes36a75f12012-09-18 13:36:54 -04007793 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
7794 hdev);
7795
Johan Hedberg333ae952015-03-17 13:48:47 +02007796 cmd = pending_find(MGMT_OP_DISCONNECT, hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007797 if (!cmd)
Marcel Holtmann78929242013-10-06 23:55:47 -07007798 return;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007799
Andre Guedes3655bba2013-10-30 19:01:40 -03007800 cp = cmd->param;
7801
7802 if (bacmp(bdaddr, &cp->addr.bdaddr))
7803 return;
7804
7805 if (cp->addr.type != bdaddr_type)
7806 return;
7807
Johan Hedbergf5818c22014-12-05 13:36:02 +02007808 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007809 mgmt_pending_remove(cmd);
Johan Hedbergf7520542011-01-20 12:34:39 +02007810}
Johan Hedberg17d5c042011-01-22 06:09:08 +02007811
Marcel Holtmann445608d2013-10-06 23:55:48 -07007812void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
7813 u8 addr_type, u8 status)
Johan Hedberg17d5c042011-01-22 06:09:08 +02007814{
7815 struct mgmt_ev_connect_failed ev;
Johan Hedbergc9910d02014-02-27 14:35:12 +02007816
Johan Hedberg84c61d92014-08-01 11:13:30 +03007817 /* The connection is still in hci_conn_hash so test for 1
7818 * instead of 0 to know if this is the last one.
7819 */
7820 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
7821 cancel_delayed_work(&hdev->power_off);
7822 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedbergc9910d02014-02-27 14:35:12 +02007823 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02007824
Johan Hedberg4c659c32011-11-07 23:13:39 +02007825 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007826 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergca69b792011-11-11 18:10:00 +02007827 ev.status = mgmt_status(status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02007828
Marcel Holtmann445608d2013-10-06 23:55:48 -07007829 mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg17d5c042011-01-22 06:09:08 +02007830}
Johan Hedberg980e1a52011-01-22 06:10:07 +02007831
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07007832void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007833{
7834 struct mgmt_ev_pin_code_request ev;
7835
Johan Hedbergd8457692012-02-17 14:24:57 +02007836 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03007837 ev.addr.type = BDADDR_BREDR;
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02007838 ev.secure = secure;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007839
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07007840 mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007841}
7842
Marcel Holtmanne669cf82013-10-15 14:26:21 -07007843void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
7844 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007845{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007846 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007847
Johan Hedberg333ae952015-03-17 13:48:47 +02007848 cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007849 if (!cmd)
Marcel Holtmanne669cf82013-10-15 14:26:21 -07007850 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007851
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007852 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007853 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007854}
7855
Marcel Holtmann3eb38522013-10-15 14:26:22 -07007856void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
7857 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007858{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007859 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007860
Johan Hedberg333ae952015-03-17 13:48:47 +02007861 cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007862 if (!cmd)
Marcel Holtmann3eb38522013-10-15 14:26:22 -07007863 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007864
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007865 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007866 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007867}
Johan Hedberga5c29682011-02-19 12:05:57 -03007868
Johan Hedberg744cf192011-11-08 20:40:14 +02007869int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg39adbff2014-03-20 08:18:14 +02007870 u8 link_type, u8 addr_type, u32 value,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007871 u8 confirm_hint)
Johan Hedberga5c29682011-02-19 12:05:57 -03007872{
7873 struct mgmt_ev_user_confirm_request ev;
7874
Johan Hedberg744cf192011-11-08 20:40:14 +02007875 BT_DBG("%s", hdev->name);
Johan Hedberga5c29682011-02-19 12:05:57 -03007876
Johan Hedberg272d90d2012-02-09 15:26:12 +02007877 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007878 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberg55bc1a32011-04-28 11:28:56 -07007879 ev.confirm_hint = confirm_hint;
Johan Hedberg39adbff2014-03-20 08:18:14 +02007880 ev.value = cpu_to_le32(value);
Johan Hedberga5c29682011-02-19 12:05:57 -03007881
Johan Hedberg744cf192011-11-08 20:40:14 +02007882 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007883 NULL);
Johan Hedberga5c29682011-02-19 12:05:57 -03007884}
7885
Johan Hedberg272d90d2012-02-09 15:26:12 +02007886int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03007887 u8 link_type, u8 addr_type)
Brian Gix604086b2011-11-23 08:28:33 -08007888{
7889 struct mgmt_ev_user_passkey_request ev;
7890
7891 BT_DBG("%s", hdev->name);
7892
Johan Hedberg272d90d2012-02-09 15:26:12 +02007893 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007894 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Brian Gix604086b2011-11-23 08:28:33 -08007895
7896 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007897 NULL);
Brian Gix604086b2011-11-23 08:28:33 -08007898}
7899
Brian Gix0df4c182011-11-16 13:53:13 -08007900static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03007901 u8 link_type, u8 addr_type, u8 status,
7902 u8 opcode)
Johan Hedberga5c29682011-02-19 12:05:57 -03007903{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007904 struct mgmt_pending_cmd *cmd;
Johan Hedberga5c29682011-02-19 12:05:57 -03007905
Johan Hedberg333ae952015-03-17 13:48:47 +02007906 cmd = pending_find(opcode, hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03007907 if (!cmd)
7908 return -ENOENT;
7909
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007910 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007911 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03007912
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007913 return 0;
Johan Hedberga5c29682011-02-19 12:05:57 -03007914}
7915
Johan Hedberg744cf192011-11-08 20:40:14 +02007916int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007917 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03007918{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007919 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007920 status, MGMT_OP_USER_CONFIRM_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03007921}
7922
Johan Hedberg272d90d2012-02-09 15:26:12 +02007923int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007924 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03007925{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007926 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03007927 status,
7928 MGMT_OP_USER_CONFIRM_NEG_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03007929}
Johan Hedberg2a611692011-02-19 12:06:00 -03007930
Brian Gix604086b2011-11-23 08:28:33 -08007931int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007932 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08007933{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007934 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007935 status, MGMT_OP_USER_PASSKEY_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08007936}
7937
Johan Hedberg272d90d2012-02-09 15:26:12 +02007938int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007939 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08007940{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007941 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03007942 status,
7943 MGMT_OP_USER_PASSKEY_NEG_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08007944}
7945
Johan Hedberg92a25252012-09-06 18:39:26 +03007946int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
7947 u8 link_type, u8 addr_type, u32 passkey,
7948 u8 entered)
7949{
7950 struct mgmt_ev_passkey_notify ev;
7951
7952 BT_DBG("%s", hdev->name);
7953
7954 bacpy(&ev.addr.bdaddr, bdaddr);
7955 ev.addr.type = link_to_bdaddr(link_type, addr_type);
7956 ev.passkey = __cpu_to_le32(passkey);
7957 ev.entered = entered;
7958
7959 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
7960}
7961
Johan Hedberge1e930f2014-09-08 17:09:49 -07007962void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status)
Johan Hedberg2a611692011-02-19 12:06:00 -03007963{
7964 struct mgmt_ev_auth_failed ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007965 struct mgmt_pending_cmd *cmd;
Johan Hedberge1e930f2014-09-08 17:09:49 -07007966 u8 status = mgmt_status(hci_status);
Johan Hedberg2a611692011-02-19 12:06:00 -03007967
Johan Hedberge1e930f2014-09-08 17:09:49 -07007968 bacpy(&ev.addr.bdaddr, &conn->dst);
7969 ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
7970 ev.status = status;
Johan Hedberg2a611692011-02-19 12:06:00 -03007971
Johan Hedberge1e930f2014-09-08 17:09:49 -07007972 cmd = find_pairing(conn);
7973
7974 mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev),
7975 cmd ? cmd->sk : NULL);
7976
Johan Hedberga511b352014-12-11 21:45:45 +02007977 if (cmd) {
7978 cmd->cmd_complete(cmd, status);
7979 mgmt_pending_remove(cmd);
7980 }
Johan Hedberg2a611692011-02-19 12:06:00 -03007981}
Johan Hedbergb312b1612011-03-16 14:29:37 +02007982
Marcel Holtmann464996a2013-10-15 14:26:24 -07007983void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007984{
7985 struct cmd_lookup match = { NULL, hdev };
Marcel Holtmann464996a2013-10-15 14:26:24 -07007986 bool changed;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007987
7988 if (status) {
7989 u8 mgmt_err = mgmt_status(status);
7990 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007991 cmd_status_rsp, &mgmt_err);
Marcel Holtmann464996a2013-10-15 14:26:24 -07007992 return;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007993 }
7994
Marcel Holtmann464996a2013-10-15 14:26:24 -07007995 if (test_bit(HCI_AUTH, &hdev->flags))
Marcel Holtmann238be782015-03-13 02:11:06 -07007996 changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY);
Marcel Holtmann464996a2013-10-15 14:26:24 -07007997 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007998 changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02007999
Johan Hedberg33ef95e2012-02-16 23:56:27 +02008000 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03008001 &match);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02008002
Johan Hedberg47990ea2012-02-22 11:58:37 +02008003 if (changed)
Marcel Holtmann464996a2013-10-15 14:26:24 -07008004 new_settings(hdev, match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02008005
8006 if (match.sk)
8007 sock_put(match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02008008}
8009
Johan Hedberg890ea892013-03-15 17:06:52 -05008010static void clear_eir(struct hci_request *req)
Johan Hedbergcacaf522012-02-21 00:52:42 +02008011{
Johan Hedberg890ea892013-03-15 17:06:52 -05008012 struct hci_dev *hdev = req->hdev;
Johan Hedbergcacaf522012-02-21 00:52:42 +02008013 struct hci_cp_write_eir cp;
8014
Johan Hedberg976eb202012-10-24 21:12:01 +03008015 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05008016 return;
Johan Hedbergcacaf522012-02-21 00:52:42 +02008017
Johan Hedbergc80da272012-02-22 15:38:48 +02008018 memset(hdev->eir, 0, sizeof(hdev->eir));
8019
Johan Hedbergcacaf522012-02-21 00:52:42 +02008020 memset(&cp, 0, sizeof(cp));
8021
Johan Hedberg890ea892013-03-15 17:06:52 -05008022 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedbergcacaf522012-02-21 00:52:42 +02008023}
8024
Marcel Holtmann3e248562013-10-15 14:26:25 -07008025void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02008026{
8027 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg890ea892013-03-15 17:06:52 -05008028 struct hci_request req;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02008029 bool changed = false;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02008030
8031 if (status) {
8032 u8 mgmt_err = mgmt_status(status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02008033
Marcel Holtmanna69d8922015-03-13 02:11:05 -07008034 if (enable && hci_dev_test_and_clear_flag(hdev,
8035 HCI_SSP_ENABLED)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07008036 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmann3e248562013-10-15 14:26:25 -07008037 new_settings(hdev, NULL);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07008038 }
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02008039
Gustavo F. Padovan04124682012-03-08 01:25:00 -03008040 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
8041 &mgmt_err);
Marcel Holtmann3e248562013-10-15 14:26:25 -07008042 return;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02008043 }
8044
8045 if (enable) {
Marcel Holtmann238be782015-03-13 02:11:06 -07008046 changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02008047 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07008048 changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07008049 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07008050 changed = hci_dev_test_and_clear_flag(hdev,
8051 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07008052 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07008053 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02008054 }
8055
8056 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
8057
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02008058 if (changed)
Marcel Holtmann3e248562013-10-15 14:26:25 -07008059 new_settings(hdev, match.sk);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02008060
Johan Hedberg5fc6ebb2012-02-22 15:10:59 +02008061 if (match.sk)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02008062 sock_put(match.sk);
8063
Johan Hedberg890ea892013-03-15 17:06:52 -05008064 hci_req_init(&req, hdev);
8065
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07008066 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
8067 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03008068 hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE,
8069 sizeof(enable), &enable);
Johan Hedberg890ea892013-03-15 17:06:52 -05008070 update_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03008071 } else {
Johan Hedberg890ea892013-03-15 17:06:52 -05008072 clear_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03008073 }
Johan Hedberg890ea892013-03-15 17:06:52 -05008074
8075 hci_req_run(&req, NULL);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02008076}
8077
Johan Hedberg3b0602c2015-03-06 21:08:55 +02008078static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg90e70452012-02-23 23:09:40 +02008079{
8080 struct cmd_lookup *match = data;
8081
Johan Hedberg90e70452012-02-23 23:09:40 +02008082 if (match->sk == NULL) {
8083 match->sk = cmd->sk;
8084 sock_hold(match->sk);
8085 }
Johan Hedberg90e70452012-02-23 23:09:40 +02008086}
8087
Marcel Holtmann4e1b0242013-10-15 14:26:26 -07008088void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
8089 u8 status)
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01008090{
Johan Hedberg90e70452012-02-23 23:09:40 +02008091 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01008092
Johan Hedberg92da6092013-03-15 17:06:55 -05008093 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
8094 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
8095 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
Johan Hedberg90e70452012-02-23 23:09:40 +02008096
8097 if (!status)
Marcel Holtmannf6b77122015-03-14 19:28:05 -07008098 mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
8099 dev_class, 3, NULL);
Johan Hedberg90e70452012-02-23 23:09:40 +02008100
8101 if (match.sk)
8102 sock_put(match.sk);
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01008103}
8104
Marcel Holtmann7667da32013-10-15 14:26:27 -07008105void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
Johan Hedbergb312b1612011-03-16 14:29:37 +02008106{
Johan Hedbergb312b1612011-03-16 14:29:37 +02008107 struct mgmt_cp_set_local_name ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02008108 struct mgmt_pending_cmd *cmd;
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02008109
Johan Hedberg13928972013-03-15 17:07:00 -05008110 if (status)
Marcel Holtmann7667da32013-10-15 14:26:27 -07008111 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02008112
8113 memset(&ev, 0, sizeof(ev));
8114 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02008115 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
Johan Hedbergb312b1612011-03-16 14:29:37 +02008116
Johan Hedberg333ae952015-03-17 13:48:47 +02008117 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05008118 if (!cmd) {
8119 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
Johan Hedbergb312b1612011-03-16 14:29:37 +02008120
Johan Hedberg13928972013-03-15 17:07:00 -05008121 /* If this is a HCI command related to powering on the
8122 * HCI dev don't send any mgmt signals.
8123 */
Johan Hedberg333ae952015-03-17 13:48:47 +02008124 if (pending_find(MGMT_OP_SET_POWERED, hdev))
Marcel Holtmann7667da32013-10-15 14:26:27 -07008125 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02008126 }
8127
Marcel Holtmannf6b77122015-03-14 19:28:05 -07008128 mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
8129 cmd ? cmd->sk : NULL);
Johan Hedbergb312b1612011-03-16 14:29:37 +02008130}
Szymon Jancc35938b2011-03-22 13:12:21 +01008131
Jakub Pawlowski799ce932014-12-05 10:55:58 +01008132static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16])
8133{
8134 int i;
8135
8136 for (i = 0; i < uuid_count; i++) {
8137 if (!memcmp(uuid, uuids[i], 16))
8138 return true;
8139 }
8140
8141 return false;
8142}
8143
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01008144static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16])
8145{
Jakub Pawlowski799ce932014-12-05 10:55:58 +01008146 u16 parsed = 0;
8147
8148 while (parsed < eir_len) {
8149 u8 field_len = eir[0];
8150 u8 uuid[16];
8151 int i;
8152
8153 if (field_len == 0)
8154 break;
8155
8156 if (eir_len - parsed < field_len + 1)
8157 break;
8158
8159 switch (eir[1]) {
8160 case EIR_UUID16_ALL:
8161 case EIR_UUID16_SOME:
8162 for (i = 0; i + 3 <= field_len; i += 2) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02008163 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01008164 uuid[13] = eir[i + 3];
8165 uuid[12] = eir[i + 2];
8166 if (has_uuid(uuid, uuid_count, uuids))
8167 return true;
8168 }
8169 break;
8170 case EIR_UUID32_ALL:
8171 case EIR_UUID32_SOME:
8172 for (i = 0; i + 5 <= field_len; i += 4) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02008173 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01008174 uuid[15] = eir[i + 5];
8175 uuid[14] = eir[i + 4];
8176 uuid[13] = eir[i + 3];
8177 uuid[12] = eir[i + 2];
8178 if (has_uuid(uuid, uuid_count, uuids))
8179 return true;
8180 }
8181 break;
8182 case EIR_UUID128_ALL:
8183 case EIR_UUID128_SOME:
8184 for (i = 0; i + 17 <= field_len; i += 16) {
8185 memcpy(uuid, eir + i + 2, 16);
8186 if (has_uuid(uuid, uuid_count, uuids))
8187 return true;
8188 }
8189 break;
8190 }
8191
8192 parsed += field_len + 1;
8193 eir += field_len + 1;
8194 }
8195
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01008196 return false;
8197}
8198
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08008199static void restart_le_scan(struct hci_dev *hdev)
8200{
8201 /* If controller is not scanning we are done. */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07008202 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08008203 return;
8204
8205 if (time_after(jiffies + DISCOV_LE_RESTART_DELAY,
8206 hdev->discovery.scan_start +
8207 hdev->discovery.scan_duration))
8208 return;
8209
8210 queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart,
8211 DISCOV_LE_RESTART_DELAY);
8212}
8213
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008214static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir,
8215 u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
8216{
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008217 /* If a RSSI threshold has been specified, and
8218 * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with
8219 * a RSSI smaller than the RSSI threshold will be dropped. If the quirk
8220 * is set, let it through for further processing, as we might need to
8221 * restart the scan.
8222 *
8223 * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry,
8224 * the results are also dropped.
8225 */
8226 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
8227 (rssi == HCI_RSSI_INVALID ||
8228 (rssi < hdev->discovery.rssi &&
8229 !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))
8230 return false;
8231
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008232 if (hdev->discovery.uuid_count != 0) {
8233 /* If a list of UUIDs is provided in filter, results with no
8234 * matching UUID should be dropped.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008235 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008236 if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count,
8237 hdev->discovery.uuids) &&
8238 !eir_has_uuids(scan_rsp, scan_rsp_len,
8239 hdev->discovery.uuid_count,
8240 hdev->discovery.uuids))
8241 return false;
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008242 }
8243
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008244 /* If duplicate filtering does not report RSSI changes, then restart
8245 * scanning to ensure updated result with updated RSSI values.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008246 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008247 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) {
8248 restart_le_scan(hdev);
8249
8250 /* Validate RSSI value against the RSSI threshold once more. */
8251 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
8252 rssi < hdev->discovery.rssi)
8253 return false;
8254 }
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008255
8256 return true;
8257}
8258
Marcel Holtmann901801b2013-10-06 23:55:51 -07008259void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
Marcel Holtmannaf589252014-07-01 14:11:20 +02008260 u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
8261 u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
Johan Hedberge17acd42011-03-30 23:57:16 +03008262{
Johan Hedberge319d2e2012-01-15 19:51:59 +02008263 char buf[512];
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008264 struct mgmt_ev_device_found *ev = (void *)buf;
Johan Hedberg1dc06092012-01-15 21:01:23 +02008265 size_t ev_size;
Johan Hedberge17acd42011-03-30 23:57:16 +03008266
Johan Hedberg75ce2082014-07-02 22:42:01 +03008267 /* Don't send events for a non-kernel initiated discovery. With
8268 * LE one exception is if we have pend_le_reports > 0 in which
8269 * case we're doing passive scanning and want these events.
8270 */
8271 if (!hci_discovery_active(hdev)) {
8272 if (link_type == ACL_LINK)
8273 return;
Johan Hedberg66f84552014-07-04 12:37:18 +03008274 if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports))
Johan Hedberg75ce2082014-07-02 22:42:01 +03008275 return;
8276 }
Andre Guedes12602d02013-04-30 15:29:40 -03008277
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08008278 if (hdev->discovery.result_filtering) {
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008279 /* We are using service discovery */
8280 if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp,
8281 scan_rsp_len))
8282 return;
8283 }
Marcel Holtmannbda157a2014-12-05 10:55:56 +01008284
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02008285 /* Make sure that the buffer is big enough. The 5 extra bytes
8286 * are for the potential CoD field.
8287 */
8288 if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf))
Marcel Holtmann901801b2013-10-06 23:55:51 -07008289 return;
Andre Guedes7d262f82012-01-10 18:20:49 -03008290
Johan Hedberg1dc06092012-01-15 21:01:23 +02008291 memset(buf, 0, sizeof(buf));
8292
Marcel Holtmannda25cf62014-12-05 13:03:35 +01008293 /* In case of device discovery with BR/EDR devices (pre 1.2), the
8294 * RSSI value was reported as 0 when not available. This behavior
8295 * is kept when using device discovery. This is required for full
8296 * backwards compatibility with the API.
8297 *
8298 * However when using service discovery, the value 127 will be
8299 * returned when the RSSI is not available.
8300 */
Szymon Janc91200e92015-01-22 16:57:05 +01008301 if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi &&
8302 link_type == ACL_LINK)
Marcel Holtmannefb25132014-12-05 13:03:34 +01008303 rssi = 0;
8304
Johan Hedberg841c5642014-07-07 12:45:54 +03008305 bacpy(&ev->addr.bdaddr, bdaddr);
8306 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberge319d2e2012-01-15 19:51:59 +02008307 ev->rssi = rssi;
Marcel Holtmannaf589252014-07-01 14:11:20 +02008308 ev->flags = cpu_to_le32(flags);
Johan Hedberge17acd42011-03-30 23:57:16 +03008309
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008310 if (eir_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01008311 /* Copy EIR or advertising data into event */
Johan Hedberge319d2e2012-01-15 19:51:59 +02008312 memcpy(ev->eir, eir, eir_len);
Johan Hedberge17acd42011-03-30 23:57:16 +03008313
Johan Hedberg1dc06092012-01-15 21:01:23 +02008314 if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV))
8315 eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03008316 dev_class, 3);
Johan Hedberg1dc06092012-01-15 21:01:23 +02008317
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008318 if (scan_rsp_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01008319 /* Append scan response data to event */
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02008320 memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len);
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08008321
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02008322 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
8323 ev_size = sizeof(*ev) + eir_len + scan_rsp_len;
Andre Guedesf8523592011-09-09 18:56:26 -03008324
Marcel Holtmann901801b2013-10-06 23:55:51 -07008325 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL);
Johan Hedberge17acd42011-03-30 23:57:16 +03008326}
Johan Hedberga88a9652011-03-30 13:18:12 +03008327
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07008328void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
8329 u8 addr_type, s8 rssi, u8 *name, u8 name_len)
Johan Hedberga88a9652011-03-30 13:18:12 +03008330{
Johan Hedbergb644ba32012-01-17 21:48:47 +02008331 struct mgmt_ev_device_found *ev;
8332 char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2];
8333 u16 eir_len;
Johan Hedberga88a9652011-03-30 13:18:12 +03008334
Johan Hedbergb644ba32012-01-17 21:48:47 +02008335 ev = (struct mgmt_ev_device_found *) buf;
Johan Hedberga88a9652011-03-30 13:18:12 +03008336
Johan Hedbergb644ba32012-01-17 21:48:47 +02008337 memset(buf, 0, sizeof(buf));
Johan Hedberga88a9652011-03-30 13:18:12 +03008338
Johan Hedbergb644ba32012-01-17 21:48:47 +02008339 bacpy(&ev->addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03008340 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergb644ba32012-01-17 21:48:47 +02008341 ev->rssi = rssi;
8342
8343 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03008344 name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02008345
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02008346 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02008347
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07008348 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL);
Johan Hedberga88a9652011-03-30 13:18:12 +03008349}
Johan Hedberg314b2382011-04-27 10:29:57 -04008350
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07008351void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
Johan Hedberg314b2382011-04-27 10:29:57 -04008352{
Johan Hedbergf963e8e2012-02-20 23:30:44 +02008353 struct mgmt_ev_discovering ev;
Johan Hedberg164a6e72011-11-01 17:06:44 +02008354
Andre Guedes343fb142011-11-22 17:14:19 -03008355 BT_DBG("%s discovering %u", hdev->name, discovering);
8356
Johan Hedbergf963e8e2012-02-20 23:30:44 +02008357 memset(&ev, 0, sizeof(ev));
8358 ev.type = hdev->discovery.type;
8359 ev.discovering = discovering;
8360
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07008361 mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg314b2382011-04-27 10:29:57 -04008362}
Antti Julku5e762442011-08-25 16:48:02 +03008363
Marcel Holtmann1904a852015-01-11 13:50:44 -08008364static void adv_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Marcel Holtmann5976e602013-10-06 04:08:14 -07008365{
8366 BT_DBG("%s status %u", hdev->name, status);
Marcel Holtmann5976e602013-10-06 04:08:14 -07008367}
8368
8369void mgmt_reenable_advertising(struct hci_dev *hdev)
8370{
8371 struct hci_request req;
8372
Arman Uguray24b4f382015-03-23 15:57:12 -07008373 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
8374 !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
Marcel Holtmann5976e602013-10-06 04:08:14 -07008375 return;
8376
8377 hci_req_init(&req, hdev);
8378 enable_advertising(&req);
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03008379 hci_req_run(&req, adv_enable_complete);
Marcel Holtmann5976e602013-10-06 04:08:14 -07008380}
Johan Hedberg6d785aa32015-03-06 21:08:51 +02008381
8382static struct hci_mgmt_chan chan = {
8383 .channel = HCI_CHANNEL_CONTROL,
8384 .handler_count = ARRAY_SIZE(mgmt_handlers),
8385 .handlers = mgmt_handlers,
Johan Hedberg88b94ce2015-03-17 13:48:49 +02008386 .hdev_init = mgmt_init_hdev,
Johan Hedberg6d785aa32015-03-06 21:08:51 +02008387};
8388
8389int mgmt_init(void)
8390{
8391 return hci_mgmt_chan_register(&chan);
8392}
8393
8394void mgmt_exit(void)
8395{
8396 hci_mgmt_chan_unregister(&chan);
8397}