blob: 9e4b931588cf798551b41158e20699ab2d9aafd1 [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
Johan Hedberg9c176ce2016-03-09 17:52:24 +020041#define MGMT_REVISION 12
Johan Hedberg02d98122010-12-13 21:07:04 +020042
Johan Hedberge70bb2e2012-02-13 16:59:33 +020043static const u16 mgmt_commands[] = {
44 MGMT_OP_READ_INDEX_LIST,
45 MGMT_OP_READ_INFO,
46 MGMT_OP_SET_POWERED,
47 MGMT_OP_SET_DISCOVERABLE,
48 MGMT_OP_SET_CONNECTABLE,
49 MGMT_OP_SET_FAST_CONNECTABLE,
Johan Hedbergb2939472014-07-30 09:22:23 +030050 MGMT_OP_SET_BONDABLE,
Johan Hedberge70bb2e2012-02-13 16:59:33 +020051 MGMT_OP_SET_LINK_SECURITY,
52 MGMT_OP_SET_SSP,
53 MGMT_OP_SET_HS,
54 MGMT_OP_SET_LE,
55 MGMT_OP_SET_DEV_CLASS,
56 MGMT_OP_SET_LOCAL_NAME,
57 MGMT_OP_ADD_UUID,
58 MGMT_OP_REMOVE_UUID,
59 MGMT_OP_LOAD_LINK_KEYS,
60 MGMT_OP_LOAD_LONG_TERM_KEYS,
61 MGMT_OP_DISCONNECT,
62 MGMT_OP_GET_CONNECTIONS,
63 MGMT_OP_PIN_CODE_REPLY,
64 MGMT_OP_PIN_CODE_NEG_REPLY,
65 MGMT_OP_SET_IO_CAPABILITY,
66 MGMT_OP_PAIR_DEVICE,
67 MGMT_OP_CANCEL_PAIR_DEVICE,
68 MGMT_OP_UNPAIR_DEVICE,
69 MGMT_OP_USER_CONFIRM_REPLY,
70 MGMT_OP_USER_CONFIRM_NEG_REPLY,
71 MGMT_OP_USER_PASSKEY_REPLY,
72 MGMT_OP_USER_PASSKEY_NEG_REPLY,
73 MGMT_OP_READ_LOCAL_OOB_DATA,
74 MGMT_OP_ADD_REMOTE_OOB_DATA,
75 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
76 MGMT_OP_START_DISCOVERY,
77 MGMT_OP_STOP_DISCOVERY,
78 MGMT_OP_CONFIRM_NAME,
79 MGMT_OP_BLOCK_DEVICE,
80 MGMT_OP_UNBLOCK_DEVICE,
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -070081 MGMT_OP_SET_DEVICE_ID,
Johan Hedberg4375f102013-09-25 13:26:10 +030082 MGMT_OP_SET_ADVERTISING,
Johan Hedberg0663ca22013-10-02 13:43:14 +030083 MGMT_OP_SET_BREDR,
Marcel Holtmannd13eafc2013-10-02 04:41:30 -070084 MGMT_OP_SET_STATIC_ADDRESS,
Marcel Holtmann7f72134e2013-10-11 14:44:58 -070085 MGMT_OP_SET_SCAN_PARAMS,
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -080086 MGMT_OP_SET_SECURE_CONN,
Marcel Holtmann4e39ac82014-01-31 11:55:22 -080087 MGMT_OP_SET_DEBUG_KEYS,
Johan Hedberg62b04cd2014-02-23 19:42:27 +020088 MGMT_OP_SET_PRIVACY,
Johan Hedberg41edf162014-02-18 10:19:35 +020089 MGMT_OP_LOAD_IRKS,
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +020090 MGMT_OP_GET_CONN_INFO,
Johan Hedberg95868422014-06-28 17:54:07 +030091 MGMT_OP_GET_CLOCK_INFO,
Marcel Holtmann2faade52014-06-29 19:44:03 +020092 MGMT_OP_ADD_DEVICE,
93 MGMT_OP_REMOVE_DEVICE,
Johan Hedberga26f3dc2014-07-02 17:37:29 +030094 MGMT_OP_LOAD_CONN_PARAM,
Marcel Holtmann73d1df22014-07-02 22:10:52 +020095 MGMT_OP_READ_UNCONF_INDEX_LIST,
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +020096 MGMT_OP_READ_CONFIG_INFO,
Marcel Holtmanndbece372014-07-04 18:11:55 +020097 MGMT_OP_SET_EXTERNAL_CONFIG,
Marcel Holtmann9713c172014-07-06 12:11:15 +020098 MGMT_OP_SET_PUBLIC_ADDRESS,
Jakub Pawlowski66ea9422014-12-05 10:55:59 +010099 MGMT_OP_START_SERVICE_DISCOVERY,
Marcel Holtmann4f0f1552015-03-14 22:43:19 -0700100 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
Marcel Holtmann96f14742015-03-14 19:27:57 -0700101 MGMT_OP_READ_EXT_INDEX_LIST,
Marcel Holtmannd3d53052015-03-14 20:53:25 -0700102 MGMT_OP_READ_ADV_FEATURES,
Arman Uguray24b4f382015-03-23 15:57:12 -0700103 MGMT_OP_ADD_ADVERTISING,
Arman Ugurayda9293352015-03-23 15:57:13 -0700104 MGMT_OP_REMOVE_ADVERTISING,
Marcel Holtmann40b25fe2015-11-19 16:16:43 +0100105 MGMT_OP_GET_ADV_SIZE_INFO,
Johan Hedberg78b781c2016-01-05 13:19:32 +0200106 MGMT_OP_START_LIMITED_DISCOVERY,
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200107};
108
109static const u16 mgmt_events[] = {
110 MGMT_EV_CONTROLLER_ERROR,
111 MGMT_EV_INDEX_ADDED,
112 MGMT_EV_INDEX_REMOVED,
113 MGMT_EV_NEW_SETTINGS,
114 MGMT_EV_CLASS_OF_DEV_CHANGED,
115 MGMT_EV_LOCAL_NAME_CHANGED,
116 MGMT_EV_NEW_LINK_KEY,
117 MGMT_EV_NEW_LONG_TERM_KEY,
118 MGMT_EV_DEVICE_CONNECTED,
119 MGMT_EV_DEVICE_DISCONNECTED,
120 MGMT_EV_CONNECT_FAILED,
121 MGMT_EV_PIN_CODE_REQUEST,
122 MGMT_EV_USER_CONFIRM_REQUEST,
123 MGMT_EV_USER_PASSKEY_REQUEST,
124 MGMT_EV_AUTH_FAILED,
125 MGMT_EV_DEVICE_FOUND,
126 MGMT_EV_DISCOVERING,
127 MGMT_EV_DEVICE_BLOCKED,
128 MGMT_EV_DEVICE_UNBLOCKED,
129 MGMT_EV_DEVICE_UNPAIRED,
Johan Hedberg92a25252012-09-06 18:39:26 +0300130 MGMT_EV_PASSKEY_NOTIFY,
Marcel Holtmann1b60ef22014-02-21 21:35:30 -0800131 MGMT_EV_NEW_IRK,
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700132 MGMT_EV_NEW_CSRK,
Marcel Holtmann8afef092014-06-29 22:28:34 +0200133 MGMT_EV_DEVICE_ADDED,
134 MGMT_EV_DEVICE_REMOVED,
Andre Guedesffb5a8272014-07-01 18:10:11 -0300135 MGMT_EV_NEW_CONN_PARAM,
Marcel Holtmann0602a8a2014-07-02 21:30:54 +0200136 MGMT_EV_UNCONF_INDEX_ADDED,
Marcel Holtmannedd38962014-07-02 21:30:55 +0200137 MGMT_EV_UNCONF_INDEX_REMOVED,
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200138 MGMT_EV_NEW_CONFIG_OPTIONS,
Marcel Holtmannced85542015-03-14 19:27:56 -0700139 MGMT_EV_EXT_INDEX_ADDED,
140 MGMT_EV_EXT_INDEX_REMOVED,
Marcel Holtmann72000df2015-03-16 16:11:21 -0700141 MGMT_EV_LOCAL_OOB_DATA_UPDATED,
Arman Uguray24b4f382015-03-23 15:57:12 -0700142 MGMT_EV_ADVERTISING_ADDED,
143 MGMT_EV_ADVERTISING_REMOVED,
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200144};
145
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700146static const u16 mgmt_untrusted_commands[] = {
147 MGMT_OP_READ_INDEX_LIST,
148 MGMT_OP_READ_INFO,
149 MGMT_OP_READ_UNCONF_INDEX_LIST,
150 MGMT_OP_READ_CONFIG_INFO,
151 MGMT_OP_READ_EXT_INDEX_LIST,
152};
153
154static const u16 mgmt_untrusted_events[] = {
155 MGMT_EV_INDEX_ADDED,
156 MGMT_EV_INDEX_REMOVED,
157 MGMT_EV_NEW_SETTINGS,
158 MGMT_EV_CLASS_OF_DEV_CHANGED,
159 MGMT_EV_LOCAL_NAME_CHANGED,
160 MGMT_EV_UNCONF_INDEX_ADDED,
161 MGMT_EV_UNCONF_INDEX_REMOVED,
162 MGMT_EV_NEW_CONFIG_OPTIONS,
163 MGMT_EV_EXT_INDEX_ADDED,
164 MGMT_EV_EXT_INDEX_REMOVED,
165};
166
Marcel Holtmann17b02e62012-03-01 14:32:37 -0800167#define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)
Johan Hedberg7d785252011-12-15 00:47:39 +0200168
Johan Hedbergd25b78e2015-01-27 12:55:52 +0200169#define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
170 "\x00\x00\x00\x00\x00\x00\x00\x00"
171
Johan Hedbergca69b792011-11-11 18:10:00 +0200172/* HCI to MGMT error code conversion table */
173static u8 mgmt_status_table[] = {
174 MGMT_STATUS_SUCCESS,
175 MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */
176 MGMT_STATUS_NOT_CONNECTED, /* No Connection */
177 MGMT_STATUS_FAILED, /* Hardware Failure */
178 MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */
179 MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */
Johan Hedbergeadd6632014-01-13 17:15:53 +0200180 MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */
Johan Hedbergca69b792011-11-11 18:10:00 +0200181 MGMT_STATUS_NO_RESOURCES, /* Memory Full */
182 MGMT_STATUS_TIMEOUT, /* Connection Timeout */
183 MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */
184 MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */
185 MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */
186 MGMT_STATUS_BUSY, /* Command Disallowed */
187 MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */
188 MGMT_STATUS_REJECTED, /* Rejected Security */
189 MGMT_STATUS_REJECTED, /* Rejected Personal */
190 MGMT_STATUS_TIMEOUT, /* Host Timeout */
191 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */
192 MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */
193 MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */
194 MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */
195 MGMT_STATUS_DISCONNECTED, /* OE Power Off */
196 MGMT_STATUS_DISCONNECTED, /* Connection Terminated */
197 MGMT_STATUS_BUSY, /* Repeated Attempts */
198 MGMT_STATUS_REJECTED, /* Pairing Not Allowed */
199 MGMT_STATUS_FAILED, /* Unknown LMP PDU */
200 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */
201 MGMT_STATUS_REJECTED, /* SCO Offset Rejected */
202 MGMT_STATUS_REJECTED, /* SCO Interval Rejected */
203 MGMT_STATUS_REJECTED, /* Air Mode Rejected */
204 MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */
205 MGMT_STATUS_FAILED, /* Unspecified Error */
206 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */
207 MGMT_STATUS_FAILED, /* Role Change Not Allowed */
208 MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */
209 MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */
210 MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */
211 MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */
212 MGMT_STATUS_FAILED, /* Unit Link Key Used */
213 MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */
214 MGMT_STATUS_TIMEOUT, /* Instant Passed */
215 MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */
216 MGMT_STATUS_FAILED, /* Transaction Collision */
217 MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */
218 MGMT_STATUS_REJECTED, /* QoS Rejected */
219 MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */
220 MGMT_STATUS_REJECTED, /* Insufficient Security */
221 MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */
222 MGMT_STATUS_BUSY, /* Role Switch Pending */
223 MGMT_STATUS_FAILED, /* Slot Violation */
224 MGMT_STATUS_FAILED, /* Role Switch Failed */
225 MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */
226 MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */
227 MGMT_STATUS_BUSY, /* Host Busy Pairing */
228 MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */
229 MGMT_STATUS_BUSY, /* Controller Busy */
230 MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */
231 MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */
232 MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */
233 MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */
234 MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */
235};
236
237static u8 mgmt_status(u8 hci_status)
238{
239 if (hci_status < ARRAY_SIZE(mgmt_status_table))
240 return mgmt_status_table[hci_status];
241
242 return MGMT_STATUS_FAILED;
243}
244
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700245static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data,
246 u16 len, int flag)
Marcel Holtmannf9207332015-03-14 19:27:55 -0700247{
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700248 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
249 flag, NULL);
Marcel Holtmannf9207332015-03-14 19:27:55 -0700250}
251
Marcel Holtmann72000df2015-03-16 16:11:21 -0700252static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data,
253 u16 len, int flag, struct sock *skip_sk)
254{
255 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
256 flag, skip_sk);
257}
258
Marcel Holtmannf6b77122015-03-14 19:28:05 -0700259static int mgmt_generic_event(u16 event, struct hci_dev *hdev, void *data,
260 u16 len, struct sock *skip_sk)
261{
262 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
263 HCI_MGMT_GENERIC_EVENTS, skip_sk);
264}
265
Johan Hedberg7a00ff42015-03-06 21:08:56 +0200266static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len,
267 struct sock *skip_sk)
268{
269 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700270 HCI_SOCK_TRUSTED, skip_sk);
Johan Hedberg7a00ff42015-03-06 21:08:56 +0200271}
272
Johan Hedberg85813a72015-10-21 18:02:59 +0300273static u8 le_addr_type(u8 mgmt_addr_type)
274{
275 if (mgmt_addr_type == BDADDR_LE_PUBLIC)
276 return ADDR_LE_DEV_PUBLIC;
277 else
278 return ADDR_LE_DEV_RANDOM;
279}
280
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300281static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
282 u16 data_len)
Johan Hedberga38528f2011-01-22 06:46:43 +0200283{
284 struct mgmt_rp_read_version rp;
285
286 BT_DBG("sock %p", sk);
287
288 rp.version = MGMT_VERSION;
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700289 rp.revision = cpu_to_le16(MGMT_REVISION);
Johan Hedberga38528f2011-01-22 06:46:43 +0200290
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200291 return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0,
292 &rp, sizeof(rp));
Johan Hedberga38528f2011-01-22 06:46:43 +0200293}
294
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300295static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
296 u16 data_len)
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200297{
298 struct mgmt_rp_read_commands *rp;
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700299 u16 num_commands, num_events;
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200300 size_t rp_size;
301 int i, err;
302
303 BT_DBG("sock %p", sk);
304
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700305 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
306 num_commands = ARRAY_SIZE(mgmt_commands);
307 num_events = ARRAY_SIZE(mgmt_events);
308 } else {
309 num_commands = ARRAY_SIZE(mgmt_untrusted_commands);
310 num_events = ARRAY_SIZE(mgmt_untrusted_events);
311 }
312
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200313 rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16));
314
315 rp = kmalloc(rp_size, GFP_KERNEL);
316 if (!rp)
317 return -ENOMEM;
318
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700319 rp->num_commands = cpu_to_le16(num_commands);
320 rp->num_events = cpu_to_le16(num_events);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200321
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700322 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
323 __le16 *opcode = rp->opcodes;
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200324
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700325 for (i = 0; i < num_commands; i++, opcode++)
326 put_unaligned_le16(mgmt_commands[i], opcode);
327
328 for (i = 0; i < num_events; i++, opcode++)
329 put_unaligned_le16(mgmt_events[i], opcode);
330 } else {
331 __le16 *opcode = rp->opcodes;
332
333 for (i = 0; i < num_commands; i++, opcode++)
334 put_unaligned_le16(mgmt_untrusted_commands[i], opcode);
335
336 for (i = 0; i < num_events; i++, opcode++)
337 put_unaligned_le16(mgmt_untrusted_events[i], opcode);
338 }
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200339
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200340 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0,
341 rp, rp_size);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200342 kfree(rp);
343
344 return err;
345}
346
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300347static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
348 u16 data_len)
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200349{
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200350 struct mgmt_rp_read_index_list *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200351 struct hci_dev *d;
Johan Hedberga38528f2011-01-22 06:46:43 +0200352 size_t rp_len;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200353 u16 count;
Johan Hedberg476e44c2012-10-19 20:10:46 +0300354 int err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200355
356 BT_DBG("sock %p", sk);
357
358 read_lock(&hci_dev_list_lock);
359
360 count = 0;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +0300361 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200362 if (d->dev_type == HCI_BREDR &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700363 !hci_dev_test_flag(d, HCI_UNCONFIGURED))
Marcel Holtmann1514b892013-10-06 08:25:01 -0700364 count++;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200365 }
366
Johan Hedberga38528f2011-01-22 06:46:43 +0200367 rp_len = sizeof(*rp) + (2 * count);
368 rp = kmalloc(rp_len, GFP_ATOMIC);
369 if (!rp) {
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100370 read_unlock(&hci_dev_list_lock);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200371 return -ENOMEM;
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100372 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200373
Johan Hedberg476e44c2012-10-19 20:10:46 +0300374 count = 0;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200375 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700376 if (hci_dev_test_flag(d, HCI_SETUP) ||
377 hci_dev_test_flag(d, HCI_CONFIG) ||
378 hci_dev_test_flag(d, HCI_USER_CHANNEL))
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200379 continue;
380
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200381 /* Devices marked as raw-only are neither configured
382 * nor unconfigured controllers.
383 */
384 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
Marcel Holtmann0736cfa2013-08-26 21:40:51 -0700385 continue;
386
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200387 if (d->dev_type == HCI_BREDR &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700388 !hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
Marcel Holtmann1514b892013-10-06 08:25:01 -0700389 rp->index[count++] = cpu_to_le16(d->id);
390 BT_DBG("Added hci%u", d->id);
391 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200392 }
393
Johan Hedberg476e44c2012-10-19 20:10:46 +0300394 rp->num_controllers = cpu_to_le16(count);
395 rp_len = sizeof(*rp) + (2 * count);
396
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200397 read_unlock(&hci_dev_list_lock);
398
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200399 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST,
400 0, rp, rp_len);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200401
Johan Hedberga38528f2011-01-22 06:46:43 +0200402 kfree(rp);
403
404 return err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200405}
406
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200407static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
408 void *data, u16 data_len)
409{
410 struct mgmt_rp_read_unconf_index_list *rp;
411 struct hci_dev *d;
412 size_t rp_len;
413 u16 count;
414 int err;
415
416 BT_DBG("sock %p", sk);
417
418 read_lock(&hci_dev_list_lock);
419
420 count = 0;
421 list_for_each_entry(d, &hci_dev_list, list) {
422 if (d->dev_type == HCI_BREDR &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700423 hci_dev_test_flag(d, HCI_UNCONFIGURED))
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200424 count++;
425 }
426
427 rp_len = sizeof(*rp) + (2 * count);
428 rp = kmalloc(rp_len, GFP_ATOMIC);
429 if (!rp) {
430 read_unlock(&hci_dev_list_lock);
431 return -ENOMEM;
432 }
433
434 count = 0;
435 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700436 if (hci_dev_test_flag(d, HCI_SETUP) ||
437 hci_dev_test_flag(d, HCI_CONFIG) ||
438 hci_dev_test_flag(d, HCI_USER_CHANNEL))
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200439 continue;
440
441 /* Devices marked as raw-only are neither configured
442 * nor unconfigured controllers.
443 */
444 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
445 continue;
446
447 if (d->dev_type == HCI_BREDR &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700448 hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200449 rp->index[count++] = cpu_to_le16(d->id);
450 BT_DBG("Added hci%u", d->id);
451 }
452 }
453
454 rp->num_controllers = cpu_to_le16(count);
455 rp_len = sizeof(*rp) + (2 * count);
456
457 read_unlock(&hci_dev_list_lock);
458
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200459 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
460 MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len);
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200461
462 kfree(rp);
463
464 return err;
465}
466
Marcel Holtmann96f14742015-03-14 19:27:57 -0700467static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev,
468 void *data, u16 data_len)
469{
470 struct mgmt_rp_read_ext_index_list *rp;
471 struct hci_dev *d;
472 size_t rp_len;
473 u16 count;
474 int err;
475
476 BT_DBG("sock %p", sk);
477
478 read_lock(&hci_dev_list_lock);
479
480 count = 0;
481 list_for_each_entry(d, &hci_dev_list, list) {
482 if (d->dev_type == HCI_BREDR || d->dev_type == HCI_AMP)
483 count++;
484 }
485
486 rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
487 rp = kmalloc(rp_len, GFP_ATOMIC);
488 if (!rp) {
489 read_unlock(&hci_dev_list_lock);
490 return -ENOMEM;
491 }
492
493 count = 0;
494 list_for_each_entry(d, &hci_dev_list, list) {
495 if (hci_dev_test_flag(d, HCI_SETUP) ||
496 hci_dev_test_flag(d, HCI_CONFIG) ||
497 hci_dev_test_flag(d, HCI_USER_CHANNEL))
498 continue;
499
500 /* Devices marked as raw-only are neither configured
501 * nor unconfigured controllers.
502 */
503 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
504 continue;
505
506 if (d->dev_type == HCI_BREDR) {
507 if (hci_dev_test_flag(d, HCI_UNCONFIGURED))
508 rp->entry[count].type = 0x01;
509 else
510 rp->entry[count].type = 0x00;
511 } else if (d->dev_type == HCI_AMP) {
512 rp->entry[count].type = 0x02;
513 } else {
514 continue;
515 }
516
517 rp->entry[count].bus = d->bus;
518 rp->entry[count++].index = cpu_to_le16(d->id);
519 BT_DBG("Added hci%u", d->id);
520 }
521
522 rp->num_controllers = cpu_to_le16(count);
523 rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
524
525 read_unlock(&hci_dev_list_lock);
526
527 /* If this command is called at least once, then all the
528 * default index and unconfigured index events are disabled
529 * and from now on only extended index events are used.
530 */
531 hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS);
532 hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS);
533 hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
534
535 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
536 MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, rp_len);
537
538 kfree(rp);
539
540 return err;
541}
542
Marcel Holtmanndbece372014-07-04 18:11:55 +0200543static bool is_configured(struct hci_dev *hdev)
544{
545 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700546 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
Marcel Holtmanndbece372014-07-04 18:11:55 +0200547 return false;
548
549 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
550 !bacmp(&hdev->public_addr, BDADDR_ANY))
551 return false;
552
553 return true;
554}
555
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200556static __le32 get_missing_options(struct hci_dev *hdev)
557{
558 u32 options = 0;
559
Marcel Holtmanndbece372014-07-04 18:11:55 +0200560 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700561 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200562 options |= MGMT_OPTION_EXTERNAL_CONFIG;
563
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200564 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
565 !bacmp(&hdev->public_addr, BDADDR_ANY))
566 options |= MGMT_OPTION_PUBLIC_ADDRESS;
567
568 return cpu_to_le32(options);
569}
570
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200571static int new_options(struct hci_dev *hdev, struct sock *skip)
572{
573 __le32 options = get_missing_options(hdev);
574
Marcel Holtmannf6b77122015-03-14 19:28:05 -0700575 return mgmt_generic_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options,
576 sizeof(options), skip);
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200577}
578
Marcel Holtmanndbece372014-07-04 18:11:55 +0200579static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
580{
581 __le32 options = get_missing_options(hdev);
582
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200583 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options,
584 sizeof(options));
Marcel Holtmanndbece372014-07-04 18:11:55 +0200585}
586
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200587static int read_config_info(struct sock *sk, struct hci_dev *hdev,
588 void *data, u16 data_len)
589{
590 struct mgmt_rp_read_config_info rp;
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200591 u32 options = 0;
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200592
593 BT_DBG("sock %p %s", sk, hdev->name);
594
595 hci_dev_lock(hdev);
596
597 memset(&rp, 0, sizeof(rp));
598 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200599
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200600 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
601 options |= MGMT_OPTION_EXTERNAL_CONFIG;
602
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200603 if (hdev->set_bdaddr)
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200604 options |= MGMT_OPTION_PUBLIC_ADDRESS;
605
606 rp.supported_options = cpu_to_le32(options);
607 rp.missing_options = get_missing_options(hdev);
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200608
609 hci_dev_unlock(hdev);
610
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200611 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0,
612 &rp, sizeof(rp));
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200613}
614
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200615static u32 get_supported_settings(struct hci_dev *hdev)
Johan Hedberg03811012010-12-08 00:21:06 +0200616{
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200617 u32 settings = 0;
Johan Hedberg03811012010-12-08 00:21:06 +0200618
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200619 settings |= MGMT_SETTING_POWERED;
Johan Hedbergb2939472014-07-30 09:22:23 +0300620 settings |= MGMT_SETTING_BONDABLE;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800621 settings |= MGMT_SETTING_DEBUG_KEYS;
Johan Hedberg3742abf2014-07-08 16:07:34 +0300622 settings |= MGMT_SETTING_CONNECTABLE;
623 settings |= MGMT_SETTING_DISCOVERABLE;
Johan Hedberg03811012010-12-08 00:21:06 +0200624
Andre Guedesed3fa312012-07-24 15:03:46 -0300625 if (lmp_bredr_capable(hdev)) {
Johan Hedberg1a47aee2013-03-15 17:07:06 -0500626 if (hdev->hci_ver >= BLUETOOTH_VER_1_2)
627 settings |= MGMT_SETTING_FAST_CONNECTABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200628 settings |= MGMT_SETTING_BREDR;
629 settings |= MGMT_SETTING_LINK_SECURITY;
Marcel Holtmanna82974c2013-10-11 09:48:47 -0700630
631 if (lmp_ssp_capable(hdev)) {
632 settings |= MGMT_SETTING_SSP;
633 settings |= MGMT_SETTING_HS;
634 }
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800635
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -0800636 if (lmp_sc_capable(hdev))
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800637 settings |= MGMT_SETTING_SECURE_CONN;
Marcel Holtmann848566b2013-10-01 22:59:22 -0700638 }
Marcel Holtmannd7b7e792012-02-20 21:47:49 +0100639
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300640 if (lmp_le_capable(hdev)) {
Marcel Holtmann9d428202012-05-03 07:12:31 +0200641 settings |= MGMT_SETTING_LE;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300642 settings |= MGMT_SETTING_ADVERTISING;
Johan Hedberga3209692014-05-26 11:23:35 +0300643 settings |= MGMT_SETTING_SECURE_CONN;
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200644 settings |= MGMT_SETTING_PRIVACY;
Marcel Holtmann93690c22015-03-06 10:11:21 -0800645 settings |= MGMT_SETTING_STATIC_ADDRESS;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300646 }
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200647
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200648 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
649 hdev->set_bdaddr)
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200650 settings |= MGMT_SETTING_CONFIGURATION;
651
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200652 return settings;
653}
Johan Hedbergebc99fe2011-01-04 11:54:26 +0200654
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200655static u32 get_current_settings(struct hci_dev *hdev)
656{
657 u32 settings = 0;
Johan Hedbergdc4fe302011-03-16 14:29:36 +0200658
Johan Hedbergf1f0eb02012-02-21 17:15:41 +0200659 if (hdev_is_powered(hdev))
Marcel Holtmannf0d4b782012-02-21 12:14:25 +0100660 settings |= MGMT_SETTING_POWERED;
661
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700662 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200663 settings |= MGMT_SETTING_CONNECTABLE;
664
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700665 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
Johan Hedberg1a4d3c42013-03-15 17:07:08 -0500666 settings |= MGMT_SETTING_FAST_CONNECTABLE;
667
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700668 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200669 settings |= MGMT_SETTING_DISCOVERABLE;
670
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700671 if (hci_dev_test_flag(hdev, HCI_BONDABLE))
Johan Hedbergb2939472014-07-30 09:22:23 +0300672 settings |= MGMT_SETTING_BONDABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200673
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700674 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200675 settings |= MGMT_SETTING_BREDR;
676
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700677 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200678 settings |= MGMT_SETTING_LE;
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200679
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700680 if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200681 settings |= MGMT_SETTING_LINK_SECURITY;
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200682
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700683 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200684 settings |= MGMT_SETTING_SSP;
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200685
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700686 if (hci_dev_test_flag(hdev, HCI_HS_ENABLED))
Johan Hedberg6d80dfd2012-02-20 23:50:38 +0200687 settings |= MGMT_SETTING_HS;
688
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700689 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300690 settings |= MGMT_SETTING_ADVERTISING;
691
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700692 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800693 settings |= MGMT_SETTING_SECURE_CONN;
694
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700695 if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS))
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800696 settings |= MGMT_SETTING_DEBUG_KEYS;
697
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700698 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200699 settings |= MGMT_SETTING_PRIVACY;
700
Marcel Holtmann93690c22015-03-06 10:11:21 -0800701 /* The current setting for static address has two purposes. The
702 * first is to indicate if the static address will be used and
703 * the second is to indicate if it is actually set.
704 *
705 * This means if the static address is not configured, this flag
Marcel Holtmann08dc0e92015-03-25 18:32:13 -0700706 * will never be set. If the address is configured, then if the
Marcel Holtmann93690c22015-03-06 10:11:21 -0800707 * address is actually used decides if the flag is set or not.
708 *
709 * For single mode LE only controllers and dual-mode controllers
710 * with BR/EDR disabled, the existence of the static address will
711 * be evaluated.
712 */
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -0700713 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700714 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
Marcel Holtmann93690c22015-03-06 10:11:21 -0800715 !bacmp(&hdev->bdaddr, BDADDR_ANY)) {
716 if (bacmp(&hdev->static_addr, BDADDR_ANY))
717 settings |= MGMT_SETTING_STATIC_ADDRESS;
718 }
719
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200720 return settings;
Johan Hedbergc542a062011-01-26 13:11:03 +0200721}
722
Johan Hedberg333ae952015-03-17 13:48:47 +0200723static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev)
724{
725 return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev);
726}
727
Johan Hedberg333ae952015-03-17 13:48:47 +0200728static struct mgmt_pending_cmd *pending_find_data(u16 opcode,
729 struct hci_dev *hdev,
730 const void *data)
731{
732 return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data);
733}
734
Johan Hedbergf2252572015-11-18 12:49:20 +0200735u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev)
Johan Hedberg9a43e252013-10-20 19:00:07 +0300736{
Johan Hedberg3b0602c2015-03-06 21:08:55 +0200737 struct mgmt_pending_cmd *cmd;
Johan Hedberg9a43e252013-10-20 19:00:07 +0300738
739 /* If there's a pending mgmt command the flags will not yet have
740 * their final values, so check for this first.
741 */
Johan Hedberg333ae952015-03-17 13:48:47 +0200742 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg9a43e252013-10-20 19:00:07 +0300743 if (cmd) {
744 struct mgmt_mode *cp = cmd->param;
745 if (cp->val == 0x01)
746 return LE_AD_GENERAL;
747 else if (cp->val == 0x02)
748 return LE_AD_LIMITED;
749 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700750 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300751 return LE_AD_LIMITED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700752 else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300753 return LE_AD_GENERAL;
754 }
755
756 return 0;
757}
758
Johan Hedbergf2252572015-11-18 12:49:20 +0200759bool mgmt_get_connectable(struct hci_dev *hdev)
Arman Uguraye7a685d2015-03-25 18:53:40 -0700760{
761 struct mgmt_pending_cmd *cmd;
762
763 /* If there's a pending mgmt command the flag will not yet have
764 * it's final value, so check for this first.
765 */
766 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
767 if (cmd) {
768 struct mgmt_mode *cp = cmd->param;
769
770 return cp->val;
771 }
772
773 return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
774}
775
Johan Hedberg7d785252011-12-15 00:47:39 +0200776static void service_cache_off(struct work_struct *work)
777{
778 struct hci_dev *hdev = container_of(work, struct hci_dev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300779 service_cache.work);
Johan Hedberg890ea892013-03-15 17:06:52 -0500780 struct hci_request req;
Johan Hedberg7d785252011-12-15 00:47:39 +0200781
Marcel Holtmanna69d8922015-03-13 02:11:05 -0700782 if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg7d785252011-12-15 00:47:39 +0200783 return;
784
Johan Hedberg890ea892013-03-15 17:06:52 -0500785 hci_req_init(&req, hdev);
786
Johan Hedberg7d785252011-12-15 00:47:39 +0200787 hci_dev_lock(hdev);
788
Johan Hedbergb1a89172015-11-25 16:15:42 +0200789 __hci_req_update_eir(&req);
Johan Hedberg14bf5ea2015-11-22 19:00:22 +0200790 __hci_req_update_class(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +0200791
792 hci_dev_unlock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -0500793
794 hci_req_run(&req, NULL);
Johan Hedberg7d785252011-12-15 00:47:39 +0200795}
796
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200797static void rpa_expired(struct work_struct *work)
798{
799 struct hci_dev *hdev = container_of(work, struct hci_dev,
800 rpa_expired.work);
801 struct hci_request req;
802
803 BT_DBG("");
804
Marcel Holtmanna1536da2015-03-13 02:11:01 -0700805 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200806
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700807 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200808 return;
809
810 /* The generation of a new RPA and programming it into the
Johan Hedbergf2252572015-11-18 12:49:20 +0200811 * controller happens in the hci_req_enable_advertising()
812 * function.
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200813 */
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200814 hci_req_init(&req, hdev);
Johan Hedbergf2252572015-11-18 12:49:20 +0200815 __hci_req_enable_advertising(&req);
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200816 hci_req_run(&req, NULL);
817}
818
Johan Hedberg6a919082012-02-28 06:17:26 +0200819static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
Johan Hedberg7d785252011-12-15 00:47:39 +0200820{
Marcel Holtmann238be782015-03-13 02:11:06 -0700821 if (hci_dev_test_and_set_flag(hdev, HCI_MGMT))
Johan Hedberg6a919082012-02-28 06:17:26 +0200822 return;
823
Johan Hedberg4f87da82012-03-02 19:55:56 +0200824 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200825 INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
Johan Hedberg7d785252011-12-15 00:47:39 +0200826
Johan Hedberg4f87da82012-03-02 19:55:56 +0200827 /* Non-mgmt controlled devices get this bit set
828 * implicitly so that pairing works for them, however
829 * for mgmt we require user-space to explicitly enable
830 * it
831 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -0700832 hci_dev_clear_flag(hdev, HCI_BONDABLE);
Johan Hedberg7d785252011-12-15 00:47:39 +0200833}
834
Johan Hedberg0f4e68c2012-02-28 17:18:30 +0200835static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300836 void *data, u16 data_len)
Johan Hedberg03811012010-12-08 00:21:06 +0200837{
838 struct mgmt_rp_read_info rp;
Johan Hedberg03811012010-12-08 00:21:06 +0200839
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200840 BT_DBG("sock %p %s", sk, hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +0200841
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300842 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +0200843
Johan Hedberg03811012010-12-08 00:21:06 +0200844 memset(&rp, 0, sizeof(rp));
845
Johan Hedberg03811012010-12-08 00:21:06 +0200846 bacpy(&rp.bdaddr, &hdev->bdaddr);
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200847
848 rp.version = hdev->hci_ver;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200849 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200850
851 rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
852 rp.current_settings = cpu_to_le32(get_current_settings(hdev));
853
854 memcpy(rp.dev_class, hdev->dev_class, 3);
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200855
856 memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
Johan Hedberg27fcc362012-02-22 21:46:22 +0200857 memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200858
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300859 hci_dev_unlock(hdev);
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200860
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200861 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
862 sizeof(rp));
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200863}
864
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200865static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
Johan Hedberg86805702011-11-11 16:18:52 +0200866{
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200867 __le32 settings = cpu_to_le32(get_current_settings(hdev));
Johan Hedberg86805702011-11-11 16:18:52 +0200868
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200869 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings,
870 sizeof(settings));
Johan Hedberg86805702011-11-11 16:18:52 +0200871}
872
Marcel Holtmann1904a852015-01-11 13:50:44 -0800873static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg8b064a32014-02-24 14:52:22 +0200874{
875 BT_DBG("%s status 0x%02x", hdev->name, status);
876
Johan Hedberga3172b72014-02-28 09:33:44 +0200877 if (hci_conn_count(hdev) == 0) {
878 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +0200879 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberga3172b72014-02-28 09:33:44 +0200880 }
Johan Hedberg8b064a32014-02-24 14:52:22 +0200881}
882
Johan Hedbergf2252572015-11-18 12:49:20 +0200883void mgmt_advertising_added(struct sock *sk, struct hci_dev *hdev, u8 instance)
Arman Uguray912098a2015-03-23 15:57:15 -0700884{
885 struct mgmt_ev_advertising_added ev;
886
887 ev.instance = instance;
888
889 mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk);
890}
891
Johan Hedbergf2252572015-11-18 12:49:20 +0200892void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev,
893 u8 instance)
Arman Uguray912098a2015-03-23 15:57:15 -0700894{
895 struct mgmt_ev_advertising_removed ev;
896
897 ev.instance = instance;
898
899 mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk);
900}
901
Florian Grandel7816b822015-06-18 03:16:45 +0200902static void cancel_adv_timeout(struct hci_dev *hdev)
903{
904 if (hdev->adv_instance_timeout) {
905 hdev->adv_instance_timeout = 0;
906 cancel_delayed_work(&hdev->adv_instance_expire);
907 }
908}
909
Johan Hedberg8b064a32014-02-24 14:52:22 +0200910static int clean_up_hci_state(struct hci_dev *hdev)
911{
912 struct hci_request req;
913 struct hci_conn *conn;
Johan Hedberg23a48092014-07-08 16:05:06 +0300914 bool discov_stopped;
915 int err;
Johan Hedberg8b064a32014-02-24 14:52:22 +0200916
917 hci_req_init(&req, hdev);
918
919 if (test_bit(HCI_ISCAN, &hdev->flags) ||
920 test_bit(HCI_PSCAN, &hdev->flags)) {
921 u8 scan = 0x00;
922 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
923 }
924
Johan Hedbergf2252572015-11-18 12:49:20 +0200925 hci_req_clear_adv_instance(hdev, NULL, 0x00, false);
Arman Uguray912098a2015-03-23 15:57:15 -0700926
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700927 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedbergf2252572015-11-18 12:49:20 +0200928 __hci_req_disable_advertising(&req);
Johan Hedberg8b064a32014-02-24 14:52:22 +0200929
Johan Hedberg2154d3f2015-11-11 08:30:45 +0200930 discov_stopped = hci_req_stop_discovery(&req);
Johan Hedberg8b064a32014-02-24 14:52:22 +0200931
932 list_for_each_entry(conn, &hdev->conn_hash.list, list) {
Johan Hedberg89e0ccc2015-10-22 10:49:38 +0300933 /* 0x15 == Terminated due to Power Off */
934 __hci_abort_conn(&req, conn, 0x15);
Johan Hedberg8b064a32014-02-24 14:52:22 +0200935 }
936
Johan Hedberg23a48092014-07-08 16:05:06 +0300937 err = hci_req_run(&req, clean_up_hci_complete);
938 if (!err && discov_stopped)
939 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
940
941 return err;
Johan Hedberg8b064a32014-02-24 14:52:22 +0200942}
943
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200944static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300945 u16 len)
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200946{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -0300947 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +0200948 struct mgmt_pending_cmd *cmd;
Johan Hedberg4b34ee782012-02-21 14:13:02 +0200949 int err;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200950
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200951 BT_DBG("request for %s", hdev->name);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200952
Johan Hedberga7e80f22013-01-09 16:05:19 +0200953 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +0200954 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
955 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +0200956
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300957 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200958
Johan Hedberg333ae952015-03-17 13:48:47 +0200959 if (pending_find(MGMT_OP_SET_POWERED, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +0200960 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
961 MGMT_STATUS_BUSY);
Johan Hedberg87b95ba2013-09-25 13:26:06 +0300962 goto failed;
963 }
964
Johan Hedberg4b34ee782012-02-21 14:13:02 +0200965 if (!!cp->val == hdev_is_powered(hdev)) {
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200966 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200967 goto failed;
968 }
969
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200970 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
971 if (!cmd) {
972 err = -ENOMEM;
973 goto failed;
974 }
975
Johan Hedberg8b064a32014-02-24 14:52:22 +0200976 if (cp->val) {
Johan Hedberg19202572013-01-14 22:33:51 +0200977 queue_work(hdev->req_workqueue, &hdev->power_on);
Johan Hedberg8b064a32014-02-24 14:52:22 +0200978 err = 0;
979 } else {
980 /* Disconnect connections, stop scans, etc */
981 err = clean_up_hci_state(hdev);
Johan Hedberga3172b72014-02-28 09:33:44 +0200982 if (!err)
983 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
984 HCI_POWER_OFF_TIMEOUT);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200985
Johan Hedberg8b064a32014-02-24 14:52:22 +0200986 /* ENODATA means there were no HCI commands queued */
987 if (err == -ENODATA) {
Johan Hedberga3172b72014-02-28 09:33:44 +0200988 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +0200989 queue_work(hdev->req_workqueue, &hdev->power_off.work);
990 err = 0;
991 }
992 }
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200993
994failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300995 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200996 return err;
997}
998
Johan Hedbergbeadb2b2012-02-21 16:55:31 +0200999static int new_settings(struct hci_dev *hdev, struct sock *skip)
1000{
Marcel Holtmannf6b77122015-03-14 19:28:05 -07001001 __le32 ev = cpu_to_le32(get_current_settings(hdev));
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001002
Marcel Holtmannf6b77122015-03-14 19:28:05 -07001003 return mgmt_generic_event(MGMT_EV_NEW_SETTINGS, hdev, &ev,
1004 sizeof(ev), skip);
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001005}
1006
Johan Hedberg91a668b2014-07-09 13:28:26 +03001007int mgmt_new_settings(struct hci_dev *hdev)
1008{
1009 return new_settings(hdev, NULL);
1010}
1011
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001012struct cmd_lookup {
1013 struct sock *sk;
1014 struct hci_dev *hdev;
1015 u8 mgmt_status;
1016};
1017
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001018static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001019{
1020 struct cmd_lookup *match = data;
1021
1022 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
1023
1024 list_del(&cmd->list);
1025
1026 if (match->sk == NULL) {
1027 match->sk = cmd->sk;
1028 sock_hold(match->sk);
1029 }
1030
1031 mgmt_pending_free(cmd);
1032}
1033
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001034static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001035{
1036 u8 *status = data;
1037
Johan Hedberga69e8372015-03-06 21:08:53 +02001038 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001039 mgmt_pending_remove(cmd);
1040}
1041
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001042static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg1b9b5ee2014-12-05 13:36:00 +02001043{
1044 if (cmd->cmd_complete) {
1045 u8 *status = data;
1046
1047 cmd->cmd_complete(cmd, *status);
1048 mgmt_pending_remove(cmd);
1049
1050 return;
1051 }
1052
1053 cmd_status_rsp(cmd, data);
1054}
1055
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001056static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedbergf5818c22014-12-05 13:36:02 +02001057{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001058 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1059 cmd->param, cmd->param_len);
Johan Hedbergf5818c22014-12-05 13:36:02 +02001060}
1061
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001062static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001063{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001064 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1065 cmd->param, sizeof(struct mgmt_addr_info));
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001066}
1067
Johan Hedberge6fe7982013-10-02 15:45:22 +03001068static u8 mgmt_bredr_support(struct hci_dev *hdev)
1069{
1070 if (!lmp_bredr_capable(hdev))
1071 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001072 else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001073 return MGMT_STATUS_REJECTED;
1074 else
1075 return MGMT_STATUS_SUCCESS;
1076}
1077
1078static u8 mgmt_le_support(struct hci_dev *hdev)
1079{
1080 if (!lmp_le_capable(hdev))
1081 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001082 else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001083 return MGMT_STATUS_REJECTED;
1084 else
1085 return MGMT_STATUS_SUCCESS;
1086}
1087
Johan Hedbergaed1a882015-11-22 17:24:44 +03001088void mgmt_set_discoverable_complete(struct hci_dev *hdev, u8 status)
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001089{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001090 struct mgmt_pending_cmd *cmd;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001091
1092 BT_DBG("status 0x%02x", status);
1093
1094 hci_dev_lock(hdev);
1095
Johan Hedberg333ae952015-03-17 13:48:47 +02001096 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001097 if (!cmd)
1098 goto unlock;
1099
1100 if (status) {
1101 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001102 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001103 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001104 goto remove_cmd;
1105 }
1106
Johan Hedbergaed1a882015-11-22 17:24:44 +03001107 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1108 hdev->discov_timeout > 0) {
1109 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1110 queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001111 }
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001112
1113 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergaed1a882015-11-22 17:24:44 +03001114 new_settings(hdev, cmd->sk);
Marcel Holtmann970ba522013-10-15 06:33:57 -07001115
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001116remove_cmd:
1117 mgmt_pending_remove(cmd);
1118
1119unlock:
1120 hci_dev_unlock(hdev);
1121}
1122
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001123static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001124 u16 len)
Johan Hedberg73f22f62010-12-29 16:00:25 +02001125{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001126 struct mgmt_cp_set_discoverable *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001127 struct mgmt_pending_cmd *cmd;
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001128 u16 timeout;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001129 int err;
1130
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001131 BT_DBG("request for %s", hdev->name);
Johan Hedberg73f22f62010-12-29 16:00:25 +02001132
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001133 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1134 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001135 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1136 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001137
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001138 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02001139 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1140 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001141
Marcel Holtmann1f350c82012-03-12 20:31:08 -07001142 timeout = __le16_to_cpu(cp->timeout);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001143
1144 /* Disabling discoverable requires that no timeout is set,
1145 * and enabling limited discoverable requires a timeout.
1146 */
1147 if ((cp->val == 0x00 && timeout > 0) ||
1148 (cp->val == 0x02 && timeout == 0))
Johan Hedberga69e8372015-03-06 21:08:53 +02001149 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1150 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001151
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001152 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001153
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001154 if (!hdev_is_powered(hdev) && timeout > 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001155 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1156 MGMT_STATUS_NOT_POWERED);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001157 goto failed;
1158 }
1159
Johan Hedberg333ae952015-03-17 13:48:47 +02001160 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1161 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001162 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1163 MGMT_STATUS_BUSY);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001164 goto failed;
1165 }
1166
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001167 if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001168 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1169 MGMT_STATUS_REJECTED);
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001170 goto failed;
1171 }
1172
1173 if (!hdev_is_powered(hdev)) {
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001174 bool changed = false;
1175
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001176 /* Setting limited discoverable when powered off is
1177 * not a valid operation since it requires a timeout
1178 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1179 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001180 if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07001181 hci_dev_change_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001182 changed = true;
1183 }
1184
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001185 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001186 if (err < 0)
1187 goto failed;
1188
1189 if (changed)
1190 err = new_settings(hdev, sk);
1191
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001192 goto failed;
1193 }
1194
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001195 /* If the current mode is the same, then just update the timeout
1196 * value with the new value. And if only the timeout gets updated,
1197 * then no need for any HCI transactions.
1198 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001199 if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1200 (cp->val == 0x02) == hci_dev_test_flag(hdev,
1201 HCI_LIMITED_DISCOVERABLE)) {
Marcel Holtmann36261542013-10-15 08:28:51 -07001202 cancel_delayed_work(&hdev->discov_off);
1203 hdev->discov_timeout = timeout;
Marcel Holtmann955638e2012-02-22 18:21:00 +01001204
Marcel Holtmann36261542013-10-15 08:28:51 -07001205 if (cp->val && hdev->discov_timeout > 0) {
1206 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
Johan Hedbergc366f552015-11-23 15:43:06 +02001207 queue_delayed_work(hdev->req_workqueue,
1208 &hdev->discov_off, to);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001209 }
1210
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001211 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001212 goto failed;
1213 }
1214
1215 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
1216 if (!cmd) {
1217 err = -ENOMEM;
1218 goto failed;
1219 }
1220
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001221 /* Cancel any potential discoverable timeout that might be
1222 * still active and store new timeout value. The arming of
1223 * the timeout happens in the complete handler.
1224 */
1225 cancel_delayed_work(&hdev->discov_off);
1226 hdev->discov_timeout = timeout;
1227
Johan Hedbergaed1a882015-11-22 17:24:44 +03001228 if (cp->val)
1229 hci_dev_set_flag(hdev, HCI_DISCOVERABLE);
1230 else
1231 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1232
Johan Hedbergb456f872013-10-19 23:38:22 +03001233 /* Limited discoverable mode */
1234 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001235 hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001236 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001237 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001238
Johan Hedbergaed1a882015-11-22 17:24:44 +03001239 queue_work(hdev->req_workqueue, &hdev->discoverable_update);
1240 err = 0;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001241
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001242failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001243 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001244 return err;
1245}
Johan Hedberg73f22f62010-12-29 16:00:25 +02001246
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001247void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status)
Johan Hedberg2b76f452013-03-15 17:07:04 -05001248{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001249 struct mgmt_pending_cmd *cmd;
Johan Hedberg2b76f452013-03-15 17:07:04 -05001250
1251 BT_DBG("status 0x%02x", status);
1252
1253 hci_dev_lock(hdev);
1254
Johan Hedberg333ae952015-03-17 13:48:47 +02001255 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
Johan Hedberg2b76f452013-03-15 17:07:04 -05001256 if (!cmd)
1257 goto unlock;
1258
Johan Hedberg37438c12013-10-14 16:20:05 +03001259 if (status) {
1260 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001261 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg37438c12013-10-14 16:20:05 +03001262 goto remove_cmd;
1263 }
1264
Johan Hedberg2b76f452013-03-15 17:07:04 -05001265 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001266 new_settings(hdev, cmd->sk);
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001267
Johan Hedberg37438c12013-10-14 16:20:05 +03001268remove_cmd:
Johan Hedberg2b76f452013-03-15 17:07:04 -05001269 mgmt_pending_remove(cmd);
1270
1271unlock:
1272 hci_dev_unlock(hdev);
1273}
1274
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001275static int set_connectable_update_settings(struct hci_dev *hdev,
1276 struct sock *sk, u8 val)
1277{
1278 bool changed = false;
1279 int err;
1280
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001281 if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE))
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001282 changed = true;
1283
1284 if (val) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001285 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001286 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001287 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
1288 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001289 }
1290
1291 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
1292 if (err < 0)
1293 return err;
1294
Johan Hedberg562064e2014-07-08 16:35:34 +03001295 if (changed) {
Johan Hedberg01b1cb82015-11-16 12:52:21 +02001296 hci_req_update_scan(hdev);
Johan Hedberg562064e2014-07-08 16:35:34 +03001297 hci_update_background_scan(hdev);
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001298 return new_settings(hdev, sk);
Johan Hedberg562064e2014-07-08 16:35:34 +03001299 }
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001300
1301 return 0;
1302}
1303
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001304static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001305 u16 len)
Johan Hedberg73f22f62010-12-29 16:00:25 +02001306{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001307 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001308 struct mgmt_pending_cmd *cmd;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001309 int err;
1310
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001311 BT_DBG("request for %s", hdev->name);
Johan Hedberg73f22f62010-12-29 16:00:25 +02001312
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001313 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1314 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001315 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1316 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001317
Johan Hedberga7e80f22013-01-09 16:05:19 +02001318 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001319 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1320 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001321
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001322 hci_dev_lock(hdev);
Johan Hedberg73f22f62010-12-29 16:00:25 +02001323
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001324 if (!hdev_is_powered(hdev)) {
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001325 err = set_connectable_update_settings(hdev, sk, cp->val);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001326 goto failed;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001327 }
1328
Johan Hedberg333ae952015-03-17 13:48:47 +02001329 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1330 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001331 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1332 MGMT_STATUS_BUSY);
Johan Hedberg73f22f62010-12-29 16:00:25 +02001333 goto failed;
1334 }
1335
Johan Hedberg73f22f62010-12-29 16:00:25 +02001336 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
1337 if (!cmd) {
1338 err = -ENOMEM;
Johan Hedberg72a734e2010-12-30 00:38:22 +02001339 goto failed;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001340 }
1341
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001342 if (cp->val) {
1343 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
1344 } else {
1345 if (hdev->discov_timeout > 0)
1346 cancel_delayed_work(&hdev->discov_off);
Johan Hedberg2b76f452013-03-15 17:07:04 -05001347
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001348 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1349 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1350 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
Johan Hedberg9b742462013-10-14 16:20:03 +03001351 }
Johan Hedberg2b76f452013-03-15 17:07:04 -05001352
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001353 queue_work(hdev->req_workqueue, &hdev->connectable_update);
1354 err = 0;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001355
1356failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001357 hci_dev_unlock(hdev);
Johan Hedberg72a734e2010-12-30 00:38:22 +02001358 return err;
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001359}
1360
Johan Hedbergb2939472014-07-30 09:22:23 +03001361static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001362 u16 len)
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001363{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001364 struct mgmt_mode *cp = data;
Marcel Holtmann55594352013-10-06 16:11:57 -07001365 bool changed;
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001366 int err;
1367
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001368 BT_DBG("request for %s", hdev->name);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001369
Johan Hedberga7e80f22013-01-09 16:05:19 +02001370 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001371 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE,
1372 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001373
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001374 hci_dev_lock(hdev);
Johan Hedberg053f0212011-01-26 13:07:10 +02001375
1376 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07001377 changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE);
Johan Hedberg053f0212011-01-26 13:07:10 +02001378 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001379 changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE);
Johan Hedberg053f0212011-01-26 13:07:10 +02001380
Johan Hedbergb2939472014-07-30 09:22:23 +03001381 err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev);
Johan Hedberg053f0212011-01-26 13:07:10 +02001382 if (err < 0)
Marcel Holtmann55594352013-10-06 16:11:57 -07001383 goto unlock;
Johan Hedberg053f0212011-01-26 13:07:10 +02001384
Johan Hedberg82a37ad2016-03-09 17:30:34 +02001385 if (changed) {
1386 /* In limited privacy mode the change of bondable mode
1387 * may affect the local advertising address.
1388 */
1389 if (hdev_is_powered(hdev) &&
1390 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1391 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1392 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
1393 queue_work(hdev->req_workqueue,
1394 &hdev->discoverable_update);
1395
Marcel Holtmann55594352013-10-06 16:11:57 -07001396 err = new_settings(hdev, sk);
Johan Hedberg82a37ad2016-03-09 17:30:34 +02001397 }
Johan Hedberg053f0212011-01-26 13:07:10 +02001398
Marcel Holtmann55594352013-10-06 16:11:57 -07001399unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001400 hci_dev_unlock(hdev);
Johan Hedberg053f0212011-01-26 13:07:10 +02001401 return err;
1402}
1403
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001404static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
1405 u16 len)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001406{
1407 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001408 struct mgmt_pending_cmd *cmd;
Johan Hedberge6fe7982013-10-02 15:45:22 +03001409 u8 val, status;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001410 int err;
1411
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001412 BT_DBG("request for %s", hdev->name);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001413
Johan Hedberge6fe7982013-10-02 15:45:22 +03001414 status = mgmt_bredr_support(hdev);
1415 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02001416 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1417 status);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001418
Johan Hedberga7e80f22013-01-09 16:05:19 +02001419 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001420 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1421 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001422
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001423 hci_dev_lock(hdev);
1424
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001425 if (!hdev_is_powered(hdev)) {
Johan Hedberg47990ea2012-02-22 11:58:37 +02001426 bool changed = false;
1427
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001428 if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07001429 hci_dev_change_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02001430 changed = true;
1431 }
1432
1433 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1434 if (err < 0)
1435 goto failed;
1436
1437 if (changed)
1438 err = new_settings(hdev, sk);
1439
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001440 goto failed;
1441 }
1442
Johan Hedberg333ae952015-03-17 13:48:47 +02001443 if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001444 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1445 MGMT_STATUS_BUSY);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001446 goto failed;
1447 }
1448
1449 val = !!cp->val;
1450
1451 if (test_bit(HCI_AUTH, &hdev->flags) == val) {
1452 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1453 goto failed;
1454 }
1455
1456 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
1457 if (!cmd) {
1458 err = -ENOMEM;
1459 goto failed;
1460 }
1461
1462 err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
1463 if (err < 0) {
1464 mgmt_pending_remove(cmd);
1465 goto failed;
1466 }
1467
1468failed:
1469 hci_dev_unlock(hdev);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001470 return err;
1471}
1472
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001473static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001474{
1475 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001476 struct mgmt_pending_cmd *cmd;
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07001477 u8 status;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001478 int err;
1479
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001480 BT_DBG("request for %s", hdev->name);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001481
Marcel Holtmanncdba5282013-10-02 21:31:52 -07001482 status = mgmt_bredr_support(hdev);
1483 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02001484 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
Marcel Holtmanncdba5282013-10-02 21:31:52 -07001485
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001486 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02001487 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1488 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001489
Johan Hedberga7e80f22013-01-09 16:05:19 +02001490 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001491 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1492 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001493
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001494 hci_dev_lock(hdev);
Johan Hedberg6c8f12c2012-02-22 16:35:26 +02001495
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001496 if (!hdev_is_powered(hdev)) {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001497 bool changed;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02001498
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001499 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001500 changed = !hci_dev_test_and_set_flag(hdev,
1501 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001502 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001503 changed = hci_dev_test_and_clear_flag(hdev,
1504 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001505 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001506 changed = hci_dev_test_and_clear_flag(hdev,
1507 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001508 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001509 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02001510 }
1511
1512 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
1513 if (err < 0)
1514 goto failed;
1515
1516 if (changed)
1517 err = new_settings(hdev, sk);
1518
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001519 goto failed;
1520 }
1521
Johan Hedberg333ae952015-03-17 13:48:47 +02001522 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001523 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1524 MGMT_STATUS_BUSY);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001525 goto failed;
1526 }
1527
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001528 if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001529 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
1530 goto failed;
1531 }
1532
1533 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
1534 if (!cmd) {
1535 err = -ENOMEM;
1536 goto failed;
1537 }
1538
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001539 if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03001540 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
1541 sizeof(cp->val), &cp->val);
1542
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07001543 err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001544 if (err < 0) {
1545 mgmt_pending_remove(cmd);
1546 goto failed;
1547 }
1548
1549failed:
1550 hci_dev_unlock(hdev);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001551 return err;
1552}
1553
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001554static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001555{
1556 struct mgmt_mode *cp = data;
Marcel Holtmannee392692013-10-01 22:59:23 -07001557 bool changed;
Johan Hedberge6fe7982013-10-02 15:45:22 +03001558 u8 status;
Marcel Holtmannee392692013-10-01 22:59:23 -07001559 int err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001560
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001561 BT_DBG("request for %s", hdev->name);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001562
Johan Hedberge6fe7982013-10-02 15:45:22 +03001563 status = mgmt_bredr_support(hdev);
1564 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02001565 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001566
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001567 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02001568 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1569 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001570
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001571 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001572 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1573 MGMT_STATUS_REJECTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001574
Johan Hedberga7e80f22013-01-09 16:05:19 +02001575 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001576 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1577 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001578
Marcel Holtmannee392692013-10-01 22:59:23 -07001579 hci_dev_lock(hdev);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001580
Johan Hedberg333ae952015-03-17 13:48:47 +02001581 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001582 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1583 MGMT_STATUS_BUSY);
Johan Hedberga2cb01d2015-02-19 17:38:07 +02001584 goto unlock;
1585 }
1586
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001587 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001588 changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001589 } else {
1590 if (hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001591 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1592 MGMT_STATUS_REJECTED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001593 goto unlock;
1594 }
1595
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001596 changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001597 }
Marcel Holtmannee392692013-10-01 22:59:23 -07001598
1599 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
1600 if (err < 0)
1601 goto unlock;
1602
1603 if (changed)
1604 err = new_settings(hdev, sk);
1605
1606unlock:
1607 hci_dev_unlock(hdev);
1608 return err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001609}
1610
Marcel Holtmann1904a852015-01-11 13:50:44 -08001611static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001612{
1613 struct cmd_lookup match = { NULL, hdev };
1614
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05301615 hci_dev_lock(hdev);
1616
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001617 if (status) {
1618 u8 mgmt_err = mgmt_status(status);
1619
1620 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
1621 &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05301622 goto unlock;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001623 }
1624
1625 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
1626
1627 new_settings(hdev, match.sk);
1628
1629 if (match.sk)
1630 sock_put(match.sk);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001631
1632 /* Make sure the controller has a good default for
1633 * advertising data. Restrict the update to when LE
1634 * has actually been enabled. During power on, the
1635 * update in powered_update_hci will take care of it.
1636 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001637 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001638 struct hci_request req;
1639
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001640 hci_req_init(&req, hdev);
Johan Hedbergcab054a2015-11-30 11:21:45 +02001641 __hci_req_update_adv_data(&req, 0x00);
1642 __hci_req_update_scan_rsp_data(&req, 0x00);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001643 hci_req_run(&req, NULL);
Johan Hedberg2e93e532015-11-11 08:11:17 +02001644 hci_update_background_scan(hdev);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001645 }
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05301646
1647unlock:
1648 hci_dev_unlock(hdev);
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001649}
1650
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001651static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg06199cf2012-02-22 16:37:11 +02001652{
1653 struct mgmt_mode *cp = data;
1654 struct hci_cp_write_le_host_supported hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001655 struct mgmt_pending_cmd *cmd;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001656 struct hci_request req;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001657 int err;
Johan Hedberg0b60eba2012-02-28 00:57:24 +02001658 u8 val, enabled;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001659
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001660 BT_DBG("request for %s", hdev->name);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001661
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001662 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02001663 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1664 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg1de028c2012-02-29 19:55:35 -08001665
Johan Hedberga7e80f22013-01-09 16:05:19 +02001666 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001667 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1668 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001669
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07001670 /* Bluetooth single mode LE only controllers or dual-mode
1671 * controllers configured as LE only devices, do not allow
1672 * switching LE off. These have either LE enabled explicitly
1673 * or BR/EDR has been previously switched off.
1674 *
1675 * When trying to enable an already enabled LE, then gracefully
1676 * send a positive response. Trying to disable it however will
1677 * result into rejection.
1678 */
1679 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
1680 if (cp->val == 0x01)
1681 return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
1682
Johan Hedberga69e8372015-03-06 21:08:53 +02001683 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1684 MGMT_STATUS_REJECTED);
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07001685 }
Johan Hedbergc73eee92013-04-19 18:35:21 +03001686
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001687 hci_dev_lock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001688
1689 val = !!cp->val;
Gustavo Padovanffa88e02012-11-23 16:50:51 -02001690 enabled = lmp_host_le_capable(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001691
Florian Grandel847818d2015-06-18 03:16:46 +02001692 if (!val)
Johan Hedbergf2252572015-11-18 12:49:20 +02001693 hci_req_clear_adv_instance(hdev, NULL, 0x00, true);
Florian Grandel847818d2015-06-18 03:16:46 +02001694
Johan Hedberg0b60eba2012-02-28 00:57:24 +02001695 if (!hdev_is_powered(hdev) || val == enabled) {
Johan Hedberg06199cf2012-02-22 16:37:11 +02001696 bool changed = false;
1697
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001698 if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07001699 hci_dev_change_flag(hdev, HCI_LE_ENABLED);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001700 changed = true;
1701 }
1702
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001703 if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001704 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03001705 changed = true;
1706 }
1707
Johan Hedberg06199cf2012-02-22 16:37:11 +02001708 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
1709 if (err < 0)
Johan Hedberg1de028c2012-02-29 19:55:35 -08001710 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001711
1712 if (changed)
1713 err = new_settings(hdev, sk);
1714
Johan Hedberg1de028c2012-02-29 19:55:35 -08001715 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001716 }
1717
Johan Hedberg333ae952015-03-17 13:48:47 +02001718 if (pending_find(MGMT_OP_SET_LE, hdev) ||
1719 pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001720 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1721 MGMT_STATUS_BUSY);
Johan Hedberg1de028c2012-02-29 19:55:35 -08001722 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001723 }
1724
1725 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
1726 if (!cmd) {
1727 err = -ENOMEM;
Johan Hedberg1de028c2012-02-29 19:55:35 -08001728 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001729 }
1730
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001731 hci_req_init(&req, hdev);
1732
Johan Hedberg06199cf2012-02-22 16:37:11 +02001733 memset(&hci_cp, 0, sizeof(hci_cp));
1734
1735 if (val) {
1736 hci_cp.le = val;
Marcel Holtmann32226e42014-07-24 20:04:16 +02001737 hci_cp.simul = 0x00;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001738 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001739 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedbergf2252572015-11-18 12:49:20 +02001740 __hci_req_disable_advertising(&req);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001741 }
1742
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001743 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp),
1744 &hci_cp);
1745
1746 err = hci_req_run(&req, le_enable_complete);
Syam Sidhardhan0c01bc42012-04-12 20:33:21 +05301747 if (err < 0)
Johan Hedberg06199cf2012-02-22 16:37:11 +02001748 mgmt_pending_remove(cmd);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001749
Johan Hedberg1de028c2012-02-29 19:55:35 -08001750unlock:
1751 hci_dev_unlock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001752 return err;
1753}
1754
Johan Hedberg0cab9c82013-03-15 17:06:54 -05001755/* This is a helper function to test for pending mgmt commands that can
1756 * cause CoD or EIR HCI commands. We can only allow one such pending
1757 * mgmt command at a time since otherwise we cannot easily track what
1758 * the current values are, will be, and based on that calculate if a new
1759 * HCI command needs to be sent and if yes with what value.
1760 */
1761static bool pending_eir_or_class(struct hci_dev *hdev)
1762{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001763 struct mgmt_pending_cmd *cmd;
Johan Hedberg0cab9c82013-03-15 17:06:54 -05001764
1765 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
1766 switch (cmd->opcode) {
1767 case MGMT_OP_ADD_UUID:
1768 case MGMT_OP_REMOVE_UUID:
1769 case MGMT_OP_SET_DEV_CLASS:
1770 case MGMT_OP_SET_POWERED:
1771 return true;
1772 }
1773 }
1774
1775 return false;
1776}
1777
Johan Hedberg83be8ec2013-01-27 00:31:29 +02001778static const u8 bluetooth_base_uuid[] = {
1779 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
1780 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1781};
1782
1783static u8 get_uuid_size(const u8 *uuid)
1784{
1785 u32 val;
1786
1787 if (memcmp(uuid, bluetooth_base_uuid, 12))
1788 return 128;
1789
1790 val = get_unaligned_le32(&uuid[12]);
1791 if (val > 0xffff)
1792 return 32;
1793
1794 return 16;
1795}
1796
Johan Hedberg92da6092013-03-15 17:06:55 -05001797static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status)
1798{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001799 struct mgmt_pending_cmd *cmd;
Johan Hedberg92da6092013-03-15 17:06:55 -05001800
1801 hci_dev_lock(hdev);
1802
Johan Hedberg333ae952015-03-17 13:48:47 +02001803 cmd = pending_find(mgmt_op, hdev);
Johan Hedberg92da6092013-03-15 17:06:55 -05001804 if (!cmd)
1805 goto unlock;
1806
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001807 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
1808 mgmt_status(status), hdev->dev_class, 3);
Johan Hedberg92da6092013-03-15 17:06:55 -05001809
1810 mgmt_pending_remove(cmd);
1811
1812unlock:
1813 hci_dev_unlock(hdev);
1814}
1815
Marcel Holtmann1904a852015-01-11 13:50:44 -08001816static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05001817{
1818 BT_DBG("status 0x%02x", status);
1819
1820 mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status);
1821}
1822
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001823static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001824{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001825 struct mgmt_cp_add_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001826 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05001827 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001828 struct bt_uuid *uuid;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001829 int err;
1830
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001831 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001832
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001833 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001834
Johan Hedberg0cab9c82013-03-15 17:06:54 -05001835 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001836 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
1837 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02001838 goto failed;
1839 }
1840
Andre Guedes92c4c202012-06-07 19:05:44 -03001841 uuid = kmalloc(sizeof(*uuid), GFP_KERNEL);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001842 if (!uuid) {
1843 err = -ENOMEM;
1844 goto failed;
1845 }
1846
1847 memcpy(uuid->uuid, cp->uuid, 16);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001848 uuid->svc_hint = cp->svc_hint;
Johan Hedberg83be8ec2013-01-27 00:31:29 +02001849 uuid->size = get_uuid_size(cp->uuid);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001850
Johan Hedbergde66aa62013-01-27 00:31:27 +02001851 list_add_tail(&uuid->list, &hdev->uuids);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001852
Johan Hedberg890ea892013-03-15 17:06:52 -05001853 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001854
Johan Hedberg14bf5ea2015-11-22 19:00:22 +02001855 __hci_req_update_class(&req);
Johan Hedbergb1a89172015-11-25 16:15:42 +02001856 __hci_req_update_eir(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05001857
Johan Hedberg92da6092013-03-15 17:06:55 -05001858 err = hci_req_run(&req, add_uuid_complete);
1859 if (err < 0) {
1860 if (err != -ENODATA)
1861 goto failed;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001862
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001863 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
1864 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02001865 goto failed;
1866 }
1867
1868 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05001869 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02001870 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05001871 goto failed;
1872 }
1873
1874 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001875
1876failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001877 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001878 return err;
1879}
1880
Johan Hedberg24b78d02012-02-23 23:24:30 +02001881static bool enable_service_cache(struct hci_dev *hdev)
1882{
1883 if (!hdev_is_powered(hdev))
1884 return false;
1885
Marcel Holtmann238be782015-03-13 02:11:06 -07001886 if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg46818ed2013-01-14 22:33:52 +02001887 queue_delayed_work(hdev->workqueue, &hdev->service_cache,
1888 CACHE_TIMEOUT);
Johan Hedberg24b78d02012-02-23 23:24:30 +02001889 return true;
1890 }
1891
1892 return false;
1893}
1894
Marcel Holtmann1904a852015-01-11 13:50:44 -08001895static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05001896{
1897 BT_DBG("status 0x%02x", status);
1898
1899 mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status);
1900}
1901
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001902static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03001903 u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001904{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001905 struct mgmt_cp_remove_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001906 struct mgmt_pending_cmd *cmd;
Johan Hedberg056341c2013-01-27 00:31:30 +02001907 struct bt_uuid *match, *tmp;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001908 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 -05001909 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001910 int err, found;
1911
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001912 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001913
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001914 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001915
Johan Hedberg0cab9c82013-03-15 17:06:54 -05001916 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001917 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
1918 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02001919 goto unlock;
1920 }
1921
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001922 if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
Johan Hedberg35f74982014-02-18 17:14:32 +02001923 hci_uuids_clear(hdev);
Johan Hedberg4004b6d2012-02-23 21:30:12 +02001924
Johan Hedberg24b78d02012-02-23 23:24:30 +02001925 if (enable_service_cache(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001926 err = mgmt_cmd_complete(sk, hdev->id,
1927 MGMT_OP_REMOVE_UUID,
1928 0, hdev->dev_class, 3);
Johan Hedberg24b78d02012-02-23 23:24:30 +02001929 goto unlock;
1930 }
Johan Hedberg4004b6d2012-02-23 21:30:12 +02001931
Johan Hedberg9246a862012-02-23 21:33:16 +02001932 goto update_class;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001933 }
1934
1935 found = 0;
1936
Johan Hedberg056341c2013-01-27 00:31:30 +02001937 list_for_each_entry_safe(match, tmp, &hdev->uuids, list) {
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001938 if (memcmp(match->uuid, cp->uuid, 16) != 0)
1939 continue;
1940
1941 list_del(&match->list);
Johan Hedberg482049f2012-11-08 10:25:26 +01001942 kfree(match);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001943 found++;
1944 }
1945
1946 if (found == 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001947 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
1948 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001949 goto unlock;
1950 }
1951
Johan Hedberg9246a862012-02-23 21:33:16 +02001952update_class:
Johan Hedberg890ea892013-03-15 17:06:52 -05001953 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001954
Johan Hedberg14bf5ea2015-11-22 19:00:22 +02001955 __hci_req_update_class(&req);
Johan Hedbergb1a89172015-11-25 16:15:42 +02001956 __hci_req_update_eir(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05001957
Johan Hedberg92da6092013-03-15 17:06:55 -05001958 err = hci_req_run(&req, remove_uuid_complete);
1959 if (err < 0) {
1960 if (err != -ENODATA)
1961 goto unlock;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001962
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001963 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
1964 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02001965 goto unlock;
1966 }
1967
1968 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05001969 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02001970 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05001971 goto unlock;
1972 }
1973
1974 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001975
1976unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001977 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001978 return err;
1979}
1980
Marcel Holtmann1904a852015-01-11 13:50:44 -08001981static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05001982{
1983 BT_DBG("status 0x%02x", status);
1984
1985 mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status);
1986}
1987
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001988static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001989 u16 len)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001990{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001991 struct mgmt_cp_set_dev_class *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001992 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05001993 struct hci_request req;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001994 int err;
1995
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001996 BT_DBG("request for %s", hdev->name);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001997
Marcel Holtmann6203fc92013-10-02 23:37:29 -07001998 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02001999 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2000 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002001
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002002 hci_dev_lock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002003
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002004 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002005 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2006 MGMT_STATUS_BUSY);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002007 goto unlock;
2008 }
2009
2010 if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002011 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2012 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002013 goto unlock;
2014 }
2015
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002016 hdev->major_class = cp->major;
2017 hdev->minor_class = cp->minor;
2018
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002019 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002020 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2021 hdev->dev_class, 3);
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002022 goto unlock;
2023 }
2024
Johan Hedberg890ea892013-03-15 17:06:52 -05002025 hci_req_init(&req, hdev);
2026
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002027 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg7d785252011-12-15 00:47:39 +02002028 hci_dev_unlock(hdev);
2029 cancel_delayed_work_sync(&hdev->service_cache);
2030 hci_dev_lock(hdev);
Johan Hedbergb1a89172015-11-25 16:15:42 +02002031 __hci_req_update_eir(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02002032 }
Johan Hedberg14c0b602011-12-15 00:47:37 +02002033
Johan Hedberg14bf5ea2015-11-22 19:00:22 +02002034 __hci_req_update_class(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05002035
Johan Hedberg92da6092013-03-15 17:06:55 -05002036 err = hci_req_run(&req, set_class_complete);
2037 if (err < 0) {
2038 if (err != -ENODATA)
2039 goto unlock;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002040
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002041 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2042 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002043 goto unlock;
2044 }
2045
2046 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002047 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002048 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002049 goto unlock;
2050 }
2051
2052 err = 0;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002053
Johan Hedbergb5235a62012-02-21 14:32:24 +02002054unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002055 hci_dev_unlock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002056 return err;
2057}
2058
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002059static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002060 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002061{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002062 struct mgmt_cp_load_link_keys *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03002063 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
2064 sizeof(struct mgmt_link_key_info));
Szymon Janc4e51eae2011-02-25 19:05:48 +01002065 u16 key_count, expected_len;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002066 bool changed;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002067 int i;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002068
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002069 BT_DBG("request for %s", hdev->name);
2070
2071 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002072 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2073 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002074
Marcel Holtmann1f350c82012-03-12 20:31:08 -07002075 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002076 if (key_count > max_key_count) {
2077 BT_ERR("load_link_keys: too big key_count value %u",
2078 key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02002079 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2080 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002081 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002082
Johan Hedberg86742e12011-11-07 23:13:38 +02002083 expected_len = sizeof(*cp) + key_count *
2084 sizeof(struct mgmt_link_key_info);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002085 if (expected_len != len) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002086 BT_ERR("load_link_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02002087 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02002088 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2089 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002090 }
2091
Johan Hedberg4ae14302013-01-20 14:27:13 +02002092 if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002093 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2094 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ae14302013-01-20 14:27:13 +02002095
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002096 BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002097 key_count);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002098
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002099 for (i = 0; i < key_count; i++) {
2100 struct mgmt_link_key_info *key = &cp->keys[i];
2101
Marcel Holtmann8e991132014-01-10 02:07:25 -08002102 if (key->addr.type != BDADDR_BREDR || key->type > 0x08)
Johan Hedberga69e8372015-03-06 21:08:53 +02002103 return mgmt_cmd_status(sk, hdev->id,
2104 MGMT_OP_LOAD_LINK_KEYS,
2105 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002106 }
2107
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002108 hci_dev_lock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002109
2110 hci_link_keys_clear(hdev);
2111
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002112 if (cp->debug_keys)
Marcel Holtmann238be782015-03-13 02:11:06 -07002113 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002114 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002115 changed = hci_dev_test_and_clear_flag(hdev,
2116 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002117
2118 if (changed)
2119 new_settings(hdev, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002120
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002121 for (i = 0; i < key_count; i++) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002122 struct mgmt_link_key_info *key = &cp->keys[i];
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002123
Johan Hedberg58e92932014-06-24 14:00:26 +03002124 /* Always ignore debug keys and require a new pairing if
2125 * the user wants to use them.
2126 */
2127 if (key->type == HCI_LK_DEBUG_COMBINATION)
2128 continue;
2129
Johan Hedberg7652ff62014-06-24 13:15:49 +03002130 hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val,
2131 key->type, key->pin_len, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002132 }
2133
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002134 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
Johan Hedberg0e5f8752011-11-11 16:18:54 +02002135
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002136 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002137
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002138 return 0;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002139}
2140
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002141static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002142 u8 addr_type, struct sock *skip_sk)
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002143{
2144 struct mgmt_ev_device_unpaired ev;
2145
2146 bacpy(&ev.addr.bdaddr, bdaddr);
2147 ev.addr.type = addr_type;
2148
2149 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002150 skip_sk);
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002151}
2152
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002153static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002154 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002155{
Johan Hedberg124f6e32012-02-09 13:50:12 +02002156 struct mgmt_cp_unpair_device *cp = data;
2157 struct mgmt_rp_unpair_device rp;
Johan Hedbergfc643612015-10-22 09:38:31 +03002158 struct hci_conn_params *params;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002159 struct mgmt_pending_cmd *cmd;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002160 struct hci_conn *conn;
Johan Hedbergec182f02015-10-21 18:03:03 +03002161 u8 addr_type;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002162 int err;
2163
Johan Hedberga8a1d192011-11-10 15:54:38 +02002164 memset(&rp, 0, sizeof(rp));
Johan Hedberg124f6e32012-02-09 13:50:12 +02002165 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2166 rp.addr.type = cp->addr.type;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002167
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002168 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002169 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2170 MGMT_STATUS_INVALID_PARAMS,
2171 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002172
Johan Hedberg118da702013-01-20 14:27:20 +02002173 if (cp->disconnect != 0x00 && cp->disconnect != 0x01)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002174 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2175 MGMT_STATUS_INVALID_PARAMS,
2176 &rp, sizeof(rp));
Johan Hedberg118da702013-01-20 14:27:20 +02002177
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002178 hci_dev_lock(hdev);
2179
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002180 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002181 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2182 MGMT_STATUS_NOT_POWERED, &rp,
2183 sizeof(rp));
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002184 goto unlock;
2185 }
2186
Johan Hedberge0b2b272014-02-18 17:14:31 +02002187 if (cp->addr.type == BDADDR_BREDR) {
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002188 /* If disconnection is requested, then look up the
2189 * connection. If the remote device is connected, it
2190 * will be later used to terminate the link.
2191 *
2192 * Setting it to NULL explicitly will cause no
2193 * termination of the link.
2194 */
2195 if (cp->disconnect)
2196 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2197 &cp->addr.bdaddr);
2198 else
2199 conn = NULL;
2200
Johan Hedberg124f6e32012-02-09 13:50:12 +02002201 err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
Johan Hedbergec182f02015-10-21 18:03:03 +03002202 if (err < 0) {
2203 err = mgmt_cmd_complete(sk, hdev->id,
2204 MGMT_OP_UNPAIR_DEVICE,
2205 MGMT_STATUS_NOT_PAIRED, &rp,
2206 sizeof(rp));
2207 goto unlock;
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002208 }
2209
Johan Hedbergec182f02015-10-21 18:03:03 +03002210 goto done;
Johan Hedberge0b2b272014-02-18 17:14:31 +02002211 }
Vinicius Costa Gomesb0dbfb42012-02-02 21:08:03 -03002212
Johan Hedbergec182f02015-10-21 18:03:03 +03002213 /* LE address type */
2214 addr_type = le_addr_type(cp->addr.type);
2215
2216 hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type);
2217
2218 err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002219 if (err < 0) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002220 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2221 MGMT_STATUS_NOT_PAIRED, &rp,
2222 sizeof(rp));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002223 goto unlock;
2224 }
2225
Johan Hedbergec182f02015-10-21 18:03:03 +03002226 conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type);
2227 if (!conn) {
2228 hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type);
2229 goto done;
2230 }
2231
Johan Hedbergc81d5552015-10-22 09:38:35 +03002232 /* Abort any ongoing SMP pairing */
2233 smp_cancel_pairing(conn);
2234
Johan Hedbergec182f02015-10-21 18:03:03 +03002235 /* Defer clearing up the connection parameters until closing to
2236 * give a chance of keeping them if a repairing happens.
2237 */
2238 set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
2239
Johan Hedbergfc643612015-10-22 09:38:31 +03002240 /* Disable auto-connection parameters if present */
2241 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type);
2242 if (params) {
2243 if (params->explicit_connect)
2244 params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
2245 else
2246 params->auto_connect = HCI_AUTO_CONN_DISABLED;
2247 }
2248
Johan Hedbergec182f02015-10-21 18:03:03 +03002249 /* If disconnection is not requested, then clear the connection
2250 * variable so that the link is not terminated.
2251 */
2252 if (!cp->disconnect)
2253 conn = NULL;
2254
2255done:
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002256 /* If the connection variable is set, then termination of the
2257 * link is requested.
2258 */
Johan Hedberga8a1d192011-11-10 15:54:38 +02002259 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002260 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
2261 &rp, sizeof(rp));
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002262 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002263 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002264 }
2265
Johan Hedberg124f6e32012-02-09 13:50:12 +02002266 cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002267 sizeof(*cp));
Johan Hedberga8a1d192011-11-10 15:54:38 +02002268 if (!cmd) {
2269 err = -ENOMEM;
2270 goto unlock;
2271 }
2272
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02002273 cmd->cmd_complete = addr_cmd_complete;
2274
Johan Hedberg89e0ccc2015-10-22 10:49:38 +03002275 err = hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002276 if (err < 0)
2277 mgmt_pending_remove(cmd);
2278
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002279unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002280 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002281 return err;
2282}
2283
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002284static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002285 u16 len)
Johan Hedberg8962ee72011-01-20 12:40:27 +02002286{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002287 struct mgmt_cp_disconnect *cp = data;
Johan Hedberg06a63b12013-01-20 14:27:21 +02002288 struct mgmt_rp_disconnect rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002289 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002290 struct hci_conn *conn;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002291 int err;
2292
2293 BT_DBG("");
2294
Johan Hedberg06a63b12013-01-20 14:27:21 +02002295 memset(&rp, 0, sizeof(rp));
2296 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2297 rp.addr.type = cp->addr.type;
2298
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002299 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002300 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2301 MGMT_STATUS_INVALID_PARAMS,
2302 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002303
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002304 hci_dev_lock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002305
2306 if (!test_bit(HCI_UP, &hdev->flags)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002307 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2308 MGMT_STATUS_NOT_POWERED, &rp,
2309 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002310 goto failed;
2311 }
2312
Johan Hedberg333ae952015-03-17 13:48:47 +02002313 if (pending_find(MGMT_OP_DISCONNECT, hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002314 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2315 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002316 goto failed;
2317 }
2318
Andre Guedes591f47f2012-04-24 21:02:49 -03002319 if (cp->addr.type == BDADDR_BREDR)
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03002320 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2321 &cp->addr.bdaddr);
Johan Hedberg88c3df12012-02-09 14:27:38 +02002322 else
Johan Hedberg9d4c1cc2015-10-21 18:03:01 +03002323 conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr,
2324 le_addr_type(cp->addr.type));
Vinicius Costa Gomes365227e2011-05-06 18:41:44 -03002325
Vishal Agarwalf9607272012-06-13 05:32:43 +05302326 if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002327 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2328 MGMT_STATUS_NOT_CONNECTED, &rp,
2329 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002330 goto failed;
2331 }
2332
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002333 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03002334 if (!cmd) {
2335 err = -ENOMEM;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002336 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03002337 }
Johan Hedberg8962ee72011-01-20 12:40:27 +02002338
Johan Hedbergf5818c22014-12-05 13:36:02 +02002339 cmd->cmd_complete = generic_cmd_complete;
2340
Johan Hedberge3f2f922014-08-18 20:33:33 +03002341 err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002342 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03002343 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002344
2345failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002346 hci_dev_unlock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002347 return err;
2348}
2349
Andre Guedes57c14772012-04-24 21:02:50 -03002350static u8 link_to_bdaddr(u8 link_type, u8 addr_type)
Johan Hedberg4c659c32011-11-07 23:13:39 +02002351{
2352 switch (link_type) {
2353 case LE_LINK:
Johan Hedberg48264f02011-11-09 13:58:58 +02002354 switch (addr_type) {
2355 case ADDR_LE_DEV_PUBLIC:
Andre Guedes591f47f2012-04-24 21:02:49 -03002356 return BDADDR_LE_PUBLIC;
Andre Guedes0ed09142012-04-03 08:46:54 -03002357
Johan Hedberg48264f02011-11-09 13:58:58 +02002358 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03002359 /* Fallback to LE Random address type */
Andre Guedes591f47f2012-04-24 21:02:49 -03002360 return BDADDR_LE_RANDOM;
Johan Hedberg48264f02011-11-09 13:58:58 +02002361 }
Andre Guedes0ed09142012-04-03 08:46:54 -03002362
Johan Hedberg4c659c32011-11-07 23:13:39 +02002363 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03002364 /* Fallback to BR/EDR type */
Andre Guedes591f47f2012-04-24 21:02:49 -03002365 return BDADDR_BREDR;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002366 }
2367}
2368
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002369static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
2370 u16 data_len)
Johan Hedberg2784eb42011-01-21 13:56:35 +02002371{
Johan Hedberg2784eb42011-01-21 13:56:35 +02002372 struct mgmt_rp_get_connections *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002373 struct hci_conn *c;
Johan Hedberga38528f2011-01-22 06:46:43 +02002374 size_t rp_len;
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002375 int err;
2376 u16 i;
Johan Hedberg2784eb42011-01-21 13:56:35 +02002377
2378 BT_DBG("");
2379
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002380 hci_dev_lock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002381
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002382 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002383 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
2384 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002385 goto unlock;
2386 }
2387
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002388 i = 0;
Johan Hedbergb644ba32012-01-17 21:48:47 +02002389 list_for_each_entry(c, &hdev->conn_hash.list, list) {
2390 if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002391 i++;
Johan Hedberg2784eb42011-01-21 13:56:35 +02002392 }
2393
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002394 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Andre Guedes92c4c202012-06-07 19:05:44 -03002395 rp = kmalloc(rp_len, GFP_KERNEL);
Johan Hedberga38528f2011-01-22 06:46:43 +02002396 if (!rp) {
Johan Hedberg2784eb42011-01-21 13:56:35 +02002397 err = -ENOMEM;
2398 goto unlock;
2399 }
2400
Johan Hedberg2784eb42011-01-21 13:56:35 +02002401 i = 0;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002402 list_for_each_entry(c, &hdev->conn_hash.list, list) {
Johan Hedbergb644ba32012-01-17 21:48:47 +02002403 if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
2404 continue;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002405 bacpy(&rp->addr[i].bdaddr, &c->dst);
Andre Guedes57c14772012-04-24 21:02:50 -03002406 rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type);
Andre Guedes0ed09142012-04-03 08:46:54 -03002407 if (c->type == SCO_LINK || c->type == ESCO_LINK)
Johan Hedberg4c659c32011-11-07 23:13:39 +02002408 continue;
2409 i++;
2410 }
2411
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02002412 rp->conn_count = cpu_to_le16(i);
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002413
Johan Hedberg4c659c32011-11-07 23:13:39 +02002414 /* Recalculate length in case of filtered SCO connections, etc */
2415 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Johan Hedberg2784eb42011-01-21 13:56:35 +02002416
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002417 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
2418 rp_len);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002419
Johan Hedberga38528f2011-01-22 06:46:43 +02002420 kfree(rp);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002421
2422unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002423 hci_dev_unlock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002424 return err;
2425}
2426
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002427static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002428 struct mgmt_cp_pin_code_neg_reply *cp)
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002429{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002430 struct mgmt_pending_cmd *cmd;
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002431 int err;
2432
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002433 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002434 sizeof(*cp));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002435 if (!cmd)
2436 return -ENOMEM;
2437
Johan Hedbergd8457692012-02-17 14:24:57 +02002438 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002439 sizeof(cp->addr.bdaddr), &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002440 if (err < 0)
2441 mgmt_pending_remove(cmd);
2442
2443 return err;
2444}
2445
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002446static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002447 u16 len)
Johan Hedberg980e1a52011-01-22 06:10:07 +02002448{
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002449 struct hci_conn *conn;
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002450 struct mgmt_cp_pin_code_reply *cp = data;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002451 struct hci_cp_pin_code_reply reply;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002452 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002453 int err;
2454
2455 BT_DBG("");
2456
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002457 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002458
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002459 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002460 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
2461 MGMT_STATUS_NOT_POWERED);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002462 goto failed;
2463 }
2464
Johan Hedbergd8457692012-02-17 14:24:57 +02002465 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002466 if (!conn) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002467 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
2468 MGMT_STATUS_NOT_CONNECTED);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002469 goto failed;
2470 }
2471
2472 if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) {
Johan Hedbergd8457692012-02-17 14:24:57 +02002473 struct mgmt_cp_pin_code_neg_reply ncp;
2474
2475 memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002476
2477 BT_ERR("PIN code is not 16 bytes long");
2478
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002479 err = send_pin_code_neg_reply(sk, hdev, &ncp);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002480 if (err >= 0)
Johan Hedberga69e8372015-03-06 21:08:53 +02002481 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
2482 MGMT_STATUS_INVALID_PARAMS);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002483
2484 goto failed;
2485 }
2486
Gustavo F. Padovan00abfe42012-03-01 00:37:10 -03002487 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03002488 if (!cmd) {
2489 err = -ENOMEM;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002490 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03002491 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02002492
Johan Hedberg7776d1d2014-12-05 13:36:03 +02002493 cmd->cmd_complete = addr_cmd_complete;
2494
Johan Hedbergd8457692012-02-17 14:24:57 +02002495 bacpy(&reply.bdaddr, &cp->addr.bdaddr);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002496 reply.pin_len = cp->pin_len;
Waldemar Rymarkiewicz24718ca2011-06-01 17:28:47 +02002497 memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code));
Johan Hedberg980e1a52011-01-22 06:10:07 +02002498
2499 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
2500 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03002501 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002502
2503failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002504 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002505 return err;
2506}
2507
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002508static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
2509 u16 len)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002510{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002511 struct mgmt_cp_set_io_capability *cp = data;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002512
2513 BT_DBG("");
2514
Johan Hedberg4ec86d42014-06-17 15:14:48 +03002515 if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002516 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY,
2517 MGMT_STATUS_INVALID_PARAMS, NULL, 0);
Johan Hedberg4ec86d42014-06-17 15:14:48 +03002518
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002519 hci_dev_lock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002520
2521 hdev->io_capability = cp->io_capability;
2522
2523 BT_DBG("%s IO capability set to 0x%02x", hdev->name,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002524 hdev->io_capability);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002525
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002526 hci_dev_unlock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002527
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002528 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0,
2529 NULL, 0);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002530}
2531
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002532static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn)
Johan Hedberge9a416b2011-02-19 12:05:56 -03002533{
2534 struct hci_dev *hdev = conn->hdev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002535 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002536
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002537 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
Johan Hedberge9a416b2011-02-19 12:05:56 -03002538 if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
2539 continue;
2540
Johan Hedberge9a416b2011-02-19 12:05:56 -03002541 if (cmd->user_data != conn)
2542 continue;
2543
2544 return cmd;
2545 }
2546
2547 return NULL;
2548}
2549
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002550static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberge9a416b2011-02-19 12:05:56 -03002551{
2552 struct mgmt_rp_pair_device rp;
2553 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9df74652014-12-19 22:26:03 +02002554 int err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002555
Johan Hedberg61b1a7f2014-03-20 12:54:16 +02002556 bacpy(&rp.addr.bdaddr, &conn->dst);
2557 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002558
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002559 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE,
2560 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03002561
2562 /* So we don't get further callbacks for this connection */
2563 conn->connect_cfm_cb = NULL;
2564 conn->security_cfm_cb = NULL;
2565 conn->disconn_cfm_cb = NULL;
2566
David Herrmann76a68ba2013-04-06 20:28:37 +02002567 hci_conn_drop(conn);
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002568
2569 /* The device is paired so there is no need to remove
2570 * its connection parameters anymore.
2571 */
2572 clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
Johan Hedberg15013ae2014-12-11 21:45:44 +02002573
2574 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02002575
2576 return err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002577}
2578
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002579void mgmt_smp_complete(struct hci_conn *conn, bool complete)
2580{
2581 u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002582 struct mgmt_pending_cmd *cmd;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002583
2584 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02002585 if (cmd) {
Johan Hedberg04ab2742014-12-05 13:36:04 +02002586 cmd->cmd_complete(cmd, status);
Johan Hedberga511b352014-12-11 21:45:45 +02002587 mgmt_pending_remove(cmd);
2588 }
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002589}
2590
Johan Hedberge9a416b2011-02-19 12:05:56 -03002591static void pairing_complete_cb(struct hci_conn *conn, u8 status)
2592{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002593 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002594
2595 BT_DBG("status %u", status);
2596
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002597 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02002598 if (!cmd) {
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002599 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02002600 return;
2601 }
2602
2603 cmd->cmd_complete(cmd, mgmt_status(status));
2604 mgmt_pending_remove(cmd);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002605}
2606
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002607static void le_pairing_complete_cb(struct hci_conn *conn, u8 status)
Vishal Agarwal4c47d732012-06-07 20:27:35 +05302608{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002609 struct mgmt_pending_cmd *cmd;
Vishal Agarwal4c47d732012-06-07 20:27:35 +05302610
2611 BT_DBG("status %u", status);
2612
2613 if (!status)
2614 return;
2615
2616 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02002617 if (!cmd) {
Vishal Agarwal4c47d732012-06-07 20:27:35 +05302618 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02002619 return;
2620 }
2621
2622 cmd->cmd_complete(cmd, mgmt_status(status));
2623 mgmt_pending_remove(cmd);
Vishal Agarwal4c47d732012-06-07 20:27:35 +05302624}
2625
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002626static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002627 u16 len)
Johan Hedberge9a416b2011-02-19 12:05:56 -03002628{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002629 struct mgmt_cp_pair_device *cp = data;
Johan Hedberg1425acb2011-11-11 00:07:35 +02002630 struct mgmt_rp_pair_device rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002631 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002632 u8 sec_level, auth_type;
2633 struct hci_conn *conn;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002634 int err;
2635
2636 BT_DBG("");
2637
Szymon Jancf950a30e2013-01-18 12:48:07 +01002638 memset(&rp, 0, sizeof(rp));
2639 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2640 rp.addr.type = cp->addr.type;
2641
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002642 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002643 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2644 MGMT_STATUS_INVALID_PARAMS,
2645 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002646
Johan Hedberg4ec86d42014-06-17 15:14:48 +03002647 if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002648 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2649 MGMT_STATUS_INVALID_PARAMS,
2650 &rp, sizeof(rp));
Johan Hedberg4ec86d42014-06-17 15:14:48 +03002651
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002652 hci_dev_lock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002653
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002654 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002655 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2656 MGMT_STATUS_NOT_POWERED, &rp,
2657 sizeof(rp));
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002658 goto unlock;
2659 }
2660
Johan Hedberg55e76b32015-03-10 22:34:40 +02002661 if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) {
2662 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2663 MGMT_STATUS_ALREADY_PAIRED, &rp,
2664 sizeof(rp));
2665 goto unlock;
2666 }
2667
Vinicius Costa Gomesc908df32011-09-02 14:51:22 -03002668 sec_level = BT_SECURITY_MEDIUM;
Mikel Astiz6fd6b912014-04-08 14:21:32 +02002669 auth_type = HCI_AT_DEDICATED_BONDING;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002670
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002671 if (cp->addr.type == BDADDR_BREDR) {
Andre Guedes04a6c582014-02-26 20:21:44 -03002672 conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
2673 auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002674 } else {
Johan Hedberg85813a72015-10-21 18:02:59 +03002675 u8 addr_type = le_addr_type(cp->addr.type);
Jakub Pawlowski5157b8a2015-10-16 10:07:54 +03002676 struct hci_conn_params *p;
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002677
Marcel Holtmann7c264b12014-06-30 12:34:40 +02002678 /* When pairing a new device, it is expected to remember
2679 * this device for future connections. Adding the connection
2680 * parameter information ahead of time allows tracking
2681 * of the slave preferred values and will speed up any
2682 * further connection establishment.
2683 *
2684 * If connection parameters already exist, then they
2685 * will be kept and this function does nothing.
2686 */
Jakub Pawlowski5157b8a2015-10-16 10:07:54 +03002687 p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type);
2688
2689 if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT)
2690 p->auto_connect = HCI_AUTO_CONN_DISABLED;
Marcel Holtmann7c264b12014-06-30 12:34:40 +02002691
Jakub Pawlowskifa142222015-08-07 20:22:56 +02002692 conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr,
2693 addr_type, sec_level,
Johan Hedberg0ad06aa2015-11-11 14:44:57 +02002694 HCI_LE_CONN_TIMEOUT);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002695 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03002696
Ville Tervo30e76272011-02-22 16:10:53 -03002697 if (IS_ERR(conn)) {
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02002698 int status;
2699
2700 if (PTR_ERR(conn) == -EBUSY)
2701 status = MGMT_STATUS_BUSY;
Lukasz Rymanowskifaa81032015-02-11 12:31:42 +01002702 else if (PTR_ERR(conn) == -EOPNOTSUPP)
2703 status = MGMT_STATUS_NOT_SUPPORTED;
2704 else if (PTR_ERR(conn) == -ECONNREFUSED)
2705 status = MGMT_STATUS_REJECTED;
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02002706 else
2707 status = MGMT_STATUS_CONNECT_FAILED;
2708
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002709 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2710 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03002711 goto unlock;
2712 }
2713
2714 if (conn->connect_cfm_cb) {
David Herrmann76a68ba2013-04-06 20:28:37 +02002715 hci_conn_drop(conn);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002716 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2717 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03002718 goto unlock;
2719 }
2720
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002721 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002722 if (!cmd) {
2723 err = -ENOMEM;
David Herrmann76a68ba2013-04-06 20:28:37 +02002724 hci_conn_drop(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002725 goto unlock;
2726 }
2727
Johan Hedberg04ab2742014-12-05 13:36:04 +02002728 cmd->cmd_complete = pairing_complete;
2729
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03002730 /* For LE, just connecting isn't a proof that the pairing finished */
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002731 if (cp->addr.type == BDADDR_BREDR) {
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03002732 conn->connect_cfm_cb = pairing_complete_cb;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002733 conn->security_cfm_cb = pairing_complete_cb;
2734 conn->disconn_cfm_cb = pairing_complete_cb;
2735 } else {
2736 conn->connect_cfm_cb = le_pairing_complete_cb;
2737 conn->security_cfm_cb = le_pairing_complete_cb;
2738 conn->disconn_cfm_cb = le_pairing_complete_cb;
2739 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03002740
Johan Hedberge9a416b2011-02-19 12:05:56 -03002741 conn->io_capability = cp->io_cap;
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03002742 cmd->user_data = hci_conn_get(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002743
Johan Hedberg6f78fd42014-07-30 08:35:48 +03002744 if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) &&
Johan Hedberga511b352014-12-11 21:45:45 +02002745 hci_conn_security(conn, sec_level, auth_type, true)) {
2746 cmd->cmd_complete(cmd, 0);
2747 mgmt_pending_remove(cmd);
2748 }
Johan Hedberge9a416b2011-02-19 12:05:56 -03002749
2750 err = 0;
2751
2752unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002753 hci_dev_unlock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002754 return err;
2755}
2756
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002757static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
2758 u16 len)
Johan Hedberg28424702012-02-02 04:02:29 +02002759{
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02002760 struct mgmt_addr_info *addr = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002761 struct mgmt_pending_cmd *cmd;
Johan Hedberg28424702012-02-02 04:02:29 +02002762 struct hci_conn *conn;
2763 int err;
2764
2765 BT_DBG("");
2766
Johan Hedberg28424702012-02-02 04:02:29 +02002767 hci_dev_lock(hdev);
2768
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002769 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002770 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
2771 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002772 goto unlock;
2773 }
2774
Johan Hedberg333ae952015-03-17 13:48:47 +02002775 cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02002776 if (!cmd) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002777 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
2778 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02002779 goto unlock;
2780 }
2781
2782 conn = cmd->user_data;
2783
2784 if (bacmp(&addr->bdaddr, &conn->dst) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002785 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
2786 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02002787 goto unlock;
2788 }
2789
Johan Hedberga511b352014-12-11 21:45:45 +02002790 cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED);
2791 mgmt_pending_remove(cmd);
Johan Hedberg28424702012-02-02 04:02:29 +02002792
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002793 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
2794 addr, sizeof(*addr));
Johan Hedberg28424702012-02-02 04:02:29 +02002795unlock:
2796 hci_dev_unlock(hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02002797 return err;
2798}
2799
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002800static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
Johan Hedberg1707c602013-03-15 17:07:15 -05002801 struct mgmt_addr_info *addr, u16 mgmt_op,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002802 u16 hci_op, __le32 passkey)
Johan Hedberga5c29682011-02-19 12:05:57 -03002803{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002804 struct mgmt_pending_cmd *cmd;
Brian Gix0df4c182011-11-16 13:53:13 -08002805 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03002806 int err;
2807
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002808 hci_dev_lock(hdev);
Johan Hedberg08ba5382011-03-16 14:29:34 +02002809
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002810 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002811 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
2812 MGMT_STATUS_NOT_POWERED, addr,
2813 sizeof(*addr));
Brian Gix0df4c182011-11-16 13:53:13 -08002814 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03002815 }
2816
Johan Hedberg1707c602013-03-15 17:07:15 -05002817 if (addr->type == BDADDR_BREDR)
2818 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr);
Johan Hedberg272d90d2012-02-09 15:26:12 +02002819 else
Johan Hedberg9d4c1cc2015-10-21 18:03:01 +03002820 conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr,
2821 le_addr_type(addr->type));
Brian Gix47c15e22011-11-16 13:53:14 -08002822
Johan Hedberg272d90d2012-02-09 15:26:12 +02002823 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002824 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
2825 MGMT_STATUS_NOT_CONNECTED, addr,
2826 sizeof(*addr));
Johan Hedberg272d90d2012-02-09 15:26:12 +02002827 goto done;
2828 }
2829
Johan Hedberg1707c602013-03-15 17:07:15 -05002830 if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
Brian Gix5fe57d92011-12-21 16:12:13 -08002831 err = smp_user_confirm_reply(conn, mgmt_op, passkey);
Brian Gix5fe57d92011-12-21 16:12:13 -08002832 if (!err)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002833 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
2834 MGMT_STATUS_SUCCESS, addr,
2835 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08002836 else
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002837 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
2838 MGMT_STATUS_FAILED, addr,
2839 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08002840
Brian Gix47c15e22011-11-16 13:53:14 -08002841 goto done;
2842 }
2843
Johan Hedberg1707c602013-03-15 17:07:15 -05002844 cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr));
Johan Hedberga5c29682011-02-19 12:05:57 -03002845 if (!cmd) {
2846 err = -ENOMEM;
Brian Gix0df4c182011-11-16 13:53:13 -08002847 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03002848 }
2849
Johan Hedberg7776d1d2014-12-05 13:36:03 +02002850 cmd->cmd_complete = addr_cmd_complete;
2851
Brian Gix0df4c182011-11-16 13:53:13 -08002852 /* Continue with pairing via HCI */
Brian Gix604086b2011-11-23 08:28:33 -08002853 if (hci_op == HCI_OP_USER_PASSKEY_REPLY) {
2854 struct hci_cp_user_passkey_reply cp;
2855
Johan Hedberg1707c602013-03-15 17:07:15 -05002856 bacpy(&cp.bdaddr, &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08002857 cp.passkey = passkey;
2858 err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
2859 } else
Johan Hedberg1707c602013-03-15 17:07:15 -05002860 err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr),
2861 &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08002862
Johan Hedberga664b5b2011-02-19 12:06:02 -03002863 if (err < 0)
2864 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03002865
Brian Gix0df4c182011-11-16 13:53:13 -08002866done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002867 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03002868 return err;
2869}
2870
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05302871static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
2872 void *data, u16 len)
2873{
2874 struct mgmt_cp_pin_code_neg_reply *cp = data;
2875
2876 BT_DBG("");
2877
Johan Hedberg1707c602013-03-15 17:07:15 -05002878 return user_pairing_resp(sk, hdev, &cp->addr,
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05302879 MGMT_OP_PIN_CODE_NEG_REPLY,
2880 HCI_OP_PIN_CODE_NEG_REPLY, 0);
2881}
2882
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002883static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
2884 u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08002885{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002886 struct mgmt_cp_user_confirm_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08002887
2888 BT_DBG("");
2889
2890 if (len != sizeof(*cp))
Johan Hedberga69e8372015-03-06 21:08:53 +02002891 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
2892 MGMT_STATUS_INVALID_PARAMS);
Brian Gix0df4c182011-11-16 13:53:13 -08002893
Johan Hedberg1707c602013-03-15 17:07:15 -05002894 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002895 MGMT_OP_USER_CONFIRM_REPLY,
2896 HCI_OP_USER_CONFIRM_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08002897}
2898
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002899static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002900 void *data, u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08002901{
Johan Hedbergc9c26592011-12-15 00:47:41 +02002902 struct mgmt_cp_user_confirm_neg_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08002903
2904 BT_DBG("");
2905
Johan Hedberg1707c602013-03-15 17:07:15 -05002906 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002907 MGMT_OP_USER_CONFIRM_NEG_REPLY,
2908 HCI_OP_USER_CONFIRM_NEG_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08002909}
2910
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002911static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
2912 u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08002913{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002914 struct mgmt_cp_user_passkey_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08002915
2916 BT_DBG("");
2917
Johan Hedberg1707c602013-03-15 17:07:15 -05002918 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002919 MGMT_OP_USER_PASSKEY_REPLY,
2920 HCI_OP_USER_PASSKEY_REPLY, cp->passkey);
Brian Gix604086b2011-11-23 08:28:33 -08002921}
2922
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002923static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002924 void *data, u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08002925{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002926 struct mgmt_cp_user_passkey_neg_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08002927
2928 BT_DBG("");
2929
Johan Hedberg1707c602013-03-15 17:07:15 -05002930 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002931 MGMT_OP_USER_PASSKEY_NEG_REPLY,
2932 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
Brian Gix604086b2011-11-23 08:28:33 -08002933}
2934
Marcel Holtmann1904a852015-01-11 13:50:44 -08002935static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg13928972013-03-15 17:07:00 -05002936{
2937 struct mgmt_cp_set_local_name *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002938 struct mgmt_pending_cmd *cmd;
Johan Hedberg13928972013-03-15 17:07:00 -05002939
2940 BT_DBG("status 0x%02x", status);
2941
2942 hci_dev_lock(hdev);
2943
Johan Hedberg333ae952015-03-17 13:48:47 +02002944 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05002945 if (!cmd)
2946 goto unlock;
2947
2948 cp = cmd->param;
2949
2950 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02002951 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
2952 mgmt_status(status));
Johan Hedberg13928972013-03-15 17:07:00 -05002953 else
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002954 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
2955 cp, sizeof(*cp));
Johan Hedberg13928972013-03-15 17:07:00 -05002956
2957 mgmt_pending_remove(cmd);
2958
2959unlock:
2960 hci_dev_unlock(hdev);
2961}
2962
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002963static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002964 u16 len)
Johan Hedbergb312b1612011-03-16 14:29:37 +02002965{
Johan Hedberg2b4bf392012-03-03 00:19:06 +02002966 struct mgmt_cp_set_local_name *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002967 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002968 struct hci_request req;
Johan Hedbergb312b1612011-03-16 14:29:37 +02002969 int err;
2970
2971 BT_DBG("");
2972
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002973 hci_dev_lock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02002974
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05002975 /* If the old values are the same as the new ones just return a
2976 * direct command complete event.
2977 */
2978 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
2979 !memcmp(hdev->short_name, cp->short_name,
2980 sizeof(hdev->short_name))) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002981 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
2982 data, len);
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05002983 goto failed;
2984 }
2985
Johan Hedberg2b4bf392012-03-03 00:19:06 +02002986 memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02002987
Johan Hedbergb5235a62012-02-21 14:32:24 +02002988 if (!hdev_is_powered(hdev)) {
Johan Hedberg2b4bf392012-03-03 00:19:06 +02002989 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02002990
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002991 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
2992 data, len);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02002993 if (err < 0)
2994 goto failed;
2995
Marcel Holtmannf6b77122015-03-14 19:28:05 -07002996 err = mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev,
2997 data, len, sk);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02002998
Johan Hedbergb5235a62012-02-21 14:32:24 +02002999 goto failed;
3000 }
3001
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003002 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003003 if (!cmd) {
3004 err = -ENOMEM;
3005 goto failed;
3006 }
3007
Johan Hedberg13928972013-03-15 17:07:00 -05003008 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3009
Johan Hedberg890ea892013-03-15 17:06:52 -05003010 hci_req_init(&req, hdev);
Johan Hedberg3f985052013-03-15 17:07:02 -05003011
3012 if (lmp_bredr_capable(hdev)) {
Johan Hedberg00cf5042015-11-25 16:15:41 +02003013 __hci_req_update_name(&req);
Johan Hedbergb1a89172015-11-25 16:15:42 +02003014 __hci_req_update_eir(&req);
Johan Hedberg3f985052013-03-15 17:07:02 -05003015 }
3016
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003017 /* The name is stored in the scan response data and so
3018 * no need to udpate the advertising data here.
3019 */
Johan Hedberg3f985052013-03-15 17:07:02 -05003020 if (lmp_le_capable(hdev))
Johan Hedbergcab054a2015-11-30 11:21:45 +02003021 __hci_req_update_scan_rsp_data(&req, hdev->cur_adv_instance);
Johan Hedberg3f985052013-03-15 17:07:02 -05003022
Johan Hedberg13928972013-03-15 17:07:00 -05003023 err = hci_req_run(&req, set_name_complete);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003024 if (err < 0)
3025 mgmt_pending_remove(cmd);
3026
3027failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003028 hci_dev_unlock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003029 return err;
3030}
3031
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003032static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status,
3033 u16 opcode, struct sk_buff *skb)
3034{
3035 struct mgmt_rp_read_local_oob_data mgmt_rp;
3036 size_t rp_size = sizeof(mgmt_rp);
3037 struct mgmt_pending_cmd *cmd;
3038
3039 BT_DBG("%s status %u", hdev->name, status);
3040
3041 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
3042 if (!cmd)
3043 return;
3044
3045 if (status || !skb) {
3046 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3047 status ? mgmt_status(status) : MGMT_STATUS_FAILED);
3048 goto remove;
3049 }
3050
3051 memset(&mgmt_rp, 0, sizeof(mgmt_rp));
3052
3053 if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
3054 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
3055
3056 if (skb->len < sizeof(*rp)) {
3057 mgmt_cmd_status(cmd->sk, hdev->id,
3058 MGMT_OP_READ_LOCAL_OOB_DATA,
3059 MGMT_STATUS_FAILED);
3060 goto remove;
3061 }
3062
3063 memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash));
3064 memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand));
3065
3066 rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256);
3067 } else {
3068 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
3069
3070 if (skb->len < sizeof(*rp)) {
3071 mgmt_cmd_status(cmd->sk, hdev->id,
3072 MGMT_OP_READ_LOCAL_OOB_DATA,
3073 MGMT_STATUS_FAILED);
3074 goto remove;
3075 }
3076
3077 memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192));
3078 memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192));
3079
3080 memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256));
3081 memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256));
3082 }
3083
3084 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3085 MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size);
3086
3087remove:
3088 mgmt_pending_remove(cmd);
3089}
3090
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003091static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003092 void *data, u16 data_len)
Szymon Jancc35938b2011-03-22 13:12:21 +01003093{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003094 struct mgmt_pending_cmd *cmd;
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003095 struct hci_request req;
Szymon Jancc35938b2011-03-22 13:12:21 +01003096 int err;
3097
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003098 BT_DBG("%s", hdev->name);
Szymon Jancc35938b2011-03-22 13:12:21 +01003099
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003100 hci_dev_lock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003101
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003102 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003103 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3104 MGMT_STATUS_NOT_POWERED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003105 goto unlock;
3106 }
3107
Andre Guedes9a1a1992012-07-24 15:03:48 -03003108 if (!lmp_ssp_capable(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003109 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3110 MGMT_STATUS_NOT_SUPPORTED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003111 goto unlock;
3112 }
3113
Johan Hedberg333ae952015-03-17 13:48:47 +02003114 if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003115 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3116 MGMT_STATUS_BUSY);
Szymon Jancc35938b2011-03-22 13:12:21 +01003117 goto unlock;
3118 }
3119
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003120 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
Szymon Jancc35938b2011-03-22 13:12:21 +01003121 if (!cmd) {
3122 err = -ENOMEM;
3123 goto unlock;
3124 }
3125
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003126 hci_req_init(&req, hdev);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08003127
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003128 if (bredr_sc_enabled(hdev))
3129 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
3130 else
3131 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
3132
3133 err = hci_req_run_skb(&req, read_local_oob_data_complete);
Szymon Jancc35938b2011-03-22 13:12:21 +01003134 if (err < 0)
3135 mgmt_pending_remove(cmd);
3136
3137unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003138 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003139 return err;
3140}
3141
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003142static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003143 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003144{
Johan Hedberg5d57e792015-01-23 10:10:38 +02003145 struct mgmt_addr_info *addr = data;
Szymon Janc2763eda2011-03-22 13:12:22 +01003146 int err;
3147
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003148 BT_DBG("%s ", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003149
Johan Hedberg5d57e792015-01-23 10:10:38 +02003150 if (!bdaddr_type_is_valid(addr->type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003151 return mgmt_cmd_complete(sk, hdev->id,
3152 MGMT_OP_ADD_REMOTE_OOB_DATA,
3153 MGMT_STATUS_INVALID_PARAMS,
3154 addr, sizeof(*addr));
Johan Hedberg5d57e792015-01-23 10:10:38 +02003155
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003156 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003157
Marcel Holtmannec109112014-01-10 02:07:30 -08003158 if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) {
3159 struct mgmt_cp_add_remote_oob_data *cp = data;
3160 u8 status;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003161
Johan Hedbergc19a4952014-11-17 20:52:19 +02003162 if (cp->addr.type != BDADDR_BREDR) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003163 err = mgmt_cmd_complete(sk, hdev->id,
3164 MGMT_OP_ADD_REMOTE_OOB_DATA,
3165 MGMT_STATUS_INVALID_PARAMS,
3166 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02003167 goto unlock;
3168 }
3169
Marcel Holtmannec109112014-01-10 02:07:30 -08003170 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg6928a922014-10-26 20:46:09 +01003171 cp->addr.type, cp->hash,
3172 cp->rand, NULL, NULL);
Marcel Holtmannec109112014-01-10 02:07:30 -08003173 if (err < 0)
3174 status = MGMT_STATUS_FAILED;
3175 else
3176 status = MGMT_STATUS_SUCCESS;
3177
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003178 err = mgmt_cmd_complete(sk, hdev->id,
3179 MGMT_OP_ADD_REMOTE_OOB_DATA, status,
3180 &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08003181 } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) {
3182 struct mgmt_cp_add_remote_oob_ext_data *cp = data;
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003183 u8 *rand192, *hash192, *rand256, *hash256;
Marcel Holtmannec109112014-01-10 02:07:30 -08003184 u8 status;
3185
Johan Hedberg86df9202014-10-26 20:52:27 +01003186 if (bdaddr_type_is_le(cp->addr.type)) {
Johan Hedbergd25b78e2015-01-27 12:55:52 +02003187 /* Enforce zero-valued 192-bit parameters as
3188 * long as legacy SMP OOB isn't implemented.
3189 */
3190 if (memcmp(cp->rand192, ZERO_KEY, 16) ||
3191 memcmp(cp->hash192, ZERO_KEY, 16)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003192 err = mgmt_cmd_complete(sk, hdev->id,
3193 MGMT_OP_ADD_REMOTE_OOB_DATA,
3194 MGMT_STATUS_INVALID_PARAMS,
3195 addr, sizeof(*addr));
Johan Hedbergd25b78e2015-01-27 12:55:52 +02003196 goto unlock;
3197 }
3198
Johan Hedberg86df9202014-10-26 20:52:27 +01003199 rand192 = NULL;
3200 hash192 = NULL;
3201 } else {
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003202 /* In case one of the P-192 values is set to zero,
3203 * then just disable OOB data for P-192.
3204 */
3205 if (!memcmp(cp->rand192, ZERO_KEY, 16) ||
3206 !memcmp(cp->hash192, ZERO_KEY, 16)) {
3207 rand192 = NULL;
3208 hash192 = NULL;
3209 } else {
3210 rand192 = cp->rand192;
3211 hash192 = cp->hash192;
3212 }
3213 }
3214
3215 /* In case one of the P-256 values is set to zero, then just
3216 * disable OOB data for P-256.
3217 */
3218 if (!memcmp(cp->rand256, ZERO_KEY, 16) ||
3219 !memcmp(cp->hash256, ZERO_KEY, 16)) {
3220 rand256 = NULL;
3221 hash256 = NULL;
3222 } else {
3223 rand256 = cp->rand256;
3224 hash256 = cp->hash256;
Johan Hedberg86df9202014-10-26 20:52:27 +01003225 }
3226
Johan Hedberg81328d52014-10-26 20:33:47 +01003227 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg86df9202014-10-26 20:52:27 +01003228 cp->addr.type, hash192, rand192,
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003229 hash256, rand256);
Marcel Holtmannec109112014-01-10 02:07:30 -08003230 if (err < 0)
3231 status = MGMT_STATUS_FAILED;
3232 else
3233 status = MGMT_STATUS_SUCCESS;
3234
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003235 err = mgmt_cmd_complete(sk, hdev->id,
3236 MGMT_OP_ADD_REMOTE_OOB_DATA,
3237 status, &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08003238 } else {
3239 BT_ERR("add_remote_oob_data: invalid length of %u bytes", len);
Johan Hedberga69e8372015-03-06 21:08:53 +02003240 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3241 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannec109112014-01-10 02:07:30 -08003242 }
Szymon Janc2763eda2011-03-22 13:12:22 +01003243
Johan Hedbergc19a4952014-11-17 20:52:19 +02003244unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003245 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003246 return err;
3247}
3248
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003249static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003250 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003251{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003252 struct mgmt_cp_remove_remote_oob_data *cp = data;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003253 u8 status;
Szymon Janc2763eda2011-03-22 13:12:22 +01003254 int err;
3255
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003256 BT_DBG("%s", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003257
Johan Hedbergc19a4952014-11-17 20:52:19 +02003258 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003259 return mgmt_cmd_complete(sk, hdev->id,
3260 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
3261 MGMT_STATUS_INVALID_PARAMS,
3262 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02003263
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003264 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003265
Johan Hedbergeedbd582014-11-15 09:34:23 +02003266 if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
3267 hci_remote_oob_data_clear(hdev);
3268 status = MGMT_STATUS_SUCCESS;
3269 goto done;
3270 }
3271
Johan Hedberg6928a922014-10-26 20:46:09 +01003272 err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type);
Szymon Janc2763eda2011-03-22 13:12:22 +01003273 if (err < 0)
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003274 status = MGMT_STATUS_INVALID_PARAMS;
Szymon Janc2763eda2011-03-22 13:12:22 +01003275 else
Szymon Janca6785be2012-12-13 15:11:21 +01003276 status = MGMT_STATUS_SUCCESS;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003277
Johan Hedbergeedbd582014-11-15 09:34:23 +02003278done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003279 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
3280 status, &cp->addr, sizeof(cp->addr));
Szymon Janc2763eda2011-03-22 13:12:22 +01003281
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003282 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003283 return err;
3284}
3285
Johan Hedberge68f0722015-11-11 08:30:30 +02003286void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status)
Andre Guedes7c307722013-04-30 15:29:28 -03003287{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003288 struct mgmt_pending_cmd *cmd;
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01003289
Andre Guedes7c307722013-04-30 15:29:28 -03003290 BT_DBG("status %d", status);
3291
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003292 hci_dev_lock(hdev);
3293
Johan Hedberg333ae952015-03-17 13:48:47 +02003294 cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003295 if (!cmd)
Johan Hedberg333ae952015-03-17 13:48:47 +02003296 cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003297
Johan Hedberg78b781c2016-01-05 13:19:32 +02003298 if (!cmd)
3299 cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev);
3300
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003301 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02003302 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003303 mgmt_pending_remove(cmd);
Andre Guedes7c307722013-04-30 15:29:28 -03003304 }
3305
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003306 hci_dev_unlock(hdev);
Andre Guedes7c307722013-04-30 15:29:28 -03003307}
3308
Johan Hedberg591752a2015-11-11 08:11:24 +02003309static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type,
3310 uint8_t *mgmt_status)
3311{
3312 switch (type) {
3313 case DISCOV_TYPE_LE:
3314 *mgmt_status = mgmt_le_support(hdev);
3315 if (*mgmt_status)
3316 return false;
3317 break;
3318 case DISCOV_TYPE_INTERLEAVED:
3319 *mgmt_status = mgmt_le_support(hdev);
3320 if (*mgmt_status)
3321 return false;
3322 /* Intentional fall-through */
3323 case DISCOV_TYPE_BREDR:
3324 *mgmt_status = mgmt_bredr_support(hdev);
3325 if (*mgmt_status)
3326 return false;
3327 break;
3328 default:
3329 *mgmt_status = MGMT_STATUS_INVALID_PARAMS;
3330 return false;
3331 }
3332
3333 return true;
3334}
3335
Johan Hedberg78b781c2016-01-05 13:19:32 +02003336static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev,
3337 u16 op, void *data, u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04003338{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003339 struct mgmt_cp_start_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003340 struct mgmt_pending_cmd *cmd;
Marcel Holtmann80190442014-12-04 11:36:36 +01003341 u8 status;
Johan Hedberg14a53662011-04-27 10:29:56 -04003342 int err;
3343
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003344 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04003345
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003346 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04003347
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003348 if (!hdev_is_powered(hdev)) {
Johan Hedberg78b781c2016-01-05 13:19:32 +02003349 err = mgmt_cmd_complete(sk, hdev->id, op,
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003350 MGMT_STATUS_NOT_POWERED,
3351 &cp->type, sizeof(cp->type));
Johan Hedbergbd2d1332011-11-07 23:13:37 +02003352 goto failed;
3353 }
3354
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01003355 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003356 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg78b781c2016-01-05 13:19:32 +02003357 err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY,
3358 &cp->type, sizeof(cp->type));
Andre Guedes642be6c2012-03-21 00:03:37 -03003359 goto failed;
3360 }
3361
Johan Hedberg591752a2015-11-11 08:11:24 +02003362 if (!discovery_type_is_valid(hdev, cp->type, &status)) {
Johan Hedberg78b781c2016-01-05 13:19:32 +02003363 err = mgmt_cmd_complete(sk, hdev->id, op, status,
3364 &cp->type, sizeof(cp->type));
Johan Hedberg591752a2015-11-11 08:11:24 +02003365 goto failed;
3366 }
3367
Marcel Holtmann22078802014-12-05 11:45:22 +01003368 /* Clear the discovery filter first to free any previously
3369 * allocated memory for the UUID list.
3370 */
3371 hci_discovery_filter_clear(hdev);
3372
Andre Guedes4aab14e2012-02-17 20:39:36 -03003373 hdev->discovery.type = cp->type;
Marcel Holtmannda25cf62014-12-05 13:03:35 +01003374 hdev->discovery.report_invalid_rssi = false;
Johan Hedberg78b781c2016-01-05 13:19:32 +02003375 if (op == MGMT_OP_START_LIMITED_DISCOVERY)
3376 hdev->discovery.limited = true;
3377 else
3378 hdev->discovery.limited = false;
Andre Guedes4aab14e2012-02-17 20:39:36 -03003379
Johan Hedberg78b781c2016-01-05 13:19:32 +02003380 cmd = mgmt_pending_add(sk, op, hdev, data, len);
Johan Hedberge68f0722015-11-11 08:30:30 +02003381 if (!cmd) {
3382 err = -ENOMEM;
Johan Hedberg04106752013-01-10 14:54:09 +02003383 goto failed;
Andre Guedesf39799f2012-02-17 20:39:35 -03003384 }
Andre Guedes3fd24152012-02-03 17:48:01 -03003385
Johan Hedberge68f0722015-11-11 08:30:30 +02003386 cmd->cmd_complete = generic_cmd_complete;
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01003387
3388 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
Johan Hedberge68f0722015-11-11 08:30:30 +02003389 queue_work(hdev->req_workqueue, &hdev->discov_update);
3390 err = 0;
Johan Hedberg14a53662011-04-27 10:29:56 -04003391
3392failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003393 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04003394 return err;
3395}
3396
Johan Hedberg78b781c2016-01-05 13:19:32 +02003397static int start_discovery(struct sock *sk, struct hci_dev *hdev,
3398 void *data, u16 len)
3399{
3400 return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY,
3401 data, len);
3402}
3403
3404static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev,
3405 void *data, u16 len)
3406{
3407 return start_discovery_internal(sk, hdev,
3408 MGMT_OP_START_LIMITED_DISCOVERY,
3409 data, len);
3410}
3411
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003412static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd,
3413 u8 status)
Andre Guedes1183fdc2013-04-30 15:29:35 -03003414{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003415 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
3416 cmd->param, 1);
Johan Hedberg2922a942014-12-05 13:36:06 +02003417}
3418
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003419static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
3420 void *data, u16 len)
3421{
3422 struct mgmt_cp_start_service_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003423 struct mgmt_pending_cmd *cmd;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003424 const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16);
3425 u16 uuid_count, expected_len;
3426 u8 status;
Andre Guedes1183fdc2013-04-30 15:29:35 -03003427 int err;
3428
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003429 BT_DBG("%s", hdev->name);
Andre Guedes1183fdc2013-04-30 15:29:35 -03003430
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003431 hci_dev_lock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03003432
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003433 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003434 err = mgmt_cmd_complete(sk, hdev->id,
3435 MGMT_OP_START_SERVICE_DISCOVERY,
3436 MGMT_STATUS_NOT_POWERED,
3437 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003438 goto failed;
3439 }
3440
3441 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003442 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003443 err = mgmt_cmd_complete(sk, hdev->id,
3444 MGMT_OP_START_SERVICE_DISCOVERY,
3445 MGMT_STATUS_BUSY, &cp->type,
3446 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003447 goto failed;
3448 }
3449
3450 uuid_count = __le16_to_cpu(cp->uuid_count);
3451 if (uuid_count > max_uuid_count) {
3452 BT_ERR("service_discovery: too big uuid_count value %u",
3453 uuid_count);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003454 err = mgmt_cmd_complete(sk, hdev->id,
3455 MGMT_OP_START_SERVICE_DISCOVERY,
3456 MGMT_STATUS_INVALID_PARAMS, &cp->type,
3457 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003458 goto failed;
3459 }
3460
3461 expected_len = sizeof(*cp) + uuid_count * 16;
3462 if (expected_len != len) {
3463 BT_ERR("service_discovery: expected %u bytes, got %u bytes",
3464 expected_len, len);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003465 err = mgmt_cmd_complete(sk, hdev->id,
3466 MGMT_OP_START_SERVICE_DISCOVERY,
3467 MGMT_STATUS_INVALID_PARAMS, &cp->type,
3468 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003469 goto failed;
3470 }
3471
Johan Hedberg591752a2015-11-11 08:11:24 +02003472 if (!discovery_type_is_valid(hdev, cp->type, &status)) {
3473 err = mgmt_cmd_complete(sk, hdev->id,
3474 MGMT_OP_START_SERVICE_DISCOVERY,
3475 status, &cp->type, sizeof(cp->type));
3476 goto failed;
3477 }
3478
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003479 cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY,
Johan Hedberg2922a942014-12-05 13:36:06 +02003480 hdev, data, len);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003481 if (!cmd) {
3482 err = -ENOMEM;
3483 goto failed;
3484 }
3485
Johan Hedberg2922a942014-12-05 13:36:06 +02003486 cmd->cmd_complete = service_discovery_cmd_complete;
3487
Marcel Holtmann22078802014-12-05 11:45:22 +01003488 /* Clear the discovery filter first to free any previously
3489 * allocated memory for the UUID list.
3490 */
3491 hci_discovery_filter_clear(hdev);
3492
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08003493 hdev->discovery.result_filtering = true;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003494 hdev->discovery.type = cp->type;
3495 hdev->discovery.rssi = cp->rssi;
3496 hdev->discovery.uuid_count = uuid_count;
3497
3498 if (uuid_count > 0) {
3499 hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16,
3500 GFP_KERNEL);
3501 if (!hdev->discovery.uuids) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003502 err = mgmt_cmd_complete(sk, hdev->id,
3503 MGMT_OP_START_SERVICE_DISCOVERY,
3504 MGMT_STATUS_FAILED,
3505 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003506 mgmt_pending_remove(cmd);
3507 goto failed;
3508 }
3509 }
3510
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003511 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
Johan Hedberge68f0722015-11-11 08:30:30 +02003512 queue_work(hdev->req_workqueue, &hdev->discov_update);
3513 err = 0;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003514
3515failed:
3516 hci_dev_unlock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03003517 return err;
3518}
3519
Johan Hedberg2154d3f2015-11-11 08:30:45 +02003520void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status)
Andre Guedes0e05bba2013-04-30 15:29:33 -03003521{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003522 struct mgmt_pending_cmd *cmd;
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003523
Andre Guedes0e05bba2013-04-30 15:29:33 -03003524 BT_DBG("status %d", status);
3525
3526 hci_dev_lock(hdev);
3527
Johan Hedberg333ae952015-03-17 13:48:47 +02003528 cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003529 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02003530 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003531 mgmt_pending_remove(cmd);
Andre Guedes0e05bba2013-04-30 15:29:33 -03003532 }
3533
Andre Guedes0e05bba2013-04-30 15:29:33 -03003534 hci_dev_unlock(hdev);
3535}
3536
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003537static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003538 u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04003539{
Johan Hedbergd9306502012-02-20 23:25:18 +02003540 struct mgmt_cp_stop_discovery *mgmt_cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003541 struct mgmt_pending_cmd *cmd;
Johan Hedberg14a53662011-04-27 10:29:56 -04003542 int err;
3543
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003544 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04003545
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003546 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04003547
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003548 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003549 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
3550 MGMT_STATUS_REJECTED, &mgmt_cp->type,
3551 sizeof(mgmt_cp->type));
Johan Hedbergd9306502012-02-20 23:25:18 +02003552 goto unlock;
3553 }
3554
3555 if (hdev->discovery.type != mgmt_cp->type) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003556 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
3557 MGMT_STATUS_INVALID_PARAMS,
3558 &mgmt_cp->type, sizeof(mgmt_cp->type));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003559 goto unlock;
Johan Hedbergff9ef572012-01-04 14:23:45 +02003560 }
3561
Johan Hedberg2922a942014-12-05 13:36:06 +02003562 cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len);
Johan Hedberg14a53662011-04-27 10:29:56 -04003563 if (!cmd) {
3564 err = -ENOMEM;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003565 goto unlock;
Johan Hedberg14a53662011-04-27 10:29:56 -04003566 }
3567
Johan Hedberg2922a942014-12-05 13:36:06 +02003568 cmd->cmd_complete = generic_cmd_complete;
3569
Johan Hedberg2154d3f2015-11-11 08:30:45 +02003570 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
3571 queue_work(hdev->req_workqueue, &hdev->discov_update);
3572 err = 0;
Johan Hedberg14a53662011-04-27 10:29:56 -04003573
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003574unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003575 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04003576 return err;
3577}
3578
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003579static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003580 u16 len)
Johan Hedberg561aafb2012-01-04 13:31:59 +02003581{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003582 struct mgmt_cp_confirm_name *cp = data;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003583 struct inquiry_entry *e;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003584 int err;
3585
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003586 BT_DBG("%s", hdev->name);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003587
Johan Hedberg561aafb2012-01-04 13:31:59 +02003588 hci_dev_lock(hdev);
3589
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003590 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003591 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
3592 MGMT_STATUS_FAILED, &cp->addr,
3593 sizeof(cp->addr));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003594 goto failed;
3595 }
3596
Johan Hedberga198e7b2012-02-17 14:27:06 +02003597 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003598 if (!e) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003599 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
3600 MGMT_STATUS_INVALID_PARAMS, &cp->addr,
3601 sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02003602 goto failed;
3603 }
3604
3605 if (cp->name_known) {
3606 e->name_state = NAME_KNOWN;
3607 list_del(&e->list);
3608 } else {
3609 e->name_state = NAME_NEEDED;
Johan Hedberga3d4e202012-01-09 00:53:02 +02003610 hci_inquiry_cache_update_resolve(hdev, e);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003611 }
3612
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003613 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0,
3614 &cp->addr, sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02003615
3616failed:
3617 hci_dev_unlock(hdev);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003618 return err;
3619}
3620
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003621static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003622 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03003623{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003624 struct mgmt_cp_block_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003625 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03003626 int err;
3627
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003628 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03003629
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003630 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003631 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
3632 MGMT_STATUS_INVALID_PARAMS,
3633 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003634
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003635 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03003636
Johan Hedbergdcc36c12014-07-09 12:59:13 +03003637 err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr,
3638 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03003639 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003640 status = MGMT_STATUS_FAILED;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03003641 goto done;
3642 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003643
Johan Hedberg2a8357f2014-07-01 22:09:47 +03003644 mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr),
3645 sk);
3646 status = MGMT_STATUS_SUCCESS;
3647
3648done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003649 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
3650 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03003651
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003652 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03003653
3654 return err;
3655}
3656
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003657static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003658 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03003659{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003660 struct mgmt_cp_unblock_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003661 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03003662 int err;
3663
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003664 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03003665
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003666 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003667 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
3668 MGMT_STATUS_INVALID_PARAMS,
3669 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003670
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003671 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03003672
Johan Hedbergdcc36c12014-07-09 12:59:13 +03003673 err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr,
3674 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03003675 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003676 status = MGMT_STATUS_INVALID_PARAMS;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03003677 goto done;
3678 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003679
Johan Hedberg2a8357f2014-07-01 22:09:47 +03003680 mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr),
3681 sk);
3682 status = MGMT_STATUS_SUCCESS;
3683
3684done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003685 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
3686 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03003687
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003688 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03003689
3690 return err;
3691}
3692
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003693static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
3694 u16 len)
3695{
3696 struct mgmt_cp_set_device_id *cp = data;
Johan Hedberg890ea892013-03-15 17:06:52 -05003697 struct hci_request req;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003698 int err;
Szymon Jancc72d4b82012-03-16 16:02:57 +01003699 __u16 source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003700
3701 BT_DBG("%s", hdev->name);
3702
Szymon Jancc72d4b82012-03-16 16:02:57 +01003703 source = __le16_to_cpu(cp->source);
3704
3705 if (source > 0x0002)
Johan Hedberga69e8372015-03-06 21:08:53 +02003706 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
3707 MGMT_STATUS_INVALID_PARAMS);
Szymon Jancc72d4b82012-03-16 16:02:57 +01003708
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003709 hci_dev_lock(hdev);
3710
Szymon Jancc72d4b82012-03-16 16:02:57 +01003711 hdev->devid_source = source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003712 hdev->devid_vendor = __le16_to_cpu(cp->vendor);
3713 hdev->devid_product = __le16_to_cpu(cp->product);
3714 hdev->devid_version = __le16_to_cpu(cp->version);
3715
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003716 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0,
3717 NULL, 0);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003718
Johan Hedberg890ea892013-03-15 17:06:52 -05003719 hci_req_init(&req, hdev);
Johan Hedbergb1a89172015-11-25 16:15:42 +02003720 __hci_req_update_eir(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05003721 hci_req_run(&req, NULL);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003722
3723 hci_dev_unlock(hdev);
3724
3725 return err;
3726}
3727
Arman Uguray24b4f382015-03-23 15:57:12 -07003728static void enable_advertising_instance(struct hci_dev *hdev, u8 status,
3729 u16 opcode)
3730{
3731 BT_DBG("status %d", status);
3732}
3733
Marcel Holtmann1904a852015-01-11 13:50:44 -08003734static void set_advertising_complete(struct hci_dev *hdev, u8 status,
3735 u16 opcode)
Johan Hedberg4375f102013-09-25 13:26:10 +03003736{
3737 struct cmd_lookup match = { NULL, hdev };
Arman Uguray24b4f382015-03-23 15:57:12 -07003738 struct hci_request req;
Florian Grandel7816b822015-06-18 03:16:45 +02003739 u8 instance;
3740 struct adv_info *adv_instance;
3741 int err;
Johan Hedberg4375f102013-09-25 13:26:10 +03003742
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05303743 hci_dev_lock(hdev);
3744
Johan Hedberg4375f102013-09-25 13:26:10 +03003745 if (status) {
3746 u8 mgmt_err = mgmt_status(status);
3747
3748 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev,
3749 cmd_status_rsp, &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05303750 goto unlock;
Johan Hedberg4375f102013-09-25 13:26:10 +03003751 }
3752
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003753 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Marcel Holtmanna1536da2015-03-13 02:11:01 -07003754 hci_dev_set_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03003755 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07003756 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03003757
Johan Hedberg4375f102013-09-25 13:26:10 +03003758 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
3759 &match);
3760
3761 new_settings(hdev, match.sk);
3762
3763 if (match.sk)
3764 sock_put(match.sk);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05303765
Arman Uguray24b4f382015-03-23 15:57:12 -07003766 /* If "Set Advertising" was just disabled and instance advertising was
Florian Grandel7816b822015-06-18 03:16:45 +02003767 * set up earlier, then re-enable multi-instance advertising.
Arman Uguray24b4f382015-03-23 15:57:12 -07003768 */
3769 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
Florian Grandel7816b822015-06-18 03:16:45 +02003770 list_empty(&hdev->adv_instances))
Arman Uguray24b4f382015-03-23 15:57:12 -07003771 goto unlock;
3772
Florian Grandel7816b822015-06-18 03:16:45 +02003773 instance = hdev->cur_adv_instance;
3774 if (!instance) {
3775 adv_instance = list_first_entry_or_null(&hdev->adv_instances,
3776 struct adv_info, list);
3777 if (!adv_instance)
3778 goto unlock;
3779
3780 instance = adv_instance->instance;
3781 }
3782
Arman Uguray24b4f382015-03-23 15:57:12 -07003783 hci_req_init(&req, hdev);
3784
Johan Hedbergf2252572015-11-18 12:49:20 +02003785 err = __hci_req_schedule_adv_instance(&req, instance, true);
Arman Uguray24b4f382015-03-23 15:57:12 -07003786
Florian Grandel7816b822015-06-18 03:16:45 +02003787 if (!err)
3788 err = hci_req_run(&req, enable_advertising_instance);
3789
3790 if (err)
Arman Uguray24b4f382015-03-23 15:57:12 -07003791 BT_ERR("Failed to re-configure advertising");
3792
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05303793unlock:
3794 hci_dev_unlock(hdev);
Johan Hedberg4375f102013-09-25 13:26:10 +03003795}
3796
Marcel Holtmann21b51872013-10-10 09:47:53 -07003797static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
3798 u16 len)
Johan Hedberg4375f102013-09-25 13:26:10 +03003799{
3800 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003801 struct mgmt_pending_cmd *cmd;
Johan Hedberg4375f102013-09-25 13:26:10 +03003802 struct hci_request req;
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07003803 u8 val, status;
Johan Hedberg4375f102013-09-25 13:26:10 +03003804 int err;
3805
3806 BT_DBG("request for %s", hdev->name);
3807
Johan Hedberge6fe7982013-10-02 15:45:22 +03003808 status = mgmt_le_support(hdev);
3809 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02003810 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
3811 status);
Johan Hedberg4375f102013-09-25 13:26:10 +03003812
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07003813 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02003814 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
3815 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4375f102013-09-25 13:26:10 +03003816
3817 hci_dev_lock(hdev);
3818
3819 val = !!cp->val;
Johan Hedberg4375f102013-09-25 13:26:10 +03003820
Johan Hedbergf74ca9b2013-10-08 15:52:18 +02003821 /* The following conditions are ones which mean that we should
3822 * not do any HCI communication but directly send a mgmt
3823 * response to user space (after toggling the flag if
3824 * necessary).
3825 */
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07003826 if (!hdev_is_powered(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003827 (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
3828 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) ||
Johan Hedberge8bb6b92014-07-08 15:07:53 +03003829 hci_conn_num(hdev, LE_LINK) > 0 ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003830 (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Johan Hedberge8bb6b92014-07-08 15:07:53 +03003831 hdev->le_scan_type == LE_SCAN_ACTIVE)) {
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07003832 bool changed;
Johan Hedberg4375f102013-09-25 13:26:10 +03003833
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07003834 if (cp->val) {
Johan Hedbergcab054a2015-11-30 11:21:45 +02003835 hdev->cur_adv_instance = 0x00;
Marcel Holtmann238be782015-03-13 02:11:06 -07003836 changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07003837 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07003838 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07003839 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07003840 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07003841 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07003842 changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07003843 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Johan Hedberg4375f102013-09-25 13:26:10 +03003844 }
3845
3846 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
3847 if (err < 0)
3848 goto unlock;
3849
3850 if (changed)
3851 err = new_settings(hdev, sk);
3852
3853 goto unlock;
3854 }
3855
Johan Hedberg333ae952015-03-17 13:48:47 +02003856 if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
3857 pending_find(MGMT_OP_SET_LE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003858 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
3859 MGMT_STATUS_BUSY);
Johan Hedberg4375f102013-09-25 13:26:10 +03003860 goto unlock;
3861 }
3862
3863 cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
3864 if (!cmd) {
3865 err = -ENOMEM;
3866 goto unlock;
3867 }
3868
3869 hci_req_init(&req, hdev);
3870
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07003871 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07003872 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07003873 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07003874 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07003875
Florian Grandel7816b822015-06-18 03:16:45 +02003876 cancel_adv_timeout(hdev);
3877
Arman Uguray24b4f382015-03-23 15:57:12 -07003878 if (val) {
Florian Grandel7816b822015-06-18 03:16:45 +02003879 /* Switch to instance "0" for the Set Advertising setting.
3880 * We cannot use update_[adv|scan_rsp]_data() here as the
3881 * HCI_ADVERTISING flag is not yet set.
3882 */
Johan Hedbergcab054a2015-11-30 11:21:45 +02003883 hdev->cur_adv_instance = 0x00;
Johan Hedbergf2252572015-11-18 12:49:20 +02003884 __hci_req_update_adv_data(&req, 0x00);
3885 __hci_req_update_scan_rsp_data(&req, 0x00);
3886 __hci_req_enable_advertising(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07003887 } else {
Johan Hedbergf2252572015-11-18 12:49:20 +02003888 __hci_req_disable_advertising(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07003889 }
Johan Hedberg4375f102013-09-25 13:26:10 +03003890
3891 err = hci_req_run(&req, set_advertising_complete);
3892 if (err < 0)
3893 mgmt_pending_remove(cmd);
3894
3895unlock:
3896 hci_dev_unlock(hdev);
3897 return err;
3898}
3899
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07003900static int set_static_address(struct sock *sk, struct hci_dev *hdev,
3901 void *data, u16 len)
3902{
3903 struct mgmt_cp_set_static_address *cp = data;
3904 int err;
3905
3906 BT_DBG("%s", hdev->name);
3907
Marcel Holtmann62af4442013-10-02 22:10:32 -07003908 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02003909 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
3910 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07003911
3912 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02003913 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
3914 MGMT_STATUS_REJECTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07003915
3916 if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
3917 if (!bacmp(&cp->bdaddr, BDADDR_NONE))
Johan Hedberga69e8372015-03-06 21:08:53 +02003918 return mgmt_cmd_status(sk, hdev->id,
3919 MGMT_OP_SET_STATIC_ADDRESS,
3920 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07003921
3922 /* Two most significant bits shall be set */
3923 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
Johan Hedberga69e8372015-03-06 21:08:53 +02003924 return mgmt_cmd_status(sk, hdev->id,
3925 MGMT_OP_SET_STATIC_ADDRESS,
3926 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07003927 }
3928
3929 hci_dev_lock(hdev);
3930
3931 bacpy(&hdev->static_addr, &cp->bdaddr);
3932
Marcel Holtmann93690c22015-03-06 10:11:21 -08003933 err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev);
3934 if (err < 0)
3935 goto unlock;
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07003936
Marcel Holtmann93690c22015-03-06 10:11:21 -08003937 err = new_settings(hdev, sk);
3938
3939unlock:
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07003940 hci_dev_unlock(hdev);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07003941 return err;
3942}
3943
Marcel Holtmann14b49b92013-10-11 08:23:20 -07003944static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
3945 void *data, u16 len)
3946{
3947 struct mgmt_cp_set_scan_params *cp = data;
3948 __u16 interval, window;
3949 int err;
3950
3951 BT_DBG("%s", hdev->name);
3952
3953 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02003954 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
3955 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07003956
3957 interval = __le16_to_cpu(cp->interval);
3958
3959 if (interval < 0x0004 || interval > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02003960 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
3961 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07003962
3963 window = __le16_to_cpu(cp->window);
3964
3965 if (window < 0x0004 || window > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02003966 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
3967 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07003968
Marcel Holtmann899e1072013-10-14 09:55:32 -07003969 if (window > interval)
Johan Hedberga69e8372015-03-06 21:08:53 +02003970 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
3971 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann899e1072013-10-14 09:55:32 -07003972
Marcel Holtmann14b49b92013-10-11 08:23:20 -07003973 hci_dev_lock(hdev);
3974
3975 hdev->le_scan_interval = interval;
3976 hdev->le_scan_window = window;
3977
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003978 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0,
3979 NULL, 0);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07003980
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03003981 /* If background scan is running, restart it so new parameters are
3982 * loaded.
3983 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003984 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03003985 hdev->discovery.state == DISCOVERY_STOPPED) {
3986 struct hci_request req;
3987
3988 hci_req_init(&req, hdev);
3989
3990 hci_req_add_le_scan_disable(&req);
3991 hci_req_add_le_passive_scan(&req);
3992
3993 hci_req_run(&req, NULL);
3994 }
3995
Marcel Holtmann14b49b92013-10-11 08:23:20 -07003996 hci_dev_unlock(hdev);
3997
3998 return err;
3999}
4000
Marcel Holtmann1904a852015-01-11 13:50:44 -08004001static void fast_connectable_complete(struct hci_dev *hdev, u8 status,
4002 u16 opcode)
Johan Hedberg33e38b32013-03-15 17:07:05 -05004003{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004004 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004005
4006 BT_DBG("status 0x%02x", status);
4007
4008 hci_dev_lock(hdev);
4009
Johan Hedberg333ae952015-03-17 13:48:47 +02004010 cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004011 if (!cmd)
4012 goto unlock;
4013
4014 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004015 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4016 mgmt_status(status));
Johan Hedberg33e38b32013-03-15 17:07:05 -05004017 } else {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004018 struct mgmt_mode *cp = cmd->param;
4019
4020 if (cp->val)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004021 hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004022 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004023 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004024
Johan Hedberg33e38b32013-03-15 17:07:05 -05004025 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
4026 new_settings(hdev, cmd->sk);
4027 }
4028
4029 mgmt_pending_remove(cmd);
4030
4031unlock:
4032 hci_dev_unlock(hdev);
4033}
4034
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004035static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004036 void *data, u16 len)
Antti Julkuf6422ec2011-06-22 13:11:56 +03004037{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004038 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004039 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004040 struct hci_request req;
Antti Julkuf6422ec2011-06-22 13:11:56 +03004041 int err;
4042
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004043 BT_DBG("%s", hdev->name);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004044
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004045 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
Johan Hedberg56f87902013-10-02 13:43:13 +03004046 hdev->hci_ver < BLUETOOTH_VER_1_2)
Johan Hedberga69e8372015-03-06 21:08:53 +02004047 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4048 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03004049
Johan Hedberga7e80f22013-01-09 16:05:19 +02004050 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02004051 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4052 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02004053
Antti Julkuf6422ec2011-06-22 13:11:56 +03004054 hci_dev_lock(hdev);
4055
Johan Hedberg333ae952015-03-17 13:48:47 +02004056 if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004057 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4058 MGMT_STATUS_BUSY);
Johan Hedberg05cbf292013-03-15 17:07:07 -05004059 goto unlock;
4060 }
4061
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004062 if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004063 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4064 hdev);
4065 goto unlock;
4066 }
4067
Johan Hedberg406ef2a2015-03-10 20:14:27 +02004068 if (!hdev_is_powered(hdev)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07004069 hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg406ef2a2015-03-10 20:14:27 +02004070 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4071 hdev);
4072 new_settings(hdev, sk);
4073 goto unlock;
4074 }
4075
Johan Hedberg33e38b32013-03-15 17:07:05 -05004076 cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev,
4077 data, len);
4078 if (!cmd) {
4079 err = -ENOMEM;
4080 goto unlock;
4081 }
4082
4083 hci_req_init(&req, hdev);
4084
Johan Hedbergbf943cb2015-11-25 16:15:43 +02004085 __hci_req_write_fast_connectable(&req, cp->val);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004086
4087 err = hci_req_run(&req, fast_connectable_complete);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004088 if (err < 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004089 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4090 MGMT_STATUS_FAILED);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004091 mgmt_pending_remove(cmd);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004092 }
4093
Johan Hedberg33e38b32013-03-15 17:07:05 -05004094unlock:
Antti Julkuf6422ec2011-06-22 13:11:56 +03004095 hci_dev_unlock(hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004096
Antti Julkuf6422ec2011-06-22 13:11:56 +03004097 return err;
4098}
4099
Marcel Holtmann1904a852015-01-11 13:50:44 -08004100static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg0663ca22013-10-02 13:43:14 +03004101{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004102 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03004103
4104 BT_DBG("status 0x%02x", status);
4105
4106 hci_dev_lock(hdev);
4107
Johan Hedberg333ae952015-03-17 13:48:47 +02004108 cmd = pending_find(MGMT_OP_SET_BREDR, hdev);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004109 if (!cmd)
4110 goto unlock;
4111
4112 if (status) {
4113 u8 mgmt_err = mgmt_status(status);
4114
4115 /* We need to restore the flag if related HCI commands
4116 * failed.
4117 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004118 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004119
Johan Hedberga69e8372015-03-06 21:08:53 +02004120 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004121 } else {
4122 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
4123 new_settings(hdev, cmd->sk);
4124 }
4125
4126 mgmt_pending_remove(cmd);
4127
4128unlock:
4129 hci_dev_unlock(hdev);
4130}
4131
4132static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
4133{
4134 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004135 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03004136 struct hci_request req;
4137 int err;
4138
4139 BT_DBG("request for %s", hdev->name);
4140
4141 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004142 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4143 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004144
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004145 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02004146 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4147 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004148
4149 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02004150 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4151 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004152
4153 hci_dev_lock(hdev);
4154
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004155 if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg0663ca22013-10-02 13:43:14 +03004156 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4157 goto unlock;
4158 }
4159
4160 if (!hdev_is_powered(hdev)) {
4161 if (!cp->val) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004162 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
4163 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
4164 hci_dev_clear_flag(hdev, HCI_LINK_SECURITY);
4165 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
4166 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004167 }
4168
Marcel Holtmannce05d602015-03-13 02:11:03 -07004169 hci_dev_change_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004170
4171 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4172 if (err < 0)
4173 goto unlock;
4174
4175 err = new_settings(hdev, sk);
4176 goto unlock;
4177 }
4178
4179 /* Reject disabling when powered on */
4180 if (!cp->val) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004181 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4182 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004183 goto unlock;
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08004184 } else {
4185 /* When configuring a dual-mode controller to operate
4186 * with LE only and using a static address, then switching
4187 * BR/EDR back on is not allowed.
4188 *
4189 * Dual-mode controllers shall operate with the public
4190 * address as its identity address for BR/EDR and LE. So
4191 * reject the attempt to create an invalid configuration.
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08004192 *
4193 * The same restrictions applies when secure connections
4194 * has been enabled. For BR/EDR this is a controller feature
4195 * while for LE it is a host stack feature. This means that
4196 * switching BR/EDR back on when secure connections has been
4197 * enabled is not a supported transaction.
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08004198 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004199 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08004200 (bacmp(&hdev->static_addr, BDADDR_ANY) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004201 hci_dev_test_flag(hdev, HCI_SC_ENABLED))) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004202 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4203 MGMT_STATUS_REJECTED);
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08004204 goto unlock;
4205 }
Johan Hedberg0663ca22013-10-02 13:43:14 +03004206 }
4207
Johan Hedberg333ae952015-03-17 13:48:47 +02004208 if (pending_find(MGMT_OP_SET_BREDR, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004209 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4210 MGMT_STATUS_BUSY);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004211 goto unlock;
4212 }
4213
4214 cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len);
4215 if (!cmd) {
4216 err = -ENOMEM;
4217 goto unlock;
4218 }
4219
Johan Hedbergf2252572015-11-18 12:49:20 +02004220 /* We need to flip the bit already here so that
4221 * hci_req_update_adv_data generates the correct flags.
Johan Hedberg0663ca22013-10-02 13:43:14 +03004222 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004223 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004224
4225 hci_req_init(&req, hdev);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004226
Johan Hedbergbf943cb2015-11-25 16:15:43 +02004227 __hci_req_write_fast_connectable(&req, false);
Johan Hedberg01b1cb82015-11-16 12:52:21 +02004228 __hci_req_update_scan(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004229
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07004230 /* Since only the advertising data flags will change, there
4231 * is no need to update the scan response data.
4232 */
Johan Hedbergcab054a2015-11-30 11:21:45 +02004233 __hci_req_update_adv_data(&req, hdev->cur_adv_instance);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004234
Johan Hedberg0663ca22013-10-02 13:43:14 +03004235 err = hci_req_run(&req, set_bredr_complete);
4236 if (err < 0)
4237 mgmt_pending_remove(cmd);
4238
4239unlock:
4240 hci_dev_unlock(hdev);
4241 return err;
4242}
4243
Johan Hedberga1443f52015-01-23 15:42:46 +02004244static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
4245{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004246 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02004247 struct mgmt_mode *cp;
4248
4249 BT_DBG("%s status %u", hdev->name, status);
4250
4251 hci_dev_lock(hdev);
4252
Johan Hedberg333ae952015-03-17 13:48:47 +02004253 cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev);
Johan Hedberga1443f52015-01-23 15:42:46 +02004254 if (!cmd)
4255 goto unlock;
4256
4257 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004258 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
4259 mgmt_status(status));
Johan Hedberga1443f52015-01-23 15:42:46 +02004260 goto remove;
4261 }
4262
4263 cp = cmd->param;
4264
4265 switch (cp->val) {
4266 case 0x00:
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004267 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
4268 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02004269 break;
4270 case 0x01:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004271 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004272 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02004273 break;
4274 case 0x02:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004275 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
4276 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02004277 break;
4278 }
4279
4280 send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev);
4281 new_settings(hdev, cmd->sk);
4282
4283remove:
4284 mgmt_pending_remove(cmd);
4285unlock:
4286 hci_dev_unlock(hdev);
4287}
4288
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004289static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
4290 void *data, u16 len)
4291{
4292 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004293 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02004294 struct hci_request req;
Johan Hedberga3209692014-05-26 11:23:35 +03004295 u8 val;
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004296 int err;
4297
4298 BT_DBG("request for %s", hdev->name);
4299
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08004300 if (!lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004301 !hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02004302 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4303 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004304
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004305 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Johan Hedberg59200282015-01-28 19:56:00 +02004306 lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004307 !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02004308 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4309 MGMT_STATUS_REJECTED);
Marcel Holtmanned93ec62015-01-22 11:15:22 -08004310
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004311 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02004312 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004313 MGMT_STATUS_INVALID_PARAMS);
4314
4315 hci_dev_lock(hdev);
4316
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08004317 if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004318 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004319 bool changed;
4320
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004321 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07004322 changed = !hci_dev_test_and_set_flag(hdev,
4323 HCI_SC_ENABLED);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004324 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004325 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004326 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004327 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004328 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07004329 changed = hci_dev_test_and_clear_flag(hdev,
4330 HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004331 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004332 }
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004333
4334 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
4335 if (err < 0)
4336 goto failed;
4337
4338 if (changed)
4339 err = new_settings(hdev, sk);
4340
4341 goto failed;
4342 }
4343
Johan Hedberg333ae952015-03-17 13:48:47 +02004344 if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004345 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4346 MGMT_STATUS_BUSY);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004347 goto failed;
4348 }
4349
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004350 val = !!cp->val;
4351
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004352 if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
4353 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004354 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
4355 goto failed;
4356 }
4357
4358 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len);
4359 if (!cmd) {
4360 err = -ENOMEM;
4361 goto failed;
4362 }
4363
Johan Hedberga1443f52015-01-23 15:42:46 +02004364 hci_req_init(&req, hdev);
4365 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val);
4366 err = hci_req_run(&req, sc_enable_complete);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004367 if (err < 0) {
4368 mgmt_pending_remove(cmd);
4369 goto failed;
4370 }
4371
4372failed:
4373 hci_dev_unlock(hdev);
4374 return err;
4375}
4376
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004377static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
4378 void *data, u16 len)
4379{
4380 struct mgmt_mode *cp = data;
Johan Hedbergb97109792014-06-24 14:00:28 +03004381 bool changed, use_changed;
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004382 int err;
4383
4384 BT_DBG("request for %s", hdev->name);
4385
Johan Hedbergb97109792014-06-24 14:00:28 +03004386 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02004387 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
4388 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004389
4390 hci_dev_lock(hdev);
4391
4392 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07004393 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004394 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07004395 changed = hci_dev_test_and_clear_flag(hdev,
4396 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004397
Johan Hedbergb97109792014-06-24 14:00:28 +03004398 if (cp->val == 0x02)
Marcel Holtmann238be782015-03-13 02:11:06 -07004399 use_changed = !hci_dev_test_and_set_flag(hdev,
4400 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03004401 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07004402 use_changed = hci_dev_test_and_clear_flag(hdev,
4403 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03004404
4405 if (hdev_is_powered(hdev) && use_changed &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004406 hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergb97109792014-06-24 14:00:28 +03004407 u8 mode = (cp->val == 0x02) ? 0x01 : 0x00;
4408 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
4409 sizeof(mode), &mode);
4410 }
4411
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004412 err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev);
4413 if (err < 0)
4414 goto unlock;
4415
4416 if (changed)
4417 err = new_settings(hdev, sk);
4418
4419unlock:
4420 hci_dev_unlock(hdev);
4421 return err;
4422}
4423
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004424static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
4425 u16 len)
4426{
4427 struct mgmt_cp_set_privacy *cp = cp_data;
4428 bool changed;
4429 int err;
4430
4431 BT_DBG("request for %s", hdev->name);
4432
4433 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004434 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
4435 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004436
Johan Hedberg82a37ad2016-03-09 17:30:34 +02004437 if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02004438 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
4439 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004440
4441 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004442 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
4443 MGMT_STATUS_REJECTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004444
4445 hci_dev_lock(hdev);
4446
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02004447 /* If user space supports this command it is also expected to
4448 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
4449 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004450 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02004451
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004452 if (cp->privacy) {
Marcel Holtmann238be782015-03-13 02:11:06 -07004453 changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004454 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004455 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedberg82a37ad2016-03-09 17:30:34 +02004456 if (cp->privacy == 0x02)
4457 hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY);
4458 else
4459 hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004460 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07004461 changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004462 memset(hdev->irk, 0, sizeof(hdev->irk));
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004463 hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedberg82a37ad2016-03-09 17:30:34 +02004464 hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004465 }
4466
4467 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
4468 if (err < 0)
4469 goto unlock;
4470
4471 if (changed)
4472 err = new_settings(hdev, sk);
4473
4474unlock:
4475 hci_dev_unlock(hdev);
4476 return err;
4477}
4478
Johan Hedberg41edf162014-02-18 10:19:35 +02004479static bool irk_is_valid(struct mgmt_irk_info *irk)
4480{
4481 switch (irk->addr.type) {
4482 case BDADDR_LE_PUBLIC:
4483 return true;
4484
4485 case BDADDR_LE_RANDOM:
4486 /* Two most significant bits shall be set */
4487 if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0)
4488 return false;
4489 return true;
4490 }
4491
4492 return false;
4493}
4494
4495static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
4496 u16 len)
4497{
4498 struct mgmt_cp_load_irks *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03004499 const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) /
4500 sizeof(struct mgmt_irk_info));
Johan Hedberg41edf162014-02-18 10:19:35 +02004501 u16 irk_count, expected_len;
4502 int i, err;
4503
4504 BT_DBG("request for %s", hdev->name);
4505
4506 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004507 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
4508 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg41edf162014-02-18 10:19:35 +02004509
4510 irk_count = __le16_to_cpu(cp->irk_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03004511 if (irk_count > max_irk_count) {
4512 BT_ERR("load_irks: too big irk_count value %u", irk_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02004513 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
4514 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03004515 }
Johan Hedberg41edf162014-02-18 10:19:35 +02004516
4517 expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info);
4518 if (expected_len != len) {
4519 BT_ERR("load_irks: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02004520 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02004521 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
4522 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02004523 }
4524
4525 BT_DBG("%s irk_count %u", hdev->name, irk_count);
4526
4527 for (i = 0; i < irk_count; i++) {
4528 struct mgmt_irk_info *key = &cp->irks[i];
4529
4530 if (!irk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02004531 return mgmt_cmd_status(sk, hdev->id,
4532 MGMT_OP_LOAD_IRKS,
4533 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02004534 }
4535
4536 hci_dev_lock(hdev);
4537
4538 hci_smp_irks_clear(hdev);
4539
4540 for (i = 0; i < irk_count; i++) {
4541 struct mgmt_irk_info *irk = &cp->irks[i];
Johan Hedberg41edf162014-02-18 10:19:35 +02004542
Johan Hedberg85813a72015-10-21 18:02:59 +03004543 hci_add_irk(hdev, &irk->addr.bdaddr,
4544 le_addr_type(irk->addr.type), irk->val,
Johan Hedberg41edf162014-02-18 10:19:35 +02004545 BDADDR_ANY);
4546 }
4547
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004548 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedberg41edf162014-02-18 10:19:35 +02004549
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004550 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
Johan Hedberg41edf162014-02-18 10:19:35 +02004551
4552 hci_dev_unlock(hdev);
4553
4554 return err;
4555}
4556
Johan Hedberg3f706b72013-01-20 14:27:16 +02004557static bool ltk_is_valid(struct mgmt_ltk_info *key)
4558{
4559 if (key->master != 0x00 && key->master != 0x01)
4560 return false;
Marcel Holtmann490cb0b2014-02-16 12:59:05 -08004561
4562 switch (key->addr.type) {
4563 case BDADDR_LE_PUBLIC:
4564 return true;
4565
4566 case BDADDR_LE_RANDOM:
4567 /* Two most significant bits shall be set */
4568 if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0)
4569 return false;
4570 return true;
4571 }
4572
4573 return false;
Johan Hedberg3f706b72013-01-20 14:27:16 +02004574}
4575
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004576static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004577 void *cp_data, u16 len)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004578{
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004579 struct mgmt_cp_load_long_term_keys *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03004580 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
4581 sizeof(struct mgmt_ltk_info));
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004582 u16 key_count, expected_len;
Johan Hedberg715a5bf2013-01-09 15:29:34 +02004583 int i, err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004584
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07004585 BT_DBG("request for %s", hdev->name);
4586
4587 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004588 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
4589 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07004590
Marcel Holtmann1f350c82012-03-12 20:31:08 -07004591 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03004592 if (key_count > max_key_count) {
4593 BT_ERR("load_ltks: too big key_count value %u", key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02004594 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
4595 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03004596 }
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004597
4598 expected_len = sizeof(*cp) + key_count *
4599 sizeof(struct mgmt_ltk_info);
4600 if (expected_len != len) {
4601 BT_ERR("load_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02004602 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02004603 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
4604 MGMT_STATUS_INVALID_PARAMS);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004605 }
4606
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004607 BT_DBG("%s key_count %u", hdev->name, key_count);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004608
Johan Hedberg54ad6d82013-01-20 14:27:15 +02004609 for (i = 0; i < key_count; i++) {
4610 struct mgmt_ltk_info *key = &cp->keys[i];
4611
Johan Hedberg3f706b72013-01-20 14:27:16 +02004612 if (!ltk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02004613 return mgmt_cmd_status(sk, hdev->id,
4614 MGMT_OP_LOAD_LONG_TERM_KEYS,
4615 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg54ad6d82013-01-20 14:27:15 +02004616 }
4617
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004618 hci_dev_lock(hdev);
4619
4620 hci_smp_ltks_clear(hdev);
4621
4622 for (i = 0; i < key_count; i++) {
4623 struct mgmt_ltk_info *key = &cp->keys[i];
Johan Hedberg85813a72015-10-21 18:02:59 +03004624 u8 type, authenticated;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004625
Johan Hedberg61b43352014-05-29 19:36:53 +03004626 switch (key->type) {
4627 case MGMT_LTK_UNAUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03004628 authenticated = 0x00;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03004629 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03004630 break;
4631 case MGMT_LTK_AUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03004632 authenticated = 0x01;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03004633 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03004634 break;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03004635 case MGMT_LTK_P256_UNAUTH:
4636 authenticated = 0x00;
4637 type = SMP_LTK_P256;
4638 break;
4639 case MGMT_LTK_P256_AUTH:
4640 authenticated = 0x01;
4641 type = SMP_LTK_P256;
4642 break;
4643 case MGMT_LTK_P256_DEBUG:
4644 authenticated = 0x00;
4645 type = SMP_LTK_P256_DEBUG;
Johan Hedberg61b43352014-05-29 19:36:53 +03004646 default:
4647 continue;
4648 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03004649
Johan Hedberg85813a72015-10-21 18:02:59 +03004650 hci_add_ltk(hdev, &key->addr.bdaddr,
4651 le_addr_type(key->addr.type), type, authenticated,
4652 key->val, key->enc_size, key->ediv, key->rand);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004653 }
4654
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004655 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
Johan Hedberg715a5bf2013-01-09 15:29:34 +02004656 NULL, 0);
4657
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004658 hci_dev_unlock(hdev);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004659
Johan Hedberg715a5bf2013-01-09 15:29:34 +02004660 return err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004661}
4662
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004663static int conn_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004664{
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004665 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004666 struct mgmt_rp_get_conn_info rp;
Johan Hedberg9df74652014-12-19 22:26:03 +02004667 int err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004668
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004669 memcpy(&rp.addr, cmd->param, sizeof(rp.addr));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004670
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004671 if (status == MGMT_STATUS_SUCCESS) {
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004672 rp.rssi = conn->rssi;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004673 rp.tx_power = conn->tx_power;
4674 rp.max_tx_power = conn->max_tx_power;
4675 } else {
4676 rp.rssi = HCI_RSSI_INVALID;
4677 rp.tx_power = HCI_TX_POWER_INVALID;
4678 rp.max_tx_power = HCI_TX_POWER_INVALID;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004679 }
4680
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004681 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO,
4682 status, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004683
4684 hci_conn_drop(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03004685 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02004686
4687 return err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004688}
4689
Marcel Holtmann1904a852015-01-11 13:50:44 -08004690static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status,
4691 u16 opcode)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004692{
4693 struct hci_cp_read_rssi *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004694 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004695 struct hci_conn *conn;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004696 u16 handle;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004697 u8 status;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004698
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004699 BT_DBG("status 0x%02x", hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004700
4701 hci_dev_lock(hdev);
4702
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004703 /* Commands sent in request are either Read RSSI or Read Transmit Power
4704 * Level so we check which one was last sent to retrieve connection
4705 * handle. Both commands have handle as first parameter so it's safe to
4706 * cast data on the same command struct.
4707 *
4708 * First command sent is always Read RSSI and we fail only if it fails.
4709 * In other case we simply override error to indicate success as we
4710 * already remembered if TX power value is actually valid.
4711 */
4712 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI);
4713 if (!cp) {
4714 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004715 status = MGMT_STATUS_SUCCESS;
4716 } else {
4717 status = mgmt_status(hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004718 }
4719
4720 if (!cp) {
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004721 BT_ERR("invalid sent_cmd in conn_info response");
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004722 goto unlock;
4723 }
4724
4725 handle = __le16_to_cpu(cp->handle);
4726 conn = hci_conn_hash_lookup_handle(hdev, handle);
4727 if (!conn) {
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004728 BT_ERR("unknown handle (%d) in conn_info response", handle);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004729 goto unlock;
4730 }
4731
Johan Hedberg333ae952015-03-17 13:48:47 +02004732 cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004733 if (!cmd)
4734 goto unlock;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004735
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004736 cmd->cmd_complete(cmd, status);
4737 mgmt_pending_remove(cmd);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004738
4739unlock:
4740 hci_dev_unlock(hdev);
4741}
4742
4743static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
4744 u16 len)
4745{
4746 struct mgmt_cp_get_conn_info *cp = data;
4747 struct mgmt_rp_get_conn_info rp;
4748 struct hci_conn *conn;
4749 unsigned long conn_info_age;
4750 int err = 0;
4751
4752 BT_DBG("%s", hdev->name);
4753
4754 memset(&rp, 0, sizeof(rp));
4755 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
4756 rp.addr.type = cp->addr.type;
4757
4758 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004759 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
4760 MGMT_STATUS_INVALID_PARAMS,
4761 &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004762
4763 hci_dev_lock(hdev);
4764
4765 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004766 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
4767 MGMT_STATUS_NOT_POWERED, &rp,
4768 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004769 goto unlock;
4770 }
4771
4772 if (cp->addr.type == BDADDR_BREDR)
4773 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
4774 &cp->addr.bdaddr);
4775 else
4776 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
4777
4778 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004779 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
4780 MGMT_STATUS_NOT_CONNECTED, &rp,
4781 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004782 goto unlock;
4783 }
4784
Johan Hedberg333ae952015-03-17 13:48:47 +02004785 if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004786 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
4787 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004788 goto unlock;
4789 }
4790
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004791 /* To avoid client trying to guess when to poll again for information we
4792 * calculate conn info age as random value between min/max set in hdev.
4793 */
4794 conn_info_age = hdev->conn_info_min_age +
4795 prandom_u32_max(hdev->conn_info_max_age -
4796 hdev->conn_info_min_age);
4797
4798 /* Query controller to refresh cached values if they are too old or were
4799 * never read.
4800 */
Andrzej Kaczmarekf4e2dd52014-05-16 16:48:57 +02004801 if (time_after(jiffies, conn->conn_info_timestamp +
4802 msecs_to_jiffies(conn_info_age)) ||
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004803 !conn->conn_info_timestamp) {
4804 struct hci_request req;
4805 struct hci_cp_read_tx_power req_txp_cp;
4806 struct hci_cp_read_rssi req_rssi_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004807 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004808
4809 hci_req_init(&req, hdev);
4810 req_rssi_cp.handle = cpu_to_le16(conn->handle);
4811 hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp),
4812 &req_rssi_cp);
4813
Andrzej Kaczmarekf7faab02014-05-14 13:43:04 +02004814 /* For LE links TX power does not change thus we don't need to
4815 * query for it once value is known.
4816 */
4817 if (!bdaddr_type_is_le(cp->addr.type) ||
4818 conn->tx_power == HCI_TX_POWER_INVALID) {
4819 req_txp_cp.handle = cpu_to_le16(conn->handle);
4820 req_txp_cp.type = 0x00;
4821 hci_req_add(&req, HCI_OP_READ_TX_POWER,
4822 sizeof(req_txp_cp), &req_txp_cp);
4823 }
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004824
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02004825 /* Max TX power needs to be read only once per connection */
4826 if (conn->max_tx_power == HCI_TX_POWER_INVALID) {
4827 req_txp_cp.handle = cpu_to_le16(conn->handle);
4828 req_txp_cp.type = 0x01;
4829 hci_req_add(&req, HCI_OP_READ_TX_POWER,
4830 sizeof(req_txp_cp), &req_txp_cp);
4831 }
4832
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004833 err = hci_req_run(&req, conn_info_refresh_complete);
4834 if (err < 0)
4835 goto unlock;
4836
4837 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev,
4838 data, len);
4839 if (!cmd) {
4840 err = -ENOMEM;
4841 goto unlock;
4842 }
4843
4844 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03004845 cmd->user_data = hci_conn_get(conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02004846 cmd->cmd_complete = conn_info_cmd_complete;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004847
4848 conn->conn_info_timestamp = jiffies;
4849 } else {
4850 /* Cache is valid, just reply with values cached in hci_conn */
4851 rp.rssi = conn->rssi;
4852 rp.tx_power = conn->tx_power;
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02004853 rp.max_tx_power = conn->max_tx_power;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004854
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004855 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
4856 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004857 }
4858
4859unlock:
4860 hci_dev_unlock(hdev);
4861 return err;
4862}
4863
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004864static int clock_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg69487372014-12-05 13:36:07 +02004865{
4866 struct hci_conn *conn = cmd->user_data;
4867 struct mgmt_rp_get_clock_info rp;
4868 struct hci_dev *hdev;
Johan Hedberg9df74652014-12-19 22:26:03 +02004869 int err;
Johan Hedberg69487372014-12-05 13:36:07 +02004870
4871 memset(&rp, 0, sizeof(rp));
4872 memcpy(&rp.addr, &cmd->param, sizeof(rp.addr));
4873
4874 if (status)
4875 goto complete;
4876
4877 hdev = hci_dev_get(cmd->index);
4878 if (hdev) {
4879 rp.local_clock = cpu_to_le32(hdev->clock);
4880 hci_dev_put(hdev);
4881 }
4882
4883 if (conn) {
4884 rp.piconet_clock = cpu_to_le32(conn->clock);
4885 rp.accuracy = cpu_to_le16(conn->clock_accuracy);
4886 }
4887
4888complete:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004889 err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp,
4890 sizeof(rp));
Johan Hedberg69487372014-12-05 13:36:07 +02004891
4892 if (conn) {
4893 hci_conn_drop(conn);
4894 hci_conn_put(conn);
4895 }
Johan Hedberg9df74652014-12-19 22:26:03 +02004896
4897 return err;
Johan Hedberg69487372014-12-05 13:36:07 +02004898}
4899
Marcel Holtmann1904a852015-01-11 13:50:44 -08004900static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg95868422014-06-28 17:54:07 +03004901{
Johan Hedberg95868422014-06-28 17:54:07 +03004902 struct hci_cp_read_clock *hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004903 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03004904 struct hci_conn *conn;
4905
4906 BT_DBG("%s status %u", hdev->name, status);
4907
4908 hci_dev_lock(hdev);
4909
4910 hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
4911 if (!hci_cp)
4912 goto unlock;
4913
4914 if (hci_cp->which) {
4915 u16 handle = __le16_to_cpu(hci_cp->handle);
4916 conn = hci_conn_hash_lookup_handle(hdev, handle);
4917 } else {
4918 conn = NULL;
4919 }
4920
Johan Hedberg333ae952015-03-17 13:48:47 +02004921 cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn);
Johan Hedberg95868422014-06-28 17:54:07 +03004922 if (!cmd)
4923 goto unlock;
4924
Johan Hedberg69487372014-12-05 13:36:07 +02004925 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberg95868422014-06-28 17:54:07 +03004926 mgmt_pending_remove(cmd);
Johan Hedberg95868422014-06-28 17:54:07 +03004927
4928unlock:
4929 hci_dev_unlock(hdev);
4930}
4931
4932static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
4933 u16 len)
4934{
4935 struct mgmt_cp_get_clock_info *cp = data;
4936 struct mgmt_rp_get_clock_info rp;
4937 struct hci_cp_read_clock hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004938 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03004939 struct hci_request req;
4940 struct hci_conn *conn;
4941 int err;
4942
4943 BT_DBG("%s", hdev->name);
4944
4945 memset(&rp, 0, sizeof(rp));
4946 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
4947 rp.addr.type = cp->addr.type;
4948
4949 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004950 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
4951 MGMT_STATUS_INVALID_PARAMS,
4952 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03004953
4954 hci_dev_lock(hdev);
4955
4956 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004957 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
4958 MGMT_STATUS_NOT_POWERED, &rp,
4959 sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03004960 goto unlock;
4961 }
4962
4963 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
4964 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
4965 &cp->addr.bdaddr);
4966 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004967 err = mgmt_cmd_complete(sk, hdev->id,
4968 MGMT_OP_GET_CLOCK_INFO,
4969 MGMT_STATUS_NOT_CONNECTED,
4970 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03004971 goto unlock;
4972 }
4973 } else {
4974 conn = NULL;
4975 }
4976
4977 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len);
4978 if (!cmd) {
4979 err = -ENOMEM;
4980 goto unlock;
4981 }
4982
Johan Hedberg69487372014-12-05 13:36:07 +02004983 cmd->cmd_complete = clock_info_cmd_complete;
4984
Johan Hedberg95868422014-06-28 17:54:07 +03004985 hci_req_init(&req, hdev);
4986
4987 memset(&hci_cp, 0, sizeof(hci_cp));
4988 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
4989
4990 if (conn) {
4991 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03004992 cmd->user_data = hci_conn_get(conn);
Johan Hedberg95868422014-06-28 17:54:07 +03004993
4994 hci_cp.handle = cpu_to_le16(conn->handle);
4995 hci_cp.which = 0x01; /* Piconet clock */
4996 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
4997 }
4998
4999 err = hci_req_run(&req, get_clock_info_complete);
5000 if (err < 0)
5001 mgmt_pending_remove(cmd);
5002
5003unlock:
5004 hci_dev_unlock(hdev);
5005 return err;
5006}
5007
Johan Hedberg5a154e62014-12-19 22:26:02 +02005008static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
5009{
5010 struct hci_conn *conn;
5011
5012 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
5013 if (!conn)
5014 return false;
5015
5016 if (conn->dst_type != type)
5017 return false;
5018
5019 if (conn->state != BT_CONNECTED)
5020 return false;
5021
5022 return true;
5023}
5024
5025/* This function requires the caller holds hdev->lock */
Johan Hedberg51d7a942015-11-11 08:11:18 +02005026static int hci_conn_params_set(struct hci_dev *hdev, bdaddr_t *addr,
Johan Hedberg5a154e62014-12-19 22:26:02 +02005027 u8 addr_type, u8 auto_connect)
5028{
Johan Hedberg5a154e62014-12-19 22:26:02 +02005029 struct hci_conn_params *params;
5030
5031 params = hci_conn_params_add(hdev, addr, addr_type);
5032 if (!params)
5033 return -EIO;
5034
5035 if (params->auto_connect == auto_connect)
5036 return 0;
5037
5038 list_del_init(&params->action);
5039
5040 switch (auto_connect) {
5041 case HCI_AUTO_CONN_DISABLED:
5042 case HCI_AUTO_CONN_LINK_LOSS:
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005043 /* If auto connect is being disabled when we're trying to
5044 * connect to device, keep connecting.
5045 */
5046 if (params->explicit_connect)
5047 list_add(&params->action, &hdev->pend_le_conns);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005048 break;
5049 case HCI_AUTO_CONN_REPORT:
Johan Hedberg49c50922015-10-16 10:07:51 +03005050 if (params->explicit_connect)
5051 list_add(&params->action, &hdev->pend_le_conns);
5052 else
5053 list_add(&params->action, &hdev->pend_le_reports);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005054 break;
5055 case HCI_AUTO_CONN_DIRECT:
5056 case HCI_AUTO_CONN_ALWAYS:
Johan Hedberg51d7a942015-11-11 08:11:18 +02005057 if (!is_connected(hdev, addr, addr_type))
Johan Hedberg5a154e62014-12-19 22:26:02 +02005058 list_add(&params->action, &hdev->pend_le_conns);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005059 break;
5060 }
5061
5062 params->auto_connect = auto_connect;
5063
5064 BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
5065 auto_connect);
5066
5067 return 0;
5068}
5069
Marcel Holtmann8afef092014-06-29 22:28:34 +02005070static void device_added(struct sock *sk, struct hci_dev *hdev,
5071 bdaddr_t *bdaddr, u8 type, u8 action)
5072{
5073 struct mgmt_ev_device_added ev;
5074
5075 bacpy(&ev.addr.bdaddr, bdaddr);
5076 ev.addr.type = type;
5077 ev.action = action;
5078
5079 mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk);
5080}
5081
Marcel Holtmann2faade52014-06-29 19:44:03 +02005082static int add_device(struct sock *sk, struct hci_dev *hdev,
5083 void *data, u16 len)
5084{
5085 struct mgmt_cp_add_device *cp = data;
5086 u8 auto_conn, addr_type;
5087 int err;
5088
5089 BT_DBG("%s", hdev->name);
5090
Johan Hedberg66593582014-07-09 12:59:14 +03005091 if (!bdaddr_type_is_valid(cp->addr.type) ||
Marcel Holtmann2faade52014-06-29 19:44:03 +02005092 !bacmp(&cp->addr.bdaddr, BDADDR_ANY))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005093 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5094 MGMT_STATUS_INVALID_PARAMS,
5095 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005096
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005097 if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005098 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5099 MGMT_STATUS_INVALID_PARAMS,
5100 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005101
5102 hci_dev_lock(hdev);
5103
Johan Hedberg66593582014-07-09 12:59:14 +03005104 if (cp->addr.type == BDADDR_BREDR) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005105 /* Only incoming connections action is supported for now */
Johan Hedberg66593582014-07-09 12:59:14 +03005106 if (cp->action != 0x01) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005107 err = mgmt_cmd_complete(sk, hdev->id,
5108 MGMT_OP_ADD_DEVICE,
5109 MGMT_STATUS_INVALID_PARAMS,
5110 &cp->addr, sizeof(cp->addr));
Johan Hedberg66593582014-07-09 12:59:14 +03005111 goto unlock;
5112 }
5113
5114 err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr,
5115 cp->addr.type);
5116 if (err)
5117 goto unlock;
Johan Hedberga3974072014-07-09 12:59:15 +03005118
Johan Hedberg01b1cb82015-11-16 12:52:21 +02005119 hci_req_update_scan(hdev);
Johan Hedberga3974072014-07-09 12:59:15 +03005120
Johan Hedberg66593582014-07-09 12:59:14 +03005121 goto added;
5122 }
5123
Johan Hedberg85813a72015-10-21 18:02:59 +03005124 addr_type = le_addr_type(cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005125
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005126 if (cp->action == 0x02)
Marcel Holtmann2faade52014-06-29 19:44:03 +02005127 auto_conn = HCI_AUTO_CONN_ALWAYS;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005128 else if (cp->action == 0x01)
5129 auto_conn = HCI_AUTO_CONN_DIRECT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005130 else
Johan Hedberga3451d22014-07-02 17:37:27 +03005131 auto_conn = HCI_AUTO_CONN_REPORT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005132
Jakub Pawlowski9a0a8a82015-07-20 13:12:49 +02005133 /* Kernel internally uses conn_params with resolvable private
5134 * address, but Add Device allows only identity addresses.
5135 * Make sure it is enforced before calling
5136 * hci_conn_params_lookup.
5137 */
5138 if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005139 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5140 MGMT_STATUS_INVALID_PARAMS,
5141 &cp->addr, sizeof(cp->addr));
Jakub Pawlowski9a0a8a82015-07-20 13:12:49 +02005142 goto unlock;
5143 }
5144
Marcel Holtmannbf5b3c82014-06-30 12:34:39 +02005145 /* If the connection parameters don't exist for this device,
5146 * they will be created and configured with defaults.
5147 */
Johan Hedberg51d7a942015-11-11 08:11:18 +02005148 if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type,
Marcel Holtmannd06b50c2014-07-01 12:11:06 +02005149 auto_conn) < 0) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005150 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5151 MGMT_STATUS_FAILED, &cp->addr,
5152 sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005153 goto unlock;
5154 }
5155
Johan Hedberg51d7a942015-11-11 08:11:18 +02005156 hci_update_background_scan(hdev);
5157
Johan Hedberg66593582014-07-09 12:59:14 +03005158added:
Marcel Holtmann8afef092014-06-29 22:28:34 +02005159 device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
5160
Johan Hedberg51d7a942015-11-11 08:11:18 +02005161 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5162 MGMT_STATUS_SUCCESS, &cp->addr,
5163 sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005164
5165unlock:
5166 hci_dev_unlock(hdev);
5167 return err;
5168}
5169
Marcel Holtmann8afef092014-06-29 22:28:34 +02005170static void device_removed(struct sock *sk, struct hci_dev *hdev,
5171 bdaddr_t *bdaddr, u8 type)
5172{
5173 struct mgmt_ev_device_removed ev;
5174
5175 bacpy(&ev.addr.bdaddr, bdaddr);
5176 ev.addr.type = type;
5177
5178 mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk);
5179}
5180
Marcel Holtmann2faade52014-06-29 19:44:03 +02005181static int remove_device(struct sock *sk, struct hci_dev *hdev,
5182 void *data, u16 len)
5183{
5184 struct mgmt_cp_remove_device *cp = data;
5185 int err;
5186
5187 BT_DBG("%s", hdev->name);
5188
5189 hci_dev_lock(hdev);
5190
5191 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
Johan Hedbergc71593d2014-07-02 17:37:28 +03005192 struct hci_conn_params *params;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005193 u8 addr_type;
5194
Johan Hedberg66593582014-07-09 12:59:14 +03005195 if (!bdaddr_type_is_valid(cp->addr.type)) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005196 err = mgmt_cmd_complete(sk, hdev->id,
5197 MGMT_OP_REMOVE_DEVICE,
5198 MGMT_STATUS_INVALID_PARAMS,
5199 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005200 goto unlock;
5201 }
5202
Johan Hedberg66593582014-07-09 12:59:14 +03005203 if (cp->addr.type == BDADDR_BREDR) {
5204 err = hci_bdaddr_list_del(&hdev->whitelist,
5205 &cp->addr.bdaddr,
5206 cp->addr.type);
5207 if (err) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005208 err = mgmt_cmd_complete(sk, hdev->id,
5209 MGMT_OP_REMOVE_DEVICE,
5210 MGMT_STATUS_INVALID_PARAMS,
5211 &cp->addr,
5212 sizeof(cp->addr));
Johan Hedberg66593582014-07-09 12:59:14 +03005213 goto unlock;
5214 }
5215
Johan Hedberg01b1cb82015-11-16 12:52:21 +02005216 hci_req_update_scan(hdev);
Johan Hedberga3974072014-07-09 12:59:15 +03005217
Johan Hedberg66593582014-07-09 12:59:14 +03005218 device_removed(sk, hdev, &cp->addr.bdaddr,
5219 cp->addr.type);
5220 goto complete;
5221 }
5222
Johan Hedberg85813a72015-10-21 18:02:59 +03005223 addr_type = le_addr_type(cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005224
Jakub Pawlowski9a0a8a82015-07-20 13:12:49 +02005225 /* Kernel internally uses conn_params with resolvable private
5226 * address, but Remove Device allows only identity addresses.
5227 * Make sure it is enforced before calling
5228 * hci_conn_params_lookup.
5229 */
5230 if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005231 err = mgmt_cmd_complete(sk, hdev->id,
5232 MGMT_OP_REMOVE_DEVICE,
5233 MGMT_STATUS_INVALID_PARAMS,
5234 &cp->addr, sizeof(cp->addr));
Jakub Pawlowski9a0a8a82015-07-20 13:12:49 +02005235 goto unlock;
5236 }
5237
Johan Hedbergc71593d2014-07-02 17:37:28 +03005238 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
5239 addr_type);
5240 if (!params) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005241 err = mgmt_cmd_complete(sk, hdev->id,
5242 MGMT_OP_REMOVE_DEVICE,
5243 MGMT_STATUS_INVALID_PARAMS,
5244 &cp->addr, sizeof(cp->addr));
Johan Hedbergc71593d2014-07-02 17:37:28 +03005245 goto unlock;
5246 }
5247
Johan Hedberg679d2b62015-10-16 10:07:52 +03005248 if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
5249 params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005250 err = mgmt_cmd_complete(sk, hdev->id,
5251 MGMT_OP_REMOVE_DEVICE,
5252 MGMT_STATUS_INVALID_PARAMS,
5253 &cp->addr, sizeof(cp->addr));
Johan Hedbergc71593d2014-07-02 17:37:28 +03005254 goto unlock;
5255 }
5256
Johan Hedbergd1dbf122014-07-04 16:17:23 +03005257 list_del(&params->action);
Johan Hedbergc71593d2014-07-02 17:37:28 +03005258 list_del(&params->list);
5259 kfree(params);
Johan Hedberg51d7a942015-11-11 08:11:18 +02005260 hci_update_background_scan(hdev);
Marcel Holtmann8afef092014-06-29 22:28:34 +02005261
5262 device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005263 } else {
Johan Hedberg19de0822014-07-06 13:06:51 +03005264 struct hci_conn_params *p, *tmp;
Johan Hedberg66593582014-07-09 12:59:14 +03005265 struct bdaddr_list *b, *btmp;
Johan Hedberg19de0822014-07-06 13:06:51 +03005266
Marcel Holtmann2faade52014-06-29 19:44:03 +02005267 if (cp->addr.type) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005268 err = mgmt_cmd_complete(sk, hdev->id,
5269 MGMT_OP_REMOVE_DEVICE,
5270 MGMT_STATUS_INVALID_PARAMS,
5271 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005272 goto unlock;
5273 }
5274
Johan Hedberg66593582014-07-09 12:59:14 +03005275 list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) {
5276 device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type);
5277 list_del(&b->list);
5278 kfree(b);
5279 }
5280
Johan Hedberg01b1cb82015-11-16 12:52:21 +02005281 hci_req_update_scan(hdev);
Johan Hedberga3974072014-07-09 12:59:15 +03005282
Johan Hedberg19de0822014-07-06 13:06:51 +03005283 list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) {
5284 if (p->auto_connect == HCI_AUTO_CONN_DISABLED)
5285 continue;
5286 device_removed(sk, hdev, &p->addr, p->addr_type);
Johan Hedberg679d2b62015-10-16 10:07:52 +03005287 if (p->explicit_connect) {
5288 p->auto_connect = HCI_AUTO_CONN_EXPLICIT;
5289 continue;
5290 }
Johan Hedberg19de0822014-07-06 13:06:51 +03005291 list_del(&p->action);
5292 list_del(&p->list);
5293 kfree(p);
5294 }
5295
5296 BT_DBG("All LE connection parameters were removed");
5297
Johan Hedberg51d7a942015-11-11 08:11:18 +02005298 hci_update_background_scan(hdev);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005299 }
5300
Johan Hedberg66593582014-07-09 12:59:14 +03005301complete:
Johan Hedberg51d7a942015-11-11 08:11:18 +02005302 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE,
5303 MGMT_STATUS_SUCCESS, &cp->addr,
5304 sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005305unlock:
5306 hci_dev_unlock(hdev);
5307 return err;
5308}
5309
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005310static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
5311 u16 len)
5312{
5313 struct mgmt_cp_load_conn_param *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005314 const u16 max_param_count = ((U16_MAX - sizeof(*cp)) /
5315 sizeof(struct mgmt_conn_param));
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005316 u16 param_count, expected_len;
5317 int i;
5318
5319 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005320 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
5321 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005322
5323 param_count = __le16_to_cpu(cp->param_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005324 if (param_count > max_param_count) {
5325 BT_ERR("load_conn_param: too big param_count value %u",
5326 param_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02005327 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
5328 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005329 }
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005330
5331 expected_len = sizeof(*cp) + param_count *
5332 sizeof(struct mgmt_conn_param);
5333 if (expected_len != len) {
5334 BT_ERR("load_conn_param: expected %u bytes, got %u bytes",
5335 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02005336 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
5337 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005338 }
5339
5340 BT_DBG("%s param_count %u", hdev->name, param_count);
5341
5342 hci_dev_lock(hdev);
5343
5344 hci_conn_params_clear_disabled(hdev);
5345
5346 for (i = 0; i < param_count; i++) {
5347 struct mgmt_conn_param *param = &cp->params[i];
5348 struct hci_conn_params *hci_param;
5349 u16 min, max, latency, timeout;
5350 u8 addr_type;
5351
5352 BT_DBG("Adding %pMR (type %u)", &param->addr.bdaddr,
5353 param->addr.type);
5354
5355 if (param->addr.type == BDADDR_LE_PUBLIC) {
5356 addr_type = ADDR_LE_DEV_PUBLIC;
5357 } else if (param->addr.type == BDADDR_LE_RANDOM) {
5358 addr_type = ADDR_LE_DEV_RANDOM;
5359 } else {
5360 BT_ERR("Ignoring invalid connection parameters");
5361 continue;
5362 }
5363
5364 min = le16_to_cpu(param->min_interval);
5365 max = le16_to_cpu(param->max_interval);
5366 latency = le16_to_cpu(param->latency);
5367 timeout = le16_to_cpu(param->timeout);
5368
5369 BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x",
5370 min, max, latency, timeout);
5371
5372 if (hci_check_conn_params(min, max, latency, timeout) < 0) {
5373 BT_ERR("Ignoring invalid connection parameters");
5374 continue;
5375 }
5376
5377 hci_param = hci_conn_params_add(hdev, &param->addr.bdaddr,
5378 addr_type);
5379 if (!hci_param) {
5380 BT_ERR("Failed to add connection parameters");
5381 continue;
5382 }
5383
5384 hci_param->conn_min_interval = min;
5385 hci_param->conn_max_interval = max;
5386 hci_param->conn_latency = latency;
5387 hci_param->supervision_timeout = timeout;
5388 }
5389
5390 hci_dev_unlock(hdev);
5391
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005392 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0,
5393 NULL, 0);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005394}
5395
Marcel Holtmanndbece372014-07-04 18:11:55 +02005396static int set_external_config(struct sock *sk, struct hci_dev *hdev,
5397 void *data, u16 len)
5398{
5399 struct mgmt_cp_set_external_config *cp = data;
5400 bool changed;
5401 int err;
5402
5403 BT_DBG("%s", hdev->name);
5404
5405 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005406 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
5407 MGMT_STATUS_REJECTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02005408
5409 if (cp->config != 0x00 && cp->config != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02005410 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
5411 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmanndbece372014-07-04 18:11:55 +02005412
5413 if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
Johan Hedberga69e8372015-03-06 21:08:53 +02005414 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
5415 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02005416
5417 hci_dev_lock(hdev);
5418
5419 if (cp->config)
Marcel Holtmann238be782015-03-13 02:11:06 -07005420 changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02005421 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005422 changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02005423
5424 err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
5425 if (err < 0)
5426 goto unlock;
5427
5428 if (!changed)
5429 goto unlock;
5430
Marcel Holtmannf4537c02014-07-04 19:06:23 +02005431 err = new_options(hdev, sk);
5432
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005433 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) {
Marcel Holtmanndbece372014-07-04 18:11:55 +02005434 mgmt_index_removed(hdev);
Marcel Holtmannd603b762014-07-06 12:11:14 +02005435
Marcel Holtmann516018a2015-03-13 02:11:04 -07005436 if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005437 hci_dev_set_flag(hdev, HCI_CONFIG);
5438 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmannd603b762014-07-06 12:11:14 +02005439
5440 queue_work(hdev->req_workqueue, &hdev->power_on);
5441 } else {
Marcel Holtmann5ea234d2014-07-06 12:11:16 +02005442 set_bit(HCI_RAW, &hdev->flags);
Marcel Holtmannd603b762014-07-06 12:11:14 +02005443 mgmt_index_added(hdev);
5444 }
Marcel Holtmanndbece372014-07-04 18:11:55 +02005445 }
5446
5447unlock:
5448 hci_dev_unlock(hdev);
5449 return err;
5450}
5451
Marcel Holtmann9713c172014-07-06 12:11:15 +02005452static int set_public_address(struct sock *sk, struct hci_dev *hdev,
5453 void *data, u16 len)
5454{
5455 struct mgmt_cp_set_public_address *cp = data;
5456 bool changed;
5457 int err;
5458
5459 BT_DBG("%s", hdev->name);
5460
5461 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005462 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
5463 MGMT_STATUS_REJECTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02005464
5465 if (!bacmp(&cp->bdaddr, BDADDR_ANY))
Johan Hedberga69e8372015-03-06 21:08:53 +02005466 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
5467 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann9713c172014-07-06 12:11:15 +02005468
5469 if (!hdev->set_bdaddr)
Johan Hedberga69e8372015-03-06 21:08:53 +02005470 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
5471 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02005472
5473 hci_dev_lock(hdev);
5474
5475 changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
5476 bacpy(&hdev->public_addr, &cp->bdaddr);
5477
5478 err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev);
5479 if (err < 0)
5480 goto unlock;
5481
5482 if (!changed)
5483 goto unlock;
5484
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005485 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
Marcel Holtmann9713c172014-07-06 12:11:15 +02005486 err = new_options(hdev, sk);
5487
5488 if (is_configured(hdev)) {
5489 mgmt_index_removed(hdev);
5490
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005491 hci_dev_clear_flag(hdev, HCI_UNCONFIGURED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02005492
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005493 hci_dev_set_flag(hdev, HCI_CONFIG);
5494 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmann9713c172014-07-06 12:11:15 +02005495
5496 queue_work(hdev->req_workqueue, &hdev->power_on);
5497 }
5498
5499unlock:
5500 hci_dev_unlock(hdev);
5501 return err;
5502}
5503
Marcel Holtmannbea41602015-03-14 22:43:17 -07005504static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
5505 u8 data_len)
5506{
5507 eir[eir_len++] = sizeof(type) + data_len;
5508 eir[eir_len++] = type;
5509 memcpy(&eir[eir_len], data, data_len);
5510 eir_len += data_len;
5511
5512 return eir_len;
5513}
5514
Johan Hedberg40f66c02015-04-07 21:52:22 +03005515static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status,
5516 u16 opcode, struct sk_buff *skb)
5517{
5518 const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp;
5519 struct mgmt_rp_read_local_oob_ext_data *mgmt_rp;
5520 u8 *h192, *r192, *h256, *r256;
5521 struct mgmt_pending_cmd *cmd;
5522 u16 eir_len;
5523 int err;
5524
5525 BT_DBG("%s status %u", hdev->name, status);
5526
5527 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev);
5528 if (!cmd)
5529 return;
5530
5531 mgmt_cp = cmd->param;
5532
5533 if (status) {
5534 status = mgmt_status(status);
5535 eir_len = 0;
5536
5537 h192 = NULL;
5538 r192 = NULL;
5539 h256 = NULL;
5540 r256 = NULL;
5541 } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
5542 struct hci_rp_read_local_oob_data *rp;
5543
5544 if (skb->len != sizeof(*rp)) {
5545 status = MGMT_STATUS_FAILED;
5546 eir_len = 0;
5547 } else {
5548 status = MGMT_STATUS_SUCCESS;
5549 rp = (void *)skb->data;
5550
5551 eir_len = 5 + 18 + 18;
5552 h192 = rp->hash;
5553 r192 = rp->rand;
5554 h256 = NULL;
5555 r256 = NULL;
5556 }
5557 } else {
5558 struct hci_rp_read_local_oob_ext_data *rp;
5559
5560 if (skb->len != sizeof(*rp)) {
5561 status = MGMT_STATUS_FAILED;
5562 eir_len = 0;
5563 } else {
5564 status = MGMT_STATUS_SUCCESS;
5565 rp = (void *)skb->data;
5566
5567 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
5568 eir_len = 5 + 18 + 18;
5569 h192 = NULL;
5570 r192 = NULL;
5571 } else {
5572 eir_len = 5 + 18 + 18 + 18 + 18;
5573 h192 = rp->hash192;
5574 r192 = rp->rand192;
5575 }
5576
5577 h256 = rp->hash256;
5578 r256 = rp->rand256;
5579 }
5580 }
5581
5582 mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL);
5583 if (!mgmt_rp)
5584 goto done;
5585
5586 if (status)
5587 goto send_rsp;
5588
5589 eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV,
5590 hdev->dev_class, 3);
5591
5592 if (h192 && r192) {
5593 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
5594 EIR_SSP_HASH_C192, h192, 16);
5595 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
5596 EIR_SSP_RAND_R192, r192, 16);
5597 }
5598
5599 if (h256 && r256) {
5600 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
5601 EIR_SSP_HASH_C256, h256, 16);
5602 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
5603 EIR_SSP_RAND_R256, r256, 16);
5604 }
5605
5606send_rsp:
5607 mgmt_rp->type = mgmt_cp->type;
5608 mgmt_rp->eir_len = cpu_to_le16(eir_len);
5609
5610 err = mgmt_cmd_complete(cmd->sk, hdev->id,
5611 MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status,
5612 mgmt_rp, sizeof(*mgmt_rp) + eir_len);
5613 if (err < 0 || status)
5614 goto done;
5615
5616 hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS);
5617
5618 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
5619 mgmt_rp, sizeof(*mgmt_rp) + eir_len,
5620 HCI_MGMT_OOB_DATA_EVENTS, cmd->sk);
5621done:
5622 kfree(mgmt_rp);
5623 mgmt_pending_remove(cmd);
5624}
5625
5626static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk,
5627 struct mgmt_cp_read_local_oob_ext_data *cp)
5628{
5629 struct mgmt_pending_cmd *cmd;
5630 struct hci_request req;
5631 int err;
5632
5633 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev,
5634 cp, sizeof(*cp));
5635 if (!cmd)
5636 return -ENOMEM;
5637
5638 hci_req_init(&req, hdev);
5639
5640 if (bredr_sc_enabled(hdev))
5641 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
5642 else
5643 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
5644
5645 err = hci_req_run_skb(&req, read_local_oob_ext_data_complete);
5646 if (err < 0) {
5647 mgmt_pending_remove(cmd);
5648 return err;
5649 }
5650
5651 return 0;
5652}
5653
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005654static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
5655 void *data, u16 data_len)
5656{
5657 struct mgmt_cp_read_local_oob_ext_data *cp = data;
5658 struct mgmt_rp_read_local_oob_ext_data *rp;
5659 size_t rp_len;
5660 u16 eir_len;
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07005661 u8 status, flags, role, addr[7], hash[16], rand[16];
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005662 int err;
5663
5664 BT_DBG("%s", hdev->name);
5665
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07005666 if (hdev_is_powered(hdev)) {
5667 switch (cp->type) {
5668 case BIT(BDADDR_BREDR):
5669 status = mgmt_bredr_support(hdev);
5670 if (status)
5671 eir_len = 0;
5672 else
5673 eir_len = 5;
5674 break;
5675 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
5676 status = mgmt_le_support(hdev);
5677 if (status)
5678 eir_len = 0;
5679 else
5680 eir_len = 9 + 3 + 18 + 18 + 3;
5681 break;
5682 default:
5683 status = MGMT_STATUS_INVALID_PARAMS;
5684 eir_len = 0;
5685 break;
5686 }
5687 } else {
5688 status = MGMT_STATUS_NOT_POWERED;
5689 eir_len = 0;
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005690 }
5691
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005692 rp_len = sizeof(*rp) + eir_len;
5693 rp = kmalloc(rp_len, GFP_ATOMIC);
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07005694 if (!rp)
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005695 return -ENOMEM;
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07005696
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07005697 if (status)
5698 goto complete;
5699
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07005700 hci_dev_lock(hdev);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005701
5702 eir_len = 0;
5703 switch (cp->type) {
5704 case BIT(BDADDR_BREDR):
Johan Hedberg40f66c02015-04-07 21:52:22 +03005705 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
5706 err = read_local_ssp_oob_req(hdev, sk, cp);
5707 hci_dev_unlock(hdev);
5708 if (!err)
5709 goto done;
5710
5711 status = MGMT_STATUS_FAILED;
5712 goto complete;
5713 } else {
5714 eir_len = eir_append_data(rp->eir, eir_len,
5715 EIR_CLASS_OF_DEV,
5716 hdev->dev_class, 3);
5717 }
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005718 break;
5719 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
Marcel Holtmann5082a592015-03-16 12:39:00 -07005720 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
5721 smp_generate_oob(hdev, hash, rand) < 0) {
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07005722 hci_dev_unlock(hdev);
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07005723 status = MGMT_STATUS_FAILED;
5724 goto complete;
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07005725 }
5726
Marcel Holtmanne2135682015-04-02 12:00:58 -07005727 /* This should return the active RPA, but since the RPA
5728 * is only programmed on demand, it is really hard to fill
5729 * this in at the moment. For now disallow retrieving
5730 * local out-of-band data when privacy is in use.
5731 *
5732 * Returning the identity address will not help here since
5733 * pairing happens before the identity resolving key is
5734 * known and thus the connection establishment happens
5735 * based on the RPA and not the identity address.
5736 */
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005737 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
Marcel Holtmanne2135682015-04-02 12:00:58 -07005738 hci_dev_unlock(hdev);
5739 status = MGMT_STATUS_REJECTED;
5740 goto complete;
5741 }
5742
5743 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
5744 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
5745 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
5746 bacmp(&hdev->static_addr, BDADDR_ANY))) {
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005747 memcpy(addr, &hdev->static_addr, 6);
5748 addr[6] = 0x01;
5749 } else {
5750 memcpy(addr, &hdev->bdaddr, 6);
5751 addr[6] = 0x00;
5752 }
5753
5754 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR,
5755 addr, sizeof(addr));
5756
5757 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
5758 role = 0x02;
5759 else
5760 role = 0x01;
5761
5762 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE,
5763 &role, sizeof(role));
5764
Marcel Holtmann5082a592015-03-16 12:39:00 -07005765 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) {
5766 eir_len = eir_append_data(rp->eir, eir_len,
5767 EIR_LE_SC_CONFIRM,
5768 hash, sizeof(hash));
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07005769
Marcel Holtmann5082a592015-03-16 12:39:00 -07005770 eir_len = eir_append_data(rp->eir, eir_len,
5771 EIR_LE_SC_RANDOM,
5772 rand, sizeof(rand));
5773 }
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07005774
Johan Hedbergf2252572015-11-18 12:49:20 +02005775 flags = mgmt_get_adv_discov_flags(hdev);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005776
5777 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5778 flags |= LE_AD_NO_BREDR;
5779
5780 eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS,
5781 &flags, sizeof(flags));
5782 break;
5783 }
5784
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005785 hci_dev_unlock(hdev);
5786
Marcel Holtmann72000df2015-03-16 16:11:21 -07005787 hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS);
5788
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07005789 status = MGMT_STATUS_SUCCESS;
5790
5791complete:
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07005792 rp->type = cp->type;
5793 rp->eir_len = cpu_to_le16(eir_len);
5794
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005795 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07005796 status, rp, sizeof(*rp) + eir_len);
5797 if (err < 0 || status)
Marcel Holtmann72000df2015-03-16 16:11:21 -07005798 goto done;
5799
5800 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
5801 rp, sizeof(*rp) + eir_len,
5802 HCI_MGMT_OOB_DATA_EVENTS, sk);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005803
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07005804done:
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07005805 kfree(rp);
5806
5807 return err;
5808}
5809
Arman Uguray089fa8c2015-03-25 18:53:45 -07005810static u32 get_supported_adv_flags(struct hci_dev *hdev)
5811{
5812 u32 flags = 0;
5813
5814 flags |= MGMT_ADV_FLAG_CONNECTABLE;
5815 flags |= MGMT_ADV_FLAG_DISCOV;
5816 flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
5817 flags |= MGMT_ADV_FLAG_MANAGED_FLAGS;
5818
5819 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID)
5820 flags |= MGMT_ADV_FLAG_TX_POWER;
5821
5822 return flags;
5823}
5824
Marcel Holtmannd3d53052015-03-14 20:53:25 -07005825static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
5826 void *data, u16 data_len)
5827{
5828 struct mgmt_rp_read_adv_features *rp;
5829 size_t rp_len;
Johan Hedberg02c04af2015-11-26 12:15:58 +02005830 int err;
Florian Grandel286e0c82015-06-18 03:16:38 +02005831 struct adv_info *adv_instance;
Arman Uguray089fa8c2015-03-25 18:53:45 -07005832 u32 supported_flags;
Johan Hedberg02c04af2015-11-26 12:15:58 +02005833 u8 *instance;
Marcel Holtmannd3d53052015-03-14 20:53:25 -07005834
5835 BT_DBG("%s", hdev->name);
5836
Arman Uguray089fa8c2015-03-25 18:53:45 -07005837 if (!lmp_le_capable(hdev))
5838 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
5839 MGMT_STATUS_REJECTED);
5840
Marcel Holtmannd3d53052015-03-14 20:53:25 -07005841 hci_dev_lock(hdev);
5842
Johan Hedberg02c04af2015-11-26 12:15:58 +02005843 rp_len = sizeof(*rp) + hdev->adv_instance_cnt;
Marcel Holtmannd3d53052015-03-14 20:53:25 -07005844 rp = kmalloc(rp_len, GFP_ATOMIC);
5845 if (!rp) {
5846 hci_dev_unlock(hdev);
5847 return -ENOMEM;
5848 }
5849
Arman Uguray089fa8c2015-03-25 18:53:45 -07005850 supported_flags = get_supported_adv_flags(hdev);
5851
5852 rp->supported_flags = cpu_to_le32(supported_flags);
Marcel Holtmanndc5d82a2015-03-19 17:22:25 -07005853 rp->max_adv_data_len = HCI_MAX_AD_LENGTH;
5854 rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH;
Florian Grandeld2609b32015-06-18 03:16:34 +02005855 rp->max_instances = HCI_MAX_ADV_INSTANCES;
Johan Hedberg02c04af2015-11-26 12:15:58 +02005856 rp->num_instances = hdev->adv_instance_cnt;
Arman Uguray24b4f382015-03-23 15:57:12 -07005857
Johan Hedberg02c04af2015-11-26 12:15:58 +02005858 instance = rp->instance;
5859 list_for_each_entry(adv_instance, &hdev->adv_instances, list) {
5860 *instance = adv_instance->instance;
5861 instance++;
Arman Uguray24b4f382015-03-23 15:57:12 -07005862 }
Marcel Holtmannd3d53052015-03-14 20:53:25 -07005863
5864 hci_dev_unlock(hdev);
5865
5866 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
5867 MGMT_STATUS_SUCCESS, rp, rp_len);
5868
5869 kfree(rp);
5870
5871 return err;
5872}
5873
Arman Uguray4117ed72015-03-23 15:57:14 -07005874static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data,
Arman Ugurayb44133f2015-03-25 18:53:41 -07005875 u8 len, bool is_adv_data)
Arman Uguray24b4f382015-03-23 15:57:12 -07005876{
Arman Uguray4117ed72015-03-23 15:57:14 -07005877 u8 max_len = HCI_MAX_AD_LENGTH;
Arman Uguray24b4f382015-03-23 15:57:12 -07005878 int i, cur_len;
Arman Ugurayb44133f2015-03-25 18:53:41 -07005879 bool flags_managed = false;
Arman Uguray5507e352015-03-25 18:53:44 -07005880 bool tx_power_managed = false;
Arman Uguray24b4f382015-03-23 15:57:12 -07005881
Marcel Holtmann31a32482015-11-19 16:16:42 +01005882 if (is_adv_data) {
5883 if (adv_flags & (MGMT_ADV_FLAG_DISCOV |
5884 MGMT_ADV_FLAG_LIMITED_DISCOV |
5885 MGMT_ADV_FLAG_MANAGED_FLAGS)) {
5886 flags_managed = true;
5887 max_len -= 3;
5888 }
Arman Uguray24b4f382015-03-23 15:57:12 -07005889
Marcel Holtmann31a32482015-11-19 16:16:42 +01005890 if (adv_flags & MGMT_ADV_FLAG_TX_POWER) {
5891 tx_power_managed = true;
5892 max_len -= 3;
5893 }
Arman Uguray5507e352015-03-25 18:53:44 -07005894 }
5895
Arman Uguray4117ed72015-03-23 15:57:14 -07005896 if (len > max_len)
Arman Uguray24b4f382015-03-23 15:57:12 -07005897 return false;
5898
Arman Uguray4117ed72015-03-23 15:57:14 -07005899 /* Make sure that the data is correctly formatted. */
5900 for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) {
5901 cur_len = data[i];
Arman Uguray24b4f382015-03-23 15:57:12 -07005902
Arman Ugurayb44133f2015-03-25 18:53:41 -07005903 if (flags_managed && data[i + 1] == EIR_FLAGS)
5904 return false;
5905
Arman Uguray5507e352015-03-25 18:53:44 -07005906 if (tx_power_managed && data[i + 1] == EIR_TX_POWER)
5907 return false;
5908
Arman Uguray24b4f382015-03-23 15:57:12 -07005909 /* If the current field length would exceed the total data
5910 * length, then it's invalid.
5911 */
Arman Uguray4117ed72015-03-23 15:57:14 -07005912 if (i + cur_len >= len)
Arman Uguray24b4f382015-03-23 15:57:12 -07005913 return false;
5914 }
5915
5916 return true;
5917}
5918
Arman Uguray24b4f382015-03-23 15:57:12 -07005919static void add_advertising_complete(struct hci_dev *hdev, u8 status,
5920 u16 opcode)
5921{
5922 struct mgmt_pending_cmd *cmd;
Florian Grandelfffd38b2015-06-18 03:16:47 +02005923 struct mgmt_cp_add_advertising *cp;
Arman Uguray24b4f382015-03-23 15:57:12 -07005924 struct mgmt_rp_add_advertising rp;
Florian Grandelfffd38b2015-06-18 03:16:47 +02005925 struct adv_info *adv_instance, *n;
5926 u8 instance;
Arman Uguray24b4f382015-03-23 15:57:12 -07005927
5928 BT_DBG("status %d", status);
5929
5930 hci_dev_lock(hdev);
5931
5932 cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev);
5933
Florian Grandelfffd38b2015-06-18 03:16:47 +02005934 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) {
5935 if (!adv_instance->pending)
5936 continue;
5937
5938 if (!status) {
5939 adv_instance->pending = false;
5940 continue;
5941 }
5942
5943 instance = adv_instance->instance;
5944
5945 if (hdev->cur_adv_instance == instance)
5946 cancel_adv_timeout(hdev);
5947
5948 hci_remove_adv_instance(hdev, instance);
Johan Hedbergf2252572015-11-18 12:49:20 +02005949 mgmt_advertising_removed(cmd ? cmd->sk : NULL, hdev, instance);
Arman Uguray24b4f382015-03-23 15:57:12 -07005950 }
5951
5952 if (!cmd)
5953 goto unlock;
5954
Florian Grandelfffd38b2015-06-18 03:16:47 +02005955 cp = cmd->param;
5956 rp.instance = cp->instance;
Arman Uguray24b4f382015-03-23 15:57:12 -07005957
5958 if (status)
5959 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
5960 mgmt_status(status));
5961 else
5962 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
5963 mgmt_status(status), &rp, sizeof(rp));
5964
5965 mgmt_pending_remove(cmd);
5966
5967unlock:
5968 hci_dev_unlock(hdev);
5969}
5970
5971static int add_advertising(struct sock *sk, struct hci_dev *hdev,
5972 void *data, u16 data_len)
5973{
5974 struct mgmt_cp_add_advertising *cp = data;
5975 struct mgmt_rp_add_advertising rp;
5976 u32 flags;
Arman Uguray089fa8c2015-03-25 18:53:45 -07005977 u32 supported_flags;
Arman Uguray24b4f382015-03-23 15:57:12 -07005978 u8 status;
Florian Grandelfffd38b2015-06-18 03:16:47 +02005979 u16 timeout, duration;
5980 unsigned int prev_instance_cnt = hdev->adv_instance_cnt;
5981 u8 schedule_instance = 0;
5982 struct adv_info *next_instance;
Arman Uguray24b4f382015-03-23 15:57:12 -07005983 int err;
5984 struct mgmt_pending_cmd *cmd;
5985 struct hci_request req;
5986
5987 BT_DBG("%s", hdev->name);
5988
5989 status = mgmt_le_support(hdev);
5990 if (status)
5991 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
5992 status);
5993
Marcel Holtmannceff86a2015-11-19 16:16:41 +01005994 if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES)
5995 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
5996 MGMT_STATUS_INVALID_PARAMS);
5997
Johan Hedberg6a0e7802016-03-11 09:56:33 +02005998 if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len)
5999 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6000 MGMT_STATUS_INVALID_PARAMS);
6001
Arman Uguray24b4f382015-03-23 15:57:12 -07006002 flags = __le32_to_cpu(cp->flags);
Arman Uguray912098a2015-03-23 15:57:15 -07006003 timeout = __le16_to_cpu(cp->timeout);
Florian Grandelfffd38b2015-06-18 03:16:47 +02006004 duration = __le16_to_cpu(cp->duration);
Arman Uguray24b4f382015-03-23 15:57:12 -07006005
Florian Grandelfffd38b2015-06-18 03:16:47 +02006006 /* The current implementation only supports a subset of the specified
6007 * flags.
Arman Uguray089fa8c2015-03-25 18:53:45 -07006008 */
6009 supported_flags = get_supported_adv_flags(hdev);
Florian Grandelfffd38b2015-06-18 03:16:47 +02006010 if (flags & ~supported_flags)
Arman Uguray24b4f382015-03-23 15:57:12 -07006011 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6012 MGMT_STATUS_INVALID_PARAMS);
6013
6014 hci_dev_lock(hdev);
6015
Arman Uguray912098a2015-03-23 15:57:15 -07006016 if (timeout && !hdev_is_powered(hdev)) {
6017 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6018 MGMT_STATUS_REJECTED);
6019 goto unlock;
6020 }
6021
Arman Uguray24b4f382015-03-23 15:57:12 -07006022 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
Arman Ugurayda9293352015-03-23 15:57:13 -07006023 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
Arman Uguray24b4f382015-03-23 15:57:12 -07006024 pending_find(MGMT_OP_SET_LE, hdev)) {
6025 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6026 MGMT_STATUS_BUSY);
6027 goto unlock;
6028 }
6029
Arman Ugurayb44133f2015-03-25 18:53:41 -07006030 if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) ||
Arman Uguray4117ed72015-03-23 15:57:14 -07006031 !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len,
Arman Ugurayb44133f2015-03-25 18:53:41 -07006032 cp->scan_rsp_len, false)) {
Arman Uguray24b4f382015-03-23 15:57:12 -07006033 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6034 MGMT_STATUS_INVALID_PARAMS);
6035 goto unlock;
6036 }
6037
Florian Grandelfffd38b2015-06-18 03:16:47 +02006038 err = hci_add_adv_instance(hdev, cp->instance, flags,
6039 cp->adv_data_len, cp->data,
6040 cp->scan_rsp_len,
6041 cp->data + cp->adv_data_len,
6042 timeout, duration);
6043 if (err < 0) {
6044 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6045 MGMT_STATUS_FAILED);
6046 goto unlock;
6047 }
Arman Uguray24b4f382015-03-23 15:57:12 -07006048
Florian Grandelfffd38b2015-06-18 03:16:47 +02006049 /* Only trigger an advertising added event if a new instance was
6050 * actually added.
6051 */
6052 if (hdev->adv_instance_cnt > prev_instance_cnt)
Johan Hedbergf2252572015-11-18 12:49:20 +02006053 mgmt_advertising_added(sk, hdev, cp->instance);
Arman Uguray24b4f382015-03-23 15:57:12 -07006054
Florian Grandelfffd38b2015-06-18 03:16:47 +02006055 if (hdev->cur_adv_instance == cp->instance) {
6056 /* If the currently advertised instance is being changed then
6057 * cancel the current advertising and schedule the next
6058 * instance. If there is only one instance then the overridden
6059 * advertising data will be visible right away.
6060 */
6061 cancel_adv_timeout(hdev);
Arman Uguray912098a2015-03-23 15:57:15 -07006062
Florian Grandelfffd38b2015-06-18 03:16:47 +02006063 next_instance = hci_get_next_instance(hdev, cp->instance);
6064 if (next_instance)
6065 schedule_instance = next_instance->instance;
6066 } else if (!hdev->adv_instance_timeout) {
6067 /* Immediately advertise the new instance if no other
6068 * instance is currently being advertised.
6069 */
6070 schedule_instance = cp->instance;
6071 }
Arman Uguray912098a2015-03-23 15:57:15 -07006072
Florian Grandelfffd38b2015-06-18 03:16:47 +02006073 /* If the HCI_ADVERTISING flag is set or the device isn't powered or
6074 * there is no instance to be advertised then we have no HCI
6075 * communication to make. Simply return.
Arman Uguray24b4f382015-03-23 15:57:12 -07006076 */
6077 if (!hdev_is_powered(hdev) ||
Florian Grandelfffd38b2015-06-18 03:16:47 +02006078 hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
6079 !schedule_instance) {
6080 rp.instance = cp->instance;
Arman Uguray24b4f382015-03-23 15:57:12 -07006081 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6082 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
6083 goto unlock;
6084 }
6085
6086 /* We're good to go, update advertising data, parameters, and start
6087 * advertising.
6088 */
6089 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data,
6090 data_len);
6091 if (!cmd) {
6092 err = -ENOMEM;
6093 goto unlock;
6094 }
6095
6096 hci_req_init(&req, hdev);
6097
Johan Hedbergf2252572015-11-18 12:49:20 +02006098 err = __hci_req_schedule_adv_instance(&req, schedule_instance, true);
Arman Uguray24b4f382015-03-23 15:57:12 -07006099
Florian Grandelfffd38b2015-06-18 03:16:47 +02006100 if (!err)
6101 err = hci_req_run(&req, add_advertising_complete);
6102
Arman Uguray24b4f382015-03-23 15:57:12 -07006103 if (err < 0)
6104 mgmt_pending_remove(cmd);
6105
6106unlock:
6107 hci_dev_unlock(hdev);
6108
6109 return err;
6110}
6111
Arman Ugurayda9293352015-03-23 15:57:13 -07006112static void remove_advertising_complete(struct hci_dev *hdev, u8 status,
6113 u16 opcode)
6114{
6115 struct mgmt_pending_cmd *cmd;
Florian Grandel01948332015-06-18 03:16:48 +02006116 struct mgmt_cp_remove_advertising *cp;
Arman Ugurayda9293352015-03-23 15:57:13 -07006117 struct mgmt_rp_remove_advertising rp;
6118
6119 BT_DBG("status %d", status);
6120
6121 hci_dev_lock(hdev);
6122
6123 /* A failure status here only means that we failed to disable
6124 * advertising. Otherwise, the advertising instance has been removed,
6125 * so report success.
6126 */
6127 cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev);
6128 if (!cmd)
6129 goto unlock;
6130
Florian Grandel01948332015-06-18 03:16:48 +02006131 cp = cmd->param;
6132 rp.instance = cp->instance;
Arman Ugurayda9293352015-03-23 15:57:13 -07006133
6134 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS,
6135 &rp, sizeof(rp));
6136 mgmt_pending_remove(cmd);
6137
6138unlock:
6139 hci_dev_unlock(hdev);
6140}
6141
6142static int remove_advertising(struct sock *sk, struct hci_dev *hdev,
6143 void *data, u16 data_len)
6144{
6145 struct mgmt_cp_remove_advertising *cp = data;
6146 struct mgmt_rp_remove_advertising rp;
Arman Ugurayda9293352015-03-23 15:57:13 -07006147 struct mgmt_pending_cmd *cmd;
6148 struct hci_request req;
Johan Hedberg952497b2015-06-18 21:05:31 +03006149 int err;
Arman Ugurayda9293352015-03-23 15:57:13 -07006150
6151 BT_DBG("%s", hdev->name);
6152
Arman Ugurayda9293352015-03-23 15:57:13 -07006153 hci_dev_lock(hdev);
6154
Johan Hedberg952497b2015-06-18 21:05:31 +03006155 if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) {
Florian Grandel01948332015-06-18 03:16:48 +02006156 err = mgmt_cmd_status(sk, hdev->id,
6157 MGMT_OP_REMOVE_ADVERTISING,
6158 MGMT_STATUS_INVALID_PARAMS);
6159 goto unlock;
6160 }
6161
Arman Ugurayda9293352015-03-23 15:57:13 -07006162 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
6163 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
6164 pending_find(MGMT_OP_SET_LE, hdev)) {
6165 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
6166 MGMT_STATUS_BUSY);
6167 goto unlock;
6168 }
6169
Johan Hedberg17fd08f2015-11-26 12:15:59 +02006170 if (list_empty(&hdev->adv_instances)) {
Arman Ugurayda9293352015-03-23 15:57:13 -07006171 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
6172 MGMT_STATUS_INVALID_PARAMS);
6173 goto unlock;
6174 }
6175
Florian Grandel01948332015-06-18 03:16:48 +02006176 hci_req_init(&req, hdev);
Arman Uguray912098a2015-03-23 15:57:15 -07006177
Johan Hedbergf2252572015-11-18 12:49:20 +02006178 hci_req_clear_adv_instance(hdev, &req, cp->instance, true);
Arman Ugurayda9293352015-03-23 15:57:13 -07006179
Florian Grandel01948332015-06-18 03:16:48 +02006180 if (list_empty(&hdev->adv_instances))
Johan Hedbergf2252572015-11-18 12:49:20 +02006181 __hci_req_disable_advertising(&req);
Arman Ugurayda9293352015-03-23 15:57:13 -07006182
Florian Grandel01948332015-06-18 03:16:48 +02006183 /* If no HCI commands have been collected so far or the HCI_ADVERTISING
6184 * flag is set or the device isn't powered then we have no HCI
6185 * communication to make. Simply return.
Arman Ugurayda9293352015-03-23 15:57:13 -07006186 */
Florian Grandel01948332015-06-18 03:16:48 +02006187 if (skb_queue_empty(&req.cmd_q) ||
6188 !hdev_is_powered(hdev) ||
Arman Ugurayda9293352015-03-23 15:57:13 -07006189 hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
Florian Grandel01948332015-06-18 03:16:48 +02006190 rp.instance = cp->instance;
Arman Ugurayda9293352015-03-23 15:57:13 -07006191 err = mgmt_cmd_complete(sk, hdev->id,
6192 MGMT_OP_REMOVE_ADVERTISING,
6193 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
6194 goto unlock;
6195 }
6196
6197 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data,
6198 data_len);
6199 if (!cmd) {
6200 err = -ENOMEM;
6201 goto unlock;
6202 }
6203
Arman Ugurayda9293352015-03-23 15:57:13 -07006204 err = hci_req_run(&req, remove_advertising_complete);
6205 if (err < 0)
6206 mgmt_pending_remove(cmd);
6207
6208unlock:
6209 hci_dev_unlock(hdev);
6210
6211 return err;
6212}
6213
Marcel Holtmann40b25fe2015-11-19 16:16:43 +01006214static u8 tlv_data_max_len(u32 adv_flags, bool is_adv_data)
6215{
6216 u8 max_len = HCI_MAX_AD_LENGTH;
6217
6218 if (is_adv_data) {
6219 if (adv_flags & (MGMT_ADV_FLAG_DISCOV |
6220 MGMT_ADV_FLAG_LIMITED_DISCOV |
6221 MGMT_ADV_FLAG_MANAGED_FLAGS))
6222 max_len -= 3;
6223
6224 if (adv_flags & MGMT_ADV_FLAG_TX_POWER)
6225 max_len -= 3;
6226 }
6227
6228 return max_len;
6229}
6230
6231static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev,
6232 void *data, u16 data_len)
6233{
6234 struct mgmt_cp_get_adv_size_info *cp = data;
6235 struct mgmt_rp_get_adv_size_info rp;
6236 u32 flags, supported_flags;
6237 int err;
6238
6239 BT_DBG("%s", hdev->name);
6240
6241 if (!lmp_le_capable(hdev))
6242 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
6243 MGMT_STATUS_REJECTED);
6244
6245 if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES)
6246 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
6247 MGMT_STATUS_INVALID_PARAMS);
6248
6249 flags = __le32_to_cpu(cp->flags);
6250
6251 /* The current implementation only supports a subset of the specified
6252 * flags.
6253 */
6254 supported_flags = get_supported_adv_flags(hdev);
6255 if (flags & ~supported_flags)
6256 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
6257 MGMT_STATUS_INVALID_PARAMS);
6258
6259 rp.instance = cp->instance;
6260 rp.flags = cp->flags;
6261 rp.max_adv_data_len = tlv_data_max_len(flags, true);
6262 rp.max_scan_rsp_len = tlv_data_max_len(flags, false);
6263
6264 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
6265 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
6266
6267 return err;
6268}
6269
Johan Hedberg6d785aa32015-03-06 21:08:51 +02006270static const struct hci_mgmt_handler mgmt_handlers[] = {
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02006271 { NULL }, /* 0x0000 (no command) */
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006272 { read_version, MGMT_READ_VERSION_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006273 HCI_MGMT_NO_HDEV |
6274 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006275 { read_commands, MGMT_READ_COMMANDS_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006276 HCI_MGMT_NO_HDEV |
6277 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006278 { read_index_list, MGMT_READ_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006279 HCI_MGMT_NO_HDEV |
6280 HCI_MGMT_UNTRUSTED },
6281 { read_controller_info, MGMT_READ_INFO_SIZE,
6282 HCI_MGMT_UNTRUSTED },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006283 { set_powered, MGMT_SETTING_SIZE },
6284 { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE },
6285 { set_connectable, MGMT_SETTING_SIZE },
6286 { set_fast_connectable, MGMT_SETTING_SIZE },
6287 { set_bondable, MGMT_SETTING_SIZE },
6288 { set_link_security, MGMT_SETTING_SIZE },
6289 { set_ssp, MGMT_SETTING_SIZE },
6290 { set_hs, MGMT_SETTING_SIZE },
6291 { set_le, MGMT_SETTING_SIZE },
6292 { set_dev_class, MGMT_SET_DEV_CLASS_SIZE },
6293 { set_local_name, MGMT_SET_LOCAL_NAME_SIZE },
6294 { add_uuid, MGMT_ADD_UUID_SIZE },
6295 { remove_uuid, MGMT_REMOVE_UUID_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006296 { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE,
6297 HCI_MGMT_VAR_LEN },
6298 { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE,
6299 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006300 { disconnect, MGMT_DISCONNECT_SIZE },
6301 { get_connections, MGMT_GET_CONNECTIONS_SIZE },
6302 { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE },
6303 { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE },
6304 { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE },
6305 { pair_device, MGMT_PAIR_DEVICE_SIZE },
6306 { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE },
6307 { unpair_device, MGMT_UNPAIR_DEVICE_SIZE },
6308 { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE },
6309 { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
6310 { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE },
6311 { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006312 { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE },
6313 { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE,
6314 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006315 { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
6316 { start_discovery, MGMT_START_DISCOVERY_SIZE },
6317 { stop_discovery, MGMT_STOP_DISCOVERY_SIZE },
6318 { confirm_name, MGMT_CONFIRM_NAME_SIZE },
6319 { block_device, MGMT_BLOCK_DEVICE_SIZE },
6320 { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE },
6321 { set_device_id, MGMT_SET_DEVICE_ID_SIZE },
6322 { set_advertising, MGMT_SETTING_SIZE },
6323 { set_bredr, MGMT_SETTING_SIZE },
6324 { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE },
6325 { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE },
6326 { set_secure_conn, MGMT_SETTING_SIZE },
6327 { set_debug_keys, MGMT_SETTING_SIZE },
6328 { set_privacy, MGMT_SET_PRIVACY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006329 { load_irks, MGMT_LOAD_IRKS_SIZE,
6330 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006331 { get_conn_info, MGMT_GET_CONN_INFO_SIZE },
6332 { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE },
6333 { add_device, MGMT_ADD_DEVICE_SIZE },
6334 { remove_device, MGMT_REMOVE_DEVICE_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006335 { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE,
6336 HCI_MGMT_VAR_LEN },
6337 { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006338 HCI_MGMT_NO_HDEV |
6339 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006340 { read_config_info, MGMT_READ_CONFIG_INFO_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006341 HCI_MGMT_UNCONFIGURED |
6342 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006343 { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE,
6344 HCI_MGMT_UNCONFIGURED },
6345 { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE,
6346 HCI_MGMT_UNCONFIGURED },
6347 { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE,
6348 HCI_MGMT_VAR_LEN },
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006349 { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE },
Marcel Holtmann96f14742015-03-14 19:27:57 -07006350 { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006351 HCI_MGMT_NO_HDEV |
6352 HCI_MGMT_UNTRUSTED },
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006353 { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE },
Arman Uguray24b4f382015-03-23 15:57:12 -07006354 { add_advertising, MGMT_ADD_ADVERTISING_SIZE,
6355 HCI_MGMT_VAR_LEN },
Arman Ugurayda9293352015-03-23 15:57:13 -07006356 { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE },
Marcel Holtmann40b25fe2015-11-19 16:16:43 +01006357 { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE },
Johan Hedberg78b781c2016-01-05 13:19:32 +02006358 { start_limited_discovery, MGMT_START_DISCOVERY_SIZE },
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02006359};
6360
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07006361void mgmt_index_added(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02006362{
Marcel Holtmannced85542015-03-14 19:27:56 -07006363 struct mgmt_ev_ext_index ev;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +03006364
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02006365 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
6366 return;
6367
Marcel Holtmannf9207332015-03-14 19:27:55 -07006368 switch (hdev->dev_type) {
6369 case HCI_BREDR:
6370 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
6371 mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev,
6372 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07006373 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006374 } else {
6375 mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0,
6376 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07006377 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006378 }
6379 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07006380 case HCI_AMP:
6381 ev.type = 0x02;
6382 break;
6383 default:
6384 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006385 }
Marcel Holtmannced85542015-03-14 19:27:56 -07006386
6387 ev.bus = hdev->bus;
6388
6389 mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev),
6390 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergc71e97b2010-12-13 21:07:07 +02006391}
6392
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07006393void mgmt_index_removed(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02006394{
Marcel Holtmannced85542015-03-14 19:27:56 -07006395 struct mgmt_ev_ext_index ev;
Johan Hedberg5f159032012-03-02 03:13:19 +02006396 u8 status = MGMT_STATUS_INVALID_INDEX;
Johan Hedbergb24752f2011-11-03 14:40:33 +02006397
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02006398 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
6399 return;
6400
Marcel Holtmannf9207332015-03-14 19:27:55 -07006401 switch (hdev->dev_type) {
6402 case HCI_BREDR:
6403 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedbergb24752f2011-11-03 14:40:33 +02006404
Marcel Holtmannf9207332015-03-14 19:27:55 -07006405 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
6406 mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev,
6407 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07006408 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006409 } else {
6410 mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0,
6411 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07006412 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006413 }
6414 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07006415 case HCI_AMP:
6416 ev.type = 0x02;
6417 break;
6418 default:
6419 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006420 }
Marcel Holtmannced85542015-03-14 19:27:56 -07006421
6422 ev.bus = hdev->bus;
6423
6424 mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev),
6425 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02006426}
6427
Andre Guedes6046dc32014-02-26 20:21:51 -03006428/* This function requires the caller holds hdev->lock */
Johan Hedbergaf02dd42015-11-11 08:11:21 +02006429static void restart_le_actions(struct hci_dev *hdev)
Andre Guedes6046dc32014-02-26 20:21:51 -03006430{
6431 struct hci_conn_params *p;
6432
6433 list_for_each_entry(p, &hdev->le_conn_params, list) {
Johan Hedbergd7347f32014-07-04 12:37:23 +03006434 /* Needed for AUTO_OFF case where might not "really"
6435 * have been powered off.
6436 */
6437 list_del_init(&p->action);
6438
6439 switch (p->auto_connect) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006440 case HCI_AUTO_CONN_DIRECT:
Johan Hedbergd7347f32014-07-04 12:37:23 +03006441 case HCI_AUTO_CONN_ALWAYS:
6442 list_add(&p->action, &hdev->pend_le_conns);
6443 break;
6444 case HCI_AUTO_CONN_REPORT:
6445 list_add(&p->action, &hdev->pend_le_reports);
6446 break;
6447 default:
6448 break;
Marcel Holtmannc83ed192014-07-01 19:28:24 +02006449 }
Andre Guedes6046dc32014-02-26 20:21:51 -03006450 }
6451}
6452
Johan Hedberg2ff13892015-11-25 16:15:44 +02006453void mgmt_power_on(struct hci_dev *hdev, int err)
Johan Hedberg229ab392013-03-15 17:06:53 -05006454{
6455 struct cmd_lookup match = { NULL, hdev };
6456
Johan Hedberg2ff13892015-11-25 16:15:44 +02006457 BT_DBG("err %d", err);
Johan Hedberg229ab392013-03-15 17:06:53 -05006458
Johan Hedberg2ff13892015-11-25 16:15:44 +02006459 hci_dev_lock(hdev);
6460
6461 if (!err) {
Johan Hedbergaf02dd42015-11-11 08:11:21 +02006462 restart_le_actions(hdev);
6463 hci_update_background_scan(hdev);
Marcel Holtmann162a3ba2015-01-14 15:43:11 -08006464 }
6465
Johan Hedberg229ab392013-03-15 17:06:53 -05006466 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
6467
6468 new_settings(hdev, match.sk);
6469
Johan Hedberg229ab392013-03-15 17:06:53 -05006470 if (match.sk)
6471 sock_put(match.sk);
Johan Hedberg2ff13892015-11-25 16:15:44 +02006472
6473 hci_dev_unlock(hdev);
Johan Hedberg229ab392013-03-15 17:06:53 -05006474}
6475
Johan Hedberg2ff13892015-11-25 16:15:44 +02006476void __mgmt_power_off(struct hci_dev *hdev)
Johan Hedberg5add6af2010-12-16 10:00:37 +02006477{
Johan Hedberg76a7f3a2012-02-17 00:34:40 +02006478 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg98459042014-12-12 11:15:21 +02006479 u8 status, zero_cod[] = { 0, 0, 0 };
Johan Hedbergb24752f2011-11-03 14:40:33 +02006480
Johan Hedberg229ab392013-03-15 17:06:53 -05006481 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
Johan Hedberg98459042014-12-12 11:15:21 +02006482
6483 /* If the power off is because of hdev unregistration let
6484 * use the appropriate INVALID_INDEX status. Otherwise use
6485 * NOT_POWERED. We cover both scenarios here since later in
6486 * mgmt_index_removed() any hci_conn callbacks will have already
6487 * been triggered, potentially causing misleading DISCONNECTED
6488 * status responses.
6489 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006490 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
Johan Hedberg98459042014-12-12 11:15:21 +02006491 status = MGMT_STATUS_INVALID_INDEX;
6492 else
6493 status = MGMT_STATUS_NOT_POWERED;
6494
6495 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedberg229ab392013-03-15 17:06:53 -05006496
6497 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0)
Marcel Holtmannf6b77122015-03-14 19:28:05 -07006498 mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
6499 zero_cod, sizeof(zero_cod), NULL);
Johan Hedberg229ab392013-03-15 17:06:53 -05006500
Johan Hedberg2ff13892015-11-25 16:15:44 +02006501 new_settings(hdev, match.sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02006502
6503 if (match.sk)
6504 sock_put(match.sk);
Johan Hedberg5add6af2010-12-16 10:00:37 +02006505}
Johan Hedberg73f22f62010-12-29 16:00:25 +02006506
Marcel Holtmann3eec7052013-10-06 23:55:46 -07006507void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
Johan Hedberg96570ff2013-05-29 09:51:29 +03006508{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006509 struct mgmt_pending_cmd *cmd;
Johan Hedberg96570ff2013-05-29 09:51:29 +03006510 u8 status;
6511
Johan Hedberg333ae952015-03-17 13:48:47 +02006512 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg96570ff2013-05-29 09:51:29 +03006513 if (!cmd)
Marcel Holtmann3eec7052013-10-06 23:55:46 -07006514 return;
Johan Hedberg96570ff2013-05-29 09:51:29 +03006515
6516 if (err == -ERFKILL)
6517 status = MGMT_STATUS_RFKILLED;
6518 else
6519 status = MGMT_STATUS_FAILED;
6520
Johan Hedberga69e8372015-03-06 21:08:53 +02006521 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
Johan Hedberg96570ff2013-05-29 09:51:29 +03006522
6523 mgmt_pending_remove(cmd);
Johan Hedberg96570ff2013-05-29 09:51:29 +03006524}
6525
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07006526void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
6527 bool persistent)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006528{
Johan Hedberg86742e12011-11-07 23:13:38 +02006529 struct mgmt_ev_new_link_key ev;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006530
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006531 memset(&ev, 0, sizeof(ev));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006532
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006533 ev.store_hint = persistent;
Johan Hedbergd753fdc2012-02-17 14:06:34 +02006534 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03006535 ev.key.addr.type = BDADDR_BREDR;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006536 ev.key.type = key->type;
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03006537 memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006538 ev.key.pin_len = key->pin_len;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006539
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07006540 mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006541}
Johan Hedbergf7520542011-01-20 12:34:39 +02006542
Johan Hedbergd7b25452014-05-23 13:19:53 +03006543static u8 mgmt_ltk_type(struct smp_ltk *ltk)
6544{
Johan Hedberg23fb8de2014-05-23 13:15:37 +03006545 switch (ltk->type) {
6546 case SMP_LTK:
6547 case SMP_LTK_SLAVE:
6548 if (ltk->authenticated)
6549 return MGMT_LTK_AUTHENTICATED;
6550 return MGMT_LTK_UNAUTHENTICATED;
6551 case SMP_LTK_P256:
6552 if (ltk->authenticated)
6553 return MGMT_LTK_P256_AUTH;
6554 return MGMT_LTK_P256_UNAUTH;
6555 case SMP_LTK_P256_DEBUG:
6556 return MGMT_LTK_P256_DEBUG;
6557 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03006558
6559 return MGMT_LTK_UNAUTHENTICATED;
6560}
6561
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07006562void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006563{
6564 struct mgmt_ev_new_long_term_key ev;
6565
6566 memset(&ev, 0, sizeof(ev));
6567
Marcel Holtmann5192d302014-02-19 17:11:58 -08006568 /* Devices using resolvable or non-resolvable random addresses
Florian Grandelf72186d2015-05-26 03:31:09 +02006569 * without providing an identity resolving key don't require
Marcel Holtmann5192d302014-02-19 17:11:58 -08006570 * to store long term keys. Their addresses will change the
6571 * next time around.
6572 *
6573 * Only when a remote device provides an identity address
6574 * make sure the long term key is stored. If the remote
6575 * identity is known, the long term keys are internally
6576 * mapped to the identity address. So allow static random
6577 * and public addresses here.
6578 */
Johan Hedbergba74b662014-02-19 14:57:45 +02006579 if (key->bdaddr_type == ADDR_LE_DEV_RANDOM &&
6580 (key->bdaddr.b[5] & 0xc0) != 0xc0)
6581 ev.store_hint = 0x00;
6582 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07006583 ev.store_hint = persistent;
Johan Hedbergba74b662014-02-19 14:57:45 +02006584
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006585 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03006586 ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
Johan Hedbergd7b25452014-05-23 13:19:53 +03006587 ev.key.type = mgmt_ltk_type(key);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006588 ev.key.enc_size = key->enc_size;
6589 ev.key.ediv = key->ediv;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08006590 ev.key.rand = key->rand;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006591
Johan Hedberg2ceba532014-06-16 19:25:16 +03006592 if (key->type == SMP_LTK)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006593 ev.key.master = 1;
6594
Johan Hedberg1fc62c52015-06-10 11:11:20 +03006595 /* Make sure we copy only the significant bytes based on the
6596 * encryption key size, and set the rest of the value to zeroes.
6597 */
Jakub Pawlowskicb922052015-08-05 23:16:29 +02006598 memcpy(ev.key.val, key->val, key->enc_size);
Johan Hedberg1fc62c52015-06-10 11:11:20 +03006599 memset(ev.key.val + key->enc_size, 0,
6600 sizeof(ev.key.val) - key->enc_size);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006601
Marcel Holtmann083368f2013-10-15 14:26:29 -07006602 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006603}
6604
Johan Hedbergcad20c22015-10-12 13:36:19 +02006605void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent)
Johan Hedberg95fbac82014-02-19 15:18:31 +02006606{
6607 struct mgmt_ev_new_irk ev;
6608
6609 memset(&ev, 0, sizeof(ev));
6610
Johan Hedbergcad20c22015-10-12 13:36:19 +02006611 ev.store_hint = persistent;
Marcel Holtmannbab6d1e2014-02-19 11:51:54 -08006612
Johan Hedberg95fbac82014-02-19 15:18:31 +02006613 bacpy(&ev.rpa, &irk->rpa);
6614 bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr);
6615 ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type);
6616 memcpy(ev.irk.val, irk->val, sizeof(irk->val));
6617
6618 mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
6619}
6620
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07006621void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
6622 bool persistent)
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07006623{
6624 struct mgmt_ev_new_csrk ev;
6625
6626 memset(&ev, 0, sizeof(ev));
6627
6628 /* Devices using resolvable or non-resolvable random addresses
Florian Grandelf72186d2015-05-26 03:31:09 +02006629 * without providing an identity resolving key don't require
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07006630 * to store signature resolving keys. Their addresses will change
6631 * the next time around.
6632 *
6633 * Only when a remote device provides an identity address
6634 * make sure the signature resolving key is stored. So allow
6635 * static random and public addresses here.
6636 */
6637 if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM &&
6638 (csrk->bdaddr.b[5] & 0xc0) != 0xc0)
6639 ev.store_hint = 0x00;
6640 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07006641 ev.store_hint = persistent;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07006642
6643 bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
6644 ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
Johan Hedberg4cd39282015-02-27 10:11:13 +02006645 ev.key.type = csrk->type;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07006646 memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
6647
6648 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
6649}
6650
Andre Guedesffb5a8272014-07-01 18:10:11 -03006651void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedbergf4869e22014-07-02 17:37:32 +03006652 u8 bdaddr_type, u8 store_hint, u16 min_interval,
6653 u16 max_interval, u16 latency, u16 timeout)
Andre Guedesffb5a8272014-07-01 18:10:11 -03006654{
6655 struct mgmt_ev_new_conn_param ev;
6656
Johan Hedbergc103aea2014-07-02 17:37:34 +03006657 if (!hci_is_identity_address(bdaddr, bdaddr_type))
6658 return;
6659
Andre Guedesffb5a8272014-07-01 18:10:11 -03006660 memset(&ev, 0, sizeof(ev));
6661 bacpy(&ev.addr.bdaddr, bdaddr);
6662 ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type);
Johan Hedbergf4869e22014-07-02 17:37:32 +03006663 ev.store_hint = store_hint;
Andre Guedesffb5a8272014-07-01 18:10:11 -03006664 ev.min_interval = cpu_to_le16(min_interval);
6665 ev.max_interval = cpu_to_le16(max_interval);
6666 ev.latency = cpu_to_le16(latency);
6667 ev.timeout = cpu_to_le16(timeout);
6668
6669 mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL);
6670}
6671
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00006672void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
6673 u32 flags, u8 *name, u8 name_len)
Johan Hedbergf7520542011-01-20 12:34:39 +02006674{
Johan Hedbergb644ba32012-01-17 21:48:47 +02006675 char buf[512];
6676 struct mgmt_ev_device_connected *ev = (void *) buf;
6677 u16 eir_len = 0;
Johan Hedbergf7520542011-01-20 12:34:39 +02006678
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00006679 bacpy(&ev->addr.bdaddr, &conn->dst);
6680 ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedbergf7520542011-01-20 12:34:39 +02006681
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02006682 ev->flags = __cpu_to_le32(flags);
Johan Hedberg08c79b62012-02-23 22:31:51 +02006683
Alfonso Acostafd45ada2014-10-07 08:44:11 +00006684 /* We must ensure that the EIR Data fields are ordered and
6685 * unique. Keep it simple for now and avoid the problem by not
6686 * adding any BR/EDR data to the LE adv.
6687 */
6688 if (conn->le_adv_data_len > 0) {
6689 memcpy(&ev->eir[eir_len],
6690 conn->le_adv_data, conn->le_adv_data_len);
6691 eir_len = conn->le_adv_data_len;
6692 } else {
6693 if (name_len > 0)
6694 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE,
6695 name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02006696
Alfonso Acostaddbea5c2014-10-07 08:44:12 +00006697 if (memcmp(conn->dev_class, "\0\0\0", 3) != 0)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00006698 eir_len = eir_append_data(ev->eir, eir_len,
6699 EIR_CLASS_OF_DEV,
6700 conn->dev_class, 3);
6701 }
Johan Hedbergb644ba32012-01-17 21:48:47 +02006702
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02006703 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02006704
Marcel Holtmannecd90ae2013-10-06 23:55:49 -07006705 mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf,
6706 sizeof(*ev) + eir_len, NULL);
Johan Hedbergf7520542011-01-20 12:34:39 +02006707}
6708
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006709static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg8962ee72011-01-20 12:40:27 +02006710{
Johan Hedberg8962ee72011-01-20 12:40:27 +02006711 struct sock **sk = data;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006712
Johan Hedbergf5818c22014-12-05 13:36:02 +02006713 cmd->cmd_complete(cmd, 0);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006714
6715 *sk = cmd->sk;
6716 sock_hold(*sk);
6717
Johan Hedberga664b5b2011-02-19 12:06:02 -03006718 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006719}
6720
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006721static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberga8a1d192011-11-10 15:54:38 +02006722{
Johan Hedbergb1078ad2012-02-09 17:21:16 +02006723 struct hci_dev *hdev = data;
Johan Hedberg124f6e32012-02-09 13:50:12 +02006724 struct mgmt_cp_unpair_device *cp = cmd->param;
Johan Hedberga8a1d192011-11-10 15:54:38 +02006725
Johan Hedbergb1078ad2012-02-09 17:21:16 +02006726 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
6727
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02006728 cmd->cmd_complete(cmd, 0);
Johan Hedberga8a1d192011-11-10 15:54:38 +02006729 mgmt_pending_remove(cmd);
6730}
6731
Johan Hedberg84c61d92014-08-01 11:13:30 +03006732bool mgmt_powering_down(struct hci_dev *hdev)
6733{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006734 struct mgmt_pending_cmd *cmd;
Johan Hedberg84c61d92014-08-01 11:13:30 +03006735 struct mgmt_mode *cp;
6736
Johan Hedberg333ae952015-03-17 13:48:47 +02006737 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg84c61d92014-08-01 11:13:30 +03006738 if (!cmd)
6739 return false;
6740
6741 cp = cmd->param;
6742 if (!cp->val)
6743 return true;
6744
6745 return false;
6746}
6747
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07006748void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02006749 u8 link_type, u8 addr_type, u8 reason,
6750 bool mgmt_connected)
Johan Hedbergf7520542011-01-20 12:34:39 +02006751{
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02006752 struct mgmt_ev_device_disconnected ev;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006753 struct sock *sk = NULL;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006754
Johan Hedberg84c61d92014-08-01 11:13:30 +03006755 /* The connection is still in hci_conn_hash so test for 1
6756 * instead of 0 to know if this is the last one.
6757 */
6758 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
6759 cancel_delayed_work(&hdev->power_off);
6760 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberg8b064a32014-02-24 14:52:22 +02006761 }
6762
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02006763 if (!mgmt_connected)
6764 return;
6765
Andre Guedes57eb7762013-10-30 19:01:41 -03006766 if (link_type != ACL_LINK && link_type != LE_LINK)
6767 return;
6768
Johan Hedberg744cf192011-11-08 20:40:14 +02006769 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
Johan Hedbergf7520542011-01-20 12:34:39 +02006770
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02006771 bacpy(&ev.addr.bdaddr, bdaddr);
6772 ev.addr.type = link_to_bdaddr(link_type, addr_type);
6773 ev.reason = reason;
Johan Hedbergf7520542011-01-20 12:34:39 +02006774
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07006775 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006776
6777 if (sk)
Szymon Jancd97dcb62012-03-16 16:02:56 +01006778 sock_put(sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006779
Johan Hedberg124f6e32012-02-09 13:50:12 +02006780 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006781 hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006782}
6783
Marcel Holtmann78929242013-10-06 23:55:47 -07006784void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
6785 u8 link_type, u8 addr_type, u8 status)
Johan Hedberg8962ee72011-01-20 12:40:27 +02006786{
Andre Guedes3655bba2013-10-30 19:01:40 -03006787 u8 bdaddr_type = link_to_bdaddr(link_type, addr_type);
6788 struct mgmt_cp_disconnect *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006789 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006790
Jefferson Delfes36a75f12012-09-18 13:36:54 -04006791 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
6792 hdev);
6793
Johan Hedberg333ae952015-03-17 13:48:47 +02006794 cmd = pending_find(MGMT_OP_DISCONNECT, hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006795 if (!cmd)
Marcel Holtmann78929242013-10-06 23:55:47 -07006796 return;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006797
Andre Guedes3655bba2013-10-30 19:01:40 -03006798 cp = cmd->param;
6799
6800 if (bacmp(bdaddr, &cp->addr.bdaddr))
6801 return;
6802
6803 if (cp->addr.type != bdaddr_type)
6804 return;
6805
Johan Hedbergf5818c22014-12-05 13:36:02 +02006806 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03006807 mgmt_pending_remove(cmd);
Johan Hedbergf7520542011-01-20 12:34:39 +02006808}
Johan Hedberg17d5c042011-01-22 06:09:08 +02006809
Marcel Holtmann445608d2013-10-06 23:55:48 -07006810void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
6811 u8 addr_type, u8 status)
Johan Hedberg17d5c042011-01-22 06:09:08 +02006812{
6813 struct mgmt_ev_connect_failed ev;
Johan Hedbergc9910d02014-02-27 14:35:12 +02006814
Johan Hedberg84c61d92014-08-01 11:13:30 +03006815 /* The connection is still in hci_conn_hash so test for 1
6816 * instead of 0 to know if this is the last one.
6817 */
6818 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
6819 cancel_delayed_work(&hdev->power_off);
6820 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedbergc9910d02014-02-27 14:35:12 +02006821 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02006822
Johan Hedberg4c659c32011-11-07 23:13:39 +02006823 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03006824 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergca69b792011-11-11 18:10:00 +02006825 ev.status = mgmt_status(status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02006826
Marcel Holtmann445608d2013-10-06 23:55:48 -07006827 mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg17d5c042011-01-22 06:09:08 +02006828}
Johan Hedberg980e1a52011-01-22 06:10:07 +02006829
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07006830void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
Johan Hedberg980e1a52011-01-22 06:10:07 +02006831{
6832 struct mgmt_ev_pin_code_request ev;
6833
Johan Hedbergd8457692012-02-17 14:24:57 +02006834 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03006835 ev.addr.type = BDADDR_BREDR;
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02006836 ev.secure = secure;
Johan Hedberg980e1a52011-01-22 06:10:07 +02006837
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07006838 mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg980e1a52011-01-22 06:10:07 +02006839}
6840
Marcel Holtmanne669cf82013-10-15 14:26:21 -07006841void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
6842 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02006843{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006844 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02006845
Johan Hedberg333ae952015-03-17 13:48:47 +02006846 cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02006847 if (!cmd)
Marcel Holtmanne669cf82013-10-15 14:26:21 -07006848 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02006849
Johan Hedberg7776d1d2014-12-05 13:36:03 +02006850 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03006851 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02006852}
6853
Marcel Holtmann3eb38522013-10-15 14:26:22 -07006854void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
6855 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02006856{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006857 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02006858
Johan Hedberg333ae952015-03-17 13:48:47 +02006859 cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02006860 if (!cmd)
Marcel Holtmann3eb38522013-10-15 14:26:22 -07006861 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02006862
Johan Hedberg7776d1d2014-12-05 13:36:03 +02006863 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03006864 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02006865}
Johan Hedberga5c29682011-02-19 12:05:57 -03006866
Johan Hedberg744cf192011-11-08 20:40:14 +02006867int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg39adbff2014-03-20 08:18:14 +02006868 u8 link_type, u8 addr_type, u32 value,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006869 u8 confirm_hint)
Johan Hedberga5c29682011-02-19 12:05:57 -03006870{
6871 struct mgmt_ev_user_confirm_request ev;
6872
Johan Hedberg744cf192011-11-08 20:40:14 +02006873 BT_DBG("%s", hdev->name);
Johan Hedberga5c29682011-02-19 12:05:57 -03006874
Johan Hedberg272d90d2012-02-09 15:26:12 +02006875 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03006876 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberg55bc1a32011-04-28 11:28:56 -07006877 ev.confirm_hint = confirm_hint;
Johan Hedberg39adbff2014-03-20 08:18:14 +02006878 ev.value = cpu_to_le32(value);
Johan Hedberga5c29682011-02-19 12:05:57 -03006879
Johan Hedberg744cf192011-11-08 20:40:14 +02006880 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006881 NULL);
Johan Hedberga5c29682011-02-19 12:05:57 -03006882}
6883
Johan Hedberg272d90d2012-02-09 15:26:12 +02006884int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03006885 u8 link_type, u8 addr_type)
Brian Gix604086b2011-11-23 08:28:33 -08006886{
6887 struct mgmt_ev_user_passkey_request ev;
6888
6889 BT_DBG("%s", hdev->name);
6890
Johan Hedberg272d90d2012-02-09 15:26:12 +02006891 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03006892 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Brian Gix604086b2011-11-23 08:28:33 -08006893
6894 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006895 NULL);
Brian Gix604086b2011-11-23 08:28:33 -08006896}
6897
Brian Gix0df4c182011-11-16 13:53:13 -08006898static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03006899 u8 link_type, u8 addr_type, u8 status,
6900 u8 opcode)
Johan Hedberga5c29682011-02-19 12:05:57 -03006901{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006902 struct mgmt_pending_cmd *cmd;
Johan Hedberga5c29682011-02-19 12:05:57 -03006903
Johan Hedberg333ae952015-03-17 13:48:47 +02006904 cmd = pending_find(opcode, hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03006905 if (!cmd)
6906 return -ENOENT;
6907
Johan Hedberg7776d1d2014-12-05 13:36:03 +02006908 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03006909 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03006910
Johan Hedberg7776d1d2014-12-05 13:36:03 +02006911 return 0;
Johan Hedberga5c29682011-02-19 12:05:57 -03006912}
6913
Johan Hedberg744cf192011-11-08 20:40:14 +02006914int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006915 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03006916{
Johan Hedberg272d90d2012-02-09 15:26:12 +02006917 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006918 status, MGMT_OP_USER_CONFIRM_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03006919}
6920
Johan Hedberg272d90d2012-02-09 15:26:12 +02006921int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006922 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03006923{
Johan Hedberg272d90d2012-02-09 15:26:12 +02006924 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03006925 status,
6926 MGMT_OP_USER_CONFIRM_NEG_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03006927}
Johan Hedberg2a611692011-02-19 12:06:00 -03006928
Brian Gix604086b2011-11-23 08:28:33 -08006929int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006930 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08006931{
Johan Hedberg272d90d2012-02-09 15:26:12 +02006932 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006933 status, MGMT_OP_USER_PASSKEY_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08006934}
6935
Johan Hedberg272d90d2012-02-09 15:26:12 +02006936int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006937 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08006938{
Johan Hedberg272d90d2012-02-09 15:26:12 +02006939 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03006940 status,
6941 MGMT_OP_USER_PASSKEY_NEG_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08006942}
6943
Johan Hedberg92a25252012-09-06 18:39:26 +03006944int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
6945 u8 link_type, u8 addr_type, u32 passkey,
6946 u8 entered)
6947{
6948 struct mgmt_ev_passkey_notify ev;
6949
6950 BT_DBG("%s", hdev->name);
6951
6952 bacpy(&ev.addr.bdaddr, bdaddr);
6953 ev.addr.type = link_to_bdaddr(link_type, addr_type);
6954 ev.passkey = __cpu_to_le32(passkey);
6955 ev.entered = entered;
6956
6957 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
6958}
6959
Johan Hedberge1e930f2014-09-08 17:09:49 -07006960void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status)
Johan Hedberg2a611692011-02-19 12:06:00 -03006961{
6962 struct mgmt_ev_auth_failed ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006963 struct mgmt_pending_cmd *cmd;
Johan Hedberge1e930f2014-09-08 17:09:49 -07006964 u8 status = mgmt_status(hci_status);
Johan Hedberg2a611692011-02-19 12:06:00 -03006965
Johan Hedberge1e930f2014-09-08 17:09:49 -07006966 bacpy(&ev.addr.bdaddr, &conn->dst);
6967 ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
6968 ev.status = status;
Johan Hedberg2a611692011-02-19 12:06:00 -03006969
Johan Hedberge1e930f2014-09-08 17:09:49 -07006970 cmd = find_pairing(conn);
6971
6972 mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev),
6973 cmd ? cmd->sk : NULL);
6974
Johan Hedberga511b352014-12-11 21:45:45 +02006975 if (cmd) {
6976 cmd->cmd_complete(cmd, status);
6977 mgmt_pending_remove(cmd);
6978 }
Johan Hedberg2a611692011-02-19 12:06:00 -03006979}
Johan Hedbergb312b1612011-03-16 14:29:37 +02006980
Marcel Holtmann464996a2013-10-15 14:26:24 -07006981void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02006982{
6983 struct cmd_lookup match = { NULL, hdev };
Marcel Holtmann464996a2013-10-15 14:26:24 -07006984 bool changed;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02006985
6986 if (status) {
6987 u8 mgmt_err = mgmt_status(status);
6988 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006989 cmd_status_rsp, &mgmt_err);
Marcel Holtmann464996a2013-10-15 14:26:24 -07006990 return;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02006991 }
6992
Marcel Holtmann464996a2013-10-15 14:26:24 -07006993 if (test_bit(HCI_AUTH, &hdev->flags))
Marcel Holtmann238be782015-03-13 02:11:06 -07006994 changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY);
Marcel Holtmann464996a2013-10-15 14:26:24 -07006995 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07006996 changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02006997
Johan Hedberg33ef95e2012-02-16 23:56:27 +02006998 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006999 &match);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007000
Johan Hedberg47990ea2012-02-22 11:58:37 +02007001 if (changed)
Marcel Holtmann464996a2013-10-15 14:26:24 -07007002 new_settings(hdev, match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007003
7004 if (match.sk)
7005 sock_put(match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007006}
7007
Johan Hedberg890ea892013-03-15 17:06:52 -05007008static void clear_eir(struct hci_request *req)
Johan Hedbergcacaf522012-02-21 00:52:42 +02007009{
Johan Hedberg890ea892013-03-15 17:06:52 -05007010 struct hci_dev *hdev = req->hdev;
Johan Hedbergcacaf522012-02-21 00:52:42 +02007011 struct hci_cp_write_eir cp;
7012
Johan Hedberg976eb202012-10-24 21:12:01 +03007013 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05007014 return;
Johan Hedbergcacaf522012-02-21 00:52:42 +02007015
Johan Hedbergc80da272012-02-22 15:38:48 +02007016 memset(hdev->eir, 0, sizeof(hdev->eir));
7017
Johan Hedbergcacaf522012-02-21 00:52:42 +02007018 memset(&cp, 0, sizeof(cp));
7019
Johan Hedberg890ea892013-03-15 17:06:52 -05007020 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedbergcacaf522012-02-21 00:52:42 +02007021}
7022
Marcel Holtmann3e248562013-10-15 14:26:25 -07007023void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007024{
7025 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg890ea892013-03-15 17:06:52 -05007026 struct hci_request req;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007027 bool changed = false;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007028
7029 if (status) {
7030 u8 mgmt_err = mgmt_status(status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007031
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007032 if (enable && hci_dev_test_and_clear_flag(hdev,
7033 HCI_SSP_ENABLED)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07007034 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmann3e248562013-10-15 14:26:25 -07007035 new_settings(hdev, NULL);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007036 }
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007037
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007038 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
7039 &mgmt_err);
Marcel Holtmann3e248562013-10-15 14:26:25 -07007040 return;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007041 }
7042
7043 if (enable) {
Marcel Holtmann238be782015-03-13 02:11:06 -07007044 changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007045 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007046 changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007047 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007048 changed = hci_dev_test_and_clear_flag(hdev,
7049 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007050 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07007051 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007052 }
7053
7054 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
7055
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007056 if (changed)
Marcel Holtmann3e248562013-10-15 14:26:25 -07007057 new_settings(hdev, match.sk);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007058
Johan Hedberg5fc6ebb2012-02-22 15:10:59 +02007059 if (match.sk)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007060 sock_put(match.sk);
7061
Johan Hedberg890ea892013-03-15 17:06:52 -05007062 hci_req_init(&req, hdev);
7063
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007064 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
7065 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03007066 hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE,
7067 sizeof(enable), &enable);
Johan Hedbergb1a89172015-11-25 16:15:42 +02007068 __hci_req_update_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03007069 } else {
Johan Hedberg890ea892013-03-15 17:06:52 -05007070 clear_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03007071 }
Johan Hedberg890ea892013-03-15 17:06:52 -05007072
7073 hci_req_run(&req, NULL);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007074}
7075
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007076static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg90e70452012-02-23 23:09:40 +02007077{
7078 struct cmd_lookup *match = data;
7079
Johan Hedberg90e70452012-02-23 23:09:40 +02007080 if (match->sk == NULL) {
7081 match->sk = cmd->sk;
7082 sock_hold(match->sk);
7083 }
Johan Hedberg90e70452012-02-23 23:09:40 +02007084}
7085
Marcel Holtmann4e1b0242013-10-15 14:26:26 -07007086void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
7087 u8 status)
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007088{
Johan Hedberg90e70452012-02-23 23:09:40 +02007089 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007090
Johan Hedberg92da6092013-03-15 17:06:55 -05007091 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
7092 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
7093 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
Johan Hedberg90e70452012-02-23 23:09:40 +02007094
7095 if (!status)
Marcel Holtmannf6b77122015-03-14 19:28:05 -07007096 mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
7097 dev_class, 3, NULL);
Johan Hedberg90e70452012-02-23 23:09:40 +02007098
7099 if (match.sk)
7100 sock_put(match.sk);
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007101}
7102
Marcel Holtmann7667da32013-10-15 14:26:27 -07007103void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
Johan Hedbergb312b1612011-03-16 14:29:37 +02007104{
Johan Hedbergb312b1612011-03-16 14:29:37 +02007105 struct mgmt_cp_set_local_name ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007106 struct mgmt_pending_cmd *cmd;
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02007107
Johan Hedberg13928972013-03-15 17:07:00 -05007108 if (status)
Marcel Holtmann7667da32013-10-15 14:26:27 -07007109 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02007110
7111 memset(&ev, 0, sizeof(ev));
7112 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02007113 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
Johan Hedbergb312b1612011-03-16 14:29:37 +02007114
Johan Hedberg333ae952015-03-17 13:48:47 +02007115 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05007116 if (!cmd) {
7117 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
Johan Hedbergb312b1612011-03-16 14:29:37 +02007118
Johan Hedberg13928972013-03-15 17:07:00 -05007119 /* If this is a HCI command related to powering on the
7120 * HCI dev don't send any mgmt signals.
7121 */
Johan Hedberg333ae952015-03-17 13:48:47 +02007122 if (pending_find(MGMT_OP_SET_POWERED, hdev))
Marcel Holtmann7667da32013-10-15 14:26:27 -07007123 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02007124 }
7125
Marcel Holtmannf6b77122015-03-14 19:28:05 -07007126 mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
7127 cmd ? cmd->sk : NULL);
Johan Hedbergb312b1612011-03-16 14:29:37 +02007128}
Szymon Jancc35938b2011-03-22 13:12:21 +01007129
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007130static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16])
7131{
7132 int i;
7133
7134 for (i = 0; i < uuid_count; i++) {
7135 if (!memcmp(uuid, uuids[i], 16))
7136 return true;
7137 }
7138
7139 return false;
7140}
7141
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007142static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16])
7143{
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007144 u16 parsed = 0;
7145
7146 while (parsed < eir_len) {
7147 u8 field_len = eir[0];
7148 u8 uuid[16];
7149 int i;
7150
7151 if (field_len == 0)
7152 break;
7153
7154 if (eir_len - parsed < field_len + 1)
7155 break;
7156
7157 switch (eir[1]) {
7158 case EIR_UUID16_ALL:
7159 case EIR_UUID16_SOME:
7160 for (i = 0; i + 3 <= field_len; i += 2) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02007161 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007162 uuid[13] = eir[i + 3];
7163 uuid[12] = eir[i + 2];
7164 if (has_uuid(uuid, uuid_count, uuids))
7165 return true;
7166 }
7167 break;
7168 case EIR_UUID32_ALL:
7169 case EIR_UUID32_SOME:
7170 for (i = 0; i + 5 <= field_len; i += 4) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02007171 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007172 uuid[15] = eir[i + 5];
7173 uuid[14] = eir[i + 4];
7174 uuid[13] = eir[i + 3];
7175 uuid[12] = eir[i + 2];
7176 if (has_uuid(uuid, uuid_count, uuids))
7177 return true;
7178 }
7179 break;
7180 case EIR_UUID128_ALL:
7181 case EIR_UUID128_SOME:
7182 for (i = 0; i + 17 <= field_len; i += 16) {
7183 memcpy(uuid, eir + i + 2, 16);
7184 if (has_uuid(uuid, uuid_count, uuids))
7185 return true;
7186 }
7187 break;
7188 }
7189
7190 parsed += field_len + 1;
7191 eir += field_len + 1;
7192 }
7193
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007194 return false;
7195}
7196
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007197static void restart_le_scan(struct hci_dev *hdev)
7198{
7199 /* If controller is not scanning we are done. */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007200 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007201 return;
7202
7203 if (time_after(jiffies + DISCOV_LE_RESTART_DELAY,
7204 hdev->discovery.scan_start +
7205 hdev->discovery.scan_duration))
7206 return;
7207
Johan Hedberg7c1fbed2015-11-11 08:11:23 +02007208 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart,
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007209 DISCOV_LE_RESTART_DELAY);
7210}
7211
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007212static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir,
7213 u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
7214{
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007215 /* If a RSSI threshold has been specified, and
7216 * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with
7217 * a RSSI smaller than the RSSI threshold will be dropped. If the quirk
7218 * is set, let it through for further processing, as we might need to
7219 * restart the scan.
7220 *
7221 * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry,
7222 * the results are also dropped.
7223 */
7224 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
7225 (rssi == HCI_RSSI_INVALID ||
7226 (rssi < hdev->discovery.rssi &&
7227 !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))
7228 return false;
7229
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08007230 if (hdev->discovery.uuid_count != 0) {
7231 /* If a list of UUIDs is provided in filter, results with no
7232 * matching UUID should be dropped.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007233 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08007234 if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count,
7235 hdev->discovery.uuids) &&
7236 !eir_has_uuids(scan_rsp, scan_rsp_len,
7237 hdev->discovery.uuid_count,
7238 hdev->discovery.uuids))
7239 return false;
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007240 }
7241
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08007242 /* If duplicate filtering does not report RSSI changes, then restart
7243 * scanning to ensure updated result with updated RSSI values.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007244 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08007245 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) {
7246 restart_le_scan(hdev);
7247
7248 /* Validate RSSI value against the RSSI threshold once more. */
7249 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
7250 rssi < hdev->discovery.rssi)
7251 return false;
7252 }
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007253
7254 return true;
7255}
7256
Marcel Holtmann901801b2013-10-06 23:55:51 -07007257void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
Marcel Holtmannaf589252014-07-01 14:11:20 +02007258 u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
7259 u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
Johan Hedberge17acd42011-03-30 23:57:16 +03007260{
Johan Hedberge319d2e2012-01-15 19:51:59 +02007261 char buf[512];
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007262 struct mgmt_ev_device_found *ev = (void *)buf;
Johan Hedberg1dc06092012-01-15 21:01:23 +02007263 size_t ev_size;
Johan Hedberge17acd42011-03-30 23:57:16 +03007264
Johan Hedberg75ce2082014-07-02 22:42:01 +03007265 /* Don't send events for a non-kernel initiated discovery. With
7266 * LE one exception is if we have pend_le_reports > 0 in which
7267 * case we're doing passive scanning and want these events.
7268 */
7269 if (!hci_discovery_active(hdev)) {
7270 if (link_type == ACL_LINK)
7271 return;
Johan Hedberg66f84552014-07-04 12:37:18 +03007272 if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports))
Johan Hedberg75ce2082014-07-02 22:42:01 +03007273 return;
7274 }
Andre Guedes12602d02013-04-30 15:29:40 -03007275
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08007276 if (hdev->discovery.result_filtering) {
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007277 /* We are using service discovery */
7278 if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp,
7279 scan_rsp_len))
7280 return;
7281 }
Marcel Holtmannbda157a2014-12-05 10:55:56 +01007282
Johan Hedberg78b781c2016-01-05 13:19:32 +02007283 if (hdev->discovery.limited) {
7284 /* Check for limited discoverable bit */
7285 if (dev_class) {
7286 if (!(dev_class[1] & 0x20))
7287 return;
7288 } else {
7289 u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL);
7290 if (!flags || !(flags[0] & LE_AD_LIMITED))
7291 return;
7292 }
7293 }
7294
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02007295 /* Make sure that the buffer is big enough. The 5 extra bytes
7296 * are for the potential CoD field.
7297 */
7298 if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf))
Marcel Holtmann901801b2013-10-06 23:55:51 -07007299 return;
Andre Guedes7d262f82012-01-10 18:20:49 -03007300
Johan Hedberg1dc06092012-01-15 21:01:23 +02007301 memset(buf, 0, sizeof(buf));
7302
Marcel Holtmannda25cf62014-12-05 13:03:35 +01007303 /* In case of device discovery with BR/EDR devices (pre 1.2), the
7304 * RSSI value was reported as 0 when not available. This behavior
7305 * is kept when using device discovery. This is required for full
7306 * backwards compatibility with the API.
7307 *
7308 * However when using service discovery, the value 127 will be
7309 * returned when the RSSI is not available.
7310 */
Szymon Janc91200e92015-01-22 16:57:05 +01007311 if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi &&
7312 link_type == ACL_LINK)
Marcel Holtmannefb25132014-12-05 13:03:34 +01007313 rssi = 0;
7314
Johan Hedberg841c5642014-07-07 12:45:54 +03007315 bacpy(&ev->addr.bdaddr, bdaddr);
7316 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberge319d2e2012-01-15 19:51:59 +02007317 ev->rssi = rssi;
Marcel Holtmannaf589252014-07-01 14:11:20 +02007318 ev->flags = cpu_to_le32(flags);
Johan Hedberge17acd42011-03-30 23:57:16 +03007319
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007320 if (eir_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007321 /* Copy EIR or advertising data into event */
Johan Hedberge319d2e2012-01-15 19:51:59 +02007322 memcpy(ev->eir, eir, eir_len);
Johan Hedberge17acd42011-03-30 23:57:16 +03007323
Johan Hedberg0d3b7f62016-01-05 13:19:31 +02007324 if (dev_class && !eir_get_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
7325 NULL))
Johan Hedberg1dc06092012-01-15 21:01:23 +02007326 eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007327 dev_class, 3);
Johan Hedberg1dc06092012-01-15 21:01:23 +02007328
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007329 if (scan_rsp_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007330 /* Append scan response data to event */
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02007331 memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len);
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007332
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02007333 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
7334 ev_size = sizeof(*ev) + eir_len + scan_rsp_len;
Andre Guedesf8523592011-09-09 18:56:26 -03007335
Marcel Holtmann901801b2013-10-06 23:55:51 -07007336 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL);
Johan Hedberge17acd42011-03-30 23:57:16 +03007337}
Johan Hedberga88a9652011-03-30 13:18:12 +03007338
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07007339void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
7340 u8 addr_type, s8 rssi, u8 *name, u8 name_len)
Johan Hedberga88a9652011-03-30 13:18:12 +03007341{
Johan Hedbergb644ba32012-01-17 21:48:47 +02007342 struct mgmt_ev_device_found *ev;
7343 char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2];
7344 u16 eir_len;
Johan Hedberga88a9652011-03-30 13:18:12 +03007345
Johan Hedbergb644ba32012-01-17 21:48:47 +02007346 ev = (struct mgmt_ev_device_found *) buf;
Johan Hedberga88a9652011-03-30 13:18:12 +03007347
Johan Hedbergb644ba32012-01-17 21:48:47 +02007348 memset(buf, 0, sizeof(buf));
Johan Hedberga88a9652011-03-30 13:18:12 +03007349
Johan Hedbergb644ba32012-01-17 21:48:47 +02007350 bacpy(&ev->addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007351 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007352 ev->rssi = rssi;
7353
7354 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007355 name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007356
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02007357 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007358
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07007359 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL);
Johan Hedberga88a9652011-03-30 13:18:12 +03007360}
Johan Hedberg314b2382011-04-27 10:29:57 -04007361
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07007362void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
Johan Hedberg314b2382011-04-27 10:29:57 -04007363{
Johan Hedbergf963e8e2012-02-20 23:30:44 +02007364 struct mgmt_ev_discovering ev;
Johan Hedberg164a6e72011-11-01 17:06:44 +02007365
Andre Guedes343fb142011-11-22 17:14:19 -03007366 BT_DBG("%s discovering %u", hdev->name, discovering);
7367
Johan Hedbergf963e8e2012-02-20 23:30:44 +02007368 memset(&ev, 0, sizeof(ev));
7369 ev.type = hdev->discovery.type;
7370 ev.discovering = discovering;
7371
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07007372 mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg314b2382011-04-27 10:29:57 -04007373}
Antti Julku5e762442011-08-25 16:48:02 +03007374
Johan Hedberg6d785aa32015-03-06 21:08:51 +02007375static struct hci_mgmt_chan chan = {
7376 .channel = HCI_CHANNEL_CONTROL,
7377 .handler_count = ARRAY_SIZE(mgmt_handlers),
7378 .handlers = mgmt_handlers,
Johan Hedberg88b94ce2015-03-17 13:48:49 +02007379 .hdev_init = mgmt_init_hdev,
Johan Hedberg6d785aa32015-03-06 21:08:51 +02007380};
7381
7382int mgmt_init(void)
7383{
7384 return hci_mgmt_chan_register(&chan);
7385}
7386
7387void mgmt_exit(void)
7388{
7389 hci_mgmt_chan_unregister(&chan);
7390}