blob: d8c66663ade8cd77ee7894c9ac47c3e9c0fbf5a8 [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 Hedberg4bc58f52014-05-20 09:45:47 +030032#include <net/bluetooth/l2cap.h>
Johan Hedberg03811012010-12-08 00:21:06 +020033#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070034
35#include "smp.h"
Johan Hedberg03811012010-12-08 00:21:06 +020036
Johan Hedberg2da9c552012-02-17 14:39:28 +020037#define MGMT_VERSION 1
Marcel Holtmann730f0912014-06-28 12:36:10 +020038#define MGMT_REVISION 7
Johan Hedberg02d98122010-12-13 21:07:04 +020039
Johan Hedberge70bb2e2012-02-13 16:59:33 +020040static const u16 mgmt_commands[] = {
41 MGMT_OP_READ_INDEX_LIST,
42 MGMT_OP_READ_INFO,
43 MGMT_OP_SET_POWERED,
44 MGMT_OP_SET_DISCOVERABLE,
45 MGMT_OP_SET_CONNECTABLE,
46 MGMT_OP_SET_FAST_CONNECTABLE,
Johan Hedbergb2939472014-07-30 09:22:23 +030047 MGMT_OP_SET_BONDABLE,
Johan Hedberge70bb2e2012-02-13 16:59:33 +020048 MGMT_OP_SET_LINK_SECURITY,
49 MGMT_OP_SET_SSP,
50 MGMT_OP_SET_HS,
51 MGMT_OP_SET_LE,
52 MGMT_OP_SET_DEV_CLASS,
53 MGMT_OP_SET_LOCAL_NAME,
54 MGMT_OP_ADD_UUID,
55 MGMT_OP_REMOVE_UUID,
56 MGMT_OP_LOAD_LINK_KEYS,
57 MGMT_OP_LOAD_LONG_TERM_KEYS,
58 MGMT_OP_DISCONNECT,
59 MGMT_OP_GET_CONNECTIONS,
60 MGMT_OP_PIN_CODE_REPLY,
61 MGMT_OP_PIN_CODE_NEG_REPLY,
62 MGMT_OP_SET_IO_CAPABILITY,
63 MGMT_OP_PAIR_DEVICE,
64 MGMT_OP_CANCEL_PAIR_DEVICE,
65 MGMT_OP_UNPAIR_DEVICE,
66 MGMT_OP_USER_CONFIRM_REPLY,
67 MGMT_OP_USER_CONFIRM_NEG_REPLY,
68 MGMT_OP_USER_PASSKEY_REPLY,
69 MGMT_OP_USER_PASSKEY_NEG_REPLY,
70 MGMT_OP_READ_LOCAL_OOB_DATA,
71 MGMT_OP_ADD_REMOTE_OOB_DATA,
72 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
73 MGMT_OP_START_DISCOVERY,
74 MGMT_OP_STOP_DISCOVERY,
75 MGMT_OP_CONFIRM_NAME,
76 MGMT_OP_BLOCK_DEVICE,
77 MGMT_OP_UNBLOCK_DEVICE,
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -070078 MGMT_OP_SET_DEVICE_ID,
Johan Hedberg4375f102013-09-25 13:26:10 +030079 MGMT_OP_SET_ADVERTISING,
Johan Hedberg0663ca22013-10-02 13:43:14 +030080 MGMT_OP_SET_BREDR,
Marcel Holtmannd13eafc2013-10-02 04:41:30 -070081 MGMT_OP_SET_STATIC_ADDRESS,
Marcel Holtmann7f72134e2013-10-11 14:44:58 -070082 MGMT_OP_SET_SCAN_PARAMS,
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -080083 MGMT_OP_SET_SECURE_CONN,
Marcel Holtmann4e39ac82014-01-31 11:55:22 -080084 MGMT_OP_SET_DEBUG_KEYS,
Johan Hedberg62b04cd2014-02-23 19:42:27 +020085 MGMT_OP_SET_PRIVACY,
Johan Hedberg41edf162014-02-18 10:19:35 +020086 MGMT_OP_LOAD_IRKS,
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +020087 MGMT_OP_GET_CONN_INFO,
Johan Hedberg95868422014-06-28 17:54:07 +030088 MGMT_OP_GET_CLOCK_INFO,
Marcel Holtmann2faade52014-06-29 19:44:03 +020089 MGMT_OP_ADD_DEVICE,
90 MGMT_OP_REMOVE_DEVICE,
Johan Hedberga26f3dc2014-07-02 17:37:29 +030091 MGMT_OP_LOAD_CONN_PARAM,
Marcel Holtmann73d1df22014-07-02 22:10:52 +020092 MGMT_OP_READ_UNCONF_INDEX_LIST,
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +020093 MGMT_OP_READ_CONFIG_INFO,
Marcel Holtmanndbece372014-07-04 18:11:55 +020094 MGMT_OP_SET_EXTERNAL_CONFIG,
Marcel Holtmann9713c172014-07-06 12:11:15 +020095 MGMT_OP_SET_PUBLIC_ADDRESS,
Johan Hedberge70bb2e2012-02-13 16:59:33 +020096};
97
98static const u16 mgmt_events[] = {
99 MGMT_EV_CONTROLLER_ERROR,
100 MGMT_EV_INDEX_ADDED,
101 MGMT_EV_INDEX_REMOVED,
102 MGMT_EV_NEW_SETTINGS,
103 MGMT_EV_CLASS_OF_DEV_CHANGED,
104 MGMT_EV_LOCAL_NAME_CHANGED,
105 MGMT_EV_NEW_LINK_KEY,
106 MGMT_EV_NEW_LONG_TERM_KEY,
107 MGMT_EV_DEVICE_CONNECTED,
108 MGMT_EV_DEVICE_DISCONNECTED,
109 MGMT_EV_CONNECT_FAILED,
110 MGMT_EV_PIN_CODE_REQUEST,
111 MGMT_EV_USER_CONFIRM_REQUEST,
112 MGMT_EV_USER_PASSKEY_REQUEST,
113 MGMT_EV_AUTH_FAILED,
114 MGMT_EV_DEVICE_FOUND,
115 MGMT_EV_DISCOVERING,
116 MGMT_EV_DEVICE_BLOCKED,
117 MGMT_EV_DEVICE_UNBLOCKED,
118 MGMT_EV_DEVICE_UNPAIRED,
Johan Hedberg92a25252012-09-06 18:39:26 +0300119 MGMT_EV_PASSKEY_NOTIFY,
Marcel Holtmann1b60ef22014-02-21 21:35:30 -0800120 MGMT_EV_NEW_IRK,
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700121 MGMT_EV_NEW_CSRK,
Marcel Holtmann8afef092014-06-29 22:28:34 +0200122 MGMT_EV_DEVICE_ADDED,
123 MGMT_EV_DEVICE_REMOVED,
Andre Guedesffb5a8272014-07-01 18:10:11 -0300124 MGMT_EV_NEW_CONN_PARAM,
Marcel Holtmann0602a8a2014-07-02 21:30:54 +0200125 MGMT_EV_UNCONF_INDEX_ADDED,
Marcel Holtmannedd38962014-07-02 21:30:55 +0200126 MGMT_EV_UNCONF_INDEX_REMOVED,
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200127 MGMT_EV_NEW_CONFIG_OPTIONS,
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200128};
129
Marcel Holtmann17b02e62012-03-01 14:32:37 -0800130#define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)
Johan Hedberg7d785252011-12-15 00:47:39 +0200131
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200132struct pending_cmd {
133 struct list_head list;
Johan Hedbergfc2f4b12011-11-09 13:58:56 +0200134 u16 opcode;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200135 int index;
Szymon Jancc68fb7f2011-03-22 13:12:19 +0100136 void *param;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200137 struct sock *sk;
Johan Hedberge9a416b2011-02-19 12:05:56 -0300138 void *user_data;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200139};
140
Johan Hedbergca69b792011-11-11 18:10:00 +0200141/* HCI to MGMT error code conversion table */
142static u8 mgmt_status_table[] = {
143 MGMT_STATUS_SUCCESS,
144 MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */
145 MGMT_STATUS_NOT_CONNECTED, /* No Connection */
146 MGMT_STATUS_FAILED, /* Hardware Failure */
147 MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */
148 MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */
Johan Hedbergeadd6632014-01-13 17:15:53 +0200149 MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */
Johan Hedbergca69b792011-11-11 18:10:00 +0200150 MGMT_STATUS_NO_RESOURCES, /* Memory Full */
151 MGMT_STATUS_TIMEOUT, /* Connection Timeout */
152 MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */
153 MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */
154 MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */
155 MGMT_STATUS_BUSY, /* Command Disallowed */
156 MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */
157 MGMT_STATUS_REJECTED, /* Rejected Security */
158 MGMT_STATUS_REJECTED, /* Rejected Personal */
159 MGMT_STATUS_TIMEOUT, /* Host Timeout */
160 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */
161 MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */
162 MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */
163 MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */
164 MGMT_STATUS_DISCONNECTED, /* OE Power Off */
165 MGMT_STATUS_DISCONNECTED, /* Connection Terminated */
166 MGMT_STATUS_BUSY, /* Repeated Attempts */
167 MGMT_STATUS_REJECTED, /* Pairing Not Allowed */
168 MGMT_STATUS_FAILED, /* Unknown LMP PDU */
169 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */
170 MGMT_STATUS_REJECTED, /* SCO Offset Rejected */
171 MGMT_STATUS_REJECTED, /* SCO Interval Rejected */
172 MGMT_STATUS_REJECTED, /* Air Mode Rejected */
173 MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */
174 MGMT_STATUS_FAILED, /* Unspecified Error */
175 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */
176 MGMT_STATUS_FAILED, /* Role Change Not Allowed */
177 MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */
178 MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */
179 MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */
180 MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */
181 MGMT_STATUS_FAILED, /* Unit Link Key Used */
182 MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */
183 MGMT_STATUS_TIMEOUT, /* Instant Passed */
184 MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */
185 MGMT_STATUS_FAILED, /* Transaction Collision */
186 MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */
187 MGMT_STATUS_REJECTED, /* QoS Rejected */
188 MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */
189 MGMT_STATUS_REJECTED, /* Insufficient Security */
190 MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */
191 MGMT_STATUS_BUSY, /* Role Switch Pending */
192 MGMT_STATUS_FAILED, /* Slot Violation */
193 MGMT_STATUS_FAILED, /* Role Switch Failed */
194 MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */
195 MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */
196 MGMT_STATUS_BUSY, /* Host Busy Pairing */
197 MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */
198 MGMT_STATUS_BUSY, /* Controller Busy */
199 MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */
200 MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */
201 MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */
202 MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */
203 MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */
204};
205
206static u8 mgmt_status(u8 hci_status)
207{
208 if (hci_status < ARRAY_SIZE(mgmt_status_table))
209 return mgmt_status_table[hci_status];
210
211 return MGMT_STATUS_FAILED;
212}
213
Marcel Holtmann04c60f052014-07-04 19:06:22 +0200214static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len,
215 struct sock *skip_sk)
216{
217 struct sk_buff *skb;
218 struct mgmt_hdr *hdr;
219
220 skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL);
221 if (!skb)
222 return -ENOMEM;
223
224 hdr = (void *) skb_put(skb, sizeof(*hdr));
225 hdr->opcode = cpu_to_le16(event);
226 if (hdev)
227 hdr->index = cpu_to_le16(hdev->id);
228 else
229 hdr->index = cpu_to_le16(MGMT_INDEX_NONE);
230 hdr->len = cpu_to_le16(data_len);
231
232 if (data)
233 memcpy(skb_put(skb, data_len), data, data_len);
234
235 /* Time stamp */
236 __net_timestamp(skb);
237
238 hci_send_to_control(skb, skip_sk);
239 kfree_skb(skb);
240
241 return 0;
242}
243
Szymon Janc4e51eae2011-02-25 19:05:48 +0100244static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status)
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200245{
246 struct sk_buff *skb;
247 struct mgmt_hdr *hdr;
248 struct mgmt_ev_cmd_status *ev;
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300249 int err;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200250
Szymon Janc34eb5252011-02-28 14:10:08 +0100251 BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status);
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200252
Andre Guedes790eff42012-06-07 19:05:46 -0300253 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL);
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200254 if (!skb)
255 return -ENOMEM;
256
257 hdr = (void *) skb_put(skb, sizeof(*hdr));
258
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700259 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS);
Szymon Janc4e51eae2011-02-25 19:05:48 +0100260 hdr->index = cpu_to_le16(index);
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200261 hdr->len = cpu_to_le16(sizeof(*ev));
262
263 ev = (void *) skb_put(skb, sizeof(*ev));
264 ev->status = status;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200265 ev->opcode = cpu_to_le16(cmd);
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200266
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300267 err = sock_queue_rcv_skb(sk, skb);
268 if (err < 0)
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200269 kfree_skb(skb);
270
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300271 return err;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200272}
273
Johan Hedbergaee9b212012-02-18 15:07:59 +0200274static int cmd_complete(struct sock *sk, u16 index, u16 cmd, u8 status,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300275 void *rp, size_t rp_len)
Johan Hedberg02d98122010-12-13 21:07:04 +0200276{
277 struct sk_buff *skb;
278 struct mgmt_hdr *hdr;
279 struct mgmt_ev_cmd_complete *ev;
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300280 int err;
Johan Hedberg02d98122010-12-13 21:07:04 +0200281
282 BT_DBG("sock %p", sk);
283
Andre Guedes790eff42012-06-07 19:05:46 -0300284 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL);
Johan Hedberg02d98122010-12-13 21:07:04 +0200285 if (!skb)
286 return -ENOMEM;
287
288 hdr = (void *) skb_put(skb, sizeof(*hdr));
Johan Hedberga38528f2011-01-22 06:46:43 +0200289
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700290 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE);
Szymon Janc4e51eae2011-02-25 19:05:48 +0100291 hdr->index = cpu_to_le16(index);
Johan Hedberga38528f2011-01-22 06:46:43 +0200292 hdr->len = cpu_to_le16(sizeof(*ev) + rp_len);
Johan Hedberg02d98122010-12-13 21:07:04 +0200293
Johan Hedberga38528f2011-01-22 06:46:43 +0200294 ev = (void *) skb_put(skb, sizeof(*ev) + rp_len);
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200295 ev->opcode = cpu_to_le16(cmd);
Johan Hedbergaee9b212012-02-18 15:07:59 +0200296 ev->status = status;
Szymon Janc8020c162011-02-28 14:09:50 +0100297
298 if (rp)
299 memcpy(ev->data, rp, rp_len);
Johan Hedberg02d98122010-12-13 21:07:04 +0200300
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300301 err = sock_queue_rcv_skb(sk, skb);
302 if (err < 0)
Johan Hedberg02d98122010-12-13 21:07:04 +0200303 kfree_skb(skb);
304
Marcel Holtmanne5f0e152012-02-22 11:59:01 +0100305 return err;
Johan Hedberg02d98122010-12-13 21:07:04 +0200306}
307
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300308static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
309 u16 data_len)
Johan Hedberga38528f2011-01-22 06:46:43 +0200310{
311 struct mgmt_rp_read_version rp;
312
313 BT_DBG("sock %p", sk);
314
315 rp.version = MGMT_VERSION;
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700316 rp.revision = cpu_to_le16(MGMT_REVISION);
Johan Hedberga38528f2011-01-22 06:46:43 +0200317
Johan Hedbergaee9b212012-02-18 15:07:59 +0200318 return cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, &rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300319 sizeof(rp));
Johan Hedberga38528f2011-01-22 06:46:43 +0200320}
321
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300322static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
323 u16 data_len)
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200324{
325 struct mgmt_rp_read_commands *rp;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200326 const u16 num_commands = ARRAY_SIZE(mgmt_commands);
327 const u16 num_events = ARRAY_SIZE(mgmt_events);
Andrei Emeltchenko2e3c35e2012-03-14 18:54:15 +0200328 __le16 *opcode;
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200329 size_t rp_size;
330 int i, err;
331
332 BT_DBG("sock %p", sk);
333
334 rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16));
335
336 rp = kmalloc(rp_size, GFP_KERNEL);
337 if (!rp)
338 return -ENOMEM;
339
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700340 rp->num_commands = cpu_to_le16(num_commands);
341 rp->num_events = cpu_to_le16(num_events);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200342
343 for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++)
344 put_unaligned_le16(mgmt_commands[i], opcode);
345
346 for (i = 0; i < num_events; i++, opcode++)
347 put_unaligned_le16(mgmt_events[i], opcode);
348
Johan Hedbergaee9b212012-02-18 15:07:59 +0200349 err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300350 rp_size);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200351 kfree(rp);
352
353 return err;
354}
355
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300356static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
357 u16 data_len)
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200358{
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200359 struct mgmt_rp_read_index_list *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200360 struct hci_dev *d;
Johan Hedberga38528f2011-01-22 06:46:43 +0200361 size_t rp_len;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200362 u16 count;
Johan Hedberg476e44c2012-10-19 20:10:46 +0300363 int err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200364
365 BT_DBG("sock %p", sk);
366
367 read_lock(&hci_dev_list_lock);
368
369 count = 0;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +0300370 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200371 if (d->dev_type == HCI_BREDR &&
372 !test_bit(HCI_UNCONFIGURED, &d->dev_flags))
Marcel Holtmann1514b892013-10-06 08:25:01 -0700373 count++;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200374 }
375
Johan Hedberga38528f2011-01-22 06:46:43 +0200376 rp_len = sizeof(*rp) + (2 * count);
377 rp = kmalloc(rp_len, GFP_ATOMIC);
378 if (!rp) {
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100379 read_unlock(&hci_dev_list_lock);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200380 return -ENOMEM;
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100381 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200382
Johan Hedberg476e44c2012-10-19 20:10:46 +0300383 count = 0;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200384 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200385 if (test_bit(HCI_SETUP, &d->dev_flags) ||
Marcel Holtmannd603b76b2014-07-06 12:11:14 +0200386 test_bit(HCI_CONFIG, &d->dev_flags) ||
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200387 test_bit(HCI_USER_CHANNEL, &d->dev_flags))
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200388 continue;
389
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200390 /* Devices marked as raw-only are neither configured
391 * nor unconfigured controllers.
392 */
393 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
Marcel Holtmann0736cfa2013-08-26 21:40:51 -0700394 continue;
395
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200396 if (d->dev_type == HCI_BREDR &&
397 !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) {
Marcel Holtmann1514b892013-10-06 08:25:01 -0700398 rp->index[count++] = cpu_to_le16(d->id);
399 BT_DBG("Added hci%u", d->id);
400 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200401 }
402
Johan Hedberg476e44c2012-10-19 20:10:46 +0300403 rp->num_controllers = cpu_to_le16(count);
404 rp_len = sizeof(*rp) + (2 * count);
405
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200406 read_unlock(&hci_dev_list_lock);
407
Johan Hedbergaee9b212012-02-18 15:07:59 +0200408 err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, 0, rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300409 rp_len);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200410
Johan Hedberga38528f2011-01-22 06:46:43 +0200411 kfree(rp);
412
413 return err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200414}
415
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200416static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
417 void *data, u16 data_len)
418{
419 struct mgmt_rp_read_unconf_index_list *rp;
420 struct hci_dev *d;
421 size_t rp_len;
422 u16 count;
423 int err;
424
425 BT_DBG("sock %p", sk);
426
427 read_lock(&hci_dev_list_lock);
428
429 count = 0;
430 list_for_each_entry(d, &hci_dev_list, list) {
431 if (d->dev_type == HCI_BREDR &&
432 test_bit(HCI_UNCONFIGURED, &d->dev_flags))
433 count++;
434 }
435
436 rp_len = sizeof(*rp) + (2 * count);
437 rp = kmalloc(rp_len, GFP_ATOMIC);
438 if (!rp) {
439 read_unlock(&hci_dev_list_lock);
440 return -ENOMEM;
441 }
442
443 count = 0;
444 list_for_each_entry(d, &hci_dev_list, list) {
445 if (test_bit(HCI_SETUP, &d->dev_flags) ||
Marcel Holtmannd603b76b2014-07-06 12:11:14 +0200446 test_bit(HCI_CONFIG, &d->dev_flags) ||
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200447 test_bit(HCI_USER_CHANNEL, &d->dev_flags))
448 continue;
449
450 /* Devices marked as raw-only are neither configured
451 * nor unconfigured controllers.
452 */
453 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
454 continue;
455
456 if (d->dev_type == HCI_BREDR &&
457 test_bit(HCI_UNCONFIGURED, &d->dev_flags)) {
458 rp->index[count++] = cpu_to_le16(d->id);
459 BT_DBG("Added hci%u", d->id);
460 }
461 }
462
463 rp->num_controllers = cpu_to_le16(count);
464 rp_len = sizeof(*rp) + (2 * count);
465
466 read_unlock(&hci_dev_list_lock);
467
468 err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_UNCONF_INDEX_LIST,
469 0, rp, rp_len);
470
471 kfree(rp);
472
473 return err;
474}
475
Marcel Holtmanndbece372014-07-04 18:11:55 +0200476static bool is_configured(struct hci_dev *hdev)
477{
478 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
479 !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags))
480 return false;
481
482 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
483 !bacmp(&hdev->public_addr, BDADDR_ANY))
484 return false;
485
486 return true;
487}
488
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200489static __le32 get_missing_options(struct hci_dev *hdev)
490{
491 u32 options = 0;
492
Marcel Holtmanndbece372014-07-04 18:11:55 +0200493 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
494 !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags))
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200495 options |= MGMT_OPTION_EXTERNAL_CONFIG;
496
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200497 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
498 !bacmp(&hdev->public_addr, BDADDR_ANY))
499 options |= MGMT_OPTION_PUBLIC_ADDRESS;
500
501 return cpu_to_le32(options);
502}
503
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200504static int new_options(struct hci_dev *hdev, struct sock *skip)
505{
506 __le32 options = get_missing_options(hdev);
507
508 return mgmt_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options,
509 sizeof(options), skip);
510}
511
Marcel Holtmanndbece372014-07-04 18:11:55 +0200512static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
513{
514 __le32 options = get_missing_options(hdev);
515
516 return cmd_complete(sk, hdev->id, opcode, 0, &options,
517 sizeof(options));
518}
519
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200520static int read_config_info(struct sock *sk, struct hci_dev *hdev,
521 void *data, u16 data_len)
522{
523 struct mgmt_rp_read_config_info rp;
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200524 u32 options = 0;
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200525
526 BT_DBG("sock %p %s", sk, hdev->name);
527
528 hci_dev_lock(hdev);
529
530 memset(&rp, 0, sizeof(rp));
531 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200532
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200533 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
534 options |= MGMT_OPTION_EXTERNAL_CONFIG;
535
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200536 if (hdev->set_bdaddr)
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200537 options |= MGMT_OPTION_PUBLIC_ADDRESS;
538
539 rp.supported_options = cpu_to_le32(options);
540 rp.missing_options = get_missing_options(hdev);
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200541
542 hci_dev_unlock(hdev);
543
544 return cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, &rp,
545 sizeof(rp));
546}
547
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200548static u32 get_supported_settings(struct hci_dev *hdev)
Johan Hedberg03811012010-12-08 00:21:06 +0200549{
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200550 u32 settings = 0;
Johan Hedberg03811012010-12-08 00:21:06 +0200551
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200552 settings |= MGMT_SETTING_POWERED;
Johan Hedbergb2939472014-07-30 09:22:23 +0300553 settings |= MGMT_SETTING_BONDABLE;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800554 settings |= MGMT_SETTING_DEBUG_KEYS;
Johan Hedberg3742abf2014-07-08 16:07:34 +0300555 settings |= MGMT_SETTING_CONNECTABLE;
556 settings |= MGMT_SETTING_DISCOVERABLE;
Johan Hedberg03811012010-12-08 00:21:06 +0200557
Andre Guedesed3fa312012-07-24 15:03:46 -0300558 if (lmp_bredr_capable(hdev)) {
Johan Hedberg1a47aee2013-03-15 17:07:06 -0500559 if (hdev->hci_ver >= BLUETOOTH_VER_1_2)
560 settings |= MGMT_SETTING_FAST_CONNECTABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200561 settings |= MGMT_SETTING_BREDR;
562 settings |= MGMT_SETTING_LINK_SECURITY;
Marcel Holtmanna82974c2013-10-11 09:48:47 -0700563
564 if (lmp_ssp_capable(hdev)) {
565 settings |= MGMT_SETTING_SSP;
566 settings |= MGMT_SETTING_HS;
567 }
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800568
Marcel Holtmann5afeac12014-01-10 02:07:27 -0800569 if (lmp_sc_capable(hdev) ||
Marcel Holtmann111902f2014-06-21 04:53:17 +0200570 test_bit(HCI_FORCE_SC, &hdev->dbg_flags))
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800571 settings |= MGMT_SETTING_SECURE_CONN;
Marcel Holtmann848566b2013-10-01 22:59:22 -0700572 }
Marcel Holtmannd7b7e792012-02-20 21:47:49 +0100573
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300574 if (lmp_le_capable(hdev)) {
Marcel Holtmann9d428202012-05-03 07:12:31 +0200575 settings |= MGMT_SETTING_LE;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300576 settings |= MGMT_SETTING_ADVERTISING;
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200577 settings |= MGMT_SETTING_PRIVACY;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300578 }
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200579
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200580 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
581 hdev->set_bdaddr)
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200582 settings |= MGMT_SETTING_CONFIGURATION;
583
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200584 return settings;
585}
Johan Hedbergebc99fe2011-01-04 11:54:26 +0200586
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200587static u32 get_current_settings(struct hci_dev *hdev)
588{
589 u32 settings = 0;
Johan Hedbergdc4fe302011-03-16 14:29:36 +0200590
Johan Hedbergf1f0eb02012-02-21 17:15:41 +0200591 if (hdev_is_powered(hdev))
Marcel Holtmannf0d4b782012-02-21 12:14:25 +0100592 settings |= MGMT_SETTING_POWERED;
593
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200594 if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200595 settings |= MGMT_SETTING_CONNECTABLE;
596
Johan Hedberg1a4d3c42013-03-15 17:07:08 -0500597 if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags))
598 settings |= MGMT_SETTING_FAST_CONNECTABLE;
599
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200600 if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200601 settings |= MGMT_SETTING_DISCOVERABLE;
602
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300603 if (test_bit(HCI_BONDABLE, &hdev->dev_flags))
Johan Hedbergb2939472014-07-30 09:22:23 +0300604 settings |= MGMT_SETTING_BONDABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200605
Johan Hedberg56f87902013-10-02 13:43:13 +0300606 if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200607 settings |= MGMT_SETTING_BREDR;
608
Johan Hedberg06199cf2012-02-22 16:37:11 +0200609 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200610 settings |= MGMT_SETTING_LE;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200611
Johan Hedberg47990ea2012-02-22 11:58:37 +0200612 if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200613 settings |= MGMT_SETTING_LINK_SECURITY;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200614
Johan Hedberg84bde9d6c2012-01-25 14:21:06 +0200615 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200616 settings |= MGMT_SETTING_SSP;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200617
Johan Hedberg6d80dfd2012-02-20 23:50:38 +0200618 if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags))
619 settings |= MGMT_SETTING_HS;
620
Johan Hedbergf3d3444a2013-10-05 12:01:04 +0200621 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300622 settings |= MGMT_SETTING_ADVERTISING;
623
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800624 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags))
625 settings |= MGMT_SETTING_SECURE_CONN;
626
Johan Hedberg0663b292014-06-24 13:15:50 +0300627 if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags))
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800628 settings |= MGMT_SETTING_DEBUG_KEYS;
629
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200630 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
631 settings |= MGMT_SETTING_PRIVACY;
632
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200633 return settings;
Johan Hedbergc542a062011-01-26 13:11:03 +0200634}
635
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300636#define PNP_INFO_SVCLASS_ID 0x1200
637
Johan Hedberg213202e2013-01-27 00:31:33 +0200638static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
639{
640 u8 *ptr = data, *uuids_start = NULL;
641 struct bt_uuid *uuid;
642
643 if (len < 4)
644 return ptr;
645
646 list_for_each_entry(uuid, &hdev->uuids, list) {
647 u16 uuid16;
648
649 if (uuid->size != 16)
650 continue;
651
652 uuid16 = get_unaligned_le16(&uuid->uuid[12]);
653 if (uuid16 < 0x1100)
654 continue;
655
656 if (uuid16 == PNP_INFO_SVCLASS_ID)
657 continue;
658
659 if (!uuids_start) {
660 uuids_start = ptr;
661 uuids_start[0] = 1;
662 uuids_start[1] = EIR_UUID16_ALL;
663 ptr += 2;
664 }
665
666 /* Stop if not enough space to put next UUID */
667 if ((ptr - data) + sizeof(u16) > len) {
668 uuids_start[1] = EIR_UUID16_SOME;
669 break;
670 }
671
672 *ptr++ = (uuid16 & 0x00ff);
673 *ptr++ = (uuid16 & 0xff00) >> 8;
674 uuids_start[0] += sizeof(uuid16);
675 }
676
677 return ptr;
678}
679
Johan Hedbergcdf19632013-01-27 00:31:34 +0200680static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
681{
682 u8 *ptr = data, *uuids_start = NULL;
683 struct bt_uuid *uuid;
684
685 if (len < 6)
686 return ptr;
687
688 list_for_each_entry(uuid, &hdev->uuids, list) {
689 if (uuid->size != 32)
690 continue;
691
692 if (!uuids_start) {
693 uuids_start = ptr;
694 uuids_start[0] = 1;
695 uuids_start[1] = EIR_UUID32_ALL;
696 ptr += 2;
697 }
698
699 /* Stop if not enough space to put next UUID */
700 if ((ptr - data) + sizeof(u32) > len) {
701 uuids_start[1] = EIR_UUID32_SOME;
702 break;
703 }
704
705 memcpy(ptr, &uuid->uuid[12], sizeof(u32));
706 ptr += sizeof(u32);
707 uuids_start[0] += sizeof(u32);
708 }
709
710 return ptr;
711}
712
Johan Hedbergc00d5752013-01-27 00:31:35 +0200713static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
714{
715 u8 *ptr = data, *uuids_start = NULL;
716 struct bt_uuid *uuid;
717
718 if (len < 18)
719 return ptr;
720
721 list_for_each_entry(uuid, &hdev->uuids, list) {
722 if (uuid->size != 128)
723 continue;
724
725 if (!uuids_start) {
726 uuids_start = ptr;
727 uuids_start[0] = 1;
728 uuids_start[1] = EIR_UUID128_ALL;
729 ptr += 2;
730 }
731
732 /* Stop if not enough space to put next UUID */
733 if ((ptr - data) + 16 > len) {
734 uuids_start[1] = EIR_UUID128_SOME;
735 break;
736 }
737
738 memcpy(ptr, uuid->uuid, 16);
739 ptr += 16;
740 uuids_start[0] += 16;
741 }
742
743 return ptr;
744}
745
Johan Hedbergeb2a8d22013-10-19 23:38:20 +0300746static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev)
747{
748 struct pending_cmd *cmd;
749
750 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
751 if (cmd->opcode == opcode)
752 return cmd;
753 }
754
755 return NULL;
756}
757
Johan Hedberg95868422014-06-28 17:54:07 +0300758static struct pending_cmd *mgmt_pending_find_data(u16 opcode,
759 struct hci_dev *hdev,
760 const void *data)
761{
762 struct pending_cmd *cmd;
763
764 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
765 if (cmd->user_data != data)
766 continue;
767 if (cmd->opcode == opcode)
768 return cmd;
769 }
770
771 return NULL;
772}
773
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700774static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
775{
Marcel Holtmann7a5f4992013-10-16 00:16:49 -0700776 u8 ad_len = 0;
777 size_t name_len;
778
779 name_len = strlen(hdev->dev_name);
780 if (name_len > 0) {
781 size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2;
782
783 if (name_len > max_len) {
784 name_len = max_len;
785 ptr[1] = EIR_NAME_SHORT;
786 } else
787 ptr[1] = EIR_NAME_COMPLETE;
788
789 ptr[0] = name_len + 1;
790
791 memcpy(ptr + 2, hdev->dev_name, name_len);
792
793 ad_len += (name_len + 2);
794 ptr += (name_len + 2);
795 }
796
797 return ad_len;
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700798}
799
800static void update_scan_rsp_data(struct hci_request *req)
801{
802 struct hci_dev *hdev = req->hdev;
803 struct hci_cp_le_set_scan_rsp_data cp;
804 u8 len;
805
Johan Hedberg7751ef12013-10-19 23:38:15 +0300806 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700807 return;
808
809 memset(&cp, 0, sizeof(cp));
810
811 len = create_scan_rsp_data(hdev, cp.data);
812
Johan Hedbergeb438b52013-10-16 15:31:07 +0300813 if (hdev->scan_rsp_data_len == len &&
814 memcmp(cp.data, hdev->scan_rsp_data, len) == 0)
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700815 return;
816
Johan Hedbergeb438b52013-10-16 15:31:07 +0300817 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
818 hdev->scan_rsp_data_len = len;
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700819
820 cp.length = len;
821
822 hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp);
823}
824
Johan Hedberg9a43e252013-10-20 19:00:07 +0300825static u8 get_adv_discov_flags(struct hci_dev *hdev)
826{
827 struct pending_cmd *cmd;
828
829 /* If there's a pending mgmt command the flags will not yet have
830 * their final values, so check for this first.
831 */
832 cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
833 if (cmd) {
834 struct mgmt_mode *cp = cmd->param;
835 if (cp->val == 0x01)
836 return LE_AD_GENERAL;
837 else if (cp->val == 0x02)
838 return LE_AD_LIMITED;
839 } else {
840 if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags))
841 return LE_AD_LIMITED;
842 else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
843 return LE_AD_GENERAL;
844 }
845
846 return 0;
847}
848
Marcel Holtmann46cad2e2013-10-16 00:16:46 -0700849static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr)
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700850{
851 u8 ad_len = 0, flags = 0;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700852
Johan Hedberg9a43e252013-10-20 19:00:07 +0300853 flags |= get_adv_discov_flags(hdev);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700854
Johan Hedberge8340042014-01-30 11:16:50 -0800855 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700856 flags |= LE_AD_NO_BREDR;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700857
858 if (flags) {
859 BT_DBG("adv flags 0x%02x", flags);
860
861 ptr[0] = 2;
862 ptr[1] = EIR_FLAGS;
863 ptr[2] = flags;
864
865 ad_len += 3;
866 ptr += 3;
867 }
868
869 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) {
870 ptr[0] = 2;
871 ptr[1] = EIR_TX_POWER;
872 ptr[2] = (u8) hdev->adv_tx_power;
873
874 ad_len += 3;
875 ptr += 3;
876 }
877
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700878 return ad_len;
879}
880
Marcel Holtmann5947f4b2013-10-16 00:16:50 -0700881static void update_adv_data(struct hci_request *req)
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700882{
883 struct hci_dev *hdev = req->hdev;
884 struct hci_cp_le_set_adv_data cp;
885 u8 len;
886
Johan Hedberg10994ce2013-10-19 23:38:16 +0300887 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700888 return;
889
890 memset(&cp, 0, sizeof(cp));
891
Marcel Holtmann46cad2e2013-10-16 00:16:46 -0700892 len = create_adv_data(hdev, cp.data);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700893
894 if (hdev->adv_data_len == len &&
895 memcmp(cp.data, hdev->adv_data, len) == 0)
896 return;
897
898 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
899 hdev->adv_data_len = len;
900
901 cp.length = len;
902
903 hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
904}
905
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300906int mgmt_update_adv_data(struct hci_dev *hdev)
907{
908 struct hci_request req;
909
910 hci_req_init(&req, hdev);
911 update_adv_data(&req);
912
913 return hci_req_run(&req, NULL);
914}
915
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300916static void create_eir(struct hci_dev *hdev, u8 *data)
917{
918 u8 *ptr = data;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300919 size_t name_len;
920
921 name_len = strlen(hdev->dev_name);
922
923 if (name_len > 0) {
924 /* EIR Data type */
925 if (name_len > 48) {
926 name_len = 48;
927 ptr[1] = EIR_NAME_SHORT;
928 } else
929 ptr[1] = EIR_NAME_COMPLETE;
930
931 /* EIR Data length */
932 ptr[0] = name_len + 1;
933
934 memcpy(ptr + 2, hdev->dev_name, name_len);
935
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300936 ptr += (name_len + 2);
937 }
938
Johan Hedbergbbaf4442012-11-08 01:22:59 +0100939 if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) {
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700940 ptr[0] = 2;
941 ptr[1] = EIR_TX_POWER;
942 ptr[2] = (u8) hdev->inq_tx_power;
943
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700944 ptr += 3;
945 }
946
Marcel Holtmann2b9be132012-03-11 19:32:12 -0700947 if (hdev->devid_source > 0) {
948 ptr[0] = 9;
949 ptr[1] = EIR_DEVICE_ID;
950
951 put_unaligned_le16(hdev->devid_source, ptr + 2);
952 put_unaligned_le16(hdev->devid_vendor, ptr + 4);
953 put_unaligned_le16(hdev->devid_product, ptr + 6);
954 put_unaligned_le16(hdev->devid_version, ptr + 8);
955
Marcel Holtmann2b9be132012-03-11 19:32:12 -0700956 ptr += 10;
957 }
958
Johan Hedberg213202e2013-01-27 00:31:33 +0200959 ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedbergcdf19632013-01-27 00:31:34 +0200960 ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedbergc00d5752013-01-27 00:31:35 +0200961 ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300962}
963
Johan Hedberg890ea892013-03-15 17:06:52 -0500964static void update_eir(struct hci_request *req)
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300965{
Johan Hedberg890ea892013-03-15 17:06:52 -0500966 struct hci_dev *hdev = req->hdev;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300967 struct hci_cp_write_eir cp;
968
Johan Hedberg504c8dc2012-02-23 13:30:41 +0200969 if (!hdev_is_powered(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -0500970 return;
Johan Hedberg7770c4a2012-02-22 22:06:38 +0200971
Johan Hedberg976eb202012-10-24 21:12:01 +0300972 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -0500973 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300974
Johan Hedberg84bde9d6c2012-01-25 14:21:06 +0200975 if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
Johan Hedberg890ea892013-03-15 17:06:52 -0500976 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300977
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200978 if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
Johan Hedberg890ea892013-03-15 17:06:52 -0500979 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300980
981 memset(&cp, 0, sizeof(cp));
982
983 create_eir(hdev, cp.data);
984
985 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
Johan Hedberg890ea892013-03-15 17:06:52 -0500986 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300987
988 memcpy(hdev->eir, cp.data, sizeof(cp.data));
989
Johan Hedberg890ea892013-03-15 17:06:52 -0500990 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300991}
992
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200993static u8 get_service_classes(struct hci_dev *hdev)
994{
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -0300995 struct bt_uuid *uuid;
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200996 u8 val = 0;
997
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -0300998 list_for_each_entry(uuid, &hdev->uuids, list)
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200999 val |= uuid->svc_hint;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001000
1001 return val;
1002}
1003
Johan Hedberg890ea892013-03-15 17:06:52 -05001004static void update_class(struct hci_request *req)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001005{
Johan Hedberg890ea892013-03-15 17:06:52 -05001006 struct hci_dev *hdev = req->hdev;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001007 u8 cod[3];
1008
1009 BT_DBG("%s", hdev->name);
1010
Johan Hedberg504c8dc2012-02-23 13:30:41 +02001011 if (!hdev_is_powered(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05001012 return;
Johan Hedberg7770c4a2012-02-22 22:06:38 +02001013
Johan Hedbergf87ea1d2013-10-19 23:38:17 +03001014 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
1015 return;
1016
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001017 if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
Johan Hedberg890ea892013-03-15 17:06:52 -05001018 return;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001019
1020 cod[0] = hdev->minor_class;
1021 cod[1] = hdev->major_class;
1022 cod[2] = get_service_classes(hdev);
1023
Marcel Holtmann6acd7db2013-10-15 06:33:53 -07001024 if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags))
1025 cod[1] |= 0x20;
1026
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001027 if (memcmp(cod, hdev->dev_class, 3) == 0)
Johan Hedberg890ea892013-03-15 17:06:52 -05001028 return;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001029
Johan Hedberg890ea892013-03-15 17:06:52 -05001030 hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001031}
1032
Johan Hedberga4858cb2014-02-25 19:56:31 +02001033static bool get_connectable(struct hci_dev *hdev)
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001034{
1035 struct pending_cmd *cmd;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001036
1037 /* If there's a pending mgmt command the flag will not yet have
1038 * it's final value, so check for this first.
1039 */
1040 cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
1041 if (cmd) {
1042 struct mgmt_mode *cp = cmd->param;
Johan Hedberga4858cb2014-02-25 19:56:31 +02001043 return cp->val;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001044 }
1045
Johan Hedberga4858cb2014-02-25 19:56:31 +02001046 return test_bit(HCI_CONNECTABLE, &hdev->dev_flags);
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001047}
1048
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001049static void disable_advertising(struct hci_request *req)
1050{
1051 u8 enable = 0x00;
1052
1053 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1054}
1055
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001056static void enable_advertising(struct hci_request *req)
1057{
1058 struct hci_dev *hdev = req->hdev;
1059 struct hci_cp_le_set_adv_param cp;
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001060 u8 own_addr_type, enable = 0x01;
Johan Hedberga4858cb2014-02-25 19:56:31 +02001061 bool connectable;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001062
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001063 if (hci_conn_num(hdev, LE_LINK) > 0)
1064 return;
1065
1066 if (test_bit(HCI_LE_ADV, &hdev->dev_flags))
1067 disable_advertising(req);
1068
Johan Hedberg5ce194c2014-07-08 15:07:49 +03001069 /* Clear the HCI_LE_ADV bit temporarily so that the
Johan Hedberg8d972502014-02-28 12:54:14 +02001070 * hci_update_random_address knows that it's safe to go ahead
1071 * and write a new random address. The flag will be set back on
1072 * as soon as the SET_ADV_ENABLE HCI command completes.
1073 */
Johan Hedberg5ce194c2014-07-08 15:07:49 +03001074 clear_bit(HCI_LE_ADV, &hdev->dev_flags);
Johan Hedberg8d972502014-02-28 12:54:14 +02001075
Johan Hedberga4858cb2014-02-25 19:56:31 +02001076 connectable = get_connectable(hdev);
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001077
Johan Hedberga4858cb2014-02-25 19:56:31 +02001078 /* Set require_privacy to true only when non-connectable
1079 * advertising is used. In that case it is fine to use a
1080 * non-resolvable private address.
1081 */
1082 if (hci_update_random_address(req, !connectable, &own_addr_type) < 0)
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001083 return;
1084
Marcel Holtmann41c90c12014-02-23 20:25:55 -08001085 memset(&cp, 0, sizeof(cp));
Georg Lukas628531c2014-07-26 13:59:57 +02001086 cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval);
1087 cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval);
Johan Hedberga4858cb2014-02-25 19:56:31 +02001088 cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND;
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001089 cp.own_address_type = own_addr_type;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001090 cp.channel_map = hdev->le_adv_channel_map;
1091
1092 hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
1093
1094 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1095}
1096
Johan Hedberg7d785252011-12-15 00:47:39 +02001097static void service_cache_off(struct work_struct *work)
1098{
1099 struct hci_dev *hdev = container_of(work, struct hci_dev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001100 service_cache.work);
Johan Hedberg890ea892013-03-15 17:06:52 -05001101 struct hci_request req;
Johan Hedberg7d785252011-12-15 00:47:39 +02001102
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001103 if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
Johan Hedberg7d785252011-12-15 00:47:39 +02001104 return;
1105
Johan Hedberg890ea892013-03-15 17:06:52 -05001106 hci_req_init(&req, hdev);
1107
Johan Hedberg7d785252011-12-15 00:47:39 +02001108 hci_dev_lock(hdev);
1109
Johan Hedberg890ea892013-03-15 17:06:52 -05001110 update_eir(&req);
1111 update_class(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02001112
1113 hci_dev_unlock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -05001114
1115 hci_req_run(&req, NULL);
Johan Hedberg7d785252011-12-15 00:47:39 +02001116}
1117
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001118static void rpa_expired(struct work_struct *work)
1119{
1120 struct hci_dev *hdev = container_of(work, struct hci_dev,
1121 rpa_expired.work);
1122 struct hci_request req;
1123
1124 BT_DBG("");
1125
1126 set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
1127
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001128 if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags))
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001129 return;
1130
1131 /* The generation of a new RPA and programming it into the
1132 * controller happens in the enable_advertising() function.
1133 */
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001134 hci_req_init(&req, hdev);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001135 enable_advertising(&req);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001136 hci_req_run(&req, NULL);
1137}
1138
Johan Hedberg6a919082012-02-28 06:17:26 +02001139static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
Johan Hedberg7d785252011-12-15 00:47:39 +02001140{
Johan Hedberg4f87da82012-03-02 19:55:56 +02001141 if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg6a919082012-02-28 06:17:26 +02001142 return;
1143
Johan Hedberg4f87da82012-03-02 19:55:56 +02001144 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001145 INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
Johan Hedberg7d785252011-12-15 00:47:39 +02001146
Johan Hedberg4f87da82012-03-02 19:55:56 +02001147 /* Non-mgmt controlled devices get this bit set
1148 * implicitly so that pairing works for them, however
1149 * for mgmt we require user-space to explicitly enable
1150 * it
1151 */
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001152 clear_bit(HCI_BONDABLE, &hdev->dev_flags);
Johan Hedberg7d785252011-12-15 00:47:39 +02001153}
1154
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02001155static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001156 void *data, u16 data_len)
Johan Hedberg03811012010-12-08 00:21:06 +02001157{
1158 struct mgmt_rp_read_info rp;
Johan Hedberg03811012010-12-08 00:21:06 +02001159
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001160 BT_DBG("sock %p %s", sk, hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001161
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001162 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001163
Johan Hedberg03811012010-12-08 00:21:06 +02001164 memset(&rp, 0, sizeof(rp));
1165
Johan Hedberg03811012010-12-08 00:21:06 +02001166 bacpy(&rp.bdaddr, &hdev->bdaddr);
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001167
1168 rp.version = hdev->hci_ver;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02001169 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001170
1171 rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
1172 rp.current_settings = cpu_to_le32(get_current_settings(hdev));
1173
1174 memcpy(rp.dev_class, hdev->dev_class, 3);
Johan Hedberg03811012010-12-08 00:21:06 +02001175
1176 memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
Johan Hedberg27fcc362012-02-22 21:46:22 +02001177 memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
Johan Hedberg03811012010-12-08 00:21:06 +02001178
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001179 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001180
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001181 return cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001182 sizeof(rp));
Johan Hedberg03811012010-12-08 00:21:06 +02001183}
1184
1185static void mgmt_pending_free(struct pending_cmd *cmd)
1186{
1187 sock_put(cmd->sk);
1188 kfree(cmd->param);
1189 kfree(cmd);
1190}
1191
1192static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001193 struct hci_dev *hdev, void *data,
1194 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001195{
1196 struct pending_cmd *cmd;
1197
Johan Hedbergfca20012014-06-28 17:54:05 +03001198 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
Johan Hedberg03811012010-12-08 00:21:06 +02001199 if (!cmd)
1200 return NULL;
1201
1202 cmd->opcode = opcode;
1203 cmd->index = hdev->id;
1204
Andre Guedes12b94562012-06-07 19:05:45 -03001205 cmd->param = kmalloc(len, GFP_KERNEL);
Johan Hedberg03811012010-12-08 00:21:06 +02001206 if (!cmd->param) {
1207 kfree(cmd);
1208 return NULL;
1209 }
1210
1211 if (data)
1212 memcpy(cmd->param, data, len);
1213
1214 cmd->sk = sk;
1215 sock_hold(sk);
1216
1217 list_add(&cmd->list, &hdev->mgmt_pending);
1218
1219 return cmd;
1220}
1221
1222static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03001223 void (*cb)(struct pending_cmd *cmd,
1224 void *data),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001225 void *data)
Johan Hedberg03811012010-12-08 00:21:06 +02001226{
Andre Guedesa3d09352013-02-01 11:21:30 -03001227 struct pending_cmd *cmd, *tmp;
Johan Hedberg03811012010-12-08 00:21:06 +02001228
Andre Guedesa3d09352013-02-01 11:21:30 -03001229 list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) {
Johan Hedberg03811012010-12-08 00:21:06 +02001230 if (opcode > 0 && cmd->opcode != opcode)
1231 continue;
1232
1233 cb(cmd, data);
1234 }
1235}
1236
Johan Hedberg03811012010-12-08 00:21:06 +02001237static void mgmt_pending_remove(struct pending_cmd *cmd)
1238{
1239 list_del(&cmd->list);
1240 mgmt_pending_free(cmd);
1241}
1242
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001243static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
Johan Hedberg86805702011-11-11 16:18:52 +02001244{
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001245 __le32 settings = cpu_to_le32(get_current_settings(hdev));
Johan Hedberg86805702011-11-11 16:18:52 +02001246
Johan Hedbergaee9b212012-02-18 15:07:59 +02001247 return cmd_complete(sk, hdev->id, opcode, 0, &settings,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001248 sizeof(settings));
Johan Hedberg86805702011-11-11 16:18:52 +02001249}
1250
Johan Hedberg8b064a32014-02-24 14:52:22 +02001251static void clean_up_hci_complete(struct hci_dev *hdev, u8 status)
1252{
1253 BT_DBG("%s status 0x%02x", hdev->name, status);
1254
Johan Hedberga3172b72014-02-28 09:33:44 +02001255 if (hci_conn_count(hdev) == 0) {
1256 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001257 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberga3172b72014-02-28 09:33:44 +02001258 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001259}
1260
Johan Hedberg23a48092014-07-08 16:05:06 +03001261static bool hci_stop_discovery(struct hci_request *req)
Johan Hedberg21a60d32014-06-10 14:05:58 +03001262{
1263 struct hci_dev *hdev = req->hdev;
1264 struct hci_cp_remote_name_req_cancel cp;
1265 struct inquiry_entry *e;
1266
1267 switch (hdev->discovery.state) {
1268 case DISCOVERY_FINDING:
1269 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
1270 hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL);
1271 } else {
1272 cancel_delayed_work(&hdev->le_scan_disable);
1273 hci_req_add_le_scan_disable(req);
1274 }
1275
Johan Hedberg23a48092014-07-08 16:05:06 +03001276 return true;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001277
1278 case DISCOVERY_RESOLVING:
1279 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
1280 NAME_PENDING);
1281 if (!e)
Johan Hedberg23a48092014-07-08 16:05:06 +03001282 break;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001283
1284 bacpy(&cp.bdaddr, &e->data.bdaddr);
1285 hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp),
1286 &cp);
1287
Johan Hedberg23a48092014-07-08 16:05:06 +03001288 return true;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001289
1290 default:
1291 /* Passive scanning */
Johan Hedberg23a48092014-07-08 16:05:06 +03001292 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) {
Johan Hedberg21a60d32014-06-10 14:05:58 +03001293 hci_req_add_le_scan_disable(req);
Johan Hedberg23a48092014-07-08 16:05:06 +03001294 return true;
1295 }
1296
Johan Hedberg21a60d32014-06-10 14:05:58 +03001297 break;
1298 }
Johan Hedberg23a48092014-07-08 16:05:06 +03001299
1300 return false;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001301}
1302
Johan Hedberg8b064a32014-02-24 14:52:22 +02001303static int clean_up_hci_state(struct hci_dev *hdev)
1304{
1305 struct hci_request req;
1306 struct hci_conn *conn;
Johan Hedberg23a48092014-07-08 16:05:06 +03001307 bool discov_stopped;
1308 int err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001309
1310 hci_req_init(&req, hdev);
1311
1312 if (test_bit(HCI_ISCAN, &hdev->flags) ||
1313 test_bit(HCI_PSCAN, &hdev->flags)) {
1314 u8 scan = 0x00;
1315 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1316 }
1317
Johan Hedberg73e082f2014-07-08 15:07:51 +03001318 if (test_bit(HCI_LE_ADV, &hdev->dev_flags))
Johan Hedberg8b064a32014-02-24 14:52:22 +02001319 disable_advertising(&req);
1320
Johan Hedberg23a48092014-07-08 16:05:06 +03001321 discov_stopped = hci_stop_discovery(&req);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001322
1323 list_for_each_entry(conn, &hdev->conn_hash.list, list) {
1324 struct hci_cp_disconnect dc;
Johan Hedbergc9910d02014-02-27 14:35:12 +02001325 struct hci_cp_reject_conn_req rej;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001326
Johan Hedbergc9910d02014-02-27 14:35:12 +02001327 switch (conn->state) {
1328 case BT_CONNECTED:
1329 case BT_CONFIG:
1330 dc.handle = cpu_to_le16(conn->handle);
1331 dc.reason = 0x15; /* Terminated due to Power Off */
1332 hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc);
1333 break;
1334 case BT_CONNECT:
1335 if (conn->type == LE_LINK)
1336 hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL,
1337 0, NULL);
1338 else if (conn->type == ACL_LINK)
1339 hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL,
1340 6, &conn->dst);
1341 break;
1342 case BT_CONNECT2:
1343 bacpy(&rej.bdaddr, &conn->dst);
1344 rej.reason = 0x15; /* Terminated due to Power Off */
1345 if (conn->type == ACL_LINK)
1346 hci_req_add(&req, HCI_OP_REJECT_CONN_REQ,
1347 sizeof(rej), &rej);
1348 else if (conn->type == SCO_LINK)
1349 hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ,
1350 sizeof(rej), &rej);
1351 break;
1352 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001353 }
1354
Johan Hedberg23a48092014-07-08 16:05:06 +03001355 err = hci_req_run(&req, clean_up_hci_complete);
1356 if (!err && discov_stopped)
1357 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
1358
1359 return err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001360}
1361
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001362static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001363 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001364{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001365 struct mgmt_mode *cp = data;
Johan Hedberg03811012010-12-08 00:21:06 +02001366 struct pending_cmd *cmd;
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001367 int err;
Johan Hedberg03811012010-12-08 00:21:06 +02001368
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001369 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001370
Johan Hedberga7e80f22013-01-09 16:05:19 +02001371 if (cp->val != 0x00 && cp->val != 0x01)
1372 return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1373 MGMT_STATUS_INVALID_PARAMS);
1374
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001375 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001376
Johan Hedberg87b95ba2013-09-25 13:26:06 +03001377 if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) {
1378 err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1379 MGMT_STATUS_BUSY);
1380 goto failed;
1381 }
1382
Marcel Holtmannf0d4b782012-02-21 12:14:25 +01001383 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) {
1384 cancel_delayed_work(&hdev->power_off);
1385
1386 if (cp->val) {
Johan Hedberga1d70452013-01-09 15:29:40 +02001387 mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev,
1388 data, len);
1389 err = mgmt_powered(hdev, 1);
Marcel Holtmannf0d4b782012-02-21 12:14:25 +01001390 goto failed;
1391 }
1392 }
1393
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001394 if (!!cp->val == hdev_is_powered(hdev)) {
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001395 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001396 goto failed;
1397 }
1398
Johan Hedberg03811012010-12-08 00:21:06 +02001399 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
1400 if (!cmd) {
1401 err = -ENOMEM;
1402 goto failed;
1403 }
1404
Johan Hedberg8b064a32014-02-24 14:52:22 +02001405 if (cp->val) {
Johan Hedberg19202572013-01-14 22:33:51 +02001406 queue_work(hdev->req_workqueue, &hdev->power_on);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001407 err = 0;
1408 } else {
1409 /* Disconnect connections, stop scans, etc */
1410 err = clean_up_hci_state(hdev);
Johan Hedberga3172b72014-02-28 09:33:44 +02001411 if (!err)
1412 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
1413 HCI_POWER_OFF_TIMEOUT);
Johan Hedberg03811012010-12-08 00:21:06 +02001414
Johan Hedberg8b064a32014-02-24 14:52:22 +02001415 /* ENODATA means there were no HCI commands queued */
1416 if (err == -ENODATA) {
Johan Hedberga3172b72014-02-28 09:33:44 +02001417 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001418 queue_work(hdev->req_workqueue, &hdev->power_off.work);
1419 err = 0;
1420 }
1421 }
Johan Hedberg03811012010-12-08 00:21:06 +02001422
1423failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001424 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001425 return err;
1426}
1427
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001428static int new_settings(struct hci_dev *hdev, struct sock *skip)
1429{
1430 __le32 ev;
1431
1432 ev = cpu_to_le32(get_current_settings(hdev));
1433
1434 return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip);
1435}
1436
Johan Hedberg91a668b2014-07-09 13:28:26 +03001437int mgmt_new_settings(struct hci_dev *hdev)
1438{
1439 return new_settings(hdev, NULL);
1440}
1441
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001442struct cmd_lookup {
1443 struct sock *sk;
1444 struct hci_dev *hdev;
1445 u8 mgmt_status;
1446};
1447
1448static void settings_rsp(struct pending_cmd *cmd, void *data)
1449{
1450 struct cmd_lookup *match = data;
1451
1452 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
1453
1454 list_del(&cmd->list);
1455
1456 if (match->sk == NULL) {
1457 match->sk = cmd->sk;
1458 sock_hold(match->sk);
1459 }
1460
1461 mgmt_pending_free(cmd);
1462}
1463
1464static void cmd_status_rsp(struct pending_cmd *cmd, void *data)
1465{
1466 u8 *status = data;
1467
1468 cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
1469 mgmt_pending_remove(cmd);
1470}
1471
Johan Hedberge6fe7982013-10-02 15:45:22 +03001472static u8 mgmt_bredr_support(struct hci_dev *hdev)
1473{
1474 if (!lmp_bredr_capable(hdev))
1475 return MGMT_STATUS_NOT_SUPPORTED;
1476 else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
1477 return MGMT_STATUS_REJECTED;
1478 else
1479 return MGMT_STATUS_SUCCESS;
1480}
1481
1482static u8 mgmt_le_support(struct hci_dev *hdev)
1483{
1484 if (!lmp_le_capable(hdev))
1485 return MGMT_STATUS_NOT_SUPPORTED;
1486 else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
1487 return MGMT_STATUS_REJECTED;
1488 else
1489 return MGMT_STATUS_SUCCESS;
1490}
1491
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001492static void set_discoverable_complete(struct hci_dev *hdev, u8 status)
1493{
1494 struct pending_cmd *cmd;
1495 struct mgmt_mode *cp;
Marcel Holtmann970ba522013-10-15 06:33:57 -07001496 struct hci_request req;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001497 bool changed;
1498
1499 BT_DBG("status 0x%02x", status);
1500
1501 hci_dev_lock(hdev);
1502
1503 cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
1504 if (!cmd)
1505 goto unlock;
1506
1507 if (status) {
1508 u8 mgmt_err = mgmt_status(status);
1509 cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001510 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001511 goto remove_cmd;
1512 }
1513
1514 cp = cmd->param;
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001515 if (cp->val) {
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001516 changed = !test_and_set_bit(HCI_DISCOVERABLE,
1517 &hdev->dev_flags);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001518
1519 if (hdev->discov_timeout > 0) {
1520 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1521 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
1522 to);
1523 }
1524 } else {
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001525 changed = test_and_clear_bit(HCI_DISCOVERABLE,
1526 &hdev->dev_flags);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001527 }
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001528
1529 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1530
1531 if (changed)
1532 new_settings(hdev, cmd->sk);
1533
Marcel Holtmann970ba522013-10-15 06:33:57 -07001534 /* When the discoverable mode gets changed, make sure
1535 * that class of device has the limited discoverable
Johan Hedberg432df052014-08-01 11:13:31 +03001536 * bit correctly set. Also update page scan based on whitelist
1537 * entries.
Marcel Holtmann970ba522013-10-15 06:33:57 -07001538 */
1539 hci_req_init(&req, hdev);
Johan Hedberg432df052014-08-01 11:13:31 +03001540 hci_update_page_scan(hdev, &req);
Marcel Holtmann970ba522013-10-15 06:33:57 -07001541 update_class(&req);
1542 hci_req_run(&req, NULL);
1543
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001544remove_cmd:
1545 mgmt_pending_remove(cmd);
1546
1547unlock:
1548 hci_dev_unlock(hdev);
1549}
1550
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001551static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001552 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001553{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001554 struct mgmt_cp_set_discoverable *cp = data;
Johan Hedberg03811012010-12-08 00:21:06 +02001555 struct pending_cmd *cmd;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001556 struct hci_request req;
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001557 u16 timeout;
Johan Hedberg9a43e252013-10-20 19:00:07 +03001558 u8 scan;
Johan Hedberg03811012010-12-08 00:21:06 +02001559 int err;
1560
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001561 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001562
Johan Hedberg9a43e252013-10-20 19:00:07 +03001563 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) &&
1564 !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
Johan Hedberg33c525c2012-10-24 21:11:58 +03001565 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Johan Hedberg9a43e252013-10-20 19:00:07 +03001566 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001567
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001568 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga7e80f22013-01-09 16:05:19 +02001569 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1570 MGMT_STATUS_INVALID_PARAMS);
1571
Marcel Holtmann1f350c82012-03-12 20:31:08 -07001572 timeout = __le16_to_cpu(cp->timeout);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001573
1574 /* Disabling discoverable requires that no timeout is set,
1575 * and enabling limited discoverable requires a timeout.
1576 */
1577 if ((cp->val == 0x00 && timeout > 0) ||
1578 (cp->val == 0x02 && timeout == 0))
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001579 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001580 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001581
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001582 hci_dev_lock(hdev);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001583
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001584 if (!hdev_is_powered(hdev) && timeout > 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001585 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001586 MGMT_STATUS_NOT_POWERED);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001587 goto failed;
1588 }
1589
1590 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03001591 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001592 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001593 MGMT_STATUS_BUSY);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001594 goto failed;
1595 }
1596
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001597 if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001598 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001599 MGMT_STATUS_REJECTED);
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001600 goto failed;
1601 }
1602
1603 if (!hdev_is_powered(hdev)) {
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001604 bool changed = false;
1605
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001606 /* Setting limited discoverable when powered off is
1607 * not a valid operation since it requires a timeout
1608 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1609 */
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001610 if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) {
1611 change_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
1612 changed = true;
1613 }
1614
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001615 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001616 if (err < 0)
1617 goto failed;
1618
1619 if (changed)
1620 err = new_settings(hdev, sk);
1621
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001622 goto failed;
1623 }
1624
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001625 /* If the current mode is the same, then just update the timeout
1626 * value with the new value. And if only the timeout gets updated,
1627 * then no need for any HCI transactions.
1628 */
1629 if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) &&
1630 (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE,
1631 &hdev->dev_flags)) {
Marcel Holtmann36261542013-10-15 08:28:51 -07001632 cancel_delayed_work(&hdev->discov_off);
1633 hdev->discov_timeout = timeout;
Marcel Holtmann955638e2012-02-22 18:21:00 +01001634
Marcel Holtmann36261542013-10-15 08:28:51 -07001635 if (cp->val && hdev->discov_timeout > 0) {
1636 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001637 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
Marcel Holtmann36261542013-10-15 08:28:51 -07001638 to);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001639 }
1640
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001641 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001642 goto failed;
1643 }
1644
1645 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
1646 if (!cmd) {
1647 err = -ENOMEM;
1648 goto failed;
1649 }
1650
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001651 /* Cancel any potential discoverable timeout that might be
1652 * still active and store new timeout value. The arming of
1653 * the timeout happens in the complete handler.
1654 */
1655 cancel_delayed_work(&hdev->discov_off);
1656 hdev->discov_timeout = timeout;
1657
Johan Hedbergb456f872013-10-19 23:38:22 +03001658 /* Limited discoverable mode */
1659 if (cp->val == 0x02)
1660 set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1661 else
1662 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1663
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001664 hci_req_init(&req, hdev);
1665
Johan Hedberg9a43e252013-10-20 19:00:07 +03001666 /* The procedure for LE-only controllers is much simpler - just
1667 * update the advertising data.
1668 */
1669 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
1670 goto update_ad;
1671
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001672 scan = SCAN_PAGE;
1673
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001674 if (cp->val) {
1675 struct hci_cp_write_current_iac_lap hci_cp;
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001676
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001677 if (cp->val == 0x02) {
1678 /* Limited discoverable mode */
Marcel Holtmann33337dc2013-10-23 08:28:01 -07001679 hci_cp.num_iac = min_t(u8, hdev->num_iac, 2);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001680 hci_cp.iac_lap[0] = 0x00; /* LIAC */
1681 hci_cp.iac_lap[1] = 0x8b;
1682 hci_cp.iac_lap[2] = 0x9e;
1683 hci_cp.iac_lap[3] = 0x33; /* GIAC */
1684 hci_cp.iac_lap[4] = 0x8b;
1685 hci_cp.iac_lap[5] = 0x9e;
1686 } else {
1687 /* General discoverable mode */
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001688 hci_cp.num_iac = 1;
1689 hci_cp.iac_lap[0] = 0x33; /* GIAC */
1690 hci_cp.iac_lap[1] = 0x8b;
1691 hci_cp.iac_lap[2] = 0x9e;
1692 }
1693
1694 hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP,
1695 (hci_cp.num_iac * 3) + 1, &hci_cp);
1696
1697 scan |= SCAN_INQUIRY;
1698 } else {
1699 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1700 }
1701
1702 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001703
Johan Hedberg9a43e252013-10-20 19:00:07 +03001704update_ad:
1705 update_adv_data(&req);
1706
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001707 err = hci_req_run(&req, set_discoverable_complete);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001708 if (err < 0)
1709 mgmt_pending_remove(cmd);
1710
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001711failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001712 hci_dev_unlock(hdev);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001713 return err;
1714}
1715
Johan Hedberg406d7802013-03-15 17:07:09 -05001716static void write_fast_connectable(struct hci_request *req, bool enable)
1717{
Johan Hedbergbd98b992013-03-15 17:07:13 -05001718 struct hci_dev *hdev = req->hdev;
Johan Hedberg406d7802013-03-15 17:07:09 -05001719 struct hci_cp_write_page_scan_activity acp;
1720 u8 type;
1721
Johan Hedberg547003b2013-10-21 16:51:53 +03001722 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
1723 return;
1724
Johan Hedberg4c01f8b2013-03-15 17:07:14 -05001725 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
1726 return;
1727
Johan Hedberg406d7802013-03-15 17:07:09 -05001728 if (enable) {
1729 type = PAGE_SCAN_TYPE_INTERLACED;
1730
1731 /* 160 msec page scan interval */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001732 acp.interval = cpu_to_le16(0x0100);
Johan Hedberg406d7802013-03-15 17:07:09 -05001733 } else {
1734 type = PAGE_SCAN_TYPE_STANDARD; /* default */
1735
1736 /* default 1.28 sec page scan */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001737 acp.interval = cpu_to_le16(0x0800);
Johan Hedberg406d7802013-03-15 17:07:09 -05001738 }
1739
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001740 acp.window = cpu_to_le16(0x0012);
Johan Hedberg406d7802013-03-15 17:07:09 -05001741
Johan Hedbergbd98b992013-03-15 17:07:13 -05001742 if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval ||
1743 __cpu_to_le16(hdev->page_scan_window) != acp.window)
1744 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
1745 sizeof(acp), &acp);
1746
1747 if (hdev->page_scan_type != type)
1748 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type);
Johan Hedberg406d7802013-03-15 17:07:09 -05001749}
1750
Johan Hedberg2b76f452013-03-15 17:07:04 -05001751static void set_connectable_complete(struct hci_dev *hdev, u8 status)
1752{
1753 struct pending_cmd *cmd;
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001754 struct mgmt_mode *cp;
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001755 bool conn_changed, discov_changed;
Johan Hedberg2b76f452013-03-15 17:07:04 -05001756
1757 BT_DBG("status 0x%02x", status);
1758
1759 hci_dev_lock(hdev);
1760
1761 cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
1762 if (!cmd)
1763 goto unlock;
1764
Johan Hedberg37438c12013-10-14 16:20:05 +03001765 if (status) {
1766 u8 mgmt_err = mgmt_status(status);
1767 cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
1768 goto remove_cmd;
1769 }
1770
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001771 cp = cmd->param;
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001772 if (cp->val) {
1773 conn_changed = !test_and_set_bit(HCI_CONNECTABLE,
1774 &hdev->dev_flags);
1775 discov_changed = false;
1776 } else {
1777 conn_changed = test_and_clear_bit(HCI_CONNECTABLE,
1778 &hdev->dev_flags);
1779 discov_changed = test_and_clear_bit(HCI_DISCOVERABLE,
1780 &hdev->dev_flags);
1781 }
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001782
Johan Hedberg2b76f452013-03-15 17:07:04 -05001783 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
1784
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001785 if (conn_changed || discov_changed) {
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001786 new_settings(hdev, cmd->sk);
Johan Hedberg432df052014-08-01 11:13:31 +03001787 hci_update_page_scan(hdev, NULL);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001788 if (discov_changed)
1789 mgmt_update_adv_data(hdev);
Johan Hedberg2b7be332014-07-07 14:40:22 +03001790 hci_update_background_scan(hdev);
1791 }
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001792
Johan Hedberg37438c12013-10-14 16:20:05 +03001793remove_cmd:
Johan Hedberg2b76f452013-03-15 17:07:04 -05001794 mgmt_pending_remove(cmd);
1795
1796unlock:
1797 hci_dev_unlock(hdev);
1798}
1799
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001800static int set_connectable_update_settings(struct hci_dev *hdev,
1801 struct sock *sk, u8 val)
1802{
1803 bool changed = false;
1804 int err;
1805
1806 if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
1807 changed = true;
1808
1809 if (val) {
1810 set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
1811 } else {
1812 clear_bit(HCI_CONNECTABLE, &hdev->dev_flags);
1813 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
1814 }
1815
1816 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
1817 if (err < 0)
1818 return err;
1819
Johan Hedberg562064e2014-07-08 16:35:34 +03001820 if (changed) {
Johan Hedberg432df052014-08-01 11:13:31 +03001821 hci_update_page_scan(hdev, NULL);
Johan Hedberg562064e2014-07-08 16:35:34 +03001822 hci_update_background_scan(hdev);
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001823 return new_settings(hdev, sk);
Johan Hedberg562064e2014-07-08 16:35:34 +03001824 }
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001825
1826 return 0;
1827}
1828
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001829static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001830 u16 len)
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001831{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001832 struct mgmt_mode *cp = data;
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001833 struct pending_cmd *cmd;
Johan Hedberg2b76f452013-03-15 17:07:04 -05001834 struct hci_request req;
Johan Hedberg1987fdc2013-10-14 21:15:24 +03001835 u8 scan;
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001836 int err;
Johan Hedberg03811012010-12-08 00:21:06 +02001837
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001838 BT_DBG("request for %s", hdev->name);
Johan Hedberge41d8b42010-12-13 21:07:03 +02001839
Johan Hedberg1987fdc2013-10-14 21:15:24 +03001840 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) &&
1841 !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
Johan Hedberg33c525c2012-10-24 21:11:58 +03001842 return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
Johan Hedberg1987fdc2013-10-14 21:15:24 +03001843 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001844
Johan Hedberga7e80f22013-01-09 16:05:19 +02001845 if (cp->val != 0x00 && cp->val != 0x01)
1846 return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1847 MGMT_STATUS_INVALID_PARAMS);
1848
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001849 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001850
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001851 if (!hdev_is_powered(hdev)) {
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001852 err = set_connectable_update_settings(hdev, sk, cp->val);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001853 goto failed;
1854 }
1855
1856 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03001857 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001858 err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001859 MGMT_STATUS_BUSY);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001860 goto failed;
1861 }
1862
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001863 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
1864 if (!cmd) {
1865 err = -ENOMEM;
1866 goto failed;
1867 }
1868
Johan Hedberg2b76f452013-03-15 17:07:04 -05001869 hci_req_init(&req, hdev);
1870
Johan Hedberg9a43e252013-10-20 19:00:07 +03001871 /* If BR/EDR is not enabled and we disable advertising as a
1872 * by-product of disabling connectable, we need to update the
1873 * advertising flags.
1874 */
1875 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
1876 if (!cp->val) {
1877 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1878 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
1879 }
1880 update_adv_data(&req);
1881 } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) {
Johan Hedberg9b742462013-10-14 16:20:03 +03001882 if (cp->val) {
1883 scan = SCAN_PAGE;
1884 } else {
Johan Hedberg3bd27242014-07-28 20:53:58 +03001885 /* If we don't have any whitelist entries just
1886 * disable all scanning. If there are entries
1887 * and we had both page and inquiry scanning
1888 * enabled then fall back to only page scanning.
1889 * Otherwise no changes are needed.
1890 */
1891 if (list_empty(&hdev->whitelist))
1892 scan = SCAN_DISABLED;
1893 else if (test_bit(HCI_ISCAN, &hdev->flags))
1894 scan = SCAN_PAGE;
1895 else
1896 goto no_scan_update;
Johan Hedberg9b742462013-10-14 16:20:03 +03001897
1898 if (test_bit(HCI_ISCAN, &hdev->flags) &&
Marcel Holtmann8d6083f2013-10-14 16:38:45 -07001899 hdev->discov_timeout > 0)
Johan Hedberg9b742462013-10-14 16:20:03 +03001900 cancel_delayed_work(&hdev->discov_off);
1901 }
1902
1903 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1904 }
Johan Hedberg2b76f452013-03-15 17:07:04 -05001905
Johan Hedberg3bd27242014-07-28 20:53:58 +03001906no_scan_update:
Johan Hedberg4c01f8b2013-03-15 17:07:14 -05001907 /* If we're going from non-connectable to connectable or
1908 * vice-versa when fast connectable is enabled ensure that fast
1909 * connectable gets disabled. write_fast_connectable won't do
1910 * anything if the page scan parameters are already what they
1911 * should be.
1912 */
1913 if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags))
Johan Hedberge36a3762013-03-15 17:07:10 -05001914 write_fast_connectable(&req, false);
1915
Johan Hedberge8b12022014-07-10 10:51:27 +03001916 /* Update the advertising parameters if necessary */
1917 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
Johan Hedberg1987fdc2013-10-14 21:15:24 +03001918 enable_advertising(&req);
Johan Hedberg1987fdc2013-10-14 21:15:24 +03001919
Johan Hedberg2b76f452013-03-15 17:07:04 -05001920 err = hci_req_run(&req, set_connectable_complete);
Johan Hedberg9b742462013-10-14 16:20:03 +03001921 if (err < 0) {
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001922 mgmt_pending_remove(cmd);
Johan Hedberg9b742462013-10-14 16:20:03 +03001923 if (err == -ENODATA)
Johan Hedberga81070b2013-10-19 23:38:19 +03001924 err = set_connectable_update_settings(hdev, sk,
1925 cp->val);
Johan Hedberg9b742462013-10-14 16:20:03 +03001926 goto failed;
1927 }
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001928
1929failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001930 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001931 return err;
1932}
1933
Johan Hedbergb2939472014-07-30 09:22:23 +03001934static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001935 u16 len)
Johan Hedberg73f22f62010-12-29 16:00:25 +02001936{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001937 struct mgmt_mode *cp = data;
Marcel Holtmann55594352013-10-06 16:11:57 -07001938 bool changed;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001939 int err;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001940
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001941 BT_DBG("request for %s", hdev->name);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001942
Johan Hedberga7e80f22013-01-09 16:05:19 +02001943 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedbergb2939472014-07-30 09:22:23 +03001944 return cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE,
Johan Hedberga7e80f22013-01-09 16:05:19 +02001945 MGMT_STATUS_INVALID_PARAMS);
1946
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001947 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001948
1949 if (cp->val)
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001950 changed = !test_and_set_bit(HCI_BONDABLE, &hdev->dev_flags);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001951 else
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001952 changed = test_and_clear_bit(HCI_BONDABLE, &hdev->dev_flags);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001953
Johan Hedbergb2939472014-07-30 09:22:23 +03001954 err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001955 if (err < 0)
Marcel Holtmann55594352013-10-06 16:11:57 -07001956 goto unlock;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001957
Marcel Holtmann55594352013-10-06 16:11:57 -07001958 if (changed)
1959 err = new_settings(hdev, sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001960
Marcel Holtmann55594352013-10-06 16:11:57 -07001961unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001962 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001963 return err;
1964}
Johan Hedberg72a734e2010-12-30 00:38:22 +02001965
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001966static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
1967 u16 len)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001968{
1969 struct mgmt_mode *cp = data;
1970 struct pending_cmd *cmd;
Johan Hedberge6fe7982013-10-02 15:45:22 +03001971 u8 val, status;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001972 int err;
1973
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001974 BT_DBG("request for %s", hdev->name);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001975
Johan Hedberge6fe7982013-10-02 15:45:22 +03001976 status = mgmt_bredr_support(hdev);
1977 if (status)
Johan Hedberg33c525c2012-10-24 21:11:58 +03001978 return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
Johan Hedberge6fe7982013-10-02 15:45:22 +03001979 status);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001980
Johan Hedberga7e80f22013-01-09 16:05:19 +02001981 if (cp->val != 0x00 && cp->val != 0x01)
1982 return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1983 MGMT_STATUS_INVALID_PARAMS);
1984
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001985 hci_dev_lock(hdev);
1986
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001987 if (!hdev_is_powered(hdev)) {
Johan Hedberg47990ea2012-02-22 11:58:37 +02001988 bool changed = false;
1989
1990 if (!!cp->val != test_bit(HCI_LINK_SECURITY,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03001991 &hdev->dev_flags)) {
Johan Hedberg47990ea2012-02-22 11:58:37 +02001992 change_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
1993 changed = true;
1994 }
1995
1996 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1997 if (err < 0)
1998 goto failed;
1999
2000 if (changed)
2001 err = new_settings(hdev, sk);
2002
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002003 goto failed;
2004 }
2005
2006 if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002007 err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002008 MGMT_STATUS_BUSY);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002009 goto failed;
2010 }
2011
2012 val = !!cp->val;
2013
2014 if (test_bit(HCI_AUTH, &hdev->flags) == val) {
2015 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
2016 goto failed;
2017 }
2018
2019 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
2020 if (!cmd) {
2021 err = -ENOMEM;
2022 goto failed;
2023 }
2024
2025 err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
2026 if (err < 0) {
2027 mgmt_pending_remove(cmd);
2028 goto failed;
2029 }
2030
2031failed:
2032 hci_dev_unlock(hdev);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002033 return err;
2034}
2035
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002036static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002037{
2038 struct mgmt_mode *cp = data;
2039 struct pending_cmd *cmd;
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07002040 u8 status;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002041 int err;
2042
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002043 BT_DBG("request for %s", hdev->name);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002044
Marcel Holtmanncdba5282013-10-02 21:31:52 -07002045 status = mgmt_bredr_support(hdev);
2046 if (status)
2047 return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
2048
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002049 if (!lmp_ssp_capable(hdev))
2050 return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2051 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002052
Johan Hedberga7e80f22013-01-09 16:05:19 +02002053 if (cp->val != 0x00 && cp->val != 0x01)
2054 return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2055 MGMT_STATUS_INVALID_PARAMS);
2056
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002057 hci_dev_lock(hdev);
Johan Hedberg6c8f12c2012-02-22 16:35:26 +02002058
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002059 if (!hdev_is_powered(hdev)) {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002060 bool changed;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02002061
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002062 if (cp->val) {
2063 changed = !test_and_set_bit(HCI_SSP_ENABLED,
2064 &hdev->dev_flags);
2065 } else {
2066 changed = test_and_clear_bit(HCI_SSP_ENABLED,
2067 &hdev->dev_flags);
2068 if (!changed)
2069 changed = test_and_clear_bit(HCI_HS_ENABLED,
2070 &hdev->dev_flags);
2071 else
2072 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02002073 }
2074
2075 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2076 if (err < 0)
2077 goto failed;
2078
2079 if (changed)
2080 err = new_settings(hdev, sk);
2081
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002082 goto failed;
2083 }
2084
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002085 if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) ||
2086 mgmt_pending_find(MGMT_OP_SET_HS, hdev)) {
Szymon Jancd97dcb62012-03-16 16:02:56 +01002087 err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2088 MGMT_STATUS_BUSY);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002089 goto failed;
2090 }
2091
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07002092 if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002093 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2094 goto failed;
2095 }
2096
2097 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
2098 if (!cmd) {
2099 err = -ENOMEM;
2100 goto failed;
2101 }
2102
Johan Hedberg37699722014-06-24 14:00:27 +03002103 if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags))
2104 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2105 sizeof(cp->val), &cp->val);
2106
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07002107 err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002108 if (err < 0) {
2109 mgmt_pending_remove(cmd);
2110 goto failed;
2111 }
2112
2113failed:
2114 hci_dev_unlock(hdev);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002115 return err;
2116}
2117
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002118static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002119{
2120 struct mgmt_mode *cp = data;
Marcel Holtmannee392692013-10-01 22:59:23 -07002121 bool changed;
Johan Hedberge6fe7982013-10-02 15:45:22 +03002122 u8 status;
Marcel Holtmannee392692013-10-01 22:59:23 -07002123 int err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002124
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002125 BT_DBG("request for %s", hdev->name);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002126
Johan Hedberge6fe7982013-10-02 15:45:22 +03002127 status = mgmt_bredr_support(hdev);
2128 if (status)
2129 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002130
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002131 if (!lmp_ssp_capable(hdev))
2132 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2133 MGMT_STATUS_NOT_SUPPORTED);
2134
2135 if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
2136 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2137 MGMT_STATUS_REJECTED);
2138
Johan Hedberga7e80f22013-01-09 16:05:19 +02002139 if (cp->val != 0x00 && cp->val != 0x01)
2140 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2141 MGMT_STATUS_INVALID_PARAMS);
2142
Marcel Holtmannee392692013-10-01 22:59:23 -07002143 hci_dev_lock(hdev);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002144
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002145 if (cp->val) {
Marcel Holtmannee392692013-10-01 22:59:23 -07002146 changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002147 } else {
2148 if (hdev_is_powered(hdev)) {
2149 err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2150 MGMT_STATUS_REJECTED);
2151 goto unlock;
2152 }
2153
Marcel Holtmannee392692013-10-01 22:59:23 -07002154 changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002155 }
Marcel Holtmannee392692013-10-01 22:59:23 -07002156
2157 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
2158 if (err < 0)
2159 goto unlock;
2160
2161 if (changed)
2162 err = new_settings(hdev, sk);
2163
2164unlock:
2165 hci_dev_unlock(hdev);
2166 return err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002167}
2168
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002169static void le_enable_complete(struct hci_dev *hdev, u8 status)
2170{
2171 struct cmd_lookup match = { NULL, hdev };
2172
2173 if (status) {
2174 u8 mgmt_err = mgmt_status(status);
2175
2176 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
2177 &mgmt_err);
2178 return;
2179 }
2180
2181 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
2182
2183 new_settings(hdev, match.sk);
2184
2185 if (match.sk)
2186 sock_put(match.sk);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002187
2188 /* Make sure the controller has a good default for
2189 * advertising data. Restrict the update to when LE
2190 * has actually been enabled. During power on, the
2191 * update in powered_update_hci will take care of it.
2192 */
2193 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
2194 struct hci_request req;
2195
2196 hci_dev_lock(hdev);
2197
2198 hci_req_init(&req, hdev);
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07002199 update_adv_data(&req);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07002200 update_scan_rsp_data(&req);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002201 hci_req_run(&req, NULL);
2202
Johan Hedberga70f4b52014-07-07 15:19:50 +03002203 hci_update_background_scan(hdev);
2204
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002205 hci_dev_unlock(hdev);
2206 }
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002207}
2208
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002209static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg06199cf2012-02-22 16:37:11 +02002210{
2211 struct mgmt_mode *cp = data;
2212 struct hci_cp_write_le_host_supported hci_cp;
2213 struct pending_cmd *cmd;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002214 struct hci_request req;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002215 int err;
Johan Hedberg0b60eba2012-02-28 00:57:24 +02002216 u8 val, enabled;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002217
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002218 BT_DBG("request for %s", hdev->name);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002219
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002220 if (!lmp_le_capable(hdev))
2221 return cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2222 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg1de028c2012-02-29 19:55:35 -08002223
Johan Hedberga7e80f22013-01-09 16:05:19 +02002224 if (cp->val != 0x00 && cp->val != 0x01)
2225 return cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2226 MGMT_STATUS_INVALID_PARAMS);
2227
Johan Hedbergc73eee92013-04-19 18:35:21 +03002228 /* LE-only devices do not allow toggling LE on/off */
Johan Hedberg56f87902013-10-02 13:43:13 +03002229 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
Johan Hedbergc73eee92013-04-19 18:35:21 +03002230 return cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2231 MGMT_STATUS_REJECTED);
2232
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002233 hci_dev_lock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002234
2235 val = !!cp->val;
Gustavo Padovanffa88e02012-11-23 16:50:51 -02002236 enabled = lmp_host_le_capable(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002237
Johan Hedberg0b60eba2012-02-28 00:57:24 +02002238 if (!hdev_is_powered(hdev) || val == enabled) {
Johan Hedberg06199cf2012-02-22 16:37:11 +02002239 bool changed = false;
2240
2241 if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
2242 change_bit(HCI_LE_ENABLED, &hdev->dev_flags);
2243 changed = true;
2244 }
2245
Johan Hedbergf3d3444a2013-10-05 12:01:04 +02002246 if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
2247 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03002248 changed = true;
2249 }
2250
Johan Hedberg06199cf2012-02-22 16:37:11 +02002251 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2252 if (err < 0)
Johan Hedberg1de028c2012-02-29 19:55:35 -08002253 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002254
2255 if (changed)
2256 err = new_settings(hdev, sk);
2257
Johan Hedberg1de028c2012-02-29 19:55:35 -08002258 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002259 }
2260
Johan Hedberg4375f102013-09-25 13:26:10 +03002261 if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) ||
2262 mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002263 err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002264 MGMT_STATUS_BUSY);
Johan Hedberg1de028c2012-02-29 19:55:35 -08002265 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002266 }
2267
2268 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
2269 if (!cmd) {
2270 err = -ENOMEM;
Johan Hedberg1de028c2012-02-29 19:55:35 -08002271 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002272 }
2273
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002274 hci_req_init(&req, hdev);
2275
Johan Hedberg06199cf2012-02-22 16:37:11 +02002276 memset(&hci_cp, 0, sizeof(hci_cp));
2277
2278 if (val) {
2279 hci_cp.le = val;
Marcel Holtmann32226e42014-07-24 20:04:16 +02002280 hci_cp.simul = 0x00;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002281 } else {
Johan Hedberg73e082f2014-07-08 15:07:51 +03002282 if (test_bit(HCI_LE_ADV, &hdev->dev_flags))
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002283 disable_advertising(&req);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002284 }
2285
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002286 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp),
2287 &hci_cp);
2288
2289 err = hci_req_run(&req, le_enable_complete);
Syam Sidhardhan0c01bc42012-04-12 20:33:21 +05302290 if (err < 0)
Johan Hedberg06199cf2012-02-22 16:37:11 +02002291 mgmt_pending_remove(cmd);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002292
Johan Hedberg1de028c2012-02-29 19:55:35 -08002293unlock:
2294 hci_dev_unlock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002295 return err;
2296}
2297
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002298/* This is a helper function to test for pending mgmt commands that can
2299 * cause CoD or EIR HCI commands. We can only allow one such pending
2300 * mgmt command at a time since otherwise we cannot easily track what
2301 * the current values are, will be, and based on that calculate if a new
2302 * HCI command needs to be sent and if yes with what value.
2303 */
2304static bool pending_eir_or_class(struct hci_dev *hdev)
2305{
2306 struct pending_cmd *cmd;
2307
2308 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
2309 switch (cmd->opcode) {
2310 case MGMT_OP_ADD_UUID:
2311 case MGMT_OP_REMOVE_UUID:
2312 case MGMT_OP_SET_DEV_CLASS:
2313 case MGMT_OP_SET_POWERED:
2314 return true;
2315 }
2316 }
2317
2318 return false;
2319}
2320
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002321static const u8 bluetooth_base_uuid[] = {
2322 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
2323 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2324};
2325
2326static u8 get_uuid_size(const u8 *uuid)
2327{
2328 u32 val;
2329
2330 if (memcmp(uuid, bluetooth_base_uuid, 12))
2331 return 128;
2332
2333 val = get_unaligned_le32(&uuid[12]);
2334 if (val > 0xffff)
2335 return 32;
2336
2337 return 16;
2338}
2339
Johan Hedberg92da6092013-03-15 17:06:55 -05002340static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status)
2341{
2342 struct pending_cmd *cmd;
2343
2344 hci_dev_lock(hdev);
2345
2346 cmd = mgmt_pending_find(mgmt_op, hdev);
2347 if (!cmd)
2348 goto unlock;
2349
2350 cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status),
2351 hdev->dev_class, 3);
2352
2353 mgmt_pending_remove(cmd);
2354
2355unlock:
2356 hci_dev_unlock(hdev);
2357}
2358
2359static void add_uuid_complete(struct hci_dev *hdev, u8 status)
2360{
2361 BT_DBG("status 0x%02x", status);
2362
2363 mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status);
2364}
2365
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002366static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002367{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002368 struct mgmt_cp_add_uuid *cp = data;
Johan Hedberg90e70452012-02-23 23:09:40 +02002369 struct pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002370 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002371 struct bt_uuid *uuid;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002372 int err;
2373
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002374 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002375
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002376 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002377
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002378 if (pending_eir_or_class(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002379 err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002380 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002381 goto failed;
2382 }
2383
Andre Guedes92c4c202012-06-07 19:05:44 -03002384 uuid = kmalloc(sizeof(*uuid), GFP_KERNEL);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002385 if (!uuid) {
2386 err = -ENOMEM;
2387 goto failed;
2388 }
2389
2390 memcpy(uuid->uuid, cp->uuid, 16);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002391 uuid->svc_hint = cp->svc_hint;
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002392 uuid->size = get_uuid_size(cp->uuid);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002393
Johan Hedbergde66aa62013-01-27 00:31:27 +02002394 list_add_tail(&uuid->list, &hdev->uuids);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002395
Johan Hedberg890ea892013-03-15 17:06:52 -05002396 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002397
Johan Hedberg890ea892013-03-15 17:06:52 -05002398 update_class(&req);
2399 update_eir(&req);
2400
Johan Hedberg92da6092013-03-15 17:06:55 -05002401 err = hci_req_run(&req, add_uuid_complete);
2402 if (err < 0) {
2403 if (err != -ENODATA)
2404 goto failed;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002405
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002406 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002407 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002408 goto failed;
2409 }
2410
2411 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002412 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002413 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002414 goto failed;
2415 }
2416
2417 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002418
2419failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002420 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002421 return err;
2422}
2423
Johan Hedberg24b78d02012-02-23 23:24:30 +02002424static bool enable_service_cache(struct hci_dev *hdev)
2425{
2426 if (!hdev_is_powered(hdev))
2427 return false;
2428
2429 if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) {
Johan Hedberg46818ed2013-01-14 22:33:52 +02002430 queue_delayed_work(hdev->workqueue, &hdev->service_cache,
2431 CACHE_TIMEOUT);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002432 return true;
2433 }
2434
2435 return false;
2436}
2437
Johan Hedberg92da6092013-03-15 17:06:55 -05002438static void remove_uuid_complete(struct hci_dev *hdev, u8 status)
2439{
2440 BT_DBG("status 0x%02x", status);
2441
2442 mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status);
2443}
2444
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002445static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002446 u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002447{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002448 struct mgmt_cp_remove_uuid *cp = data;
Johan Hedberg90e70452012-02-23 23:09:40 +02002449 struct pending_cmd *cmd;
Johan Hedberg056341c2013-01-27 00:31:30 +02002450 struct bt_uuid *match, *tmp;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002451 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 -05002452 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002453 int err, found;
2454
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002455 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002456
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002457 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002458
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002459 if (pending_eir_or_class(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002460 err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002461 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002462 goto unlock;
2463 }
2464
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002465 if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
Johan Hedberg35f74982014-02-18 17:14:32 +02002466 hci_uuids_clear(hdev);
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002467
Johan Hedberg24b78d02012-02-23 23:24:30 +02002468 if (enable_service_cache(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002469 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002470 0, hdev->dev_class, 3);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002471 goto unlock;
2472 }
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002473
Johan Hedberg9246a862012-02-23 21:33:16 +02002474 goto update_class;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002475 }
2476
2477 found = 0;
2478
Johan Hedberg056341c2013-01-27 00:31:30 +02002479 list_for_each_entry_safe(match, tmp, &hdev->uuids, list) {
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002480 if (memcmp(match->uuid, cp->uuid, 16) != 0)
2481 continue;
2482
2483 list_del(&match->list);
Johan Hedberg482049f2012-11-08 10:25:26 +01002484 kfree(match);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002485 found++;
2486 }
2487
2488 if (found == 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002489 err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002490 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002491 goto unlock;
2492 }
2493
Johan Hedberg9246a862012-02-23 21:33:16 +02002494update_class:
Johan Hedberg890ea892013-03-15 17:06:52 -05002495 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002496
Johan Hedberg890ea892013-03-15 17:06:52 -05002497 update_class(&req);
2498 update_eir(&req);
2499
Johan Hedberg92da6092013-03-15 17:06:55 -05002500 err = hci_req_run(&req, remove_uuid_complete);
2501 if (err < 0) {
2502 if (err != -ENODATA)
2503 goto unlock;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002504
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002505 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002506 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002507 goto unlock;
2508 }
2509
2510 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002511 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002512 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002513 goto unlock;
2514 }
2515
2516 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002517
2518unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002519 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002520 return err;
2521}
2522
Johan Hedberg92da6092013-03-15 17:06:55 -05002523static void set_class_complete(struct hci_dev *hdev, u8 status)
2524{
2525 BT_DBG("status 0x%02x", status);
2526
2527 mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status);
2528}
2529
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002530static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002531 u16 len)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002532{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002533 struct mgmt_cp_set_dev_class *cp = data;
Johan Hedberg90e70452012-02-23 23:09:40 +02002534 struct pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002535 struct hci_request req;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002536 int err;
2537
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002538 BT_DBG("request for %s", hdev->name);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002539
Marcel Holtmann6203fc92013-10-02 23:37:29 -07002540 if (!lmp_bredr_capable(hdev))
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002541 return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2542 MGMT_STATUS_NOT_SUPPORTED);
2543
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002544 hci_dev_lock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002545
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002546 if (pending_eir_or_class(hdev)) {
2547 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2548 MGMT_STATUS_BUSY);
2549 goto unlock;
2550 }
2551
2552 if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
2553 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2554 MGMT_STATUS_INVALID_PARAMS);
2555 goto unlock;
2556 }
2557
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002558 hdev->major_class = cp->major;
2559 hdev->minor_class = cp->minor;
2560
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002561 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002562 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002563 hdev->dev_class, 3);
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002564 goto unlock;
2565 }
2566
Johan Hedberg890ea892013-03-15 17:06:52 -05002567 hci_req_init(&req, hdev);
2568
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02002569 if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) {
Johan Hedberg7d785252011-12-15 00:47:39 +02002570 hci_dev_unlock(hdev);
2571 cancel_delayed_work_sync(&hdev->service_cache);
2572 hci_dev_lock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -05002573 update_eir(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02002574 }
Johan Hedberg14c0b602011-12-15 00:47:37 +02002575
Johan Hedberg890ea892013-03-15 17:06:52 -05002576 update_class(&req);
2577
Johan Hedberg92da6092013-03-15 17:06:55 -05002578 err = hci_req_run(&req, set_class_complete);
2579 if (err < 0) {
2580 if (err != -ENODATA)
2581 goto unlock;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002582
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002583 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002584 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002585 goto unlock;
2586 }
2587
2588 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002589 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002590 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002591 goto unlock;
2592 }
2593
2594 err = 0;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002595
Johan Hedbergb5235a62012-02-21 14:32:24 +02002596unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002597 hci_dev_unlock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002598 return err;
2599}
2600
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002601static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002602 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002603{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002604 struct mgmt_cp_load_link_keys *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03002605 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
2606 sizeof(struct mgmt_link_key_info));
Szymon Janc4e51eae2011-02-25 19:05:48 +01002607 u16 key_count, expected_len;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002608 bool changed;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002609 int i;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002610
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002611 BT_DBG("request for %s", hdev->name);
2612
2613 if (!lmp_bredr_capable(hdev))
2614 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2615 MGMT_STATUS_NOT_SUPPORTED);
2616
Marcel Holtmann1f350c82012-03-12 20:31:08 -07002617 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002618 if (key_count > max_key_count) {
2619 BT_ERR("load_link_keys: too big key_count value %u",
2620 key_count);
2621 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2622 MGMT_STATUS_INVALID_PARAMS);
2623 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002624
Johan Hedberg86742e12011-11-07 23:13:38 +02002625 expected_len = sizeof(*cp) + key_count *
2626 sizeof(struct mgmt_link_key_info);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002627 if (expected_len != len) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002628 BT_ERR("load_link_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02002629 expected_len, len);
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002630 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002631 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002632 }
2633
Johan Hedberg4ae14302013-01-20 14:27:13 +02002634 if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01)
2635 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2636 MGMT_STATUS_INVALID_PARAMS);
2637
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002638 BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002639 key_count);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002640
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002641 for (i = 0; i < key_count; i++) {
2642 struct mgmt_link_key_info *key = &cp->keys[i];
2643
Marcel Holtmann8e991132014-01-10 02:07:25 -08002644 if (key->addr.type != BDADDR_BREDR || key->type > 0x08)
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002645 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2646 MGMT_STATUS_INVALID_PARAMS);
2647 }
2648
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002649 hci_dev_lock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002650
2651 hci_link_keys_clear(hdev);
2652
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002653 if (cp->debug_keys)
Johan Hedberg0663b292014-06-24 13:15:50 +03002654 changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS,
2655 &hdev->dev_flags);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002656 else
Johan Hedberg0663b292014-06-24 13:15:50 +03002657 changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS,
2658 &hdev->dev_flags);
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002659
2660 if (changed)
2661 new_settings(hdev, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002662
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002663 for (i = 0; i < key_count; i++) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002664 struct mgmt_link_key_info *key = &cp->keys[i];
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002665
Johan Hedberg58e92932014-06-24 14:00:26 +03002666 /* Always ignore debug keys and require a new pairing if
2667 * the user wants to use them.
2668 */
2669 if (key->type == HCI_LK_DEBUG_COMBINATION)
2670 continue;
2671
Johan Hedberg7652ff62014-06-24 13:15:49 +03002672 hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val,
2673 key->type, key->pin_len, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002674 }
2675
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002676 cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
Johan Hedberg0e5f8752011-11-11 16:18:54 +02002677
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002678 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002679
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002680 return 0;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002681}
2682
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002683static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002684 u8 addr_type, struct sock *skip_sk)
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002685{
2686 struct mgmt_ev_device_unpaired ev;
2687
2688 bacpy(&ev.addr.bdaddr, bdaddr);
2689 ev.addr.type = addr_type;
2690
2691 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002692 skip_sk);
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002693}
2694
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002695static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002696 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002697{
Johan Hedberg124f6e32012-02-09 13:50:12 +02002698 struct mgmt_cp_unpair_device *cp = data;
2699 struct mgmt_rp_unpair_device rp;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002700 struct hci_cp_disconnect dc;
2701 struct pending_cmd *cmd;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002702 struct hci_conn *conn;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002703 int err;
2704
Johan Hedberga8a1d192011-11-10 15:54:38 +02002705 memset(&rp, 0, sizeof(rp));
Johan Hedberg124f6e32012-02-09 13:50:12 +02002706 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2707 rp.addr.type = cp->addr.type;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002708
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002709 if (!bdaddr_type_is_valid(cp->addr.type))
2710 return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2711 MGMT_STATUS_INVALID_PARAMS,
2712 &rp, sizeof(rp));
2713
Johan Hedberg118da702013-01-20 14:27:20 +02002714 if (cp->disconnect != 0x00 && cp->disconnect != 0x01)
2715 return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2716 MGMT_STATUS_INVALID_PARAMS,
2717 &rp, sizeof(rp));
2718
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002719 hci_dev_lock(hdev);
2720
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002721 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002722 err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002723 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp));
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002724 goto unlock;
2725 }
2726
Johan Hedberge0b2b272014-02-18 17:14:31 +02002727 if (cp->addr.type == BDADDR_BREDR) {
Johan Hedberg124f6e32012-02-09 13:50:12 +02002728 err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
Johan Hedberge0b2b272014-02-18 17:14:31 +02002729 } else {
2730 u8 addr_type;
2731
2732 if (cp->addr.type == BDADDR_LE_PUBLIC)
2733 addr_type = ADDR_LE_DEV_PUBLIC;
2734 else
2735 addr_type = ADDR_LE_DEV_RANDOM;
2736
Johan Hedberga7ec7332014-02-18 17:14:35 +02002737 hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type);
2738
Andre Guedesa9b0a042014-02-26 20:21:52 -03002739 hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type);
2740
Johan Hedberge0b2b272014-02-18 17:14:31 +02002741 err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type);
2742 }
Vinicius Costa Gomesb0dbfb42012-02-02 21:08:03 -03002743
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002744 if (err < 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002745 err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002746 MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002747 goto unlock;
2748 }
2749
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002750 if (cp->disconnect) {
Andre Guedes591f47f2012-04-24 21:02:49 -03002751 if (cp->addr.type == BDADDR_BREDR)
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002752 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002753 &cp->addr.bdaddr);
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002754 else
2755 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002756 &cp->addr.bdaddr);
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002757 } else {
2758 conn = NULL;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002759 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002760
Johan Hedberga8a1d192011-11-10 15:54:38 +02002761 if (!conn) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002762 err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002763 &rp, sizeof(rp));
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002764 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002765 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002766 }
2767
Johan Hedberg124f6e32012-02-09 13:50:12 +02002768 cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002769 sizeof(*cp));
Johan Hedberga8a1d192011-11-10 15:54:38 +02002770 if (!cmd) {
2771 err = -ENOMEM;
2772 goto unlock;
2773 }
2774
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02002775 dc.handle = cpu_to_le16(conn->handle);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002776 dc.reason = 0x13; /* Remote User Terminated Connection */
2777 err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc);
2778 if (err < 0)
2779 mgmt_pending_remove(cmd);
2780
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002781unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002782 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002783 return err;
2784}
2785
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002786static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002787 u16 len)
Johan Hedberg8962ee72011-01-20 12:40:27 +02002788{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002789 struct mgmt_cp_disconnect *cp = data;
Johan Hedberg06a63b12013-01-20 14:27:21 +02002790 struct mgmt_rp_disconnect rp;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002791 struct hci_cp_disconnect dc;
Johan Hedberg366a0332011-02-19 12:05:55 -03002792 struct pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002793 struct hci_conn *conn;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002794 int err;
2795
2796 BT_DBG("");
2797
Johan Hedberg06a63b12013-01-20 14:27:21 +02002798 memset(&rp, 0, sizeof(rp));
2799 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2800 rp.addr.type = cp->addr.type;
2801
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002802 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg06a63b12013-01-20 14:27:21 +02002803 return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2804 MGMT_STATUS_INVALID_PARAMS,
2805 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002806
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002807 hci_dev_lock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002808
2809 if (!test_bit(HCI_UP, &hdev->flags)) {
Johan Hedberg06a63b12013-01-20 14:27:21 +02002810 err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2811 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002812 goto failed;
2813 }
2814
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002815 if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) {
Johan Hedberg06a63b12013-01-20 14:27:21 +02002816 err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2817 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002818 goto failed;
2819 }
2820
Andre Guedes591f47f2012-04-24 21:02:49 -03002821 if (cp->addr.type == BDADDR_BREDR)
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03002822 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2823 &cp->addr.bdaddr);
Johan Hedberg88c3df12012-02-09 14:27:38 +02002824 else
2825 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
Vinicius Costa Gomes365227e2011-05-06 18:41:44 -03002826
Vishal Agarwalf9607272012-06-13 05:32:43 +05302827 if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) {
Johan Hedberg06a63b12013-01-20 14:27:21 +02002828 err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2829 MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002830 goto failed;
2831 }
2832
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002833 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03002834 if (!cmd) {
2835 err = -ENOMEM;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002836 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03002837 }
Johan Hedberg8962ee72011-01-20 12:40:27 +02002838
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02002839 dc.handle = cpu_to_le16(conn->handle);
Andre Guedes3701f942012-06-11 18:41:12 -03002840 dc.reason = HCI_ERROR_REMOTE_USER_TERM;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002841
2842 err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc);
2843 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03002844 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002845
2846failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002847 hci_dev_unlock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002848 return err;
2849}
2850
Andre Guedes57c14772012-04-24 21:02:50 -03002851static u8 link_to_bdaddr(u8 link_type, u8 addr_type)
Johan Hedberg4c659c32011-11-07 23:13:39 +02002852{
2853 switch (link_type) {
2854 case LE_LINK:
Johan Hedberg48264f02011-11-09 13:58:58 +02002855 switch (addr_type) {
2856 case ADDR_LE_DEV_PUBLIC:
Andre Guedes591f47f2012-04-24 21:02:49 -03002857 return BDADDR_LE_PUBLIC;
Andre Guedes0ed09142012-04-03 08:46:54 -03002858
Johan Hedberg48264f02011-11-09 13:58:58 +02002859 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03002860 /* Fallback to LE Random address type */
Andre Guedes591f47f2012-04-24 21:02:49 -03002861 return BDADDR_LE_RANDOM;
Johan Hedberg48264f02011-11-09 13:58:58 +02002862 }
Andre Guedes0ed09142012-04-03 08:46:54 -03002863
Johan Hedberg4c659c32011-11-07 23:13:39 +02002864 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03002865 /* Fallback to BR/EDR type */
Andre Guedes591f47f2012-04-24 21:02:49 -03002866 return BDADDR_BREDR;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002867 }
2868}
2869
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002870static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
2871 u16 data_len)
Johan Hedberg2784eb42011-01-21 13:56:35 +02002872{
Johan Hedberg2784eb42011-01-21 13:56:35 +02002873 struct mgmt_rp_get_connections *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002874 struct hci_conn *c;
Johan Hedberga38528f2011-01-22 06:46:43 +02002875 size_t rp_len;
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002876 int err;
2877 u16 i;
Johan Hedberg2784eb42011-01-21 13:56:35 +02002878
2879 BT_DBG("");
2880
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002881 hci_dev_lock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002882
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002883 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002884 err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002885 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002886 goto unlock;
2887 }
2888
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002889 i = 0;
Johan Hedbergb644ba32012-01-17 21:48:47 +02002890 list_for_each_entry(c, &hdev->conn_hash.list, list) {
2891 if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002892 i++;
Johan Hedberg2784eb42011-01-21 13:56:35 +02002893 }
2894
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002895 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Andre Guedes92c4c202012-06-07 19:05:44 -03002896 rp = kmalloc(rp_len, GFP_KERNEL);
Johan Hedberga38528f2011-01-22 06:46:43 +02002897 if (!rp) {
Johan Hedberg2784eb42011-01-21 13:56:35 +02002898 err = -ENOMEM;
2899 goto unlock;
2900 }
2901
Johan Hedberg2784eb42011-01-21 13:56:35 +02002902 i = 0;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002903 list_for_each_entry(c, &hdev->conn_hash.list, list) {
Johan Hedbergb644ba32012-01-17 21:48:47 +02002904 if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
2905 continue;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002906 bacpy(&rp->addr[i].bdaddr, &c->dst);
Andre Guedes57c14772012-04-24 21:02:50 -03002907 rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type);
Andre Guedes0ed09142012-04-03 08:46:54 -03002908 if (c->type == SCO_LINK || c->type == ESCO_LINK)
Johan Hedberg4c659c32011-11-07 23:13:39 +02002909 continue;
2910 i++;
2911 }
2912
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02002913 rp->conn_count = cpu_to_le16(i);
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002914
Johan Hedberg4c659c32011-11-07 23:13:39 +02002915 /* Recalculate length in case of filtered SCO connections, etc */
2916 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Johan Hedberg2784eb42011-01-21 13:56:35 +02002917
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002918 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002919 rp_len);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002920
Johan Hedberga38528f2011-01-22 06:46:43 +02002921 kfree(rp);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002922
2923unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002924 hci_dev_unlock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002925 return err;
2926}
2927
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002928static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002929 struct mgmt_cp_pin_code_neg_reply *cp)
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002930{
2931 struct pending_cmd *cmd;
2932 int err;
2933
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002934 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002935 sizeof(*cp));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002936 if (!cmd)
2937 return -ENOMEM;
2938
Johan Hedbergd8457692012-02-17 14:24:57 +02002939 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002940 sizeof(cp->addr.bdaddr), &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002941 if (err < 0)
2942 mgmt_pending_remove(cmd);
2943
2944 return err;
2945}
2946
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002947static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002948 u16 len)
Johan Hedberg980e1a52011-01-22 06:10:07 +02002949{
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002950 struct hci_conn *conn;
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002951 struct mgmt_cp_pin_code_reply *cp = data;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002952 struct hci_cp_pin_code_reply reply;
Johan Hedberg366a0332011-02-19 12:05:55 -03002953 struct pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002954 int err;
2955
2956 BT_DBG("");
2957
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002958 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002959
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002960 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002961 err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002962 MGMT_STATUS_NOT_POWERED);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002963 goto failed;
2964 }
2965
Johan Hedbergd8457692012-02-17 14:24:57 +02002966 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002967 if (!conn) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002968 err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002969 MGMT_STATUS_NOT_CONNECTED);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002970 goto failed;
2971 }
2972
2973 if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) {
Johan Hedbergd8457692012-02-17 14:24:57 +02002974 struct mgmt_cp_pin_code_neg_reply ncp;
2975
2976 memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002977
2978 BT_ERR("PIN code is not 16 bytes long");
2979
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002980 err = send_pin_code_neg_reply(sk, hdev, &ncp);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002981 if (err >= 0)
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002982 err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002983 MGMT_STATUS_INVALID_PARAMS);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002984
2985 goto failed;
2986 }
2987
Gustavo F. Padovan00abfe42012-03-01 00:37:10 -03002988 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03002989 if (!cmd) {
2990 err = -ENOMEM;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002991 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03002992 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02002993
Johan Hedbergd8457692012-02-17 14:24:57 +02002994 bacpy(&reply.bdaddr, &cp->addr.bdaddr);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002995 reply.pin_len = cp->pin_len;
Waldemar Rymarkiewicz24718ca2011-06-01 17:28:47 +02002996 memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code));
Johan Hedberg980e1a52011-01-22 06:10:07 +02002997
2998 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
2999 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03003000 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003001
3002failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003003 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003004 return err;
3005}
3006
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003007static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
3008 u16 len)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003009{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003010 struct mgmt_cp_set_io_capability *cp = data;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003011
3012 BT_DBG("");
3013
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003014 if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY)
3015 return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY,
3016 MGMT_STATUS_INVALID_PARAMS, NULL, 0);
3017
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003018 hci_dev_lock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003019
3020 hdev->io_capability = cp->io_capability;
3021
3022 BT_DBG("%s IO capability set to 0x%02x", hdev->name,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003023 hdev->io_capability);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003024
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003025 hci_dev_unlock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003026
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003027 return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL,
3028 0);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003029}
3030
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003031static struct pending_cmd *find_pairing(struct hci_conn *conn)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003032{
3033 struct hci_dev *hdev = conn->hdev;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02003034 struct pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003035
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003036 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
Johan Hedberge9a416b2011-02-19 12:05:56 -03003037 if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
3038 continue;
3039
Johan Hedberge9a416b2011-02-19 12:05:56 -03003040 if (cmd->user_data != conn)
3041 continue;
3042
3043 return cmd;
3044 }
3045
3046 return NULL;
3047}
3048
3049static void pairing_complete(struct pending_cmd *cmd, u8 status)
3050{
3051 struct mgmt_rp_pair_device rp;
3052 struct hci_conn *conn = cmd->user_data;
3053
Johan Hedberg61b1a7f2014-03-20 12:54:16 +02003054 bacpy(&rp.addr.bdaddr, &conn->dst);
3055 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003056
Johan Hedbergaee9b212012-02-18 15:07:59 +02003057 cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003058 &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003059
3060 /* So we don't get further callbacks for this connection */
3061 conn->connect_cfm_cb = NULL;
3062 conn->security_cfm_cb = NULL;
3063 conn->disconn_cfm_cb = NULL;
3064
David Herrmann76a68ba2013-04-06 20:28:37 +02003065 hci_conn_drop(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03003066 hci_conn_put(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003067
Johan Hedberga664b5b2011-02-19 12:06:02 -03003068 mgmt_pending_remove(cmd);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003069}
3070
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003071void mgmt_smp_complete(struct hci_conn *conn, bool complete)
3072{
3073 u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED;
3074 struct pending_cmd *cmd;
3075
3076 cmd = find_pairing(conn);
3077 if (cmd)
3078 pairing_complete(cmd, status);
3079}
3080
Johan Hedberge9a416b2011-02-19 12:05:56 -03003081static void pairing_complete_cb(struct hci_conn *conn, u8 status)
3082{
3083 struct pending_cmd *cmd;
3084
3085 BT_DBG("status %u", status);
3086
Johan Hedberg56e5cb82011-11-08 20:40:16 +02003087 cmd = find_pairing(conn);
3088 if (!cmd)
3089 BT_DBG("Unable to find a pending command");
3090 else
Johan Hedberge2113262012-02-18 15:20:03 +02003091 pairing_complete(cmd, mgmt_status(status));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003092}
3093
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003094static void le_pairing_complete_cb(struct hci_conn *conn, u8 status)
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303095{
3096 struct pending_cmd *cmd;
3097
3098 BT_DBG("status %u", status);
3099
3100 if (!status)
3101 return;
3102
3103 cmd = find_pairing(conn);
3104 if (!cmd)
3105 BT_DBG("Unable to find a pending command");
3106 else
3107 pairing_complete(cmd, mgmt_status(status));
3108}
3109
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003110static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003111 u16 len)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003112{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003113 struct mgmt_cp_pair_device *cp = data;
Johan Hedberg1425acb2011-11-11 00:07:35 +02003114 struct mgmt_rp_pair_device rp;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003115 struct pending_cmd *cmd;
3116 u8 sec_level, auth_type;
3117 struct hci_conn *conn;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003118 int err;
3119
3120 BT_DBG("");
3121
Szymon Jancf950a30e2013-01-18 12:48:07 +01003122 memset(&rp, 0, sizeof(rp));
3123 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3124 rp.addr.type = cp->addr.type;
3125
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003126 if (!bdaddr_type_is_valid(cp->addr.type))
3127 return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3128 MGMT_STATUS_INVALID_PARAMS,
3129 &rp, sizeof(rp));
3130
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003131 if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY)
3132 return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3133 MGMT_STATUS_INVALID_PARAMS,
3134 &rp, sizeof(rp));
3135
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003136 hci_dev_lock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003137
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003138 if (!hdev_is_powered(hdev)) {
Szymon Jancf950a30e2013-01-18 12:48:07 +01003139 err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3140 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp));
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003141 goto unlock;
3142 }
3143
Vinicius Costa Gomesc908df32011-09-02 14:51:22 -03003144 sec_level = BT_SECURITY_MEDIUM;
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003145 auth_type = HCI_AT_DEDICATED_BONDING;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003146
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003147 if (cp->addr.type == BDADDR_BREDR) {
Andre Guedes04a6c582014-02-26 20:21:44 -03003148 conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
3149 auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003150 } else {
3151 u8 addr_type;
3152
3153 /* Convert from L2CAP channel address type to HCI address type
3154 */
3155 if (cp->addr.type == BDADDR_LE_PUBLIC)
3156 addr_type = ADDR_LE_DEV_PUBLIC;
3157 else
3158 addr_type = ADDR_LE_DEV_RANDOM;
3159
Marcel Holtmann7c264b12014-06-30 12:34:40 +02003160 /* When pairing a new device, it is expected to remember
3161 * this device for future connections. Adding the connection
3162 * parameter information ahead of time allows tracking
3163 * of the slave preferred values and will speed up any
3164 * further connection establishment.
3165 *
3166 * If connection parameters already exist, then they
3167 * will be kept and this function does nothing.
3168 */
3169 hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type);
3170
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003171 conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type,
Johan Hedberge804d252014-07-16 11:42:28 +03003172 sec_level, HCI_LE_CONN_TIMEOUT,
3173 HCI_ROLE_MASTER);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003174 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003175
Ville Tervo30e76272011-02-22 16:10:53 -03003176 if (IS_ERR(conn)) {
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02003177 int status;
3178
3179 if (PTR_ERR(conn) == -EBUSY)
3180 status = MGMT_STATUS_BUSY;
3181 else
3182 status = MGMT_STATUS_CONNECT_FAILED;
3183
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003184 err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02003185 status, &rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003186 sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003187 goto unlock;
3188 }
3189
3190 if (conn->connect_cfm_cb) {
David Herrmann76a68ba2013-04-06 20:28:37 +02003191 hci_conn_drop(conn);
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003192 err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003193 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003194 goto unlock;
3195 }
3196
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003197 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003198 if (!cmd) {
3199 err = -ENOMEM;
David Herrmann76a68ba2013-04-06 20:28:37 +02003200 hci_conn_drop(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003201 goto unlock;
3202 }
3203
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003204 /* For LE, just connecting isn't a proof that the pairing finished */
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003205 if (cp->addr.type == BDADDR_BREDR) {
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003206 conn->connect_cfm_cb = pairing_complete_cb;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003207 conn->security_cfm_cb = pairing_complete_cb;
3208 conn->disconn_cfm_cb = pairing_complete_cb;
3209 } else {
3210 conn->connect_cfm_cb = le_pairing_complete_cb;
3211 conn->security_cfm_cb = le_pairing_complete_cb;
3212 conn->disconn_cfm_cb = le_pairing_complete_cb;
3213 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003214
Johan Hedberge9a416b2011-02-19 12:05:56 -03003215 conn->io_capability = cp->io_cap;
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03003216 cmd->user_data = hci_conn_get(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003217
Johan Hedberg6f78fd42014-07-30 08:35:48 +03003218 if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) &&
Johan Hedberge7cafc42014-07-17 15:35:38 +03003219 hci_conn_security(conn, sec_level, auth_type, true))
Johan Hedberge9a416b2011-02-19 12:05:56 -03003220 pairing_complete(cmd, 0);
3221
3222 err = 0;
3223
3224unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003225 hci_dev_unlock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003226 return err;
3227}
3228
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003229static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3230 u16 len)
Johan Hedberg28424702012-02-02 04:02:29 +02003231{
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003232 struct mgmt_addr_info *addr = data;
Johan Hedberg28424702012-02-02 04:02:29 +02003233 struct pending_cmd *cmd;
3234 struct hci_conn *conn;
3235 int err;
3236
3237 BT_DBG("");
3238
Johan Hedberg28424702012-02-02 04:02:29 +02003239 hci_dev_lock(hdev);
3240
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003241 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003242 err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003243 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003244 goto unlock;
3245 }
3246
Johan Hedberg28424702012-02-02 04:02:29 +02003247 cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev);
3248 if (!cmd) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003249 err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003250 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003251 goto unlock;
3252 }
3253
3254 conn = cmd->user_data;
3255
3256 if (bacmp(&addr->bdaddr, &conn->dst) != 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003257 err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003258 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003259 goto unlock;
3260 }
3261
3262 pairing_complete(cmd, MGMT_STATUS_CANCELLED);
3263
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003264 err = cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003265 addr, sizeof(*addr));
Johan Hedberg28424702012-02-02 04:02:29 +02003266unlock:
3267 hci_dev_unlock(hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02003268 return err;
3269}
3270
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003271static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
Johan Hedberg1707c602013-03-15 17:07:15 -05003272 struct mgmt_addr_info *addr, u16 mgmt_op,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003273 u16 hci_op, __le32 passkey)
Johan Hedberga5c29682011-02-19 12:05:57 -03003274{
Johan Hedberga5c29682011-02-19 12:05:57 -03003275 struct pending_cmd *cmd;
Brian Gix0df4c182011-11-16 13:53:13 -08003276 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03003277 int err;
3278
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003279 hci_dev_lock(hdev);
Johan Hedberg08ba5382011-03-16 14:29:34 +02003280
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003281 if (!hdev_is_powered(hdev)) {
Johan Hedbergfeb94d32013-03-15 17:07:16 -05003282 err = cmd_complete(sk, hdev->id, mgmt_op,
3283 MGMT_STATUS_NOT_POWERED, addr,
3284 sizeof(*addr));
Brian Gix0df4c182011-11-16 13:53:13 -08003285 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003286 }
3287
Johan Hedberg1707c602013-03-15 17:07:15 -05003288 if (addr->type == BDADDR_BREDR)
3289 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr);
Johan Hedberg272d90d2012-02-09 15:26:12 +02003290 else
Johan Hedberg1707c602013-03-15 17:07:15 -05003291 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr);
Brian Gix47c15e22011-11-16 13:53:14 -08003292
Johan Hedberg272d90d2012-02-09 15:26:12 +02003293 if (!conn) {
Johan Hedbergfeb94d32013-03-15 17:07:16 -05003294 err = cmd_complete(sk, hdev->id, mgmt_op,
3295 MGMT_STATUS_NOT_CONNECTED, addr,
3296 sizeof(*addr));
Johan Hedberg272d90d2012-02-09 15:26:12 +02003297 goto done;
3298 }
3299
Johan Hedberg1707c602013-03-15 17:07:15 -05003300 if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
Brian Gix5fe57d92011-12-21 16:12:13 -08003301 err = smp_user_confirm_reply(conn, mgmt_op, passkey);
Brian Gix5fe57d92011-12-21 16:12:13 -08003302 if (!err)
Johan Hedbergfeb94d32013-03-15 17:07:16 -05003303 err = cmd_complete(sk, hdev->id, mgmt_op,
3304 MGMT_STATUS_SUCCESS, addr,
3305 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003306 else
Johan Hedbergfeb94d32013-03-15 17:07:16 -05003307 err = cmd_complete(sk, hdev->id, mgmt_op,
3308 MGMT_STATUS_FAILED, addr,
3309 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003310
Brian Gix47c15e22011-11-16 13:53:14 -08003311 goto done;
3312 }
3313
Johan Hedberg1707c602013-03-15 17:07:15 -05003314 cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr));
Johan Hedberga5c29682011-02-19 12:05:57 -03003315 if (!cmd) {
3316 err = -ENOMEM;
Brian Gix0df4c182011-11-16 13:53:13 -08003317 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003318 }
3319
Brian Gix0df4c182011-11-16 13:53:13 -08003320 /* Continue with pairing via HCI */
Brian Gix604086b2011-11-23 08:28:33 -08003321 if (hci_op == HCI_OP_USER_PASSKEY_REPLY) {
3322 struct hci_cp_user_passkey_reply cp;
3323
Johan Hedberg1707c602013-03-15 17:07:15 -05003324 bacpy(&cp.bdaddr, &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003325 cp.passkey = passkey;
3326 err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
3327 } else
Johan Hedberg1707c602013-03-15 17:07:15 -05003328 err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr),
3329 &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003330
Johan Hedberga664b5b2011-02-19 12:06:02 -03003331 if (err < 0)
3332 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03003333
Brian Gix0df4c182011-11-16 13:53:13 -08003334done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003335 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03003336 return err;
3337}
3338
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303339static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
3340 void *data, u16 len)
3341{
3342 struct mgmt_cp_pin_code_neg_reply *cp = data;
3343
3344 BT_DBG("");
3345
Johan Hedberg1707c602013-03-15 17:07:15 -05003346 return user_pairing_resp(sk, hdev, &cp->addr,
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303347 MGMT_OP_PIN_CODE_NEG_REPLY,
3348 HCI_OP_PIN_CODE_NEG_REPLY, 0);
3349}
3350
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003351static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3352 u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003353{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003354 struct mgmt_cp_user_confirm_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003355
3356 BT_DBG("");
3357
3358 if (len != sizeof(*cp))
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003359 return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003360 MGMT_STATUS_INVALID_PARAMS);
Brian Gix0df4c182011-11-16 13:53:13 -08003361
Johan Hedberg1707c602013-03-15 17:07:15 -05003362 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003363 MGMT_OP_USER_CONFIRM_REPLY,
3364 HCI_OP_USER_CONFIRM_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003365}
3366
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003367static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003368 void *data, u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003369{
Johan Hedbergc9c26592011-12-15 00:47:41 +02003370 struct mgmt_cp_user_confirm_neg_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003371
3372 BT_DBG("");
3373
Johan Hedberg1707c602013-03-15 17:07:15 -05003374 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003375 MGMT_OP_USER_CONFIRM_NEG_REPLY,
3376 HCI_OP_USER_CONFIRM_NEG_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003377}
3378
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003379static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3380 u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003381{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003382 struct mgmt_cp_user_passkey_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003383
3384 BT_DBG("");
3385
Johan Hedberg1707c602013-03-15 17:07:15 -05003386 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003387 MGMT_OP_USER_PASSKEY_REPLY,
3388 HCI_OP_USER_PASSKEY_REPLY, cp->passkey);
Brian Gix604086b2011-11-23 08:28:33 -08003389}
3390
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003391static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003392 void *data, u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003393{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003394 struct mgmt_cp_user_passkey_neg_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003395
3396 BT_DBG("");
3397
Johan Hedberg1707c602013-03-15 17:07:15 -05003398 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003399 MGMT_OP_USER_PASSKEY_NEG_REPLY,
3400 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
Brian Gix604086b2011-11-23 08:28:33 -08003401}
3402
Johan Hedberg13928972013-03-15 17:07:00 -05003403static void update_name(struct hci_request *req)
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003404{
Johan Hedberg13928972013-03-15 17:07:00 -05003405 struct hci_dev *hdev = req->hdev;
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003406 struct hci_cp_write_local_name cp;
3407
Johan Hedberg13928972013-03-15 17:07:00 -05003408 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003409
Johan Hedberg890ea892013-03-15 17:06:52 -05003410 hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp);
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003411}
3412
Johan Hedberg13928972013-03-15 17:07:00 -05003413static void set_name_complete(struct hci_dev *hdev, u8 status)
3414{
3415 struct mgmt_cp_set_local_name *cp;
3416 struct pending_cmd *cmd;
3417
3418 BT_DBG("status 0x%02x", status);
3419
3420 hci_dev_lock(hdev);
3421
3422 cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
3423 if (!cmd)
3424 goto unlock;
3425
3426 cp = cmd->param;
3427
3428 if (status)
3429 cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
3430 mgmt_status(status));
3431 else
3432 cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3433 cp, sizeof(*cp));
3434
3435 mgmt_pending_remove(cmd);
3436
3437unlock:
3438 hci_dev_unlock(hdev);
3439}
3440
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003441static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003442 u16 len)
Johan Hedbergb312b1612011-03-16 14:29:37 +02003443{
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003444 struct mgmt_cp_set_local_name *cp = data;
Johan Hedbergb312b1612011-03-16 14:29:37 +02003445 struct pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05003446 struct hci_request req;
Johan Hedbergb312b1612011-03-16 14:29:37 +02003447 int err;
3448
3449 BT_DBG("");
3450
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003451 hci_dev_lock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003452
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003453 /* If the old values are the same as the new ones just return a
3454 * direct command complete event.
3455 */
3456 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
3457 !memcmp(hdev->short_name, cp->short_name,
3458 sizeof(hdev->short_name))) {
3459 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3460 data, len);
3461 goto failed;
3462 }
3463
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003464 memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003465
Johan Hedbergb5235a62012-02-21 14:32:24 +02003466 if (!hdev_is_powered(hdev)) {
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003467 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003468
3469 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003470 data, len);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003471 if (err < 0)
3472 goto failed;
3473
3474 err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003475 sk);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003476
Johan Hedbergb5235a62012-02-21 14:32:24 +02003477 goto failed;
3478 }
3479
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003480 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003481 if (!cmd) {
3482 err = -ENOMEM;
3483 goto failed;
3484 }
3485
Johan Hedberg13928972013-03-15 17:07:00 -05003486 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3487
Johan Hedberg890ea892013-03-15 17:06:52 -05003488 hci_req_init(&req, hdev);
Johan Hedberg3f985052013-03-15 17:07:02 -05003489
3490 if (lmp_bredr_capable(hdev)) {
3491 update_name(&req);
3492 update_eir(&req);
3493 }
3494
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003495 /* The name is stored in the scan response data and so
3496 * no need to udpate the advertising data here.
3497 */
Johan Hedberg3f985052013-03-15 17:07:02 -05003498 if (lmp_le_capable(hdev))
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003499 update_scan_rsp_data(&req);
Johan Hedberg3f985052013-03-15 17:07:02 -05003500
Johan Hedberg13928972013-03-15 17:07:00 -05003501 err = hci_req_run(&req, set_name_complete);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003502 if (err < 0)
3503 mgmt_pending_remove(cmd);
3504
3505failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003506 hci_dev_unlock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003507 return err;
3508}
3509
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003510static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003511 void *data, u16 data_len)
Szymon Jancc35938b2011-03-22 13:12:21 +01003512{
Szymon Jancc35938b2011-03-22 13:12:21 +01003513 struct pending_cmd *cmd;
3514 int err;
3515
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003516 BT_DBG("%s", hdev->name);
Szymon Jancc35938b2011-03-22 13:12:21 +01003517
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003518 hci_dev_lock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003519
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003520 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003521 err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003522 MGMT_STATUS_NOT_POWERED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003523 goto unlock;
3524 }
3525
Andre Guedes9a1a1992012-07-24 15:03:48 -03003526 if (!lmp_ssp_capable(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003527 err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003528 MGMT_STATUS_NOT_SUPPORTED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003529 goto unlock;
3530 }
3531
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003532 if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003533 err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003534 MGMT_STATUS_BUSY);
Szymon Jancc35938b2011-03-22 13:12:21 +01003535 goto unlock;
3536 }
3537
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003538 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
Szymon Jancc35938b2011-03-22 13:12:21 +01003539 if (!cmd) {
3540 err = -ENOMEM;
3541 goto unlock;
3542 }
3543
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08003544 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags))
3545 err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA,
3546 0, NULL);
3547 else
3548 err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
3549
Szymon Jancc35938b2011-03-22 13:12:21 +01003550 if (err < 0)
3551 mgmt_pending_remove(cmd);
3552
3553unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003554 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003555 return err;
3556}
3557
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003558static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003559 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003560{
Szymon Janc2763eda2011-03-22 13:12:22 +01003561 int err;
3562
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003563 BT_DBG("%s ", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003564
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003565 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003566
Marcel Holtmannec109112014-01-10 02:07:30 -08003567 if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) {
3568 struct mgmt_cp_add_remote_oob_data *cp = data;
3569 u8 status;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003570
Marcel Holtmannec109112014-01-10 02:07:30 -08003571 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
3572 cp->hash, cp->randomizer);
3573 if (err < 0)
3574 status = MGMT_STATUS_FAILED;
3575 else
3576 status = MGMT_STATUS_SUCCESS;
3577
3578 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3579 status, &cp->addr, sizeof(cp->addr));
3580 } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) {
3581 struct mgmt_cp_add_remote_oob_ext_data *cp = data;
3582 u8 status;
3583
3584 err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr,
3585 cp->hash192,
3586 cp->randomizer192,
3587 cp->hash256,
3588 cp->randomizer256);
3589 if (err < 0)
3590 status = MGMT_STATUS_FAILED;
3591 else
3592 status = MGMT_STATUS_SUCCESS;
3593
3594 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3595 status, &cp->addr, sizeof(cp->addr));
3596 } else {
3597 BT_ERR("add_remote_oob_data: invalid length of %u bytes", len);
3598 err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3599 MGMT_STATUS_INVALID_PARAMS);
3600 }
Szymon Janc2763eda2011-03-22 13:12:22 +01003601
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003602 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003603 return err;
3604}
3605
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003606static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003607 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003608{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003609 struct mgmt_cp_remove_remote_oob_data *cp = data;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003610 u8 status;
Szymon Janc2763eda2011-03-22 13:12:22 +01003611 int err;
3612
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003613 BT_DBG("%s", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003614
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003615 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003616
Johan Hedberg664ce4c2012-02-09 15:44:09 +02003617 err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr);
Szymon Janc2763eda2011-03-22 13:12:22 +01003618 if (err < 0)
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003619 status = MGMT_STATUS_INVALID_PARAMS;
Szymon Janc2763eda2011-03-22 13:12:22 +01003620 else
Szymon Janca6785be2012-12-13 15:11:21 +01003621 status = MGMT_STATUS_SUCCESS;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003622
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003623 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003624 status, &cp->addr, sizeof(cp->addr));
Szymon Janc2763eda2011-03-22 13:12:22 +01003625
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003626 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003627 return err;
3628}
3629
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003630static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
3631{
3632 struct pending_cmd *cmd;
3633 u8 type;
3634 int err;
3635
3636 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
3637
3638 cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev);
3639 if (!cmd)
3640 return -ENOENT;
3641
3642 type = hdev->discovery.type;
3643
3644 err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status),
3645 &type, sizeof(type));
3646 mgmt_pending_remove(cmd);
3647
3648 return err;
3649}
3650
Andre Guedes7c307722013-04-30 15:29:28 -03003651static void start_discovery_complete(struct hci_dev *hdev, u8 status)
3652{
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01003653 unsigned long timeout = 0;
3654
Andre Guedes7c307722013-04-30 15:29:28 -03003655 BT_DBG("status %d", status);
3656
3657 if (status) {
3658 hci_dev_lock(hdev);
3659 mgmt_start_discovery_failed(hdev, status);
3660 hci_dev_unlock(hdev);
3661 return;
3662 }
3663
3664 hci_dev_lock(hdev);
3665 hci_discovery_set_state(hdev, DISCOVERY_FINDING);
3666 hci_dev_unlock(hdev);
3667
3668 switch (hdev->discovery.type) {
3669 case DISCOV_TYPE_LE:
Lukasz Rymanowski3d5a76f2014-03-27 20:55:21 +01003670 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
Andre Guedes7c307722013-04-30 15:29:28 -03003671 break;
3672
3673 case DISCOV_TYPE_INTERLEAVED:
Lukasz Rymanowskib9a7a612014-03-27 20:55:20 +01003674 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
Andre Guedes7c307722013-04-30 15:29:28 -03003675 break;
3676
3677 case DISCOV_TYPE_BREDR:
3678 break;
3679
3680 default:
3681 BT_ERR("Invalid discovery type %d", hdev->discovery.type);
3682 }
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01003683
3684 if (!timeout)
3685 return;
3686
3687 queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout);
Andre Guedes7c307722013-04-30 15:29:28 -03003688}
3689
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003690static int start_discovery(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003691 void *data, u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04003692{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003693 struct mgmt_cp_start_discovery *cp = data;
Johan Hedberg14a53662011-04-27 10:29:56 -04003694 struct pending_cmd *cmd;
Andre Guedes7c307722013-04-30 15:29:28 -03003695 struct hci_cp_le_set_scan_param param_cp;
3696 struct hci_cp_le_set_scan_enable enable_cp;
3697 struct hci_cp_inquiry inq_cp;
3698 struct hci_request req;
3699 /* General inquiry access code (GIAC) */
3700 u8 lap[3] = { 0x33, 0x8b, 0x9e };
Johan Hedbergd9483942014-02-23 19:42:24 +02003701 u8 status, own_addr_type;
Johan Hedberg14a53662011-04-27 10:29:56 -04003702 int err;
3703
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003704 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04003705
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003706 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04003707
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003708 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003709 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003710 MGMT_STATUS_NOT_POWERED);
Johan Hedbergbd2d1332011-11-07 23:13:37 +02003711 goto failed;
3712 }
3713
Andre Guedes642be6c2012-03-21 00:03:37 -03003714 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) {
3715 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
3716 MGMT_STATUS_BUSY);
3717 goto failed;
3718 }
3719
Johan Hedbergff9ef572012-01-04 14:23:45 +02003720 if (hdev->discovery.state != DISCOVERY_STOPPED) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003721 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003722 MGMT_STATUS_BUSY);
Johan Hedbergff9ef572012-01-04 14:23:45 +02003723 goto failed;
3724 }
3725
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003726 cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0);
Johan Hedberg14a53662011-04-27 10:29:56 -04003727 if (!cmd) {
3728 err = -ENOMEM;
3729 goto failed;
3730 }
3731
Andre Guedes4aab14e2012-02-17 20:39:36 -03003732 hdev->discovery.type = cp->type;
3733
Andre Guedes7c307722013-04-30 15:29:28 -03003734 hci_req_init(&req, hdev);
3735
Andre Guedes4aab14e2012-02-17 20:39:36 -03003736 switch (hdev->discovery.type) {
Andre Guedesf39799f2012-02-17 20:39:35 -03003737 case DISCOV_TYPE_BREDR:
Johan Hedberge6fe7982013-10-02 15:45:22 +03003738 status = mgmt_bredr_support(hdev);
3739 if (status) {
Johan Hedberg04106752013-01-10 14:54:09 +02003740 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
Johan Hedberge6fe7982013-10-02 15:45:22 +03003741 status);
Johan Hedberg04106752013-01-10 14:54:09 +02003742 mgmt_pending_remove(cmd);
3743 goto failed;
3744 }
3745
Andre Guedes7c307722013-04-30 15:29:28 -03003746 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
3747 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
3748 MGMT_STATUS_BUSY);
3749 mgmt_pending_remove(cmd);
3750 goto failed;
3751 }
3752
3753 hci_inquiry_cache_flush(hdev);
3754
3755 memset(&inq_cp, 0, sizeof(inq_cp));
3756 memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap));
Andre Guedes0d8cc932013-04-30 15:29:31 -03003757 inq_cp.length = DISCOV_BREDR_INQUIRY_LEN;
Andre Guedes7c307722013-04-30 15:29:28 -03003758 hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp);
Andre Guedesf39799f2012-02-17 20:39:35 -03003759 break;
3760
3761 case DISCOV_TYPE_LE:
Andre Guedes7c307722013-04-30 15:29:28 -03003762 case DISCOV_TYPE_INTERLEAVED:
Johan Hedberge6fe7982013-10-02 15:45:22 +03003763 status = mgmt_le_support(hdev);
3764 if (status) {
Johan Hedberg04106752013-01-10 14:54:09 +02003765 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
Johan Hedberge6fe7982013-10-02 15:45:22 +03003766 status);
Johan Hedberg04106752013-01-10 14:54:09 +02003767 mgmt_pending_remove(cmd);
3768 goto failed;
3769 }
3770
Andre Guedes7c307722013-04-30 15:29:28 -03003771 if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED &&
Johan Hedberg56f87902013-10-02 13:43:13 +03003772 !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
Johan Hedberg04106752013-01-10 14:54:09 +02003773 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
3774 MGMT_STATUS_NOT_SUPPORTED);
3775 mgmt_pending_remove(cmd);
3776 goto failed;
3777 }
3778
Johan Hedberge8bb6b92014-07-08 15:07:53 +03003779 if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) {
3780 /* Don't let discovery abort an outgoing
3781 * connection attempt that's using directed
3782 * advertising.
3783 */
3784 if (hci_conn_hash_lookup_state(hdev, LE_LINK,
3785 BT_CONNECT)) {
3786 err = cmd_status(sk, hdev->id,
3787 MGMT_OP_START_DISCOVERY,
3788 MGMT_STATUS_REJECTED);
3789 mgmt_pending_remove(cmd);
3790 goto failed;
3791 }
3792
3793 disable_advertising(&req);
Andre Guedes7c307722013-04-30 15:29:28 -03003794 }
3795
Andre Guedesc54c3862014-02-26 20:21:50 -03003796 /* If controller is scanning, it means the background scanning
3797 * is running. Thus, we should temporarily stop it in order to
3798 * set the discovery scanning parameters.
3799 */
3800 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags))
3801 hci_req_add_le_scan_disable(&req);
Andre Guedes7c307722013-04-30 15:29:28 -03003802
3803 memset(&param_cp, 0, sizeof(param_cp));
Johan Hedbergd9483942014-02-23 19:42:24 +02003804
Marcel Holtmann94b1fc92014-02-23 20:25:54 -08003805 /* All active scans will be done with either a resolvable
3806 * private address (when privacy feature has been enabled)
3807 * or unresolvable private address.
3808 */
3809 err = hci_update_random_address(&req, true, &own_addr_type);
Johan Hedbergd9483942014-02-23 19:42:24 +02003810 if (err < 0) {
3811 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
3812 MGMT_STATUS_FAILED);
3813 mgmt_pending_remove(cmd);
3814 goto failed;
3815 }
3816
Andre Guedes7c307722013-04-30 15:29:28 -03003817 param_cp.type = LE_SCAN_ACTIVE;
Andre Guedes0d8cc932013-04-30 15:29:31 -03003818 param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT);
3819 param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN);
Johan Hedbergd9483942014-02-23 19:42:24 +02003820 param_cp.own_address_type = own_addr_type;
Andre Guedes7c307722013-04-30 15:29:28 -03003821 hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
3822 &param_cp);
3823
3824 memset(&enable_cp, 0, sizeof(enable_cp));
3825 enable_cp.enable = LE_SCAN_ENABLE;
3826 enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
3827 hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
3828 &enable_cp);
Andre Guedes5e0452c2012-02-17 20:39:38 -03003829 break;
3830
Andre Guedesf39799f2012-02-17 20:39:35 -03003831 default:
Johan Hedberg04106752013-01-10 14:54:09 +02003832 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
3833 MGMT_STATUS_INVALID_PARAMS);
3834 mgmt_pending_remove(cmd);
3835 goto failed;
Andre Guedesf39799f2012-02-17 20:39:35 -03003836 }
Andre Guedes3fd24152012-02-03 17:48:01 -03003837
Andre Guedes7c307722013-04-30 15:29:28 -03003838 err = hci_req_run(&req, start_discovery_complete);
Johan Hedberg14a53662011-04-27 10:29:56 -04003839 if (err < 0)
3840 mgmt_pending_remove(cmd);
Johan Hedbergff9ef572012-01-04 14:23:45 +02003841 else
3842 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
Johan Hedberg14a53662011-04-27 10:29:56 -04003843
3844failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003845 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04003846 return err;
3847}
3848
Andre Guedes1183fdc2013-04-30 15:29:35 -03003849static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status)
3850{
3851 struct pending_cmd *cmd;
3852 int err;
3853
3854 cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
3855 if (!cmd)
3856 return -ENOENT;
3857
3858 err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status),
3859 &hdev->discovery.type, sizeof(hdev->discovery.type));
3860 mgmt_pending_remove(cmd);
3861
3862 return err;
3863}
3864
Andre Guedes0e05bba2013-04-30 15:29:33 -03003865static void stop_discovery_complete(struct hci_dev *hdev, u8 status)
3866{
3867 BT_DBG("status %d", status);
3868
3869 hci_dev_lock(hdev);
3870
3871 if (status) {
3872 mgmt_stop_discovery_failed(hdev, status);
3873 goto unlock;
3874 }
3875
3876 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
3877
3878unlock:
3879 hci_dev_unlock(hdev);
3880}
3881
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003882static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003883 u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04003884{
Johan Hedbergd9306502012-02-20 23:25:18 +02003885 struct mgmt_cp_stop_discovery *mgmt_cp = data;
Johan Hedberg14a53662011-04-27 10:29:56 -04003886 struct pending_cmd *cmd;
Andre Guedes0e05bba2013-04-30 15:29:33 -03003887 struct hci_request req;
Johan Hedberg14a53662011-04-27 10:29:56 -04003888 int err;
3889
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003890 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04003891
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003892 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04003893
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003894 if (!hci_discovery_active(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003895 err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003896 MGMT_STATUS_REJECTED, &mgmt_cp->type,
3897 sizeof(mgmt_cp->type));
Johan Hedbergd9306502012-02-20 23:25:18 +02003898 goto unlock;
3899 }
3900
3901 if (hdev->discovery.type != mgmt_cp->type) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003902 err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003903 MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type,
3904 sizeof(mgmt_cp->type));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003905 goto unlock;
Johan Hedbergff9ef572012-01-04 14:23:45 +02003906 }
3907
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003908 cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0);
Johan Hedberg14a53662011-04-27 10:29:56 -04003909 if (!cmd) {
3910 err = -ENOMEM;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003911 goto unlock;
Johan Hedberg14a53662011-04-27 10:29:56 -04003912 }
3913
Andre Guedes0e05bba2013-04-30 15:29:33 -03003914 hci_req_init(&req, hdev);
3915
Johan Hedberg21a60d32014-06-10 14:05:58 +03003916 hci_stop_discovery(&req);
Andre Guedes0e05bba2013-04-30 15:29:33 -03003917
Johan Hedberg21a60d32014-06-10 14:05:58 +03003918 err = hci_req_run(&req, stop_discovery_complete);
3919 if (!err) {
3920 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
Andre Guedes0e05bba2013-04-30 15:29:33 -03003921 goto unlock;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003922 }
3923
Johan Hedberg21a60d32014-06-10 14:05:58 +03003924 mgmt_pending_remove(cmd);
3925
3926 /* If no HCI commands were sent we're done */
3927 if (err == -ENODATA) {
3928 err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0,
3929 &mgmt_cp->type, sizeof(mgmt_cp->type));
3930 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
3931 }
Johan Hedberg14a53662011-04-27 10:29:56 -04003932
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003933unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003934 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04003935 return err;
3936}
3937
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003938static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003939 u16 len)
Johan Hedberg561aafb2012-01-04 13:31:59 +02003940{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003941 struct mgmt_cp_confirm_name *cp = data;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003942 struct inquiry_entry *e;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003943 int err;
3944
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003945 BT_DBG("%s", hdev->name);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003946
Johan Hedberg561aafb2012-01-04 13:31:59 +02003947 hci_dev_lock(hdev);
3948
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003949 if (!hci_discovery_active(hdev)) {
Lukasz Rymanowskid3a25412014-02-27 16:47:28 +01003950 err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
3951 MGMT_STATUS_FAILED, &cp->addr,
3952 sizeof(cp->addr));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003953 goto failed;
3954 }
3955
Johan Hedberga198e7b2012-02-17 14:27:06 +02003956 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003957 if (!e) {
Lukasz Rymanowskid3a25412014-02-27 16:47:28 +01003958 err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
3959 MGMT_STATUS_INVALID_PARAMS, &cp->addr,
3960 sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02003961 goto failed;
3962 }
3963
3964 if (cp->name_known) {
3965 e->name_state = NAME_KNOWN;
3966 list_del(&e->list);
3967 } else {
3968 e->name_state = NAME_NEEDED;
Johan Hedberga3d4e202012-01-09 00:53:02 +02003969 hci_inquiry_cache_update_resolve(hdev, e);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003970 }
3971
Johan Hedberge3846622013-01-09 15:29:33 +02003972 err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr,
3973 sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02003974
3975failed:
3976 hci_dev_unlock(hdev);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003977 return err;
3978}
3979
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003980static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003981 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03003982{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003983 struct mgmt_cp_block_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003984 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03003985 int err;
3986
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003987 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03003988
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003989 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg5d0846d2013-01-20 14:27:22 +02003990 return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
3991 MGMT_STATUS_INVALID_PARAMS,
3992 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003993
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003994 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03003995
Johan Hedbergdcc36c12014-07-09 12:59:13 +03003996 err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr,
3997 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03003998 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003999 status = MGMT_STATUS_FAILED;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004000 goto done;
4001 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004002
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004003 mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4004 sk);
4005 status = MGMT_STATUS_SUCCESS;
4006
4007done:
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004008 err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004009 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03004010
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004011 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03004012
4013 return err;
4014}
4015
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004016static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004017 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03004018{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004019 struct mgmt_cp_unblock_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004020 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03004021 int err;
4022
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004023 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03004024
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004025 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg5d0846d2013-01-20 14:27:22 +02004026 return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
4027 MGMT_STATUS_INVALID_PARAMS,
4028 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004029
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004030 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03004031
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004032 err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr,
4033 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004034 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004035 status = MGMT_STATUS_INVALID_PARAMS;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004036 goto done;
4037 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004038
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004039 mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4040 sk);
4041 status = MGMT_STATUS_SUCCESS;
4042
4043done:
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004044 err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004045 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03004046
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004047 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03004048
4049 return err;
4050}
4051
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004052static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
4053 u16 len)
4054{
4055 struct mgmt_cp_set_device_id *cp = data;
Johan Hedberg890ea892013-03-15 17:06:52 -05004056 struct hci_request req;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004057 int err;
Szymon Jancc72d4b82012-03-16 16:02:57 +01004058 __u16 source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004059
4060 BT_DBG("%s", hdev->name);
4061
Szymon Jancc72d4b82012-03-16 16:02:57 +01004062 source = __le16_to_cpu(cp->source);
4063
4064 if (source > 0x0002)
4065 return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
4066 MGMT_STATUS_INVALID_PARAMS);
4067
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004068 hci_dev_lock(hdev);
4069
Szymon Jancc72d4b82012-03-16 16:02:57 +01004070 hdev->devid_source = source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004071 hdev->devid_vendor = __le16_to_cpu(cp->vendor);
4072 hdev->devid_product = __le16_to_cpu(cp->product);
4073 hdev->devid_version = __le16_to_cpu(cp->version);
4074
4075 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0);
4076
Johan Hedberg890ea892013-03-15 17:06:52 -05004077 hci_req_init(&req, hdev);
4078 update_eir(&req);
4079 hci_req_run(&req, NULL);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004080
4081 hci_dev_unlock(hdev);
4082
4083 return err;
4084}
4085
Johan Hedberg4375f102013-09-25 13:26:10 +03004086static void set_advertising_complete(struct hci_dev *hdev, u8 status)
4087{
4088 struct cmd_lookup match = { NULL, hdev };
4089
4090 if (status) {
4091 u8 mgmt_err = mgmt_status(status);
4092
4093 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev,
4094 cmd_status_rsp, &mgmt_err);
4095 return;
4096 }
4097
Johan Hedbergc93bd152014-07-08 15:07:48 +03004098 if (test_bit(HCI_LE_ADV, &hdev->dev_flags))
4099 set_bit(HCI_ADVERTISING, &hdev->dev_flags);
4100 else
4101 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
4102
Johan Hedberg4375f102013-09-25 13:26:10 +03004103 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
4104 &match);
4105
4106 new_settings(hdev, match.sk);
4107
4108 if (match.sk)
4109 sock_put(match.sk);
4110}
4111
Marcel Holtmann21b51872013-10-10 09:47:53 -07004112static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
4113 u16 len)
Johan Hedberg4375f102013-09-25 13:26:10 +03004114{
4115 struct mgmt_mode *cp = data;
4116 struct pending_cmd *cmd;
4117 struct hci_request req;
Johan Hedberge6fe7982013-10-02 15:45:22 +03004118 u8 val, enabled, status;
Johan Hedberg4375f102013-09-25 13:26:10 +03004119 int err;
4120
4121 BT_DBG("request for %s", hdev->name);
4122
Johan Hedberge6fe7982013-10-02 15:45:22 +03004123 status = mgmt_le_support(hdev);
4124 if (status)
Johan Hedberg4375f102013-09-25 13:26:10 +03004125 return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
Johan Hedberge6fe7982013-10-02 15:45:22 +03004126 status);
Johan Hedberg4375f102013-09-25 13:26:10 +03004127
4128 if (cp->val != 0x00 && cp->val != 0x01)
4129 return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4130 MGMT_STATUS_INVALID_PARAMS);
4131
4132 hci_dev_lock(hdev);
4133
4134 val = !!cp->val;
Johan Hedbergf3d3444a2013-10-05 12:01:04 +02004135 enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags);
Johan Hedberg4375f102013-09-25 13:26:10 +03004136
Johan Hedbergf74ca9b2013-10-08 15:52:18 +02004137 /* The following conditions are ones which mean that we should
4138 * not do any HCI communication but directly send a mgmt
4139 * response to user space (after toggling the flag if
4140 * necessary).
4141 */
4142 if (!hdev_is_powered(hdev) || val == enabled ||
Johan Hedberge8bb6b92014-07-08 15:07:53 +03004143 hci_conn_num(hdev, LE_LINK) > 0 ||
4144 (test_bit(HCI_LE_SCAN, &hdev->dev_flags) &&
4145 hdev->le_scan_type == LE_SCAN_ACTIVE)) {
Johan Hedberg4375f102013-09-25 13:26:10 +03004146 bool changed = false;
4147
Johan Hedbergf3d3444a2013-10-05 12:01:04 +02004148 if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
4149 change_bit(HCI_ADVERTISING, &hdev->dev_flags);
Johan Hedberg4375f102013-09-25 13:26:10 +03004150 changed = true;
4151 }
4152
4153 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
4154 if (err < 0)
4155 goto unlock;
4156
4157 if (changed)
4158 err = new_settings(hdev, sk);
4159
4160 goto unlock;
4161 }
4162
4163 if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
4164 mgmt_pending_find(MGMT_OP_SET_LE, hdev)) {
4165 err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4166 MGMT_STATUS_BUSY);
4167 goto unlock;
4168 }
4169
4170 cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
4171 if (!cmd) {
4172 err = -ENOMEM;
4173 goto unlock;
4174 }
4175
4176 hci_req_init(&req, hdev);
4177
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07004178 if (val)
4179 enable_advertising(&req);
4180 else
4181 disable_advertising(&req);
Johan Hedberg4375f102013-09-25 13:26:10 +03004182
4183 err = hci_req_run(&req, set_advertising_complete);
4184 if (err < 0)
4185 mgmt_pending_remove(cmd);
4186
4187unlock:
4188 hci_dev_unlock(hdev);
4189 return err;
4190}
4191
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004192static int set_static_address(struct sock *sk, struct hci_dev *hdev,
4193 void *data, u16 len)
4194{
4195 struct mgmt_cp_set_static_address *cp = data;
4196 int err;
4197
4198 BT_DBG("%s", hdev->name);
4199
Marcel Holtmann62af4442013-10-02 22:10:32 -07004200 if (!lmp_le_capable(hdev))
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004201 return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
Marcel Holtmann62af4442013-10-02 22:10:32 -07004202 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004203
4204 if (hdev_is_powered(hdev))
4205 return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4206 MGMT_STATUS_REJECTED);
4207
4208 if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
4209 if (!bacmp(&cp->bdaddr, BDADDR_NONE))
4210 return cmd_status(sk, hdev->id,
4211 MGMT_OP_SET_STATIC_ADDRESS,
4212 MGMT_STATUS_INVALID_PARAMS);
4213
4214 /* Two most significant bits shall be set */
4215 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
4216 return cmd_status(sk, hdev->id,
4217 MGMT_OP_SET_STATIC_ADDRESS,
4218 MGMT_STATUS_INVALID_PARAMS);
4219 }
4220
4221 hci_dev_lock(hdev);
4222
4223 bacpy(&hdev->static_addr, &cp->bdaddr);
4224
4225 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0);
4226
4227 hci_dev_unlock(hdev);
4228
4229 return err;
4230}
4231
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004232static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
4233 void *data, u16 len)
4234{
4235 struct mgmt_cp_set_scan_params *cp = data;
4236 __u16 interval, window;
4237 int err;
4238
4239 BT_DBG("%s", hdev->name);
4240
4241 if (!lmp_le_capable(hdev))
4242 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4243 MGMT_STATUS_NOT_SUPPORTED);
4244
4245 interval = __le16_to_cpu(cp->interval);
4246
4247 if (interval < 0x0004 || interval > 0x4000)
4248 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4249 MGMT_STATUS_INVALID_PARAMS);
4250
4251 window = __le16_to_cpu(cp->window);
4252
4253 if (window < 0x0004 || window > 0x4000)
4254 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4255 MGMT_STATUS_INVALID_PARAMS);
4256
Marcel Holtmann899e1072013-10-14 09:55:32 -07004257 if (window > interval)
4258 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4259 MGMT_STATUS_INVALID_PARAMS);
4260
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004261 hci_dev_lock(hdev);
4262
4263 hdev->le_scan_interval = interval;
4264 hdev->le_scan_window = window;
4265
4266 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0);
4267
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03004268 /* If background scan is running, restart it so new parameters are
4269 * loaded.
4270 */
4271 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) &&
4272 hdev->discovery.state == DISCOVERY_STOPPED) {
4273 struct hci_request req;
4274
4275 hci_req_init(&req, hdev);
4276
4277 hci_req_add_le_scan_disable(&req);
4278 hci_req_add_le_passive_scan(&req);
4279
4280 hci_req_run(&req, NULL);
4281 }
4282
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004283 hci_dev_unlock(hdev);
4284
4285 return err;
4286}
4287
Johan Hedberg33e38b32013-03-15 17:07:05 -05004288static void fast_connectable_complete(struct hci_dev *hdev, u8 status)
4289{
4290 struct pending_cmd *cmd;
4291
4292 BT_DBG("status 0x%02x", status);
4293
4294 hci_dev_lock(hdev);
4295
4296 cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev);
4297 if (!cmd)
4298 goto unlock;
4299
4300 if (status) {
4301 cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4302 mgmt_status(status));
4303 } else {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004304 struct mgmt_mode *cp = cmd->param;
4305
4306 if (cp->val)
4307 set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags);
4308 else
4309 clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags);
4310
Johan Hedberg33e38b32013-03-15 17:07:05 -05004311 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
4312 new_settings(hdev, cmd->sk);
4313 }
4314
4315 mgmt_pending_remove(cmd);
4316
4317unlock:
4318 hci_dev_unlock(hdev);
4319}
4320
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004321static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004322 void *data, u16 len)
Antti Julkuf6422ec2011-06-22 13:11:56 +03004323{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004324 struct mgmt_mode *cp = data;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004325 struct pending_cmd *cmd;
4326 struct hci_request req;
Antti Julkuf6422ec2011-06-22 13:11:56 +03004327 int err;
4328
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004329 BT_DBG("%s", hdev->name);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004330
Johan Hedberg56f87902013-10-02 13:43:13 +03004331 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) ||
4332 hdev->hci_ver < BLUETOOTH_VER_1_2)
Johan Hedberg33c525c2012-10-24 21:11:58 +03004333 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4334 MGMT_STATUS_NOT_SUPPORTED);
4335
Johan Hedberga7e80f22013-01-09 16:05:19 +02004336 if (cp->val != 0x00 && cp->val != 0x01)
4337 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4338 MGMT_STATUS_INVALID_PARAMS);
4339
Johan Hedberg5400c042012-02-21 16:40:33 +02004340 if (!hdev_is_powered(hdev))
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004341 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004342 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5400c042012-02-21 16:40:33 +02004343
4344 if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004345 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004346 MGMT_STATUS_REJECTED);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004347
4348 hci_dev_lock(hdev);
4349
Johan Hedberg05cbf292013-03-15 17:07:07 -05004350 if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) {
4351 err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4352 MGMT_STATUS_BUSY);
4353 goto unlock;
4354 }
4355
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004356 if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) {
4357 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4358 hdev);
4359 goto unlock;
4360 }
4361
Johan Hedberg33e38b32013-03-15 17:07:05 -05004362 cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev,
4363 data, len);
4364 if (!cmd) {
4365 err = -ENOMEM;
4366 goto unlock;
4367 }
4368
4369 hci_req_init(&req, hdev);
4370
Johan Hedberg406d7802013-03-15 17:07:09 -05004371 write_fast_connectable(&req, cp->val);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004372
4373 err = hci_req_run(&req, fast_connectable_complete);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004374 if (err < 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004375 err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004376 MGMT_STATUS_FAILED);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004377 mgmt_pending_remove(cmd);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004378 }
4379
Johan Hedberg33e38b32013-03-15 17:07:05 -05004380unlock:
Antti Julkuf6422ec2011-06-22 13:11:56 +03004381 hci_dev_unlock(hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004382
Antti Julkuf6422ec2011-06-22 13:11:56 +03004383 return err;
4384}
4385
Johan Hedberg0663ca22013-10-02 13:43:14 +03004386static void set_bredr_complete(struct hci_dev *hdev, u8 status)
4387{
4388 struct pending_cmd *cmd;
4389
4390 BT_DBG("status 0x%02x", status);
4391
4392 hci_dev_lock(hdev);
4393
4394 cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev);
4395 if (!cmd)
4396 goto unlock;
4397
4398 if (status) {
4399 u8 mgmt_err = mgmt_status(status);
4400
4401 /* We need to restore the flag if related HCI commands
4402 * failed.
4403 */
4404 clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
4405
4406 cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
4407 } else {
4408 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
4409 new_settings(hdev, cmd->sk);
4410 }
4411
4412 mgmt_pending_remove(cmd);
4413
4414unlock:
4415 hci_dev_unlock(hdev);
4416}
4417
4418static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
4419{
4420 struct mgmt_mode *cp = data;
4421 struct pending_cmd *cmd;
4422 struct hci_request req;
4423 int err;
4424
4425 BT_DBG("request for %s", hdev->name);
4426
4427 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
4428 return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4429 MGMT_STATUS_NOT_SUPPORTED);
4430
4431 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
4432 return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4433 MGMT_STATUS_REJECTED);
4434
4435 if (cp->val != 0x00 && cp->val != 0x01)
4436 return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4437 MGMT_STATUS_INVALID_PARAMS);
4438
4439 hci_dev_lock(hdev);
4440
4441 if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
4442 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4443 goto unlock;
4444 }
4445
4446 if (!hdev_is_powered(hdev)) {
4447 if (!cp->val) {
Johan Hedberg0663ca22013-10-02 13:43:14 +03004448 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
4449 clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
4450 clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
4451 clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags);
4452 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
4453 }
4454
4455 change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
4456
4457 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4458 if (err < 0)
4459 goto unlock;
4460
4461 err = new_settings(hdev, sk);
4462 goto unlock;
4463 }
4464
4465 /* Reject disabling when powered on */
4466 if (!cp->val) {
4467 err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4468 MGMT_STATUS_REJECTED);
4469 goto unlock;
4470 }
4471
4472 if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) {
4473 err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4474 MGMT_STATUS_BUSY);
4475 goto unlock;
4476 }
4477
4478 cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len);
4479 if (!cmd) {
4480 err = -ENOMEM;
4481 goto unlock;
4482 }
4483
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07004484 /* We need to flip the bit already here so that update_adv_data
Johan Hedberg0663ca22013-10-02 13:43:14 +03004485 * generates the correct flags.
4486 */
4487 set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
4488
4489 hci_req_init(&req, hdev);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004490
Johan Hedberg432df052014-08-01 11:13:31 +03004491 write_fast_connectable(&req, false);
4492 hci_update_page_scan(hdev, &req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004493
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07004494 /* Since only the advertising data flags will change, there
4495 * is no need to update the scan response data.
4496 */
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07004497 update_adv_data(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004498
Johan Hedberg0663ca22013-10-02 13:43:14 +03004499 err = hci_req_run(&req, set_bredr_complete);
4500 if (err < 0)
4501 mgmt_pending_remove(cmd);
4502
4503unlock:
4504 hci_dev_unlock(hdev);
4505 return err;
4506}
4507
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004508static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
4509 void *data, u16 len)
4510{
4511 struct mgmt_mode *cp = data;
4512 struct pending_cmd *cmd;
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004513 u8 val, status;
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004514 int err;
4515
4516 BT_DBG("request for %s", hdev->name);
4517
4518 status = mgmt_bredr_support(hdev);
4519 if (status)
4520 return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4521 status);
4522
Marcel Holtmann5afeac12014-01-10 02:07:27 -08004523 if (!lmp_sc_capable(hdev) &&
Marcel Holtmann111902f2014-06-21 04:53:17 +02004524 !test_bit(HCI_FORCE_SC, &hdev->dbg_flags))
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004525 return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4526 MGMT_STATUS_NOT_SUPPORTED);
4527
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004528 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004529 return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4530 MGMT_STATUS_INVALID_PARAMS);
4531
4532 hci_dev_lock(hdev);
4533
4534 if (!hdev_is_powered(hdev)) {
4535 bool changed;
4536
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004537 if (cp->val) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004538 changed = !test_and_set_bit(HCI_SC_ENABLED,
4539 &hdev->dev_flags);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004540 if (cp->val == 0x02)
4541 set_bit(HCI_SC_ONLY, &hdev->dev_flags);
4542 else
4543 clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
4544 } else {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004545 changed = test_and_clear_bit(HCI_SC_ENABLED,
4546 &hdev->dev_flags);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004547 clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
4548 }
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004549
4550 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
4551 if (err < 0)
4552 goto failed;
4553
4554 if (changed)
4555 err = new_settings(hdev, sk);
4556
4557 goto failed;
4558 }
4559
4560 if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) {
4561 err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4562 MGMT_STATUS_BUSY);
4563 goto failed;
4564 }
4565
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004566 val = !!cp->val;
4567
4568 if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) &&
4569 (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004570 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
4571 goto failed;
4572 }
4573
4574 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len);
4575 if (!cmd) {
4576 err = -ENOMEM;
4577 goto failed;
4578 }
4579
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004580 err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004581 if (err < 0) {
4582 mgmt_pending_remove(cmd);
4583 goto failed;
4584 }
4585
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004586 if (cp->val == 0x02)
4587 set_bit(HCI_SC_ONLY, &hdev->dev_flags);
4588 else
4589 clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
4590
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004591failed:
4592 hci_dev_unlock(hdev);
4593 return err;
4594}
4595
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004596static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
4597 void *data, u16 len)
4598{
4599 struct mgmt_mode *cp = data;
Johan Hedbergb97109792014-06-24 14:00:28 +03004600 bool changed, use_changed;
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004601 int err;
4602
4603 BT_DBG("request for %s", hdev->name);
4604
Johan Hedbergb97109792014-06-24 14:00:28 +03004605 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004606 return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
4607 MGMT_STATUS_INVALID_PARAMS);
4608
4609 hci_dev_lock(hdev);
4610
4611 if (cp->val)
Johan Hedberg0663b292014-06-24 13:15:50 +03004612 changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS,
4613 &hdev->dev_flags);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004614 else
Johan Hedberg0663b292014-06-24 13:15:50 +03004615 changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS,
4616 &hdev->dev_flags);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004617
Johan Hedbergb97109792014-06-24 14:00:28 +03004618 if (cp->val == 0x02)
4619 use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS,
4620 &hdev->dev_flags);
4621 else
4622 use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS,
4623 &hdev->dev_flags);
4624
4625 if (hdev_is_powered(hdev) && use_changed &&
4626 test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
4627 u8 mode = (cp->val == 0x02) ? 0x01 : 0x00;
4628 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
4629 sizeof(mode), &mode);
4630 }
4631
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004632 err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev);
4633 if (err < 0)
4634 goto unlock;
4635
4636 if (changed)
4637 err = new_settings(hdev, sk);
4638
4639unlock:
4640 hci_dev_unlock(hdev);
4641 return err;
4642}
4643
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004644static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
4645 u16 len)
4646{
4647 struct mgmt_cp_set_privacy *cp = cp_data;
4648 bool changed;
4649 int err;
4650
4651 BT_DBG("request for %s", hdev->name);
4652
4653 if (!lmp_le_capable(hdev))
4654 return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
4655 MGMT_STATUS_NOT_SUPPORTED);
4656
4657 if (cp->privacy != 0x00 && cp->privacy != 0x01)
4658 return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
4659 MGMT_STATUS_INVALID_PARAMS);
4660
4661 if (hdev_is_powered(hdev))
4662 return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
4663 MGMT_STATUS_REJECTED);
4664
4665 hci_dev_lock(hdev);
4666
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02004667 /* If user space supports this command it is also expected to
4668 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
4669 */
4670 set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags);
4671
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004672 if (cp->privacy) {
4673 changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags);
4674 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
4675 set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
4676 } else {
4677 changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags);
4678 memset(hdev->irk, 0, sizeof(hdev->irk));
4679 clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
4680 }
4681
4682 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
4683 if (err < 0)
4684 goto unlock;
4685
4686 if (changed)
4687 err = new_settings(hdev, sk);
4688
4689unlock:
4690 hci_dev_unlock(hdev);
4691 return err;
4692}
4693
Johan Hedberg41edf162014-02-18 10:19:35 +02004694static bool irk_is_valid(struct mgmt_irk_info *irk)
4695{
4696 switch (irk->addr.type) {
4697 case BDADDR_LE_PUBLIC:
4698 return true;
4699
4700 case BDADDR_LE_RANDOM:
4701 /* Two most significant bits shall be set */
4702 if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0)
4703 return false;
4704 return true;
4705 }
4706
4707 return false;
4708}
4709
4710static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
4711 u16 len)
4712{
4713 struct mgmt_cp_load_irks *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03004714 const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) /
4715 sizeof(struct mgmt_irk_info));
Johan Hedberg41edf162014-02-18 10:19:35 +02004716 u16 irk_count, expected_len;
4717 int i, err;
4718
4719 BT_DBG("request for %s", hdev->name);
4720
4721 if (!lmp_le_capable(hdev))
4722 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
4723 MGMT_STATUS_NOT_SUPPORTED);
4724
4725 irk_count = __le16_to_cpu(cp->irk_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03004726 if (irk_count > max_irk_count) {
4727 BT_ERR("load_irks: too big irk_count value %u", irk_count);
4728 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
4729 MGMT_STATUS_INVALID_PARAMS);
4730 }
Johan Hedberg41edf162014-02-18 10:19:35 +02004731
4732 expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info);
4733 if (expected_len != len) {
4734 BT_ERR("load_irks: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02004735 expected_len, len);
Johan Hedberg41edf162014-02-18 10:19:35 +02004736 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
4737 MGMT_STATUS_INVALID_PARAMS);
4738 }
4739
4740 BT_DBG("%s irk_count %u", hdev->name, irk_count);
4741
4742 for (i = 0; i < irk_count; i++) {
4743 struct mgmt_irk_info *key = &cp->irks[i];
4744
4745 if (!irk_is_valid(key))
4746 return cmd_status(sk, hdev->id,
4747 MGMT_OP_LOAD_IRKS,
4748 MGMT_STATUS_INVALID_PARAMS);
4749 }
4750
4751 hci_dev_lock(hdev);
4752
4753 hci_smp_irks_clear(hdev);
4754
4755 for (i = 0; i < irk_count; i++) {
4756 struct mgmt_irk_info *irk = &cp->irks[i];
4757 u8 addr_type;
4758
4759 if (irk->addr.type == BDADDR_LE_PUBLIC)
4760 addr_type = ADDR_LE_DEV_PUBLIC;
4761 else
4762 addr_type = ADDR_LE_DEV_RANDOM;
4763
4764 hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val,
4765 BDADDR_ANY);
4766 }
4767
4768 set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags);
4769
4770 err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
4771
4772 hci_dev_unlock(hdev);
4773
4774 return err;
4775}
4776
Johan Hedberg3f706b72013-01-20 14:27:16 +02004777static bool ltk_is_valid(struct mgmt_ltk_info *key)
4778{
4779 if (key->master != 0x00 && key->master != 0x01)
4780 return false;
Marcel Holtmann490cb0b2014-02-16 12:59:05 -08004781
4782 switch (key->addr.type) {
4783 case BDADDR_LE_PUBLIC:
4784 return true;
4785
4786 case BDADDR_LE_RANDOM:
4787 /* Two most significant bits shall be set */
4788 if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0)
4789 return false;
4790 return true;
4791 }
4792
4793 return false;
Johan Hedberg3f706b72013-01-20 14:27:16 +02004794}
4795
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004796static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004797 void *cp_data, u16 len)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004798{
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004799 struct mgmt_cp_load_long_term_keys *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03004800 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
4801 sizeof(struct mgmt_ltk_info));
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004802 u16 key_count, expected_len;
Johan Hedberg715a5bf2013-01-09 15:29:34 +02004803 int i, err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004804
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07004805 BT_DBG("request for %s", hdev->name);
4806
4807 if (!lmp_le_capable(hdev))
4808 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
4809 MGMT_STATUS_NOT_SUPPORTED);
4810
Marcel Holtmann1f350c82012-03-12 20:31:08 -07004811 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03004812 if (key_count > max_key_count) {
4813 BT_ERR("load_ltks: too big key_count value %u", key_count);
4814 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
4815 MGMT_STATUS_INVALID_PARAMS);
4816 }
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004817
4818 expected_len = sizeof(*cp) + key_count *
4819 sizeof(struct mgmt_ltk_info);
4820 if (expected_len != len) {
4821 BT_ERR("load_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02004822 expected_len, len);
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004823 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
Johan Hedberge57e6192013-01-20 14:27:14 +02004824 MGMT_STATUS_INVALID_PARAMS);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004825 }
4826
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004827 BT_DBG("%s key_count %u", hdev->name, key_count);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004828
Johan Hedberg54ad6d82013-01-20 14:27:15 +02004829 for (i = 0; i < key_count; i++) {
4830 struct mgmt_ltk_info *key = &cp->keys[i];
4831
Johan Hedberg3f706b72013-01-20 14:27:16 +02004832 if (!ltk_is_valid(key))
Johan Hedberg54ad6d82013-01-20 14:27:15 +02004833 return cmd_status(sk, hdev->id,
4834 MGMT_OP_LOAD_LONG_TERM_KEYS,
4835 MGMT_STATUS_INVALID_PARAMS);
4836 }
4837
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004838 hci_dev_lock(hdev);
4839
4840 hci_smp_ltks_clear(hdev);
4841
4842 for (i = 0; i < key_count; i++) {
4843 struct mgmt_ltk_info *key = &cp->keys[i];
Johan Hedbergd7b25452014-05-23 13:19:53 +03004844 u8 type, addr_type, authenticated;
Marcel Holtmann79d95a12013-10-13 03:57:38 -07004845
4846 if (key->addr.type == BDADDR_LE_PUBLIC)
4847 addr_type = ADDR_LE_DEV_PUBLIC;
4848 else
4849 addr_type = ADDR_LE_DEV_RANDOM;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004850
4851 if (key->master)
Johan Hedberg2ceba532014-06-16 19:25:16 +03004852 type = SMP_LTK;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004853 else
Johan Hedberg2ceba532014-06-16 19:25:16 +03004854 type = SMP_LTK_SLAVE;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004855
Johan Hedberg61b43352014-05-29 19:36:53 +03004856 switch (key->type) {
4857 case MGMT_LTK_UNAUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03004858 authenticated = 0x00;
Johan Hedberg61b43352014-05-29 19:36:53 +03004859 break;
4860 case MGMT_LTK_AUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03004861 authenticated = 0x01;
Johan Hedberg61b43352014-05-29 19:36:53 +03004862 break;
4863 default:
4864 continue;
4865 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03004866
Johan Hedberg35d70272014-02-19 14:57:47 +02004867 hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type,
Johan Hedbergd7b25452014-05-23 13:19:53 +03004868 authenticated, key->val, key->enc_size, key->ediv,
Johan Hedberg35d70272014-02-19 14:57:47 +02004869 key->rand);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004870 }
4871
Johan Hedberg715a5bf2013-01-09 15:29:34 +02004872 err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
4873 NULL, 0);
4874
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004875 hci_dev_unlock(hdev);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004876
Johan Hedberg715a5bf2013-01-09 15:29:34 +02004877 return err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004878}
4879
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004880struct cmd_conn_lookup {
4881 struct hci_conn *conn;
4882 bool valid_tx_power;
4883 u8 mgmt_status;
4884};
4885
4886static void get_conn_info_complete(struct pending_cmd *cmd, void *data)
4887{
4888 struct cmd_conn_lookup *match = data;
4889 struct mgmt_cp_get_conn_info *cp;
4890 struct mgmt_rp_get_conn_info rp;
4891 struct hci_conn *conn = cmd->user_data;
4892
4893 if (conn != match->conn)
4894 return;
4895
4896 cp = (struct mgmt_cp_get_conn_info *) cmd->param;
4897
4898 memset(&rp, 0, sizeof(rp));
4899 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
4900 rp.addr.type = cp->addr.type;
4901
4902 if (!match->mgmt_status) {
4903 rp.rssi = conn->rssi;
4904
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02004905 if (match->valid_tx_power) {
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004906 rp.tx_power = conn->tx_power;
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02004907 rp.max_tx_power = conn->max_tx_power;
4908 } else {
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004909 rp.tx_power = HCI_TX_POWER_INVALID;
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02004910 rp.max_tx_power = HCI_TX_POWER_INVALID;
4911 }
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004912 }
4913
4914 cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO,
4915 match->mgmt_status, &rp, sizeof(rp));
4916
4917 hci_conn_drop(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03004918 hci_conn_put(conn);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004919
4920 mgmt_pending_remove(cmd);
4921}
4922
4923static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status)
4924{
4925 struct hci_cp_read_rssi *cp;
4926 struct hci_conn *conn;
4927 struct cmd_conn_lookup match;
4928 u16 handle;
4929
4930 BT_DBG("status 0x%02x", status);
4931
4932 hci_dev_lock(hdev);
4933
4934 /* TX power data is valid in case request completed successfully,
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02004935 * otherwise we assume it's not valid. At the moment we assume that
4936 * either both or none of current and max values are valid to keep code
4937 * simple.
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02004938 */
4939 match.valid_tx_power = !status;
4940
4941 /* Commands sent in request are either Read RSSI or Read Transmit Power
4942 * Level so we check which one was last sent to retrieve connection
4943 * handle. Both commands have handle as first parameter so it's safe to
4944 * cast data on the same command struct.
4945 *
4946 * First command sent is always Read RSSI and we fail only if it fails.
4947 * In other case we simply override error to indicate success as we
4948 * already remembered if TX power value is actually valid.
4949 */
4950 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI);
4951 if (!cp) {
4952 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
4953 status = 0;
4954 }
4955
4956 if (!cp) {
4957 BT_ERR("invalid sent_cmd in response");
4958 goto unlock;
4959 }
4960
4961 handle = __le16_to_cpu(cp->handle);
4962 conn = hci_conn_hash_lookup_handle(hdev, handle);
4963 if (!conn) {
4964 BT_ERR("unknown handle (%d) in response", handle);
4965 goto unlock;
4966 }
4967
4968 match.conn = conn;
4969 match.mgmt_status = mgmt_status(status);
4970
4971 /* Cache refresh is complete, now reply for mgmt request for given
4972 * connection only.
4973 */
4974 mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev,
4975 get_conn_info_complete, &match);
4976
4977unlock:
4978 hci_dev_unlock(hdev);
4979}
4980
4981static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
4982 u16 len)
4983{
4984 struct mgmt_cp_get_conn_info *cp = data;
4985 struct mgmt_rp_get_conn_info rp;
4986 struct hci_conn *conn;
4987 unsigned long conn_info_age;
4988 int err = 0;
4989
4990 BT_DBG("%s", hdev->name);
4991
4992 memset(&rp, 0, sizeof(rp));
4993 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
4994 rp.addr.type = cp->addr.type;
4995
4996 if (!bdaddr_type_is_valid(cp->addr.type))
4997 return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
4998 MGMT_STATUS_INVALID_PARAMS,
4999 &rp, sizeof(rp));
5000
5001 hci_dev_lock(hdev);
5002
5003 if (!hdev_is_powered(hdev)) {
5004 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5005 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp));
5006 goto unlock;
5007 }
5008
5009 if (cp->addr.type == BDADDR_BREDR)
5010 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5011 &cp->addr.bdaddr);
5012 else
5013 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
5014
5015 if (!conn || conn->state != BT_CONNECTED) {
5016 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5017 MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp));
5018 goto unlock;
5019 }
5020
5021 /* To avoid client trying to guess when to poll again for information we
5022 * calculate conn info age as random value between min/max set in hdev.
5023 */
5024 conn_info_age = hdev->conn_info_min_age +
5025 prandom_u32_max(hdev->conn_info_max_age -
5026 hdev->conn_info_min_age);
5027
5028 /* Query controller to refresh cached values if they are too old or were
5029 * never read.
5030 */
Andrzej Kaczmarekf4e2dd52014-05-16 16:48:57 +02005031 if (time_after(jiffies, conn->conn_info_timestamp +
5032 msecs_to_jiffies(conn_info_age)) ||
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005033 !conn->conn_info_timestamp) {
5034 struct hci_request req;
5035 struct hci_cp_read_tx_power req_txp_cp;
5036 struct hci_cp_read_rssi req_rssi_cp;
5037 struct pending_cmd *cmd;
5038
5039 hci_req_init(&req, hdev);
5040 req_rssi_cp.handle = cpu_to_le16(conn->handle);
5041 hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp),
5042 &req_rssi_cp);
5043
Andrzej Kaczmarekf7faab02014-05-14 13:43:04 +02005044 /* For LE links TX power does not change thus we don't need to
5045 * query for it once value is known.
5046 */
5047 if (!bdaddr_type_is_le(cp->addr.type) ||
5048 conn->tx_power == HCI_TX_POWER_INVALID) {
5049 req_txp_cp.handle = cpu_to_le16(conn->handle);
5050 req_txp_cp.type = 0x00;
5051 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5052 sizeof(req_txp_cp), &req_txp_cp);
5053 }
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005054
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005055 /* Max TX power needs to be read only once per connection */
5056 if (conn->max_tx_power == HCI_TX_POWER_INVALID) {
5057 req_txp_cp.handle = cpu_to_le16(conn->handle);
5058 req_txp_cp.type = 0x01;
5059 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5060 sizeof(req_txp_cp), &req_txp_cp);
5061 }
5062
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005063 err = hci_req_run(&req, conn_info_refresh_complete);
5064 if (err < 0)
5065 goto unlock;
5066
5067 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev,
5068 data, len);
5069 if (!cmd) {
5070 err = -ENOMEM;
5071 goto unlock;
5072 }
5073
5074 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005075 cmd->user_data = hci_conn_get(conn);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005076
5077 conn->conn_info_timestamp = jiffies;
5078 } else {
5079 /* Cache is valid, just reply with values cached in hci_conn */
5080 rp.rssi = conn->rssi;
5081 rp.tx_power = conn->tx_power;
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005082 rp.max_tx_power = conn->max_tx_power;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005083
5084 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5085 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
5086 }
5087
5088unlock:
5089 hci_dev_unlock(hdev);
5090 return err;
5091}
5092
Johan Hedberg95868422014-06-28 17:54:07 +03005093static void get_clock_info_complete(struct hci_dev *hdev, u8 status)
5094{
5095 struct mgmt_cp_get_clock_info *cp;
5096 struct mgmt_rp_get_clock_info rp;
5097 struct hci_cp_read_clock *hci_cp;
5098 struct pending_cmd *cmd;
5099 struct hci_conn *conn;
5100
5101 BT_DBG("%s status %u", hdev->name, status);
5102
5103 hci_dev_lock(hdev);
5104
5105 hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
5106 if (!hci_cp)
5107 goto unlock;
5108
5109 if (hci_cp->which) {
5110 u16 handle = __le16_to_cpu(hci_cp->handle);
5111 conn = hci_conn_hash_lookup_handle(hdev, handle);
5112 } else {
5113 conn = NULL;
5114 }
5115
5116 cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn);
5117 if (!cmd)
5118 goto unlock;
5119
5120 cp = cmd->param;
5121
5122 memset(&rp, 0, sizeof(rp));
5123 memcpy(&rp.addr, &cp->addr, sizeof(rp.addr));
5124
5125 if (status)
5126 goto send_rsp;
5127
5128 rp.local_clock = cpu_to_le32(hdev->clock);
5129
5130 if (conn) {
5131 rp.piconet_clock = cpu_to_le32(conn->clock);
5132 rp.accuracy = cpu_to_le16(conn->clock_accuracy);
5133 }
5134
5135send_rsp:
5136 cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status),
5137 &rp, sizeof(rp));
5138 mgmt_pending_remove(cmd);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005139 if (conn) {
Johan Hedberg95868422014-06-28 17:54:07 +03005140 hci_conn_drop(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005141 hci_conn_put(conn);
5142 }
Johan Hedberg95868422014-06-28 17:54:07 +03005143
5144unlock:
5145 hci_dev_unlock(hdev);
5146}
5147
5148static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
5149 u16 len)
5150{
5151 struct mgmt_cp_get_clock_info *cp = data;
5152 struct mgmt_rp_get_clock_info rp;
5153 struct hci_cp_read_clock hci_cp;
5154 struct pending_cmd *cmd;
5155 struct hci_request req;
5156 struct hci_conn *conn;
5157 int err;
5158
5159 BT_DBG("%s", hdev->name);
5160
5161 memset(&rp, 0, sizeof(rp));
5162 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5163 rp.addr.type = cp->addr.type;
5164
5165 if (cp->addr.type != BDADDR_BREDR)
5166 return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5167 MGMT_STATUS_INVALID_PARAMS,
5168 &rp, sizeof(rp));
5169
5170 hci_dev_lock(hdev);
5171
5172 if (!hdev_is_powered(hdev)) {
5173 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5174 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp));
5175 goto unlock;
5176 }
5177
5178 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
5179 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5180 &cp->addr.bdaddr);
5181 if (!conn || conn->state != BT_CONNECTED) {
5182 err = cmd_complete(sk, hdev->id,
5183 MGMT_OP_GET_CLOCK_INFO,
5184 MGMT_STATUS_NOT_CONNECTED,
5185 &rp, sizeof(rp));
5186 goto unlock;
5187 }
5188 } else {
5189 conn = NULL;
5190 }
5191
5192 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len);
5193 if (!cmd) {
5194 err = -ENOMEM;
5195 goto unlock;
5196 }
5197
5198 hci_req_init(&req, hdev);
5199
5200 memset(&hci_cp, 0, sizeof(hci_cp));
5201 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5202
5203 if (conn) {
5204 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005205 cmd->user_data = hci_conn_get(conn);
Johan Hedberg95868422014-06-28 17:54:07 +03005206
5207 hci_cp.handle = cpu_to_le16(conn->handle);
5208 hci_cp.which = 0x01; /* Piconet clock */
5209 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5210 }
5211
5212 err = hci_req_run(&req, get_clock_info_complete);
5213 if (err < 0)
5214 mgmt_pending_remove(cmd);
5215
5216unlock:
5217 hci_dev_unlock(hdev);
5218 return err;
5219}
5220
Marcel Holtmann8afef092014-06-29 22:28:34 +02005221static void device_added(struct sock *sk, struct hci_dev *hdev,
5222 bdaddr_t *bdaddr, u8 type, u8 action)
5223{
5224 struct mgmt_ev_device_added ev;
5225
5226 bacpy(&ev.addr.bdaddr, bdaddr);
5227 ev.addr.type = type;
5228 ev.action = action;
5229
5230 mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk);
5231}
5232
Marcel Holtmann2faade52014-06-29 19:44:03 +02005233static int add_device(struct sock *sk, struct hci_dev *hdev,
5234 void *data, u16 len)
5235{
5236 struct mgmt_cp_add_device *cp = data;
5237 u8 auto_conn, addr_type;
5238 int err;
5239
5240 BT_DBG("%s", hdev->name);
5241
Johan Hedberg66593582014-07-09 12:59:14 +03005242 if (!bdaddr_type_is_valid(cp->addr.type) ||
Marcel Holtmann2faade52014-06-29 19:44:03 +02005243 !bacmp(&cp->addr.bdaddr, BDADDR_ANY))
5244 return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5245 MGMT_STATUS_INVALID_PARAMS,
5246 &cp->addr, sizeof(cp->addr));
5247
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005248 if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02)
Marcel Holtmann2faade52014-06-29 19:44:03 +02005249 return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5250 MGMT_STATUS_INVALID_PARAMS,
5251 &cp->addr, sizeof(cp->addr));
5252
5253 hci_dev_lock(hdev);
5254
Johan Hedberg66593582014-07-09 12:59:14 +03005255 if (cp->addr.type == BDADDR_BREDR) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005256 /* Only incoming connections action is supported for now */
Johan Hedberg66593582014-07-09 12:59:14 +03005257 if (cp->action != 0x01) {
5258 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5259 MGMT_STATUS_INVALID_PARAMS,
5260 &cp->addr, sizeof(cp->addr));
5261 goto unlock;
5262 }
5263
5264 err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr,
5265 cp->addr.type);
5266 if (err)
5267 goto unlock;
Johan Hedberga3974072014-07-09 12:59:15 +03005268
Johan Hedberg432df052014-08-01 11:13:31 +03005269 hci_update_page_scan(hdev, NULL);
Johan Hedberga3974072014-07-09 12:59:15 +03005270
Johan Hedberg66593582014-07-09 12:59:14 +03005271 goto added;
5272 }
5273
Marcel Holtmann2faade52014-06-29 19:44:03 +02005274 if (cp->addr.type == BDADDR_LE_PUBLIC)
5275 addr_type = ADDR_LE_DEV_PUBLIC;
5276 else
5277 addr_type = ADDR_LE_DEV_RANDOM;
5278
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005279 if (cp->action == 0x02)
Marcel Holtmann2faade52014-06-29 19:44:03 +02005280 auto_conn = HCI_AUTO_CONN_ALWAYS;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005281 else if (cp->action == 0x01)
5282 auto_conn = HCI_AUTO_CONN_DIRECT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005283 else
Johan Hedberga3451d22014-07-02 17:37:27 +03005284 auto_conn = HCI_AUTO_CONN_REPORT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005285
Marcel Holtmannbf5b3c82014-06-30 12:34:39 +02005286 /* If the connection parameters don't exist for this device,
5287 * they will be created and configured with defaults.
5288 */
Marcel Holtmannd06b50c2014-07-01 12:11:06 +02005289 if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type,
5290 auto_conn) < 0) {
Marcel Holtmann2faade52014-06-29 19:44:03 +02005291 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5292 MGMT_STATUS_FAILED,
5293 &cp->addr, sizeof(cp->addr));
5294 goto unlock;
5295 }
5296
Johan Hedberg66593582014-07-09 12:59:14 +03005297added:
Marcel Holtmann8afef092014-06-29 22:28:34 +02005298 device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
5299
Marcel Holtmann2faade52014-06-29 19:44:03 +02005300 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5301 MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr));
5302
5303unlock:
5304 hci_dev_unlock(hdev);
5305 return err;
5306}
5307
Marcel Holtmann8afef092014-06-29 22:28:34 +02005308static void device_removed(struct sock *sk, struct hci_dev *hdev,
5309 bdaddr_t *bdaddr, u8 type)
5310{
5311 struct mgmt_ev_device_removed ev;
5312
5313 bacpy(&ev.addr.bdaddr, bdaddr);
5314 ev.addr.type = type;
5315
5316 mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk);
5317}
5318
Marcel Holtmann2faade52014-06-29 19:44:03 +02005319static int remove_device(struct sock *sk, struct hci_dev *hdev,
5320 void *data, u16 len)
5321{
5322 struct mgmt_cp_remove_device *cp = data;
5323 int err;
5324
5325 BT_DBG("%s", hdev->name);
5326
5327 hci_dev_lock(hdev);
5328
5329 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
Johan Hedbergc71593d2014-07-02 17:37:28 +03005330 struct hci_conn_params *params;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005331 u8 addr_type;
5332
Johan Hedberg66593582014-07-09 12:59:14 +03005333 if (!bdaddr_type_is_valid(cp->addr.type)) {
Marcel Holtmann2faade52014-06-29 19:44:03 +02005334 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE,
5335 MGMT_STATUS_INVALID_PARAMS,
5336 &cp->addr, sizeof(cp->addr));
5337 goto unlock;
5338 }
5339
Johan Hedberg66593582014-07-09 12:59:14 +03005340 if (cp->addr.type == BDADDR_BREDR) {
5341 err = hci_bdaddr_list_del(&hdev->whitelist,
5342 &cp->addr.bdaddr,
5343 cp->addr.type);
5344 if (err) {
5345 err = cmd_complete(sk, hdev->id,
5346 MGMT_OP_REMOVE_DEVICE,
5347 MGMT_STATUS_INVALID_PARAMS,
5348 &cp->addr, sizeof(cp->addr));
5349 goto unlock;
5350 }
5351
Johan Hedberg432df052014-08-01 11:13:31 +03005352 hci_update_page_scan(hdev, NULL);
Johan Hedberga3974072014-07-09 12:59:15 +03005353
Johan Hedberg66593582014-07-09 12:59:14 +03005354 device_removed(sk, hdev, &cp->addr.bdaddr,
5355 cp->addr.type);
5356 goto complete;
5357 }
5358
Marcel Holtmann2faade52014-06-29 19:44:03 +02005359 if (cp->addr.type == BDADDR_LE_PUBLIC)
5360 addr_type = ADDR_LE_DEV_PUBLIC;
5361 else
5362 addr_type = ADDR_LE_DEV_RANDOM;
5363
Johan Hedbergc71593d2014-07-02 17:37:28 +03005364 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
5365 addr_type);
5366 if (!params) {
5367 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE,
5368 MGMT_STATUS_INVALID_PARAMS,
5369 &cp->addr, sizeof(cp->addr));
5370 goto unlock;
5371 }
5372
5373 if (params->auto_connect == HCI_AUTO_CONN_DISABLED) {
5374 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE,
5375 MGMT_STATUS_INVALID_PARAMS,
5376 &cp->addr, sizeof(cp->addr));
5377 goto unlock;
5378 }
5379
Johan Hedbergd1dbf122014-07-04 16:17:23 +03005380 list_del(&params->action);
Johan Hedbergc71593d2014-07-02 17:37:28 +03005381 list_del(&params->list);
5382 kfree(params);
Johan Hedberg95305ba2014-07-04 12:37:21 +03005383 hci_update_background_scan(hdev);
Marcel Holtmann8afef092014-06-29 22:28:34 +02005384
5385 device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005386 } else {
Johan Hedberg19de0822014-07-06 13:06:51 +03005387 struct hci_conn_params *p, *tmp;
Johan Hedberg66593582014-07-09 12:59:14 +03005388 struct bdaddr_list *b, *btmp;
Johan Hedberg19de0822014-07-06 13:06:51 +03005389
Marcel Holtmann2faade52014-06-29 19:44:03 +02005390 if (cp->addr.type) {
5391 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE,
5392 MGMT_STATUS_INVALID_PARAMS,
5393 &cp->addr, sizeof(cp->addr));
5394 goto unlock;
5395 }
5396
Johan Hedberg66593582014-07-09 12:59:14 +03005397 list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) {
5398 device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type);
5399 list_del(&b->list);
5400 kfree(b);
5401 }
5402
Johan Hedberg432df052014-08-01 11:13:31 +03005403 hci_update_page_scan(hdev, NULL);
Johan Hedberga3974072014-07-09 12:59:15 +03005404
Johan Hedberg19de0822014-07-06 13:06:51 +03005405 list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) {
5406 if (p->auto_connect == HCI_AUTO_CONN_DISABLED)
5407 continue;
5408 device_removed(sk, hdev, &p->addr, p->addr_type);
5409 list_del(&p->action);
5410 list_del(&p->list);
5411 kfree(p);
5412 }
5413
5414 BT_DBG("All LE connection parameters were removed");
5415
5416 hci_update_background_scan(hdev);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005417 }
5418
Johan Hedberg66593582014-07-09 12:59:14 +03005419complete:
Marcel Holtmann2faade52014-06-29 19:44:03 +02005420 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE,
5421 MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr));
5422
5423unlock:
5424 hci_dev_unlock(hdev);
5425 return err;
5426}
5427
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005428static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
5429 u16 len)
5430{
5431 struct mgmt_cp_load_conn_param *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005432 const u16 max_param_count = ((U16_MAX - sizeof(*cp)) /
5433 sizeof(struct mgmt_conn_param));
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005434 u16 param_count, expected_len;
5435 int i;
5436
5437 if (!lmp_le_capable(hdev))
5438 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
5439 MGMT_STATUS_NOT_SUPPORTED);
5440
5441 param_count = __le16_to_cpu(cp->param_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005442 if (param_count > max_param_count) {
5443 BT_ERR("load_conn_param: too big param_count value %u",
5444 param_count);
5445 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
5446 MGMT_STATUS_INVALID_PARAMS);
5447 }
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005448
5449 expected_len = sizeof(*cp) + param_count *
5450 sizeof(struct mgmt_conn_param);
5451 if (expected_len != len) {
5452 BT_ERR("load_conn_param: expected %u bytes, got %u bytes",
5453 expected_len, len);
5454 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
5455 MGMT_STATUS_INVALID_PARAMS);
5456 }
5457
5458 BT_DBG("%s param_count %u", hdev->name, param_count);
5459
5460 hci_dev_lock(hdev);
5461
5462 hci_conn_params_clear_disabled(hdev);
5463
5464 for (i = 0; i < param_count; i++) {
5465 struct mgmt_conn_param *param = &cp->params[i];
5466 struct hci_conn_params *hci_param;
5467 u16 min, max, latency, timeout;
5468 u8 addr_type;
5469
5470 BT_DBG("Adding %pMR (type %u)", &param->addr.bdaddr,
5471 param->addr.type);
5472
5473 if (param->addr.type == BDADDR_LE_PUBLIC) {
5474 addr_type = ADDR_LE_DEV_PUBLIC;
5475 } else if (param->addr.type == BDADDR_LE_RANDOM) {
5476 addr_type = ADDR_LE_DEV_RANDOM;
5477 } else {
5478 BT_ERR("Ignoring invalid connection parameters");
5479 continue;
5480 }
5481
5482 min = le16_to_cpu(param->min_interval);
5483 max = le16_to_cpu(param->max_interval);
5484 latency = le16_to_cpu(param->latency);
5485 timeout = le16_to_cpu(param->timeout);
5486
5487 BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x",
5488 min, max, latency, timeout);
5489
5490 if (hci_check_conn_params(min, max, latency, timeout) < 0) {
5491 BT_ERR("Ignoring invalid connection parameters");
5492 continue;
5493 }
5494
5495 hci_param = hci_conn_params_add(hdev, &param->addr.bdaddr,
5496 addr_type);
5497 if (!hci_param) {
5498 BT_ERR("Failed to add connection parameters");
5499 continue;
5500 }
5501
5502 hci_param->conn_min_interval = min;
5503 hci_param->conn_max_interval = max;
5504 hci_param->conn_latency = latency;
5505 hci_param->supervision_timeout = timeout;
5506 }
5507
5508 hci_dev_unlock(hdev);
5509
5510 return cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, NULL, 0);
5511}
5512
Marcel Holtmanndbece372014-07-04 18:11:55 +02005513static int set_external_config(struct sock *sk, struct hci_dev *hdev,
5514 void *data, u16 len)
5515{
5516 struct mgmt_cp_set_external_config *cp = data;
5517 bool changed;
5518 int err;
5519
5520 BT_DBG("%s", hdev->name);
5521
5522 if (hdev_is_powered(hdev))
5523 return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
5524 MGMT_STATUS_REJECTED);
5525
5526 if (cp->config != 0x00 && cp->config != 0x01)
5527 return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
5528 MGMT_STATUS_INVALID_PARAMS);
5529
5530 if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
5531 return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
5532 MGMT_STATUS_NOT_SUPPORTED);
5533
5534 hci_dev_lock(hdev);
5535
5536 if (cp->config)
5537 changed = !test_and_set_bit(HCI_EXT_CONFIGURED,
5538 &hdev->dev_flags);
5539 else
5540 changed = test_and_clear_bit(HCI_EXT_CONFIGURED,
5541 &hdev->dev_flags);
5542
5543 err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
5544 if (err < 0)
5545 goto unlock;
5546
5547 if (!changed)
5548 goto unlock;
5549
Marcel Holtmannf4537c02014-07-04 19:06:23 +02005550 err = new_options(hdev, sk);
5551
Marcel Holtmanndbece372014-07-04 18:11:55 +02005552 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) == is_configured(hdev)) {
5553 mgmt_index_removed(hdev);
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02005554
5555 if (test_and_change_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) {
5556 set_bit(HCI_CONFIG, &hdev->dev_flags);
5557 set_bit(HCI_AUTO_OFF, &hdev->dev_flags);
5558
5559 queue_work(hdev->req_workqueue, &hdev->power_on);
5560 } else {
Marcel Holtmann5ea234d2014-07-06 12:11:16 +02005561 set_bit(HCI_RAW, &hdev->flags);
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02005562 mgmt_index_added(hdev);
5563 }
Marcel Holtmanndbece372014-07-04 18:11:55 +02005564 }
5565
5566unlock:
5567 hci_dev_unlock(hdev);
5568 return err;
5569}
5570
Marcel Holtmann9713c172014-07-06 12:11:15 +02005571static int set_public_address(struct sock *sk, struct hci_dev *hdev,
5572 void *data, u16 len)
5573{
5574 struct mgmt_cp_set_public_address *cp = data;
5575 bool changed;
5576 int err;
5577
5578 BT_DBG("%s", hdev->name);
5579
5580 if (hdev_is_powered(hdev))
5581 return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
5582 MGMT_STATUS_REJECTED);
5583
5584 if (!bacmp(&cp->bdaddr, BDADDR_ANY))
5585 return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
5586 MGMT_STATUS_INVALID_PARAMS);
5587
5588 if (!hdev->set_bdaddr)
5589 return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
5590 MGMT_STATUS_NOT_SUPPORTED);
5591
5592 hci_dev_lock(hdev);
5593
5594 changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
5595 bacpy(&hdev->public_addr, &cp->bdaddr);
5596
5597 err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev);
5598 if (err < 0)
5599 goto unlock;
5600
5601 if (!changed)
5602 goto unlock;
5603
5604 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags))
5605 err = new_options(hdev, sk);
5606
5607 if (is_configured(hdev)) {
5608 mgmt_index_removed(hdev);
5609
5610 clear_bit(HCI_UNCONFIGURED, &hdev->dev_flags);
5611
5612 set_bit(HCI_CONFIG, &hdev->dev_flags);
5613 set_bit(HCI_AUTO_OFF, &hdev->dev_flags);
5614
5615 queue_work(hdev->req_workqueue, &hdev->power_on);
5616 }
5617
5618unlock:
5619 hci_dev_unlock(hdev);
5620 return err;
5621}
5622
Andrei Emeltchenko2e3c35e2012-03-14 18:54:15 +02005623static const struct mgmt_handler {
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005624 int (*func) (struct sock *sk, struct hci_dev *hdev, void *data,
5625 u16 data_len);
Johan Hedbergbe22b542012-03-01 22:24:41 +02005626 bool var_len;
5627 size_t data_len;
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02005628} mgmt_handlers[] = {
5629 { NULL }, /* 0x0000 (no command) */
Johan Hedbergbe22b542012-03-01 22:24:41 +02005630 { read_version, false, MGMT_READ_VERSION_SIZE },
5631 { read_commands, false, MGMT_READ_COMMANDS_SIZE },
5632 { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE },
5633 { read_controller_info, false, MGMT_READ_INFO_SIZE },
5634 { set_powered, false, MGMT_SETTING_SIZE },
5635 { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE },
5636 { set_connectable, false, MGMT_SETTING_SIZE },
5637 { set_fast_connectable, false, MGMT_SETTING_SIZE },
Johan Hedbergb2939472014-07-30 09:22:23 +03005638 { set_bondable, false, MGMT_SETTING_SIZE },
Johan Hedbergbe22b542012-03-01 22:24:41 +02005639 { set_link_security, false, MGMT_SETTING_SIZE },
5640 { set_ssp, false, MGMT_SETTING_SIZE },
5641 { set_hs, false, MGMT_SETTING_SIZE },
5642 { set_le, false, MGMT_SETTING_SIZE },
5643 { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE },
5644 { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE },
5645 { add_uuid, false, MGMT_ADD_UUID_SIZE },
5646 { remove_uuid, false, MGMT_REMOVE_UUID_SIZE },
5647 { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE },
5648 { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE },
5649 { disconnect, false, MGMT_DISCONNECT_SIZE },
5650 { get_connections, false, MGMT_GET_CONNECTIONS_SIZE },
5651 { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE },
5652 { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE },
5653 { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE },
5654 { pair_device, false, MGMT_PAIR_DEVICE_SIZE },
5655 { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE },
5656 { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE },
5657 { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE },
5658 { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
5659 { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE },
5660 { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
5661 { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE },
Marcel Holtmannec109112014-01-10 02:07:30 -08005662 { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE },
Johan Hedbergbe22b542012-03-01 22:24:41 +02005663 { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
5664 { start_discovery, false, MGMT_START_DISCOVERY_SIZE },
5665 { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE },
5666 { confirm_name, false, MGMT_CONFIRM_NAME_SIZE },
5667 { block_device, false, MGMT_BLOCK_DEVICE_SIZE },
5668 { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE },
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07005669 { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE },
Johan Hedberg4375f102013-09-25 13:26:10 +03005670 { set_advertising, false, MGMT_SETTING_SIZE },
Johan Hedberg0663ca22013-10-02 13:43:14 +03005671 { set_bredr, false, MGMT_SETTING_SIZE },
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07005672 { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE },
Marcel Holtmann14b49b92013-10-11 08:23:20 -07005673 { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE },
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005674 { set_secure_conn, false, MGMT_SETTING_SIZE },
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005675 { set_debug_keys, false, MGMT_SETTING_SIZE },
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005676 { set_privacy, false, MGMT_SET_PRIVACY_SIZE },
Johan Hedberg41edf162014-02-18 10:19:35 +02005677 { load_irks, true, MGMT_LOAD_IRKS_SIZE },
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005678 { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE },
Johan Hedberg95868422014-06-28 17:54:07 +03005679 { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE },
Marcel Holtmann2faade52014-06-29 19:44:03 +02005680 { add_device, false, MGMT_ADD_DEVICE_SIZE },
5681 { remove_device, false, MGMT_REMOVE_DEVICE_SIZE },
Marcel Holtmann73d1df22014-07-02 22:10:52 +02005682 { load_conn_param, true, MGMT_LOAD_CONN_PARAM_SIZE },
5683 { read_unconf_index_list, false, MGMT_READ_UNCONF_INDEX_LIST_SIZE },
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +02005684 { read_config_info, false, MGMT_READ_CONFIG_INFO_SIZE },
Marcel Holtmanndbece372014-07-04 18:11:55 +02005685 { set_external_config, false, MGMT_SET_EXTERNAL_CONFIG_SIZE },
Marcel Holtmann9713c172014-07-06 12:11:15 +02005686 { set_public_address, false, MGMT_SET_PUBLIC_ADDRESS_SIZE },
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02005687};
5688
Johan Hedberg03811012010-12-08 00:21:06 +02005689int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
5690{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03005691 void *buf;
5692 u8 *cp;
Johan Hedberg03811012010-12-08 00:21:06 +02005693 struct mgmt_hdr *hdr;
Szymon Janc4e51eae2011-02-25 19:05:48 +01005694 u16 opcode, index, len;
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005695 struct hci_dev *hdev = NULL;
Andrei Emeltchenko2e3c35e2012-03-14 18:54:15 +02005696 const struct mgmt_handler *handler;
Johan Hedberg03811012010-12-08 00:21:06 +02005697 int err;
5698
5699 BT_DBG("got %zu bytes", msglen);
5700
5701 if (msglen < sizeof(*hdr))
5702 return -EINVAL;
5703
Gustavo F. Padovane63a15e2011-04-04 18:56:53 -03005704 buf = kmalloc(msglen, GFP_KERNEL);
Johan Hedberg03811012010-12-08 00:21:06 +02005705 if (!buf)
5706 return -ENOMEM;
5707
5708 if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) {
5709 err = -EFAULT;
5710 goto done;
5711 }
5712
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03005713 hdr = buf;
Marcel Holtmann1f350c82012-03-12 20:31:08 -07005714 opcode = __le16_to_cpu(hdr->opcode);
5715 index = __le16_to_cpu(hdr->index);
5716 len = __le16_to_cpu(hdr->len);
Johan Hedberg03811012010-12-08 00:21:06 +02005717
5718 if (len != msglen - sizeof(*hdr)) {
5719 err = -EINVAL;
5720 goto done;
5721 }
5722
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02005723 if (index != MGMT_INDEX_NONE) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005724 hdev = hci_dev_get(index);
5725 if (!hdev) {
5726 err = cmd_status(sk, index, opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005727 MGMT_STATUS_INVALID_INDEX);
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005728 goto done;
5729 }
Marcel Holtmann0736cfa2013-08-26 21:40:51 -07005730
Johan Hedbergcebf4cf2013-10-10 18:06:04 +02005731 if (test_bit(HCI_SETUP, &hdev->dev_flags) ||
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02005732 test_bit(HCI_CONFIG, &hdev->dev_flags) ||
Marcel Holtmann4a964402014-07-02 19:10:33 +02005733 test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
Marcel Holtmann0736cfa2013-08-26 21:40:51 -07005734 err = cmd_status(sk, index, opcode,
5735 MGMT_STATUS_INVALID_INDEX);
5736 goto done;
5737 }
Marcel Holtmann42a9bc142014-07-04 16:54:40 +02005738
5739 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) &&
Marcel Holtmanndbece372014-07-04 18:11:55 +02005740 opcode != MGMT_OP_READ_CONFIG_INFO &&
Marcel Holtmann9713c172014-07-06 12:11:15 +02005741 opcode != MGMT_OP_SET_EXTERNAL_CONFIG &&
5742 opcode != MGMT_OP_SET_PUBLIC_ADDRESS) {
Marcel Holtmann42a9bc142014-07-04 16:54:40 +02005743 err = cmd_status(sk, index, opcode,
5744 MGMT_STATUS_INVALID_INDEX);
5745 goto done;
5746 }
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005747 }
5748
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02005749 if (opcode >= ARRAY_SIZE(mgmt_handlers) ||
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03005750 mgmt_handlers[opcode].func == NULL) {
Johan Hedberg03811012010-12-08 00:21:06 +02005751 BT_DBG("Unknown op %u", opcode);
Johan Hedbergca69b792011-11-11 18:10:00 +02005752 err = cmd_status(sk, index, opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005753 MGMT_STATUS_UNKNOWN_COMMAND);
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02005754 goto done;
Johan Hedberg03811012010-12-08 00:21:06 +02005755 }
5756
Marcel Holtmann73d1df22014-07-02 22:10:52 +02005757 if (hdev && (opcode <= MGMT_OP_READ_INDEX_LIST ||
5758 opcode == MGMT_OP_READ_UNCONF_INDEX_LIST)) {
5759 err = cmd_status(sk, index, opcode,
5760 MGMT_STATUS_INVALID_INDEX);
5761 goto done;
5762 }
5763
5764 if (!hdev && (opcode > MGMT_OP_READ_INDEX_LIST &&
5765 opcode != MGMT_OP_READ_UNCONF_INDEX_LIST)) {
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02005766 err = cmd_status(sk, index, opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005767 MGMT_STATUS_INVALID_INDEX);
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02005768 goto done;
5769 }
5770
Johan Hedbergbe22b542012-03-01 22:24:41 +02005771 handler = &mgmt_handlers[opcode];
5772
5773 if ((handler->var_len && len < handler->data_len) ||
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03005774 (!handler->var_len && len != handler->data_len)) {
Johan Hedbergbe22b542012-03-01 22:24:41 +02005775 err = cmd_status(sk, index, opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005776 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergbe22b542012-03-01 22:24:41 +02005777 goto done;
5778 }
5779
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02005780 if (hdev)
5781 mgmt_init_hdev(sk, hdev);
5782
5783 cp = buf + sizeof(*hdr);
5784
Johan Hedbergbe22b542012-03-01 22:24:41 +02005785 err = handler->func(sk, hdev, cp, len);
Johan Hedberge41d8b42010-12-13 21:07:03 +02005786 if (err < 0)
5787 goto done;
5788
Johan Hedberg03811012010-12-08 00:21:06 +02005789 err = msglen;
5790
5791done:
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005792 if (hdev)
5793 hci_dev_put(hdev);
5794
Johan Hedberg03811012010-12-08 00:21:06 +02005795 kfree(buf);
5796 return err;
5797}
Johan Hedbergc71e97b2010-12-13 21:07:07 +02005798
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07005799void mgmt_index_added(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02005800{
Marcel Holtmann1514b892013-10-06 08:25:01 -07005801 if (hdev->dev_type != HCI_BREDR)
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07005802 return;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +03005803
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02005804 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
5805 return;
5806
5807 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags))
5808 mgmt_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, NULL, 0, NULL);
5809 else
5810 mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL);
Johan Hedbergc71e97b2010-12-13 21:07:07 +02005811}
5812
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07005813void mgmt_index_removed(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02005814{
Johan Hedberg5f159032012-03-02 03:13:19 +02005815 u8 status = MGMT_STATUS_INVALID_INDEX;
Johan Hedbergb24752f2011-11-03 14:40:33 +02005816
Marcel Holtmann1514b892013-10-06 08:25:01 -07005817 if (hdev->dev_type != HCI_BREDR)
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07005818 return;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +03005819
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02005820 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
5821 return;
5822
Johan Hedberg744cf192011-11-08 20:40:14 +02005823 mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status);
Johan Hedbergb24752f2011-11-03 14:40:33 +02005824
Marcel Holtmannedd38962014-07-02 21:30:55 +02005825 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags))
5826 mgmt_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, NULL, 0, NULL);
5827 else
5828 mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02005829}
5830
Andre Guedes6046dc32014-02-26 20:21:51 -03005831/* This function requires the caller holds hdev->lock */
Johan Hedbergd7347f32014-07-04 12:37:23 +03005832static void restart_le_actions(struct hci_dev *hdev)
Andre Guedes6046dc32014-02-26 20:21:51 -03005833{
5834 struct hci_conn_params *p;
5835
5836 list_for_each_entry(p, &hdev->le_conn_params, list) {
Johan Hedbergd7347f32014-07-04 12:37:23 +03005837 /* Needed for AUTO_OFF case where might not "really"
5838 * have been powered off.
5839 */
5840 list_del_init(&p->action);
5841
5842 switch (p->auto_connect) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005843 case HCI_AUTO_CONN_DIRECT:
Johan Hedbergd7347f32014-07-04 12:37:23 +03005844 case HCI_AUTO_CONN_ALWAYS:
5845 list_add(&p->action, &hdev->pend_le_conns);
5846 break;
5847 case HCI_AUTO_CONN_REPORT:
5848 list_add(&p->action, &hdev->pend_le_reports);
5849 break;
5850 default:
5851 break;
Marcel Holtmannc83ed192014-07-01 19:28:24 +02005852 }
Andre Guedes6046dc32014-02-26 20:21:51 -03005853 }
Marcel Holtmannc83ed192014-07-01 19:28:24 +02005854
Marcel Holtmannc83ed192014-07-01 19:28:24 +02005855 hci_update_background_scan(hdev);
Andre Guedes6046dc32014-02-26 20:21:51 -03005856}
5857
Johan Hedberg229ab392013-03-15 17:06:53 -05005858static void powered_complete(struct hci_dev *hdev, u8 status)
5859{
5860 struct cmd_lookup match = { NULL, hdev };
5861
5862 BT_DBG("status 0x%02x", status);
5863
5864 hci_dev_lock(hdev);
5865
Johan Hedbergd7347f32014-07-04 12:37:23 +03005866 restart_le_actions(hdev);
Andre Guedes6046dc32014-02-26 20:21:51 -03005867
Johan Hedberg229ab392013-03-15 17:06:53 -05005868 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
5869
5870 new_settings(hdev, match.sk);
5871
5872 hci_dev_unlock(hdev);
5873
5874 if (match.sk)
5875 sock_put(match.sk);
5876}
5877
Johan Hedberg70da6242013-03-15 17:06:51 -05005878static int powered_update_hci(struct hci_dev *hdev)
5879{
Johan Hedberg890ea892013-03-15 17:06:52 -05005880 struct hci_request req;
Johan Hedberg70da6242013-03-15 17:06:51 -05005881 u8 link_sec;
5882
Johan Hedberg890ea892013-03-15 17:06:52 -05005883 hci_req_init(&req, hdev);
5884
Johan Hedberg70da6242013-03-15 17:06:51 -05005885 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) &&
5886 !lmp_host_ssp_capable(hdev)) {
5887 u8 ssp = 1;
5888
Johan Hedberg890ea892013-03-15 17:06:52 -05005889 hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp);
Johan Hedberg70da6242013-03-15 17:06:51 -05005890 }
5891
Johan Hedbergc73eee92013-04-19 18:35:21 +03005892 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) &&
5893 lmp_bredr_capable(hdev)) {
Johan Hedberg70da6242013-03-15 17:06:51 -05005894 struct hci_cp_write_le_host_supported cp;
5895
Marcel Holtmann32226e42014-07-24 20:04:16 +02005896 cp.le = 0x01;
5897 cp.simul = 0x00;
Johan Hedberg70da6242013-03-15 17:06:51 -05005898
5899 /* Check first if we already have the right
5900 * host state (host features set)
5901 */
5902 if (cp.le != lmp_host_le_capable(hdev) ||
5903 cp.simul != lmp_host_le_br_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05005904 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED,
5905 sizeof(cp), &cp);
Johan Hedberg70da6242013-03-15 17:06:51 -05005906 }
5907
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07005908 if (lmp_le_capable(hdev)) {
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07005909 /* Make sure the controller has a good default for
5910 * advertising data. This also applies to the case
5911 * where BR/EDR was toggled during the AUTO_OFF phase.
5912 */
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07005913 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07005914 update_adv_data(&req);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07005915 update_scan_rsp_data(&req);
5916 }
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07005917
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07005918 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
5919 enable_advertising(&req);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03005920 }
5921
Johan Hedberg70da6242013-03-15 17:06:51 -05005922 link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
5923 if (link_sec != test_bit(HCI_AUTH, &hdev->flags))
Johan Hedberg890ea892013-03-15 17:06:52 -05005924 hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE,
5925 sizeof(link_sec), &link_sec);
Johan Hedberg70da6242013-03-15 17:06:51 -05005926
5927 if (lmp_bredr_capable(hdev)) {
Johan Hedberg432df052014-08-01 11:13:31 +03005928 write_fast_connectable(&req, false);
5929 hci_update_page_scan(hdev, &req);
Johan Hedberg890ea892013-03-15 17:06:52 -05005930 update_class(&req);
Johan Hedberg13928972013-03-15 17:07:00 -05005931 update_name(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05005932 update_eir(&req);
Johan Hedberg70da6242013-03-15 17:06:51 -05005933 }
5934
Johan Hedberg229ab392013-03-15 17:06:53 -05005935 return hci_req_run(&req, powered_complete);
Johan Hedberg70da6242013-03-15 17:06:51 -05005936}
5937
Johan Hedberg744cf192011-11-08 20:40:14 +02005938int mgmt_powered(struct hci_dev *hdev, u8 powered)
Johan Hedberg5add6af2010-12-16 10:00:37 +02005939{
Johan Hedberg76a7f3a2012-02-17 00:34:40 +02005940 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg229ab392013-03-15 17:06:53 -05005941 u8 status_not_powered = MGMT_STATUS_NOT_POWERED;
5942 u8 zero_cod[] = { 0, 0, 0 };
Johan Hedberg7bb895d2012-02-17 01:20:00 +02005943 int err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02005944
Johan Hedberg5e5282b2012-02-21 16:01:30 +02005945 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
5946 return 0;
5947
Johan Hedberg5e5282b2012-02-21 16:01:30 +02005948 if (powered) {
Johan Hedberg229ab392013-03-15 17:06:53 -05005949 if (powered_update_hci(hdev) == 0)
5950 return 0;
Johan Hedbergfe038882013-01-16 16:15:34 +02005951
Johan Hedberg229ab392013-03-15 17:06:53 -05005952 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp,
5953 &match);
5954 goto new_settings;
Johan Hedbergb24752f2011-11-03 14:40:33 +02005955 }
5956
Johan Hedberg229ab392013-03-15 17:06:53 -05005957 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
5958 mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered);
5959
5960 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0)
5961 mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
5962 zero_cod, sizeof(zero_cod), NULL);
5963
5964new_settings:
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02005965 err = new_settings(hdev, match.sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02005966
5967 if (match.sk)
5968 sock_put(match.sk);
5969
Johan Hedberg7bb895d2012-02-17 01:20:00 +02005970 return err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02005971}
Johan Hedberg73f22f62010-12-29 16:00:25 +02005972
Marcel Holtmann3eec7052013-10-06 23:55:46 -07005973void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
Johan Hedberg96570ff2013-05-29 09:51:29 +03005974{
5975 struct pending_cmd *cmd;
5976 u8 status;
5977
5978 cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev);
5979 if (!cmd)
Marcel Holtmann3eec7052013-10-06 23:55:46 -07005980 return;
Johan Hedberg96570ff2013-05-29 09:51:29 +03005981
5982 if (err == -ERFKILL)
5983 status = MGMT_STATUS_RFKILLED;
5984 else
5985 status = MGMT_STATUS_FAILED;
5986
Marcel Holtmann3eec7052013-10-06 23:55:46 -07005987 cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
Johan Hedberg96570ff2013-05-29 09:51:29 +03005988
5989 mgmt_pending_remove(cmd);
Johan Hedberg96570ff2013-05-29 09:51:29 +03005990}
5991
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07005992void mgmt_discoverable_timeout(struct hci_dev *hdev)
5993{
5994 struct hci_request req;
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07005995
5996 hci_dev_lock(hdev);
5997
5998 /* When discoverable timeout triggers, then just make sure
5999 * the limited discoverable flag is cleared. Even in the case
6000 * of a timeout triggered from general discoverable, it is
6001 * safe to unconditionally clear the flag.
6002 */
6003 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
Johan Hedberg9a43e252013-10-20 19:00:07 +03006004 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07006005
6006 hci_req_init(&req, hdev);
Johan Hedberg4b580612013-10-19 23:38:21 +03006007 if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
6008 u8 scan = SCAN_PAGE;
6009 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE,
6010 sizeof(scan), &scan);
6011 }
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07006012 update_class(&req);
Johan Hedberg9a43e252013-10-20 19:00:07 +03006013 update_adv_data(&req);
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07006014 hci_req_run(&req, NULL);
6015
6016 hdev->discov_timeout = 0;
6017
Johan Hedberg9a43e252013-10-20 19:00:07 +03006018 new_settings(hdev, NULL);
6019
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07006020 hci_dev_unlock(hdev);
6021}
6022
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07006023void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
6024 bool persistent)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006025{
Johan Hedberg86742e12011-11-07 23:13:38 +02006026 struct mgmt_ev_new_link_key ev;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006027
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006028 memset(&ev, 0, sizeof(ev));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006029
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006030 ev.store_hint = persistent;
Johan Hedbergd753fdc2012-02-17 14:06:34 +02006031 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03006032 ev.key.addr.type = BDADDR_BREDR;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006033 ev.key.type = key->type;
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03006034 memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006035 ev.key.pin_len = key->pin_len;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006036
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07006037 mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006038}
Johan Hedbergf7520542011-01-20 12:34:39 +02006039
Johan Hedbergd7b25452014-05-23 13:19:53 +03006040static u8 mgmt_ltk_type(struct smp_ltk *ltk)
6041{
6042 if (ltk->authenticated)
6043 return MGMT_LTK_AUTHENTICATED;
6044
6045 return MGMT_LTK_UNAUTHENTICATED;
6046}
6047
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07006048void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006049{
6050 struct mgmt_ev_new_long_term_key ev;
6051
6052 memset(&ev, 0, sizeof(ev));
6053
Marcel Holtmann5192d302014-02-19 17:11:58 -08006054 /* Devices using resolvable or non-resolvable random addresses
6055 * without providing an indentity resolving key don't require
6056 * to store long term keys. Their addresses will change the
6057 * next time around.
6058 *
6059 * Only when a remote device provides an identity address
6060 * make sure the long term key is stored. If the remote
6061 * identity is known, the long term keys are internally
6062 * mapped to the identity address. So allow static random
6063 * and public addresses here.
6064 */
Johan Hedbergba74b662014-02-19 14:57:45 +02006065 if (key->bdaddr_type == ADDR_LE_DEV_RANDOM &&
6066 (key->bdaddr.b[5] & 0xc0) != 0xc0)
6067 ev.store_hint = 0x00;
6068 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07006069 ev.store_hint = persistent;
Johan Hedbergba74b662014-02-19 14:57:45 +02006070
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006071 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03006072 ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
Johan Hedbergd7b25452014-05-23 13:19:53 +03006073 ev.key.type = mgmt_ltk_type(key);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006074 ev.key.enc_size = key->enc_size;
6075 ev.key.ediv = key->ediv;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08006076 ev.key.rand = key->rand;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006077
Johan Hedberg2ceba532014-06-16 19:25:16 +03006078 if (key->type == SMP_LTK)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006079 ev.key.master = 1;
6080
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006081 memcpy(ev.key.val, key->val, sizeof(key->val));
6082
Marcel Holtmann083368f2013-10-15 14:26:29 -07006083 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006084}
6085
Johan Hedberg95fbac82014-02-19 15:18:31 +02006086void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk)
6087{
6088 struct mgmt_ev_new_irk ev;
6089
6090 memset(&ev, 0, sizeof(ev));
6091
Marcel Holtmannbab6d1e2014-02-19 11:51:54 -08006092 /* For identity resolving keys from devices that are already
6093 * using a public address or static random address, do not
6094 * ask for storing this key. The identity resolving key really
6095 * is only mandatory for devices using resovlable random
6096 * addresses.
6097 *
6098 * Storing all identity resolving keys has the downside that
6099 * they will be also loaded on next boot of they system. More
6100 * identity resolving keys, means more time during scanning is
6101 * needed to actually resolve these addresses.
6102 */
6103 if (bacmp(&irk->rpa, BDADDR_ANY))
6104 ev.store_hint = 0x01;
6105 else
6106 ev.store_hint = 0x00;
6107
Johan Hedberg95fbac82014-02-19 15:18:31 +02006108 bacpy(&ev.rpa, &irk->rpa);
6109 bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr);
6110 ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type);
6111 memcpy(ev.irk.val, irk->val, sizeof(irk->val));
6112
6113 mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
6114}
6115
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07006116void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
6117 bool persistent)
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07006118{
6119 struct mgmt_ev_new_csrk ev;
6120
6121 memset(&ev, 0, sizeof(ev));
6122
6123 /* Devices using resolvable or non-resolvable random addresses
6124 * without providing an indentity resolving key don't require
6125 * to store signature resolving keys. Their addresses will change
6126 * the next time around.
6127 *
6128 * Only when a remote device provides an identity address
6129 * make sure the signature resolving key is stored. So allow
6130 * static random and public addresses here.
6131 */
6132 if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM &&
6133 (csrk->bdaddr.b[5] & 0xc0) != 0xc0)
6134 ev.store_hint = 0x00;
6135 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07006136 ev.store_hint = persistent;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07006137
6138 bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
6139 ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
6140 ev.key.master = csrk->master;
6141 memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
6142
6143 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
6144}
6145
Andre Guedesffb5a8272014-07-01 18:10:11 -03006146void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedbergf4869e22014-07-02 17:37:32 +03006147 u8 bdaddr_type, u8 store_hint, u16 min_interval,
6148 u16 max_interval, u16 latency, u16 timeout)
Andre Guedesffb5a8272014-07-01 18:10:11 -03006149{
6150 struct mgmt_ev_new_conn_param ev;
6151
Johan Hedbergc103aea2014-07-02 17:37:34 +03006152 if (!hci_is_identity_address(bdaddr, bdaddr_type))
6153 return;
6154
Andre Guedesffb5a8272014-07-01 18:10:11 -03006155 memset(&ev, 0, sizeof(ev));
6156 bacpy(&ev.addr.bdaddr, bdaddr);
6157 ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type);
Johan Hedbergf4869e22014-07-02 17:37:32 +03006158 ev.store_hint = store_hint;
Andre Guedesffb5a8272014-07-01 18:10:11 -03006159 ev.min_interval = cpu_to_le16(min_interval);
6160 ev.max_interval = cpu_to_le16(max_interval);
6161 ev.latency = cpu_to_le16(latency);
6162 ev.timeout = cpu_to_le16(timeout);
6163
6164 mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL);
6165}
6166
Marcel Holtmann94933992013-10-15 10:26:39 -07006167static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
6168 u8 data_len)
6169{
6170 eir[eir_len++] = sizeof(type) + data_len;
6171 eir[eir_len++] = type;
6172 memcpy(&eir[eir_len], data, data_len);
6173 eir_len += data_len;
6174
6175 return eir_len;
6176}
6177
Marcel Holtmannecd90ae2013-10-06 23:55:49 -07006178void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
6179 u8 addr_type, u32 flags, u8 *name, u8 name_len,
6180 u8 *dev_class)
Johan Hedbergf7520542011-01-20 12:34:39 +02006181{
Johan Hedbergb644ba32012-01-17 21:48:47 +02006182 char buf[512];
6183 struct mgmt_ev_device_connected *ev = (void *) buf;
6184 u16 eir_len = 0;
Johan Hedbergf7520542011-01-20 12:34:39 +02006185
Johan Hedbergb644ba32012-01-17 21:48:47 +02006186 bacpy(&ev->addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03006187 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergf7520542011-01-20 12:34:39 +02006188
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02006189 ev->flags = __cpu_to_le32(flags);
Johan Hedberg08c79b62012-02-23 22:31:51 +02006190
Johan Hedbergb644ba32012-01-17 21:48:47 +02006191 if (name_len > 0)
6192 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006193 name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02006194
6195 if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0)
Brian Gix53156382012-03-09 14:07:03 -08006196 eir_len = eir_append_data(ev->eir, eir_len,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006197 EIR_CLASS_OF_DEV, dev_class, 3);
Johan Hedbergb644ba32012-01-17 21:48:47 +02006198
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02006199 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02006200
Marcel Holtmannecd90ae2013-10-06 23:55:49 -07006201 mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf,
6202 sizeof(*ev) + eir_len, NULL);
Johan Hedbergf7520542011-01-20 12:34:39 +02006203}
6204
Johan Hedberg8962ee72011-01-20 12:40:27 +02006205static void disconnect_rsp(struct pending_cmd *cmd, void *data)
6206{
Szymon Jancc68fb7f2011-03-22 13:12:19 +01006207 struct mgmt_cp_disconnect *cp = cmd->param;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006208 struct sock **sk = data;
Johan Hedberga38528f2011-01-22 06:46:43 +02006209 struct mgmt_rp_disconnect rp;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006210
Johan Hedberg88c3df12012-02-09 14:27:38 +02006211 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
6212 rp.addr.type = cp->addr.type;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006213
Johan Hedbergaee9b212012-02-18 15:07:59 +02006214 cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006215 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02006216
6217 *sk = cmd->sk;
6218 sock_hold(*sk);
6219
Johan Hedberga664b5b2011-02-19 12:06:02 -03006220 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006221}
6222
Johan Hedberg124f6e32012-02-09 13:50:12 +02006223static void unpair_device_rsp(struct pending_cmd *cmd, void *data)
Johan Hedberga8a1d192011-11-10 15:54:38 +02006224{
Johan Hedbergb1078ad2012-02-09 17:21:16 +02006225 struct hci_dev *hdev = data;
Johan Hedberg124f6e32012-02-09 13:50:12 +02006226 struct mgmt_cp_unpair_device *cp = cmd->param;
6227 struct mgmt_rp_unpair_device rp;
Johan Hedberga8a1d192011-11-10 15:54:38 +02006228
6229 memset(&rp, 0, sizeof(rp));
Johan Hedberg124f6e32012-02-09 13:50:12 +02006230 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
6231 rp.addr.type = cp->addr.type;
Johan Hedberga8a1d192011-11-10 15:54:38 +02006232
Johan Hedbergb1078ad2012-02-09 17:21:16 +02006233 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
6234
Johan Hedbergaee9b212012-02-18 15:07:59 +02006235 cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp));
Johan Hedberga8a1d192011-11-10 15:54:38 +02006236
6237 mgmt_pending_remove(cmd);
6238}
6239
Johan Hedberg84c61d92014-08-01 11:13:30 +03006240bool mgmt_powering_down(struct hci_dev *hdev)
6241{
6242 struct pending_cmd *cmd;
6243 struct mgmt_mode *cp;
6244
6245 cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev);
6246 if (!cmd)
6247 return false;
6248
6249 cp = cmd->param;
6250 if (!cp->val)
6251 return true;
6252
6253 return false;
6254}
6255
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07006256void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02006257 u8 link_type, u8 addr_type, u8 reason,
6258 bool mgmt_connected)
Johan Hedbergf7520542011-01-20 12:34:39 +02006259{
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02006260 struct mgmt_ev_device_disconnected ev;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006261 struct sock *sk = NULL;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006262
Johan Hedberg84c61d92014-08-01 11:13:30 +03006263 /* The connection is still in hci_conn_hash so test for 1
6264 * instead of 0 to know if this is the last one.
6265 */
6266 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
6267 cancel_delayed_work(&hdev->power_off);
6268 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberg8b064a32014-02-24 14:52:22 +02006269 }
6270
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02006271 if (!mgmt_connected)
6272 return;
6273
Andre Guedes57eb7762013-10-30 19:01:41 -03006274 if (link_type != ACL_LINK && link_type != LE_LINK)
6275 return;
6276
Johan Hedberg744cf192011-11-08 20:40:14 +02006277 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
Johan Hedbergf7520542011-01-20 12:34:39 +02006278
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02006279 bacpy(&ev.addr.bdaddr, bdaddr);
6280 ev.addr.type = link_to_bdaddr(link_type, addr_type);
6281 ev.reason = reason;
Johan Hedbergf7520542011-01-20 12:34:39 +02006282
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07006283 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006284
6285 if (sk)
Szymon Jancd97dcb62012-03-16 16:02:56 +01006286 sock_put(sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006287
Johan Hedberg124f6e32012-02-09 13:50:12 +02006288 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006289 hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006290}
6291
Marcel Holtmann78929242013-10-06 23:55:47 -07006292void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
6293 u8 link_type, u8 addr_type, u8 status)
Johan Hedberg8962ee72011-01-20 12:40:27 +02006294{
Andre Guedes3655bba2013-10-30 19:01:40 -03006295 u8 bdaddr_type = link_to_bdaddr(link_type, addr_type);
6296 struct mgmt_cp_disconnect *cp;
Johan Hedberg88c3df12012-02-09 14:27:38 +02006297 struct mgmt_rp_disconnect rp;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006298 struct pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006299
Jefferson Delfes36a75f12012-09-18 13:36:54 -04006300 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
6301 hdev);
6302
Johan Hedberg2e58ef32011-11-08 20:40:15 +02006303 cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006304 if (!cmd)
Marcel Holtmann78929242013-10-06 23:55:47 -07006305 return;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006306
Andre Guedes3655bba2013-10-30 19:01:40 -03006307 cp = cmd->param;
6308
6309 if (bacmp(bdaddr, &cp->addr.bdaddr))
6310 return;
6311
6312 if (cp->addr.type != bdaddr_type)
6313 return;
6314
Johan Hedberg88c3df12012-02-09 14:27:38 +02006315 bacpy(&rp.addr.bdaddr, bdaddr);
Andre Guedes3655bba2013-10-30 19:01:40 -03006316 rp.addr.type = bdaddr_type;
Johan Hedberg37d9ef72011-11-10 15:54:39 +02006317
Marcel Holtmann78929242013-10-06 23:55:47 -07006318 cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT,
6319 mgmt_status(status), &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02006320
Johan Hedberga664b5b2011-02-19 12:06:02 -03006321 mgmt_pending_remove(cmd);
Johan Hedbergf7520542011-01-20 12:34:39 +02006322}
Johan Hedberg17d5c042011-01-22 06:09:08 +02006323
Marcel Holtmann445608d2013-10-06 23:55:48 -07006324void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
6325 u8 addr_type, u8 status)
Johan Hedberg17d5c042011-01-22 06:09:08 +02006326{
6327 struct mgmt_ev_connect_failed ev;
Johan Hedbergc9910d02014-02-27 14:35:12 +02006328
Johan Hedberg84c61d92014-08-01 11:13:30 +03006329 /* The connection is still in hci_conn_hash so test for 1
6330 * instead of 0 to know if this is the last one.
6331 */
6332 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
6333 cancel_delayed_work(&hdev->power_off);
6334 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedbergc9910d02014-02-27 14:35:12 +02006335 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02006336
Johan Hedberg4c659c32011-11-07 23:13:39 +02006337 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03006338 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergca69b792011-11-11 18:10:00 +02006339 ev.status = mgmt_status(status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02006340
Marcel Holtmann445608d2013-10-06 23:55:48 -07006341 mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg17d5c042011-01-22 06:09:08 +02006342}
Johan Hedberg980e1a52011-01-22 06:10:07 +02006343
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07006344void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
Johan Hedberg980e1a52011-01-22 06:10:07 +02006345{
6346 struct mgmt_ev_pin_code_request ev;
6347
Johan Hedbergd8457692012-02-17 14:24:57 +02006348 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03006349 ev.addr.type = BDADDR_BREDR;
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02006350 ev.secure = secure;
Johan Hedberg980e1a52011-01-22 06:10:07 +02006351
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07006352 mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg980e1a52011-01-22 06:10:07 +02006353}
6354
Marcel Holtmanne669cf82013-10-15 14:26:21 -07006355void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
6356 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02006357{
6358 struct pending_cmd *cmd;
Johan Hedbergac56fb12011-02-19 12:05:59 -03006359 struct mgmt_rp_pin_code_reply rp;
Johan Hedberg980e1a52011-01-22 06:10:07 +02006360
Johan Hedberg2e58ef32011-11-08 20:40:15 +02006361 cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02006362 if (!cmd)
Marcel Holtmanne669cf82013-10-15 14:26:21 -07006363 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02006364
Johan Hedbergd8457692012-02-17 14:24:57 +02006365 bacpy(&rp.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03006366 rp.addr.type = BDADDR_BREDR;
Johan Hedbergac56fb12011-02-19 12:05:59 -03006367
Marcel Holtmanne669cf82013-10-15 14:26:21 -07006368 cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
6369 mgmt_status(status), &rp, sizeof(rp));
Johan Hedberg980e1a52011-01-22 06:10:07 +02006370
Johan Hedberga664b5b2011-02-19 12:06:02 -03006371 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02006372}
6373
Marcel Holtmann3eb38522013-10-15 14:26:22 -07006374void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
6375 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02006376{
6377 struct pending_cmd *cmd;
Johan Hedbergac56fb12011-02-19 12:05:59 -03006378 struct mgmt_rp_pin_code_reply rp;
Johan Hedberg980e1a52011-01-22 06:10:07 +02006379
Johan Hedberg2e58ef32011-11-08 20:40:15 +02006380 cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02006381 if (!cmd)
Marcel Holtmann3eb38522013-10-15 14:26:22 -07006382 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02006383
Johan Hedbergd8457692012-02-17 14:24:57 +02006384 bacpy(&rp.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03006385 rp.addr.type = BDADDR_BREDR;
Johan Hedbergac56fb12011-02-19 12:05:59 -03006386
Marcel Holtmann3eb38522013-10-15 14:26:22 -07006387 cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY,
6388 mgmt_status(status), &rp, sizeof(rp));
Johan Hedberg980e1a52011-01-22 06:10:07 +02006389
Johan Hedberga664b5b2011-02-19 12:06:02 -03006390 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02006391}
Johan Hedberga5c29682011-02-19 12:05:57 -03006392
Johan Hedberg744cf192011-11-08 20:40:14 +02006393int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg39adbff2014-03-20 08:18:14 +02006394 u8 link_type, u8 addr_type, u32 value,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006395 u8 confirm_hint)
Johan Hedberga5c29682011-02-19 12:05:57 -03006396{
6397 struct mgmt_ev_user_confirm_request ev;
6398
Johan Hedberg744cf192011-11-08 20:40:14 +02006399 BT_DBG("%s", hdev->name);
Johan Hedberga5c29682011-02-19 12:05:57 -03006400
Johan Hedberg272d90d2012-02-09 15:26:12 +02006401 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03006402 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberg55bc1a32011-04-28 11:28:56 -07006403 ev.confirm_hint = confirm_hint;
Johan Hedberg39adbff2014-03-20 08:18:14 +02006404 ev.value = cpu_to_le32(value);
Johan Hedberga5c29682011-02-19 12:05:57 -03006405
Johan Hedberg744cf192011-11-08 20:40:14 +02006406 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006407 NULL);
Johan Hedberga5c29682011-02-19 12:05:57 -03006408}
6409
Johan Hedberg272d90d2012-02-09 15:26:12 +02006410int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03006411 u8 link_type, u8 addr_type)
Brian Gix604086b2011-11-23 08:28:33 -08006412{
6413 struct mgmt_ev_user_passkey_request ev;
6414
6415 BT_DBG("%s", hdev->name);
6416
Johan Hedberg272d90d2012-02-09 15:26:12 +02006417 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03006418 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Brian Gix604086b2011-11-23 08:28:33 -08006419
6420 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006421 NULL);
Brian Gix604086b2011-11-23 08:28:33 -08006422}
6423
Brian Gix0df4c182011-11-16 13:53:13 -08006424static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03006425 u8 link_type, u8 addr_type, u8 status,
6426 u8 opcode)
Johan Hedberga5c29682011-02-19 12:05:57 -03006427{
6428 struct pending_cmd *cmd;
6429 struct mgmt_rp_user_confirm_reply rp;
6430 int err;
6431
Johan Hedberg2e58ef32011-11-08 20:40:15 +02006432 cmd = mgmt_pending_find(opcode, hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03006433 if (!cmd)
6434 return -ENOENT;
6435
Johan Hedberg272d90d2012-02-09 15:26:12 +02006436 bacpy(&rp.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03006437 rp.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergaee9b212012-02-18 15:07:59 +02006438 err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006439 &rp, sizeof(rp));
Johan Hedberga5c29682011-02-19 12:05:57 -03006440
Johan Hedberga664b5b2011-02-19 12:06:02 -03006441 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03006442
6443 return err;
6444}
6445
Johan Hedberg744cf192011-11-08 20:40:14 +02006446int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006447 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03006448{
Johan Hedberg272d90d2012-02-09 15:26:12 +02006449 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006450 status, MGMT_OP_USER_CONFIRM_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03006451}
6452
Johan Hedberg272d90d2012-02-09 15:26:12 +02006453int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006454 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03006455{
Johan Hedberg272d90d2012-02-09 15:26:12 +02006456 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03006457 status,
6458 MGMT_OP_USER_CONFIRM_NEG_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03006459}
Johan Hedberg2a611692011-02-19 12:06:00 -03006460
Brian Gix604086b2011-11-23 08:28:33 -08006461int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006462 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08006463{
Johan Hedberg272d90d2012-02-09 15:26:12 +02006464 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006465 status, MGMT_OP_USER_PASSKEY_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08006466}
6467
Johan Hedberg272d90d2012-02-09 15:26:12 +02006468int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006469 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08006470{
Johan Hedberg272d90d2012-02-09 15:26:12 +02006471 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03006472 status,
6473 MGMT_OP_USER_PASSKEY_NEG_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08006474}
6475
Johan Hedberg92a25252012-09-06 18:39:26 +03006476int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
6477 u8 link_type, u8 addr_type, u32 passkey,
6478 u8 entered)
6479{
6480 struct mgmt_ev_passkey_notify ev;
6481
6482 BT_DBG("%s", hdev->name);
6483
6484 bacpy(&ev.addr.bdaddr, bdaddr);
6485 ev.addr.type = link_to_bdaddr(link_type, addr_type);
6486 ev.passkey = __cpu_to_le32(passkey);
6487 ev.entered = entered;
6488
6489 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
6490}
6491
Marcel Holtmanne5460992013-10-15 14:26:23 -07006492void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
6493 u8 addr_type, u8 status)
Johan Hedberg2a611692011-02-19 12:06:00 -03006494{
6495 struct mgmt_ev_auth_failed ev;
6496
Johan Hedbergbab73cb2012-02-09 16:07:29 +02006497 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03006498 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergca69b792011-11-11 18:10:00 +02006499 ev.status = mgmt_status(status);
Johan Hedberg2a611692011-02-19 12:06:00 -03006500
Marcel Holtmanne5460992013-10-15 14:26:23 -07006501 mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg2a611692011-02-19 12:06:00 -03006502}
Johan Hedbergb312b1612011-03-16 14:29:37 +02006503
Marcel Holtmann464996a2013-10-15 14:26:24 -07006504void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02006505{
6506 struct cmd_lookup match = { NULL, hdev };
Marcel Holtmann464996a2013-10-15 14:26:24 -07006507 bool changed;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02006508
6509 if (status) {
6510 u8 mgmt_err = mgmt_status(status);
6511 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006512 cmd_status_rsp, &mgmt_err);
Marcel Holtmann464996a2013-10-15 14:26:24 -07006513 return;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02006514 }
6515
Marcel Holtmann464996a2013-10-15 14:26:24 -07006516 if (test_bit(HCI_AUTH, &hdev->flags))
6517 changed = !test_and_set_bit(HCI_LINK_SECURITY,
6518 &hdev->dev_flags);
6519 else
6520 changed = test_and_clear_bit(HCI_LINK_SECURITY,
6521 &hdev->dev_flags);
Johan Hedberg47990ea2012-02-22 11:58:37 +02006522
Johan Hedberg33ef95e2012-02-16 23:56:27 +02006523 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006524 &match);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02006525
Johan Hedberg47990ea2012-02-22 11:58:37 +02006526 if (changed)
Marcel Holtmann464996a2013-10-15 14:26:24 -07006527 new_settings(hdev, match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02006528
6529 if (match.sk)
6530 sock_put(match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02006531}
6532
Johan Hedberg890ea892013-03-15 17:06:52 -05006533static void clear_eir(struct hci_request *req)
Johan Hedbergcacaf522012-02-21 00:52:42 +02006534{
Johan Hedberg890ea892013-03-15 17:06:52 -05006535 struct hci_dev *hdev = req->hdev;
Johan Hedbergcacaf522012-02-21 00:52:42 +02006536 struct hci_cp_write_eir cp;
6537
Johan Hedberg976eb202012-10-24 21:12:01 +03006538 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05006539 return;
Johan Hedbergcacaf522012-02-21 00:52:42 +02006540
Johan Hedbergc80da272012-02-22 15:38:48 +02006541 memset(hdev->eir, 0, sizeof(hdev->eir));
6542
Johan Hedbergcacaf522012-02-21 00:52:42 +02006543 memset(&cp, 0, sizeof(cp));
6544
Johan Hedberg890ea892013-03-15 17:06:52 -05006545 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedbergcacaf522012-02-21 00:52:42 +02006546}
6547
Marcel Holtmann3e248562013-10-15 14:26:25 -07006548void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02006549{
6550 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg890ea892013-03-15 17:06:52 -05006551 struct hci_request req;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02006552 bool changed = false;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02006553
6554 if (status) {
6555 u8 mgmt_err = mgmt_status(status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02006556
6557 if (enable && test_and_clear_bit(HCI_SSP_ENABLED,
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07006558 &hdev->dev_flags)) {
6559 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
Marcel Holtmann3e248562013-10-15 14:26:25 -07006560 new_settings(hdev, NULL);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07006561 }
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02006562
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006563 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
6564 &mgmt_err);
Marcel Holtmann3e248562013-10-15 14:26:25 -07006565 return;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02006566 }
6567
6568 if (enable) {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07006569 changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02006570 } else {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07006571 changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
6572 if (!changed)
6573 changed = test_and_clear_bit(HCI_HS_ENABLED,
6574 &hdev->dev_flags);
6575 else
6576 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02006577 }
6578
6579 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
6580
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02006581 if (changed)
Marcel Holtmann3e248562013-10-15 14:26:25 -07006582 new_settings(hdev, match.sk);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02006583
Johan Hedberg5fc6ebb2012-02-22 15:10:59 +02006584 if (match.sk)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02006585 sock_put(match.sk);
6586
Johan Hedberg890ea892013-03-15 17:06:52 -05006587 hci_req_init(&req, hdev);
6588
Johan Hedberg37699722014-06-24 14:00:27 +03006589 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
6590 if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags))
6591 hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE,
6592 sizeof(enable), &enable);
Johan Hedberg890ea892013-03-15 17:06:52 -05006593 update_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03006594 } else {
Johan Hedberg890ea892013-03-15 17:06:52 -05006595 clear_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03006596 }
Johan Hedberg890ea892013-03-15 17:06:52 -05006597
6598 hci_req_run(&req, NULL);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02006599}
6600
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08006601void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
6602{
6603 struct cmd_lookup match = { NULL, hdev };
6604 bool changed = false;
6605
6606 if (status) {
6607 u8 mgmt_err = mgmt_status(status);
6608
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08006609 if (enable) {
6610 if (test_and_clear_bit(HCI_SC_ENABLED,
6611 &hdev->dev_flags))
6612 new_settings(hdev, NULL);
6613 clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
6614 }
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08006615
6616 mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev,
6617 cmd_status_rsp, &mgmt_err);
6618 return;
6619 }
6620
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08006621 if (enable) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08006622 changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08006623 } else {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08006624 changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08006625 clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
6626 }
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08006627
6628 mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev,
6629 settings_rsp, &match);
6630
6631 if (changed)
6632 new_settings(hdev, match.sk);
6633
6634 if (match.sk)
6635 sock_put(match.sk);
6636}
6637
Johan Hedberg92da6092013-03-15 17:06:55 -05006638static void sk_lookup(struct pending_cmd *cmd, void *data)
Johan Hedberg90e70452012-02-23 23:09:40 +02006639{
6640 struct cmd_lookup *match = data;
6641
Johan Hedberg90e70452012-02-23 23:09:40 +02006642 if (match->sk == NULL) {
6643 match->sk = cmd->sk;
6644 sock_hold(match->sk);
6645 }
Johan Hedberg90e70452012-02-23 23:09:40 +02006646}
6647
Marcel Holtmann4e1b0242013-10-15 14:26:26 -07006648void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
6649 u8 status)
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01006650{
Johan Hedberg90e70452012-02-23 23:09:40 +02006651 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01006652
Johan Hedberg92da6092013-03-15 17:06:55 -05006653 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
6654 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
6655 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
Johan Hedberg90e70452012-02-23 23:09:40 +02006656
6657 if (!status)
Marcel Holtmann4e1b0242013-10-15 14:26:26 -07006658 mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3,
6659 NULL);
Johan Hedberg90e70452012-02-23 23:09:40 +02006660
6661 if (match.sk)
6662 sock_put(match.sk);
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01006663}
6664
Marcel Holtmann7667da32013-10-15 14:26:27 -07006665void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
Johan Hedbergb312b1612011-03-16 14:29:37 +02006666{
Johan Hedbergb312b1612011-03-16 14:29:37 +02006667 struct mgmt_cp_set_local_name ev;
Johan Hedberg13928972013-03-15 17:07:00 -05006668 struct pending_cmd *cmd;
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02006669
Johan Hedberg13928972013-03-15 17:07:00 -05006670 if (status)
Marcel Holtmann7667da32013-10-15 14:26:27 -07006671 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02006672
6673 memset(&ev, 0, sizeof(ev));
6674 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02006675 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
Johan Hedbergb312b1612011-03-16 14:29:37 +02006676
Johan Hedberg2e58ef32011-11-08 20:40:15 +02006677 cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05006678 if (!cmd) {
6679 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
Johan Hedbergb312b1612011-03-16 14:29:37 +02006680
Johan Hedberg13928972013-03-15 17:07:00 -05006681 /* If this is a HCI command related to powering on the
6682 * HCI dev don't send any mgmt signals.
6683 */
6684 if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev))
Marcel Holtmann7667da32013-10-15 14:26:27 -07006685 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02006686 }
6687
Marcel Holtmann7667da32013-10-15 14:26:27 -07006688 mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
6689 cmd ? cmd->sk : NULL);
Johan Hedbergb312b1612011-03-16 14:29:37 +02006690}
Szymon Jancc35938b2011-03-22 13:12:21 +01006691
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08006692void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192,
6693 u8 *randomizer192, u8 *hash256,
6694 u8 *randomizer256, u8 status)
Szymon Jancc35938b2011-03-22 13:12:21 +01006695{
6696 struct pending_cmd *cmd;
Szymon Jancc35938b2011-03-22 13:12:21 +01006697
Johan Hedberg744cf192011-11-08 20:40:14 +02006698 BT_DBG("%s status %u", hdev->name, status);
Szymon Jancc35938b2011-03-22 13:12:21 +01006699
Johan Hedberg2e58ef32011-11-08 20:40:15 +02006700 cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01006701 if (!cmd)
Marcel Holtmann3edaf092013-10-15 14:26:28 -07006702 return;
Szymon Jancc35938b2011-03-22 13:12:21 +01006703
6704 if (status) {
Marcel Holtmann3edaf092013-10-15 14:26:28 -07006705 cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
6706 mgmt_status(status));
Szymon Jancc35938b2011-03-22 13:12:21 +01006707 } else {
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08006708 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) &&
6709 hash256 && randomizer256) {
6710 struct mgmt_rp_read_local_oob_ext_data rp;
Szymon Jancc35938b2011-03-22 13:12:21 +01006711
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08006712 memcpy(rp.hash192, hash192, sizeof(rp.hash192));
6713 memcpy(rp.randomizer192, randomizer192,
6714 sizeof(rp.randomizer192));
Szymon Jancc35938b2011-03-22 13:12:21 +01006715
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08006716 memcpy(rp.hash256, hash256, sizeof(rp.hash256));
6717 memcpy(rp.randomizer256, randomizer256,
6718 sizeof(rp.randomizer256));
6719
6720 cmd_complete(cmd->sk, hdev->id,
6721 MGMT_OP_READ_LOCAL_OOB_DATA, 0,
6722 &rp, sizeof(rp));
6723 } else {
6724 struct mgmt_rp_read_local_oob_data rp;
6725
6726 memcpy(rp.hash, hash192, sizeof(rp.hash));
6727 memcpy(rp.randomizer, randomizer192,
6728 sizeof(rp.randomizer));
6729
6730 cmd_complete(cmd->sk, hdev->id,
6731 MGMT_OP_READ_LOCAL_OOB_DATA, 0,
6732 &rp, sizeof(rp));
6733 }
Szymon Jancc35938b2011-03-22 13:12:21 +01006734 }
6735
6736 mgmt_pending_remove(cmd);
Szymon Jancc35938b2011-03-22 13:12:21 +01006737}
Johan Hedberge17acd42011-03-30 23:57:16 +03006738
Marcel Holtmann901801b2013-10-06 23:55:51 -07006739void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
Marcel Holtmannaf589252014-07-01 14:11:20 +02006740 u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
6741 u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
Johan Hedberge17acd42011-03-30 23:57:16 +03006742{
Johan Hedberge319d2e2012-01-15 19:51:59 +02006743 char buf[512];
6744 struct mgmt_ev_device_found *ev = (void *) buf;
Johan Hedberg1dc06092012-01-15 21:01:23 +02006745 size_t ev_size;
Johan Hedberge17acd42011-03-30 23:57:16 +03006746
Johan Hedberg75ce2082014-07-02 22:42:01 +03006747 /* Don't send events for a non-kernel initiated discovery. With
6748 * LE one exception is if we have pend_le_reports > 0 in which
6749 * case we're doing passive scanning and want these events.
6750 */
6751 if (!hci_discovery_active(hdev)) {
6752 if (link_type == ACL_LINK)
6753 return;
Johan Hedberg66f84552014-07-04 12:37:18 +03006754 if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports))
Johan Hedberg75ce2082014-07-02 22:42:01 +03006755 return;
6756 }
Andre Guedes12602d02013-04-30 15:29:40 -03006757
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02006758 /* Make sure that the buffer is big enough. The 5 extra bytes
6759 * are for the potential CoD field.
6760 */
6761 if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf))
Marcel Holtmann901801b2013-10-06 23:55:51 -07006762 return;
Andre Guedes7d262f82012-01-10 18:20:49 -03006763
Johan Hedberg1dc06092012-01-15 21:01:23 +02006764 memset(buf, 0, sizeof(buf));
6765
Johan Hedberg841c5642014-07-07 12:45:54 +03006766 bacpy(&ev->addr.bdaddr, bdaddr);
6767 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberge319d2e2012-01-15 19:51:59 +02006768 ev->rssi = rssi;
Marcel Holtmannaf589252014-07-01 14:11:20 +02006769 ev->flags = cpu_to_le32(flags);
Johan Hedberge17acd42011-03-30 23:57:16 +03006770
Johan Hedberg1dc06092012-01-15 21:01:23 +02006771 if (eir_len > 0)
Johan Hedberge319d2e2012-01-15 19:51:59 +02006772 memcpy(ev->eir, eir, eir_len);
Johan Hedberge17acd42011-03-30 23:57:16 +03006773
Johan Hedberg1dc06092012-01-15 21:01:23 +02006774 if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV))
6775 eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006776 dev_class, 3);
Johan Hedberg1dc06092012-01-15 21:01:23 +02006777
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02006778 if (scan_rsp_len > 0)
6779 memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len);
6780
6781 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
6782 ev_size = sizeof(*ev) + eir_len + scan_rsp_len;
Andre Guedesf8523592011-09-09 18:56:26 -03006783
Marcel Holtmann901801b2013-10-06 23:55:51 -07006784 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL);
Johan Hedberge17acd42011-03-30 23:57:16 +03006785}
Johan Hedberga88a9652011-03-30 13:18:12 +03006786
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07006787void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
6788 u8 addr_type, s8 rssi, u8 *name, u8 name_len)
Johan Hedberga88a9652011-03-30 13:18:12 +03006789{
Johan Hedbergb644ba32012-01-17 21:48:47 +02006790 struct mgmt_ev_device_found *ev;
6791 char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2];
6792 u16 eir_len;
Johan Hedberga88a9652011-03-30 13:18:12 +03006793
Johan Hedbergb644ba32012-01-17 21:48:47 +02006794 ev = (struct mgmt_ev_device_found *) buf;
Johan Hedberga88a9652011-03-30 13:18:12 +03006795
Johan Hedbergb644ba32012-01-17 21:48:47 +02006796 memset(buf, 0, sizeof(buf));
Johan Hedberga88a9652011-03-30 13:18:12 +03006797
Johan Hedbergb644ba32012-01-17 21:48:47 +02006798 bacpy(&ev->addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03006799 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergb644ba32012-01-17 21:48:47 +02006800 ev->rssi = rssi;
6801
6802 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006803 name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02006804
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02006805 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02006806
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07006807 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL);
Johan Hedberga88a9652011-03-30 13:18:12 +03006808}
Johan Hedberg314b2382011-04-27 10:29:57 -04006809
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07006810void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
Johan Hedberg314b2382011-04-27 10:29:57 -04006811{
Johan Hedbergf963e8e2012-02-20 23:30:44 +02006812 struct mgmt_ev_discovering ev;
Johan Hedberg164a6e72011-11-01 17:06:44 +02006813 struct pending_cmd *cmd;
6814
Andre Guedes343fb142011-11-22 17:14:19 -03006815 BT_DBG("%s discovering %u", hdev->name, discovering);
6816
Johan Hedberg164a6e72011-11-01 17:06:44 +02006817 if (discovering)
Johan Hedberg2e58ef32011-11-08 20:40:15 +02006818 cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev);
Johan Hedberg164a6e72011-11-01 17:06:44 +02006819 else
Johan Hedberg2e58ef32011-11-08 20:40:15 +02006820 cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
Johan Hedberg164a6e72011-11-01 17:06:44 +02006821
6822 if (cmd != NULL) {
Johan Hedbergf808e162012-02-19 12:52:07 +02006823 u8 type = hdev->discovery.type;
6824
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006825 cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type,
6826 sizeof(type));
Johan Hedberg164a6e72011-11-01 17:06:44 +02006827 mgmt_pending_remove(cmd);
6828 }
6829
Johan Hedbergf963e8e2012-02-20 23:30:44 +02006830 memset(&ev, 0, sizeof(ev));
6831 ev.type = hdev->discovery.type;
6832 ev.discovering = discovering;
6833
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07006834 mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg314b2382011-04-27 10:29:57 -04006835}
Antti Julku5e762442011-08-25 16:48:02 +03006836
Marcel Holtmann5976e602013-10-06 04:08:14 -07006837static void adv_enable_complete(struct hci_dev *hdev, u8 status)
6838{
6839 BT_DBG("%s status %u", hdev->name, status);
Marcel Holtmann5976e602013-10-06 04:08:14 -07006840}
6841
6842void mgmt_reenable_advertising(struct hci_dev *hdev)
6843{
6844 struct hci_request req;
6845
Marcel Holtmann5976e602013-10-06 04:08:14 -07006846 if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags))
6847 return;
6848
6849 hci_req_init(&req, hdev);
6850 enable_advertising(&req);
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03006851 hci_req_run(&req, adv_enable_complete);
Marcel Holtmann5976e602013-10-06 04:08:14 -07006852}