blob: fc807dcc253370de589a75038ecfa2307b873207 [file] [log] [blame]
Johan Hedberg03811012010-12-08 00:21:06 +02001/*
2 BlueZ - Bluetooth protocol stack for Linux
Johan Hedbergea585ab2012-02-17 14:50:39 +02003
Johan Hedberg03811012010-12-08 00:21:06 +02004 Copyright (C) 2010 Nokia Corporation
Johan Hedbergea585ab2012-02-17 14:50:39 +02005 Copyright (C) 2011-2012 Intel Corporation
Johan Hedberg03811012010-12-08 00:21:06 +02006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth HCI Management interface */
26
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040027#include <linux/module.h>
Johan Hedberg03811012010-12-08 00:21:06 +020028#include <asm/unaligned.h>
29
30#include <net/bluetooth/bluetooth.h>
31#include <net/bluetooth/hci_core.h>
Johan Hedberg71290692015-02-20 13:26:23 +020032#include <net/bluetooth/hci_sock.h>
Johan Hedberg4bc58f52014-05-20 09:45:47 +030033#include <net/bluetooth/l2cap.h>
Johan Hedberg03811012010-12-08 00:21:06 +020034#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070035
Johan Hedberg0857dd32014-12-19 13:40:20 +020036#include "hci_request.h"
Marcel Holtmannac4b7232013-10-10 14:54:16 -070037#include "smp.h"
Johan Hedberga380b6c2015-03-17 13:48:48 +020038#include "mgmt_util.h"
Johan Hedberg03811012010-12-08 00:21:06 +020039
Johan Hedberg2da9c552012-02-17 14:39:28 +020040#define MGMT_VERSION 1
Marcel Holtmannbeb1c212015-03-10 14:04:52 -070041#define MGMT_REVISION 9
Johan Hedberg02d98122010-12-13 21:07:04 +020042
Johan Hedberge70bb2e2012-02-13 16:59:33 +020043static const u16 mgmt_commands[] = {
44 MGMT_OP_READ_INDEX_LIST,
45 MGMT_OP_READ_INFO,
46 MGMT_OP_SET_POWERED,
47 MGMT_OP_SET_DISCOVERABLE,
48 MGMT_OP_SET_CONNECTABLE,
49 MGMT_OP_SET_FAST_CONNECTABLE,
Johan Hedbergb2939472014-07-30 09:22:23 +030050 MGMT_OP_SET_BONDABLE,
Johan Hedberge70bb2e2012-02-13 16:59:33 +020051 MGMT_OP_SET_LINK_SECURITY,
52 MGMT_OP_SET_SSP,
53 MGMT_OP_SET_HS,
54 MGMT_OP_SET_LE,
55 MGMT_OP_SET_DEV_CLASS,
56 MGMT_OP_SET_LOCAL_NAME,
57 MGMT_OP_ADD_UUID,
58 MGMT_OP_REMOVE_UUID,
59 MGMT_OP_LOAD_LINK_KEYS,
60 MGMT_OP_LOAD_LONG_TERM_KEYS,
61 MGMT_OP_DISCONNECT,
62 MGMT_OP_GET_CONNECTIONS,
63 MGMT_OP_PIN_CODE_REPLY,
64 MGMT_OP_PIN_CODE_NEG_REPLY,
65 MGMT_OP_SET_IO_CAPABILITY,
66 MGMT_OP_PAIR_DEVICE,
67 MGMT_OP_CANCEL_PAIR_DEVICE,
68 MGMT_OP_UNPAIR_DEVICE,
69 MGMT_OP_USER_CONFIRM_REPLY,
70 MGMT_OP_USER_CONFIRM_NEG_REPLY,
71 MGMT_OP_USER_PASSKEY_REPLY,
72 MGMT_OP_USER_PASSKEY_NEG_REPLY,
73 MGMT_OP_READ_LOCAL_OOB_DATA,
74 MGMT_OP_ADD_REMOTE_OOB_DATA,
75 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
76 MGMT_OP_START_DISCOVERY,
77 MGMT_OP_STOP_DISCOVERY,
78 MGMT_OP_CONFIRM_NAME,
79 MGMT_OP_BLOCK_DEVICE,
80 MGMT_OP_UNBLOCK_DEVICE,
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -070081 MGMT_OP_SET_DEVICE_ID,
Johan Hedberg4375f102013-09-25 13:26:10 +030082 MGMT_OP_SET_ADVERTISING,
Johan Hedberg0663ca22013-10-02 13:43:14 +030083 MGMT_OP_SET_BREDR,
Marcel Holtmannd13eafc2013-10-02 04:41:30 -070084 MGMT_OP_SET_STATIC_ADDRESS,
Marcel Holtmann7f72134e2013-10-11 14:44:58 -070085 MGMT_OP_SET_SCAN_PARAMS,
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -080086 MGMT_OP_SET_SECURE_CONN,
Marcel Holtmann4e39ac82014-01-31 11:55:22 -080087 MGMT_OP_SET_DEBUG_KEYS,
Johan Hedberg62b04cd2014-02-23 19:42:27 +020088 MGMT_OP_SET_PRIVACY,
Johan Hedberg41edf162014-02-18 10:19:35 +020089 MGMT_OP_LOAD_IRKS,
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +020090 MGMT_OP_GET_CONN_INFO,
Johan Hedberg95868422014-06-28 17:54:07 +030091 MGMT_OP_GET_CLOCK_INFO,
Marcel Holtmann2faade52014-06-29 19:44:03 +020092 MGMT_OP_ADD_DEVICE,
93 MGMT_OP_REMOVE_DEVICE,
Johan Hedberga26f3dc2014-07-02 17:37:29 +030094 MGMT_OP_LOAD_CONN_PARAM,
Marcel Holtmann73d1df22014-07-02 22:10:52 +020095 MGMT_OP_READ_UNCONF_INDEX_LIST,
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +020096 MGMT_OP_READ_CONFIG_INFO,
Marcel Holtmanndbece372014-07-04 18:11:55 +020097 MGMT_OP_SET_EXTERNAL_CONFIG,
Marcel Holtmann9713c172014-07-06 12:11:15 +020098 MGMT_OP_SET_PUBLIC_ADDRESS,
Jakub Pawlowski66ea9422014-12-05 10:55:59 +010099 MGMT_OP_START_SERVICE_DISCOVERY,
Marcel Holtmann4f0f1552015-03-14 22:43:19 -0700100 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
Marcel Holtmann96f14742015-03-14 19:27:57 -0700101 MGMT_OP_READ_EXT_INDEX_LIST,
Marcel Holtmannd3d53052015-03-14 20:53:25 -0700102 MGMT_OP_READ_ADV_FEATURES,
Arman Uguray24b4f382015-03-23 15:57:12 -0700103 MGMT_OP_ADD_ADVERTISING,
Arman Ugurayda9293352015-03-23 15:57:13 -0700104 MGMT_OP_REMOVE_ADVERTISING,
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200105};
106
107static const u16 mgmt_events[] = {
108 MGMT_EV_CONTROLLER_ERROR,
109 MGMT_EV_INDEX_ADDED,
110 MGMT_EV_INDEX_REMOVED,
111 MGMT_EV_NEW_SETTINGS,
112 MGMT_EV_CLASS_OF_DEV_CHANGED,
113 MGMT_EV_LOCAL_NAME_CHANGED,
114 MGMT_EV_NEW_LINK_KEY,
115 MGMT_EV_NEW_LONG_TERM_KEY,
116 MGMT_EV_DEVICE_CONNECTED,
117 MGMT_EV_DEVICE_DISCONNECTED,
118 MGMT_EV_CONNECT_FAILED,
119 MGMT_EV_PIN_CODE_REQUEST,
120 MGMT_EV_USER_CONFIRM_REQUEST,
121 MGMT_EV_USER_PASSKEY_REQUEST,
122 MGMT_EV_AUTH_FAILED,
123 MGMT_EV_DEVICE_FOUND,
124 MGMT_EV_DISCOVERING,
125 MGMT_EV_DEVICE_BLOCKED,
126 MGMT_EV_DEVICE_UNBLOCKED,
127 MGMT_EV_DEVICE_UNPAIRED,
Johan Hedberg92a25252012-09-06 18:39:26 +0300128 MGMT_EV_PASSKEY_NOTIFY,
Marcel Holtmann1b60ef22014-02-21 21:35:30 -0800129 MGMT_EV_NEW_IRK,
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700130 MGMT_EV_NEW_CSRK,
Marcel Holtmann8afef092014-06-29 22:28:34 +0200131 MGMT_EV_DEVICE_ADDED,
132 MGMT_EV_DEVICE_REMOVED,
Andre Guedesffb5a8272014-07-01 18:10:11 -0300133 MGMT_EV_NEW_CONN_PARAM,
Marcel Holtmann0602a8a2014-07-02 21:30:54 +0200134 MGMT_EV_UNCONF_INDEX_ADDED,
Marcel Holtmannedd38962014-07-02 21:30:55 +0200135 MGMT_EV_UNCONF_INDEX_REMOVED,
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200136 MGMT_EV_NEW_CONFIG_OPTIONS,
Marcel Holtmannced85542015-03-14 19:27:56 -0700137 MGMT_EV_EXT_INDEX_ADDED,
138 MGMT_EV_EXT_INDEX_REMOVED,
Marcel Holtmann72000df2015-03-16 16:11:21 -0700139 MGMT_EV_LOCAL_OOB_DATA_UPDATED,
Arman Uguray24b4f382015-03-23 15:57:12 -0700140 MGMT_EV_ADVERTISING_ADDED,
141 MGMT_EV_ADVERTISING_REMOVED,
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200142};
143
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700144static const u16 mgmt_untrusted_commands[] = {
145 MGMT_OP_READ_INDEX_LIST,
146 MGMT_OP_READ_INFO,
147 MGMT_OP_READ_UNCONF_INDEX_LIST,
148 MGMT_OP_READ_CONFIG_INFO,
149 MGMT_OP_READ_EXT_INDEX_LIST,
150};
151
152static const u16 mgmt_untrusted_events[] = {
153 MGMT_EV_INDEX_ADDED,
154 MGMT_EV_INDEX_REMOVED,
155 MGMT_EV_NEW_SETTINGS,
156 MGMT_EV_CLASS_OF_DEV_CHANGED,
157 MGMT_EV_LOCAL_NAME_CHANGED,
158 MGMT_EV_UNCONF_INDEX_ADDED,
159 MGMT_EV_UNCONF_INDEX_REMOVED,
160 MGMT_EV_NEW_CONFIG_OPTIONS,
161 MGMT_EV_EXT_INDEX_ADDED,
162 MGMT_EV_EXT_INDEX_REMOVED,
163};
164
Marcel Holtmann17b02e62012-03-01 14:32:37 -0800165#define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)
Johan Hedberg7d785252011-12-15 00:47:39 +0200166
Johan Hedbergd25b78e2015-01-27 12:55:52 +0200167#define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
168 "\x00\x00\x00\x00\x00\x00\x00\x00"
169
Johan Hedbergca69b792011-11-11 18:10:00 +0200170/* HCI to MGMT error code conversion table */
171static u8 mgmt_status_table[] = {
172 MGMT_STATUS_SUCCESS,
173 MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */
174 MGMT_STATUS_NOT_CONNECTED, /* No Connection */
175 MGMT_STATUS_FAILED, /* Hardware Failure */
176 MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */
177 MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */
Johan Hedbergeadd6632014-01-13 17:15:53 +0200178 MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */
Johan Hedbergca69b792011-11-11 18:10:00 +0200179 MGMT_STATUS_NO_RESOURCES, /* Memory Full */
180 MGMT_STATUS_TIMEOUT, /* Connection Timeout */
181 MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */
182 MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */
183 MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */
184 MGMT_STATUS_BUSY, /* Command Disallowed */
185 MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */
186 MGMT_STATUS_REJECTED, /* Rejected Security */
187 MGMT_STATUS_REJECTED, /* Rejected Personal */
188 MGMT_STATUS_TIMEOUT, /* Host Timeout */
189 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */
190 MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */
191 MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */
192 MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */
193 MGMT_STATUS_DISCONNECTED, /* OE Power Off */
194 MGMT_STATUS_DISCONNECTED, /* Connection Terminated */
195 MGMT_STATUS_BUSY, /* Repeated Attempts */
196 MGMT_STATUS_REJECTED, /* Pairing Not Allowed */
197 MGMT_STATUS_FAILED, /* Unknown LMP PDU */
198 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */
199 MGMT_STATUS_REJECTED, /* SCO Offset Rejected */
200 MGMT_STATUS_REJECTED, /* SCO Interval Rejected */
201 MGMT_STATUS_REJECTED, /* Air Mode Rejected */
202 MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */
203 MGMT_STATUS_FAILED, /* Unspecified Error */
204 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */
205 MGMT_STATUS_FAILED, /* Role Change Not Allowed */
206 MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */
207 MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */
208 MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */
209 MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */
210 MGMT_STATUS_FAILED, /* Unit Link Key Used */
211 MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */
212 MGMT_STATUS_TIMEOUT, /* Instant Passed */
213 MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */
214 MGMT_STATUS_FAILED, /* Transaction Collision */
215 MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */
216 MGMT_STATUS_REJECTED, /* QoS Rejected */
217 MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */
218 MGMT_STATUS_REJECTED, /* Insufficient Security */
219 MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */
220 MGMT_STATUS_BUSY, /* Role Switch Pending */
221 MGMT_STATUS_FAILED, /* Slot Violation */
222 MGMT_STATUS_FAILED, /* Role Switch Failed */
223 MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */
224 MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */
225 MGMT_STATUS_BUSY, /* Host Busy Pairing */
226 MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */
227 MGMT_STATUS_BUSY, /* Controller Busy */
228 MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */
229 MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */
230 MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */
231 MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */
232 MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */
233};
234
235static u8 mgmt_status(u8 hci_status)
236{
237 if (hci_status < ARRAY_SIZE(mgmt_status_table))
238 return mgmt_status_table[hci_status];
239
240 return MGMT_STATUS_FAILED;
241}
242
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700243static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data,
244 u16 len, int flag)
Marcel Holtmannf9207332015-03-14 19:27:55 -0700245{
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700246 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
247 flag, NULL);
Marcel Holtmannf9207332015-03-14 19:27:55 -0700248}
249
Marcel Holtmann72000df2015-03-16 16:11:21 -0700250static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data,
251 u16 len, int flag, struct sock *skip_sk)
252{
253 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
254 flag, skip_sk);
255}
256
Marcel Holtmannf6b77122015-03-14 19:28:05 -0700257static int mgmt_generic_event(u16 event, struct hci_dev *hdev, void *data,
258 u16 len, struct sock *skip_sk)
259{
260 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
261 HCI_MGMT_GENERIC_EVENTS, skip_sk);
262}
263
Johan Hedberg7a00ff42015-03-06 21:08:56 +0200264static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len,
265 struct sock *skip_sk)
266{
267 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700268 HCI_SOCK_TRUSTED, skip_sk);
Johan Hedberg7a00ff42015-03-06 21:08:56 +0200269}
270
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300271static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
272 u16 data_len)
Johan Hedberga38528f2011-01-22 06:46:43 +0200273{
274 struct mgmt_rp_read_version rp;
275
276 BT_DBG("sock %p", sk);
277
278 rp.version = MGMT_VERSION;
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700279 rp.revision = cpu_to_le16(MGMT_REVISION);
Johan Hedberga38528f2011-01-22 06:46:43 +0200280
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200281 return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0,
282 &rp, sizeof(rp));
Johan Hedberga38528f2011-01-22 06:46:43 +0200283}
284
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300285static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
286 u16 data_len)
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200287{
288 struct mgmt_rp_read_commands *rp;
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700289 u16 num_commands, num_events;
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200290 size_t rp_size;
291 int i, err;
292
293 BT_DBG("sock %p", sk);
294
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700295 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
296 num_commands = ARRAY_SIZE(mgmt_commands);
297 num_events = ARRAY_SIZE(mgmt_events);
298 } else {
299 num_commands = ARRAY_SIZE(mgmt_untrusted_commands);
300 num_events = ARRAY_SIZE(mgmt_untrusted_events);
301 }
302
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200303 rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16));
304
305 rp = kmalloc(rp_size, GFP_KERNEL);
306 if (!rp)
307 return -ENOMEM;
308
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700309 rp->num_commands = cpu_to_le16(num_commands);
310 rp->num_events = cpu_to_le16(num_events);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200311
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700312 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
313 __le16 *opcode = rp->opcodes;
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200314
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700315 for (i = 0; i < num_commands; i++, opcode++)
316 put_unaligned_le16(mgmt_commands[i], opcode);
317
318 for (i = 0; i < num_events; i++, opcode++)
319 put_unaligned_le16(mgmt_events[i], opcode);
320 } else {
321 __le16 *opcode = rp->opcodes;
322
323 for (i = 0; i < num_commands; i++, opcode++)
324 put_unaligned_le16(mgmt_untrusted_commands[i], opcode);
325
326 for (i = 0; i < num_events; i++, opcode++)
327 put_unaligned_le16(mgmt_untrusted_events[i], opcode);
328 }
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200329
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200330 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0,
331 rp, rp_size);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200332 kfree(rp);
333
334 return err;
335}
336
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300337static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
338 u16 data_len)
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200339{
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200340 struct mgmt_rp_read_index_list *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200341 struct hci_dev *d;
Johan Hedberga38528f2011-01-22 06:46:43 +0200342 size_t rp_len;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200343 u16 count;
Johan Hedberg476e44c2012-10-19 20:10:46 +0300344 int err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200345
346 BT_DBG("sock %p", sk);
347
348 read_lock(&hci_dev_list_lock);
349
350 count = 0;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +0300351 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200352 if (d->dev_type == HCI_BREDR &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700353 !hci_dev_test_flag(d, HCI_UNCONFIGURED))
Marcel Holtmann1514b892013-10-06 08:25:01 -0700354 count++;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200355 }
356
Johan Hedberga38528f2011-01-22 06:46:43 +0200357 rp_len = sizeof(*rp) + (2 * count);
358 rp = kmalloc(rp_len, GFP_ATOMIC);
359 if (!rp) {
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100360 read_unlock(&hci_dev_list_lock);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200361 return -ENOMEM;
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100362 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200363
Johan Hedberg476e44c2012-10-19 20:10:46 +0300364 count = 0;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200365 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700366 if (hci_dev_test_flag(d, HCI_SETUP) ||
367 hci_dev_test_flag(d, HCI_CONFIG) ||
368 hci_dev_test_flag(d, HCI_USER_CHANNEL))
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200369 continue;
370
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200371 /* Devices marked as raw-only are neither configured
372 * nor unconfigured controllers.
373 */
374 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
Marcel Holtmann0736cfa2013-08-26 21:40:51 -0700375 continue;
376
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200377 if (d->dev_type == HCI_BREDR &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700378 !hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
Marcel Holtmann1514b892013-10-06 08:25:01 -0700379 rp->index[count++] = cpu_to_le16(d->id);
380 BT_DBG("Added hci%u", d->id);
381 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200382 }
383
Johan Hedberg476e44c2012-10-19 20:10:46 +0300384 rp->num_controllers = cpu_to_le16(count);
385 rp_len = sizeof(*rp) + (2 * count);
386
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200387 read_unlock(&hci_dev_list_lock);
388
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200389 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST,
390 0, rp, rp_len);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200391
Johan Hedberga38528f2011-01-22 06:46:43 +0200392 kfree(rp);
393
394 return err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200395}
396
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200397static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
398 void *data, u16 data_len)
399{
400 struct mgmt_rp_read_unconf_index_list *rp;
401 struct hci_dev *d;
402 size_t rp_len;
403 u16 count;
404 int err;
405
406 BT_DBG("sock %p", sk);
407
408 read_lock(&hci_dev_list_lock);
409
410 count = 0;
411 list_for_each_entry(d, &hci_dev_list, list) {
412 if (d->dev_type == HCI_BREDR &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700413 hci_dev_test_flag(d, HCI_UNCONFIGURED))
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200414 count++;
415 }
416
417 rp_len = sizeof(*rp) + (2 * count);
418 rp = kmalloc(rp_len, GFP_ATOMIC);
419 if (!rp) {
420 read_unlock(&hci_dev_list_lock);
421 return -ENOMEM;
422 }
423
424 count = 0;
425 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700426 if (hci_dev_test_flag(d, HCI_SETUP) ||
427 hci_dev_test_flag(d, HCI_CONFIG) ||
428 hci_dev_test_flag(d, HCI_USER_CHANNEL))
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200429 continue;
430
431 /* Devices marked as raw-only are neither configured
432 * nor unconfigured controllers.
433 */
434 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
435 continue;
436
437 if (d->dev_type == HCI_BREDR &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700438 hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200439 rp->index[count++] = cpu_to_le16(d->id);
440 BT_DBG("Added hci%u", d->id);
441 }
442 }
443
444 rp->num_controllers = cpu_to_le16(count);
445 rp_len = sizeof(*rp) + (2 * count);
446
447 read_unlock(&hci_dev_list_lock);
448
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200449 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
450 MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len);
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200451
452 kfree(rp);
453
454 return err;
455}
456
Marcel Holtmann96f14742015-03-14 19:27:57 -0700457static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev,
458 void *data, u16 data_len)
459{
460 struct mgmt_rp_read_ext_index_list *rp;
461 struct hci_dev *d;
462 size_t rp_len;
463 u16 count;
464 int err;
465
466 BT_DBG("sock %p", sk);
467
468 read_lock(&hci_dev_list_lock);
469
470 count = 0;
471 list_for_each_entry(d, &hci_dev_list, list) {
472 if (d->dev_type == HCI_BREDR || d->dev_type == HCI_AMP)
473 count++;
474 }
475
476 rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
477 rp = kmalloc(rp_len, GFP_ATOMIC);
478 if (!rp) {
479 read_unlock(&hci_dev_list_lock);
480 return -ENOMEM;
481 }
482
483 count = 0;
484 list_for_each_entry(d, &hci_dev_list, list) {
485 if (hci_dev_test_flag(d, HCI_SETUP) ||
486 hci_dev_test_flag(d, HCI_CONFIG) ||
487 hci_dev_test_flag(d, HCI_USER_CHANNEL))
488 continue;
489
490 /* Devices marked as raw-only are neither configured
491 * nor unconfigured controllers.
492 */
493 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
494 continue;
495
496 if (d->dev_type == HCI_BREDR) {
497 if (hci_dev_test_flag(d, HCI_UNCONFIGURED))
498 rp->entry[count].type = 0x01;
499 else
500 rp->entry[count].type = 0x00;
501 } else if (d->dev_type == HCI_AMP) {
502 rp->entry[count].type = 0x02;
503 } else {
504 continue;
505 }
506
507 rp->entry[count].bus = d->bus;
508 rp->entry[count++].index = cpu_to_le16(d->id);
509 BT_DBG("Added hci%u", d->id);
510 }
511
512 rp->num_controllers = cpu_to_le16(count);
513 rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
514
515 read_unlock(&hci_dev_list_lock);
516
517 /* If this command is called at least once, then all the
518 * default index and unconfigured index events are disabled
519 * and from now on only extended index events are used.
520 */
521 hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS);
522 hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS);
523 hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
524
525 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
526 MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, rp_len);
527
528 kfree(rp);
529
530 return err;
531}
532
Marcel Holtmanndbece372014-07-04 18:11:55 +0200533static bool is_configured(struct hci_dev *hdev)
534{
535 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700536 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
Marcel Holtmanndbece372014-07-04 18:11:55 +0200537 return false;
538
539 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
540 !bacmp(&hdev->public_addr, BDADDR_ANY))
541 return false;
542
543 return true;
544}
545
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200546static __le32 get_missing_options(struct hci_dev *hdev)
547{
548 u32 options = 0;
549
Marcel Holtmanndbece372014-07-04 18:11:55 +0200550 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700551 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200552 options |= MGMT_OPTION_EXTERNAL_CONFIG;
553
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200554 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
555 !bacmp(&hdev->public_addr, BDADDR_ANY))
556 options |= MGMT_OPTION_PUBLIC_ADDRESS;
557
558 return cpu_to_le32(options);
559}
560
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200561static int new_options(struct hci_dev *hdev, struct sock *skip)
562{
563 __le32 options = get_missing_options(hdev);
564
Marcel Holtmannf6b77122015-03-14 19:28:05 -0700565 return mgmt_generic_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options,
566 sizeof(options), skip);
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200567}
568
Marcel Holtmanndbece372014-07-04 18:11:55 +0200569static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
570{
571 __le32 options = get_missing_options(hdev);
572
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200573 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options,
574 sizeof(options));
Marcel Holtmanndbece372014-07-04 18:11:55 +0200575}
576
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200577static int read_config_info(struct sock *sk, struct hci_dev *hdev,
578 void *data, u16 data_len)
579{
580 struct mgmt_rp_read_config_info rp;
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200581 u32 options = 0;
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200582
583 BT_DBG("sock %p %s", sk, hdev->name);
584
585 hci_dev_lock(hdev);
586
587 memset(&rp, 0, sizeof(rp));
588 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200589
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200590 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
591 options |= MGMT_OPTION_EXTERNAL_CONFIG;
592
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200593 if (hdev->set_bdaddr)
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200594 options |= MGMT_OPTION_PUBLIC_ADDRESS;
595
596 rp.supported_options = cpu_to_le32(options);
597 rp.missing_options = get_missing_options(hdev);
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200598
599 hci_dev_unlock(hdev);
600
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200601 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0,
602 &rp, sizeof(rp));
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200603}
604
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200605static u32 get_supported_settings(struct hci_dev *hdev)
Johan Hedberg03811012010-12-08 00:21:06 +0200606{
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200607 u32 settings = 0;
Johan Hedberg03811012010-12-08 00:21:06 +0200608
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200609 settings |= MGMT_SETTING_POWERED;
Johan Hedbergb2939472014-07-30 09:22:23 +0300610 settings |= MGMT_SETTING_BONDABLE;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800611 settings |= MGMT_SETTING_DEBUG_KEYS;
Johan Hedberg3742abf2014-07-08 16:07:34 +0300612 settings |= MGMT_SETTING_CONNECTABLE;
613 settings |= MGMT_SETTING_DISCOVERABLE;
Johan Hedberg03811012010-12-08 00:21:06 +0200614
Andre Guedesed3fa312012-07-24 15:03:46 -0300615 if (lmp_bredr_capable(hdev)) {
Johan Hedberg1a47aee2013-03-15 17:07:06 -0500616 if (hdev->hci_ver >= BLUETOOTH_VER_1_2)
617 settings |= MGMT_SETTING_FAST_CONNECTABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200618 settings |= MGMT_SETTING_BREDR;
619 settings |= MGMT_SETTING_LINK_SECURITY;
Marcel Holtmanna82974c2013-10-11 09:48:47 -0700620
621 if (lmp_ssp_capable(hdev)) {
622 settings |= MGMT_SETTING_SSP;
623 settings |= MGMT_SETTING_HS;
624 }
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800625
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -0800626 if (lmp_sc_capable(hdev))
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800627 settings |= MGMT_SETTING_SECURE_CONN;
Marcel Holtmann848566b2013-10-01 22:59:22 -0700628 }
Marcel Holtmannd7b7e792012-02-20 21:47:49 +0100629
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300630 if (lmp_le_capable(hdev)) {
Marcel Holtmann9d428202012-05-03 07:12:31 +0200631 settings |= MGMT_SETTING_LE;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300632 settings |= MGMT_SETTING_ADVERTISING;
Johan Hedberga3209692014-05-26 11:23:35 +0300633 settings |= MGMT_SETTING_SECURE_CONN;
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200634 settings |= MGMT_SETTING_PRIVACY;
Marcel Holtmann93690c22015-03-06 10:11:21 -0800635 settings |= MGMT_SETTING_STATIC_ADDRESS;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300636 }
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200637
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200638 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
639 hdev->set_bdaddr)
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200640 settings |= MGMT_SETTING_CONFIGURATION;
641
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200642 return settings;
643}
Johan Hedbergebc99fe2011-01-04 11:54:26 +0200644
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200645static u32 get_current_settings(struct hci_dev *hdev)
646{
647 u32 settings = 0;
Johan Hedbergdc4fe302011-03-16 14:29:36 +0200648
Johan Hedbergf1f0eb02012-02-21 17:15:41 +0200649 if (hdev_is_powered(hdev))
Marcel Holtmannf0d4b782012-02-21 12:14:25 +0100650 settings |= MGMT_SETTING_POWERED;
651
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700652 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200653 settings |= MGMT_SETTING_CONNECTABLE;
654
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700655 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
Johan Hedberg1a4d3c42013-03-15 17:07:08 -0500656 settings |= MGMT_SETTING_FAST_CONNECTABLE;
657
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700658 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200659 settings |= MGMT_SETTING_DISCOVERABLE;
660
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700661 if (hci_dev_test_flag(hdev, HCI_BONDABLE))
Johan Hedbergb2939472014-07-30 09:22:23 +0300662 settings |= MGMT_SETTING_BONDABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200663
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700664 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200665 settings |= MGMT_SETTING_BREDR;
666
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700667 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200668 settings |= MGMT_SETTING_LE;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200669
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700670 if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200671 settings |= MGMT_SETTING_LINK_SECURITY;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200672
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700673 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200674 settings |= MGMT_SETTING_SSP;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200675
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700676 if (hci_dev_test_flag(hdev, HCI_HS_ENABLED))
Johan Hedberg6d80dfd2012-02-20 23:50:38 +0200677 settings |= MGMT_SETTING_HS;
678
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700679 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300680 settings |= MGMT_SETTING_ADVERTISING;
681
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700682 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800683 settings |= MGMT_SETTING_SECURE_CONN;
684
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700685 if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS))
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800686 settings |= MGMT_SETTING_DEBUG_KEYS;
687
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700688 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200689 settings |= MGMT_SETTING_PRIVACY;
690
Marcel Holtmann93690c22015-03-06 10:11:21 -0800691 /* The current setting for static address has two purposes. The
692 * first is to indicate if the static address will be used and
693 * the second is to indicate if it is actually set.
694 *
695 * This means if the static address is not configured, this flag
Marcel Holtmann08dc0e92015-03-25 18:32:13 -0700696 * will never be set. If the address is configured, then if the
Marcel Holtmann93690c22015-03-06 10:11:21 -0800697 * address is actually used decides if the flag is set or not.
698 *
699 * For single mode LE only controllers and dual-mode controllers
700 * with BR/EDR disabled, the existence of the static address will
701 * be evaluated.
702 */
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -0700703 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700704 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
Marcel Holtmann93690c22015-03-06 10:11:21 -0800705 !bacmp(&hdev->bdaddr, BDADDR_ANY)) {
706 if (bacmp(&hdev->static_addr, BDADDR_ANY))
707 settings |= MGMT_SETTING_STATIC_ADDRESS;
708 }
709
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200710 return settings;
Johan Hedbergc542a062011-01-26 13:11:03 +0200711}
712
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300713#define PNP_INFO_SVCLASS_ID 0x1200
714
Johan Hedberg213202e2013-01-27 00:31:33 +0200715static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
716{
717 u8 *ptr = data, *uuids_start = NULL;
718 struct bt_uuid *uuid;
719
720 if (len < 4)
721 return ptr;
722
723 list_for_each_entry(uuid, &hdev->uuids, list) {
724 u16 uuid16;
725
726 if (uuid->size != 16)
727 continue;
728
729 uuid16 = get_unaligned_le16(&uuid->uuid[12]);
730 if (uuid16 < 0x1100)
731 continue;
732
733 if (uuid16 == PNP_INFO_SVCLASS_ID)
734 continue;
735
736 if (!uuids_start) {
737 uuids_start = ptr;
738 uuids_start[0] = 1;
739 uuids_start[1] = EIR_UUID16_ALL;
740 ptr += 2;
741 }
742
743 /* Stop if not enough space to put next UUID */
744 if ((ptr - data) + sizeof(u16) > len) {
745 uuids_start[1] = EIR_UUID16_SOME;
746 break;
747 }
748
749 *ptr++ = (uuid16 & 0x00ff);
750 *ptr++ = (uuid16 & 0xff00) >> 8;
751 uuids_start[0] += sizeof(uuid16);
752 }
753
754 return ptr;
755}
756
Johan Hedbergcdf19632013-01-27 00:31:34 +0200757static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
758{
759 u8 *ptr = data, *uuids_start = NULL;
760 struct bt_uuid *uuid;
761
762 if (len < 6)
763 return ptr;
764
765 list_for_each_entry(uuid, &hdev->uuids, list) {
766 if (uuid->size != 32)
767 continue;
768
769 if (!uuids_start) {
770 uuids_start = ptr;
771 uuids_start[0] = 1;
772 uuids_start[1] = EIR_UUID32_ALL;
773 ptr += 2;
774 }
775
776 /* Stop if not enough space to put next UUID */
777 if ((ptr - data) + sizeof(u32) > len) {
778 uuids_start[1] = EIR_UUID32_SOME;
779 break;
780 }
781
782 memcpy(ptr, &uuid->uuid[12], sizeof(u32));
783 ptr += sizeof(u32);
784 uuids_start[0] += sizeof(u32);
785 }
786
787 return ptr;
788}
789
Johan Hedbergc00d5752013-01-27 00:31:35 +0200790static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
791{
792 u8 *ptr = data, *uuids_start = NULL;
793 struct bt_uuid *uuid;
794
795 if (len < 18)
796 return ptr;
797
798 list_for_each_entry(uuid, &hdev->uuids, list) {
799 if (uuid->size != 128)
800 continue;
801
802 if (!uuids_start) {
803 uuids_start = ptr;
804 uuids_start[0] = 1;
805 uuids_start[1] = EIR_UUID128_ALL;
806 ptr += 2;
807 }
808
809 /* Stop if not enough space to put next UUID */
810 if ((ptr - data) + 16 > len) {
811 uuids_start[1] = EIR_UUID128_SOME;
812 break;
813 }
814
815 memcpy(ptr, uuid->uuid, 16);
816 ptr += 16;
817 uuids_start[0] += 16;
818 }
819
820 return ptr;
821}
822
Johan Hedberg333ae952015-03-17 13:48:47 +0200823static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev)
824{
825 return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev);
826}
827
Johan Hedberg333ae952015-03-17 13:48:47 +0200828static struct mgmt_pending_cmd *pending_find_data(u16 opcode,
829 struct hci_dev *hdev,
830 const void *data)
831{
832 return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data);
833}
834
Florian Grandel91aa9bb2015-06-18 03:16:36 +0200835static u8 get_current_adv_instance(struct hci_dev *hdev)
836{
837 /* The "Set Advertising" setting supersedes the "Add Advertising"
838 * setting. Here we set the advertising data based on which
839 * setting was set. When neither apply, default to the global settings,
840 * represented by instance "0".
841 */
842 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
843 !hci_dev_test_flag(hdev, HCI_ADVERTISING))
Florian Grandel3ff37e62015-06-18 03:16:39 +0200844 return hdev->cur_adv_instance;
Florian Grandel91aa9bb2015-06-18 03:16:36 +0200845
846 return 0x00;
847}
848
Arman Uguray4117ed72015-03-23 15:57:14 -0700849static u8 create_default_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700850{
Marcel Holtmann7a5f4992013-10-16 00:16:49 -0700851 u8 ad_len = 0;
852 size_t name_len;
853
854 name_len = strlen(hdev->dev_name);
855 if (name_len > 0) {
856 size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2;
857
858 if (name_len > max_len) {
859 name_len = max_len;
860 ptr[1] = EIR_NAME_SHORT;
861 } else
862 ptr[1] = EIR_NAME_COMPLETE;
863
864 ptr[0] = name_len + 1;
865
866 memcpy(ptr + 2, hdev->dev_name, name_len);
867
868 ad_len += (name_len + 2);
869 ptr += (name_len + 2);
870 }
871
872 return ad_len;
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700873}
874
Florian Grandelca21fbe2015-06-18 03:16:43 +0200875static u8 create_instance_scan_rsp_data(struct hci_dev *hdev, u8 instance,
876 u8 *ptr)
Arman Uguray4117ed72015-03-23 15:57:14 -0700877{
Florian Grandelca21fbe2015-06-18 03:16:43 +0200878 struct adv_info *adv_instance;
879
880 adv_instance = hci_find_adv_instance(hdev, instance);
881 if (!adv_instance)
882 return 0;
883
Arman Uguray4117ed72015-03-23 15:57:14 -0700884 /* TODO: Set the appropriate entries based on advertising instance flags
885 * here once flags other than 0 are supported.
886 */
Florian Grandelca21fbe2015-06-18 03:16:43 +0200887 memcpy(ptr, adv_instance->scan_rsp_data,
888 adv_instance->scan_rsp_len);
Arman Uguray4117ed72015-03-23 15:57:14 -0700889
Florian Grandelca21fbe2015-06-18 03:16:43 +0200890 return adv_instance->scan_rsp_len;
Arman Uguray4117ed72015-03-23 15:57:14 -0700891}
892
Florian Grandelefae0022015-06-18 03:16:37 +0200893static void update_inst_scan_rsp_data(struct hci_request *req, u8 instance)
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700894{
895 struct hci_dev *hdev = req->hdev;
896 struct hci_cp_le_set_scan_rsp_data cp;
897 u8 len;
898
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700899 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700900 return;
901
902 memset(&cp, 0, sizeof(cp));
903
Arman Uguray4117ed72015-03-23 15:57:14 -0700904 if (instance)
Florian Grandelca21fbe2015-06-18 03:16:43 +0200905 len = create_instance_scan_rsp_data(hdev, instance, cp.data);
Arman Uguray4117ed72015-03-23 15:57:14 -0700906 else
907 len = create_default_scan_rsp_data(hdev, cp.data);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700908
Johan Hedbergeb438b52013-10-16 15:31:07 +0300909 if (hdev->scan_rsp_data_len == len &&
Arman Uguray4117ed72015-03-23 15:57:14 -0700910 !memcmp(cp.data, hdev->scan_rsp_data, len))
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700911 return;
912
Johan Hedbergeb438b52013-10-16 15:31:07 +0300913 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
914 hdev->scan_rsp_data_len = len;
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700915
916 cp.length = len;
917
918 hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp);
919}
920
Arman Uguray4117ed72015-03-23 15:57:14 -0700921static void update_scan_rsp_data(struct hci_request *req)
922{
Florian Grandelefae0022015-06-18 03:16:37 +0200923 update_inst_scan_rsp_data(req, get_current_adv_instance(req->hdev));
Arman Uguray4117ed72015-03-23 15:57:14 -0700924}
925
Johan Hedberg9a43e252013-10-20 19:00:07 +0300926static u8 get_adv_discov_flags(struct hci_dev *hdev)
927{
Johan Hedberg3b0602c2015-03-06 21:08:55 +0200928 struct mgmt_pending_cmd *cmd;
Johan Hedberg9a43e252013-10-20 19:00:07 +0300929
930 /* If there's a pending mgmt command the flags will not yet have
931 * their final values, so check for this first.
932 */
Johan Hedberg333ae952015-03-17 13:48:47 +0200933 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg9a43e252013-10-20 19:00:07 +0300934 if (cmd) {
935 struct mgmt_mode *cp = cmd->param;
936 if (cp->val == 0x01)
937 return LE_AD_GENERAL;
938 else if (cp->val == 0x02)
939 return LE_AD_LIMITED;
940 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700941 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300942 return LE_AD_LIMITED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700943 else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300944 return LE_AD_GENERAL;
945 }
946
947 return 0;
948}
949
Arman Uguraye7a685d2015-03-25 18:53:40 -0700950static bool get_connectable(struct hci_dev *hdev)
951{
952 struct mgmt_pending_cmd *cmd;
953
954 /* If there's a pending mgmt command the flag will not yet have
955 * it's final value, so check for this first.
956 */
957 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
958 if (cmd) {
959 struct mgmt_mode *cp = cmd->param;
960
961 return cp->val;
962 }
963
964 return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
965}
966
967static u32 get_adv_instance_flags(struct hci_dev *hdev, u8 instance)
968{
969 u32 flags;
Florian Grandel411b4122015-06-18 03:16:40 +0200970 struct adv_info *adv_instance;
Arman Uguraye7a685d2015-03-25 18:53:40 -0700971
Florian Grandelbea28e62015-06-18 03:16:41 +0200972 if (instance == 0x00) {
973 /* Instance 0 always manages the "Tx Power" and "Flags"
974 * fields
975 */
976 flags = MGMT_ADV_FLAG_TX_POWER | MGMT_ADV_FLAG_MANAGED_FLAGS;
Arman Uguraye7a685d2015-03-25 18:53:40 -0700977
Florian Grandelbea28e62015-06-18 03:16:41 +0200978 /* For instance 0, the HCI_ADVERTISING_CONNECTABLE setting
979 * corresponds to the "connectable" instance flag.
980 */
981 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE))
982 flags |= MGMT_ADV_FLAG_CONNECTABLE;
Florian Grandel411b4122015-06-18 03:16:40 +0200983
Florian Grandelbea28e62015-06-18 03:16:41 +0200984 return flags;
Florian Grandel411b4122015-06-18 03:16:40 +0200985 }
Arman Uguraye7a685d2015-03-25 18:53:40 -0700986
Florian Grandelbea28e62015-06-18 03:16:41 +0200987 adv_instance = hci_find_adv_instance(hdev, instance);
Arman Uguraye7a685d2015-03-25 18:53:40 -0700988
Florian Grandelbea28e62015-06-18 03:16:41 +0200989 /* Return 0 when we got an invalid instance identifier. */
990 if (!adv_instance)
991 return 0;
Arman Uguraye7a685d2015-03-25 18:53:40 -0700992
Florian Grandelbea28e62015-06-18 03:16:41 +0200993 return adv_instance->flags;
Arman Uguraye7a685d2015-03-25 18:53:40 -0700994}
995
Florian Grandel7b683b72015-06-18 03:16:42 +0200996static u8 get_cur_adv_instance_scan_rsp_len(struct hci_dev *hdev)
Arman Ugurayc7d48832015-03-28 12:38:59 -0700997{
Florian Grandel7b683b72015-06-18 03:16:42 +0200998 u8 instance = get_current_adv_instance(hdev);
999 struct adv_info *adv_instance;
1000
1001 /* Ignore instance 0 */
1002 if (instance == 0x00)
1003 return 0;
1004
1005 adv_instance = hci_find_adv_instance(hdev, instance);
1006 if (!adv_instance)
Arman Ugurayc7d48832015-03-28 12:38:59 -07001007 return 0;
1008
1009 /* TODO: Take into account the "appearance" and "local-name" flags here.
1010 * These are currently being ignored as they are not supported.
1011 */
Florian Grandel7b683b72015-06-18 03:16:42 +02001012 return adv_instance->scan_rsp_len;
Arman Ugurayc7d48832015-03-28 12:38:59 -07001013}
1014
Arman Ugurayfdf51782015-03-25 18:53:46 -07001015static u8 create_instance_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
1016{
1017 u8 ad_len = 0, flags = 0;
1018 u32 instance_flags = get_adv_instance_flags(hdev, instance);
1019
1020 /* The Add Advertising command allows userspace to set both the general
1021 * and limited discoverable flags.
1022 */
1023 if (instance_flags & MGMT_ADV_FLAG_DISCOV)
1024 flags |= LE_AD_GENERAL;
1025
1026 if (instance_flags & MGMT_ADV_FLAG_LIMITED_DISCOV)
1027 flags |= LE_AD_LIMITED;
1028
1029 if (flags || (instance_flags & MGMT_ADV_FLAG_MANAGED_FLAGS)) {
1030 /* If a discovery flag wasn't provided, simply use the global
1031 * settings.
1032 */
1033 if (!flags)
1034 flags |= get_adv_discov_flags(hdev);
1035
1036 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1037 flags |= LE_AD_NO_BREDR;
1038
1039 /* If flags would still be empty, then there is no need to
1040 * include the "Flags" AD field".
1041 */
1042 if (flags) {
1043 ptr[0] = 0x02;
1044 ptr[1] = EIR_FLAGS;
1045 ptr[2] = flags;
1046
1047 ad_len += 3;
1048 ptr += 3;
1049 }
1050 }
1051
Marcel Holtmann38c8af62015-04-03 13:23:12 -07001052 if (instance) {
1053 memcpy(ptr, hdev->adv_instance.adv_data,
1054 hdev->adv_instance.adv_data_len);
1055
1056 ad_len += hdev->adv_instance.adv_data_len;
1057 ptr += hdev->adv_instance.adv_data_len;
1058 }
1059
Arman Ugurayfdf51782015-03-25 18:53:46 -07001060 /* Provide Tx Power only if we can provide a valid value for it */
1061 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID &&
1062 (instance_flags & MGMT_ADV_FLAG_TX_POWER)) {
1063 ptr[0] = 0x02;
1064 ptr[1] = EIR_TX_POWER;
1065 ptr[2] = (u8)hdev->adv_tx_power;
1066
1067 ad_len += 3;
1068 ptr += 3;
1069 }
1070
Arman Ugurayfdf51782015-03-25 18:53:46 -07001071 return ad_len;
1072}
1073
Florian Grandelefae0022015-06-18 03:16:37 +02001074static void update_inst_adv_data(struct hci_request *req, u8 instance)
Arman Ugurayfdf51782015-03-25 18:53:46 -07001075{
1076 struct hci_dev *hdev = req->hdev;
1077 struct hci_cp_le_set_adv_data cp;
1078 u8 len;
1079
1080 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1081 return;
1082
1083 memset(&cp, 0, sizeof(cp));
1084
1085 len = create_instance_adv_data(hdev, instance, cp.data);
1086
1087 /* There's nothing to do if the data hasn't changed */
1088 if (hdev->adv_data_len == len &&
1089 memcmp(cp.data, hdev->adv_data, len) == 0)
1090 return;
1091
1092 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1093 hdev->adv_data_len = len;
1094
1095 cp.length = len;
1096
1097 hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
1098}
1099
Arman Uguraye7a685d2015-03-25 18:53:40 -07001100static void update_adv_data(struct hci_request *req)
1101{
Florian Grandelefae0022015-06-18 03:16:37 +02001102 update_inst_adv_data(req, get_current_adv_instance(req->hdev));
Arman Uguray24b4f382015-03-23 15:57:12 -07001103}
1104
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001105int mgmt_update_adv_data(struct hci_dev *hdev)
1106{
1107 struct hci_request req;
1108
1109 hci_req_init(&req, hdev);
1110 update_adv_data(&req);
1111
1112 return hci_req_run(&req, NULL);
1113}
1114
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001115static void create_eir(struct hci_dev *hdev, u8 *data)
1116{
1117 u8 *ptr = data;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001118 size_t name_len;
1119
1120 name_len = strlen(hdev->dev_name);
1121
1122 if (name_len > 0) {
1123 /* EIR Data type */
1124 if (name_len > 48) {
1125 name_len = 48;
1126 ptr[1] = EIR_NAME_SHORT;
1127 } else
1128 ptr[1] = EIR_NAME_COMPLETE;
1129
1130 /* EIR Data length */
1131 ptr[0] = name_len + 1;
1132
1133 memcpy(ptr + 2, hdev->dev_name, name_len);
1134
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001135 ptr += (name_len + 2);
1136 }
1137
Johan Hedbergbbaf4442012-11-08 01:22:59 +01001138 if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) {
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -07001139 ptr[0] = 2;
1140 ptr[1] = EIR_TX_POWER;
1141 ptr[2] = (u8) hdev->inq_tx_power;
1142
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -07001143 ptr += 3;
1144 }
1145
Marcel Holtmann2b9be132012-03-11 19:32:12 -07001146 if (hdev->devid_source > 0) {
1147 ptr[0] = 9;
1148 ptr[1] = EIR_DEVICE_ID;
1149
1150 put_unaligned_le16(hdev->devid_source, ptr + 2);
1151 put_unaligned_le16(hdev->devid_vendor, ptr + 4);
1152 put_unaligned_le16(hdev->devid_product, ptr + 6);
1153 put_unaligned_le16(hdev->devid_version, ptr + 8);
1154
Marcel Holtmann2b9be132012-03-11 19:32:12 -07001155 ptr += 10;
1156 }
1157
Johan Hedberg213202e2013-01-27 00:31:33 +02001158 ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedbergcdf19632013-01-27 00:31:34 +02001159 ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedbergc00d5752013-01-27 00:31:35 +02001160 ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001161}
1162
Johan Hedberg890ea892013-03-15 17:06:52 -05001163static void update_eir(struct hci_request *req)
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001164{
Johan Hedberg890ea892013-03-15 17:06:52 -05001165 struct hci_dev *hdev = req->hdev;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001166 struct hci_cp_write_eir cp;
1167
Johan Hedberg504c8dc2012-02-23 13:30:41 +02001168 if (!hdev_is_powered(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05001169 return;
Johan Hedberg7770c4a2012-02-22 22:06:38 +02001170
Johan Hedberg976eb202012-10-24 21:12:01 +03001171 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05001172 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001173
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001174 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberg890ea892013-03-15 17:06:52 -05001175 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001176
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001177 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg890ea892013-03-15 17:06:52 -05001178 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001179
1180 memset(&cp, 0, sizeof(cp));
1181
1182 create_eir(hdev, cp.data);
1183
1184 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
Johan Hedberg890ea892013-03-15 17:06:52 -05001185 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001186
1187 memcpy(hdev->eir, cp.data, sizeof(cp.data));
1188
Johan Hedberg890ea892013-03-15 17:06:52 -05001189 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001190}
1191
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001192static u8 get_service_classes(struct hci_dev *hdev)
1193{
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -03001194 struct bt_uuid *uuid;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001195 u8 val = 0;
1196
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -03001197 list_for_each_entry(uuid, &hdev->uuids, list)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001198 val |= uuid->svc_hint;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001199
1200 return val;
1201}
1202
Johan Hedberg890ea892013-03-15 17:06:52 -05001203static void update_class(struct hci_request *req)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001204{
Johan Hedberg890ea892013-03-15 17:06:52 -05001205 struct hci_dev *hdev = req->hdev;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001206 u8 cod[3];
1207
1208 BT_DBG("%s", hdev->name);
1209
Johan Hedberg504c8dc2012-02-23 13:30:41 +02001210 if (!hdev_is_powered(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05001211 return;
Johan Hedberg7770c4a2012-02-22 22:06:38 +02001212
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001213 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedbergf87ea1d2013-10-19 23:38:17 +03001214 return;
1215
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001216 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg890ea892013-03-15 17:06:52 -05001217 return;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001218
1219 cod[0] = hdev->minor_class;
1220 cod[1] = hdev->major_class;
1221 cod[2] = get_service_classes(hdev);
1222
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001223 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
Marcel Holtmann6acd7db2013-10-15 06:33:53 -07001224 cod[1] |= 0x20;
1225
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001226 if (memcmp(cod, hdev->dev_class, 3) == 0)
Johan Hedberg890ea892013-03-15 17:06:52 -05001227 return;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001228
Johan Hedberg890ea892013-03-15 17:06:52 -05001229 hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001230}
1231
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001232static void disable_advertising(struct hci_request *req)
1233{
1234 u8 enable = 0x00;
1235
1236 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1237}
1238
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001239static void enable_advertising(struct hci_request *req)
1240{
1241 struct hci_dev *hdev = req->hdev;
1242 struct hci_cp_le_set_adv_param cp;
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001243 u8 own_addr_type, enable = 0x01;
Johan Hedberga4858cb2014-02-25 19:56:31 +02001244 bool connectable;
Arman Uguraye7a685d2015-03-25 18:53:40 -07001245 u8 instance;
1246 u32 flags;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001247
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001248 if (hci_conn_num(hdev, LE_LINK) > 0)
1249 return;
1250
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001251 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001252 disable_advertising(req);
1253
Johan Hedberg5ce194c2014-07-08 15:07:49 +03001254 /* Clear the HCI_LE_ADV bit temporarily so that the
Johan Hedberg8d972502014-02-28 12:54:14 +02001255 * hci_update_random_address knows that it's safe to go ahead
1256 * and write a new random address. The flag will be set back on
1257 * as soon as the SET_ADV_ENABLE HCI command completes.
1258 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001259 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Johan Hedberg8d972502014-02-28 12:54:14 +02001260
Arman Uguraye7a685d2015-03-25 18:53:40 -07001261 instance = get_current_adv_instance(hdev);
1262 flags = get_adv_instance_flags(hdev, instance);
Arman Ugurayfaccb952015-03-28 12:38:58 -07001263
1264 /* If the "connectable" instance flag was not set, then choose between
1265 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1266 */
1267 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1268 get_connectable(hdev);
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001269
Johan Hedberga4858cb2014-02-25 19:56:31 +02001270 /* Set require_privacy to true only when non-connectable
1271 * advertising is used. In that case it is fine to use a
1272 * non-resolvable private address.
1273 */
1274 if (hci_update_random_address(req, !connectable, &own_addr_type) < 0)
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001275 return;
1276
Marcel Holtmann41c90c12014-02-23 20:25:55 -08001277 memset(&cp, 0, sizeof(cp));
Georg Lukas628531c2014-07-26 13:59:57 +02001278 cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval);
1279 cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval);
Arman Ugurayc7d48832015-03-28 12:38:59 -07001280
1281 if (connectable)
1282 cp.type = LE_ADV_IND;
Florian Grandel7b683b72015-06-18 03:16:42 +02001283 else if (get_cur_adv_instance_scan_rsp_len(hdev))
Arman Ugurayc7d48832015-03-28 12:38:59 -07001284 cp.type = LE_ADV_SCAN_IND;
1285 else
1286 cp.type = LE_ADV_NONCONN_IND;
1287
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001288 cp.own_address_type = own_addr_type;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001289 cp.channel_map = hdev->le_adv_channel_map;
1290
1291 hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
1292
1293 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1294}
1295
Johan Hedberg7d785252011-12-15 00:47:39 +02001296static void service_cache_off(struct work_struct *work)
1297{
1298 struct hci_dev *hdev = container_of(work, struct hci_dev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001299 service_cache.work);
Johan Hedberg890ea892013-03-15 17:06:52 -05001300 struct hci_request req;
Johan Hedberg7d785252011-12-15 00:47:39 +02001301
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001302 if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg7d785252011-12-15 00:47:39 +02001303 return;
1304
Johan Hedberg890ea892013-03-15 17:06:52 -05001305 hci_req_init(&req, hdev);
1306
Johan Hedberg7d785252011-12-15 00:47:39 +02001307 hci_dev_lock(hdev);
1308
Johan Hedberg890ea892013-03-15 17:06:52 -05001309 update_eir(&req);
1310 update_class(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02001311
1312 hci_dev_unlock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -05001313
1314 hci_req_run(&req, NULL);
Johan Hedberg7d785252011-12-15 00:47:39 +02001315}
1316
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001317static void rpa_expired(struct work_struct *work)
1318{
1319 struct hci_dev *hdev = container_of(work, struct hci_dev,
1320 rpa_expired.work);
1321 struct hci_request req;
1322
1323 BT_DBG("");
1324
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001325 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001326
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001327 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001328 return;
1329
1330 /* The generation of a new RPA and programming it into the
1331 * controller happens in the enable_advertising() function.
1332 */
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001333 hci_req_init(&req, hdev);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001334 enable_advertising(&req);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001335 hci_req_run(&req, NULL);
1336}
1337
Johan Hedberg6a919082012-02-28 06:17:26 +02001338static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
Johan Hedberg7d785252011-12-15 00:47:39 +02001339{
Marcel Holtmann238be782015-03-13 02:11:06 -07001340 if (hci_dev_test_and_set_flag(hdev, HCI_MGMT))
Johan Hedberg6a919082012-02-28 06:17:26 +02001341 return;
1342
Johan Hedberg4f87da82012-03-02 19:55:56 +02001343 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001344 INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
Johan Hedberg7d785252011-12-15 00:47:39 +02001345
Johan Hedberg4f87da82012-03-02 19:55:56 +02001346 /* Non-mgmt controlled devices get this bit set
1347 * implicitly so that pairing works for them, however
1348 * for mgmt we require user-space to explicitly enable
1349 * it
1350 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001351 hci_dev_clear_flag(hdev, HCI_BONDABLE);
Johan Hedberg7d785252011-12-15 00:47:39 +02001352}
1353
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02001354static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001355 void *data, u16 data_len)
Johan Hedberg03811012010-12-08 00:21:06 +02001356{
1357 struct mgmt_rp_read_info rp;
Johan Hedberg03811012010-12-08 00:21:06 +02001358
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001359 BT_DBG("sock %p %s", sk, hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001360
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001361 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001362
Johan Hedberg03811012010-12-08 00:21:06 +02001363 memset(&rp, 0, sizeof(rp));
1364
Johan Hedberg03811012010-12-08 00:21:06 +02001365 bacpy(&rp.bdaddr, &hdev->bdaddr);
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001366
1367 rp.version = hdev->hci_ver;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02001368 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001369
1370 rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
1371 rp.current_settings = cpu_to_le32(get_current_settings(hdev));
1372
1373 memcpy(rp.dev_class, hdev->dev_class, 3);
Johan Hedberg03811012010-12-08 00:21:06 +02001374
1375 memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
Johan Hedberg27fcc362012-02-22 21:46:22 +02001376 memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
Johan Hedberg03811012010-12-08 00:21:06 +02001377
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001378 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001379
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001380 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
1381 sizeof(rp));
Johan Hedberg03811012010-12-08 00:21:06 +02001382}
1383
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001384static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
Johan Hedberg86805702011-11-11 16:18:52 +02001385{
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001386 __le32 settings = cpu_to_le32(get_current_settings(hdev));
Johan Hedberg86805702011-11-11 16:18:52 +02001387
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001388 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings,
1389 sizeof(settings));
Johan Hedberg86805702011-11-11 16:18:52 +02001390}
1391
Marcel Holtmann1904a852015-01-11 13:50:44 -08001392static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg8b064a32014-02-24 14:52:22 +02001393{
1394 BT_DBG("%s status 0x%02x", hdev->name, status);
1395
Johan Hedberga3172b72014-02-28 09:33:44 +02001396 if (hci_conn_count(hdev) == 0) {
1397 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001398 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberga3172b72014-02-28 09:33:44 +02001399 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001400}
1401
Johan Hedberg23a48092014-07-08 16:05:06 +03001402static bool hci_stop_discovery(struct hci_request *req)
Johan Hedberg21a60d32014-06-10 14:05:58 +03001403{
1404 struct hci_dev *hdev = req->hdev;
1405 struct hci_cp_remote_name_req_cancel cp;
1406 struct inquiry_entry *e;
1407
1408 switch (hdev->discovery.state) {
1409 case DISCOVERY_FINDING:
Jakub Pawlowski07d23342015-03-17 09:04:14 -07001410 if (test_bit(HCI_INQUIRY, &hdev->flags))
Johan Hedberg21a60d32014-06-10 14:05:58 +03001411 hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL);
Jakub Pawlowski07d23342015-03-17 09:04:14 -07001412
1413 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
Johan Hedberg21a60d32014-06-10 14:05:58 +03001414 cancel_delayed_work(&hdev->le_scan_disable);
1415 hci_req_add_le_scan_disable(req);
1416 }
1417
Johan Hedberg23a48092014-07-08 16:05:06 +03001418 return true;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001419
1420 case DISCOVERY_RESOLVING:
1421 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
1422 NAME_PENDING);
1423 if (!e)
Johan Hedberg23a48092014-07-08 16:05:06 +03001424 break;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001425
1426 bacpy(&cp.bdaddr, &e->data.bdaddr);
1427 hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp),
1428 &cp);
1429
Johan Hedberg23a48092014-07-08 16:05:06 +03001430 return true;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001431
1432 default:
1433 /* Passive scanning */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001434 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
Johan Hedberg21a60d32014-06-10 14:05:58 +03001435 hci_req_add_le_scan_disable(req);
Johan Hedberg23a48092014-07-08 16:05:06 +03001436 return true;
1437 }
1438
Johan Hedberg21a60d32014-06-10 14:05:58 +03001439 break;
1440 }
Johan Hedberg23a48092014-07-08 16:05:06 +03001441
1442 return false;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001443}
1444
Arman Uguray912098a2015-03-23 15:57:15 -07001445static void advertising_added(struct sock *sk, struct hci_dev *hdev,
1446 u8 instance)
1447{
1448 struct mgmt_ev_advertising_added ev;
1449
1450 ev.instance = instance;
1451
1452 mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk);
1453}
1454
1455static void advertising_removed(struct sock *sk, struct hci_dev *hdev,
1456 u8 instance)
1457{
1458 struct mgmt_ev_advertising_removed ev;
1459
1460 ev.instance = instance;
1461
1462 mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk);
1463}
1464
1465static void clear_adv_instance(struct hci_dev *hdev)
1466{
1467 struct hci_request req;
1468
1469 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
1470 return;
1471
Florian Grandel5d900e42015-06-18 03:16:35 +02001472 if (hdev->adv_instance_timeout)
1473 cancel_delayed_work(&hdev->adv_instance_expire);
Arman Uguray912098a2015-03-23 15:57:15 -07001474
1475 memset(&hdev->adv_instance, 0, sizeof(hdev->adv_instance));
1476 advertising_removed(NULL, hdev, 1);
1477 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
1478
1479 if (!hdev_is_powered(hdev) ||
1480 hci_dev_test_flag(hdev, HCI_ADVERTISING))
1481 return;
1482
1483 hci_req_init(&req, hdev);
1484 disable_advertising(&req);
1485 hci_req_run(&req, NULL);
1486}
1487
Johan Hedberg8b064a32014-02-24 14:52:22 +02001488static int clean_up_hci_state(struct hci_dev *hdev)
1489{
1490 struct hci_request req;
1491 struct hci_conn *conn;
Johan Hedberg23a48092014-07-08 16:05:06 +03001492 bool discov_stopped;
1493 int err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001494
1495 hci_req_init(&req, hdev);
1496
1497 if (test_bit(HCI_ISCAN, &hdev->flags) ||
1498 test_bit(HCI_PSCAN, &hdev->flags)) {
1499 u8 scan = 0x00;
1500 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1501 }
1502
Florian Grandel5d900e42015-06-18 03:16:35 +02001503 if (hdev->adv_instance_timeout)
Arman Uguray912098a2015-03-23 15:57:15 -07001504 clear_adv_instance(hdev);
1505
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001506 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedberg8b064a32014-02-24 14:52:22 +02001507 disable_advertising(&req);
1508
Johan Hedberg23a48092014-07-08 16:05:06 +03001509 discov_stopped = hci_stop_discovery(&req);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001510
1511 list_for_each_entry(conn, &hdev->conn_hash.list, list) {
1512 struct hci_cp_disconnect dc;
Johan Hedbergc9910d02014-02-27 14:35:12 +02001513 struct hci_cp_reject_conn_req rej;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001514
Johan Hedbergc9910d02014-02-27 14:35:12 +02001515 switch (conn->state) {
1516 case BT_CONNECTED:
1517 case BT_CONFIG:
1518 dc.handle = cpu_to_le16(conn->handle);
1519 dc.reason = 0x15; /* Terminated due to Power Off */
1520 hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc);
1521 break;
1522 case BT_CONNECT:
1523 if (conn->type == LE_LINK)
1524 hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL,
1525 0, NULL);
1526 else if (conn->type == ACL_LINK)
1527 hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL,
1528 6, &conn->dst);
1529 break;
1530 case BT_CONNECT2:
1531 bacpy(&rej.bdaddr, &conn->dst);
1532 rej.reason = 0x15; /* Terminated due to Power Off */
1533 if (conn->type == ACL_LINK)
1534 hci_req_add(&req, HCI_OP_REJECT_CONN_REQ,
1535 sizeof(rej), &rej);
1536 else if (conn->type == SCO_LINK)
1537 hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ,
1538 sizeof(rej), &rej);
1539 break;
1540 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001541 }
1542
Johan Hedberg23a48092014-07-08 16:05:06 +03001543 err = hci_req_run(&req, clean_up_hci_complete);
1544 if (!err && discov_stopped)
1545 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
1546
1547 return err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001548}
1549
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001550static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001551 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001552{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001553 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001554 struct mgmt_pending_cmd *cmd;
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001555 int err;
Johan Hedberg03811012010-12-08 00:21:06 +02001556
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001557 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001558
Johan Hedberga7e80f22013-01-09 16:05:19 +02001559 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001560 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1561 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001562
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001563 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001564
Johan Hedberg333ae952015-03-17 13:48:47 +02001565 if (pending_find(MGMT_OP_SET_POWERED, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001566 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1567 MGMT_STATUS_BUSY);
Johan Hedberg87b95ba2013-09-25 13:26:06 +03001568 goto failed;
1569 }
1570
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001571 if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
Marcel Holtmannf0d4b782012-02-21 12:14:25 +01001572 cancel_delayed_work(&hdev->power_off);
1573
1574 if (cp->val) {
Johan Hedberga1d70452013-01-09 15:29:40 +02001575 mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev,
1576 data, len);
1577 err = mgmt_powered(hdev, 1);
Marcel Holtmannf0d4b782012-02-21 12:14:25 +01001578 goto failed;
1579 }
1580 }
1581
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001582 if (!!cp->val == hdev_is_powered(hdev)) {
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001583 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001584 goto failed;
1585 }
1586
Johan Hedberg03811012010-12-08 00:21:06 +02001587 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
1588 if (!cmd) {
1589 err = -ENOMEM;
1590 goto failed;
1591 }
1592
Johan Hedberg8b064a32014-02-24 14:52:22 +02001593 if (cp->val) {
Johan Hedberg19202572013-01-14 22:33:51 +02001594 queue_work(hdev->req_workqueue, &hdev->power_on);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001595 err = 0;
1596 } else {
1597 /* Disconnect connections, stop scans, etc */
1598 err = clean_up_hci_state(hdev);
Johan Hedberga3172b72014-02-28 09:33:44 +02001599 if (!err)
1600 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
1601 HCI_POWER_OFF_TIMEOUT);
Johan Hedberg03811012010-12-08 00:21:06 +02001602
Johan Hedberg8b064a32014-02-24 14:52:22 +02001603 /* ENODATA means there were no HCI commands queued */
1604 if (err == -ENODATA) {
Johan Hedberga3172b72014-02-28 09:33:44 +02001605 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001606 queue_work(hdev->req_workqueue, &hdev->power_off.work);
1607 err = 0;
1608 }
1609 }
Johan Hedberg03811012010-12-08 00:21:06 +02001610
1611failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001612 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001613 return err;
1614}
1615
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001616static int new_settings(struct hci_dev *hdev, struct sock *skip)
1617{
Marcel Holtmannf6b77122015-03-14 19:28:05 -07001618 __le32 ev = cpu_to_le32(get_current_settings(hdev));
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001619
Marcel Holtmannf6b77122015-03-14 19:28:05 -07001620 return mgmt_generic_event(MGMT_EV_NEW_SETTINGS, hdev, &ev,
1621 sizeof(ev), skip);
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001622}
1623
Johan Hedberg91a668b2014-07-09 13:28:26 +03001624int mgmt_new_settings(struct hci_dev *hdev)
1625{
1626 return new_settings(hdev, NULL);
1627}
1628
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001629struct cmd_lookup {
1630 struct sock *sk;
1631 struct hci_dev *hdev;
1632 u8 mgmt_status;
1633};
1634
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001635static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001636{
1637 struct cmd_lookup *match = data;
1638
1639 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
1640
1641 list_del(&cmd->list);
1642
1643 if (match->sk == NULL) {
1644 match->sk = cmd->sk;
1645 sock_hold(match->sk);
1646 }
1647
1648 mgmt_pending_free(cmd);
1649}
1650
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001651static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001652{
1653 u8 *status = data;
1654
Johan Hedberga69e8372015-03-06 21:08:53 +02001655 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001656 mgmt_pending_remove(cmd);
1657}
1658
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001659static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg1b9b5ee2014-12-05 13:36:00 +02001660{
1661 if (cmd->cmd_complete) {
1662 u8 *status = data;
1663
1664 cmd->cmd_complete(cmd, *status);
1665 mgmt_pending_remove(cmd);
1666
1667 return;
1668 }
1669
1670 cmd_status_rsp(cmd, data);
1671}
1672
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001673static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedbergf5818c22014-12-05 13:36:02 +02001674{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001675 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1676 cmd->param, cmd->param_len);
Johan Hedbergf5818c22014-12-05 13:36:02 +02001677}
1678
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001679static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001680{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001681 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1682 cmd->param, sizeof(struct mgmt_addr_info));
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001683}
1684
Johan Hedberge6fe7982013-10-02 15:45:22 +03001685static u8 mgmt_bredr_support(struct hci_dev *hdev)
1686{
1687 if (!lmp_bredr_capable(hdev))
1688 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001689 else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001690 return MGMT_STATUS_REJECTED;
1691 else
1692 return MGMT_STATUS_SUCCESS;
1693}
1694
1695static u8 mgmt_le_support(struct hci_dev *hdev)
1696{
1697 if (!lmp_le_capable(hdev))
1698 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001699 else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001700 return MGMT_STATUS_REJECTED;
1701 else
1702 return MGMT_STATUS_SUCCESS;
1703}
1704
Marcel Holtmann1904a852015-01-11 13:50:44 -08001705static void set_discoverable_complete(struct hci_dev *hdev, u8 status,
1706 u16 opcode)
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001707{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001708 struct mgmt_pending_cmd *cmd;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001709 struct mgmt_mode *cp;
Marcel Holtmann970ba522013-10-15 06:33:57 -07001710 struct hci_request req;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001711 bool changed;
1712
1713 BT_DBG("status 0x%02x", status);
1714
1715 hci_dev_lock(hdev);
1716
Johan Hedberg333ae952015-03-17 13:48:47 +02001717 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001718 if (!cmd)
1719 goto unlock;
1720
1721 if (status) {
1722 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001723 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001724 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001725 goto remove_cmd;
1726 }
1727
1728 cp = cmd->param;
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001729 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001730 changed = !hci_dev_test_and_set_flag(hdev, HCI_DISCOVERABLE);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001731
1732 if (hdev->discov_timeout > 0) {
1733 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1734 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
1735 to);
1736 }
1737 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001738 changed = hci_dev_test_and_clear_flag(hdev, HCI_DISCOVERABLE);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001739 }
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001740
1741 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1742
1743 if (changed)
1744 new_settings(hdev, cmd->sk);
1745
Marcel Holtmann970ba522013-10-15 06:33:57 -07001746 /* When the discoverable mode gets changed, make sure
1747 * that class of device has the limited discoverable
Johan Hedberg432df052014-08-01 11:13:31 +03001748 * bit correctly set. Also update page scan based on whitelist
1749 * entries.
Marcel Holtmann970ba522013-10-15 06:33:57 -07001750 */
1751 hci_req_init(&req, hdev);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02001752 __hci_update_page_scan(&req);
Marcel Holtmann970ba522013-10-15 06:33:57 -07001753 update_class(&req);
1754 hci_req_run(&req, NULL);
1755
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001756remove_cmd:
1757 mgmt_pending_remove(cmd);
1758
1759unlock:
1760 hci_dev_unlock(hdev);
1761}
1762
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001763static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001764 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001765{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001766 struct mgmt_cp_set_discoverable *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001767 struct mgmt_pending_cmd *cmd;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001768 struct hci_request req;
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001769 u16 timeout;
Johan Hedberg9a43e252013-10-20 19:00:07 +03001770 u8 scan;
Johan Hedberg03811012010-12-08 00:21:06 +02001771 int err;
Johan Hedberge41d8b42010-12-13 21:07:03 +02001772
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001773 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001774
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001775 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1776 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001777 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1778 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001779
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001780 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02001781 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1782 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001783
Marcel Holtmann1f350c82012-03-12 20:31:08 -07001784 timeout = __le16_to_cpu(cp->timeout);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001785
1786 /* Disabling discoverable requires that no timeout is set,
1787 * and enabling limited discoverable requires a timeout.
1788 */
1789 if ((cp->val == 0x00 && timeout > 0) ||
1790 (cp->val == 0x02 && timeout == 0))
Johan Hedberga69e8372015-03-06 21:08:53 +02001791 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1792 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001793
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001794 hci_dev_lock(hdev);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001795
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001796 if (!hdev_is_powered(hdev) && timeout > 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001797 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1798 MGMT_STATUS_NOT_POWERED);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001799 goto failed;
1800 }
1801
Johan Hedberg333ae952015-03-17 13:48:47 +02001802 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1803 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001804 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1805 MGMT_STATUS_BUSY);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001806 goto failed;
1807 }
1808
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001809 if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001810 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1811 MGMT_STATUS_REJECTED);
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001812 goto failed;
1813 }
1814
1815 if (!hdev_is_powered(hdev)) {
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001816 bool changed = false;
1817
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001818 /* Setting limited discoverable when powered off is
1819 * not a valid operation since it requires a timeout
1820 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1821 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001822 if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07001823 hci_dev_change_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001824 changed = true;
1825 }
1826
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001827 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001828 if (err < 0)
1829 goto failed;
1830
1831 if (changed)
1832 err = new_settings(hdev, sk);
1833
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001834 goto failed;
1835 }
1836
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001837 /* If the current mode is the same, then just update the timeout
1838 * value with the new value. And if only the timeout gets updated,
1839 * then no need for any HCI transactions.
1840 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001841 if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1842 (cp->val == 0x02) == hci_dev_test_flag(hdev,
1843 HCI_LIMITED_DISCOVERABLE)) {
Marcel Holtmann36261542013-10-15 08:28:51 -07001844 cancel_delayed_work(&hdev->discov_off);
1845 hdev->discov_timeout = timeout;
Marcel Holtmann955638e2012-02-22 18:21:00 +01001846
Marcel Holtmann36261542013-10-15 08:28:51 -07001847 if (cp->val && hdev->discov_timeout > 0) {
1848 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001849 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
Marcel Holtmann36261542013-10-15 08:28:51 -07001850 to);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001851 }
1852
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001853 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001854 goto failed;
1855 }
1856
1857 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
1858 if (!cmd) {
1859 err = -ENOMEM;
1860 goto failed;
1861 }
1862
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001863 /* Cancel any potential discoverable timeout that might be
1864 * still active and store new timeout value. The arming of
1865 * the timeout happens in the complete handler.
1866 */
1867 cancel_delayed_work(&hdev->discov_off);
1868 hdev->discov_timeout = timeout;
1869
Johan Hedbergb456f872013-10-19 23:38:22 +03001870 /* Limited discoverable mode */
1871 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001872 hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001873 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001874 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001875
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001876 hci_req_init(&req, hdev);
1877
Johan Hedberg9a43e252013-10-20 19:00:07 +03001878 /* The procedure for LE-only controllers is much simpler - just
1879 * update the advertising data.
1880 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001881 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberg9a43e252013-10-20 19:00:07 +03001882 goto update_ad;
1883
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001884 scan = SCAN_PAGE;
1885
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001886 if (cp->val) {
1887 struct hci_cp_write_current_iac_lap hci_cp;
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001888
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001889 if (cp->val == 0x02) {
1890 /* Limited discoverable mode */
Marcel Holtmann33337dc2013-10-23 08:28:01 -07001891 hci_cp.num_iac = min_t(u8, hdev->num_iac, 2);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001892 hci_cp.iac_lap[0] = 0x00; /* LIAC */
1893 hci_cp.iac_lap[1] = 0x8b;
1894 hci_cp.iac_lap[2] = 0x9e;
1895 hci_cp.iac_lap[3] = 0x33; /* GIAC */
1896 hci_cp.iac_lap[4] = 0x8b;
1897 hci_cp.iac_lap[5] = 0x9e;
1898 } else {
1899 /* General discoverable mode */
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001900 hci_cp.num_iac = 1;
1901 hci_cp.iac_lap[0] = 0x33; /* GIAC */
1902 hci_cp.iac_lap[1] = 0x8b;
1903 hci_cp.iac_lap[2] = 0x9e;
1904 }
1905
1906 hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP,
1907 (hci_cp.num_iac * 3) + 1, &hci_cp);
1908
1909 scan |= SCAN_INQUIRY;
1910 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001911 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001912 }
1913
1914 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001915
Johan Hedberg9a43e252013-10-20 19:00:07 +03001916update_ad:
1917 update_adv_data(&req);
1918
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001919 err = hci_req_run(&req, set_discoverable_complete);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001920 if (err < 0)
1921 mgmt_pending_remove(cmd);
1922
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001923failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001924 hci_dev_unlock(hdev);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001925 return err;
1926}
1927
Johan Hedberg406d7802013-03-15 17:07:09 -05001928static void write_fast_connectable(struct hci_request *req, bool enable)
1929{
Johan Hedbergbd98b992013-03-15 17:07:13 -05001930 struct hci_dev *hdev = req->hdev;
Johan Hedberg406d7802013-03-15 17:07:09 -05001931 struct hci_cp_write_page_scan_activity acp;
1932 u8 type;
1933
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001934 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberg547003b2013-10-21 16:51:53 +03001935 return;
1936
Johan Hedberg4c01f8b2013-03-15 17:07:14 -05001937 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
1938 return;
1939
Johan Hedberg406d7802013-03-15 17:07:09 -05001940 if (enable) {
1941 type = PAGE_SCAN_TYPE_INTERLACED;
1942
1943 /* 160 msec page scan interval */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001944 acp.interval = cpu_to_le16(0x0100);
Johan Hedberg406d7802013-03-15 17:07:09 -05001945 } else {
1946 type = PAGE_SCAN_TYPE_STANDARD; /* default */
1947
1948 /* default 1.28 sec page scan */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001949 acp.interval = cpu_to_le16(0x0800);
Johan Hedberg406d7802013-03-15 17:07:09 -05001950 }
1951
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001952 acp.window = cpu_to_le16(0x0012);
Johan Hedberg406d7802013-03-15 17:07:09 -05001953
Johan Hedbergbd98b992013-03-15 17:07:13 -05001954 if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval ||
1955 __cpu_to_le16(hdev->page_scan_window) != acp.window)
1956 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
1957 sizeof(acp), &acp);
1958
1959 if (hdev->page_scan_type != type)
1960 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type);
Johan Hedberg406d7802013-03-15 17:07:09 -05001961}
1962
Marcel Holtmann1904a852015-01-11 13:50:44 -08001963static void set_connectable_complete(struct hci_dev *hdev, u8 status,
1964 u16 opcode)
Johan Hedberg2b76f452013-03-15 17:07:04 -05001965{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001966 struct mgmt_pending_cmd *cmd;
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001967 struct mgmt_mode *cp;
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001968 bool conn_changed, discov_changed;
Johan Hedberg2b76f452013-03-15 17:07:04 -05001969
1970 BT_DBG("status 0x%02x", status);
1971
1972 hci_dev_lock(hdev);
1973
Johan Hedberg333ae952015-03-17 13:48:47 +02001974 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
Johan Hedberg2b76f452013-03-15 17:07:04 -05001975 if (!cmd)
1976 goto unlock;
1977
Johan Hedberg37438c12013-10-14 16:20:05 +03001978 if (status) {
1979 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001980 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg37438c12013-10-14 16:20:05 +03001981 goto remove_cmd;
1982 }
1983
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001984 cp = cmd->param;
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001985 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001986 conn_changed = !hci_dev_test_and_set_flag(hdev,
1987 HCI_CONNECTABLE);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001988 discov_changed = false;
1989 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001990 conn_changed = hci_dev_test_and_clear_flag(hdev,
1991 HCI_CONNECTABLE);
1992 discov_changed = hci_dev_test_and_clear_flag(hdev,
1993 HCI_DISCOVERABLE);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001994 }
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001995
Johan Hedberg2b76f452013-03-15 17:07:04 -05001996 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
1997
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001998 if (conn_changed || discov_changed) {
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001999 new_settings(hdev, cmd->sk);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02002000 hci_update_page_scan(hdev);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03002001 if (discov_changed)
2002 mgmt_update_adv_data(hdev);
Johan Hedberg2b7be332014-07-07 14:40:22 +03002003 hci_update_background_scan(hdev);
2004 }
Johan Hedbergd7b856f2013-10-14 16:20:04 +03002005
Johan Hedberg37438c12013-10-14 16:20:05 +03002006remove_cmd:
Johan Hedberg2b76f452013-03-15 17:07:04 -05002007 mgmt_pending_remove(cmd);
2008
2009unlock:
2010 hci_dev_unlock(hdev);
2011}
2012
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03002013static int set_connectable_update_settings(struct hci_dev *hdev,
2014 struct sock *sk, u8 val)
2015{
2016 bool changed = false;
2017 int err;
2018
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002019 if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE))
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03002020 changed = true;
2021
2022 if (val) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07002023 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03002024 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002025 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
2026 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03002027 }
2028
2029 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
2030 if (err < 0)
2031 return err;
2032
Johan Hedberg562064e2014-07-08 16:35:34 +03002033 if (changed) {
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02002034 hci_update_page_scan(hdev);
Johan Hedberg562064e2014-07-08 16:35:34 +03002035 hci_update_background_scan(hdev);
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03002036 return new_settings(hdev, sk);
Johan Hedberg562064e2014-07-08 16:35:34 +03002037 }
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03002038
2039 return 0;
2040}
2041
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002042static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002043 u16 len)
Johan Hedbergf7b64e692010-12-13 21:07:06 +02002044{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002045 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002046 struct mgmt_pending_cmd *cmd;
Johan Hedberg2b76f452013-03-15 17:07:04 -05002047 struct hci_request req;
Johan Hedberg1987fdc2013-10-14 21:15:24 +03002048 u8 scan;
Johan Hedbergf7b64e692010-12-13 21:07:06 +02002049 int err;
Johan Hedberg03811012010-12-08 00:21:06 +02002050
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002051 BT_DBG("request for %s", hdev->name);
Johan Hedberge41d8b42010-12-13 21:07:03 +02002052
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002053 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
2054 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02002055 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
2056 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03002057
Johan Hedberga7e80f22013-01-09 16:05:19 +02002058 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002059 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
2060 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002061
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002062 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002063
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002064 if (!hdev_is_powered(hdev)) {
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03002065 err = set_connectable_update_settings(hdev, sk, cp->val);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002066 goto failed;
2067 }
2068
Johan Hedberg333ae952015-03-17 13:48:47 +02002069 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
2070 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002071 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
2072 MGMT_STATUS_BUSY);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002073 goto failed;
2074 }
2075
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002076 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
2077 if (!cmd) {
2078 err = -ENOMEM;
2079 goto failed;
2080 }
2081
Johan Hedberg2b76f452013-03-15 17:07:04 -05002082 hci_req_init(&req, hdev);
2083
Johan Hedberg9a43e252013-10-20 19:00:07 +03002084 /* If BR/EDR is not enabled and we disable advertising as a
2085 * by-product of disabling connectable, we need to update the
2086 * advertising flags.
2087 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002088 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg9a43e252013-10-20 19:00:07 +03002089 if (!cp->val) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002090 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2091 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberg9a43e252013-10-20 19:00:07 +03002092 }
2093 update_adv_data(&req);
2094 } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) {
Johan Hedberg9b742462013-10-14 16:20:03 +03002095 if (cp->val) {
2096 scan = SCAN_PAGE;
2097 } else {
Johan Hedberg3bd27242014-07-28 20:53:58 +03002098 /* If we don't have any whitelist entries just
2099 * disable all scanning. If there are entries
2100 * and we had both page and inquiry scanning
2101 * enabled then fall back to only page scanning.
2102 * Otherwise no changes are needed.
2103 */
2104 if (list_empty(&hdev->whitelist))
2105 scan = SCAN_DISABLED;
2106 else if (test_bit(HCI_ISCAN, &hdev->flags))
2107 scan = SCAN_PAGE;
2108 else
2109 goto no_scan_update;
Johan Hedberg9b742462013-10-14 16:20:03 +03002110
2111 if (test_bit(HCI_ISCAN, &hdev->flags) &&
Marcel Holtmann8d6083f2013-10-14 16:38:45 -07002112 hdev->discov_timeout > 0)
Johan Hedberg9b742462013-10-14 16:20:03 +03002113 cancel_delayed_work(&hdev->discov_off);
2114 }
2115
2116 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
2117 }
Johan Hedberg2b76f452013-03-15 17:07:04 -05002118
Johan Hedberg3bd27242014-07-28 20:53:58 +03002119no_scan_update:
Johan Hedberge8b12022014-07-10 10:51:27 +03002120 /* Update the advertising parameters if necessary */
Arman Uguray880897d2015-03-28 12:39:00 -07002121 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
2122 hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
Johan Hedberg1987fdc2013-10-14 21:15:24 +03002123 enable_advertising(&req);
Johan Hedberg1987fdc2013-10-14 21:15:24 +03002124
Johan Hedberg2b76f452013-03-15 17:07:04 -05002125 err = hci_req_run(&req, set_connectable_complete);
Johan Hedberg9b742462013-10-14 16:20:03 +03002126 if (err < 0) {
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002127 mgmt_pending_remove(cmd);
Johan Hedberg9b742462013-10-14 16:20:03 +03002128 if (err == -ENODATA)
Johan Hedberga81070b2013-10-19 23:38:19 +03002129 err = set_connectable_update_settings(hdev, sk,
2130 cp->val);
Johan Hedberg9b742462013-10-14 16:20:03 +03002131 goto failed;
2132 }
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002133
2134failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002135 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002136 return err;
2137}
2138
Johan Hedbergb2939472014-07-30 09:22:23 +03002139static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002140 u16 len)
Johan Hedberg73f22f62010-12-29 16:00:25 +02002141{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002142 struct mgmt_mode *cp = data;
Marcel Holtmann55594352013-10-06 16:11:57 -07002143 bool changed;
Johan Hedberg73f22f62010-12-29 16:00:25 +02002144 int err;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002145
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002146 BT_DBG("request for %s", hdev->name);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002147
Johan Hedberga7e80f22013-01-09 16:05:19 +02002148 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002149 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE,
2150 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002151
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002152 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002153
2154 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07002155 changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002156 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002157 changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002158
Johan Hedbergb2939472014-07-30 09:22:23 +03002159 err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002160 if (err < 0)
Marcel Holtmann55594352013-10-06 16:11:57 -07002161 goto unlock;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002162
Marcel Holtmann55594352013-10-06 16:11:57 -07002163 if (changed)
2164 err = new_settings(hdev, sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002165
Marcel Holtmann55594352013-10-06 16:11:57 -07002166unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002167 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002168 return err;
2169}
Johan Hedberg72a734e2010-12-30 00:38:22 +02002170
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002171static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
2172 u16 len)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002173{
2174 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002175 struct mgmt_pending_cmd *cmd;
Johan Hedberge6fe7982013-10-02 15:45:22 +03002176 u8 val, status;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002177 int err;
2178
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002179 BT_DBG("request for %s", hdev->name);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002180
Johan Hedberge6fe7982013-10-02 15:45:22 +03002181 status = mgmt_bredr_support(hdev);
2182 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02002183 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2184 status);
Johan Hedberg33c525c2012-10-24 21:11:58 +03002185
Johan Hedberga7e80f22013-01-09 16:05:19 +02002186 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002187 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2188 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002189
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002190 hci_dev_lock(hdev);
2191
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002192 if (!hdev_is_powered(hdev)) {
Johan Hedberg47990ea2012-02-22 11:58:37 +02002193 bool changed = false;
2194
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002195 if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07002196 hci_dev_change_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02002197 changed = true;
2198 }
2199
2200 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
2201 if (err < 0)
2202 goto failed;
2203
2204 if (changed)
2205 err = new_settings(hdev, sk);
2206
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002207 goto failed;
2208 }
2209
Johan Hedberg333ae952015-03-17 13:48:47 +02002210 if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002211 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2212 MGMT_STATUS_BUSY);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002213 goto failed;
2214 }
2215
2216 val = !!cp->val;
2217
2218 if (test_bit(HCI_AUTH, &hdev->flags) == val) {
2219 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
2220 goto failed;
2221 }
2222
2223 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
2224 if (!cmd) {
2225 err = -ENOMEM;
2226 goto failed;
2227 }
2228
2229 err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
2230 if (err < 0) {
2231 mgmt_pending_remove(cmd);
2232 goto failed;
2233 }
2234
2235failed:
2236 hci_dev_unlock(hdev);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002237 return err;
2238}
2239
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002240static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002241{
2242 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002243 struct mgmt_pending_cmd *cmd;
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07002244 u8 status;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002245 int err;
2246
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002247 BT_DBG("request for %s", hdev->name);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002248
Marcel Holtmanncdba5282013-10-02 21:31:52 -07002249 status = mgmt_bredr_support(hdev);
2250 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02002251 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
Marcel Holtmanncdba5282013-10-02 21:31:52 -07002252
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002253 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002254 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2255 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002256
Johan Hedberga7e80f22013-01-09 16:05:19 +02002257 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002258 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2259 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002260
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002261 hci_dev_lock(hdev);
Johan Hedberg6c8f12c2012-02-22 16:35:26 +02002262
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002263 if (!hdev_is_powered(hdev)) {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002264 bool changed;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02002265
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002266 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07002267 changed = !hci_dev_test_and_set_flag(hdev,
2268 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002269 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002270 changed = hci_dev_test_and_clear_flag(hdev,
2271 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002272 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002273 changed = hci_dev_test_and_clear_flag(hdev,
2274 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002275 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002276 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02002277 }
2278
2279 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2280 if (err < 0)
2281 goto failed;
2282
2283 if (changed)
2284 err = new_settings(hdev, sk);
2285
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002286 goto failed;
2287 }
2288
Johan Hedberg333ae952015-03-17 13:48:47 +02002289 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002290 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2291 MGMT_STATUS_BUSY);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002292 goto failed;
2293 }
2294
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002295 if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002296 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2297 goto failed;
2298 }
2299
2300 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
2301 if (!cmd) {
2302 err = -ENOMEM;
2303 goto failed;
2304 }
2305
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002306 if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03002307 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2308 sizeof(cp->val), &cp->val);
2309
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07002310 err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002311 if (err < 0) {
2312 mgmt_pending_remove(cmd);
2313 goto failed;
2314 }
2315
2316failed:
2317 hci_dev_unlock(hdev);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002318 return err;
2319}
2320
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002321static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002322{
2323 struct mgmt_mode *cp = data;
Marcel Holtmannee392692013-10-01 22:59:23 -07002324 bool changed;
Johan Hedberge6fe7982013-10-02 15:45:22 +03002325 u8 status;
Marcel Holtmannee392692013-10-01 22:59:23 -07002326 int err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002327
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002328 BT_DBG("request for %s", hdev->name);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002329
Johan Hedberge6fe7982013-10-02 15:45:22 +03002330 status = mgmt_bredr_support(hdev);
2331 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02002332 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002333
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002334 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002335 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2336 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002337
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002338 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02002339 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2340 MGMT_STATUS_REJECTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002341
Johan Hedberga7e80f22013-01-09 16:05:19 +02002342 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002343 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2344 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002345
Marcel Holtmannee392692013-10-01 22:59:23 -07002346 hci_dev_lock(hdev);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002347
Johan Hedberg333ae952015-03-17 13:48:47 +02002348 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002349 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2350 MGMT_STATUS_BUSY);
Johan Hedberga2cb01d2015-02-19 17:38:07 +02002351 goto unlock;
2352 }
2353
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002354 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07002355 changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002356 } else {
2357 if (hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002358 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2359 MGMT_STATUS_REJECTED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002360 goto unlock;
2361 }
2362
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002363 changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002364 }
Marcel Holtmannee392692013-10-01 22:59:23 -07002365
2366 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
2367 if (err < 0)
2368 goto unlock;
2369
2370 if (changed)
2371 err = new_settings(hdev, sk);
2372
2373unlock:
2374 hci_dev_unlock(hdev);
2375 return err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002376}
2377
Marcel Holtmann1904a852015-01-11 13:50:44 -08002378static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002379{
2380 struct cmd_lookup match = { NULL, hdev };
2381
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302382 hci_dev_lock(hdev);
2383
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002384 if (status) {
2385 u8 mgmt_err = mgmt_status(status);
2386
2387 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
2388 &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302389 goto unlock;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002390 }
2391
2392 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
2393
2394 new_settings(hdev, match.sk);
2395
2396 if (match.sk)
2397 sock_put(match.sk);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002398
2399 /* Make sure the controller has a good default for
2400 * advertising data. Restrict the update to when LE
2401 * has actually been enabled. During power on, the
2402 * update in powered_update_hci will take care of it.
2403 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002404 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002405 struct hci_request req;
2406
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002407 hci_req_init(&req, hdev);
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07002408 update_adv_data(&req);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07002409 update_scan_rsp_data(&req);
Johan Hedberg2cf22212014-12-19 22:26:00 +02002410 __hci_update_background_scan(&req);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002411 hci_req_run(&req, NULL);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002412 }
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302413
2414unlock:
2415 hci_dev_unlock(hdev);
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002416}
2417
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002418static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg06199cf2012-02-22 16:37:11 +02002419{
2420 struct mgmt_mode *cp = data;
2421 struct hci_cp_write_le_host_supported hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002422 struct mgmt_pending_cmd *cmd;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002423 struct hci_request req;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002424 int err;
Johan Hedberg0b60eba2012-02-28 00:57:24 +02002425 u8 val, enabled;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002426
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002427 BT_DBG("request for %s", hdev->name);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002428
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002429 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002430 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2431 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg1de028c2012-02-29 19:55:35 -08002432
Johan Hedberga7e80f22013-01-09 16:05:19 +02002433 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002434 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2435 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002436
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07002437 /* Bluetooth single mode LE only controllers or dual-mode
2438 * controllers configured as LE only devices, do not allow
2439 * switching LE off. These have either LE enabled explicitly
2440 * or BR/EDR has been previously switched off.
2441 *
2442 * When trying to enable an already enabled LE, then gracefully
2443 * send a positive response. Trying to disable it however will
2444 * result into rejection.
2445 */
2446 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
2447 if (cp->val == 0x01)
2448 return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2449
Johan Hedberga69e8372015-03-06 21:08:53 +02002450 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2451 MGMT_STATUS_REJECTED);
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07002452 }
Johan Hedbergc73eee92013-04-19 18:35:21 +03002453
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002454 hci_dev_lock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002455
2456 val = !!cp->val;
Gustavo Padovanffa88e02012-11-23 16:50:51 -02002457 enabled = lmp_host_le_capable(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002458
Johan Hedberg0b60eba2012-02-28 00:57:24 +02002459 if (!hdev_is_powered(hdev) || val == enabled) {
Johan Hedberg06199cf2012-02-22 16:37:11 +02002460 bool changed = false;
2461
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002462 if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07002463 hci_dev_change_flag(hdev, HCI_LE_ENABLED);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002464 changed = true;
2465 }
2466
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002467 if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002468 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03002469 changed = true;
2470 }
2471
Johan Hedberg06199cf2012-02-22 16:37:11 +02002472 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2473 if (err < 0)
Johan Hedberg1de028c2012-02-29 19:55:35 -08002474 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002475
2476 if (changed)
2477 err = new_settings(hdev, sk);
2478
Johan Hedberg1de028c2012-02-29 19:55:35 -08002479 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002480 }
2481
Johan Hedberg333ae952015-03-17 13:48:47 +02002482 if (pending_find(MGMT_OP_SET_LE, hdev) ||
2483 pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002484 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2485 MGMT_STATUS_BUSY);
Johan Hedberg1de028c2012-02-29 19:55:35 -08002486 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002487 }
2488
2489 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
2490 if (!cmd) {
2491 err = -ENOMEM;
Johan Hedberg1de028c2012-02-29 19:55:35 -08002492 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002493 }
2494
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002495 hci_req_init(&req, hdev);
2496
Johan Hedberg06199cf2012-02-22 16:37:11 +02002497 memset(&hci_cp, 0, sizeof(hci_cp));
2498
2499 if (val) {
2500 hci_cp.le = val;
Marcel Holtmann32226e42014-07-24 20:04:16 +02002501 hci_cp.simul = 0x00;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002502 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002503 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002504 disable_advertising(&req);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002505 }
2506
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002507 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp),
2508 &hci_cp);
2509
2510 err = hci_req_run(&req, le_enable_complete);
Syam Sidhardhan0c01bc42012-04-12 20:33:21 +05302511 if (err < 0)
Johan Hedberg06199cf2012-02-22 16:37:11 +02002512 mgmt_pending_remove(cmd);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002513
Johan Hedberg1de028c2012-02-29 19:55:35 -08002514unlock:
2515 hci_dev_unlock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002516 return err;
2517}
2518
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002519/* This is a helper function to test for pending mgmt commands that can
2520 * cause CoD or EIR HCI commands. We can only allow one such pending
2521 * mgmt command at a time since otherwise we cannot easily track what
2522 * the current values are, will be, and based on that calculate if a new
2523 * HCI command needs to be sent and if yes with what value.
2524 */
2525static bool pending_eir_or_class(struct hci_dev *hdev)
2526{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002527 struct mgmt_pending_cmd *cmd;
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002528
2529 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
2530 switch (cmd->opcode) {
2531 case MGMT_OP_ADD_UUID:
2532 case MGMT_OP_REMOVE_UUID:
2533 case MGMT_OP_SET_DEV_CLASS:
2534 case MGMT_OP_SET_POWERED:
2535 return true;
2536 }
2537 }
2538
2539 return false;
2540}
2541
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002542static const u8 bluetooth_base_uuid[] = {
2543 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
2544 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2545};
2546
2547static u8 get_uuid_size(const u8 *uuid)
2548{
2549 u32 val;
2550
2551 if (memcmp(uuid, bluetooth_base_uuid, 12))
2552 return 128;
2553
2554 val = get_unaligned_le32(&uuid[12]);
2555 if (val > 0xffff)
2556 return 32;
2557
2558 return 16;
2559}
2560
Johan Hedberg92da6092013-03-15 17:06:55 -05002561static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status)
2562{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002563 struct mgmt_pending_cmd *cmd;
Johan Hedberg92da6092013-03-15 17:06:55 -05002564
2565 hci_dev_lock(hdev);
2566
Johan Hedberg333ae952015-03-17 13:48:47 +02002567 cmd = pending_find(mgmt_op, hdev);
Johan Hedberg92da6092013-03-15 17:06:55 -05002568 if (!cmd)
2569 goto unlock;
2570
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002571 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
2572 mgmt_status(status), hdev->dev_class, 3);
Johan Hedberg92da6092013-03-15 17:06:55 -05002573
2574 mgmt_pending_remove(cmd);
2575
2576unlock:
2577 hci_dev_unlock(hdev);
2578}
2579
Marcel Holtmann1904a852015-01-11 13:50:44 -08002580static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002581{
2582 BT_DBG("status 0x%02x", status);
2583
2584 mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status);
2585}
2586
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002587static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002588{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002589 struct mgmt_cp_add_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002590 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002591 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002592 struct bt_uuid *uuid;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002593 int err;
2594
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002595 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002596
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002597 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002598
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002599 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002600 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
2601 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002602 goto failed;
2603 }
2604
Andre Guedes92c4c202012-06-07 19:05:44 -03002605 uuid = kmalloc(sizeof(*uuid), GFP_KERNEL);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002606 if (!uuid) {
2607 err = -ENOMEM;
2608 goto failed;
2609 }
2610
2611 memcpy(uuid->uuid, cp->uuid, 16);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002612 uuid->svc_hint = cp->svc_hint;
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002613 uuid->size = get_uuid_size(cp->uuid);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002614
Johan Hedbergde66aa62013-01-27 00:31:27 +02002615 list_add_tail(&uuid->list, &hdev->uuids);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002616
Johan Hedberg890ea892013-03-15 17:06:52 -05002617 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002618
Johan Hedberg890ea892013-03-15 17:06:52 -05002619 update_class(&req);
2620 update_eir(&req);
2621
Johan Hedberg92da6092013-03-15 17:06:55 -05002622 err = hci_req_run(&req, add_uuid_complete);
2623 if (err < 0) {
2624 if (err != -ENODATA)
2625 goto failed;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002626
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002627 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
2628 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002629 goto failed;
2630 }
2631
2632 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002633 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002634 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002635 goto failed;
2636 }
2637
2638 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002639
2640failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002641 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002642 return err;
2643}
2644
Johan Hedberg24b78d02012-02-23 23:24:30 +02002645static bool enable_service_cache(struct hci_dev *hdev)
2646{
2647 if (!hdev_is_powered(hdev))
2648 return false;
2649
Marcel Holtmann238be782015-03-13 02:11:06 -07002650 if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg46818ed2013-01-14 22:33:52 +02002651 queue_delayed_work(hdev->workqueue, &hdev->service_cache,
2652 CACHE_TIMEOUT);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002653 return true;
2654 }
2655
2656 return false;
2657}
2658
Marcel Holtmann1904a852015-01-11 13:50:44 -08002659static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002660{
2661 BT_DBG("status 0x%02x", status);
2662
2663 mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status);
2664}
2665
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002666static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002667 u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002668{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002669 struct mgmt_cp_remove_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002670 struct mgmt_pending_cmd *cmd;
Johan Hedberg056341c2013-01-27 00:31:30 +02002671 struct bt_uuid *match, *tmp;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002672 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 -05002673 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002674 int err, found;
2675
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002676 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002677
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002678 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002679
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002680 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002681 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2682 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002683 goto unlock;
2684 }
2685
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002686 if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
Johan Hedberg35f74982014-02-18 17:14:32 +02002687 hci_uuids_clear(hdev);
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002688
Johan Hedberg24b78d02012-02-23 23:24:30 +02002689 if (enable_service_cache(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002690 err = mgmt_cmd_complete(sk, hdev->id,
2691 MGMT_OP_REMOVE_UUID,
2692 0, hdev->dev_class, 3);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002693 goto unlock;
2694 }
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002695
Johan Hedberg9246a862012-02-23 21:33:16 +02002696 goto update_class;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002697 }
2698
2699 found = 0;
2700
Johan Hedberg056341c2013-01-27 00:31:30 +02002701 list_for_each_entry_safe(match, tmp, &hdev->uuids, list) {
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002702 if (memcmp(match->uuid, cp->uuid, 16) != 0)
2703 continue;
2704
2705 list_del(&match->list);
Johan Hedberg482049f2012-11-08 10:25:26 +01002706 kfree(match);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002707 found++;
2708 }
2709
2710 if (found == 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002711 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2712 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002713 goto unlock;
2714 }
2715
Johan Hedberg9246a862012-02-23 21:33:16 +02002716update_class:
Johan Hedberg890ea892013-03-15 17:06:52 -05002717 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002718
Johan Hedberg890ea892013-03-15 17:06:52 -05002719 update_class(&req);
2720 update_eir(&req);
2721
Johan Hedberg92da6092013-03-15 17:06:55 -05002722 err = hci_req_run(&req, remove_uuid_complete);
2723 if (err < 0) {
2724 if (err != -ENODATA)
2725 goto unlock;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002726
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002727 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
2728 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002729 goto unlock;
2730 }
2731
2732 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002733 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002734 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002735 goto unlock;
2736 }
2737
2738 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002739
2740unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002741 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002742 return err;
2743}
2744
Marcel Holtmann1904a852015-01-11 13:50:44 -08002745static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002746{
2747 BT_DBG("status 0x%02x", status);
2748
2749 mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status);
2750}
2751
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002752static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002753 u16 len)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002754{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002755 struct mgmt_cp_set_dev_class *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002756 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002757 struct hci_request req;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002758 int err;
2759
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002760 BT_DBG("request for %s", hdev->name);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002761
Marcel Holtmann6203fc92013-10-02 23:37:29 -07002762 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002763 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2764 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002765
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002766 hci_dev_lock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002767
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002768 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002769 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2770 MGMT_STATUS_BUSY);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002771 goto unlock;
2772 }
2773
2774 if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002775 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2776 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002777 goto unlock;
2778 }
2779
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002780 hdev->major_class = cp->major;
2781 hdev->minor_class = cp->minor;
2782
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002783 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002784 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2785 hdev->dev_class, 3);
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002786 goto unlock;
2787 }
2788
Johan Hedberg890ea892013-03-15 17:06:52 -05002789 hci_req_init(&req, hdev);
2790
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002791 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg7d785252011-12-15 00:47:39 +02002792 hci_dev_unlock(hdev);
2793 cancel_delayed_work_sync(&hdev->service_cache);
2794 hci_dev_lock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -05002795 update_eir(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02002796 }
Johan Hedberg14c0b602011-12-15 00:47:37 +02002797
Johan Hedberg890ea892013-03-15 17:06:52 -05002798 update_class(&req);
2799
Johan Hedberg92da6092013-03-15 17:06:55 -05002800 err = hci_req_run(&req, set_class_complete);
2801 if (err < 0) {
2802 if (err != -ENODATA)
2803 goto unlock;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002804
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002805 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2806 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002807 goto unlock;
2808 }
2809
2810 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002811 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002812 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002813 goto unlock;
2814 }
2815
2816 err = 0;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002817
Johan Hedbergb5235a62012-02-21 14:32:24 +02002818unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002819 hci_dev_unlock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002820 return err;
2821}
2822
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002823static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002824 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002825{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002826 struct mgmt_cp_load_link_keys *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03002827 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
2828 sizeof(struct mgmt_link_key_info));
Szymon Janc4e51eae2011-02-25 19:05:48 +01002829 u16 key_count, expected_len;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002830 bool changed;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002831 int i;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002832
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002833 BT_DBG("request for %s", hdev->name);
2834
2835 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002836 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2837 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002838
Marcel Holtmann1f350c82012-03-12 20:31:08 -07002839 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002840 if (key_count > max_key_count) {
2841 BT_ERR("load_link_keys: too big key_count value %u",
2842 key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02002843 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2844 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002845 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002846
Johan Hedberg86742e12011-11-07 23:13:38 +02002847 expected_len = sizeof(*cp) + key_count *
2848 sizeof(struct mgmt_link_key_info);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002849 if (expected_len != len) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002850 BT_ERR("load_link_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02002851 expected_len, len);
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 Hedberg55ed8ca12011-01-17 14:41:05 +02002854 }
2855
Johan Hedberg4ae14302013-01-20 14:27:13 +02002856 if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002857 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2858 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ae14302013-01-20 14:27:13 +02002859
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002860 BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002861 key_count);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002862
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002863 for (i = 0; i < key_count; i++) {
2864 struct mgmt_link_key_info *key = &cp->keys[i];
2865
Marcel Holtmann8e991132014-01-10 02:07:25 -08002866 if (key->addr.type != BDADDR_BREDR || key->type > 0x08)
Johan Hedberga69e8372015-03-06 21:08:53 +02002867 return mgmt_cmd_status(sk, hdev->id,
2868 MGMT_OP_LOAD_LINK_KEYS,
2869 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002870 }
2871
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002872 hci_dev_lock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002873
2874 hci_link_keys_clear(hdev);
2875
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002876 if (cp->debug_keys)
Marcel Holtmann238be782015-03-13 02:11:06 -07002877 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002878 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002879 changed = hci_dev_test_and_clear_flag(hdev,
2880 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002881
2882 if (changed)
2883 new_settings(hdev, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002884
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002885 for (i = 0; i < key_count; i++) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002886 struct mgmt_link_key_info *key = &cp->keys[i];
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002887
Johan Hedberg58e92932014-06-24 14:00:26 +03002888 /* Always ignore debug keys and require a new pairing if
2889 * the user wants to use them.
2890 */
2891 if (key->type == HCI_LK_DEBUG_COMBINATION)
2892 continue;
2893
Johan Hedberg7652ff62014-06-24 13:15:49 +03002894 hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val,
2895 key->type, key->pin_len, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002896 }
2897
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002898 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
Johan Hedberg0e5f8752011-11-11 16:18:54 +02002899
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002900 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002901
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002902 return 0;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002903}
2904
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002905static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002906 u8 addr_type, struct sock *skip_sk)
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002907{
2908 struct mgmt_ev_device_unpaired ev;
2909
2910 bacpy(&ev.addr.bdaddr, bdaddr);
2911 ev.addr.type = addr_type;
2912
2913 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002914 skip_sk);
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002915}
2916
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002917static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002918 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002919{
Johan Hedberg124f6e32012-02-09 13:50:12 +02002920 struct mgmt_cp_unpair_device *cp = data;
2921 struct mgmt_rp_unpair_device rp;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002922 struct hci_cp_disconnect dc;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002923 struct mgmt_pending_cmd *cmd;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002924 struct hci_conn *conn;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002925 int err;
2926
Johan Hedberga8a1d192011-11-10 15:54:38 +02002927 memset(&rp, 0, sizeof(rp));
Johan Hedberg124f6e32012-02-09 13:50:12 +02002928 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2929 rp.addr.type = cp->addr.type;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002930
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002931 if (!bdaddr_type_is_valid(cp->addr.type))
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 Hedberg4ee71b22013-01-20 14:27:19 +02002935
Johan Hedberg118da702013-01-20 14:27:20 +02002936 if (cp->disconnect != 0x00 && cp->disconnect != 0x01)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002937 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2938 MGMT_STATUS_INVALID_PARAMS,
2939 &rp, sizeof(rp));
Johan Hedberg118da702013-01-20 14:27:20 +02002940
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002941 hci_dev_lock(hdev);
2942
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002943 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002944 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2945 MGMT_STATUS_NOT_POWERED, &rp,
2946 sizeof(rp));
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002947 goto unlock;
2948 }
2949
Johan Hedberge0b2b272014-02-18 17:14:31 +02002950 if (cp->addr.type == BDADDR_BREDR) {
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002951 /* If disconnection is requested, then look up the
2952 * connection. If the remote device is connected, it
2953 * will be later used to terminate the link.
2954 *
2955 * Setting it to NULL explicitly will cause no
2956 * termination of the link.
2957 */
2958 if (cp->disconnect)
2959 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2960 &cp->addr.bdaddr);
2961 else
2962 conn = NULL;
2963
Johan Hedberg124f6e32012-02-09 13:50:12 +02002964 err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
Johan Hedberge0b2b272014-02-18 17:14:31 +02002965 } else {
2966 u8 addr_type;
2967
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002968 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK,
2969 &cp->addr.bdaddr);
2970 if (conn) {
2971 /* Defer clearing up the connection parameters
2972 * until closing to give a chance of keeping
2973 * them if a repairing happens.
2974 */
2975 set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
2976
2977 /* If disconnection is not requested, then
2978 * clear the connection variable so that the
2979 * link is not terminated.
2980 */
2981 if (!cp->disconnect)
2982 conn = NULL;
2983 }
2984
Johan Hedberge0b2b272014-02-18 17:14:31 +02002985 if (cp->addr.type == BDADDR_LE_PUBLIC)
2986 addr_type = ADDR_LE_DEV_PUBLIC;
2987 else
2988 addr_type = ADDR_LE_DEV_RANDOM;
2989
Johan Hedberga7ec7332014-02-18 17:14:35 +02002990 hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type);
2991
Johan Hedberge0b2b272014-02-18 17:14:31 +02002992 err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type);
2993 }
Vinicius Costa Gomesb0dbfb42012-02-02 21:08:03 -03002994
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002995 if (err < 0) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002996 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2997 MGMT_STATUS_NOT_PAIRED, &rp,
2998 sizeof(rp));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002999 goto unlock;
3000 }
3001
Alfonso Acosta89cbb062014-10-11 21:44:47 +00003002 /* If the connection variable is set, then termination of the
3003 * link is requested.
3004 */
Johan Hedberga8a1d192011-11-10 15:54:38 +02003005 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003006 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
3007 &rp, sizeof(rp));
Johan Hedbergb1078ad2012-02-09 17:21:16 +02003008 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
Johan Hedberga8a1d192011-11-10 15:54:38 +02003009 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003010 }
3011
Johan Hedberg124f6e32012-02-09 13:50:12 +02003012 cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003013 sizeof(*cp));
Johan Hedberga8a1d192011-11-10 15:54:38 +02003014 if (!cmd) {
3015 err = -ENOMEM;
3016 goto unlock;
3017 }
3018
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02003019 cmd->cmd_complete = addr_cmd_complete;
3020
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02003021 dc.handle = cpu_to_le16(conn->handle);
Johan Hedberga8a1d192011-11-10 15:54:38 +02003022 dc.reason = 0x13; /* Remote User Terminated Connection */
3023 err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc);
3024 if (err < 0)
3025 mgmt_pending_remove(cmd);
3026
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003027unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003028 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003029 return err;
3030}
3031
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003032static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003033 u16 len)
Johan Hedberg8962ee72011-01-20 12:40:27 +02003034{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003035 struct mgmt_cp_disconnect *cp = data;
Johan Hedberg06a63b12013-01-20 14:27:21 +02003036 struct mgmt_rp_disconnect rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003037 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02003038 struct hci_conn *conn;
Johan Hedberg8962ee72011-01-20 12:40:27 +02003039 int err;
3040
3041 BT_DBG("");
3042
Johan Hedberg06a63b12013-01-20 14:27:21 +02003043 memset(&rp, 0, sizeof(rp));
3044 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3045 rp.addr.type = cp->addr.type;
3046
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003047 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003048 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3049 MGMT_STATUS_INVALID_PARAMS,
3050 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003051
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003052 hci_dev_lock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003053
3054 if (!test_bit(HCI_UP, &hdev->flags)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003055 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3056 MGMT_STATUS_NOT_POWERED, &rp,
3057 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02003058 goto failed;
3059 }
3060
Johan Hedberg333ae952015-03-17 13:48:47 +02003061 if (pending_find(MGMT_OP_DISCONNECT, hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003062 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3063 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02003064 goto failed;
3065 }
3066
Andre Guedes591f47f2012-04-24 21:02:49 -03003067 if (cp->addr.type == BDADDR_BREDR)
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003068 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
3069 &cp->addr.bdaddr);
Johan Hedberg88c3df12012-02-09 14:27:38 +02003070 else
3071 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
Vinicius Costa Gomes365227e2011-05-06 18:41:44 -03003072
Vishal Agarwalf9607272012-06-13 05:32:43 +05303073 if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003074 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3075 MGMT_STATUS_NOT_CONNECTED, &rp,
3076 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02003077 goto failed;
3078 }
3079
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003080 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03003081 if (!cmd) {
3082 err = -ENOMEM;
Johan Hedberg8962ee72011-01-20 12:40:27 +02003083 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03003084 }
Johan Hedberg8962ee72011-01-20 12:40:27 +02003085
Johan Hedbergf5818c22014-12-05 13:36:02 +02003086 cmd->cmd_complete = generic_cmd_complete;
3087
Johan Hedberge3f2f922014-08-18 20:33:33 +03003088 err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003089 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03003090 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003091
3092failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003093 hci_dev_unlock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003094 return err;
3095}
3096
Andre Guedes57c14772012-04-24 21:02:50 -03003097static u8 link_to_bdaddr(u8 link_type, u8 addr_type)
Johan Hedberg4c659c32011-11-07 23:13:39 +02003098{
3099 switch (link_type) {
3100 case LE_LINK:
Johan Hedberg48264f02011-11-09 13:58:58 +02003101 switch (addr_type) {
3102 case ADDR_LE_DEV_PUBLIC:
Andre Guedes591f47f2012-04-24 21:02:49 -03003103 return BDADDR_LE_PUBLIC;
Andre Guedes0ed09142012-04-03 08:46:54 -03003104
Johan Hedberg48264f02011-11-09 13:58:58 +02003105 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03003106 /* Fallback to LE Random address type */
Andre Guedes591f47f2012-04-24 21:02:49 -03003107 return BDADDR_LE_RANDOM;
Johan Hedberg48264f02011-11-09 13:58:58 +02003108 }
Andre Guedes0ed09142012-04-03 08:46:54 -03003109
Johan Hedberg4c659c32011-11-07 23:13:39 +02003110 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03003111 /* Fallback to BR/EDR type */
Andre Guedes591f47f2012-04-24 21:02:49 -03003112 return BDADDR_BREDR;
Johan Hedberg4c659c32011-11-07 23:13:39 +02003113 }
3114}
3115
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003116static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
3117 u16 data_len)
Johan Hedberg2784eb42011-01-21 13:56:35 +02003118{
Johan Hedberg2784eb42011-01-21 13:56:35 +02003119 struct mgmt_rp_get_connections *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02003120 struct hci_conn *c;
Johan Hedberga38528f2011-01-22 06:46:43 +02003121 size_t rp_len;
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003122 int err;
3123 u16 i;
Johan Hedberg2784eb42011-01-21 13:56:35 +02003124
3125 BT_DBG("");
3126
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003127 hci_dev_lock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02003128
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003129 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003130 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
3131 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003132 goto unlock;
3133 }
3134
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003135 i = 0;
Johan Hedbergb644ba32012-01-17 21:48:47 +02003136 list_for_each_entry(c, &hdev->conn_hash.list, list) {
3137 if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003138 i++;
Johan Hedberg2784eb42011-01-21 13:56:35 +02003139 }
3140
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003141 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Andre Guedes92c4c202012-06-07 19:05:44 -03003142 rp = kmalloc(rp_len, GFP_KERNEL);
Johan Hedberga38528f2011-01-22 06:46:43 +02003143 if (!rp) {
Johan Hedberg2784eb42011-01-21 13:56:35 +02003144 err = -ENOMEM;
3145 goto unlock;
3146 }
3147
Johan Hedberg2784eb42011-01-21 13:56:35 +02003148 i = 0;
Johan Hedberg4c659c32011-11-07 23:13:39 +02003149 list_for_each_entry(c, &hdev->conn_hash.list, list) {
Johan Hedbergb644ba32012-01-17 21:48:47 +02003150 if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
3151 continue;
Johan Hedberg4c659c32011-11-07 23:13:39 +02003152 bacpy(&rp->addr[i].bdaddr, &c->dst);
Andre Guedes57c14772012-04-24 21:02:50 -03003153 rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type);
Andre Guedes0ed09142012-04-03 08:46:54 -03003154 if (c->type == SCO_LINK || c->type == ESCO_LINK)
Johan Hedberg4c659c32011-11-07 23:13:39 +02003155 continue;
3156 i++;
3157 }
3158
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02003159 rp->conn_count = cpu_to_le16(i);
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02003160
Johan Hedberg4c659c32011-11-07 23:13:39 +02003161 /* Recalculate length in case of filtered SCO connections, etc */
3162 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Johan Hedberg2784eb42011-01-21 13:56:35 +02003163
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003164 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
3165 rp_len);
Johan Hedberg2784eb42011-01-21 13:56:35 +02003166
Johan Hedberga38528f2011-01-22 06:46:43 +02003167 kfree(rp);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003168
3169unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003170 hci_dev_unlock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02003171 return err;
3172}
3173
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003174static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003175 struct mgmt_cp_pin_code_neg_reply *cp)
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003176{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003177 struct mgmt_pending_cmd *cmd;
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003178 int err;
3179
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003180 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003181 sizeof(*cp));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003182 if (!cmd)
3183 return -ENOMEM;
3184
Johan Hedbergd8457692012-02-17 14:24:57 +02003185 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003186 sizeof(cp->addr.bdaddr), &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003187 if (err < 0)
3188 mgmt_pending_remove(cmd);
3189
3190 return err;
3191}
3192
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003193static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003194 u16 len)
Johan Hedberg980e1a52011-01-22 06:10:07 +02003195{
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003196 struct hci_conn *conn;
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003197 struct mgmt_cp_pin_code_reply *cp = data;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003198 struct hci_cp_pin_code_reply reply;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003199 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003200 int err;
3201
3202 BT_DBG("");
3203
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003204 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003205
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003206 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003207 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3208 MGMT_STATUS_NOT_POWERED);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003209 goto failed;
3210 }
3211
Johan Hedbergd8457692012-02-17 14:24:57 +02003212 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003213 if (!conn) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003214 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3215 MGMT_STATUS_NOT_CONNECTED);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003216 goto failed;
3217 }
3218
3219 if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) {
Johan Hedbergd8457692012-02-17 14:24:57 +02003220 struct mgmt_cp_pin_code_neg_reply ncp;
3221
3222 memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003223
3224 BT_ERR("PIN code is not 16 bytes long");
3225
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003226 err = send_pin_code_neg_reply(sk, hdev, &ncp);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003227 if (err >= 0)
Johan Hedberga69e8372015-03-06 21:08:53 +02003228 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3229 MGMT_STATUS_INVALID_PARAMS);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003230
3231 goto failed;
3232 }
3233
Gustavo F. Padovan00abfe42012-03-01 00:37:10 -03003234 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03003235 if (!cmd) {
3236 err = -ENOMEM;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003237 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03003238 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02003239
Johan Hedberg7776d1d2014-12-05 13:36:03 +02003240 cmd->cmd_complete = addr_cmd_complete;
3241
Johan Hedbergd8457692012-02-17 14:24:57 +02003242 bacpy(&reply.bdaddr, &cp->addr.bdaddr);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003243 reply.pin_len = cp->pin_len;
Waldemar Rymarkiewicz24718ca2011-06-01 17:28:47 +02003244 memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code));
Johan Hedberg980e1a52011-01-22 06:10:07 +02003245
3246 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
3247 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03003248 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003249
3250failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003251 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003252 return err;
3253}
3254
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003255static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
3256 u16 len)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003257{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003258 struct mgmt_cp_set_io_capability *cp = data;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003259
3260 BT_DBG("");
3261
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003262 if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003263 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY,
3264 MGMT_STATUS_INVALID_PARAMS, NULL, 0);
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003265
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003266 hci_dev_lock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003267
3268 hdev->io_capability = cp->io_capability;
3269
3270 BT_DBG("%s IO capability set to 0x%02x", hdev->name,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003271 hdev->io_capability);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003272
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003273 hci_dev_unlock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003274
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003275 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0,
3276 NULL, 0);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003277}
3278
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003279static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003280{
3281 struct hci_dev *hdev = conn->hdev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003282 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003283
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003284 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
Johan Hedberge9a416b2011-02-19 12:05:56 -03003285 if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
3286 continue;
3287
Johan Hedberge9a416b2011-02-19 12:05:56 -03003288 if (cmd->user_data != conn)
3289 continue;
3290
3291 return cmd;
3292 }
3293
3294 return NULL;
3295}
3296
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003297static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003298{
3299 struct mgmt_rp_pair_device rp;
3300 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9df74652014-12-19 22:26:03 +02003301 int err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003302
Johan Hedberg61b1a7f2014-03-20 12:54:16 +02003303 bacpy(&rp.addr.bdaddr, &conn->dst);
3304 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003305
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003306 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE,
3307 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003308
3309 /* So we don't get further callbacks for this connection */
3310 conn->connect_cfm_cb = NULL;
3311 conn->security_cfm_cb = NULL;
3312 conn->disconn_cfm_cb = NULL;
3313
David Herrmann76a68ba2013-04-06 20:28:37 +02003314 hci_conn_drop(conn);
Alfonso Acosta89cbb062014-10-11 21:44:47 +00003315
3316 /* The device is paired so there is no need to remove
3317 * its connection parameters anymore.
3318 */
3319 clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
Johan Hedberg15013ae2014-12-11 21:45:44 +02003320
3321 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02003322
3323 return err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003324}
3325
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003326void mgmt_smp_complete(struct hci_conn *conn, bool complete)
3327{
3328 u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003329 struct mgmt_pending_cmd *cmd;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003330
3331 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003332 if (cmd) {
Johan Hedberg04ab2742014-12-05 13:36:04 +02003333 cmd->cmd_complete(cmd, status);
Johan Hedberga511b352014-12-11 21:45:45 +02003334 mgmt_pending_remove(cmd);
3335 }
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003336}
3337
Johan Hedberge9a416b2011-02-19 12:05:56 -03003338static void pairing_complete_cb(struct hci_conn *conn, u8 status)
3339{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003340 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003341
3342 BT_DBG("status %u", status);
3343
Johan Hedberg56e5cb82011-11-08 20:40:16 +02003344 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003345 if (!cmd) {
Johan Hedberg56e5cb82011-11-08 20:40:16 +02003346 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02003347 return;
3348 }
3349
3350 cmd->cmd_complete(cmd, mgmt_status(status));
3351 mgmt_pending_remove(cmd);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003352}
3353
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003354static void le_pairing_complete_cb(struct hci_conn *conn, u8 status)
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303355{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003356 struct mgmt_pending_cmd *cmd;
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303357
3358 BT_DBG("status %u", status);
3359
3360 if (!status)
3361 return;
3362
3363 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003364 if (!cmd) {
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303365 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02003366 return;
3367 }
3368
3369 cmd->cmd_complete(cmd, mgmt_status(status));
3370 mgmt_pending_remove(cmd);
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303371}
3372
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003373static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003374 u16 len)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003375{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003376 struct mgmt_cp_pair_device *cp = data;
Johan Hedberg1425acb2011-11-11 00:07:35 +02003377 struct mgmt_rp_pair_device rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003378 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003379 u8 sec_level, auth_type;
3380 struct hci_conn *conn;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003381 int err;
3382
3383 BT_DBG("");
3384
Szymon Jancf950a30e2013-01-18 12:48:07 +01003385 memset(&rp, 0, sizeof(rp));
3386 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3387 rp.addr.type = cp->addr.type;
3388
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003389 if (!bdaddr_type_is_valid(cp->addr.type))
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 Hedberg4ee71b22013-01-20 14:27:19 +02003393
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003394 if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003395 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3396 MGMT_STATUS_INVALID_PARAMS,
3397 &rp, sizeof(rp));
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003398
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003399 hci_dev_lock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003400
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003401 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003402 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3403 MGMT_STATUS_NOT_POWERED, &rp,
3404 sizeof(rp));
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003405 goto unlock;
3406 }
3407
Johan Hedberg55e76b32015-03-10 22:34:40 +02003408 if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) {
3409 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3410 MGMT_STATUS_ALREADY_PAIRED, &rp,
3411 sizeof(rp));
3412 goto unlock;
3413 }
3414
Vinicius Costa Gomesc908df32011-09-02 14:51:22 -03003415 sec_level = BT_SECURITY_MEDIUM;
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003416 auth_type = HCI_AT_DEDICATED_BONDING;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003417
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003418 if (cp->addr.type == BDADDR_BREDR) {
Andre Guedes04a6c582014-02-26 20:21:44 -03003419 conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
3420 auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003421 } else {
3422 u8 addr_type;
3423
3424 /* Convert from L2CAP channel address type to HCI address type
3425 */
3426 if (cp->addr.type == BDADDR_LE_PUBLIC)
3427 addr_type = ADDR_LE_DEV_PUBLIC;
3428 else
3429 addr_type = ADDR_LE_DEV_RANDOM;
3430
Marcel Holtmann7c264b12014-06-30 12:34:40 +02003431 /* When pairing a new device, it is expected to remember
3432 * this device for future connections. Adding the connection
3433 * parameter information ahead of time allows tracking
3434 * of the slave preferred values and will speed up any
3435 * further connection establishment.
3436 *
3437 * If connection parameters already exist, then they
3438 * will be kept and this function does nothing.
3439 */
3440 hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type);
3441
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003442 conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type,
Johan Hedberge804d252014-07-16 11:42:28 +03003443 sec_level, HCI_LE_CONN_TIMEOUT,
3444 HCI_ROLE_MASTER);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003445 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003446
Ville Tervo30e76272011-02-22 16:10:53 -03003447 if (IS_ERR(conn)) {
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02003448 int status;
3449
3450 if (PTR_ERR(conn) == -EBUSY)
3451 status = MGMT_STATUS_BUSY;
Lukasz Rymanowskifaa81032015-02-11 12:31:42 +01003452 else if (PTR_ERR(conn) == -EOPNOTSUPP)
3453 status = MGMT_STATUS_NOT_SUPPORTED;
3454 else if (PTR_ERR(conn) == -ECONNREFUSED)
3455 status = MGMT_STATUS_REJECTED;
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02003456 else
3457 status = MGMT_STATUS_CONNECT_FAILED;
3458
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003459 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3460 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003461 goto unlock;
3462 }
3463
3464 if (conn->connect_cfm_cb) {
David Herrmann76a68ba2013-04-06 20:28:37 +02003465 hci_conn_drop(conn);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003466 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3467 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003468 goto unlock;
3469 }
3470
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003471 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003472 if (!cmd) {
3473 err = -ENOMEM;
David Herrmann76a68ba2013-04-06 20:28:37 +02003474 hci_conn_drop(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003475 goto unlock;
3476 }
3477
Johan Hedberg04ab2742014-12-05 13:36:04 +02003478 cmd->cmd_complete = pairing_complete;
3479
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003480 /* For LE, just connecting isn't a proof that the pairing finished */
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003481 if (cp->addr.type == BDADDR_BREDR) {
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003482 conn->connect_cfm_cb = pairing_complete_cb;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003483 conn->security_cfm_cb = pairing_complete_cb;
3484 conn->disconn_cfm_cb = pairing_complete_cb;
3485 } else {
3486 conn->connect_cfm_cb = le_pairing_complete_cb;
3487 conn->security_cfm_cb = le_pairing_complete_cb;
3488 conn->disconn_cfm_cb = le_pairing_complete_cb;
3489 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003490
Johan Hedberge9a416b2011-02-19 12:05:56 -03003491 conn->io_capability = cp->io_cap;
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03003492 cmd->user_data = hci_conn_get(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003493
Johan Hedberg6f78fd42014-07-30 08:35:48 +03003494 if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) &&
Johan Hedberga511b352014-12-11 21:45:45 +02003495 hci_conn_security(conn, sec_level, auth_type, true)) {
3496 cmd->cmd_complete(cmd, 0);
3497 mgmt_pending_remove(cmd);
3498 }
Johan Hedberge9a416b2011-02-19 12:05:56 -03003499
3500 err = 0;
3501
3502unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003503 hci_dev_unlock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003504 return err;
3505}
3506
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003507static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3508 u16 len)
Johan Hedberg28424702012-02-02 04:02:29 +02003509{
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003510 struct mgmt_addr_info *addr = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003511 struct mgmt_pending_cmd *cmd;
Johan Hedberg28424702012-02-02 04:02:29 +02003512 struct hci_conn *conn;
3513 int err;
3514
3515 BT_DBG("");
3516
Johan Hedberg28424702012-02-02 04:02:29 +02003517 hci_dev_lock(hdev);
3518
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003519 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003520 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3521 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003522 goto unlock;
3523 }
3524
Johan Hedberg333ae952015-03-17 13:48:47 +02003525 cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02003526 if (!cmd) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003527 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3528 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003529 goto unlock;
3530 }
3531
3532 conn = cmd->user_data;
3533
3534 if (bacmp(&addr->bdaddr, &conn->dst) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003535 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3536 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003537 goto unlock;
3538 }
3539
Johan Hedberga511b352014-12-11 21:45:45 +02003540 cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED);
3541 mgmt_pending_remove(cmd);
Johan Hedberg28424702012-02-02 04:02:29 +02003542
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003543 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
3544 addr, sizeof(*addr));
Johan Hedberg28424702012-02-02 04:02:29 +02003545unlock:
3546 hci_dev_unlock(hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02003547 return err;
3548}
3549
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003550static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
Johan Hedberg1707c602013-03-15 17:07:15 -05003551 struct mgmt_addr_info *addr, u16 mgmt_op,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003552 u16 hci_op, __le32 passkey)
Johan Hedberga5c29682011-02-19 12:05:57 -03003553{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003554 struct mgmt_pending_cmd *cmd;
Brian Gix0df4c182011-11-16 13:53:13 -08003555 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03003556 int err;
3557
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003558 hci_dev_lock(hdev);
Johan Hedberg08ba5382011-03-16 14:29:34 +02003559
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003560 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003561 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3562 MGMT_STATUS_NOT_POWERED, addr,
3563 sizeof(*addr));
Brian Gix0df4c182011-11-16 13:53:13 -08003564 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003565 }
3566
Johan Hedberg1707c602013-03-15 17:07:15 -05003567 if (addr->type == BDADDR_BREDR)
3568 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr);
Johan Hedberg272d90d2012-02-09 15:26:12 +02003569 else
Johan Hedberg1707c602013-03-15 17:07:15 -05003570 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr);
Brian Gix47c15e22011-11-16 13:53:14 -08003571
Johan Hedberg272d90d2012-02-09 15:26:12 +02003572 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003573 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3574 MGMT_STATUS_NOT_CONNECTED, addr,
3575 sizeof(*addr));
Johan Hedberg272d90d2012-02-09 15:26:12 +02003576 goto done;
3577 }
3578
Johan Hedberg1707c602013-03-15 17:07:15 -05003579 if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
Brian Gix5fe57d92011-12-21 16:12:13 -08003580 err = smp_user_confirm_reply(conn, mgmt_op, passkey);
Brian Gix5fe57d92011-12-21 16:12:13 -08003581 if (!err)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003582 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3583 MGMT_STATUS_SUCCESS, addr,
3584 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003585 else
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003586 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3587 MGMT_STATUS_FAILED, addr,
3588 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003589
Brian Gix47c15e22011-11-16 13:53:14 -08003590 goto done;
3591 }
3592
Johan Hedberg1707c602013-03-15 17:07:15 -05003593 cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr));
Johan Hedberga5c29682011-02-19 12:05:57 -03003594 if (!cmd) {
3595 err = -ENOMEM;
Brian Gix0df4c182011-11-16 13:53:13 -08003596 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003597 }
3598
Johan Hedberg7776d1d2014-12-05 13:36:03 +02003599 cmd->cmd_complete = addr_cmd_complete;
3600
Brian Gix0df4c182011-11-16 13:53:13 -08003601 /* Continue with pairing via HCI */
Brian Gix604086b2011-11-23 08:28:33 -08003602 if (hci_op == HCI_OP_USER_PASSKEY_REPLY) {
3603 struct hci_cp_user_passkey_reply cp;
3604
Johan Hedberg1707c602013-03-15 17:07:15 -05003605 bacpy(&cp.bdaddr, &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003606 cp.passkey = passkey;
3607 err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
3608 } else
Johan Hedberg1707c602013-03-15 17:07:15 -05003609 err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr),
3610 &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003611
Johan Hedberga664b5b2011-02-19 12:06:02 -03003612 if (err < 0)
3613 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03003614
Brian Gix0df4c182011-11-16 13:53:13 -08003615done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003616 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03003617 return err;
3618}
3619
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303620static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
3621 void *data, u16 len)
3622{
3623 struct mgmt_cp_pin_code_neg_reply *cp = data;
3624
3625 BT_DBG("");
3626
Johan Hedberg1707c602013-03-15 17:07:15 -05003627 return user_pairing_resp(sk, hdev, &cp->addr,
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303628 MGMT_OP_PIN_CODE_NEG_REPLY,
3629 HCI_OP_PIN_CODE_NEG_REPLY, 0);
3630}
3631
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003632static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3633 u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003634{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003635 struct mgmt_cp_user_confirm_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003636
3637 BT_DBG("");
3638
3639 if (len != sizeof(*cp))
Johan Hedberga69e8372015-03-06 21:08:53 +02003640 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
3641 MGMT_STATUS_INVALID_PARAMS);
Brian Gix0df4c182011-11-16 13:53:13 -08003642
Johan Hedberg1707c602013-03-15 17:07:15 -05003643 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003644 MGMT_OP_USER_CONFIRM_REPLY,
3645 HCI_OP_USER_CONFIRM_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003646}
3647
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003648static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003649 void *data, u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003650{
Johan Hedbergc9c26592011-12-15 00:47:41 +02003651 struct mgmt_cp_user_confirm_neg_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003652
3653 BT_DBG("");
3654
Johan Hedberg1707c602013-03-15 17:07:15 -05003655 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003656 MGMT_OP_USER_CONFIRM_NEG_REPLY,
3657 HCI_OP_USER_CONFIRM_NEG_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003658}
3659
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003660static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3661 u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003662{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003663 struct mgmt_cp_user_passkey_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003664
3665 BT_DBG("");
3666
Johan Hedberg1707c602013-03-15 17:07:15 -05003667 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003668 MGMT_OP_USER_PASSKEY_REPLY,
3669 HCI_OP_USER_PASSKEY_REPLY, cp->passkey);
Brian Gix604086b2011-11-23 08:28:33 -08003670}
3671
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003672static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003673 void *data, u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003674{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003675 struct mgmt_cp_user_passkey_neg_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003676
3677 BT_DBG("");
3678
Johan Hedberg1707c602013-03-15 17:07:15 -05003679 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003680 MGMT_OP_USER_PASSKEY_NEG_REPLY,
3681 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
Brian Gix604086b2011-11-23 08:28:33 -08003682}
3683
Johan Hedberg13928972013-03-15 17:07:00 -05003684static void update_name(struct hci_request *req)
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003685{
Johan Hedberg13928972013-03-15 17:07:00 -05003686 struct hci_dev *hdev = req->hdev;
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003687 struct hci_cp_write_local_name cp;
3688
Johan Hedberg13928972013-03-15 17:07:00 -05003689 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003690
Johan Hedberg890ea892013-03-15 17:06:52 -05003691 hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp);
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003692}
3693
Marcel Holtmann1904a852015-01-11 13:50:44 -08003694static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg13928972013-03-15 17:07:00 -05003695{
3696 struct mgmt_cp_set_local_name *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003697 struct mgmt_pending_cmd *cmd;
Johan Hedberg13928972013-03-15 17:07:00 -05003698
3699 BT_DBG("status 0x%02x", status);
3700
3701 hci_dev_lock(hdev);
3702
Johan Hedberg333ae952015-03-17 13:48:47 +02003703 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05003704 if (!cmd)
3705 goto unlock;
3706
3707 cp = cmd->param;
3708
3709 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02003710 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
3711 mgmt_status(status));
Johan Hedberg13928972013-03-15 17:07:00 -05003712 else
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003713 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3714 cp, sizeof(*cp));
Johan Hedberg13928972013-03-15 17:07:00 -05003715
3716 mgmt_pending_remove(cmd);
3717
3718unlock:
3719 hci_dev_unlock(hdev);
3720}
3721
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003722static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003723 u16 len)
Johan Hedbergb312b1612011-03-16 14:29:37 +02003724{
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003725 struct mgmt_cp_set_local_name *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003726 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05003727 struct hci_request req;
Johan Hedbergb312b1612011-03-16 14:29:37 +02003728 int err;
3729
3730 BT_DBG("");
3731
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003732 hci_dev_lock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003733
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003734 /* If the old values are the same as the new ones just return a
3735 * direct command complete event.
3736 */
3737 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
3738 !memcmp(hdev->short_name, cp->short_name,
3739 sizeof(hdev->short_name))) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003740 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3741 data, len);
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003742 goto failed;
3743 }
3744
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003745 memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003746
Johan Hedbergb5235a62012-02-21 14:32:24 +02003747 if (!hdev_is_powered(hdev)) {
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003748 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003749
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003750 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3751 data, len);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003752 if (err < 0)
3753 goto failed;
3754
Marcel Holtmannf6b77122015-03-14 19:28:05 -07003755 err = mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev,
3756 data, len, sk);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003757
Johan Hedbergb5235a62012-02-21 14:32:24 +02003758 goto failed;
3759 }
3760
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003761 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003762 if (!cmd) {
3763 err = -ENOMEM;
3764 goto failed;
3765 }
3766
Johan Hedberg13928972013-03-15 17:07:00 -05003767 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3768
Johan Hedberg890ea892013-03-15 17:06:52 -05003769 hci_req_init(&req, hdev);
Johan Hedberg3f985052013-03-15 17:07:02 -05003770
3771 if (lmp_bredr_capable(hdev)) {
3772 update_name(&req);
3773 update_eir(&req);
3774 }
3775
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003776 /* The name is stored in the scan response data and so
3777 * no need to udpate the advertising data here.
3778 */
Johan Hedberg3f985052013-03-15 17:07:02 -05003779 if (lmp_le_capable(hdev))
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003780 update_scan_rsp_data(&req);
Johan Hedberg3f985052013-03-15 17:07:02 -05003781
Johan Hedberg13928972013-03-15 17:07:00 -05003782 err = hci_req_run(&req, set_name_complete);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003783 if (err < 0)
3784 mgmt_pending_remove(cmd);
3785
3786failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003787 hci_dev_unlock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003788 return err;
3789}
3790
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003791static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status,
3792 u16 opcode, struct sk_buff *skb)
3793{
3794 struct mgmt_rp_read_local_oob_data mgmt_rp;
3795 size_t rp_size = sizeof(mgmt_rp);
3796 struct mgmt_pending_cmd *cmd;
3797
3798 BT_DBG("%s status %u", hdev->name, status);
3799
3800 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
3801 if (!cmd)
3802 return;
3803
3804 if (status || !skb) {
3805 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3806 status ? mgmt_status(status) : MGMT_STATUS_FAILED);
3807 goto remove;
3808 }
3809
3810 memset(&mgmt_rp, 0, sizeof(mgmt_rp));
3811
3812 if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
3813 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
3814
3815 if (skb->len < sizeof(*rp)) {
3816 mgmt_cmd_status(cmd->sk, hdev->id,
3817 MGMT_OP_READ_LOCAL_OOB_DATA,
3818 MGMT_STATUS_FAILED);
3819 goto remove;
3820 }
3821
3822 memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash));
3823 memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand));
3824
3825 rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256);
3826 } else {
3827 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
3828
3829 if (skb->len < sizeof(*rp)) {
3830 mgmt_cmd_status(cmd->sk, hdev->id,
3831 MGMT_OP_READ_LOCAL_OOB_DATA,
3832 MGMT_STATUS_FAILED);
3833 goto remove;
3834 }
3835
3836 memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192));
3837 memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192));
3838
3839 memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256));
3840 memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256));
3841 }
3842
3843 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3844 MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size);
3845
3846remove:
3847 mgmt_pending_remove(cmd);
3848}
3849
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003850static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003851 void *data, u16 data_len)
Szymon Jancc35938b2011-03-22 13:12:21 +01003852{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003853 struct mgmt_pending_cmd *cmd;
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003854 struct hci_request req;
Szymon Jancc35938b2011-03-22 13:12:21 +01003855 int err;
3856
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003857 BT_DBG("%s", hdev->name);
Szymon Jancc35938b2011-03-22 13:12:21 +01003858
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003859 hci_dev_lock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003860
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003861 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003862 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3863 MGMT_STATUS_NOT_POWERED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003864 goto unlock;
3865 }
3866
Andre Guedes9a1a1992012-07-24 15:03:48 -03003867 if (!lmp_ssp_capable(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003868 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3869 MGMT_STATUS_NOT_SUPPORTED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003870 goto unlock;
3871 }
3872
Johan Hedberg333ae952015-03-17 13:48:47 +02003873 if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003874 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3875 MGMT_STATUS_BUSY);
Szymon Jancc35938b2011-03-22 13:12:21 +01003876 goto unlock;
3877 }
3878
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003879 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
Szymon Jancc35938b2011-03-22 13:12:21 +01003880 if (!cmd) {
3881 err = -ENOMEM;
3882 goto unlock;
3883 }
3884
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003885 hci_req_init(&req, hdev);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08003886
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003887 if (bredr_sc_enabled(hdev))
3888 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
3889 else
3890 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
3891
3892 err = hci_req_run_skb(&req, read_local_oob_data_complete);
Szymon Jancc35938b2011-03-22 13:12:21 +01003893 if (err < 0)
3894 mgmt_pending_remove(cmd);
3895
3896unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003897 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003898 return err;
3899}
3900
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003901static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003902 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003903{
Johan Hedberg5d57e792015-01-23 10:10:38 +02003904 struct mgmt_addr_info *addr = data;
Szymon Janc2763eda2011-03-22 13:12:22 +01003905 int err;
3906
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003907 BT_DBG("%s ", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003908
Johan Hedberg5d57e792015-01-23 10:10:38 +02003909 if (!bdaddr_type_is_valid(addr->type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003910 return mgmt_cmd_complete(sk, hdev->id,
3911 MGMT_OP_ADD_REMOTE_OOB_DATA,
3912 MGMT_STATUS_INVALID_PARAMS,
3913 addr, sizeof(*addr));
Johan Hedberg5d57e792015-01-23 10:10:38 +02003914
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003915 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003916
Marcel Holtmannec109112014-01-10 02:07:30 -08003917 if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) {
3918 struct mgmt_cp_add_remote_oob_data *cp = data;
3919 u8 status;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003920
Johan Hedbergc19a4952014-11-17 20:52:19 +02003921 if (cp->addr.type != BDADDR_BREDR) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003922 err = mgmt_cmd_complete(sk, hdev->id,
3923 MGMT_OP_ADD_REMOTE_OOB_DATA,
3924 MGMT_STATUS_INVALID_PARAMS,
3925 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02003926 goto unlock;
3927 }
3928
Marcel Holtmannec109112014-01-10 02:07:30 -08003929 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg6928a922014-10-26 20:46:09 +01003930 cp->addr.type, cp->hash,
3931 cp->rand, NULL, NULL);
Marcel Holtmannec109112014-01-10 02:07:30 -08003932 if (err < 0)
3933 status = MGMT_STATUS_FAILED;
3934 else
3935 status = MGMT_STATUS_SUCCESS;
3936
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003937 err = mgmt_cmd_complete(sk, hdev->id,
3938 MGMT_OP_ADD_REMOTE_OOB_DATA, status,
3939 &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08003940 } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) {
3941 struct mgmt_cp_add_remote_oob_ext_data *cp = data;
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003942 u8 *rand192, *hash192, *rand256, *hash256;
Marcel Holtmannec109112014-01-10 02:07:30 -08003943 u8 status;
3944
Johan Hedberg86df9202014-10-26 20:52:27 +01003945 if (bdaddr_type_is_le(cp->addr.type)) {
Johan Hedbergd25b78e2015-01-27 12:55:52 +02003946 /* Enforce zero-valued 192-bit parameters as
3947 * long as legacy SMP OOB isn't implemented.
3948 */
3949 if (memcmp(cp->rand192, ZERO_KEY, 16) ||
3950 memcmp(cp->hash192, ZERO_KEY, 16)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003951 err = mgmt_cmd_complete(sk, hdev->id,
3952 MGMT_OP_ADD_REMOTE_OOB_DATA,
3953 MGMT_STATUS_INVALID_PARAMS,
3954 addr, sizeof(*addr));
Johan Hedbergd25b78e2015-01-27 12:55:52 +02003955 goto unlock;
3956 }
3957
Johan Hedberg86df9202014-10-26 20:52:27 +01003958 rand192 = NULL;
3959 hash192 = NULL;
3960 } else {
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003961 /* In case one of the P-192 values is set to zero,
3962 * then just disable OOB data for P-192.
3963 */
3964 if (!memcmp(cp->rand192, ZERO_KEY, 16) ||
3965 !memcmp(cp->hash192, ZERO_KEY, 16)) {
3966 rand192 = NULL;
3967 hash192 = NULL;
3968 } else {
3969 rand192 = cp->rand192;
3970 hash192 = cp->hash192;
3971 }
3972 }
3973
3974 /* In case one of the P-256 values is set to zero, then just
3975 * disable OOB data for P-256.
3976 */
3977 if (!memcmp(cp->rand256, ZERO_KEY, 16) ||
3978 !memcmp(cp->hash256, ZERO_KEY, 16)) {
3979 rand256 = NULL;
3980 hash256 = NULL;
3981 } else {
3982 rand256 = cp->rand256;
3983 hash256 = cp->hash256;
Johan Hedberg86df9202014-10-26 20:52:27 +01003984 }
3985
Johan Hedberg81328d5c2014-10-26 20:33:47 +01003986 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg86df9202014-10-26 20:52:27 +01003987 cp->addr.type, hash192, rand192,
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003988 hash256, rand256);
Marcel Holtmannec109112014-01-10 02:07:30 -08003989 if (err < 0)
3990 status = MGMT_STATUS_FAILED;
3991 else
3992 status = MGMT_STATUS_SUCCESS;
3993
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003994 err = mgmt_cmd_complete(sk, hdev->id,
3995 MGMT_OP_ADD_REMOTE_OOB_DATA,
3996 status, &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08003997 } else {
3998 BT_ERR("add_remote_oob_data: invalid length of %u bytes", len);
Johan Hedberga69e8372015-03-06 21:08:53 +02003999 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
4000 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannec109112014-01-10 02:07:30 -08004001 }
Szymon Janc2763eda2011-03-22 13:12:22 +01004002
Johan Hedbergc19a4952014-11-17 20:52:19 +02004003unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004004 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01004005 return err;
4006}
4007
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004008static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03004009 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01004010{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004011 struct mgmt_cp_remove_remote_oob_data *cp = data;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02004012 u8 status;
Szymon Janc2763eda2011-03-22 13:12:22 +01004013 int err;
4014
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004015 BT_DBG("%s", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01004016
Johan Hedbergc19a4952014-11-17 20:52:19 +02004017 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004018 return mgmt_cmd_complete(sk, hdev->id,
4019 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
4020 MGMT_STATUS_INVALID_PARAMS,
4021 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02004022
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004023 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01004024
Johan Hedbergeedbd582014-11-15 09:34:23 +02004025 if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
4026 hci_remote_oob_data_clear(hdev);
4027 status = MGMT_STATUS_SUCCESS;
4028 goto done;
4029 }
4030
Johan Hedberg6928a922014-10-26 20:46:09 +01004031 err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type);
Szymon Janc2763eda2011-03-22 13:12:22 +01004032 if (err < 0)
Johan Hedbergbf1e3542012-02-19 13:16:14 +02004033 status = MGMT_STATUS_INVALID_PARAMS;
Szymon Janc2763eda2011-03-22 13:12:22 +01004034 else
Szymon Janca6785be2012-12-13 15:11:21 +01004035 status = MGMT_STATUS_SUCCESS;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02004036
Johan Hedbergeedbd582014-11-15 09:34:23 +02004037done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004038 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
4039 status, &cp->addr, sizeof(cp->addr));
Szymon Janc2763eda2011-03-22 13:12:22 +01004040
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004041 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01004042 return err;
4043}
4044
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004045static bool trigger_bredr_inquiry(struct hci_request *req, u8 *status)
4046{
4047 struct hci_dev *hdev = req->hdev;
4048 struct hci_cp_inquiry cp;
4049 /* General inquiry access code (GIAC) */
4050 u8 lap[3] = { 0x33, 0x8b, 0x9e };
4051
4052 *status = mgmt_bredr_support(hdev);
4053 if (*status)
4054 return false;
4055
4056 if (hci_dev_test_flag(hdev, HCI_INQUIRY)) {
4057 *status = MGMT_STATUS_BUSY;
4058 return false;
4059 }
4060
4061 hci_inquiry_cache_flush(hdev);
4062
4063 memset(&cp, 0, sizeof(cp));
4064 memcpy(&cp.lap, lap, sizeof(cp.lap));
4065 cp.length = DISCOV_BREDR_INQUIRY_LEN;
4066
4067 hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp);
4068
4069 return true;
4070}
4071
4072static bool trigger_le_scan(struct hci_request *req, u16 interval, u8 *status)
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004073{
Marcel Holtmann80190442014-12-04 11:36:36 +01004074 struct hci_dev *hdev = req->hdev;
4075 struct hci_cp_le_set_scan_param param_cp;
4076 struct hci_cp_le_set_scan_enable enable_cp;
Marcel Holtmann80190442014-12-04 11:36:36 +01004077 u8 own_addr_type;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004078 int err;
4079
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004080 *status = mgmt_le_support(hdev);
4081 if (*status)
4082 return false;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004083
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004084 if (hci_dev_test_flag(hdev, HCI_LE_ADV)) {
4085 /* Don't let discovery abort an outgoing connection attempt
4086 * that's using directed advertising.
4087 */
4088 if (hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT)) {
4089 *status = MGMT_STATUS_REJECTED;
Marcel Holtmann80190442014-12-04 11:36:36 +01004090 return false;
4091 }
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004092
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004093 disable_advertising(req);
4094 }
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004095
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004096 /* If controller is scanning, it means the background scanning is
4097 * running. Thus, we should temporarily stop it in order to set the
4098 * discovery scanning parameters.
4099 */
4100 if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
4101 hci_req_add_le_scan_disable(req);
4102
4103 /* All active scans will be done with either a resolvable private
4104 * address (when privacy feature has been enabled) or non-resolvable
4105 * private address.
4106 */
4107 err = hci_update_random_address(req, true, &own_addr_type);
4108 if (err < 0) {
4109 *status = MGMT_STATUS_FAILED;
4110 return false;
4111 }
4112
4113 memset(&param_cp, 0, sizeof(param_cp));
4114 param_cp.type = LE_SCAN_ACTIVE;
4115 param_cp.interval = cpu_to_le16(interval);
4116 param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN);
4117 param_cp.own_address_type = own_addr_type;
4118
4119 hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
4120 &param_cp);
4121
4122 memset(&enable_cp, 0, sizeof(enable_cp));
4123 enable_cp.enable = LE_SCAN_ENABLE;
4124 enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
4125
4126 hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
4127 &enable_cp);
4128
4129 return true;
4130}
4131
4132static bool trigger_discovery(struct hci_request *req, u8 *status)
4133{
4134 struct hci_dev *hdev = req->hdev;
4135
4136 switch (hdev->discovery.type) {
4137 case DISCOV_TYPE_BREDR:
4138 if (!trigger_bredr_inquiry(req, status))
4139 return false;
Marcel Holtmann80190442014-12-04 11:36:36 +01004140 break;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004141
Marcel Holtmann80190442014-12-04 11:36:36 +01004142 case DISCOV_TYPE_INTERLEAVED:
Jakub Pawlowski07d23342015-03-17 09:04:14 -07004143 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
4144 &hdev->quirks)) {
4145 /* During simultaneous discovery, we double LE scan
4146 * interval. We must leave some time for the controller
4147 * to do BR/EDR inquiry.
4148 */
4149 if (!trigger_le_scan(req, DISCOV_LE_SCAN_INT * 2,
4150 status))
4151 return false;
4152
4153 if (!trigger_bredr_inquiry(req, status))
4154 return false;
4155
4156 return true;
4157 }
4158
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004159 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Marcel Holtmann80190442014-12-04 11:36:36 +01004160 *status = MGMT_STATUS_NOT_SUPPORTED;
4161 return false;
4162 }
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004163 /* fall through */
Marcel Holtmann80190442014-12-04 11:36:36 +01004164
Jakub Pawlowski812abb12015-03-17 09:04:13 -07004165 case DISCOV_TYPE_LE:
4166 if (!trigger_le_scan(req, DISCOV_LE_SCAN_INT, status))
Marcel Holtmann80190442014-12-04 11:36:36 +01004167 return false;
Marcel Holtmann80190442014-12-04 11:36:36 +01004168 break;
4169
4170 default:
4171 *status = MGMT_STATUS_INVALID_PARAMS;
4172 return false;
4173 }
4174
4175 return true;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03004176}
4177
Marcel Holtmann1904a852015-01-11 13:50:44 -08004178static void start_discovery_complete(struct hci_dev *hdev, u8 status,
4179 u16 opcode)
Andre Guedes7c307722013-04-30 15:29:28 -03004180{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004181 struct mgmt_pending_cmd *cmd;
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004182 unsigned long timeout;
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01004183
Andre Guedes7c307722013-04-30 15:29:28 -03004184 BT_DBG("status %d", status);
4185
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004186 hci_dev_lock(hdev);
4187
Johan Hedberg333ae952015-03-17 13:48:47 +02004188 cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004189 if (!cmd)
Johan Hedberg333ae952015-03-17 13:48:47 +02004190 cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004191
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004192 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02004193 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004194 mgmt_pending_remove(cmd);
Andre Guedes7c307722013-04-30 15:29:28 -03004195 }
4196
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004197 if (status) {
4198 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4199 goto unlock;
4200 }
4201
Andre Guedes7c307722013-04-30 15:29:28 -03004202 hci_discovery_set_state(hdev, DISCOVERY_FINDING);
Andre Guedes7c307722013-04-30 15:29:28 -03004203
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08004204 /* If the scan involves LE scan, pick proper timeout to schedule
4205 * hdev->le_scan_disable that will stop it.
4206 */
Andre Guedes7c307722013-04-30 15:29:28 -03004207 switch (hdev->discovery.type) {
4208 case DISCOV_TYPE_LE:
Lukasz Rymanowski3d5a76f2014-03-27 20:55:21 +01004209 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
Andre Guedes7c307722013-04-30 15:29:28 -03004210 break;
Andre Guedes7c307722013-04-30 15:29:28 -03004211 case DISCOV_TYPE_INTERLEAVED:
Jakub Pawlowski07d23342015-03-17 09:04:14 -07004212 /* When running simultaneous discovery, the LE scanning time
4213 * should occupy the whole discovery time sine BR/EDR inquiry
4214 * and LE scanning are scheduled by the controller.
4215 *
4216 * For interleaving discovery in comparison, BR/EDR inquiry
4217 * and LE scanning are done sequentially with separate
4218 * timeouts.
4219 */
4220 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
4221 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
4222 else
4223 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
Andre Guedes7c307722013-04-30 15:29:28 -03004224 break;
Andre Guedes7c307722013-04-30 15:29:28 -03004225 case DISCOV_TYPE_BREDR:
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004226 timeout = 0;
Andre Guedes7c307722013-04-30 15:29:28 -03004227 break;
Andre Guedes7c307722013-04-30 15:29:28 -03004228 default:
4229 BT_ERR("Invalid discovery type %d", hdev->discovery.type);
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004230 timeout = 0;
4231 break;
Andre Guedes7c307722013-04-30 15:29:28 -03004232 }
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01004233
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08004234 if (timeout) {
4235 /* When service discovery is used and the controller has
4236 * a strict duplicate filter, it is important to remember
4237 * the start and duration of the scan. This is required
4238 * for restarting scanning during the discovery phase.
4239 */
4240 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER,
4241 &hdev->quirks) &&
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08004242 hdev->discovery.result_filtering) {
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08004243 hdev->discovery.scan_start = jiffies;
4244 hdev->discovery.scan_duration = timeout;
4245 }
4246
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004247 queue_delayed_work(hdev->workqueue,
4248 &hdev->le_scan_disable, timeout);
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08004249 }
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01004250
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004251unlock:
4252 hci_dev_unlock(hdev);
Andre Guedes7c307722013-04-30 15:29:28 -03004253}
4254
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004255static int start_discovery(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004256 void *data, u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04004257{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004258 struct mgmt_cp_start_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004259 struct mgmt_pending_cmd *cmd;
Andre Guedes7c307722013-04-30 15:29:28 -03004260 struct hci_request req;
Marcel Holtmann80190442014-12-04 11:36:36 +01004261 u8 status;
Johan Hedberg14a53662011-04-27 10:29:56 -04004262 int err;
4263
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004264 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04004265
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004266 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004267
Johan Hedberg4b34ee782012-02-21 14:13:02 +02004268 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004269 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4270 MGMT_STATUS_NOT_POWERED,
4271 &cp->type, sizeof(cp->type));
Johan Hedbergbd2d1332011-11-07 23:13:37 +02004272 goto failed;
4273 }
4274
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004275 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004276 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004277 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4278 MGMT_STATUS_BUSY, &cp->type,
4279 sizeof(cp->type));
Andre Guedes642be6c2012-03-21 00:03:37 -03004280 goto failed;
4281 }
4282
Johan Hedberg2922a942014-12-05 13:36:06 +02004283 cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, data, len);
Johan Hedberg14a53662011-04-27 10:29:56 -04004284 if (!cmd) {
4285 err = -ENOMEM;
4286 goto failed;
4287 }
4288
Johan Hedberg2922a942014-12-05 13:36:06 +02004289 cmd->cmd_complete = generic_cmd_complete;
4290
Marcel Holtmann22078802014-12-05 11:45:22 +01004291 /* Clear the discovery filter first to free any previously
4292 * allocated memory for the UUID list.
4293 */
4294 hci_discovery_filter_clear(hdev);
4295
Andre Guedes4aab14e2012-02-17 20:39:36 -03004296 hdev->discovery.type = cp->type;
Marcel Holtmannda25cf62014-12-05 13:03:35 +01004297 hdev->discovery.report_invalid_rssi = false;
Andre Guedes4aab14e2012-02-17 20:39:36 -03004298
Andre Guedes7c307722013-04-30 15:29:28 -03004299 hci_req_init(&req, hdev);
4300
Marcel Holtmann80190442014-12-04 11:36:36 +01004301 if (!trigger_discovery(&req, &status)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004302 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4303 status, &cp->type, sizeof(cp->type));
Johan Hedberg04106752013-01-10 14:54:09 +02004304 mgmt_pending_remove(cmd);
4305 goto failed;
Andre Guedesf39799f2012-02-17 20:39:35 -03004306 }
Andre Guedes3fd24152012-02-03 17:48:01 -03004307
Andre Guedes7c307722013-04-30 15:29:28 -03004308 err = hci_req_run(&req, start_discovery_complete);
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004309 if (err < 0) {
Johan Hedberg14a53662011-04-27 10:29:56 -04004310 mgmt_pending_remove(cmd);
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004311 goto failed;
4312 }
4313
4314 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
Johan Hedberg14a53662011-04-27 10:29:56 -04004315
4316failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004317 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004318 return err;
4319}
4320
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004321static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd,
4322 u8 status)
Andre Guedes1183fdc2013-04-30 15:29:35 -03004323{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004324 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
4325 cmd->param, 1);
Johan Hedberg2922a942014-12-05 13:36:06 +02004326}
4327
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004328static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
4329 void *data, u16 len)
4330{
4331 struct mgmt_cp_start_service_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004332 struct mgmt_pending_cmd *cmd;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004333 struct hci_request req;
4334 const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16);
4335 u16 uuid_count, expected_len;
4336 u8 status;
Andre Guedes1183fdc2013-04-30 15:29:35 -03004337 int err;
4338
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004339 BT_DBG("%s", hdev->name);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004340
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004341 hci_dev_lock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004342
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004343 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004344 err = mgmt_cmd_complete(sk, hdev->id,
4345 MGMT_OP_START_SERVICE_DISCOVERY,
4346 MGMT_STATUS_NOT_POWERED,
4347 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004348 goto failed;
4349 }
4350
4351 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004352 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004353 err = mgmt_cmd_complete(sk, hdev->id,
4354 MGMT_OP_START_SERVICE_DISCOVERY,
4355 MGMT_STATUS_BUSY, &cp->type,
4356 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004357 goto failed;
4358 }
4359
4360 uuid_count = __le16_to_cpu(cp->uuid_count);
4361 if (uuid_count > max_uuid_count) {
4362 BT_ERR("service_discovery: too big uuid_count value %u",
4363 uuid_count);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004364 err = mgmt_cmd_complete(sk, hdev->id,
4365 MGMT_OP_START_SERVICE_DISCOVERY,
4366 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4367 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004368 goto failed;
4369 }
4370
4371 expected_len = sizeof(*cp) + uuid_count * 16;
4372 if (expected_len != len) {
4373 BT_ERR("service_discovery: expected %u bytes, got %u bytes",
4374 expected_len, len);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004375 err = mgmt_cmd_complete(sk, hdev->id,
4376 MGMT_OP_START_SERVICE_DISCOVERY,
4377 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4378 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004379 goto failed;
4380 }
4381
4382 cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY,
Johan Hedberg2922a942014-12-05 13:36:06 +02004383 hdev, data, len);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004384 if (!cmd) {
4385 err = -ENOMEM;
4386 goto failed;
4387 }
4388
Johan Hedberg2922a942014-12-05 13:36:06 +02004389 cmd->cmd_complete = service_discovery_cmd_complete;
4390
Marcel Holtmann22078802014-12-05 11:45:22 +01004391 /* Clear the discovery filter first to free any previously
4392 * allocated memory for the UUID list.
4393 */
4394 hci_discovery_filter_clear(hdev);
4395
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08004396 hdev->discovery.result_filtering = true;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004397 hdev->discovery.type = cp->type;
4398 hdev->discovery.rssi = cp->rssi;
4399 hdev->discovery.uuid_count = uuid_count;
4400
4401 if (uuid_count > 0) {
4402 hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16,
4403 GFP_KERNEL);
4404 if (!hdev->discovery.uuids) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004405 err = mgmt_cmd_complete(sk, hdev->id,
4406 MGMT_OP_START_SERVICE_DISCOVERY,
4407 MGMT_STATUS_FAILED,
4408 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004409 mgmt_pending_remove(cmd);
4410 goto failed;
4411 }
4412 }
4413
4414 hci_req_init(&req, hdev);
4415
4416 if (!trigger_discovery(&req, &status)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004417 err = mgmt_cmd_complete(sk, hdev->id,
4418 MGMT_OP_START_SERVICE_DISCOVERY,
4419 status, &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004420 mgmt_pending_remove(cmd);
4421 goto failed;
4422 }
4423
4424 err = hci_req_run(&req, start_discovery_complete);
4425 if (err < 0) {
4426 mgmt_pending_remove(cmd);
4427 goto failed;
4428 }
4429
4430 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
4431
4432failed:
4433 hci_dev_unlock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004434 return err;
4435}
4436
Marcel Holtmann1904a852015-01-11 13:50:44 -08004437static void stop_discovery_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Andre Guedes0e05bba2013-04-30 15:29:33 -03004438{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004439 struct mgmt_pending_cmd *cmd;
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004440
Andre Guedes0e05bba2013-04-30 15:29:33 -03004441 BT_DBG("status %d", status);
4442
4443 hci_dev_lock(hdev);
4444
Johan Hedberg333ae952015-03-17 13:48:47 +02004445 cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004446 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02004447 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004448 mgmt_pending_remove(cmd);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004449 }
4450
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004451 if (!status)
4452 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004453
Andre Guedes0e05bba2013-04-30 15:29:33 -03004454 hci_dev_unlock(hdev);
4455}
4456
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004457static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004458 u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04004459{
Johan Hedbergd9306502012-02-20 23:25:18 +02004460 struct mgmt_cp_stop_discovery *mgmt_cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004461 struct mgmt_pending_cmd *cmd;
Andre Guedes0e05bba2013-04-30 15:29:33 -03004462 struct hci_request req;
Johan Hedberg14a53662011-04-27 10:29:56 -04004463 int err;
4464
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004465 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04004466
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004467 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004468
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004469 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004470 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4471 MGMT_STATUS_REJECTED, &mgmt_cp->type,
4472 sizeof(mgmt_cp->type));
Johan Hedbergd9306502012-02-20 23:25:18 +02004473 goto unlock;
4474 }
4475
4476 if (hdev->discovery.type != mgmt_cp->type) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004477 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4478 MGMT_STATUS_INVALID_PARAMS,
4479 &mgmt_cp->type, sizeof(mgmt_cp->type));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004480 goto unlock;
Johan Hedbergff9ef572012-01-04 14:23:45 +02004481 }
4482
Johan Hedberg2922a942014-12-05 13:36:06 +02004483 cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len);
Johan Hedberg14a53662011-04-27 10:29:56 -04004484 if (!cmd) {
4485 err = -ENOMEM;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004486 goto unlock;
Johan Hedberg14a53662011-04-27 10:29:56 -04004487 }
4488
Johan Hedberg2922a942014-12-05 13:36:06 +02004489 cmd->cmd_complete = generic_cmd_complete;
4490
Andre Guedes0e05bba2013-04-30 15:29:33 -03004491 hci_req_init(&req, hdev);
4492
Johan Hedberg21a60d32014-06-10 14:05:58 +03004493 hci_stop_discovery(&req);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004494
Johan Hedberg21a60d32014-06-10 14:05:58 +03004495 err = hci_req_run(&req, stop_discovery_complete);
4496 if (!err) {
4497 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004498 goto unlock;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004499 }
4500
Johan Hedberg21a60d32014-06-10 14:05:58 +03004501 mgmt_pending_remove(cmd);
4502
4503 /* If no HCI commands were sent we're done */
4504 if (err == -ENODATA) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004505 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0,
4506 &mgmt_cp->type, sizeof(mgmt_cp->type));
Johan Hedberg21a60d32014-06-10 14:05:58 +03004507 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4508 }
Johan Hedberg14a53662011-04-27 10:29:56 -04004509
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004510unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004511 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004512 return err;
4513}
4514
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004515static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004516 u16 len)
Johan Hedberg561aafb2012-01-04 13:31:59 +02004517{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004518 struct mgmt_cp_confirm_name *cp = data;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004519 struct inquiry_entry *e;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004520 int err;
4521
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004522 BT_DBG("%s", hdev->name);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004523
Johan Hedberg561aafb2012-01-04 13:31:59 +02004524 hci_dev_lock(hdev);
4525
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004526 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004527 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4528 MGMT_STATUS_FAILED, &cp->addr,
4529 sizeof(cp->addr));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004530 goto failed;
4531 }
4532
Johan Hedberga198e7b2012-02-17 14:27:06 +02004533 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004534 if (!e) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004535 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4536 MGMT_STATUS_INVALID_PARAMS, &cp->addr,
4537 sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02004538 goto failed;
4539 }
4540
4541 if (cp->name_known) {
4542 e->name_state = NAME_KNOWN;
4543 list_del(&e->list);
4544 } else {
4545 e->name_state = NAME_NEEDED;
Johan Hedberga3d4e202012-01-09 00:53:02 +02004546 hci_inquiry_cache_update_resolve(hdev, e);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004547 }
4548
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004549 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0,
4550 &cp->addr, sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02004551
4552failed:
4553 hci_dev_unlock(hdev);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004554 return err;
4555}
4556
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004557static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004558 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03004559{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004560 struct mgmt_cp_block_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004561 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03004562 int err;
4563
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004564 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03004565
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004566 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004567 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
4568 MGMT_STATUS_INVALID_PARAMS,
4569 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004570
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004571 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03004572
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004573 err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr,
4574 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004575 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004576 status = MGMT_STATUS_FAILED;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004577 goto done;
4578 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004579
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004580 mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4581 sk);
4582 status = MGMT_STATUS_SUCCESS;
4583
4584done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004585 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
4586 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03004587
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004588 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03004589
4590 return err;
4591}
4592
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004593static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004594 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03004595{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004596 struct mgmt_cp_unblock_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004597 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03004598 int err;
4599
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004600 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03004601
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004602 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004603 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
4604 MGMT_STATUS_INVALID_PARAMS,
4605 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004606
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004607 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03004608
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004609 err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr,
4610 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004611 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004612 status = MGMT_STATUS_INVALID_PARAMS;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004613 goto done;
4614 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004615
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004616 mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4617 sk);
4618 status = MGMT_STATUS_SUCCESS;
4619
4620done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004621 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
4622 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03004623
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004624 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03004625
4626 return err;
4627}
4628
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004629static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
4630 u16 len)
4631{
4632 struct mgmt_cp_set_device_id *cp = data;
Johan Hedberg890ea892013-03-15 17:06:52 -05004633 struct hci_request req;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004634 int err;
Szymon Jancc72d4b82012-03-16 16:02:57 +01004635 __u16 source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004636
4637 BT_DBG("%s", hdev->name);
4638
Szymon Jancc72d4b82012-03-16 16:02:57 +01004639 source = __le16_to_cpu(cp->source);
4640
4641 if (source > 0x0002)
Johan Hedberga69e8372015-03-06 21:08:53 +02004642 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
4643 MGMT_STATUS_INVALID_PARAMS);
Szymon Jancc72d4b82012-03-16 16:02:57 +01004644
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004645 hci_dev_lock(hdev);
4646
Szymon Jancc72d4b82012-03-16 16:02:57 +01004647 hdev->devid_source = source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004648 hdev->devid_vendor = __le16_to_cpu(cp->vendor);
4649 hdev->devid_product = __le16_to_cpu(cp->product);
4650 hdev->devid_version = __le16_to_cpu(cp->version);
4651
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004652 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0,
4653 NULL, 0);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004654
Johan Hedberg890ea892013-03-15 17:06:52 -05004655 hci_req_init(&req, hdev);
4656 update_eir(&req);
4657 hci_req_run(&req, NULL);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004658
4659 hci_dev_unlock(hdev);
4660
4661 return err;
4662}
4663
Arman Uguray24b4f382015-03-23 15:57:12 -07004664static void enable_advertising_instance(struct hci_dev *hdev, u8 status,
4665 u16 opcode)
4666{
4667 BT_DBG("status %d", status);
4668}
4669
Marcel Holtmann1904a852015-01-11 13:50:44 -08004670static void set_advertising_complete(struct hci_dev *hdev, u8 status,
4671 u16 opcode)
Johan Hedberg4375f102013-09-25 13:26:10 +03004672{
4673 struct cmd_lookup match = { NULL, hdev };
Arman Uguray24b4f382015-03-23 15:57:12 -07004674 struct hci_request req;
Johan Hedberg4375f102013-09-25 13:26:10 +03004675
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304676 hci_dev_lock(hdev);
4677
Johan Hedberg4375f102013-09-25 13:26:10 +03004678 if (status) {
4679 u8 mgmt_err = mgmt_status(status);
4680
4681 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev,
4682 cmd_status_rsp, &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304683 goto unlock;
Johan Hedberg4375f102013-09-25 13:26:10 +03004684 }
4685
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004686 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004687 hci_dev_set_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03004688 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004689 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03004690
Johan Hedberg4375f102013-09-25 13:26:10 +03004691 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
4692 &match);
4693
4694 new_settings(hdev, match.sk);
4695
4696 if (match.sk)
4697 sock_put(match.sk);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304698
Arman Uguray24b4f382015-03-23 15:57:12 -07004699 /* If "Set Advertising" was just disabled and instance advertising was
4700 * set up earlier, then enable the advertising instance.
4701 */
4702 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
4703 !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
4704 goto unlock;
4705
4706 hci_req_init(&req, hdev);
4707
4708 update_adv_data(&req);
4709 enable_advertising(&req);
4710
4711 if (hci_req_run(&req, enable_advertising_instance) < 0)
4712 BT_ERR("Failed to re-configure advertising");
4713
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304714unlock:
4715 hci_dev_unlock(hdev);
Johan Hedberg4375f102013-09-25 13:26:10 +03004716}
4717
Marcel Holtmann21b51872013-10-10 09:47:53 -07004718static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
4719 u16 len)
Johan Hedberg4375f102013-09-25 13:26:10 +03004720{
4721 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004722 struct mgmt_pending_cmd *cmd;
Johan Hedberg4375f102013-09-25 13:26:10 +03004723 struct hci_request req;
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004724 u8 val, status;
Johan Hedberg4375f102013-09-25 13:26:10 +03004725 int err;
4726
4727 BT_DBG("request for %s", hdev->name);
4728
Johan Hedberge6fe7982013-10-02 15:45:22 +03004729 status = mgmt_le_support(hdev);
4730 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02004731 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4732 status);
Johan Hedberg4375f102013-09-25 13:26:10 +03004733
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004734 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02004735 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4736 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4375f102013-09-25 13:26:10 +03004737
4738 hci_dev_lock(hdev);
4739
4740 val = !!cp->val;
Johan Hedberg4375f102013-09-25 13:26:10 +03004741
Johan Hedbergf74ca9b2013-10-08 15:52:18 +02004742 /* The following conditions are ones which mean that we should
4743 * not do any HCI communication but directly send a mgmt
4744 * response to user space (after toggling the flag if
4745 * necessary).
4746 */
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004747 if (!hdev_is_powered(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004748 (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
4749 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) ||
Johan Hedberge8bb6b92014-07-08 15:07:53 +03004750 hci_conn_num(hdev, LE_LINK) > 0 ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004751 (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Johan Hedberge8bb6b92014-07-08 15:07:53 +03004752 hdev->le_scan_type == LE_SCAN_ACTIVE)) {
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004753 bool changed;
Johan Hedberg4375f102013-09-25 13:26:10 +03004754
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004755 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07004756 changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004757 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004758 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004759 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004760 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004761 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07004762 changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004763 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Johan Hedberg4375f102013-09-25 13:26:10 +03004764 }
4765
4766 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
4767 if (err < 0)
4768 goto unlock;
4769
4770 if (changed)
4771 err = new_settings(hdev, sk);
4772
4773 goto unlock;
4774 }
4775
Johan Hedberg333ae952015-03-17 13:48:47 +02004776 if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
4777 pending_find(MGMT_OP_SET_LE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004778 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4779 MGMT_STATUS_BUSY);
Johan Hedberg4375f102013-09-25 13:26:10 +03004780 goto unlock;
4781 }
4782
4783 cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
4784 if (!cmd) {
4785 err = -ENOMEM;
4786 goto unlock;
4787 }
4788
4789 hci_req_init(&req, hdev);
4790
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004791 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004792 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004793 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004794 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004795
Arman Uguray24b4f382015-03-23 15:57:12 -07004796 if (val) {
4797 /* Switch to instance "0" for the Set Advertising setting. */
Florian Grandelefae0022015-06-18 03:16:37 +02004798 update_inst_adv_data(&req, 0x00);
4799 update_inst_scan_rsp_data(&req, 0x00);
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07004800 enable_advertising(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07004801 } else {
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07004802 disable_advertising(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07004803 }
Johan Hedberg4375f102013-09-25 13:26:10 +03004804
4805 err = hci_req_run(&req, set_advertising_complete);
4806 if (err < 0)
4807 mgmt_pending_remove(cmd);
4808
4809unlock:
4810 hci_dev_unlock(hdev);
4811 return err;
4812}
4813
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004814static int set_static_address(struct sock *sk, struct hci_dev *hdev,
4815 void *data, u16 len)
4816{
4817 struct mgmt_cp_set_static_address *cp = data;
4818 int err;
4819
4820 BT_DBG("%s", hdev->name);
4821
Marcel Holtmann62af4442013-10-02 22:10:32 -07004822 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004823 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4824 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004825
4826 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004827 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4828 MGMT_STATUS_REJECTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004829
4830 if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
4831 if (!bacmp(&cp->bdaddr, BDADDR_NONE))
Johan Hedberga69e8372015-03-06 21:08:53 +02004832 return mgmt_cmd_status(sk, hdev->id,
4833 MGMT_OP_SET_STATIC_ADDRESS,
4834 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004835
4836 /* Two most significant bits shall be set */
4837 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
Johan Hedberga69e8372015-03-06 21:08:53 +02004838 return mgmt_cmd_status(sk, hdev->id,
4839 MGMT_OP_SET_STATIC_ADDRESS,
4840 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004841 }
4842
4843 hci_dev_lock(hdev);
4844
4845 bacpy(&hdev->static_addr, &cp->bdaddr);
4846
Marcel Holtmann93690c22015-03-06 10:11:21 -08004847 err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev);
4848 if (err < 0)
4849 goto unlock;
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004850
Marcel Holtmann93690c22015-03-06 10:11:21 -08004851 err = new_settings(hdev, sk);
4852
4853unlock:
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004854 hci_dev_unlock(hdev);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004855 return err;
4856}
4857
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004858static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
4859 void *data, u16 len)
4860{
4861 struct mgmt_cp_set_scan_params *cp = data;
4862 __u16 interval, window;
4863 int err;
4864
4865 BT_DBG("%s", hdev->name);
4866
4867 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004868 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4869 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004870
4871 interval = __le16_to_cpu(cp->interval);
4872
4873 if (interval < 0x0004 || interval > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02004874 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4875 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004876
4877 window = __le16_to_cpu(cp->window);
4878
4879 if (window < 0x0004 || window > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02004880 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4881 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004882
Marcel Holtmann899e1072013-10-14 09:55:32 -07004883 if (window > interval)
Johan Hedberga69e8372015-03-06 21:08:53 +02004884 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4885 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann899e1072013-10-14 09:55:32 -07004886
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004887 hci_dev_lock(hdev);
4888
4889 hdev->le_scan_interval = interval;
4890 hdev->le_scan_window = window;
4891
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004892 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0,
4893 NULL, 0);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004894
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03004895 /* If background scan is running, restart it so new parameters are
4896 * loaded.
4897 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004898 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03004899 hdev->discovery.state == DISCOVERY_STOPPED) {
4900 struct hci_request req;
4901
4902 hci_req_init(&req, hdev);
4903
4904 hci_req_add_le_scan_disable(&req);
4905 hci_req_add_le_passive_scan(&req);
4906
4907 hci_req_run(&req, NULL);
4908 }
4909
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004910 hci_dev_unlock(hdev);
4911
4912 return err;
4913}
4914
Marcel Holtmann1904a852015-01-11 13:50:44 -08004915static void fast_connectable_complete(struct hci_dev *hdev, u8 status,
4916 u16 opcode)
Johan Hedberg33e38b32013-03-15 17:07:05 -05004917{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004918 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004919
4920 BT_DBG("status 0x%02x", status);
4921
4922 hci_dev_lock(hdev);
4923
Johan Hedberg333ae952015-03-17 13:48:47 +02004924 cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004925 if (!cmd)
4926 goto unlock;
4927
4928 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004929 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4930 mgmt_status(status));
Johan Hedberg33e38b32013-03-15 17:07:05 -05004931 } else {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004932 struct mgmt_mode *cp = cmd->param;
4933
4934 if (cp->val)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004935 hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004936 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004937 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004938
Johan Hedberg33e38b32013-03-15 17:07:05 -05004939 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
4940 new_settings(hdev, cmd->sk);
4941 }
4942
4943 mgmt_pending_remove(cmd);
4944
4945unlock:
4946 hci_dev_unlock(hdev);
4947}
4948
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004949static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004950 void *data, u16 len)
Antti Julkuf6422ec2011-06-22 13:11:56 +03004951{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004952 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004953 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004954 struct hci_request req;
Antti Julkuf6422ec2011-06-22 13:11:56 +03004955 int err;
4956
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004957 BT_DBG("%s", hdev->name);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004958
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004959 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
Johan Hedberg56f87902013-10-02 13:43:13 +03004960 hdev->hci_ver < BLUETOOTH_VER_1_2)
Johan Hedberga69e8372015-03-06 21:08:53 +02004961 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4962 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03004963
Johan Hedberga7e80f22013-01-09 16:05:19 +02004964 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02004965 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4966 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02004967
Antti Julkuf6422ec2011-06-22 13:11:56 +03004968 hci_dev_lock(hdev);
4969
Johan Hedberg333ae952015-03-17 13:48:47 +02004970 if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004971 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4972 MGMT_STATUS_BUSY);
Johan Hedberg05cbf292013-03-15 17:07:07 -05004973 goto unlock;
4974 }
4975
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004976 if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004977 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4978 hdev);
4979 goto unlock;
4980 }
4981
Johan Hedberg406ef2a2015-03-10 20:14:27 +02004982 if (!hdev_is_powered(hdev)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07004983 hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg406ef2a2015-03-10 20:14:27 +02004984 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4985 hdev);
4986 new_settings(hdev, sk);
4987 goto unlock;
4988 }
4989
Johan Hedberg33e38b32013-03-15 17:07:05 -05004990 cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev,
4991 data, len);
4992 if (!cmd) {
4993 err = -ENOMEM;
4994 goto unlock;
4995 }
4996
4997 hci_req_init(&req, hdev);
4998
Johan Hedberg406d7802013-03-15 17:07:09 -05004999 write_fast_connectable(&req, cp->val);
Johan Hedberg33e38b32013-03-15 17:07:05 -05005000
5001 err = hci_req_run(&req, fast_connectable_complete);
Antti Julkuf6422ec2011-06-22 13:11:56 +03005002 if (err < 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005003 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
5004 MGMT_STATUS_FAILED);
Johan Hedberg33e38b32013-03-15 17:07:05 -05005005 mgmt_pending_remove(cmd);
Antti Julkuf6422ec2011-06-22 13:11:56 +03005006 }
5007
Johan Hedberg33e38b32013-03-15 17:07:05 -05005008unlock:
Antti Julkuf6422ec2011-06-22 13:11:56 +03005009 hci_dev_unlock(hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05005010
Antti Julkuf6422ec2011-06-22 13:11:56 +03005011 return err;
5012}
5013
Marcel Holtmann1904a852015-01-11 13:50:44 -08005014static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg0663ca22013-10-02 13:43:14 +03005015{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005016 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03005017
5018 BT_DBG("status 0x%02x", status);
5019
5020 hci_dev_lock(hdev);
5021
Johan Hedberg333ae952015-03-17 13:48:47 +02005022 cmd = pending_find(MGMT_OP_SET_BREDR, hdev);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005023 if (!cmd)
5024 goto unlock;
5025
5026 if (status) {
5027 u8 mgmt_err = mgmt_status(status);
5028
5029 /* We need to restore the flag if related HCI commands
5030 * failed.
5031 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005032 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005033
Johan Hedberga69e8372015-03-06 21:08:53 +02005034 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005035 } else {
5036 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
5037 new_settings(hdev, cmd->sk);
5038 }
5039
5040 mgmt_pending_remove(cmd);
5041
5042unlock:
5043 hci_dev_unlock(hdev);
5044}
5045
5046static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
5047{
5048 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005049 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03005050 struct hci_request req;
5051 int err;
5052
5053 BT_DBG("request for %s", hdev->name);
5054
5055 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005056 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5057 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005058
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005059 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02005060 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5061 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005062
5063 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02005064 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5065 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005066
5067 hci_dev_lock(hdev);
5068
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005069 if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg0663ca22013-10-02 13:43:14 +03005070 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
5071 goto unlock;
5072 }
5073
5074 if (!hdev_is_powered(hdev)) {
5075 if (!cp->val) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005076 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
5077 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
5078 hci_dev_clear_flag(hdev, HCI_LINK_SECURITY);
5079 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
5080 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005081 }
5082
Marcel Holtmannce05d602015-03-13 02:11:03 -07005083 hci_dev_change_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005084
5085 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
5086 if (err < 0)
5087 goto unlock;
5088
5089 err = new_settings(hdev, sk);
5090 goto unlock;
5091 }
5092
5093 /* Reject disabling when powered on */
5094 if (!cp->val) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005095 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5096 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005097 goto unlock;
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08005098 } else {
5099 /* When configuring a dual-mode controller to operate
5100 * with LE only and using a static address, then switching
5101 * BR/EDR back on is not allowed.
5102 *
5103 * Dual-mode controllers shall operate with the public
5104 * address as its identity address for BR/EDR and LE. So
5105 * reject the attempt to create an invalid configuration.
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08005106 *
5107 * The same restrictions applies when secure connections
5108 * has been enabled. For BR/EDR this is a controller feature
5109 * while for LE it is a host stack feature. This means that
5110 * switching BR/EDR back on when secure connections has been
5111 * enabled is not a supported transaction.
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08005112 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005113 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08005114 (bacmp(&hdev->static_addr, BDADDR_ANY) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005115 hci_dev_test_flag(hdev, HCI_SC_ENABLED))) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005116 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5117 MGMT_STATUS_REJECTED);
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08005118 goto unlock;
5119 }
Johan Hedberg0663ca22013-10-02 13:43:14 +03005120 }
5121
Johan Hedberg333ae952015-03-17 13:48:47 +02005122 if (pending_find(MGMT_OP_SET_BREDR, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005123 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5124 MGMT_STATUS_BUSY);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005125 goto unlock;
5126 }
5127
5128 cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len);
5129 if (!cmd) {
5130 err = -ENOMEM;
5131 goto unlock;
5132 }
5133
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07005134 /* We need to flip the bit already here so that update_adv_data
Johan Hedberg0663ca22013-10-02 13:43:14 +03005135 * generates the correct flags.
5136 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005137 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005138
5139 hci_req_init(&req, hdev);
Johan Hedbergaa8af462013-10-14 21:15:26 +03005140
Johan Hedberg432df052014-08-01 11:13:31 +03005141 write_fast_connectable(&req, false);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02005142 __hci_update_page_scan(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03005143
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07005144 /* Since only the advertising data flags will change, there
5145 * is no need to update the scan response data.
5146 */
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07005147 update_adv_data(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03005148
Johan Hedberg0663ca22013-10-02 13:43:14 +03005149 err = hci_req_run(&req, set_bredr_complete);
5150 if (err < 0)
5151 mgmt_pending_remove(cmd);
5152
5153unlock:
5154 hci_dev_unlock(hdev);
5155 return err;
5156}
5157
Johan Hedberga1443f52015-01-23 15:42:46 +02005158static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
5159{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005160 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02005161 struct mgmt_mode *cp;
5162
5163 BT_DBG("%s status %u", hdev->name, status);
5164
5165 hci_dev_lock(hdev);
5166
Johan Hedberg333ae952015-03-17 13:48:47 +02005167 cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev);
Johan Hedberga1443f52015-01-23 15:42:46 +02005168 if (!cmd)
5169 goto unlock;
5170
5171 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005172 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
5173 mgmt_status(status));
Johan Hedberga1443f52015-01-23 15:42:46 +02005174 goto remove;
5175 }
5176
5177 cp = cmd->param;
5178
5179 switch (cp->val) {
5180 case 0x00:
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005181 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
5182 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02005183 break;
5184 case 0x01:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005185 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005186 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02005187 break;
5188 case 0x02:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005189 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
5190 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02005191 break;
5192 }
5193
5194 send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev);
5195 new_settings(hdev, cmd->sk);
5196
5197remove:
5198 mgmt_pending_remove(cmd);
5199unlock:
5200 hci_dev_unlock(hdev);
5201}
5202
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005203static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
5204 void *data, u16 len)
5205{
5206 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005207 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02005208 struct hci_request req;
Johan Hedberga3209692014-05-26 11:23:35 +03005209 u8 val;
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005210 int err;
5211
5212 BT_DBG("request for %s", hdev->name);
5213
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08005214 if (!lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005215 !hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02005216 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5217 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005218
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005219 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Johan Hedberg59200282015-01-28 19:56:00 +02005220 lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005221 !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02005222 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5223 MGMT_STATUS_REJECTED);
Marcel Holtmanned93ec62015-01-22 11:15:22 -08005224
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005225 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02005226 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005227 MGMT_STATUS_INVALID_PARAMS);
5228
5229 hci_dev_lock(hdev);
5230
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08005231 if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005232 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005233 bool changed;
5234
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005235 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07005236 changed = !hci_dev_test_and_set_flag(hdev,
5237 HCI_SC_ENABLED);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005238 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005239 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005240 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005241 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005242 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005243 changed = hci_dev_test_and_clear_flag(hdev,
5244 HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005245 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005246 }
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005247
5248 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
5249 if (err < 0)
5250 goto failed;
5251
5252 if (changed)
5253 err = new_settings(hdev, sk);
5254
5255 goto failed;
5256 }
5257
Johan Hedberg333ae952015-03-17 13:48:47 +02005258 if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005259 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5260 MGMT_STATUS_BUSY);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005261 goto failed;
5262 }
5263
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005264 val = !!cp->val;
5265
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005266 if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
5267 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005268 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
5269 goto failed;
5270 }
5271
5272 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len);
5273 if (!cmd) {
5274 err = -ENOMEM;
5275 goto failed;
5276 }
5277
Johan Hedberga1443f52015-01-23 15:42:46 +02005278 hci_req_init(&req, hdev);
5279 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val);
5280 err = hci_req_run(&req, sc_enable_complete);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005281 if (err < 0) {
5282 mgmt_pending_remove(cmd);
5283 goto failed;
5284 }
5285
5286failed:
5287 hci_dev_unlock(hdev);
5288 return err;
5289}
5290
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005291static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
5292 void *data, u16 len)
5293{
5294 struct mgmt_mode *cp = data;
Johan Hedbergb97109792014-06-24 14:00:28 +03005295 bool changed, use_changed;
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005296 int err;
5297
5298 BT_DBG("request for %s", hdev->name);
5299
Johan Hedbergb97109792014-06-24 14:00:28 +03005300 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02005301 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
5302 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005303
5304 hci_dev_lock(hdev);
5305
5306 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07005307 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005308 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005309 changed = hci_dev_test_and_clear_flag(hdev,
5310 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005311
Johan Hedbergb97109792014-06-24 14:00:28 +03005312 if (cp->val == 0x02)
Marcel Holtmann238be782015-03-13 02:11:06 -07005313 use_changed = !hci_dev_test_and_set_flag(hdev,
5314 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03005315 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005316 use_changed = hci_dev_test_and_clear_flag(hdev,
5317 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03005318
5319 if (hdev_is_powered(hdev) && use_changed &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005320 hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergb97109792014-06-24 14:00:28 +03005321 u8 mode = (cp->val == 0x02) ? 0x01 : 0x00;
5322 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
5323 sizeof(mode), &mode);
5324 }
5325
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005326 err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev);
5327 if (err < 0)
5328 goto unlock;
5329
5330 if (changed)
5331 err = new_settings(hdev, sk);
5332
5333unlock:
5334 hci_dev_unlock(hdev);
5335 return err;
5336}
5337
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005338static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5339 u16 len)
5340{
5341 struct mgmt_cp_set_privacy *cp = cp_data;
5342 bool changed;
5343 int err;
5344
5345 BT_DBG("request for %s", hdev->name);
5346
5347 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005348 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5349 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005350
5351 if (cp->privacy != 0x00 && cp->privacy != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02005352 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5353 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005354
5355 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005356 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5357 MGMT_STATUS_REJECTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005358
5359 hci_dev_lock(hdev);
5360
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02005361 /* If user space supports this command it is also expected to
5362 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
5363 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005364 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02005365
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005366 if (cp->privacy) {
Marcel Holtmann238be782015-03-13 02:11:06 -07005367 changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005368 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005369 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005370 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005371 changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005372 memset(hdev->irk, 0, sizeof(hdev->irk));
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005373 hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005374 }
5375
5376 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
5377 if (err < 0)
5378 goto unlock;
5379
5380 if (changed)
5381 err = new_settings(hdev, sk);
5382
5383unlock:
5384 hci_dev_unlock(hdev);
5385 return err;
5386}
5387
Johan Hedberg41edf162014-02-18 10:19:35 +02005388static bool irk_is_valid(struct mgmt_irk_info *irk)
5389{
5390 switch (irk->addr.type) {
5391 case BDADDR_LE_PUBLIC:
5392 return true;
5393
5394 case BDADDR_LE_RANDOM:
5395 /* Two most significant bits shall be set */
5396 if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0)
5397 return false;
5398 return true;
5399 }
5400
5401 return false;
5402}
5403
5404static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5405 u16 len)
5406{
5407 struct mgmt_cp_load_irks *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005408 const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) /
5409 sizeof(struct mgmt_irk_info));
Johan Hedberg41edf162014-02-18 10:19:35 +02005410 u16 irk_count, expected_len;
5411 int i, err;
5412
5413 BT_DBG("request for %s", hdev->name);
5414
5415 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005416 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5417 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg41edf162014-02-18 10:19:35 +02005418
5419 irk_count = __le16_to_cpu(cp->irk_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005420 if (irk_count > max_irk_count) {
5421 BT_ERR("load_irks: too big irk_count value %u", irk_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02005422 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5423 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005424 }
Johan Hedberg41edf162014-02-18 10:19:35 +02005425
5426 expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info);
5427 if (expected_len != len) {
5428 BT_ERR("load_irks: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02005429 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02005430 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5431 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02005432 }
5433
5434 BT_DBG("%s irk_count %u", hdev->name, irk_count);
5435
5436 for (i = 0; i < irk_count; i++) {
5437 struct mgmt_irk_info *key = &cp->irks[i];
5438
5439 if (!irk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02005440 return mgmt_cmd_status(sk, hdev->id,
5441 MGMT_OP_LOAD_IRKS,
5442 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02005443 }
5444
5445 hci_dev_lock(hdev);
5446
5447 hci_smp_irks_clear(hdev);
5448
5449 for (i = 0; i < irk_count; i++) {
5450 struct mgmt_irk_info *irk = &cp->irks[i];
5451 u8 addr_type;
5452
5453 if (irk->addr.type == BDADDR_LE_PUBLIC)
5454 addr_type = ADDR_LE_DEV_PUBLIC;
5455 else
5456 addr_type = ADDR_LE_DEV_RANDOM;
5457
5458 hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val,
5459 BDADDR_ANY);
5460 }
5461
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005462 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedberg41edf162014-02-18 10:19:35 +02005463
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005464 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
Johan Hedberg41edf162014-02-18 10:19:35 +02005465
5466 hci_dev_unlock(hdev);
5467
5468 return err;
5469}
5470
Johan Hedberg3f706b72013-01-20 14:27:16 +02005471static bool ltk_is_valid(struct mgmt_ltk_info *key)
5472{
5473 if (key->master != 0x00 && key->master != 0x01)
5474 return false;
Marcel Holtmann490cb0b2014-02-16 12:59:05 -08005475
5476 switch (key->addr.type) {
5477 case BDADDR_LE_PUBLIC:
5478 return true;
5479
5480 case BDADDR_LE_RANDOM:
5481 /* Two most significant bits shall be set */
5482 if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0)
5483 return false;
5484 return true;
5485 }
5486
5487 return false;
Johan Hedberg3f706b72013-01-20 14:27:16 +02005488}
5489
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005490static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005491 void *cp_data, u16 len)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005492{
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005493 struct mgmt_cp_load_long_term_keys *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005494 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
5495 sizeof(struct mgmt_ltk_info));
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005496 u16 key_count, expected_len;
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005497 int i, err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005498
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07005499 BT_DBG("request for %s", hdev->name);
5500
5501 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005502 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5503 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07005504
Marcel Holtmann1f350c82012-03-12 20:31:08 -07005505 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005506 if (key_count > max_key_count) {
5507 BT_ERR("load_ltks: too big key_count value %u", key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02005508 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5509 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005510 }
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005511
5512 expected_len = sizeof(*cp) + key_count *
5513 sizeof(struct mgmt_ltk_info);
5514 if (expected_len != len) {
5515 BT_ERR("load_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02005516 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02005517 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5518 MGMT_STATUS_INVALID_PARAMS);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005519 }
5520
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005521 BT_DBG("%s key_count %u", hdev->name, key_count);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005522
Johan Hedberg54ad6d82013-01-20 14:27:15 +02005523 for (i = 0; i < key_count; i++) {
5524 struct mgmt_ltk_info *key = &cp->keys[i];
5525
Johan Hedberg3f706b72013-01-20 14:27:16 +02005526 if (!ltk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02005527 return mgmt_cmd_status(sk, hdev->id,
5528 MGMT_OP_LOAD_LONG_TERM_KEYS,
5529 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg54ad6d82013-01-20 14:27:15 +02005530 }
5531
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005532 hci_dev_lock(hdev);
5533
5534 hci_smp_ltks_clear(hdev);
5535
5536 for (i = 0; i < key_count; i++) {
5537 struct mgmt_ltk_info *key = &cp->keys[i];
Johan Hedbergd7b25452014-05-23 13:19:53 +03005538 u8 type, addr_type, authenticated;
Marcel Holtmann79d95a12013-10-13 03:57:38 -07005539
5540 if (key->addr.type == BDADDR_LE_PUBLIC)
5541 addr_type = ADDR_LE_DEV_PUBLIC;
5542 else
5543 addr_type = ADDR_LE_DEV_RANDOM;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005544
Johan Hedberg61b43352014-05-29 19:36:53 +03005545 switch (key->type) {
5546 case MGMT_LTK_UNAUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03005547 authenticated = 0x00;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005548 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03005549 break;
5550 case MGMT_LTK_AUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03005551 authenticated = 0x01;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005552 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03005553 break;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005554 case MGMT_LTK_P256_UNAUTH:
5555 authenticated = 0x00;
5556 type = SMP_LTK_P256;
5557 break;
5558 case MGMT_LTK_P256_AUTH:
5559 authenticated = 0x01;
5560 type = SMP_LTK_P256;
5561 break;
5562 case MGMT_LTK_P256_DEBUG:
5563 authenticated = 0x00;
5564 type = SMP_LTK_P256_DEBUG;
Johan Hedberg61b43352014-05-29 19:36:53 +03005565 default:
5566 continue;
5567 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03005568
Johan Hedberg35d70272014-02-19 14:57:47 +02005569 hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type,
Johan Hedbergd7b25452014-05-23 13:19:53 +03005570 authenticated, key->val, key->enc_size, key->ediv,
Johan Hedberg35d70272014-02-19 14:57:47 +02005571 key->rand);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005572 }
5573
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005574 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005575 NULL, 0);
5576
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005577 hci_dev_unlock(hdev);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005578
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005579 return err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005580}
5581
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005582static int conn_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005583{
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005584 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005585 struct mgmt_rp_get_conn_info rp;
Johan Hedberg9df74652014-12-19 22:26:03 +02005586 int err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005587
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005588 memcpy(&rp.addr, cmd->param, sizeof(rp.addr));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005589
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005590 if (status == MGMT_STATUS_SUCCESS) {
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005591 rp.rssi = conn->rssi;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005592 rp.tx_power = conn->tx_power;
5593 rp.max_tx_power = conn->max_tx_power;
5594 } else {
5595 rp.rssi = HCI_RSSI_INVALID;
5596 rp.tx_power = HCI_TX_POWER_INVALID;
5597 rp.max_tx_power = HCI_TX_POWER_INVALID;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005598 }
5599
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005600 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO,
5601 status, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005602
5603 hci_conn_drop(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005604 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02005605
5606 return err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005607}
5608
Marcel Holtmann1904a852015-01-11 13:50:44 -08005609static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status,
5610 u16 opcode)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005611{
5612 struct hci_cp_read_rssi *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005613 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005614 struct hci_conn *conn;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005615 u16 handle;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005616 u8 status;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005617
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005618 BT_DBG("status 0x%02x", hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005619
5620 hci_dev_lock(hdev);
5621
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005622 /* Commands sent in request are either Read RSSI or Read Transmit Power
5623 * Level so we check which one was last sent to retrieve connection
5624 * handle. Both commands have handle as first parameter so it's safe to
5625 * cast data on the same command struct.
5626 *
5627 * First command sent is always Read RSSI and we fail only if it fails.
5628 * In other case we simply override error to indicate success as we
5629 * already remembered if TX power value is actually valid.
5630 */
5631 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI);
5632 if (!cp) {
5633 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005634 status = MGMT_STATUS_SUCCESS;
5635 } else {
5636 status = mgmt_status(hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005637 }
5638
5639 if (!cp) {
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005640 BT_ERR("invalid sent_cmd in conn_info response");
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005641 goto unlock;
5642 }
5643
5644 handle = __le16_to_cpu(cp->handle);
5645 conn = hci_conn_hash_lookup_handle(hdev, handle);
5646 if (!conn) {
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005647 BT_ERR("unknown handle (%d) in conn_info response", handle);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005648 goto unlock;
5649 }
5650
Johan Hedberg333ae952015-03-17 13:48:47 +02005651 cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005652 if (!cmd)
5653 goto unlock;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005654
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005655 cmd->cmd_complete(cmd, status);
5656 mgmt_pending_remove(cmd);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005657
5658unlock:
5659 hci_dev_unlock(hdev);
5660}
5661
5662static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
5663 u16 len)
5664{
5665 struct mgmt_cp_get_conn_info *cp = data;
5666 struct mgmt_rp_get_conn_info rp;
5667 struct hci_conn *conn;
5668 unsigned long conn_info_age;
5669 int err = 0;
5670
5671 BT_DBG("%s", hdev->name);
5672
5673 memset(&rp, 0, sizeof(rp));
5674 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5675 rp.addr.type = cp->addr.type;
5676
5677 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005678 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5679 MGMT_STATUS_INVALID_PARAMS,
5680 &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005681
5682 hci_dev_lock(hdev);
5683
5684 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005685 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5686 MGMT_STATUS_NOT_POWERED, &rp,
5687 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005688 goto unlock;
5689 }
5690
5691 if (cp->addr.type == BDADDR_BREDR)
5692 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5693 &cp->addr.bdaddr);
5694 else
5695 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
5696
5697 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005698 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5699 MGMT_STATUS_NOT_CONNECTED, &rp,
5700 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005701 goto unlock;
5702 }
5703
Johan Hedberg333ae952015-03-17 13:48:47 +02005704 if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005705 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5706 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005707 goto unlock;
5708 }
5709
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005710 /* To avoid client trying to guess when to poll again for information we
5711 * calculate conn info age as random value between min/max set in hdev.
5712 */
5713 conn_info_age = hdev->conn_info_min_age +
5714 prandom_u32_max(hdev->conn_info_max_age -
5715 hdev->conn_info_min_age);
5716
5717 /* Query controller to refresh cached values if they are too old or were
5718 * never read.
5719 */
Andrzej Kaczmarekf4e2dd52014-05-16 16:48:57 +02005720 if (time_after(jiffies, conn->conn_info_timestamp +
5721 msecs_to_jiffies(conn_info_age)) ||
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005722 !conn->conn_info_timestamp) {
5723 struct hci_request req;
5724 struct hci_cp_read_tx_power req_txp_cp;
5725 struct hci_cp_read_rssi req_rssi_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005726 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005727
5728 hci_req_init(&req, hdev);
5729 req_rssi_cp.handle = cpu_to_le16(conn->handle);
5730 hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp),
5731 &req_rssi_cp);
5732
Andrzej Kaczmarekf7faab02014-05-14 13:43:04 +02005733 /* For LE links TX power does not change thus we don't need to
5734 * query for it once value is known.
5735 */
5736 if (!bdaddr_type_is_le(cp->addr.type) ||
5737 conn->tx_power == HCI_TX_POWER_INVALID) {
5738 req_txp_cp.handle = cpu_to_le16(conn->handle);
5739 req_txp_cp.type = 0x00;
5740 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5741 sizeof(req_txp_cp), &req_txp_cp);
5742 }
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005743
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005744 /* Max TX power needs to be read only once per connection */
5745 if (conn->max_tx_power == HCI_TX_POWER_INVALID) {
5746 req_txp_cp.handle = cpu_to_le16(conn->handle);
5747 req_txp_cp.type = 0x01;
5748 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5749 sizeof(req_txp_cp), &req_txp_cp);
5750 }
5751
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005752 err = hci_req_run(&req, conn_info_refresh_complete);
5753 if (err < 0)
5754 goto unlock;
5755
5756 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev,
5757 data, len);
5758 if (!cmd) {
5759 err = -ENOMEM;
5760 goto unlock;
5761 }
5762
5763 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005764 cmd->user_data = hci_conn_get(conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005765 cmd->cmd_complete = conn_info_cmd_complete;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005766
5767 conn->conn_info_timestamp = jiffies;
5768 } else {
5769 /* Cache is valid, just reply with values cached in hci_conn */
5770 rp.rssi = conn->rssi;
5771 rp.tx_power = conn->tx_power;
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005772 rp.max_tx_power = conn->max_tx_power;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005773
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005774 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5775 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005776 }
5777
5778unlock:
5779 hci_dev_unlock(hdev);
5780 return err;
5781}
5782
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005783static int clock_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg69487372014-12-05 13:36:07 +02005784{
5785 struct hci_conn *conn = cmd->user_data;
5786 struct mgmt_rp_get_clock_info rp;
5787 struct hci_dev *hdev;
Johan Hedberg9df74652014-12-19 22:26:03 +02005788 int err;
Johan Hedberg69487372014-12-05 13:36:07 +02005789
5790 memset(&rp, 0, sizeof(rp));
5791 memcpy(&rp.addr, &cmd->param, sizeof(rp.addr));
5792
5793 if (status)
5794 goto complete;
5795
5796 hdev = hci_dev_get(cmd->index);
5797 if (hdev) {
5798 rp.local_clock = cpu_to_le32(hdev->clock);
5799 hci_dev_put(hdev);
5800 }
5801
5802 if (conn) {
5803 rp.piconet_clock = cpu_to_le32(conn->clock);
5804 rp.accuracy = cpu_to_le16(conn->clock_accuracy);
5805 }
5806
5807complete:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005808 err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp,
5809 sizeof(rp));
Johan Hedberg69487372014-12-05 13:36:07 +02005810
5811 if (conn) {
5812 hci_conn_drop(conn);
5813 hci_conn_put(conn);
5814 }
Johan Hedberg9df74652014-12-19 22:26:03 +02005815
5816 return err;
Johan Hedberg69487372014-12-05 13:36:07 +02005817}
5818
Marcel Holtmann1904a852015-01-11 13:50:44 -08005819static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg95868422014-06-28 17:54:07 +03005820{
Johan Hedberg95868422014-06-28 17:54:07 +03005821 struct hci_cp_read_clock *hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005822 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03005823 struct hci_conn *conn;
5824
5825 BT_DBG("%s status %u", hdev->name, status);
5826
5827 hci_dev_lock(hdev);
5828
5829 hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
5830 if (!hci_cp)
5831 goto unlock;
5832
5833 if (hci_cp->which) {
5834 u16 handle = __le16_to_cpu(hci_cp->handle);
5835 conn = hci_conn_hash_lookup_handle(hdev, handle);
5836 } else {
5837 conn = NULL;
5838 }
5839
Johan Hedberg333ae952015-03-17 13:48:47 +02005840 cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn);
Johan Hedberg95868422014-06-28 17:54:07 +03005841 if (!cmd)
5842 goto unlock;
5843
Johan Hedberg69487372014-12-05 13:36:07 +02005844 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberg95868422014-06-28 17:54:07 +03005845 mgmt_pending_remove(cmd);
Johan Hedberg95868422014-06-28 17:54:07 +03005846
5847unlock:
5848 hci_dev_unlock(hdev);
5849}
5850
5851static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
5852 u16 len)
5853{
5854 struct mgmt_cp_get_clock_info *cp = data;
5855 struct mgmt_rp_get_clock_info rp;
5856 struct hci_cp_read_clock hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005857 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03005858 struct hci_request req;
5859 struct hci_conn *conn;
5860 int err;
5861
5862 BT_DBG("%s", hdev->name);
5863
5864 memset(&rp, 0, sizeof(rp));
5865 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5866 rp.addr.type = cp->addr.type;
5867
5868 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005869 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5870 MGMT_STATUS_INVALID_PARAMS,
5871 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005872
5873 hci_dev_lock(hdev);
5874
5875 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005876 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5877 MGMT_STATUS_NOT_POWERED, &rp,
5878 sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005879 goto unlock;
5880 }
5881
5882 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
5883 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5884 &cp->addr.bdaddr);
5885 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005886 err = mgmt_cmd_complete(sk, hdev->id,
5887 MGMT_OP_GET_CLOCK_INFO,
5888 MGMT_STATUS_NOT_CONNECTED,
5889 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005890 goto unlock;
5891 }
5892 } else {
5893 conn = NULL;
5894 }
5895
5896 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len);
5897 if (!cmd) {
5898 err = -ENOMEM;
5899 goto unlock;
5900 }
5901
Johan Hedberg69487372014-12-05 13:36:07 +02005902 cmd->cmd_complete = clock_info_cmd_complete;
5903
Johan Hedberg95868422014-06-28 17:54:07 +03005904 hci_req_init(&req, hdev);
5905
5906 memset(&hci_cp, 0, sizeof(hci_cp));
5907 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5908
5909 if (conn) {
5910 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005911 cmd->user_data = hci_conn_get(conn);
Johan Hedberg95868422014-06-28 17:54:07 +03005912
5913 hci_cp.handle = cpu_to_le16(conn->handle);
5914 hci_cp.which = 0x01; /* Piconet clock */
5915 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5916 }
5917
5918 err = hci_req_run(&req, get_clock_info_complete);
5919 if (err < 0)
5920 mgmt_pending_remove(cmd);
5921
5922unlock:
5923 hci_dev_unlock(hdev);
5924 return err;
5925}
5926
Johan Hedberg5a154e62014-12-19 22:26:02 +02005927static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
5928{
5929 struct hci_conn *conn;
5930
5931 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
5932 if (!conn)
5933 return false;
5934
5935 if (conn->dst_type != type)
5936 return false;
5937
5938 if (conn->state != BT_CONNECTED)
5939 return false;
5940
5941 return true;
5942}
5943
5944/* This function requires the caller holds hdev->lock */
5945static int hci_conn_params_set(struct hci_request *req, bdaddr_t *addr,
5946 u8 addr_type, u8 auto_connect)
5947{
5948 struct hci_dev *hdev = req->hdev;
5949 struct hci_conn_params *params;
5950
5951 params = hci_conn_params_add(hdev, addr, addr_type);
5952 if (!params)
5953 return -EIO;
5954
5955 if (params->auto_connect == auto_connect)
5956 return 0;
5957
5958 list_del_init(&params->action);
5959
5960 switch (auto_connect) {
5961 case HCI_AUTO_CONN_DISABLED:
5962 case HCI_AUTO_CONN_LINK_LOSS:
5963 __hci_update_background_scan(req);
5964 break;
5965 case HCI_AUTO_CONN_REPORT:
5966 list_add(&params->action, &hdev->pend_le_reports);
5967 __hci_update_background_scan(req);
5968 break;
5969 case HCI_AUTO_CONN_DIRECT:
5970 case HCI_AUTO_CONN_ALWAYS:
5971 if (!is_connected(hdev, addr, addr_type)) {
5972 list_add(&params->action, &hdev->pend_le_conns);
5973 __hci_update_background_scan(req);
5974 }
5975 break;
5976 }
5977
5978 params->auto_connect = auto_connect;
5979
5980 BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
5981 auto_connect);
5982
5983 return 0;
5984}
5985
Marcel Holtmann8afef092014-06-29 22:28:34 +02005986static void device_added(struct sock *sk, struct hci_dev *hdev,
5987 bdaddr_t *bdaddr, u8 type, u8 action)
5988{
5989 struct mgmt_ev_device_added ev;
5990
5991 bacpy(&ev.addr.bdaddr, bdaddr);
5992 ev.addr.type = type;
5993 ev.action = action;
5994
5995 mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk);
5996}
5997
Marcel Holtmann1904a852015-01-11 13:50:44 -08005998static void add_device_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg5a154e62014-12-19 22:26:02 +02005999{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006000 struct mgmt_pending_cmd *cmd;
Johan Hedberg5a154e62014-12-19 22:26:02 +02006001
6002 BT_DBG("status 0x%02x", status);
6003
6004 hci_dev_lock(hdev);
6005
Johan Hedberg333ae952015-03-17 13:48:47 +02006006 cmd = pending_find(MGMT_OP_ADD_DEVICE, hdev);
Johan Hedberg5a154e62014-12-19 22:26:02 +02006007 if (!cmd)
6008 goto unlock;
6009
6010 cmd->cmd_complete(cmd, mgmt_status(status));
6011 mgmt_pending_remove(cmd);
6012
6013unlock:
6014 hci_dev_unlock(hdev);
6015}
6016
Marcel Holtmann2faade52014-06-29 19:44:03 +02006017static int add_device(struct sock *sk, struct hci_dev *hdev,
6018 void *data, u16 len)
6019{
6020 struct mgmt_cp_add_device *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006021 struct mgmt_pending_cmd *cmd;
Johan Hedberg5a154e62014-12-19 22:26:02 +02006022 struct hci_request req;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006023 u8 auto_conn, addr_type;
6024 int err;
6025
6026 BT_DBG("%s", hdev->name);
6027
Johan Hedberg66593582014-07-09 12:59:14 +03006028 if (!bdaddr_type_is_valid(cp->addr.type) ||
Marcel Holtmann2faade52014-06-29 19:44:03 +02006029 !bacmp(&cp->addr.bdaddr, BDADDR_ANY))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006030 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
6031 MGMT_STATUS_INVALID_PARAMS,
6032 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02006033
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006034 if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006035 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
6036 MGMT_STATUS_INVALID_PARAMS,
6037 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02006038
Johan Hedberg5a154e62014-12-19 22:26:02 +02006039 hci_req_init(&req, hdev);
6040
Marcel Holtmann2faade52014-06-29 19:44:03 +02006041 hci_dev_lock(hdev);
6042
Johan Hedberg5a154e62014-12-19 22:26:02 +02006043 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_DEVICE, hdev, data, len);
6044 if (!cmd) {
6045 err = -ENOMEM;
6046 goto unlock;
6047 }
6048
6049 cmd->cmd_complete = addr_cmd_complete;
6050
Johan Hedberg66593582014-07-09 12:59:14 +03006051 if (cp->addr.type == BDADDR_BREDR) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006052 /* Only incoming connections action is supported for now */
Johan Hedberg66593582014-07-09 12:59:14 +03006053 if (cp->action != 0x01) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006054 err = cmd->cmd_complete(cmd,
6055 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg5a154e62014-12-19 22:26:02 +02006056 mgmt_pending_remove(cmd);
Johan Hedberg66593582014-07-09 12:59:14 +03006057 goto unlock;
6058 }
6059
6060 err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr,
6061 cp->addr.type);
6062 if (err)
6063 goto unlock;
Johan Hedberga3974072014-07-09 12:59:15 +03006064
Johan Hedberg5a154e62014-12-19 22:26:02 +02006065 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03006066
Johan Hedberg66593582014-07-09 12:59:14 +03006067 goto added;
6068 }
6069
Marcel Holtmann2faade52014-06-29 19:44:03 +02006070 if (cp->addr.type == BDADDR_LE_PUBLIC)
6071 addr_type = ADDR_LE_DEV_PUBLIC;
6072 else
6073 addr_type = ADDR_LE_DEV_RANDOM;
6074
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006075 if (cp->action == 0x02)
Marcel Holtmann2faade52014-06-29 19:44:03 +02006076 auto_conn = HCI_AUTO_CONN_ALWAYS;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006077 else if (cp->action == 0x01)
6078 auto_conn = HCI_AUTO_CONN_DIRECT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006079 else
Johan Hedberga3451d22014-07-02 17:37:27 +03006080 auto_conn = HCI_AUTO_CONN_REPORT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006081
Marcel Holtmannbf5b3c82014-06-30 12:34:39 +02006082 /* If the connection parameters don't exist for this device,
6083 * they will be created and configured with defaults.
6084 */
Johan Hedberg5a154e62014-12-19 22:26:02 +02006085 if (hci_conn_params_set(&req, &cp->addr.bdaddr, addr_type,
Marcel Holtmannd06b50c2014-07-01 12:11:06 +02006086 auto_conn) < 0) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006087 err = cmd->cmd_complete(cmd, MGMT_STATUS_FAILED);
Johan Hedberg5a154e62014-12-19 22:26:02 +02006088 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006089 goto unlock;
6090 }
6091
Johan Hedberg66593582014-07-09 12:59:14 +03006092added:
Marcel Holtmann8afef092014-06-29 22:28:34 +02006093 device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
6094
Johan Hedberg5a154e62014-12-19 22:26:02 +02006095 err = hci_req_run(&req, add_device_complete);
6096 if (err < 0) {
6097 /* ENODATA means no HCI commands were needed (e.g. if
6098 * the adapter is powered off).
6099 */
Johan Hedberg9df74652014-12-19 22:26:03 +02006100 if (err == -ENODATA)
6101 err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS);
Johan Hedberg5a154e62014-12-19 22:26:02 +02006102 mgmt_pending_remove(cmd);
6103 }
Marcel Holtmann2faade52014-06-29 19:44:03 +02006104
6105unlock:
6106 hci_dev_unlock(hdev);
6107 return err;
6108}
6109
Marcel Holtmann8afef092014-06-29 22:28:34 +02006110static void device_removed(struct sock *sk, struct hci_dev *hdev,
6111 bdaddr_t *bdaddr, u8 type)
6112{
6113 struct mgmt_ev_device_removed ev;
6114
6115 bacpy(&ev.addr.bdaddr, bdaddr);
6116 ev.addr.type = type;
6117
6118 mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk);
6119}
6120
Marcel Holtmann1904a852015-01-11 13:50:44 -08006121static void remove_device_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006122{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006123 struct mgmt_pending_cmd *cmd;
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006124
6125 BT_DBG("status 0x%02x", status);
6126
6127 hci_dev_lock(hdev);
6128
Johan Hedberg333ae952015-03-17 13:48:47 +02006129 cmd = pending_find(MGMT_OP_REMOVE_DEVICE, hdev);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006130 if (!cmd)
6131 goto unlock;
6132
6133 cmd->cmd_complete(cmd, mgmt_status(status));
6134 mgmt_pending_remove(cmd);
6135
6136unlock:
6137 hci_dev_unlock(hdev);
6138}
6139
Marcel Holtmann2faade52014-06-29 19:44:03 +02006140static int remove_device(struct sock *sk, struct hci_dev *hdev,
6141 void *data, u16 len)
6142{
6143 struct mgmt_cp_remove_device *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006144 struct mgmt_pending_cmd *cmd;
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006145 struct hci_request req;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006146 int err;
6147
6148 BT_DBG("%s", hdev->name);
6149
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006150 hci_req_init(&req, hdev);
6151
Marcel Holtmann2faade52014-06-29 19:44:03 +02006152 hci_dev_lock(hdev);
6153
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006154 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_DEVICE, hdev, data, len);
6155 if (!cmd) {
6156 err = -ENOMEM;
6157 goto unlock;
6158 }
6159
6160 cmd->cmd_complete = addr_cmd_complete;
6161
Marcel Holtmann2faade52014-06-29 19:44:03 +02006162 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
Johan Hedbergc71593d2014-07-02 17:37:28 +03006163 struct hci_conn_params *params;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006164 u8 addr_type;
6165
Johan Hedberg66593582014-07-09 12:59:14 +03006166 if (!bdaddr_type_is_valid(cp->addr.type)) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006167 err = cmd->cmd_complete(cmd,
6168 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006169 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006170 goto unlock;
6171 }
6172
Johan Hedberg66593582014-07-09 12:59:14 +03006173 if (cp->addr.type == BDADDR_BREDR) {
6174 err = hci_bdaddr_list_del(&hdev->whitelist,
6175 &cp->addr.bdaddr,
6176 cp->addr.type);
6177 if (err) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006178 err = cmd->cmd_complete(cmd,
6179 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006180 mgmt_pending_remove(cmd);
Johan Hedberg66593582014-07-09 12:59:14 +03006181 goto unlock;
6182 }
6183
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006184 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03006185
Johan Hedberg66593582014-07-09 12:59:14 +03006186 device_removed(sk, hdev, &cp->addr.bdaddr,
6187 cp->addr.type);
6188 goto complete;
6189 }
6190
Marcel Holtmann2faade52014-06-29 19:44:03 +02006191 if (cp->addr.type == BDADDR_LE_PUBLIC)
6192 addr_type = ADDR_LE_DEV_PUBLIC;
6193 else
6194 addr_type = ADDR_LE_DEV_RANDOM;
6195
Johan Hedbergc71593d2014-07-02 17:37:28 +03006196 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
6197 addr_type);
6198 if (!params) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006199 err = cmd->cmd_complete(cmd,
6200 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006201 mgmt_pending_remove(cmd);
Johan Hedbergc71593d2014-07-02 17:37:28 +03006202 goto unlock;
6203 }
6204
6205 if (params->auto_connect == HCI_AUTO_CONN_DISABLED) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006206 err = cmd->cmd_complete(cmd,
6207 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006208 mgmt_pending_remove(cmd);
Johan Hedbergc71593d2014-07-02 17:37:28 +03006209 goto unlock;
6210 }
6211
Johan Hedbergd1dbf122014-07-04 16:17:23 +03006212 list_del(&params->action);
Johan Hedbergc71593d2014-07-02 17:37:28 +03006213 list_del(&params->list);
6214 kfree(params);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006215 __hci_update_background_scan(&req);
Marcel Holtmann8afef092014-06-29 22:28:34 +02006216
6217 device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006218 } else {
Johan Hedberg19de0822014-07-06 13:06:51 +03006219 struct hci_conn_params *p, *tmp;
Johan Hedberg66593582014-07-09 12:59:14 +03006220 struct bdaddr_list *b, *btmp;
Johan Hedberg19de0822014-07-06 13:06:51 +03006221
Marcel Holtmann2faade52014-06-29 19:44:03 +02006222 if (cp->addr.type) {
Johan Hedberg9df74652014-12-19 22:26:03 +02006223 err = cmd->cmd_complete(cmd,
6224 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006225 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006226 goto unlock;
6227 }
6228
Johan Hedberg66593582014-07-09 12:59:14 +03006229 list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) {
6230 device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type);
6231 list_del(&b->list);
6232 kfree(b);
6233 }
6234
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006235 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03006236
Johan Hedberg19de0822014-07-06 13:06:51 +03006237 list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) {
6238 if (p->auto_connect == HCI_AUTO_CONN_DISABLED)
6239 continue;
6240 device_removed(sk, hdev, &p->addr, p->addr_type);
6241 list_del(&p->action);
6242 list_del(&p->list);
6243 kfree(p);
6244 }
6245
6246 BT_DBG("All LE connection parameters were removed");
6247
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006248 __hci_update_background_scan(&req);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006249 }
6250
Johan Hedberg66593582014-07-09 12:59:14 +03006251complete:
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006252 err = hci_req_run(&req, remove_device_complete);
6253 if (err < 0) {
6254 /* ENODATA means no HCI commands were needed (e.g. if
6255 * the adapter is powered off).
6256 */
Johan Hedberg9df74652014-12-19 22:26:03 +02006257 if (err == -ENODATA)
6258 err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006259 mgmt_pending_remove(cmd);
6260 }
Marcel Holtmann2faade52014-06-29 19:44:03 +02006261
6262unlock:
6263 hci_dev_unlock(hdev);
6264 return err;
6265}
6266
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006267static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
6268 u16 len)
6269{
6270 struct mgmt_cp_load_conn_param *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03006271 const u16 max_param_count = ((U16_MAX - sizeof(*cp)) /
6272 sizeof(struct mgmt_conn_param));
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006273 u16 param_count, expected_len;
6274 int i;
6275
6276 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006277 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6278 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006279
6280 param_count = __le16_to_cpu(cp->param_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03006281 if (param_count > max_param_count) {
6282 BT_ERR("load_conn_param: too big param_count value %u",
6283 param_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02006284 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6285 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03006286 }
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006287
6288 expected_len = sizeof(*cp) + param_count *
6289 sizeof(struct mgmt_conn_param);
6290 if (expected_len != len) {
6291 BT_ERR("load_conn_param: expected %u bytes, got %u bytes",
6292 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02006293 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6294 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006295 }
6296
6297 BT_DBG("%s param_count %u", hdev->name, param_count);
6298
6299 hci_dev_lock(hdev);
6300
6301 hci_conn_params_clear_disabled(hdev);
6302
6303 for (i = 0; i < param_count; i++) {
6304 struct mgmt_conn_param *param = &cp->params[i];
6305 struct hci_conn_params *hci_param;
6306 u16 min, max, latency, timeout;
6307 u8 addr_type;
6308
6309 BT_DBG("Adding %pMR (type %u)", &param->addr.bdaddr,
6310 param->addr.type);
6311
6312 if (param->addr.type == BDADDR_LE_PUBLIC) {
6313 addr_type = ADDR_LE_DEV_PUBLIC;
6314 } else if (param->addr.type == BDADDR_LE_RANDOM) {
6315 addr_type = ADDR_LE_DEV_RANDOM;
6316 } else {
6317 BT_ERR("Ignoring invalid connection parameters");
6318 continue;
6319 }
6320
6321 min = le16_to_cpu(param->min_interval);
6322 max = le16_to_cpu(param->max_interval);
6323 latency = le16_to_cpu(param->latency);
6324 timeout = le16_to_cpu(param->timeout);
6325
6326 BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x",
6327 min, max, latency, timeout);
6328
6329 if (hci_check_conn_params(min, max, latency, timeout) < 0) {
6330 BT_ERR("Ignoring invalid connection parameters");
6331 continue;
6332 }
6333
6334 hci_param = hci_conn_params_add(hdev, &param->addr.bdaddr,
6335 addr_type);
6336 if (!hci_param) {
6337 BT_ERR("Failed to add connection parameters");
6338 continue;
6339 }
6340
6341 hci_param->conn_min_interval = min;
6342 hci_param->conn_max_interval = max;
6343 hci_param->conn_latency = latency;
6344 hci_param->supervision_timeout = timeout;
6345 }
6346
6347 hci_dev_unlock(hdev);
6348
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006349 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0,
6350 NULL, 0);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006351}
6352
Marcel Holtmanndbece372014-07-04 18:11:55 +02006353static int set_external_config(struct sock *sk, struct hci_dev *hdev,
6354 void *data, u16 len)
6355{
6356 struct mgmt_cp_set_external_config *cp = data;
6357 bool changed;
6358 int err;
6359
6360 BT_DBG("%s", hdev->name);
6361
6362 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006363 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6364 MGMT_STATUS_REJECTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006365
6366 if (cp->config != 0x00 && cp->config != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02006367 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6368 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006369
6370 if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
Johan Hedberga69e8372015-03-06 21:08:53 +02006371 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6372 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006373
6374 hci_dev_lock(hdev);
6375
6376 if (cp->config)
Marcel Holtmann238be782015-03-13 02:11:06 -07006377 changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006378 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07006379 changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006380
6381 err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
6382 if (err < 0)
6383 goto unlock;
6384
6385 if (!changed)
6386 goto unlock;
6387
Marcel Holtmannf4537c02014-07-04 19:06:23 +02006388 err = new_options(hdev, sk);
6389
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006390 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) {
Marcel Holtmanndbece372014-07-04 18:11:55 +02006391 mgmt_index_removed(hdev);
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02006392
Marcel Holtmann516018a2015-03-13 02:11:04 -07006393 if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006394 hci_dev_set_flag(hdev, HCI_CONFIG);
6395 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02006396
6397 queue_work(hdev->req_workqueue, &hdev->power_on);
6398 } else {
Marcel Holtmann5ea234d2014-07-06 12:11:16 +02006399 set_bit(HCI_RAW, &hdev->flags);
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02006400 mgmt_index_added(hdev);
6401 }
Marcel Holtmanndbece372014-07-04 18:11:55 +02006402 }
6403
6404unlock:
6405 hci_dev_unlock(hdev);
6406 return err;
6407}
6408
Marcel Holtmann9713c172014-07-06 12:11:15 +02006409static int set_public_address(struct sock *sk, struct hci_dev *hdev,
6410 void *data, u16 len)
6411{
6412 struct mgmt_cp_set_public_address *cp = data;
6413 bool changed;
6414 int err;
6415
6416 BT_DBG("%s", hdev->name);
6417
6418 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006419 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6420 MGMT_STATUS_REJECTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006421
6422 if (!bacmp(&cp->bdaddr, BDADDR_ANY))
Johan Hedberga69e8372015-03-06 21:08:53 +02006423 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6424 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006425
6426 if (!hdev->set_bdaddr)
Johan Hedberga69e8372015-03-06 21:08:53 +02006427 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6428 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006429
6430 hci_dev_lock(hdev);
6431
6432 changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
6433 bacpy(&hdev->public_addr, &cp->bdaddr);
6434
6435 err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev);
6436 if (err < 0)
6437 goto unlock;
6438
6439 if (!changed)
6440 goto unlock;
6441
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006442 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
Marcel Holtmann9713c172014-07-06 12:11:15 +02006443 err = new_options(hdev, sk);
6444
6445 if (is_configured(hdev)) {
6446 mgmt_index_removed(hdev);
6447
Marcel Holtmanna358dc12015-03-13 02:11:02 -07006448 hci_dev_clear_flag(hdev, HCI_UNCONFIGURED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006449
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006450 hci_dev_set_flag(hdev, HCI_CONFIG);
6451 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006452
6453 queue_work(hdev->req_workqueue, &hdev->power_on);
6454 }
6455
6456unlock:
6457 hci_dev_unlock(hdev);
6458 return err;
6459}
6460
Marcel Holtmannbea41602015-03-14 22:43:17 -07006461static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
6462 u8 data_len)
6463{
6464 eir[eir_len++] = sizeof(type) + data_len;
6465 eir[eir_len++] = type;
6466 memcpy(&eir[eir_len], data, data_len);
6467 eir_len += data_len;
6468
6469 return eir_len;
6470}
6471
Johan Hedberg40f66c02015-04-07 21:52:22 +03006472static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status,
6473 u16 opcode, struct sk_buff *skb)
6474{
6475 const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp;
6476 struct mgmt_rp_read_local_oob_ext_data *mgmt_rp;
6477 u8 *h192, *r192, *h256, *r256;
6478 struct mgmt_pending_cmd *cmd;
6479 u16 eir_len;
6480 int err;
6481
6482 BT_DBG("%s status %u", hdev->name, status);
6483
6484 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev);
6485 if (!cmd)
6486 return;
6487
6488 mgmt_cp = cmd->param;
6489
6490 if (status) {
6491 status = mgmt_status(status);
6492 eir_len = 0;
6493
6494 h192 = NULL;
6495 r192 = NULL;
6496 h256 = NULL;
6497 r256 = NULL;
6498 } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
6499 struct hci_rp_read_local_oob_data *rp;
6500
6501 if (skb->len != sizeof(*rp)) {
6502 status = MGMT_STATUS_FAILED;
6503 eir_len = 0;
6504 } else {
6505 status = MGMT_STATUS_SUCCESS;
6506 rp = (void *)skb->data;
6507
6508 eir_len = 5 + 18 + 18;
6509 h192 = rp->hash;
6510 r192 = rp->rand;
6511 h256 = NULL;
6512 r256 = NULL;
6513 }
6514 } else {
6515 struct hci_rp_read_local_oob_ext_data *rp;
6516
6517 if (skb->len != sizeof(*rp)) {
6518 status = MGMT_STATUS_FAILED;
6519 eir_len = 0;
6520 } else {
6521 status = MGMT_STATUS_SUCCESS;
6522 rp = (void *)skb->data;
6523
6524 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
6525 eir_len = 5 + 18 + 18;
6526 h192 = NULL;
6527 r192 = NULL;
6528 } else {
6529 eir_len = 5 + 18 + 18 + 18 + 18;
6530 h192 = rp->hash192;
6531 r192 = rp->rand192;
6532 }
6533
6534 h256 = rp->hash256;
6535 r256 = rp->rand256;
6536 }
6537 }
6538
6539 mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL);
6540 if (!mgmt_rp)
6541 goto done;
6542
6543 if (status)
6544 goto send_rsp;
6545
6546 eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV,
6547 hdev->dev_class, 3);
6548
6549 if (h192 && r192) {
6550 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6551 EIR_SSP_HASH_C192, h192, 16);
6552 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6553 EIR_SSP_RAND_R192, r192, 16);
6554 }
6555
6556 if (h256 && r256) {
6557 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6558 EIR_SSP_HASH_C256, h256, 16);
6559 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6560 EIR_SSP_RAND_R256, r256, 16);
6561 }
6562
6563send_rsp:
6564 mgmt_rp->type = mgmt_cp->type;
6565 mgmt_rp->eir_len = cpu_to_le16(eir_len);
6566
6567 err = mgmt_cmd_complete(cmd->sk, hdev->id,
6568 MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status,
6569 mgmt_rp, sizeof(*mgmt_rp) + eir_len);
6570 if (err < 0 || status)
6571 goto done;
6572
6573 hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS);
6574
6575 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
6576 mgmt_rp, sizeof(*mgmt_rp) + eir_len,
6577 HCI_MGMT_OOB_DATA_EVENTS, cmd->sk);
6578done:
6579 kfree(mgmt_rp);
6580 mgmt_pending_remove(cmd);
6581}
6582
6583static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk,
6584 struct mgmt_cp_read_local_oob_ext_data *cp)
6585{
6586 struct mgmt_pending_cmd *cmd;
6587 struct hci_request req;
6588 int err;
6589
6590 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev,
6591 cp, sizeof(*cp));
6592 if (!cmd)
6593 return -ENOMEM;
6594
6595 hci_req_init(&req, hdev);
6596
6597 if (bredr_sc_enabled(hdev))
6598 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
6599 else
6600 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
6601
6602 err = hci_req_run_skb(&req, read_local_oob_ext_data_complete);
6603 if (err < 0) {
6604 mgmt_pending_remove(cmd);
6605 return err;
6606 }
6607
6608 return 0;
6609}
6610
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006611static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
6612 void *data, u16 data_len)
6613{
6614 struct mgmt_cp_read_local_oob_ext_data *cp = data;
6615 struct mgmt_rp_read_local_oob_ext_data *rp;
6616 size_t rp_len;
6617 u16 eir_len;
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006618 u8 status, flags, role, addr[7], hash[16], rand[16];
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006619 int err;
6620
6621 BT_DBG("%s", hdev->name);
6622
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07006623 if (hdev_is_powered(hdev)) {
6624 switch (cp->type) {
6625 case BIT(BDADDR_BREDR):
6626 status = mgmt_bredr_support(hdev);
6627 if (status)
6628 eir_len = 0;
6629 else
6630 eir_len = 5;
6631 break;
6632 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
6633 status = mgmt_le_support(hdev);
6634 if (status)
6635 eir_len = 0;
6636 else
6637 eir_len = 9 + 3 + 18 + 18 + 3;
6638 break;
6639 default:
6640 status = MGMT_STATUS_INVALID_PARAMS;
6641 eir_len = 0;
6642 break;
6643 }
6644 } else {
6645 status = MGMT_STATUS_NOT_POWERED;
6646 eir_len = 0;
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006647 }
6648
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006649 rp_len = sizeof(*rp) + eir_len;
6650 rp = kmalloc(rp_len, GFP_ATOMIC);
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07006651 if (!rp)
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006652 return -ENOMEM;
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07006653
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07006654 if (status)
6655 goto complete;
6656
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07006657 hci_dev_lock(hdev);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006658
6659 eir_len = 0;
6660 switch (cp->type) {
6661 case BIT(BDADDR_BREDR):
Johan Hedberg40f66c02015-04-07 21:52:22 +03006662 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
6663 err = read_local_ssp_oob_req(hdev, sk, cp);
6664 hci_dev_unlock(hdev);
6665 if (!err)
6666 goto done;
6667
6668 status = MGMT_STATUS_FAILED;
6669 goto complete;
6670 } else {
6671 eir_len = eir_append_data(rp->eir, eir_len,
6672 EIR_CLASS_OF_DEV,
6673 hdev->dev_class, 3);
6674 }
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006675 break;
6676 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
Marcel Holtmann5082a592015-03-16 12:39:00 -07006677 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
6678 smp_generate_oob(hdev, hash, rand) < 0) {
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006679 hci_dev_unlock(hdev);
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07006680 status = MGMT_STATUS_FAILED;
6681 goto complete;
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006682 }
6683
Marcel Holtmanne2135682015-04-02 12:00:58 -07006684 /* This should return the active RPA, but since the RPA
6685 * is only programmed on demand, it is really hard to fill
6686 * this in at the moment. For now disallow retrieving
6687 * local out-of-band data when privacy is in use.
6688 *
6689 * Returning the identity address will not help here since
6690 * pairing happens before the identity resolving key is
6691 * known and thus the connection establishment happens
6692 * based on the RPA and not the identity address.
6693 */
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006694 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
Marcel Holtmanne2135682015-04-02 12:00:58 -07006695 hci_dev_unlock(hdev);
6696 status = MGMT_STATUS_REJECTED;
6697 goto complete;
6698 }
6699
6700 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
6701 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
6702 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
6703 bacmp(&hdev->static_addr, BDADDR_ANY))) {
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006704 memcpy(addr, &hdev->static_addr, 6);
6705 addr[6] = 0x01;
6706 } else {
6707 memcpy(addr, &hdev->bdaddr, 6);
6708 addr[6] = 0x00;
6709 }
6710
6711 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR,
6712 addr, sizeof(addr));
6713
6714 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
6715 role = 0x02;
6716 else
6717 role = 0x01;
6718
6719 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE,
6720 &role, sizeof(role));
6721
Marcel Holtmann5082a592015-03-16 12:39:00 -07006722 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) {
6723 eir_len = eir_append_data(rp->eir, eir_len,
6724 EIR_LE_SC_CONFIRM,
6725 hash, sizeof(hash));
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006726
Marcel Holtmann5082a592015-03-16 12:39:00 -07006727 eir_len = eir_append_data(rp->eir, eir_len,
6728 EIR_LE_SC_RANDOM,
6729 rand, sizeof(rand));
6730 }
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006731
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006732 flags = get_adv_discov_flags(hdev);
6733
6734 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
6735 flags |= LE_AD_NO_BREDR;
6736
6737 eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS,
6738 &flags, sizeof(flags));
6739 break;
6740 }
6741
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006742 hci_dev_unlock(hdev);
6743
Marcel Holtmann72000df2015-03-16 16:11:21 -07006744 hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS);
6745
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07006746 status = MGMT_STATUS_SUCCESS;
6747
6748complete:
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07006749 rp->type = cp->type;
6750 rp->eir_len = cpu_to_le16(eir_len);
6751
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006752 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07006753 status, rp, sizeof(*rp) + eir_len);
6754 if (err < 0 || status)
Marcel Holtmann72000df2015-03-16 16:11:21 -07006755 goto done;
6756
6757 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
6758 rp, sizeof(*rp) + eir_len,
6759 HCI_MGMT_OOB_DATA_EVENTS, sk);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006760
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006761done:
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006762 kfree(rp);
6763
6764 return err;
6765}
6766
Arman Uguray089fa8c2015-03-25 18:53:45 -07006767static u32 get_supported_adv_flags(struct hci_dev *hdev)
6768{
6769 u32 flags = 0;
6770
6771 flags |= MGMT_ADV_FLAG_CONNECTABLE;
6772 flags |= MGMT_ADV_FLAG_DISCOV;
6773 flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
6774 flags |= MGMT_ADV_FLAG_MANAGED_FLAGS;
6775
6776 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID)
6777 flags |= MGMT_ADV_FLAG_TX_POWER;
6778
6779 return flags;
6780}
6781
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006782static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
6783 void *data, u16 data_len)
6784{
6785 struct mgmt_rp_read_adv_features *rp;
6786 size_t rp_len;
Florian Grandel286e0c82015-06-18 03:16:38 +02006787 int err, i;
Arman Uguray24b4f382015-03-23 15:57:12 -07006788 bool instance;
Florian Grandel286e0c82015-06-18 03:16:38 +02006789 struct adv_info *adv_instance;
Arman Uguray089fa8c2015-03-25 18:53:45 -07006790 u32 supported_flags;
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006791
6792 BT_DBG("%s", hdev->name);
6793
Arman Uguray089fa8c2015-03-25 18:53:45 -07006794 if (!lmp_le_capable(hdev))
6795 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
6796 MGMT_STATUS_REJECTED);
6797
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006798 hci_dev_lock(hdev);
6799
6800 rp_len = sizeof(*rp);
Arman Uguray24b4f382015-03-23 15:57:12 -07006801
Arman Uguray24b4f382015-03-23 15:57:12 -07006802 instance = hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE);
6803 if (instance)
Florian Grandel286e0c82015-06-18 03:16:38 +02006804 rp_len += hdev->adv_instance_cnt;
Arman Uguray24b4f382015-03-23 15:57:12 -07006805
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006806 rp = kmalloc(rp_len, GFP_ATOMIC);
6807 if (!rp) {
6808 hci_dev_unlock(hdev);
6809 return -ENOMEM;
6810 }
6811
Arman Uguray089fa8c2015-03-25 18:53:45 -07006812 supported_flags = get_supported_adv_flags(hdev);
6813
6814 rp->supported_flags = cpu_to_le32(supported_flags);
Marcel Holtmanndc5d82a2015-03-19 17:22:25 -07006815 rp->max_adv_data_len = HCI_MAX_AD_LENGTH;
6816 rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH;
Florian Grandeld2609b32015-06-18 03:16:34 +02006817 rp->max_instances = HCI_MAX_ADV_INSTANCES;
Arman Uguray24b4f382015-03-23 15:57:12 -07006818
Arman Uguray24b4f382015-03-23 15:57:12 -07006819 if (instance) {
Florian Grandel286e0c82015-06-18 03:16:38 +02006820 i = 0;
6821 list_for_each_entry(adv_instance, &hdev->adv_instances, list) {
6822 if (i >= hdev->adv_instance_cnt)
6823 break;
6824
6825 rp->instance[i] = adv_instance->instance;
6826 i++;
6827 }
6828 rp->num_instances = hdev->adv_instance_cnt;
Arman Uguray24b4f382015-03-23 15:57:12 -07006829 } else {
6830 rp->num_instances = 0;
6831 }
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006832
6833 hci_dev_unlock(hdev);
6834
6835 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
6836 MGMT_STATUS_SUCCESS, rp, rp_len);
6837
6838 kfree(rp);
6839
6840 return err;
6841}
6842
Arman Uguray4117ed72015-03-23 15:57:14 -07006843static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data,
Arman Ugurayb44133f2015-03-25 18:53:41 -07006844 u8 len, bool is_adv_data)
Arman Uguray24b4f382015-03-23 15:57:12 -07006845{
Arman Uguray4117ed72015-03-23 15:57:14 -07006846 u8 max_len = HCI_MAX_AD_LENGTH;
Arman Uguray24b4f382015-03-23 15:57:12 -07006847 int i, cur_len;
Arman Ugurayb44133f2015-03-25 18:53:41 -07006848 bool flags_managed = false;
Arman Uguray5507e352015-03-25 18:53:44 -07006849 bool tx_power_managed = false;
Arman Uguray67e0c0c2015-03-25 18:53:43 -07006850 u32 flags_params = MGMT_ADV_FLAG_DISCOV | MGMT_ADV_FLAG_LIMITED_DISCOV |
6851 MGMT_ADV_FLAG_MANAGED_FLAGS;
Arman Uguray24b4f382015-03-23 15:57:12 -07006852
Arman Uguray807ec772015-03-25 18:53:42 -07006853 if (is_adv_data && (adv_flags & flags_params)) {
Arman Ugurayb44133f2015-03-25 18:53:41 -07006854 flags_managed = true;
6855 max_len -= 3;
6856 }
Arman Uguray24b4f382015-03-23 15:57:12 -07006857
Arman Uguray5507e352015-03-25 18:53:44 -07006858 if (is_adv_data && (adv_flags & MGMT_ADV_FLAG_TX_POWER)) {
6859 tx_power_managed = true;
6860 max_len -= 3;
6861 }
6862
Arman Uguray4117ed72015-03-23 15:57:14 -07006863 if (len > max_len)
Arman Uguray24b4f382015-03-23 15:57:12 -07006864 return false;
6865
Arman Uguray4117ed72015-03-23 15:57:14 -07006866 /* Make sure that the data is correctly formatted. */
6867 for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) {
6868 cur_len = data[i];
Arman Uguray24b4f382015-03-23 15:57:12 -07006869
Arman Ugurayb44133f2015-03-25 18:53:41 -07006870 if (flags_managed && data[i + 1] == EIR_FLAGS)
6871 return false;
6872
Arman Uguray5507e352015-03-25 18:53:44 -07006873 if (tx_power_managed && data[i + 1] == EIR_TX_POWER)
6874 return false;
6875
Arman Uguray24b4f382015-03-23 15:57:12 -07006876 /* If the current field length would exceed the total data
6877 * length, then it's invalid.
6878 */
Arman Uguray4117ed72015-03-23 15:57:14 -07006879 if (i + cur_len >= len)
Arman Uguray24b4f382015-03-23 15:57:12 -07006880 return false;
6881 }
6882
6883 return true;
6884}
6885
Arman Uguray24b4f382015-03-23 15:57:12 -07006886static void add_advertising_complete(struct hci_dev *hdev, u8 status,
6887 u16 opcode)
6888{
6889 struct mgmt_pending_cmd *cmd;
6890 struct mgmt_rp_add_advertising rp;
6891
6892 BT_DBG("status %d", status);
6893
6894 hci_dev_lock(hdev);
6895
6896 cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev);
6897
6898 if (status) {
6899 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
6900 memset(&hdev->adv_instance, 0, sizeof(hdev->adv_instance));
6901 advertising_removed(cmd ? cmd->sk : NULL, hdev, 1);
6902 }
6903
6904 if (!cmd)
6905 goto unlock;
6906
6907 rp.instance = 0x01;
6908
6909 if (status)
6910 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
6911 mgmt_status(status));
6912 else
6913 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
6914 mgmt_status(status), &rp, sizeof(rp));
6915
6916 mgmt_pending_remove(cmd);
6917
6918unlock:
6919 hci_dev_unlock(hdev);
6920}
6921
Florian Grandel5d900e42015-06-18 03:16:35 +02006922void mgmt_adv_timeout_expired(struct hci_dev *hdev)
Arman Uguray912098a2015-03-23 15:57:15 -07006923{
Florian Grandel5d900e42015-06-18 03:16:35 +02006924 hdev->adv_instance_timeout = 0;
Arman Uguray912098a2015-03-23 15:57:15 -07006925
6926 hci_dev_lock(hdev);
6927 clear_adv_instance(hdev);
6928 hci_dev_unlock(hdev);
6929}
6930
Arman Uguray24b4f382015-03-23 15:57:12 -07006931static int add_advertising(struct sock *sk, struct hci_dev *hdev,
6932 void *data, u16 data_len)
6933{
6934 struct mgmt_cp_add_advertising *cp = data;
6935 struct mgmt_rp_add_advertising rp;
6936 u32 flags;
Arman Uguray089fa8c2015-03-25 18:53:45 -07006937 u32 supported_flags;
Arman Uguray24b4f382015-03-23 15:57:12 -07006938 u8 status;
Arman Uguray912098a2015-03-23 15:57:15 -07006939 u16 timeout;
Arman Uguray24b4f382015-03-23 15:57:12 -07006940 int err;
6941 struct mgmt_pending_cmd *cmd;
6942 struct hci_request req;
6943
6944 BT_DBG("%s", hdev->name);
6945
6946 status = mgmt_le_support(hdev);
6947 if (status)
6948 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6949 status);
6950
6951 flags = __le32_to_cpu(cp->flags);
Arman Uguray912098a2015-03-23 15:57:15 -07006952 timeout = __le16_to_cpu(cp->timeout);
Arman Uguray24b4f382015-03-23 15:57:12 -07006953
Arman Uguray089fa8c2015-03-25 18:53:45 -07006954 /* The current implementation only supports adding one instance and only
6955 * a subset of the specified flags.
6956 */
6957 supported_flags = get_supported_adv_flags(hdev);
6958 if (cp->instance != 0x01 || (flags & ~supported_flags))
Arman Uguray24b4f382015-03-23 15:57:12 -07006959 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6960 MGMT_STATUS_INVALID_PARAMS);
6961
6962 hci_dev_lock(hdev);
6963
Arman Uguray912098a2015-03-23 15:57:15 -07006964 if (timeout && !hdev_is_powered(hdev)) {
6965 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6966 MGMT_STATUS_REJECTED);
6967 goto unlock;
6968 }
6969
Arman Uguray24b4f382015-03-23 15:57:12 -07006970 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
Arman Ugurayda9293352015-03-23 15:57:13 -07006971 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
Arman Uguray24b4f382015-03-23 15:57:12 -07006972 pending_find(MGMT_OP_SET_LE, hdev)) {
6973 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6974 MGMT_STATUS_BUSY);
6975 goto unlock;
6976 }
6977
Arman Ugurayb44133f2015-03-25 18:53:41 -07006978 if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) ||
Arman Uguray4117ed72015-03-23 15:57:14 -07006979 !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len,
Arman Ugurayb44133f2015-03-25 18:53:41 -07006980 cp->scan_rsp_len, false)) {
Arman Uguray24b4f382015-03-23 15:57:12 -07006981 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6982 MGMT_STATUS_INVALID_PARAMS);
6983 goto unlock;
6984 }
6985
6986 hdev->adv_instance.flags = flags;
6987 hdev->adv_instance.adv_data_len = cp->adv_data_len;
6988 hdev->adv_instance.scan_rsp_len = cp->scan_rsp_len;
6989
6990 if (cp->adv_data_len)
6991 memcpy(hdev->adv_instance.adv_data, cp->data, cp->adv_data_len);
6992
6993 if (cp->scan_rsp_len)
6994 memcpy(hdev->adv_instance.scan_rsp_data,
6995 cp->data + cp->adv_data_len, cp->scan_rsp_len);
6996
Florian Grandel5d900e42015-06-18 03:16:35 +02006997 if (hdev->adv_instance_timeout)
6998 cancel_delayed_work(&hdev->adv_instance_expire);
Arman Uguray912098a2015-03-23 15:57:15 -07006999
Florian Grandel5d900e42015-06-18 03:16:35 +02007000 hdev->adv_instance_timeout = timeout;
Arman Uguray912098a2015-03-23 15:57:15 -07007001
7002 if (timeout)
7003 queue_delayed_work(hdev->workqueue,
Florian Grandel5d900e42015-06-18 03:16:35 +02007004 &hdev->adv_instance_expire,
Arman Uguray912098a2015-03-23 15:57:15 -07007005 msecs_to_jiffies(timeout * 1000));
7006
Arman Uguray24b4f382015-03-23 15:57:12 -07007007 if (!hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING_INSTANCE))
7008 advertising_added(sk, hdev, 1);
7009
7010 /* If the HCI_ADVERTISING flag is set or the device isn't powered then
7011 * we have no HCI communication to make. Simply return.
7012 */
7013 if (!hdev_is_powered(hdev) ||
7014 hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
7015 rp.instance = 0x01;
7016 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7017 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
7018 goto unlock;
7019 }
7020
7021 /* We're good to go, update advertising data, parameters, and start
7022 * advertising.
7023 */
7024 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data,
7025 data_len);
7026 if (!cmd) {
7027 err = -ENOMEM;
7028 goto unlock;
7029 }
7030
7031 hci_req_init(&req, hdev);
7032
7033 update_adv_data(&req);
Arman Uguray4117ed72015-03-23 15:57:14 -07007034 update_scan_rsp_data(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07007035 enable_advertising(&req);
7036
7037 err = hci_req_run(&req, add_advertising_complete);
7038 if (err < 0)
7039 mgmt_pending_remove(cmd);
7040
7041unlock:
7042 hci_dev_unlock(hdev);
7043
7044 return err;
7045}
7046
Arman Ugurayda9293352015-03-23 15:57:13 -07007047static void remove_advertising_complete(struct hci_dev *hdev, u8 status,
7048 u16 opcode)
7049{
7050 struct mgmt_pending_cmd *cmd;
7051 struct mgmt_rp_remove_advertising rp;
7052
7053 BT_DBG("status %d", status);
7054
7055 hci_dev_lock(hdev);
7056
7057 /* A failure status here only means that we failed to disable
7058 * advertising. Otherwise, the advertising instance has been removed,
7059 * so report success.
7060 */
7061 cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev);
7062 if (!cmd)
7063 goto unlock;
7064
7065 rp.instance = 1;
7066
7067 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS,
7068 &rp, sizeof(rp));
7069 mgmt_pending_remove(cmd);
7070
7071unlock:
7072 hci_dev_unlock(hdev);
7073}
7074
7075static int remove_advertising(struct sock *sk, struct hci_dev *hdev,
7076 void *data, u16 data_len)
7077{
7078 struct mgmt_cp_remove_advertising *cp = data;
7079 struct mgmt_rp_remove_advertising rp;
7080 int err;
7081 struct mgmt_pending_cmd *cmd;
7082 struct hci_request req;
7083
7084 BT_DBG("%s", hdev->name);
7085
7086 /* The current implementation only allows modifying instance no 1. A
7087 * value of 0 indicates that all instances should be cleared.
7088 */
7089 if (cp->instance > 1)
7090 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
7091 MGMT_STATUS_INVALID_PARAMS);
7092
7093 hci_dev_lock(hdev);
7094
7095 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
7096 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
7097 pending_find(MGMT_OP_SET_LE, hdev)) {
7098 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
7099 MGMT_STATUS_BUSY);
7100 goto unlock;
7101 }
7102
7103 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE)) {
7104 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
7105 MGMT_STATUS_INVALID_PARAMS);
7106 goto unlock;
7107 }
7108
Florian Grandel5d900e42015-06-18 03:16:35 +02007109 if (hdev->adv_instance_timeout)
7110 cancel_delayed_work(&hdev->adv_instance_expire);
Arman Uguray912098a2015-03-23 15:57:15 -07007111
Arman Ugurayda9293352015-03-23 15:57:13 -07007112 memset(&hdev->adv_instance, 0, sizeof(hdev->adv_instance));
7113
7114 advertising_removed(sk, hdev, 1);
7115
7116 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
7117
7118 /* If the HCI_ADVERTISING flag is set or the device isn't powered then
7119 * we have no HCI communication to make. Simply return.
7120 */
7121 if (!hdev_is_powered(hdev) ||
7122 hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
7123 rp.instance = 1;
7124 err = mgmt_cmd_complete(sk, hdev->id,
7125 MGMT_OP_REMOVE_ADVERTISING,
7126 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
7127 goto unlock;
7128 }
7129
7130 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data,
7131 data_len);
7132 if (!cmd) {
7133 err = -ENOMEM;
7134 goto unlock;
7135 }
7136
7137 hci_req_init(&req, hdev);
7138 disable_advertising(&req);
7139
7140 err = hci_req_run(&req, remove_advertising_complete);
7141 if (err < 0)
7142 mgmt_pending_remove(cmd);
7143
7144unlock:
7145 hci_dev_unlock(hdev);
7146
7147 return err;
7148}
7149
Johan Hedberg6d785aa32015-03-06 21:08:51 +02007150static const struct hci_mgmt_handler mgmt_handlers[] = {
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02007151 { NULL }, /* 0x0000 (no command) */
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007152 { read_version, MGMT_READ_VERSION_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007153 HCI_MGMT_NO_HDEV |
7154 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007155 { read_commands, MGMT_READ_COMMANDS_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007156 HCI_MGMT_NO_HDEV |
7157 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007158 { read_index_list, MGMT_READ_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007159 HCI_MGMT_NO_HDEV |
7160 HCI_MGMT_UNTRUSTED },
7161 { read_controller_info, MGMT_READ_INFO_SIZE,
7162 HCI_MGMT_UNTRUSTED },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07007163 { set_powered, MGMT_SETTING_SIZE },
7164 { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE },
7165 { set_connectable, MGMT_SETTING_SIZE },
7166 { set_fast_connectable, MGMT_SETTING_SIZE },
7167 { set_bondable, MGMT_SETTING_SIZE },
7168 { set_link_security, MGMT_SETTING_SIZE },
7169 { set_ssp, MGMT_SETTING_SIZE },
7170 { set_hs, MGMT_SETTING_SIZE },
7171 { set_le, MGMT_SETTING_SIZE },
7172 { set_dev_class, MGMT_SET_DEV_CLASS_SIZE },
7173 { set_local_name, MGMT_SET_LOCAL_NAME_SIZE },
7174 { add_uuid, MGMT_ADD_UUID_SIZE },
7175 { remove_uuid, MGMT_REMOVE_UUID_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007176 { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE,
7177 HCI_MGMT_VAR_LEN },
7178 { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE,
7179 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07007180 { disconnect, MGMT_DISCONNECT_SIZE },
7181 { get_connections, MGMT_GET_CONNECTIONS_SIZE },
7182 { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE },
7183 { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE },
7184 { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE },
7185 { pair_device, MGMT_PAIR_DEVICE_SIZE },
7186 { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE },
7187 { unpair_device, MGMT_UNPAIR_DEVICE_SIZE },
7188 { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE },
7189 { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
7190 { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE },
7191 { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007192 { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE },
7193 { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE,
7194 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07007195 { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
7196 { start_discovery, MGMT_START_DISCOVERY_SIZE },
7197 { stop_discovery, MGMT_STOP_DISCOVERY_SIZE },
7198 { confirm_name, MGMT_CONFIRM_NAME_SIZE },
7199 { block_device, MGMT_BLOCK_DEVICE_SIZE },
7200 { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE },
7201 { set_device_id, MGMT_SET_DEVICE_ID_SIZE },
7202 { set_advertising, MGMT_SETTING_SIZE },
7203 { set_bredr, MGMT_SETTING_SIZE },
7204 { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE },
7205 { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE },
7206 { set_secure_conn, MGMT_SETTING_SIZE },
7207 { set_debug_keys, MGMT_SETTING_SIZE },
7208 { set_privacy, MGMT_SET_PRIVACY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007209 { load_irks, MGMT_LOAD_IRKS_SIZE,
7210 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07007211 { get_conn_info, MGMT_GET_CONN_INFO_SIZE },
7212 { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE },
7213 { add_device, MGMT_ADD_DEVICE_SIZE },
7214 { remove_device, MGMT_REMOVE_DEVICE_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007215 { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE,
7216 HCI_MGMT_VAR_LEN },
7217 { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007218 HCI_MGMT_NO_HDEV |
7219 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007220 { read_config_info, MGMT_READ_CONFIG_INFO_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007221 HCI_MGMT_UNCONFIGURED |
7222 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007223 { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE,
7224 HCI_MGMT_UNCONFIGURED },
7225 { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE,
7226 HCI_MGMT_UNCONFIGURED },
7227 { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE,
7228 HCI_MGMT_VAR_LEN },
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007229 { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE },
Marcel Holtmann96f14742015-03-14 19:27:57 -07007230 { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007231 HCI_MGMT_NO_HDEV |
7232 HCI_MGMT_UNTRUSTED },
Marcel Holtmannd3d53052015-03-14 20:53:25 -07007233 { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE },
Arman Uguray24b4f382015-03-23 15:57:12 -07007234 { add_advertising, MGMT_ADD_ADVERTISING_SIZE,
7235 HCI_MGMT_VAR_LEN },
Arman Ugurayda9293352015-03-23 15:57:13 -07007236 { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE },
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02007237};
7238
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07007239void mgmt_index_added(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02007240{
Marcel Holtmannced85542015-03-14 19:27:56 -07007241 struct mgmt_ev_ext_index ev;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +03007242
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02007243 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
7244 return;
7245
Marcel Holtmannf9207332015-03-14 19:27:55 -07007246 switch (hdev->dev_type) {
7247 case HCI_BREDR:
7248 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
7249 mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev,
7250 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007251 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007252 } else {
7253 mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0,
7254 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007255 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007256 }
7257 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07007258 case HCI_AMP:
7259 ev.type = 0x02;
7260 break;
7261 default:
7262 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007263 }
Marcel Holtmannced85542015-03-14 19:27:56 -07007264
7265 ev.bus = hdev->bus;
7266
7267 mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev),
7268 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergc71e97b2010-12-13 21:07:07 +02007269}
7270
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07007271void mgmt_index_removed(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02007272{
Marcel Holtmannced85542015-03-14 19:27:56 -07007273 struct mgmt_ev_ext_index ev;
Johan Hedberg5f159032012-03-02 03:13:19 +02007274 u8 status = MGMT_STATUS_INVALID_INDEX;
Johan Hedbergb24752f2011-11-03 14:40:33 +02007275
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02007276 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
7277 return;
7278
Marcel Holtmannf9207332015-03-14 19:27:55 -07007279 switch (hdev->dev_type) {
7280 case HCI_BREDR:
7281 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedbergb24752f2011-11-03 14:40:33 +02007282
Marcel Holtmannf9207332015-03-14 19:27:55 -07007283 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
7284 mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev,
7285 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007286 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007287 } else {
7288 mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0,
7289 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007290 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007291 }
7292 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07007293 case HCI_AMP:
7294 ev.type = 0x02;
7295 break;
7296 default:
7297 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007298 }
Marcel Holtmannced85542015-03-14 19:27:56 -07007299
7300 ev.bus = hdev->bus;
7301
7302 mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev),
7303 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02007304}
7305
Andre Guedes6046dc32014-02-26 20:21:51 -03007306/* This function requires the caller holds hdev->lock */
Johan Hedberg2cf22212014-12-19 22:26:00 +02007307static void restart_le_actions(struct hci_request *req)
Andre Guedes6046dc32014-02-26 20:21:51 -03007308{
Johan Hedberg2cf22212014-12-19 22:26:00 +02007309 struct hci_dev *hdev = req->hdev;
Andre Guedes6046dc32014-02-26 20:21:51 -03007310 struct hci_conn_params *p;
7311
7312 list_for_each_entry(p, &hdev->le_conn_params, list) {
Johan Hedbergd7347f32014-07-04 12:37:23 +03007313 /* Needed for AUTO_OFF case where might not "really"
7314 * have been powered off.
7315 */
7316 list_del_init(&p->action);
7317
7318 switch (p->auto_connect) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02007319 case HCI_AUTO_CONN_DIRECT:
Johan Hedbergd7347f32014-07-04 12:37:23 +03007320 case HCI_AUTO_CONN_ALWAYS:
7321 list_add(&p->action, &hdev->pend_le_conns);
7322 break;
7323 case HCI_AUTO_CONN_REPORT:
7324 list_add(&p->action, &hdev->pend_le_reports);
7325 break;
7326 default:
7327 break;
Marcel Holtmannc83ed192014-07-01 19:28:24 +02007328 }
Andre Guedes6046dc32014-02-26 20:21:51 -03007329 }
Marcel Holtmannc83ed192014-07-01 19:28:24 +02007330
Johan Hedberg2cf22212014-12-19 22:26:00 +02007331 __hci_update_background_scan(req);
Andre Guedes6046dc32014-02-26 20:21:51 -03007332}
7333
Marcel Holtmann1904a852015-01-11 13:50:44 -08007334static void powered_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg229ab392013-03-15 17:06:53 -05007335{
7336 struct cmd_lookup match = { NULL, hdev };
7337
7338 BT_DBG("status 0x%02x", status);
7339
Marcel Holtmann162a3ba2015-01-14 15:43:11 -08007340 if (!status) {
7341 /* Register the available SMP channels (BR/EDR and LE) only
7342 * when successfully powering on the controller. This late
7343 * registration is required so that LE SMP can clearly
7344 * decide if the public address or static address is used.
7345 */
7346 smp_register(hdev);
7347 }
7348
Johan Hedberg229ab392013-03-15 17:06:53 -05007349 hci_dev_lock(hdev);
7350
7351 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
7352
7353 new_settings(hdev, match.sk);
7354
7355 hci_dev_unlock(hdev);
7356
7357 if (match.sk)
7358 sock_put(match.sk);
7359}
7360
Johan Hedberg70da6242013-03-15 17:06:51 -05007361static int powered_update_hci(struct hci_dev *hdev)
7362{
Johan Hedberg890ea892013-03-15 17:06:52 -05007363 struct hci_request req;
Johan Hedberg70da6242013-03-15 17:06:51 -05007364 u8 link_sec;
7365
Johan Hedberg890ea892013-03-15 17:06:52 -05007366 hci_req_init(&req, hdev);
7367
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007368 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
Johan Hedberg70da6242013-03-15 17:06:51 -05007369 !lmp_host_ssp_capable(hdev)) {
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08007370 u8 mode = 0x01;
Johan Hedberg70da6242013-03-15 17:06:51 -05007371
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08007372 hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode);
Johan Hedberg70da6242013-03-15 17:06:51 -05007373
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08007374 if (bredr_sc_enabled(hdev) && !lmp_host_sc_capable(hdev)) {
7375 u8 support = 0x01;
7376
7377 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT,
7378 sizeof(support), &support);
7379 }
Johan Hedbergec6f99b2014-12-12 13:30:11 +02007380 }
7381
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007382 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
Johan Hedbergc73eee92013-04-19 18:35:21 +03007383 lmp_bredr_capable(hdev)) {
Johan Hedberg70da6242013-03-15 17:06:51 -05007384 struct hci_cp_write_le_host_supported cp;
7385
Marcel Holtmann32226e42014-07-24 20:04:16 +02007386 cp.le = 0x01;
7387 cp.simul = 0x00;
Johan Hedberg70da6242013-03-15 17:06:51 -05007388
7389 /* Check first if we already have the right
7390 * host state (host features set)
7391 */
7392 if (cp.le != lmp_host_le_capable(hdev) ||
7393 cp.simul != lmp_host_le_br_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05007394 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED,
7395 sizeof(cp), &cp);
Johan Hedberg70da6242013-03-15 17:06:51 -05007396 }
7397
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07007398 if (lmp_le_capable(hdev)) {
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07007399 /* Make sure the controller has a good default for
7400 * advertising data. This also applies to the case
7401 * where BR/EDR was toggled during the AUTO_OFF phase.
7402 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007403 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07007404 update_adv_data(&req);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07007405 update_scan_rsp_data(&req);
7406 }
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07007407
Arman Uguray24b4f382015-03-23 15:57:12 -07007408 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
7409 hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07007410 enable_advertising(&req);
Johan Hedberg2cf22212014-12-19 22:26:00 +02007411
7412 restart_le_actions(&req);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03007413 }
7414
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007415 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg70da6242013-03-15 17:06:51 -05007416 if (link_sec != test_bit(HCI_AUTH, &hdev->flags))
Johan Hedberg890ea892013-03-15 17:06:52 -05007417 hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE,
7418 sizeof(link_sec), &link_sec);
Johan Hedberg70da6242013-03-15 17:06:51 -05007419
7420 if (lmp_bredr_capable(hdev)) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007421 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
Johan Hedberg406ef2a2015-03-10 20:14:27 +02007422 write_fast_connectable(&req, true);
7423 else
7424 write_fast_connectable(&req, false);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02007425 __hci_update_page_scan(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05007426 update_class(&req);
Johan Hedberg13928972013-03-15 17:07:00 -05007427 update_name(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05007428 update_eir(&req);
Johan Hedberg70da6242013-03-15 17:06:51 -05007429 }
7430
Johan Hedberg229ab392013-03-15 17:06:53 -05007431 return hci_req_run(&req, powered_complete);
Johan Hedberg70da6242013-03-15 17:06:51 -05007432}
7433
Johan Hedberg744cf192011-11-08 20:40:14 +02007434int mgmt_powered(struct hci_dev *hdev, u8 powered)
Johan Hedberg5add6af2010-12-16 10:00:37 +02007435{
Johan Hedberg76a7f3a2012-02-17 00:34:40 +02007436 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg98459042014-12-12 11:15:21 +02007437 u8 status, zero_cod[] = { 0, 0, 0 };
Johan Hedberg7bb895d2012-02-17 01:20:00 +02007438 int err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02007439
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007440 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg5e5282b2012-02-21 16:01:30 +02007441 return 0;
7442
Johan Hedberg5e5282b2012-02-21 16:01:30 +02007443 if (powered) {
Johan Hedberg229ab392013-03-15 17:06:53 -05007444 if (powered_update_hci(hdev) == 0)
7445 return 0;
Johan Hedbergfe038882013-01-16 16:15:34 +02007446
Johan Hedberg229ab392013-03-15 17:06:53 -05007447 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp,
7448 &match);
7449 goto new_settings;
Johan Hedbergb24752f2011-11-03 14:40:33 +02007450 }
7451
Johan Hedberg229ab392013-03-15 17:06:53 -05007452 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
Johan Hedberg98459042014-12-12 11:15:21 +02007453
7454 /* If the power off is because of hdev unregistration let
7455 * use the appropriate INVALID_INDEX status. Otherwise use
7456 * NOT_POWERED. We cover both scenarios here since later in
7457 * mgmt_index_removed() any hci_conn callbacks will have already
7458 * been triggered, potentially causing misleading DISCONNECTED
7459 * status responses.
7460 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007461 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
Johan Hedberg98459042014-12-12 11:15:21 +02007462 status = MGMT_STATUS_INVALID_INDEX;
7463 else
7464 status = MGMT_STATUS_NOT_POWERED;
7465
7466 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedberg229ab392013-03-15 17:06:53 -05007467
7468 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0)
Marcel Holtmannf6b77122015-03-14 19:28:05 -07007469 mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
7470 zero_cod, sizeof(zero_cod), NULL);
Johan Hedberg229ab392013-03-15 17:06:53 -05007471
7472new_settings:
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02007473 err = new_settings(hdev, match.sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02007474
7475 if (match.sk)
7476 sock_put(match.sk);
7477
Johan Hedberg7bb895d2012-02-17 01:20:00 +02007478 return err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02007479}
Johan Hedberg73f22f62010-12-29 16:00:25 +02007480
Marcel Holtmann3eec7052013-10-06 23:55:46 -07007481void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
Johan Hedberg96570ff2013-05-29 09:51:29 +03007482{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007483 struct mgmt_pending_cmd *cmd;
Johan Hedberg96570ff2013-05-29 09:51:29 +03007484 u8 status;
7485
Johan Hedberg333ae952015-03-17 13:48:47 +02007486 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg96570ff2013-05-29 09:51:29 +03007487 if (!cmd)
Marcel Holtmann3eec7052013-10-06 23:55:46 -07007488 return;
Johan Hedberg96570ff2013-05-29 09:51:29 +03007489
7490 if (err == -ERFKILL)
7491 status = MGMT_STATUS_RFKILLED;
7492 else
7493 status = MGMT_STATUS_FAILED;
7494
Johan Hedberga69e8372015-03-06 21:08:53 +02007495 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
Johan Hedberg96570ff2013-05-29 09:51:29 +03007496
7497 mgmt_pending_remove(cmd);
Johan Hedberg96570ff2013-05-29 09:51:29 +03007498}
7499
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007500void mgmt_discoverable_timeout(struct hci_dev *hdev)
7501{
7502 struct hci_request req;
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007503
7504 hci_dev_lock(hdev);
7505
7506 /* When discoverable timeout triggers, then just make sure
7507 * the limited discoverable flag is cleared. Even in the case
7508 * of a timeout triggered from general discoverable, it is
7509 * safe to unconditionally clear the flag.
7510 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07007511 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
7512 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007513
7514 hci_req_init(&req, hdev);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007515 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg4b580612013-10-19 23:38:21 +03007516 u8 scan = SCAN_PAGE;
7517 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE,
7518 sizeof(scan), &scan);
7519 }
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007520 update_class(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07007521
7522 /* Advertising instances don't use the global discoverable setting, so
7523 * only update AD if advertising was enabled using Set Advertising.
7524 */
7525 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
7526 update_adv_data(&req);
7527
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007528 hci_req_run(&req, NULL);
7529
7530 hdev->discov_timeout = 0;
7531
Johan Hedberg9a43e252013-10-20 19:00:07 +03007532 new_settings(hdev, NULL);
7533
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07007534 hci_dev_unlock(hdev);
7535}
7536
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07007537void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
7538 bool persistent)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007539{
Johan Hedberg86742e12011-11-07 23:13:38 +02007540 struct mgmt_ev_new_link_key ev;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007541
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007542 memset(&ev, 0, sizeof(ev));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007543
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007544 ev.store_hint = persistent;
Johan Hedbergd753fdc2012-02-17 14:06:34 +02007545 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03007546 ev.key.addr.type = BDADDR_BREDR;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007547 ev.key.type = key->type;
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03007548 memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007549 ev.key.pin_len = key->pin_len;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007550
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07007551 mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007552}
Johan Hedbergf7520542011-01-20 12:34:39 +02007553
Johan Hedbergd7b25452014-05-23 13:19:53 +03007554static u8 mgmt_ltk_type(struct smp_ltk *ltk)
7555{
Johan Hedberg23fb8de2014-05-23 13:15:37 +03007556 switch (ltk->type) {
7557 case SMP_LTK:
7558 case SMP_LTK_SLAVE:
7559 if (ltk->authenticated)
7560 return MGMT_LTK_AUTHENTICATED;
7561 return MGMT_LTK_UNAUTHENTICATED;
7562 case SMP_LTK_P256:
7563 if (ltk->authenticated)
7564 return MGMT_LTK_P256_AUTH;
7565 return MGMT_LTK_P256_UNAUTH;
7566 case SMP_LTK_P256_DEBUG:
7567 return MGMT_LTK_P256_DEBUG;
7568 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03007569
7570 return MGMT_LTK_UNAUTHENTICATED;
7571}
7572
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007573void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007574{
7575 struct mgmt_ev_new_long_term_key ev;
7576
7577 memset(&ev, 0, sizeof(ev));
7578
Marcel Holtmann5192d302014-02-19 17:11:58 -08007579 /* Devices using resolvable or non-resolvable random addresses
Florian Grandelf72186d2015-05-26 03:31:09 +02007580 * without providing an identity resolving key don't require
Marcel Holtmann5192d302014-02-19 17:11:58 -08007581 * to store long term keys. Their addresses will change the
7582 * next time around.
7583 *
7584 * Only when a remote device provides an identity address
7585 * make sure the long term key is stored. If the remote
7586 * identity is known, the long term keys are internally
7587 * mapped to the identity address. So allow static random
7588 * and public addresses here.
7589 */
Johan Hedbergba74b662014-02-19 14:57:45 +02007590 if (key->bdaddr_type == ADDR_LE_DEV_RANDOM &&
7591 (key->bdaddr.b[5] & 0xc0) != 0xc0)
7592 ev.store_hint = 0x00;
7593 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007594 ev.store_hint = persistent;
Johan Hedbergba74b662014-02-19 14:57:45 +02007595
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007596 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007597 ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
Johan Hedbergd7b25452014-05-23 13:19:53 +03007598 ev.key.type = mgmt_ltk_type(key);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007599 ev.key.enc_size = key->enc_size;
7600 ev.key.ediv = key->ediv;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08007601 ev.key.rand = key->rand;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007602
Johan Hedberg2ceba532014-06-16 19:25:16 +03007603 if (key->type == SMP_LTK)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007604 ev.key.master = 1;
7605
Johan Hedberg1fc62c52015-06-10 11:11:20 +03007606 /* Make sure we copy only the significant bytes based on the
7607 * encryption key size, and set the rest of the value to zeroes.
7608 */
7609 memcpy(ev.key.val, key->val, sizeof(key->enc_size));
7610 memset(ev.key.val + key->enc_size, 0,
7611 sizeof(ev.key.val) - key->enc_size);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007612
Marcel Holtmann083368f2013-10-15 14:26:29 -07007613 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007614}
7615
Johan Hedberg95fbac82014-02-19 15:18:31 +02007616void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk)
7617{
7618 struct mgmt_ev_new_irk ev;
7619
7620 memset(&ev, 0, sizeof(ev));
7621
Marcel Holtmannbab6d1e2014-02-19 11:51:54 -08007622 /* For identity resolving keys from devices that are already
7623 * using a public address or static random address, do not
7624 * ask for storing this key. The identity resolving key really
Florian Grandelf72186d2015-05-26 03:31:09 +02007625 * is only mandatory for devices using resolvable random
Marcel Holtmannbab6d1e2014-02-19 11:51:54 -08007626 * addresses.
7627 *
7628 * Storing all identity resolving keys has the downside that
7629 * they will be also loaded on next boot of they system. More
7630 * identity resolving keys, means more time during scanning is
7631 * needed to actually resolve these addresses.
7632 */
7633 if (bacmp(&irk->rpa, BDADDR_ANY))
7634 ev.store_hint = 0x01;
7635 else
7636 ev.store_hint = 0x00;
7637
Johan Hedberg95fbac82014-02-19 15:18:31 +02007638 bacpy(&ev.rpa, &irk->rpa);
7639 bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr);
7640 ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type);
7641 memcpy(ev.irk.val, irk->val, sizeof(irk->val));
7642
7643 mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
7644}
7645
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007646void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
7647 bool persistent)
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007648{
7649 struct mgmt_ev_new_csrk ev;
7650
7651 memset(&ev, 0, sizeof(ev));
7652
7653 /* Devices using resolvable or non-resolvable random addresses
Florian Grandelf72186d2015-05-26 03:31:09 +02007654 * without providing an identity resolving key don't require
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007655 * to store signature resolving keys. Their addresses will change
7656 * the next time around.
7657 *
7658 * Only when a remote device provides an identity address
7659 * make sure the signature resolving key is stored. So allow
7660 * static random and public addresses here.
7661 */
7662 if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM &&
7663 (csrk->bdaddr.b[5] & 0xc0) != 0xc0)
7664 ev.store_hint = 0x00;
7665 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007666 ev.store_hint = persistent;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007667
7668 bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
7669 ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
Johan Hedberg4cd39282015-02-27 10:11:13 +02007670 ev.key.type = csrk->type;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007671 memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
7672
7673 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
7674}
7675
Andre Guedesffb5a8272014-07-01 18:10:11 -03007676void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedbergf4869e22014-07-02 17:37:32 +03007677 u8 bdaddr_type, u8 store_hint, u16 min_interval,
7678 u16 max_interval, u16 latency, u16 timeout)
Andre Guedesffb5a8272014-07-01 18:10:11 -03007679{
7680 struct mgmt_ev_new_conn_param ev;
7681
Johan Hedbergc103aea2014-07-02 17:37:34 +03007682 if (!hci_is_identity_address(bdaddr, bdaddr_type))
7683 return;
7684
Andre Guedesffb5a8272014-07-01 18:10:11 -03007685 memset(&ev, 0, sizeof(ev));
7686 bacpy(&ev.addr.bdaddr, bdaddr);
7687 ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type);
Johan Hedbergf4869e22014-07-02 17:37:32 +03007688 ev.store_hint = store_hint;
Andre Guedesffb5a8272014-07-01 18:10:11 -03007689 ev.min_interval = cpu_to_le16(min_interval);
7690 ev.max_interval = cpu_to_le16(max_interval);
7691 ev.latency = cpu_to_le16(latency);
7692 ev.timeout = cpu_to_le16(timeout);
7693
7694 mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL);
7695}
7696
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00007697void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
7698 u32 flags, u8 *name, u8 name_len)
Johan Hedbergf7520542011-01-20 12:34:39 +02007699{
Johan Hedbergb644ba32012-01-17 21:48:47 +02007700 char buf[512];
7701 struct mgmt_ev_device_connected *ev = (void *) buf;
7702 u16 eir_len = 0;
Johan Hedbergf7520542011-01-20 12:34:39 +02007703
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00007704 bacpy(&ev->addr.bdaddr, &conn->dst);
7705 ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedbergf7520542011-01-20 12:34:39 +02007706
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02007707 ev->flags = __cpu_to_le32(flags);
Johan Hedberg08c79b62012-02-23 22:31:51 +02007708
Alfonso Acostafd45ada2014-10-07 08:44:11 +00007709 /* We must ensure that the EIR Data fields are ordered and
7710 * unique. Keep it simple for now and avoid the problem by not
7711 * adding any BR/EDR data to the LE adv.
7712 */
7713 if (conn->le_adv_data_len > 0) {
7714 memcpy(&ev->eir[eir_len],
7715 conn->le_adv_data, conn->le_adv_data_len);
7716 eir_len = conn->le_adv_data_len;
7717 } else {
7718 if (name_len > 0)
7719 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE,
7720 name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007721
Alfonso Acostaddbea5c2014-10-07 08:44:12 +00007722 if (memcmp(conn->dev_class, "\0\0\0", 3) != 0)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00007723 eir_len = eir_append_data(ev->eir, eir_len,
7724 EIR_CLASS_OF_DEV,
7725 conn->dev_class, 3);
7726 }
Johan Hedbergb644ba32012-01-17 21:48:47 +02007727
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02007728 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007729
Marcel Holtmannecd90ae2013-10-06 23:55:49 -07007730 mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf,
7731 sizeof(*ev) + eir_len, NULL);
Johan Hedbergf7520542011-01-20 12:34:39 +02007732}
7733
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007734static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg8962ee72011-01-20 12:40:27 +02007735{
Johan Hedberg8962ee72011-01-20 12:40:27 +02007736 struct sock **sk = data;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007737
Johan Hedbergf5818c22014-12-05 13:36:02 +02007738 cmd->cmd_complete(cmd, 0);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007739
7740 *sk = cmd->sk;
7741 sock_hold(*sk);
7742
Johan Hedberga664b5b2011-02-19 12:06:02 -03007743 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007744}
7745
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007746static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberga8a1d192011-11-10 15:54:38 +02007747{
Johan Hedbergb1078ad2012-02-09 17:21:16 +02007748 struct hci_dev *hdev = data;
Johan Hedberg124f6e32012-02-09 13:50:12 +02007749 struct mgmt_cp_unpair_device *cp = cmd->param;
Johan Hedberga8a1d192011-11-10 15:54:38 +02007750
Johan Hedbergb1078ad2012-02-09 17:21:16 +02007751 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
7752
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02007753 cmd->cmd_complete(cmd, 0);
Johan Hedberga8a1d192011-11-10 15:54:38 +02007754 mgmt_pending_remove(cmd);
7755}
7756
Johan Hedberg84c61d92014-08-01 11:13:30 +03007757bool mgmt_powering_down(struct hci_dev *hdev)
7758{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007759 struct mgmt_pending_cmd *cmd;
Johan Hedberg84c61d92014-08-01 11:13:30 +03007760 struct mgmt_mode *cp;
7761
Johan Hedberg333ae952015-03-17 13:48:47 +02007762 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg84c61d92014-08-01 11:13:30 +03007763 if (!cmd)
7764 return false;
7765
7766 cp = cmd->param;
7767 if (!cp->val)
7768 return true;
7769
7770 return false;
7771}
7772
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07007773void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02007774 u8 link_type, u8 addr_type, u8 reason,
7775 bool mgmt_connected)
Johan Hedbergf7520542011-01-20 12:34:39 +02007776{
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02007777 struct mgmt_ev_device_disconnected ev;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007778 struct sock *sk = NULL;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007779
Johan Hedberg84c61d92014-08-01 11:13:30 +03007780 /* The connection is still in hci_conn_hash so test for 1
7781 * instead of 0 to know if this is the last one.
7782 */
7783 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
7784 cancel_delayed_work(&hdev->power_off);
7785 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberg8b064a32014-02-24 14:52:22 +02007786 }
7787
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02007788 if (!mgmt_connected)
7789 return;
7790
Andre Guedes57eb7762013-10-30 19:01:41 -03007791 if (link_type != ACL_LINK && link_type != LE_LINK)
7792 return;
7793
Johan Hedberg744cf192011-11-08 20:40:14 +02007794 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
Johan Hedbergf7520542011-01-20 12:34:39 +02007795
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02007796 bacpy(&ev.addr.bdaddr, bdaddr);
7797 ev.addr.type = link_to_bdaddr(link_type, addr_type);
7798 ev.reason = reason;
Johan Hedbergf7520542011-01-20 12:34:39 +02007799
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07007800 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007801
7802 if (sk)
Szymon Jancd97dcb62012-03-16 16:02:56 +01007803 sock_put(sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007804
Johan Hedberg124f6e32012-02-09 13:50:12 +02007805 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007806 hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007807}
7808
Marcel Holtmann78929242013-10-06 23:55:47 -07007809void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
7810 u8 link_type, u8 addr_type, u8 status)
Johan Hedberg8962ee72011-01-20 12:40:27 +02007811{
Andre Guedes3655bba2013-10-30 19:01:40 -03007812 u8 bdaddr_type = link_to_bdaddr(link_type, addr_type);
7813 struct mgmt_cp_disconnect *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007814 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007815
Jefferson Delfes36a75f12012-09-18 13:36:54 -04007816 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
7817 hdev);
7818
Johan Hedberg333ae952015-03-17 13:48:47 +02007819 cmd = pending_find(MGMT_OP_DISCONNECT, hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007820 if (!cmd)
Marcel Holtmann78929242013-10-06 23:55:47 -07007821 return;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007822
Andre Guedes3655bba2013-10-30 19:01:40 -03007823 cp = cmd->param;
7824
7825 if (bacmp(bdaddr, &cp->addr.bdaddr))
7826 return;
7827
7828 if (cp->addr.type != bdaddr_type)
7829 return;
7830
Johan Hedbergf5818c22014-12-05 13:36:02 +02007831 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007832 mgmt_pending_remove(cmd);
Johan Hedbergf7520542011-01-20 12:34:39 +02007833}
Johan Hedberg17d5c042011-01-22 06:09:08 +02007834
Marcel Holtmann445608d2013-10-06 23:55:48 -07007835void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
7836 u8 addr_type, u8 status)
Johan Hedberg17d5c042011-01-22 06:09:08 +02007837{
7838 struct mgmt_ev_connect_failed ev;
Johan Hedbergc9910d02014-02-27 14:35:12 +02007839
Johan Hedberg84c61d92014-08-01 11:13:30 +03007840 /* The connection is still in hci_conn_hash so test for 1
7841 * instead of 0 to know if this is the last one.
7842 */
7843 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
7844 cancel_delayed_work(&hdev->power_off);
7845 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedbergc9910d02014-02-27 14:35:12 +02007846 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02007847
Johan Hedberg4c659c32011-11-07 23:13:39 +02007848 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007849 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergca69b792011-11-11 18:10:00 +02007850 ev.status = mgmt_status(status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02007851
Marcel Holtmann445608d2013-10-06 23:55:48 -07007852 mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg17d5c042011-01-22 06:09:08 +02007853}
Johan Hedberg980e1a52011-01-22 06:10:07 +02007854
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07007855void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007856{
7857 struct mgmt_ev_pin_code_request ev;
7858
Johan Hedbergd8457692012-02-17 14:24:57 +02007859 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03007860 ev.addr.type = BDADDR_BREDR;
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02007861 ev.secure = secure;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007862
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07007863 mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007864}
7865
Marcel Holtmanne669cf82013-10-15 14:26:21 -07007866void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
7867 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007868{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007869 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007870
Johan Hedberg333ae952015-03-17 13:48:47 +02007871 cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007872 if (!cmd)
Marcel Holtmanne669cf82013-10-15 14:26:21 -07007873 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007874
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007875 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007876 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007877}
7878
Marcel Holtmann3eb38522013-10-15 14:26:22 -07007879void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
7880 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007881{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007882 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007883
Johan Hedberg333ae952015-03-17 13:48:47 +02007884 cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007885 if (!cmd)
Marcel Holtmann3eb38522013-10-15 14:26:22 -07007886 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007887
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007888 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007889 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007890}
Johan Hedberga5c29682011-02-19 12:05:57 -03007891
Johan Hedberg744cf192011-11-08 20:40:14 +02007892int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg39adbff2014-03-20 08:18:14 +02007893 u8 link_type, u8 addr_type, u32 value,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007894 u8 confirm_hint)
Johan Hedberga5c29682011-02-19 12:05:57 -03007895{
7896 struct mgmt_ev_user_confirm_request ev;
7897
Johan Hedberg744cf192011-11-08 20:40:14 +02007898 BT_DBG("%s", hdev->name);
Johan Hedberga5c29682011-02-19 12:05:57 -03007899
Johan Hedberg272d90d2012-02-09 15:26:12 +02007900 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007901 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberg55bc1a32011-04-28 11:28:56 -07007902 ev.confirm_hint = confirm_hint;
Johan Hedberg39adbff2014-03-20 08:18:14 +02007903 ev.value = cpu_to_le32(value);
Johan Hedberga5c29682011-02-19 12:05:57 -03007904
Johan Hedberg744cf192011-11-08 20:40:14 +02007905 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007906 NULL);
Johan Hedberga5c29682011-02-19 12:05:57 -03007907}
7908
Johan Hedberg272d90d2012-02-09 15:26:12 +02007909int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03007910 u8 link_type, u8 addr_type)
Brian Gix604086b2011-11-23 08:28:33 -08007911{
7912 struct mgmt_ev_user_passkey_request ev;
7913
7914 BT_DBG("%s", hdev->name);
7915
Johan Hedberg272d90d2012-02-09 15:26:12 +02007916 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007917 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Brian Gix604086b2011-11-23 08:28:33 -08007918
7919 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007920 NULL);
Brian Gix604086b2011-11-23 08:28:33 -08007921}
7922
Brian Gix0df4c182011-11-16 13:53:13 -08007923static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03007924 u8 link_type, u8 addr_type, u8 status,
7925 u8 opcode)
Johan Hedberga5c29682011-02-19 12:05:57 -03007926{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007927 struct mgmt_pending_cmd *cmd;
Johan Hedberga5c29682011-02-19 12:05:57 -03007928
Johan Hedberg333ae952015-03-17 13:48:47 +02007929 cmd = pending_find(opcode, hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03007930 if (!cmd)
7931 return -ENOENT;
7932
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007933 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007934 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03007935
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007936 return 0;
Johan Hedberga5c29682011-02-19 12:05:57 -03007937}
7938
Johan Hedberg744cf192011-11-08 20:40:14 +02007939int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007940 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03007941{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007942 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007943 status, MGMT_OP_USER_CONFIRM_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03007944}
7945
Johan Hedberg272d90d2012-02-09 15:26:12 +02007946int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007947 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03007948{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007949 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03007950 status,
7951 MGMT_OP_USER_CONFIRM_NEG_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03007952}
Johan Hedberg2a611692011-02-19 12:06:00 -03007953
Brian Gix604086b2011-11-23 08:28:33 -08007954int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007955 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08007956{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007957 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007958 status, MGMT_OP_USER_PASSKEY_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08007959}
7960
Johan Hedberg272d90d2012-02-09 15:26:12 +02007961int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007962 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08007963{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007964 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03007965 status,
7966 MGMT_OP_USER_PASSKEY_NEG_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08007967}
7968
Johan Hedberg92a25252012-09-06 18:39:26 +03007969int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
7970 u8 link_type, u8 addr_type, u32 passkey,
7971 u8 entered)
7972{
7973 struct mgmt_ev_passkey_notify ev;
7974
7975 BT_DBG("%s", hdev->name);
7976
7977 bacpy(&ev.addr.bdaddr, bdaddr);
7978 ev.addr.type = link_to_bdaddr(link_type, addr_type);
7979 ev.passkey = __cpu_to_le32(passkey);
7980 ev.entered = entered;
7981
7982 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
7983}
7984
Johan Hedberge1e930f2014-09-08 17:09:49 -07007985void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status)
Johan Hedberg2a611692011-02-19 12:06:00 -03007986{
7987 struct mgmt_ev_auth_failed ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007988 struct mgmt_pending_cmd *cmd;
Johan Hedberge1e930f2014-09-08 17:09:49 -07007989 u8 status = mgmt_status(hci_status);
Johan Hedberg2a611692011-02-19 12:06:00 -03007990
Johan Hedberge1e930f2014-09-08 17:09:49 -07007991 bacpy(&ev.addr.bdaddr, &conn->dst);
7992 ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
7993 ev.status = status;
Johan Hedberg2a611692011-02-19 12:06:00 -03007994
Johan Hedberge1e930f2014-09-08 17:09:49 -07007995 cmd = find_pairing(conn);
7996
7997 mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev),
7998 cmd ? cmd->sk : NULL);
7999
Johan Hedberga511b352014-12-11 21:45:45 +02008000 if (cmd) {
8001 cmd->cmd_complete(cmd, status);
8002 mgmt_pending_remove(cmd);
8003 }
Johan Hedberg2a611692011-02-19 12:06:00 -03008004}
Johan Hedbergb312b1612011-03-16 14:29:37 +02008005
Marcel Holtmann464996a2013-10-15 14:26:24 -07008006void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02008007{
8008 struct cmd_lookup match = { NULL, hdev };
Marcel Holtmann464996a2013-10-15 14:26:24 -07008009 bool changed;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02008010
8011 if (status) {
8012 u8 mgmt_err = mgmt_status(status);
8013 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03008014 cmd_status_rsp, &mgmt_err);
Marcel Holtmann464996a2013-10-15 14:26:24 -07008015 return;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02008016 }
8017
Marcel Holtmann464996a2013-10-15 14:26:24 -07008018 if (test_bit(HCI_AUTH, &hdev->flags))
Marcel Holtmann238be782015-03-13 02:11:06 -07008019 changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY);
Marcel Holtmann464996a2013-10-15 14:26:24 -07008020 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07008021 changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02008022
Johan Hedberg33ef95e2012-02-16 23:56:27 +02008023 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03008024 &match);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02008025
Johan Hedberg47990ea2012-02-22 11:58:37 +02008026 if (changed)
Marcel Holtmann464996a2013-10-15 14:26:24 -07008027 new_settings(hdev, match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02008028
8029 if (match.sk)
8030 sock_put(match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02008031}
8032
Johan Hedberg890ea892013-03-15 17:06:52 -05008033static void clear_eir(struct hci_request *req)
Johan Hedbergcacaf522012-02-21 00:52:42 +02008034{
Johan Hedberg890ea892013-03-15 17:06:52 -05008035 struct hci_dev *hdev = req->hdev;
Johan Hedbergcacaf522012-02-21 00:52:42 +02008036 struct hci_cp_write_eir cp;
8037
Johan Hedberg976eb202012-10-24 21:12:01 +03008038 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05008039 return;
Johan Hedbergcacaf522012-02-21 00:52:42 +02008040
Johan Hedbergc80da272012-02-22 15:38:48 +02008041 memset(hdev->eir, 0, sizeof(hdev->eir));
8042
Johan Hedbergcacaf522012-02-21 00:52:42 +02008043 memset(&cp, 0, sizeof(cp));
8044
Johan Hedberg890ea892013-03-15 17:06:52 -05008045 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedbergcacaf522012-02-21 00:52:42 +02008046}
8047
Marcel Holtmann3e248562013-10-15 14:26:25 -07008048void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02008049{
8050 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg890ea892013-03-15 17:06:52 -05008051 struct hci_request req;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02008052 bool changed = false;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02008053
8054 if (status) {
8055 u8 mgmt_err = mgmt_status(status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02008056
Marcel Holtmanna69d8922015-03-13 02:11:05 -07008057 if (enable && hci_dev_test_and_clear_flag(hdev,
8058 HCI_SSP_ENABLED)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07008059 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmann3e248562013-10-15 14:26:25 -07008060 new_settings(hdev, NULL);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07008061 }
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02008062
Gustavo F. Padovan04124682012-03-08 01:25:00 -03008063 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
8064 &mgmt_err);
Marcel Holtmann3e248562013-10-15 14:26:25 -07008065 return;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02008066 }
8067
8068 if (enable) {
Marcel Holtmann238be782015-03-13 02:11:06 -07008069 changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02008070 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07008071 changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07008072 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07008073 changed = hci_dev_test_and_clear_flag(hdev,
8074 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07008075 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07008076 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02008077 }
8078
8079 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
8080
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02008081 if (changed)
Marcel Holtmann3e248562013-10-15 14:26:25 -07008082 new_settings(hdev, match.sk);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02008083
Johan Hedberg5fc6ebb2012-02-22 15:10:59 +02008084 if (match.sk)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02008085 sock_put(match.sk);
8086
Johan Hedberg890ea892013-03-15 17:06:52 -05008087 hci_req_init(&req, hdev);
8088
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07008089 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
8090 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03008091 hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE,
8092 sizeof(enable), &enable);
Johan Hedberg890ea892013-03-15 17:06:52 -05008093 update_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03008094 } else {
Johan Hedberg890ea892013-03-15 17:06:52 -05008095 clear_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03008096 }
Johan Hedberg890ea892013-03-15 17:06:52 -05008097
8098 hci_req_run(&req, NULL);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02008099}
8100
Johan Hedberg3b0602c2015-03-06 21:08:55 +02008101static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg90e70452012-02-23 23:09:40 +02008102{
8103 struct cmd_lookup *match = data;
8104
Johan Hedberg90e70452012-02-23 23:09:40 +02008105 if (match->sk == NULL) {
8106 match->sk = cmd->sk;
8107 sock_hold(match->sk);
8108 }
Johan Hedberg90e70452012-02-23 23:09:40 +02008109}
8110
Marcel Holtmann4e1b0242013-10-15 14:26:26 -07008111void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
8112 u8 status)
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01008113{
Johan Hedberg90e70452012-02-23 23:09:40 +02008114 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01008115
Johan Hedberg92da6092013-03-15 17:06:55 -05008116 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
8117 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
8118 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
Johan Hedberg90e70452012-02-23 23:09:40 +02008119
8120 if (!status)
Marcel Holtmannf6b77122015-03-14 19:28:05 -07008121 mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
8122 dev_class, 3, NULL);
Johan Hedberg90e70452012-02-23 23:09:40 +02008123
8124 if (match.sk)
8125 sock_put(match.sk);
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01008126}
8127
Marcel Holtmann7667da32013-10-15 14:26:27 -07008128void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
Johan Hedbergb312b1612011-03-16 14:29:37 +02008129{
Johan Hedbergb312b1612011-03-16 14:29:37 +02008130 struct mgmt_cp_set_local_name ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02008131 struct mgmt_pending_cmd *cmd;
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02008132
Johan Hedberg13928972013-03-15 17:07:00 -05008133 if (status)
Marcel Holtmann7667da32013-10-15 14:26:27 -07008134 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02008135
8136 memset(&ev, 0, sizeof(ev));
8137 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02008138 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
Johan Hedbergb312b1612011-03-16 14:29:37 +02008139
Johan Hedberg333ae952015-03-17 13:48:47 +02008140 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05008141 if (!cmd) {
8142 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
Johan Hedbergb312b1612011-03-16 14:29:37 +02008143
Johan Hedberg13928972013-03-15 17:07:00 -05008144 /* If this is a HCI command related to powering on the
8145 * HCI dev don't send any mgmt signals.
8146 */
Johan Hedberg333ae952015-03-17 13:48:47 +02008147 if (pending_find(MGMT_OP_SET_POWERED, hdev))
Marcel Holtmann7667da32013-10-15 14:26:27 -07008148 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02008149 }
8150
Marcel Holtmannf6b77122015-03-14 19:28:05 -07008151 mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
8152 cmd ? cmd->sk : NULL);
Johan Hedbergb312b1612011-03-16 14:29:37 +02008153}
Szymon Jancc35938b2011-03-22 13:12:21 +01008154
Jakub Pawlowski799ce932014-12-05 10:55:58 +01008155static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16])
8156{
8157 int i;
8158
8159 for (i = 0; i < uuid_count; i++) {
8160 if (!memcmp(uuid, uuids[i], 16))
8161 return true;
8162 }
8163
8164 return false;
8165}
8166
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01008167static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16])
8168{
Jakub Pawlowski799ce932014-12-05 10:55:58 +01008169 u16 parsed = 0;
8170
8171 while (parsed < eir_len) {
8172 u8 field_len = eir[0];
8173 u8 uuid[16];
8174 int i;
8175
8176 if (field_len == 0)
8177 break;
8178
8179 if (eir_len - parsed < field_len + 1)
8180 break;
8181
8182 switch (eir[1]) {
8183 case EIR_UUID16_ALL:
8184 case EIR_UUID16_SOME:
8185 for (i = 0; i + 3 <= field_len; i += 2) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02008186 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01008187 uuid[13] = eir[i + 3];
8188 uuid[12] = eir[i + 2];
8189 if (has_uuid(uuid, uuid_count, uuids))
8190 return true;
8191 }
8192 break;
8193 case EIR_UUID32_ALL:
8194 case EIR_UUID32_SOME:
8195 for (i = 0; i + 5 <= field_len; i += 4) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02008196 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01008197 uuid[15] = eir[i + 5];
8198 uuid[14] = eir[i + 4];
8199 uuid[13] = eir[i + 3];
8200 uuid[12] = eir[i + 2];
8201 if (has_uuid(uuid, uuid_count, uuids))
8202 return true;
8203 }
8204 break;
8205 case EIR_UUID128_ALL:
8206 case EIR_UUID128_SOME:
8207 for (i = 0; i + 17 <= field_len; i += 16) {
8208 memcpy(uuid, eir + i + 2, 16);
8209 if (has_uuid(uuid, uuid_count, uuids))
8210 return true;
8211 }
8212 break;
8213 }
8214
8215 parsed += field_len + 1;
8216 eir += field_len + 1;
8217 }
8218
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01008219 return false;
8220}
8221
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08008222static void restart_le_scan(struct hci_dev *hdev)
8223{
8224 /* If controller is not scanning we are done. */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07008225 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08008226 return;
8227
8228 if (time_after(jiffies + DISCOV_LE_RESTART_DELAY,
8229 hdev->discovery.scan_start +
8230 hdev->discovery.scan_duration))
8231 return;
8232
8233 queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart,
8234 DISCOV_LE_RESTART_DELAY);
8235}
8236
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008237static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir,
8238 u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
8239{
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008240 /* If a RSSI threshold has been specified, and
8241 * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with
8242 * a RSSI smaller than the RSSI threshold will be dropped. If the quirk
8243 * is set, let it through for further processing, as we might need to
8244 * restart the scan.
8245 *
8246 * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry,
8247 * the results are also dropped.
8248 */
8249 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
8250 (rssi == HCI_RSSI_INVALID ||
8251 (rssi < hdev->discovery.rssi &&
8252 !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))
8253 return false;
8254
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008255 if (hdev->discovery.uuid_count != 0) {
8256 /* If a list of UUIDs is provided in filter, results with no
8257 * matching UUID should be dropped.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008258 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008259 if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count,
8260 hdev->discovery.uuids) &&
8261 !eir_has_uuids(scan_rsp, scan_rsp_len,
8262 hdev->discovery.uuid_count,
8263 hdev->discovery.uuids))
8264 return false;
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008265 }
8266
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008267 /* If duplicate filtering does not report RSSI changes, then restart
8268 * scanning to ensure updated result with updated RSSI values.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008269 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008270 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) {
8271 restart_le_scan(hdev);
8272
8273 /* Validate RSSI value against the RSSI threshold once more. */
8274 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
8275 rssi < hdev->discovery.rssi)
8276 return false;
8277 }
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008278
8279 return true;
8280}
8281
Marcel Holtmann901801b2013-10-06 23:55:51 -07008282void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
Marcel Holtmannaf589252014-07-01 14:11:20 +02008283 u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
8284 u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
Johan Hedberge17acd42011-03-30 23:57:16 +03008285{
Johan Hedberge319d2e2012-01-15 19:51:59 +02008286 char buf[512];
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008287 struct mgmt_ev_device_found *ev = (void *)buf;
Johan Hedberg1dc06092012-01-15 21:01:23 +02008288 size_t ev_size;
Johan Hedberge17acd42011-03-30 23:57:16 +03008289
Johan Hedberg75ce2082014-07-02 22:42:01 +03008290 /* Don't send events for a non-kernel initiated discovery. With
8291 * LE one exception is if we have pend_le_reports > 0 in which
8292 * case we're doing passive scanning and want these events.
8293 */
8294 if (!hci_discovery_active(hdev)) {
8295 if (link_type == ACL_LINK)
8296 return;
Johan Hedberg66f84552014-07-04 12:37:18 +03008297 if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports))
Johan Hedberg75ce2082014-07-02 22:42:01 +03008298 return;
8299 }
Andre Guedes12602d02013-04-30 15:29:40 -03008300
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08008301 if (hdev->discovery.result_filtering) {
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008302 /* We are using service discovery */
8303 if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp,
8304 scan_rsp_len))
8305 return;
8306 }
Marcel Holtmannbda157a2014-12-05 10:55:56 +01008307
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02008308 /* Make sure that the buffer is big enough. The 5 extra bytes
8309 * are for the potential CoD field.
8310 */
8311 if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf))
Marcel Holtmann901801b2013-10-06 23:55:51 -07008312 return;
Andre Guedes7d262f82012-01-10 18:20:49 -03008313
Johan Hedberg1dc06092012-01-15 21:01:23 +02008314 memset(buf, 0, sizeof(buf));
8315
Marcel Holtmannda25cf62014-12-05 13:03:35 +01008316 /* In case of device discovery with BR/EDR devices (pre 1.2), the
8317 * RSSI value was reported as 0 when not available. This behavior
8318 * is kept when using device discovery. This is required for full
8319 * backwards compatibility with the API.
8320 *
8321 * However when using service discovery, the value 127 will be
8322 * returned when the RSSI is not available.
8323 */
Szymon Janc91200e92015-01-22 16:57:05 +01008324 if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi &&
8325 link_type == ACL_LINK)
Marcel Holtmannefb25132014-12-05 13:03:34 +01008326 rssi = 0;
8327
Johan Hedberg841c5642014-07-07 12:45:54 +03008328 bacpy(&ev->addr.bdaddr, bdaddr);
8329 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberge319d2e2012-01-15 19:51:59 +02008330 ev->rssi = rssi;
Marcel Holtmannaf589252014-07-01 14:11:20 +02008331 ev->flags = cpu_to_le32(flags);
Johan Hedberge17acd42011-03-30 23:57:16 +03008332
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008333 if (eir_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01008334 /* Copy EIR or advertising data into event */
Johan Hedberge319d2e2012-01-15 19:51:59 +02008335 memcpy(ev->eir, eir, eir_len);
Johan Hedberge17acd42011-03-30 23:57:16 +03008336
Johan Hedberg1dc06092012-01-15 21:01:23 +02008337 if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV))
8338 eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03008339 dev_class, 3);
Johan Hedberg1dc06092012-01-15 21:01:23 +02008340
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008341 if (scan_rsp_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01008342 /* Append scan response data to event */
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02008343 memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len);
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08008344
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02008345 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
8346 ev_size = sizeof(*ev) + eir_len + scan_rsp_len;
Andre Guedesf8523592011-09-09 18:56:26 -03008347
Marcel Holtmann901801b2013-10-06 23:55:51 -07008348 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL);
Johan Hedberge17acd42011-03-30 23:57:16 +03008349}
Johan Hedberga88a9652011-03-30 13:18:12 +03008350
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07008351void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
8352 u8 addr_type, s8 rssi, u8 *name, u8 name_len)
Johan Hedberga88a9652011-03-30 13:18:12 +03008353{
Johan Hedbergb644ba32012-01-17 21:48:47 +02008354 struct mgmt_ev_device_found *ev;
8355 char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2];
8356 u16 eir_len;
Johan Hedberga88a9652011-03-30 13:18:12 +03008357
Johan Hedbergb644ba32012-01-17 21:48:47 +02008358 ev = (struct mgmt_ev_device_found *) buf;
Johan Hedberga88a9652011-03-30 13:18:12 +03008359
Johan Hedbergb644ba32012-01-17 21:48:47 +02008360 memset(buf, 0, sizeof(buf));
Johan Hedberga88a9652011-03-30 13:18:12 +03008361
Johan Hedbergb644ba32012-01-17 21:48:47 +02008362 bacpy(&ev->addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03008363 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergb644ba32012-01-17 21:48:47 +02008364 ev->rssi = rssi;
8365
8366 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03008367 name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02008368
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02008369 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02008370
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07008371 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL);
Johan Hedberga88a9652011-03-30 13:18:12 +03008372}
Johan Hedberg314b2382011-04-27 10:29:57 -04008373
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07008374void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
Johan Hedberg314b2382011-04-27 10:29:57 -04008375{
Johan Hedbergf963e8e2012-02-20 23:30:44 +02008376 struct mgmt_ev_discovering ev;
Johan Hedberg164a6e72011-11-01 17:06:44 +02008377
Andre Guedes343fb142011-11-22 17:14:19 -03008378 BT_DBG("%s discovering %u", hdev->name, discovering);
8379
Johan Hedbergf963e8e2012-02-20 23:30:44 +02008380 memset(&ev, 0, sizeof(ev));
8381 ev.type = hdev->discovery.type;
8382 ev.discovering = discovering;
8383
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07008384 mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg314b2382011-04-27 10:29:57 -04008385}
Antti Julku5e762442011-08-25 16:48:02 +03008386
Marcel Holtmann1904a852015-01-11 13:50:44 -08008387static void adv_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Marcel Holtmann5976e602013-10-06 04:08:14 -07008388{
8389 BT_DBG("%s status %u", hdev->name, status);
Marcel Holtmann5976e602013-10-06 04:08:14 -07008390}
8391
8392void mgmt_reenable_advertising(struct hci_dev *hdev)
8393{
8394 struct hci_request req;
8395
Arman Uguray24b4f382015-03-23 15:57:12 -07008396 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
8397 !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
Marcel Holtmann5976e602013-10-06 04:08:14 -07008398 return;
8399
8400 hci_req_init(&req, hdev);
8401 enable_advertising(&req);
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03008402 hci_req_run(&req, adv_enable_complete);
Marcel Holtmann5976e602013-10-06 04:08:14 -07008403}
Johan Hedberg6d785aa32015-03-06 21:08:51 +02008404
8405static struct hci_mgmt_chan chan = {
8406 .channel = HCI_CHANNEL_CONTROL,
8407 .handler_count = ARRAY_SIZE(mgmt_handlers),
8408 .handlers = mgmt_handlers,
Johan Hedberg88b94ce2015-03-17 13:48:49 +02008409 .hdev_init = mgmt_init_hdev,
Johan Hedberg6d785aa32015-03-06 21:08:51 +02008410};
8411
8412int mgmt_init(void)
8413{
8414 return hci_mgmt_chan_register(&chan);
8415}
8416
8417void mgmt_exit(void)
8418{
8419 hci_mgmt_chan_unregister(&chan);
8420}