blob: a9a0036ff0a713eae4c202cdd757c5060faf8aba [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
Arman Uguray4117ed72015-03-23 15:57:14 -0700835static u8 create_default_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700836{
Marcel Holtmann7a5f4992013-10-16 00:16:49 -0700837 u8 ad_len = 0;
838 size_t name_len;
839
840 name_len = strlen(hdev->dev_name);
841 if (name_len > 0) {
842 size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2;
843
844 if (name_len > max_len) {
845 name_len = max_len;
846 ptr[1] = EIR_NAME_SHORT;
847 } else
848 ptr[1] = EIR_NAME_COMPLETE;
849
850 ptr[0] = name_len + 1;
851
852 memcpy(ptr + 2, hdev->dev_name, name_len);
853
854 ad_len += (name_len + 2);
855 ptr += (name_len + 2);
856 }
857
858 return ad_len;
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700859}
860
Arman Uguray4117ed72015-03-23 15:57:14 -0700861static u8 create_instance_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
862{
863 /* TODO: Set the appropriate entries based on advertising instance flags
864 * here once flags other than 0 are supported.
865 */
866 memcpy(ptr, hdev->adv_instance.scan_rsp_data,
867 hdev->adv_instance.scan_rsp_len);
868
869 return hdev->adv_instance.scan_rsp_len;
870}
871
872static void update_scan_rsp_data_for_instance(struct hci_request *req,
873 u8 instance)
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700874{
875 struct hci_dev *hdev = req->hdev;
876 struct hci_cp_le_set_scan_rsp_data cp;
877 u8 len;
878
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700879 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700880 return;
881
882 memset(&cp, 0, sizeof(cp));
883
Arman Uguray4117ed72015-03-23 15:57:14 -0700884 if (instance)
885 len = create_instance_scan_rsp_data(hdev, cp.data);
886 else
887 len = create_default_scan_rsp_data(hdev, cp.data);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700888
Johan Hedbergeb438b52013-10-16 15:31:07 +0300889 if (hdev->scan_rsp_data_len == len &&
Arman Uguray4117ed72015-03-23 15:57:14 -0700890 !memcmp(cp.data, hdev->scan_rsp_data, len))
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700891 return;
892
Johan Hedbergeb438b52013-10-16 15:31:07 +0300893 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
894 hdev->scan_rsp_data_len = len;
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700895
896 cp.length = len;
897
898 hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp);
899}
900
Arman Uguray4117ed72015-03-23 15:57:14 -0700901static void update_scan_rsp_data(struct hci_request *req)
902{
903 struct hci_dev *hdev = req->hdev;
904 u8 instance;
905
906 /* The "Set Advertising" setting supersedes the "Add Advertising"
907 * setting. Here we set the scan response data based on which
908 * setting was set. When neither apply, default to the global settings,
909 * represented by instance "0".
910 */
911 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
912 !hci_dev_test_flag(hdev, HCI_ADVERTISING))
913 instance = 0x01;
914 else
915 instance = 0x00;
916
917 update_scan_rsp_data_for_instance(req, instance);
918}
919
Johan Hedberg9a43e252013-10-20 19:00:07 +0300920static u8 get_adv_discov_flags(struct hci_dev *hdev)
921{
Johan Hedberg3b0602c2015-03-06 21:08:55 +0200922 struct mgmt_pending_cmd *cmd;
Johan Hedberg9a43e252013-10-20 19:00:07 +0300923
924 /* If there's a pending mgmt command the flags will not yet have
925 * their final values, so check for this first.
926 */
Johan Hedberg333ae952015-03-17 13:48:47 +0200927 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg9a43e252013-10-20 19:00:07 +0300928 if (cmd) {
929 struct mgmt_mode *cp = cmd->param;
930 if (cp->val == 0x01)
931 return LE_AD_GENERAL;
932 else if (cp->val == 0x02)
933 return LE_AD_LIMITED;
934 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700935 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300936 return LE_AD_LIMITED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700937 else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300938 return LE_AD_GENERAL;
939 }
940
941 return 0;
942}
943
Arman Uguraye7a685d2015-03-25 18:53:40 -0700944static u8 get_current_adv_instance(struct hci_dev *hdev)
Arman Uguray24b4f382015-03-23 15:57:12 -0700945{
Arman Uguray24b4f382015-03-23 15:57:12 -0700946 /* The "Set Advertising" setting supersedes the "Add Advertising"
947 * setting. Here we set the advertising data based on which
948 * setting was set. When neither apply, default to the global settings,
949 * represented by instance "0".
950 */
951 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
952 !hci_dev_test_flag(hdev, HCI_ADVERTISING))
Arman Uguraye7a685d2015-03-25 18:53:40 -0700953 return 0x01;
954
955 return 0x00;
956}
957
958static bool get_connectable(struct hci_dev *hdev)
959{
960 struct mgmt_pending_cmd *cmd;
961
962 /* If there's a pending mgmt command the flag will not yet have
963 * it's final value, so check for this first.
964 */
965 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
966 if (cmd) {
967 struct mgmt_mode *cp = cmd->param;
968
969 return cp->val;
970 }
971
972 return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
973}
974
975static u32 get_adv_instance_flags(struct hci_dev *hdev, u8 instance)
976{
977 u32 flags;
978
979 if (instance > 0x01)
980 return 0;
981
Arman Ugurayfdf51782015-03-25 18:53:46 -0700982 if (instance == 0x01)
Arman Uguraye7a685d2015-03-25 18:53:40 -0700983 return hdev->adv_instance.flags;
984
Arman Ugurayfdf51782015-03-25 18:53:46 -0700985 /* Instance 0 always manages the "Tx Power" and "Flags" fields */
986 flags = MGMT_ADV_FLAG_TX_POWER | MGMT_ADV_FLAG_MANAGED_FLAGS;
Arman Uguraye7a685d2015-03-25 18:53:40 -0700987
Arman Ugurayfaccb952015-03-28 12:38:58 -0700988 /* For instance 0, the HCI_ADVERTISING_CONNECTABLE setting corresponds
989 * to the "connectable" instance flag.
990 */
991 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE))
Arman Uguraye7a685d2015-03-25 18:53:40 -0700992 flags |= MGMT_ADV_FLAG_CONNECTABLE;
993
Arman Uguraye7a685d2015-03-25 18:53:40 -0700994 return flags;
995}
996
Arman Ugurayc7d48832015-03-28 12:38:59 -0700997static u8 get_adv_instance_scan_rsp_len(struct hci_dev *hdev, u8 instance)
998{
999 /* Ignore instance 0 and other unsupported instances */
1000 if (instance != 0x01)
1001 return 0;
1002
1003 /* TODO: Take into account the "appearance" and "local-name" flags here.
1004 * These are currently being ignored as they are not supported.
1005 */
1006 return hdev->adv_instance.scan_rsp_len;
1007}
1008
Arman Ugurayfdf51782015-03-25 18:53:46 -07001009static u8 create_instance_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
1010{
1011 u8 ad_len = 0, flags = 0;
1012 u32 instance_flags = get_adv_instance_flags(hdev, instance);
1013
1014 /* The Add Advertising command allows userspace to set both the general
1015 * and limited discoverable flags.
1016 */
1017 if (instance_flags & MGMT_ADV_FLAG_DISCOV)
1018 flags |= LE_AD_GENERAL;
1019
1020 if (instance_flags & MGMT_ADV_FLAG_LIMITED_DISCOV)
1021 flags |= LE_AD_LIMITED;
1022
1023 if (flags || (instance_flags & MGMT_ADV_FLAG_MANAGED_FLAGS)) {
1024 /* If a discovery flag wasn't provided, simply use the global
1025 * settings.
1026 */
1027 if (!flags)
1028 flags |= get_adv_discov_flags(hdev);
1029
1030 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1031 flags |= LE_AD_NO_BREDR;
1032
1033 /* If flags would still be empty, then there is no need to
1034 * include the "Flags" AD field".
1035 */
1036 if (flags) {
1037 ptr[0] = 0x02;
1038 ptr[1] = EIR_FLAGS;
1039 ptr[2] = flags;
1040
1041 ad_len += 3;
1042 ptr += 3;
1043 }
1044 }
1045
1046 /* Provide Tx Power only if we can provide a valid value for it */
1047 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID &&
1048 (instance_flags & MGMT_ADV_FLAG_TX_POWER)) {
1049 ptr[0] = 0x02;
1050 ptr[1] = EIR_TX_POWER;
1051 ptr[2] = (u8)hdev->adv_tx_power;
1052
1053 ad_len += 3;
1054 ptr += 3;
1055 }
1056
1057 if (instance) {
1058 memcpy(ptr, hdev->adv_instance.adv_data,
1059 hdev->adv_instance.adv_data_len);
1060 ad_len += hdev->adv_instance.adv_data_len;
1061 }
1062
1063 return ad_len;
1064}
1065
1066static void update_adv_data_for_instance(struct hci_request *req, u8 instance)
1067{
1068 struct hci_dev *hdev = req->hdev;
1069 struct hci_cp_le_set_adv_data cp;
1070 u8 len;
1071
1072 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1073 return;
1074
1075 memset(&cp, 0, sizeof(cp));
1076
1077 len = create_instance_adv_data(hdev, instance, cp.data);
1078
1079 /* There's nothing to do if the data hasn't changed */
1080 if (hdev->adv_data_len == len &&
1081 memcmp(cp.data, hdev->adv_data, len) == 0)
1082 return;
1083
1084 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1085 hdev->adv_data_len = len;
1086
1087 cp.length = len;
1088
1089 hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
1090}
1091
Arman Uguraye7a685d2015-03-25 18:53:40 -07001092static void update_adv_data(struct hci_request *req)
1093{
1094 struct hci_dev *hdev = req->hdev;
1095 u8 instance = get_current_adv_instance(hdev);
Arman Uguray24b4f382015-03-23 15:57:12 -07001096
1097 update_adv_data_for_instance(req, instance);
1098}
1099
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001100int mgmt_update_adv_data(struct hci_dev *hdev)
1101{
1102 struct hci_request req;
1103
1104 hci_req_init(&req, hdev);
1105 update_adv_data(&req);
1106
1107 return hci_req_run(&req, NULL);
1108}
1109
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001110static void create_eir(struct hci_dev *hdev, u8 *data)
1111{
1112 u8 *ptr = data;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001113 size_t name_len;
1114
1115 name_len = strlen(hdev->dev_name);
1116
1117 if (name_len > 0) {
1118 /* EIR Data type */
1119 if (name_len > 48) {
1120 name_len = 48;
1121 ptr[1] = EIR_NAME_SHORT;
1122 } else
1123 ptr[1] = EIR_NAME_COMPLETE;
1124
1125 /* EIR Data length */
1126 ptr[0] = name_len + 1;
1127
1128 memcpy(ptr + 2, hdev->dev_name, name_len);
1129
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001130 ptr += (name_len + 2);
1131 }
1132
Johan Hedbergbbaf4442012-11-08 01:22:59 +01001133 if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) {
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -07001134 ptr[0] = 2;
1135 ptr[1] = EIR_TX_POWER;
1136 ptr[2] = (u8) hdev->inq_tx_power;
1137
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -07001138 ptr += 3;
1139 }
1140
Marcel Holtmann2b9be132012-03-11 19:32:12 -07001141 if (hdev->devid_source > 0) {
1142 ptr[0] = 9;
1143 ptr[1] = EIR_DEVICE_ID;
1144
1145 put_unaligned_le16(hdev->devid_source, ptr + 2);
1146 put_unaligned_le16(hdev->devid_vendor, ptr + 4);
1147 put_unaligned_le16(hdev->devid_product, ptr + 6);
1148 put_unaligned_le16(hdev->devid_version, ptr + 8);
1149
Marcel Holtmann2b9be132012-03-11 19:32:12 -07001150 ptr += 10;
1151 }
1152
Johan Hedberg213202e2013-01-27 00:31:33 +02001153 ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedbergcdf19632013-01-27 00:31:34 +02001154 ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedbergc00d5752013-01-27 00:31:35 +02001155 ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001156}
1157
Johan Hedberg890ea892013-03-15 17:06:52 -05001158static void update_eir(struct hci_request *req)
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001159{
Johan Hedberg890ea892013-03-15 17:06:52 -05001160 struct hci_dev *hdev = req->hdev;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001161 struct hci_cp_write_eir cp;
1162
Johan Hedberg504c8dc2012-02-23 13:30:41 +02001163 if (!hdev_is_powered(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05001164 return;
Johan Hedberg7770c4a2012-02-22 22:06:38 +02001165
Johan Hedberg976eb202012-10-24 21:12:01 +03001166 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05001167 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001168
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001169 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberg890ea892013-03-15 17:06:52 -05001170 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001171
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001172 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg890ea892013-03-15 17:06:52 -05001173 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001174
1175 memset(&cp, 0, sizeof(cp));
1176
1177 create_eir(hdev, cp.data);
1178
1179 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
Johan Hedberg890ea892013-03-15 17:06:52 -05001180 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001181
1182 memcpy(hdev->eir, cp.data, sizeof(cp.data));
1183
Johan Hedberg890ea892013-03-15 17:06:52 -05001184 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001185}
1186
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001187static u8 get_service_classes(struct hci_dev *hdev)
1188{
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -03001189 struct bt_uuid *uuid;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001190 u8 val = 0;
1191
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -03001192 list_for_each_entry(uuid, &hdev->uuids, list)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001193 val |= uuid->svc_hint;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001194
1195 return val;
1196}
1197
Johan Hedberg890ea892013-03-15 17:06:52 -05001198static void update_class(struct hci_request *req)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001199{
Johan Hedberg890ea892013-03-15 17:06:52 -05001200 struct hci_dev *hdev = req->hdev;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001201 u8 cod[3];
1202
1203 BT_DBG("%s", hdev->name);
1204
Johan Hedberg504c8dc2012-02-23 13:30:41 +02001205 if (!hdev_is_powered(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05001206 return;
Johan Hedberg7770c4a2012-02-22 22:06:38 +02001207
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001208 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedbergf87ea1d2013-10-19 23:38:17 +03001209 return;
1210
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001211 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg890ea892013-03-15 17:06:52 -05001212 return;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001213
1214 cod[0] = hdev->minor_class;
1215 cod[1] = hdev->major_class;
1216 cod[2] = get_service_classes(hdev);
1217
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001218 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
Marcel Holtmann6acd7db2013-10-15 06:33:53 -07001219 cod[1] |= 0x20;
1220
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001221 if (memcmp(cod, hdev->dev_class, 3) == 0)
Johan Hedberg890ea892013-03-15 17:06:52 -05001222 return;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001223
Johan Hedberg890ea892013-03-15 17:06:52 -05001224 hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001225}
1226
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001227static void disable_advertising(struct hci_request *req)
1228{
1229 u8 enable = 0x00;
1230
1231 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1232}
1233
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001234static void enable_advertising(struct hci_request *req)
1235{
1236 struct hci_dev *hdev = req->hdev;
1237 struct hci_cp_le_set_adv_param cp;
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001238 u8 own_addr_type, enable = 0x01;
Johan Hedberga4858cb2014-02-25 19:56:31 +02001239 bool connectable;
Arman Uguraye7a685d2015-03-25 18:53:40 -07001240 u8 instance;
1241 u32 flags;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001242
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001243 if (hci_conn_num(hdev, LE_LINK) > 0)
1244 return;
1245
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001246 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001247 disable_advertising(req);
1248
Johan Hedberg5ce194c2014-07-08 15:07:49 +03001249 /* Clear the HCI_LE_ADV bit temporarily so that the
Johan Hedberg8d972502014-02-28 12:54:14 +02001250 * hci_update_random_address knows that it's safe to go ahead
1251 * and write a new random address. The flag will be set back on
1252 * as soon as the SET_ADV_ENABLE HCI command completes.
1253 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001254 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Johan Hedberg8d972502014-02-28 12:54:14 +02001255
Arman Uguraye7a685d2015-03-25 18:53:40 -07001256 instance = get_current_adv_instance(hdev);
1257 flags = get_adv_instance_flags(hdev, instance);
Arman Ugurayfaccb952015-03-28 12:38:58 -07001258
1259 /* If the "connectable" instance flag was not set, then choose between
1260 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1261 */
1262 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1263 get_connectable(hdev);
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001264
Johan Hedberga4858cb2014-02-25 19:56:31 +02001265 /* Set require_privacy to true only when non-connectable
1266 * advertising is used. In that case it is fine to use a
1267 * non-resolvable private address.
1268 */
1269 if (hci_update_random_address(req, !connectable, &own_addr_type) < 0)
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001270 return;
1271
Marcel Holtmann41c90c12014-02-23 20:25:55 -08001272 memset(&cp, 0, sizeof(cp));
Georg Lukas628531c2014-07-26 13:59:57 +02001273 cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval);
1274 cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval);
Arman Ugurayc7d48832015-03-28 12:38:59 -07001275
1276 if (connectable)
1277 cp.type = LE_ADV_IND;
1278 else if (get_adv_instance_scan_rsp_len(hdev, instance))
1279 cp.type = LE_ADV_SCAN_IND;
1280 else
1281 cp.type = LE_ADV_NONCONN_IND;
1282
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001283 cp.own_address_type = own_addr_type;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001284 cp.channel_map = hdev->le_adv_channel_map;
1285
1286 hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
1287
1288 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1289}
1290
Johan Hedberg7d785252011-12-15 00:47:39 +02001291static void service_cache_off(struct work_struct *work)
1292{
1293 struct hci_dev *hdev = container_of(work, struct hci_dev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001294 service_cache.work);
Johan Hedberg890ea892013-03-15 17:06:52 -05001295 struct hci_request req;
Johan Hedberg7d785252011-12-15 00:47:39 +02001296
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001297 if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg7d785252011-12-15 00:47:39 +02001298 return;
1299
Johan Hedberg890ea892013-03-15 17:06:52 -05001300 hci_req_init(&req, hdev);
1301
Johan Hedberg7d785252011-12-15 00:47:39 +02001302 hci_dev_lock(hdev);
1303
Johan Hedberg890ea892013-03-15 17:06:52 -05001304 update_eir(&req);
1305 update_class(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02001306
1307 hci_dev_unlock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -05001308
1309 hci_req_run(&req, NULL);
Johan Hedberg7d785252011-12-15 00:47:39 +02001310}
1311
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001312static void rpa_expired(struct work_struct *work)
1313{
1314 struct hci_dev *hdev = container_of(work, struct hci_dev,
1315 rpa_expired.work);
1316 struct hci_request req;
1317
1318 BT_DBG("");
1319
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001320 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001321
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001322 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001323 return;
1324
1325 /* The generation of a new RPA and programming it into the
1326 * controller happens in the enable_advertising() function.
1327 */
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001328 hci_req_init(&req, hdev);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001329 enable_advertising(&req);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001330 hci_req_run(&req, NULL);
1331}
1332
Johan Hedberg6a919082012-02-28 06:17:26 +02001333static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
Johan Hedberg7d785252011-12-15 00:47:39 +02001334{
Marcel Holtmann238be782015-03-13 02:11:06 -07001335 if (hci_dev_test_and_set_flag(hdev, HCI_MGMT))
Johan Hedberg6a919082012-02-28 06:17:26 +02001336 return;
1337
Johan Hedberg4f87da82012-03-02 19:55:56 +02001338 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001339 INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
Johan Hedberg7d785252011-12-15 00:47:39 +02001340
Johan Hedberg4f87da82012-03-02 19:55:56 +02001341 /* Non-mgmt controlled devices get this bit set
1342 * implicitly so that pairing works for them, however
1343 * for mgmt we require user-space to explicitly enable
1344 * it
1345 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001346 hci_dev_clear_flag(hdev, HCI_BONDABLE);
Johan Hedberg7d785252011-12-15 00:47:39 +02001347}
1348
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02001349static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001350 void *data, u16 data_len)
Johan Hedberg03811012010-12-08 00:21:06 +02001351{
1352 struct mgmt_rp_read_info rp;
Johan Hedberg03811012010-12-08 00:21:06 +02001353
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001354 BT_DBG("sock %p %s", sk, hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001355
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001356 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001357
Johan Hedberg03811012010-12-08 00:21:06 +02001358 memset(&rp, 0, sizeof(rp));
1359
Johan Hedberg03811012010-12-08 00:21:06 +02001360 bacpy(&rp.bdaddr, &hdev->bdaddr);
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001361
1362 rp.version = hdev->hci_ver;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02001363 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001364
1365 rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
1366 rp.current_settings = cpu_to_le32(get_current_settings(hdev));
1367
1368 memcpy(rp.dev_class, hdev->dev_class, 3);
Johan Hedberg03811012010-12-08 00:21:06 +02001369
1370 memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
Johan Hedberg27fcc362012-02-22 21:46:22 +02001371 memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
Johan Hedberg03811012010-12-08 00:21:06 +02001372
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001373 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001374
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001375 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
1376 sizeof(rp));
Johan Hedberg03811012010-12-08 00:21:06 +02001377}
1378
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001379static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
Johan Hedberg86805702011-11-11 16:18:52 +02001380{
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001381 __le32 settings = cpu_to_le32(get_current_settings(hdev));
Johan Hedberg86805702011-11-11 16:18:52 +02001382
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001383 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings,
1384 sizeof(settings));
Johan Hedberg86805702011-11-11 16:18:52 +02001385}
1386
Marcel Holtmann1904a852015-01-11 13:50:44 -08001387static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg8b064a32014-02-24 14:52:22 +02001388{
1389 BT_DBG("%s status 0x%02x", hdev->name, status);
1390
Johan Hedberga3172b72014-02-28 09:33:44 +02001391 if (hci_conn_count(hdev) == 0) {
1392 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001393 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberga3172b72014-02-28 09:33:44 +02001394 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001395}
1396
Johan Hedberg23a48092014-07-08 16:05:06 +03001397static bool hci_stop_discovery(struct hci_request *req)
Johan Hedberg21a60d32014-06-10 14:05:58 +03001398{
1399 struct hci_dev *hdev = req->hdev;
1400 struct hci_cp_remote_name_req_cancel cp;
1401 struct inquiry_entry *e;
1402
1403 switch (hdev->discovery.state) {
1404 case DISCOVERY_FINDING:
Jakub Pawlowski07d23342015-03-17 09:04:14 -07001405 if (test_bit(HCI_INQUIRY, &hdev->flags))
Johan Hedberg21a60d32014-06-10 14:05:58 +03001406 hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL);
Jakub Pawlowski07d23342015-03-17 09:04:14 -07001407
1408 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
Johan Hedberg21a60d32014-06-10 14:05:58 +03001409 cancel_delayed_work(&hdev->le_scan_disable);
1410 hci_req_add_le_scan_disable(req);
1411 }
1412
Johan Hedberg23a48092014-07-08 16:05:06 +03001413 return true;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001414
1415 case DISCOVERY_RESOLVING:
1416 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
1417 NAME_PENDING);
1418 if (!e)
Johan Hedberg23a48092014-07-08 16:05:06 +03001419 break;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001420
1421 bacpy(&cp.bdaddr, &e->data.bdaddr);
1422 hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp),
1423 &cp);
1424
Johan Hedberg23a48092014-07-08 16:05:06 +03001425 return true;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001426
1427 default:
1428 /* Passive scanning */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001429 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
Johan Hedberg21a60d32014-06-10 14:05:58 +03001430 hci_req_add_le_scan_disable(req);
Johan Hedberg23a48092014-07-08 16:05:06 +03001431 return true;
1432 }
1433
Johan Hedberg21a60d32014-06-10 14:05:58 +03001434 break;
1435 }
Johan Hedberg23a48092014-07-08 16:05:06 +03001436
1437 return false;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001438}
1439
Arman Uguray912098a2015-03-23 15:57:15 -07001440static void advertising_added(struct sock *sk, struct hci_dev *hdev,
1441 u8 instance)
1442{
1443 struct mgmt_ev_advertising_added ev;
1444
1445 ev.instance = instance;
1446
1447 mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk);
1448}
1449
1450static void advertising_removed(struct sock *sk, struct hci_dev *hdev,
1451 u8 instance)
1452{
1453 struct mgmt_ev_advertising_removed ev;
1454
1455 ev.instance = instance;
1456
1457 mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk);
1458}
1459
1460static void clear_adv_instance(struct hci_dev *hdev)
1461{
1462 struct hci_request req;
1463
1464 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
1465 return;
1466
1467 if (hdev->adv_instance.timeout)
1468 cancel_delayed_work(&hdev->adv_instance.timeout_exp);
1469
1470 memset(&hdev->adv_instance, 0, sizeof(hdev->adv_instance));
1471 advertising_removed(NULL, hdev, 1);
1472 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
1473
1474 if (!hdev_is_powered(hdev) ||
1475 hci_dev_test_flag(hdev, HCI_ADVERTISING))
1476 return;
1477
1478 hci_req_init(&req, hdev);
1479 disable_advertising(&req);
1480 hci_req_run(&req, NULL);
1481}
1482
Johan Hedberg8b064a32014-02-24 14:52:22 +02001483static int clean_up_hci_state(struct hci_dev *hdev)
1484{
1485 struct hci_request req;
1486 struct hci_conn *conn;
Johan Hedberg23a48092014-07-08 16:05:06 +03001487 bool discov_stopped;
1488 int err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001489
1490 hci_req_init(&req, hdev);
1491
1492 if (test_bit(HCI_ISCAN, &hdev->flags) ||
1493 test_bit(HCI_PSCAN, &hdev->flags)) {
1494 u8 scan = 0x00;
1495 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1496 }
1497
Arman Uguray912098a2015-03-23 15:57:15 -07001498 if (hdev->adv_instance.timeout)
1499 clear_adv_instance(hdev);
1500
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001501 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedberg8b064a32014-02-24 14:52:22 +02001502 disable_advertising(&req);
1503
Johan Hedberg23a48092014-07-08 16:05:06 +03001504 discov_stopped = hci_stop_discovery(&req);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001505
1506 list_for_each_entry(conn, &hdev->conn_hash.list, list) {
1507 struct hci_cp_disconnect dc;
Johan Hedbergc9910d02014-02-27 14:35:12 +02001508 struct hci_cp_reject_conn_req rej;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001509
Johan Hedbergc9910d02014-02-27 14:35:12 +02001510 switch (conn->state) {
1511 case BT_CONNECTED:
1512 case BT_CONFIG:
1513 dc.handle = cpu_to_le16(conn->handle);
1514 dc.reason = 0x15; /* Terminated due to Power Off */
1515 hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc);
1516 break;
1517 case BT_CONNECT:
1518 if (conn->type == LE_LINK)
1519 hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL,
1520 0, NULL);
1521 else if (conn->type == ACL_LINK)
1522 hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL,
1523 6, &conn->dst);
1524 break;
1525 case BT_CONNECT2:
1526 bacpy(&rej.bdaddr, &conn->dst);
1527 rej.reason = 0x15; /* Terminated due to Power Off */
1528 if (conn->type == ACL_LINK)
1529 hci_req_add(&req, HCI_OP_REJECT_CONN_REQ,
1530 sizeof(rej), &rej);
1531 else if (conn->type == SCO_LINK)
1532 hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ,
1533 sizeof(rej), &rej);
1534 break;
1535 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001536 }
1537
Johan Hedberg23a48092014-07-08 16:05:06 +03001538 err = hci_req_run(&req, clean_up_hci_complete);
1539 if (!err && discov_stopped)
1540 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
1541
1542 return err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001543}
1544
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001545static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001546 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001547{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001548 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001549 struct mgmt_pending_cmd *cmd;
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001550 int err;
Johan Hedberg03811012010-12-08 00:21:06 +02001551
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001552 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001553
Johan Hedberga7e80f22013-01-09 16:05:19 +02001554 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001555 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1556 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001557
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001558 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001559
Johan Hedberg333ae952015-03-17 13:48:47 +02001560 if (pending_find(MGMT_OP_SET_POWERED, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001561 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1562 MGMT_STATUS_BUSY);
Johan Hedberg87b95ba2013-09-25 13:26:06 +03001563 goto failed;
1564 }
1565
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001566 if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
Marcel Holtmannf0d4b782012-02-21 12:14:25 +01001567 cancel_delayed_work(&hdev->power_off);
1568
1569 if (cp->val) {
Johan Hedberga1d70452013-01-09 15:29:40 +02001570 mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev,
1571 data, len);
1572 err = mgmt_powered(hdev, 1);
Marcel Holtmannf0d4b782012-02-21 12:14:25 +01001573 goto failed;
1574 }
1575 }
1576
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001577 if (!!cp->val == hdev_is_powered(hdev)) {
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001578 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001579 goto failed;
1580 }
1581
Johan Hedberg03811012010-12-08 00:21:06 +02001582 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
1583 if (!cmd) {
1584 err = -ENOMEM;
1585 goto failed;
1586 }
1587
Johan Hedberg8b064a32014-02-24 14:52:22 +02001588 if (cp->val) {
Johan Hedberg19202572013-01-14 22:33:51 +02001589 queue_work(hdev->req_workqueue, &hdev->power_on);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001590 err = 0;
1591 } else {
1592 /* Disconnect connections, stop scans, etc */
1593 err = clean_up_hci_state(hdev);
Johan Hedberga3172b72014-02-28 09:33:44 +02001594 if (!err)
1595 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
1596 HCI_POWER_OFF_TIMEOUT);
Johan Hedberg03811012010-12-08 00:21:06 +02001597
Johan Hedberg8b064a32014-02-24 14:52:22 +02001598 /* ENODATA means there were no HCI commands queued */
1599 if (err == -ENODATA) {
Johan Hedberga3172b72014-02-28 09:33:44 +02001600 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001601 queue_work(hdev->req_workqueue, &hdev->power_off.work);
1602 err = 0;
1603 }
1604 }
Johan Hedberg03811012010-12-08 00:21:06 +02001605
1606failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001607 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001608 return err;
1609}
1610
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001611static int new_settings(struct hci_dev *hdev, struct sock *skip)
1612{
Marcel Holtmannf6b77122015-03-14 19:28:05 -07001613 __le32 ev = cpu_to_le32(get_current_settings(hdev));
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001614
Marcel Holtmannf6b77122015-03-14 19:28:05 -07001615 return mgmt_generic_event(MGMT_EV_NEW_SETTINGS, hdev, &ev,
1616 sizeof(ev), skip);
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001617}
1618
Johan Hedberg91a668b2014-07-09 13:28:26 +03001619int mgmt_new_settings(struct hci_dev *hdev)
1620{
1621 return new_settings(hdev, NULL);
1622}
1623
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001624struct cmd_lookup {
1625 struct sock *sk;
1626 struct hci_dev *hdev;
1627 u8 mgmt_status;
1628};
1629
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001630static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001631{
1632 struct cmd_lookup *match = data;
1633
1634 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
1635
1636 list_del(&cmd->list);
1637
1638 if (match->sk == NULL) {
1639 match->sk = cmd->sk;
1640 sock_hold(match->sk);
1641 }
1642
1643 mgmt_pending_free(cmd);
1644}
1645
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001646static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001647{
1648 u8 *status = data;
1649
Johan Hedberga69e8372015-03-06 21:08:53 +02001650 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001651 mgmt_pending_remove(cmd);
1652}
1653
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001654static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg1b9b5ee2014-12-05 13:36:00 +02001655{
1656 if (cmd->cmd_complete) {
1657 u8 *status = data;
1658
1659 cmd->cmd_complete(cmd, *status);
1660 mgmt_pending_remove(cmd);
1661
1662 return;
1663 }
1664
1665 cmd_status_rsp(cmd, data);
1666}
1667
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001668static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedbergf5818c22014-12-05 13:36:02 +02001669{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001670 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1671 cmd->param, cmd->param_len);
Johan Hedbergf5818c22014-12-05 13:36:02 +02001672}
1673
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001674static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001675{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001676 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1677 cmd->param, sizeof(struct mgmt_addr_info));
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001678}
1679
Johan Hedberge6fe7982013-10-02 15:45:22 +03001680static u8 mgmt_bredr_support(struct hci_dev *hdev)
1681{
1682 if (!lmp_bredr_capable(hdev))
1683 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001684 else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001685 return MGMT_STATUS_REJECTED;
1686 else
1687 return MGMT_STATUS_SUCCESS;
1688}
1689
1690static u8 mgmt_le_support(struct hci_dev *hdev)
1691{
1692 if (!lmp_le_capable(hdev))
1693 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001694 else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001695 return MGMT_STATUS_REJECTED;
1696 else
1697 return MGMT_STATUS_SUCCESS;
1698}
1699
Marcel Holtmann1904a852015-01-11 13:50:44 -08001700static void set_discoverable_complete(struct hci_dev *hdev, u8 status,
1701 u16 opcode)
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001702{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001703 struct mgmt_pending_cmd *cmd;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001704 struct mgmt_mode *cp;
Marcel Holtmann970ba522013-10-15 06:33:57 -07001705 struct hci_request req;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001706 bool changed;
1707
1708 BT_DBG("status 0x%02x", status);
1709
1710 hci_dev_lock(hdev);
1711
Johan Hedberg333ae952015-03-17 13:48:47 +02001712 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001713 if (!cmd)
1714 goto unlock;
1715
1716 if (status) {
1717 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001718 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001719 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001720 goto remove_cmd;
1721 }
1722
1723 cp = cmd->param;
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001724 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001725 changed = !hci_dev_test_and_set_flag(hdev, HCI_DISCOVERABLE);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001726
1727 if (hdev->discov_timeout > 0) {
1728 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1729 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
1730 to);
1731 }
1732 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001733 changed = hci_dev_test_and_clear_flag(hdev, HCI_DISCOVERABLE);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001734 }
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001735
1736 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1737
1738 if (changed)
1739 new_settings(hdev, cmd->sk);
1740
Marcel Holtmann970ba522013-10-15 06:33:57 -07001741 /* When the discoverable mode gets changed, make sure
1742 * that class of device has the limited discoverable
Johan Hedberg432df052014-08-01 11:13:31 +03001743 * bit correctly set. Also update page scan based on whitelist
1744 * entries.
Marcel Holtmann970ba522013-10-15 06:33:57 -07001745 */
1746 hci_req_init(&req, hdev);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02001747 __hci_update_page_scan(&req);
Marcel Holtmann970ba522013-10-15 06:33:57 -07001748 update_class(&req);
1749 hci_req_run(&req, NULL);
1750
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001751remove_cmd:
1752 mgmt_pending_remove(cmd);
1753
1754unlock:
1755 hci_dev_unlock(hdev);
1756}
1757
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001758static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001759 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001760{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001761 struct mgmt_cp_set_discoverable *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001762 struct mgmt_pending_cmd *cmd;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001763 struct hci_request req;
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001764 u16 timeout;
Johan Hedberg9a43e252013-10-20 19:00:07 +03001765 u8 scan;
Johan Hedberg03811012010-12-08 00:21:06 +02001766 int err;
Johan Hedberge41d8b42010-12-13 21:07:03 +02001767
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001768 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001769
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001770 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1771 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001772 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1773 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001774
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001775 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02001776 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1777 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001778
Marcel Holtmann1f350c82012-03-12 20:31:08 -07001779 timeout = __le16_to_cpu(cp->timeout);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001780
1781 /* Disabling discoverable requires that no timeout is set,
1782 * and enabling limited discoverable requires a timeout.
1783 */
1784 if ((cp->val == 0x00 && timeout > 0) ||
1785 (cp->val == 0x02 && timeout == 0))
Johan Hedberga69e8372015-03-06 21:08:53 +02001786 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1787 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001788
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001789 hci_dev_lock(hdev);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001790
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001791 if (!hdev_is_powered(hdev) && timeout > 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001792 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1793 MGMT_STATUS_NOT_POWERED);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001794 goto failed;
1795 }
1796
Johan Hedberg333ae952015-03-17 13:48:47 +02001797 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1798 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001799 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1800 MGMT_STATUS_BUSY);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001801 goto failed;
1802 }
1803
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001804 if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001805 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1806 MGMT_STATUS_REJECTED);
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001807 goto failed;
1808 }
1809
1810 if (!hdev_is_powered(hdev)) {
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001811 bool changed = false;
1812
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001813 /* Setting limited discoverable when powered off is
1814 * not a valid operation since it requires a timeout
1815 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1816 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001817 if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07001818 hci_dev_change_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001819 changed = true;
1820 }
1821
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001822 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001823 if (err < 0)
1824 goto failed;
1825
1826 if (changed)
1827 err = new_settings(hdev, sk);
1828
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001829 goto failed;
1830 }
1831
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001832 /* If the current mode is the same, then just update the timeout
1833 * value with the new value. And if only the timeout gets updated,
1834 * then no need for any HCI transactions.
1835 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001836 if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1837 (cp->val == 0x02) == hci_dev_test_flag(hdev,
1838 HCI_LIMITED_DISCOVERABLE)) {
Marcel Holtmann36261542013-10-15 08:28:51 -07001839 cancel_delayed_work(&hdev->discov_off);
1840 hdev->discov_timeout = timeout;
Marcel Holtmann955638e2012-02-22 18:21:00 +01001841
Marcel Holtmann36261542013-10-15 08:28:51 -07001842 if (cp->val && hdev->discov_timeout > 0) {
1843 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001844 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
Marcel Holtmann36261542013-10-15 08:28:51 -07001845 to);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001846 }
1847
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001848 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001849 goto failed;
1850 }
1851
1852 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
1853 if (!cmd) {
1854 err = -ENOMEM;
1855 goto failed;
1856 }
1857
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001858 /* Cancel any potential discoverable timeout that might be
1859 * still active and store new timeout value. The arming of
1860 * the timeout happens in the complete handler.
1861 */
1862 cancel_delayed_work(&hdev->discov_off);
1863 hdev->discov_timeout = timeout;
1864
Johan Hedbergb456f872013-10-19 23:38:22 +03001865 /* Limited discoverable mode */
1866 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001867 hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001868 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001869 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001870
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001871 hci_req_init(&req, hdev);
1872
Johan Hedberg9a43e252013-10-20 19:00:07 +03001873 /* The procedure for LE-only controllers is much simpler - just
1874 * update the advertising data.
1875 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001876 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberg9a43e252013-10-20 19:00:07 +03001877 goto update_ad;
1878
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001879 scan = SCAN_PAGE;
1880
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001881 if (cp->val) {
1882 struct hci_cp_write_current_iac_lap hci_cp;
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001883
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001884 if (cp->val == 0x02) {
1885 /* Limited discoverable mode */
Marcel Holtmann33337dc2013-10-23 08:28:01 -07001886 hci_cp.num_iac = min_t(u8, hdev->num_iac, 2);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001887 hci_cp.iac_lap[0] = 0x00; /* LIAC */
1888 hci_cp.iac_lap[1] = 0x8b;
1889 hci_cp.iac_lap[2] = 0x9e;
1890 hci_cp.iac_lap[3] = 0x33; /* GIAC */
1891 hci_cp.iac_lap[4] = 0x8b;
1892 hci_cp.iac_lap[5] = 0x9e;
1893 } else {
1894 /* General discoverable mode */
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001895 hci_cp.num_iac = 1;
1896 hci_cp.iac_lap[0] = 0x33; /* GIAC */
1897 hci_cp.iac_lap[1] = 0x8b;
1898 hci_cp.iac_lap[2] = 0x9e;
1899 }
1900
1901 hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP,
1902 (hci_cp.num_iac * 3) + 1, &hci_cp);
1903
1904 scan |= SCAN_INQUIRY;
1905 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001906 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001907 }
1908
1909 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001910
Johan Hedberg9a43e252013-10-20 19:00:07 +03001911update_ad:
1912 update_adv_data(&req);
1913
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001914 err = hci_req_run(&req, set_discoverable_complete);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001915 if (err < 0)
1916 mgmt_pending_remove(cmd);
1917
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001918failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001919 hci_dev_unlock(hdev);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001920 return err;
1921}
1922
Johan Hedberg406d7802013-03-15 17:07:09 -05001923static void write_fast_connectable(struct hci_request *req, bool enable)
1924{
Johan Hedbergbd98b992013-03-15 17:07:13 -05001925 struct hci_dev *hdev = req->hdev;
Johan Hedberg406d7802013-03-15 17:07:09 -05001926 struct hci_cp_write_page_scan_activity acp;
1927 u8 type;
1928
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001929 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberg547003b2013-10-21 16:51:53 +03001930 return;
1931
Johan Hedberg4c01f8b2013-03-15 17:07:14 -05001932 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
1933 return;
1934
Johan Hedberg406d7802013-03-15 17:07:09 -05001935 if (enable) {
1936 type = PAGE_SCAN_TYPE_INTERLACED;
1937
1938 /* 160 msec page scan interval */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001939 acp.interval = cpu_to_le16(0x0100);
Johan Hedberg406d7802013-03-15 17:07:09 -05001940 } else {
1941 type = PAGE_SCAN_TYPE_STANDARD; /* default */
1942
1943 /* default 1.28 sec page scan */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001944 acp.interval = cpu_to_le16(0x0800);
Johan Hedberg406d7802013-03-15 17:07:09 -05001945 }
1946
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001947 acp.window = cpu_to_le16(0x0012);
Johan Hedberg406d7802013-03-15 17:07:09 -05001948
Johan Hedbergbd98b992013-03-15 17:07:13 -05001949 if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval ||
1950 __cpu_to_le16(hdev->page_scan_window) != acp.window)
1951 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
1952 sizeof(acp), &acp);
1953
1954 if (hdev->page_scan_type != type)
1955 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type);
Johan Hedberg406d7802013-03-15 17:07:09 -05001956}
1957
Marcel Holtmann1904a852015-01-11 13:50:44 -08001958static void set_connectable_complete(struct hci_dev *hdev, u8 status,
1959 u16 opcode)
Johan Hedberg2b76f452013-03-15 17:07:04 -05001960{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001961 struct mgmt_pending_cmd *cmd;
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001962 struct mgmt_mode *cp;
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001963 bool conn_changed, discov_changed;
Johan Hedberg2b76f452013-03-15 17:07:04 -05001964
1965 BT_DBG("status 0x%02x", status);
1966
1967 hci_dev_lock(hdev);
1968
Johan Hedberg333ae952015-03-17 13:48:47 +02001969 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
Johan Hedberg2b76f452013-03-15 17:07:04 -05001970 if (!cmd)
1971 goto unlock;
1972
Johan Hedberg37438c12013-10-14 16:20:05 +03001973 if (status) {
1974 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001975 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg37438c12013-10-14 16:20:05 +03001976 goto remove_cmd;
1977 }
1978
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001979 cp = cmd->param;
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001980 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001981 conn_changed = !hci_dev_test_and_set_flag(hdev,
1982 HCI_CONNECTABLE);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001983 discov_changed = false;
1984 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001985 conn_changed = hci_dev_test_and_clear_flag(hdev,
1986 HCI_CONNECTABLE);
1987 discov_changed = hci_dev_test_and_clear_flag(hdev,
1988 HCI_DISCOVERABLE);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001989 }
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001990
Johan Hedberg2b76f452013-03-15 17:07:04 -05001991 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
1992
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001993 if (conn_changed || discov_changed) {
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001994 new_settings(hdev, cmd->sk);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02001995 hci_update_page_scan(hdev);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001996 if (discov_changed)
1997 mgmt_update_adv_data(hdev);
Johan Hedberg2b7be332014-07-07 14:40:22 +03001998 hci_update_background_scan(hdev);
1999 }
Johan Hedbergd7b856f2013-10-14 16:20:04 +03002000
Johan Hedberg37438c12013-10-14 16:20:05 +03002001remove_cmd:
Johan Hedberg2b76f452013-03-15 17:07:04 -05002002 mgmt_pending_remove(cmd);
2003
2004unlock:
2005 hci_dev_unlock(hdev);
2006}
2007
Johan Hedberge8ba3a12013-10-19 23:38:18 +03002008static int set_connectable_update_settings(struct hci_dev *hdev,
2009 struct sock *sk, u8 val)
2010{
2011 bool changed = false;
2012 int err;
2013
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002014 if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE))
Johan Hedberge8ba3a12013-10-19 23:38:18 +03002015 changed = true;
2016
2017 if (val) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07002018 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
Johan Hedberge8ba3a12013-10-19 23:38:18 +03002019 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002020 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
2021 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberge8ba3a12013-10-19 23:38:18 +03002022 }
2023
2024 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
2025 if (err < 0)
2026 return err;
2027
Johan Hedberg562064e2014-07-08 16:35:34 +03002028 if (changed) {
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02002029 hci_update_page_scan(hdev);
Johan Hedberg562064e2014-07-08 16:35:34 +03002030 hci_update_background_scan(hdev);
Johan Hedberge8ba3a12013-10-19 23:38:18 +03002031 return new_settings(hdev, sk);
Johan Hedberg562064e2014-07-08 16:35:34 +03002032 }
Johan Hedberge8ba3a12013-10-19 23:38:18 +03002033
2034 return 0;
2035}
2036
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002037static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002038 u16 len)
Johan Hedbergf7b64e62010-12-13 21:07:06 +02002039{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002040 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002041 struct mgmt_pending_cmd *cmd;
Johan Hedberg2b76f452013-03-15 17:07:04 -05002042 struct hci_request req;
Johan Hedberg1987fdc2013-10-14 21:15:24 +03002043 u8 scan;
Johan Hedbergf7b64e62010-12-13 21:07:06 +02002044 int err;
Johan Hedberg03811012010-12-08 00:21:06 +02002045
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002046 BT_DBG("request for %s", hdev->name);
Johan Hedberge41d8b42010-12-13 21:07:03 +02002047
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002048 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
2049 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02002050 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
2051 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03002052
Johan Hedberga7e80f22013-01-09 16:05:19 +02002053 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002054 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
2055 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002056
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002057 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002058
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002059 if (!hdev_is_powered(hdev)) {
Johan Hedberge8ba3a12013-10-19 23:38:18 +03002060 err = set_connectable_update_settings(hdev, sk, cp->val);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002061 goto failed;
2062 }
2063
Johan Hedberg333ae952015-03-17 13:48:47 +02002064 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
2065 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002066 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
2067 MGMT_STATUS_BUSY);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002068 goto failed;
2069 }
2070
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002071 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
2072 if (!cmd) {
2073 err = -ENOMEM;
2074 goto failed;
2075 }
2076
Johan Hedberg2b76f452013-03-15 17:07:04 -05002077 hci_req_init(&req, hdev);
2078
Johan Hedberg9a43e252013-10-20 19:00:07 +03002079 /* If BR/EDR is not enabled and we disable advertising as a
2080 * by-product of disabling connectable, we need to update the
2081 * advertising flags.
2082 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002083 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg9a43e252013-10-20 19:00:07 +03002084 if (!cp->val) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002085 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2086 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberg9a43e252013-10-20 19:00:07 +03002087 }
2088 update_adv_data(&req);
2089 } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) {
Johan Hedberg9b742462013-10-14 16:20:03 +03002090 if (cp->val) {
2091 scan = SCAN_PAGE;
2092 } else {
Johan Hedberg3bd27242014-07-28 20:53:58 +03002093 /* If we don't have any whitelist entries just
2094 * disable all scanning. If there are entries
2095 * and we had both page and inquiry scanning
2096 * enabled then fall back to only page scanning.
2097 * Otherwise no changes are needed.
2098 */
2099 if (list_empty(&hdev->whitelist))
2100 scan = SCAN_DISABLED;
2101 else if (test_bit(HCI_ISCAN, &hdev->flags))
2102 scan = SCAN_PAGE;
2103 else
2104 goto no_scan_update;
Johan Hedberg9b742462013-10-14 16:20:03 +03002105
2106 if (test_bit(HCI_ISCAN, &hdev->flags) &&
Marcel Holtmann8d6083f2013-10-14 16:38:45 -07002107 hdev->discov_timeout > 0)
Johan Hedberg9b742462013-10-14 16:20:03 +03002108 cancel_delayed_work(&hdev->discov_off);
2109 }
2110
2111 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
2112 }
Johan Hedberg2b76f452013-03-15 17:07:04 -05002113
Johan Hedberg3bd27242014-07-28 20:53:58 +03002114no_scan_update:
Johan Hedberge8b12022014-07-10 10:51:27 +03002115 /* Update the advertising parameters if necessary */
Arman Uguray880897d2015-03-28 12:39:00 -07002116 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
2117 hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
Johan Hedberg1987fdc2013-10-14 21:15:24 +03002118 enable_advertising(&req);
Johan Hedberg1987fdc2013-10-14 21:15:24 +03002119
Johan Hedberg2b76f452013-03-15 17:07:04 -05002120 err = hci_req_run(&req, set_connectable_complete);
Johan Hedberg9b742462013-10-14 16:20:03 +03002121 if (err < 0) {
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002122 mgmt_pending_remove(cmd);
Johan Hedberg9b742462013-10-14 16:20:03 +03002123 if (err == -ENODATA)
Johan Hedberga81070b2013-10-19 23:38:19 +03002124 err = set_connectable_update_settings(hdev, sk,
2125 cp->val);
Johan Hedberg9b742462013-10-14 16:20:03 +03002126 goto failed;
2127 }
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002128
2129failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002130 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002131 return err;
2132}
2133
Johan Hedbergb2939472014-07-30 09:22:23 +03002134static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002135 u16 len)
Johan Hedberg73f22f62010-12-29 16:00:25 +02002136{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002137 struct mgmt_mode *cp = data;
Marcel Holtmann55594352013-10-06 16:11:57 -07002138 bool changed;
Johan Hedberg73f22f62010-12-29 16:00:25 +02002139 int err;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002140
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002141 BT_DBG("request for %s", hdev->name);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002142
Johan Hedberga7e80f22013-01-09 16:05:19 +02002143 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002144 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE,
2145 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002146
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002147 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002148
2149 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07002150 changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002151 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002152 changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002153
Johan Hedbergb2939472014-07-30 09:22:23 +03002154 err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002155 if (err < 0)
Marcel Holtmann55594352013-10-06 16:11:57 -07002156 goto unlock;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002157
Marcel Holtmann55594352013-10-06 16:11:57 -07002158 if (changed)
2159 err = new_settings(hdev, sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002160
Marcel Holtmann55594352013-10-06 16:11:57 -07002161unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002162 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002163 return err;
2164}
Johan Hedberg72a734e2010-12-30 00:38:22 +02002165
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002166static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
2167 u16 len)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002168{
2169 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002170 struct mgmt_pending_cmd *cmd;
Johan Hedberge6fe7982013-10-02 15:45:22 +03002171 u8 val, status;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002172 int err;
2173
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002174 BT_DBG("request for %s", hdev->name);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002175
Johan Hedberge6fe7982013-10-02 15:45:22 +03002176 status = mgmt_bredr_support(hdev);
2177 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02002178 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2179 status);
Johan Hedberg33c525c2012-10-24 21:11:58 +03002180
Johan Hedberga7e80f22013-01-09 16:05:19 +02002181 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002182 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2183 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002184
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002185 hci_dev_lock(hdev);
2186
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002187 if (!hdev_is_powered(hdev)) {
Johan Hedberg47990ea2012-02-22 11:58:37 +02002188 bool changed = false;
2189
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002190 if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07002191 hci_dev_change_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02002192 changed = true;
2193 }
2194
2195 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
2196 if (err < 0)
2197 goto failed;
2198
2199 if (changed)
2200 err = new_settings(hdev, sk);
2201
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002202 goto failed;
2203 }
2204
Johan Hedberg333ae952015-03-17 13:48:47 +02002205 if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002206 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2207 MGMT_STATUS_BUSY);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002208 goto failed;
2209 }
2210
2211 val = !!cp->val;
2212
2213 if (test_bit(HCI_AUTH, &hdev->flags) == val) {
2214 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
2215 goto failed;
2216 }
2217
2218 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
2219 if (!cmd) {
2220 err = -ENOMEM;
2221 goto failed;
2222 }
2223
2224 err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
2225 if (err < 0) {
2226 mgmt_pending_remove(cmd);
2227 goto failed;
2228 }
2229
2230failed:
2231 hci_dev_unlock(hdev);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002232 return err;
2233}
2234
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002235static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002236{
2237 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002238 struct mgmt_pending_cmd *cmd;
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07002239 u8 status;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002240 int err;
2241
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002242 BT_DBG("request for %s", hdev->name);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002243
Marcel Holtmanncdba5282013-10-02 21:31:52 -07002244 status = mgmt_bredr_support(hdev);
2245 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02002246 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
Marcel Holtmanncdba5282013-10-02 21:31:52 -07002247
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002248 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002249 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2250 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002251
Johan Hedberga7e80f22013-01-09 16:05:19 +02002252 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002253 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2254 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002255
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002256 hci_dev_lock(hdev);
Johan Hedberg6c8f12c2012-02-22 16:35:26 +02002257
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002258 if (!hdev_is_powered(hdev)) {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002259 bool changed;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02002260
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002261 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07002262 changed = !hci_dev_test_and_set_flag(hdev,
2263 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002264 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002265 changed = hci_dev_test_and_clear_flag(hdev,
2266 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002267 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002268 changed = hci_dev_test_and_clear_flag(hdev,
2269 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002270 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002271 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02002272 }
2273
2274 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2275 if (err < 0)
2276 goto failed;
2277
2278 if (changed)
2279 err = new_settings(hdev, sk);
2280
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002281 goto failed;
2282 }
2283
Johan Hedberg333ae952015-03-17 13:48:47 +02002284 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002285 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2286 MGMT_STATUS_BUSY);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002287 goto failed;
2288 }
2289
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002290 if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002291 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2292 goto failed;
2293 }
2294
2295 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
2296 if (!cmd) {
2297 err = -ENOMEM;
2298 goto failed;
2299 }
2300
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002301 if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03002302 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2303 sizeof(cp->val), &cp->val);
2304
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07002305 err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002306 if (err < 0) {
2307 mgmt_pending_remove(cmd);
2308 goto failed;
2309 }
2310
2311failed:
2312 hci_dev_unlock(hdev);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002313 return err;
2314}
2315
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002316static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002317{
2318 struct mgmt_mode *cp = data;
Marcel Holtmannee392692013-10-01 22:59:23 -07002319 bool changed;
Johan Hedberge6fe7982013-10-02 15:45:22 +03002320 u8 status;
Marcel Holtmannee392692013-10-01 22:59:23 -07002321 int err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002322
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002323 BT_DBG("request for %s", hdev->name);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002324
Johan Hedberge6fe7982013-10-02 15:45:22 +03002325 status = mgmt_bredr_support(hdev);
2326 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02002327 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002328
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002329 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002330 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2331 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002332
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002333 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02002334 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2335 MGMT_STATUS_REJECTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002336
Johan Hedberga7e80f22013-01-09 16:05:19 +02002337 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002338 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2339 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002340
Marcel Holtmannee392692013-10-01 22:59:23 -07002341 hci_dev_lock(hdev);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002342
Johan Hedberg333ae952015-03-17 13:48:47 +02002343 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002344 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2345 MGMT_STATUS_BUSY);
Johan Hedberga2cb01d2015-02-19 17:38:07 +02002346 goto unlock;
2347 }
2348
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002349 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07002350 changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002351 } else {
2352 if (hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002353 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2354 MGMT_STATUS_REJECTED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002355 goto unlock;
2356 }
2357
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002358 changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002359 }
Marcel Holtmannee392692013-10-01 22:59:23 -07002360
2361 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
2362 if (err < 0)
2363 goto unlock;
2364
2365 if (changed)
2366 err = new_settings(hdev, sk);
2367
2368unlock:
2369 hci_dev_unlock(hdev);
2370 return err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002371}
2372
Marcel Holtmann1904a852015-01-11 13:50:44 -08002373static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002374{
2375 struct cmd_lookup match = { NULL, hdev };
2376
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302377 hci_dev_lock(hdev);
2378
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002379 if (status) {
2380 u8 mgmt_err = mgmt_status(status);
2381
2382 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
2383 &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302384 goto unlock;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002385 }
2386
2387 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
2388
2389 new_settings(hdev, match.sk);
2390
2391 if (match.sk)
2392 sock_put(match.sk);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002393
2394 /* Make sure the controller has a good default for
2395 * advertising data. Restrict the update to when LE
2396 * has actually been enabled. During power on, the
2397 * update in powered_update_hci will take care of it.
2398 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002399 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002400 struct hci_request req;
2401
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002402 hci_req_init(&req, hdev);
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07002403 update_adv_data(&req);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07002404 update_scan_rsp_data(&req);
Johan Hedberg2cf22212014-12-19 22:26:00 +02002405 __hci_update_background_scan(&req);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002406 hci_req_run(&req, NULL);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002407 }
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302408
2409unlock:
2410 hci_dev_unlock(hdev);
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002411}
2412
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002413static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg06199cf2012-02-22 16:37:11 +02002414{
2415 struct mgmt_mode *cp = data;
2416 struct hci_cp_write_le_host_supported hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002417 struct mgmt_pending_cmd *cmd;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002418 struct hci_request req;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002419 int err;
Johan Hedberg0b60eba2012-02-28 00:57:24 +02002420 u8 val, enabled;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002421
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002422 BT_DBG("request for %s", hdev->name);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002423
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002424 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002425 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2426 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg1de028c2012-02-29 19:55:35 -08002427
Johan Hedberga7e80f22013-01-09 16:05:19 +02002428 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002429 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2430 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002431
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07002432 /* Bluetooth single mode LE only controllers or dual-mode
2433 * controllers configured as LE only devices, do not allow
2434 * switching LE off. These have either LE enabled explicitly
2435 * or BR/EDR has been previously switched off.
2436 *
2437 * When trying to enable an already enabled LE, then gracefully
2438 * send a positive response. Trying to disable it however will
2439 * result into rejection.
2440 */
2441 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
2442 if (cp->val == 0x01)
2443 return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2444
Johan Hedberga69e8372015-03-06 21:08:53 +02002445 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2446 MGMT_STATUS_REJECTED);
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07002447 }
Johan Hedbergc73eee92013-04-19 18:35:21 +03002448
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002449 hci_dev_lock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002450
2451 val = !!cp->val;
Gustavo Padovanffa88e02012-11-23 16:50:51 -02002452 enabled = lmp_host_le_capable(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002453
Johan Hedberg0b60eba2012-02-28 00:57:24 +02002454 if (!hdev_is_powered(hdev) || val == enabled) {
Johan Hedberg06199cf2012-02-22 16:37:11 +02002455 bool changed = false;
2456
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002457 if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07002458 hci_dev_change_flag(hdev, HCI_LE_ENABLED);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002459 changed = true;
2460 }
2461
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002462 if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002463 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03002464 changed = true;
2465 }
2466
Johan Hedberg06199cf2012-02-22 16:37:11 +02002467 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2468 if (err < 0)
Johan Hedberg1de028c2012-02-29 19:55:35 -08002469 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002470
2471 if (changed)
2472 err = new_settings(hdev, sk);
2473
Johan Hedberg1de028c2012-02-29 19:55:35 -08002474 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002475 }
2476
Johan Hedberg333ae952015-03-17 13:48:47 +02002477 if (pending_find(MGMT_OP_SET_LE, hdev) ||
2478 pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002479 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2480 MGMT_STATUS_BUSY);
Johan Hedberg1de028c2012-02-29 19:55:35 -08002481 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002482 }
2483
2484 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
2485 if (!cmd) {
2486 err = -ENOMEM;
Johan Hedberg1de028c2012-02-29 19:55:35 -08002487 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002488 }
2489
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002490 hci_req_init(&req, hdev);
2491
Johan Hedberg06199cf2012-02-22 16:37:11 +02002492 memset(&hci_cp, 0, sizeof(hci_cp));
2493
2494 if (val) {
2495 hci_cp.le = val;
Marcel Holtmann32226e42014-07-24 20:04:16 +02002496 hci_cp.simul = 0x00;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002497 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002498 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002499 disable_advertising(&req);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002500 }
2501
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002502 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp),
2503 &hci_cp);
2504
2505 err = hci_req_run(&req, le_enable_complete);
Syam Sidhardhan0c01bc42012-04-12 20:33:21 +05302506 if (err < 0)
Johan Hedberg06199cf2012-02-22 16:37:11 +02002507 mgmt_pending_remove(cmd);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002508
Johan Hedberg1de028c2012-02-29 19:55:35 -08002509unlock:
2510 hci_dev_unlock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002511 return err;
2512}
2513
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002514/* This is a helper function to test for pending mgmt commands that can
2515 * cause CoD or EIR HCI commands. We can only allow one such pending
2516 * mgmt command at a time since otherwise we cannot easily track what
2517 * the current values are, will be, and based on that calculate if a new
2518 * HCI command needs to be sent and if yes with what value.
2519 */
2520static bool pending_eir_or_class(struct hci_dev *hdev)
2521{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002522 struct mgmt_pending_cmd *cmd;
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002523
2524 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
2525 switch (cmd->opcode) {
2526 case MGMT_OP_ADD_UUID:
2527 case MGMT_OP_REMOVE_UUID:
2528 case MGMT_OP_SET_DEV_CLASS:
2529 case MGMT_OP_SET_POWERED:
2530 return true;
2531 }
2532 }
2533
2534 return false;
2535}
2536
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002537static const u8 bluetooth_base_uuid[] = {
2538 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
2539 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2540};
2541
2542static u8 get_uuid_size(const u8 *uuid)
2543{
2544 u32 val;
2545
2546 if (memcmp(uuid, bluetooth_base_uuid, 12))
2547 return 128;
2548
2549 val = get_unaligned_le32(&uuid[12]);
2550 if (val > 0xffff)
2551 return 32;
2552
2553 return 16;
2554}
2555
Johan Hedberg92da6092013-03-15 17:06:55 -05002556static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status)
2557{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002558 struct mgmt_pending_cmd *cmd;
Johan Hedberg92da6092013-03-15 17:06:55 -05002559
2560 hci_dev_lock(hdev);
2561
Johan Hedberg333ae952015-03-17 13:48:47 +02002562 cmd = pending_find(mgmt_op, hdev);
Johan Hedberg92da6092013-03-15 17:06:55 -05002563 if (!cmd)
2564 goto unlock;
2565
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002566 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
2567 mgmt_status(status), hdev->dev_class, 3);
Johan Hedberg92da6092013-03-15 17:06:55 -05002568
2569 mgmt_pending_remove(cmd);
2570
2571unlock:
2572 hci_dev_unlock(hdev);
2573}
2574
Marcel Holtmann1904a852015-01-11 13:50:44 -08002575static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002576{
2577 BT_DBG("status 0x%02x", status);
2578
2579 mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status);
2580}
2581
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002582static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002583{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002584 struct mgmt_cp_add_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002585 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002586 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002587 struct bt_uuid *uuid;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002588 int err;
2589
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002590 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002591
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002592 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002593
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002594 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002595 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
2596 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002597 goto failed;
2598 }
2599
Andre Guedes92c4c202012-06-07 19:05:44 -03002600 uuid = kmalloc(sizeof(*uuid), GFP_KERNEL);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002601 if (!uuid) {
2602 err = -ENOMEM;
2603 goto failed;
2604 }
2605
2606 memcpy(uuid->uuid, cp->uuid, 16);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002607 uuid->svc_hint = cp->svc_hint;
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002608 uuid->size = get_uuid_size(cp->uuid);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002609
Johan Hedbergde66aa62013-01-27 00:31:27 +02002610 list_add_tail(&uuid->list, &hdev->uuids);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002611
Johan Hedberg890ea892013-03-15 17:06:52 -05002612 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002613
Johan Hedberg890ea892013-03-15 17:06:52 -05002614 update_class(&req);
2615 update_eir(&req);
2616
Johan Hedberg92da6092013-03-15 17:06:55 -05002617 err = hci_req_run(&req, add_uuid_complete);
2618 if (err < 0) {
2619 if (err != -ENODATA)
2620 goto failed;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002621
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002622 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
2623 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002624 goto failed;
2625 }
2626
2627 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002628 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002629 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002630 goto failed;
2631 }
2632
2633 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002634
2635failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002636 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002637 return err;
2638}
2639
Johan Hedberg24b78d02012-02-23 23:24:30 +02002640static bool enable_service_cache(struct hci_dev *hdev)
2641{
2642 if (!hdev_is_powered(hdev))
2643 return false;
2644
Marcel Holtmann238be782015-03-13 02:11:06 -07002645 if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg46818ed2013-01-14 22:33:52 +02002646 queue_delayed_work(hdev->workqueue, &hdev->service_cache,
2647 CACHE_TIMEOUT);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002648 return true;
2649 }
2650
2651 return false;
2652}
2653
Marcel Holtmann1904a852015-01-11 13:50:44 -08002654static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002655{
2656 BT_DBG("status 0x%02x", status);
2657
2658 mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status);
2659}
2660
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002661static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002662 u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002663{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002664 struct mgmt_cp_remove_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002665 struct mgmt_pending_cmd *cmd;
Johan Hedberg056341c2013-01-27 00:31:30 +02002666 struct bt_uuid *match, *tmp;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002667 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 -05002668 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002669 int err, found;
2670
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002671 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002672
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002673 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002674
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002675 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002676 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2677 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002678 goto unlock;
2679 }
2680
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002681 if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
Johan Hedberg35f74982014-02-18 17:14:32 +02002682 hci_uuids_clear(hdev);
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002683
Johan Hedberg24b78d02012-02-23 23:24:30 +02002684 if (enable_service_cache(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002685 err = mgmt_cmd_complete(sk, hdev->id,
2686 MGMT_OP_REMOVE_UUID,
2687 0, hdev->dev_class, 3);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002688 goto unlock;
2689 }
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002690
Johan Hedberg9246a862012-02-23 21:33:16 +02002691 goto update_class;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002692 }
2693
2694 found = 0;
2695
Johan Hedberg056341c2013-01-27 00:31:30 +02002696 list_for_each_entry_safe(match, tmp, &hdev->uuids, list) {
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002697 if (memcmp(match->uuid, cp->uuid, 16) != 0)
2698 continue;
2699
2700 list_del(&match->list);
Johan Hedberg482049f2012-11-08 10:25:26 +01002701 kfree(match);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002702 found++;
2703 }
2704
2705 if (found == 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002706 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2707 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002708 goto unlock;
2709 }
2710
Johan Hedberg9246a862012-02-23 21:33:16 +02002711update_class:
Johan Hedberg890ea892013-03-15 17:06:52 -05002712 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002713
Johan Hedberg890ea892013-03-15 17:06:52 -05002714 update_class(&req);
2715 update_eir(&req);
2716
Johan Hedberg92da6092013-03-15 17:06:55 -05002717 err = hci_req_run(&req, remove_uuid_complete);
2718 if (err < 0) {
2719 if (err != -ENODATA)
2720 goto unlock;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002721
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002722 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
2723 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002724 goto unlock;
2725 }
2726
2727 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002728 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002729 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002730 goto unlock;
2731 }
2732
2733 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002734
2735unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002736 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002737 return err;
2738}
2739
Marcel Holtmann1904a852015-01-11 13:50:44 -08002740static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002741{
2742 BT_DBG("status 0x%02x", status);
2743
2744 mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status);
2745}
2746
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002747static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002748 u16 len)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002749{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002750 struct mgmt_cp_set_dev_class *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002751 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002752 struct hci_request req;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002753 int err;
2754
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002755 BT_DBG("request for %s", hdev->name);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002756
Marcel Holtmann6203fc92013-10-02 23:37:29 -07002757 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002758 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2759 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002760
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002761 hci_dev_lock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002762
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002763 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002764 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2765 MGMT_STATUS_BUSY);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002766 goto unlock;
2767 }
2768
2769 if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002770 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2771 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002772 goto unlock;
2773 }
2774
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002775 hdev->major_class = cp->major;
2776 hdev->minor_class = cp->minor;
2777
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002778 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002779 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2780 hdev->dev_class, 3);
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002781 goto unlock;
2782 }
2783
Johan Hedberg890ea892013-03-15 17:06:52 -05002784 hci_req_init(&req, hdev);
2785
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002786 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg7d785252011-12-15 00:47:39 +02002787 hci_dev_unlock(hdev);
2788 cancel_delayed_work_sync(&hdev->service_cache);
2789 hci_dev_lock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -05002790 update_eir(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02002791 }
Johan Hedberg14c0b602011-12-15 00:47:37 +02002792
Johan Hedberg890ea892013-03-15 17:06:52 -05002793 update_class(&req);
2794
Johan Hedberg92da6092013-03-15 17:06:55 -05002795 err = hci_req_run(&req, set_class_complete);
2796 if (err < 0) {
2797 if (err != -ENODATA)
2798 goto unlock;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002799
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002800 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2801 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002802 goto unlock;
2803 }
2804
2805 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002806 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002807 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002808 goto unlock;
2809 }
2810
2811 err = 0;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002812
Johan Hedbergb5235a62012-02-21 14:32:24 +02002813unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002814 hci_dev_unlock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002815 return err;
2816}
2817
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002818static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002819 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002820{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002821 struct mgmt_cp_load_link_keys *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03002822 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
2823 sizeof(struct mgmt_link_key_info));
Szymon Janc4e51eae2011-02-25 19:05:48 +01002824 u16 key_count, expected_len;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002825 bool changed;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002826 int i;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002827
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002828 BT_DBG("request for %s", hdev->name);
2829
2830 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002831 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2832 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002833
Marcel Holtmann1f350c82012-03-12 20:31:08 -07002834 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002835 if (key_count > max_key_count) {
2836 BT_ERR("load_link_keys: too big key_count value %u",
2837 key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02002838 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2839 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002840 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002841
Johan Hedberg86742e12011-11-07 23:13:38 +02002842 expected_len = sizeof(*cp) + key_count *
2843 sizeof(struct mgmt_link_key_info);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002844 if (expected_len != len) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002845 BT_ERR("load_link_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02002846 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02002847 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2848 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002849 }
2850
Johan Hedberg4ae14302013-01-20 14:27:13 +02002851 if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002852 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2853 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ae14302013-01-20 14:27:13 +02002854
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002855 BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002856 key_count);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002857
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002858 for (i = 0; i < key_count; i++) {
2859 struct mgmt_link_key_info *key = &cp->keys[i];
2860
Marcel Holtmann8e991132014-01-10 02:07:25 -08002861 if (key->addr.type != BDADDR_BREDR || key->type > 0x08)
Johan Hedberga69e8372015-03-06 21:08:53 +02002862 return mgmt_cmd_status(sk, hdev->id,
2863 MGMT_OP_LOAD_LINK_KEYS,
2864 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002865 }
2866
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002867 hci_dev_lock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002868
2869 hci_link_keys_clear(hdev);
2870
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002871 if (cp->debug_keys)
Marcel Holtmann238be782015-03-13 02:11:06 -07002872 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002873 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002874 changed = hci_dev_test_and_clear_flag(hdev,
2875 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002876
2877 if (changed)
2878 new_settings(hdev, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002879
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002880 for (i = 0; i < key_count; i++) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002881 struct mgmt_link_key_info *key = &cp->keys[i];
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002882
Johan Hedberg58e92932014-06-24 14:00:26 +03002883 /* Always ignore debug keys and require a new pairing if
2884 * the user wants to use them.
2885 */
2886 if (key->type == HCI_LK_DEBUG_COMBINATION)
2887 continue;
2888
Johan Hedberg7652ff62014-06-24 13:15:49 +03002889 hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val,
2890 key->type, key->pin_len, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002891 }
2892
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002893 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
Johan Hedberg0e5f8752011-11-11 16:18:54 +02002894
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002895 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002896
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002897 return 0;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002898}
2899
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002900static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002901 u8 addr_type, struct sock *skip_sk)
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002902{
2903 struct mgmt_ev_device_unpaired ev;
2904
2905 bacpy(&ev.addr.bdaddr, bdaddr);
2906 ev.addr.type = addr_type;
2907
2908 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002909 skip_sk);
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002910}
2911
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002912static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002913 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002914{
Johan Hedberg124f6e32012-02-09 13:50:12 +02002915 struct mgmt_cp_unpair_device *cp = data;
2916 struct mgmt_rp_unpair_device rp;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002917 struct hci_cp_disconnect dc;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002918 struct mgmt_pending_cmd *cmd;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002919 struct hci_conn *conn;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002920 int err;
2921
Johan Hedberga8a1d192011-11-10 15:54:38 +02002922 memset(&rp, 0, sizeof(rp));
Johan Hedberg124f6e32012-02-09 13:50:12 +02002923 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2924 rp.addr.type = cp->addr.type;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002925
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002926 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002927 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2928 MGMT_STATUS_INVALID_PARAMS,
2929 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002930
Johan Hedberg118da702013-01-20 14:27:20 +02002931 if (cp->disconnect != 0x00 && cp->disconnect != 0x01)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002932 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2933 MGMT_STATUS_INVALID_PARAMS,
2934 &rp, sizeof(rp));
Johan Hedberg118da702013-01-20 14:27:20 +02002935
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002936 hci_dev_lock(hdev);
2937
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002938 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002939 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2940 MGMT_STATUS_NOT_POWERED, &rp,
2941 sizeof(rp));
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002942 goto unlock;
2943 }
2944
Johan Hedberge0b2b272014-02-18 17:14:31 +02002945 if (cp->addr.type == BDADDR_BREDR) {
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002946 /* If disconnection is requested, then look up the
2947 * connection. If the remote device is connected, it
2948 * will be later used to terminate the link.
2949 *
2950 * Setting it to NULL explicitly will cause no
2951 * termination of the link.
2952 */
2953 if (cp->disconnect)
2954 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2955 &cp->addr.bdaddr);
2956 else
2957 conn = NULL;
2958
Johan Hedberg124f6e32012-02-09 13:50:12 +02002959 err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
Johan Hedberge0b2b272014-02-18 17:14:31 +02002960 } else {
2961 u8 addr_type;
2962
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002963 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK,
2964 &cp->addr.bdaddr);
2965 if (conn) {
2966 /* Defer clearing up the connection parameters
2967 * until closing to give a chance of keeping
2968 * them if a repairing happens.
2969 */
2970 set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
2971
2972 /* If disconnection is not requested, then
2973 * clear the connection variable so that the
2974 * link is not terminated.
2975 */
2976 if (!cp->disconnect)
2977 conn = NULL;
2978 }
2979
Johan Hedberge0b2b272014-02-18 17:14:31 +02002980 if (cp->addr.type == BDADDR_LE_PUBLIC)
2981 addr_type = ADDR_LE_DEV_PUBLIC;
2982 else
2983 addr_type = ADDR_LE_DEV_RANDOM;
2984
Johan Hedberga7ec7332014-02-18 17:14:35 +02002985 hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type);
2986
Johan Hedberge0b2b272014-02-18 17:14:31 +02002987 err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type);
2988 }
Vinicius Costa Gomesb0dbfb42012-02-02 21:08:03 -03002989
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002990 if (err < 0) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002991 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2992 MGMT_STATUS_NOT_PAIRED, &rp,
2993 sizeof(rp));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002994 goto unlock;
2995 }
2996
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002997 /* If the connection variable is set, then termination of the
2998 * link is requested.
2999 */
Johan Hedberga8a1d192011-11-10 15:54:38 +02003000 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003001 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
3002 &rp, sizeof(rp));
Johan Hedbergb1078ad2012-02-09 17:21:16 +02003003 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
Johan Hedberga8a1d192011-11-10 15:54:38 +02003004 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003005 }
3006
Johan Hedberg124f6e32012-02-09 13:50:12 +02003007 cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003008 sizeof(*cp));
Johan Hedberga8a1d192011-11-10 15:54:38 +02003009 if (!cmd) {
3010 err = -ENOMEM;
3011 goto unlock;
3012 }
3013
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02003014 cmd->cmd_complete = addr_cmd_complete;
3015
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02003016 dc.handle = cpu_to_le16(conn->handle);
Johan Hedberga8a1d192011-11-10 15:54:38 +02003017 dc.reason = 0x13; /* Remote User Terminated Connection */
3018 err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc);
3019 if (err < 0)
3020 mgmt_pending_remove(cmd);
3021
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003022unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003023 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003024 return err;
3025}
3026
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003027static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003028 u16 len)
Johan Hedberg8962ee72011-01-20 12:40:27 +02003029{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003030 struct mgmt_cp_disconnect *cp = data;
Johan Hedberg06a63b12013-01-20 14:27:21 +02003031 struct mgmt_rp_disconnect rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003032 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02003033 struct hci_conn *conn;
Johan Hedberg8962ee72011-01-20 12:40:27 +02003034 int err;
3035
3036 BT_DBG("");
3037
Johan Hedberg06a63b12013-01-20 14:27:21 +02003038 memset(&rp, 0, sizeof(rp));
3039 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3040 rp.addr.type = cp->addr.type;
3041
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003042 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003043 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3044 MGMT_STATUS_INVALID_PARAMS,
3045 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003046
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003047 hci_dev_lock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003048
3049 if (!test_bit(HCI_UP, &hdev->flags)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003050 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3051 MGMT_STATUS_NOT_POWERED, &rp,
3052 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02003053 goto failed;
3054 }
3055
Johan Hedberg333ae952015-03-17 13:48:47 +02003056 if (pending_find(MGMT_OP_DISCONNECT, hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003057 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3058 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02003059 goto failed;
3060 }
3061
Andre Guedes591f47f2012-04-24 21:02:49 -03003062 if (cp->addr.type == BDADDR_BREDR)
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003063 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
3064 &cp->addr.bdaddr);
Johan Hedberg88c3df12012-02-09 14:27:38 +02003065 else
3066 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
Vinicius Costa Gomes365227e2011-05-06 18:41:44 -03003067
Vishal Agarwalf9607272012-06-13 05:32:43 +05303068 if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003069 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3070 MGMT_STATUS_NOT_CONNECTED, &rp,
3071 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02003072 goto failed;
3073 }
3074
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003075 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03003076 if (!cmd) {
3077 err = -ENOMEM;
Johan Hedberg8962ee72011-01-20 12:40:27 +02003078 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03003079 }
Johan Hedberg8962ee72011-01-20 12:40:27 +02003080
Johan Hedbergf5818c22014-12-05 13:36:02 +02003081 cmd->cmd_complete = generic_cmd_complete;
3082
Johan Hedberge3f2f922014-08-18 20:33:33 +03003083 err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003084 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03003085 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003086
3087failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003088 hci_dev_unlock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003089 return err;
3090}
3091
Andre Guedes57c14772012-04-24 21:02:50 -03003092static u8 link_to_bdaddr(u8 link_type, u8 addr_type)
Johan Hedberg4c659c32011-11-07 23:13:39 +02003093{
3094 switch (link_type) {
3095 case LE_LINK:
Johan Hedberg48264f02011-11-09 13:58:58 +02003096 switch (addr_type) {
3097 case ADDR_LE_DEV_PUBLIC:
Andre Guedes591f47f2012-04-24 21:02:49 -03003098 return BDADDR_LE_PUBLIC;
Andre Guedes0ed09142012-04-03 08:46:54 -03003099
Johan Hedberg48264f02011-11-09 13:58:58 +02003100 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03003101 /* Fallback to LE Random address type */
Andre Guedes591f47f2012-04-24 21:02:49 -03003102 return BDADDR_LE_RANDOM;
Johan Hedberg48264f02011-11-09 13:58:58 +02003103 }
Andre Guedes0ed09142012-04-03 08:46:54 -03003104
Johan Hedberg4c659c32011-11-07 23:13:39 +02003105 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03003106 /* Fallback to BR/EDR type */
Andre Guedes591f47f2012-04-24 21:02:49 -03003107 return BDADDR_BREDR;
Johan Hedberg4c659c32011-11-07 23:13:39 +02003108 }
3109}
3110
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003111static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
3112 u16 data_len)
Johan Hedberg2784eb42011-01-21 13:56:35 +02003113{
Johan Hedberg2784eb42011-01-21 13:56:35 +02003114 struct mgmt_rp_get_connections *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02003115 struct hci_conn *c;
Johan Hedberga38528f2011-01-22 06:46:43 +02003116 size_t rp_len;
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003117 int err;
3118 u16 i;
Johan Hedberg2784eb42011-01-21 13:56:35 +02003119
3120 BT_DBG("");
3121
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003122 hci_dev_lock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02003123
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003124 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003125 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
3126 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003127 goto unlock;
3128 }
3129
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003130 i = 0;
Johan Hedbergb644ba32012-01-17 21:48:47 +02003131 list_for_each_entry(c, &hdev->conn_hash.list, list) {
3132 if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003133 i++;
Johan Hedberg2784eb42011-01-21 13:56:35 +02003134 }
3135
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003136 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Andre Guedes92c4c202012-06-07 19:05:44 -03003137 rp = kmalloc(rp_len, GFP_KERNEL);
Johan Hedberga38528f2011-01-22 06:46:43 +02003138 if (!rp) {
Johan Hedberg2784eb42011-01-21 13:56:35 +02003139 err = -ENOMEM;
3140 goto unlock;
3141 }
3142
Johan Hedberg2784eb42011-01-21 13:56:35 +02003143 i = 0;
Johan Hedberg4c659c32011-11-07 23:13:39 +02003144 list_for_each_entry(c, &hdev->conn_hash.list, list) {
Johan Hedbergb644ba32012-01-17 21:48:47 +02003145 if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
3146 continue;
Johan Hedberg4c659c32011-11-07 23:13:39 +02003147 bacpy(&rp->addr[i].bdaddr, &c->dst);
Andre Guedes57c14772012-04-24 21:02:50 -03003148 rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type);
Andre Guedes0ed09142012-04-03 08:46:54 -03003149 if (c->type == SCO_LINK || c->type == ESCO_LINK)
Johan Hedberg4c659c32011-11-07 23:13:39 +02003150 continue;
3151 i++;
3152 }
3153
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02003154 rp->conn_count = cpu_to_le16(i);
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003155
Johan Hedberg4c659c32011-11-07 23:13:39 +02003156 /* Recalculate length in case of filtered SCO connections, etc */
3157 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Johan Hedberg2784eb42011-01-21 13:56:35 +02003158
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003159 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
3160 rp_len);
Johan Hedberg2784eb42011-01-21 13:56:35 +02003161
Johan Hedberga38528f2011-01-22 06:46:43 +02003162 kfree(rp);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003163
3164unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003165 hci_dev_unlock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02003166 return err;
3167}
3168
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003169static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003170 struct mgmt_cp_pin_code_neg_reply *cp)
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003171{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003172 struct mgmt_pending_cmd *cmd;
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003173 int err;
3174
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003175 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003176 sizeof(*cp));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003177 if (!cmd)
3178 return -ENOMEM;
3179
Johan Hedbergd8457692012-02-17 14:24:57 +02003180 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003181 sizeof(cp->addr.bdaddr), &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003182 if (err < 0)
3183 mgmt_pending_remove(cmd);
3184
3185 return err;
3186}
3187
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003188static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003189 u16 len)
Johan Hedberg980e1a52011-01-22 06:10:07 +02003190{
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003191 struct hci_conn *conn;
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003192 struct mgmt_cp_pin_code_reply *cp = data;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003193 struct hci_cp_pin_code_reply reply;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003194 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003195 int err;
3196
3197 BT_DBG("");
3198
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003199 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003200
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003201 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003202 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3203 MGMT_STATUS_NOT_POWERED);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003204 goto failed;
3205 }
3206
Johan Hedbergd8457692012-02-17 14:24:57 +02003207 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003208 if (!conn) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003209 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3210 MGMT_STATUS_NOT_CONNECTED);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003211 goto failed;
3212 }
3213
3214 if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) {
Johan Hedbergd8457692012-02-17 14:24:57 +02003215 struct mgmt_cp_pin_code_neg_reply ncp;
3216
3217 memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003218
3219 BT_ERR("PIN code is not 16 bytes long");
3220
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003221 err = send_pin_code_neg_reply(sk, hdev, &ncp);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003222 if (err >= 0)
Johan Hedberga69e8372015-03-06 21:08:53 +02003223 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3224 MGMT_STATUS_INVALID_PARAMS);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003225
3226 goto failed;
3227 }
3228
Gustavo F. Padovan00abfe42012-03-01 00:37:10 -03003229 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03003230 if (!cmd) {
3231 err = -ENOMEM;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003232 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03003233 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02003234
Johan Hedberg7776d1d2014-12-05 13:36:03 +02003235 cmd->cmd_complete = addr_cmd_complete;
3236
Johan Hedbergd8457692012-02-17 14:24:57 +02003237 bacpy(&reply.bdaddr, &cp->addr.bdaddr);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003238 reply.pin_len = cp->pin_len;
Waldemar Rymarkiewicz24718ca2011-06-01 17:28:47 +02003239 memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code));
Johan Hedberg980e1a52011-01-22 06:10:07 +02003240
3241 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
3242 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03003243 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003244
3245failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003246 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003247 return err;
3248}
3249
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003250static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
3251 u16 len)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003252{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003253 struct mgmt_cp_set_io_capability *cp = data;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003254
3255 BT_DBG("");
3256
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003257 if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003258 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY,
3259 MGMT_STATUS_INVALID_PARAMS, NULL, 0);
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003260
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003261 hci_dev_lock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003262
3263 hdev->io_capability = cp->io_capability;
3264
3265 BT_DBG("%s IO capability set to 0x%02x", hdev->name,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003266 hdev->io_capability);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003267
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003268 hci_dev_unlock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003269
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003270 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0,
3271 NULL, 0);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003272}
3273
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003274static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003275{
3276 struct hci_dev *hdev = conn->hdev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003277 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003278
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003279 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
Johan Hedberge9a416b2011-02-19 12:05:56 -03003280 if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
3281 continue;
3282
Johan Hedberge9a416b2011-02-19 12:05:56 -03003283 if (cmd->user_data != conn)
3284 continue;
3285
3286 return cmd;
3287 }
3288
3289 return NULL;
3290}
3291
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003292static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003293{
3294 struct mgmt_rp_pair_device rp;
3295 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9df74652014-12-19 22:26:03 +02003296 int err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003297
Johan Hedberg61b1a7f2014-03-20 12:54:16 +02003298 bacpy(&rp.addr.bdaddr, &conn->dst);
3299 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003300
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003301 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE,
3302 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003303
3304 /* So we don't get further callbacks for this connection */
3305 conn->connect_cfm_cb = NULL;
3306 conn->security_cfm_cb = NULL;
3307 conn->disconn_cfm_cb = NULL;
3308
David Herrmann76a68ba2013-04-06 20:28:37 +02003309 hci_conn_drop(conn);
Alfonso Acosta89cbb062014-10-11 21:44:47 +00003310
3311 /* The device is paired so there is no need to remove
3312 * its connection parameters anymore.
3313 */
3314 clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
Johan Hedberg15013ae2014-12-11 21:45:44 +02003315
3316 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02003317
3318 return err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003319}
3320
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003321void mgmt_smp_complete(struct hci_conn *conn, bool complete)
3322{
3323 u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003324 struct mgmt_pending_cmd *cmd;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003325
3326 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003327 if (cmd) {
Johan Hedberg04ab2742014-12-05 13:36:04 +02003328 cmd->cmd_complete(cmd, status);
Johan Hedberga511b352014-12-11 21:45:45 +02003329 mgmt_pending_remove(cmd);
3330 }
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003331}
3332
Johan Hedberge9a416b2011-02-19 12:05:56 -03003333static void pairing_complete_cb(struct hci_conn *conn, u8 status)
3334{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003335 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003336
3337 BT_DBG("status %u", status);
3338
Johan Hedberg56e5cb82011-11-08 20:40:16 +02003339 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003340 if (!cmd) {
Johan Hedberg56e5cb82011-11-08 20:40:16 +02003341 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02003342 return;
3343 }
3344
3345 cmd->cmd_complete(cmd, mgmt_status(status));
3346 mgmt_pending_remove(cmd);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003347}
3348
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003349static void le_pairing_complete_cb(struct hci_conn *conn, u8 status)
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303350{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003351 struct mgmt_pending_cmd *cmd;
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303352
3353 BT_DBG("status %u", status);
3354
3355 if (!status)
3356 return;
3357
3358 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003359 if (!cmd) {
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303360 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02003361 return;
3362 }
3363
3364 cmd->cmd_complete(cmd, mgmt_status(status));
3365 mgmt_pending_remove(cmd);
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303366}
3367
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003368static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003369 u16 len)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003370{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003371 struct mgmt_cp_pair_device *cp = data;
Johan Hedberg1425acb2011-11-11 00:07:35 +02003372 struct mgmt_rp_pair_device rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003373 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003374 u8 sec_level, auth_type;
3375 struct hci_conn *conn;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003376 int err;
3377
3378 BT_DBG("");
3379
Szymon Jancf950a30e2013-01-18 12:48:07 +01003380 memset(&rp, 0, sizeof(rp));
3381 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3382 rp.addr.type = cp->addr.type;
3383
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003384 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003385 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3386 MGMT_STATUS_INVALID_PARAMS,
3387 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003388
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003389 if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003390 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3391 MGMT_STATUS_INVALID_PARAMS,
3392 &rp, sizeof(rp));
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003393
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003394 hci_dev_lock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003395
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003396 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003397 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3398 MGMT_STATUS_NOT_POWERED, &rp,
3399 sizeof(rp));
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003400 goto unlock;
3401 }
3402
Johan Hedberg55e76b32015-03-10 22:34:40 +02003403 if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) {
3404 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3405 MGMT_STATUS_ALREADY_PAIRED, &rp,
3406 sizeof(rp));
3407 goto unlock;
3408 }
3409
Vinicius Costa Gomesc908df32011-09-02 14:51:22 -03003410 sec_level = BT_SECURITY_MEDIUM;
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003411 auth_type = HCI_AT_DEDICATED_BONDING;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003412
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003413 if (cp->addr.type == BDADDR_BREDR) {
Andre Guedes04a6c582014-02-26 20:21:44 -03003414 conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
3415 auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003416 } else {
3417 u8 addr_type;
3418
3419 /* Convert from L2CAP channel address type to HCI address type
3420 */
3421 if (cp->addr.type == BDADDR_LE_PUBLIC)
3422 addr_type = ADDR_LE_DEV_PUBLIC;
3423 else
3424 addr_type = ADDR_LE_DEV_RANDOM;
3425
Marcel Holtmann7c264b12014-06-30 12:34:40 +02003426 /* When pairing a new device, it is expected to remember
3427 * this device for future connections. Adding the connection
3428 * parameter information ahead of time allows tracking
3429 * of the slave preferred values and will speed up any
3430 * further connection establishment.
3431 *
3432 * If connection parameters already exist, then they
3433 * will be kept and this function does nothing.
3434 */
3435 hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type);
3436
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003437 conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type,
Johan Hedberge804d252014-07-16 11:42:28 +03003438 sec_level, HCI_LE_CONN_TIMEOUT,
3439 HCI_ROLE_MASTER);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003440 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003441
Ville Tervo30e76272011-02-22 16:10:53 -03003442 if (IS_ERR(conn)) {
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02003443 int status;
3444
3445 if (PTR_ERR(conn) == -EBUSY)
3446 status = MGMT_STATUS_BUSY;
Lukasz Rymanowskifaa81032015-02-11 12:31:42 +01003447 else if (PTR_ERR(conn) == -EOPNOTSUPP)
3448 status = MGMT_STATUS_NOT_SUPPORTED;
3449 else if (PTR_ERR(conn) == -ECONNREFUSED)
3450 status = MGMT_STATUS_REJECTED;
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02003451 else
3452 status = MGMT_STATUS_CONNECT_FAILED;
3453
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003454 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3455 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003456 goto unlock;
3457 }
3458
3459 if (conn->connect_cfm_cb) {
David Herrmann76a68ba2013-04-06 20:28:37 +02003460 hci_conn_drop(conn);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003461 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3462 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003463 goto unlock;
3464 }
3465
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003466 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003467 if (!cmd) {
3468 err = -ENOMEM;
David Herrmann76a68ba2013-04-06 20:28:37 +02003469 hci_conn_drop(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003470 goto unlock;
3471 }
3472
Johan Hedberg04ab2742014-12-05 13:36:04 +02003473 cmd->cmd_complete = pairing_complete;
3474
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003475 /* For LE, just connecting isn't a proof that the pairing finished */
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003476 if (cp->addr.type == BDADDR_BREDR) {
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003477 conn->connect_cfm_cb = pairing_complete_cb;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003478 conn->security_cfm_cb = pairing_complete_cb;
3479 conn->disconn_cfm_cb = pairing_complete_cb;
3480 } else {
3481 conn->connect_cfm_cb = le_pairing_complete_cb;
3482 conn->security_cfm_cb = le_pairing_complete_cb;
3483 conn->disconn_cfm_cb = le_pairing_complete_cb;
3484 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003485
Johan Hedberge9a416b2011-02-19 12:05:56 -03003486 conn->io_capability = cp->io_cap;
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03003487 cmd->user_data = hci_conn_get(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003488
Johan Hedberg6f78fd42014-07-30 08:35:48 +03003489 if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) &&
Johan Hedberga511b352014-12-11 21:45:45 +02003490 hci_conn_security(conn, sec_level, auth_type, true)) {
3491 cmd->cmd_complete(cmd, 0);
3492 mgmt_pending_remove(cmd);
3493 }
Johan Hedberge9a416b2011-02-19 12:05:56 -03003494
3495 err = 0;
3496
3497unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003498 hci_dev_unlock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003499 return err;
3500}
3501
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003502static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3503 u16 len)
Johan Hedberg28424702012-02-02 04:02:29 +02003504{
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003505 struct mgmt_addr_info *addr = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003506 struct mgmt_pending_cmd *cmd;
Johan Hedberg28424702012-02-02 04:02:29 +02003507 struct hci_conn *conn;
3508 int err;
3509
3510 BT_DBG("");
3511
Johan Hedberg28424702012-02-02 04:02:29 +02003512 hci_dev_lock(hdev);
3513
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003514 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003515 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3516 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003517 goto unlock;
3518 }
3519
Johan Hedberg333ae952015-03-17 13:48:47 +02003520 cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02003521 if (!cmd) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003522 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3523 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003524 goto unlock;
3525 }
3526
3527 conn = cmd->user_data;
3528
3529 if (bacmp(&addr->bdaddr, &conn->dst) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003530 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3531 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003532 goto unlock;
3533 }
3534
Johan Hedberga511b352014-12-11 21:45:45 +02003535 cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED);
3536 mgmt_pending_remove(cmd);
Johan Hedberg28424702012-02-02 04:02:29 +02003537
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003538 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
3539 addr, sizeof(*addr));
Johan Hedberg28424702012-02-02 04:02:29 +02003540unlock:
3541 hci_dev_unlock(hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02003542 return err;
3543}
3544
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003545static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
Johan Hedberg1707c602013-03-15 17:07:15 -05003546 struct mgmt_addr_info *addr, u16 mgmt_op,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003547 u16 hci_op, __le32 passkey)
Johan Hedberga5c29682011-02-19 12:05:57 -03003548{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003549 struct mgmt_pending_cmd *cmd;
Brian Gix0df4c182011-11-16 13:53:13 -08003550 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03003551 int err;
3552
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003553 hci_dev_lock(hdev);
Johan Hedberg08ba5382011-03-16 14:29:34 +02003554
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003555 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003556 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3557 MGMT_STATUS_NOT_POWERED, addr,
3558 sizeof(*addr));
Brian Gix0df4c182011-11-16 13:53:13 -08003559 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003560 }
3561
Johan Hedberg1707c602013-03-15 17:07:15 -05003562 if (addr->type == BDADDR_BREDR)
3563 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr);
Johan Hedberg272d90d2012-02-09 15:26:12 +02003564 else
Johan Hedberg1707c602013-03-15 17:07:15 -05003565 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr);
Brian Gix47c15e22011-11-16 13:53:14 -08003566
Johan Hedberg272d90d2012-02-09 15:26:12 +02003567 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003568 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3569 MGMT_STATUS_NOT_CONNECTED, addr,
3570 sizeof(*addr));
Johan Hedberg272d90d2012-02-09 15:26:12 +02003571 goto done;
3572 }
3573
Johan Hedberg1707c602013-03-15 17:07:15 -05003574 if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
Brian Gix5fe57d92011-12-21 16:12:13 -08003575 err = smp_user_confirm_reply(conn, mgmt_op, passkey);
Brian Gix5fe57d92011-12-21 16:12:13 -08003576 if (!err)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003577 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3578 MGMT_STATUS_SUCCESS, addr,
3579 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003580 else
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003581 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3582 MGMT_STATUS_FAILED, addr,
3583 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003584
Brian Gix47c15e22011-11-16 13:53:14 -08003585 goto done;
3586 }
3587
Johan Hedberg1707c602013-03-15 17:07:15 -05003588 cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr));
Johan Hedberga5c29682011-02-19 12:05:57 -03003589 if (!cmd) {
3590 err = -ENOMEM;
Brian Gix0df4c182011-11-16 13:53:13 -08003591 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003592 }
3593
Johan Hedberg7776d1d2014-12-05 13:36:03 +02003594 cmd->cmd_complete = addr_cmd_complete;
3595
Brian Gix0df4c182011-11-16 13:53:13 -08003596 /* Continue with pairing via HCI */
Brian Gix604086b2011-11-23 08:28:33 -08003597 if (hci_op == HCI_OP_USER_PASSKEY_REPLY) {
3598 struct hci_cp_user_passkey_reply cp;
3599
Johan Hedberg1707c602013-03-15 17:07:15 -05003600 bacpy(&cp.bdaddr, &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003601 cp.passkey = passkey;
3602 err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
3603 } else
Johan Hedberg1707c602013-03-15 17:07:15 -05003604 err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr),
3605 &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003606
Johan Hedberga664b5b2011-02-19 12:06:02 -03003607 if (err < 0)
3608 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03003609
Brian Gix0df4c182011-11-16 13:53:13 -08003610done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003611 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03003612 return err;
3613}
3614
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303615static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
3616 void *data, u16 len)
3617{
3618 struct mgmt_cp_pin_code_neg_reply *cp = data;
3619
3620 BT_DBG("");
3621
Johan Hedberg1707c602013-03-15 17:07:15 -05003622 return user_pairing_resp(sk, hdev, &cp->addr,
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303623 MGMT_OP_PIN_CODE_NEG_REPLY,
3624 HCI_OP_PIN_CODE_NEG_REPLY, 0);
3625}
3626
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003627static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3628 u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003629{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003630 struct mgmt_cp_user_confirm_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003631
3632 BT_DBG("");
3633
3634 if (len != sizeof(*cp))
Johan Hedberga69e8372015-03-06 21:08:53 +02003635 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
3636 MGMT_STATUS_INVALID_PARAMS);
Brian Gix0df4c182011-11-16 13:53:13 -08003637
Johan Hedberg1707c602013-03-15 17:07:15 -05003638 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003639 MGMT_OP_USER_CONFIRM_REPLY,
3640 HCI_OP_USER_CONFIRM_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003641}
3642
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003643static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003644 void *data, u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003645{
Johan Hedbergc9c26592011-12-15 00:47:41 +02003646 struct mgmt_cp_user_confirm_neg_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003647
3648 BT_DBG("");
3649
Johan Hedberg1707c602013-03-15 17:07:15 -05003650 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003651 MGMT_OP_USER_CONFIRM_NEG_REPLY,
3652 HCI_OP_USER_CONFIRM_NEG_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003653}
3654
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003655static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3656 u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003657{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003658 struct mgmt_cp_user_passkey_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003659
3660 BT_DBG("");
3661
Johan Hedberg1707c602013-03-15 17:07:15 -05003662 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003663 MGMT_OP_USER_PASSKEY_REPLY,
3664 HCI_OP_USER_PASSKEY_REPLY, cp->passkey);
Brian Gix604086b2011-11-23 08:28:33 -08003665}
3666
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003667static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003668 void *data, u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003669{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003670 struct mgmt_cp_user_passkey_neg_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003671
3672 BT_DBG("");
3673
Johan Hedberg1707c602013-03-15 17:07:15 -05003674 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003675 MGMT_OP_USER_PASSKEY_NEG_REPLY,
3676 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
Brian Gix604086b2011-11-23 08:28:33 -08003677}
3678
Johan Hedberg13928972013-03-15 17:07:00 -05003679static void update_name(struct hci_request *req)
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003680{
Johan Hedberg13928972013-03-15 17:07:00 -05003681 struct hci_dev *hdev = req->hdev;
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003682 struct hci_cp_write_local_name cp;
3683
Johan Hedberg13928972013-03-15 17:07:00 -05003684 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003685
Johan Hedberg890ea892013-03-15 17:06:52 -05003686 hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp);
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003687}
3688
Marcel Holtmann1904a852015-01-11 13:50:44 -08003689static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg13928972013-03-15 17:07:00 -05003690{
3691 struct mgmt_cp_set_local_name *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003692 struct mgmt_pending_cmd *cmd;
Johan Hedberg13928972013-03-15 17:07:00 -05003693
3694 BT_DBG("status 0x%02x", status);
3695
3696 hci_dev_lock(hdev);
3697
Johan Hedberg333ae952015-03-17 13:48:47 +02003698 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05003699 if (!cmd)
3700 goto unlock;
3701
3702 cp = cmd->param;
3703
3704 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02003705 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
3706 mgmt_status(status));
Johan Hedberg13928972013-03-15 17:07:00 -05003707 else
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003708 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3709 cp, sizeof(*cp));
Johan Hedberg13928972013-03-15 17:07:00 -05003710
3711 mgmt_pending_remove(cmd);
3712
3713unlock:
3714 hci_dev_unlock(hdev);
3715}
3716
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003717static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003718 u16 len)
Johan Hedbergb312b1612011-03-16 14:29:37 +02003719{
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003720 struct mgmt_cp_set_local_name *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003721 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05003722 struct hci_request req;
Johan Hedbergb312b1612011-03-16 14:29:37 +02003723 int err;
3724
3725 BT_DBG("");
3726
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003727 hci_dev_lock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003728
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003729 /* If the old values are the same as the new ones just return a
3730 * direct command complete event.
3731 */
3732 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
3733 !memcmp(hdev->short_name, cp->short_name,
3734 sizeof(hdev->short_name))) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003735 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3736 data, len);
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003737 goto failed;
3738 }
3739
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003740 memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003741
Johan Hedbergb5235a62012-02-21 14:32:24 +02003742 if (!hdev_is_powered(hdev)) {
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003743 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003744
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003745 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3746 data, len);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003747 if (err < 0)
3748 goto failed;
3749
Marcel Holtmannf6b77122015-03-14 19:28:05 -07003750 err = mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev,
3751 data, len, sk);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003752
Johan Hedbergb5235a62012-02-21 14:32:24 +02003753 goto failed;
3754 }
3755
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003756 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003757 if (!cmd) {
3758 err = -ENOMEM;
3759 goto failed;
3760 }
3761
Johan Hedberg13928972013-03-15 17:07:00 -05003762 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3763
Johan Hedberg890ea892013-03-15 17:06:52 -05003764 hci_req_init(&req, hdev);
Johan Hedberg3f985052013-03-15 17:07:02 -05003765
3766 if (lmp_bredr_capable(hdev)) {
3767 update_name(&req);
3768 update_eir(&req);
3769 }
3770
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003771 /* The name is stored in the scan response data and so
3772 * no need to udpate the advertising data here.
3773 */
Johan Hedberg3f985052013-03-15 17:07:02 -05003774 if (lmp_le_capable(hdev))
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003775 update_scan_rsp_data(&req);
Johan Hedberg3f985052013-03-15 17:07:02 -05003776
Johan Hedberg13928972013-03-15 17:07:00 -05003777 err = hci_req_run(&req, set_name_complete);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003778 if (err < 0)
3779 mgmt_pending_remove(cmd);
3780
3781failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003782 hci_dev_unlock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003783 return err;
3784}
3785
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003786static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003787 void *data, u16 data_len)
Szymon Jancc35938b2011-03-22 13:12:21 +01003788{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003789 struct mgmt_pending_cmd *cmd;
Szymon Jancc35938b2011-03-22 13:12:21 +01003790 int err;
3791
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003792 BT_DBG("%s", hdev->name);
Szymon Jancc35938b2011-03-22 13:12:21 +01003793
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003794 hci_dev_lock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003795
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003796 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003797 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3798 MGMT_STATUS_NOT_POWERED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003799 goto unlock;
3800 }
3801
Andre Guedes9a1a1992012-07-24 15:03:48 -03003802 if (!lmp_ssp_capable(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003803 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3804 MGMT_STATUS_NOT_SUPPORTED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003805 goto unlock;
3806 }
3807
Johan Hedberg333ae952015-03-17 13:48:47 +02003808 if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003809 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3810 MGMT_STATUS_BUSY);
Szymon Jancc35938b2011-03-22 13:12:21 +01003811 goto unlock;
3812 }
3813
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003814 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
Szymon Jancc35938b2011-03-22 13:12:21 +01003815 if (!cmd) {
3816 err = -ENOMEM;
3817 goto unlock;
3818 }
3819
Johan Hedberg710f11c2014-05-26 11:21:22 +03003820 if (bredr_sc_enabled(hdev))
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08003821 err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA,
3822 0, NULL);
3823 else
3824 err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
3825
Szymon Jancc35938b2011-03-22 13:12:21 +01003826 if (err < 0)
3827 mgmt_pending_remove(cmd);
3828
3829unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003830 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003831 return err;
3832}
3833
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003834static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003835 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003836{
Johan Hedberg5d57e792015-01-23 10:10:38 +02003837 struct mgmt_addr_info *addr = data;
Szymon Janc2763eda2011-03-22 13:12:22 +01003838 int err;
3839
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003840 BT_DBG("%s ", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003841
Johan Hedberg5d57e792015-01-23 10:10:38 +02003842 if (!bdaddr_type_is_valid(addr->type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003843 return mgmt_cmd_complete(sk, hdev->id,
3844 MGMT_OP_ADD_REMOTE_OOB_DATA,
3845 MGMT_STATUS_INVALID_PARAMS,
3846 addr, sizeof(*addr));
Johan Hedberg5d57e792015-01-23 10:10:38 +02003847
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003848 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003849
Marcel Holtmannec109112014-01-10 02:07:30 -08003850 if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) {
3851 struct mgmt_cp_add_remote_oob_data *cp = data;
3852 u8 status;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003853
Johan Hedbergc19a4952014-11-17 20:52:19 +02003854 if (cp->addr.type != BDADDR_BREDR) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003855 err = mgmt_cmd_complete(sk, hdev->id,
3856 MGMT_OP_ADD_REMOTE_OOB_DATA,
3857 MGMT_STATUS_INVALID_PARAMS,
3858 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02003859 goto unlock;
3860 }
3861
Marcel Holtmannec109112014-01-10 02:07:30 -08003862 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg6928a922014-10-26 20:46:09 +01003863 cp->addr.type, cp->hash,
3864 cp->rand, NULL, NULL);
Marcel Holtmannec109112014-01-10 02:07:30 -08003865 if (err < 0)
3866 status = MGMT_STATUS_FAILED;
3867 else
3868 status = MGMT_STATUS_SUCCESS;
3869
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003870 err = mgmt_cmd_complete(sk, hdev->id,
3871 MGMT_OP_ADD_REMOTE_OOB_DATA, status,
3872 &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08003873 } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) {
3874 struct mgmt_cp_add_remote_oob_ext_data *cp = data;
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003875 u8 *rand192, *hash192, *rand256, *hash256;
Marcel Holtmannec109112014-01-10 02:07:30 -08003876 u8 status;
3877
Johan Hedberg86df9202014-10-26 20:52:27 +01003878 if (bdaddr_type_is_le(cp->addr.type)) {
Johan Hedbergd25b78e2015-01-27 12:55:52 +02003879 /* Enforce zero-valued 192-bit parameters as
3880 * long as legacy SMP OOB isn't implemented.
3881 */
3882 if (memcmp(cp->rand192, ZERO_KEY, 16) ||
3883 memcmp(cp->hash192, ZERO_KEY, 16)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003884 err = mgmt_cmd_complete(sk, hdev->id,
3885 MGMT_OP_ADD_REMOTE_OOB_DATA,
3886 MGMT_STATUS_INVALID_PARAMS,
3887 addr, sizeof(*addr));
Johan Hedbergd25b78e2015-01-27 12:55:52 +02003888 goto unlock;
3889 }
3890
Johan Hedberg86df9202014-10-26 20:52:27 +01003891 rand192 = NULL;
3892 hash192 = NULL;
3893 } else {
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003894 /* In case one of the P-192 values is set to zero,
3895 * then just disable OOB data for P-192.
3896 */
3897 if (!memcmp(cp->rand192, ZERO_KEY, 16) ||
3898 !memcmp(cp->hash192, ZERO_KEY, 16)) {
3899 rand192 = NULL;
3900 hash192 = NULL;
3901 } else {
3902 rand192 = cp->rand192;
3903 hash192 = cp->hash192;
3904 }
3905 }
3906
3907 /* In case one of the P-256 values is set to zero, then just
3908 * disable OOB data for P-256.
3909 */
3910 if (!memcmp(cp->rand256, ZERO_KEY, 16) ||
3911 !memcmp(cp->hash256, ZERO_KEY, 16)) {
3912 rand256 = NULL;
3913 hash256 = NULL;
3914 } else {
3915 rand256 = cp->rand256;
3916 hash256 = cp->hash256;
Johan Hedberg86df9202014-10-26 20:52:27 +01003917 }
3918
Johan Hedberg81328d52014-10-26 20:33:47 +01003919 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg86df9202014-10-26 20:52:27 +01003920 cp->addr.type, hash192, rand192,
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003921 hash256, rand256);
Marcel Holtmannec109112014-01-10 02:07:30 -08003922 if (err < 0)
3923 status = MGMT_STATUS_FAILED;
3924 else
3925 status = MGMT_STATUS_SUCCESS;
3926
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003927 err = mgmt_cmd_complete(sk, hdev->id,
3928 MGMT_OP_ADD_REMOTE_OOB_DATA,
3929 status, &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08003930 } else {
3931 BT_ERR("add_remote_oob_data: invalid length of %u bytes", len);
Johan Hedberga69e8372015-03-06 21:08:53 +02003932 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3933 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannec109112014-01-10 02:07:30 -08003934 }
Szymon Janc2763eda2011-03-22 13:12:22 +01003935
Johan Hedbergc19a4952014-11-17 20:52:19 +02003936unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003937 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003938 return err;
3939}
3940
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003941static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003942 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003943{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003944 struct mgmt_cp_remove_remote_oob_data *cp = data;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003945 u8 status;
Szymon Janc2763eda2011-03-22 13:12:22 +01003946 int err;
3947
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003948 BT_DBG("%s", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003949
Johan Hedbergc19a4952014-11-17 20:52:19 +02003950 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003951 return mgmt_cmd_complete(sk, hdev->id,
3952 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
3953 MGMT_STATUS_INVALID_PARAMS,
3954 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02003955
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003956 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003957
Johan Hedbergeedbd582014-11-15 09:34:23 +02003958 if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
3959 hci_remote_oob_data_clear(hdev);
3960 status = MGMT_STATUS_SUCCESS;
3961 goto done;
3962 }
3963
Johan Hedberg6928a922014-10-26 20:46:09 +01003964 err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type);
Szymon Janc2763eda2011-03-22 13:12:22 +01003965 if (err < 0)
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003966 status = MGMT_STATUS_INVALID_PARAMS;
Szymon Janc2763eda2011-03-22 13:12:22 +01003967 else
Szymon Janca6785be2012-12-13 15:11:21 +01003968 status = MGMT_STATUS_SUCCESS;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003969
Johan Hedbergeedbd582014-11-15 09:34:23 +02003970done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003971 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
3972 status, &cp->addr, sizeof(cp->addr));
Szymon Janc2763eda2011-03-22 13:12:22 +01003973
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003974 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003975 return err;
3976}
3977
Jakub Pawlowski812abb12015-03-17 09:04:13 -07003978static bool trigger_bredr_inquiry(struct hci_request *req, u8 *status)
3979{
3980 struct hci_dev *hdev = req->hdev;
3981 struct hci_cp_inquiry cp;
3982 /* General inquiry access code (GIAC) */
3983 u8 lap[3] = { 0x33, 0x8b, 0x9e };
3984
3985 *status = mgmt_bredr_support(hdev);
3986 if (*status)
3987 return false;
3988
3989 if (hci_dev_test_flag(hdev, HCI_INQUIRY)) {
3990 *status = MGMT_STATUS_BUSY;
3991 return false;
3992 }
3993
3994 hci_inquiry_cache_flush(hdev);
3995
3996 memset(&cp, 0, sizeof(cp));
3997 memcpy(&cp.lap, lap, sizeof(cp.lap));
3998 cp.length = DISCOV_BREDR_INQUIRY_LEN;
3999
4000 hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp);
4001
4002 return true;
4003}
4004
4005static bool trigger_le_scan(struct hci_request *req, u16 interval, u8 *status)
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004006{
Marcel Holtmann80190442014-12-04 11:36:36 +01004007 struct hci_dev *hdev = req->hdev;
4008 struct hci_cp_le_set_scan_param param_cp;
4009 struct hci_cp_le_set_scan_enable enable_cp;
Marcel Holtmann80190442014-12-04 11:36:36 +01004010 u8 own_addr_type;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004011 int err;
4012
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004013 *status = mgmt_le_support(hdev);
4014 if (*status)
4015 return false;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004016
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004017 if (hci_dev_test_flag(hdev, HCI_LE_ADV)) {
4018 /* Don't let discovery abort an outgoing connection attempt
4019 * that's using directed advertising.
4020 */
4021 if (hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT)) {
4022 *status = MGMT_STATUS_REJECTED;
Marcel Holtmann80190442014-12-04 11:36:36 +01004023 return false;
4024 }
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004025
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004026 disable_advertising(req);
4027 }
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004028
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004029 /* If controller is scanning, it means the background scanning is
4030 * running. Thus, we should temporarily stop it in order to set the
4031 * discovery scanning parameters.
4032 */
4033 if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
4034 hci_req_add_le_scan_disable(req);
4035
4036 /* All active scans will be done with either a resolvable private
4037 * address (when privacy feature has been enabled) or non-resolvable
4038 * private address.
4039 */
4040 err = hci_update_random_address(req, true, &own_addr_type);
4041 if (err < 0) {
4042 *status = MGMT_STATUS_FAILED;
4043 return false;
4044 }
4045
4046 memset(&param_cp, 0, sizeof(param_cp));
4047 param_cp.type = LE_SCAN_ACTIVE;
4048 param_cp.interval = cpu_to_le16(interval);
4049 param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN);
4050 param_cp.own_address_type = own_addr_type;
4051
4052 hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
4053 &param_cp);
4054
4055 memset(&enable_cp, 0, sizeof(enable_cp));
4056 enable_cp.enable = LE_SCAN_ENABLE;
4057 enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
4058
4059 hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
4060 &enable_cp);
4061
4062 return true;
4063}
4064
4065static bool trigger_discovery(struct hci_request *req, u8 *status)
4066{
4067 struct hci_dev *hdev = req->hdev;
4068
4069 switch (hdev->discovery.type) {
4070 case DISCOV_TYPE_BREDR:
4071 if (!trigger_bredr_inquiry(req, status))
4072 return false;
Marcel Holtmann80190442014-12-04 11:36:36 +01004073 break;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004074
Marcel Holtmann80190442014-12-04 11:36:36 +01004075 case DISCOV_TYPE_INTERLEAVED:
Jakub Pawlowski07d23342015-03-17 09:04:14 -07004076 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
4077 &hdev->quirks)) {
4078 /* During simultaneous discovery, we double LE scan
4079 * interval. We must leave some time for the controller
4080 * to do BR/EDR inquiry.
4081 */
4082 if (!trigger_le_scan(req, DISCOV_LE_SCAN_INT * 2,
4083 status))
4084 return false;
4085
4086 if (!trigger_bredr_inquiry(req, status))
4087 return false;
4088
4089 return true;
4090 }
4091
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004092 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Marcel Holtmann80190442014-12-04 11:36:36 +01004093 *status = MGMT_STATUS_NOT_SUPPORTED;
4094 return false;
4095 }
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004096 /* fall through */
Marcel Holtmann80190442014-12-04 11:36:36 +01004097
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004098 case DISCOV_TYPE_LE:
4099 if (!trigger_le_scan(req, DISCOV_LE_SCAN_INT, status))
Marcel Holtmann80190442014-12-04 11:36:36 +01004100 return false;
Marcel Holtmann80190442014-12-04 11:36:36 +01004101 break;
4102
4103 default:
4104 *status = MGMT_STATUS_INVALID_PARAMS;
4105 return false;
4106 }
4107
4108 return true;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004109}
4110
Marcel Holtmann1904a852015-01-11 13:50:44 -08004111static void start_discovery_complete(struct hci_dev *hdev, u8 status,
4112 u16 opcode)
Andre Guedes7c307722013-04-30 15:29:28 -03004113{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004114 struct mgmt_pending_cmd *cmd;
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004115 unsigned long timeout;
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01004116
Andre Guedes7c307722013-04-30 15:29:28 -03004117 BT_DBG("status %d", status);
4118
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004119 hci_dev_lock(hdev);
4120
Johan Hedberg333ae952015-03-17 13:48:47 +02004121 cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004122 if (!cmd)
Johan Hedberg333ae952015-03-17 13:48:47 +02004123 cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004124
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004125 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02004126 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004127 mgmt_pending_remove(cmd);
Andre Guedes7c307722013-04-30 15:29:28 -03004128 }
4129
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004130 if (status) {
4131 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4132 goto unlock;
4133 }
4134
Andre Guedes7c307722013-04-30 15:29:28 -03004135 hci_discovery_set_state(hdev, DISCOVERY_FINDING);
Andre Guedes7c307722013-04-30 15:29:28 -03004136
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08004137 /* If the scan involves LE scan, pick proper timeout to schedule
4138 * hdev->le_scan_disable that will stop it.
4139 */
Andre Guedes7c307722013-04-30 15:29:28 -03004140 switch (hdev->discovery.type) {
4141 case DISCOV_TYPE_LE:
Lukasz Rymanowski3d5a76f2014-03-27 20:55:21 +01004142 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
Andre Guedes7c307722013-04-30 15:29:28 -03004143 break;
Andre Guedes7c307722013-04-30 15:29:28 -03004144 case DISCOV_TYPE_INTERLEAVED:
Jakub Pawlowski07d23342015-03-17 09:04:14 -07004145 /* When running simultaneous discovery, the LE scanning time
4146 * should occupy the whole discovery time sine BR/EDR inquiry
4147 * and LE scanning are scheduled by the controller.
4148 *
4149 * For interleaving discovery in comparison, BR/EDR inquiry
4150 * and LE scanning are done sequentially with separate
4151 * timeouts.
4152 */
4153 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
4154 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
4155 else
4156 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
Andre Guedes7c307722013-04-30 15:29:28 -03004157 break;
Andre Guedes7c307722013-04-30 15:29:28 -03004158 case DISCOV_TYPE_BREDR:
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004159 timeout = 0;
Andre Guedes7c307722013-04-30 15:29:28 -03004160 break;
Andre Guedes7c307722013-04-30 15:29:28 -03004161 default:
4162 BT_ERR("Invalid discovery type %d", hdev->discovery.type);
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004163 timeout = 0;
4164 break;
Andre Guedes7c307722013-04-30 15:29:28 -03004165 }
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01004166
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08004167 if (timeout) {
4168 /* When service discovery is used and the controller has
4169 * a strict duplicate filter, it is important to remember
4170 * the start and duration of the scan. This is required
4171 * for restarting scanning during the discovery phase.
4172 */
4173 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER,
4174 &hdev->quirks) &&
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08004175 hdev->discovery.result_filtering) {
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08004176 hdev->discovery.scan_start = jiffies;
4177 hdev->discovery.scan_duration = timeout;
4178 }
4179
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004180 queue_delayed_work(hdev->workqueue,
4181 &hdev->le_scan_disable, timeout);
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08004182 }
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01004183
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004184unlock:
4185 hci_dev_unlock(hdev);
Andre Guedes7c307722013-04-30 15:29:28 -03004186}
4187
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004188static int start_discovery(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004189 void *data, u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04004190{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004191 struct mgmt_cp_start_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004192 struct mgmt_pending_cmd *cmd;
Andre Guedes7c307722013-04-30 15:29:28 -03004193 struct hci_request req;
Marcel Holtmann80190442014-12-04 11:36:36 +01004194 u8 status;
Johan Hedberg14a53662011-04-27 10:29:56 -04004195 int err;
4196
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004197 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04004198
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004199 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004200
Johan Hedberg4b34ee782012-02-21 14:13:02 +02004201 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004202 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4203 MGMT_STATUS_NOT_POWERED,
4204 &cp->type, sizeof(cp->type));
Johan Hedbergbd2d1332011-11-07 23:13:37 +02004205 goto failed;
4206 }
4207
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004208 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004209 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004210 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4211 MGMT_STATUS_BUSY, &cp->type,
4212 sizeof(cp->type));
Andre Guedes642be6c2012-03-21 00:03:37 -03004213 goto failed;
4214 }
4215
Johan Hedberg2922a942014-12-05 13:36:06 +02004216 cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, data, len);
Johan Hedberg14a53662011-04-27 10:29:56 -04004217 if (!cmd) {
4218 err = -ENOMEM;
4219 goto failed;
4220 }
4221
Johan Hedberg2922a942014-12-05 13:36:06 +02004222 cmd->cmd_complete = generic_cmd_complete;
4223
Marcel Holtmann22078802014-12-05 11:45:22 +01004224 /* Clear the discovery filter first to free any previously
4225 * allocated memory for the UUID list.
4226 */
4227 hci_discovery_filter_clear(hdev);
4228
Andre Guedes4aab14e2012-02-17 20:39:36 -03004229 hdev->discovery.type = cp->type;
Marcel Holtmannda25cf62014-12-05 13:03:35 +01004230 hdev->discovery.report_invalid_rssi = false;
Andre Guedes4aab14e2012-02-17 20:39:36 -03004231
Andre Guedes7c307722013-04-30 15:29:28 -03004232 hci_req_init(&req, hdev);
4233
Marcel Holtmann80190442014-12-04 11:36:36 +01004234 if (!trigger_discovery(&req, &status)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004235 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4236 status, &cp->type, sizeof(cp->type));
Johan Hedberg04106752013-01-10 14:54:09 +02004237 mgmt_pending_remove(cmd);
4238 goto failed;
Andre Guedesf39799f2012-02-17 20:39:35 -03004239 }
Andre Guedes3fd24152012-02-03 17:48:01 -03004240
Andre Guedes7c307722013-04-30 15:29:28 -03004241 err = hci_req_run(&req, start_discovery_complete);
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004242 if (err < 0) {
Johan Hedberg14a53662011-04-27 10:29:56 -04004243 mgmt_pending_remove(cmd);
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004244 goto failed;
4245 }
4246
4247 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
Johan Hedberg14a53662011-04-27 10:29:56 -04004248
4249failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004250 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004251 return err;
4252}
4253
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004254static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd,
4255 u8 status)
Andre Guedes1183fdc2013-04-30 15:29:35 -03004256{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004257 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
4258 cmd->param, 1);
Johan Hedberg2922a942014-12-05 13:36:06 +02004259}
4260
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004261static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
4262 void *data, u16 len)
4263{
4264 struct mgmt_cp_start_service_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004265 struct mgmt_pending_cmd *cmd;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004266 struct hci_request req;
4267 const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16);
4268 u16 uuid_count, expected_len;
4269 u8 status;
Andre Guedes1183fdc2013-04-30 15:29:35 -03004270 int err;
4271
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004272 BT_DBG("%s", hdev->name);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004273
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004274 hci_dev_lock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004275
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004276 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004277 err = mgmt_cmd_complete(sk, hdev->id,
4278 MGMT_OP_START_SERVICE_DISCOVERY,
4279 MGMT_STATUS_NOT_POWERED,
4280 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004281 goto failed;
4282 }
4283
4284 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004285 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004286 err = mgmt_cmd_complete(sk, hdev->id,
4287 MGMT_OP_START_SERVICE_DISCOVERY,
4288 MGMT_STATUS_BUSY, &cp->type,
4289 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004290 goto failed;
4291 }
4292
4293 uuid_count = __le16_to_cpu(cp->uuid_count);
4294 if (uuid_count > max_uuid_count) {
4295 BT_ERR("service_discovery: too big uuid_count value %u",
4296 uuid_count);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004297 err = mgmt_cmd_complete(sk, hdev->id,
4298 MGMT_OP_START_SERVICE_DISCOVERY,
4299 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4300 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004301 goto failed;
4302 }
4303
4304 expected_len = sizeof(*cp) + uuid_count * 16;
4305 if (expected_len != len) {
4306 BT_ERR("service_discovery: expected %u bytes, got %u bytes",
4307 expected_len, len);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004308 err = mgmt_cmd_complete(sk, hdev->id,
4309 MGMT_OP_START_SERVICE_DISCOVERY,
4310 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4311 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004312 goto failed;
4313 }
4314
4315 cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY,
Johan Hedberg2922a942014-12-05 13:36:06 +02004316 hdev, data, len);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004317 if (!cmd) {
4318 err = -ENOMEM;
4319 goto failed;
4320 }
4321
Johan Hedberg2922a942014-12-05 13:36:06 +02004322 cmd->cmd_complete = service_discovery_cmd_complete;
4323
Marcel Holtmann22078802014-12-05 11:45:22 +01004324 /* Clear the discovery filter first to free any previously
4325 * allocated memory for the UUID list.
4326 */
4327 hci_discovery_filter_clear(hdev);
4328
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08004329 hdev->discovery.result_filtering = true;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004330 hdev->discovery.type = cp->type;
4331 hdev->discovery.rssi = cp->rssi;
4332 hdev->discovery.uuid_count = uuid_count;
4333
4334 if (uuid_count > 0) {
4335 hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16,
4336 GFP_KERNEL);
4337 if (!hdev->discovery.uuids) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004338 err = mgmt_cmd_complete(sk, hdev->id,
4339 MGMT_OP_START_SERVICE_DISCOVERY,
4340 MGMT_STATUS_FAILED,
4341 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004342 mgmt_pending_remove(cmd);
4343 goto failed;
4344 }
4345 }
4346
4347 hci_req_init(&req, hdev);
4348
4349 if (!trigger_discovery(&req, &status)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004350 err = mgmt_cmd_complete(sk, hdev->id,
4351 MGMT_OP_START_SERVICE_DISCOVERY,
4352 status, &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004353 mgmt_pending_remove(cmd);
4354 goto failed;
4355 }
4356
4357 err = hci_req_run(&req, start_discovery_complete);
4358 if (err < 0) {
4359 mgmt_pending_remove(cmd);
4360 goto failed;
4361 }
4362
4363 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
4364
4365failed:
4366 hci_dev_unlock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004367 return err;
4368}
4369
Marcel Holtmann1904a852015-01-11 13:50:44 -08004370static void stop_discovery_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Andre Guedes0e05bba2013-04-30 15:29:33 -03004371{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004372 struct mgmt_pending_cmd *cmd;
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004373
Andre Guedes0e05bba2013-04-30 15:29:33 -03004374 BT_DBG("status %d", status);
4375
4376 hci_dev_lock(hdev);
4377
Johan Hedberg333ae952015-03-17 13:48:47 +02004378 cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004379 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02004380 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004381 mgmt_pending_remove(cmd);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004382 }
4383
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004384 if (!status)
4385 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004386
Andre Guedes0e05bba2013-04-30 15:29:33 -03004387 hci_dev_unlock(hdev);
4388}
4389
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004390static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004391 u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04004392{
Johan Hedbergd9306502012-02-20 23:25:18 +02004393 struct mgmt_cp_stop_discovery *mgmt_cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004394 struct mgmt_pending_cmd *cmd;
Andre Guedes0e05bba2013-04-30 15:29:33 -03004395 struct hci_request req;
Johan Hedberg14a53662011-04-27 10:29:56 -04004396 int err;
4397
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004398 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04004399
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004400 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004401
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004402 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004403 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4404 MGMT_STATUS_REJECTED, &mgmt_cp->type,
4405 sizeof(mgmt_cp->type));
Johan Hedbergd9306502012-02-20 23:25:18 +02004406 goto unlock;
4407 }
4408
4409 if (hdev->discovery.type != mgmt_cp->type) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004410 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4411 MGMT_STATUS_INVALID_PARAMS,
4412 &mgmt_cp->type, sizeof(mgmt_cp->type));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004413 goto unlock;
Johan Hedbergff9ef572012-01-04 14:23:45 +02004414 }
4415
Johan Hedberg2922a942014-12-05 13:36:06 +02004416 cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len);
Johan Hedberg14a53662011-04-27 10:29:56 -04004417 if (!cmd) {
4418 err = -ENOMEM;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004419 goto unlock;
Johan Hedberg14a53662011-04-27 10:29:56 -04004420 }
4421
Johan Hedberg2922a942014-12-05 13:36:06 +02004422 cmd->cmd_complete = generic_cmd_complete;
4423
Andre Guedes0e05bba2013-04-30 15:29:33 -03004424 hci_req_init(&req, hdev);
4425
Johan Hedberg21a60d32014-06-10 14:05:58 +03004426 hci_stop_discovery(&req);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004427
Johan Hedberg21a60d32014-06-10 14:05:58 +03004428 err = hci_req_run(&req, stop_discovery_complete);
4429 if (!err) {
4430 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004431 goto unlock;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004432 }
4433
Johan Hedberg21a60d32014-06-10 14:05:58 +03004434 mgmt_pending_remove(cmd);
4435
4436 /* If no HCI commands were sent we're done */
4437 if (err == -ENODATA) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004438 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0,
4439 &mgmt_cp->type, sizeof(mgmt_cp->type));
Johan Hedberg21a60d32014-06-10 14:05:58 +03004440 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4441 }
Johan Hedberg14a53662011-04-27 10:29:56 -04004442
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004443unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004444 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004445 return err;
4446}
4447
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004448static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004449 u16 len)
Johan Hedberg561aafb2012-01-04 13:31:59 +02004450{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004451 struct mgmt_cp_confirm_name *cp = data;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004452 struct inquiry_entry *e;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004453 int err;
4454
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004455 BT_DBG("%s", hdev->name);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004456
Johan Hedberg561aafb2012-01-04 13:31:59 +02004457 hci_dev_lock(hdev);
4458
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004459 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004460 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4461 MGMT_STATUS_FAILED, &cp->addr,
4462 sizeof(cp->addr));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004463 goto failed;
4464 }
4465
Johan Hedberga198e7b2012-02-17 14:27:06 +02004466 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004467 if (!e) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004468 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4469 MGMT_STATUS_INVALID_PARAMS, &cp->addr,
4470 sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02004471 goto failed;
4472 }
4473
4474 if (cp->name_known) {
4475 e->name_state = NAME_KNOWN;
4476 list_del(&e->list);
4477 } else {
4478 e->name_state = NAME_NEEDED;
Johan Hedberga3d4e202012-01-09 00:53:02 +02004479 hci_inquiry_cache_update_resolve(hdev, e);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004480 }
4481
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004482 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0,
4483 &cp->addr, sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02004484
4485failed:
4486 hci_dev_unlock(hdev);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004487 return err;
4488}
4489
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004490static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004491 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03004492{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004493 struct mgmt_cp_block_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004494 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03004495 int err;
4496
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004497 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03004498
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004499 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004500 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
4501 MGMT_STATUS_INVALID_PARAMS,
4502 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004503
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004504 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03004505
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004506 err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr,
4507 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004508 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004509 status = MGMT_STATUS_FAILED;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004510 goto done;
4511 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004512
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004513 mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4514 sk);
4515 status = MGMT_STATUS_SUCCESS;
4516
4517done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004518 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
4519 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03004520
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004521 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03004522
4523 return err;
4524}
4525
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004526static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004527 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03004528{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004529 struct mgmt_cp_unblock_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004530 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03004531 int err;
4532
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004533 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03004534
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004535 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004536 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
4537 MGMT_STATUS_INVALID_PARAMS,
4538 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004539
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004540 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03004541
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004542 err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr,
4543 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004544 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004545 status = MGMT_STATUS_INVALID_PARAMS;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004546 goto done;
4547 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004548
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004549 mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4550 sk);
4551 status = MGMT_STATUS_SUCCESS;
4552
4553done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004554 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
4555 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03004556
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004557 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03004558
4559 return err;
4560}
4561
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004562static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
4563 u16 len)
4564{
4565 struct mgmt_cp_set_device_id *cp = data;
Johan Hedberg890ea892013-03-15 17:06:52 -05004566 struct hci_request req;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004567 int err;
Szymon Jancc72d4b82012-03-16 16:02:57 +01004568 __u16 source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004569
4570 BT_DBG("%s", hdev->name);
4571
Szymon Jancc72d4b82012-03-16 16:02:57 +01004572 source = __le16_to_cpu(cp->source);
4573
4574 if (source > 0x0002)
Johan Hedberga69e8372015-03-06 21:08:53 +02004575 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
4576 MGMT_STATUS_INVALID_PARAMS);
Szymon Jancc72d4b82012-03-16 16:02:57 +01004577
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004578 hci_dev_lock(hdev);
4579
Szymon Jancc72d4b82012-03-16 16:02:57 +01004580 hdev->devid_source = source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004581 hdev->devid_vendor = __le16_to_cpu(cp->vendor);
4582 hdev->devid_product = __le16_to_cpu(cp->product);
4583 hdev->devid_version = __le16_to_cpu(cp->version);
4584
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004585 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0,
4586 NULL, 0);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004587
Johan Hedberg890ea892013-03-15 17:06:52 -05004588 hci_req_init(&req, hdev);
4589 update_eir(&req);
4590 hci_req_run(&req, NULL);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004591
4592 hci_dev_unlock(hdev);
4593
4594 return err;
4595}
4596
Arman Uguray24b4f382015-03-23 15:57:12 -07004597static void enable_advertising_instance(struct hci_dev *hdev, u8 status,
4598 u16 opcode)
4599{
4600 BT_DBG("status %d", status);
4601}
4602
Marcel Holtmann1904a852015-01-11 13:50:44 -08004603static void set_advertising_complete(struct hci_dev *hdev, u8 status,
4604 u16 opcode)
Johan Hedberg4375f102013-09-25 13:26:10 +03004605{
4606 struct cmd_lookup match = { NULL, hdev };
Arman Uguray24b4f382015-03-23 15:57:12 -07004607 struct hci_request req;
Johan Hedberg4375f102013-09-25 13:26:10 +03004608
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304609 hci_dev_lock(hdev);
4610
Johan Hedberg4375f102013-09-25 13:26:10 +03004611 if (status) {
4612 u8 mgmt_err = mgmt_status(status);
4613
4614 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev,
4615 cmd_status_rsp, &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304616 goto unlock;
Johan Hedberg4375f102013-09-25 13:26:10 +03004617 }
4618
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004619 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004620 hci_dev_set_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03004621 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004622 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03004623
Johan Hedberg4375f102013-09-25 13:26:10 +03004624 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
4625 &match);
4626
4627 new_settings(hdev, match.sk);
4628
4629 if (match.sk)
4630 sock_put(match.sk);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304631
Arman Uguray24b4f382015-03-23 15:57:12 -07004632 /* If "Set Advertising" was just disabled and instance advertising was
4633 * set up earlier, then enable the advertising instance.
4634 */
4635 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
4636 !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
4637 goto unlock;
4638
4639 hci_req_init(&req, hdev);
4640
4641 update_adv_data(&req);
4642 enable_advertising(&req);
4643
4644 if (hci_req_run(&req, enable_advertising_instance) < 0)
4645 BT_ERR("Failed to re-configure advertising");
4646
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304647unlock:
4648 hci_dev_unlock(hdev);
Johan Hedberg4375f102013-09-25 13:26:10 +03004649}
4650
Marcel Holtmann21b51872013-10-10 09:47:53 -07004651static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
4652 u16 len)
Johan Hedberg4375f102013-09-25 13:26:10 +03004653{
4654 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004655 struct mgmt_pending_cmd *cmd;
Johan Hedberg4375f102013-09-25 13:26:10 +03004656 struct hci_request req;
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004657 u8 val, status;
Johan Hedberg4375f102013-09-25 13:26:10 +03004658 int err;
4659
4660 BT_DBG("request for %s", hdev->name);
4661
Johan Hedberge6fe7982013-10-02 15:45:22 +03004662 status = mgmt_le_support(hdev);
4663 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02004664 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4665 status);
Johan Hedberg4375f102013-09-25 13:26:10 +03004666
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004667 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02004668 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4669 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4375f102013-09-25 13:26:10 +03004670
4671 hci_dev_lock(hdev);
4672
4673 val = !!cp->val;
Johan Hedberg4375f102013-09-25 13:26:10 +03004674
Johan Hedbergf74ca9b2013-10-08 15:52:18 +02004675 /* The following conditions are ones which mean that we should
4676 * not do any HCI communication but directly send a mgmt
4677 * response to user space (after toggling the flag if
4678 * necessary).
4679 */
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004680 if (!hdev_is_powered(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004681 (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
4682 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) ||
Johan Hedberge8bb6b92014-07-08 15:07:53 +03004683 hci_conn_num(hdev, LE_LINK) > 0 ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004684 (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Johan Hedberge8bb6b92014-07-08 15:07:53 +03004685 hdev->le_scan_type == LE_SCAN_ACTIVE)) {
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004686 bool changed;
Johan Hedberg4375f102013-09-25 13:26:10 +03004687
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004688 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07004689 changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004690 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004691 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004692 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004693 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004694 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07004695 changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004696 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Johan Hedberg4375f102013-09-25 13:26:10 +03004697 }
4698
4699 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
4700 if (err < 0)
4701 goto unlock;
4702
4703 if (changed)
4704 err = new_settings(hdev, sk);
4705
4706 goto unlock;
4707 }
4708
Johan Hedberg333ae952015-03-17 13:48:47 +02004709 if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
4710 pending_find(MGMT_OP_SET_LE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004711 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4712 MGMT_STATUS_BUSY);
Johan Hedberg4375f102013-09-25 13:26:10 +03004713 goto unlock;
4714 }
4715
4716 cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
4717 if (!cmd) {
4718 err = -ENOMEM;
4719 goto unlock;
4720 }
4721
4722 hci_req_init(&req, hdev);
4723
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004724 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004725 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004726 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004727 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004728
Arman Uguray24b4f382015-03-23 15:57:12 -07004729 if (val) {
4730 /* Switch to instance "0" for the Set Advertising setting. */
4731 update_adv_data_for_instance(&req, 0);
Arman Uguray4117ed72015-03-23 15:57:14 -07004732 update_scan_rsp_data_for_instance(&req, 0);
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07004733 enable_advertising(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07004734 } else {
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07004735 disable_advertising(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07004736 }
Johan Hedberg4375f102013-09-25 13:26:10 +03004737
4738 err = hci_req_run(&req, set_advertising_complete);
4739 if (err < 0)
4740 mgmt_pending_remove(cmd);
4741
4742unlock:
4743 hci_dev_unlock(hdev);
4744 return err;
4745}
4746
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004747static int set_static_address(struct sock *sk, struct hci_dev *hdev,
4748 void *data, u16 len)
4749{
4750 struct mgmt_cp_set_static_address *cp = data;
4751 int err;
4752
4753 BT_DBG("%s", hdev->name);
4754
Marcel Holtmann62af4442013-10-02 22:10:32 -07004755 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004756 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4757 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004758
4759 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004760 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4761 MGMT_STATUS_REJECTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004762
4763 if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
4764 if (!bacmp(&cp->bdaddr, BDADDR_NONE))
Johan Hedberga69e8372015-03-06 21:08:53 +02004765 return mgmt_cmd_status(sk, hdev->id,
4766 MGMT_OP_SET_STATIC_ADDRESS,
4767 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004768
4769 /* Two most significant bits shall be set */
4770 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
Johan Hedberga69e8372015-03-06 21:08:53 +02004771 return mgmt_cmd_status(sk, hdev->id,
4772 MGMT_OP_SET_STATIC_ADDRESS,
4773 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004774 }
4775
4776 hci_dev_lock(hdev);
4777
4778 bacpy(&hdev->static_addr, &cp->bdaddr);
4779
Marcel Holtmann93690c22015-03-06 10:11:21 -08004780 err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev);
4781 if (err < 0)
4782 goto unlock;
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004783
Marcel Holtmann93690c22015-03-06 10:11:21 -08004784 err = new_settings(hdev, sk);
4785
4786unlock:
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004787 hci_dev_unlock(hdev);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004788 return err;
4789}
4790
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004791static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
4792 void *data, u16 len)
4793{
4794 struct mgmt_cp_set_scan_params *cp = data;
4795 __u16 interval, window;
4796 int err;
4797
4798 BT_DBG("%s", hdev->name);
4799
4800 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004801 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4802 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004803
4804 interval = __le16_to_cpu(cp->interval);
4805
4806 if (interval < 0x0004 || interval > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02004807 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4808 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004809
4810 window = __le16_to_cpu(cp->window);
4811
4812 if (window < 0x0004 || window > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02004813 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4814 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004815
Marcel Holtmann899e1072013-10-14 09:55:32 -07004816 if (window > interval)
Johan Hedberga69e8372015-03-06 21:08:53 +02004817 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4818 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann899e1072013-10-14 09:55:32 -07004819
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004820 hci_dev_lock(hdev);
4821
4822 hdev->le_scan_interval = interval;
4823 hdev->le_scan_window = window;
4824
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004825 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0,
4826 NULL, 0);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004827
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03004828 /* If background scan is running, restart it so new parameters are
4829 * loaded.
4830 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004831 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03004832 hdev->discovery.state == DISCOVERY_STOPPED) {
4833 struct hci_request req;
4834
4835 hci_req_init(&req, hdev);
4836
4837 hci_req_add_le_scan_disable(&req);
4838 hci_req_add_le_passive_scan(&req);
4839
4840 hci_req_run(&req, NULL);
4841 }
4842
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004843 hci_dev_unlock(hdev);
4844
4845 return err;
4846}
4847
Marcel Holtmann1904a852015-01-11 13:50:44 -08004848static void fast_connectable_complete(struct hci_dev *hdev, u8 status,
4849 u16 opcode)
Johan Hedberg33e38b32013-03-15 17:07:05 -05004850{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004851 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004852
4853 BT_DBG("status 0x%02x", status);
4854
4855 hci_dev_lock(hdev);
4856
Johan Hedberg333ae952015-03-17 13:48:47 +02004857 cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004858 if (!cmd)
4859 goto unlock;
4860
4861 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004862 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4863 mgmt_status(status));
Johan Hedberg33e38b32013-03-15 17:07:05 -05004864 } else {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004865 struct mgmt_mode *cp = cmd->param;
4866
4867 if (cp->val)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004868 hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004869 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004870 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004871
Johan Hedberg33e38b32013-03-15 17:07:05 -05004872 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
4873 new_settings(hdev, cmd->sk);
4874 }
4875
4876 mgmt_pending_remove(cmd);
4877
4878unlock:
4879 hci_dev_unlock(hdev);
4880}
4881
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004882static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004883 void *data, u16 len)
Antti Julkuf6422ec2011-06-22 13:11:56 +03004884{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004885 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004886 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004887 struct hci_request req;
Antti Julkuf6422ec2011-06-22 13:11:56 +03004888 int err;
4889
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004890 BT_DBG("%s", hdev->name);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004891
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004892 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
Johan Hedberg56f87902013-10-02 13:43:13 +03004893 hdev->hci_ver < BLUETOOTH_VER_1_2)
Johan Hedberga69e8372015-03-06 21:08:53 +02004894 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4895 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03004896
Johan Hedberga7e80f22013-01-09 16:05:19 +02004897 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02004898 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4899 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02004900
Antti Julkuf6422ec2011-06-22 13:11:56 +03004901 hci_dev_lock(hdev);
4902
Johan Hedberg333ae952015-03-17 13:48:47 +02004903 if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004904 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4905 MGMT_STATUS_BUSY);
Johan Hedberg05cbf292013-03-15 17:07:07 -05004906 goto unlock;
4907 }
4908
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004909 if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004910 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4911 hdev);
4912 goto unlock;
4913 }
4914
Johan Hedberg406ef2a2015-03-10 20:14:27 +02004915 if (!hdev_is_powered(hdev)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07004916 hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg406ef2a2015-03-10 20:14:27 +02004917 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4918 hdev);
4919 new_settings(hdev, sk);
4920 goto unlock;
4921 }
4922
Johan Hedberg33e38b32013-03-15 17:07:05 -05004923 cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev,
4924 data, len);
4925 if (!cmd) {
4926 err = -ENOMEM;
4927 goto unlock;
4928 }
4929
4930 hci_req_init(&req, hdev);
4931
Johan Hedberg406d7802013-03-15 17:07:09 -05004932 write_fast_connectable(&req, cp->val);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004933
4934 err = hci_req_run(&req, fast_connectable_complete);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004935 if (err < 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004936 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4937 MGMT_STATUS_FAILED);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004938 mgmt_pending_remove(cmd);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004939 }
4940
Johan Hedberg33e38b32013-03-15 17:07:05 -05004941unlock:
Antti Julkuf6422ec2011-06-22 13:11:56 +03004942 hci_dev_unlock(hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004943
Antti Julkuf6422ec2011-06-22 13:11:56 +03004944 return err;
4945}
4946
Marcel Holtmann1904a852015-01-11 13:50:44 -08004947static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg0663ca22013-10-02 13:43:14 +03004948{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004949 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03004950
4951 BT_DBG("status 0x%02x", status);
4952
4953 hci_dev_lock(hdev);
4954
Johan Hedberg333ae952015-03-17 13:48:47 +02004955 cmd = pending_find(MGMT_OP_SET_BREDR, hdev);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004956 if (!cmd)
4957 goto unlock;
4958
4959 if (status) {
4960 u8 mgmt_err = mgmt_status(status);
4961
4962 /* We need to restore the flag if related HCI commands
4963 * failed.
4964 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004965 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004966
Johan Hedberga69e8372015-03-06 21:08:53 +02004967 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004968 } else {
4969 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
4970 new_settings(hdev, cmd->sk);
4971 }
4972
4973 mgmt_pending_remove(cmd);
4974
4975unlock:
4976 hci_dev_unlock(hdev);
4977}
4978
4979static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
4980{
4981 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004982 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03004983 struct hci_request req;
4984 int err;
4985
4986 BT_DBG("request for %s", hdev->name);
4987
4988 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004989 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4990 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004991
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004992 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02004993 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4994 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004995
4996 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02004997 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4998 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004999
5000 hci_dev_lock(hdev);
5001
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005002 if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg0663ca22013-10-02 13:43:14 +03005003 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
5004 goto unlock;
5005 }
5006
5007 if (!hdev_is_powered(hdev)) {
5008 if (!cp->val) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005009 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
5010 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
5011 hci_dev_clear_flag(hdev, HCI_LINK_SECURITY);
5012 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
5013 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005014 }
5015
Marcel Holtmannce05d602015-03-13 02:11:03 -07005016 hci_dev_change_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005017
5018 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
5019 if (err < 0)
5020 goto unlock;
5021
5022 err = new_settings(hdev, sk);
5023 goto unlock;
5024 }
5025
5026 /* Reject disabling when powered on */
5027 if (!cp->val) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005028 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5029 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005030 goto unlock;
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08005031 } else {
5032 /* When configuring a dual-mode controller to operate
5033 * with LE only and using a static address, then switching
5034 * BR/EDR back on is not allowed.
5035 *
5036 * Dual-mode controllers shall operate with the public
5037 * address as its identity address for BR/EDR and LE. So
5038 * reject the attempt to create an invalid configuration.
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08005039 *
5040 * The same restrictions applies when secure connections
5041 * has been enabled. For BR/EDR this is a controller feature
5042 * while for LE it is a host stack feature. This means that
5043 * switching BR/EDR back on when secure connections has been
5044 * enabled is not a supported transaction.
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08005045 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005046 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08005047 (bacmp(&hdev->static_addr, BDADDR_ANY) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005048 hci_dev_test_flag(hdev, HCI_SC_ENABLED))) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005049 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5050 MGMT_STATUS_REJECTED);
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08005051 goto unlock;
5052 }
Johan Hedberg0663ca22013-10-02 13:43:14 +03005053 }
5054
Johan Hedberg333ae952015-03-17 13:48:47 +02005055 if (pending_find(MGMT_OP_SET_BREDR, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005056 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5057 MGMT_STATUS_BUSY);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005058 goto unlock;
5059 }
5060
5061 cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len);
5062 if (!cmd) {
5063 err = -ENOMEM;
5064 goto unlock;
5065 }
5066
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07005067 /* We need to flip the bit already here so that update_adv_data
Johan Hedberg0663ca22013-10-02 13:43:14 +03005068 * generates the correct flags.
5069 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005070 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005071
5072 hci_req_init(&req, hdev);
Johan Hedbergaa8af462013-10-14 21:15:26 +03005073
Johan Hedberg432df052014-08-01 11:13:31 +03005074 write_fast_connectable(&req, false);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02005075 __hci_update_page_scan(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03005076
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07005077 /* Since only the advertising data flags will change, there
5078 * is no need to update the scan response data.
5079 */
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07005080 update_adv_data(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03005081
Johan Hedberg0663ca22013-10-02 13:43:14 +03005082 err = hci_req_run(&req, set_bredr_complete);
5083 if (err < 0)
5084 mgmt_pending_remove(cmd);
5085
5086unlock:
5087 hci_dev_unlock(hdev);
5088 return err;
5089}
5090
Johan Hedberga1443f52015-01-23 15:42:46 +02005091static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
5092{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005093 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02005094 struct mgmt_mode *cp;
5095
5096 BT_DBG("%s status %u", hdev->name, status);
5097
5098 hci_dev_lock(hdev);
5099
Johan Hedberg333ae952015-03-17 13:48:47 +02005100 cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev);
Johan Hedberga1443f52015-01-23 15:42:46 +02005101 if (!cmd)
5102 goto unlock;
5103
5104 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005105 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
5106 mgmt_status(status));
Johan Hedberga1443f52015-01-23 15:42:46 +02005107 goto remove;
5108 }
5109
5110 cp = cmd->param;
5111
5112 switch (cp->val) {
5113 case 0x00:
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005114 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
5115 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02005116 break;
5117 case 0x01:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005118 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005119 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02005120 break;
5121 case 0x02:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005122 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
5123 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02005124 break;
5125 }
5126
5127 send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev);
5128 new_settings(hdev, cmd->sk);
5129
5130remove:
5131 mgmt_pending_remove(cmd);
5132unlock:
5133 hci_dev_unlock(hdev);
5134}
5135
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005136static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
5137 void *data, u16 len)
5138{
5139 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005140 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02005141 struct hci_request req;
Johan Hedberga3209692014-05-26 11:23:35 +03005142 u8 val;
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005143 int err;
5144
5145 BT_DBG("request for %s", hdev->name);
5146
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08005147 if (!lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005148 !hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02005149 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5150 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005151
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005152 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Johan Hedberg59200282015-01-28 19:56:00 +02005153 lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005154 !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02005155 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5156 MGMT_STATUS_REJECTED);
Marcel Holtmanned93ec62015-01-22 11:15:22 -08005157
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005158 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02005159 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005160 MGMT_STATUS_INVALID_PARAMS);
5161
5162 hci_dev_lock(hdev);
5163
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08005164 if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005165 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005166 bool changed;
5167
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005168 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07005169 changed = !hci_dev_test_and_set_flag(hdev,
5170 HCI_SC_ENABLED);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005171 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005172 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005173 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005174 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005175 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005176 changed = hci_dev_test_and_clear_flag(hdev,
5177 HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005178 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005179 }
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005180
5181 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
5182 if (err < 0)
5183 goto failed;
5184
5185 if (changed)
5186 err = new_settings(hdev, sk);
5187
5188 goto failed;
5189 }
5190
Johan Hedberg333ae952015-03-17 13:48:47 +02005191 if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005192 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5193 MGMT_STATUS_BUSY);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005194 goto failed;
5195 }
5196
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005197 val = !!cp->val;
5198
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005199 if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
5200 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005201 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
5202 goto failed;
5203 }
5204
5205 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len);
5206 if (!cmd) {
5207 err = -ENOMEM;
5208 goto failed;
5209 }
5210
Johan Hedberga1443f52015-01-23 15:42:46 +02005211 hci_req_init(&req, hdev);
5212 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val);
5213 err = hci_req_run(&req, sc_enable_complete);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005214 if (err < 0) {
5215 mgmt_pending_remove(cmd);
5216 goto failed;
5217 }
5218
5219failed:
5220 hci_dev_unlock(hdev);
5221 return err;
5222}
5223
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005224static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
5225 void *data, u16 len)
5226{
5227 struct mgmt_mode *cp = data;
Johan Hedbergb97109792014-06-24 14:00:28 +03005228 bool changed, use_changed;
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005229 int err;
5230
5231 BT_DBG("request for %s", hdev->name);
5232
Johan Hedbergb97109792014-06-24 14:00:28 +03005233 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02005234 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
5235 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005236
5237 hci_dev_lock(hdev);
5238
5239 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07005240 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005241 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005242 changed = hci_dev_test_and_clear_flag(hdev,
5243 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005244
Johan Hedbergb97109792014-06-24 14:00:28 +03005245 if (cp->val == 0x02)
Marcel Holtmann238be782015-03-13 02:11:06 -07005246 use_changed = !hci_dev_test_and_set_flag(hdev,
5247 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03005248 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005249 use_changed = hci_dev_test_and_clear_flag(hdev,
5250 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03005251
5252 if (hdev_is_powered(hdev) && use_changed &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005253 hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergb97109792014-06-24 14:00:28 +03005254 u8 mode = (cp->val == 0x02) ? 0x01 : 0x00;
5255 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
5256 sizeof(mode), &mode);
5257 }
5258
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005259 err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev);
5260 if (err < 0)
5261 goto unlock;
5262
5263 if (changed)
5264 err = new_settings(hdev, sk);
5265
5266unlock:
5267 hci_dev_unlock(hdev);
5268 return err;
5269}
5270
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005271static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5272 u16 len)
5273{
5274 struct mgmt_cp_set_privacy *cp = cp_data;
5275 bool changed;
5276 int err;
5277
5278 BT_DBG("request for %s", hdev->name);
5279
5280 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005281 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5282 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005283
5284 if (cp->privacy != 0x00 && cp->privacy != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02005285 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5286 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005287
5288 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005289 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5290 MGMT_STATUS_REJECTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005291
5292 hci_dev_lock(hdev);
5293
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02005294 /* If user space supports this command it is also expected to
5295 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
5296 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005297 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02005298
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005299 if (cp->privacy) {
Marcel Holtmann238be782015-03-13 02:11:06 -07005300 changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005301 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005302 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005303 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005304 changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005305 memset(hdev->irk, 0, sizeof(hdev->irk));
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005306 hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005307 }
5308
5309 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
5310 if (err < 0)
5311 goto unlock;
5312
5313 if (changed)
5314 err = new_settings(hdev, sk);
5315
5316unlock:
5317 hci_dev_unlock(hdev);
5318 return err;
5319}
5320
Johan Hedberg41edf162014-02-18 10:19:35 +02005321static bool irk_is_valid(struct mgmt_irk_info *irk)
5322{
5323 switch (irk->addr.type) {
5324 case BDADDR_LE_PUBLIC:
5325 return true;
5326
5327 case BDADDR_LE_RANDOM:
5328 /* Two most significant bits shall be set */
5329 if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0)
5330 return false;
5331 return true;
5332 }
5333
5334 return false;
5335}
5336
5337static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5338 u16 len)
5339{
5340 struct mgmt_cp_load_irks *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005341 const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) /
5342 sizeof(struct mgmt_irk_info));
Johan Hedberg41edf162014-02-18 10:19:35 +02005343 u16 irk_count, expected_len;
5344 int i, err;
5345
5346 BT_DBG("request for %s", hdev->name);
5347
5348 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005349 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5350 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg41edf162014-02-18 10:19:35 +02005351
5352 irk_count = __le16_to_cpu(cp->irk_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005353 if (irk_count > max_irk_count) {
5354 BT_ERR("load_irks: too big irk_count value %u", irk_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02005355 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5356 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005357 }
Johan Hedberg41edf162014-02-18 10:19:35 +02005358
5359 expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info);
5360 if (expected_len != len) {
5361 BT_ERR("load_irks: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02005362 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02005363 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5364 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02005365 }
5366
5367 BT_DBG("%s irk_count %u", hdev->name, irk_count);
5368
5369 for (i = 0; i < irk_count; i++) {
5370 struct mgmt_irk_info *key = &cp->irks[i];
5371
5372 if (!irk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02005373 return mgmt_cmd_status(sk, hdev->id,
5374 MGMT_OP_LOAD_IRKS,
5375 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02005376 }
5377
5378 hci_dev_lock(hdev);
5379
5380 hci_smp_irks_clear(hdev);
5381
5382 for (i = 0; i < irk_count; i++) {
5383 struct mgmt_irk_info *irk = &cp->irks[i];
5384 u8 addr_type;
5385
5386 if (irk->addr.type == BDADDR_LE_PUBLIC)
5387 addr_type = ADDR_LE_DEV_PUBLIC;
5388 else
5389 addr_type = ADDR_LE_DEV_RANDOM;
5390
5391 hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val,
5392 BDADDR_ANY);
5393 }
5394
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005395 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedberg41edf162014-02-18 10:19:35 +02005396
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005397 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
Johan Hedberg41edf162014-02-18 10:19:35 +02005398
5399 hci_dev_unlock(hdev);
5400
5401 return err;
5402}
5403
Johan Hedberg3f706b72013-01-20 14:27:16 +02005404static bool ltk_is_valid(struct mgmt_ltk_info *key)
5405{
5406 if (key->master != 0x00 && key->master != 0x01)
5407 return false;
Marcel Holtmann490cb0b2014-02-16 12:59:05 -08005408
5409 switch (key->addr.type) {
5410 case BDADDR_LE_PUBLIC:
5411 return true;
5412
5413 case BDADDR_LE_RANDOM:
5414 /* Two most significant bits shall be set */
5415 if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0)
5416 return false;
5417 return true;
5418 }
5419
5420 return false;
Johan Hedberg3f706b72013-01-20 14:27:16 +02005421}
5422
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005423static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005424 void *cp_data, u16 len)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005425{
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005426 struct mgmt_cp_load_long_term_keys *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005427 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
5428 sizeof(struct mgmt_ltk_info));
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005429 u16 key_count, expected_len;
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005430 int i, err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005431
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07005432 BT_DBG("request for %s", hdev->name);
5433
5434 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005435 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5436 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07005437
Marcel Holtmann1f350c82012-03-12 20:31:08 -07005438 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005439 if (key_count > max_key_count) {
5440 BT_ERR("load_ltks: too big key_count value %u", key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02005441 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5442 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005443 }
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005444
5445 expected_len = sizeof(*cp) + key_count *
5446 sizeof(struct mgmt_ltk_info);
5447 if (expected_len != len) {
5448 BT_ERR("load_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02005449 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02005450 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5451 MGMT_STATUS_INVALID_PARAMS);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005452 }
5453
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005454 BT_DBG("%s key_count %u", hdev->name, key_count);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005455
Johan Hedberg54ad6d82013-01-20 14:27:15 +02005456 for (i = 0; i < key_count; i++) {
5457 struct mgmt_ltk_info *key = &cp->keys[i];
5458
Johan Hedberg3f706b72013-01-20 14:27:16 +02005459 if (!ltk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02005460 return mgmt_cmd_status(sk, hdev->id,
5461 MGMT_OP_LOAD_LONG_TERM_KEYS,
5462 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg54ad6d82013-01-20 14:27:15 +02005463 }
5464
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005465 hci_dev_lock(hdev);
5466
5467 hci_smp_ltks_clear(hdev);
5468
5469 for (i = 0; i < key_count; i++) {
5470 struct mgmt_ltk_info *key = &cp->keys[i];
Johan Hedbergd7b25452014-05-23 13:19:53 +03005471 u8 type, addr_type, authenticated;
Marcel Holtmann79d95a12013-10-13 03:57:38 -07005472
5473 if (key->addr.type == BDADDR_LE_PUBLIC)
5474 addr_type = ADDR_LE_DEV_PUBLIC;
5475 else
5476 addr_type = ADDR_LE_DEV_RANDOM;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005477
Johan Hedberg61b43352014-05-29 19:36:53 +03005478 switch (key->type) {
5479 case MGMT_LTK_UNAUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03005480 authenticated = 0x00;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005481 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03005482 break;
5483 case MGMT_LTK_AUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03005484 authenticated = 0x01;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005485 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03005486 break;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005487 case MGMT_LTK_P256_UNAUTH:
5488 authenticated = 0x00;
5489 type = SMP_LTK_P256;
5490 break;
5491 case MGMT_LTK_P256_AUTH:
5492 authenticated = 0x01;
5493 type = SMP_LTK_P256;
5494 break;
5495 case MGMT_LTK_P256_DEBUG:
5496 authenticated = 0x00;
5497 type = SMP_LTK_P256_DEBUG;
Johan Hedberg61b43352014-05-29 19:36:53 +03005498 default:
5499 continue;
5500 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03005501
Johan Hedberg35d70272014-02-19 14:57:47 +02005502 hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type,
Johan Hedbergd7b25452014-05-23 13:19:53 +03005503 authenticated, key->val, key->enc_size, key->ediv,
Johan Hedberg35d70272014-02-19 14:57:47 +02005504 key->rand);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005505 }
5506
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005507 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005508 NULL, 0);
5509
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005510 hci_dev_unlock(hdev);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005511
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005512 return err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005513}
5514
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005515static int conn_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005516{
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005517 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005518 struct mgmt_rp_get_conn_info rp;
Johan Hedberg9df74652014-12-19 22:26:03 +02005519 int err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005520
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005521 memcpy(&rp.addr, cmd->param, sizeof(rp.addr));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005522
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005523 if (status == MGMT_STATUS_SUCCESS) {
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005524 rp.rssi = conn->rssi;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005525 rp.tx_power = conn->tx_power;
5526 rp.max_tx_power = conn->max_tx_power;
5527 } else {
5528 rp.rssi = HCI_RSSI_INVALID;
5529 rp.tx_power = HCI_TX_POWER_INVALID;
5530 rp.max_tx_power = HCI_TX_POWER_INVALID;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005531 }
5532
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005533 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO,
5534 status, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005535
5536 hci_conn_drop(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005537 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02005538
5539 return err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005540}
5541
Marcel Holtmann1904a852015-01-11 13:50:44 -08005542static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status,
5543 u16 opcode)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005544{
5545 struct hci_cp_read_rssi *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005546 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005547 struct hci_conn *conn;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005548 u16 handle;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005549 u8 status;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005550
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005551 BT_DBG("status 0x%02x", hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005552
5553 hci_dev_lock(hdev);
5554
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005555 /* Commands sent in request are either Read RSSI or Read Transmit Power
5556 * Level so we check which one was last sent to retrieve connection
5557 * handle. Both commands have handle as first parameter so it's safe to
5558 * cast data on the same command struct.
5559 *
5560 * First command sent is always Read RSSI and we fail only if it fails.
5561 * In other case we simply override error to indicate success as we
5562 * already remembered if TX power value is actually valid.
5563 */
5564 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI);
5565 if (!cp) {
5566 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005567 status = MGMT_STATUS_SUCCESS;
5568 } else {
5569 status = mgmt_status(hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005570 }
5571
5572 if (!cp) {
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005573 BT_ERR("invalid sent_cmd in conn_info response");
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005574 goto unlock;
5575 }
5576
5577 handle = __le16_to_cpu(cp->handle);
5578 conn = hci_conn_hash_lookup_handle(hdev, handle);
5579 if (!conn) {
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005580 BT_ERR("unknown handle (%d) in conn_info response", handle);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005581 goto unlock;
5582 }
5583
Johan Hedberg333ae952015-03-17 13:48:47 +02005584 cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005585 if (!cmd)
5586 goto unlock;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005587
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005588 cmd->cmd_complete(cmd, status);
5589 mgmt_pending_remove(cmd);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005590
5591unlock:
5592 hci_dev_unlock(hdev);
5593}
5594
5595static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
5596 u16 len)
5597{
5598 struct mgmt_cp_get_conn_info *cp = data;
5599 struct mgmt_rp_get_conn_info rp;
5600 struct hci_conn *conn;
5601 unsigned long conn_info_age;
5602 int err = 0;
5603
5604 BT_DBG("%s", hdev->name);
5605
5606 memset(&rp, 0, sizeof(rp));
5607 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5608 rp.addr.type = cp->addr.type;
5609
5610 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005611 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5612 MGMT_STATUS_INVALID_PARAMS,
5613 &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005614
5615 hci_dev_lock(hdev);
5616
5617 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005618 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5619 MGMT_STATUS_NOT_POWERED, &rp,
5620 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005621 goto unlock;
5622 }
5623
5624 if (cp->addr.type == BDADDR_BREDR)
5625 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5626 &cp->addr.bdaddr);
5627 else
5628 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
5629
5630 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005631 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5632 MGMT_STATUS_NOT_CONNECTED, &rp,
5633 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005634 goto unlock;
5635 }
5636
Johan Hedberg333ae952015-03-17 13:48:47 +02005637 if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005638 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5639 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005640 goto unlock;
5641 }
5642
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005643 /* To avoid client trying to guess when to poll again for information we
5644 * calculate conn info age as random value between min/max set in hdev.
5645 */
5646 conn_info_age = hdev->conn_info_min_age +
5647 prandom_u32_max(hdev->conn_info_max_age -
5648 hdev->conn_info_min_age);
5649
5650 /* Query controller to refresh cached values if they are too old or were
5651 * never read.
5652 */
Andrzej Kaczmarekf4e2dd52014-05-16 16:48:57 +02005653 if (time_after(jiffies, conn->conn_info_timestamp +
5654 msecs_to_jiffies(conn_info_age)) ||
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005655 !conn->conn_info_timestamp) {
5656 struct hci_request req;
5657 struct hci_cp_read_tx_power req_txp_cp;
5658 struct hci_cp_read_rssi req_rssi_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005659 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005660
5661 hci_req_init(&req, hdev);
5662 req_rssi_cp.handle = cpu_to_le16(conn->handle);
5663 hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp),
5664 &req_rssi_cp);
5665
Andrzej Kaczmarekf7faab02014-05-14 13:43:04 +02005666 /* For LE links TX power does not change thus we don't need to
5667 * query for it once value is known.
5668 */
5669 if (!bdaddr_type_is_le(cp->addr.type) ||
5670 conn->tx_power == HCI_TX_POWER_INVALID) {
5671 req_txp_cp.handle = cpu_to_le16(conn->handle);
5672 req_txp_cp.type = 0x00;
5673 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5674 sizeof(req_txp_cp), &req_txp_cp);
5675 }
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005676
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005677 /* Max TX power needs to be read only once per connection */
5678 if (conn->max_tx_power == HCI_TX_POWER_INVALID) {
5679 req_txp_cp.handle = cpu_to_le16(conn->handle);
5680 req_txp_cp.type = 0x01;
5681 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5682 sizeof(req_txp_cp), &req_txp_cp);
5683 }
5684
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005685 err = hci_req_run(&req, conn_info_refresh_complete);
5686 if (err < 0)
5687 goto unlock;
5688
5689 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev,
5690 data, len);
5691 if (!cmd) {
5692 err = -ENOMEM;
5693 goto unlock;
5694 }
5695
5696 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005697 cmd->user_data = hci_conn_get(conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005698 cmd->cmd_complete = conn_info_cmd_complete;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005699
5700 conn->conn_info_timestamp = jiffies;
5701 } else {
5702 /* Cache is valid, just reply with values cached in hci_conn */
5703 rp.rssi = conn->rssi;
5704 rp.tx_power = conn->tx_power;
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005705 rp.max_tx_power = conn->max_tx_power;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005706
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005707 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5708 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005709 }
5710
5711unlock:
5712 hci_dev_unlock(hdev);
5713 return err;
5714}
5715
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005716static int clock_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg69487372014-12-05 13:36:07 +02005717{
5718 struct hci_conn *conn = cmd->user_data;
5719 struct mgmt_rp_get_clock_info rp;
5720 struct hci_dev *hdev;
Johan Hedberg9df74652014-12-19 22:26:03 +02005721 int err;
Johan Hedberg69487372014-12-05 13:36:07 +02005722
5723 memset(&rp, 0, sizeof(rp));
5724 memcpy(&rp.addr, &cmd->param, sizeof(rp.addr));
5725
5726 if (status)
5727 goto complete;
5728
5729 hdev = hci_dev_get(cmd->index);
5730 if (hdev) {
5731 rp.local_clock = cpu_to_le32(hdev->clock);
5732 hci_dev_put(hdev);
5733 }
5734
5735 if (conn) {
5736 rp.piconet_clock = cpu_to_le32(conn->clock);
5737 rp.accuracy = cpu_to_le16(conn->clock_accuracy);
5738 }
5739
5740complete:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005741 err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp,
5742 sizeof(rp));
Johan Hedberg69487372014-12-05 13:36:07 +02005743
5744 if (conn) {
5745 hci_conn_drop(conn);
5746 hci_conn_put(conn);
5747 }
Johan Hedberg9df74652014-12-19 22:26:03 +02005748
5749 return err;
Johan Hedberg69487372014-12-05 13:36:07 +02005750}
5751
Marcel Holtmann1904a852015-01-11 13:50:44 -08005752static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg95868422014-06-28 17:54:07 +03005753{
Johan Hedberg95868422014-06-28 17:54:07 +03005754 struct hci_cp_read_clock *hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005755 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03005756 struct hci_conn *conn;
5757
5758 BT_DBG("%s status %u", hdev->name, status);
5759
5760 hci_dev_lock(hdev);
5761
5762 hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
5763 if (!hci_cp)
5764 goto unlock;
5765
5766 if (hci_cp->which) {
5767 u16 handle = __le16_to_cpu(hci_cp->handle);
5768 conn = hci_conn_hash_lookup_handle(hdev, handle);
5769 } else {
5770 conn = NULL;
5771 }
5772
Johan Hedberg333ae952015-03-17 13:48:47 +02005773 cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn);
Johan Hedberg95868422014-06-28 17:54:07 +03005774 if (!cmd)
5775 goto unlock;
5776
Johan Hedberg69487372014-12-05 13:36:07 +02005777 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberg95868422014-06-28 17:54:07 +03005778 mgmt_pending_remove(cmd);
Johan Hedberg95868422014-06-28 17:54:07 +03005779
5780unlock:
5781 hci_dev_unlock(hdev);
5782}
5783
5784static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
5785 u16 len)
5786{
5787 struct mgmt_cp_get_clock_info *cp = data;
5788 struct mgmt_rp_get_clock_info rp;
5789 struct hci_cp_read_clock hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005790 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03005791 struct hci_request req;
5792 struct hci_conn *conn;
5793 int err;
5794
5795 BT_DBG("%s", hdev->name);
5796
5797 memset(&rp, 0, sizeof(rp));
5798 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5799 rp.addr.type = cp->addr.type;
5800
5801 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005802 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5803 MGMT_STATUS_INVALID_PARAMS,
5804 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005805
5806 hci_dev_lock(hdev);
5807
5808 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005809 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5810 MGMT_STATUS_NOT_POWERED, &rp,
5811 sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005812 goto unlock;
5813 }
5814
5815 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
5816 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5817 &cp->addr.bdaddr);
5818 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005819 err = mgmt_cmd_complete(sk, hdev->id,
5820 MGMT_OP_GET_CLOCK_INFO,
5821 MGMT_STATUS_NOT_CONNECTED,
5822 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005823 goto unlock;
5824 }
5825 } else {
5826 conn = NULL;
5827 }
5828
5829 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len);
5830 if (!cmd) {
5831 err = -ENOMEM;
5832 goto unlock;
5833 }
5834
Johan Hedberg69487372014-12-05 13:36:07 +02005835 cmd->cmd_complete = clock_info_cmd_complete;
5836
Johan Hedberg95868422014-06-28 17:54:07 +03005837 hci_req_init(&req, hdev);
5838
5839 memset(&hci_cp, 0, sizeof(hci_cp));
5840 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5841
5842 if (conn) {
5843 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005844 cmd->user_data = hci_conn_get(conn);
Johan Hedberg95868422014-06-28 17:54:07 +03005845
5846 hci_cp.handle = cpu_to_le16(conn->handle);
5847 hci_cp.which = 0x01; /* Piconet clock */
5848 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5849 }
5850
5851 err = hci_req_run(&req, get_clock_info_complete);
5852 if (err < 0)
5853 mgmt_pending_remove(cmd);
5854
5855unlock:
5856 hci_dev_unlock(hdev);
5857 return err;
5858}
5859
Johan Hedberg5a154e62014-12-19 22:26:02 +02005860static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
5861{
5862 struct hci_conn *conn;
5863
5864 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
5865 if (!conn)
5866 return false;
5867
5868 if (conn->dst_type != type)
5869 return false;
5870
5871 if (conn->state != BT_CONNECTED)
5872 return false;
5873
5874 return true;
5875}
5876
5877/* This function requires the caller holds hdev->lock */
5878static int hci_conn_params_set(struct hci_request *req, bdaddr_t *addr,
5879 u8 addr_type, u8 auto_connect)
5880{
5881 struct hci_dev *hdev = req->hdev;
5882 struct hci_conn_params *params;
5883
5884 params = hci_conn_params_add(hdev, addr, addr_type);
5885 if (!params)
5886 return -EIO;
5887
5888 if (params->auto_connect == auto_connect)
5889 return 0;
5890
5891 list_del_init(&params->action);
5892
5893 switch (auto_connect) {
5894 case HCI_AUTO_CONN_DISABLED:
5895 case HCI_AUTO_CONN_LINK_LOSS:
5896 __hci_update_background_scan(req);
5897 break;
5898 case HCI_AUTO_CONN_REPORT:
5899 list_add(&params->action, &hdev->pend_le_reports);
5900 __hci_update_background_scan(req);
5901 break;
5902 case HCI_AUTO_CONN_DIRECT:
5903 case HCI_AUTO_CONN_ALWAYS:
5904 if (!is_connected(hdev, addr, addr_type)) {
5905 list_add(&params->action, &hdev->pend_le_conns);
5906 __hci_update_background_scan(req);
5907 }
5908 break;
5909 }
5910
5911 params->auto_connect = auto_connect;
5912
5913 BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
5914 auto_connect);
5915
5916 return 0;
5917}
5918
Marcel Holtmann8afef092014-06-29 22:28:34 +02005919static void device_added(struct sock *sk, struct hci_dev *hdev,
5920 bdaddr_t *bdaddr, u8 type, u8 action)
5921{
5922 struct mgmt_ev_device_added ev;
5923
5924 bacpy(&ev.addr.bdaddr, bdaddr);
5925 ev.addr.type = type;
5926 ev.action = action;
5927
5928 mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk);
5929}
5930
Marcel Holtmann1904a852015-01-11 13:50:44 -08005931static void add_device_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg5a154e62014-12-19 22:26:02 +02005932{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005933 struct mgmt_pending_cmd *cmd;
Johan Hedberg5a154e62014-12-19 22:26:02 +02005934
5935 BT_DBG("status 0x%02x", status);
5936
5937 hci_dev_lock(hdev);
5938
Johan Hedberg333ae952015-03-17 13:48:47 +02005939 cmd = pending_find(MGMT_OP_ADD_DEVICE, hdev);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005940 if (!cmd)
5941 goto unlock;
5942
5943 cmd->cmd_complete(cmd, mgmt_status(status));
5944 mgmt_pending_remove(cmd);
5945
5946unlock:
5947 hci_dev_unlock(hdev);
5948}
5949
Marcel Holtmann2faade52014-06-29 19:44:03 +02005950static int add_device(struct sock *sk, struct hci_dev *hdev,
5951 void *data, u16 len)
5952{
5953 struct mgmt_cp_add_device *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005954 struct mgmt_pending_cmd *cmd;
Johan Hedberg5a154e62014-12-19 22:26:02 +02005955 struct hci_request req;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005956 u8 auto_conn, addr_type;
5957 int err;
5958
5959 BT_DBG("%s", hdev->name);
5960
Johan Hedberg66593582014-07-09 12:59:14 +03005961 if (!bdaddr_type_is_valid(cp->addr.type) ||
Marcel Holtmann2faade52014-06-29 19:44:03 +02005962 !bacmp(&cp->addr.bdaddr, BDADDR_ANY))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005963 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5964 MGMT_STATUS_INVALID_PARAMS,
5965 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005966
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005967 if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005968 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5969 MGMT_STATUS_INVALID_PARAMS,
5970 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005971
Johan Hedberg5a154e62014-12-19 22:26:02 +02005972 hci_req_init(&req, hdev);
5973
Marcel Holtmann2faade52014-06-29 19:44:03 +02005974 hci_dev_lock(hdev);
5975
Johan Hedberg5a154e62014-12-19 22:26:02 +02005976 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_DEVICE, hdev, data, len);
5977 if (!cmd) {
5978 err = -ENOMEM;
5979 goto unlock;
5980 }
5981
5982 cmd->cmd_complete = addr_cmd_complete;
5983
Johan Hedberg66593582014-07-09 12:59:14 +03005984 if (cp->addr.type == BDADDR_BREDR) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005985 /* Only incoming connections action is supported for now */
Johan Hedberg66593582014-07-09 12:59:14 +03005986 if (cp->action != 0x01) {
Johan Hedberg9df74652014-12-19 22:26:03 +02005987 err = cmd->cmd_complete(cmd,
5988 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005989 mgmt_pending_remove(cmd);
Johan Hedberg66593582014-07-09 12:59:14 +03005990 goto unlock;
5991 }
5992
5993 err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr,
5994 cp->addr.type);
5995 if (err)
5996 goto unlock;
Johan Hedberga3974072014-07-09 12:59:15 +03005997
Johan Hedberg5a154e62014-12-19 22:26:02 +02005998 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03005999
Johan Hedberg66593582014-07-09 12:59:14 +03006000 goto added;
6001 }
6002
Marcel Holtmann2faade52014-06-29 19:44:03 +02006003 if (cp->addr.type == BDADDR_LE_PUBLIC)
6004 addr_type = ADDR_LE_DEV_PUBLIC;
6005 else
6006 addr_type = ADDR_LE_DEV_RANDOM;
6007
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006008 if (cp->action == 0x02)
Marcel Holtmann2faade52014-06-29 19:44:03 +02006009 auto_conn = HCI_AUTO_CONN_ALWAYS;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006010 else if (cp->action == 0x01)
6011 auto_conn = HCI_AUTO_CONN_DIRECT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006012 else
Johan Hedberga3451d22014-07-02 17:37:27 +03006013 auto_conn = HCI_AUTO_CONN_REPORT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006014
Marcel Holtmannbf5b3c82014-06-30 12:34:39 +02006015 /* If the connection parameters don't exist for this device,
6016 * they will be created and configured with defaults.
6017 */
Johan Hedberg5a154e62014-12-19 22:26:02 +02006018 if (hci_conn_params_set(&req, &cp->addr.bdaddr, addr_type,
Marcel Holtmannd06b50c2014-07-01 12:11:06 +02006019 auto_conn) < 0) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006020 err = cmd->cmd_complete(cmd, MGMT_STATUS_FAILED);
Johan Hedberg5a154e62014-12-19 22:26:02 +02006021 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006022 goto unlock;
6023 }
6024
Johan Hedberg66593582014-07-09 12:59:14 +03006025added:
Marcel Holtmann8afef092014-06-29 22:28:34 +02006026 device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
6027
Johan Hedberg5a154e62014-12-19 22:26:02 +02006028 err = hci_req_run(&req, add_device_complete);
6029 if (err < 0) {
6030 /* ENODATA means no HCI commands were needed (e.g. if
6031 * the adapter is powered off).
6032 */
Johan Hedberg9df74652014-12-19 22:26:03 +02006033 if (err == -ENODATA)
6034 err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS);
Johan Hedberg5a154e62014-12-19 22:26:02 +02006035 mgmt_pending_remove(cmd);
6036 }
Marcel Holtmann2faade52014-06-29 19:44:03 +02006037
6038unlock:
6039 hci_dev_unlock(hdev);
6040 return err;
6041}
6042
Marcel Holtmann8afef092014-06-29 22:28:34 +02006043static void device_removed(struct sock *sk, struct hci_dev *hdev,
6044 bdaddr_t *bdaddr, u8 type)
6045{
6046 struct mgmt_ev_device_removed ev;
6047
6048 bacpy(&ev.addr.bdaddr, bdaddr);
6049 ev.addr.type = type;
6050
6051 mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk);
6052}
6053
Marcel Holtmann1904a852015-01-11 13:50:44 -08006054static void remove_device_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006055{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006056 struct mgmt_pending_cmd *cmd;
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006057
6058 BT_DBG("status 0x%02x", status);
6059
6060 hci_dev_lock(hdev);
6061
Johan Hedberg333ae952015-03-17 13:48:47 +02006062 cmd = pending_find(MGMT_OP_REMOVE_DEVICE, hdev);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006063 if (!cmd)
6064 goto unlock;
6065
6066 cmd->cmd_complete(cmd, mgmt_status(status));
6067 mgmt_pending_remove(cmd);
6068
6069unlock:
6070 hci_dev_unlock(hdev);
6071}
6072
Marcel Holtmann2faade52014-06-29 19:44:03 +02006073static int remove_device(struct sock *sk, struct hci_dev *hdev,
6074 void *data, u16 len)
6075{
6076 struct mgmt_cp_remove_device *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006077 struct mgmt_pending_cmd *cmd;
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006078 struct hci_request req;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006079 int err;
6080
6081 BT_DBG("%s", hdev->name);
6082
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006083 hci_req_init(&req, hdev);
6084
Marcel Holtmann2faade52014-06-29 19:44:03 +02006085 hci_dev_lock(hdev);
6086
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006087 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_DEVICE, hdev, data, len);
6088 if (!cmd) {
6089 err = -ENOMEM;
6090 goto unlock;
6091 }
6092
6093 cmd->cmd_complete = addr_cmd_complete;
6094
Marcel Holtmann2faade52014-06-29 19:44:03 +02006095 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
Johan Hedbergc71593d2014-07-02 17:37:28 +03006096 struct hci_conn_params *params;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006097 u8 addr_type;
6098
Johan Hedberg66593582014-07-09 12:59:14 +03006099 if (!bdaddr_type_is_valid(cp->addr.type)) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006100 err = cmd->cmd_complete(cmd,
6101 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006102 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006103 goto unlock;
6104 }
6105
Johan Hedberg66593582014-07-09 12:59:14 +03006106 if (cp->addr.type == BDADDR_BREDR) {
6107 err = hci_bdaddr_list_del(&hdev->whitelist,
6108 &cp->addr.bdaddr,
6109 cp->addr.type);
6110 if (err) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006111 err = cmd->cmd_complete(cmd,
6112 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006113 mgmt_pending_remove(cmd);
Johan Hedberg66593582014-07-09 12:59:14 +03006114 goto unlock;
6115 }
6116
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006117 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03006118
Johan Hedberg66593582014-07-09 12:59:14 +03006119 device_removed(sk, hdev, &cp->addr.bdaddr,
6120 cp->addr.type);
6121 goto complete;
6122 }
6123
Marcel Holtmann2faade52014-06-29 19:44:03 +02006124 if (cp->addr.type == BDADDR_LE_PUBLIC)
6125 addr_type = ADDR_LE_DEV_PUBLIC;
6126 else
6127 addr_type = ADDR_LE_DEV_RANDOM;
6128
Johan Hedbergc71593d2014-07-02 17:37:28 +03006129 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
6130 addr_type);
6131 if (!params) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006132 err = cmd->cmd_complete(cmd,
6133 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006134 mgmt_pending_remove(cmd);
Johan Hedbergc71593d2014-07-02 17:37:28 +03006135 goto unlock;
6136 }
6137
6138 if (params->auto_connect == HCI_AUTO_CONN_DISABLED) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006139 err = cmd->cmd_complete(cmd,
6140 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006141 mgmt_pending_remove(cmd);
Johan Hedbergc71593d2014-07-02 17:37:28 +03006142 goto unlock;
6143 }
6144
Johan Hedbergd1dbf122014-07-04 16:17:23 +03006145 list_del(&params->action);
Johan Hedbergc71593d2014-07-02 17:37:28 +03006146 list_del(&params->list);
6147 kfree(params);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006148 __hci_update_background_scan(&req);
Marcel Holtmann8afef092014-06-29 22:28:34 +02006149
6150 device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006151 } else {
Johan Hedberg19de0822014-07-06 13:06:51 +03006152 struct hci_conn_params *p, *tmp;
Johan Hedberg66593582014-07-09 12:59:14 +03006153 struct bdaddr_list *b, *btmp;
Johan Hedberg19de0822014-07-06 13:06:51 +03006154
Marcel Holtmann2faade52014-06-29 19:44:03 +02006155 if (cp->addr.type) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006156 err = cmd->cmd_complete(cmd,
6157 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006158 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006159 goto unlock;
6160 }
6161
Johan Hedberg66593582014-07-09 12:59:14 +03006162 list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) {
6163 device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type);
6164 list_del(&b->list);
6165 kfree(b);
6166 }
6167
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006168 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03006169
Johan Hedberg19de0822014-07-06 13:06:51 +03006170 list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) {
6171 if (p->auto_connect == HCI_AUTO_CONN_DISABLED)
6172 continue;
6173 device_removed(sk, hdev, &p->addr, p->addr_type);
6174 list_del(&p->action);
6175 list_del(&p->list);
6176 kfree(p);
6177 }
6178
6179 BT_DBG("All LE connection parameters were removed");
6180
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006181 __hci_update_background_scan(&req);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006182 }
6183
Johan Hedberg66593582014-07-09 12:59:14 +03006184complete:
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006185 err = hci_req_run(&req, remove_device_complete);
6186 if (err < 0) {
6187 /* ENODATA means no HCI commands were needed (e.g. if
6188 * the adapter is powered off).
6189 */
Johan Hedberg9df74652014-12-19 22:26:03 +02006190 if (err == -ENODATA)
6191 err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006192 mgmt_pending_remove(cmd);
6193 }
Marcel Holtmann2faade52014-06-29 19:44:03 +02006194
6195unlock:
6196 hci_dev_unlock(hdev);
6197 return err;
6198}
6199
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006200static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
6201 u16 len)
6202{
6203 struct mgmt_cp_load_conn_param *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03006204 const u16 max_param_count = ((U16_MAX - sizeof(*cp)) /
6205 sizeof(struct mgmt_conn_param));
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006206 u16 param_count, expected_len;
6207 int i;
6208
6209 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006210 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6211 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006212
6213 param_count = __le16_to_cpu(cp->param_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03006214 if (param_count > max_param_count) {
6215 BT_ERR("load_conn_param: too big param_count value %u",
6216 param_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02006217 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6218 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03006219 }
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006220
6221 expected_len = sizeof(*cp) + param_count *
6222 sizeof(struct mgmt_conn_param);
6223 if (expected_len != len) {
6224 BT_ERR("load_conn_param: expected %u bytes, got %u bytes",
6225 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02006226 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6227 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006228 }
6229
6230 BT_DBG("%s param_count %u", hdev->name, param_count);
6231
6232 hci_dev_lock(hdev);
6233
6234 hci_conn_params_clear_disabled(hdev);
6235
6236 for (i = 0; i < param_count; i++) {
6237 struct mgmt_conn_param *param = &cp->params[i];
6238 struct hci_conn_params *hci_param;
6239 u16 min, max, latency, timeout;
6240 u8 addr_type;
6241
6242 BT_DBG("Adding %pMR (type %u)", &param->addr.bdaddr,
6243 param->addr.type);
6244
6245 if (param->addr.type == BDADDR_LE_PUBLIC) {
6246 addr_type = ADDR_LE_DEV_PUBLIC;
6247 } else if (param->addr.type == BDADDR_LE_RANDOM) {
6248 addr_type = ADDR_LE_DEV_RANDOM;
6249 } else {
6250 BT_ERR("Ignoring invalid connection parameters");
6251 continue;
6252 }
6253
6254 min = le16_to_cpu(param->min_interval);
6255 max = le16_to_cpu(param->max_interval);
6256 latency = le16_to_cpu(param->latency);
6257 timeout = le16_to_cpu(param->timeout);
6258
6259 BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x",
6260 min, max, latency, timeout);
6261
6262 if (hci_check_conn_params(min, max, latency, timeout) < 0) {
6263 BT_ERR("Ignoring invalid connection parameters");
6264 continue;
6265 }
6266
6267 hci_param = hci_conn_params_add(hdev, &param->addr.bdaddr,
6268 addr_type);
6269 if (!hci_param) {
6270 BT_ERR("Failed to add connection parameters");
6271 continue;
6272 }
6273
6274 hci_param->conn_min_interval = min;
6275 hci_param->conn_max_interval = max;
6276 hci_param->conn_latency = latency;
6277 hci_param->supervision_timeout = timeout;
6278 }
6279
6280 hci_dev_unlock(hdev);
6281
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006282 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0,
6283 NULL, 0);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006284}
6285
Marcel Holtmanndbece372014-07-04 18:11:55 +02006286static int set_external_config(struct sock *sk, struct hci_dev *hdev,
6287 void *data, u16 len)
6288{
6289 struct mgmt_cp_set_external_config *cp = data;
6290 bool changed;
6291 int err;
6292
6293 BT_DBG("%s", hdev->name);
6294
6295 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006296 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6297 MGMT_STATUS_REJECTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006298
6299 if (cp->config != 0x00 && cp->config != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02006300 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6301 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006302
6303 if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
Johan Hedberga69e8372015-03-06 21:08:53 +02006304 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6305 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006306
6307 hci_dev_lock(hdev);
6308
6309 if (cp->config)
Marcel Holtmann238be782015-03-13 02:11:06 -07006310 changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006311 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07006312 changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006313
6314 err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
6315 if (err < 0)
6316 goto unlock;
6317
6318 if (!changed)
6319 goto unlock;
6320
Marcel Holtmannf4537c02014-07-04 19:06:23 +02006321 err = new_options(hdev, sk);
6322
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006323 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) {
Marcel Holtmanndbece372014-07-04 18:11:55 +02006324 mgmt_index_removed(hdev);
Marcel Holtmannd603b762014-07-06 12:11:14 +02006325
Marcel Holtmann516018a2015-03-13 02:11:04 -07006326 if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006327 hci_dev_set_flag(hdev, HCI_CONFIG);
6328 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmannd603b762014-07-06 12:11:14 +02006329
6330 queue_work(hdev->req_workqueue, &hdev->power_on);
6331 } else {
Marcel Holtmann5ea234d2014-07-06 12:11:16 +02006332 set_bit(HCI_RAW, &hdev->flags);
Marcel Holtmannd603b762014-07-06 12:11:14 +02006333 mgmt_index_added(hdev);
6334 }
Marcel Holtmanndbece372014-07-04 18:11:55 +02006335 }
6336
6337unlock:
6338 hci_dev_unlock(hdev);
6339 return err;
6340}
6341
Marcel Holtmann9713c172014-07-06 12:11:15 +02006342static int set_public_address(struct sock *sk, struct hci_dev *hdev,
6343 void *data, u16 len)
6344{
6345 struct mgmt_cp_set_public_address *cp = data;
6346 bool changed;
6347 int err;
6348
6349 BT_DBG("%s", hdev->name);
6350
6351 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006352 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6353 MGMT_STATUS_REJECTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006354
6355 if (!bacmp(&cp->bdaddr, BDADDR_ANY))
Johan Hedberga69e8372015-03-06 21:08:53 +02006356 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6357 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006358
6359 if (!hdev->set_bdaddr)
Johan Hedberga69e8372015-03-06 21:08:53 +02006360 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6361 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006362
6363 hci_dev_lock(hdev);
6364
6365 changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
6366 bacpy(&hdev->public_addr, &cp->bdaddr);
6367
6368 err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev);
6369 if (err < 0)
6370 goto unlock;
6371
6372 if (!changed)
6373 goto unlock;
6374
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006375 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
Marcel Holtmann9713c172014-07-06 12:11:15 +02006376 err = new_options(hdev, sk);
6377
6378 if (is_configured(hdev)) {
6379 mgmt_index_removed(hdev);
6380
Marcel Holtmanna358dc12015-03-13 02:11:02 -07006381 hci_dev_clear_flag(hdev, HCI_UNCONFIGURED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006382
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006383 hci_dev_set_flag(hdev, HCI_CONFIG);
6384 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006385
6386 queue_work(hdev->req_workqueue, &hdev->power_on);
6387 }
6388
6389unlock:
6390 hci_dev_unlock(hdev);
6391 return err;
6392}
6393
Marcel Holtmannbea41602015-03-14 22:43:17 -07006394static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
6395 u8 data_len)
6396{
6397 eir[eir_len++] = sizeof(type) + data_len;
6398 eir[eir_len++] = type;
6399 memcpy(&eir[eir_len], data, data_len);
6400 eir_len += data_len;
6401
6402 return eir_len;
6403}
6404
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006405static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
6406 void *data, u16 data_len)
6407{
6408 struct mgmt_cp_read_local_oob_ext_data *cp = data;
6409 struct mgmt_rp_read_local_oob_ext_data *rp;
6410 size_t rp_len;
6411 u16 eir_len;
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006412 u8 status, flags, role, addr[7], hash[16], rand[16];
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006413 int err;
6414
6415 BT_DBG("%s", hdev->name);
6416
6417 if (!hdev_is_powered(hdev))
6418 return mgmt_cmd_complete(sk, hdev->id,
6419 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
6420 MGMT_STATUS_NOT_POWERED,
6421 &cp->type, sizeof(cp->type));
6422
6423 switch (cp->type) {
6424 case BIT(BDADDR_BREDR):
6425 status = mgmt_bredr_support(hdev);
6426 if (status)
6427 return mgmt_cmd_complete(sk, hdev->id,
6428 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
6429 status, &cp->type,
6430 sizeof(cp->type));
6431 eir_len = 5;
6432 break;
6433 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
6434 status = mgmt_le_support(hdev);
6435 if (status)
6436 return mgmt_cmd_complete(sk, hdev->id,
6437 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
6438 status, &cp->type,
6439 sizeof(cp->type));
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006440 eir_len = 9 + 3 + 18 + 18 + 3;
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006441 break;
6442 default:
6443 return mgmt_cmd_complete(sk, hdev->id,
6444 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
6445 MGMT_STATUS_INVALID_PARAMS,
6446 &cp->type, sizeof(cp->type));
6447 }
6448
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006449 rp_len = sizeof(*rp) + eir_len;
6450 rp = kmalloc(rp_len, GFP_ATOMIC);
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07006451 if (!rp)
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006452 return -ENOMEM;
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07006453
6454 hci_dev_lock(hdev);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006455
6456 eir_len = 0;
6457 switch (cp->type) {
6458 case BIT(BDADDR_BREDR):
6459 eir_len = eir_append_data(rp->eir, eir_len, EIR_CLASS_OF_DEV,
6460 hdev->dev_class, 3);
6461 break;
6462 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
Marcel Holtmann5082a592015-03-16 12:39:00 -07006463 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
6464 smp_generate_oob(hdev, hash, rand) < 0) {
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006465 hci_dev_unlock(hdev);
6466 err = mgmt_cmd_complete(sk, hdev->id,
Marcel Holtmann5082a592015-03-16 12:39:00 -07006467 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
6468 MGMT_STATUS_FAILED,
6469 &cp->type, sizeof(cp->type));
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006470 goto done;
6471 }
6472
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006473 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
6474 memcpy(addr, &hdev->rpa, 6);
6475 addr[6] = 0x01;
6476 } else if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
6477 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
6478 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
6479 bacmp(&hdev->static_addr, BDADDR_ANY))) {
6480 memcpy(addr, &hdev->static_addr, 6);
6481 addr[6] = 0x01;
6482 } else {
6483 memcpy(addr, &hdev->bdaddr, 6);
6484 addr[6] = 0x00;
6485 }
6486
6487 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR,
6488 addr, sizeof(addr));
6489
6490 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
6491 role = 0x02;
6492 else
6493 role = 0x01;
6494
6495 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE,
6496 &role, sizeof(role));
6497
Marcel Holtmann5082a592015-03-16 12:39:00 -07006498 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) {
6499 eir_len = eir_append_data(rp->eir, eir_len,
6500 EIR_LE_SC_CONFIRM,
6501 hash, sizeof(hash));
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006502
Marcel Holtmann5082a592015-03-16 12:39:00 -07006503 eir_len = eir_append_data(rp->eir, eir_len,
6504 EIR_LE_SC_RANDOM,
6505 rand, sizeof(rand));
6506 }
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006507
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006508 flags = get_adv_discov_flags(hdev);
6509
6510 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
6511 flags |= LE_AD_NO_BREDR;
6512
6513 eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS,
6514 &flags, sizeof(flags));
6515 break;
6516 }
6517
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006518 hci_dev_unlock(hdev);
6519
Marcel Holtmann72000df2015-03-16 16:11:21 -07006520 hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS);
6521
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07006522 rp->type = cp->type;
6523 rp->eir_len = cpu_to_le16(eir_len);
6524
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006525 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
Marcel Holtmann5425f982015-03-16 16:05:44 -07006526 MGMT_STATUS_SUCCESS, rp, sizeof(*rp) + eir_len);
Marcel Holtmann72000df2015-03-16 16:11:21 -07006527 if (err < 0)
6528 goto done;
6529
6530 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
6531 rp, sizeof(*rp) + eir_len,
6532 HCI_MGMT_OOB_DATA_EVENTS, sk);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006533
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006534done:
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006535 kfree(rp);
6536
6537 return err;
6538}
6539
Arman Uguray089fa8c2015-03-25 18:53:45 -07006540static u32 get_supported_adv_flags(struct hci_dev *hdev)
6541{
6542 u32 flags = 0;
6543
6544 flags |= MGMT_ADV_FLAG_CONNECTABLE;
6545 flags |= MGMT_ADV_FLAG_DISCOV;
6546 flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
6547 flags |= MGMT_ADV_FLAG_MANAGED_FLAGS;
6548
6549 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID)
6550 flags |= MGMT_ADV_FLAG_TX_POWER;
6551
6552 return flags;
6553}
6554
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006555static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
6556 void *data, u16 data_len)
6557{
6558 struct mgmt_rp_read_adv_features *rp;
6559 size_t rp_len;
6560 int err;
Arman Uguray24b4f382015-03-23 15:57:12 -07006561 bool instance;
Arman Uguray089fa8c2015-03-25 18:53:45 -07006562 u32 supported_flags;
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006563
6564 BT_DBG("%s", hdev->name);
6565
Arman Uguray089fa8c2015-03-25 18:53:45 -07006566 if (!lmp_le_capable(hdev))
6567 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
6568 MGMT_STATUS_REJECTED);
6569
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006570 hci_dev_lock(hdev);
6571
6572 rp_len = sizeof(*rp);
Arman Uguray24b4f382015-03-23 15:57:12 -07006573
6574 /* Currently only one instance is supported, so just add 1 to the
6575 * response length.
6576 */
6577 instance = hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE);
6578 if (instance)
6579 rp_len++;
6580
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006581 rp = kmalloc(rp_len, GFP_ATOMIC);
6582 if (!rp) {
6583 hci_dev_unlock(hdev);
6584 return -ENOMEM;
6585 }
6586
Arman Uguray089fa8c2015-03-25 18:53:45 -07006587 supported_flags = get_supported_adv_flags(hdev);
6588
6589 rp->supported_flags = cpu_to_le32(supported_flags);
Marcel Holtmanndc5d82a2015-03-19 17:22:25 -07006590 rp->max_adv_data_len = HCI_MAX_AD_LENGTH;
6591 rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH;
Arman Uguray24b4f382015-03-23 15:57:12 -07006592 rp->max_instances = 1;
6593
6594 /* Currently only one instance is supported, so simply return the
6595 * current instance number.
6596 */
6597 if (instance) {
6598 rp->num_instances = 1;
6599 rp->instance[0] = 1;
6600 } else {
6601 rp->num_instances = 0;
6602 }
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006603
6604 hci_dev_unlock(hdev);
6605
6606 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
6607 MGMT_STATUS_SUCCESS, rp, rp_len);
6608
6609 kfree(rp);
6610
6611 return err;
6612}
6613
Arman Uguray4117ed72015-03-23 15:57:14 -07006614static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data,
Arman Ugurayb44133f2015-03-25 18:53:41 -07006615 u8 len, bool is_adv_data)
Arman Uguray24b4f382015-03-23 15:57:12 -07006616{
Arman Uguray4117ed72015-03-23 15:57:14 -07006617 u8 max_len = HCI_MAX_AD_LENGTH;
Arman Uguray24b4f382015-03-23 15:57:12 -07006618 int i, cur_len;
Arman Ugurayb44133f2015-03-25 18:53:41 -07006619 bool flags_managed = false;
Arman Uguray5507e352015-03-25 18:53:44 -07006620 bool tx_power_managed = false;
Arman Uguray67e0c0c2015-03-25 18:53:43 -07006621 u32 flags_params = MGMT_ADV_FLAG_DISCOV | MGMT_ADV_FLAG_LIMITED_DISCOV |
6622 MGMT_ADV_FLAG_MANAGED_FLAGS;
Arman Uguray24b4f382015-03-23 15:57:12 -07006623
Arman Uguray807ec772015-03-25 18:53:42 -07006624 if (is_adv_data && (adv_flags & flags_params)) {
Arman Ugurayb44133f2015-03-25 18:53:41 -07006625 flags_managed = true;
6626 max_len -= 3;
6627 }
Arman Uguray24b4f382015-03-23 15:57:12 -07006628
Arman Uguray5507e352015-03-25 18:53:44 -07006629 if (is_adv_data && (adv_flags & MGMT_ADV_FLAG_TX_POWER)) {
6630 tx_power_managed = true;
6631 max_len -= 3;
6632 }
6633
Arman Uguray4117ed72015-03-23 15:57:14 -07006634 if (len > max_len)
Arman Uguray24b4f382015-03-23 15:57:12 -07006635 return false;
6636
Arman Uguray4117ed72015-03-23 15:57:14 -07006637 /* Make sure that the data is correctly formatted. */
6638 for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) {
6639 cur_len = data[i];
Arman Uguray24b4f382015-03-23 15:57:12 -07006640
Arman Ugurayb44133f2015-03-25 18:53:41 -07006641 if (flags_managed && data[i + 1] == EIR_FLAGS)
6642 return false;
6643
Arman Uguray5507e352015-03-25 18:53:44 -07006644 if (tx_power_managed && data[i + 1] == EIR_TX_POWER)
6645 return false;
6646
Arman Uguray24b4f382015-03-23 15:57:12 -07006647 /* If the current field length would exceed the total data
6648 * length, then it's invalid.
6649 */
Arman Uguray4117ed72015-03-23 15:57:14 -07006650 if (i + cur_len >= len)
Arman Uguray24b4f382015-03-23 15:57:12 -07006651 return false;
6652 }
6653
6654 return true;
6655}
6656
Arman Uguray24b4f382015-03-23 15:57:12 -07006657static void add_advertising_complete(struct hci_dev *hdev, u8 status,
6658 u16 opcode)
6659{
6660 struct mgmt_pending_cmd *cmd;
6661 struct mgmt_rp_add_advertising rp;
6662
6663 BT_DBG("status %d", status);
6664
6665 hci_dev_lock(hdev);
6666
6667 cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev);
6668
6669 if (status) {
6670 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
6671 memset(&hdev->adv_instance, 0, sizeof(hdev->adv_instance));
6672 advertising_removed(cmd ? cmd->sk : NULL, hdev, 1);
6673 }
6674
6675 if (!cmd)
6676 goto unlock;
6677
6678 rp.instance = 0x01;
6679
6680 if (status)
6681 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
6682 mgmt_status(status));
6683 else
6684 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
6685 mgmt_status(status), &rp, sizeof(rp));
6686
6687 mgmt_pending_remove(cmd);
6688
6689unlock:
6690 hci_dev_unlock(hdev);
6691}
6692
Arman Uguray912098a2015-03-23 15:57:15 -07006693static void adv_timeout_expired(struct work_struct *work)
6694{
6695 struct hci_dev *hdev = container_of(work, struct hci_dev,
6696 adv_instance.timeout_exp.work);
6697
6698 hdev->adv_instance.timeout = 0;
6699
6700 hci_dev_lock(hdev);
6701 clear_adv_instance(hdev);
6702 hci_dev_unlock(hdev);
6703}
6704
Arman Uguray24b4f382015-03-23 15:57:12 -07006705static int add_advertising(struct sock *sk, struct hci_dev *hdev,
6706 void *data, u16 data_len)
6707{
6708 struct mgmt_cp_add_advertising *cp = data;
6709 struct mgmt_rp_add_advertising rp;
6710 u32 flags;
Arman Uguray089fa8c2015-03-25 18:53:45 -07006711 u32 supported_flags;
Arman Uguray24b4f382015-03-23 15:57:12 -07006712 u8 status;
Arman Uguray912098a2015-03-23 15:57:15 -07006713 u16 timeout;
Arman Uguray24b4f382015-03-23 15:57:12 -07006714 int err;
6715 struct mgmt_pending_cmd *cmd;
6716 struct hci_request req;
6717
6718 BT_DBG("%s", hdev->name);
6719
6720 status = mgmt_le_support(hdev);
6721 if (status)
6722 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6723 status);
6724
6725 flags = __le32_to_cpu(cp->flags);
Arman Uguray912098a2015-03-23 15:57:15 -07006726 timeout = __le16_to_cpu(cp->timeout);
Arman Uguray24b4f382015-03-23 15:57:12 -07006727
Arman Uguray089fa8c2015-03-25 18:53:45 -07006728 /* The current implementation only supports adding one instance and only
6729 * a subset of the specified flags.
6730 */
6731 supported_flags = get_supported_adv_flags(hdev);
6732 if (cp->instance != 0x01 || (flags & ~supported_flags))
Arman Uguray24b4f382015-03-23 15:57:12 -07006733 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6734 MGMT_STATUS_INVALID_PARAMS);
6735
6736 hci_dev_lock(hdev);
6737
Arman Uguray912098a2015-03-23 15:57:15 -07006738 if (timeout && !hdev_is_powered(hdev)) {
6739 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6740 MGMT_STATUS_REJECTED);
6741 goto unlock;
6742 }
6743
Arman Uguray24b4f382015-03-23 15:57:12 -07006744 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
Arman Ugurayda9293352015-03-23 15:57:13 -07006745 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
Arman Uguray24b4f382015-03-23 15:57:12 -07006746 pending_find(MGMT_OP_SET_LE, hdev)) {
6747 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6748 MGMT_STATUS_BUSY);
6749 goto unlock;
6750 }
6751
Arman Ugurayb44133f2015-03-25 18:53:41 -07006752 if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) ||
Arman Uguray4117ed72015-03-23 15:57:14 -07006753 !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len,
Arman Ugurayb44133f2015-03-25 18:53:41 -07006754 cp->scan_rsp_len, false)) {
Arman Uguray24b4f382015-03-23 15:57:12 -07006755 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6756 MGMT_STATUS_INVALID_PARAMS);
6757 goto unlock;
6758 }
6759
Arman Uguray912098a2015-03-23 15:57:15 -07006760 INIT_DELAYED_WORK(&hdev->adv_instance.timeout_exp, adv_timeout_expired);
6761
Arman Uguray24b4f382015-03-23 15:57:12 -07006762 hdev->adv_instance.flags = flags;
6763 hdev->adv_instance.adv_data_len = cp->adv_data_len;
6764 hdev->adv_instance.scan_rsp_len = cp->scan_rsp_len;
6765
6766 if (cp->adv_data_len)
6767 memcpy(hdev->adv_instance.adv_data, cp->data, cp->adv_data_len);
6768
6769 if (cp->scan_rsp_len)
6770 memcpy(hdev->adv_instance.scan_rsp_data,
6771 cp->data + cp->adv_data_len, cp->scan_rsp_len);
6772
Arman Uguray912098a2015-03-23 15:57:15 -07006773 if (hdev->adv_instance.timeout)
6774 cancel_delayed_work(&hdev->adv_instance.timeout_exp);
6775
6776 hdev->adv_instance.timeout = timeout;
6777
6778 if (timeout)
6779 queue_delayed_work(hdev->workqueue,
6780 &hdev->adv_instance.timeout_exp,
6781 msecs_to_jiffies(timeout * 1000));
6782
Arman Uguray24b4f382015-03-23 15:57:12 -07006783 if (!hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING_INSTANCE))
6784 advertising_added(sk, hdev, 1);
6785
6786 /* If the HCI_ADVERTISING flag is set or the device isn't powered then
6787 * we have no HCI communication to make. Simply return.
6788 */
6789 if (!hdev_is_powered(hdev) ||
6790 hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
6791 rp.instance = 0x01;
6792 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6793 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
6794 goto unlock;
6795 }
6796
6797 /* We're good to go, update advertising data, parameters, and start
6798 * advertising.
6799 */
6800 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data,
6801 data_len);
6802 if (!cmd) {
6803 err = -ENOMEM;
6804 goto unlock;
6805 }
6806
6807 hci_req_init(&req, hdev);
6808
6809 update_adv_data(&req);
Arman Uguray4117ed72015-03-23 15:57:14 -07006810 update_scan_rsp_data(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07006811 enable_advertising(&req);
6812
6813 err = hci_req_run(&req, add_advertising_complete);
6814 if (err < 0)
6815 mgmt_pending_remove(cmd);
6816
6817unlock:
6818 hci_dev_unlock(hdev);
6819
6820 return err;
6821}
6822
Arman Ugurayda9293352015-03-23 15:57:13 -07006823static void remove_advertising_complete(struct hci_dev *hdev, u8 status,
6824 u16 opcode)
6825{
6826 struct mgmt_pending_cmd *cmd;
6827 struct mgmt_rp_remove_advertising rp;
6828
6829 BT_DBG("status %d", status);
6830
6831 hci_dev_lock(hdev);
6832
6833 /* A failure status here only means that we failed to disable
6834 * advertising. Otherwise, the advertising instance has been removed,
6835 * so report success.
6836 */
6837 cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev);
6838 if (!cmd)
6839 goto unlock;
6840
6841 rp.instance = 1;
6842
6843 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS,
6844 &rp, sizeof(rp));
6845 mgmt_pending_remove(cmd);
6846
6847unlock:
6848 hci_dev_unlock(hdev);
6849}
6850
6851static int remove_advertising(struct sock *sk, struct hci_dev *hdev,
6852 void *data, u16 data_len)
6853{
6854 struct mgmt_cp_remove_advertising *cp = data;
6855 struct mgmt_rp_remove_advertising rp;
6856 int err;
6857 struct mgmt_pending_cmd *cmd;
6858 struct hci_request req;
6859
6860 BT_DBG("%s", hdev->name);
6861
6862 /* The current implementation only allows modifying instance no 1. A
6863 * value of 0 indicates that all instances should be cleared.
6864 */
6865 if (cp->instance > 1)
6866 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
6867 MGMT_STATUS_INVALID_PARAMS);
6868
6869 hci_dev_lock(hdev);
6870
6871 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
6872 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
6873 pending_find(MGMT_OP_SET_LE, hdev)) {
6874 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
6875 MGMT_STATUS_BUSY);
6876 goto unlock;
6877 }
6878
6879 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE)) {
6880 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
6881 MGMT_STATUS_INVALID_PARAMS);
6882 goto unlock;
6883 }
6884
Arman Uguray912098a2015-03-23 15:57:15 -07006885 if (hdev->adv_instance.timeout)
6886 cancel_delayed_work(&hdev->adv_instance.timeout_exp);
6887
Arman Ugurayda9293352015-03-23 15:57:13 -07006888 memset(&hdev->adv_instance, 0, sizeof(hdev->adv_instance));
6889
6890 advertising_removed(sk, hdev, 1);
6891
6892 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
6893
6894 /* If the HCI_ADVERTISING flag is set or the device isn't powered then
6895 * we have no HCI communication to make. Simply return.
6896 */
6897 if (!hdev_is_powered(hdev) ||
6898 hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
6899 rp.instance = 1;
6900 err = mgmt_cmd_complete(sk, hdev->id,
6901 MGMT_OP_REMOVE_ADVERTISING,
6902 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
6903 goto unlock;
6904 }
6905
6906 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data,
6907 data_len);
6908 if (!cmd) {
6909 err = -ENOMEM;
6910 goto unlock;
6911 }
6912
6913 hci_req_init(&req, hdev);
6914 disable_advertising(&req);
6915
6916 err = hci_req_run(&req, remove_advertising_complete);
6917 if (err < 0)
6918 mgmt_pending_remove(cmd);
6919
6920unlock:
6921 hci_dev_unlock(hdev);
6922
6923 return err;
6924}
6925
Johan Hedberg6d785aa32015-03-06 21:08:51 +02006926static const struct hci_mgmt_handler mgmt_handlers[] = {
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02006927 { NULL }, /* 0x0000 (no command) */
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006928 { read_version, MGMT_READ_VERSION_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006929 HCI_MGMT_NO_HDEV |
6930 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006931 { read_commands, MGMT_READ_COMMANDS_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006932 HCI_MGMT_NO_HDEV |
6933 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006934 { read_index_list, MGMT_READ_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006935 HCI_MGMT_NO_HDEV |
6936 HCI_MGMT_UNTRUSTED },
6937 { read_controller_info, MGMT_READ_INFO_SIZE,
6938 HCI_MGMT_UNTRUSTED },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006939 { set_powered, MGMT_SETTING_SIZE },
6940 { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE },
6941 { set_connectable, MGMT_SETTING_SIZE },
6942 { set_fast_connectable, MGMT_SETTING_SIZE },
6943 { set_bondable, MGMT_SETTING_SIZE },
6944 { set_link_security, MGMT_SETTING_SIZE },
6945 { set_ssp, MGMT_SETTING_SIZE },
6946 { set_hs, MGMT_SETTING_SIZE },
6947 { set_le, MGMT_SETTING_SIZE },
6948 { set_dev_class, MGMT_SET_DEV_CLASS_SIZE },
6949 { set_local_name, MGMT_SET_LOCAL_NAME_SIZE },
6950 { add_uuid, MGMT_ADD_UUID_SIZE },
6951 { remove_uuid, MGMT_REMOVE_UUID_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006952 { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE,
6953 HCI_MGMT_VAR_LEN },
6954 { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE,
6955 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006956 { disconnect, MGMT_DISCONNECT_SIZE },
6957 { get_connections, MGMT_GET_CONNECTIONS_SIZE },
6958 { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE },
6959 { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE },
6960 { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE },
6961 { pair_device, MGMT_PAIR_DEVICE_SIZE },
6962 { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE },
6963 { unpair_device, MGMT_UNPAIR_DEVICE_SIZE },
6964 { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE },
6965 { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
6966 { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE },
6967 { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006968 { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE },
6969 { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE,
6970 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006971 { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
6972 { start_discovery, MGMT_START_DISCOVERY_SIZE },
6973 { stop_discovery, MGMT_STOP_DISCOVERY_SIZE },
6974 { confirm_name, MGMT_CONFIRM_NAME_SIZE },
6975 { block_device, MGMT_BLOCK_DEVICE_SIZE },
6976 { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE },
6977 { set_device_id, MGMT_SET_DEVICE_ID_SIZE },
6978 { set_advertising, MGMT_SETTING_SIZE },
6979 { set_bredr, MGMT_SETTING_SIZE },
6980 { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE },
6981 { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE },
6982 { set_secure_conn, MGMT_SETTING_SIZE },
6983 { set_debug_keys, MGMT_SETTING_SIZE },
6984 { set_privacy, MGMT_SET_PRIVACY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006985 { load_irks, MGMT_LOAD_IRKS_SIZE,
6986 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006987 { get_conn_info, MGMT_GET_CONN_INFO_SIZE },
6988 { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE },
6989 { add_device, MGMT_ADD_DEVICE_SIZE },
6990 { remove_device, MGMT_REMOVE_DEVICE_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006991 { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE,
6992 HCI_MGMT_VAR_LEN },
6993 { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006994 HCI_MGMT_NO_HDEV |
6995 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006996 { read_config_info, MGMT_READ_CONFIG_INFO_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006997 HCI_MGMT_UNCONFIGURED |
6998 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006999 { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE,
7000 HCI_MGMT_UNCONFIGURED },
7001 { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE,
7002 HCI_MGMT_UNCONFIGURED },
7003 { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE,
7004 HCI_MGMT_VAR_LEN },
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007005 { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE },
Marcel Holtmann96f14742015-03-14 19:27:57 -07007006 { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007007 HCI_MGMT_NO_HDEV |
7008 HCI_MGMT_UNTRUSTED },
Marcel Holtmannd3d53052015-03-14 20:53:25 -07007009 { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE },
Arman Uguray24b4f382015-03-23 15:57:12 -07007010 { add_advertising, MGMT_ADD_ADVERTISING_SIZE,
7011 HCI_MGMT_VAR_LEN },
Arman Ugurayda9293352015-03-23 15:57:13 -07007012 { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE },
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02007013};
7014
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07007015void mgmt_index_added(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02007016{
Marcel Holtmannced85542015-03-14 19:27:56 -07007017 struct mgmt_ev_ext_index ev;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +03007018
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02007019 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
7020 return;
7021
Marcel Holtmannf9207332015-03-14 19:27:55 -07007022 switch (hdev->dev_type) {
7023 case HCI_BREDR:
7024 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
7025 mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev,
7026 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007027 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007028 } else {
7029 mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0,
7030 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007031 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007032 }
7033 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07007034 case HCI_AMP:
7035 ev.type = 0x02;
7036 break;
7037 default:
7038 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007039 }
Marcel Holtmannced85542015-03-14 19:27:56 -07007040
7041 ev.bus = hdev->bus;
7042
7043 mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev),
7044 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergc71e97b2010-12-13 21:07:07 +02007045}
7046
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07007047void mgmt_index_removed(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02007048{
Marcel Holtmannced85542015-03-14 19:27:56 -07007049 struct mgmt_ev_ext_index ev;
Johan Hedberg5f159032012-03-02 03:13:19 +02007050 u8 status = MGMT_STATUS_INVALID_INDEX;
Johan Hedbergb24752f2011-11-03 14:40:33 +02007051
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02007052 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
7053 return;
7054
Marcel Holtmannf9207332015-03-14 19:27:55 -07007055 switch (hdev->dev_type) {
7056 case HCI_BREDR:
7057 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedbergb24752f2011-11-03 14:40:33 +02007058
Marcel Holtmannf9207332015-03-14 19:27:55 -07007059 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
7060 mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev,
7061 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007062 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007063 } else {
7064 mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0,
7065 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007066 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007067 }
7068 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07007069 case HCI_AMP:
7070 ev.type = 0x02;
7071 break;
7072 default:
7073 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007074 }
Marcel Holtmannced85542015-03-14 19:27:56 -07007075
7076 ev.bus = hdev->bus;
7077
7078 mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev),
7079 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02007080}
7081
Andre Guedes6046dc32014-02-26 20:21:51 -03007082/* This function requires the caller holds hdev->lock */
Johan Hedberg2cf22212014-12-19 22:26:00 +02007083static void restart_le_actions(struct hci_request *req)
Andre Guedes6046dc32014-02-26 20:21:51 -03007084{
Johan Hedberg2cf22212014-12-19 22:26:00 +02007085 struct hci_dev *hdev = req->hdev;
Andre Guedes6046dc32014-02-26 20:21:51 -03007086 struct hci_conn_params *p;
7087
7088 list_for_each_entry(p, &hdev->le_conn_params, list) {
Johan Hedbergd7347f32014-07-04 12:37:23 +03007089 /* Needed for AUTO_OFF case where might not "really"
7090 * have been powered off.
7091 */
7092 list_del_init(&p->action);
7093
7094 switch (p->auto_connect) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02007095 case HCI_AUTO_CONN_DIRECT:
Johan Hedbergd7347f32014-07-04 12:37:23 +03007096 case HCI_AUTO_CONN_ALWAYS:
7097 list_add(&p->action, &hdev->pend_le_conns);
7098 break;
7099 case HCI_AUTO_CONN_REPORT:
7100 list_add(&p->action, &hdev->pend_le_reports);
7101 break;
7102 default:
7103 break;
Marcel Holtmannc83ed192014-07-01 19:28:24 +02007104 }
Andre Guedes6046dc32014-02-26 20:21:51 -03007105 }
Marcel Holtmannc83ed192014-07-01 19:28:24 +02007106
Johan Hedberg2cf22212014-12-19 22:26:00 +02007107 __hci_update_background_scan(req);
Andre Guedes6046dc32014-02-26 20:21:51 -03007108}
7109
Marcel Holtmann1904a852015-01-11 13:50:44 -08007110static void powered_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg229ab392013-03-15 17:06:53 -05007111{
7112 struct cmd_lookup match = { NULL, hdev };
7113
7114 BT_DBG("status 0x%02x", status);
7115
Marcel Holtmann162a3ba2015-01-14 15:43:11 -08007116 if (!status) {
7117 /* Register the available SMP channels (BR/EDR and LE) only
7118 * when successfully powering on the controller. This late
7119 * registration is required so that LE SMP can clearly
7120 * decide if the public address or static address is used.
7121 */
7122 smp_register(hdev);
7123 }
7124
Johan Hedberg229ab392013-03-15 17:06:53 -05007125 hci_dev_lock(hdev);
7126
7127 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
7128
7129 new_settings(hdev, match.sk);
7130
7131 hci_dev_unlock(hdev);
7132
7133 if (match.sk)
7134 sock_put(match.sk);
7135}
7136
Johan Hedberg70da6242013-03-15 17:06:51 -05007137static int powered_update_hci(struct hci_dev *hdev)
7138{
Johan Hedberg890ea892013-03-15 17:06:52 -05007139 struct hci_request req;
Johan Hedberg70da6242013-03-15 17:06:51 -05007140 u8 link_sec;
7141
Johan Hedberg890ea892013-03-15 17:06:52 -05007142 hci_req_init(&req, hdev);
7143
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007144 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
Johan Hedberg70da6242013-03-15 17:06:51 -05007145 !lmp_host_ssp_capable(hdev)) {
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08007146 u8 mode = 0x01;
Johan Hedberg70da6242013-03-15 17:06:51 -05007147
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08007148 hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode);
Johan Hedberg70da6242013-03-15 17:06:51 -05007149
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08007150 if (bredr_sc_enabled(hdev) && !lmp_host_sc_capable(hdev)) {
7151 u8 support = 0x01;
7152
7153 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT,
7154 sizeof(support), &support);
7155 }
Johan Hedbergec6f99b2014-12-12 13:30:11 +02007156 }
7157
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007158 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
Johan Hedbergc73eee92013-04-19 18:35:21 +03007159 lmp_bredr_capable(hdev)) {
Johan Hedberg70da6242013-03-15 17:06:51 -05007160 struct hci_cp_write_le_host_supported cp;
7161
Marcel Holtmann32226e42014-07-24 20:04:16 +02007162 cp.le = 0x01;
7163 cp.simul = 0x00;
Johan Hedberg70da6242013-03-15 17:06:51 -05007164
7165 /* Check first if we already have the right
7166 * host state (host features set)
7167 */
7168 if (cp.le != lmp_host_le_capable(hdev) ||
7169 cp.simul != lmp_host_le_br_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05007170 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED,
7171 sizeof(cp), &cp);
Johan Hedberg70da6242013-03-15 17:06:51 -05007172 }
7173
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07007174 if (lmp_le_capable(hdev)) {
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07007175 /* Make sure the controller has a good default for
7176 * advertising data. This also applies to the case
7177 * where BR/EDR was toggled during the AUTO_OFF phase.
7178 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007179 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07007180 update_adv_data(&req);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07007181 update_scan_rsp_data(&req);
7182 }
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07007183
Arman Uguray24b4f382015-03-23 15:57:12 -07007184 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
7185 hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07007186 enable_advertising(&req);
Johan Hedberg2cf22212014-12-19 22:26:00 +02007187
7188 restart_le_actions(&req);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03007189 }
7190
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007191 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg70da6242013-03-15 17:06:51 -05007192 if (link_sec != test_bit(HCI_AUTH, &hdev->flags))
Johan Hedberg890ea892013-03-15 17:06:52 -05007193 hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE,
7194 sizeof(link_sec), &link_sec);
Johan Hedberg70da6242013-03-15 17:06:51 -05007195
7196 if (lmp_bredr_capable(hdev)) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007197 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
Johan Hedberg406ef2a2015-03-10 20:14:27 +02007198 write_fast_connectable(&req, true);
7199 else
7200 write_fast_connectable(&req, false);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02007201 __hci_update_page_scan(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05007202 update_class(&req);
Johan Hedberg13928972013-03-15 17:07:00 -05007203 update_name(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05007204 update_eir(&req);
Johan Hedberg70da6242013-03-15 17:06:51 -05007205 }
7206
Johan Hedberg229ab392013-03-15 17:06:53 -05007207 return hci_req_run(&req, powered_complete);
Johan Hedberg70da6242013-03-15 17:06:51 -05007208}
7209
Johan Hedberg744cf192011-11-08 20:40:14 +02007210int mgmt_powered(struct hci_dev *hdev, u8 powered)
Johan Hedberg5add6af2010-12-16 10:00:37 +02007211{
Johan Hedberg76a7f3a2012-02-17 00:34:40 +02007212 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg98459042014-12-12 11:15:21 +02007213 u8 status, zero_cod[] = { 0, 0, 0 };
Johan Hedberg7bb895d2012-02-17 01:20:00 +02007214 int err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02007215
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007216 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg5e5282b2012-02-21 16:01:30 +02007217 return 0;
7218
Johan Hedberg5e5282b2012-02-21 16:01:30 +02007219 if (powered) {
Johan Hedberg229ab392013-03-15 17:06:53 -05007220 if (powered_update_hci(hdev) == 0)
7221 return 0;
Johan Hedbergfe038882013-01-16 16:15:34 +02007222
Johan Hedberg229ab392013-03-15 17:06:53 -05007223 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp,
7224 &match);
7225 goto new_settings;
Johan Hedbergb24752f2011-11-03 14:40:33 +02007226 }
7227
Johan Hedberg229ab392013-03-15 17:06:53 -05007228 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
Johan Hedberg98459042014-12-12 11:15:21 +02007229
7230 /* If the power off is because of hdev unregistration let
7231 * use the appropriate INVALID_INDEX status. Otherwise use
7232 * NOT_POWERED. We cover both scenarios here since later in
7233 * mgmt_index_removed() any hci_conn callbacks will have already
7234 * been triggered, potentially causing misleading DISCONNECTED
7235 * status responses.
7236 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007237 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
Johan Hedberg98459042014-12-12 11:15:21 +02007238 status = MGMT_STATUS_INVALID_INDEX;
7239 else
7240 status = MGMT_STATUS_NOT_POWERED;
7241
7242 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedberg229ab392013-03-15 17:06:53 -05007243
7244 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0)
Marcel Holtmannf6b77122015-03-14 19:28:05 -07007245 mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
7246 zero_cod, sizeof(zero_cod), NULL);
Johan Hedberg229ab392013-03-15 17:06:53 -05007247
7248new_settings:
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02007249 err = new_settings(hdev, match.sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02007250
7251 if (match.sk)
7252 sock_put(match.sk);
7253
Johan Hedberg7bb895d2012-02-17 01:20:00 +02007254 return err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02007255}
Johan Hedberg73f22f62010-12-29 16:00:25 +02007256
Marcel Holtmann3eec7052013-10-06 23:55:46 -07007257void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
Johan Hedberg96570ff2013-05-29 09:51:29 +03007258{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007259 struct mgmt_pending_cmd *cmd;
Johan Hedberg96570ff2013-05-29 09:51:29 +03007260 u8 status;
7261
Johan Hedberg333ae952015-03-17 13:48:47 +02007262 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg96570ff2013-05-29 09:51:29 +03007263 if (!cmd)
Marcel Holtmann3eec7052013-10-06 23:55:46 -07007264 return;
Johan Hedberg96570ff2013-05-29 09:51:29 +03007265
7266 if (err == -ERFKILL)
7267 status = MGMT_STATUS_RFKILLED;
7268 else
7269 status = MGMT_STATUS_FAILED;
7270
Johan Hedberga69e8372015-03-06 21:08:53 +02007271 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
Johan Hedberg96570ff2013-05-29 09:51:29 +03007272
7273 mgmt_pending_remove(cmd);
Johan Hedberg96570ff2013-05-29 09:51:29 +03007274}
7275
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007276void mgmt_discoverable_timeout(struct hci_dev *hdev)
7277{
7278 struct hci_request req;
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007279
7280 hci_dev_lock(hdev);
7281
7282 /* When discoverable timeout triggers, then just make sure
7283 * the limited discoverable flag is cleared. Even in the case
7284 * of a timeout triggered from general discoverable, it is
7285 * safe to unconditionally clear the flag.
7286 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07007287 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
7288 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007289
7290 hci_req_init(&req, hdev);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007291 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg4b580612013-10-19 23:38:21 +03007292 u8 scan = SCAN_PAGE;
7293 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE,
7294 sizeof(scan), &scan);
7295 }
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007296 update_class(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07007297
7298 /* Advertising instances don't use the global discoverable setting, so
7299 * only update AD if advertising was enabled using Set Advertising.
7300 */
7301 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
7302 update_adv_data(&req);
7303
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007304 hci_req_run(&req, NULL);
7305
7306 hdev->discov_timeout = 0;
7307
Johan Hedberg9a43e252013-10-20 19:00:07 +03007308 new_settings(hdev, NULL);
7309
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007310 hci_dev_unlock(hdev);
7311}
7312
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07007313void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
7314 bool persistent)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007315{
Johan Hedberg86742e12011-11-07 23:13:38 +02007316 struct mgmt_ev_new_link_key ev;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007317
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007318 memset(&ev, 0, sizeof(ev));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007319
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007320 ev.store_hint = persistent;
Johan Hedbergd753fdc2012-02-17 14:06:34 +02007321 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03007322 ev.key.addr.type = BDADDR_BREDR;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007323 ev.key.type = key->type;
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03007324 memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007325 ev.key.pin_len = key->pin_len;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007326
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07007327 mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007328}
Johan Hedbergf7520542011-01-20 12:34:39 +02007329
Johan Hedbergd7b25452014-05-23 13:19:53 +03007330static u8 mgmt_ltk_type(struct smp_ltk *ltk)
7331{
Johan Hedberg23fb8de2014-05-23 13:15:37 +03007332 switch (ltk->type) {
7333 case SMP_LTK:
7334 case SMP_LTK_SLAVE:
7335 if (ltk->authenticated)
7336 return MGMT_LTK_AUTHENTICATED;
7337 return MGMT_LTK_UNAUTHENTICATED;
7338 case SMP_LTK_P256:
7339 if (ltk->authenticated)
7340 return MGMT_LTK_P256_AUTH;
7341 return MGMT_LTK_P256_UNAUTH;
7342 case SMP_LTK_P256_DEBUG:
7343 return MGMT_LTK_P256_DEBUG;
7344 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03007345
7346 return MGMT_LTK_UNAUTHENTICATED;
7347}
7348
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007349void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007350{
7351 struct mgmt_ev_new_long_term_key ev;
7352
7353 memset(&ev, 0, sizeof(ev));
7354
Marcel Holtmann5192d302014-02-19 17:11:58 -08007355 /* Devices using resolvable or non-resolvable random addresses
7356 * without providing an indentity resolving key don't require
7357 * to store long term keys. Their addresses will change the
7358 * next time around.
7359 *
7360 * Only when a remote device provides an identity address
7361 * make sure the long term key is stored. If the remote
7362 * identity is known, the long term keys are internally
7363 * mapped to the identity address. So allow static random
7364 * and public addresses here.
7365 */
Johan Hedbergba74b662014-02-19 14:57:45 +02007366 if (key->bdaddr_type == ADDR_LE_DEV_RANDOM &&
7367 (key->bdaddr.b[5] & 0xc0) != 0xc0)
7368 ev.store_hint = 0x00;
7369 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007370 ev.store_hint = persistent;
Johan Hedbergba74b662014-02-19 14:57:45 +02007371
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007372 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007373 ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
Johan Hedbergd7b25452014-05-23 13:19:53 +03007374 ev.key.type = mgmt_ltk_type(key);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007375 ev.key.enc_size = key->enc_size;
7376 ev.key.ediv = key->ediv;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08007377 ev.key.rand = key->rand;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007378
Johan Hedberg2ceba532014-06-16 19:25:16 +03007379 if (key->type == SMP_LTK)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007380 ev.key.master = 1;
7381
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007382 memcpy(ev.key.val, key->val, sizeof(key->val));
7383
Marcel Holtmann083368f2013-10-15 14:26:29 -07007384 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007385}
7386
Johan Hedberg95fbac82014-02-19 15:18:31 +02007387void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk)
7388{
7389 struct mgmt_ev_new_irk ev;
7390
7391 memset(&ev, 0, sizeof(ev));
7392
Marcel Holtmannbab6d1e2014-02-19 11:51:54 -08007393 /* For identity resolving keys from devices that are already
7394 * using a public address or static random address, do not
7395 * ask for storing this key. The identity resolving key really
7396 * is only mandatory for devices using resovlable random
7397 * addresses.
7398 *
7399 * Storing all identity resolving keys has the downside that
7400 * they will be also loaded on next boot of they system. More
7401 * identity resolving keys, means more time during scanning is
7402 * needed to actually resolve these addresses.
7403 */
7404 if (bacmp(&irk->rpa, BDADDR_ANY))
7405 ev.store_hint = 0x01;
7406 else
7407 ev.store_hint = 0x00;
7408
Johan Hedberg95fbac82014-02-19 15:18:31 +02007409 bacpy(&ev.rpa, &irk->rpa);
7410 bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr);
7411 ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type);
7412 memcpy(ev.irk.val, irk->val, sizeof(irk->val));
7413
7414 mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
7415}
7416
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007417void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
7418 bool persistent)
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007419{
7420 struct mgmt_ev_new_csrk ev;
7421
7422 memset(&ev, 0, sizeof(ev));
7423
7424 /* Devices using resolvable or non-resolvable random addresses
7425 * without providing an indentity resolving key don't require
7426 * to store signature resolving keys. Their addresses will change
7427 * the next time around.
7428 *
7429 * Only when a remote device provides an identity address
7430 * make sure the signature resolving key is stored. So allow
7431 * static random and public addresses here.
7432 */
7433 if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM &&
7434 (csrk->bdaddr.b[5] & 0xc0) != 0xc0)
7435 ev.store_hint = 0x00;
7436 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007437 ev.store_hint = persistent;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007438
7439 bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
7440 ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
Johan Hedberg4cd39282015-02-27 10:11:13 +02007441 ev.key.type = csrk->type;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007442 memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
7443
7444 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
7445}
7446
Andre Guedesffb5a8272014-07-01 18:10:11 -03007447void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedbergf4869e22014-07-02 17:37:32 +03007448 u8 bdaddr_type, u8 store_hint, u16 min_interval,
7449 u16 max_interval, u16 latency, u16 timeout)
Andre Guedesffb5a8272014-07-01 18:10:11 -03007450{
7451 struct mgmt_ev_new_conn_param ev;
7452
Johan Hedbergc103aea2014-07-02 17:37:34 +03007453 if (!hci_is_identity_address(bdaddr, bdaddr_type))
7454 return;
7455
Andre Guedesffb5a8272014-07-01 18:10:11 -03007456 memset(&ev, 0, sizeof(ev));
7457 bacpy(&ev.addr.bdaddr, bdaddr);
7458 ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type);
Johan Hedbergf4869e22014-07-02 17:37:32 +03007459 ev.store_hint = store_hint;
Andre Guedesffb5a8272014-07-01 18:10:11 -03007460 ev.min_interval = cpu_to_le16(min_interval);
7461 ev.max_interval = cpu_to_le16(max_interval);
7462 ev.latency = cpu_to_le16(latency);
7463 ev.timeout = cpu_to_le16(timeout);
7464
7465 mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL);
7466}
7467
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00007468void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
7469 u32 flags, u8 *name, u8 name_len)
Johan Hedbergf7520542011-01-20 12:34:39 +02007470{
Johan Hedbergb644ba32012-01-17 21:48:47 +02007471 char buf[512];
7472 struct mgmt_ev_device_connected *ev = (void *) buf;
7473 u16 eir_len = 0;
Johan Hedbergf7520542011-01-20 12:34:39 +02007474
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00007475 bacpy(&ev->addr.bdaddr, &conn->dst);
7476 ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedbergf7520542011-01-20 12:34:39 +02007477
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02007478 ev->flags = __cpu_to_le32(flags);
Johan Hedberg08c79b62012-02-23 22:31:51 +02007479
Alfonso Acostafd45ada2014-10-07 08:44:11 +00007480 /* We must ensure that the EIR Data fields are ordered and
7481 * unique. Keep it simple for now and avoid the problem by not
7482 * adding any BR/EDR data to the LE adv.
7483 */
7484 if (conn->le_adv_data_len > 0) {
7485 memcpy(&ev->eir[eir_len],
7486 conn->le_adv_data, conn->le_adv_data_len);
7487 eir_len = conn->le_adv_data_len;
7488 } else {
7489 if (name_len > 0)
7490 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE,
7491 name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007492
Alfonso Acostaddbea5c2014-10-07 08:44:12 +00007493 if (memcmp(conn->dev_class, "\0\0\0", 3) != 0)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00007494 eir_len = eir_append_data(ev->eir, eir_len,
7495 EIR_CLASS_OF_DEV,
7496 conn->dev_class, 3);
7497 }
Johan Hedbergb644ba32012-01-17 21:48:47 +02007498
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02007499 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007500
Marcel Holtmannecd90ae2013-10-06 23:55:49 -07007501 mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf,
7502 sizeof(*ev) + eir_len, NULL);
Johan Hedbergf7520542011-01-20 12:34:39 +02007503}
7504
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007505static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg8962ee72011-01-20 12:40:27 +02007506{
Johan Hedberg8962ee72011-01-20 12:40:27 +02007507 struct sock **sk = data;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007508
Johan Hedbergf5818c22014-12-05 13:36:02 +02007509 cmd->cmd_complete(cmd, 0);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007510
7511 *sk = cmd->sk;
7512 sock_hold(*sk);
7513
Johan Hedberga664b5b2011-02-19 12:06:02 -03007514 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007515}
7516
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007517static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberga8a1d192011-11-10 15:54:38 +02007518{
Johan Hedbergb1078ad2012-02-09 17:21:16 +02007519 struct hci_dev *hdev = data;
Johan Hedberg124f6e32012-02-09 13:50:12 +02007520 struct mgmt_cp_unpair_device *cp = cmd->param;
Johan Hedberga8a1d192011-11-10 15:54:38 +02007521
Johan Hedbergb1078ad2012-02-09 17:21:16 +02007522 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
7523
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02007524 cmd->cmd_complete(cmd, 0);
Johan Hedberga8a1d192011-11-10 15:54:38 +02007525 mgmt_pending_remove(cmd);
7526}
7527
Johan Hedberg84c61d92014-08-01 11:13:30 +03007528bool mgmt_powering_down(struct hci_dev *hdev)
7529{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007530 struct mgmt_pending_cmd *cmd;
Johan Hedberg84c61d92014-08-01 11:13:30 +03007531 struct mgmt_mode *cp;
7532
Johan Hedberg333ae952015-03-17 13:48:47 +02007533 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg84c61d92014-08-01 11:13:30 +03007534 if (!cmd)
7535 return false;
7536
7537 cp = cmd->param;
7538 if (!cp->val)
7539 return true;
7540
7541 return false;
7542}
7543
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07007544void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02007545 u8 link_type, u8 addr_type, u8 reason,
7546 bool mgmt_connected)
Johan Hedbergf7520542011-01-20 12:34:39 +02007547{
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02007548 struct mgmt_ev_device_disconnected ev;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007549 struct sock *sk = NULL;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007550
Johan Hedberg84c61d92014-08-01 11:13:30 +03007551 /* The connection is still in hci_conn_hash so test for 1
7552 * instead of 0 to know if this is the last one.
7553 */
7554 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
7555 cancel_delayed_work(&hdev->power_off);
7556 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberg8b064a32014-02-24 14:52:22 +02007557 }
7558
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02007559 if (!mgmt_connected)
7560 return;
7561
Andre Guedes57eb7762013-10-30 19:01:41 -03007562 if (link_type != ACL_LINK && link_type != LE_LINK)
7563 return;
7564
Johan Hedberg744cf192011-11-08 20:40:14 +02007565 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
Johan Hedbergf7520542011-01-20 12:34:39 +02007566
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02007567 bacpy(&ev.addr.bdaddr, bdaddr);
7568 ev.addr.type = link_to_bdaddr(link_type, addr_type);
7569 ev.reason = reason;
Johan Hedbergf7520542011-01-20 12:34:39 +02007570
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07007571 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007572
7573 if (sk)
Szymon Jancd97dcb62012-03-16 16:02:56 +01007574 sock_put(sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007575
Johan Hedberg124f6e32012-02-09 13:50:12 +02007576 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007577 hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007578}
7579
Marcel Holtmann78929242013-10-06 23:55:47 -07007580void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
7581 u8 link_type, u8 addr_type, u8 status)
Johan Hedberg8962ee72011-01-20 12:40:27 +02007582{
Andre Guedes3655bba2013-10-30 19:01:40 -03007583 u8 bdaddr_type = link_to_bdaddr(link_type, addr_type);
7584 struct mgmt_cp_disconnect *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007585 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007586
Jefferson Delfes36a75f12012-09-18 13:36:54 -04007587 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
7588 hdev);
7589
Johan Hedberg333ae952015-03-17 13:48:47 +02007590 cmd = pending_find(MGMT_OP_DISCONNECT, hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007591 if (!cmd)
Marcel Holtmann78929242013-10-06 23:55:47 -07007592 return;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007593
Andre Guedes3655bba2013-10-30 19:01:40 -03007594 cp = cmd->param;
7595
7596 if (bacmp(bdaddr, &cp->addr.bdaddr))
7597 return;
7598
7599 if (cp->addr.type != bdaddr_type)
7600 return;
7601
Johan Hedbergf5818c22014-12-05 13:36:02 +02007602 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007603 mgmt_pending_remove(cmd);
Johan Hedbergf7520542011-01-20 12:34:39 +02007604}
Johan Hedberg17d5c042011-01-22 06:09:08 +02007605
Marcel Holtmann445608d2013-10-06 23:55:48 -07007606void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
7607 u8 addr_type, u8 status)
Johan Hedberg17d5c042011-01-22 06:09:08 +02007608{
7609 struct mgmt_ev_connect_failed ev;
Johan Hedbergc9910d02014-02-27 14:35:12 +02007610
Johan Hedberg84c61d92014-08-01 11:13:30 +03007611 /* The connection is still in hci_conn_hash so test for 1
7612 * instead of 0 to know if this is the last one.
7613 */
7614 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
7615 cancel_delayed_work(&hdev->power_off);
7616 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedbergc9910d02014-02-27 14:35:12 +02007617 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02007618
Johan Hedberg4c659c32011-11-07 23:13:39 +02007619 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007620 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergca69b792011-11-11 18:10:00 +02007621 ev.status = mgmt_status(status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02007622
Marcel Holtmann445608d2013-10-06 23:55:48 -07007623 mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg17d5c042011-01-22 06:09:08 +02007624}
Johan Hedberg980e1a52011-01-22 06:10:07 +02007625
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07007626void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007627{
7628 struct mgmt_ev_pin_code_request ev;
7629
Johan Hedbergd8457692012-02-17 14:24:57 +02007630 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03007631 ev.addr.type = BDADDR_BREDR;
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02007632 ev.secure = secure;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007633
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07007634 mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007635}
7636
Marcel Holtmanne669cf82013-10-15 14:26:21 -07007637void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
7638 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007639{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007640 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007641
Johan Hedberg333ae952015-03-17 13:48:47 +02007642 cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007643 if (!cmd)
Marcel Holtmanne669cf82013-10-15 14:26:21 -07007644 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007645
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007646 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007647 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007648}
7649
Marcel Holtmann3eb38522013-10-15 14:26:22 -07007650void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
7651 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007652{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007653 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007654
Johan Hedberg333ae952015-03-17 13:48:47 +02007655 cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007656 if (!cmd)
Marcel Holtmann3eb38522013-10-15 14:26:22 -07007657 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007658
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007659 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007660 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007661}
Johan Hedberga5c29682011-02-19 12:05:57 -03007662
Johan Hedberg744cf192011-11-08 20:40:14 +02007663int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg39adbff2014-03-20 08:18:14 +02007664 u8 link_type, u8 addr_type, u32 value,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007665 u8 confirm_hint)
Johan Hedberga5c29682011-02-19 12:05:57 -03007666{
7667 struct mgmt_ev_user_confirm_request ev;
7668
Johan Hedberg744cf192011-11-08 20:40:14 +02007669 BT_DBG("%s", hdev->name);
Johan Hedberga5c29682011-02-19 12:05:57 -03007670
Johan Hedberg272d90d2012-02-09 15:26:12 +02007671 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007672 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberg55bc1a32011-04-28 11:28:56 -07007673 ev.confirm_hint = confirm_hint;
Johan Hedberg39adbff2014-03-20 08:18:14 +02007674 ev.value = cpu_to_le32(value);
Johan Hedberga5c29682011-02-19 12:05:57 -03007675
Johan Hedberg744cf192011-11-08 20:40:14 +02007676 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007677 NULL);
Johan Hedberga5c29682011-02-19 12:05:57 -03007678}
7679
Johan Hedberg272d90d2012-02-09 15:26:12 +02007680int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03007681 u8 link_type, u8 addr_type)
Brian Gix604086b2011-11-23 08:28:33 -08007682{
7683 struct mgmt_ev_user_passkey_request ev;
7684
7685 BT_DBG("%s", hdev->name);
7686
Johan Hedberg272d90d2012-02-09 15:26:12 +02007687 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007688 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Brian Gix604086b2011-11-23 08:28:33 -08007689
7690 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007691 NULL);
Brian Gix604086b2011-11-23 08:28:33 -08007692}
7693
Brian Gix0df4c182011-11-16 13:53:13 -08007694static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03007695 u8 link_type, u8 addr_type, u8 status,
7696 u8 opcode)
Johan Hedberga5c29682011-02-19 12:05:57 -03007697{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007698 struct mgmt_pending_cmd *cmd;
Johan Hedberga5c29682011-02-19 12:05:57 -03007699
Johan Hedberg333ae952015-03-17 13:48:47 +02007700 cmd = pending_find(opcode, hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03007701 if (!cmd)
7702 return -ENOENT;
7703
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007704 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007705 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03007706
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007707 return 0;
Johan Hedberga5c29682011-02-19 12:05:57 -03007708}
7709
Johan Hedberg744cf192011-11-08 20:40:14 +02007710int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007711 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03007712{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007713 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007714 status, MGMT_OP_USER_CONFIRM_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03007715}
7716
Johan Hedberg272d90d2012-02-09 15:26:12 +02007717int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007718 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03007719{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007720 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03007721 status,
7722 MGMT_OP_USER_CONFIRM_NEG_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03007723}
Johan Hedberg2a611692011-02-19 12:06:00 -03007724
Brian Gix604086b2011-11-23 08:28:33 -08007725int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007726 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08007727{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007728 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007729 status, MGMT_OP_USER_PASSKEY_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08007730}
7731
Johan Hedberg272d90d2012-02-09 15:26:12 +02007732int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007733 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08007734{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007735 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03007736 status,
7737 MGMT_OP_USER_PASSKEY_NEG_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08007738}
7739
Johan Hedberg92a25252012-09-06 18:39:26 +03007740int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
7741 u8 link_type, u8 addr_type, u32 passkey,
7742 u8 entered)
7743{
7744 struct mgmt_ev_passkey_notify ev;
7745
7746 BT_DBG("%s", hdev->name);
7747
7748 bacpy(&ev.addr.bdaddr, bdaddr);
7749 ev.addr.type = link_to_bdaddr(link_type, addr_type);
7750 ev.passkey = __cpu_to_le32(passkey);
7751 ev.entered = entered;
7752
7753 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
7754}
7755
Johan Hedberge1e930f2014-09-08 17:09:49 -07007756void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status)
Johan Hedberg2a611692011-02-19 12:06:00 -03007757{
7758 struct mgmt_ev_auth_failed ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007759 struct mgmt_pending_cmd *cmd;
Johan Hedberge1e930f2014-09-08 17:09:49 -07007760 u8 status = mgmt_status(hci_status);
Johan Hedberg2a611692011-02-19 12:06:00 -03007761
Johan Hedberge1e930f2014-09-08 17:09:49 -07007762 bacpy(&ev.addr.bdaddr, &conn->dst);
7763 ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
7764 ev.status = status;
Johan Hedberg2a611692011-02-19 12:06:00 -03007765
Johan Hedberge1e930f2014-09-08 17:09:49 -07007766 cmd = find_pairing(conn);
7767
7768 mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev),
7769 cmd ? cmd->sk : NULL);
7770
Johan Hedberga511b352014-12-11 21:45:45 +02007771 if (cmd) {
7772 cmd->cmd_complete(cmd, status);
7773 mgmt_pending_remove(cmd);
7774 }
Johan Hedberg2a611692011-02-19 12:06:00 -03007775}
Johan Hedbergb312b1612011-03-16 14:29:37 +02007776
Marcel Holtmann464996a2013-10-15 14:26:24 -07007777void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007778{
7779 struct cmd_lookup match = { NULL, hdev };
Marcel Holtmann464996a2013-10-15 14:26:24 -07007780 bool changed;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007781
7782 if (status) {
7783 u8 mgmt_err = mgmt_status(status);
7784 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007785 cmd_status_rsp, &mgmt_err);
Marcel Holtmann464996a2013-10-15 14:26:24 -07007786 return;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007787 }
7788
Marcel Holtmann464996a2013-10-15 14:26:24 -07007789 if (test_bit(HCI_AUTH, &hdev->flags))
Marcel Holtmann238be782015-03-13 02:11:06 -07007790 changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY);
Marcel Holtmann464996a2013-10-15 14:26:24 -07007791 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007792 changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02007793
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007794 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007795 &match);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007796
Johan Hedberg47990ea2012-02-22 11:58:37 +02007797 if (changed)
Marcel Holtmann464996a2013-10-15 14:26:24 -07007798 new_settings(hdev, match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007799
7800 if (match.sk)
7801 sock_put(match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007802}
7803
Johan Hedberg890ea892013-03-15 17:06:52 -05007804static void clear_eir(struct hci_request *req)
Johan Hedbergcacaf522012-02-21 00:52:42 +02007805{
Johan Hedberg890ea892013-03-15 17:06:52 -05007806 struct hci_dev *hdev = req->hdev;
Johan Hedbergcacaf522012-02-21 00:52:42 +02007807 struct hci_cp_write_eir cp;
7808
Johan Hedberg976eb202012-10-24 21:12:01 +03007809 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05007810 return;
Johan Hedbergcacaf522012-02-21 00:52:42 +02007811
Johan Hedbergc80da272012-02-22 15:38:48 +02007812 memset(hdev->eir, 0, sizeof(hdev->eir));
7813
Johan Hedbergcacaf522012-02-21 00:52:42 +02007814 memset(&cp, 0, sizeof(cp));
7815
Johan Hedberg890ea892013-03-15 17:06:52 -05007816 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedbergcacaf522012-02-21 00:52:42 +02007817}
7818
Marcel Holtmann3e248562013-10-15 14:26:25 -07007819void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007820{
7821 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg890ea892013-03-15 17:06:52 -05007822 struct hci_request req;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007823 bool changed = false;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007824
7825 if (status) {
7826 u8 mgmt_err = mgmt_status(status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007827
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007828 if (enable && hci_dev_test_and_clear_flag(hdev,
7829 HCI_SSP_ENABLED)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07007830 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmann3e248562013-10-15 14:26:25 -07007831 new_settings(hdev, NULL);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007832 }
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007833
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007834 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
7835 &mgmt_err);
Marcel Holtmann3e248562013-10-15 14:26:25 -07007836 return;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007837 }
7838
7839 if (enable) {
Marcel Holtmann238be782015-03-13 02:11:06 -07007840 changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007841 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007842 changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007843 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007844 changed = hci_dev_test_and_clear_flag(hdev,
7845 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007846 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07007847 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007848 }
7849
7850 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
7851
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007852 if (changed)
Marcel Holtmann3e248562013-10-15 14:26:25 -07007853 new_settings(hdev, match.sk);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007854
Johan Hedberg5fc6ebb2012-02-22 15:10:59 +02007855 if (match.sk)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007856 sock_put(match.sk);
7857
Johan Hedberg890ea892013-03-15 17:06:52 -05007858 hci_req_init(&req, hdev);
7859
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007860 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
7861 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03007862 hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE,
7863 sizeof(enable), &enable);
Johan Hedberg890ea892013-03-15 17:06:52 -05007864 update_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03007865 } else {
Johan Hedberg890ea892013-03-15 17:06:52 -05007866 clear_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03007867 }
Johan Hedberg890ea892013-03-15 17:06:52 -05007868
7869 hci_req_run(&req, NULL);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007870}
7871
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007872static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg90e70452012-02-23 23:09:40 +02007873{
7874 struct cmd_lookup *match = data;
7875
Johan Hedberg90e70452012-02-23 23:09:40 +02007876 if (match->sk == NULL) {
7877 match->sk = cmd->sk;
7878 sock_hold(match->sk);
7879 }
Johan Hedberg90e70452012-02-23 23:09:40 +02007880}
7881
Marcel Holtmann4e1b0242013-10-15 14:26:26 -07007882void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
7883 u8 status)
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007884{
Johan Hedberg90e70452012-02-23 23:09:40 +02007885 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007886
Johan Hedberg92da6092013-03-15 17:06:55 -05007887 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
7888 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
7889 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
Johan Hedberg90e70452012-02-23 23:09:40 +02007890
7891 if (!status)
Marcel Holtmannf6b77122015-03-14 19:28:05 -07007892 mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
7893 dev_class, 3, NULL);
Johan Hedberg90e70452012-02-23 23:09:40 +02007894
7895 if (match.sk)
7896 sock_put(match.sk);
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007897}
7898
Marcel Holtmann7667da32013-10-15 14:26:27 -07007899void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
Johan Hedbergb312b1612011-03-16 14:29:37 +02007900{
Johan Hedbergb312b1612011-03-16 14:29:37 +02007901 struct mgmt_cp_set_local_name ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007902 struct mgmt_pending_cmd *cmd;
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02007903
Johan Hedberg13928972013-03-15 17:07:00 -05007904 if (status)
Marcel Holtmann7667da32013-10-15 14:26:27 -07007905 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02007906
7907 memset(&ev, 0, sizeof(ev));
7908 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02007909 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
Johan Hedbergb312b1612011-03-16 14:29:37 +02007910
Johan Hedberg333ae952015-03-17 13:48:47 +02007911 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05007912 if (!cmd) {
7913 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
Johan Hedbergb312b1612011-03-16 14:29:37 +02007914
Johan Hedberg13928972013-03-15 17:07:00 -05007915 /* If this is a HCI command related to powering on the
7916 * HCI dev don't send any mgmt signals.
7917 */
Johan Hedberg333ae952015-03-17 13:48:47 +02007918 if (pending_find(MGMT_OP_SET_POWERED, hdev))
Marcel Holtmann7667da32013-10-15 14:26:27 -07007919 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02007920 }
7921
Marcel Holtmannf6b77122015-03-14 19:28:05 -07007922 mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
7923 cmd ? cmd->sk : NULL);
Johan Hedbergb312b1612011-03-16 14:29:37 +02007924}
Szymon Jancc35938b2011-03-22 13:12:21 +01007925
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08007926void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192,
Johan Hedberg38da1702014-11-17 20:52:20 +02007927 u8 *rand192, u8 *hash256, u8 *rand256,
7928 u8 status)
Szymon Jancc35938b2011-03-22 13:12:21 +01007929{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007930 struct mgmt_pending_cmd *cmd;
Szymon Jancc35938b2011-03-22 13:12:21 +01007931
Johan Hedberg744cf192011-11-08 20:40:14 +02007932 BT_DBG("%s status %u", hdev->name, status);
Szymon Jancc35938b2011-03-22 13:12:21 +01007933
Johan Hedberg333ae952015-03-17 13:48:47 +02007934 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01007935 if (!cmd)
Marcel Holtmann3edaf092013-10-15 14:26:28 -07007936 return;
Szymon Jancc35938b2011-03-22 13:12:21 +01007937
7938 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02007939 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
7940 mgmt_status(status));
Szymon Jancc35938b2011-03-22 13:12:21 +01007941 } else {
Johan Hedberg66f096f2015-02-02 13:23:42 +02007942 struct mgmt_rp_read_local_oob_data rp;
7943 size_t rp_size = sizeof(rp);
7944
7945 memcpy(rp.hash192, hash192, sizeof(rp.hash192));
7946 memcpy(rp.rand192, rand192, sizeof(rp.rand192));
7947
Johan Hedberg710f11c2014-05-26 11:21:22 +03007948 if (bredr_sc_enabled(hdev) && hash256 && rand256) {
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08007949 memcpy(rp.hash256, hash256, sizeof(rp.hash256));
Johan Hedberg38da1702014-11-17 20:52:20 +02007950 memcpy(rp.rand256, rand256, sizeof(rp.rand256));
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08007951 } else {
Johan Hedberg66f096f2015-02-02 13:23:42 +02007952 rp_size -= sizeof(rp.hash256) + sizeof(rp.rand256);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08007953 }
Johan Hedberg66f096f2015-02-02 13:23:42 +02007954
Johan Hedberg2a1afb52015-03-06 21:08:54 +02007955 mgmt_cmd_complete(cmd->sk, hdev->id,
7956 MGMT_OP_READ_LOCAL_OOB_DATA, 0,
7957 &rp, rp_size);
Szymon Jancc35938b2011-03-22 13:12:21 +01007958 }
7959
7960 mgmt_pending_remove(cmd);
Szymon Jancc35938b2011-03-22 13:12:21 +01007961}
Johan Hedberge17acd42011-03-30 23:57:16 +03007962
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007963static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16])
7964{
7965 int i;
7966
7967 for (i = 0; i < uuid_count; i++) {
7968 if (!memcmp(uuid, uuids[i], 16))
7969 return true;
7970 }
7971
7972 return false;
7973}
7974
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007975static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16])
7976{
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007977 u16 parsed = 0;
7978
7979 while (parsed < eir_len) {
7980 u8 field_len = eir[0];
7981 u8 uuid[16];
7982 int i;
7983
7984 if (field_len == 0)
7985 break;
7986
7987 if (eir_len - parsed < field_len + 1)
7988 break;
7989
7990 switch (eir[1]) {
7991 case EIR_UUID16_ALL:
7992 case EIR_UUID16_SOME:
7993 for (i = 0; i + 3 <= field_len; i += 2) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02007994 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007995 uuid[13] = eir[i + 3];
7996 uuid[12] = eir[i + 2];
7997 if (has_uuid(uuid, uuid_count, uuids))
7998 return true;
7999 }
8000 break;
8001 case EIR_UUID32_ALL:
8002 case EIR_UUID32_SOME:
8003 for (i = 0; i + 5 <= field_len; i += 4) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02008004 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01008005 uuid[15] = eir[i + 5];
8006 uuid[14] = eir[i + 4];
8007 uuid[13] = eir[i + 3];
8008 uuid[12] = eir[i + 2];
8009 if (has_uuid(uuid, uuid_count, uuids))
8010 return true;
8011 }
8012 break;
8013 case EIR_UUID128_ALL:
8014 case EIR_UUID128_SOME:
8015 for (i = 0; i + 17 <= field_len; i += 16) {
8016 memcpy(uuid, eir + i + 2, 16);
8017 if (has_uuid(uuid, uuid_count, uuids))
8018 return true;
8019 }
8020 break;
8021 }
8022
8023 parsed += field_len + 1;
8024 eir += field_len + 1;
8025 }
8026
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01008027 return false;
8028}
8029
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08008030static void restart_le_scan(struct hci_dev *hdev)
8031{
8032 /* If controller is not scanning we are done. */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07008033 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08008034 return;
8035
8036 if (time_after(jiffies + DISCOV_LE_RESTART_DELAY,
8037 hdev->discovery.scan_start +
8038 hdev->discovery.scan_duration))
8039 return;
8040
8041 queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart,
8042 DISCOV_LE_RESTART_DELAY);
8043}
8044
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008045static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir,
8046 u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
8047{
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008048 /* If a RSSI threshold has been specified, and
8049 * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with
8050 * a RSSI smaller than the RSSI threshold will be dropped. If the quirk
8051 * is set, let it through for further processing, as we might need to
8052 * restart the scan.
8053 *
8054 * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry,
8055 * the results are also dropped.
8056 */
8057 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
8058 (rssi == HCI_RSSI_INVALID ||
8059 (rssi < hdev->discovery.rssi &&
8060 !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))
8061 return false;
8062
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008063 if (hdev->discovery.uuid_count != 0) {
8064 /* If a list of UUIDs is provided in filter, results with no
8065 * matching UUID should be dropped.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008066 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008067 if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count,
8068 hdev->discovery.uuids) &&
8069 !eir_has_uuids(scan_rsp, scan_rsp_len,
8070 hdev->discovery.uuid_count,
8071 hdev->discovery.uuids))
8072 return false;
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008073 }
8074
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008075 /* If duplicate filtering does not report RSSI changes, then restart
8076 * scanning to ensure updated result with updated RSSI values.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008077 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008078 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) {
8079 restart_le_scan(hdev);
8080
8081 /* Validate RSSI value against the RSSI threshold once more. */
8082 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
8083 rssi < hdev->discovery.rssi)
8084 return false;
8085 }
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008086
8087 return true;
8088}
8089
Marcel Holtmann901801b2013-10-06 23:55:51 -07008090void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
Marcel Holtmannaf589252014-07-01 14:11:20 +02008091 u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
8092 u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
Johan Hedberge17acd42011-03-30 23:57:16 +03008093{
Johan Hedberge319d2e2012-01-15 19:51:59 +02008094 char buf[512];
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008095 struct mgmt_ev_device_found *ev = (void *)buf;
Johan Hedberg1dc06092012-01-15 21:01:23 +02008096 size_t ev_size;
Johan Hedberge17acd42011-03-30 23:57:16 +03008097
Johan Hedberg75ce2082014-07-02 22:42:01 +03008098 /* Don't send events for a non-kernel initiated discovery. With
8099 * LE one exception is if we have pend_le_reports > 0 in which
8100 * case we're doing passive scanning and want these events.
8101 */
8102 if (!hci_discovery_active(hdev)) {
8103 if (link_type == ACL_LINK)
8104 return;
Johan Hedberg66f84552014-07-04 12:37:18 +03008105 if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports))
Johan Hedberg75ce2082014-07-02 22:42:01 +03008106 return;
8107 }
Andre Guedes12602d02013-04-30 15:29:40 -03008108
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08008109 if (hdev->discovery.result_filtering) {
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008110 /* We are using service discovery */
8111 if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp,
8112 scan_rsp_len))
8113 return;
8114 }
Marcel Holtmannbda157a2014-12-05 10:55:56 +01008115
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02008116 /* Make sure that the buffer is big enough. The 5 extra bytes
8117 * are for the potential CoD field.
8118 */
8119 if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf))
Marcel Holtmann901801b2013-10-06 23:55:51 -07008120 return;
Andre Guedes7d262f82012-01-10 18:20:49 -03008121
Johan Hedberg1dc06092012-01-15 21:01:23 +02008122 memset(buf, 0, sizeof(buf));
8123
Marcel Holtmannda25cf62014-12-05 13:03:35 +01008124 /* In case of device discovery with BR/EDR devices (pre 1.2), the
8125 * RSSI value was reported as 0 when not available. This behavior
8126 * is kept when using device discovery. This is required for full
8127 * backwards compatibility with the API.
8128 *
8129 * However when using service discovery, the value 127 will be
8130 * returned when the RSSI is not available.
8131 */
Szymon Janc91200e92015-01-22 16:57:05 +01008132 if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi &&
8133 link_type == ACL_LINK)
Marcel Holtmannefb25132014-12-05 13:03:34 +01008134 rssi = 0;
8135
Johan Hedberg841c5642014-07-07 12:45:54 +03008136 bacpy(&ev->addr.bdaddr, bdaddr);
8137 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberge319d2e2012-01-15 19:51:59 +02008138 ev->rssi = rssi;
Marcel Holtmannaf589252014-07-01 14:11:20 +02008139 ev->flags = cpu_to_le32(flags);
Johan Hedberge17acd42011-03-30 23:57:16 +03008140
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008141 if (eir_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01008142 /* Copy EIR or advertising data into event */
Johan Hedberge319d2e2012-01-15 19:51:59 +02008143 memcpy(ev->eir, eir, eir_len);
Johan Hedberge17acd42011-03-30 23:57:16 +03008144
Johan Hedberg1dc06092012-01-15 21:01:23 +02008145 if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV))
8146 eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03008147 dev_class, 3);
Johan Hedberg1dc06092012-01-15 21:01:23 +02008148
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008149 if (scan_rsp_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01008150 /* Append scan response data to event */
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02008151 memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len);
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08008152
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02008153 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
8154 ev_size = sizeof(*ev) + eir_len + scan_rsp_len;
Andre Guedesf8523592011-09-09 18:56:26 -03008155
Marcel Holtmann901801b2013-10-06 23:55:51 -07008156 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL);
Johan Hedberge17acd42011-03-30 23:57:16 +03008157}
Johan Hedberga88a9652011-03-30 13:18:12 +03008158
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07008159void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
8160 u8 addr_type, s8 rssi, u8 *name, u8 name_len)
Johan Hedberga88a9652011-03-30 13:18:12 +03008161{
Johan Hedbergb644ba32012-01-17 21:48:47 +02008162 struct mgmt_ev_device_found *ev;
8163 char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2];
8164 u16 eir_len;
Johan Hedberga88a9652011-03-30 13:18:12 +03008165
Johan Hedbergb644ba32012-01-17 21:48:47 +02008166 ev = (struct mgmt_ev_device_found *) buf;
Johan Hedberga88a9652011-03-30 13:18:12 +03008167
Johan Hedbergb644ba32012-01-17 21:48:47 +02008168 memset(buf, 0, sizeof(buf));
Johan Hedberga88a9652011-03-30 13:18:12 +03008169
Johan Hedbergb644ba32012-01-17 21:48:47 +02008170 bacpy(&ev->addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03008171 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergb644ba32012-01-17 21:48:47 +02008172 ev->rssi = rssi;
8173
8174 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03008175 name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02008176
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02008177 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02008178
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07008179 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL);
Johan Hedberga88a9652011-03-30 13:18:12 +03008180}
Johan Hedberg314b2382011-04-27 10:29:57 -04008181
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07008182void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
Johan Hedberg314b2382011-04-27 10:29:57 -04008183{
Johan Hedbergf963e8e2012-02-20 23:30:44 +02008184 struct mgmt_ev_discovering ev;
Johan Hedberg164a6e72011-11-01 17:06:44 +02008185
Andre Guedes343fb142011-11-22 17:14:19 -03008186 BT_DBG("%s discovering %u", hdev->name, discovering);
8187
Johan Hedbergf963e8e2012-02-20 23:30:44 +02008188 memset(&ev, 0, sizeof(ev));
8189 ev.type = hdev->discovery.type;
8190 ev.discovering = discovering;
8191
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07008192 mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg314b2382011-04-27 10:29:57 -04008193}
Antti Julku5e762442011-08-25 16:48:02 +03008194
Marcel Holtmann1904a852015-01-11 13:50:44 -08008195static void adv_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Marcel Holtmann5976e602013-10-06 04:08:14 -07008196{
8197 BT_DBG("%s status %u", hdev->name, status);
Marcel Holtmann5976e602013-10-06 04:08:14 -07008198}
8199
8200void mgmt_reenable_advertising(struct hci_dev *hdev)
8201{
8202 struct hci_request req;
8203
Arman Uguray24b4f382015-03-23 15:57:12 -07008204 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
8205 !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
Marcel Holtmann5976e602013-10-06 04:08:14 -07008206 return;
8207
8208 hci_req_init(&req, hdev);
8209 enable_advertising(&req);
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03008210 hci_req_run(&req, adv_enable_complete);
Marcel Holtmann5976e602013-10-06 04:08:14 -07008211}
Johan Hedberg6d785aa32015-03-06 21:08:51 +02008212
8213static struct hci_mgmt_chan chan = {
8214 .channel = HCI_CHANNEL_CONTROL,
8215 .handler_count = ARRAY_SIZE(mgmt_handlers),
8216 .handlers = mgmt_handlers,
Johan Hedberg88b94ce2015-03-17 13:48:49 +02008217 .hdev_init = mgmt_init_hdev,
Johan Hedberg6d785aa32015-03-06 21:08:51 +02008218};
8219
8220int mgmt_init(void)
8221{
8222 return hci_mgmt_chan_register(&chan);
8223}
8224
8225void mgmt_exit(void)
8226{
8227 hci_mgmt_chan_unregister(&chan);
8228}