blob: 1b528dea98444ec3c85079199b1ab985340cdfe9 [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
Johan Hedberg0857dd32014-12-19 13:40:20 +020035#include "hci_request.h"
Marcel Holtmannac4b7232013-10-10 14:54:16 -070036#include "smp.h"
Johan Hedberg03811012010-12-08 00:21:06 +020037
Johan Hedberg2da9c552012-02-17 14:39:28 +020038#define MGMT_VERSION 1
Marcel Holtmann854bda12014-12-03 19:52:43 +010039#define MGMT_REVISION 8
Johan Hedberg02d98122010-12-13 21:07:04 +020040
Johan Hedberge70bb2e2012-02-13 16:59:33 +020041static const u16 mgmt_commands[] = {
42 MGMT_OP_READ_INDEX_LIST,
43 MGMT_OP_READ_INFO,
44 MGMT_OP_SET_POWERED,
45 MGMT_OP_SET_DISCOVERABLE,
46 MGMT_OP_SET_CONNECTABLE,
47 MGMT_OP_SET_FAST_CONNECTABLE,
Johan Hedbergb2939472014-07-30 09:22:23 +030048 MGMT_OP_SET_BONDABLE,
Johan Hedberge70bb2e2012-02-13 16:59:33 +020049 MGMT_OP_SET_LINK_SECURITY,
50 MGMT_OP_SET_SSP,
51 MGMT_OP_SET_HS,
52 MGMT_OP_SET_LE,
53 MGMT_OP_SET_DEV_CLASS,
54 MGMT_OP_SET_LOCAL_NAME,
55 MGMT_OP_ADD_UUID,
56 MGMT_OP_REMOVE_UUID,
57 MGMT_OP_LOAD_LINK_KEYS,
58 MGMT_OP_LOAD_LONG_TERM_KEYS,
59 MGMT_OP_DISCONNECT,
60 MGMT_OP_GET_CONNECTIONS,
61 MGMT_OP_PIN_CODE_REPLY,
62 MGMT_OP_PIN_CODE_NEG_REPLY,
63 MGMT_OP_SET_IO_CAPABILITY,
64 MGMT_OP_PAIR_DEVICE,
65 MGMT_OP_CANCEL_PAIR_DEVICE,
66 MGMT_OP_UNPAIR_DEVICE,
67 MGMT_OP_USER_CONFIRM_REPLY,
68 MGMT_OP_USER_CONFIRM_NEG_REPLY,
69 MGMT_OP_USER_PASSKEY_REPLY,
70 MGMT_OP_USER_PASSKEY_NEG_REPLY,
71 MGMT_OP_READ_LOCAL_OOB_DATA,
72 MGMT_OP_ADD_REMOTE_OOB_DATA,
73 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
74 MGMT_OP_START_DISCOVERY,
75 MGMT_OP_STOP_DISCOVERY,
76 MGMT_OP_CONFIRM_NAME,
77 MGMT_OP_BLOCK_DEVICE,
78 MGMT_OP_UNBLOCK_DEVICE,
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -070079 MGMT_OP_SET_DEVICE_ID,
Johan Hedberg4375f102013-09-25 13:26:10 +030080 MGMT_OP_SET_ADVERTISING,
Johan Hedberg0663ca22013-10-02 13:43:14 +030081 MGMT_OP_SET_BREDR,
Marcel Holtmannd13eafc2013-10-02 04:41:30 -070082 MGMT_OP_SET_STATIC_ADDRESS,
Marcel Holtmann7f72134e2013-10-11 14:44:58 -070083 MGMT_OP_SET_SCAN_PARAMS,
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -080084 MGMT_OP_SET_SECURE_CONN,
Marcel Holtmann4e39ac82014-01-31 11:55:22 -080085 MGMT_OP_SET_DEBUG_KEYS,
Johan Hedberg62b04cd2014-02-23 19:42:27 +020086 MGMT_OP_SET_PRIVACY,
Johan Hedberg41edf162014-02-18 10:19:35 +020087 MGMT_OP_LOAD_IRKS,
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +020088 MGMT_OP_GET_CONN_INFO,
Johan Hedberg95868422014-06-28 17:54:07 +030089 MGMT_OP_GET_CLOCK_INFO,
Marcel Holtmann2faade52014-06-29 19:44:03 +020090 MGMT_OP_ADD_DEVICE,
91 MGMT_OP_REMOVE_DEVICE,
Johan Hedberga26f3dc2014-07-02 17:37:29 +030092 MGMT_OP_LOAD_CONN_PARAM,
Marcel Holtmann73d1df22014-07-02 22:10:52 +020093 MGMT_OP_READ_UNCONF_INDEX_LIST,
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +020094 MGMT_OP_READ_CONFIG_INFO,
Marcel Holtmanndbece372014-07-04 18:11:55 +020095 MGMT_OP_SET_EXTERNAL_CONFIG,
Marcel Holtmann9713c172014-07-06 12:11:15 +020096 MGMT_OP_SET_PUBLIC_ADDRESS,
Jakub Pawlowski66ea9422014-12-05 10:55:59 +010097 MGMT_OP_START_SERVICE_DISCOVERY,
Johan Hedberge70bb2e2012-02-13 16:59:33 +020098};
99
100static const u16 mgmt_events[] = {
101 MGMT_EV_CONTROLLER_ERROR,
102 MGMT_EV_INDEX_ADDED,
103 MGMT_EV_INDEX_REMOVED,
104 MGMT_EV_NEW_SETTINGS,
105 MGMT_EV_CLASS_OF_DEV_CHANGED,
106 MGMT_EV_LOCAL_NAME_CHANGED,
107 MGMT_EV_NEW_LINK_KEY,
108 MGMT_EV_NEW_LONG_TERM_KEY,
109 MGMT_EV_DEVICE_CONNECTED,
110 MGMT_EV_DEVICE_DISCONNECTED,
111 MGMT_EV_CONNECT_FAILED,
112 MGMT_EV_PIN_CODE_REQUEST,
113 MGMT_EV_USER_CONFIRM_REQUEST,
114 MGMT_EV_USER_PASSKEY_REQUEST,
115 MGMT_EV_AUTH_FAILED,
116 MGMT_EV_DEVICE_FOUND,
117 MGMT_EV_DISCOVERING,
118 MGMT_EV_DEVICE_BLOCKED,
119 MGMT_EV_DEVICE_UNBLOCKED,
120 MGMT_EV_DEVICE_UNPAIRED,
Johan Hedberg92a25252012-09-06 18:39:26 +0300121 MGMT_EV_PASSKEY_NOTIFY,
Marcel Holtmann1b60ef22014-02-21 21:35:30 -0800122 MGMT_EV_NEW_IRK,
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700123 MGMT_EV_NEW_CSRK,
Marcel Holtmann8afef092014-06-29 22:28:34 +0200124 MGMT_EV_DEVICE_ADDED,
125 MGMT_EV_DEVICE_REMOVED,
Andre Guedesffb5a8272014-07-01 18:10:11 -0300126 MGMT_EV_NEW_CONN_PARAM,
Marcel Holtmann0602a8a2014-07-02 21:30:54 +0200127 MGMT_EV_UNCONF_INDEX_ADDED,
Marcel Holtmannedd38962014-07-02 21:30:55 +0200128 MGMT_EV_UNCONF_INDEX_REMOVED,
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200129 MGMT_EV_NEW_CONFIG_OPTIONS,
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200130};
131
Marcel Holtmann17b02e62012-03-01 14:32:37 -0800132#define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)
Johan Hedberg7d785252011-12-15 00:47:39 +0200133
Johan Hedbergd25b78e2015-01-27 12:55:52 +0200134#define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
135 "\x00\x00\x00\x00\x00\x00\x00\x00"
136
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200137struct pending_cmd {
138 struct list_head list;
Johan Hedbergfc2f4b12011-11-09 13:58:56 +0200139 u16 opcode;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200140 int index;
Szymon Jancc68fb7f2011-03-22 13:12:19 +0100141 void *param;
Johan Hedberg323b0b82014-12-05 13:36:01 +0200142 size_t param_len;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200143 struct sock *sk;
Johan Hedberge9a416b2011-02-19 12:05:56 -0300144 void *user_data;
Johan Hedberg9df74652014-12-19 22:26:03 +0200145 int (*cmd_complete)(struct pending_cmd *cmd, u8 status);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200146};
147
Johan Hedbergca69b792011-11-11 18:10:00 +0200148/* HCI to MGMT error code conversion table */
149static u8 mgmt_status_table[] = {
150 MGMT_STATUS_SUCCESS,
151 MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */
152 MGMT_STATUS_NOT_CONNECTED, /* No Connection */
153 MGMT_STATUS_FAILED, /* Hardware Failure */
154 MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */
155 MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */
Johan Hedbergeadd6632014-01-13 17:15:53 +0200156 MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */
Johan Hedbergca69b792011-11-11 18:10:00 +0200157 MGMT_STATUS_NO_RESOURCES, /* Memory Full */
158 MGMT_STATUS_TIMEOUT, /* Connection Timeout */
159 MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */
160 MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */
161 MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */
162 MGMT_STATUS_BUSY, /* Command Disallowed */
163 MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */
164 MGMT_STATUS_REJECTED, /* Rejected Security */
165 MGMT_STATUS_REJECTED, /* Rejected Personal */
166 MGMT_STATUS_TIMEOUT, /* Host Timeout */
167 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */
168 MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */
169 MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */
170 MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */
171 MGMT_STATUS_DISCONNECTED, /* OE Power Off */
172 MGMT_STATUS_DISCONNECTED, /* Connection Terminated */
173 MGMT_STATUS_BUSY, /* Repeated Attempts */
174 MGMT_STATUS_REJECTED, /* Pairing Not Allowed */
175 MGMT_STATUS_FAILED, /* Unknown LMP PDU */
176 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */
177 MGMT_STATUS_REJECTED, /* SCO Offset Rejected */
178 MGMT_STATUS_REJECTED, /* SCO Interval Rejected */
179 MGMT_STATUS_REJECTED, /* Air Mode Rejected */
180 MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */
181 MGMT_STATUS_FAILED, /* Unspecified Error */
182 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */
183 MGMT_STATUS_FAILED, /* Role Change Not Allowed */
184 MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */
185 MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */
186 MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */
187 MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */
188 MGMT_STATUS_FAILED, /* Unit Link Key Used */
189 MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */
190 MGMT_STATUS_TIMEOUT, /* Instant Passed */
191 MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */
192 MGMT_STATUS_FAILED, /* Transaction Collision */
193 MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */
194 MGMT_STATUS_REJECTED, /* QoS Rejected */
195 MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */
196 MGMT_STATUS_REJECTED, /* Insufficient Security */
197 MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */
198 MGMT_STATUS_BUSY, /* Role Switch Pending */
199 MGMT_STATUS_FAILED, /* Slot Violation */
200 MGMT_STATUS_FAILED, /* Role Switch Failed */
201 MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */
202 MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */
203 MGMT_STATUS_BUSY, /* Host Busy Pairing */
204 MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */
205 MGMT_STATUS_BUSY, /* Controller Busy */
206 MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */
207 MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */
208 MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */
209 MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */
210 MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */
211};
212
213static u8 mgmt_status(u8 hci_status)
214{
215 if (hci_status < ARRAY_SIZE(mgmt_status_table))
216 return mgmt_status_table[hci_status];
217
218 return MGMT_STATUS_FAILED;
219}
220
Marcel Holtmann04c60f052014-07-04 19:06:22 +0200221static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len,
222 struct sock *skip_sk)
223{
224 struct sk_buff *skb;
225 struct mgmt_hdr *hdr;
226
227 skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL);
228 if (!skb)
229 return -ENOMEM;
230
231 hdr = (void *) skb_put(skb, sizeof(*hdr));
232 hdr->opcode = cpu_to_le16(event);
233 if (hdev)
234 hdr->index = cpu_to_le16(hdev->id);
235 else
236 hdr->index = cpu_to_le16(MGMT_INDEX_NONE);
237 hdr->len = cpu_to_le16(data_len);
238
239 if (data)
240 memcpy(skb_put(skb, data_len), data, data_len);
241
242 /* Time stamp */
243 __net_timestamp(skb);
244
245 hci_send_to_control(skb, skip_sk);
246 kfree_skb(skb);
247
248 return 0;
249}
250
Szymon Janc4e51eae2011-02-25 19:05:48 +0100251static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status)
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200252{
253 struct sk_buff *skb;
254 struct mgmt_hdr *hdr;
255 struct mgmt_ev_cmd_status *ev;
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300256 int err;
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200257
Szymon Janc34eb5252011-02-28 14:10:08 +0100258 BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status);
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200259
Andre Guedes790eff42012-06-07 19:05:46 -0300260 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL);
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200261 if (!skb)
262 return -ENOMEM;
263
264 hdr = (void *) skb_put(skb, sizeof(*hdr));
265
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700266 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS);
Szymon Janc4e51eae2011-02-25 19:05:48 +0100267 hdr->index = cpu_to_le16(index);
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200268 hdr->len = cpu_to_le16(sizeof(*ev));
269
270 ev = (void *) skb_put(skb, sizeof(*ev));
271 ev->status = status;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200272 ev->opcode = cpu_to_le16(cmd);
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200273
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300274 err = sock_queue_rcv_skb(sk, skb);
275 if (err < 0)
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200276 kfree_skb(skb);
277
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300278 return err;
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200279}
280
Johan Hedbergaee9b2182012-02-18 15:07:59 +0200281static int cmd_complete(struct sock *sk, u16 index, u16 cmd, u8 status,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300282 void *rp, size_t rp_len)
Johan Hedberg02d98122010-12-13 21:07:04 +0200283{
284 struct sk_buff *skb;
285 struct mgmt_hdr *hdr;
286 struct mgmt_ev_cmd_complete *ev;
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300287 int err;
Johan Hedberg02d98122010-12-13 21:07:04 +0200288
289 BT_DBG("sock %p", sk);
290
Andre Guedes790eff42012-06-07 19:05:46 -0300291 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL);
Johan Hedberg02d98122010-12-13 21:07:04 +0200292 if (!skb)
293 return -ENOMEM;
294
295 hdr = (void *) skb_put(skb, sizeof(*hdr));
Johan Hedberga38528f2011-01-22 06:46:43 +0200296
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700297 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE);
Szymon Janc4e51eae2011-02-25 19:05:48 +0100298 hdr->index = cpu_to_le16(index);
Johan Hedberga38528f2011-01-22 06:46:43 +0200299 hdr->len = cpu_to_le16(sizeof(*ev) + rp_len);
Johan Hedberg02d98122010-12-13 21:07:04 +0200300
Johan Hedberga38528f2011-01-22 06:46:43 +0200301 ev = (void *) skb_put(skb, sizeof(*ev) + rp_len);
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200302 ev->opcode = cpu_to_le16(cmd);
Johan Hedbergaee9b2182012-02-18 15:07:59 +0200303 ev->status = status;
Szymon Janc8020c162011-02-28 14:09:50 +0100304
305 if (rp)
306 memcpy(ev->data, rp, rp_len);
Johan Hedberg02d98122010-12-13 21:07:04 +0200307
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300308 err = sock_queue_rcv_skb(sk, skb);
309 if (err < 0)
Johan Hedberg02d98122010-12-13 21:07:04 +0200310 kfree_skb(skb);
311
Marcel Holtmanne5f0e152012-02-22 11:59:01 +0100312 return err;
Johan Hedberg02d98122010-12-13 21:07:04 +0200313}
314
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300315static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
316 u16 data_len)
Johan Hedberga38528f2011-01-22 06:46:43 +0200317{
318 struct mgmt_rp_read_version rp;
319
320 BT_DBG("sock %p", sk);
321
322 rp.version = MGMT_VERSION;
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700323 rp.revision = cpu_to_le16(MGMT_REVISION);
Johan Hedberga38528f2011-01-22 06:46:43 +0200324
Johan Hedbergaee9b2182012-02-18 15:07:59 +0200325 return cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, &rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300326 sizeof(rp));
Johan Hedberga38528f2011-01-22 06:46:43 +0200327}
328
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300329static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
330 u16 data_len)
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200331{
332 struct mgmt_rp_read_commands *rp;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200333 const u16 num_commands = ARRAY_SIZE(mgmt_commands);
334 const u16 num_events = ARRAY_SIZE(mgmt_events);
Andrei Emeltchenko2e3c35e2012-03-14 18:54:15 +0200335 __le16 *opcode;
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200336 size_t rp_size;
337 int i, err;
338
339 BT_DBG("sock %p", sk);
340
341 rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16));
342
343 rp = kmalloc(rp_size, GFP_KERNEL);
344 if (!rp)
345 return -ENOMEM;
346
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700347 rp->num_commands = cpu_to_le16(num_commands);
348 rp->num_events = cpu_to_le16(num_events);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200349
350 for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++)
351 put_unaligned_le16(mgmt_commands[i], opcode);
352
353 for (i = 0; i < num_events; i++, opcode++)
354 put_unaligned_le16(mgmt_events[i], opcode);
355
Johan Hedbergaee9b2182012-02-18 15:07:59 +0200356 err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300357 rp_size);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200358 kfree(rp);
359
360 return err;
361}
362
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300363static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
364 u16 data_len)
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200365{
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200366 struct mgmt_rp_read_index_list *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200367 struct hci_dev *d;
Johan Hedberga38528f2011-01-22 06:46:43 +0200368 size_t rp_len;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200369 u16 count;
Johan Hedberg476e44c2012-10-19 20:10:46 +0300370 int err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200371
372 BT_DBG("sock %p", sk);
373
374 read_lock(&hci_dev_list_lock);
375
376 count = 0;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +0300377 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200378 if (d->dev_type == HCI_BREDR &&
379 !test_bit(HCI_UNCONFIGURED, &d->dev_flags))
Marcel Holtmann1514b892013-10-06 08:25:01 -0700380 count++;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200381 }
382
Johan Hedberga38528f2011-01-22 06:46:43 +0200383 rp_len = sizeof(*rp) + (2 * count);
384 rp = kmalloc(rp_len, GFP_ATOMIC);
385 if (!rp) {
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100386 read_unlock(&hci_dev_list_lock);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200387 return -ENOMEM;
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100388 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200389
Johan Hedberg476e44c2012-10-19 20:10:46 +0300390 count = 0;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200391 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200392 if (test_bit(HCI_SETUP, &d->dev_flags) ||
Marcel Holtmannd603b762014-07-06 12:11:14 +0200393 test_bit(HCI_CONFIG, &d->dev_flags) ||
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200394 test_bit(HCI_USER_CHANNEL, &d->dev_flags))
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200395 continue;
396
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200397 /* Devices marked as raw-only are neither configured
398 * nor unconfigured controllers.
399 */
400 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
Marcel Holtmann0736cfa2013-08-26 21:40:51 -0700401 continue;
402
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200403 if (d->dev_type == HCI_BREDR &&
404 !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) {
Marcel Holtmann1514b892013-10-06 08:25:01 -0700405 rp->index[count++] = cpu_to_le16(d->id);
406 BT_DBG("Added hci%u", d->id);
407 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200408 }
409
Johan Hedberg476e44c2012-10-19 20:10:46 +0300410 rp->num_controllers = cpu_to_le16(count);
411 rp_len = sizeof(*rp) + (2 * count);
412
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200413 read_unlock(&hci_dev_list_lock);
414
Johan Hedbergaee9b2182012-02-18 15:07:59 +0200415 err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, 0, rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300416 rp_len);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200417
Johan Hedberga38528f2011-01-22 06:46:43 +0200418 kfree(rp);
419
420 return err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200421}
422
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200423static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
424 void *data, u16 data_len)
425{
426 struct mgmt_rp_read_unconf_index_list *rp;
427 struct hci_dev *d;
428 size_t rp_len;
429 u16 count;
430 int err;
431
432 BT_DBG("sock %p", sk);
433
434 read_lock(&hci_dev_list_lock);
435
436 count = 0;
437 list_for_each_entry(d, &hci_dev_list, list) {
438 if (d->dev_type == HCI_BREDR &&
439 test_bit(HCI_UNCONFIGURED, &d->dev_flags))
440 count++;
441 }
442
443 rp_len = sizeof(*rp) + (2 * count);
444 rp = kmalloc(rp_len, GFP_ATOMIC);
445 if (!rp) {
446 read_unlock(&hci_dev_list_lock);
447 return -ENOMEM;
448 }
449
450 count = 0;
451 list_for_each_entry(d, &hci_dev_list, list) {
452 if (test_bit(HCI_SETUP, &d->dev_flags) ||
Marcel Holtmannd603b762014-07-06 12:11:14 +0200453 test_bit(HCI_CONFIG, &d->dev_flags) ||
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200454 test_bit(HCI_USER_CHANNEL, &d->dev_flags))
455 continue;
456
457 /* Devices marked as raw-only are neither configured
458 * nor unconfigured controllers.
459 */
460 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
461 continue;
462
463 if (d->dev_type == HCI_BREDR &&
464 test_bit(HCI_UNCONFIGURED, &d->dev_flags)) {
465 rp->index[count++] = cpu_to_le16(d->id);
466 BT_DBG("Added hci%u", d->id);
467 }
468 }
469
470 rp->num_controllers = cpu_to_le16(count);
471 rp_len = sizeof(*rp) + (2 * count);
472
473 read_unlock(&hci_dev_list_lock);
474
475 err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_UNCONF_INDEX_LIST,
476 0, rp, rp_len);
477
478 kfree(rp);
479
480 return err;
481}
482
Marcel Holtmanndbece372014-07-04 18:11:55 +0200483static bool is_configured(struct hci_dev *hdev)
484{
485 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
486 !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags))
487 return false;
488
489 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
490 !bacmp(&hdev->public_addr, BDADDR_ANY))
491 return false;
492
493 return true;
494}
495
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200496static __le32 get_missing_options(struct hci_dev *hdev)
497{
498 u32 options = 0;
499
Marcel Holtmanndbece372014-07-04 18:11:55 +0200500 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
501 !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags))
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200502 options |= MGMT_OPTION_EXTERNAL_CONFIG;
503
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200504 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
505 !bacmp(&hdev->public_addr, BDADDR_ANY))
506 options |= MGMT_OPTION_PUBLIC_ADDRESS;
507
508 return cpu_to_le32(options);
509}
510
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200511static int new_options(struct hci_dev *hdev, struct sock *skip)
512{
513 __le32 options = get_missing_options(hdev);
514
515 return mgmt_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options,
516 sizeof(options), skip);
517}
518
Marcel Holtmanndbece372014-07-04 18:11:55 +0200519static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
520{
521 __le32 options = get_missing_options(hdev);
522
523 return cmd_complete(sk, hdev->id, opcode, 0, &options,
524 sizeof(options));
525}
526
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200527static int read_config_info(struct sock *sk, struct hci_dev *hdev,
528 void *data, u16 data_len)
529{
530 struct mgmt_rp_read_config_info rp;
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200531 u32 options = 0;
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200532
533 BT_DBG("sock %p %s", sk, hdev->name);
534
535 hci_dev_lock(hdev);
536
537 memset(&rp, 0, sizeof(rp));
538 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200539
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200540 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
541 options |= MGMT_OPTION_EXTERNAL_CONFIG;
542
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200543 if (hdev->set_bdaddr)
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200544 options |= MGMT_OPTION_PUBLIC_ADDRESS;
545
546 rp.supported_options = cpu_to_le32(options);
547 rp.missing_options = get_missing_options(hdev);
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200548
549 hci_dev_unlock(hdev);
550
551 return cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, &rp,
552 sizeof(rp));
553}
554
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200555static u32 get_supported_settings(struct hci_dev *hdev)
Johan Hedberg03811012010-12-08 00:21:06 +0200556{
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200557 u32 settings = 0;
Johan Hedberg03811012010-12-08 00:21:06 +0200558
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200559 settings |= MGMT_SETTING_POWERED;
Johan Hedbergb2939472014-07-30 09:22:23 +0300560 settings |= MGMT_SETTING_BONDABLE;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800561 settings |= MGMT_SETTING_DEBUG_KEYS;
Johan Hedberg3742abf2014-07-08 16:07:34 +0300562 settings |= MGMT_SETTING_CONNECTABLE;
563 settings |= MGMT_SETTING_DISCOVERABLE;
Johan Hedberg03811012010-12-08 00:21:06 +0200564
Andre Guedesed3fa312012-07-24 15:03:46 -0300565 if (lmp_bredr_capable(hdev)) {
Johan Hedberg1a47aee2013-03-15 17:07:06 -0500566 if (hdev->hci_ver >= BLUETOOTH_VER_1_2)
567 settings |= MGMT_SETTING_FAST_CONNECTABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200568 settings |= MGMT_SETTING_BREDR;
569 settings |= MGMT_SETTING_LINK_SECURITY;
Marcel Holtmanna82974c2013-10-11 09:48:47 -0700570
571 if (lmp_ssp_capable(hdev)) {
572 settings |= MGMT_SETTING_SSP;
573 settings |= MGMT_SETTING_HS;
574 }
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800575
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -0800576 if (lmp_sc_capable(hdev))
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800577 settings |= MGMT_SETTING_SECURE_CONN;
Marcel Holtmann848566b2013-10-01 22:59:22 -0700578 }
Marcel Holtmannd7b7e792012-02-20 21:47:49 +0100579
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300580 if (lmp_le_capable(hdev)) {
Marcel Holtmann9d428202012-05-03 07:12:31 +0200581 settings |= MGMT_SETTING_LE;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300582 settings |= MGMT_SETTING_ADVERTISING;
Johan Hedberga3209692014-05-26 11:23:35 +0300583 settings |= MGMT_SETTING_SECURE_CONN;
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200584 settings |= MGMT_SETTING_PRIVACY;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300585 }
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200586
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200587 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
588 hdev->set_bdaddr)
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200589 settings |= MGMT_SETTING_CONFIGURATION;
590
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200591 return settings;
592}
Johan Hedbergebc99fe2011-01-04 11:54:26 +0200593
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200594static u32 get_current_settings(struct hci_dev *hdev)
595{
596 u32 settings = 0;
Johan Hedbergdc4fe302011-03-16 14:29:36 +0200597
Johan Hedbergf1f0eb02012-02-21 17:15:41 +0200598 if (hdev_is_powered(hdev))
Marcel Holtmannf0d4b782012-02-21 12:14:25 +0100599 settings |= MGMT_SETTING_POWERED;
600
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200601 if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200602 settings |= MGMT_SETTING_CONNECTABLE;
603
Johan Hedberg1a4d3c42013-03-15 17:07:08 -0500604 if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags))
605 settings |= MGMT_SETTING_FAST_CONNECTABLE;
606
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200607 if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200608 settings |= MGMT_SETTING_DISCOVERABLE;
609
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300610 if (test_bit(HCI_BONDABLE, &hdev->dev_flags))
Johan Hedbergb2939472014-07-30 09:22:23 +0300611 settings |= MGMT_SETTING_BONDABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200612
Johan Hedberg56f87902013-10-02 13:43:13 +0300613 if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200614 settings |= MGMT_SETTING_BREDR;
615
Johan Hedberg06199cf2012-02-22 16:37:11 +0200616 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200617 settings |= MGMT_SETTING_LE;
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200618
Johan Hedberg47990ea2012-02-22 11:58:37 +0200619 if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200620 settings |= MGMT_SETTING_LINK_SECURITY;
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200621
Johan Hedberg84bde9d2012-01-25 14:21:06 +0200622 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200623 settings |= MGMT_SETTING_SSP;
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200624
Johan Hedberg6d80dfd2012-02-20 23:50:38 +0200625 if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags))
626 settings |= MGMT_SETTING_HS;
627
Johan Hedbergf3d3444a2013-10-05 12:01:04 +0200628 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300629 settings |= MGMT_SETTING_ADVERTISING;
630
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800631 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags))
632 settings |= MGMT_SETTING_SECURE_CONN;
633
Johan Hedberg0663b292014-06-24 13:15:50 +0300634 if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags))
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800635 settings |= MGMT_SETTING_DEBUG_KEYS;
636
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200637 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
638 settings |= MGMT_SETTING_PRIVACY;
639
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200640 return settings;
Johan Hedbergc542a062011-01-26 13:11:03 +0200641}
642
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300643#define PNP_INFO_SVCLASS_ID 0x1200
644
Johan Hedberg213202e2013-01-27 00:31:33 +0200645static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
646{
647 u8 *ptr = data, *uuids_start = NULL;
648 struct bt_uuid *uuid;
649
650 if (len < 4)
651 return ptr;
652
653 list_for_each_entry(uuid, &hdev->uuids, list) {
654 u16 uuid16;
655
656 if (uuid->size != 16)
657 continue;
658
659 uuid16 = get_unaligned_le16(&uuid->uuid[12]);
660 if (uuid16 < 0x1100)
661 continue;
662
663 if (uuid16 == PNP_INFO_SVCLASS_ID)
664 continue;
665
666 if (!uuids_start) {
667 uuids_start = ptr;
668 uuids_start[0] = 1;
669 uuids_start[1] = EIR_UUID16_ALL;
670 ptr += 2;
671 }
672
673 /* Stop if not enough space to put next UUID */
674 if ((ptr - data) + sizeof(u16) > len) {
675 uuids_start[1] = EIR_UUID16_SOME;
676 break;
677 }
678
679 *ptr++ = (uuid16 & 0x00ff);
680 *ptr++ = (uuid16 & 0xff00) >> 8;
681 uuids_start[0] += sizeof(uuid16);
682 }
683
684 return ptr;
685}
686
Johan Hedbergcdf19632013-01-27 00:31:34 +0200687static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
688{
689 u8 *ptr = data, *uuids_start = NULL;
690 struct bt_uuid *uuid;
691
692 if (len < 6)
693 return ptr;
694
695 list_for_each_entry(uuid, &hdev->uuids, list) {
696 if (uuid->size != 32)
697 continue;
698
699 if (!uuids_start) {
700 uuids_start = ptr;
701 uuids_start[0] = 1;
702 uuids_start[1] = EIR_UUID32_ALL;
703 ptr += 2;
704 }
705
706 /* Stop if not enough space to put next UUID */
707 if ((ptr - data) + sizeof(u32) > len) {
708 uuids_start[1] = EIR_UUID32_SOME;
709 break;
710 }
711
712 memcpy(ptr, &uuid->uuid[12], sizeof(u32));
713 ptr += sizeof(u32);
714 uuids_start[0] += sizeof(u32);
715 }
716
717 return ptr;
718}
719
Johan Hedbergc00d5752013-01-27 00:31:35 +0200720static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
721{
722 u8 *ptr = data, *uuids_start = NULL;
723 struct bt_uuid *uuid;
724
725 if (len < 18)
726 return ptr;
727
728 list_for_each_entry(uuid, &hdev->uuids, list) {
729 if (uuid->size != 128)
730 continue;
731
732 if (!uuids_start) {
733 uuids_start = ptr;
734 uuids_start[0] = 1;
735 uuids_start[1] = EIR_UUID128_ALL;
736 ptr += 2;
737 }
738
739 /* Stop if not enough space to put next UUID */
740 if ((ptr - data) + 16 > len) {
741 uuids_start[1] = EIR_UUID128_SOME;
742 break;
743 }
744
745 memcpy(ptr, uuid->uuid, 16);
746 ptr += 16;
747 uuids_start[0] += 16;
748 }
749
750 return ptr;
751}
752
Johan Hedbergeb2a8d22013-10-19 23:38:20 +0300753static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev)
754{
755 struct pending_cmd *cmd;
756
757 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
758 if (cmd->opcode == opcode)
759 return cmd;
760 }
761
762 return NULL;
763}
764
Johan Hedberg95868422014-06-28 17:54:07 +0300765static struct pending_cmd *mgmt_pending_find_data(u16 opcode,
766 struct hci_dev *hdev,
767 const void *data)
768{
769 struct pending_cmd *cmd;
770
771 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
772 if (cmd->user_data != data)
773 continue;
774 if (cmd->opcode == opcode)
775 return cmd;
776 }
777
778 return NULL;
779}
780
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700781static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
782{
Marcel Holtmann7a5f4992013-10-16 00:16:49 -0700783 u8 ad_len = 0;
784 size_t name_len;
785
786 name_len = strlen(hdev->dev_name);
787 if (name_len > 0) {
788 size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2;
789
790 if (name_len > max_len) {
791 name_len = max_len;
792 ptr[1] = EIR_NAME_SHORT;
793 } else
794 ptr[1] = EIR_NAME_COMPLETE;
795
796 ptr[0] = name_len + 1;
797
798 memcpy(ptr + 2, hdev->dev_name, name_len);
799
800 ad_len += (name_len + 2);
801 ptr += (name_len + 2);
802 }
803
804 return ad_len;
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700805}
806
807static void update_scan_rsp_data(struct hci_request *req)
808{
809 struct hci_dev *hdev = req->hdev;
810 struct hci_cp_le_set_scan_rsp_data cp;
811 u8 len;
812
Johan Hedberg7751ef12013-10-19 23:38:15 +0300813 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700814 return;
815
816 memset(&cp, 0, sizeof(cp));
817
818 len = create_scan_rsp_data(hdev, cp.data);
819
Johan Hedbergeb438b52013-10-16 15:31:07 +0300820 if (hdev->scan_rsp_data_len == len &&
821 memcmp(cp.data, hdev->scan_rsp_data, len) == 0)
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700822 return;
823
Johan Hedbergeb438b52013-10-16 15:31:07 +0300824 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
825 hdev->scan_rsp_data_len = len;
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700826
827 cp.length = len;
828
829 hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp);
830}
831
Johan Hedberg9a43e252013-10-20 19:00:07 +0300832static u8 get_adv_discov_flags(struct hci_dev *hdev)
833{
834 struct pending_cmd *cmd;
835
836 /* If there's a pending mgmt command the flags will not yet have
837 * their final values, so check for this first.
838 */
839 cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
840 if (cmd) {
841 struct mgmt_mode *cp = cmd->param;
842 if (cp->val == 0x01)
843 return LE_AD_GENERAL;
844 else if (cp->val == 0x02)
845 return LE_AD_LIMITED;
846 } else {
847 if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags))
848 return LE_AD_LIMITED;
849 else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
850 return LE_AD_GENERAL;
851 }
852
853 return 0;
854}
855
Marcel Holtmann46cad2e2013-10-16 00:16:46 -0700856static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr)
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700857{
858 u8 ad_len = 0, flags = 0;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700859
Johan Hedberg9a43e252013-10-20 19:00:07 +0300860 flags |= get_adv_discov_flags(hdev);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700861
Johan Hedberge8340042014-01-30 11:16:50 -0800862 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700863 flags |= LE_AD_NO_BREDR;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700864
865 if (flags) {
866 BT_DBG("adv flags 0x%02x", flags);
867
868 ptr[0] = 2;
869 ptr[1] = EIR_FLAGS;
870 ptr[2] = flags;
871
872 ad_len += 3;
873 ptr += 3;
874 }
875
876 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) {
877 ptr[0] = 2;
878 ptr[1] = EIR_TX_POWER;
879 ptr[2] = (u8) hdev->adv_tx_power;
880
881 ad_len += 3;
882 ptr += 3;
883 }
884
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700885 return ad_len;
886}
887
Marcel Holtmann5947f4b2013-10-16 00:16:50 -0700888static void update_adv_data(struct hci_request *req)
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700889{
890 struct hci_dev *hdev = req->hdev;
891 struct hci_cp_le_set_adv_data cp;
892 u8 len;
893
Johan Hedberg10994ce2013-10-19 23:38:16 +0300894 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700895 return;
896
897 memset(&cp, 0, sizeof(cp));
898
Marcel Holtmann46cad2e2013-10-16 00:16:46 -0700899 len = create_adv_data(hdev, cp.data);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700900
901 if (hdev->adv_data_len == len &&
902 memcmp(cp.data, hdev->adv_data, len) == 0)
903 return;
904
905 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
906 hdev->adv_data_len = len;
907
908 cp.length = len;
909
910 hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
911}
912
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300913int mgmt_update_adv_data(struct hci_dev *hdev)
914{
915 struct hci_request req;
916
917 hci_req_init(&req, hdev);
918 update_adv_data(&req);
919
920 return hci_req_run(&req, NULL);
921}
922
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300923static void create_eir(struct hci_dev *hdev, u8 *data)
924{
925 u8 *ptr = data;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300926 size_t name_len;
927
928 name_len = strlen(hdev->dev_name);
929
930 if (name_len > 0) {
931 /* EIR Data type */
932 if (name_len > 48) {
933 name_len = 48;
934 ptr[1] = EIR_NAME_SHORT;
935 } else
936 ptr[1] = EIR_NAME_COMPLETE;
937
938 /* EIR Data length */
939 ptr[0] = name_len + 1;
940
941 memcpy(ptr + 2, hdev->dev_name, name_len);
942
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300943 ptr += (name_len + 2);
944 }
945
Johan Hedbergbbaf4442012-11-08 01:22:59 +0100946 if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) {
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700947 ptr[0] = 2;
948 ptr[1] = EIR_TX_POWER;
949 ptr[2] = (u8) hdev->inq_tx_power;
950
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700951 ptr += 3;
952 }
953
Marcel Holtmann2b9be132012-03-11 19:32:12 -0700954 if (hdev->devid_source > 0) {
955 ptr[0] = 9;
956 ptr[1] = EIR_DEVICE_ID;
957
958 put_unaligned_le16(hdev->devid_source, ptr + 2);
959 put_unaligned_le16(hdev->devid_vendor, ptr + 4);
960 put_unaligned_le16(hdev->devid_product, ptr + 6);
961 put_unaligned_le16(hdev->devid_version, ptr + 8);
962
Marcel Holtmann2b9be132012-03-11 19:32:12 -0700963 ptr += 10;
964 }
965
Johan Hedberg213202e2013-01-27 00:31:33 +0200966 ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedbergcdf19632013-01-27 00:31:34 +0200967 ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedbergc00d5752013-01-27 00:31:35 +0200968 ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300969}
970
Johan Hedberg890ea892013-03-15 17:06:52 -0500971static void update_eir(struct hci_request *req)
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300972{
Johan Hedberg890ea892013-03-15 17:06:52 -0500973 struct hci_dev *hdev = req->hdev;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300974 struct hci_cp_write_eir cp;
975
Johan Hedberg504c8dc2012-02-23 13:30:41 +0200976 if (!hdev_is_powered(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -0500977 return;
Johan Hedberg7770c4a2012-02-22 22:06:38 +0200978
Johan Hedberg976eb202012-10-24 21:12:01 +0300979 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -0500980 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300981
Johan Hedberg84bde9d2012-01-25 14:21:06 +0200982 if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
Johan Hedberg890ea892013-03-15 17:06:52 -0500983 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300984
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200985 if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
Johan Hedberg890ea892013-03-15 17:06:52 -0500986 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300987
988 memset(&cp, 0, sizeof(cp));
989
990 create_eir(hdev, cp.data);
991
992 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
Johan Hedberg890ea892013-03-15 17:06:52 -0500993 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300994
995 memcpy(hdev->eir, cp.data, sizeof(cp.data));
996
Johan Hedberg890ea892013-03-15 17:06:52 -0500997 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300998}
999
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001000static u8 get_service_classes(struct hci_dev *hdev)
1001{
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -03001002 struct bt_uuid *uuid;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001003 u8 val = 0;
1004
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -03001005 list_for_each_entry(uuid, &hdev->uuids, list)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001006 val |= uuid->svc_hint;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001007
1008 return val;
1009}
1010
Johan Hedberg890ea892013-03-15 17:06:52 -05001011static void update_class(struct hci_request *req)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001012{
Johan Hedberg890ea892013-03-15 17:06:52 -05001013 struct hci_dev *hdev = req->hdev;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001014 u8 cod[3];
1015
1016 BT_DBG("%s", hdev->name);
1017
Johan Hedberg504c8dc2012-02-23 13:30:41 +02001018 if (!hdev_is_powered(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05001019 return;
Johan Hedberg7770c4a2012-02-22 22:06:38 +02001020
Johan Hedbergf87ea1d2013-10-19 23:38:17 +03001021 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
1022 return;
1023
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001024 if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
Johan Hedberg890ea892013-03-15 17:06:52 -05001025 return;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001026
1027 cod[0] = hdev->minor_class;
1028 cod[1] = hdev->major_class;
1029 cod[2] = get_service_classes(hdev);
1030
Marcel Holtmann6acd7db2013-10-15 06:33:53 -07001031 if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags))
1032 cod[1] |= 0x20;
1033
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001034 if (memcmp(cod, hdev->dev_class, 3) == 0)
Johan Hedberg890ea892013-03-15 17:06:52 -05001035 return;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001036
Johan Hedberg890ea892013-03-15 17:06:52 -05001037 hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001038}
1039
Johan Hedberga4858cb2014-02-25 19:56:31 +02001040static bool get_connectable(struct hci_dev *hdev)
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001041{
1042 struct pending_cmd *cmd;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001043
1044 /* If there's a pending mgmt command the flag will not yet have
1045 * it's final value, so check for this first.
1046 */
1047 cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
1048 if (cmd) {
1049 struct mgmt_mode *cp = cmd->param;
Johan Hedberga4858cb2014-02-25 19:56:31 +02001050 return cp->val;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001051 }
1052
Johan Hedberga4858cb2014-02-25 19:56:31 +02001053 return test_bit(HCI_CONNECTABLE, &hdev->dev_flags);
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001054}
1055
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001056static void disable_advertising(struct hci_request *req)
1057{
1058 u8 enable = 0x00;
1059
1060 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1061}
1062
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001063static void enable_advertising(struct hci_request *req)
1064{
1065 struct hci_dev *hdev = req->hdev;
1066 struct hci_cp_le_set_adv_param cp;
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001067 u8 own_addr_type, enable = 0x01;
Johan Hedberga4858cb2014-02-25 19:56:31 +02001068 bool connectable;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001069
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001070 if (hci_conn_num(hdev, LE_LINK) > 0)
1071 return;
1072
1073 if (test_bit(HCI_LE_ADV, &hdev->dev_flags))
1074 disable_advertising(req);
1075
Johan Hedberg5ce194c2014-07-08 15:07:49 +03001076 /* Clear the HCI_LE_ADV bit temporarily so that the
Johan Hedberg8d972502014-02-28 12:54:14 +02001077 * hci_update_random_address knows that it's safe to go ahead
1078 * and write a new random address. The flag will be set back on
1079 * as soon as the SET_ADV_ENABLE HCI command completes.
1080 */
Johan Hedberg5ce194c2014-07-08 15:07:49 +03001081 clear_bit(HCI_LE_ADV, &hdev->dev_flags);
Johan Hedberg8d972502014-02-28 12:54:14 +02001082
Johan Hedberga4858cb2014-02-25 19:56:31 +02001083 connectable = get_connectable(hdev);
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001084
Johan Hedberga4858cb2014-02-25 19:56:31 +02001085 /* Set require_privacy to true only when non-connectable
1086 * advertising is used. In that case it is fine to use a
1087 * non-resolvable private address.
1088 */
1089 if (hci_update_random_address(req, !connectable, &own_addr_type) < 0)
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001090 return;
1091
Marcel Holtmann41c90c12014-02-23 20:25:55 -08001092 memset(&cp, 0, sizeof(cp));
Georg Lukas628531c2014-07-26 13:59:57 +02001093 cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval);
1094 cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval);
Johan Hedberga4858cb2014-02-25 19:56:31 +02001095 cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND;
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001096 cp.own_address_type = own_addr_type;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001097 cp.channel_map = hdev->le_adv_channel_map;
1098
1099 hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
1100
1101 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1102}
1103
Johan Hedberg7d785252011-12-15 00:47:39 +02001104static void service_cache_off(struct work_struct *work)
1105{
1106 struct hci_dev *hdev = container_of(work, struct hci_dev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001107 service_cache.work);
Johan Hedberg890ea892013-03-15 17:06:52 -05001108 struct hci_request req;
Johan Hedberg7d785252011-12-15 00:47:39 +02001109
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001110 if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
Johan Hedberg7d785252011-12-15 00:47:39 +02001111 return;
1112
Johan Hedberg890ea892013-03-15 17:06:52 -05001113 hci_req_init(&req, hdev);
1114
Johan Hedberg7d785252011-12-15 00:47:39 +02001115 hci_dev_lock(hdev);
1116
Johan Hedberg890ea892013-03-15 17:06:52 -05001117 update_eir(&req);
1118 update_class(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02001119
1120 hci_dev_unlock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -05001121
1122 hci_req_run(&req, NULL);
Johan Hedberg7d785252011-12-15 00:47:39 +02001123}
1124
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001125static void rpa_expired(struct work_struct *work)
1126{
1127 struct hci_dev *hdev = container_of(work, struct hci_dev,
1128 rpa_expired.work);
1129 struct hci_request req;
1130
1131 BT_DBG("");
1132
1133 set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
1134
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001135 if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags))
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001136 return;
1137
1138 /* The generation of a new RPA and programming it into the
1139 * controller happens in the enable_advertising() function.
1140 */
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001141 hci_req_init(&req, hdev);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001142 enable_advertising(&req);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001143 hci_req_run(&req, NULL);
1144}
1145
Johan Hedberg6a919082012-02-28 06:17:26 +02001146static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
Johan Hedberg7d785252011-12-15 00:47:39 +02001147{
Johan Hedberg4f87da82012-03-02 19:55:56 +02001148 if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg6a919082012-02-28 06:17:26 +02001149 return;
1150
Johan Hedberg4f87da82012-03-02 19:55:56 +02001151 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001152 INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
Johan Hedberg7d785252011-12-15 00:47:39 +02001153
Johan Hedberg4f87da82012-03-02 19:55:56 +02001154 /* Non-mgmt controlled devices get this bit set
1155 * implicitly so that pairing works for them, however
1156 * for mgmt we require user-space to explicitly enable
1157 * it
1158 */
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001159 clear_bit(HCI_BONDABLE, &hdev->dev_flags);
Johan Hedberg7d785252011-12-15 00:47:39 +02001160}
1161
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02001162static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001163 void *data, u16 data_len)
Johan Hedberg03811012010-12-08 00:21:06 +02001164{
1165 struct mgmt_rp_read_info rp;
Johan Hedberg03811012010-12-08 00:21:06 +02001166
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001167 BT_DBG("sock %p %s", sk, hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001168
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001169 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001170
Johan Hedberg03811012010-12-08 00:21:06 +02001171 memset(&rp, 0, sizeof(rp));
1172
Johan Hedberg03811012010-12-08 00:21:06 +02001173 bacpy(&rp.bdaddr, &hdev->bdaddr);
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001174
1175 rp.version = hdev->hci_ver;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02001176 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001177
1178 rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
1179 rp.current_settings = cpu_to_le32(get_current_settings(hdev));
1180
1181 memcpy(rp.dev_class, hdev->dev_class, 3);
Johan Hedberg03811012010-12-08 00:21:06 +02001182
1183 memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
Johan Hedberg27fcc362012-02-22 21:46:22 +02001184 memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
Johan Hedberg03811012010-12-08 00:21:06 +02001185
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001186 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001187
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001188 return cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001189 sizeof(rp));
Johan Hedberg03811012010-12-08 00:21:06 +02001190}
1191
1192static void mgmt_pending_free(struct pending_cmd *cmd)
1193{
1194 sock_put(cmd->sk);
1195 kfree(cmd->param);
1196 kfree(cmd);
1197}
1198
1199static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001200 struct hci_dev *hdev, void *data,
1201 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001202{
1203 struct pending_cmd *cmd;
1204
Johan Hedbergfca20012014-06-28 17:54:05 +03001205 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
Johan Hedberg03811012010-12-08 00:21:06 +02001206 if (!cmd)
1207 return NULL;
1208
1209 cmd->opcode = opcode;
1210 cmd->index = hdev->id;
1211
Johan Hedberg323b0b82014-12-05 13:36:01 +02001212 cmd->param = kmemdup(data, len, GFP_KERNEL);
Johan Hedberg03811012010-12-08 00:21:06 +02001213 if (!cmd->param) {
1214 kfree(cmd);
1215 return NULL;
1216 }
1217
Johan Hedberg323b0b82014-12-05 13:36:01 +02001218 cmd->param_len = len;
Johan Hedberg03811012010-12-08 00:21:06 +02001219
1220 cmd->sk = sk;
1221 sock_hold(sk);
1222
1223 list_add(&cmd->list, &hdev->mgmt_pending);
1224
1225 return cmd;
1226}
1227
1228static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03001229 void (*cb)(struct pending_cmd *cmd,
1230 void *data),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001231 void *data)
Johan Hedberg03811012010-12-08 00:21:06 +02001232{
Andre Guedesa3d09352013-02-01 11:21:30 -03001233 struct pending_cmd *cmd, *tmp;
Johan Hedberg03811012010-12-08 00:21:06 +02001234
Andre Guedesa3d09352013-02-01 11:21:30 -03001235 list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) {
Johan Hedberg03811012010-12-08 00:21:06 +02001236 if (opcode > 0 && cmd->opcode != opcode)
1237 continue;
1238
1239 cb(cmd, data);
1240 }
1241}
1242
Johan Hedberg03811012010-12-08 00:21:06 +02001243static void mgmt_pending_remove(struct pending_cmd *cmd)
1244{
1245 list_del(&cmd->list);
1246 mgmt_pending_free(cmd);
1247}
1248
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001249static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
Johan Hedberg86805702011-11-11 16:18:52 +02001250{
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001251 __le32 settings = cpu_to_le32(get_current_settings(hdev));
Johan Hedberg86805702011-11-11 16:18:52 +02001252
Johan Hedbergaee9b2182012-02-18 15:07:59 +02001253 return cmd_complete(sk, hdev->id, opcode, 0, &settings,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001254 sizeof(settings));
Johan Hedberg86805702011-11-11 16:18:52 +02001255}
1256
Marcel Holtmann1904a852015-01-11 13:50:44 -08001257static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg8b064a32014-02-24 14:52:22 +02001258{
1259 BT_DBG("%s status 0x%02x", hdev->name, status);
1260
Johan Hedberga3172b72014-02-28 09:33:44 +02001261 if (hci_conn_count(hdev) == 0) {
1262 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001263 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberga3172b72014-02-28 09:33:44 +02001264 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001265}
1266
Johan Hedberg23a48092014-07-08 16:05:06 +03001267static bool hci_stop_discovery(struct hci_request *req)
Johan Hedberg21a60d32014-06-10 14:05:58 +03001268{
1269 struct hci_dev *hdev = req->hdev;
1270 struct hci_cp_remote_name_req_cancel cp;
1271 struct inquiry_entry *e;
1272
1273 switch (hdev->discovery.state) {
1274 case DISCOVERY_FINDING:
1275 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
1276 hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL);
1277 } else {
1278 cancel_delayed_work(&hdev->le_scan_disable);
1279 hci_req_add_le_scan_disable(req);
1280 }
1281
Johan Hedberg23a48092014-07-08 16:05:06 +03001282 return true;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001283
1284 case DISCOVERY_RESOLVING:
1285 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
1286 NAME_PENDING);
1287 if (!e)
Johan Hedberg23a48092014-07-08 16:05:06 +03001288 break;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001289
1290 bacpy(&cp.bdaddr, &e->data.bdaddr);
1291 hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp),
1292 &cp);
1293
Johan Hedberg23a48092014-07-08 16:05:06 +03001294 return true;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001295
1296 default:
1297 /* Passive scanning */
Johan Hedberg23a48092014-07-08 16:05:06 +03001298 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) {
Johan Hedberg21a60d32014-06-10 14:05:58 +03001299 hci_req_add_le_scan_disable(req);
Johan Hedberg23a48092014-07-08 16:05:06 +03001300 return true;
1301 }
1302
Johan Hedberg21a60d32014-06-10 14:05:58 +03001303 break;
1304 }
Johan Hedberg23a48092014-07-08 16:05:06 +03001305
1306 return false;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001307}
1308
Johan Hedberg8b064a32014-02-24 14:52:22 +02001309static int clean_up_hci_state(struct hci_dev *hdev)
1310{
1311 struct hci_request req;
1312 struct hci_conn *conn;
Johan Hedberg23a48092014-07-08 16:05:06 +03001313 bool discov_stopped;
1314 int err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001315
1316 hci_req_init(&req, hdev);
1317
1318 if (test_bit(HCI_ISCAN, &hdev->flags) ||
1319 test_bit(HCI_PSCAN, &hdev->flags)) {
1320 u8 scan = 0x00;
1321 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1322 }
1323
Johan Hedberg73e082f2014-07-08 15:07:51 +03001324 if (test_bit(HCI_LE_ADV, &hdev->dev_flags))
Johan Hedberg8b064a32014-02-24 14:52:22 +02001325 disable_advertising(&req);
1326
Johan Hedberg23a48092014-07-08 16:05:06 +03001327 discov_stopped = hci_stop_discovery(&req);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001328
1329 list_for_each_entry(conn, &hdev->conn_hash.list, list) {
1330 struct hci_cp_disconnect dc;
Johan Hedbergc9910d02014-02-27 14:35:12 +02001331 struct hci_cp_reject_conn_req rej;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001332
Johan Hedbergc9910d02014-02-27 14:35:12 +02001333 switch (conn->state) {
1334 case BT_CONNECTED:
1335 case BT_CONFIG:
1336 dc.handle = cpu_to_le16(conn->handle);
1337 dc.reason = 0x15; /* Terminated due to Power Off */
1338 hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc);
1339 break;
1340 case BT_CONNECT:
1341 if (conn->type == LE_LINK)
1342 hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL,
1343 0, NULL);
1344 else if (conn->type == ACL_LINK)
1345 hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL,
1346 6, &conn->dst);
1347 break;
1348 case BT_CONNECT2:
1349 bacpy(&rej.bdaddr, &conn->dst);
1350 rej.reason = 0x15; /* Terminated due to Power Off */
1351 if (conn->type == ACL_LINK)
1352 hci_req_add(&req, HCI_OP_REJECT_CONN_REQ,
1353 sizeof(rej), &rej);
1354 else if (conn->type == SCO_LINK)
1355 hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ,
1356 sizeof(rej), &rej);
1357 break;
1358 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001359 }
1360
Johan Hedberg23a48092014-07-08 16:05:06 +03001361 err = hci_req_run(&req, clean_up_hci_complete);
1362 if (!err && discov_stopped)
1363 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
1364
1365 return err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001366}
1367
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001368static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001369 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001370{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001371 struct mgmt_mode *cp = data;
Johan Hedberg03811012010-12-08 00:21:06 +02001372 struct pending_cmd *cmd;
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001373 int err;
Johan Hedberg03811012010-12-08 00:21:06 +02001374
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001375 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001376
Johan Hedberga7e80f22013-01-09 16:05:19 +02001377 if (cp->val != 0x00 && cp->val != 0x01)
1378 return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1379 MGMT_STATUS_INVALID_PARAMS);
1380
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001381 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001382
Johan Hedberg87b95ba2013-09-25 13:26:06 +03001383 if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) {
1384 err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1385 MGMT_STATUS_BUSY);
1386 goto failed;
1387 }
1388
Marcel Holtmannf0d4b782012-02-21 12:14:25 +01001389 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) {
1390 cancel_delayed_work(&hdev->power_off);
1391
1392 if (cp->val) {
Johan Hedberga1d70452013-01-09 15:29:40 +02001393 mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev,
1394 data, len);
1395 err = mgmt_powered(hdev, 1);
Marcel Holtmannf0d4b782012-02-21 12:14:25 +01001396 goto failed;
1397 }
1398 }
1399
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001400 if (!!cp->val == hdev_is_powered(hdev)) {
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001401 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001402 goto failed;
1403 }
1404
Johan Hedberg03811012010-12-08 00:21:06 +02001405 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
1406 if (!cmd) {
1407 err = -ENOMEM;
1408 goto failed;
1409 }
1410
Johan Hedberg8b064a32014-02-24 14:52:22 +02001411 if (cp->val) {
Johan Hedberg19202572013-01-14 22:33:51 +02001412 queue_work(hdev->req_workqueue, &hdev->power_on);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001413 err = 0;
1414 } else {
1415 /* Disconnect connections, stop scans, etc */
1416 err = clean_up_hci_state(hdev);
Johan Hedberga3172b72014-02-28 09:33:44 +02001417 if (!err)
1418 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
1419 HCI_POWER_OFF_TIMEOUT);
Johan Hedberg03811012010-12-08 00:21:06 +02001420
Johan Hedberg8b064a32014-02-24 14:52:22 +02001421 /* ENODATA means there were no HCI commands queued */
1422 if (err == -ENODATA) {
Johan Hedberga3172b72014-02-28 09:33:44 +02001423 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001424 queue_work(hdev->req_workqueue, &hdev->power_off.work);
1425 err = 0;
1426 }
1427 }
Johan Hedberg03811012010-12-08 00:21:06 +02001428
1429failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001430 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001431 return err;
1432}
1433
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001434static int new_settings(struct hci_dev *hdev, struct sock *skip)
1435{
1436 __le32 ev;
1437
1438 ev = cpu_to_le32(get_current_settings(hdev));
1439
1440 return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip);
1441}
1442
Johan Hedberg91a668b2014-07-09 13:28:26 +03001443int mgmt_new_settings(struct hci_dev *hdev)
1444{
1445 return new_settings(hdev, NULL);
1446}
1447
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001448struct cmd_lookup {
1449 struct sock *sk;
1450 struct hci_dev *hdev;
1451 u8 mgmt_status;
1452};
1453
1454static void settings_rsp(struct pending_cmd *cmd, void *data)
1455{
1456 struct cmd_lookup *match = data;
1457
1458 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
1459
1460 list_del(&cmd->list);
1461
1462 if (match->sk == NULL) {
1463 match->sk = cmd->sk;
1464 sock_hold(match->sk);
1465 }
1466
1467 mgmt_pending_free(cmd);
1468}
1469
1470static void cmd_status_rsp(struct pending_cmd *cmd, void *data)
1471{
1472 u8 *status = data;
1473
1474 cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
1475 mgmt_pending_remove(cmd);
1476}
1477
Johan Hedberg1b9b5ee2014-12-05 13:36:00 +02001478static void cmd_complete_rsp(struct pending_cmd *cmd, void *data)
1479{
1480 if (cmd->cmd_complete) {
1481 u8 *status = data;
1482
1483 cmd->cmd_complete(cmd, *status);
1484 mgmt_pending_remove(cmd);
1485
1486 return;
1487 }
1488
1489 cmd_status_rsp(cmd, data);
1490}
1491
Johan Hedberg9df74652014-12-19 22:26:03 +02001492static int generic_cmd_complete(struct pending_cmd *cmd, u8 status)
Johan Hedbergf5818c22014-12-05 13:36:02 +02001493{
Johan Hedberg9df74652014-12-19 22:26:03 +02001494 return cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1495 cmd->param, cmd->param_len);
Johan Hedbergf5818c22014-12-05 13:36:02 +02001496}
1497
Johan Hedberg9df74652014-12-19 22:26:03 +02001498static int addr_cmd_complete(struct pending_cmd *cmd, u8 status)
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001499{
Johan Hedberg9df74652014-12-19 22:26:03 +02001500 return cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, cmd->param,
1501 sizeof(struct mgmt_addr_info));
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001502}
1503
Johan Hedberge6fe7982013-10-02 15:45:22 +03001504static u8 mgmt_bredr_support(struct hci_dev *hdev)
1505{
1506 if (!lmp_bredr_capable(hdev))
1507 return MGMT_STATUS_NOT_SUPPORTED;
1508 else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
1509 return MGMT_STATUS_REJECTED;
1510 else
1511 return MGMT_STATUS_SUCCESS;
1512}
1513
1514static u8 mgmt_le_support(struct hci_dev *hdev)
1515{
1516 if (!lmp_le_capable(hdev))
1517 return MGMT_STATUS_NOT_SUPPORTED;
1518 else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
1519 return MGMT_STATUS_REJECTED;
1520 else
1521 return MGMT_STATUS_SUCCESS;
1522}
1523
Marcel Holtmann1904a852015-01-11 13:50:44 -08001524static void set_discoverable_complete(struct hci_dev *hdev, u8 status,
1525 u16 opcode)
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001526{
1527 struct pending_cmd *cmd;
1528 struct mgmt_mode *cp;
Marcel Holtmann970ba522013-10-15 06:33:57 -07001529 struct hci_request req;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001530 bool changed;
1531
1532 BT_DBG("status 0x%02x", status);
1533
1534 hci_dev_lock(hdev);
1535
1536 cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
1537 if (!cmd)
1538 goto unlock;
1539
1540 if (status) {
1541 u8 mgmt_err = mgmt_status(status);
1542 cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001543 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001544 goto remove_cmd;
1545 }
1546
1547 cp = cmd->param;
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001548 if (cp->val) {
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001549 changed = !test_and_set_bit(HCI_DISCOVERABLE,
1550 &hdev->dev_flags);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001551
1552 if (hdev->discov_timeout > 0) {
1553 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1554 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
1555 to);
1556 }
1557 } else {
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001558 changed = test_and_clear_bit(HCI_DISCOVERABLE,
1559 &hdev->dev_flags);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001560 }
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001561
1562 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1563
1564 if (changed)
1565 new_settings(hdev, cmd->sk);
1566
Marcel Holtmann970ba522013-10-15 06:33:57 -07001567 /* When the discoverable mode gets changed, make sure
1568 * that class of device has the limited discoverable
Johan Hedberg432df052014-08-01 11:13:31 +03001569 * bit correctly set. Also update page scan based on whitelist
1570 * entries.
Marcel Holtmann970ba522013-10-15 06:33:57 -07001571 */
1572 hci_req_init(&req, hdev);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02001573 __hci_update_page_scan(&req);
Marcel Holtmann970ba522013-10-15 06:33:57 -07001574 update_class(&req);
1575 hci_req_run(&req, NULL);
1576
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001577remove_cmd:
1578 mgmt_pending_remove(cmd);
1579
1580unlock:
1581 hci_dev_unlock(hdev);
1582}
1583
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001584static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001585 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001586{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001587 struct mgmt_cp_set_discoverable *cp = data;
Johan Hedberg03811012010-12-08 00:21:06 +02001588 struct pending_cmd *cmd;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001589 struct hci_request req;
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001590 u16 timeout;
Johan Hedberg9a43e252013-10-20 19:00:07 +03001591 u8 scan;
Johan Hedberg03811012010-12-08 00:21:06 +02001592 int err;
1593
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001594 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001595
Johan Hedberg9a43e252013-10-20 19:00:07 +03001596 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) &&
1597 !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
Johan Hedberg33c525c2012-10-24 21:11:58 +03001598 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Johan Hedberg9a43e252013-10-20 19:00:07 +03001599 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001600
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001601 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga7e80f22013-01-09 16:05:19 +02001602 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1603 MGMT_STATUS_INVALID_PARAMS);
1604
Marcel Holtmann1f350c82012-03-12 20:31:08 -07001605 timeout = __le16_to_cpu(cp->timeout);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001606
1607 /* Disabling discoverable requires that no timeout is set,
1608 * and enabling limited discoverable requires a timeout.
1609 */
1610 if ((cp->val == 0x00 && timeout > 0) ||
1611 (cp->val == 0x02 && timeout == 0))
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001612 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001613 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001614
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001615 hci_dev_lock(hdev);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001616
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001617 if (!hdev_is_powered(hdev) && timeout > 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001618 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001619 MGMT_STATUS_NOT_POWERED);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001620 goto failed;
1621 }
1622
1623 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03001624 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001625 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001626 MGMT_STATUS_BUSY);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001627 goto failed;
1628 }
1629
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001630 if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001631 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001632 MGMT_STATUS_REJECTED);
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001633 goto failed;
1634 }
1635
1636 if (!hdev_is_powered(hdev)) {
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001637 bool changed = false;
1638
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001639 /* Setting limited discoverable when powered off is
1640 * not a valid operation since it requires a timeout
1641 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1642 */
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001643 if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) {
1644 change_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
1645 changed = true;
1646 }
1647
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001648 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001649 if (err < 0)
1650 goto failed;
1651
1652 if (changed)
1653 err = new_settings(hdev, sk);
1654
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001655 goto failed;
1656 }
1657
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001658 /* If the current mode is the same, then just update the timeout
1659 * value with the new value. And if only the timeout gets updated,
1660 * then no need for any HCI transactions.
1661 */
1662 if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) &&
1663 (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE,
1664 &hdev->dev_flags)) {
Marcel Holtmann36261542013-10-15 08:28:51 -07001665 cancel_delayed_work(&hdev->discov_off);
1666 hdev->discov_timeout = timeout;
Marcel Holtmann955638e2012-02-22 18:21:00 +01001667
Marcel Holtmann36261542013-10-15 08:28:51 -07001668 if (cp->val && hdev->discov_timeout > 0) {
1669 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001670 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
Marcel Holtmann36261542013-10-15 08:28:51 -07001671 to);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001672 }
1673
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001674 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001675 goto failed;
1676 }
1677
1678 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
1679 if (!cmd) {
1680 err = -ENOMEM;
1681 goto failed;
1682 }
1683
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001684 /* Cancel any potential discoverable timeout that might be
1685 * still active and store new timeout value. The arming of
1686 * the timeout happens in the complete handler.
1687 */
1688 cancel_delayed_work(&hdev->discov_off);
1689 hdev->discov_timeout = timeout;
1690
Johan Hedbergb456f872013-10-19 23:38:22 +03001691 /* Limited discoverable mode */
1692 if (cp->val == 0x02)
1693 set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1694 else
1695 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1696
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001697 hci_req_init(&req, hdev);
1698
Johan Hedberg9a43e252013-10-20 19:00:07 +03001699 /* The procedure for LE-only controllers is much simpler - just
1700 * update the advertising data.
1701 */
1702 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
1703 goto update_ad;
1704
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001705 scan = SCAN_PAGE;
1706
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001707 if (cp->val) {
1708 struct hci_cp_write_current_iac_lap hci_cp;
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001709
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001710 if (cp->val == 0x02) {
1711 /* Limited discoverable mode */
Marcel Holtmann33337dc2013-10-23 08:28:01 -07001712 hci_cp.num_iac = min_t(u8, hdev->num_iac, 2);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001713 hci_cp.iac_lap[0] = 0x00; /* LIAC */
1714 hci_cp.iac_lap[1] = 0x8b;
1715 hci_cp.iac_lap[2] = 0x9e;
1716 hci_cp.iac_lap[3] = 0x33; /* GIAC */
1717 hci_cp.iac_lap[4] = 0x8b;
1718 hci_cp.iac_lap[5] = 0x9e;
1719 } else {
1720 /* General discoverable mode */
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001721 hci_cp.num_iac = 1;
1722 hci_cp.iac_lap[0] = 0x33; /* GIAC */
1723 hci_cp.iac_lap[1] = 0x8b;
1724 hci_cp.iac_lap[2] = 0x9e;
1725 }
1726
1727 hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP,
1728 (hci_cp.num_iac * 3) + 1, &hci_cp);
1729
1730 scan |= SCAN_INQUIRY;
1731 } else {
1732 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1733 }
1734
1735 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001736
Johan Hedberg9a43e252013-10-20 19:00:07 +03001737update_ad:
1738 update_adv_data(&req);
1739
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001740 err = hci_req_run(&req, set_discoverable_complete);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001741 if (err < 0)
1742 mgmt_pending_remove(cmd);
1743
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001744failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001745 hci_dev_unlock(hdev);
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001746 return err;
1747}
1748
Johan Hedberg406d7802013-03-15 17:07:09 -05001749static void write_fast_connectable(struct hci_request *req, bool enable)
1750{
Johan Hedbergbd98b992013-03-15 17:07:13 -05001751 struct hci_dev *hdev = req->hdev;
Johan Hedberg406d7802013-03-15 17:07:09 -05001752 struct hci_cp_write_page_scan_activity acp;
1753 u8 type;
1754
Johan Hedberg547003b2013-10-21 16:51:53 +03001755 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
1756 return;
1757
Johan Hedberg4c01f8b2013-03-15 17:07:14 -05001758 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
1759 return;
1760
Johan Hedberg406d7802013-03-15 17:07:09 -05001761 if (enable) {
1762 type = PAGE_SCAN_TYPE_INTERLACED;
1763
1764 /* 160 msec page scan interval */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001765 acp.interval = cpu_to_le16(0x0100);
Johan Hedberg406d7802013-03-15 17:07:09 -05001766 } else {
1767 type = PAGE_SCAN_TYPE_STANDARD; /* default */
1768
1769 /* default 1.28 sec page scan */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001770 acp.interval = cpu_to_le16(0x0800);
Johan Hedberg406d7802013-03-15 17:07:09 -05001771 }
1772
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001773 acp.window = cpu_to_le16(0x0012);
Johan Hedberg406d7802013-03-15 17:07:09 -05001774
Johan Hedbergbd98b992013-03-15 17:07:13 -05001775 if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval ||
1776 __cpu_to_le16(hdev->page_scan_window) != acp.window)
1777 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
1778 sizeof(acp), &acp);
1779
1780 if (hdev->page_scan_type != type)
1781 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type);
Johan Hedberg406d7802013-03-15 17:07:09 -05001782}
1783
Marcel Holtmann1904a852015-01-11 13:50:44 -08001784static void set_connectable_complete(struct hci_dev *hdev, u8 status,
1785 u16 opcode)
Johan Hedberg2b76f452013-03-15 17:07:04 -05001786{
1787 struct pending_cmd *cmd;
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001788 struct mgmt_mode *cp;
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001789 bool conn_changed, discov_changed;
Johan Hedberg2b76f452013-03-15 17:07:04 -05001790
1791 BT_DBG("status 0x%02x", status);
1792
1793 hci_dev_lock(hdev);
1794
1795 cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
1796 if (!cmd)
1797 goto unlock;
1798
Johan Hedberg37438c12013-10-14 16:20:05 +03001799 if (status) {
1800 u8 mgmt_err = mgmt_status(status);
1801 cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
1802 goto remove_cmd;
1803 }
1804
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001805 cp = cmd->param;
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001806 if (cp->val) {
1807 conn_changed = !test_and_set_bit(HCI_CONNECTABLE,
1808 &hdev->dev_flags);
1809 discov_changed = false;
1810 } else {
1811 conn_changed = test_and_clear_bit(HCI_CONNECTABLE,
1812 &hdev->dev_flags);
1813 discov_changed = test_and_clear_bit(HCI_DISCOVERABLE,
1814 &hdev->dev_flags);
1815 }
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001816
Johan Hedberg2b76f452013-03-15 17:07:04 -05001817 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
1818
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001819 if (conn_changed || discov_changed) {
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001820 new_settings(hdev, cmd->sk);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02001821 hci_update_page_scan(hdev);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001822 if (discov_changed)
1823 mgmt_update_adv_data(hdev);
Johan Hedberg2b7be332014-07-07 14:40:22 +03001824 hci_update_background_scan(hdev);
1825 }
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001826
Johan Hedberg37438c12013-10-14 16:20:05 +03001827remove_cmd:
Johan Hedberg2b76f452013-03-15 17:07:04 -05001828 mgmt_pending_remove(cmd);
1829
1830unlock:
1831 hci_dev_unlock(hdev);
1832}
1833
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001834static int set_connectable_update_settings(struct hci_dev *hdev,
1835 struct sock *sk, u8 val)
1836{
1837 bool changed = false;
1838 int err;
1839
1840 if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
1841 changed = true;
1842
1843 if (val) {
1844 set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
1845 } else {
1846 clear_bit(HCI_CONNECTABLE, &hdev->dev_flags);
1847 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
1848 }
1849
1850 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
1851 if (err < 0)
1852 return err;
1853
Johan Hedberg562064e2014-07-08 16:35:34 +03001854 if (changed) {
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02001855 hci_update_page_scan(hdev);
Johan Hedberg562064e2014-07-08 16:35:34 +03001856 hci_update_background_scan(hdev);
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001857 return new_settings(hdev, sk);
Johan Hedberg562064e2014-07-08 16:35:34 +03001858 }
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001859
1860 return 0;
1861}
1862
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001863static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001864 u16 len)
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001865{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001866 struct mgmt_mode *cp = data;
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001867 struct pending_cmd *cmd;
Johan Hedberg2b76f452013-03-15 17:07:04 -05001868 struct hci_request req;
Johan Hedberg1987fdc2013-10-14 21:15:24 +03001869 u8 scan;
Johan Hedbergf7b64e62010-12-13 21:07:06 +02001870 int err;
Johan Hedberg03811012010-12-08 00:21:06 +02001871
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001872 BT_DBG("request for %s", hdev->name);
Johan Hedberge41d8b42010-12-13 21:07:03 +02001873
Johan Hedberg1987fdc2013-10-14 21:15:24 +03001874 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) &&
1875 !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
Johan Hedberg33c525c2012-10-24 21:11:58 +03001876 return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
Johan Hedberg1987fdc2013-10-14 21:15:24 +03001877 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001878
Johan Hedberga7e80f22013-01-09 16:05:19 +02001879 if (cp->val != 0x00 && cp->val != 0x01)
1880 return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1881 MGMT_STATUS_INVALID_PARAMS);
1882
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001883 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001884
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001885 if (!hdev_is_powered(hdev)) {
Johan Hedberge8ba3a12013-10-19 23:38:18 +03001886 err = set_connectable_update_settings(hdev, sk, cp->val);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001887 goto failed;
1888 }
1889
1890 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03001891 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001892 err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001893 MGMT_STATUS_BUSY);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001894 goto failed;
1895 }
1896
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001897 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
1898 if (!cmd) {
1899 err = -ENOMEM;
1900 goto failed;
1901 }
1902
Johan Hedberg2b76f452013-03-15 17:07:04 -05001903 hci_req_init(&req, hdev);
1904
Johan Hedberg9a43e252013-10-20 19:00:07 +03001905 /* If BR/EDR is not enabled and we disable advertising as a
1906 * by-product of disabling connectable, we need to update the
1907 * advertising flags.
1908 */
1909 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
1910 if (!cp->val) {
1911 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1912 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
1913 }
1914 update_adv_data(&req);
1915 } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) {
Johan Hedberg9b742462013-10-14 16:20:03 +03001916 if (cp->val) {
1917 scan = SCAN_PAGE;
1918 } else {
Johan Hedberg3bd27242014-07-28 20:53:58 +03001919 /* If we don't have any whitelist entries just
1920 * disable all scanning. If there are entries
1921 * and we had both page and inquiry scanning
1922 * enabled then fall back to only page scanning.
1923 * Otherwise no changes are needed.
1924 */
1925 if (list_empty(&hdev->whitelist))
1926 scan = SCAN_DISABLED;
1927 else if (test_bit(HCI_ISCAN, &hdev->flags))
1928 scan = SCAN_PAGE;
1929 else
1930 goto no_scan_update;
Johan Hedberg9b742462013-10-14 16:20:03 +03001931
1932 if (test_bit(HCI_ISCAN, &hdev->flags) &&
Marcel Holtmann8d6083f2013-10-14 16:38:45 -07001933 hdev->discov_timeout > 0)
Johan Hedberg9b742462013-10-14 16:20:03 +03001934 cancel_delayed_work(&hdev->discov_off);
1935 }
1936
1937 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1938 }
Johan Hedberg2b76f452013-03-15 17:07:04 -05001939
Johan Hedberg3bd27242014-07-28 20:53:58 +03001940no_scan_update:
Johan Hedberg4c01f8b2013-03-15 17:07:14 -05001941 /* If we're going from non-connectable to connectable or
1942 * vice-versa when fast connectable is enabled ensure that fast
1943 * connectable gets disabled. write_fast_connectable won't do
1944 * anything if the page scan parameters are already what they
1945 * should be.
1946 */
1947 if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags))
Johan Hedberge36a3762013-03-15 17:07:10 -05001948 write_fast_connectable(&req, false);
1949
Johan Hedberge8b12022014-07-10 10:51:27 +03001950 /* Update the advertising parameters if necessary */
1951 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
Johan Hedberg1987fdc2013-10-14 21:15:24 +03001952 enable_advertising(&req);
Johan Hedberg1987fdc2013-10-14 21:15:24 +03001953
Johan Hedberg2b76f452013-03-15 17:07:04 -05001954 err = hci_req_run(&req, set_connectable_complete);
Johan Hedberg9b742462013-10-14 16:20:03 +03001955 if (err < 0) {
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001956 mgmt_pending_remove(cmd);
Johan Hedberg9b742462013-10-14 16:20:03 +03001957 if (err == -ENODATA)
Johan Hedberga81070b2013-10-19 23:38:19 +03001958 err = set_connectable_update_settings(hdev, sk,
1959 cp->val);
Johan Hedberg9b742462013-10-14 16:20:03 +03001960 goto failed;
1961 }
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001962
1963failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001964 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001965 return err;
1966}
1967
Johan Hedbergb2939472014-07-30 09:22:23 +03001968static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001969 u16 len)
Johan Hedberg73f22f62010-12-29 16:00:25 +02001970{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001971 struct mgmt_mode *cp = data;
Marcel Holtmann55594352013-10-06 16:11:57 -07001972 bool changed;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001973 int err;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001974
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001975 BT_DBG("request for %s", hdev->name);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001976
Johan Hedberga7e80f22013-01-09 16:05:19 +02001977 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedbergb2939472014-07-30 09:22:23 +03001978 return cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE,
Johan Hedberga7e80f22013-01-09 16:05:19 +02001979 MGMT_STATUS_INVALID_PARAMS);
1980
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001981 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001982
1983 if (cp->val)
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001984 changed = !test_and_set_bit(HCI_BONDABLE, &hdev->dev_flags);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001985 else
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001986 changed = test_and_clear_bit(HCI_BONDABLE, &hdev->dev_flags);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001987
Johan Hedbergb2939472014-07-30 09:22:23 +03001988 err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001989 if (err < 0)
Marcel Holtmann55594352013-10-06 16:11:57 -07001990 goto unlock;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001991
Marcel Holtmann55594352013-10-06 16:11:57 -07001992 if (changed)
1993 err = new_settings(hdev, sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001994
Marcel Holtmann55594352013-10-06 16:11:57 -07001995unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001996 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001997 return err;
1998}
Johan Hedberg72a734e2010-12-30 00:38:22 +02001999
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002000static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
2001 u16 len)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002002{
2003 struct mgmt_mode *cp = data;
2004 struct pending_cmd *cmd;
Johan Hedberge6fe7982013-10-02 15:45:22 +03002005 u8 val, status;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002006 int err;
2007
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002008 BT_DBG("request for %s", hdev->name);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002009
Johan Hedberge6fe7982013-10-02 15:45:22 +03002010 status = mgmt_bredr_support(hdev);
2011 if (status)
Johan Hedberg33c525c2012-10-24 21:11:58 +03002012 return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
Johan Hedberge6fe7982013-10-02 15:45:22 +03002013 status);
Johan Hedberg33c525c2012-10-24 21:11:58 +03002014
Johan Hedberga7e80f22013-01-09 16:05:19 +02002015 if (cp->val != 0x00 && cp->val != 0x01)
2016 return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2017 MGMT_STATUS_INVALID_PARAMS);
2018
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002019 hci_dev_lock(hdev);
2020
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002021 if (!hdev_is_powered(hdev)) {
Johan Hedberg47990ea2012-02-22 11:58:37 +02002022 bool changed = false;
2023
2024 if (!!cp->val != test_bit(HCI_LINK_SECURITY,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002025 &hdev->dev_flags)) {
Johan Hedberg47990ea2012-02-22 11:58:37 +02002026 change_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
2027 changed = true;
2028 }
2029
2030 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
2031 if (err < 0)
2032 goto failed;
2033
2034 if (changed)
2035 err = new_settings(hdev, sk);
2036
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002037 goto failed;
2038 }
2039
2040 if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002041 err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002042 MGMT_STATUS_BUSY);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002043 goto failed;
2044 }
2045
2046 val = !!cp->val;
2047
2048 if (test_bit(HCI_AUTH, &hdev->flags) == val) {
2049 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
2050 goto failed;
2051 }
2052
2053 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
2054 if (!cmd) {
2055 err = -ENOMEM;
2056 goto failed;
2057 }
2058
2059 err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
2060 if (err < 0) {
2061 mgmt_pending_remove(cmd);
2062 goto failed;
2063 }
2064
2065failed:
2066 hci_dev_unlock(hdev);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002067 return err;
2068}
2069
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002070static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002071{
2072 struct mgmt_mode *cp = data;
2073 struct pending_cmd *cmd;
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07002074 u8 status;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002075 int err;
2076
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002077 BT_DBG("request for %s", hdev->name);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002078
Marcel Holtmanncdba5282013-10-02 21:31:52 -07002079 status = mgmt_bredr_support(hdev);
2080 if (status)
2081 return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
2082
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002083 if (!lmp_ssp_capable(hdev))
2084 return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2085 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002086
Johan Hedberga7e80f22013-01-09 16:05:19 +02002087 if (cp->val != 0x00 && cp->val != 0x01)
2088 return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2089 MGMT_STATUS_INVALID_PARAMS);
2090
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002091 hci_dev_lock(hdev);
Johan Hedberg6c8f12c2012-02-22 16:35:26 +02002092
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002093 if (!hdev_is_powered(hdev)) {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002094 bool changed;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02002095
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002096 if (cp->val) {
2097 changed = !test_and_set_bit(HCI_SSP_ENABLED,
2098 &hdev->dev_flags);
2099 } else {
2100 changed = test_and_clear_bit(HCI_SSP_ENABLED,
2101 &hdev->dev_flags);
2102 if (!changed)
2103 changed = test_and_clear_bit(HCI_HS_ENABLED,
2104 &hdev->dev_flags);
2105 else
2106 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02002107 }
2108
2109 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2110 if (err < 0)
2111 goto failed;
2112
2113 if (changed)
2114 err = new_settings(hdev, sk);
2115
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002116 goto failed;
2117 }
2118
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002119 if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) ||
2120 mgmt_pending_find(MGMT_OP_SET_HS, hdev)) {
Szymon Jancd97dcb62012-03-16 16:02:56 +01002121 err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2122 MGMT_STATUS_BUSY);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002123 goto failed;
2124 }
2125
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07002126 if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002127 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2128 goto failed;
2129 }
2130
2131 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
2132 if (!cmd) {
2133 err = -ENOMEM;
2134 goto failed;
2135 }
2136
Johan Hedberg37699722014-06-24 14:00:27 +03002137 if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags))
2138 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2139 sizeof(cp->val), &cp->val);
2140
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07002141 err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002142 if (err < 0) {
2143 mgmt_pending_remove(cmd);
2144 goto failed;
2145 }
2146
2147failed:
2148 hci_dev_unlock(hdev);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002149 return err;
2150}
2151
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002152static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002153{
2154 struct mgmt_mode *cp = data;
Marcel Holtmannee392692013-10-01 22:59:23 -07002155 bool changed;
Johan Hedberge6fe7982013-10-02 15:45:22 +03002156 u8 status;
Marcel Holtmannee392692013-10-01 22:59:23 -07002157 int err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002158
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002159 BT_DBG("request for %s", hdev->name);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002160
Johan Hedberge6fe7982013-10-02 15:45:22 +03002161 status = mgmt_bredr_support(hdev);
2162 if (status)
2163 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002164
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002165 if (!lmp_ssp_capable(hdev))
2166 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2167 MGMT_STATUS_NOT_SUPPORTED);
2168
2169 if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
2170 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2171 MGMT_STATUS_REJECTED);
2172
Johan Hedberga7e80f22013-01-09 16:05:19 +02002173 if (cp->val != 0x00 && cp->val != 0x01)
2174 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2175 MGMT_STATUS_INVALID_PARAMS);
2176
Marcel Holtmannee392692013-10-01 22:59:23 -07002177 hci_dev_lock(hdev);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002178
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002179 if (cp->val) {
Marcel Holtmannee392692013-10-01 22:59:23 -07002180 changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002181 } else {
2182 if (hdev_is_powered(hdev)) {
2183 err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2184 MGMT_STATUS_REJECTED);
2185 goto unlock;
2186 }
2187
Marcel Holtmannee392692013-10-01 22:59:23 -07002188 changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002189 }
Marcel Holtmannee392692013-10-01 22:59:23 -07002190
2191 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
2192 if (err < 0)
2193 goto unlock;
2194
2195 if (changed)
2196 err = new_settings(hdev, sk);
2197
2198unlock:
2199 hci_dev_unlock(hdev);
2200 return err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002201}
2202
Marcel Holtmann1904a852015-01-11 13:50:44 -08002203static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002204{
2205 struct cmd_lookup match = { NULL, hdev };
2206
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302207 hci_dev_lock(hdev);
2208
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002209 if (status) {
2210 u8 mgmt_err = mgmt_status(status);
2211
2212 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
2213 &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302214 goto unlock;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002215 }
2216
2217 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
2218
2219 new_settings(hdev, match.sk);
2220
2221 if (match.sk)
2222 sock_put(match.sk);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002223
2224 /* Make sure the controller has a good default for
2225 * advertising data. Restrict the update to when LE
2226 * has actually been enabled. During power on, the
2227 * update in powered_update_hci will take care of it.
2228 */
2229 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
2230 struct hci_request req;
2231
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002232 hci_req_init(&req, hdev);
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07002233 update_adv_data(&req);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07002234 update_scan_rsp_data(&req);
Johan Hedberg2cf22212014-12-19 22:26:00 +02002235 __hci_update_background_scan(&req);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002236 hci_req_run(&req, NULL);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002237 }
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302238
2239unlock:
2240 hci_dev_unlock(hdev);
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002241}
2242
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002243static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg06199cf2012-02-22 16:37:11 +02002244{
2245 struct mgmt_mode *cp = data;
2246 struct hci_cp_write_le_host_supported hci_cp;
2247 struct pending_cmd *cmd;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002248 struct hci_request req;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002249 int err;
Johan Hedberg0b60eba2012-02-28 00:57:24 +02002250 u8 val, enabled;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002251
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002252 BT_DBG("request for %s", hdev->name);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002253
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002254 if (!lmp_le_capable(hdev))
2255 return cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2256 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg1de028c2012-02-29 19:55:35 -08002257
Johan Hedberga7e80f22013-01-09 16:05:19 +02002258 if (cp->val != 0x00 && cp->val != 0x01)
2259 return cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2260 MGMT_STATUS_INVALID_PARAMS);
2261
Johan Hedbergc73eee92013-04-19 18:35:21 +03002262 /* LE-only devices do not allow toggling LE on/off */
Johan Hedberg56f87902013-10-02 13:43:13 +03002263 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
Johan Hedbergc73eee92013-04-19 18:35:21 +03002264 return cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2265 MGMT_STATUS_REJECTED);
2266
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002267 hci_dev_lock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002268
2269 val = !!cp->val;
Gustavo Padovanffa88e02012-11-23 16:50:51 -02002270 enabled = lmp_host_le_capable(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002271
Johan Hedberg0b60eba2012-02-28 00:57:24 +02002272 if (!hdev_is_powered(hdev) || val == enabled) {
Johan Hedberg06199cf2012-02-22 16:37:11 +02002273 bool changed = false;
2274
2275 if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
2276 change_bit(HCI_LE_ENABLED, &hdev->dev_flags);
2277 changed = true;
2278 }
2279
Johan Hedbergf3d3444a2013-10-05 12:01:04 +02002280 if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
2281 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03002282 changed = true;
2283 }
2284
Johan Hedberg06199cf2012-02-22 16:37:11 +02002285 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2286 if (err < 0)
Johan Hedberg1de028c2012-02-29 19:55:35 -08002287 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002288
2289 if (changed)
2290 err = new_settings(hdev, sk);
2291
Johan Hedberg1de028c2012-02-29 19:55:35 -08002292 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002293 }
2294
Johan Hedberg4375f102013-09-25 13:26:10 +03002295 if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) ||
2296 mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002297 err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002298 MGMT_STATUS_BUSY);
Johan Hedberg1de028c2012-02-29 19:55:35 -08002299 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002300 }
2301
2302 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
2303 if (!cmd) {
2304 err = -ENOMEM;
Johan Hedberg1de028c2012-02-29 19:55:35 -08002305 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002306 }
2307
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002308 hci_req_init(&req, hdev);
2309
Johan Hedberg06199cf2012-02-22 16:37:11 +02002310 memset(&hci_cp, 0, sizeof(hci_cp));
2311
2312 if (val) {
2313 hci_cp.le = val;
Marcel Holtmann32226e42014-07-24 20:04:16 +02002314 hci_cp.simul = 0x00;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002315 } else {
Johan Hedberg73e082f2014-07-08 15:07:51 +03002316 if (test_bit(HCI_LE_ADV, &hdev->dev_flags))
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002317 disable_advertising(&req);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002318 }
2319
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002320 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp),
2321 &hci_cp);
2322
2323 err = hci_req_run(&req, le_enable_complete);
Syam Sidhardhan0c01bc42012-04-12 20:33:21 +05302324 if (err < 0)
Johan Hedberg06199cf2012-02-22 16:37:11 +02002325 mgmt_pending_remove(cmd);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002326
Johan Hedberg1de028c2012-02-29 19:55:35 -08002327unlock:
2328 hci_dev_unlock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002329 return err;
2330}
2331
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002332/* This is a helper function to test for pending mgmt commands that can
2333 * cause CoD or EIR HCI commands. We can only allow one such pending
2334 * mgmt command at a time since otherwise we cannot easily track what
2335 * the current values are, will be, and based on that calculate if a new
2336 * HCI command needs to be sent and if yes with what value.
2337 */
2338static bool pending_eir_or_class(struct hci_dev *hdev)
2339{
2340 struct pending_cmd *cmd;
2341
2342 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
2343 switch (cmd->opcode) {
2344 case MGMT_OP_ADD_UUID:
2345 case MGMT_OP_REMOVE_UUID:
2346 case MGMT_OP_SET_DEV_CLASS:
2347 case MGMT_OP_SET_POWERED:
2348 return true;
2349 }
2350 }
2351
2352 return false;
2353}
2354
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002355static const u8 bluetooth_base_uuid[] = {
2356 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
2357 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2358};
2359
2360static u8 get_uuid_size(const u8 *uuid)
2361{
2362 u32 val;
2363
2364 if (memcmp(uuid, bluetooth_base_uuid, 12))
2365 return 128;
2366
2367 val = get_unaligned_le32(&uuid[12]);
2368 if (val > 0xffff)
2369 return 32;
2370
2371 return 16;
2372}
2373
Johan Hedberg92da6092013-03-15 17:06:55 -05002374static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status)
2375{
2376 struct pending_cmd *cmd;
2377
2378 hci_dev_lock(hdev);
2379
2380 cmd = mgmt_pending_find(mgmt_op, hdev);
2381 if (!cmd)
2382 goto unlock;
2383
2384 cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status),
2385 hdev->dev_class, 3);
2386
2387 mgmt_pending_remove(cmd);
2388
2389unlock:
2390 hci_dev_unlock(hdev);
2391}
2392
Marcel Holtmann1904a852015-01-11 13:50:44 -08002393static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002394{
2395 BT_DBG("status 0x%02x", status);
2396
2397 mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status);
2398}
2399
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002400static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002401{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002402 struct mgmt_cp_add_uuid *cp = data;
Johan Hedberg90e70452012-02-23 23:09:40 +02002403 struct pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002404 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002405 struct bt_uuid *uuid;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002406 int err;
2407
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002408 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002409
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002410 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002411
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002412 if (pending_eir_or_class(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002413 err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002414 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002415 goto failed;
2416 }
2417
Andre Guedes92c4c202012-06-07 19:05:44 -03002418 uuid = kmalloc(sizeof(*uuid), GFP_KERNEL);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002419 if (!uuid) {
2420 err = -ENOMEM;
2421 goto failed;
2422 }
2423
2424 memcpy(uuid->uuid, cp->uuid, 16);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002425 uuid->svc_hint = cp->svc_hint;
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002426 uuid->size = get_uuid_size(cp->uuid);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002427
Johan Hedbergde66aa62013-01-27 00:31:27 +02002428 list_add_tail(&uuid->list, &hdev->uuids);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002429
Johan Hedberg890ea892013-03-15 17:06:52 -05002430 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002431
Johan Hedberg890ea892013-03-15 17:06:52 -05002432 update_class(&req);
2433 update_eir(&req);
2434
Johan Hedberg92da6092013-03-15 17:06:55 -05002435 err = hci_req_run(&req, add_uuid_complete);
2436 if (err < 0) {
2437 if (err != -ENODATA)
2438 goto failed;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002439
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002440 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002441 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002442 goto failed;
2443 }
2444
2445 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002446 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002447 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002448 goto failed;
2449 }
2450
2451 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002452
2453failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002454 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002455 return err;
2456}
2457
Johan Hedberg24b78d02012-02-23 23:24:30 +02002458static bool enable_service_cache(struct hci_dev *hdev)
2459{
2460 if (!hdev_is_powered(hdev))
2461 return false;
2462
2463 if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) {
Johan Hedberg46818ed2013-01-14 22:33:52 +02002464 queue_delayed_work(hdev->workqueue, &hdev->service_cache,
2465 CACHE_TIMEOUT);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002466 return true;
2467 }
2468
2469 return false;
2470}
2471
Marcel Holtmann1904a852015-01-11 13:50:44 -08002472static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002473{
2474 BT_DBG("status 0x%02x", status);
2475
2476 mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status);
2477}
2478
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002479static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002480 u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002481{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002482 struct mgmt_cp_remove_uuid *cp = data;
Johan Hedberg90e70452012-02-23 23:09:40 +02002483 struct pending_cmd *cmd;
Johan Hedberg056341c2013-01-27 00:31:30 +02002484 struct bt_uuid *match, *tmp;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002485 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 -05002486 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002487 int err, found;
2488
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002489 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002490
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002491 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002492
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002493 if (pending_eir_or_class(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002494 err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002495 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002496 goto unlock;
2497 }
2498
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002499 if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
Johan Hedberg35f74982014-02-18 17:14:32 +02002500 hci_uuids_clear(hdev);
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002501
Johan Hedberg24b78d02012-02-23 23:24:30 +02002502 if (enable_service_cache(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002503 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002504 0, hdev->dev_class, 3);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002505 goto unlock;
2506 }
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002507
Johan Hedberg9246a862012-02-23 21:33:16 +02002508 goto update_class;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002509 }
2510
2511 found = 0;
2512
Johan Hedberg056341c2013-01-27 00:31:30 +02002513 list_for_each_entry_safe(match, tmp, &hdev->uuids, list) {
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002514 if (memcmp(match->uuid, cp->uuid, 16) != 0)
2515 continue;
2516
2517 list_del(&match->list);
Johan Hedberg482049f2012-11-08 10:25:26 +01002518 kfree(match);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002519 found++;
2520 }
2521
2522 if (found == 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002523 err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002524 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002525 goto unlock;
2526 }
2527
Johan Hedberg9246a862012-02-23 21:33:16 +02002528update_class:
Johan Hedberg890ea892013-03-15 17:06:52 -05002529 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002530
Johan Hedberg890ea892013-03-15 17:06:52 -05002531 update_class(&req);
2532 update_eir(&req);
2533
Johan Hedberg92da6092013-03-15 17:06:55 -05002534 err = hci_req_run(&req, remove_uuid_complete);
2535 if (err < 0) {
2536 if (err != -ENODATA)
2537 goto unlock;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002538
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002539 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002540 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002541 goto unlock;
2542 }
2543
2544 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002545 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002546 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002547 goto unlock;
2548 }
2549
2550 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002551
2552unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002553 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002554 return err;
2555}
2556
Marcel Holtmann1904a852015-01-11 13:50:44 -08002557static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002558{
2559 BT_DBG("status 0x%02x", status);
2560
2561 mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status);
2562}
2563
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002564static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002565 u16 len)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002566{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002567 struct mgmt_cp_set_dev_class *cp = data;
Johan Hedberg90e70452012-02-23 23:09:40 +02002568 struct pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002569 struct hci_request req;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002570 int err;
2571
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002572 BT_DBG("request for %s", hdev->name);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002573
Marcel Holtmann6203fc92013-10-02 23:37:29 -07002574 if (!lmp_bredr_capable(hdev))
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002575 return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2576 MGMT_STATUS_NOT_SUPPORTED);
2577
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002578 hci_dev_lock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002579
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002580 if (pending_eir_or_class(hdev)) {
2581 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2582 MGMT_STATUS_BUSY);
2583 goto unlock;
2584 }
2585
2586 if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
2587 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2588 MGMT_STATUS_INVALID_PARAMS);
2589 goto unlock;
2590 }
2591
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002592 hdev->major_class = cp->major;
2593 hdev->minor_class = cp->minor;
2594
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002595 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002596 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002597 hdev->dev_class, 3);
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002598 goto unlock;
2599 }
2600
Johan Hedberg890ea892013-03-15 17:06:52 -05002601 hci_req_init(&req, hdev);
2602
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02002603 if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) {
Johan Hedberg7d785252011-12-15 00:47:39 +02002604 hci_dev_unlock(hdev);
2605 cancel_delayed_work_sync(&hdev->service_cache);
2606 hci_dev_lock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -05002607 update_eir(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02002608 }
Johan Hedberg14c0b602011-12-15 00:47:37 +02002609
Johan Hedberg890ea892013-03-15 17:06:52 -05002610 update_class(&req);
2611
Johan Hedberg92da6092013-03-15 17:06:55 -05002612 err = hci_req_run(&req, set_class_complete);
2613 if (err < 0) {
2614 if (err != -ENODATA)
2615 goto unlock;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002616
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002617 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002618 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002619 goto unlock;
2620 }
2621
2622 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002623 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002624 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002625 goto unlock;
2626 }
2627
2628 err = 0;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002629
Johan Hedbergb5235a62012-02-21 14:32:24 +02002630unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002631 hci_dev_unlock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002632 return err;
2633}
2634
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002635static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002636 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002637{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002638 struct mgmt_cp_load_link_keys *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03002639 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
2640 sizeof(struct mgmt_link_key_info));
Szymon Janc4e51eae2011-02-25 19:05:48 +01002641 u16 key_count, expected_len;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002642 bool changed;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002643 int i;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002644
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002645 BT_DBG("request for %s", hdev->name);
2646
2647 if (!lmp_bredr_capable(hdev))
2648 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2649 MGMT_STATUS_NOT_SUPPORTED);
2650
Marcel Holtmann1f350c82012-03-12 20:31:08 -07002651 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002652 if (key_count > max_key_count) {
2653 BT_ERR("load_link_keys: too big key_count value %u",
2654 key_count);
2655 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2656 MGMT_STATUS_INVALID_PARAMS);
2657 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002658
Johan Hedberg86742e12011-11-07 23:13:38 +02002659 expected_len = sizeof(*cp) + key_count *
2660 sizeof(struct mgmt_link_key_info);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002661 if (expected_len != len) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002662 BT_ERR("load_link_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02002663 expected_len, len);
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002664 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002665 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002666 }
2667
Johan Hedberg4ae14302013-01-20 14:27:13 +02002668 if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01)
2669 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2670 MGMT_STATUS_INVALID_PARAMS);
2671
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002672 BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002673 key_count);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002674
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002675 for (i = 0; i < key_count; i++) {
2676 struct mgmt_link_key_info *key = &cp->keys[i];
2677
Marcel Holtmann8e991132014-01-10 02:07:25 -08002678 if (key->addr.type != BDADDR_BREDR || key->type > 0x08)
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002679 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2680 MGMT_STATUS_INVALID_PARAMS);
2681 }
2682
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002683 hci_dev_lock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002684
2685 hci_link_keys_clear(hdev);
2686
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002687 if (cp->debug_keys)
Johan Hedberg0663b292014-06-24 13:15:50 +03002688 changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS,
2689 &hdev->dev_flags);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002690 else
Johan Hedberg0663b292014-06-24 13:15:50 +03002691 changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS,
2692 &hdev->dev_flags);
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002693
2694 if (changed)
2695 new_settings(hdev, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002696
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002697 for (i = 0; i < key_count; i++) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002698 struct mgmt_link_key_info *key = &cp->keys[i];
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002699
Johan Hedberg58e92932014-06-24 14:00:26 +03002700 /* Always ignore debug keys and require a new pairing if
2701 * the user wants to use them.
2702 */
2703 if (key->type == HCI_LK_DEBUG_COMBINATION)
2704 continue;
2705
Johan Hedberg7652ff62014-06-24 13:15:49 +03002706 hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val,
2707 key->type, key->pin_len, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002708 }
2709
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002710 cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
Johan Hedberg0e5f8752011-11-11 16:18:54 +02002711
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002712 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002713
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002714 return 0;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002715}
2716
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002717static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002718 u8 addr_type, struct sock *skip_sk)
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002719{
2720 struct mgmt_ev_device_unpaired ev;
2721
2722 bacpy(&ev.addr.bdaddr, bdaddr);
2723 ev.addr.type = addr_type;
2724
2725 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002726 skip_sk);
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002727}
2728
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002729static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002730 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002731{
Johan Hedberg124f6e32012-02-09 13:50:12 +02002732 struct mgmt_cp_unpair_device *cp = data;
2733 struct mgmt_rp_unpair_device rp;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002734 struct hci_cp_disconnect dc;
2735 struct pending_cmd *cmd;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002736 struct hci_conn *conn;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002737 int err;
2738
Johan Hedberga8a1d192011-11-10 15:54:38 +02002739 memset(&rp, 0, sizeof(rp));
Johan Hedberg124f6e32012-02-09 13:50:12 +02002740 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2741 rp.addr.type = cp->addr.type;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002742
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002743 if (!bdaddr_type_is_valid(cp->addr.type))
2744 return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2745 MGMT_STATUS_INVALID_PARAMS,
2746 &rp, sizeof(rp));
2747
Johan Hedberg118da702013-01-20 14:27:20 +02002748 if (cp->disconnect != 0x00 && cp->disconnect != 0x01)
2749 return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2750 MGMT_STATUS_INVALID_PARAMS,
2751 &rp, sizeof(rp));
2752
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002753 hci_dev_lock(hdev);
2754
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002755 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002756 err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002757 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp));
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002758 goto unlock;
2759 }
2760
Johan Hedberge0b2b272014-02-18 17:14:31 +02002761 if (cp->addr.type == BDADDR_BREDR) {
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002762 /* If disconnection is requested, then look up the
2763 * connection. If the remote device is connected, it
2764 * will be later used to terminate the link.
2765 *
2766 * Setting it to NULL explicitly will cause no
2767 * termination of the link.
2768 */
2769 if (cp->disconnect)
2770 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2771 &cp->addr.bdaddr);
2772 else
2773 conn = NULL;
2774
Johan Hedberg124f6e32012-02-09 13:50:12 +02002775 err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
Johan Hedberge0b2b272014-02-18 17:14:31 +02002776 } else {
2777 u8 addr_type;
2778
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002779 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK,
2780 &cp->addr.bdaddr);
2781 if (conn) {
2782 /* Defer clearing up the connection parameters
2783 * until closing to give a chance of keeping
2784 * them if a repairing happens.
2785 */
2786 set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
2787
2788 /* If disconnection is not requested, then
2789 * clear the connection variable so that the
2790 * link is not terminated.
2791 */
2792 if (!cp->disconnect)
2793 conn = NULL;
2794 }
2795
Johan Hedberge0b2b272014-02-18 17:14:31 +02002796 if (cp->addr.type == BDADDR_LE_PUBLIC)
2797 addr_type = ADDR_LE_DEV_PUBLIC;
2798 else
2799 addr_type = ADDR_LE_DEV_RANDOM;
2800
Johan Hedberga7ec7332014-02-18 17:14:35 +02002801 hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type);
2802
Johan Hedberge0b2b272014-02-18 17:14:31 +02002803 err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type);
2804 }
Vinicius Costa Gomesb0dbfb42012-02-02 21:08:03 -03002805
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002806 if (err < 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002807 err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002808 MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002809 goto unlock;
2810 }
2811
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002812 /* If the connection variable is set, then termination of the
2813 * link is requested.
2814 */
Johan Hedberga8a1d192011-11-10 15:54:38 +02002815 if (!conn) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002816 err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002817 &rp, sizeof(rp));
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002818 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002819 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002820 }
2821
Johan Hedberg124f6e32012-02-09 13:50:12 +02002822 cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002823 sizeof(*cp));
Johan Hedberga8a1d192011-11-10 15:54:38 +02002824 if (!cmd) {
2825 err = -ENOMEM;
2826 goto unlock;
2827 }
2828
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02002829 cmd->cmd_complete = addr_cmd_complete;
2830
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02002831 dc.handle = cpu_to_le16(conn->handle);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002832 dc.reason = 0x13; /* Remote User Terminated Connection */
2833 err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc);
2834 if (err < 0)
2835 mgmt_pending_remove(cmd);
2836
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002837unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002838 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002839 return err;
2840}
2841
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002842static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002843 u16 len)
Johan Hedberg8962ee72011-01-20 12:40:27 +02002844{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002845 struct mgmt_cp_disconnect *cp = data;
Johan Hedberg06a63b12013-01-20 14:27:21 +02002846 struct mgmt_rp_disconnect rp;
Johan Hedberg366a0332011-02-19 12:05:55 -03002847 struct pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002848 struct hci_conn *conn;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002849 int err;
2850
2851 BT_DBG("");
2852
Johan Hedberg06a63b12013-01-20 14:27:21 +02002853 memset(&rp, 0, sizeof(rp));
2854 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2855 rp.addr.type = cp->addr.type;
2856
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002857 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg06a63b12013-01-20 14:27:21 +02002858 return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2859 MGMT_STATUS_INVALID_PARAMS,
2860 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002861
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002862 hci_dev_lock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002863
2864 if (!test_bit(HCI_UP, &hdev->flags)) {
Johan Hedberg06a63b12013-01-20 14:27:21 +02002865 err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2866 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002867 goto failed;
2868 }
2869
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002870 if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) {
Johan Hedberg06a63b12013-01-20 14:27:21 +02002871 err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2872 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002873 goto failed;
2874 }
2875
Andre Guedes591f47f2012-04-24 21:02:49 -03002876 if (cp->addr.type == BDADDR_BREDR)
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03002877 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2878 &cp->addr.bdaddr);
Johan Hedberg88c3df12012-02-09 14:27:38 +02002879 else
2880 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
Vinicius Costa Gomes365227e2011-05-06 18:41:44 -03002881
Vishal Agarwalf9607272012-06-13 05:32:43 +05302882 if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) {
Johan Hedberg06a63b12013-01-20 14:27:21 +02002883 err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2884 MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002885 goto failed;
2886 }
2887
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002888 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03002889 if (!cmd) {
2890 err = -ENOMEM;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002891 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03002892 }
Johan Hedberg8962ee72011-01-20 12:40:27 +02002893
Johan Hedbergf5818c22014-12-05 13:36:02 +02002894 cmd->cmd_complete = generic_cmd_complete;
2895
Johan Hedberge3f2f922014-08-18 20:33:33 +03002896 err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002897 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03002898 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002899
2900failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002901 hci_dev_unlock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002902 return err;
2903}
2904
Andre Guedes57c14772012-04-24 21:02:50 -03002905static u8 link_to_bdaddr(u8 link_type, u8 addr_type)
Johan Hedberg4c659c32011-11-07 23:13:39 +02002906{
2907 switch (link_type) {
2908 case LE_LINK:
Johan Hedberg48264f02011-11-09 13:58:58 +02002909 switch (addr_type) {
2910 case ADDR_LE_DEV_PUBLIC:
Andre Guedes591f47f2012-04-24 21:02:49 -03002911 return BDADDR_LE_PUBLIC;
Andre Guedes0ed09142012-04-03 08:46:54 -03002912
Johan Hedberg48264f02011-11-09 13:58:58 +02002913 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03002914 /* Fallback to LE Random address type */
Andre Guedes591f47f2012-04-24 21:02:49 -03002915 return BDADDR_LE_RANDOM;
Johan Hedberg48264f02011-11-09 13:58:58 +02002916 }
Andre Guedes0ed09142012-04-03 08:46:54 -03002917
Johan Hedberg4c659c32011-11-07 23:13:39 +02002918 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03002919 /* Fallback to BR/EDR type */
Andre Guedes591f47f2012-04-24 21:02:49 -03002920 return BDADDR_BREDR;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002921 }
2922}
2923
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002924static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
2925 u16 data_len)
Johan Hedberg2784eb42011-01-21 13:56:35 +02002926{
Johan Hedberg2784eb42011-01-21 13:56:35 +02002927 struct mgmt_rp_get_connections *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002928 struct hci_conn *c;
Johan Hedberga38528f2011-01-22 06:46:43 +02002929 size_t rp_len;
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002930 int err;
2931 u16 i;
Johan Hedberg2784eb42011-01-21 13:56:35 +02002932
2933 BT_DBG("");
2934
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002935 hci_dev_lock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002936
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002937 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002938 err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002939 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002940 goto unlock;
2941 }
2942
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002943 i = 0;
Johan Hedbergb644ba32012-01-17 21:48:47 +02002944 list_for_each_entry(c, &hdev->conn_hash.list, list) {
2945 if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002946 i++;
Johan Hedberg2784eb42011-01-21 13:56:35 +02002947 }
2948
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002949 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Andre Guedes92c4c202012-06-07 19:05:44 -03002950 rp = kmalloc(rp_len, GFP_KERNEL);
Johan Hedberga38528f2011-01-22 06:46:43 +02002951 if (!rp) {
Johan Hedberg2784eb42011-01-21 13:56:35 +02002952 err = -ENOMEM;
2953 goto unlock;
2954 }
2955
Johan Hedberg2784eb42011-01-21 13:56:35 +02002956 i = 0;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002957 list_for_each_entry(c, &hdev->conn_hash.list, list) {
Johan Hedbergb644ba32012-01-17 21:48:47 +02002958 if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
2959 continue;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002960 bacpy(&rp->addr[i].bdaddr, &c->dst);
Andre Guedes57c14772012-04-24 21:02:50 -03002961 rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type);
Andre Guedes0ed09142012-04-03 08:46:54 -03002962 if (c->type == SCO_LINK || c->type == ESCO_LINK)
Johan Hedberg4c659c32011-11-07 23:13:39 +02002963 continue;
2964 i++;
2965 }
2966
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02002967 rp->conn_count = cpu_to_le16(i);
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002968
Johan Hedberg4c659c32011-11-07 23:13:39 +02002969 /* Recalculate length in case of filtered SCO connections, etc */
2970 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Johan Hedberg2784eb42011-01-21 13:56:35 +02002971
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002972 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002973 rp_len);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002974
Johan Hedberga38528f2011-01-22 06:46:43 +02002975 kfree(rp);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002976
2977unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002978 hci_dev_unlock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002979 return err;
2980}
2981
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002982static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002983 struct mgmt_cp_pin_code_neg_reply *cp)
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002984{
2985 struct pending_cmd *cmd;
2986 int err;
2987
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002988 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002989 sizeof(*cp));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002990 if (!cmd)
2991 return -ENOMEM;
2992
Johan Hedbergd8457692012-02-17 14:24:57 +02002993 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002994 sizeof(cp->addr.bdaddr), &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002995 if (err < 0)
2996 mgmt_pending_remove(cmd);
2997
2998 return err;
2999}
3000
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003001static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003002 u16 len)
Johan Hedberg980e1a52011-01-22 06:10:07 +02003003{
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003004 struct hci_conn *conn;
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003005 struct mgmt_cp_pin_code_reply *cp = data;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003006 struct hci_cp_pin_code_reply reply;
Johan Hedberg366a0332011-02-19 12:05:55 -03003007 struct pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003008 int err;
3009
3010 BT_DBG("");
3011
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003012 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003013
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003014 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003015 err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003016 MGMT_STATUS_NOT_POWERED);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003017 goto failed;
3018 }
3019
Johan Hedbergd8457692012-02-17 14:24:57 +02003020 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003021 if (!conn) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003022 err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003023 MGMT_STATUS_NOT_CONNECTED);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003024 goto failed;
3025 }
3026
3027 if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) {
Johan Hedbergd8457692012-02-17 14:24:57 +02003028 struct mgmt_cp_pin_code_neg_reply ncp;
3029
3030 memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003031
3032 BT_ERR("PIN code is not 16 bytes long");
3033
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003034 err = send_pin_code_neg_reply(sk, hdev, &ncp);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003035 if (err >= 0)
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003036 err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003037 MGMT_STATUS_INVALID_PARAMS);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003038
3039 goto failed;
3040 }
3041
Gustavo F. Padovan00abfe42012-03-01 00:37:10 -03003042 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03003043 if (!cmd) {
3044 err = -ENOMEM;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003045 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03003046 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02003047
Johan Hedberg7776d1d2014-12-05 13:36:03 +02003048 cmd->cmd_complete = addr_cmd_complete;
3049
Johan Hedbergd8457692012-02-17 14:24:57 +02003050 bacpy(&reply.bdaddr, &cp->addr.bdaddr);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003051 reply.pin_len = cp->pin_len;
Waldemar Rymarkiewicz24718ca2011-06-01 17:28:47 +02003052 memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code));
Johan Hedberg980e1a52011-01-22 06:10:07 +02003053
3054 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
3055 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03003056 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003057
3058failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003059 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003060 return err;
3061}
3062
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003063static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
3064 u16 len)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003065{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003066 struct mgmt_cp_set_io_capability *cp = data;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003067
3068 BT_DBG("");
3069
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003070 if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY)
3071 return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY,
3072 MGMT_STATUS_INVALID_PARAMS, NULL, 0);
3073
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003074 hci_dev_lock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003075
3076 hdev->io_capability = cp->io_capability;
3077
3078 BT_DBG("%s IO capability set to 0x%02x", hdev->name,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003079 hdev->io_capability);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003080
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003081 hci_dev_unlock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003082
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003083 return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL,
3084 0);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003085}
3086
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003087static struct pending_cmd *find_pairing(struct hci_conn *conn)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003088{
3089 struct hci_dev *hdev = conn->hdev;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02003090 struct pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003091
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003092 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
Johan Hedberge9a416b2011-02-19 12:05:56 -03003093 if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
3094 continue;
3095
Johan Hedberge9a416b2011-02-19 12:05:56 -03003096 if (cmd->user_data != conn)
3097 continue;
3098
3099 return cmd;
3100 }
3101
3102 return NULL;
3103}
3104
Johan Hedberg9df74652014-12-19 22:26:03 +02003105static int pairing_complete(struct pending_cmd *cmd, u8 status)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003106{
3107 struct mgmt_rp_pair_device rp;
3108 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9df74652014-12-19 22:26:03 +02003109 int err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003110
Johan Hedberg61b1a7f2014-03-20 12:54:16 +02003111 bacpy(&rp.addr.bdaddr, &conn->dst);
3112 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003113
Johan Hedberg9df74652014-12-19 22:26:03 +02003114 err = cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status,
3115 &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003116
3117 /* So we don't get further callbacks for this connection */
3118 conn->connect_cfm_cb = NULL;
3119 conn->security_cfm_cb = NULL;
3120 conn->disconn_cfm_cb = NULL;
3121
David Herrmann76a68ba2013-04-06 20:28:37 +02003122 hci_conn_drop(conn);
Alfonso Acosta89cbb062014-10-11 21:44:47 +00003123
3124 /* The device is paired so there is no need to remove
3125 * its connection parameters anymore.
3126 */
3127 clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
Johan Hedberg15013ae2014-12-11 21:45:44 +02003128
3129 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02003130
3131 return err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003132}
3133
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003134void mgmt_smp_complete(struct hci_conn *conn, bool complete)
3135{
3136 u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED;
3137 struct pending_cmd *cmd;
3138
3139 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003140 if (cmd) {
Johan Hedberg04ab2742014-12-05 13:36:04 +02003141 cmd->cmd_complete(cmd, status);
Johan Hedberga511b352014-12-11 21:45:45 +02003142 mgmt_pending_remove(cmd);
3143 }
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003144}
3145
Johan Hedberge9a416b2011-02-19 12:05:56 -03003146static void pairing_complete_cb(struct hci_conn *conn, u8 status)
3147{
3148 struct pending_cmd *cmd;
3149
3150 BT_DBG("status %u", status);
3151
Johan Hedberg56e5cb82011-11-08 20:40:16 +02003152 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003153 if (!cmd) {
Johan Hedberg56e5cb82011-11-08 20:40:16 +02003154 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02003155 return;
3156 }
3157
3158 cmd->cmd_complete(cmd, mgmt_status(status));
3159 mgmt_pending_remove(cmd);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003160}
3161
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003162static void le_pairing_complete_cb(struct hci_conn *conn, u8 status)
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303163{
3164 struct pending_cmd *cmd;
3165
3166 BT_DBG("status %u", status);
3167
3168 if (!status)
3169 return;
3170
3171 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003172 if (!cmd) {
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303173 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02003174 return;
3175 }
3176
3177 cmd->cmd_complete(cmd, mgmt_status(status));
3178 mgmt_pending_remove(cmd);
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303179}
3180
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003181static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003182 u16 len)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003183{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003184 struct mgmt_cp_pair_device *cp = data;
Johan Hedberg1425acb2011-11-11 00:07:35 +02003185 struct mgmt_rp_pair_device rp;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003186 struct pending_cmd *cmd;
3187 u8 sec_level, auth_type;
3188 struct hci_conn *conn;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003189 int err;
3190
3191 BT_DBG("");
3192
Szymon Jancf950a30e2013-01-18 12:48:07 +01003193 memset(&rp, 0, sizeof(rp));
3194 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3195 rp.addr.type = cp->addr.type;
3196
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003197 if (!bdaddr_type_is_valid(cp->addr.type))
3198 return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3199 MGMT_STATUS_INVALID_PARAMS,
3200 &rp, sizeof(rp));
3201
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003202 if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY)
3203 return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3204 MGMT_STATUS_INVALID_PARAMS,
3205 &rp, sizeof(rp));
3206
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003207 hci_dev_lock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003208
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003209 if (!hdev_is_powered(hdev)) {
Szymon Jancf950a30e2013-01-18 12:48:07 +01003210 err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3211 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp));
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003212 goto unlock;
3213 }
3214
Vinicius Costa Gomesc908df32011-09-02 14:51:22 -03003215 sec_level = BT_SECURITY_MEDIUM;
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003216 auth_type = HCI_AT_DEDICATED_BONDING;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003217
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003218 if (cp->addr.type == BDADDR_BREDR) {
Andre Guedes04a6c582014-02-26 20:21:44 -03003219 conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
3220 auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003221 } else {
3222 u8 addr_type;
3223
3224 /* Convert from L2CAP channel address type to HCI address type
3225 */
3226 if (cp->addr.type == BDADDR_LE_PUBLIC)
3227 addr_type = ADDR_LE_DEV_PUBLIC;
3228 else
3229 addr_type = ADDR_LE_DEV_RANDOM;
3230
Marcel Holtmann7c264b12014-06-30 12:34:40 +02003231 /* When pairing a new device, it is expected to remember
3232 * this device for future connections. Adding the connection
3233 * parameter information ahead of time allows tracking
3234 * of the slave preferred values and will speed up any
3235 * further connection establishment.
3236 *
3237 * If connection parameters already exist, then they
3238 * will be kept and this function does nothing.
3239 */
3240 hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type);
3241
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003242 conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type,
Johan Hedberge804d252014-07-16 11:42:28 +03003243 sec_level, HCI_LE_CONN_TIMEOUT,
3244 HCI_ROLE_MASTER);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003245 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003246
Ville Tervo30e76272011-02-22 16:10:53 -03003247 if (IS_ERR(conn)) {
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02003248 int status;
3249
3250 if (PTR_ERR(conn) == -EBUSY)
3251 status = MGMT_STATUS_BUSY;
Lukasz Rymanowskifaa81032015-02-11 12:31:42 +01003252 else if (PTR_ERR(conn) == -EOPNOTSUPP)
3253 status = MGMT_STATUS_NOT_SUPPORTED;
3254 else if (PTR_ERR(conn) == -ECONNREFUSED)
3255 status = MGMT_STATUS_REJECTED;
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02003256 else
3257 status = MGMT_STATUS_CONNECT_FAILED;
3258
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003259 err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02003260 status, &rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003261 sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003262 goto unlock;
3263 }
3264
3265 if (conn->connect_cfm_cb) {
David Herrmann76a68ba2013-04-06 20:28:37 +02003266 hci_conn_drop(conn);
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003267 err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003268 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003269 goto unlock;
3270 }
3271
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003272 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003273 if (!cmd) {
3274 err = -ENOMEM;
David Herrmann76a68ba2013-04-06 20:28:37 +02003275 hci_conn_drop(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003276 goto unlock;
3277 }
3278
Johan Hedberg04ab2742014-12-05 13:36:04 +02003279 cmd->cmd_complete = pairing_complete;
3280
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003281 /* For LE, just connecting isn't a proof that the pairing finished */
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003282 if (cp->addr.type == BDADDR_BREDR) {
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003283 conn->connect_cfm_cb = pairing_complete_cb;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003284 conn->security_cfm_cb = pairing_complete_cb;
3285 conn->disconn_cfm_cb = pairing_complete_cb;
3286 } else {
3287 conn->connect_cfm_cb = le_pairing_complete_cb;
3288 conn->security_cfm_cb = le_pairing_complete_cb;
3289 conn->disconn_cfm_cb = le_pairing_complete_cb;
3290 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003291
Johan Hedberge9a416b2011-02-19 12:05:56 -03003292 conn->io_capability = cp->io_cap;
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03003293 cmd->user_data = hci_conn_get(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003294
Johan Hedberg6f78fd42014-07-30 08:35:48 +03003295 if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) &&
Johan Hedberga511b352014-12-11 21:45:45 +02003296 hci_conn_security(conn, sec_level, auth_type, true)) {
3297 cmd->cmd_complete(cmd, 0);
3298 mgmt_pending_remove(cmd);
3299 }
Johan Hedberge9a416b2011-02-19 12:05:56 -03003300
3301 err = 0;
3302
3303unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003304 hci_dev_unlock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003305 return err;
3306}
3307
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003308static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3309 u16 len)
Johan Hedberg28424702012-02-02 04:02:29 +02003310{
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003311 struct mgmt_addr_info *addr = data;
Johan Hedberg28424702012-02-02 04:02:29 +02003312 struct pending_cmd *cmd;
3313 struct hci_conn *conn;
3314 int err;
3315
3316 BT_DBG("");
3317
Johan Hedberg28424702012-02-02 04:02:29 +02003318 hci_dev_lock(hdev);
3319
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003320 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003321 err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003322 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003323 goto unlock;
3324 }
3325
Johan Hedberg28424702012-02-02 04:02:29 +02003326 cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev);
3327 if (!cmd) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003328 err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003329 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003330 goto unlock;
3331 }
3332
3333 conn = cmd->user_data;
3334
3335 if (bacmp(&addr->bdaddr, &conn->dst) != 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003336 err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003337 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003338 goto unlock;
3339 }
3340
Johan Hedberga511b352014-12-11 21:45:45 +02003341 cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED);
3342 mgmt_pending_remove(cmd);
Johan Hedberg28424702012-02-02 04:02:29 +02003343
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003344 err = cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003345 addr, sizeof(*addr));
Johan Hedberg28424702012-02-02 04:02:29 +02003346unlock:
3347 hci_dev_unlock(hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02003348 return err;
3349}
3350
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003351static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
Johan Hedberg1707c602013-03-15 17:07:15 -05003352 struct mgmt_addr_info *addr, u16 mgmt_op,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003353 u16 hci_op, __le32 passkey)
Johan Hedberga5c29682011-02-19 12:05:57 -03003354{
Johan Hedberga5c29682011-02-19 12:05:57 -03003355 struct pending_cmd *cmd;
Brian Gix0df4c182011-11-16 13:53:13 -08003356 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03003357 int err;
3358
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003359 hci_dev_lock(hdev);
Johan Hedberg08ba5382011-03-16 14:29:34 +02003360
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003361 if (!hdev_is_powered(hdev)) {
Johan Hedbergfeb94d32013-03-15 17:07:16 -05003362 err = cmd_complete(sk, hdev->id, mgmt_op,
3363 MGMT_STATUS_NOT_POWERED, addr,
3364 sizeof(*addr));
Brian Gix0df4c182011-11-16 13:53:13 -08003365 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003366 }
3367
Johan Hedberg1707c602013-03-15 17:07:15 -05003368 if (addr->type == BDADDR_BREDR)
3369 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr);
Johan Hedberg272d90d2012-02-09 15:26:12 +02003370 else
Johan Hedberg1707c602013-03-15 17:07:15 -05003371 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr);
Brian Gix47c15e22011-11-16 13:53:14 -08003372
Johan Hedberg272d90d2012-02-09 15:26:12 +02003373 if (!conn) {
Johan Hedbergfeb94d32013-03-15 17:07:16 -05003374 err = cmd_complete(sk, hdev->id, mgmt_op,
3375 MGMT_STATUS_NOT_CONNECTED, addr,
3376 sizeof(*addr));
Johan Hedberg272d90d2012-02-09 15:26:12 +02003377 goto done;
3378 }
3379
Johan Hedberg1707c602013-03-15 17:07:15 -05003380 if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
Brian Gix5fe57d92011-12-21 16:12:13 -08003381 err = smp_user_confirm_reply(conn, mgmt_op, passkey);
Brian Gix5fe57d92011-12-21 16:12:13 -08003382 if (!err)
Johan Hedbergfeb94d32013-03-15 17:07:16 -05003383 err = cmd_complete(sk, hdev->id, mgmt_op,
3384 MGMT_STATUS_SUCCESS, addr,
3385 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003386 else
Johan Hedbergfeb94d32013-03-15 17:07:16 -05003387 err = cmd_complete(sk, hdev->id, mgmt_op,
3388 MGMT_STATUS_FAILED, addr,
3389 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003390
Brian Gix47c15e22011-11-16 13:53:14 -08003391 goto done;
3392 }
3393
Johan Hedberg1707c602013-03-15 17:07:15 -05003394 cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr));
Johan Hedberga5c29682011-02-19 12:05:57 -03003395 if (!cmd) {
3396 err = -ENOMEM;
Brian Gix0df4c182011-11-16 13:53:13 -08003397 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003398 }
3399
Johan Hedberg7776d1d2014-12-05 13:36:03 +02003400 cmd->cmd_complete = addr_cmd_complete;
3401
Brian Gix0df4c182011-11-16 13:53:13 -08003402 /* Continue with pairing via HCI */
Brian Gix604086b2011-11-23 08:28:33 -08003403 if (hci_op == HCI_OP_USER_PASSKEY_REPLY) {
3404 struct hci_cp_user_passkey_reply cp;
3405
Johan Hedberg1707c602013-03-15 17:07:15 -05003406 bacpy(&cp.bdaddr, &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003407 cp.passkey = passkey;
3408 err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
3409 } else
Johan Hedberg1707c602013-03-15 17:07:15 -05003410 err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr),
3411 &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003412
Johan Hedberga664b5b2011-02-19 12:06:02 -03003413 if (err < 0)
3414 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03003415
Brian Gix0df4c182011-11-16 13:53:13 -08003416done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003417 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03003418 return err;
3419}
3420
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303421static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
3422 void *data, u16 len)
3423{
3424 struct mgmt_cp_pin_code_neg_reply *cp = data;
3425
3426 BT_DBG("");
3427
Johan Hedberg1707c602013-03-15 17:07:15 -05003428 return user_pairing_resp(sk, hdev, &cp->addr,
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303429 MGMT_OP_PIN_CODE_NEG_REPLY,
3430 HCI_OP_PIN_CODE_NEG_REPLY, 0);
3431}
3432
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003433static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3434 u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003435{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003436 struct mgmt_cp_user_confirm_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003437
3438 BT_DBG("");
3439
3440 if (len != sizeof(*cp))
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003441 return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003442 MGMT_STATUS_INVALID_PARAMS);
Brian Gix0df4c182011-11-16 13:53:13 -08003443
Johan Hedberg1707c602013-03-15 17:07:15 -05003444 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003445 MGMT_OP_USER_CONFIRM_REPLY,
3446 HCI_OP_USER_CONFIRM_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003447}
3448
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003449static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003450 void *data, u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003451{
Johan Hedbergc9c26592011-12-15 00:47:41 +02003452 struct mgmt_cp_user_confirm_neg_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003453
3454 BT_DBG("");
3455
Johan Hedberg1707c602013-03-15 17:07:15 -05003456 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003457 MGMT_OP_USER_CONFIRM_NEG_REPLY,
3458 HCI_OP_USER_CONFIRM_NEG_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003459}
3460
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003461static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3462 u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003463{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003464 struct mgmt_cp_user_passkey_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003465
3466 BT_DBG("");
3467
Johan Hedberg1707c602013-03-15 17:07:15 -05003468 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003469 MGMT_OP_USER_PASSKEY_REPLY,
3470 HCI_OP_USER_PASSKEY_REPLY, cp->passkey);
Brian Gix604086b2011-11-23 08:28:33 -08003471}
3472
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003473static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003474 void *data, u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003475{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003476 struct mgmt_cp_user_passkey_neg_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003477
3478 BT_DBG("");
3479
Johan Hedberg1707c602013-03-15 17:07:15 -05003480 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003481 MGMT_OP_USER_PASSKEY_NEG_REPLY,
3482 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
Brian Gix604086b2011-11-23 08:28:33 -08003483}
3484
Johan Hedberg13928972013-03-15 17:07:00 -05003485static void update_name(struct hci_request *req)
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003486{
Johan Hedberg13928972013-03-15 17:07:00 -05003487 struct hci_dev *hdev = req->hdev;
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003488 struct hci_cp_write_local_name cp;
3489
Johan Hedberg13928972013-03-15 17:07:00 -05003490 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003491
Johan Hedberg890ea892013-03-15 17:06:52 -05003492 hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp);
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003493}
3494
Marcel Holtmann1904a852015-01-11 13:50:44 -08003495static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg13928972013-03-15 17:07:00 -05003496{
3497 struct mgmt_cp_set_local_name *cp;
3498 struct pending_cmd *cmd;
3499
3500 BT_DBG("status 0x%02x", status);
3501
3502 hci_dev_lock(hdev);
3503
3504 cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
3505 if (!cmd)
3506 goto unlock;
3507
3508 cp = cmd->param;
3509
3510 if (status)
3511 cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
3512 mgmt_status(status));
3513 else
3514 cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3515 cp, sizeof(*cp));
3516
3517 mgmt_pending_remove(cmd);
3518
3519unlock:
3520 hci_dev_unlock(hdev);
3521}
3522
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003523static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003524 u16 len)
Johan Hedbergb312b1612011-03-16 14:29:37 +02003525{
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003526 struct mgmt_cp_set_local_name *cp = data;
Johan Hedbergb312b1612011-03-16 14:29:37 +02003527 struct pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05003528 struct hci_request req;
Johan Hedbergb312b1612011-03-16 14:29:37 +02003529 int err;
3530
3531 BT_DBG("");
3532
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003533 hci_dev_lock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003534
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003535 /* If the old values are the same as the new ones just return a
3536 * direct command complete event.
3537 */
3538 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
3539 !memcmp(hdev->short_name, cp->short_name,
3540 sizeof(hdev->short_name))) {
3541 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3542 data, len);
3543 goto failed;
3544 }
3545
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003546 memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003547
Johan Hedbergb5235a62012-02-21 14:32:24 +02003548 if (!hdev_is_powered(hdev)) {
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003549 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003550
3551 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003552 data, len);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003553 if (err < 0)
3554 goto failed;
3555
3556 err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003557 sk);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003558
Johan Hedbergb5235a62012-02-21 14:32:24 +02003559 goto failed;
3560 }
3561
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003562 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003563 if (!cmd) {
3564 err = -ENOMEM;
3565 goto failed;
3566 }
3567
Johan Hedberg13928972013-03-15 17:07:00 -05003568 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3569
Johan Hedberg890ea892013-03-15 17:06:52 -05003570 hci_req_init(&req, hdev);
Johan Hedberg3f985052013-03-15 17:07:02 -05003571
3572 if (lmp_bredr_capable(hdev)) {
3573 update_name(&req);
3574 update_eir(&req);
3575 }
3576
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003577 /* The name is stored in the scan response data and so
3578 * no need to udpate the advertising data here.
3579 */
Johan Hedberg3f985052013-03-15 17:07:02 -05003580 if (lmp_le_capable(hdev))
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003581 update_scan_rsp_data(&req);
Johan Hedberg3f985052013-03-15 17:07:02 -05003582
Johan Hedberg13928972013-03-15 17:07:00 -05003583 err = hci_req_run(&req, set_name_complete);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003584 if (err < 0)
3585 mgmt_pending_remove(cmd);
3586
3587failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003588 hci_dev_unlock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003589 return err;
3590}
3591
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003592static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003593 void *data, u16 data_len)
Szymon Jancc35938b2011-03-22 13:12:21 +01003594{
Szymon Jancc35938b2011-03-22 13:12:21 +01003595 struct pending_cmd *cmd;
3596 int err;
3597
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003598 BT_DBG("%s", hdev->name);
Szymon Jancc35938b2011-03-22 13:12:21 +01003599
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003600 hci_dev_lock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003601
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003602 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003603 err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003604 MGMT_STATUS_NOT_POWERED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003605 goto unlock;
3606 }
3607
Andre Guedes9a1a1992012-07-24 15:03:48 -03003608 if (!lmp_ssp_capable(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003609 err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003610 MGMT_STATUS_NOT_SUPPORTED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003611 goto unlock;
3612 }
3613
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003614 if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003615 err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003616 MGMT_STATUS_BUSY);
Szymon Jancc35938b2011-03-22 13:12:21 +01003617 goto unlock;
3618 }
3619
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003620 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
Szymon Jancc35938b2011-03-22 13:12:21 +01003621 if (!cmd) {
3622 err = -ENOMEM;
3623 goto unlock;
3624 }
3625
Johan Hedberg710f11c2014-05-26 11:21:22 +03003626 if (bredr_sc_enabled(hdev))
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08003627 err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA,
3628 0, NULL);
3629 else
3630 err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
3631
Szymon Jancc35938b2011-03-22 13:12:21 +01003632 if (err < 0)
3633 mgmt_pending_remove(cmd);
3634
3635unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003636 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003637 return err;
3638}
3639
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003640static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003641 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003642{
Johan Hedberg5d57e792015-01-23 10:10:38 +02003643 struct mgmt_addr_info *addr = data;
Szymon Janc2763eda2011-03-22 13:12:22 +01003644 int err;
3645
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003646 BT_DBG("%s ", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003647
Johan Hedberg5d57e792015-01-23 10:10:38 +02003648 if (!bdaddr_type_is_valid(addr->type))
3649 return cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3650 MGMT_STATUS_INVALID_PARAMS, addr,
3651 sizeof(*addr));
3652
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003653 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003654
Marcel Holtmannec109112014-01-10 02:07:30 -08003655 if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) {
3656 struct mgmt_cp_add_remote_oob_data *cp = data;
3657 u8 status;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003658
Johan Hedbergc19a4952014-11-17 20:52:19 +02003659 if (cp->addr.type != BDADDR_BREDR) {
3660 err = cmd_complete(sk, hdev->id,
3661 MGMT_OP_ADD_REMOTE_OOB_DATA,
3662 MGMT_STATUS_INVALID_PARAMS,
3663 &cp->addr, sizeof(cp->addr));
3664 goto unlock;
3665 }
3666
Marcel Holtmannec109112014-01-10 02:07:30 -08003667 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg6928a922014-10-26 20:46:09 +01003668 cp->addr.type, cp->hash,
3669 cp->rand, NULL, NULL);
Marcel Holtmannec109112014-01-10 02:07:30 -08003670 if (err < 0)
3671 status = MGMT_STATUS_FAILED;
3672 else
3673 status = MGMT_STATUS_SUCCESS;
3674
3675 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3676 status, &cp->addr, sizeof(cp->addr));
3677 } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) {
3678 struct mgmt_cp_add_remote_oob_ext_data *cp = data;
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003679 u8 *rand192, *hash192, *rand256, *hash256;
Marcel Holtmannec109112014-01-10 02:07:30 -08003680 u8 status;
3681
Johan Hedberg86df9202014-10-26 20:52:27 +01003682 if (bdaddr_type_is_le(cp->addr.type)) {
Johan Hedbergd25b78e2015-01-27 12:55:52 +02003683 /* Enforce zero-valued 192-bit parameters as
3684 * long as legacy SMP OOB isn't implemented.
3685 */
3686 if (memcmp(cp->rand192, ZERO_KEY, 16) ||
3687 memcmp(cp->hash192, ZERO_KEY, 16)) {
3688 err = cmd_complete(sk, hdev->id,
3689 MGMT_OP_ADD_REMOTE_OOB_DATA,
3690 MGMT_STATUS_INVALID_PARAMS,
3691 addr, sizeof(*addr));
3692 goto unlock;
3693 }
3694
Johan Hedberg86df9202014-10-26 20:52:27 +01003695 rand192 = NULL;
3696 hash192 = NULL;
3697 } else {
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003698 /* In case one of the P-192 values is set to zero,
3699 * then just disable OOB data for P-192.
3700 */
3701 if (!memcmp(cp->rand192, ZERO_KEY, 16) ||
3702 !memcmp(cp->hash192, ZERO_KEY, 16)) {
3703 rand192 = NULL;
3704 hash192 = NULL;
3705 } else {
3706 rand192 = cp->rand192;
3707 hash192 = cp->hash192;
3708 }
3709 }
3710
3711 /* In case one of the P-256 values is set to zero, then just
3712 * disable OOB data for P-256.
3713 */
3714 if (!memcmp(cp->rand256, ZERO_KEY, 16) ||
3715 !memcmp(cp->hash256, ZERO_KEY, 16)) {
3716 rand256 = NULL;
3717 hash256 = NULL;
3718 } else {
3719 rand256 = cp->rand256;
3720 hash256 = cp->hash256;
Johan Hedberg86df9202014-10-26 20:52:27 +01003721 }
3722
Johan Hedberg81328d52014-10-26 20:33:47 +01003723 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg86df9202014-10-26 20:52:27 +01003724 cp->addr.type, hash192, rand192,
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003725 hash256, rand256);
Marcel Holtmannec109112014-01-10 02:07:30 -08003726 if (err < 0)
3727 status = MGMT_STATUS_FAILED;
3728 else
3729 status = MGMT_STATUS_SUCCESS;
3730
3731 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3732 status, &cp->addr, sizeof(cp->addr));
3733 } else {
3734 BT_ERR("add_remote_oob_data: invalid length of %u bytes", len);
3735 err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3736 MGMT_STATUS_INVALID_PARAMS);
3737 }
Szymon Janc2763eda2011-03-22 13:12:22 +01003738
Johan Hedbergc19a4952014-11-17 20:52:19 +02003739unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003740 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003741 return err;
3742}
3743
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003744static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003745 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003746{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003747 struct mgmt_cp_remove_remote_oob_data *cp = data;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003748 u8 status;
Szymon Janc2763eda2011-03-22 13:12:22 +01003749 int err;
3750
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003751 BT_DBG("%s", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003752
Johan Hedbergc19a4952014-11-17 20:52:19 +02003753 if (cp->addr.type != BDADDR_BREDR)
3754 return cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
3755 MGMT_STATUS_INVALID_PARAMS,
3756 &cp->addr, sizeof(cp->addr));
3757
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003758 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003759
Johan Hedbergeedbd582014-11-15 09:34:23 +02003760 if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
3761 hci_remote_oob_data_clear(hdev);
3762 status = MGMT_STATUS_SUCCESS;
3763 goto done;
3764 }
3765
Johan Hedberg6928a922014-10-26 20:46:09 +01003766 err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type);
Szymon Janc2763eda2011-03-22 13:12:22 +01003767 if (err < 0)
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003768 status = MGMT_STATUS_INVALID_PARAMS;
Szymon Janc2763eda2011-03-22 13:12:22 +01003769 else
Szymon Janca6785be2012-12-13 15:11:21 +01003770 status = MGMT_STATUS_SUCCESS;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003771
Johan Hedbergeedbd582014-11-15 09:34:23 +02003772done:
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003773 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003774 status, &cp->addr, sizeof(cp->addr));
Szymon Janc2763eda2011-03-22 13:12:22 +01003775
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003776 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003777 return err;
3778}
3779
Marcel Holtmann80190442014-12-04 11:36:36 +01003780static bool trigger_discovery(struct hci_request *req, u8 *status)
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003781{
Marcel Holtmann80190442014-12-04 11:36:36 +01003782 struct hci_dev *hdev = req->hdev;
3783 struct hci_cp_le_set_scan_param param_cp;
3784 struct hci_cp_le_set_scan_enable enable_cp;
3785 struct hci_cp_inquiry inq_cp;
3786 /* General inquiry access code (GIAC) */
3787 u8 lap[3] = { 0x33, 0x8b, 0x9e };
3788 u8 own_addr_type;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003789 int err;
3790
Marcel Holtmann80190442014-12-04 11:36:36 +01003791 switch (hdev->discovery.type) {
3792 case DISCOV_TYPE_BREDR:
3793 *status = mgmt_bredr_support(hdev);
3794 if (*status)
3795 return false;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003796
Marcel Holtmann80190442014-12-04 11:36:36 +01003797 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
3798 *status = MGMT_STATUS_BUSY;
3799 return false;
3800 }
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003801
Marcel Holtmann80190442014-12-04 11:36:36 +01003802 hci_inquiry_cache_flush(hdev);
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003803
Marcel Holtmann80190442014-12-04 11:36:36 +01003804 memset(&inq_cp, 0, sizeof(inq_cp));
3805 memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap));
3806 inq_cp.length = DISCOV_BREDR_INQUIRY_LEN;
3807 hci_req_add(req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp);
3808 break;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003809
Marcel Holtmann80190442014-12-04 11:36:36 +01003810 case DISCOV_TYPE_LE:
3811 case DISCOV_TYPE_INTERLEAVED:
3812 *status = mgmt_le_support(hdev);
3813 if (*status)
3814 return false;
3815
3816 if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED &&
3817 !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
3818 *status = MGMT_STATUS_NOT_SUPPORTED;
3819 return false;
3820 }
3821
3822 if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) {
3823 /* Don't let discovery abort an outgoing
3824 * connection attempt that's using directed
3825 * advertising.
3826 */
3827 if (hci_conn_hash_lookup_state(hdev, LE_LINK,
3828 BT_CONNECT)) {
3829 *status = MGMT_STATUS_REJECTED;
3830 return false;
3831 }
3832
3833 disable_advertising(req);
3834 }
3835
3836 /* If controller is scanning, it means the background scanning
3837 * is running. Thus, we should temporarily stop it in order to
3838 * set the discovery scanning parameters.
3839 */
3840 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags))
3841 hci_req_add_le_scan_disable(req);
3842
3843 memset(&param_cp, 0, sizeof(param_cp));
3844
3845 /* All active scans will be done with either a resolvable
3846 * private address (when privacy feature has been enabled)
Marcel Holtmann9437d2e2014-12-07 20:13:17 +01003847 * or non-resolvable private address.
Marcel Holtmann80190442014-12-04 11:36:36 +01003848 */
3849 err = hci_update_random_address(req, true, &own_addr_type);
3850 if (err < 0) {
3851 *status = MGMT_STATUS_FAILED;
3852 return false;
3853 }
3854
3855 param_cp.type = LE_SCAN_ACTIVE;
3856 param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT);
3857 param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN);
3858 param_cp.own_address_type = own_addr_type;
3859 hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
3860 &param_cp);
3861
3862 memset(&enable_cp, 0, sizeof(enable_cp));
3863 enable_cp.enable = LE_SCAN_ENABLE;
3864 enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
3865 hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
3866 &enable_cp);
3867 break;
3868
3869 default:
3870 *status = MGMT_STATUS_INVALID_PARAMS;
3871 return false;
3872 }
3873
3874 return true;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003875}
3876
Marcel Holtmann1904a852015-01-11 13:50:44 -08003877static void start_discovery_complete(struct hci_dev *hdev, u8 status,
3878 u16 opcode)
Andre Guedes7c307722013-04-30 15:29:28 -03003879{
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003880 struct pending_cmd *cmd;
3881 unsigned long timeout;
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01003882
Andre Guedes7c307722013-04-30 15:29:28 -03003883 BT_DBG("status %d", status);
3884
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003885 hci_dev_lock(hdev);
3886
3887 cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003888 if (!cmd)
3889 cmd = mgmt_pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev);
3890
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003891 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02003892 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003893 mgmt_pending_remove(cmd);
Andre Guedes7c307722013-04-30 15:29:28 -03003894 }
3895
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003896 if (status) {
3897 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
3898 goto unlock;
3899 }
3900
Andre Guedes7c307722013-04-30 15:29:28 -03003901 hci_discovery_set_state(hdev, DISCOVERY_FINDING);
Andre Guedes7c307722013-04-30 15:29:28 -03003902
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08003903 /* If the scan involves LE scan, pick proper timeout to schedule
3904 * hdev->le_scan_disable that will stop it.
3905 */
Andre Guedes7c307722013-04-30 15:29:28 -03003906 switch (hdev->discovery.type) {
3907 case DISCOV_TYPE_LE:
Lukasz Rymanowski3d5a76f2014-03-27 20:55:21 +01003908 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
Andre Guedes7c307722013-04-30 15:29:28 -03003909 break;
Andre Guedes7c307722013-04-30 15:29:28 -03003910 case DISCOV_TYPE_INTERLEAVED:
Lukasz Rymanowskib9a7a612014-03-27 20:55:20 +01003911 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
Andre Guedes7c307722013-04-30 15:29:28 -03003912 break;
Andre Guedes7c307722013-04-30 15:29:28 -03003913 case DISCOV_TYPE_BREDR:
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003914 timeout = 0;
Andre Guedes7c307722013-04-30 15:29:28 -03003915 break;
Andre Guedes7c307722013-04-30 15:29:28 -03003916 default:
3917 BT_ERR("Invalid discovery type %d", hdev->discovery.type);
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003918 timeout = 0;
3919 break;
Andre Guedes7c307722013-04-30 15:29:28 -03003920 }
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01003921
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08003922 if (timeout) {
3923 /* When service discovery is used and the controller has
3924 * a strict duplicate filter, it is important to remember
3925 * the start and duration of the scan. This is required
3926 * for restarting scanning during the discovery phase.
3927 */
3928 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER,
3929 &hdev->quirks) &&
3930 (hdev->discovery.uuid_count > 0 ||
3931 hdev->discovery.rssi != HCI_RSSI_INVALID)) {
3932 hdev->discovery.scan_start = jiffies;
3933 hdev->discovery.scan_duration = timeout;
3934 }
3935
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003936 queue_delayed_work(hdev->workqueue,
3937 &hdev->le_scan_disable, timeout);
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08003938 }
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01003939
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003940unlock:
3941 hci_dev_unlock(hdev);
Andre Guedes7c307722013-04-30 15:29:28 -03003942}
3943
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003944static int start_discovery(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003945 void *data, u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04003946{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003947 struct mgmt_cp_start_discovery *cp = data;
Johan Hedberg14a53662011-04-27 10:29:56 -04003948 struct pending_cmd *cmd;
Andre Guedes7c307722013-04-30 15:29:28 -03003949 struct hci_request req;
Marcel Holtmann80190442014-12-04 11:36:36 +01003950 u8 status;
Johan Hedberg14a53662011-04-27 10:29:56 -04003951 int err;
3952
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003953 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04003954
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003955 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04003956
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003957 if (!hdev_is_powered(hdev)) {
Szymon Janca736abc2014-11-03 14:20:56 +01003958 err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
3959 MGMT_STATUS_NOT_POWERED,
3960 &cp->type, sizeof(cp->type));
Johan Hedbergbd2d1332011-11-07 23:13:37 +02003961 goto failed;
3962 }
3963
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01003964 if (hdev->discovery.state != DISCOVERY_STOPPED ||
3965 test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) {
Szymon Janca736abc2014-11-03 14:20:56 +01003966 err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
3967 MGMT_STATUS_BUSY, &cp->type,
3968 sizeof(cp->type));
Andre Guedes642be6c2012-03-21 00:03:37 -03003969 goto failed;
3970 }
3971
Johan Hedberg2922a942014-12-05 13:36:06 +02003972 cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, data, len);
Johan Hedberg14a53662011-04-27 10:29:56 -04003973 if (!cmd) {
3974 err = -ENOMEM;
3975 goto failed;
3976 }
3977
Johan Hedberg2922a942014-12-05 13:36:06 +02003978 cmd->cmd_complete = generic_cmd_complete;
3979
Marcel Holtmann22078802014-12-05 11:45:22 +01003980 /* Clear the discovery filter first to free any previously
3981 * allocated memory for the UUID list.
3982 */
3983 hci_discovery_filter_clear(hdev);
3984
Andre Guedes4aab14e2012-02-17 20:39:36 -03003985 hdev->discovery.type = cp->type;
Marcel Holtmannda25cf62014-12-05 13:03:35 +01003986 hdev->discovery.report_invalid_rssi = false;
Andre Guedes4aab14e2012-02-17 20:39:36 -03003987
Andre Guedes7c307722013-04-30 15:29:28 -03003988 hci_req_init(&req, hdev);
3989
Marcel Holtmann80190442014-12-04 11:36:36 +01003990 if (!trigger_discovery(&req, &status)) {
Szymon Janca736abc2014-11-03 14:20:56 +01003991 err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
Marcel Holtmann80190442014-12-04 11:36:36 +01003992 status, &cp->type, sizeof(cp->type));
Johan Hedberg04106752013-01-10 14:54:09 +02003993 mgmt_pending_remove(cmd);
3994 goto failed;
Andre Guedesf39799f2012-02-17 20:39:35 -03003995 }
Andre Guedes3fd24152012-02-03 17:48:01 -03003996
Andre Guedes7c307722013-04-30 15:29:28 -03003997 err = hci_req_run(&req, start_discovery_complete);
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01003998 if (err < 0) {
Johan Hedberg14a53662011-04-27 10:29:56 -04003999 mgmt_pending_remove(cmd);
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004000 goto failed;
4001 }
4002
4003 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
Johan Hedberg14a53662011-04-27 10:29:56 -04004004
4005failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004006 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004007 return err;
4008}
4009
Johan Hedberg9df74652014-12-19 22:26:03 +02004010static int service_discovery_cmd_complete(struct pending_cmd *cmd, u8 status)
Andre Guedes1183fdc2013-04-30 15:29:35 -03004011{
Johan Hedberg9df74652014-12-19 22:26:03 +02004012 return cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
4013 cmd->param, 1);
Johan Hedberg2922a942014-12-05 13:36:06 +02004014}
4015
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004016static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
4017 void *data, u16 len)
4018{
4019 struct mgmt_cp_start_service_discovery *cp = data;
Andre Guedes1183fdc2013-04-30 15:29:35 -03004020 struct pending_cmd *cmd;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004021 struct hci_request req;
4022 const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16);
4023 u16 uuid_count, expected_len;
4024 u8 status;
Andre Guedes1183fdc2013-04-30 15:29:35 -03004025 int err;
4026
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004027 BT_DBG("%s", hdev->name);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004028
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004029 hci_dev_lock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004030
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004031 if (!hdev_is_powered(hdev)) {
4032 err = cmd_complete(sk, hdev->id,
4033 MGMT_OP_START_SERVICE_DISCOVERY,
4034 MGMT_STATUS_NOT_POWERED,
4035 &cp->type, sizeof(cp->type));
4036 goto failed;
4037 }
4038
4039 if (hdev->discovery.state != DISCOVERY_STOPPED ||
4040 test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) {
4041 err = cmd_complete(sk, hdev->id,
4042 MGMT_OP_START_SERVICE_DISCOVERY,
4043 MGMT_STATUS_BUSY, &cp->type,
4044 sizeof(cp->type));
4045 goto failed;
4046 }
4047
4048 uuid_count = __le16_to_cpu(cp->uuid_count);
4049 if (uuid_count > max_uuid_count) {
4050 BT_ERR("service_discovery: too big uuid_count value %u",
4051 uuid_count);
4052 err = cmd_complete(sk, hdev->id,
4053 MGMT_OP_START_SERVICE_DISCOVERY,
4054 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4055 sizeof(cp->type));
4056 goto failed;
4057 }
4058
4059 expected_len = sizeof(*cp) + uuid_count * 16;
4060 if (expected_len != len) {
4061 BT_ERR("service_discovery: expected %u bytes, got %u bytes",
4062 expected_len, len);
4063 err = cmd_complete(sk, hdev->id,
4064 MGMT_OP_START_SERVICE_DISCOVERY,
4065 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4066 sizeof(cp->type));
4067 goto failed;
4068 }
4069
4070 cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY,
Johan Hedberg2922a942014-12-05 13:36:06 +02004071 hdev, data, len);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004072 if (!cmd) {
4073 err = -ENOMEM;
4074 goto failed;
4075 }
4076
Johan Hedberg2922a942014-12-05 13:36:06 +02004077 cmd->cmd_complete = service_discovery_cmd_complete;
4078
Marcel Holtmann22078802014-12-05 11:45:22 +01004079 /* Clear the discovery filter first to free any previously
4080 * allocated memory for the UUID list.
4081 */
4082 hci_discovery_filter_clear(hdev);
4083
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004084 hdev->discovery.type = cp->type;
4085 hdev->discovery.rssi = cp->rssi;
4086 hdev->discovery.uuid_count = uuid_count;
4087
4088 if (uuid_count > 0) {
4089 hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16,
4090 GFP_KERNEL);
4091 if (!hdev->discovery.uuids) {
4092 err = cmd_complete(sk, hdev->id,
4093 MGMT_OP_START_SERVICE_DISCOVERY,
4094 MGMT_STATUS_FAILED,
4095 &cp->type, sizeof(cp->type));
4096 mgmt_pending_remove(cmd);
4097 goto failed;
4098 }
4099 }
4100
4101 hci_req_init(&req, hdev);
4102
4103 if (!trigger_discovery(&req, &status)) {
4104 err = cmd_complete(sk, hdev->id,
4105 MGMT_OP_START_SERVICE_DISCOVERY,
4106 status, &cp->type, sizeof(cp->type));
4107 mgmt_pending_remove(cmd);
4108 goto failed;
4109 }
4110
4111 err = hci_req_run(&req, start_discovery_complete);
4112 if (err < 0) {
4113 mgmt_pending_remove(cmd);
4114 goto failed;
4115 }
4116
4117 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
4118
4119failed:
4120 hci_dev_unlock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004121 return err;
4122}
4123
Marcel Holtmann1904a852015-01-11 13:50:44 -08004124static void stop_discovery_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Andre Guedes0e05bba2013-04-30 15:29:33 -03004125{
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004126 struct pending_cmd *cmd;
4127
Andre Guedes0e05bba2013-04-30 15:29:33 -03004128 BT_DBG("status %d", status);
4129
4130 hci_dev_lock(hdev);
4131
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004132 cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
4133 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02004134 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004135 mgmt_pending_remove(cmd);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004136 }
4137
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004138 if (!status)
4139 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004140
Andre Guedes0e05bba2013-04-30 15:29:33 -03004141 hci_dev_unlock(hdev);
4142}
4143
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004144static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004145 u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04004146{
Johan Hedbergd9306502012-02-20 23:25:18 +02004147 struct mgmt_cp_stop_discovery *mgmt_cp = data;
Johan Hedberg14a53662011-04-27 10:29:56 -04004148 struct pending_cmd *cmd;
Andre Guedes0e05bba2013-04-30 15:29:33 -03004149 struct hci_request req;
Johan Hedberg14a53662011-04-27 10:29:56 -04004150 int err;
4151
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004152 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04004153
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004154 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004155
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004156 if (!hci_discovery_active(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004157 err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004158 MGMT_STATUS_REJECTED, &mgmt_cp->type,
4159 sizeof(mgmt_cp->type));
Johan Hedbergd9306502012-02-20 23:25:18 +02004160 goto unlock;
4161 }
4162
4163 if (hdev->discovery.type != mgmt_cp->type) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004164 err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004165 MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type,
4166 sizeof(mgmt_cp->type));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004167 goto unlock;
Johan Hedbergff9ef572012-01-04 14:23:45 +02004168 }
4169
Johan Hedberg2922a942014-12-05 13:36:06 +02004170 cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len);
Johan Hedberg14a53662011-04-27 10:29:56 -04004171 if (!cmd) {
4172 err = -ENOMEM;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004173 goto unlock;
Johan Hedberg14a53662011-04-27 10:29:56 -04004174 }
4175
Johan Hedberg2922a942014-12-05 13:36:06 +02004176 cmd->cmd_complete = generic_cmd_complete;
4177
Andre Guedes0e05bba2013-04-30 15:29:33 -03004178 hci_req_init(&req, hdev);
4179
Johan Hedberg21a60d32014-06-10 14:05:58 +03004180 hci_stop_discovery(&req);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004181
Johan Hedberg21a60d32014-06-10 14:05:58 +03004182 err = hci_req_run(&req, stop_discovery_complete);
4183 if (!err) {
4184 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004185 goto unlock;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004186 }
4187
Johan Hedberg21a60d32014-06-10 14:05:58 +03004188 mgmt_pending_remove(cmd);
4189
4190 /* If no HCI commands were sent we're done */
4191 if (err == -ENODATA) {
4192 err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0,
4193 &mgmt_cp->type, sizeof(mgmt_cp->type));
4194 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4195 }
Johan Hedberg14a53662011-04-27 10:29:56 -04004196
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004197unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004198 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004199 return err;
4200}
4201
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004202static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004203 u16 len)
Johan Hedberg561aafb2012-01-04 13:31:59 +02004204{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004205 struct mgmt_cp_confirm_name *cp = data;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004206 struct inquiry_entry *e;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004207 int err;
4208
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004209 BT_DBG("%s", hdev->name);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004210
Johan Hedberg561aafb2012-01-04 13:31:59 +02004211 hci_dev_lock(hdev);
4212
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004213 if (!hci_discovery_active(hdev)) {
Lukasz Rymanowskid3a25412014-02-27 16:47:28 +01004214 err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4215 MGMT_STATUS_FAILED, &cp->addr,
4216 sizeof(cp->addr));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004217 goto failed;
4218 }
4219
Johan Hedberga198e7b2012-02-17 14:27:06 +02004220 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004221 if (!e) {
Lukasz Rymanowskid3a25412014-02-27 16:47:28 +01004222 err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4223 MGMT_STATUS_INVALID_PARAMS, &cp->addr,
4224 sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02004225 goto failed;
4226 }
4227
4228 if (cp->name_known) {
4229 e->name_state = NAME_KNOWN;
4230 list_del(&e->list);
4231 } else {
4232 e->name_state = NAME_NEEDED;
Johan Hedberga3d4e202012-01-09 00:53:02 +02004233 hci_inquiry_cache_update_resolve(hdev, e);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004234 }
4235
Johan Hedberge3846622013-01-09 15:29:33 +02004236 err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr,
4237 sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02004238
4239failed:
4240 hci_dev_unlock(hdev);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004241 return err;
4242}
4243
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004244static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004245 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03004246{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004247 struct mgmt_cp_block_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004248 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03004249 int err;
4250
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004251 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03004252
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004253 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg5d0846d2013-01-20 14:27:22 +02004254 return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
4255 MGMT_STATUS_INVALID_PARAMS,
4256 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004257
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004258 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03004259
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004260 err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr,
4261 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004262 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004263 status = MGMT_STATUS_FAILED;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004264 goto done;
4265 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004266
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004267 mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4268 sk);
4269 status = MGMT_STATUS_SUCCESS;
4270
4271done:
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004272 err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004273 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03004274
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004275 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03004276
4277 return err;
4278}
4279
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004280static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004281 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03004282{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004283 struct mgmt_cp_unblock_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004284 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03004285 int err;
4286
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004287 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03004288
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004289 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg5d0846d2013-01-20 14:27:22 +02004290 return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
4291 MGMT_STATUS_INVALID_PARAMS,
4292 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004293
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004294 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03004295
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004296 err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr,
4297 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004298 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004299 status = MGMT_STATUS_INVALID_PARAMS;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004300 goto done;
4301 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004302
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004303 mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4304 sk);
4305 status = MGMT_STATUS_SUCCESS;
4306
4307done:
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004308 err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004309 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03004310
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004311 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03004312
4313 return err;
4314}
4315
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004316static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
4317 u16 len)
4318{
4319 struct mgmt_cp_set_device_id *cp = data;
Johan Hedberg890ea892013-03-15 17:06:52 -05004320 struct hci_request req;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004321 int err;
Szymon Jancc72d4b82012-03-16 16:02:57 +01004322 __u16 source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004323
4324 BT_DBG("%s", hdev->name);
4325
Szymon Jancc72d4b82012-03-16 16:02:57 +01004326 source = __le16_to_cpu(cp->source);
4327
4328 if (source > 0x0002)
4329 return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
4330 MGMT_STATUS_INVALID_PARAMS);
4331
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004332 hci_dev_lock(hdev);
4333
Szymon Jancc72d4b82012-03-16 16:02:57 +01004334 hdev->devid_source = source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004335 hdev->devid_vendor = __le16_to_cpu(cp->vendor);
4336 hdev->devid_product = __le16_to_cpu(cp->product);
4337 hdev->devid_version = __le16_to_cpu(cp->version);
4338
4339 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0);
4340
Johan Hedberg890ea892013-03-15 17:06:52 -05004341 hci_req_init(&req, hdev);
4342 update_eir(&req);
4343 hci_req_run(&req, NULL);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004344
4345 hci_dev_unlock(hdev);
4346
4347 return err;
4348}
4349
Marcel Holtmann1904a852015-01-11 13:50:44 -08004350static void set_advertising_complete(struct hci_dev *hdev, u8 status,
4351 u16 opcode)
Johan Hedberg4375f102013-09-25 13:26:10 +03004352{
4353 struct cmd_lookup match = { NULL, hdev };
4354
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304355 hci_dev_lock(hdev);
4356
Johan Hedberg4375f102013-09-25 13:26:10 +03004357 if (status) {
4358 u8 mgmt_err = mgmt_status(status);
4359
4360 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev,
4361 cmd_status_rsp, &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304362 goto unlock;
Johan Hedberg4375f102013-09-25 13:26:10 +03004363 }
4364
Johan Hedbergc93bd152014-07-08 15:07:48 +03004365 if (test_bit(HCI_LE_ADV, &hdev->dev_flags))
4366 set_bit(HCI_ADVERTISING, &hdev->dev_flags);
4367 else
4368 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
4369
Johan Hedberg4375f102013-09-25 13:26:10 +03004370 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
4371 &match);
4372
4373 new_settings(hdev, match.sk);
4374
4375 if (match.sk)
4376 sock_put(match.sk);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304377
4378unlock:
4379 hci_dev_unlock(hdev);
Johan Hedberg4375f102013-09-25 13:26:10 +03004380}
4381
Marcel Holtmann21b51872013-10-10 09:47:53 -07004382static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
4383 u16 len)
Johan Hedberg4375f102013-09-25 13:26:10 +03004384{
4385 struct mgmt_mode *cp = data;
4386 struct pending_cmd *cmd;
4387 struct hci_request req;
Johan Hedberge6fe7982013-10-02 15:45:22 +03004388 u8 val, enabled, status;
Johan Hedberg4375f102013-09-25 13:26:10 +03004389 int err;
4390
4391 BT_DBG("request for %s", hdev->name);
4392
Johan Hedberge6fe7982013-10-02 15:45:22 +03004393 status = mgmt_le_support(hdev);
4394 if (status)
Johan Hedberg4375f102013-09-25 13:26:10 +03004395 return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
Johan Hedberge6fe7982013-10-02 15:45:22 +03004396 status);
Johan Hedberg4375f102013-09-25 13:26:10 +03004397
4398 if (cp->val != 0x00 && cp->val != 0x01)
4399 return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4400 MGMT_STATUS_INVALID_PARAMS);
4401
4402 hci_dev_lock(hdev);
4403
4404 val = !!cp->val;
Johan Hedbergf3d3444a2013-10-05 12:01:04 +02004405 enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags);
Johan Hedberg4375f102013-09-25 13:26:10 +03004406
Johan Hedbergf74ca9b2013-10-08 15:52:18 +02004407 /* The following conditions are ones which mean that we should
4408 * not do any HCI communication but directly send a mgmt
4409 * response to user space (after toggling the flag if
4410 * necessary).
4411 */
4412 if (!hdev_is_powered(hdev) || val == enabled ||
Johan Hedberge8bb6b92014-07-08 15:07:53 +03004413 hci_conn_num(hdev, LE_LINK) > 0 ||
4414 (test_bit(HCI_LE_SCAN, &hdev->dev_flags) &&
4415 hdev->le_scan_type == LE_SCAN_ACTIVE)) {
Johan Hedberg4375f102013-09-25 13:26:10 +03004416 bool changed = false;
4417
Johan Hedbergf3d3444a2013-10-05 12:01:04 +02004418 if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
4419 change_bit(HCI_ADVERTISING, &hdev->dev_flags);
Johan Hedberg4375f102013-09-25 13:26:10 +03004420 changed = true;
4421 }
4422
4423 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
4424 if (err < 0)
4425 goto unlock;
4426
4427 if (changed)
4428 err = new_settings(hdev, sk);
4429
4430 goto unlock;
4431 }
4432
4433 if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
4434 mgmt_pending_find(MGMT_OP_SET_LE, hdev)) {
4435 err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4436 MGMT_STATUS_BUSY);
4437 goto unlock;
4438 }
4439
4440 cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
4441 if (!cmd) {
4442 err = -ENOMEM;
4443 goto unlock;
4444 }
4445
4446 hci_req_init(&req, hdev);
4447
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07004448 if (val)
4449 enable_advertising(&req);
4450 else
4451 disable_advertising(&req);
Johan Hedberg4375f102013-09-25 13:26:10 +03004452
4453 err = hci_req_run(&req, set_advertising_complete);
4454 if (err < 0)
4455 mgmt_pending_remove(cmd);
4456
4457unlock:
4458 hci_dev_unlock(hdev);
4459 return err;
4460}
4461
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004462static int set_static_address(struct sock *sk, struct hci_dev *hdev,
4463 void *data, u16 len)
4464{
4465 struct mgmt_cp_set_static_address *cp = data;
4466 int err;
4467
4468 BT_DBG("%s", hdev->name);
4469
Marcel Holtmann62af4442013-10-02 22:10:32 -07004470 if (!lmp_le_capable(hdev))
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004471 return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
Marcel Holtmann62af4442013-10-02 22:10:32 -07004472 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004473
4474 if (hdev_is_powered(hdev))
4475 return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4476 MGMT_STATUS_REJECTED);
4477
4478 if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
4479 if (!bacmp(&cp->bdaddr, BDADDR_NONE))
4480 return cmd_status(sk, hdev->id,
4481 MGMT_OP_SET_STATIC_ADDRESS,
4482 MGMT_STATUS_INVALID_PARAMS);
4483
4484 /* Two most significant bits shall be set */
4485 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
4486 return cmd_status(sk, hdev->id,
4487 MGMT_OP_SET_STATIC_ADDRESS,
4488 MGMT_STATUS_INVALID_PARAMS);
4489 }
4490
4491 hci_dev_lock(hdev);
4492
4493 bacpy(&hdev->static_addr, &cp->bdaddr);
4494
4495 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0);
4496
4497 hci_dev_unlock(hdev);
4498
4499 return err;
4500}
4501
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004502static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
4503 void *data, u16 len)
4504{
4505 struct mgmt_cp_set_scan_params *cp = data;
4506 __u16 interval, window;
4507 int err;
4508
4509 BT_DBG("%s", hdev->name);
4510
4511 if (!lmp_le_capable(hdev))
4512 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4513 MGMT_STATUS_NOT_SUPPORTED);
4514
4515 interval = __le16_to_cpu(cp->interval);
4516
4517 if (interval < 0x0004 || interval > 0x4000)
4518 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4519 MGMT_STATUS_INVALID_PARAMS);
4520
4521 window = __le16_to_cpu(cp->window);
4522
4523 if (window < 0x0004 || window > 0x4000)
4524 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4525 MGMT_STATUS_INVALID_PARAMS);
4526
Marcel Holtmann899e1072013-10-14 09:55:32 -07004527 if (window > interval)
4528 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4529 MGMT_STATUS_INVALID_PARAMS);
4530
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004531 hci_dev_lock(hdev);
4532
4533 hdev->le_scan_interval = interval;
4534 hdev->le_scan_window = window;
4535
4536 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0);
4537
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03004538 /* If background scan is running, restart it so new parameters are
4539 * loaded.
4540 */
4541 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) &&
4542 hdev->discovery.state == DISCOVERY_STOPPED) {
4543 struct hci_request req;
4544
4545 hci_req_init(&req, hdev);
4546
4547 hci_req_add_le_scan_disable(&req);
4548 hci_req_add_le_passive_scan(&req);
4549
4550 hci_req_run(&req, NULL);
4551 }
4552
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004553 hci_dev_unlock(hdev);
4554
4555 return err;
4556}
4557
Marcel Holtmann1904a852015-01-11 13:50:44 -08004558static void fast_connectable_complete(struct hci_dev *hdev, u8 status,
4559 u16 opcode)
Johan Hedberg33e38b32013-03-15 17:07:05 -05004560{
4561 struct pending_cmd *cmd;
4562
4563 BT_DBG("status 0x%02x", status);
4564
4565 hci_dev_lock(hdev);
4566
4567 cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev);
4568 if (!cmd)
4569 goto unlock;
4570
4571 if (status) {
4572 cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4573 mgmt_status(status));
4574 } else {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004575 struct mgmt_mode *cp = cmd->param;
4576
4577 if (cp->val)
4578 set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags);
4579 else
4580 clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags);
4581
Johan Hedberg33e38b32013-03-15 17:07:05 -05004582 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
4583 new_settings(hdev, cmd->sk);
4584 }
4585
4586 mgmt_pending_remove(cmd);
4587
4588unlock:
4589 hci_dev_unlock(hdev);
4590}
4591
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004592static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004593 void *data, u16 len)
Antti Julkuf6422ec2011-06-22 13:11:56 +03004594{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004595 struct mgmt_mode *cp = data;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004596 struct pending_cmd *cmd;
4597 struct hci_request req;
Antti Julkuf6422ec2011-06-22 13:11:56 +03004598 int err;
4599
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004600 BT_DBG("%s", hdev->name);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004601
Johan Hedberg56f87902013-10-02 13:43:13 +03004602 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) ||
4603 hdev->hci_ver < BLUETOOTH_VER_1_2)
Johan Hedberg33c525c2012-10-24 21:11:58 +03004604 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4605 MGMT_STATUS_NOT_SUPPORTED);
4606
Johan Hedberga7e80f22013-01-09 16:05:19 +02004607 if (cp->val != 0x00 && cp->val != 0x01)
4608 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4609 MGMT_STATUS_INVALID_PARAMS);
4610
Johan Hedberg5400c042012-02-21 16:40:33 +02004611 if (!hdev_is_powered(hdev))
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004612 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004613 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5400c042012-02-21 16:40:33 +02004614
4615 if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004616 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004617 MGMT_STATUS_REJECTED);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004618
4619 hci_dev_lock(hdev);
4620
Johan Hedberg05cbf292013-03-15 17:07:07 -05004621 if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) {
4622 err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4623 MGMT_STATUS_BUSY);
4624 goto unlock;
4625 }
4626
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004627 if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) {
4628 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4629 hdev);
4630 goto unlock;
4631 }
4632
Johan Hedberg33e38b32013-03-15 17:07:05 -05004633 cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev,
4634 data, len);
4635 if (!cmd) {
4636 err = -ENOMEM;
4637 goto unlock;
4638 }
4639
4640 hci_req_init(&req, hdev);
4641
Johan Hedberg406d7802013-03-15 17:07:09 -05004642 write_fast_connectable(&req, cp->val);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004643
4644 err = hci_req_run(&req, fast_connectable_complete);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004645 if (err < 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004646 err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004647 MGMT_STATUS_FAILED);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004648 mgmt_pending_remove(cmd);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004649 }
4650
Johan Hedberg33e38b32013-03-15 17:07:05 -05004651unlock:
Antti Julkuf6422ec2011-06-22 13:11:56 +03004652 hci_dev_unlock(hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004653
Antti Julkuf6422ec2011-06-22 13:11:56 +03004654 return err;
4655}
4656
Marcel Holtmann1904a852015-01-11 13:50:44 -08004657static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg0663ca22013-10-02 13:43:14 +03004658{
4659 struct pending_cmd *cmd;
4660
4661 BT_DBG("status 0x%02x", status);
4662
4663 hci_dev_lock(hdev);
4664
4665 cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev);
4666 if (!cmd)
4667 goto unlock;
4668
4669 if (status) {
4670 u8 mgmt_err = mgmt_status(status);
4671
4672 /* We need to restore the flag if related HCI commands
4673 * failed.
4674 */
4675 clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
4676
4677 cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
4678 } else {
4679 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
4680 new_settings(hdev, cmd->sk);
4681 }
4682
4683 mgmt_pending_remove(cmd);
4684
4685unlock:
4686 hci_dev_unlock(hdev);
4687}
4688
4689static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
4690{
4691 struct mgmt_mode *cp = data;
4692 struct pending_cmd *cmd;
4693 struct hci_request req;
4694 int err;
4695
4696 BT_DBG("request for %s", hdev->name);
4697
4698 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
4699 return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4700 MGMT_STATUS_NOT_SUPPORTED);
4701
4702 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
4703 return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4704 MGMT_STATUS_REJECTED);
4705
4706 if (cp->val != 0x00 && cp->val != 0x01)
4707 return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4708 MGMT_STATUS_INVALID_PARAMS);
4709
4710 hci_dev_lock(hdev);
4711
4712 if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
4713 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4714 goto unlock;
4715 }
4716
4717 if (!hdev_is_powered(hdev)) {
4718 if (!cp->val) {
Johan Hedberg0663ca22013-10-02 13:43:14 +03004719 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
4720 clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
4721 clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
4722 clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags);
4723 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
4724 }
4725
4726 change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
4727
4728 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4729 if (err < 0)
4730 goto unlock;
4731
4732 err = new_settings(hdev, sk);
4733 goto unlock;
4734 }
4735
4736 /* Reject disabling when powered on */
4737 if (!cp->val) {
4738 err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4739 MGMT_STATUS_REJECTED);
4740 goto unlock;
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08004741 } else {
4742 /* When configuring a dual-mode controller to operate
4743 * with LE only and using a static address, then switching
4744 * BR/EDR back on is not allowed.
4745 *
4746 * Dual-mode controllers shall operate with the public
4747 * address as its identity address for BR/EDR and LE. So
4748 * reject the attempt to create an invalid configuration.
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08004749 *
4750 * The same restrictions applies when secure connections
4751 * has been enabled. For BR/EDR this is a controller feature
4752 * while for LE it is a host stack feature. This means that
4753 * switching BR/EDR back on when secure connections has been
4754 * enabled is not a supported transaction.
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08004755 */
4756 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) &&
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08004757 (bacmp(&hdev->static_addr, BDADDR_ANY) ||
4758 test_bit(HCI_SC_ENABLED, &hdev->dev_flags))) {
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08004759 err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4760 MGMT_STATUS_REJECTED);
4761 goto unlock;
4762 }
Johan Hedberg0663ca22013-10-02 13:43:14 +03004763 }
4764
4765 if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) {
4766 err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4767 MGMT_STATUS_BUSY);
4768 goto unlock;
4769 }
4770
4771 cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len);
4772 if (!cmd) {
4773 err = -ENOMEM;
4774 goto unlock;
4775 }
4776
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07004777 /* We need to flip the bit already here so that update_adv_data
Johan Hedberg0663ca22013-10-02 13:43:14 +03004778 * generates the correct flags.
4779 */
4780 set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
4781
4782 hci_req_init(&req, hdev);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004783
Johan Hedberg432df052014-08-01 11:13:31 +03004784 write_fast_connectable(&req, false);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02004785 __hci_update_page_scan(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004786
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07004787 /* Since only the advertising data flags will change, there
4788 * is no need to update the scan response data.
4789 */
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07004790 update_adv_data(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004791
Johan Hedberg0663ca22013-10-02 13:43:14 +03004792 err = hci_req_run(&req, set_bredr_complete);
4793 if (err < 0)
4794 mgmt_pending_remove(cmd);
4795
4796unlock:
4797 hci_dev_unlock(hdev);
4798 return err;
4799}
4800
Johan Hedberga1443f52015-01-23 15:42:46 +02004801static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
4802{
4803 struct pending_cmd *cmd;
4804 struct mgmt_mode *cp;
4805
4806 BT_DBG("%s status %u", hdev->name, status);
4807
4808 hci_dev_lock(hdev);
4809
4810 cmd = mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev);
4811 if (!cmd)
4812 goto unlock;
4813
4814 if (status) {
4815 cmd_status(cmd->sk, cmd->index, cmd->opcode,
4816 mgmt_status(status));
4817 goto remove;
4818 }
4819
4820 cp = cmd->param;
4821
4822 switch (cp->val) {
4823 case 0x00:
4824 clear_bit(HCI_SC_ENABLED, &hdev->dev_flags);
4825 clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
4826 break;
4827 case 0x01:
4828 set_bit(HCI_SC_ENABLED, &hdev->dev_flags);
4829 clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
4830 break;
4831 case 0x02:
4832 set_bit(HCI_SC_ENABLED, &hdev->dev_flags);
4833 set_bit(HCI_SC_ONLY, &hdev->dev_flags);
4834 break;
4835 }
4836
4837 send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev);
4838 new_settings(hdev, cmd->sk);
4839
4840remove:
4841 mgmt_pending_remove(cmd);
4842unlock:
4843 hci_dev_unlock(hdev);
4844}
4845
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004846static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
4847 void *data, u16 len)
4848{
4849 struct mgmt_mode *cp = data;
4850 struct pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02004851 struct hci_request req;
Johan Hedberga3209692014-05-26 11:23:35 +03004852 u8 val;
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004853 int err;
4854
4855 BT_DBG("request for %s", hdev->name);
4856
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08004857 if (!lmp_sc_capable(hdev) &&
4858 !test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004859 return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4860 MGMT_STATUS_NOT_SUPPORTED);
4861
Marcel Holtmanned93ec62015-01-22 11:15:22 -08004862 if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) &&
Johan Hedberg59200282015-01-28 19:56:00 +02004863 lmp_sc_capable(hdev) &&
Marcel Holtmanned93ec62015-01-22 11:15:22 -08004864 !test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
4865 return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4866 MGMT_STATUS_REJECTED);
4867
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004868 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004869 return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4870 MGMT_STATUS_INVALID_PARAMS);
4871
4872 hci_dev_lock(hdev);
4873
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08004874 if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) ||
Johan Hedberga3209692014-05-26 11:23:35 +03004875 !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004876 bool changed;
4877
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004878 if (cp->val) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004879 changed = !test_and_set_bit(HCI_SC_ENABLED,
4880 &hdev->dev_flags);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004881 if (cp->val == 0x02)
4882 set_bit(HCI_SC_ONLY, &hdev->dev_flags);
4883 else
4884 clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
4885 } else {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004886 changed = test_and_clear_bit(HCI_SC_ENABLED,
4887 &hdev->dev_flags);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004888 clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
4889 }
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004890
4891 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
4892 if (err < 0)
4893 goto failed;
4894
4895 if (changed)
4896 err = new_settings(hdev, sk);
4897
4898 goto failed;
4899 }
4900
4901 if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) {
4902 err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4903 MGMT_STATUS_BUSY);
4904 goto failed;
4905 }
4906
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004907 val = !!cp->val;
4908
4909 if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) &&
4910 (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004911 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
4912 goto failed;
4913 }
4914
4915 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len);
4916 if (!cmd) {
4917 err = -ENOMEM;
4918 goto failed;
4919 }
4920
Johan Hedberga1443f52015-01-23 15:42:46 +02004921 hci_req_init(&req, hdev);
4922 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val);
4923 err = hci_req_run(&req, sc_enable_complete);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004924 if (err < 0) {
4925 mgmt_pending_remove(cmd);
4926 goto failed;
4927 }
4928
4929failed:
4930 hci_dev_unlock(hdev);
4931 return err;
4932}
4933
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004934static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
4935 void *data, u16 len)
4936{
4937 struct mgmt_mode *cp = data;
Johan Hedbergb97109792014-06-24 14:00:28 +03004938 bool changed, use_changed;
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004939 int err;
4940
4941 BT_DBG("request for %s", hdev->name);
4942
Johan Hedbergb97109792014-06-24 14:00:28 +03004943 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004944 return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
4945 MGMT_STATUS_INVALID_PARAMS);
4946
4947 hci_dev_lock(hdev);
4948
4949 if (cp->val)
Johan Hedberg0663b292014-06-24 13:15:50 +03004950 changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS,
4951 &hdev->dev_flags);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004952 else
Johan Hedberg0663b292014-06-24 13:15:50 +03004953 changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS,
4954 &hdev->dev_flags);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004955
Johan Hedbergb97109792014-06-24 14:00:28 +03004956 if (cp->val == 0x02)
4957 use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS,
4958 &hdev->dev_flags);
4959 else
4960 use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS,
4961 &hdev->dev_flags);
4962
4963 if (hdev_is_powered(hdev) && use_changed &&
4964 test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
4965 u8 mode = (cp->val == 0x02) ? 0x01 : 0x00;
4966 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
4967 sizeof(mode), &mode);
4968 }
4969
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004970 err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev);
4971 if (err < 0)
4972 goto unlock;
4973
4974 if (changed)
4975 err = new_settings(hdev, sk);
4976
4977unlock:
4978 hci_dev_unlock(hdev);
4979 return err;
4980}
4981
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004982static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
4983 u16 len)
4984{
4985 struct mgmt_cp_set_privacy *cp = cp_data;
4986 bool changed;
4987 int err;
4988
4989 BT_DBG("request for %s", hdev->name);
4990
4991 if (!lmp_le_capable(hdev))
4992 return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
4993 MGMT_STATUS_NOT_SUPPORTED);
4994
4995 if (cp->privacy != 0x00 && cp->privacy != 0x01)
4996 return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
4997 MGMT_STATUS_INVALID_PARAMS);
4998
4999 if (hdev_is_powered(hdev))
5000 return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5001 MGMT_STATUS_REJECTED);
5002
5003 hci_dev_lock(hdev);
5004
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02005005 /* If user space supports this command it is also expected to
5006 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
5007 */
5008 set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags);
5009
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005010 if (cp->privacy) {
5011 changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags);
5012 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
5013 set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
5014 } else {
5015 changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags);
5016 memset(hdev->irk, 0, sizeof(hdev->irk));
5017 clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
5018 }
5019
5020 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
5021 if (err < 0)
5022 goto unlock;
5023
5024 if (changed)
5025 err = new_settings(hdev, sk);
5026
5027unlock:
5028 hci_dev_unlock(hdev);
5029 return err;
5030}
5031
Johan Hedberg41edf162014-02-18 10:19:35 +02005032static bool irk_is_valid(struct mgmt_irk_info *irk)
5033{
5034 switch (irk->addr.type) {
5035 case BDADDR_LE_PUBLIC:
5036 return true;
5037
5038 case BDADDR_LE_RANDOM:
5039 /* Two most significant bits shall be set */
5040 if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0)
5041 return false;
5042 return true;
5043 }
5044
5045 return false;
5046}
5047
5048static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5049 u16 len)
5050{
5051 struct mgmt_cp_load_irks *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005052 const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) /
5053 sizeof(struct mgmt_irk_info));
Johan Hedberg41edf162014-02-18 10:19:35 +02005054 u16 irk_count, expected_len;
5055 int i, err;
5056
5057 BT_DBG("request for %s", hdev->name);
5058
5059 if (!lmp_le_capable(hdev))
5060 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5061 MGMT_STATUS_NOT_SUPPORTED);
5062
5063 irk_count = __le16_to_cpu(cp->irk_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005064 if (irk_count > max_irk_count) {
5065 BT_ERR("load_irks: too big irk_count value %u", irk_count);
5066 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5067 MGMT_STATUS_INVALID_PARAMS);
5068 }
Johan Hedberg41edf162014-02-18 10:19:35 +02005069
5070 expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info);
5071 if (expected_len != len) {
5072 BT_ERR("load_irks: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02005073 expected_len, len);
Johan Hedberg41edf162014-02-18 10:19:35 +02005074 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5075 MGMT_STATUS_INVALID_PARAMS);
5076 }
5077
5078 BT_DBG("%s irk_count %u", hdev->name, irk_count);
5079
5080 for (i = 0; i < irk_count; i++) {
5081 struct mgmt_irk_info *key = &cp->irks[i];
5082
5083 if (!irk_is_valid(key))
5084 return cmd_status(sk, hdev->id,
5085 MGMT_OP_LOAD_IRKS,
5086 MGMT_STATUS_INVALID_PARAMS);
5087 }
5088
5089 hci_dev_lock(hdev);
5090
5091 hci_smp_irks_clear(hdev);
5092
5093 for (i = 0; i < irk_count; i++) {
5094 struct mgmt_irk_info *irk = &cp->irks[i];
5095 u8 addr_type;
5096
5097 if (irk->addr.type == BDADDR_LE_PUBLIC)
5098 addr_type = ADDR_LE_DEV_PUBLIC;
5099 else
5100 addr_type = ADDR_LE_DEV_RANDOM;
5101
5102 hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val,
5103 BDADDR_ANY);
5104 }
5105
5106 set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags);
5107
5108 err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
5109
5110 hci_dev_unlock(hdev);
5111
5112 return err;
5113}
5114
Johan Hedberg3f706b72013-01-20 14:27:16 +02005115static bool ltk_is_valid(struct mgmt_ltk_info *key)
5116{
5117 if (key->master != 0x00 && key->master != 0x01)
5118 return false;
Marcel Holtmann490cb0b2014-02-16 12:59:05 -08005119
5120 switch (key->addr.type) {
5121 case BDADDR_LE_PUBLIC:
5122 return true;
5123
5124 case BDADDR_LE_RANDOM:
5125 /* Two most significant bits shall be set */
5126 if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0)
5127 return false;
5128 return true;
5129 }
5130
5131 return false;
Johan Hedberg3f706b72013-01-20 14:27:16 +02005132}
5133
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005134static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005135 void *cp_data, u16 len)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005136{
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005137 struct mgmt_cp_load_long_term_keys *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005138 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
5139 sizeof(struct mgmt_ltk_info));
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005140 u16 key_count, expected_len;
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005141 int i, err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005142
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07005143 BT_DBG("request for %s", hdev->name);
5144
5145 if (!lmp_le_capable(hdev))
5146 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5147 MGMT_STATUS_NOT_SUPPORTED);
5148
Marcel Holtmann1f350c82012-03-12 20:31:08 -07005149 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005150 if (key_count > max_key_count) {
5151 BT_ERR("load_ltks: too big key_count value %u", key_count);
5152 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5153 MGMT_STATUS_INVALID_PARAMS);
5154 }
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005155
5156 expected_len = sizeof(*cp) + key_count *
5157 sizeof(struct mgmt_ltk_info);
5158 if (expected_len != len) {
5159 BT_ERR("load_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02005160 expected_len, len);
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005161 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
Johan Hedberge57e6192013-01-20 14:27:14 +02005162 MGMT_STATUS_INVALID_PARAMS);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005163 }
5164
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005165 BT_DBG("%s key_count %u", hdev->name, key_count);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005166
Johan Hedberg54ad6d82013-01-20 14:27:15 +02005167 for (i = 0; i < key_count; i++) {
5168 struct mgmt_ltk_info *key = &cp->keys[i];
5169
Johan Hedberg3f706b72013-01-20 14:27:16 +02005170 if (!ltk_is_valid(key))
Johan Hedberg54ad6d82013-01-20 14:27:15 +02005171 return cmd_status(sk, hdev->id,
5172 MGMT_OP_LOAD_LONG_TERM_KEYS,
5173 MGMT_STATUS_INVALID_PARAMS);
5174 }
5175
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005176 hci_dev_lock(hdev);
5177
5178 hci_smp_ltks_clear(hdev);
5179
5180 for (i = 0; i < key_count; i++) {
5181 struct mgmt_ltk_info *key = &cp->keys[i];
Johan Hedbergd7b25452014-05-23 13:19:53 +03005182 u8 type, addr_type, authenticated;
Marcel Holtmann79d95a12013-10-13 03:57:38 -07005183
5184 if (key->addr.type == BDADDR_LE_PUBLIC)
5185 addr_type = ADDR_LE_DEV_PUBLIC;
5186 else
5187 addr_type = ADDR_LE_DEV_RANDOM;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005188
Johan Hedberg61b43352014-05-29 19:36:53 +03005189 switch (key->type) {
5190 case MGMT_LTK_UNAUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03005191 authenticated = 0x00;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005192 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03005193 break;
5194 case MGMT_LTK_AUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03005195 authenticated = 0x01;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005196 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03005197 break;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005198 case MGMT_LTK_P256_UNAUTH:
5199 authenticated = 0x00;
5200 type = SMP_LTK_P256;
5201 break;
5202 case MGMT_LTK_P256_AUTH:
5203 authenticated = 0x01;
5204 type = SMP_LTK_P256;
5205 break;
5206 case MGMT_LTK_P256_DEBUG:
5207 authenticated = 0x00;
5208 type = SMP_LTK_P256_DEBUG;
Johan Hedberg61b43352014-05-29 19:36:53 +03005209 default:
5210 continue;
5211 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03005212
Johan Hedberg35d70272014-02-19 14:57:47 +02005213 hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type,
Johan Hedbergd7b25452014-05-23 13:19:53 +03005214 authenticated, key->val, key->enc_size, key->ediv,
Johan Hedberg35d70272014-02-19 14:57:47 +02005215 key->rand);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005216 }
5217
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005218 err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
5219 NULL, 0);
5220
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005221 hci_dev_unlock(hdev);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005222
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005223 return err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005224}
5225
Johan Hedberg9df74652014-12-19 22:26:03 +02005226static int conn_info_cmd_complete(struct pending_cmd *cmd, u8 status)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005227{
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005228 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005229 struct mgmt_rp_get_conn_info rp;
Johan Hedberg9df74652014-12-19 22:26:03 +02005230 int err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005231
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005232 memcpy(&rp.addr, cmd->param, sizeof(rp.addr));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005233
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005234 if (status == MGMT_STATUS_SUCCESS) {
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005235 rp.rssi = conn->rssi;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005236 rp.tx_power = conn->tx_power;
5237 rp.max_tx_power = conn->max_tx_power;
5238 } else {
5239 rp.rssi = HCI_RSSI_INVALID;
5240 rp.tx_power = HCI_TX_POWER_INVALID;
5241 rp.max_tx_power = HCI_TX_POWER_INVALID;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005242 }
5243
Johan Hedberg9df74652014-12-19 22:26:03 +02005244 err = cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, status,
5245 &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005246
5247 hci_conn_drop(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005248 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02005249
5250 return err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005251}
5252
Marcel Holtmann1904a852015-01-11 13:50:44 -08005253static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status,
5254 u16 opcode)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005255{
5256 struct hci_cp_read_rssi *cp;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005257 struct pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005258 struct hci_conn *conn;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005259 u16 handle;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005260 u8 status;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005261
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005262 BT_DBG("status 0x%02x", hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005263
5264 hci_dev_lock(hdev);
5265
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005266 /* Commands sent in request are either Read RSSI or Read Transmit Power
5267 * Level so we check which one was last sent to retrieve connection
5268 * handle. Both commands have handle as first parameter so it's safe to
5269 * cast data on the same command struct.
5270 *
5271 * First command sent is always Read RSSI and we fail only if it fails.
5272 * In other case we simply override error to indicate success as we
5273 * already remembered if TX power value is actually valid.
5274 */
5275 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI);
5276 if (!cp) {
5277 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005278 status = MGMT_STATUS_SUCCESS;
5279 } else {
5280 status = mgmt_status(hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005281 }
5282
5283 if (!cp) {
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005284 BT_ERR("invalid sent_cmd in conn_info response");
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005285 goto unlock;
5286 }
5287
5288 handle = __le16_to_cpu(cp->handle);
5289 conn = hci_conn_hash_lookup_handle(hdev, handle);
5290 if (!conn) {
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005291 BT_ERR("unknown handle (%d) in conn_info response", handle);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005292 goto unlock;
5293 }
5294
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005295 cmd = mgmt_pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn);
5296 if (!cmd)
5297 goto unlock;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005298
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005299 cmd->cmd_complete(cmd, status);
5300 mgmt_pending_remove(cmd);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005301
5302unlock:
5303 hci_dev_unlock(hdev);
5304}
5305
5306static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
5307 u16 len)
5308{
5309 struct mgmt_cp_get_conn_info *cp = data;
5310 struct mgmt_rp_get_conn_info rp;
5311 struct hci_conn *conn;
5312 unsigned long conn_info_age;
5313 int err = 0;
5314
5315 BT_DBG("%s", hdev->name);
5316
5317 memset(&rp, 0, sizeof(rp));
5318 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5319 rp.addr.type = cp->addr.type;
5320
5321 if (!bdaddr_type_is_valid(cp->addr.type))
5322 return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5323 MGMT_STATUS_INVALID_PARAMS,
5324 &rp, sizeof(rp));
5325
5326 hci_dev_lock(hdev);
5327
5328 if (!hdev_is_powered(hdev)) {
5329 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5330 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp));
5331 goto unlock;
5332 }
5333
5334 if (cp->addr.type == BDADDR_BREDR)
5335 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5336 &cp->addr.bdaddr);
5337 else
5338 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
5339
5340 if (!conn || conn->state != BT_CONNECTED) {
5341 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5342 MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp));
5343 goto unlock;
5344 }
5345
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005346 if (mgmt_pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) {
5347 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5348 MGMT_STATUS_BUSY, &rp, sizeof(rp));
5349 goto unlock;
5350 }
5351
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005352 /* To avoid client trying to guess when to poll again for information we
5353 * calculate conn info age as random value between min/max set in hdev.
5354 */
5355 conn_info_age = hdev->conn_info_min_age +
5356 prandom_u32_max(hdev->conn_info_max_age -
5357 hdev->conn_info_min_age);
5358
5359 /* Query controller to refresh cached values if they are too old or were
5360 * never read.
5361 */
Andrzej Kaczmarekf4e2dd52014-05-16 16:48:57 +02005362 if (time_after(jiffies, conn->conn_info_timestamp +
5363 msecs_to_jiffies(conn_info_age)) ||
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005364 !conn->conn_info_timestamp) {
5365 struct hci_request req;
5366 struct hci_cp_read_tx_power req_txp_cp;
5367 struct hci_cp_read_rssi req_rssi_cp;
5368 struct pending_cmd *cmd;
5369
5370 hci_req_init(&req, hdev);
5371 req_rssi_cp.handle = cpu_to_le16(conn->handle);
5372 hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp),
5373 &req_rssi_cp);
5374
Andrzej Kaczmarekf7faab02014-05-14 13:43:04 +02005375 /* For LE links TX power does not change thus we don't need to
5376 * query for it once value is known.
5377 */
5378 if (!bdaddr_type_is_le(cp->addr.type) ||
5379 conn->tx_power == HCI_TX_POWER_INVALID) {
5380 req_txp_cp.handle = cpu_to_le16(conn->handle);
5381 req_txp_cp.type = 0x00;
5382 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5383 sizeof(req_txp_cp), &req_txp_cp);
5384 }
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005385
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005386 /* Max TX power needs to be read only once per connection */
5387 if (conn->max_tx_power == HCI_TX_POWER_INVALID) {
5388 req_txp_cp.handle = cpu_to_le16(conn->handle);
5389 req_txp_cp.type = 0x01;
5390 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5391 sizeof(req_txp_cp), &req_txp_cp);
5392 }
5393
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005394 err = hci_req_run(&req, conn_info_refresh_complete);
5395 if (err < 0)
5396 goto unlock;
5397
5398 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev,
5399 data, len);
5400 if (!cmd) {
5401 err = -ENOMEM;
5402 goto unlock;
5403 }
5404
5405 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005406 cmd->user_data = hci_conn_get(conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005407 cmd->cmd_complete = conn_info_cmd_complete;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005408
5409 conn->conn_info_timestamp = jiffies;
5410 } else {
5411 /* Cache is valid, just reply with values cached in hci_conn */
5412 rp.rssi = conn->rssi;
5413 rp.tx_power = conn->tx_power;
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005414 rp.max_tx_power = conn->max_tx_power;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005415
5416 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5417 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
5418 }
5419
5420unlock:
5421 hci_dev_unlock(hdev);
5422 return err;
5423}
5424
Johan Hedberg9df74652014-12-19 22:26:03 +02005425static int clock_info_cmd_complete(struct pending_cmd *cmd, u8 status)
Johan Hedberg69487372014-12-05 13:36:07 +02005426{
5427 struct hci_conn *conn = cmd->user_data;
5428 struct mgmt_rp_get_clock_info rp;
5429 struct hci_dev *hdev;
Johan Hedberg9df74652014-12-19 22:26:03 +02005430 int err;
Johan Hedberg69487372014-12-05 13:36:07 +02005431
5432 memset(&rp, 0, sizeof(rp));
5433 memcpy(&rp.addr, &cmd->param, sizeof(rp.addr));
5434
5435 if (status)
5436 goto complete;
5437
5438 hdev = hci_dev_get(cmd->index);
5439 if (hdev) {
5440 rp.local_clock = cpu_to_le32(hdev->clock);
5441 hci_dev_put(hdev);
5442 }
5443
5444 if (conn) {
5445 rp.piconet_clock = cpu_to_le32(conn->clock);
5446 rp.accuracy = cpu_to_le16(conn->clock_accuracy);
5447 }
5448
5449complete:
Johan Hedberg9df74652014-12-19 22:26:03 +02005450 err = cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp,
5451 sizeof(rp));
Johan Hedberg69487372014-12-05 13:36:07 +02005452
5453 if (conn) {
5454 hci_conn_drop(conn);
5455 hci_conn_put(conn);
5456 }
Johan Hedberg9df74652014-12-19 22:26:03 +02005457
5458 return err;
Johan Hedberg69487372014-12-05 13:36:07 +02005459}
5460
Marcel Holtmann1904a852015-01-11 13:50:44 -08005461static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg95868422014-06-28 17:54:07 +03005462{
Johan Hedberg95868422014-06-28 17:54:07 +03005463 struct hci_cp_read_clock *hci_cp;
5464 struct pending_cmd *cmd;
5465 struct hci_conn *conn;
5466
5467 BT_DBG("%s status %u", hdev->name, status);
5468
5469 hci_dev_lock(hdev);
5470
5471 hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
5472 if (!hci_cp)
5473 goto unlock;
5474
5475 if (hci_cp->which) {
5476 u16 handle = __le16_to_cpu(hci_cp->handle);
5477 conn = hci_conn_hash_lookup_handle(hdev, handle);
5478 } else {
5479 conn = NULL;
5480 }
5481
5482 cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn);
5483 if (!cmd)
5484 goto unlock;
5485
Johan Hedberg69487372014-12-05 13:36:07 +02005486 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberg95868422014-06-28 17:54:07 +03005487 mgmt_pending_remove(cmd);
Johan Hedberg95868422014-06-28 17:54:07 +03005488
5489unlock:
5490 hci_dev_unlock(hdev);
5491}
5492
5493static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
5494 u16 len)
5495{
5496 struct mgmt_cp_get_clock_info *cp = data;
5497 struct mgmt_rp_get_clock_info rp;
5498 struct hci_cp_read_clock hci_cp;
5499 struct pending_cmd *cmd;
5500 struct hci_request req;
5501 struct hci_conn *conn;
5502 int err;
5503
5504 BT_DBG("%s", hdev->name);
5505
5506 memset(&rp, 0, sizeof(rp));
5507 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5508 rp.addr.type = cp->addr.type;
5509
5510 if (cp->addr.type != BDADDR_BREDR)
5511 return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5512 MGMT_STATUS_INVALID_PARAMS,
5513 &rp, sizeof(rp));
5514
5515 hci_dev_lock(hdev);
5516
5517 if (!hdev_is_powered(hdev)) {
5518 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5519 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp));
5520 goto unlock;
5521 }
5522
5523 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
5524 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5525 &cp->addr.bdaddr);
5526 if (!conn || conn->state != BT_CONNECTED) {
5527 err = cmd_complete(sk, hdev->id,
5528 MGMT_OP_GET_CLOCK_INFO,
5529 MGMT_STATUS_NOT_CONNECTED,
5530 &rp, sizeof(rp));
5531 goto unlock;
5532 }
5533 } else {
5534 conn = NULL;
5535 }
5536
5537 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len);
5538 if (!cmd) {
5539 err = -ENOMEM;
5540 goto unlock;
5541 }
5542
Johan Hedberg69487372014-12-05 13:36:07 +02005543 cmd->cmd_complete = clock_info_cmd_complete;
5544
Johan Hedberg95868422014-06-28 17:54:07 +03005545 hci_req_init(&req, hdev);
5546
5547 memset(&hci_cp, 0, sizeof(hci_cp));
5548 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5549
5550 if (conn) {
5551 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005552 cmd->user_data = hci_conn_get(conn);
Johan Hedberg95868422014-06-28 17:54:07 +03005553
5554 hci_cp.handle = cpu_to_le16(conn->handle);
5555 hci_cp.which = 0x01; /* Piconet clock */
5556 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5557 }
5558
5559 err = hci_req_run(&req, get_clock_info_complete);
5560 if (err < 0)
5561 mgmt_pending_remove(cmd);
5562
5563unlock:
5564 hci_dev_unlock(hdev);
5565 return err;
5566}
5567
Johan Hedberg5a154e62014-12-19 22:26:02 +02005568static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
5569{
5570 struct hci_conn *conn;
5571
5572 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
5573 if (!conn)
5574 return false;
5575
5576 if (conn->dst_type != type)
5577 return false;
5578
5579 if (conn->state != BT_CONNECTED)
5580 return false;
5581
5582 return true;
5583}
5584
5585/* This function requires the caller holds hdev->lock */
5586static int hci_conn_params_set(struct hci_request *req, bdaddr_t *addr,
5587 u8 addr_type, u8 auto_connect)
5588{
5589 struct hci_dev *hdev = req->hdev;
5590 struct hci_conn_params *params;
5591
5592 params = hci_conn_params_add(hdev, addr, addr_type);
5593 if (!params)
5594 return -EIO;
5595
5596 if (params->auto_connect == auto_connect)
5597 return 0;
5598
5599 list_del_init(&params->action);
5600
5601 switch (auto_connect) {
5602 case HCI_AUTO_CONN_DISABLED:
5603 case HCI_AUTO_CONN_LINK_LOSS:
5604 __hci_update_background_scan(req);
5605 break;
5606 case HCI_AUTO_CONN_REPORT:
5607 list_add(&params->action, &hdev->pend_le_reports);
5608 __hci_update_background_scan(req);
5609 break;
5610 case HCI_AUTO_CONN_DIRECT:
5611 case HCI_AUTO_CONN_ALWAYS:
5612 if (!is_connected(hdev, addr, addr_type)) {
5613 list_add(&params->action, &hdev->pend_le_conns);
5614 __hci_update_background_scan(req);
5615 }
5616 break;
5617 }
5618
5619 params->auto_connect = auto_connect;
5620
5621 BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
5622 auto_connect);
5623
5624 return 0;
5625}
5626
Marcel Holtmann8afef092014-06-29 22:28:34 +02005627static void device_added(struct sock *sk, struct hci_dev *hdev,
5628 bdaddr_t *bdaddr, u8 type, u8 action)
5629{
5630 struct mgmt_ev_device_added ev;
5631
5632 bacpy(&ev.addr.bdaddr, bdaddr);
5633 ev.addr.type = type;
5634 ev.action = action;
5635
5636 mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk);
5637}
5638
Marcel Holtmann1904a852015-01-11 13:50:44 -08005639static void add_device_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg5a154e62014-12-19 22:26:02 +02005640{
5641 struct pending_cmd *cmd;
5642
5643 BT_DBG("status 0x%02x", status);
5644
5645 hci_dev_lock(hdev);
5646
5647 cmd = mgmt_pending_find(MGMT_OP_ADD_DEVICE, hdev);
5648 if (!cmd)
5649 goto unlock;
5650
5651 cmd->cmd_complete(cmd, mgmt_status(status));
5652 mgmt_pending_remove(cmd);
5653
5654unlock:
5655 hci_dev_unlock(hdev);
5656}
5657
Marcel Holtmann2faade52014-06-29 19:44:03 +02005658static int add_device(struct sock *sk, struct hci_dev *hdev,
5659 void *data, u16 len)
5660{
5661 struct mgmt_cp_add_device *cp = data;
Johan Hedberg5a154e62014-12-19 22:26:02 +02005662 struct pending_cmd *cmd;
5663 struct hci_request req;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005664 u8 auto_conn, addr_type;
5665 int err;
5666
5667 BT_DBG("%s", hdev->name);
5668
Johan Hedberg66593582014-07-09 12:59:14 +03005669 if (!bdaddr_type_is_valid(cp->addr.type) ||
Marcel Holtmann2faade52014-06-29 19:44:03 +02005670 !bacmp(&cp->addr.bdaddr, BDADDR_ANY))
5671 return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5672 MGMT_STATUS_INVALID_PARAMS,
5673 &cp->addr, sizeof(cp->addr));
5674
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005675 if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02)
Marcel Holtmann2faade52014-06-29 19:44:03 +02005676 return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5677 MGMT_STATUS_INVALID_PARAMS,
5678 &cp->addr, sizeof(cp->addr));
5679
Johan Hedberg5a154e62014-12-19 22:26:02 +02005680 hci_req_init(&req, hdev);
5681
Marcel Holtmann2faade52014-06-29 19:44:03 +02005682 hci_dev_lock(hdev);
5683
Johan Hedberg5a154e62014-12-19 22:26:02 +02005684 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_DEVICE, hdev, data, len);
5685 if (!cmd) {
5686 err = -ENOMEM;
5687 goto unlock;
5688 }
5689
5690 cmd->cmd_complete = addr_cmd_complete;
5691
Johan Hedberg66593582014-07-09 12:59:14 +03005692 if (cp->addr.type == BDADDR_BREDR) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005693 /* Only incoming connections action is supported for now */
Johan Hedberg66593582014-07-09 12:59:14 +03005694 if (cp->action != 0x01) {
Johan Hedberg9df74652014-12-19 22:26:03 +02005695 err = cmd->cmd_complete(cmd,
5696 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005697 mgmt_pending_remove(cmd);
Johan Hedberg66593582014-07-09 12:59:14 +03005698 goto unlock;
5699 }
5700
5701 err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr,
5702 cp->addr.type);
5703 if (err)
5704 goto unlock;
Johan Hedberga3974072014-07-09 12:59:15 +03005705
Johan Hedberg5a154e62014-12-19 22:26:02 +02005706 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03005707
Johan Hedberg66593582014-07-09 12:59:14 +03005708 goto added;
5709 }
5710
Marcel Holtmann2faade52014-06-29 19:44:03 +02005711 if (cp->addr.type == BDADDR_LE_PUBLIC)
5712 addr_type = ADDR_LE_DEV_PUBLIC;
5713 else
5714 addr_type = ADDR_LE_DEV_RANDOM;
5715
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005716 if (cp->action == 0x02)
Marcel Holtmann2faade52014-06-29 19:44:03 +02005717 auto_conn = HCI_AUTO_CONN_ALWAYS;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005718 else if (cp->action == 0x01)
5719 auto_conn = HCI_AUTO_CONN_DIRECT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005720 else
Johan Hedberga3451d22014-07-02 17:37:27 +03005721 auto_conn = HCI_AUTO_CONN_REPORT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005722
Marcel Holtmannbf5b3c82014-06-30 12:34:39 +02005723 /* If the connection parameters don't exist for this device,
5724 * they will be created and configured with defaults.
5725 */
Johan Hedberg5a154e62014-12-19 22:26:02 +02005726 if (hci_conn_params_set(&req, &cp->addr.bdaddr, addr_type,
Marcel Holtmannd06b50c2014-07-01 12:11:06 +02005727 auto_conn) < 0) {
Johan Hedberg9df74652014-12-19 22:26:03 +02005728 err = cmd->cmd_complete(cmd, MGMT_STATUS_FAILED);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005729 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005730 goto unlock;
5731 }
5732
Johan Hedberg66593582014-07-09 12:59:14 +03005733added:
Marcel Holtmann8afef092014-06-29 22:28:34 +02005734 device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
5735
Johan Hedberg5a154e62014-12-19 22:26:02 +02005736 err = hci_req_run(&req, add_device_complete);
5737 if (err < 0) {
5738 /* ENODATA means no HCI commands were needed (e.g. if
5739 * the adapter is powered off).
5740 */
Johan Hedberg9df74652014-12-19 22:26:03 +02005741 if (err == -ENODATA)
5742 err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005743 mgmt_pending_remove(cmd);
5744 }
Marcel Holtmann2faade52014-06-29 19:44:03 +02005745
5746unlock:
5747 hci_dev_unlock(hdev);
5748 return err;
5749}
5750
Marcel Holtmann8afef092014-06-29 22:28:34 +02005751static void device_removed(struct sock *sk, struct hci_dev *hdev,
5752 bdaddr_t *bdaddr, u8 type)
5753{
5754 struct mgmt_ev_device_removed ev;
5755
5756 bacpy(&ev.addr.bdaddr, bdaddr);
5757 ev.addr.type = type;
5758
5759 mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk);
5760}
5761
Marcel Holtmann1904a852015-01-11 13:50:44 -08005762static void remove_device_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005763{
5764 struct pending_cmd *cmd;
5765
5766 BT_DBG("status 0x%02x", status);
5767
5768 hci_dev_lock(hdev);
5769
5770 cmd = mgmt_pending_find(MGMT_OP_REMOVE_DEVICE, hdev);
5771 if (!cmd)
5772 goto unlock;
5773
5774 cmd->cmd_complete(cmd, mgmt_status(status));
5775 mgmt_pending_remove(cmd);
5776
5777unlock:
5778 hci_dev_unlock(hdev);
5779}
5780
Marcel Holtmann2faade52014-06-29 19:44:03 +02005781static int remove_device(struct sock *sk, struct hci_dev *hdev,
5782 void *data, u16 len)
5783{
5784 struct mgmt_cp_remove_device *cp = data;
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005785 struct pending_cmd *cmd;
5786 struct hci_request req;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005787 int err;
5788
5789 BT_DBG("%s", hdev->name);
5790
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005791 hci_req_init(&req, hdev);
5792
Marcel Holtmann2faade52014-06-29 19:44:03 +02005793 hci_dev_lock(hdev);
5794
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005795 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_DEVICE, hdev, data, len);
5796 if (!cmd) {
5797 err = -ENOMEM;
5798 goto unlock;
5799 }
5800
5801 cmd->cmd_complete = addr_cmd_complete;
5802
Marcel Holtmann2faade52014-06-29 19:44:03 +02005803 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
Johan Hedbergc71593d2014-07-02 17:37:28 +03005804 struct hci_conn_params *params;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005805 u8 addr_type;
5806
Johan Hedberg66593582014-07-09 12:59:14 +03005807 if (!bdaddr_type_is_valid(cp->addr.type)) {
Johan Hedberg9df74652014-12-19 22:26:03 +02005808 err = cmd->cmd_complete(cmd,
5809 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005810 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005811 goto unlock;
5812 }
5813
Johan Hedberg66593582014-07-09 12:59:14 +03005814 if (cp->addr.type == BDADDR_BREDR) {
5815 err = hci_bdaddr_list_del(&hdev->whitelist,
5816 &cp->addr.bdaddr,
5817 cp->addr.type);
5818 if (err) {
Johan Hedberg9df74652014-12-19 22:26:03 +02005819 err = cmd->cmd_complete(cmd,
5820 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005821 mgmt_pending_remove(cmd);
Johan Hedberg66593582014-07-09 12:59:14 +03005822 goto unlock;
5823 }
5824
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005825 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03005826
Johan Hedberg66593582014-07-09 12:59:14 +03005827 device_removed(sk, hdev, &cp->addr.bdaddr,
5828 cp->addr.type);
5829 goto complete;
5830 }
5831
Marcel Holtmann2faade52014-06-29 19:44:03 +02005832 if (cp->addr.type == BDADDR_LE_PUBLIC)
5833 addr_type = ADDR_LE_DEV_PUBLIC;
5834 else
5835 addr_type = ADDR_LE_DEV_RANDOM;
5836
Johan Hedbergc71593d2014-07-02 17:37:28 +03005837 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
5838 addr_type);
5839 if (!params) {
Johan Hedberg9df74652014-12-19 22:26:03 +02005840 err = cmd->cmd_complete(cmd,
5841 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005842 mgmt_pending_remove(cmd);
Johan Hedbergc71593d2014-07-02 17:37:28 +03005843 goto unlock;
5844 }
5845
5846 if (params->auto_connect == HCI_AUTO_CONN_DISABLED) {
Johan Hedberg9df74652014-12-19 22:26:03 +02005847 err = cmd->cmd_complete(cmd,
5848 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005849 mgmt_pending_remove(cmd);
Johan Hedbergc71593d2014-07-02 17:37:28 +03005850 goto unlock;
5851 }
5852
Johan Hedbergd1dbf122014-07-04 16:17:23 +03005853 list_del(&params->action);
Johan Hedbergc71593d2014-07-02 17:37:28 +03005854 list_del(&params->list);
5855 kfree(params);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005856 __hci_update_background_scan(&req);
Marcel Holtmann8afef092014-06-29 22:28:34 +02005857
5858 device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005859 } else {
Johan Hedberg19de0822014-07-06 13:06:51 +03005860 struct hci_conn_params *p, *tmp;
Johan Hedberg66593582014-07-09 12:59:14 +03005861 struct bdaddr_list *b, *btmp;
Johan Hedberg19de0822014-07-06 13:06:51 +03005862
Marcel Holtmann2faade52014-06-29 19:44:03 +02005863 if (cp->addr.type) {
Johan Hedberg9df74652014-12-19 22:26:03 +02005864 err = cmd->cmd_complete(cmd,
5865 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005866 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005867 goto unlock;
5868 }
5869
Johan Hedberg66593582014-07-09 12:59:14 +03005870 list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) {
5871 device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type);
5872 list_del(&b->list);
5873 kfree(b);
5874 }
5875
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005876 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03005877
Johan Hedberg19de0822014-07-06 13:06:51 +03005878 list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) {
5879 if (p->auto_connect == HCI_AUTO_CONN_DISABLED)
5880 continue;
5881 device_removed(sk, hdev, &p->addr, p->addr_type);
5882 list_del(&p->action);
5883 list_del(&p->list);
5884 kfree(p);
5885 }
5886
5887 BT_DBG("All LE connection parameters were removed");
5888
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005889 __hci_update_background_scan(&req);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005890 }
5891
Johan Hedberg66593582014-07-09 12:59:14 +03005892complete:
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005893 err = hci_req_run(&req, remove_device_complete);
5894 if (err < 0) {
5895 /* ENODATA means no HCI commands were needed (e.g. if
5896 * the adapter is powered off).
5897 */
Johan Hedberg9df74652014-12-19 22:26:03 +02005898 if (err == -ENODATA)
5899 err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005900 mgmt_pending_remove(cmd);
5901 }
Marcel Holtmann2faade52014-06-29 19:44:03 +02005902
5903unlock:
5904 hci_dev_unlock(hdev);
5905 return err;
5906}
5907
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005908static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
5909 u16 len)
5910{
5911 struct mgmt_cp_load_conn_param *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005912 const u16 max_param_count = ((U16_MAX - sizeof(*cp)) /
5913 sizeof(struct mgmt_conn_param));
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005914 u16 param_count, expected_len;
5915 int i;
5916
5917 if (!lmp_le_capable(hdev))
5918 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
5919 MGMT_STATUS_NOT_SUPPORTED);
5920
5921 param_count = __le16_to_cpu(cp->param_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005922 if (param_count > max_param_count) {
5923 BT_ERR("load_conn_param: too big param_count value %u",
5924 param_count);
5925 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
5926 MGMT_STATUS_INVALID_PARAMS);
5927 }
Johan Hedberga26f3dc2014-07-02 17:37:29 +03005928
5929 expected_len = sizeof(*cp) + param_count *
5930 sizeof(struct mgmt_conn_param);
5931 if (expected_len != len) {
5932 BT_ERR("load_conn_param: expected %u bytes, got %u bytes",
5933 expected_len, len);
5934 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
5935 MGMT_STATUS_INVALID_PARAMS);
5936 }
5937
5938 BT_DBG("%s param_count %u", hdev->name, param_count);
5939
5940 hci_dev_lock(hdev);
5941
5942 hci_conn_params_clear_disabled(hdev);
5943
5944 for (i = 0; i < param_count; i++) {
5945 struct mgmt_conn_param *param = &cp->params[i];
5946 struct hci_conn_params *hci_param;
5947 u16 min, max, latency, timeout;
5948 u8 addr_type;
5949
5950 BT_DBG("Adding %pMR (type %u)", &param->addr.bdaddr,
5951 param->addr.type);
5952
5953 if (param->addr.type == BDADDR_LE_PUBLIC) {
5954 addr_type = ADDR_LE_DEV_PUBLIC;
5955 } else if (param->addr.type == BDADDR_LE_RANDOM) {
5956 addr_type = ADDR_LE_DEV_RANDOM;
5957 } else {
5958 BT_ERR("Ignoring invalid connection parameters");
5959 continue;
5960 }
5961
5962 min = le16_to_cpu(param->min_interval);
5963 max = le16_to_cpu(param->max_interval);
5964 latency = le16_to_cpu(param->latency);
5965 timeout = le16_to_cpu(param->timeout);
5966
5967 BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x",
5968 min, max, latency, timeout);
5969
5970 if (hci_check_conn_params(min, max, latency, timeout) < 0) {
5971 BT_ERR("Ignoring invalid connection parameters");
5972 continue;
5973 }
5974
5975 hci_param = hci_conn_params_add(hdev, &param->addr.bdaddr,
5976 addr_type);
5977 if (!hci_param) {
5978 BT_ERR("Failed to add connection parameters");
5979 continue;
5980 }
5981
5982 hci_param->conn_min_interval = min;
5983 hci_param->conn_max_interval = max;
5984 hci_param->conn_latency = latency;
5985 hci_param->supervision_timeout = timeout;
5986 }
5987
5988 hci_dev_unlock(hdev);
5989
5990 return cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, NULL, 0);
5991}
5992
Marcel Holtmanndbece372014-07-04 18:11:55 +02005993static int set_external_config(struct sock *sk, struct hci_dev *hdev,
5994 void *data, u16 len)
5995{
5996 struct mgmt_cp_set_external_config *cp = data;
5997 bool changed;
5998 int err;
5999
6000 BT_DBG("%s", hdev->name);
6001
6002 if (hdev_is_powered(hdev))
6003 return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6004 MGMT_STATUS_REJECTED);
6005
6006 if (cp->config != 0x00 && cp->config != 0x01)
6007 return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6008 MGMT_STATUS_INVALID_PARAMS);
6009
6010 if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
6011 return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6012 MGMT_STATUS_NOT_SUPPORTED);
6013
6014 hci_dev_lock(hdev);
6015
6016 if (cp->config)
6017 changed = !test_and_set_bit(HCI_EXT_CONFIGURED,
6018 &hdev->dev_flags);
6019 else
6020 changed = test_and_clear_bit(HCI_EXT_CONFIGURED,
6021 &hdev->dev_flags);
6022
6023 err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
6024 if (err < 0)
6025 goto unlock;
6026
6027 if (!changed)
6028 goto unlock;
6029
Marcel Holtmannf4537c02014-07-04 19:06:23 +02006030 err = new_options(hdev, sk);
6031
Marcel Holtmanndbece372014-07-04 18:11:55 +02006032 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) == is_configured(hdev)) {
6033 mgmt_index_removed(hdev);
Marcel Holtmannd603b762014-07-06 12:11:14 +02006034
6035 if (test_and_change_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) {
6036 set_bit(HCI_CONFIG, &hdev->dev_flags);
6037 set_bit(HCI_AUTO_OFF, &hdev->dev_flags);
6038
6039 queue_work(hdev->req_workqueue, &hdev->power_on);
6040 } else {
Marcel Holtmann5ea234d2014-07-06 12:11:16 +02006041 set_bit(HCI_RAW, &hdev->flags);
Marcel Holtmannd603b762014-07-06 12:11:14 +02006042 mgmt_index_added(hdev);
6043 }
Marcel Holtmanndbece372014-07-04 18:11:55 +02006044 }
6045
6046unlock:
6047 hci_dev_unlock(hdev);
6048 return err;
6049}
6050
Marcel Holtmann9713c172014-07-06 12:11:15 +02006051static int set_public_address(struct sock *sk, struct hci_dev *hdev,
6052 void *data, u16 len)
6053{
6054 struct mgmt_cp_set_public_address *cp = data;
6055 bool changed;
6056 int err;
6057
6058 BT_DBG("%s", hdev->name);
6059
6060 if (hdev_is_powered(hdev))
6061 return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6062 MGMT_STATUS_REJECTED);
6063
6064 if (!bacmp(&cp->bdaddr, BDADDR_ANY))
6065 return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6066 MGMT_STATUS_INVALID_PARAMS);
6067
6068 if (!hdev->set_bdaddr)
6069 return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6070 MGMT_STATUS_NOT_SUPPORTED);
6071
6072 hci_dev_lock(hdev);
6073
6074 changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
6075 bacpy(&hdev->public_addr, &cp->bdaddr);
6076
6077 err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev);
6078 if (err < 0)
6079 goto unlock;
6080
6081 if (!changed)
6082 goto unlock;
6083
6084 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags))
6085 err = new_options(hdev, sk);
6086
6087 if (is_configured(hdev)) {
6088 mgmt_index_removed(hdev);
6089
6090 clear_bit(HCI_UNCONFIGURED, &hdev->dev_flags);
6091
6092 set_bit(HCI_CONFIG, &hdev->dev_flags);
6093 set_bit(HCI_AUTO_OFF, &hdev->dev_flags);
6094
6095 queue_work(hdev->req_workqueue, &hdev->power_on);
6096 }
6097
6098unlock:
6099 hci_dev_unlock(hdev);
6100 return err;
6101}
6102
Andrei Emeltchenko2e3c35e2012-03-14 18:54:15 +02006103static const struct mgmt_handler {
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006104 int (*func) (struct sock *sk, struct hci_dev *hdev, void *data,
6105 u16 data_len);
Johan Hedbergbe22b542012-03-01 22:24:41 +02006106 bool var_len;
6107 size_t data_len;
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02006108} mgmt_handlers[] = {
6109 { NULL }, /* 0x0000 (no command) */
Johan Hedbergbe22b542012-03-01 22:24:41 +02006110 { read_version, false, MGMT_READ_VERSION_SIZE },
6111 { read_commands, false, MGMT_READ_COMMANDS_SIZE },
6112 { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE },
6113 { read_controller_info, false, MGMT_READ_INFO_SIZE },
6114 { set_powered, false, MGMT_SETTING_SIZE },
6115 { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE },
6116 { set_connectable, false, MGMT_SETTING_SIZE },
6117 { set_fast_connectable, false, MGMT_SETTING_SIZE },
Johan Hedbergb2939472014-07-30 09:22:23 +03006118 { set_bondable, false, MGMT_SETTING_SIZE },
Johan Hedbergbe22b542012-03-01 22:24:41 +02006119 { set_link_security, false, MGMT_SETTING_SIZE },
6120 { set_ssp, false, MGMT_SETTING_SIZE },
6121 { set_hs, false, MGMT_SETTING_SIZE },
6122 { set_le, false, MGMT_SETTING_SIZE },
6123 { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE },
6124 { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE },
6125 { add_uuid, false, MGMT_ADD_UUID_SIZE },
6126 { remove_uuid, false, MGMT_REMOVE_UUID_SIZE },
6127 { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE },
6128 { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE },
6129 { disconnect, false, MGMT_DISCONNECT_SIZE },
6130 { get_connections, false, MGMT_GET_CONNECTIONS_SIZE },
6131 { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE },
6132 { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE },
6133 { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE },
6134 { pair_device, false, MGMT_PAIR_DEVICE_SIZE },
6135 { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE },
6136 { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE },
6137 { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE },
6138 { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
6139 { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE },
6140 { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
6141 { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE },
Marcel Holtmannec109112014-01-10 02:07:30 -08006142 { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE },
Johan Hedbergbe22b542012-03-01 22:24:41 +02006143 { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
6144 { start_discovery, false, MGMT_START_DISCOVERY_SIZE },
6145 { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE },
6146 { confirm_name, false, MGMT_CONFIRM_NAME_SIZE },
6147 { block_device, false, MGMT_BLOCK_DEVICE_SIZE },
6148 { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE },
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07006149 { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE },
Johan Hedberg4375f102013-09-25 13:26:10 +03006150 { set_advertising, false, MGMT_SETTING_SIZE },
Johan Hedberg0663ca22013-10-02 13:43:14 +03006151 { set_bredr, false, MGMT_SETTING_SIZE },
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07006152 { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE },
Marcel Holtmann14b49b92013-10-11 08:23:20 -07006153 { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE },
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08006154 { set_secure_conn, false, MGMT_SETTING_SIZE },
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08006155 { set_debug_keys, false, MGMT_SETTING_SIZE },
Johan Hedberg62b04cd2014-02-23 19:42:27 +02006156 { set_privacy, false, MGMT_SET_PRIVACY_SIZE },
Johan Hedberg41edf162014-02-18 10:19:35 +02006157 { load_irks, true, MGMT_LOAD_IRKS_SIZE },
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006158 { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE },
Johan Hedberg95868422014-06-28 17:54:07 +03006159 { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE },
Marcel Holtmann2faade52014-06-29 19:44:03 +02006160 { add_device, false, MGMT_ADD_DEVICE_SIZE },
6161 { remove_device, false, MGMT_REMOVE_DEVICE_SIZE },
Marcel Holtmann73d1df22014-07-02 22:10:52 +02006162 { load_conn_param, true, MGMT_LOAD_CONN_PARAM_SIZE },
6163 { read_unconf_index_list, false, MGMT_READ_UNCONF_INDEX_LIST_SIZE },
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +02006164 { read_config_info, false, MGMT_READ_CONFIG_INFO_SIZE },
Marcel Holtmanndbece372014-07-04 18:11:55 +02006165 { set_external_config, false, MGMT_SET_EXTERNAL_CONFIG_SIZE },
Marcel Holtmann9713c172014-07-06 12:11:15 +02006166 { set_public_address, false, MGMT_SET_PUBLIC_ADDRESS_SIZE },
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01006167 { start_service_discovery,true, MGMT_START_SERVICE_DISCOVERY_SIZE },
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02006168};
6169
Johan Hedberg03811012010-12-08 00:21:06 +02006170int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
6171{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03006172 void *buf;
6173 u8 *cp;
Johan Hedberg03811012010-12-08 00:21:06 +02006174 struct mgmt_hdr *hdr;
Szymon Janc4e51eae2011-02-25 19:05:48 +01006175 u16 opcode, index, len;
Johan Hedbergbdb6d972012-02-28 06:13:32 +02006176 struct hci_dev *hdev = NULL;
Andrei Emeltchenko2e3c35e2012-03-14 18:54:15 +02006177 const struct mgmt_handler *handler;
Johan Hedberg03811012010-12-08 00:21:06 +02006178 int err;
6179
6180 BT_DBG("got %zu bytes", msglen);
6181
6182 if (msglen < sizeof(*hdr))
6183 return -EINVAL;
6184
Gustavo F. Padovane63a15e2011-04-04 18:56:53 -03006185 buf = kmalloc(msglen, GFP_KERNEL);
Johan Hedberg03811012010-12-08 00:21:06 +02006186 if (!buf)
6187 return -ENOMEM;
6188
Al Viro6ce8e9c2014-04-06 21:25:44 -04006189 if (memcpy_from_msg(buf, msg, msglen)) {
Johan Hedberg03811012010-12-08 00:21:06 +02006190 err = -EFAULT;
6191 goto done;
6192 }
6193
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03006194 hdr = buf;
Marcel Holtmann1f350c82012-03-12 20:31:08 -07006195 opcode = __le16_to_cpu(hdr->opcode);
6196 index = __le16_to_cpu(hdr->index);
6197 len = __le16_to_cpu(hdr->len);
Johan Hedberg03811012010-12-08 00:21:06 +02006198
6199 if (len != msglen - sizeof(*hdr)) {
6200 err = -EINVAL;
6201 goto done;
6202 }
6203
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02006204 if (index != MGMT_INDEX_NONE) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02006205 hdev = hci_dev_get(index);
6206 if (!hdev) {
6207 err = cmd_status(sk, index, opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006208 MGMT_STATUS_INVALID_INDEX);
Johan Hedbergbdb6d972012-02-28 06:13:32 +02006209 goto done;
6210 }
Marcel Holtmann0736cfa2013-08-26 21:40:51 -07006211
Johan Hedbergcebf4cf2013-10-10 18:06:04 +02006212 if (test_bit(HCI_SETUP, &hdev->dev_flags) ||
Marcel Holtmannd603b762014-07-06 12:11:14 +02006213 test_bit(HCI_CONFIG, &hdev->dev_flags) ||
Marcel Holtmann4a964402014-07-02 19:10:33 +02006214 test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
Marcel Holtmann0736cfa2013-08-26 21:40:51 -07006215 err = cmd_status(sk, index, opcode,
6216 MGMT_STATUS_INVALID_INDEX);
6217 goto done;
6218 }
Marcel Holtmann42a9bc142014-07-04 16:54:40 +02006219
6220 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) &&
Marcel Holtmanndbece372014-07-04 18:11:55 +02006221 opcode != MGMT_OP_READ_CONFIG_INFO &&
Marcel Holtmann9713c172014-07-06 12:11:15 +02006222 opcode != MGMT_OP_SET_EXTERNAL_CONFIG &&
6223 opcode != MGMT_OP_SET_PUBLIC_ADDRESS) {
Marcel Holtmann42a9bc142014-07-04 16:54:40 +02006224 err = cmd_status(sk, index, opcode,
6225 MGMT_STATUS_INVALID_INDEX);
6226 goto done;
6227 }
Johan Hedbergbdb6d972012-02-28 06:13:32 +02006228 }
6229
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02006230 if (opcode >= ARRAY_SIZE(mgmt_handlers) ||
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03006231 mgmt_handlers[opcode].func == NULL) {
Johan Hedberg03811012010-12-08 00:21:06 +02006232 BT_DBG("Unknown op %u", opcode);
Johan Hedbergca69b792011-11-11 18:10:00 +02006233 err = cmd_status(sk, index, opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006234 MGMT_STATUS_UNKNOWN_COMMAND);
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02006235 goto done;
Johan Hedberg03811012010-12-08 00:21:06 +02006236 }
6237
Marcel Holtmann73d1df22014-07-02 22:10:52 +02006238 if (hdev && (opcode <= MGMT_OP_READ_INDEX_LIST ||
6239 opcode == MGMT_OP_READ_UNCONF_INDEX_LIST)) {
6240 err = cmd_status(sk, index, opcode,
6241 MGMT_STATUS_INVALID_INDEX);
6242 goto done;
6243 }
6244
6245 if (!hdev && (opcode > MGMT_OP_READ_INDEX_LIST &&
6246 opcode != MGMT_OP_READ_UNCONF_INDEX_LIST)) {
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02006247 err = cmd_status(sk, index, opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006248 MGMT_STATUS_INVALID_INDEX);
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02006249 goto done;
6250 }
6251
Johan Hedbergbe22b542012-03-01 22:24:41 +02006252 handler = &mgmt_handlers[opcode];
6253
6254 if ((handler->var_len && len < handler->data_len) ||
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03006255 (!handler->var_len && len != handler->data_len)) {
Johan Hedbergbe22b542012-03-01 22:24:41 +02006256 err = cmd_status(sk, index, opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006257 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergbe22b542012-03-01 22:24:41 +02006258 goto done;
6259 }
6260
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02006261 if (hdev)
6262 mgmt_init_hdev(sk, hdev);
6263
6264 cp = buf + sizeof(*hdr);
6265
Johan Hedbergbe22b542012-03-01 22:24:41 +02006266 err = handler->func(sk, hdev, cp, len);
Johan Hedberge41d8b42010-12-13 21:07:03 +02006267 if (err < 0)
6268 goto done;
6269
Johan Hedberg03811012010-12-08 00:21:06 +02006270 err = msglen;
6271
6272done:
Johan Hedbergbdb6d972012-02-28 06:13:32 +02006273 if (hdev)
6274 hci_dev_put(hdev);
6275
Johan Hedberg03811012010-12-08 00:21:06 +02006276 kfree(buf);
6277 return err;
6278}
Johan Hedbergc71e97b2010-12-13 21:07:07 +02006279
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07006280void mgmt_index_added(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02006281{
Marcel Holtmann1514b892013-10-06 08:25:01 -07006282 if (hdev->dev_type != HCI_BREDR)
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07006283 return;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +03006284
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02006285 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
6286 return;
6287
6288 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags))
6289 mgmt_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, NULL, 0, NULL);
6290 else
6291 mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL);
Johan Hedbergc71e97b2010-12-13 21:07:07 +02006292}
6293
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07006294void mgmt_index_removed(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02006295{
Johan Hedberg5f159032012-03-02 03:13:19 +02006296 u8 status = MGMT_STATUS_INVALID_INDEX;
Johan Hedbergb24752f2011-11-03 14:40:33 +02006297
Marcel Holtmann1514b892013-10-06 08:25:01 -07006298 if (hdev->dev_type != HCI_BREDR)
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07006299 return;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +03006300
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02006301 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
6302 return;
6303
Johan Hedberg1b9b5ee2014-12-05 13:36:00 +02006304 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedbergb24752f2011-11-03 14:40:33 +02006305
Marcel Holtmannedd38962014-07-02 21:30:55 +02006306 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags))
6307 mgmt_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, NULL, 0, NULL);
6308 else
6309 mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02006310}
6311
Andre Guedes6046dc32014-02-26 20:21:51 -03006312/* This function requires the caller holds hdev->lock */
Johan Hedberg2cf22212014-12-19 22:26:00 +02006313static void restart_le_actions(struct hci_request *req)
Andre Guedes6046dc32014-02-26 20:21:51 -03006314{
Johan Hedberg2cf22212014-12-19 22:26:00 +02006315 struct hci_dev *hdev = req->hdev;
Andre Guedes6046dc32014-02-26 20:21:51 -03006316 struct hci_conn_params *p;
6317
6318 list_for_each_entry(p, &hdev->le_conn_params, list) {
Johan Hedbergd7347f32014-07-04 12:37:23 +03006319 /* Needed for AUTO_OFF case where might not "really"
6320 * have been powered off.
6321 */
6322 list_del_init(&p->action);
6323
6324 switch (p->auto_connect) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006325 case HCI_AUTO_CONN_DIRECT:
Johan Hedbergd7347f32014-07-04 12:37:23 +03006326 case HCI_AUTO_CONN_ALWAYS:
6327 list_add(&p->action, &hdev->pend_le_conns);
6328 break;
6329 case HCI_AUTO_CONN_REPORT:
6330 list_add(&p->action, &hdev->pend_le_reports);
6331 break;
6332 default:
6333 break;
Marcel Holtmannc83ed192014-07-01 19:28:24 +02006334 }
Andre Guedes6046dc32014-02-26 20:21:51 -03006335 }
Marcel Holtmannc83ed192014-07-01 19:28:24 +02006336
Johan Hedberg2cf22212014-12-19 22:26:00 +02006337 __hci_update_background_scan(req);
Andre Guedes6046dc32014-02-26 20:21:51 -03006338}
6339
Marcel Holtmann1904a852015-01-11 13:50:44 -08006340static void powered_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg229ab392013-03-15 17:06:53 -05006341{
6342 struct cmd_lookup match = { NULL, hdev };
6343
6344 BT_DBG("status 0x%02x", status);
6345
Marcel Holtmann162a3ba2015-01-14 15:43:11 -08006346 if (!status) {
6347 /* Register the available SMP channels (BR/EDR and LE) only
6348 * when successfully powering on the controller. This late
6349 * registration is required so that LE SMP can clearly
6350 * decide if the public address or static address is used.
6351 */
6352 smp_register(hdev);
6353 }
6354
Johan Hedberg229ab392013-03-15 17:06:53 -05006355 hci_dev_lock(hdev);
6356
6357 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
6358
6359 new_settings(hdev, match.sk);
6360
6361 hci_dev_unlock(hdev);
6362
6363 if (match.sk)
6364 sock_put(match.sk);
6365}
6366
Johan Hedberg70da6242013-03-15 17:06:51 -05006367static int powered_update_hci(struct hci_dev *hdev)
6368{
Johan Hedberg890ea892013-03-15 17:06:52 -05006369 struct hci_request req;
Johan Hedberg70da6242013-03-15 17:06:51 -05006370 u8 link_sec;
6371
Johan Hedberg890ea892013-03-15 17:06:52 -05006372 hci_req_init(&req, hdev);
6373
Johan Hedberg70da6242013-03-15 17:06:51 -05006374 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) &&
6375 !lmp_host_ssp_capable(hdev)) {
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08006376 u8 mode = 0x01;
Johan Hedberg70da6242013-03-15 17:06:51 -05006377
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08006378 hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode);
Johan Hedberg70da6242013-03-15 17:06:51 -05006379
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08006380 if (bredr_sc_enabled(hdev) && !lmp_host_sc_capable(hdev)) {
6381 u8 support = 0x01;
6382
6383 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT,
6384 sizeof(support), &support);
6385 }
Johan Hedbergec6f99b2014-12-12 13:30:11 +02006386 }
6387
Johan Hedbergc73eee92013-04-19 18:35:21 +03006388 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) &&
6389 lmp_bredr_capable(hdev)) {
Johan Hedberg70da6242013-03-15 17:06:51 -05006390 struct hci_cp_write_le_host_supported cp;
6391
Marcel Holtmann32226e42014-07-24 20:04:16 +02006392 cp.le = 0x01;
6393 cp.simul = 0x00;
Johan Hedberg70da6242013-03-15 17:06:51 -05006394
6395 /* Check first if we already have the right
6396 * host state (host features set)
6397 */
6398 if (cp.le != lmp_host_le_capable(hdev) ||
6399 cp.simul != lmp_host_le_br_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05006400 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED,
6401 sizeof(cp), &cp);
Johan Hedberg70da6242013-03-15 17:06:51 -05006402 }
6403
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07006404 if (lmp_le_capable(hdev)) {
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07006405 /* Make sure the controller has a good default for
6406 * advertising data. This also applies to the case
6407 * where BR/EDR was toggled during the AUTO_OFF phase.
6408 */
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07006409 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07006410 update_adv_data(&req);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07006411 update_scan_rsp_data(&req);
6412 }
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07006413
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07006414 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
6415 enable_advertising(&req);
Johan Hedberg2cf22212014-12-19 22:26:00 +02006416
6417 restart_le_actions(&req);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03006418 }
6419
Johan Hedberg70da6242013-03-15 17:06:51 -05006420 link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
6421 if (link_sec != test_bit(HCI_AUTH, &hdev->flags))
Johan Hedberg890ea892013-03-15 17:06:52 -05006422 hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE,
6423 sizeof(link_sec), &link_sec);
Johan Hedberg70da6242013-03-15 17:06:51 -05006424
6425 if (lmp_bredr_capable(hdev)) {
Johan Hedberg432df052014-08-01 11:13:31 +03006426 write_fast_connectable(&req, false);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02006427 __hci_update_page_scan(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05006428 update_class(&req);
Johan Hedberg13928972013-03-15 17:07:00 -05006429 update_name(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05006430 update_eir(&req);
Johan Hedberg70da6242013-03-15 17:06:51 -05006431 }
6432
Johan Hedberg229ab392013-03-15 17:06:53 -05006433 return hci_req_run(&req, powered_complete);
Johan Hedberg70da6242013-03-15 17:06:51 -05006434}
6435
Johan Hedberg744cf192011-11-08 20:40:14 +02006436int mgmt_powered(struct hci_dev *hdev, u8 powered)
Johan Hedberg5add6af2010-12-16 10:00:37 +02006437{
Johan Hedberg76a7f3a2012-02-17 00:34:40 +02006438 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg98459042014-12-12 11:15:21 +02006439 u8 status, zero_cod[] = { 0, 0, 0 };
Johan Hedberg7bb895d2012-02-17 01:20:00 +02006440 int err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02006441
Johan Hedberg5e5282b2012-02-21 16:01:30 +02006442 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
6443 return 0;
6444
Johan Hedberg5e5282b2012-02-21 16:01:30 +02006445 if (powered) {
Johan Hedberg229ab392013-03-15 17:06:53 -05006446 if (powered_update_hci(hdev) == 0)
6447 return 0;
Johan Hedbergfe038882013-01-16 16:15:34 +02006448
Johan Hedberg229ab392013-03-15 17:06:53 -05006449 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp,
6450 &match);
6451 goto new_settings;
Johan Hedbergb24752f2011-11-03 14:40:33 +02006452 }
6453
Johan Hedberg229ab392013-03-15 17:06:53 -05006454 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
Johan Hedberg98459042014-12-12 11:15:21 +02006455
6456 /* If the power off is because of hdev unregistration let
6457 * use the appropriate INVALID_INDEX status. Otherwise use
6458 * NOT_POWERED. We cover both scenarios here since later in
6459 * mgmt_index_removed() any hci_conn callbacks will have already
6460 * been triggered, potentially causing misleading DISCONNECTED
6461 * status responses.
6462 */
6463 if (test_bit(HCI_UNREGISTER, &hdev->dev_flags))
6464 status = MGMT_STATUS_INVALID_INDEX;
6465 else
6466 status = MGMT_STATUS_NOT_POWERED;
6467
6468 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedberg229ab392013-03-15 17:06:53 -05006469
6470 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0)
6471 mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
6472 zero_cod, sizeof(zero_cod), NULL);
6473
6474new_settings:
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02006475 err = new_settings(hdev, match.sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02006476
6477 if (match.sk)
6478 sock_put(match.sk);
6479
Johan Hedberg7bb895d2012-02-17 01:20:00 +02006480 return err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02006481}
Johan Hedberg73f22f62010-12-29 16:00:25 +02006482
Marcel Holtmann3eec7052013-10-06 23:55:46 -07006483void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
Johan Hedberg96570ff2013-05-29 09:51:29 +03006484{
6485 struct pending_cmd *cmd;
6486 u8 status;
6487
6488 cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev);
6489 if (!cmd)
Marcel Holtmann3eec7052013-10-06 23:55:46 -07006490 return;
Johan Hedberg96570ff2013-05-29 09:51:29 +03006491
6492 if (err == -ERFKILL)
6493 status = MGMT_STATUS_RFKILLED;
6494 else
6495 status = MGMT_STATUS_FAILED;
6496
Marcel Holtmann3eec7052013-10-06 23:55:46 -07006497 cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
Johan Hedberg96570ff2013-05-29 09:51:29 +03006498
6499 mgmt_pending_remove(cmd);
Johan Hedberg96570ff2013-05-29 09:51:29 +03006500}
6501
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07006502void mgmt_discoverable_timeout(struct hci_dev *hdev)
6503{
6504 struct hci_request req;
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07006505
6506 hci_dev_lock(hdev);
6507
6508 /* When discoverable timeout triggers, then just make sure
6509 * the limited discoverable flag is cleared. Even in the case
6510 * of a timeout triggered from general discoverable, it is
6511 * safe to unconditionally clear the flag.
6512 */
6513 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
Johan Hedberg9a43e252013-10-20 19:00:07 +03006514 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07006515
6516 hci_req_init(&req, hdev);
Johan Hedberg4b580612013-10-19 23:38:21 +03006517 if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
6518 u8 scan = SCAN_PAGE;
6519 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE,
6520 sizeof(scan), &scan);
6521 }
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07006522 update_class(&req);
Johan Hedberg9a43e252013-10-20 19:00:07 +03006523 update_adv_data(&req);
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07006524 hci_req_run(&req, NULL);
6525
6526 hdev->discov_timeout = 0;
6527
Johan Hedberg9a43e252013-10-20 19:00:07 +03006528 new_settings(hdev, NULL);
6529
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07006530 hci_dev_unlock(hdev);
6531}
6532
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07006533void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
6534 bool persistent)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006535{
Johan Hedberg86742e12011-11-07 23:13:38 +02006536 struct mgmt_ev_new_link_key ev;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006537
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006538 memset(&ev, 0, sizeof(ev));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006539
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006540 ev.store_hint = persistent;
Johan Hedbergd753fdc2012-02-17 14:06:34 +02006541 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03006542 ev.key.addr.type = BDADDR_BREDR;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006543 ev.key.type = key->type;
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03006544 memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006545 ev.key.pin_len = key->pin_len;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006546
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07006547 mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006548}
Johan Hedbergf7520542011-01-20 12:34:39 +02006549
Johan Hedbergd7b25452014-05-23 13:19:53 +03006550static u8 mgmt_ltk_type(struct smp_ltk *ltk)
6551{
Johan Hedberg23fb8de2014-05-23 13:15:37 +03006552 switch (ltk->type) {
6553 case SMP_LTK:
6554 case SMP_LTK_SLAVE:
6555 if (ltk->authenticated)
6556 return MGMT_LTK_AUTHENTICATED;
6557 return MGMT_LTK_UNAUTHENTICATED;
6558 case SMP_LTK_P256:
6559 if (ltk->authenticated)
6560 return MGMT_LTK_P256_AUTH;
6561 return MGMT_LTK_P256_UNAUTH;
6562 case SMP_LTK_P256_DEBUG:
6563 return MGMT_LTK_P256_DEBUG;
6564 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03006565
6566 return MGMT_LTK_UNAUTHENTICATED;
6567}
6568
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07006569void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006570{
6571 struct mgmt_ev_new_long_term_key ev;
6572
6573 memset(&ev, 0, sizeof(ev));
6574
Marcel Holtmann5192d302014-02-19 17:11:58 -08006575 /* Devices using resolvable or non-resolvable random addresses
6576 * without providing an indentity resolving key don't require
6577 * to store long term keys. Their addresses will change the
6578 * next time around.
6579 *
6580 * Only when a remote device provides an identity address
6581 * make sure the long term key is stored. If the remote
6582 * identity is known, the long term keys are internally
6583 * mapped to the identity address. So allow static random
6584 * and public addresses here.
6585 */
Johan Hedbergba74b662014-02-19 14:57:45 +02006586 if (key->bdaddr_type == ADDR_LE_DEV_RANDOM &&
6587 (key->bdaddr.b[5] & 0xc0) != 0xc0)
6588 ev.store_hint = 0x00;
6589 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07006590 ev.store_hint = persistent;
Johan Hedbergba74b662014-02-19 14:57:45 +02006591
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006592 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03006593 ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
Johan Hedbergd7b25452014-05-23 13:19:53 +03006594 ev.key.type = mgmt_ltk_type(key);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006595 ev.key.enc_size = key->enc_size;
6596 ev.key.ediv = key->ediv;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08006597 ev.key.rand = key->rand;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006598
Johan Hedberg2ceba532014-06-16 19:25:16 +03006599 if (key->type == SMP_LTK)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006600 ev.key.master = 1;
6601
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006602 memcpy(ev.key.val, key->val, sizeof(key->val));
6603
Marcel Holtmann083368f2013-10-15 14:26:29 -07006604 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006605}
6606
Johan Hedberg95fbac82014-02-19 15:18:31 +02006607void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk)
6608{
6609 struct mgmt_ev_new_irk ev;
6610
6611 memset(&ev, 0, sizeof(ev));
6612
Marcel Holtmannbab6d1e2014-02-19 11:51:54 -08006613 /* For identity resolving keys from devices that are already
6614 * using a public address or static random address, do not
6615 * ask for storing this key. The identity resolving key really
6616 * is only mandatory for devices using resovlable random
6617 * addresses.
6618 *
6619 * Storing all identity resolving keys has the downside that
6620 * they will be also loaded on next boot of they system. More
6621 * identity resolving keys, means more time during scanning is
6622 * needed to actually resolve these addresses.
6623 */
6624 if (bacmp(&irk->rpa, BDADDR_ANY))
6625 ev.store_hint = 0x01;
6626 else
6627 ev.store_hint = 0x00;
6628
Johan Hedberg95fbac82014-02-19 15:18:31 +02006629 bacpy(&ev.rpa, &irk->rpa);
6630 bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr);
6631 ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type);
6632 memcpy(ev.irk.val, irk->val, sizeof(irk->val));
6633
6634 mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
6635}
6636
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07006637void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
6638 bool persistent)
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07006639{
6640 struct mgmt_ev_new_csrk ev;
6641
6642 memset(&ev, 0, sizeof(ev));
6643
6644 /* Devices using resolvable or non-resolvable random addresses
6645 * without providing an indentity resolving key don't require
6646 * to store signature resolving keys. Their addresses will change
6647 * the next time around.
6648 *
6649 * Only when a remote device provides an identity address
6650 * make sure the signature resolving key is stored. So allow
6651 * static random and public addresses here.
6652 */
6653 if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM &&
6654 (csrk->bdaddr.b[5] & 0xc0) != 0xc0)
6655 ev.store_hint = 0x00;
6656 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07006657 ev.store_hint = persistent;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07006658
6659 bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
6660 ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
6661 ev.key.master = csrk->master;
6662 memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
6663
6664 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
6665}
6666
Andre Guedesffb5a8272014-07-01 18:10:11 -03006667void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedbergf4869e22014-07-02 17:37:32 +03006668 u8 bdaddr_type, u8 store_hint, u16 min_interval,
6669 u16 max_interval, u16 latency, u16 timeout)
Andre Guedesffb5a8272014-07-01 18:10:11 -03006670{
6671 struct mgmt_ev_new_conn_param ev;
6672
Johan Hedbergc103aea2014-07-02 17:37:34 +03006673 if (!hci_is_identity_address(bdaddr, bdaddr_type))
6674 return;
6675
Andre Guedesffb5a8272014-07-01 18:10:11 -03006676 memset(&ev, 0, sizeof(ev));
6677 bacpy(&ev.addr.bdaddr, bdaddr);
6678 ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type);
Johan Hedbergf4869e22014-07-02 17:37:32 +03006679 ev.store_hint = store_hint;
Andre Guedesffb5a8272014-07-01 18:10:11 -03006680 ev.min_interval = cpu_to_le16(min_interval);
6681 ev.max_interval = cpu_to_le16(max_interval);
6682 ev.latency = cpu_to_le16(latency);
6683 ev.timeout = cpu_to_le16(timeout);
6684
6685 mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL);
6686}
6687
Marcel Holtmann94933992013-10-15 10:26:39 -07006688static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
6689 u8 data_len)
6690{
6691 eir[eir_len++] = sizeof(type) + data_len;
6692 eir[eir_len++] = type;
6693 memcpy(&eir[eir_len], data, data_len);
6694 eir_len += data_len;
6695
6696 return eir_len;
6697}
6698
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00006699void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
6700 u32 flags, u8 *name, u8 name_len)
Johan Hedbergf7520542011-01-20 12:34:39 +02006701{
Johan Hedbergb644ba32012-01-17 21:48:47 +02006702 char buf[512];
6703 struct mgmt_ev_device_connected *ev = (void *) buf;
6704 u16 eir_len = 0;
Johan Hedbergf7520542011-01-20 12:34:39 +02006705
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00006706 bacpy(&ev->addr.bdaddr, &conn->dst);
6707 ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedbergf7520542011-01-20 12:34:39 +02006708
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02006709 ev->flags = __cpu_to_le32(flags);
Johan Hedberg08c79b62012-02-23 22:31:51 +02006710
Alfonso Acostafd45ada2014-10-07 08:44:11 +00006711 /* We must ensure that the EIR Data fields are ordered and
6712 * unique. Keep it simple for now and avoid the problem by not
6713 * adding any BR/EDR data to the LE adv.
6714 */
6715 if (conn->le_adv_data_len > 0) {
6716 memcpy(&ev->eir[eir_len],
6717 conn->le_adv_data, conn->le_adv_data_len);
6718 eir_len = conn->le_adv_data_len;
6719 } else {
6720 if (name_len > 0)
6721 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE,
6722 name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02006723
Alfonso Acostaddbea5c2014-10-07 08:44:12 +00006724 if (memcmp(conn->dev_class, "\0\0\0", 3) != 0)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00006725 eir_len = eir_append_data(ev->eir, eir_len,
6726 EIR_CLASS_OF_DEV,
6727 conn->dev_class, 3);
6728 }
Johan Hedbergb644ba32012-01-17 21:48:47 +02006729
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02006730 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02006731
Marcel Holtmannecd90ae2013-10-06 23:55:49 -07006732 mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf,
6733 sizeof(*ev) + eir_len, NULL);
Johan Hedbergf7520542011-01-20 12:34:39 +02006734}
6735
Johan Hedberg8962ee72011-01-20 12:40:27 +02006736static void disconnect_rsp(struct pending_cmd *cmd, void *data)
6737{
Johan Hedberg8962ee72011-01-20 12:40:27 +02006738 struct sock **sk = data;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006739
Johan Hedbergf5818c22014-12-05 13:36:02 +02006740 cmd->cmd_complete(cmd, 0);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006741
6742 *sk = cmd->sk;
6743 sock_hold(*sk);
6744
Johan Hedberga664b5b2011-02-19 12:06:02 -03006745 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006746}
6747
Johan Hedberg124f6e32012-02-09 13:50:12 +02006748static void unpair_device_rsp(struct pending_cmd *cmd, void *data)
Johan Hedberga8a1d192011-11-10 15:54:38 +02006749{
Johan Hedbergb1078ad2012-02-09 17:21:16 +02006750 struct hci_dev *hdev = data;
Johan Hedberg124f6e32012-02-09 13:50:12 +02006751 struct mgmt_cp_unpair_device *cp = cmd->param;
Johan Hedberga8a1d192011-11-10 15:54:38 +02006752
Johan Hedbergb1078ad2012-02-09 17:21:16 +02006753 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
6754
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02006755 cmd->cmd_complete(cmd, 0);
Johan Hedberga8a1d192011-11-10 15:54:38 +02006756 mgmt_pending_remove(cmd);
6757}
6758
Johan Hedberg84c61d92014-08-01 11:13:30 +03006759bool mgmt_powering_down(struct hci_dev *hdev)
6760{
6761 struct pending_cmd *cmd;
6762 struct mgmt_mode *cp;
6763
6764 cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev);
6765 if (!cmd)
6766 return false;
6767
6768 cp = cmd->param;
6769 if (!cp->val)
6770 return true;
6771
6772 return false;
6773}
6774
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07006775void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02006776 u8 link_type, u8 addr_type, u8 reason,
6777 bool mgmt_connected)
Johan Hedbergf7520542011-01-20 12:34:39 +02006778{
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02006779 struct mgmt_ev_device_disconnected ev;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006780 struct sock *sk = NULL;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006781
Johan Hedberg84c61d92014-08-01 11:13:30 +03006782 /* The connection is still in hci_conn_hash so test for 1
6783 * instead of 0 to know if this is the last one.
6784 */
6785 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
6786 cancel_delayed_work(&hdev->power_off);
6787 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberg8b064a32014-02-24 14:52:22 +02006788 }
6789
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02006790 if (!mgmt_connected)
6791 return;
6792
Andre Guedes57eb7762013-10-30 19:01:41 -03006793 if (link_type != ACL_LINK && link_type != LE_LINK)
6794 return;
6795
Johan Hedberg744cf192011-11-08 20:40:14 +02006796 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
Johan Hedbergf7520542011-01-20 12:34:39 +02006797
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02006798 bacpy(&ev.addr.bdaddr, bdaddr);
6799 ev.addr.type = link_to_bdaddr(link_type, addr_type);
6800 ev.reason = reason;
Johan Hedbergf7520542011-01-20 12:34:39 +02006801
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07006802 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006803
6804 if (sk)
Szymon Jancd97dcb62012-03-16 16:02:56 +01006805 sock_put(sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006806
Johan Hedberg124f6e32012-02-09 13:50:12 +02006807 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006808 hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006809}
6810
Marcel Holtmann78929242013-10-06 23:55:47 -07006811void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
6812 u8 link_type, u8 addr_type, u8 status)
Johan Hedberg8962ee72011-01-20 12:40:27 +02006813{
Andre Guedes3655bba2013-10-30 19:01:40 -03006814 u8 bdaddr_type = link_to_bdaddr(link_type, addr_type);
6815 struct mgmt_cp_disconnect *cp;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006816 struct pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006817
Jefferson Delfes36a75f12012-09-18 13:36:54 -04006818 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
6819 hdev);
6820
Johan Hedberg2e58ef32011-11-08 20:40:15 +02006821 cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02006822 if (!cmd)
Marcel Holtmann78929242013-10-06 23:55:47 -07006823 return;
Johan Hedberg8962ee72011-01-20 12:40:27 +02006824
Andre Guedes3655bba2013-10-30 19:01:40 -03006825 cp = cmd->param;
6826
6827 if (bacmp(bdaddr, &cp->addr.bdaddr))
6828 return;
6829
6830 if (cp->addr.type != bdaddr_type)
6831 return;
6832
Johan Hedbergf5818c22014-12-05 13:36:02 +02006833 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03006834 mgmt_pending_remove(cmd);
Johan Hedbergf7520542011-01-20 12:34:39 +02006835}
Johan Hedberg17d5c042011-01-22 06:09:08 +02006836
Marcel Holtmann445608d2013-10-06 23:55:48 -07006837void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
6838 u8 addr_type, u8 status)
Johan Hedberg17d5c042011-01-22 06:09:08 +02006839{
6840 struct mgmt_ev_connect_failed ev;
Johan Hedbergc9910d02014-02-27 14:35:12 +02006841
Johan Hedberg84c61d92014-08-01 11:13:30 +03006842 /* The connection is still in hci_conn_hash so test for 1
6843 * instead of 0 to know if this is the last one.
6844 */
6845 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
6846 cancel_delayed_work(&hdev->power_off);
6847 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedbergc9910d02014-02-27 14:35:12 +02006848 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02006849
Johan Hedberg4c659c32011-11-07 23:13:39 +02006850 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03006851 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergca69b792011-11-11 18:10:00 +02006852 ev.status = mgmt_status(status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02006853
Marcel Holtmann445608d2013-10-06 23:55:48 -07006854 mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg17d5c042011-01-22 06:09:08 +02006855}
Johan Hedberg980e1a52011-01-22 06:10:07 +02006856
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07006857void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
Johan Hedberg980e1a52011-01-22 06:10:07 +02006858{
6859 struct mgmt_ev_pin_code_request ev;
6860
Johan Hedbergd8457692012-02-17 14:24:57 +02006861 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03006862 ev.addr.type = BDADDR_BREDR;
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02006863 ev.secure = secure;
Johan Hedberg980e1a52011-01-22 06:10:07 +02006864
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07006865 mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg980e1a52011-01-22 06:10:07 +02006866}
6867
Marcel Holtmanne669cf82013-10-15 14:26:21 -07006868void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
6869 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02006870{
6871 struct pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02006872
Johan Hedberg2e58ef32011-11-08 20:40:15 +02006873 cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02006874 if (!cmd)
Marcel Holtmanne669cf82013-10-15 14:26:21 -07006875 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02006876
Johan Hedberg7776d1d2014-12-05 13:36:03 +02006877 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03006878 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02006879}
6880
Marcel Holtmann3eb38522013-10-15 14:26:22 -07006881void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
6882 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02006883{
6884 struct pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02006885
Johan Hedberg2e58ef32011-11-08 20:40:15 +02006886 cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02006887 if (!cmd)
Marcel Holtmann3eb38522013-10-15 14:26:22 -07006888 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02006889
Johan Hedberg7776d1d2014-12-05 13:36:03 +02006890 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03006891 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02006892}
Johan Hedberga5c29682011-02-19 12:05:57 -03006893
Johan Hedberg744cf192011-11-08 20:40:14 +02006894int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg39adbff2014-03-20 08:18:14 +02006895 u8 link_type, u8 addr_type, u32 value,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006896 u8 confirm_hint)
Johan Hedberga5c29682011-02-19 12:05:57 -03006897{
6898 struct mgmt_ev_user_confirm_request ev;
6899
Johan Hedberg744cf192011-11-08 20:40:14 +02006900 BT_DBG("%s", hdev->name);
Johan Hedberga5c29682011-02-19 12:05:57 -03006901
Johan Hedberg272d90d2012-02-09 15:26:12 +02006902 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03006903 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberg55bc1a32011-04-28 11:28:56 -07006904 ev.confirm_hint = confirm_hint;
Johan Hedberg39adbff2014-03-20 08:18:14 +02006905 ev.value = cpu_to_le32(value);
Johan Hedberga5c29682011-02-19 12:05:57 -03006906
Johan Hedberg744cf192011-11-08 20:40:14 +02006907 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006908 NULL);
Johan Hedberga5c29682011-02-19 12:05:57 -03006909}
6910
Johan Hedberg272d90d2012-02-09 15:26:12 +02006911int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03006912 u8 link_type, u8 addr_type)
Brian Gix604086b2011-11-23 08:28:33 -08006913{
6914 struct mgmt_ev_user_passkey_request ev;
6915
6916 BT_DBG("%s", hdev->name);
6917
Johan Hedberg272d90d2012-02-09 15:26:12 +02006918 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03006919 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Brian Gix604086b2011-11-23 08:28:33 -08006920
6921 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006922 NULL);
Brian Gix604086b2011-11-23 08:28:33 -08006923}
6924
Brian Gix0df4c182011-11-16 13:53:13 -08006925static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03006926 u8 link_type, u8 addr_type, u8 status,
6927 u8 opcode)
Johan Hedberga5c29682011-02-19 12:05:57 -03006928{
6929 struct pending_cmd *cmd;
Johan Hedberga5c29682011-02-19 12:05:57 -03006930
Johan Hedberg2e58ef32011-11-08 20:40:15 +02006931 cmd = mgmt_pending_find(opcode, hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03006932 if (!cmd)
6933 return -ENOENT;
6934
Johan Hedberg7776d1d2014-12-05 13:36:03 +02006935 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03006936 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03006937
Johan Hedberg7776d1d2014-12-05 13:36:03 +02006938 return 0;
Johan Hedberga5c29682011-02-19 12:05:57 -03006939}
6940
Johan Hedberg744cf192011-11-08 20:40:14 +02006941int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006942 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03006943{
Johan Hedberg272d90d2012-02-09 15:26:12 +02006944 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006945 status, MGMT_OP_USER_CONFIRM_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03006946}
6947
Johan Hedberg272d90d2012-02-09 15:26:12 +02006948int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006949 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03006950{
Johan Hedberg272d90d2012-02-09 15:26:12 +02006951 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03006952 status,
6953 MGMT_OP_USER_CONFIRM_NEG_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03006954}
Johan Hedberg2a611692011-02-19 12:06:00 -03006955
Brian Gix604086b2011-11-23 08:28:33 -08006956int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006957 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08006958{
Johan Hedberg272d90d2012-02-09 15:26:12 +02006959 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006960 status, MGMT_OP_USER_PASSKEY_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08006961}
6962
Johan Hedberg272d90d2012-02-09 15:26:12 +02006963int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006964 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08006965{
Johan Hedberg272d90d2012-02-09 15:26:12 +02006966 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03006967 status,
6968 MGMT_OP_USER_PASSKEY_NEG_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08006969}
6970
Johan Hedberg92a25252012-09-06 18:39:26 +03006971int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
6972 u8 link_type, u8 addr_type, u32 passkey,
6973 u8 entered)
6974{
6975 struct mgmt_ev_passkey_notify ev;
6976
6977 BT_DBG("%s", hdev->name);
6978
6979 bacpy(&ev.addr.bdaddr, bdaddr);
6980 ev.addr.type = link_to_bdaddr(link_type, addr_type);
6981 ev.passkey = __cpu_to_le32(passkey);
6982 ev.entered = entered;
6983
6984 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
6985}
6986
Johan Hedberge1e930f2014-09-08 17:09:49 -07006987void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status)
Johan Hedberg2a611692011-02-19 12:06:00 -03006988{
6989 struct mgmt_ev_auth_failed ev;
Johan Hedberge1e930f2014-09-08 17:09:49 -07006990 struct pending_cmd *cmd;
6991 u8 status = mgmt_status(hci_status);
Johan Hedberg2a611692011-02-19 12:06:00 -03006992
Johan Hedberge1e930f2014-09-08 17:09:49 -07006993 bacpy(&ev.addr.bdaddr, &conn->dst);
6994 ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
6995 ev.status = status;
Johan Hedberg2a611692011-02-19 12:06:00 -03006996
Johan Hedberge1e930f2014-09-08 17:09:49 -07006997 cmd = find_pairing(conn);
6998
6999 mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev),
7000 cmd ? cmd->sk : NULL);
7001
Johan Hedberga511b352014-12-11 21:45:45 +02007002 if (cmd) {
7003 cmd->cmd_complete(cmd, status);
7004 mgmt_pending_remove(cmd);
7005 }
Johan Hedberg2a611692011-02-19 12:06:00 -03007006}
Johan Hedbergb312b1612011-03-16 14:29:37 +02007007
Marcel Holtmann464996a2013-10-15 14:26:24 -07007008void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007009{
7010 struct cmd_lookup match = { NULL, hdev };
Marcel Holtmann464996a2013-10-15 14:26:24 -07007011 bool changed;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007012
7013 if (status) {
7014 u8 mgmt_err = mgmt_status(status);
7015 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007016 cmd_status_rsp, &mgmt_err);
Marcel Holtmann464996a2013-10-15 14:26:24 -07007017 return;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007018 }
7019
Marcel Holtmann464996a2013-10-15 14:26:24 -07007020 if (test_bit(HCI_AUTH, &hdev->flags))
7021 changed = !test_and_set_bit(HCI_LINK_SECURITY,
7022 &hdev->dev_flags);
7023 else
7024 changed = test_and_clear_bit(HCI_LINK_SECURITY,
7025 &hdev->dev_flags);
Johan Hedberg47990ea2012-02-22 11:58:37 +02007026
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007027 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007028 &match);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007029
Johan Hedberg47990ea2012-02-22 11:58:37 +02007030 if (changed)
Marcel Holtmann464996a2013-10-15 14:26:24 -07007031 new_settings(hdev, match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007032
7033 if (match.sk)
7034 sock_put(match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007035}
7036
Johan Hedberg890ea892013-03-15 17:06:52 -05007037static void clear_eir(struct hci_request *req)
Johan Hedbergcacaf522012-02-21 00:52:42 +02007038{
Johan Hedberg890ea892013-03-15 17:06:52 -05007039 struct hci_dev *hdev = req->hdev;
Johan Hedbergcacaf522012-02-21 00:52:42 +02007040 struct hci_cp_write_eir cp;
7041
Johan Hedberg976eb202012-10-24 21:12:01 +03007042 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05007043 return;
Johan Hedbergcacaf522012-02-21 00:52:42 +02007044
Johan Hedbergc80da272012-02-22 15:38:48 +02007045 memset(hdev->eir, 0, sizeof(hdev->eir));
7046
Johan Hedbergcacaf522012-02-21 00:52:42 +02007047 memset(&cp, 0, sizeof(cp));
7048
Johan Hedberg890ea892013-03-15 17:06:52 -05007049 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedbergcacaf522012-02-21 00:52:42 +02007050}
7051
Marcel Holtmann3e248562013-10-15 14:26:25 -07007052void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007053{
7054 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg890ea892013-03-15 17:06:52 -05007055 struct hci_request req;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007056 bool changed = false;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007057
7058 if (status) {
7059 u8 mgmt_err = mgmt_status(status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007060
7061 if (enable && test_and_clear_bit(HCI_SSP_ENABLED,
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007062 &hdev->dev_flags)) {
7063 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
Marcel Holtmann3e248562013-10-15 14:26:25 -07007064 new_settings(hdev, NULL);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007065 }
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007066
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007067 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
7068 &mgmt_err);
Marcel Holtmann3e248562013-10-15 14:26:25 -07007069 return;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007070 }
7071
7072 if (enable) {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007073 changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007074 } else {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007075 changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
7076 if (!changed)
7077 changed = test_and_clear_bit(HCI_HS_ENABLED,
7078 &hdev->dev_flags);
7079 else
7080 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007081 }
7082
7083 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
7084
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007085 if (changed)
Marcel Holtmann3e248562013-10-15 14:26:25 -07007086 new_settings(hdev, match.sk);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007087
Johan Hedberg5fc6ebb2012-02-22 15:10:59 +02007088 if (match.sk)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007089 sock_put(match.sk);
7090
Johan Hedberg890ea892013-03-15 17:06:52 -05007091 hci_req_init(&req, hdev);
7092
Johan Hedberg37699722014-06-24 14:00:27 +03007093 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
7094 if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags))
7095 hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE,
7096 sizeof(enable), &enable);
Johan Hedberg890ea892013-03-15 17:06:52 -05007097 update_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03007098 } else {
Johan Hedberg890ea892013-03-15 17:06:52 -05007099 clear_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03007100 }
Johan Hedberg890ea892013-03-15 17:06:52 -05007101
7102 hci_req_run(&req, NULL);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007103}
7104
Johan Hedberg92da6092013-03-15 17:06:55 -05007105static void sk_lookup(struct pending_cmd *cmd, void *data)
Johan Hedberg90e70452012-02-23 23:09:40 +02007106{
7107 struct cmd_lookup *match = data;
7108
Johan Hedberg90e70452012-02-23 23:09:40 +02007109 if (match->sk == NULL) {
7110 match->sk = cmd->sk;
7111 sock_hold(match->sk);
7112 }
Johan Hedberg90e70452012-02-23 23:09:40 +02007113}
7114
Marcel Holtmann4e1b0242013-10-15 14:26:26 -07007115void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
7116 u8 status)
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007117{
Johan Hedberg90e70452012-02-23 23:09:40 +02007118 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007119
Johan Hedberg92da6092013-03-15 17:06:55 -05007120 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
7121 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
7122 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
Johan Hedberg90e70452012-02-23 23:09:40 +02007123
7124 if (!status)
Marcel Holtmann4e1b0242013-10-15 14:26:26 -07007125 mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3,
7126 NULL);
Johan Hedberg90e70452012-02-23 23:09:40 +02007127
7128 if (match.sk)
7129 sock_put(match.sk);
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007130}
7131
Marcel Holtmann7667da32013-10-15 14:26:27 -07007132void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
Johan Hedbergb312b1612011-03-16 14:29:37 +02007133{
Johan Hedbergb312b1612011-03-16 14:29:37 +02007134 struct mgmt_cp_set_local_name ev;
Johan Hedberg13928972013-03-15 17:07:00 -05007135 struct pending_cmd *cmd;
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02007136
Johan Hedberg13928972013-03-15 17:07:00 -05007137 if (status)
Marcel Holtmann7667da32013-10-15 14:26:27 -07007138 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02007139
7140 memset(&ev, 0, sizeof(ev));
7141 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02007142 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
Johan Hedbergb312b1612011-03-16 14:29:37 +02007143
Johan Hedberg2e58ef32011-11-08 20:40:15 +02007144 cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05007145 if (!cmd) {
7146 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
Johan Hedbergb312b1612011-03-16 14:29:37 +02007147
Johan Hedberg13928972013-03-15 17:07:00 -05007148 /* If this is a HCI command related to powering on the
7149 * HCI dev don't send any mgmt signals.
7150 */
7151 if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev))
Marcel Holtmann7667da32013-10-15 14:26:27 -07007152 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02007153 }
7154
Marcel Holtmann7667da32013-10-15 14:26:27 -07007155 mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
7156 cmd ? cmd->sk : NULL);
Johan Hedbergb312b1612011-03-16 14:29:37 +02007157}
Szymon Jancc35938b2011-03-22 13:12:21 +01007158
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08007159void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192,
Johan Hedberg38da1702014-11-17 20:52:20 +02007160 u8 *rand192, u8 *hash256, u8 *rand256,
7161 u8 status)
Szymon Jancc35938b2011-03-22 13:12:21 +01007162{
7163 struct pending_cmd *cmd;
Szymon Jancc35938b2011-03-22 13:12:21 +01007164
Johan Hedberg744cf192011-11-08 20:40:14 +02007165 BT_DBG("%s status %u", hdev->name, status);
Szymon Jancc35938b2011-03-22 13:12:21 +01007166
Johan Hedberg2e58ef32011-11-08 20:40:15 +02007167 cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01007168 if (!cmd)
Marcel Holtmann3edaf092013-10-15 14:26:28 -07007169 return;
Szymon Jancc35938b2011-03-22 13:12:21 +01007170
7171 if (status) {
Marcel Holtmann3edaf092013-10-15 14:26:28 -07007172 cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
7173 mgmt_status(status));
Szymon Jancc35938b2011-03-22 13:12:21 +01007174 } else {
Johan Hedberg66f096f2015-02-02 13:23:42 +02007175 struct mgmt_rp_read_local_oob_data rp;
7176 size_t rp_size = sizeof(rp);
7177
7178 memcpy(rp.hash192, hash192, sizeof(rp.hash192));
7179 memcpy(rp.rand192, rand192, sizeof(rp.rand192));
7180
Johan Hedberg710f11c2014-05-26 11:21:22 +03007181 if (bredr_sc_enabled(hdev) && hash256 && rand256) {
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08007182 memcpy(rp.hash256, hash256, sizeof(rp.hash256));
Johan Hedberg38da1702014-11-17 20:52:20 +02007183 memcpy(rp.rand256, rand256, sizeof(rp.rand256));
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08007184 } else {
Johan Hedberg66f096f2015-02-02 13:23:42 +02007185 rp_size -= sizeof(rp.hash256) + sizeof(rp.rand256);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08007186 }
Johan Hedberg66f096f2015-02-02 13:23:42 +02007187
7188 cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, 0,
7189 &rp, rp_size);
Szymon Jancc35938b2011-03-22 13:12:21 +01007190 }
7191
7192 mgmt_pending_remove(cmd);
Szymon Jancc35938b2011-03-22 13:12:21 +01007193}
Johan Hedberge17acd42011-03-30 23:57:16 +03007194
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007195static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16])
7196{
7197 int i;
7198
7199 for (i = 0; i < uuid_count; i++) {
7200 if (!memcmp(uuid, uuids[i], 16))
7201 return true;
7202 }
7203
7204 return false;
7205}
7206
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007207static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16])
7208{
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007209 u16 parsed = 0;
7210
7211 while (parsed < eir_len) {
7212 u8 field_len = eir[0];
7213 u8 uuid[16];
7214 int i;
7215
7216 if (field_len == 0)
7217 break;
7218
7219 if (eir_len - parsed < field_len + 1)
7220 break;
7221
7222 switch (eir[1]) {
7223 case EIR_UUID16_ALL:
7224 case EIR_UUID16_SOME:
7225 for (i = 0; i + 3 <= field_len; i += 2) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02007226 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007227 uuid[13] = eir[i + 3];
7228 uuid[12] = eir[i + 2];
7229 if (has_uuid(uuid, uuid_count, uuids))
7230 return true;
7231 }
7232 break;
7233 case EIR_UUID32_ALL:
7234 case EIR_UUID32_SOME:
7235 for (i = 0; i + 5 <= field_len; i += 4) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02007236 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007237 uuid[15] = eir[i + 5];
7238 uuid[14] = eir[i + 4];
7239 uuid[13] = eir[i + 3];
7240 uuid[12] = eir[i + 2];
7241 if (has_uuid(uuid, uuid_count, uuids))
7242 return true;
7243 }
7244 break;
7245 case EIR_UUID128_ALL:
7246 case EIR_UUID128_SOME:
7247 for (i = 0; i + 17 <= field_len; i += 16) {
7248 memcpy(uuid, eir + i + 2, 16);
7249 if (has_uuid(uuid, uuid_count, uuids))
7250 return true;
7251 }
7252 break;
7253 }
7254
7255 parsed += field_len + 1;
7256 eir += field_len + 1;
7257 }
7258
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007259 return false;
7260}
7261
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007262static void restart_le_scan(struct hci_dev *hdev)
7263{
7264 /* If controller is not scanning we are done. */
7265 if (!test_bit(HCI_LE_SCAN, &hdev->dev_flags))
7266 return;
7267
7268 if (time_after(jiffies + DISCOV_LE_RESTART_DELAY,
7269 hdev->discovery.scan_start +
7270 hdev->discovery.scan_duration))
7271 return;
7272
7273 queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart,
7274 DISCOV_LE_RESTART_DELAY);
7275}
7276
Marcel Holtmann901801b2013-10-06 23:55:51 -07007277void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
Marcel Holtmannaf589252014-07-01 14:11:20 +02007278 u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
7279 u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
Johan Hedberge17acd42011-03-30 23:57:16 +03007280{
Johan Hedberge319d2e2012-01-15 19:51:59 +02007281 char buf[512];
7282 struct mgmt_ev_device_found *ev = (void *) buf;
Johan Hedberg1dc06092012-01-15 21:01:23 +02007283 size_t ev_size;
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007284 bool match;
Johan Hedberge17acd42011-03-30 23:57:16 +03007285
Johan Hedberg75ce2082014-07-02 22:42:01 +03007286 /* Don't send events for a non-kernel initiated discovery. With
7287 * LE one exception is if we have pend_le_reports > 0 in which
7288 * case we're doing passive scanning and want these events.
7289 */
7290 if (!hci_discovery_active(hdev)) {
7291 if (link_type == ACL_LINK)
7292 return;
Johan Hedberg66f84552014-07-04 12:37:18 +03007293 if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports))
Johan Hedberg75ce2082014-07-02 22:42:01 +03007294 return;
7295 }
Andre Guedes12602d02013-04-30 15:29:40 -03007296
Marcel Holtmannbda157a2014-12-05 10:55:56 +01007297 /* When using service discovery with a RSSI threshold, then check
7298 * if such a RSSI threshold is specified. If a RSSI threshold has
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007299 * been specified, and HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set,
7300 * then all results with a RSSI smaller than the RSSI threshold will be
7301 * dropped. If the quirk is set, let it through for further processing,
7302 * as we might need to restart the scan.
Marcel Holtmannefb25132014-12-05 13:03:34 +01007303 *
7304 * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry,
7305 * the results are also dropped.
Marcel Holtmannbda157a2014-12-05 10:55:56 +01007306 */
7307 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007308 (rssi == HCI_RSSI_INVALID ||
7309 (rssi < hdev->discovery.rssi &&
7310 !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))
Marcel Holtmannbda157a2014-12-05 10:55:56 +01007311 return;
7312
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02007313 /* Make sure that the buffer is big enough. The 5 extra bytes
7314 * are for the potential CoD field.
7315 */
7316 if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf))
Marcel Holtmann901801b2013-10-06 23:55:51 -07007317 return;
Andre Guedes7d262f82012-01-10 18:20:49 -03007318
Johan Hedberg1dc06092012-01-15 21:01:23 +02007319 memset(buf, 0, sizeof(buf));
7320
Marcel Holtmannda25cf62014-12-05 13:03:35 +01007321 /* In case of device discovery with BR/EDR devices (pre 1.2), the
7322 * RSSI value was reported as 0 when not available. This behavior
7323 * is kept when using device discovery. This is required for full
7324 * backwards compatibility with the API.
7325 *
7326 * However when using service discovery, the value 127 will be
7327 * returned when the RSSI is not available.
7328 */
Szymon Janc91200e92015-01-22 16:57:05 +01007329 if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi &&
7330 link_type == ACL_LINK)
Marcel Holtmannefb25132014-12-05 13:03:34 +01007331 rssi = 0;
7332
Johan Hedberg841c5642014-07-07 12:45:54 +03007333 bacpy(&ev->addr.bdaddr, bdaddr);
7334 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberge319d2e2012-01-15 19:51:59 +02007335 ev->rssi = rssi;
Marcel Holtmannaf589252014-07-01 14:11:20 +02007336 ev->flags = cpu_to_le32(flags);
Johan Hedberge17acd42011-03-30 23:57:16 +03007337
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007338 if (eir_len > 0) {
7339 /* When using service discovery and a list of UUID is
7340 * provided, results with no matching UUID should be
7341 * dropped. In case there is a match the result is
7342 * kept and checking possible scan response data
7343 * will be skipped.
7344 */
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007345 if (hdev->discovery.uuid_count > 0) {
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007346 match = eir_has_uuids(eir, eir_len,
7347 hdev->discovery.uuid_count,
7348 hdev->discovery.uuids);
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007349 /* If duplicate filtering does not report RSSI changes,
7350 * then restart scanning to ensure updated result with
7351 * updated RSSI values.
7352 */
7353 if (match && test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER,
7354 &hdev->quirks))
7355 restart_le_scan(hdev);
7356 } else {
Marcel Holtmannea8ae252014-12-17 18:18:08 +01007357 match = true;
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007358 }
Marcel Holtmannea8ae252014-12-17 18:18:08 +01007359
7360 if (!match && !scan_rsp_len)
7361 return;
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007362
7363 /* Copy EIR or advertising data into event */
Johan Hedberge319d2e2012-01-15 19:51:59 +02007364 memcpy(ev->eir, eir, eir_len);
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007365 } else {
7366 /* When using service discovery and a list of UUID is
7367 * provided, results with empty EIR or advertising data
7368 * should be dropped since they do not match any UUID.
7369 */
Marcel Holtmannea8ae252014-12-17 18:18:08 +01007370 if (hdev->discovery.uuid_count > 0 && !scan_rsp_len)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007371 return;
Marcel Holtmannea8ae252014-12-17 18:18:08 +01007372
7373 match = false;
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007374 }
Johan Hedberge17acd42011-03-30 23:57:16 +03007375
Johan Hedberg1dc06092012-01-15 21:01:23 +02007376 if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV))
7377 eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007378 dev_class, 3);
Johan Hedberg1dc06092012-01-15 21:01:23 +02007379
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007380 if (scan_rsp_len > 0) {
7381 /* When using service discovery and a list of UUID is
7382 * provided, results with no matching UUID should be
7383 * dropped if there is no previous match from the
7384 * advertising data.
7385 */
7386 if (hdev->discovery.uuid_count > 0) {
7387 if (!match && !eir_has_uuids(scan_rsp, scan_rsp_len,
7388 hdev->discovery.uuid_count,
7389 hdev->discovery.uuids))
7390 return;
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007391
7392 /* If duplicate filtering does not report RSSI changes,
7393 * then restart scanning to ensure updated result with
7394 * updated RSSI values.
7395 */
7396 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER,
7397 &hdev->quirks))
7398 restart_le_scan(hdev);
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007399 }
7400
7401 /* Append scan response data to event */
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02007402 memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len);
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007403 } else {
7404 /* When using service discovery and a list of UUID is
7405 * provided, results with empty scan response and no
7406 * previous matched advertising data should be dropped.
7407 */
7408 if (hdev->discovery.uuid_count > 0 && !match)
7409 return;
7410 }
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02007411
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007412 /* Validate the reported RSSI value against the RSSI threshold once more
7413 * incase HCI_QUIRK_STRICT_DUPLICATE_FILTER forced a restart of LE
7414 * scanning.
7415 */
7416 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
7417 rssi < hdev->discovery.rssi)
7418 return;
7419
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02007420 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
7421 ev_size = sizeof(*ev) + eir_len + scan_rsp_len;
Andre Guedesf8523592011-09-09 18:56:26 -03007422
Marcel Holtmann901801b2013-10-06 23:55:51 -07007423 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL);
Johan Hedberge17acd42011-03-30 23:57:16 +03007424}
Johan Hedberga88a9652011-03-30 13:18:12 +03007425
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07007426void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
7427 u8 addr_type, s8 rssi, u8 *name, u8 name_len)
Johan Hedberga88a9652011-03-30 13:18:12 +03007428{
Johan Hedbergb644ba32012-01-17 21:48:47 +02007429 struct mgmt_ev_device_found *ev;
7430 char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2];
7431 u16 eir_len;
Johan Hedberga88a9652011-03-30 13:18:12 +03007432
Johan Hedbergb644ba32012-01-17 21:48:47 +02007433 ev = (struct mgmt_ev_device_found *) buf;
Johan Hedberga88a9652011-03-30 13:18:12 +03007434
Johan Hedbergb644ba32012-01-17 21:48:47 +02007435 memset(buf, 0, sizeof(buf));
Johan Hedberga88a9652011-03-30 13:18:12 +03007436
Johan Hedbergb644ba32012-01-17 21:48:47 +02007437 bacpy(&ev->addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007438 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007439 ev->rssi = rssi;
7440
7441 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007442 name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007443
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02007444 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007445
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07007446 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL);
Johan Hedberga88a9652011-03-30 13:18:12 +03007447}
Johan Hedberg314b2382011-04-27 10:29:57 -04007448
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07007449void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
Johan Hedberg314b2382011-04-27 10:29:57 -04007450{
Johan Hedbergf963e8e2012-02-20 23:30:44 +02007451 struct mgmt_ev_discovering ev;
Johan Hedberg164a6e72011-11-01 17:06:44 +02007452
Andre Guedes343fb142011-11-22 17:14:19 -03007453 BT_DBG("%s discovering %u", hdev->name, discovering);
7454
Johan Hedbergf963e8e2012-02-20 23:30:44 +02007455 memset(&ev, 0, sizeof(ev));
7456 ev.type = hdev->discovery.type;
7457 ev.discovering = discovering;
7458
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07007459 mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg314b2382011-04-27 10:29:57 -04007460}
Antti Julku5e762442011-08-25 16:48:02 +03007461
Marcel Holtmann1904a852015-01-11 13:50:44 -08007462static void adv_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Marcel Holtmann5976e602013-10-06 04:08:14 -07007463{
7464 BT_DBG("%s status %u", hdev->name, status);
Marcel Holtmann5976e602013-10-06 04:08:14 -07007465}
7466
7467void mgmt_reenable_advertising(struct hci_dev *hdev)
7468{
7469 struct hci_request req;
7470
Marcel Holtmann5976e602013-10-06 04:08:14 -07007471 if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags))
7472 return;
7473
7474 hci_req_init(&req, hdev);
7475 enable_advertising(&req);
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03007476 hci_req_run(&req, adv_enable_complete);
Marcel Holtmann5976e602013-10-06 04:08:14 -07007477}