blob: 54abbce3a39e8bc0cdbd9018ca8e616e5c864546 [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>
32#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070033
34#include "smp.h"
Johan Hedberg03811012010-12-08 00:21:06 +020035
Johan Hedberg2da9c552012-02-17 14:39:28 +020036#define MGMT_VERSION 1
Marcel Holtmann40456642014-01-28 15:39:01 -080037#define MGMT_REVISION 5
Johan Hedberg02d98122010-12-13 21:07:04 +020038
Johan Hedberge70bb2e2012-02-13 16:59:33 +020039static const u16 mgmt_commands[] = {
40 MGMT_OP_READ_INDEX_LIST,
41 MGMT_OP_READ_INFO,
42 MGMT_OP_SET_POWERED,
43 MGMT_OP_SET_DISCOVERABLE,
44 MGMT_OP_SET_CONNECTABLE,
45 MGMT_OP_SET_FAST_CONNECTABLE,
46 MGMT_OP_SET_PAIRABLE,
47 MGMT_OP_SET_LINK_SECURITY,
48 MGMT_OP_SET_SSP,
49 MGMT_OP_SET_HS,
50 MGMT_OP_SET_LE,
51 MGMT_OP_SET_DEV_CLASS,
52 MGMT_OP_SET_LOCAL_NAME,
53 MGMT_OP_ADD_UUID,
54 MGMT_OP_REMOVE_UUID,
55 MGMT_OP_LOAD_LINK_KEYS,
56 MGMT_OP_LOAD_LONG_TERM_KEYS,
57 MGMT_OP_DISCONNECT,
58 MGMT_OP_GET_CONNECTIONS,
59 MGMT_OP_PIN_CODE_REPLY,
60 MGMT_OP_PIN_CODE_NEG_REPLY,
61 MGMT_OP_SET_IO_CAPABILITY,
62 MGMT_OP_PAIR_DEVICE,
63 MGMT_OP_CANCEL_PAIR_DEVICE,
64 MGMT_OP_UNPAIR_DEVICE,
65 MGMT_OP_USER_CONFIRM_REPLY,
66 MGMT_OP_USER_CONFIRM_NEG_REPLY,
67 MGMT_OP_USER_PASSKEY_REPLY,
68 MGMT_OP_USER_PASSKEY_NEG_REPLY,
69 MGMT_OP_READ_LOCAL_OOB_DATA,
70 MGMT_OP_ADD_REMOTE_OOB_DATA,
71 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
72 MGMT_OP_START_DISCOVERY,
73 MGMT_OP_STOP_DISCOVERY,
74 MGMT_OP_CONFIRM_NAME,
75 MGMT_OP_BLOCK_DEVICE,
76 MGMT_OP_UNBLOCK_DEVICE,
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -070077 MGMT_OP_SET_DEVICE_ID,
Johan Hedberg4375f102013-09-25 13:26:10 +030078 MGMT_OP_SET_ADVERTISING,
Johan Hedberg0663ca22013-10-02 13:43:14 +030079 MGMT_OP_SET_BREDR,
Marcel Holtmannd13eafc2013-10-02 04:41:30 -070080 MGMT_OP_SET_STATIC_ADDRESS,
Marcel Holtmann7f72134e2013-10-11 14:44:58 -070081 MGMT_OP_SET_SCAN_PARAMS,
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -080082 MGMT_OP_SET_SECURE_CONN,
Marcel Holtmann4e39ac82014-01-31 11:55:22 -080083 MGMT_OP_SET_DEBUG_KEYS,
Johan Hedberg62b04cd2014-02-23 19:42:27 +020084 MGMT_OP_SET_PRIVACY,
Johan Hedberg41edf162014-02-18 10:19:35 +020085 MGMT_OP_LOAD_IRKS,
Johan Hedberge70bb2e2012-02-13 16:59:33 +020086};
87
88static const u16 mgmt_events[] = {
89 MGMT_EV_CONTROLLER_ERROR,
90 MGMT_EV_INDEX_ADDED,
91 MGMT_EV_INDEX_REMOVED,
92 MGMT_EV_NEW_SETTINGS,
93 MGMT_EV_CLASS_OF_DEV_CHANGED,
94 MGMT_EV_LOCAL_NAME_CHANGED,
95 MGMT_EV_NEW_LINK_KEY,
96 MGMT_EV_NEW_LONG_TERM_KEY,
97 MGMT_EV_DEVICE_CONNECTED,
98 MGMT_EV_DEVICE_DISCONNECTED,
99 MGMT_EV_CONNECT_FAILED,
100 MGMT_EV_PIN_CODE_REQUEST,
101 MGMT_EV_USER_CONFIRM_REQUEST,
102 MGMT_EV_USER_PASSKEY_REQUEST,
103 MGMT_EV_AUTH_FAILED,
104 MGMT_EV_DEVICE_FOUND,
105 MGMT_EV_DISCOVERING,
106 MGMT_EV_DEVICE_BLOCKED,
107 MGMT_EV_DEVICE_UNBLOCKED,
108 MGMT_EV_DEVICE_UNPAIRED,
Johan Hedberg92a25252012-09-06 18:39:26 +0300109 MGMT_EV_PASSKEY_NOTIFY,
Marcel Holtmann1b60ef22014-02-21 21:35:30 -0800110 MGMT_EV_NEW_IRK,
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700111 MGMT_EV_NEW_CSRK,
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200112};
113
Marcel Holtmann17b02e62012-03-01 14:32:37 -0800114#define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)
Johan Hedberg7d785252011-12-15 00:47:39 +0200115
Johan Hedberg4b34ee782012-02-21 14:13:02 +0200116#define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \
117 !test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
118
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200119struct pending_cmd {
120 struct list_head list;
Johan Hedbergfc2f4b12011-11-09 13:58:56 +0200121 u16 opcode;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200122 int index;
Szymon Jancc68fb7f2011-03-22 13:12:19 +0100123 void *param;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200124 struct sock *sk;
Johan Hedberge9a416b2011-02-19 12:05:56 -0300125 void *user_data;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200126};
127
Johan Hedbergca69b792011-11-11 18:10:00 +0200128/* HCI to MGMT error code conversion table */
129static u8 mgmt_status_table[] = {
130 MGMT_STATUS_SUCCESS,
131 MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */
132 MGMT_STATUS_NOT_CONNECTED, /* No Connection */
133 MGMT_STATUS_FAILED, /* Hardware Failure */
134 MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */
135 MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */
Johan Hedbergeadd6632014-01-13 17:15:53 +0200136 MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */
Johan Hedbergca69b792011-11-11 18:10:00 +0200137 MGMT_STATUS_NO_RESOURCES, /* Memory Full */
138 MGMT_STATUS_TIMEOUT, /* Connection Timeout */
139 MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */
140 MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */
141 MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */
142 MGMT_STATUS_BUSY, /* Command Disallowed */
143 MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */
144 MGMT_STATUS_REJECTED, /* Rejected Security */
145 MGMT_STATUS_REJECTED, /* Rejected Personal */
146 MGMT_STATUS_TIMEOUT, /* Host Timeout */
147 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */
148 MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */
149 MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */
150 MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */
151 MGMT_STATUS_DISCONNECTED, /* OE Power Off */
152 MGMT_STATUS_DISCONNECTED, /* Connection Terminated */
153 MGMT_STATUS_BUSY, /* Repeated Attempts */
154 MGMT_STATUS_REJECTED, /* Pairing Not Allowed */
155 MGMT_STATUS_FAILED, /* Unknown LMP PDU */
156 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */
157 MGMT_STATUS_REJECTED, /* SCO Offset Rejected */
158 MGMT_STATUS_REJECTED, /* SCO Interval Rejected */
159 MGMT_STATUS_REJECTED, /* Air Mode Rejected */
160 MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */
161 MGMT_STATUS_FAILED, /* Unspecified Error */
162 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */
163 MGMT_STATUS_FAILED, /* Role Change Not Allowed */
164 MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */
165 MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */
166 MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */
167 MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */
168 MGMT_STATUS_FAILED, /* Unit Link Key Used */
169 MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */
170 MGMT_STATUS_TIMEOUT, /* Instant Passed */
171 MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */
172 MGMT_STATUS_FAILED, /* Transaction Collision */
173 MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */
174 MGMT_STATUS_REJECTED, /* QoS Rejected */
175 MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */
176 MGMT_STATUS_REJECTED, /* Insufficient Security */
177 MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */
178 MGMT_STATUS_BUSY, /* Role Switch Pending */
179 MGMT_STATUS_FAILED, /* Slot Violation */
180 MGMT_STATUS_FAILED, /* Role Switch Failed */
181 MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */
182 MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */
183 MGMT_STATUS_BUSY, /* Host Busy Pairing */
184 MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */
185 MGMT_STATUS_BUSY, /* Controller Busy */
186 MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */
187 MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */
188 MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */
189 MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */
190 MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */
191};
192
193static u8 mgmt_status(u8 hci_status)
194{
195 if (hci_status < ARRAY_SIZE(mgmt_status_table))
196 return mgmt_status_table[hci_status];
197
198 return MGMT_STATUS_FAILED;
199}
200
Szymon Janc4e51eae2011-02-25 19:05:48 +0100201static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status)
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200202{
203 struct sk_buff *skb;
204 struct mgmt_hdr *hdr;
205 struct mgmt_ev_cmd_status *ev;
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300206 int err;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200207
Szymon Janc34eb5252011-02-28 14:10:08 +0100208 BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status);
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200209
Andre Guedes790eff42012-06-07 19:05:46 -0300210 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL);
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200211 if (!skb)
212 return -ENOMEM;
213
214 hdr = (void *) skb_put(skb, sizeof(*hdr));
215
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700216 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS);
Szymon Janc4e51eae2011-02-25 19:05:48 +0100217 hdr->index = cpu_to_le16(index);
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200218 hdr->len = cpu_to_le16(sizeof(*ev));
219
220 ev = (void *) skb_put(skb, sizeof(*ev));
221 ev->status = status;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200222 ev->opcode = cpu_to_le16(cmd);
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200223
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300224 err = sock_queue_rcv_skb(sk, skb);
225 if (err < 0)
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200226 kfree_skb(skb);
227
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300228 return err;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200229}
230
Johan Hedbergaee9b212012-02-18 15:07:59 +0200231static int cmd_complete(struct sock *sk, u16 index, u16 cmd, u8 status,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300232 void *rp, size_t rp_len)
Johan Hedberg02d98122010-12-13 21:07:04 +0200233{
234 struct sk_buff *skb;
235 struct mgmt_hdr *hdr;
236 struct mgmt_ev_cmd_complete *ev;
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300237 int err;
Johan Hedberg02d98122010-12-13 21:07:04 +0200238
239 BT_DBG("sock %p", sk);
240
Andre Guedes790eff42012-06-07 19:05:46 -0300241 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL);
Johan Hedberg02d98122010-12-13 21:07:04 +0200242 if (!skb)
243 return -ENOMEM;
244
245 hdr = (void *) skb_put(skb, sizeof(*hdr));
Johan Hedberga38528f2011-01-22 06:46:43 +0200246
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700247 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE);
Szymon Janc4e51eae2011-02-25 19:05:48 +0100248 hdr->index = cpu_to_le16(index);
Johan Hedberga38528f2011-01-22 06:46:43 +0200249 hdr->len = cpu_to_le16(sizeof(*ev) + rp_len);
Johan Hedberg02d98122010-12-13 21:07:04 +0200250
Johan Hedberga38528f2011-01-22 06:46:43 +0200251 ev = (void *) skb_put(skb, sizeof(*ev) + rp_len);
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200252 ev->opcode = cpu_to_le16(cmd);
Johan Hedbergaee9b212012-02-18 15:07:59 +0200253 ev->status = status;
Szymon Janc8020c162011-02-28 14:09:50 +0100254
255 if (rp)
256 memcpy(ev->data, rp, rp_len);
Johan Hedberg02d98122010-12-13 21:07:04 +0200257
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300258 err = sock_queue_rcv_skb(sk, skb);
259 if (err < 0)
Johan Hedberg02d98122010-12-13 21:07:04 +0200260 kfree_skb(skb);
261
Marcel Holtmanne5f0e152012-02-22 11:59:01 +0100262 return err;
Johan Hedberg02d98122010-12-13 21:07:04 +0200263}
264
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300265static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
266 u16 data_len)
Johan Hedberga38528f2011-01-22 06:46:43 +0200267{
268 struct mgmt_rp_read_version rp;
269
270 BT_DBG("sock %p", sk);
271
272 rp.version = MGMT_VERSION;
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700273 rp.revision = cpu_to_le16(MGMT_REVISION);
Johan Hedberga38528f2011-01-22 06:46:43 +0200274
Johan Hedbergaee9b212012-02-18 15:07:59 +0200275 return cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, &rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300276 sizeof(rp));
Johan Hedberga38528f2011-01-22 06:46:43 +0200277}
278
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300279static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
280 u16 data_len)
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200281{
282 struct mgmt_rp_read_commands *rp;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200283 const u16 num_commands = ARRAY_SIZE(mgmt_commands);
284 const u16 num_events = ARRAY_SIZE(mgmt_events);
Andrei Emeltchenko2e3c35e2012-03-14 18:54:15 +0200285 __le16 *opcode;
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200286 size_t rp_size;
287 int i, err;
288
289 BT_DBG("sock %p", sk);
290
291 rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16));
292
293 rp = kmalloc(rp_size, GFP_KERNEL);
294 if (!rp)
295 return -ENOMEM;
296
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700297 rp->num_commands = cpu_to_le16(num_commands);
298 rp->num_events = cpu_to_le16(num_events);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200299
300 for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++)
301 put_unaligned_le16(mgmt_commands[i], opcode);
302
303 for (i = 0; i < num_events; i++, opcode++)
304 put_unaligned_le16(mgmt_events[i], opcode);
305
Johan Hedbergaee9b212012-02-18 15:07:59 +0200306 err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300307 rp_size);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200308 kfree(rp);
309
310 return err;
311}
312
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300313static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
314 u16 data_len)
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200315{
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200316 struct mgmt_rp_read_index_list *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200317 struct hci_dev *d;
Johan Hedberga38528f2011-01-22 06:46:43 +0200318 size_t rp_len;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200319 u16 count;
Johan Hedberg476e44c2012-10-19 20:10:46 +0300320 int err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200321
322 BT_DBG("sock %p", sk);
323
324 read_lock(&hci_dev_list_lock);
325
326 count = 0;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +0300327 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmann1514b892013-10-06 08:25:01 -0700328 if (d->dev_type == HCI_BREDR)
329 count++;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200330 }
331
Johan Hedberga38528f2011-01-22 06:46:43 +0200332 rp_len = sizeof(*rp) + (2 * count);
333 rp = kmalloc(rp_len, GFP_ATOMIC);
334 if (!rp) {
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100335 read_unlock(&hci_dev_list_lock);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200336 return -ENOMEM;
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100337 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200338
Johan Hedberg476e44c2012-10-19 20:10:46 +0300339 count = 0;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200340 list_for_each_entry(d, &hci_dev_list, list) {
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200341 if (test_bit(HCI_SETUP, &d->dev_flags))
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200342 continue;
343
Marcel Holtmann0736cfa2013-08-26 21:40:51 -0700344 if (test_bit(HCI_USER_CHANNEL, &d->dev_flags))
345 continue;
346
Marcel Holtmann1514b892013-10-06 08:25:01 -0700347 if (d->dev_type == HCI_BREDR) {
348 rp->index[count++] = cpu_to_le16(d->id);
349 BT_DBG("Added hci%u", d->id);
350 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200351 }
352
Johan Hedberg476e44c2012-10-19 20:10:46 +0300353 rp->num_controllers = cpu_to_le16(count);
354 rp_len = sizeof(*rp) + (2 * count);
355
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200356 read_unlock(&hci_dev_list_lock);
357
Johan Hedbergaee9b212012-02-18 15:07:59 +0200358 err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, 0, rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300359 rp_len);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200360
Johan Hedberga38528f2011-01-22 06:46:43 +0200361 kfree(rp);
362
363 return err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200364}
365
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200366static u32 get_supported_settings(struct hci_dev *hdev)
Johan Hedberg03811012010-12-08 00:21:06 +0200367{
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200368 u32 settings = 0;
Johan Hedberg03811012010-12-08 00:21:06 +0200369
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200370 settings |= MGMT_SETTING_POWERED;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200371 settings |= MGMT_SETTING_PAIRABLE;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800372 settings |= MGMT_SETTING_DEBUG_KEYS;
Johan Hedberg03811012010-12-08 00:21:06 +0200373
Andre Guedesed3fa312012-07-24 15:03:46 -0300374 if (lmp_bredr_capable(hdev)) {
Johan Hedberg33c525c2012-10-24 21:11:58 +0300375 settings |= MGMT_SETTING_CONNECTABLE;
Johan Hedberg1a47aee2013-03-15 17:07:06 -0500376 if (hdev->hci_ver >= BLUETOOTH_VER_1_2)
377 settings |= MGMT_SETTING_FAST_CONNECTABLE;
Johan Hedberg33c525c2012-10-24 21:11:58 +0300378 settings |= MGMT_SETTING_DISCOVERABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200379 settings |= MGMT_SETTING_BREDR;
380 settings |= MGMT_SETTING_LINK_SECURITY;
Marcel Holtmanna82974c2013-10-11 09:48:47 -0700381
382 if (lmp_ssp_capable(hdev)) {
383 settings |= MGMT_SETTING_SSP;
384 settings |= MGMT_SETTING_HS;
385 }
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800386
Marcel Holtmann5afeac12014-01-10 02:07:27 -0800387 if (lmp_sc_capable(hdev) ||
388 test_bit(HCI_FORCE_SC, &hdev->dev_flags))
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800389 settings |= MGMT_SETTING_SECURE_CONN;
Marcel Holtmann848566b2013-10-01 22:59:22 -0700390 }
Marcel Holtmannd7b7e792012-02-20 21:47:49 +0100391
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300392 if (lmp_le_capable(hdev)) {
Marcel Holtmann9d428202012-05-03 07:12:31 +0200393 settings |= MGMT_SETTING_LE;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300394 settings |= MGMT_SETTING_ADVERTISING;
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200395 settings |= MGMT_SETTING_PRIVACY;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300396 }
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200397
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200398 return settings;
399}
Johan Hedbergebc99fe2011-01-04 11:54:26 +0200400
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200401static u32 get_current_settings(struct hci_dev *hdev)
402{
403 u32 settings = 0;
Johan Hedbergdc4fe302011-03-16 14:29:36 +0200404
Johan Hedbergf1f0eb02012-02-21 17:15:41 +0200405 if (hdev_is_powered(hdev))
Marcel Holtmannf0d4b782012-02-21 12:14:25 +0100406 settings |= MGMT_SETTING_POWERED;
407
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200408 if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200409 settings |= MGMT_SETTING_CONNECTABLE;
410
Johan Hedberg1a4d3c42013-03-15 17:07:08 -0500411 if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags))
412 settings |= MGMT_SETTING_FAST_CONNECTABLE;
413
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200414 if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200415 settings |= MGMT_SETTING_DISCOVERABLE;
416
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200417 if (test_bit(HCI_PAIRABLE, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200418 settings |= MGMT_SETTING_PAIRABLE;
419
Johan Hedberg56f87902013-10-02 13:43:13 +0300420 if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200421 settings |= MGMT_SETTING_BREDR;
422
Johan Hedberg06199cf2012-02-22 16:37:11 +0200423 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200424 settings |= MGMT_SETTING_LE;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200425
Johan Hedberg47990ea2012-02-22 11:58:37 +0200426 if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200427 settings |= MGMT_SETTING_LINK_SECURITY;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200428
Johan Hedberg84bde9d2012-01-25 14:21:06 +0200429 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200430 settings |= MGMT_SETTING_SSP;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200431
Johan Hedberg6d80dfd2012-02-20 23:50:38 +0200432 if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags))
433 settings |= MGMT_SETTING_HS;
434
Johan Hedbergf3d3444a2013-10-05 12:01:04 +0200435 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300436 settings |= MGMT_SETTING_ADVERTISING;
437
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800438 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags))
439 settings |= MGMT_SETTING_SECURE_CONN;
440
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800441 if (test_bit(HCI_DEBUG_KEYS, &hdev->dev_flags))
442 settings |= MGMT_SETTING_DEBUG_KEYS;
443
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200444 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
445 settings |= MGMT_SETTING_PRIVACY;
446
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200447 return settings;
Johan Hedbergc542a062011-01-26 13:11:03 +0200448}
449
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300450#define PNP_INFO_SVCLASS_ID 0x1200
451
Johan Hedberg213202e2013-01-27 00:31:33 +0200452static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
453{
454 u8 *ptr = data, *uuids_start = NULL;
455 struct bt_uuid *uuid;
456
457 if (len < 4)
458 return ptr;
459
460 list_for_each_entry(uuid, &hdev->uuids, list) {
461 u16 uuid16;
462
463 if (uuid->size != 16)
464 continue;
465
466 uuid16 = get_unaligned_le16(&uuid->uuid[12]);
467 if (uuid16 < 0x1100)
468 continue;
469
470 if (uuid16 == PNP_INFO_SVCLASS_ID)
471 continue;
472
473 if (!uuids_start) {
474 uuids_start = ptr;
475 uuids_start[0] = 1;
476 uuids_start[1] = EIR_UUID16_ALL;
477 ptr += 2;
478 }
479
480 /* Stop if not enough space to put next UUID */
481 if ((ptr - data) + sizeof(u16) > len) {
482 uuids_start[1] = EIR_UUID16_SOME;
483 break;
484 }
485
486 *ptr++ = (uuid16 & 0x00ff);
487 *ptr++ = (uuid16 & 0xff00) >> 8;
488 uuids_start[0] += sizeof(uuid16);
489 }
490
491 return ptr;
492}
493
Johan Hedbergcdf19632013-01-27 00:31:34 +0200494static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
495{
496 u8 *ptr = data, *uuids_start = NULL;
497 struct bt_uuid *uuid;
498
499 if (len < 6)
500 return ptr;
501
502 list_for_each_entry(uuid, &hdev->uuids, list) {
503 if (uuid->size != 32)
504 continue;
505
506 if (!uuids_start) {
507 uuids_start = ptr;
508 uuids_start[0] = 1;
509 uuids_start[1] = EIR_UUID32_ALL;
510 ptr += 2;
511 }
512
513 /* Stop if not enough space to put next UUID */
514 if ((ptr - data) + sizeof(u32) > len) {
515 uuids_start[1] = EIR_UUID32_SOME;
516 break;
517 }
518
519 memcpy(ptr, &uuid->uuid[12], sizeof(u32));
520 ptr += sizeof(u32);
521 uuids_start[0] += sizeof(u32);
522 }
523
524 return ptr;
525}
526
Johan Hedbergc00d5752013-01-27 00:31:35 +0200527static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
528{
529 u8 *ptr = data, *uuids_start = NULL;
530 struct bt_uuid *uuid;
531
532 if (len < 18)
533 return ptr;
534
535 list_for_each_entry(uuid, &hdev->uuids, list) {
536 if (uuid->size != 128)
537 continue;
538
539 if (!uuids_start) {
540 uuids_start = ptr;
541 uuids_start[0] = 1;
542 uuids_start[1] = EIR_UUID128_ALL;
543 ptr += 2;
544 }
545
546 /* Stop if not enough space to put next UUID */
547 if ((ptr - data) + 16 > len) {
548 uuids_start[1] = EIR_UUID128_SOME;
549 break;
550 }
551
552 memcpy(ptr, uuid->uuid, 16);
553 ptr += 16;
554 uuids_start[0] += 16;
555 }
556
557 return ptr;
558}
559
Johan Hedbergeb2a8d22013-10-19 23:38:20 +0300560static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev)
561{
562 struct pending_cmd *cmd;
563
564 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
565 if (cmd->opcode == opcode)
566 return cmd;
567 }
568
569 return NULL;
570}
571
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700572static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
573{
Marcel Holtmann7a5f4992013-10-16 00:16:49 -0700574 u8 ad_len = 0;
575 size_t name_len;
576
577 name_len = strlen(hdev->dev_name);
578 if (name_len > 0) {
579 size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2;
580
581 if (name_len > max_len) {
582 name_len = max_len;
583 ptr[1] = EIR_NAME_SHORT;
584 } else
585 ptr[1] = EIR_NAME_COMPLETE;
586
587 ptr[0] = name_len + 1;
588
589 memcpy(ptr + 2, hdev->dev_name, name_len);
590
591 ad_len += (name_len + 2);
592 ptr += (name_len + 2);
593 }
594
595 return ad_len;
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700596}
597
598static void update_scan_rsp_data(struct hci_request *req)
599{
600 struct hci_dev *hdev = req->hdev;
601 struct hci_cp_le_set_scan_rsp_data cp;
602 u8 len;
603
Johan Hedberg7751ef12013-10-19 23:38:15 +0300604 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700605 return;
606
607 memset(&cp, 0, sizeof(cp));
608
609 len = create_scan_rsp_data(hdev, cp.data);
610
Johan Hedbergeb438b52013-10-16 15:31:07 +0300611 if (hdev->scan_rsp_data_len == len &&
612 memcmp(cp.data, hdev->scan_rsp_data, len) == 0)
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700613 return;
614
Johan Hedbergeb438b52013-10-16 15:31:07 +0300615 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
616 hdev->scan_rsp_data_len = len;
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700617
618 cp.length = len;
619
620 hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp);
621}
622
Johan Hedberg9a43e252013-10-20 19:00:07 +0300623static u8 get_adv_discov_flags(struct hci_dev *hdev)
624{
625 struct pending_cmd *cmd;
626
627 /* If there's a pending mgmt command the flags will not yet have
628 * their final values, so check for this first.
629 */
630 cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
631 if (cmd) {
632 struct mgmt_mode *cp = cmd->param;
633 if (cp->val == 0x01)
634 return LE_AD_GENERAL;
635 else if (cp->val == 0x02)
636 return LE_AD_LIMITED;
637 } else {
638 if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags))
639 return LE_AD_LIMITED;
640 else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
641 return LE_AD_GENERAL;
642 }
643
644 return 0;
645}
646
Marcel Holtmann46cad2e2013-10-16 00:16:46 -0700647static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr)
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700648{
649 u8 ad_len = 0, flags = 0;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700650
Johan Hedberg9a43e252013-10-20 19:00:07 +0300651 flags |= get_adv_discov_flags(hdev);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700652
Johan Hedberge8340042014-01-30 11:16:50 -0800653 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700654 flags |= LE_AD_NO_BREDR;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700655
656 if (flags) {
657 BT_DBG("adv flags 0x%02x", flags);
658
659 ptr[0] = 2;
660 ptr[1] = EIR_FLAGS;
661 ptr[2] = flags;
662
663 ad_len += 3;
664 ptr += 3;
665 }
666
667 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) {
668 ptr[0] = 2;
669 ptr[1] = EIR_TX_POWER;
670 ptr[2] = (u8) hdev->adv_tx_power;
671
672 ad_len += 3;
673 ptr += 3;
674 }
675
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700676 return ad_len;
677}
678
Marcel Holtmann5947f4b2013-10-16 00:16:50 -0700679static void update_adv_data(struct hci_request *req)
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700680{
681 struct hci_dev *hdev = req->hdev;
682 struct hci_cp_le_set_adv_data cp;
683 u8 len;
684
Johan Hedberg10994ce2013-10-19 23:38:16 +0300685 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700686 return;
687
688 memset(&cp, 0, sizeof(cp));
689
Marcel Holtmann46cad2e2013-10-16 00:16:46 -0700690 len = create_adv_data(hdev, cp.data);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700691
692 if (hdev->adv_data_len == len &&
693 memcmp(cp.data, hdev->adv_data, len) == 0)
694 return;
695
696 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
697 hdev->adv_data_len = len;
698
699 cp.length = len;
700
701 hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
702}
703
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300704static void create_eir(struct hci_dev *hdev, u8 *data)
705{
706 u8 *ptr = data;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300707 size_t name_len;
708
709 name_len = strlen(hdev->dev_name);
710
711 if (name_len > 0) {
712 /* EIR Data type */
713 if (name_len > 48) {
714 name_len = 48;
715 ptr[1] = EIR_NAME_SHORT;
716 } else
717 ptr[1] = EIR_NAME_COMPLETE;
718
719 /* EIR Data length */
720 ptr[0] = name_len + 1;
721
722 memcpy(ptr + 2, hdev->dev_name, name_len);
723
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300724 ptr += (name_len + 2);
725 }
726
Johan Hedbergbbaf4442012-11-08 01:22:59 +0100727 if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) {
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700728 ptr[0] = 2;
729 ptr[1] = EIR_TX_POWER;
730 ptr[2] = (u8) hdev->inq_tx_power;
731
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700732 ptr += 3;
733 }
734
Marcel Holtmann2b9be132012-03-11 19:32:12 -0700735 if (hdev->devid_source > 0) {
736 ptr[0] = 9;
737 ptr[1] = EIR_DEVICE_ID;
738
739 put_unaligned_le16(hdev->devid_source, ptr + 2);
740 put_unaligned_le16(hdev->devid_vendor, ptr + 4);
741 put_unaligned_le16(hdev->devid_product, ptr + 6);
742 put_unaligned_le16(hdev->devid_version, ptr + 8);
743
Marcel Holtmann2b9be132012-03-11 19:32:12 -0700744 ptr += 10;
745 }
746
Johan Hedberg213202e2013-01-27 00:31:33 +0200747 ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedbergcdf19632013-01-27 00:31:34 +0200748 ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedbergc00d5752013-01-27 00:31:35 +0200749 ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300750}
751
Johan Hedberg890ea892013-03-15 17:06:52 -0500752static void update_eir(struct hci_request *req)
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300753{
Johan Hedberg890ea892013-03-15 17:06:52 -0500754 struct hci_dev *hdev = req->hdev;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300755 struct hci_cp_write_eir cp;
756
Johan Hedberg504c8dc2012-02-23 13:30:41 +0200757 if (!hdev_is_powered(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -0500758 return;
Johan Hedberg7770c4a2012-02-22 22:06:38 +0200759
Johan Hedberg976eb202012-10-24 21:12:01 +0300760 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -0500761 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300762
Johan Hedberg84bde9d2012-01-25 14:21:06 +0200763 if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
Johan Hedberg890ea892013-03-15 17:06:52 -0500764 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300765
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200766 if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
Johan Hedberg890ea892013-03-15 17:06:52 -0500767 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300768
769 memset(&cp, 0, sizeof(cp));
770
771 create_eir(hdev, cp.data);
772
773 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
Johan Hedberg890ea892013-03-15 17:06:52 -0500774 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300775
776 memcpy(hdev->eir, cp.data, sizeof(cp.data));
777
Johan Hedberg890ea892013-03-15 17:06:52 -0500778 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300779}
780
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200781static u8 get_service_classes(struct hci_dev *hdev)
782{
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -0300783 struct bt_uuid *uuid;
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200784 u8 val = 0;
785
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -0300786 list_for_each_entry(uuid, &hdev->uuids, list)
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200787 val |= uuid->svc_hint;
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200788
789 return val;
790}
791
Johan Hedberg890ea892013-03-15 17:06:52 -0500792static void update_class(struct hci_request *req)
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200793{
Johan Hedberg890ea892013-03-15 17:06:52 -0500794 struct hci_dev *hdev = req->hdev;
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200795 u8 cod[3];
796
797 BT_DBG("%s", hdev->name);
798
Johan Hedberg504c8dc2012-02-23 13:30:41 +0200799 if (!hdev_is_powered(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -0500800 return;
Johan Hedberg7770c4a2012-02-22 22:06:38 +0200801
Johan Hedbergf87ea1d2013-10-19 23:38:17 +0300802 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
803 return;
804
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200805 if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
Johan Hedberg890ea892013-03-15 17:06:52 -0500806 return;
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200807
808 cod[0] = hdev->minor_class;
809 cod[1] = hdev->major_class;
810 cod[2] = get_service_classes(hdev);
811
Marcel Holtmann6acd7db2013-10-15 06:33:53 -0700812 if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags))
813 cod[1] |= 0x20;
814
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200815 if (memcmp(cod, hdev->dev_class, 3) == 0)
Johan Hedberg890ea892013-03-15 17:06:52 -0500816 return;
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200817
Johan Hedberg890ea892013-03-15 17:06:52 -0500818 hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200819}
820
Johan Hedberga4858cb2014-02-25 19:56:31 +0200821static bool get_connectable(struct hci_dev *hdev)
Johan Hedberg199a2fb2014-02-22 19:06:33 +0200822{
823 struct pending_cmd *cmd;
Johan Hedberg199a2fb2014-02-22 19:06:33 +0200824
825 /* If there's a pending mgmt command the flag will not yet have
826 * it's final value, so check for this first.
827 */
828 cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
829 if (cmd) {
830 struct mgmt_mode *cp = cmd->param;
Johan Hedberga4858cb2014-02-25 19:56:31 +0200831 return cp->val;
Johan Hedberg199a2fb2014-02-22 19:06:33 +0200832 }
833
Johan Hedberga4858cb2014-02-25 19:56:31 +0200834 return test_bit(HCI_CONNECTABLE, &hdev->dev_flags);
Johan Hedberg199a2fb2014-02-22 19:06:33 +0200835}
836
837static void enable_advertising(struct hci_request *req)
838{
839 struct hci_dev *hdev = req->hdev;
840 struct hci_cp_le_set_adv_param cp;
Johan Hedberg8f2a0602014-02-23 19:42:23 +0200841 u8 own_addr_type, enable = 0x01;
Johan Hedberga4858cb2014-02-25 19:56:31 +0200842 bool connectable;
Johan Hedberg199a2fb2014-02-22 19:06:33 +0200843
Johan Hedberg8d972502014-02-28 12:54:14 +0200844 /* Clear the HCI_ADVERTISING bit temporarily so that the
845 * hci_update_random_address knows that it's safe to go ahead
846 * and write a new random address. The flag will be set back on
847 * as soon as the SET_ADV_ENABLE HCI command completes.
848 */
849 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
850
Johan Hedberga4858cb2014-02-25 19:56:31 +0200851 connectable = get_connectable(hdev);
Johan Hedberg8f2a0602014-02-23 19:42:23 +0200852
Johan Hedberga4858cb2014-02-25 19:56:31 +0200853 /* Set require_privacy to true only when non-connectable
854 * advertising is used. In that case it is fine to use a
855 * non-resolvable private address.
856 */
857 if (hci_update_random_address(req, !connectable, &own_addr_type) < 0)
Johan Hedberg8f2a0602014-02-23 19:42:23 +0200858 return;
859
Marcel Holtmann41c90c12014-02-23 20:25:55 -0800860 memset(&cp, 0, sizeof(cp));
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700861 cp.min_interval = cpu_to_le16(0x0800);
862 cp.max_interval = cpu_to_le16(0x0800);
Johan Hedberga4858cb2014-02-25 19:56:31 +0200863 cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND;
Johan Hedberg8f2a0602014-02-23 19:42:23 +0200864 cp.own_address_type = own_addr_type;
Johan Hedberg199a2fb2014-02-22 19:06:33 +0200865 cp.channel_map = hdev->le_adv_channel_map;
866
867 hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
868
869 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
870}
871
872static void disable_advertising(struct hci_request *req)
873{
874 u8 enable = 0x00;
875
876 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
877}
878
Johan Hedberg7d785252011-12-15 00:47:39 +0200879static void service_cache_off(struct work_struct *work)
880{
881 struct hci_dev *hdev = container_of(work, struct hci_dev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300882 service_cache.work);
Johan Hedberg890ea892013-03-15 17:06:52 -0500883 struct hci_request req;
Johan Hedberg7d785252011-12-15 00:47:39 +0200884
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200885 if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
Johan Hedberg7d785252011-12-15 00:47:39 +0200886 return;
887
Johan Hedberg890ea892013-03-15 17:06:52 -0500888 hci_req_init(&req, hdev);
889
Johan Hedberg7d785252011-12-15 00:47:39 +0200890 hci_dev_lock(hdev);
891
Johan Hedberg890ea892013-03-15 17:06:52 -0500892 update_eir(&req);
893 update_class(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +0200894
895 hci_dev_unlock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -0500896
897 hci_req_run(&req, NULL);
Johan Hedberg7d785252011-12-15 00:47:39 +0200898}
899
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200900static void rpa_expired(struct work_struct *work)
901{
902 struct hci_dev *hdev = container_of(work, struct hci_dev,
903 rpa_expired.work);
904 struct hci_request req;
905
906 BT_DBG("");
907
908 set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
909
910 if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags) ||
911 hci_conn_num(hdev, LE_LINK) > 0)
912 return;
913
914 /* The generation of a new RPA and programming it into the
915 * controller happens in the enable_advertising() function.
916 */
917
918 hci_req_init(&req, hdev);
919
920 disable_advertising(&req);
921 enable_advertising(&req);
922
923 hci_req_run(&req, NULL);
924}
925
Johan Hedberg6a919082012-02-28 06:17:26 +0200926static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
Johan Hedberg7d785252011-12-15 00:47:39 +0200927{
Johan Hedberg4f87da82012-03-02 19:55:56 +0200928 if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg6a919082012-02-28 06:17:26 +0200929 return;
930
Johan Hedberg4f87da82012-03-02 19:55:56 +0200931 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200932 INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
Johan Hedberg7d785252011-12-15 00:47:39 +0200933
Johan Hedberg4f87da82012-03-02 19:55:56 +0200934 /* Non-mgmt controlled devices get this bit set
935 * implicitly so that pairing works for them, however
936 * for mgmt we require user-space to explicitly enable
937 * it
938 */
939 clear_bit(HCI_PAIRABLE, &hdev->dev_flags);
Johan Hedberg7d785252011-12-15 00:47:39 +0200940}
941
Johan Hedberg0f4e68c2012-02-28 17:18:30 +0200942static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300943 void *data, u16 data_len)
Johan Hedberg03811012010-12-08 00:21:06 +0200944{
945 struct mgmt_rp_read_info rp;
Johan Hedberg03811012010-12-08 00:21:06 +0200946
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200947 BT_DBG("sock %p %s", sk, hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +0200948
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300949 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +0200950
Johan Hedberg03811012010-12-08 00:21:06 +0200951 memset(&rp, 0, sizeof(rp));
952
Johan Hedberg03811012010-12-08 00:21:06 +0200953 bacpy(&rp.bdaddr, &hdev->bdaddr);
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200954
955 rp.version = hdev->hci_ver;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200956 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200957
958 rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
959 rp.current_settings = cpu_to_le32(get_current_settings(hdev));
960
961 memcpy(rp.dev_class, hdev->dev_class, 3);
Johan Hedberg03811012010-12-08 00:21:06 +0200962
963 memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
Johan Hedberg27fcc362012-02-22 21:46:22 +0200964 memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
Johan Hedberg03811012010-12-08 00:21:06 +0200965
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300966 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +0200967
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200968 return cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300969 sizeof(rp));
Johan Hedberg03811012010-12-08 00:21:06 +0200970}
971
972static void mgmt_pending_free(struct pending_cmd *cmd)
973{
974 sock_put(cmd->sk);
975 kfree(cmd->param);
976 kfree(cmd);
977}
978
979static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300980 struct hci_dev *hdev, void *data,
981 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +0200982{
983 struct pending_cmd *cmd;
984
Andre Guedes12b94562012-06-07 19:05:45 -0300985 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
Johan Hedberg03811012010-12-08 00:21:06 +0200986 if (!cmd)
987 return NULL;
988
989 cmd->opcode = opcode;
990 cmd->index = hdev->id;
991
Andre Guedes12b94562012-06-07 19:05:45 -0300992 cmd->param = kmalloc(len, GFP_KERNEL);
Johan Hedberg03811012010-12-08 00:21:06 +0200993 if (!cmd->param) {
994 kfree(cmd);
995 return NULL;
996 }
997
998 if (data)
999 memcpy(cmd->param, data, len);
1000
1001 cmd->sk = sk;
1002 sock_hold(sk);
1003
1004 list_add(&cmd->list, &hdev->mgmt_pending);
1005
1006 return cmd;
1007}
1008
1009static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03001010 void (*cb)(struct pending_cmd *cmd,
1011 void *data),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001012 void *data)
Johan Hedberg03811012010-12-08 00:21:06 +02001013{
Andre Guedesa3d09352013-02-01 11:21:30 -03001014 struct pending_cmd *cmd, *tmp;
Johan Hedberg03811012010-12-08 00:21:06 +02001015
Andre Guedesa3d09352013-02-01 11:21:30 -03001016 list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) {
Johan Hedberg03811012010-12-08 00:21:06 +02001017 if (opcode > 0 && cmd->opcode != opcode)
1018 continue;
1019
1020 cb(cmd, data);
1021 }
1022}
1023
Johan Hedberg03811012010-12-08 00:21:06 +02001024static void mgmt_pending_remove(struct pending_cmd *cmd)
1025{
1026 list_del(&cmd->list);
1027 mgmt_pending_free(cmd);
1028}
1029
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001030static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
Johan Hedberg86805702011-11-11 16:18:52 +02001031{
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001032 __le32 settings = cpu_to_le32(get_current_settings(hdev));
Johan Hedberg86805702011-11-11 16:18:52 +02001033
Johan Hedbergaee9b212012-02-18 15:07:59 +02001034 return cmd_complete(sk, hdev->id, opcode, 0, &settings,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001035 sizeof(settings));
Johan Hedberg86805702011-11-11 16:18:52 +02001036}
1037
Johan Hedberg8b064a32014-02-24 14:52:22 +02001038static void clean_up_hci_complete(struct hci_dev *hdev, u8 status)
1039{
1040 BT_DBG("%s status 0x%02x", hdev->name, status);
1041
Johan Hedberga3172b72014-02-28 09:33:44 +02001042 if (hci_conn_count(hdev) == 0) {
1043 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001044 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberga3172b72014-02-28 09:33:44 +02001045 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001046}
1047
1048static int clean_up_hci_state(struct hci_dev *hdev)
1049{
1050 struct hci_request req;
1051 struct hci_conn *conn;
1052
1053 hci_req_init(&req, hdev);
1054
1055 if (test_bit(HCI_ISCAN, &hdev->flags) ||
1056 test_bit(HCI_PSCAN, &hdev->flags)) {
1057 u8 scan = 0x00;
1058 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1059 }
1060
1061 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
1062 disable_advertising(&req);
1063
1064 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) {
Andre Guedesb1efcc22014-02-26 20:21:40 -03001065 hci_req_add_le_scan_disable(&req);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001066 }
1067
1068 list_for_each_entry(conn, &hdev->conn_hash.list, list) {
1069 struct hci_cp_disconnect dc;
Johan Hedbergc9910d02014-02-27 14:35:12 +02001070 struct hci_cp_reject_conn_req rej;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001071
Johan Hedbergc9910d02014-02-27 14:35:12 +02001072 switch (conn->state) {
1073 case BT_CONNECTED:
1074 case BT_CONFIG:
1075 dc.handle = cpu_to_le16(conn->handle);
1076 dc.reason = 0x15; /* Terminated due to Power Off */
1077 hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc);
1078 break;
1079 case BT_CONNECT:
1080 if (conn->type == LE_LINK)
1081 hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL,
1082 0, NULL);
1083 else if (conn->type == ACL_LINK)
1084 hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL,
1085 6, &conn->dst);
1086 break;
1087 case BT_CONNECT2:
1088 bacpy(&rej.bdaddr, &conn->dst);
1089 rej.reason = 0x15; /* Terminated due to Power Off */
1090 if (conn->type == ACL_LINK)
1091 hci_req_add(&req, HCI_OP_REJECT_CONN_REQ,
1092 sizeof(rej), &rej);
1093 else if (conn->type == SCO_LINK)
1094 hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ,
1095 sizeof(rej), &rej);
1096 break;
1097 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001098 }
1099
1100 return hci_req_run(&req, clean_up_hci_complete);
1101}
1102
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001103static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001104 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001105{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001106 struct mgmt_mode *cp = data;
Johan Hedberg03811012010-12-08 00:21:06 +02001107 struct pending_cmd *cmd;
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001108 int err;
Johan Hedberg03811012010-12-08 00:21:06 +02001109
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001110 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001111
Johan Hedberga7e80f22013-01-09 16:05:19 +02001112 if (cp->val != 0x00 && cp->val != 0x01)
1113 return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1114 MGMT_STATUS_INVALID_PARAMS);
1115
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001116 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001117
Johan Hedberg87b95ba2013-09-25 13:26:06 +03001118 if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) {
1119 err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1120 MGMT_STATUS_BUSY);
1121 goto failed;
1122 }
1123
Marcel Holtmannf0d4b782012-02-21 12:14:25 +01001124 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) {
1125 cancel_delayed_work(&hdev->power_off);
1126
1127 if (cp->val) {
Johan Hedberga1d70452013-01-09 15:29:40 +02001128 mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev,
1129 data, len);
1130 err = mgmt_powered(hdev, 1);
Marcel Holtmannf0d4b782012-02-21 12:14:25 +01001131 goto failed;
1132 }
1133 }
1134
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001135 if (!!cp->val == hdev_is_powered(hdev)) {
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001136 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001137 goto failed;
1138 }
1139
Johan Hedberg03811012010-12-08 00:21:06 +02001140 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
1141 if (!cmd) {
1142 err = -ENOMEM;
1143 goto failed;
1144 }
1145
Johan Hedberg8b064a32014-02-24 14:52:22 +02001146 if (cp->val) {
Johan Hedberg19202572013-01-14 22:33:51 +02001147 queue_work(hdev->req_workqueue, &hdev->power_on);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001148 err = 0;
1149 } else {
1150 /* Disconnect connections, stop scans, etc */
1151 err = clean_up_hci_state(hdev);
Johan Hedberga3172b72014-02-28 09:33:44 +02001152 if (!err)
1153 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
1154 HCI_POWER_OFF_TIMEOUT);
Johan Hedberg03811012010-12-08 00:21:06 +02001155
Johan Hedberg8b064a32014-02-24 14:52:22 +02001156 /* ENODATA means there were no HCI commands queued */
1157 if (err == -ENODATA) {
Johan Hedberga3172b72014-02-28 09:33:44 +02001158 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001159 queue_work(hdev->req_workqueue, &hdev->power_off.work);
1160 err = 0;
1161 }
1162 }
Johan Hedberg03811012010-12-08 00:21:06 +02001163
1164failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001165 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001166 return err;
1167}
1168
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001169static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len,
1170 struct sock *skip_sk)
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001171{
1172 struct sk_buff *skb;
1173 struct mgmt_hdr *hdr;
1174
Andre Guedes790eff42012-06-07 19:05:46 -03001175 skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL);
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001176 if (!skb)
1177 return -ENOMEM;
1178
1179 hdr = (void *) skb_put(skb, sizeof(*hdr));
1180 hdr->opcode = cpu_to_le16(event);
1181 if (hdev)
1182 hdr->index = cpu_to_le16(hdev->id);
1183 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001184 hdr->index = cpu_to_le16(MGMT_INDEX_NONE);
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001185 hdr->len = cpu_to_le16(data_len);
1186
1187 if (data)
1188 memcpy(skb_put(skb, data_len), data, data_len);
1189
Marcel Holtmann97e0bde2012-02-22 13:49:28 +01001190 /* Time stamp */
1191 __net_timestamp(skb);
1192
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001193 hci_send_to_control(skb, skip_sk);
1194 kfree_skb(skb);
1195
1196 return 0;
1197}
1198
1199static int new_settings(struct hci_dev *hdev, struct sock *skip)
1200{
1201 __le32 ev;
1202
1203 ev = cpu_to_le32(get_current_settings(hdev));
1204
1205 return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip);
1206}
1207
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001208struct cmd_lookup {
1209 struct sock *sk;
1210 struct hci_dev *hdev;
1211 u8 mgmt_status;
1212};
1213
1214static void settings_rsp(struct pending_cmd *cmd, void *data)
1215{
1216 struct cmd_lookup *match = data;
1217
1218 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
1219
1220 list_del(&cmd->list);
1221
1222 if (match->sk == NULL) {
1223 match->sk = cmd->sk;
1224 sock_hold(match->sk);
1225 }
1226
1227 mgmt_pending_free(cmd);
1228}
1229
1230static void cmd_status_rsp(struct pending_cmd *cmd, void *data)
1231{
1232 u8 *status = data;
1233
1234 cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
1235 mgmt_pending_remove(cmd);
1236}
1237
Johan Hedberge6fe7982013-10-02 15:45:22 +03001238static u8 mgmt_bredr_support(struct hci_dev *hdev)
1239{
1240 if (!lmp_bredr_capable(hdev))
1241 return MGMT_STATUS_NOT_SUPPORTED;
1242 else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
1243 return MGMT_STATUS_REJECTED;
1244 else
1245 return MGMT_STATUS_SUCCESS;
1246}
1247
1248static u8 mgmt_le_support(struct hci_dev *hdev)
1249{
1250 if (!lmp_le_capable(hdev))
1251 return MGMT_STATUS_NOT_SUPPORTED;
1252 else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
1253 return MGMT_STATUS_REJECTED;
1254 else
1255 return MGMT_STATUS_SUCCESS;
1256}
1257
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001258static void set_discoverable_complete(struct hci_dev *hdev, u8 status)
1259{
1260 struct pending_cmd *cmd;
1261 struct mgmt_mode *cp;
Marcel Holtmann970ba522013-10-15 06:33:57 -07001262 struct hci_request req;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001263 bool changed;
1264
1265 BT_DBG("status 0x%02x", status);
1266
1267 hci_dev_lock(hdev);
1268
1269 cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
1270 if (!cmd)
1271 goto unlock;
1272
1273 if (status) {
1274 u8 mgmt_err = mgmt_status(status);
1275 cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001276 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001277 goto remove_cmd;
1278 }
1279
1280 cp = cmd->param;
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001281 if (cp->val) {
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001282 changed = !test_and_set_bit(HCI_DISCOVERABLE,
1283 &hdev->dev_flags);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001284
1285 if (hdev->discov_timeout > 0) {
1286 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1287 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
1288 to);
1289 }
1290 } else {
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001291 changed = test_and_clear_bit(HCI_DISCOVERABLE,
1292 &hdev->dev_flags);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001293 }
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001294
1295 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1296
1297 if (changed)
1298 new_settings(hdev, cmd->sk);
1299
Marcel Holtmann970ba522013-10-15 06:33:57 -07001300 /* When the discoverable mode gets changed, make sure
1301 * that class of device has the limited discoverable
1302 * bit correctly set.
1303 */
1304 hci_req_init(&req, hdev);
1305 update_class(&req);
1306 hci_req_run(&req, NULL);
1307
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001308remove_cmd:
1309 mgmt_pending_remove(cmd);
1310
1311unlock:
1312 hci_dev_unlock(hdev);
1313}
1314
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001315static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001316 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001317{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001318 struct mgmt_cp_set_discoverable *cp = data;
Johan Hedberg03811012010-12-08 00:21:06 +02001319 struct pending_cmd *cmd;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001320 struct hci_request req;
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001321 u16 timeout;
Johan Hedberg9a43e252013-10-20 19:00:07 +03001322 u8 scan;
Johan Hedberg03811012010-12-08 00:21:06 +02001323 int err;
1324
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001325 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001326
Johan Hedberg9a43e252013-10-20 19:00:07 +03001327 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) &&
1328 !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
Johan Hedberg33c525c2012-10-24 21:11:58 +03001329 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Johan Hedberg9a43e252013-10-20 19:00:07 +03001330 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001331
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001332 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga7e80f22013-01-09 16:05:19 +02001333 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1334 MGMT_STATUS_INVALID_PARAMS);
1335
Marcel Holtmann1f350c82012-03-12 20:31:08 -07001336 timeout = __le16_to_cpu(cp->timeout);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001337
1338 /* Disabling discoverable requires that no timeout is set,
1339 * and enabling limited discoverable requires a timeout.
1340 */
1341 if ((cp->val == 0x00 && timeout > 0) ||
1342 (cp->val == 0x02 && timeout == 0))
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001343 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001344 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001345
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001346 hci_dev_lock(hdev);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001347
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001348 if (!hdev_is_powered(hdev) && timeout > 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001349 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001350 MGMT_STATUS_NOT_POWERED);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001351 goto failed;
1352 }
1353
1354 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03001355 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001356 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001357 MGMT_STATUS_BUSY);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001358 goto failed;
1359 }
1360
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001361 if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001362 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001363 MGMT_STATUS_REJECTED);
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001364 goto failed;
1365 }
1366
1367 if (!hdev_is_powered(hdev)) {
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001368 bool changed = false;
1369
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001370 /* Setting limited discoverable when powered off is
1371 * not a valid operation since it requires a timeout
1372 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1373 */
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001374 if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) {
1375 change_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
1376 changed = true;
1377 }
1378
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001379 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001380 if (err < 0)
1381 goto failed;
1382
1383 if (changed)
1384 err = new_settings(hdev, sk);
1385
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001386 goto failed;
1387 }
1388
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001389 /* If the current mode is the same, then just update the timeout
1390 * value with the new value. And if only the timeout gets updated,
1391 * then no need for any HCI transactions.
1392 */
1393 if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) &&
1394 (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE,
1395 &hdev->dev_flags)) {
Marcel Holtmann36261542013-10-15 08:28:51 -07001396 cancel_delayed_work(&hdev->discov_off);
1397 hdev->discov_timeout = timeout;
Marcel Holtmann955638e2012-02-22 18:21:00 +01001398
Marcel Holtmann36261542013-10-15 08:28:51 -07001399 if (cp->val && hdev->discov_timeout > 0) {
1400 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001401 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
Marcel Holtmann36261542013-10-15 08:28:51 -07001402 to);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001403 }
1404
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001405 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001406 goto failed;
1407 }
1408
1409 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
1410 if (!cmd) {
1411 err = -ENOMEM;
1412 goto failed;
1413 }
1414
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001415 /* Cancel any potential discoverable timeout that might be
1416 * still active and store new timeout value. The arming of
1417 * the timeout happens in the complete handler.
1418 */
1419 cancel_delayed_work(&hdev->discov_off);
1420 hdev->discov_timeout = timeout;
1421
Johan Hedbergb456f872013-10-19 23:38:22 +03001422 /* Limited discoverable mode */
1423 if (cp->val == 0x02)
1424 set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1425 else
1426 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1427
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001428 hci_req_init(&req, hdev);
1429
Johan Hedberg9a43e252013-10-20 19:00:07 +03001430 /* The procedure for LE-only controllers is much simpler - just
1431 * update the advertising data.
1432 */
1433 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
1434 goto update_ad;
1435
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001436 scan = SCAN_PAGE;
1437
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001438 if (cp->val) {
1439 struct hci_cp_write_current_iac_lap hci_cp;
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001440
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001441 if (cp->val == 0x02) {
1442 /* Limited discoverable mode */
Marcel Holtmann33337dc2013-10-23 08:28:01 -07001443 hci_cp.num_iac = min_t(u8, hdev->num_iac, 2);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001444 hci_cp.iac_lap[0] = 0x00; /* LIAC */
1445 hci_cp.iac_lap[1] = 0x8b;
1446 hci_cp.iac_lap[2] = 0x9e;
1447 hci_cp.iac_lap[3] = 0x33; /* GIAC */
1448 hci_cp.iac_lap[4] = 0x8b;
1449 hci_cp.iac_lap[5] = 0x9e;
1450 } else {
1451 /* General discoverable mode */
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001452 hci_cp.num_iac = 1;
1453 hci_cp.iac_lap[0] = 0x33; /* GIAC */
1454 hci_cp.iac_lap[1] = 0x8b;
1455 hci_cp.iac_lap[2] = 0x9e;
1456 }
1457
1458 hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP,
1459 (hci_cp.num_iac * 3) + 1, &hci_cp);
1460
1461 scan |= SCAN_INQUIRY;
1462 } else {
1463 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1464 }
1465
1466 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001467
Johan Hedberg9a43e252013-10-20 19:00:07 +03001468update_ad:
1469 update_adv_data(&req);
1470
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001471 err = hci_req_run(&req, set_discoverable_complete);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001472 if (err < 0)
1473 mgmt_pending_remove(cmd);
1474
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001475failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001476 hci_dev_unlock(hdev);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001477 return err;
1478}
1479
Johan Hedberg406d7802013-03-15 17:07:09 -05001480static void write_fast_connectable(struct hci_request *req, bool enable)
1481{
Johan Hedbergbd98b992013-03-15 17:07:13 -05001482 struct hci_dev *hdev = req->hdev;
Johan Hedberg406d7802013-03-15 17:07:09 -05001483 struct hci_cp_write_page_scan_activity acp;
1484 u8 type;
1485
Johan Hedberg547003b2013-10-21 16:51:53 +03001486 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
1487 return;
1488
Johan Hedberg4c01f8b2013-03-15 17:07:14 -05001489 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
1490 return;
1491
Johan Hedberg406d7802013-03-15 17:07:09 -05001492 if (enable) {
1493 type = PAGE_SCAN_TYPE_INTERLACED;
1494
1495 /* 160 msec page scan interval */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001496 acp.interval = cpu_to_le16(0x0100);
Johan Hedberg406d7802013-03-15 17:07:09 -05001497 } else {
1498 type = PAGE_SCAN_TYPE_STANDARD; /* default */
1499
1500 /* default 1.28 sec page scan */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001501 acp.interval = cpu_to_le16(0x0800);
Johan Hedberg406d7802013-03-15 17:07:09 -05001502 }
1503
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001504 acp.window = cpu_to_le16(0x0012);
Johan Hedberg406d7802013-03-15 17:07:09 -05001505
Johan Hedbergbd98b992013-03-15 17:07:13 -05001506 if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval ||
1507 __cpu_to_le16(hdev->page_scan_window) != acp.window)
1508 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
1509 sizeof(acp), &acp);
1510
1511 if (hdev->page_scan_type != type)
1512 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type);
Johan Hedberg406d7802013-03-15 17:07:09 -05001513}
1514
Johan Hedberg2b76f452013-03-15 17:07:04 -05001515static void set_connectable_complete(struct hci_dev *hdev, u8 status)
1516{
1517 struct pending_cmd *cmd;
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001518 struct mgmt_mode *cp;
1519 bool changed;
Johan Hedberg2b76f452013-03-15 17:07:04 -05001520
1521 BT_DBG("status 0x%02x", status);
1522
1523 hci_dev_lock(hdev);
1524
1525 cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
1526 if (!cmd)
1527 goto unlock;
1528
Johan Hedberg37438c12013-10-14 16:20:05 +03001529 if (status) {
1530 u8 mgmt_err = mgmt_status(status);
1531 cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
1532 goto remove_cmd;
1533 }
1534
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001535 cp = cmd->param;
1536 if (cp->val)
1537 changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
1538 else
1539 changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags);
1540
Johan Hedberg2b76f452013-03-15 17:07:04 -05001541 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
1542
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001543 if (changed)
1544 new_settings(hdev, cmd->sk);
1545
Johan Hedberg37438c12013-10-14 16:20:05 +03001546remove_cmd:
Johan Hedberg2b76f452013-03-15 17:07:04 -05001547 mgmt_pending_remove(cmd);
1548
1549unlock:
1550 hci_dev_unlock(hdev);
1551}
1552
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001553static int set_connectable_update_settings(struct hci_dev *hdev,
1554 struct sock *sk, u8 val)
1555{
1556 bool changed = false;
1557 int err;
1558
1559 if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
1560 changed = true;
1561
1562 if (val) {
1563 set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
1564 } else {
1565 clear_bit(HCI_CONNECTABLE, &hdev->dev_flags);
1566 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
1567 }
1568
1569 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
1570 if (err < 0)
1571 return err;
1572
1573 if (changed)
1574 return new_settings(hdev, sk);
1575
1576 return 0;
1577}
1578
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001579static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001580 u16 len)
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001581{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001582 struct mgmt_mode *cp = data;
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001583 struct pending_cmd *cmd;
Johan Hedberg2b76f452013-03-15 17:07:04 -05001584 struct hci_request req;
Johan Hedberg1987fdc2013-10-14 21:15:24 +03001585 u8 scan;
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001586 int err;
Johan Hedberg03811012010-12-08 00:21:06 +02001587
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001588 BT_DBG("request for %s", hdev->name);
Johan Hedberge41d8b42010-12-13 21:07:03 +02001589
Johan Hedberg1987fdc2013-10-14 21:15:24 +03001590 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) &&
1591 !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
Johan Hedberg33c525c2012-10-24 21:11:58 +03001592 return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
Johan Hedberg1987fdc2013-10-14 21:15:24 +03001593 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001594
Johan Hedberga7e80f22013-01-09 16:05:19 +02001595 if (cp->val != 0x00 && cp->val != 0x01)
1596 return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1597 MGMT_STATUS_INVALID_PARAMS);
1598
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001599 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001600
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001601 if (!hdev_is_powered(hdev)) {
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001602 err = set_connectable_update_settings(hdev, sk, cp->val);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001603 goto failed;
1604 }
1605
1606 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03001607 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001608 err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001609 MGMT_STATUS_BUSY);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001610 goto failed;
1611 }
1612
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001613 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
1614 if (!cmd) {
1615 err = -ENOMEM;
1616 goto failed;
1617 }
1618
Johan Hedberg2b76f452013-03-15 17:07:04 -05001619 hci_req_init(&req, hdev);
1620
Johan Hedberg9a43e252013-10-20 19:00:07 +03001621 /* If BR/EDR is not enabled and we disable advertising as a
1622 * by-product of disabling connectable, we need to update the
1623 * advertising flags.
1624 */
1625 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
1626 if (!cp->val) {
1627 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1628 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
1629 }
1630 update_adv_data(&req);
1631 } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) {
Johan Hedberg9b742462013-10-14 16:20:03 +03001632 if (cp->val) {
1633 scan = SCAN_PAGE;
1634 } else {
1635 scan = 0;
1636
1637 if (test_bit(HCI_ISCAN, &hdev->flags) &&
Marcel Holtmann8d6083f2013-10-14 16:38:45 -07001638 hdev->discov_timeout > 0)
Johan Hedberg9b742462013-10-14 16:20:03 +03001639 cancel_delayed_work(&hdev->discov_off);
1640 }
1641
1642 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1643 }
Johan Hedberg2b76f452013-03-15 17:07:04 -05001644
Johan Hedberg4c01f8b2013-03-15 17:07:14 -05001645 /* If we're going from non-connectable to connectable or
1646 * vice-versa when fast connectable is enabled ensure that fast
1647 * connectable gets disabled. write_fast_connectable won't do
1648 * anything if the page scan parameters are already what they
1649 * should be.
1650 */
1651 if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags))
Johan Hedberge36a3762013-03-15 17:07:10 -05001652 write_fast_connectable(&req, false);
1653
Johan Hedberg1987fdc2013-10-14 21:15:24 +03001654 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) &&
1655 hci_conn_num(hdev, LE_LINK) == 0) {
1656 disable_advertising(&req);
1657 enable_advertising(&req);
1658 }
1659
Johan Hedberg2b76f452013-03-15 17:07:04 -05001660 err = hci_req_run(&req, set_connectable_complete);
Johan Hedberg9b742462013-10-14 16:20:03 +03001661 if (err < 0) {
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001662 mgmt_pending_remove(cmd);
Johan Hedberg9b742462013-10-14 16:20:03 +03001663 if (err == -ENODATA)
Johan Hedberga81070b2013-10-19 23:38:19 +03001664 err = set_connectable_update_settings(hdev, sk,
1665 cp->val);
Johan Hedberg9b742462013-10-14 16:20:03 +03001666 goto failed;
1667 }
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001668
1669failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001670 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001671 return err;
1672}
1673
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001674static int set_pairable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001675 u16 len)
Johan Hedberg73f22f62010-12-29 16:00:25 +02001676{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001677 struct mgmt_mode *cp = data;
Marcel Holtmann55594352013-10-06 16:11:57 -07001678 bool changed;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001679 int err;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001680
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001681 BT_DBG("request for %s", hdev->name);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001682
Johan Hedberga7e80f22013-01-09 16:05:19 +02001683 if (cp->val != 0x00 && cp->val != 0x01)
1684 return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE,
1685 MGMT_STATUS_INVALID_PARAMS);
1686
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001687 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001688
1689 if (cp->val)
Marcel Holtmann55594352013-10-06 16:11:57 -07001690 changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001691 else
Marcel Holtmann55594352013-10-06 16:11:57 -07001692 changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001693
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001694 err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001695 if (err < 0)
Marcel Holtmann55594352013-10-06 16:11:57 -07001696 goto unlock;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001697
Marcel Holtmann55594352013-10-06 16:11:57 -07001698 if (changed)
1699 err = new_settings(hdev, sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001700
Marcel Holtmann55594352013-10-06 16:11:57 -07001701unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001702 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001703 return err;
1704}
Johan Hedberg72a734e2010-12-30 00:38:22 +02001705
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001706static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
1707 u16 len)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001708{
1709 struct mgmt_mode *cp = data;
1710 struct pending_cmd *cmd;
Johan Hedberge6fe7982013-10-02 15:45:22 +03001711 u8 val, status;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001712 int err;
1713
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001714 BT_DBG("request for %s", hdev->name);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001715
Johan Hedberge6fe7982013-10-02 15:45:22 +03001716 status = mgmt_bredr_support(hdev);
1717 if (status)
Johan Hedberg33c525c2012-10-24 21:11:58 +03001718 return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
Johan Hedberge6fe7982013-10-02 15:45:22 +03001719 status);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001720
Johan Hedberga7e80f22013-01-09 16:05:19 +02001721 if (cp->val != 0x00 && cp->val != 0x01)
1722 return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1723 MGMT_STATUS_INVALID_PARAMS);
1724
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001725 hci_dev_lock(hdev);
1726
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001727 if (!hdev_is_powered(hdev)) {
Johan Hedberg47990ea2012-02-22 11:58:37 +02001728 bool changed = false;
1729
1730 if (!!cp->val != test_bit(HCI_LINK_SECURITY,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03001731 &hdev->dev_flags)) {
Johan Hedberg47990ea2012-02-22 11:58:37 +02001732 change_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
1733 changed = true;
1734 }
1735
1736 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1737 if (err < 0)
1738 goto failed;
1739
1740 if (changed)
1741 err = new_settings(hdev, sk);
1742
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001743 goto failed;
1744 }
1745
1746 if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001747 err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001748 MGMT_STATUS_BUSY);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001749 goto failed;
1750 }
1751
1752 val = !!cp->val;
1753
1754 if (test_bit(HCI_AUTH, &hdev->flags) == val) {
1755 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1756 goto failed;
1757 }
1758
1759 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
1760 if (!cmd) {
1761 err = -ENOMEM;
1762 goto failed;
1763 }
1764
1765 err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
1766 if (err < 0) {
1767 mgmt_pending_remove(cmd);
1768 goto failed;
1769 }
1770
1771failed:
1772 hci_dev_unlock(hdev);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001773 return err;
1774}
1775
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001776static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001777{
1778 struct mgmt_mode *cp = data;
1779 struct pending_cmd *cmd;
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07001780 u8 status;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001781 int err;
1782
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001783 BT_DBG("request for %s", hdev->name);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001784
Marcel Holtmanncdba5282013-10-02 21:31:52 -07001785 status = mgmt_bredr_support(hdev);
1786 if (status)
1787 return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
1788
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001789 if (!lmp_ssp_capable(hdev))
1790 return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1791 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001792
Johan Hedberga7e80f22013-01-09 16:05:19 +02001793 if (cp->val != 0x00 && cp->val != 0x01)
1794 return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1795 MGMT_STATUS_INVALID_PARAMS);
1796
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001797 hci_dev_lock(hdev);
Johan Hedberg6c8f12c2012-02-22 16:35:26 +02001798
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001799 if (!hdev_is_powered(hdev)) {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001800 bool changed;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02001801
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001802 if (cp->val) {
1803 changed = !test_and_set_bit(HCI_SSP_ENABLED,
1804 &hdev->dev_flags);
1805 } else {
1806 changed = test_and_clear_bit(HCI_SSP_ENABLED,
1807 &hdev->dev_flags);
1808 if (!changed)
1809 changed = test_and_clear_bit(HCI_HS_ENABLED,
1810 &hdev->dev_flags);
1811 else
1812 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02001813 }
1814
1815 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
1816 if (err < 0)
1817 goto failed;
1818
1819 if (changed)
1820 err = new_settings(hdev, sk);
1821
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001822 goto failed;
1823 }
1824
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001825 if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) ||
1826 mgmt_pending_find(MGMT_OP_SET_HS, hdev)) {
Szymon Jancd97dcb62012-03-16 16:02:56 +01001827 err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1828 MGMT_STATUS_BUSY);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001829 goto failed;
1830 }
1831
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07001832 if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001833 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
1834 goto failed;
1835 }
1836
1837 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
1838 if (!cmd) {
1839 err = -ENOMEM;
1840 goto failed;
1841 }
1842
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07001843 err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001844 if (err < 0) {
1845 mgmt_pending_remove(cmd);
1846 goto failed;
1847 }
1848
1849failed:
1850 hci_dev_unlock(hdev);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001851 return err;
1852}
1853
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001854static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001855{
1856 struct mgmt_mode *cp = data;
Marcel Holtmannee392692013-10-01 22:59:23 -07001857 bool changed;
Johan Hedberge6fe7982013-10-02 15:45:22 +03001858 u8 status;
Marcel Holtmannee392692013-10-01 22:59:23 -07001859 int err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001860
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001861 BT_DBG("request for %s", hdev->name);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001862
Johan Hedberge6fe7982013-10-02 15:45:22 +03001863 status = mgmt_bredr_support(hdev);
1864 if (status)
1865 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001866
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001867 if (!lmp_ssp_capable(hdev))
1868 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1869 MGMT_STATUS_NOT_SUPPORTED);
1870
1871 if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
1872 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1873 MGMT_STATUS_REJECTED);
1874
Johan Hedberga7e80f22013-01-09 16:05:19 +02001875 if (cp->val != 0x00 && cp->val != 0x01)
1876 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1877 MGMT_STATUS_INVALID_PARAMS);
1878
Marcel Holtmannee392692013-10-01 22:59:23 -07001879 hci_dev_lock(hdev);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001880
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001881 if (cp->val) {
Marcel Holtmannee392692013-10-01 22:59:23 -07001882 changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001883 } else {
1884 if (hdev_is_powered(hdev)) {
1885 err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1886 MGMT_STATUS_REJECTED);
1887 goto unlock;
1888 }
1889
Marcel Holtmannee392692013-10-01 22:59:23 -07001890 changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001891 }
Marcel Holtmannee392692013-10-01 22:59:23 -07001892
1893 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
1894 if (err < 0)
1895 goto unlock;
1896
1897 if (changed)
1898 err = new_settings(hdev, sk);
1899
1900unlock:
1901 hci_dev_unlock(hdev);
1902 return err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001903}
1904
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001905static void le_enable_complete(struct hci_dev *hdev, u8 status)
1906{
1907 struct cmd_lookup match = { NULL, hdev };
1908
1909 if (status) {
1910 u8 mgmt_err = mgmt_status(status);
1911
1912 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
1913 &mgmt_err);
1914 return;
1915 }
1916
1917 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
1918
1919 new_settings(hdev, match.sk);
1920
1921 if (match.sk)
1922 sock_put(match.sk);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001923
1924 /* Make sure the controller has a good default for
1925 * advertising data. Restrict the update to when LE
1926 * has actually been enabled. During power on, the
1927 * update in powered_update_hci will take care of it.
1928 */
1929 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
1930 struct hci_request req;
1931
1932 hci_dev_lock(hdev);
1933
1934 hci_req_init(&req, hdev);
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07001935 update_adv_data(&req);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07001936 update_scan_rsp_data(&req);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001937 hci_req_run(&req, NULL);
1938
1939 hci_dev_unlock(hdev);
1940 }
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001941}
1942
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001943static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg06199cf2012-02-22 16:37:11 +02001944{
1945 struct mgmt_mode *cp = data;
1946 struct hci_cp_write_le_host_supported hci_cp;
1947 struct pending_cmd *cmd;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001948 struct hci_request req;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001949 int err;
Johan Hedberg0b60eba2012-02-28 00:57:24 +02001950 u8 val, enabled;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001951
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001952 BT_DBG("request for %s", hdev->name);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001953
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001954 if (!lmp_le_capable(hdev))
1955 return cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1956 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg1de028c2012-02-29 19:55:35 -08001957
Johan Hedberga7e80f22013-01-09 16:05:19 +02001958 if (cp->val != 0x00 && cp->val != 0x01)
1959 return cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1960 MGMT_STATUS_INVALID_PARAMS);
1961
Johan Hedbergc73eee92013-04-19 18:35:21 +03001962 /* LE-only devices do not allow toggling LE on/off */
Johan Hedberg56f87902013-10-02 13:43:13 +03001963 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
Johan Hedbergc73eee92013-04-19 18:35:21 +03001964 return cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1965 MGMT_STATUS_REJECTED);
1966
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001967 hci_dev_lock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001968
1969 val = !!cp->val;
Gustavo Padovanffa88e02012-11-23 16:50:51 -02001970 enabled = lmp_host_le_capable(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001971
Johan Hedberg0b60eba2012-02-28 00:57:24 +02001972 if (!hdev_is_powered(hdev) || val == enabled) {
Johan Hedberg06199cf2012-02-22 16:37:11 +02001973 bool changed = false;
1974
1975 if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
1976 change_bit(HCI_LE_ENABLED, &hdev->dev_flags);
1977 changed = true;
1978 }
1979
Johan Hedbergf3d3444a2013-10-05 12:01:04 +02001980 if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
1981 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03001982 changed = true;
1983 }
1984
Johan Hedberg06199cf2012-02-22 16:37:11 +02001985 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
1986 if (err < 0)
Johan Hedberg1de028c2012-02-29 19:55:35 -08001987 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001988
1989 if (changed)
1990 err = new_settings(hdev, sk);
1991
Johan Hedberg1de028c2012-02-29 19:55:35 -08001992 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001993 }
1994
Johan Hedberg4375f102013-09-25 13:26:10 +03001995 if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) ||
1996 mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001997 err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001998 MGMT_STATUS_BUSY);
Johan Hedberg1de028c2012-02-29 19:55:35 -08001999 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002000 }
2001
2002 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
2003 if (!cmd) {
2004 err = -ENOMEM;
Johan Hedberg1de028c2012-02-29 19:55:35 -08002005 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002006 }
2007
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002008 hci_req_init(&req, hdev);
2009
Johan Hedberg06199cf2012-02-22 16:37:11 +02002010 memset(&hci_cp, 0, sizeof(hci_cp));
2011
2012 if (val) {
2013 hci_cp.le = val;
Gustavo Padovanffa88e02012-11-23 16:50:51 -02002014 hci_cp.simul = lmp_le_br_capable(hdev);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002015 } else {
2016 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
2017 disable_advertising(&req);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002018 }
2019
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002020 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp),
2021 &hci_cp);
2022
2023 err = hci_req_run(&req, le_enable_complete);
Syam Sidhardhan0c01bc42012-04-12 20:33:21 +05302024 if (err < 0)
Johan Hedberg06199cf2012-02-22 16:37:11 +02002025 mgmt_pending_remove(cmd);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002026
Johan Hedberg1de028c2012-02-29 19:55:35 -08002027unlock:
2028 hci_dev_unlock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002029 return err;
2030}
2031
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002032/* This is a helper function to test for pending mgmt commands that can
2033 * cause CoD or EIR HCI commands. We can only allow one such pending
2034 * mgmt command at a time since otherwise we cannot easily track what
2035 * the current values are, will be, and based on that calculate if a new
2036 * HCI command needs to be sent and if yes with what value.
2037 */
2038static bool pending_eir_or_class(struct hci_dev *hdev)
2039{
2040 struct pending_cmd *cmd;
2041
2042 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
2043 switch (cmd->opcode) {
2044 case MGMT_OP_ADD_UUID:
2045 case MGMT_OP_REMOVE_UUID:
2046 case MGMT_OP_SET_DEV_CLASS:
2047 case MGMT_OP_SET_POWERED:
2048 return true;
2049 }
2050 }
2051
2052 return false;
2053}
2054
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002055static const u8 bluetooth_base_uuid[] = {
2056 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
2057 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2058};
2059
2060static u8 get_uuid_size(const u8 *uuid)
2061{
2062 u32 val;
2063
2064 if (memcmp(uuid, bluetooth_base_uuid, 12))
2065 return 128;
2066
2067 val = get_unaligned_le32(&uuid[12]);
2068 if (val > 0xffff)
2069 return 32;
2070
2071 return 16;
2072}
2073
Johan Hedberg92da6092013-03-15 17:06:55 -05002074static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status)
2075{
2076 struct pending_cmd *cmd;
2077
2078 hci_dev_lock(hdev);
2079
2080 cmd = mgmt_pending_find(mgmt_op, hdev);
2081 if (!cmd)
2082 goto unlock;
2083
2084 cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status),
2085 hdev->dev_class, 3);
2086
2087 mgmt_pending_remove(cmd);
2088
2089unlock:
2090 hci_dev_unlock(hdev);
2091}
2092
2093static void add_uuid_complete(struct hci_dev *hdev, u8 status)
2094{
2095 BT_DBG("status 0x%02x", status);
2096
2097 mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status);
2098}
2099
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002100static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002101{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002102 struct mgmt_cp_add_uuid *cp = data;
Johan Hedberg90e70452012-02-23 23:09:40 +02002103 struct pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002104 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002105 struct bt_uuid *uuid;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002106 int err;
2107
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002108 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002109
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002110 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002111
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002112 if (pending_eir_or_class(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002113 err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002114 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002115 goto failed;
2116 }
2117
Andre Guedes92c4c202012-06-07 19:05:44 -03002118 uuid = kmalloc(sizeof(*uuid), GFP_KERNEL);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002119 if (!uuid) {
2120 err = -ENOMEM;
2121 goto failed;
2122 }
2123
2124 memcpy(uuid->uuid, cp->uuid, 16);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002125 uuid->svc_hint = cp->svc_hint;
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002126 uuid->size = get_uuid_size(cp->uuid);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002127
Johan Hedbergde66aa62013-01-27 00:31:27 +02002128 list_add_tail(&uuid->list, &hdev->uuids);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002129
Johan Hedberg890ea892013-03-15 17:06:52 -05002130 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002131
Johan Hedberg890ea892013-03-15 17:06:52 -05002132 update_class(&req);
2133 update_eir(&req);
2134
Johan Hedberg92da6092013-03-15 17:06:55 -05002135 err = hci_req_run(&req, add_uuid_complete);
2136 if (err < 0) {
2137 if (err != -ENODATA)
2138 goto failed;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002139
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002140 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002141 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002142 goto failed;
2143 }
2144
2145 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002146 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002147 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002148 goto failed;
2149 }
2150
2151 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002152
2153failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002154 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002155 return err;
2156}
2157
Johan Hedberg24b78d02012-02-23 23:24:30 +02002158static bool enable_service_cache(struct hci_dev *hdev)
2159{
2160 if (!hdev_is_powered(hdev))
2161 return false;
2162
2163 if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) {
Johan Hedberg46818ed2013-01-14 22:33:52 +02002164 queue_delayed_work(hdev->workqueue, &hdev->service_cache,
2165 CACHE_TIMEOUT);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002166 return true;
2167 }
2168
2169 return false;
2170}
2171
Johan Hedberg92da6092013-03-15 17:06:55 -05002172static void remove_uuid_complete(struct hci_dev *hdev, u8 status)
2173{
2174 BT_DBG("status 0x%02x", status);
2175
2176 mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status);
2177}
2178
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002179static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002180 u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002181{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002182 struct mgmt_cp_remove_uuid *cp = data;
Johan Hedberg90e70452012-02-23 23:09:40 +02002183 struct pending_cmd *cmd;
Johan Hedberg056341c2013-01-27 00:31:30 +02002184 struct bt_uuid *match, *tmp;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002185 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 -05002186 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002187 int err, found;
2188
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002189 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002190
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002191 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002192
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002193 if (pending_eir_or_class(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002194 err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002195 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002196 goto unlock;
2197 }
2198
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002199 if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
Johan Hedberg35f74982014-02-18 17:14:32 +02002200 hci_uuids_clear(hdev);
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002201
Johan Hedberg24b78d02012-02-23 23:24:30 +02002202 if (enable_service_cache(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002203 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002204 0, hdev->dev_class, 3);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002205 goto unlock;
2206 }
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002207
Johan Hedberg9246a862012-02-23 21:33:16 +02002208 goto update_class;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002209 }
2210
2211 found = 0;
2212
Johan Hedberg056341c2013-01-27 00:31:30 +02002213 list_for_each_entry_safe(match, tmp, &hdev->uuids, list) {
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002214 if (memcmp(match->uuid, cp->uuid, 16) != 0)
2215 continue;
2216
2217 list_del(&match->list);
Johan Hedberg482049f2012-11-08 10:25:26 +01002218 kfree(match);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002219 found++;
2220 }
2221
2222 if (found == 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002223 err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002224 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002225 goto unlock;
2226 }
2227
Johan Hedberg9246a862012-02-23 21:33:16 +02002228update_class:
Johan Hedberg890ea892013-03-15 17:06:52 -05002229 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002230
Johan Hedberg890ea892013-03-15 17:06:52 -05002231 update_class(&req);
2232 update_eir(&req);
2233
Johan Hedberg92da6092013-03-15 17:06:55 -05002234 err = hci_req_run(&req, remove_uuid_complete);
2235 if (err < 0) {
2236 if (err != -ENODATA)
2237 goto unlock;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002238
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002239 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002240 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002241 goto unlock;
2242 }
2243
2244 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002245 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002246 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002247 goto unlock;
2248 }
2249
2250 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002251
2252unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002253 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002254 return err;
2255}
2256
Johan Hedberg92da6092013-03-15 17:06:55 -05002257static void set_class_complete(struct hci_dev *hdev, u8 status)
2258{
2259 BT_DBG("status 0x%02x", status);
2260
2261 mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status);
2262}
2263
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002264static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002265 u16 len)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002266{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002267 struct mgmt_cp_set_dev_class *cp = data;
Johan Hedberg90e70452012-02-23 23:09:40 +02002268 struct pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002269 struct hci_request req;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002270 int err;
2271
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002272 BT_DBG("request for %s", hdev->name);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002273
Marcel Holtmann6203fc92013-10-02 23:37:29 -07002274 if (!lmp_bredr_capable(hdev))
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002275 return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2276 MGMT_STATUS_NOT_SUPPORTED);
2277
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002278 hci_dev_lock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002279
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002280 if (pending_eir_or_class(hdev)) {
2281 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2282 MGMT_STATUS_BUSY);
2283 goto unlock;
2284 }
2285
2286 if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
2287 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2288 MGMT_STATUS_INVALID_PARAMS);
2289 goto unlock;
2290 }
2291
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002292 hdev->major_class = cp->major;
2293 hdev->minor_class = cp->minor;
2294
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002295 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002296 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002297 hdev->dev_class, 3);
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002298 goto unlock;
2299 }
2300
Johan Hedberg890ea892013-03-15 17:06:52 -05002301 hci_req_init(&req, hdev);
2302
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02002303 if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) {
Johan Hedberg7d785252011-12-15 00:47:39 +02002304 hci_dev_unlock(hdev);
2305 cancel_delayed_work_sync(&hdev->service_cache);
2306 hci_dev_lock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -05002307 update_eir(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02002308 }
Johan Hedberg14c0b602011-12-15 00:47:37 +02002309
Johan Hedberg890ea892013-03-15 17:06:52 -05002310 update_class(&req);
2311
Johan Hedberg92da6092013-03-15 17:06:55 -05002312 err = hci_req_run(&req, set_class_complete);
2313 if (err < 0) {
2314 if (err != -ENODATA)
2315 goto unlock;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002316
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002317 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002318 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002319 goto unlock;
2320 }
2321
2322 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002323 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002324 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002325 goto unlock;
2326 }
2327
2328 err = 0;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002329
Johan Hedbergb5235a62012-02-21 14:32:24 +02002330unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002331 hci_dev_unlock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002332 return err;
2333}
2334
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002335static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002336 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002337{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002338 struct mgmt_cp_load_link_keys *cp = data;
Szymon Janc4e51eae2011-02-25 19:05:48 +01002339 u16 key_count, expected_len;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002340 bool changed;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002341 int i;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002342
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002343 BT_DBG("request for %s", hdev->name);
2344
2345 if (!lmp_bredr_capable(hdev))
2346 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2347 MGMT_STATUS_NOT_SUPPORTED);
2348
Marcel Holtmann1f350c82012-03-12 20:31:08 -07002349 key_count = __le16_to_cpu(cp->key_count);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002350
Johan Hedberg86742e12011-11-07 23:13:38 +02002351 expected_len = sizeof(*cp) + key_count *
2352 sizeof(struct mgmt_link_key_info);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002353 if (expected_len != len) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002354 BT_ERR("load_link_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02002355 expected_len, len);
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002356 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002357 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002358 }
2359
Johan Hedberg4ae14302013-01-20 14:27:13 +02002360 if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01)
2361 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2362 MGMT_STATUS_INVALID_PARAMS);
2363
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002364 BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002365 key_count);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002366
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002367 for (i = 0; i < key_count; i++) {
2368 struct mgmt_link_key_info *key = &cp->keys[i];
2369
Marcel Holtmann8e991132014-01-10 02:07:25 -08002370 if (key->addr.type != BDADDR_BREDR || key->type > 0x08)
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002371 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2372 MGMT_STATUS_INVALID_PARAMS);
2373 }
2374
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002375 hci_dev_lock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002376
2377 hci_link_keys_clear(hdev);
2378
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002379 if (cp->debug_keys)
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002380 changed = !test_and_set_bit(HCI_DEBUG_KEYS, &hdev->dev_flags);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002381 else
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002382 changed = test_and_clear_bit(HCI_DEBUG_KEYS, &hdev->dev_flags);
2383
2384 if (changed)
2385 new_settings(hdev, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002386
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002387 for (i = 0; i < key_count; i++) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002388 struct mgmt_link_key_info *key = &cp->keys[i];
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002389
Johan Hedbergd753fdc2012-02-17 14:06:34 +02002390 hci_add_link_key(hdev, NULL, 0, &key->addr.bdaddr, key->val,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002391 key->type, key->pin_len);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002392 }
2393
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002394 cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
Johan Hedberg0e5f8752011-11-11 16:18:54 +02002395
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002396 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002397
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002398 return 0;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002399}
2400
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002401static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002402 u8 addr_type, struct sock *skip_sk)
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002403{
2404 struct mgmt_ev_device_unpaired ev;
2405
2406 bacpy(&ev.addr.bdaddr, bdaddr);
2407 ev.addr.type = addr_type;
2408
2409 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002410 skip_sk);
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002411}
2412
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002413static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002414 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002415{
Johan Hedberg124f6e32012-02-09 13:50:12 +02002416 struct mgmt_cp_unpair_device *cp = data;
2417 struct mgmt_rp_unpair_device rp;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002418 struct hci_cp_disconnect dc;
2419 struct pending_cmd *cmd;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002420 struct hci_conn *conn;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002421 int err;
2422
Johan Hedberga8a1d192011-11-10 15:54:38 +02002423 memset(&rp, 0, sizeof(rp));
Johan Hedberg124f6e32012-02-09 13:50:12 +02002424 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2425 rp.addr.type = cp->addr.type;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002426
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002427 if (!bdaddr_type_is_valid(cp->addr.type))
2428 return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2429 MGMT_STATUS_INVALID_PARAMS,
2430 &rp, sizeof(rp));
2431
Johan Hedberg118da702013-01-20 14:27:20 +02002432 if (cp->disconnect != 0x00 && cp->disconnect != 0x01)
2433 return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2434 MGMT_STATUS_INVALID_PARAMS,
2435 &rp, sizeof(rp));
2436
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002437 hci_dev_lock(hdev);
2438
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002439 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002440 err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002441 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp));
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002442 goto unlock;
2443 }
2444
Johan Hedberge0b2b272014-02-18 17:14:31 +02002445 if (cp->addr.type == BDADDR_BREDR) {
Johan Hedberg124f6e32012-02-09 13:50:12 +02002446 err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
Johan Hedberge0b2b272014-02-18 17:14:31 +02002447 } else {
2448 u8 addr_type;
2449
2450 if (cp->addr.type == BDADDR_LE_PUBLIC)
2451 addr_type = ADDR_LE_DEV_PUBLIC;
2452 else
2453 addr_type = ADDR_LE_DEV_RANDOM;
2454
Johan Hedberga7ec7332014-02-18 17:14:35 +02002455 hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type);
2456
Andre Guedesa9b0a042014-02-26 20:21:52 -03002457 hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type);
2458
Johan Hedberge0b2b272014-02-18 17:14:31 +02002459 err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type);
2460 }
Vinicius Costa Gomesb0dbfb42012-02-02 21:08:03 -03002461
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002462 if (err < 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002463 err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002464 MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002465 goto unlock;
2466 }
2467
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002468 if (cp->disconnect) {
Andre Guedes591f47f2012-04-24 21:02:49 -03002469 if (cp->addr.type == BDADDR_BREDR)
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002470 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002471 &cp->addr.bdaddr);
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002472 else
2473 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002474 &cp->addr.bdaddr);
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002475 } else {
2476 conn = NULL;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002477 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002478
Johan Hedberga8a1d192011-11-10 15:54:38 +02002479 if (!conn) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002480 err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002481 &rp, sizeof(rp));
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002482 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002483 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002484 }
2485
Johan Hedberg124f6e32012-02-09 13:50:12 +02002486 cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002487 sizeof(*cp));
Johan Hedberga8a1d192011-11-10 15:54:38 +02002488 if (!cmd) {
2489 err = -ENOMEM;
2490 goto unlock;
2491 }
2492
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02002493 dc.handle = cpu_to_le16(conn->handle);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002494 dc.reason = 0x13; /* Remote User Terminated Connection */
2495 err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc);
2496 if (err < 0)
2497 mgmt_pending_remove(cmd);
2498
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002499unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002500 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002501 return err;
2502}
2503
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002504static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002505 u16 len)
Johan Hedberg8962ee72011-01-20 12:40:27 +02002506{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002507 struct mgmt_cp_disconnect *cp = data;
Johan Hedberg06a63b12013-01-20 14:27:21 +02002508 struct mgmt_rp_disconnect rp;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002509 struct hci_cp_disconnect dc;
Johan Hedberg366a0332011-02-19 12:05:55 -03002510 struct pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002511 struct hci_conn *conn;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002512 int err;
2513
2514 BT_DBG("");
2515
Johan Hedberg06a63b12013-01-20 14:27:21 +02002516 memset(&rp, 0, sizeof(rp));
2517 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2518 rp.addr.type = cp->addr.type;
2519
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002520 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg06a63b12013-01-20 14:27:21 +02002521 return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2522 MGMT_STATUS_INVALID_PARAMS,
2523 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002524
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002525 hci_dev_lock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002526
2527 if (!test_bit(HCI_UP, &hdev->flags)) {
Johan Hedberg06a63b12013-01-20 14:27:21 +02002528 err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2529 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002530 goto failed;
2531 }
2532
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002533 if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) {
Johan Hedberg06a63b12013-01-20 14:27:21 +02002534 err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2535 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002536 goto failed;
2537 }
2538
Andre Guedes591f47f2012-04-24 21:02:49 -03002539 if (cp->addr.type == BDADDR_BREDR)
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03002540 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2541 &cp->addr.bdaddr);
Johan Hedberg88c3df12012-02-09 14:27:38 +02002542 else
2543 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
Vinicius Costa Gomes365227e2011-05-06 18:41:44 -03002544
Vishal Agarwalf9607272012-06-13 05:32:43 +05302545 if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) {
Johan Hedberg06a63b12013-01-20 14:27:21 +02002546 err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2547 MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002548 goto failed;
2549 }
2550
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002551 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03002552 if (!cmd) {
2553 err = -ENOMEM;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002554 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03002555 }
Johan Hedberg8962ee72011-01-20 12:40:27 +02002556
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02002557 dc.handle = cpu_to_le16(conn->handle);
Andre Guedes3701f942012-06-11 18:41:12 -03002558 dc.reason = HCI_ERROR_REMOTE_USER_TERM;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002559
2560 err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc);
2561 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03002562 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002563
2564failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002565 hci_dev_unlock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002566 return err;
2567}
2568
Andre Guedes57c14772012-04-24 21:02:50 -03002569static u8 link_to_bdaddr(u8 link_type, u8 addr_type)
Johan Hedberg4c659c32011-11-07 23:13:39 +02002570{
2571 switch (link_type) {
2572 case LE_LINK:
Johan Hedberg48264f02011-11-09 13:58:58 +02002573 switch (addr_type) {
2574 case ADDR_LE_DEV_PUBLIC:
Andre Guedes591f47f2012-04-24 21:02:49 -03002575 return BDADDR_LE_PUBLIC;
Andre Guedes0ed09142012-04-03 08:46:54 -03002576
Johan Hedberg48264f02011-11-09 13:58:58 +02002577 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03002578 /* Fallback to LE Random address type */
Andre Guedes591f47f2012-04-24 21:02:49 -03002579 return BDADDR_LE_RANDOM;
Johan Hedberg48264f02011-11-09 13:58:58 +02002580 }
Andre Guedes0ed09142012-04-03 08:46:54 -03002581
Johan Hedberg4c659c32011-11-07 23:13:39 +02002582 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03002583 /* Fallback to BR/EDR type */
Andre Guedes591f47f2012-04-24 21:02:49 -03002584 return BDADDR_BREDR;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002585 }
2586}
2587
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002588static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
2589 u16 data_len)
Johan Hedberg2784eb42011-01-21 13:56:35 +02002590{
Johan Hedberg2784eb42011-01-21 13:56:35 +02002591 struct mgmt_rp_get_connections *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002592 struct hci_conn *c;
Johan Hedberga38528f2011-01-22 06:46:43 +02002593 size_t rp_len;
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002594 int err;
2595 u16 i;
Johan Hedberg2784eb42011-01-21 13:56:35 +02002596
2597 BT_DBG("");
2598
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002599 hci_dev_lock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002600
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002601 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002602 err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002603 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002604 goto unlock;
2605 }
2606
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002607 i = 0;
Johan Hedbergb644ba32012-01-17 21:48:47 +02002608 list_for_each_entry(c, &hdev->conn_hash.list, list) {
2609 if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002610 i++;
Johan Hedberg2784eb42011-01-21 13:56:35 +02002611 }
2612
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002613 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Andre Guedes92c4c202012-06-07 19:05:44 -03002614 rp = kmalloc(rp_len, GFP_KERNEL);
Johan Hedberga38528f2011-01-22 06:46:43 +02002615 if (!rp) {
Johan Hedberg2784eb42011-01-21 13:56:35 +02002616 err = -ENOMEM;
2617 goto unlock;
2618 }
2619
Johan Hedberg2784eb42011-01-21 13:56:35 +02002620 i = 0;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002621 list_for_each_entry(c, &hdev->conn_hash.list, list) {
Johan Hedbergb644ba32012-01-17 21:48:47 +02002622 if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
2623 continue;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002624 bacpy(&rp->addr[i].bdaddr, &c->dst);
Andre Guedes57c14772012-04-24 21:02:50 -03002625 rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type);
Andre Guedes0ed09142012-04-03 08:46:54 -03002626 if (c->type == SCO_LINK || c->type == ESCO_LINK)
Johan Hedberg4c659c32011-11-07 23:13:39 +02002627 continue;
2628 i++;
2629 }
2630
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02002631 rp->conn_count = cpu_to_le16(i);
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002632
Johan Hedberg4c659c32011-11-07 23:13:39 +02002633 /* Recalculate length in case of filtered SCO connections, etc */
2634 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Johan Hedberg2784eb42011-01-21 13:56:35 +02002635
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002636 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002637 rp_len);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002638
Johan Hedberga38528f2011-01-22 06:46:43 +02002639 kfree(rp);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002640
2641unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002642 hci_dev_unlock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002643 return err;
2644}
2645
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002646static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002647 struct mgmt_cp_pin_code_neg_reply *cp)
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002648{
2649 struct pending_cmd *cmd;
2650 int err;
2651
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002652 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002653 sizeof(*cp));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002654 if (!cmd)
2655 return -ENOMEM;
2656
Johan Hedbergd8457692012-02-17 14:24:57 +02002657 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002658 sizeof(cp->addr.bdaddr), &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002659 if (err < 0)
2660 mgmt_pending_remove(cmd);
2661
2662 return err;
2663}
2664
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002665static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002666 u16 len)
Johan Hedberg980e1a52011-01-22 06:10:07 +02002667{
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002668 struct hci_conn *conn;
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002669 struct mgmt_cp_pin_code_reply *cp = data;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002670 struct hci_cp_pin_code_reply reply;
Johan Hedberg366a0332011-02-19 12:05:55 -03002671 struct pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002672 int err;
2673
2674 BT_DBG("");
2675
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002676 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002677
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002678 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002679 err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002680 MGMT_STATUS_NOT_POWERED);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002681 goto failed;
2682 }
2683
Johan Hedbergd8457692012-02-17 14:24:57 +02002684 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002685 if (!conn) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002686 err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002687 MGMT_STATUS_NOT_CONNECTED);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002688 goto failed;
2689 }
2690
2691 if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) {
Johan Hedbergd8457692012-02-17 14:24:57 +02002692 struct mgmt_cp_pin_code_neg_reply ncp;
2693
2694 memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002695
2696 BT_ERR("PIN code is not 16 bytes long");
2697
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002698 err = send_pin_code_neg_reply(sk, hdev, &ncp);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002699 if (err >= 0)
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002700 err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002701 MGMT_STATUS_INVALID_PARAMS);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002702
2703 goto failed;
2704 }
2705
Gustavo F. Padovan00abfe42012-03-01 00:37:10 -03002706 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03002707 if (!cmd) {
2708 err = -ENOMEM;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002709 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03002710 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02002711
Johan Hedbergd8457692012-02-17 14:24:57 +02002712 bacpy(&reply.bdaddr, &cp->addr.bdaddr);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002713 reply.pin_len = cp->pin_len;
Waldemar Rymarkiewicz24718ca2011-06-01 17:28:47 +02002714 memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code));
Johan Hedberg980e1a52011-01-22 06:10:07 +02002715
2716 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
2717 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03002718 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002719
2720failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002721 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002722 return err;
2723}
2724
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002725static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
2726 u16 len)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002727{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002728 struct mgmt_cp_set_io_capability *cp = data;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002729
2730 BT_DBG("");
2731
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002732 hci_dev_lock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002733
2734 hdev->io_capability = cp->io_capability;
2735
2736 BT_DBG("%s IO capability set to 0x%02x", hdev->name,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002737 hdev->io_capability);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002738
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002739 hci_dev_unlock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002740
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002741 return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL,
2742 0);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002743}
2744
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002745static struct pending_cmd *find_pairing(struct hci_conn *conn)
Johan Hedberge9a416b2011-02-19 12:05:56 -03002746{
2747 struct hci_dev *hdev = conn->hdev;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002748 struct pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002749
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002750 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
Johan Hedberge9a416b2011-02-19 12:05:56 -03002751 if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
2752 continue;
2753
Johan Hedberge9a416b2011-02-19 12:05:56 -03002754 if (cmd->user_data != conn)
2755 continue;
2756
2757 return cmd;
2758 }
2759
2760 return NULL;
2761}
2762
2763static void pairing_complete(struct pending_cmd *cmd, u8 status)
2764{
2765 struct mgmt_rp_pair_device rp;
2766 struct hci_conn *conn = cmd->user_data;
2767
Johan Hedberg61b1a7f2014-03-20 12:54:16 +02002768 bacpy(&rp.addr.bdaddr, &conn->dst);
2769 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002770
Johan Hedbergaee9b212012-02-18 15:07:59 +02002771 cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002772 &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03002773
2774 /* So we don't get further callbacks for this connection */
2775 conn->connect_cfm_cb = NULL;
2776 conn->security_cfm_cb = NULL;
2777 conn->disconn_cfm_cb = NULL;
2778
David Herrmann76a68ba2013-04-06 20:28:37 +02002779 hci_conn_drop(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002780
Johan Hedberga664b5b2011-02-19 12:06:02 -03002781 mgmt_pending_remove(cmd);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002782}
2783
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002784void mgmt_smp_complete(struct hci_conn *conn, bool complete)
2785{
2786 u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED;
2787 struct pending_cmd *cmd;
2788
2789 cmd = find_pairing(conn);
2790 if (cmd)
2791 pairing_complete(cmd, status);
2792}
2793
Johan Hedberge9a416b2011-02-19 12:05:56 -03002794static void pairing_complete_cb(struct hci_conn *conn, u8 status)
2795{
2796 struct pending_cmd *cmd;
2797
2798 BT_DBG("status %u", status);
2799
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002800 cmd = find_pairing(conn);
2801 if (!cmd)
2802 BT_DBG("Unable to find a pending command");
2803 else
Johan Hedberge2113262012-02-18 15:20:03 +02002804 pairing_complete(cmd, mgmt_status(status));
Johan Hedberge9a416b2011-02-19 12:05:56 -03002805}
2806
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002807static void le_pairing_complete_cb(struct hci_conn *conn, u8 status)
Vishal Agarwal4c47d732012-06-07 20:27:35 +05302808{
2809 struct pending_cmd *cmd;
2810
2811 BT_DBG("status %u", status);
2812
2813 if (!status)
2814 return;
2815
2816 cmd = find_pairing(conn);
2817 if (!cmd)
2818 BT_DBG("Unable to find a pending command");
2819 else
2820 pairing_complete(cmd, mgmt_status(status));
2821}
2822
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002823static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002824 u16 len)
Johan Hedberge9a416b2011-02-19 12:05:56 -03002825{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002826 struct mgmt_cp_pair_device *cp = data;
Johan Hedberg1425acb2011-11-11 00:07:35 +02002827 struct mgmt_rp_pair_device rp;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002828 struct pending_cmd *cmd;
2829 u8 sec_level, auth_type;
2830 struct hci_conn *conn;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002831 int err;
2832
2833 BT_DBG("");
2834
Szymon Jancf950a30e2013-01-18 12:48:07 +01002835 memset(&rp, 0, sizeof(rp));
2836 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2837 rp.addr.type = cp->addr.type;
2838
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002839 if (!bdaddr_type_is_valid(cp->addr.type))
2840 return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2841 MGMT_STATUS_INVALID_PARAMS,
2842 &rp, sizeof(rp));
2843
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002844 hci_dev_lock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002845
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002846 if (!hdev_is_powered(hdev)) {
Szymon Jancf950a30e2013-01-18 12:48:07 +01002847 err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2848 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp));
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002849 goto unlock;
2850 }
2851
Vinicius Costa Gomesc908df32011-09-02 14:51:22 -03002852 sec_level = BT_SECURITY_MEDIUM;
Mikel Astiz6fd6b912014-04-08 14:21:32 +02002853 auth_type = HCI_AT_DEDICATED_BONDING;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002854
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002855 if (cp->addr.type == BDADDR_BREDR) {
Andre Guedes04a6c582014-02-26 20:21:44 -03002856 conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
2857 auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002858 } else {
2859 u8 addr_type;
2860
2861 /* Convert from L2CAP channel address type to HCI address type
2862 */
2863 if (cp->addr.type == BDADDR_LE_PUBLIC)
2864 addr_type = ADDR_LE_DEV_PUBLIC;
2865 else
2866 addr_type = ADDR_LE_DEV_RANDOM;
2867
2868 conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type,
Andre Guedes04a6c582014-02-26 20:21:44 -03002869 sec_level, auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002870 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03002871
Ville Tervo30e76272011-02-22 16:10:53 -03002872 if (IS_ERR(conn)) {
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02002873 int status;
2874
2875 if (PTR_ERR(conn) == -EBUSY)
2876 status = MGMT_STATUS_BUSY;
2877 else
2878 status = MGMT_STATUS_CONNECT_FAILED;
2879
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002880 err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02002881 status, &rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002882 sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03002883 goto unlock;
2884 }
2885
2886 if (conn->connect_cfm_cb) {
David Herrmann76a68ba2013-04-06 20:28:37 +02002887 hci_conn_drop(conn);
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002888 err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002889 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03002890 goto unlock;
2891 }
2892
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002893 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002894 if (!cmd) {
2895 err = -ENOMEM;
David Herrmann76a68ba2013-04-06 20:28:37 +02002896 hci_conn_drop(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002897 goto unlock;
2898 }
2899
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03002900 /* For LE, just connecting isn't a proof that the pairing finished */
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002901 if (cp->addr.type == BDADDR_BREDR) {
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03002902 conn->connect_cfm_cb = pairing_complete_cb;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002903 conn->security_cfm_cb = pairing_complete_cb;
2904 conn->disconn_cfm_cb = pairing_complete_cb;
2905 } else {
2906 conn->connect_cfm_cb = le_pairing_complete_cb;
2907 conn->security_cfm_cb = le_pairing_complete_cb;
2908 conn->disconn_cfm_cb = le_pairing_complete_cb;
2909 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03002910
Johan Hedberge9a416b2011-02-19 12:05:56 -03002911 conn->io_capability = cp->io_cap;
2912 cmd->user_data = conn;
2913
2914 if (conn->state == BT_CONNECTED &&
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002915 hci_conn_security(conn, sec_level, auth_type))
Johan Hedberge9a416b2011-02-19 12:05:56 -03002916 pairing_complete(cmd, 0);
2917
2918 err = 0;
2919
2920unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002921 hci_dev_unlock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002922 return err;
2923}
2924
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002925static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
2926 u16 len)
Johan Hedberg28424702012-02-02 04:02:29 +02002927{
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02002928 struct mgmt_addr_info *addr = data;
Johan Hedberg28424702012-02-02 04:02:29 +02002929 struct pending_cmd *cmd;
2930 struct hci_conn *conn;
2931 int err;
2932
2933 BT_DBG("");
2934
Johan Hedberg28424702012-02-02 04:02:29 +02002935 hci_dev_lock(hdev);
2936
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_CANCEL_PAIR_DEVICE,
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 Hedberg28424702012-02-02 04:02:29 +02002943 cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev);
2944 if (!cmd) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002945 err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002946 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02002947 goto unlock;
2948 }
2949
2950 conn = cmd->user_data;
2951
2952 if (bacmp(&addr->bdaddr, &conn->dst) != 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002953 err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002954 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02002955 goto unlock;
2956 }
2957
2958 pairing_complete(cmd, MGMT_STATUS_CANCELLED);
2959
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002960 err = cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002961 addr, sizeof(*addr));
Johan Hedberg28424702012-02-02 04:02:29 +02002962unlock:
2963 hci_dev_unlock(hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02002964 return err;
2965}
2966
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002967static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
Johan Hedberg1707c602013-03-15 17:07:15 -05002968 struct mgmt_addr_info *addr, u16 mgmt_op,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002969 u16 hci_op, __le32 passkey)
Johan Hedberga5c29682011-02-19 12:05:57 -03002970{
Johan Hedberga5c29682011-02-19 12:05:57 -03002971 struct pending_cmd *cmd;
Brian Gix0df4c182011-11-16 13:53:13 -08002972 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03002973 int err;
2974
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002975 hci_dev_lock(hdev);
Johan Hedberg08ba5382011-03-16 14:29:34 +02002976
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002977 if (!hdev_is_powered(hdev)) {
Johan Hedbergfeb94d32013-03-15 17:07:16 -05002978 err = cmd_complete(sk, hdev->id, mgmt_op,
2979 MGMT_STATUS_NOT_POWERED, addr,
2980 sizeof(*addr));
Brian Gix0df4c182011-11-16 13:53:13 -08002981 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03002982 }
2983
Johan Hedberg1707c602013-03-15 17:07:15 -05002984 if (addr->type == BDADDR_BREDR)
2985 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr);
Johan Hedberg272d90d2012-02-09 15:26:12 +02002986 else
Johan Hedberg1707c602013-03-15 17:07:15 -05002987 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr);
Brian Gix47c15e22011-11-16 13:53:14 -08002988
Johan Hedberg272d90d2012-02-09 15:26:12 +02002989 if (!conn) {
Johan Hedbergfeb94d32013-03-15 17:07:16 -05002990 err = cmd_complete(sk, hdev->id, mgmt_op,
2991 MGMT_STATUS_NOT_CONNECTED, addr,
2992 sizeof(*addr));
Johan Hedberg272d90d2012-02-09 15:26:12 +02002993 goto done;
2994 }
2995
Johan Hedberg1707c602013-03-15 17:07:15 -05002996 if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
Brian Gix47c15e22011-11-16 13:53:14 -08002997 /* Continue with pairing via SMP */
Brian Gix5fe57d92011-12-21 16:12:13 -08002998 err = smp_user_confirm_reply(conn, mgmt_op, passkey);
Brian Gix47c15e22011-11-16 13:53:14 -08002999
Brian Gix5fe57d92011-12-21 16:12:13 -08003000 if (!err)
Johan Hedbergfeb94d32013-03-15 17:07:16 -05003001 err = cmd_complete(sk, hdev->id, mgmt_op,
3002 MGMT_STATUS_SUCCESS, addr,
3003 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003004 else
Johan Hedbergfeb94d32013-03-15 17:07:16 -05003005 err = cmd_complete(sk, hdev->id, mgmt_op,
3006 MGMT_STATUS_FAILED, addr,
3007 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003008
Brian Gix47c15e22011-11-16 13:53:14 -08003009 goto done;
3010 }
3011
Johan Hedberg1707c602013-03-15 17:07:15 -05003012 cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr));
Johan Hedberga5c29682011-02-19 12:05:57 -03003013 if (!cmd) {
3014 err = -ENOMEM;
Brian Gix0df4c182011-11-16 13:53:13 -08003015 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003016 }
3017
Brian Gix0df4c182011-11-16 13:53:13 -08003018 /* Continue with pairing via HCI */
Brian Gix604086b2011-11-23 08:28:33 -08003019 if (hci_op == HCI_OP_USER_PASSKEY_REPLY) {
3020 struct hci_cp_user_passkey_reply cp;
3021
Johan Hedberg1707c602013-03-15 17:07:15 -05003022 bacpy(&cp.bdaddr, &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003023 cp.passkey = passkey;
3024 err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
3025 } else
Johan Hedberg1707c602013-03-15 17:07:15 -05003026 err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr),
3027 &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003028
Johan Hedberga664b5b2011-02-19 12:06:02 -03003029 if (err < 0)
3030 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03003031
Brian Gix0df4c182011-11-16 13:53:13 -08003032done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003033 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03003034 return err;
3035}
3036
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303037static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
3038 void *data, u16 len)
3039{
3040 struct mgmt_cp_pin_code_neg_reply *cp = data;
3041
3042 BT_DBG("");
3043
Johan Hedberg1707c602013-03-15 17:07:15 -05003044 return user_pairing_resp(sk, hdev, &cp->addr,
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303045 MGMT_OP_PIN_CODE_NEG_REPLY,
3046 HCI_OP_PIN_CODE_NEG_REPLY, 0);
3047}
3048
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003049static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3050 u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003051{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003052 struct mgmt_cp_user_confirm_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003053
3054 BT_DBG("");
3055
3056 if (len != sizeof(*cp))
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003057 return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003058 MGMT_STATUS_INVALID_PARAMS);
Brian Gix0df4c182011-11-16 13:53:13 -08003059
Johan Hedberg1707c602013-03-15 17:07:15 -05003060 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003061 MGMT_OP_USER_CONFIRM_REPLY,
3062 HCI_OP_USER_CONFIRM_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003063}
3064
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003065static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003066 void *data, u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003067{
Johan Hedbergc9c26592011-12-15 00:47:41 +02003068 struct mgmt_cp_user_confirm_neg_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003069
3070 BT_DBG("");
3071
Johan Hedberg1707c602013-03-15 17:07:15 -05003072 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003073 MGMT_OP_USER_CONFIRM_NEG_REPLY,
3074 HCI_OP_USER_CONFIRM_NEG_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003075}
3076
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003077static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3078 u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003079{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003080 struct mgmt_cp_user_passkey_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003081
3082 BT_DBG("");
3083
Johan Hedberg1707c602013-03-15 17:07:15 -05003084 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003085 MGMT_OP_USER_PASSKEY_REPLY,
3086 HCI_OP_USER_PASSKEY_REPLY, cp->passkey);
Brian Gix604086b2011-11-23 08:28:33 -08003087}
3088
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003089static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003090 void *data, u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003091{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003092 struct mgmt_cp_user_passkey_neg_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003093
3094 BT_DBG("");
3095
Johan Hedberg1707c602013-03-15 17:07:15 -05003096 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003097 MGMT_OP_USER_PASSKEY_NEG_REPLY,
3098 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
Brian Gix604086b2011-11-23 08:28:33 -08003099}
3100
Johan Hedberg13928972013-03-15 17:07:00 -05003101static void update_name(struct hci_request *req)
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003102{
Johan Hedberg13928972013-03-15 17:07:00 -05003103 struct hci_dev *hdev = req->hdev;
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003104 struct hci_cp_write_local_name cp;
3105
Johan Hedberg13928972013-03-15 17:07:00 -05003106 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003107
Johan Hedberg890ea892013-03-15 17:06:52 -05003108 hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp);
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003109}
3110
Johan Hedberg13928972013-03-15 17:07:00 -05003111static void set_name_complete(struct hci_dev *hdev, u8 status)
3112{
3113 struct mgmt_cp_set_local_name *cp;
3114 struct pending_cmd *cmd;
3115
3116 BT_DBG("status 0x%02x", status);
3117
3118 hci_dev_lock(hdev);
3119
3120 cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
3121 if (!cmd)
3122 goto unlock;
3123
3124 cp = cmd->param;
3125
3126 if (status)
3127 cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
3128 mgmt_status(status));
3129 else
3130 cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3131 cp, sizeof(*cp));
3132
3133 mgmt_pending_remove(cmd);
3134
3135unlock:
3136 hci_dev_unlock(hdev);
3137}
3138
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003139static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003140 u16 len)
Johan Hedbergb312b1612011-03-16 14:29:37 +02003141{
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003142 struct mgmt_cp_set_local_name *cp = data;
Johan Hedbergb312b1612011-03-16 14:29:37 +02003143 struct pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05003144 struct hci_request req;
Johan Hedbergb312b1612011-03-16 14:29:37 +02003145 int err;
3146
3147 BT_DBG("");
3148
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003149 hci_dev_lock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003150
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003151 /* If the old values are the same as the new ones just return a
3152 * direct command complete event.
3153 */
3154 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
3155 !memcmp(hdev->short_name, cp->short_name,
3156 sizeof(hdev->short_name))) {
3157 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3158 data, len);
3159 goto failed;
3160 }
3161
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003162 memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003163
Johan Hedbergb5235a62012-02-21 14:32:24 +02003164 if (!hdev_is_powered(hdev)) {
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003165 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003166
3167 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003168 data, len);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003169 if (err < 0)
3170 goto failed;
3171
3172 err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003173 sk);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003174
Johan Hedbergb5235a62012-02-21 14:32:24 +02003175 goto failed;
3176 }
3177
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003178 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003179 if (!cmd) {
3180 err = -ENOMEM;
3181 goto failed;
3182 }
3183
Johan Hedberg13928972013-03-15 17:07:00 -05003184 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3185
Johan Hedberg890ea892013-03-15 17:06:52 -05003186 hci_req_init(&req, hdev);
Johan Hedberg3f985052013-03-15 17:07:02 -05003187
3188 if (lmp_bredr_capable(hdev)) {
3189 update_name(&req);
3190 update_eir(&req);
3191 }
3192
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003193 /* The name is stored in the scan response data and so
3194 * no need to udpate the advertising data here.
3195 */
Johan Hedberg3f985052013-03-15 17:07:02 -05003196 if (lmp_le_capable(hdev))
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003197 update_scan_rsp_data(&req);
Johan Hedberg3f985052013-03-15 17:07:02 -05003198
Johan Hedberg13928972013-03-15 17:07:00 -05003199 err = hci_req_run(&req, set_name_complete);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003200 if (err < 0)
3201 mgmt_pending_remove(cmd);
3202
3203failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003204 hci_dev_unlock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003205 return err;
3206}
3207
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003208static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003209 void *data, u16 data_len)
Szymon Jancc35938b2011-03-22 13:12:21 +01003210{
Szymon Jancc35938b2011-03-22 13:12:21 +01003211 struct pending_cmd *cmd;
3212 int err;
3213
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003214 BT_DBG("%s", hdev->name);
Szymon Jancc35938b2011-03-22 13:12:21 +01003215
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003216 hci_dev_lock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003217
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003218 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003219 err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003220 MGMT_STATUS_NOT_POWERED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003221 goto unlock;
3222 }
3223
Andre Guedes9a1a1992012-07-24 15:03:48 -03003224 if (!lmp_ssp_capable(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003225 err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003226 MGMT_STATUS_NOT_SUPPORTED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003227 goto unlock;
3228 }
3229
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003230 if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003231 err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003232 MGMT_STATUS_BUSY);
Szymon Jancc35938b2011-03-22 13:12:21 +01003233 goto unlock;
3234 }
3235
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003236 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
Szymon Jancc35938b2011-03-22 13:12:21 +01003237 if (!cmd) {
3238 err = -ENOMEM;
3239 goto unlock;
3240 }
3241
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08003242 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags))
3243 err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA,
3244 0, NULL);
3245 else
3246 err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
3247
Szymon Jancc35938b2011-03-22 13:12:21 +01003248 if (err < 0)
3249 mgmt_pending_remove(cmd);
3250
3251unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003252 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003253 return err;
3254}
3255
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003256static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003257 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003258{
Szymon Janc2763eda2011-03-22 13:12:22 +01003259 int err;
3260
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003261 BT_DBG("%s ", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003262
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003263 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003264
Marcel Holtmannec109112014-01-10 02:07:30 -08003265 if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) {
3266 struct mgmt_cp_add_remote_oob_data *cp = data;
3267 u8 status;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003268
Marcel Holtmannec109112014-01-10 02:07:30 -08003269 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
3270 cp->hash, cp->randomizer);
3271 if (err < 0)
3272 status = MGMT_STATUS_FAILED;
3273 else
3274 status = MGMT_STATUS_SUCCESS;
3275
3276 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3277 status, &cp->addr, sizeof(cp->addr));
3278 } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) {
3279 struct mgmt_cp_add_remote_oob_ext_data *cp = data;
3280 u8 status;
3281
3282 err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr,
3283 cp->hash192,
3284 cp->randomizer192,
3285 cp->hash256,
3286 cp->randomizer256);
3287 if (err < 0)
3288 status = MGMT_STATUS_FAILED;
3289 else
3290 status = MGMT_STATUS_SUCCESS;
3291
3292 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3293 status, &cp->addr, sizeof(cp->addr));
3294 } else {
3295 BT_ERR("add_remote_oob_data: invalid length of %u bytes", len);
3296 err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3297 MGMT_STATUS_INVALID_PARAMS);
3298 }
Szymon Janc2763eda2011-03-22 13:12:22 +01003299
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003300 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003301 return err;
3302}
3303
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003304static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003305 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003306{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003307 struct mgmt_cp_remove_remote_oob_data *cp = data;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003308 u8 status;
Szymon Janc2763eda2011-03-22 13:12:22 +01003309 int err;
3310
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003311 BT_DBG("%s", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003312
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003313 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003314
Johan Hedberg664ce4c2012-02-09 15:44:09 +02003315 err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr);
Szymon Janc2763eda2011-03-22 13:12:22 +01003316 if (err < 0)
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003317 status = MGMT_STATUS_INVALID_PARAMS;
Szymon Janc2763eda2011-03-22 13:12:22 +01003318 else
Szymon Janca6785be2012-12-13 15:11:21 +01003319 status = MGMT_STATUS_SUCCESS;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003320
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003321 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003322 status, &cp->addr, sizeof(cp->addr));
Szymon Janc2763eda2011-03-22 13:12:22 +01003323
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003324 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003325 return err;
3326}
3327
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003328static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
3329{
3330 struct pending_cmd *cmd;
3331 u8 type;
3332 int err;
3333
3334 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
3335
3336 cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev);
3337 if (!cmd)
3338 return -ENOENT;
3339
3340 type = hdev->discovery.type;
3341
3342 err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status),
3343 &type, sizeof(type));
3344 mgmt_pending_remove(cmd);
3345
3346 return err;
3347}
3348
Andre Guedes7c307722013-04-30 15:29:28 -03003349static void start_discovery_complete(struct hci_dev *hdev, u8 status)
3350{
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01003351 unsigned long timeout = 0;
3352
Andre Guedes7c307722013-04-30 15:29:28 -03003353 BT_DBG("status %d", status);
3354
3355 if (status) {
3356 hci_dev_lock(hdev);
3357 mgmt_start_discovery_failed(hdev, status);
3358 hci_dev_unlock(hdev);
3359 return;
3360 }
3361
3362 hci_dev_lock(hdev);
3363 hci_discovery_set_state(hdev, DISCOVERY_FINDING);
3364 hci_dev_unlock(hdev);
3365
3366 switch (hdev->discovery.type) {
3367 case DISCOV_TYPE_LE:
Lukasz Rymanowski3d5a76f2014-03-27 20:55:21 +01003368 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
Andre Guedes7c307722013-04-30 15:29:28 -03003369 break;
3370
3371 case DISCOV_TYPE_INTERLEAVED:
Lukasz Rymanowskib9a7a612014-03-27 20:55:20 +01003372 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
Andre Guedes7c307722013-04-30 15:29:28 -03003373 break;
3374
3375 case DISCOV_TYPE_BREDR:
3376 break;
3377
3378 default:
3379 BT_ERR("Invalid discovery type %d", hdev->discovery.type);
3380 }
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01003381
3382 if (!timeout)
3383 return;
3384
3385 queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout);
Andre Guedes7c307722013-04-30 15:29:28 -03003386}
3387
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003388static int start_discovery(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003389 void *data, u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04003390{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003391 struct mgmt_cp_start_discovery *cp = data;
Johan Hedberg14a53662011-04-27 10:29:56 -04003392 struct pending_cmd *cmd;
Andre Guedes7c307722013-04-30 15:29:28 -03003393 struct hci_cp_le_set_scan_param param_cp;
3394 struct hci_cp_le_set_scan_enable enable_cp;
3395 struct hci_cp_inquiry inq_cp;
3396 struct hci_request req;
3397 /* General inquiry access code (GIAC) */
3398 u8 lap[3] = { 0x33, 0x8b, 0x9e };
Johan Hedbergd9483942014-02-23 19:42:24 +02003399 u8 status, own_addr_type;
Johan Hedberg14a53662011-04-27 10:29:56 -04003400 int err;
3401
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003402 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04003403
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003404 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04003405
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003406 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003407 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003408 MGMT_STATUS_NOT_POWERED);
Johan Hedbergbd2d1332011-11-07 23:13:37 +02003409 goto failed;
3410 }
3411
Andre Guedes642be6c2012-03-21 00:03:37 -03003412 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) {
3413 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
3414 MGMT_STATUS_BUSY);
3415 goto failed;
3416 }
3417
Johan Hedbergff9ef572012-01-04 14:23:45 +02003418 if (hdev->discovery.state != DISCOVERY_STOPPED) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003419 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003420 MGMT_STATUS_BUSY);
Johan Hedbergff9ef572012-01-04 14:23:45 +02003421 goto failed;
3422 }
3423
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003424 cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0);
Johan Hedberg14a53662011-04-27 10:29:56 -04003425 if (!cmd) {
3426 err = -ENOMEM;
3427 goto failed;
3428 }
3429
Andre Guedes4aab14e2012-02-17 20:39:36 -03003430 hdev->discovery.type = cp->type;
3431
Andre Guedes7c307722013-04-30 15:29:28 -03003432 hci_req_init(&req, hdev);
3433
Andre Guedes4aab14e2012-02-17 20:39:36 -03003434 switch (hdev->discovery.type) {
Andre Guedesf39799f2012-02-17 20:39:35 -03003435 case DISCOV_TYPE_BREDR:
Johan Hedberge6fe7982013-10-02 15:45:22 +03003436 status = mgmt_bredr_support(hdev);
3437 if (status) {
Johan Hedberg04106752013-01-10 14:54:09 +02003438 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
Johan Hedberge6fe7982013-10-02 15:45:22 +03003439 status);
Johan Hedberg04106752013-01-10 14:54:09 +02003440 mgmt_pending_remove(cmd);
3441 goto failed;
3442 }
3443
Andre Guedes7c307722013-04-30 15:29:28 -03003444 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
3445 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
3446 MGMT_STATUS_BUSY);
3447 mgmt_pending_remove(cmd);
3448 goto failed;
3449 }
3450
3451 hci_inquiry_cache_flush(hdev);
3452
3453 memset(&inq_cp, 0, sizeof(inq_cp));
3454 memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap));
Andre Guedes0d8cc932013-04-30 15:29:31 -03003455 inq_cp.length = DISCOV_BREDR_INQUIRY_LEN;
Andre Guedes7c307722013-04-30 15:29:28 -03003456 hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp);
Andre Guedesf39799f2012-02-17 20:39:35 -03003457 break;
3458
3459 case DISCOV_TYPE_LE:
Andre Guedes7c307722013-04-30 15:29:28 -03003460 case DISCOV_TYPE_INTERLEAVED:
Johan Hedberge6fe7982013-10-02 15:45:22 +03003461 status = mgmt_le_support(hdev);
3462 if (status) {
Johan Hedberg04106752013-01-10 14:54:09 +02003463 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
Johan Hedberge6fe7982013-10-02 15:45:22 +03003464 status);
Johan Hedberg04106752013-01-10 14:54:09 +02003465 mgmt_pending_remove(cmd);
3466 goto failed;
3467 }
3468
Andre Guedes7c307722013-04-30 15:29:28 -03003469 if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED &&
Johan Hedberg56f87902013-10-02 13:43:13 +03003470 !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
Johan Hedberg04106752013-01-10 14:54:09 +02003471 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
3472 MGMT_STATUS_NOT_SUPPORTED);
3473 mgmt_pending_remove(cmd);
3474 goto failed;
3475 }
3476
Johan Hedbergf3d3444a2013-10-05 12:01:04 +02003477 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
Andre Guedes7c307722013-04-30 15:29:28 -03003478 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
3479 MGMT_STATUS_REJECTED);
3480 mgmt_pending_remove(cmd);
3481 goto failed;
3482 }
3483
Andre Guedesc54c3862014-02-26 20:21:50 -03003484 /* If controller is scanning, it means the background scanning
3485 * is running. Thus, we should temporarily stop it in order to
3486 * set the discovery scanning parameters.
3487 */
3488 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags))
3489 hci_req_add_le_scan_disable(&req);
Andre Guedes7c307722013-04-30 15:29:28 -03003490
3491 memset(&param_cp, 0, sizeof(param_cp));
Johan Hedbergd9483942014-02-23 19:42:24 +02003492
Marcel Holtmann94b1fc92014-02-23 20:25:54 -08003493 /* All active scans will be done with either a resolvable
3494 * private address (when privacy feature has been enabled)
3495 * or unresolvable private address.
3496 */
3497 err = hci_update_random_address(&req, true, &own_addr_type);
Johan Hedbergd9483942014-02-23 19:42:24 +02003498 if (err < 0) {
3499 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
3500 MGMT_STATUS_FAILED);
3501 mgmt_pending_remove(cmd);
3502 goto failed;
3503 }
3504
Andre Guedes7c307722013-04-30 15:29:28 -03003505 param_cp.type = LE_SCAN_ACTIVE;
Andre Guedes0d8cc932013-04-30 15:29:31 -03003506 param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT);
3507 param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN);
Johan Hedbergd9483942014-02-23 19:42:24 +02003508 param_cp.own_address_type = own_addr_type;
Andre Guedes7c307722013-04-30 15:29:28 -03003509 hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
3510 &param_cp);
3511
3512 memset(&enable_cp, 0, sizeof(enable_cp));
3513 enable_cp.enable = LE_SCAN_ENABLE;
3514 enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
3515 hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
3516 &enable_cp);
Andre Guedes5e0452c2012-02-17 20:39:38 -03003517 break;
3518
Andre Guedesf39799f2012-02-17 20:39:35 -03003519 default:
Johan Hedberg04106752013-01-10 14:54:09 +02003520 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
3521 MGMT_STATUS_INVALID_PARAMS);
3522 mgmt_pending_remove(cmd);
3523 goto failed;
Andre Guedesf39799f2012-02-17 20:39:35 -03003524 }
Andre Guedes3fd24152012-02-03 17:48:01 -03003525
Andre Guedes7c307722013-04-30 15:29:28 -03003526 err = hci_req_run(&req, start_discovery_complete);
Johan Hedberg14a53662011-04-27 10:29:56 -04003527 if (err < 0)
3528 mgmt_pending_remove(cmd);
Johan Hedbergff9ef572012-01-04 14:23:45 +02003529 else
3530 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
Johan Hedberg14a53662011-04-27 10:29:56 -04003531
3532failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003533 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04003534 return err;
3535}
3536
Andre Guedes1183fdc2013-04-30 15:29:35 -03003537static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status)
3538{
3539 struct pending_cmd *cmd;
3540 int err;
3541
3542 cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
3543 if (!cmd)
3544 return -ENOENT;
3545
3546 err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status),
3547 &hdev->discovery.type, sizeof(hdev->discovery.type));
3548 mgmt_pending_remove(cmd);
3549
3550 return err;
3551}
3552
Andre Guedes0e05bba2013-04-30 15:29:33 -03003553static void stop_discovery_complete(struct hci_dev *hdev, u8 status)
3554{
3555 BT_DBG("status %d", status);
3556
3557 hci_dev_lock(hdev);
3558
3559 if (status) {
3560 mgmt_stop_discovery_failed(hdev, status);
3561 goto unlock;
3562 }
3563
3564 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
3565
3566unlock:
3567 hci_dev_unlock(hdev);
3568}
3569
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003570static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003571 u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04003572{
Johan Hedbergd9306502012-02-20 23:25:18 +02003573 struct mgmt_cp_stop_discovery *mgmt_cp = data;
Johan Hedberg14a53662011-04-27 10:29:56 -04003574 struct pending_cmd *cmd;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003575 struct hci_cp_remote_name_req_cancel cp;
3576 struct inquiry_entry *e;
Andre Guedes0e05bba2013-04-30 15:29:33 -03003577 struct hci_request req;
Johan Hedberg14a53662011-04-27 10:29:56 -04003578 int err;
3579
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003580 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04003581
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003582 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04003583
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003584 if (!hci_discovery_active(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003585 err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003586 MGMT_STATUS_REJECTED, &mgmt_cp->type,
3587 sizeof(mgmt_cp->type));
Johan Hedbergd9306502012-02-20 23:25:18 +02003588 goto unlock;
3589 }
3590
3591 if (hdev->discovery.type != mgmt_cp->type) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003592 err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003593 MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type,
3594 sizeof(mgmt_cp->type));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003595 goto unlock;
Johan Hedbergff9ef572012-01-04 14:23:45 +02003596 }
3597
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003598 cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0);
Johan Hedberg14a53662011-04-27 10:29:56 -04003599 if (!cmd) {
3600 err = -ENOMEM;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003601 goto unlock;
Johan Hedberg14a53662011-04-27 10:29:56 -04003602 }
3603
Andre Guedes0e05bba2013-04-30 15:29:33 -03003604 hci_req_init(&req, hdev);
3605
Andre Guedese0d9727e2012-03-20 15:15:36 -03003606 switch (hdev->discovery.state) {
3607 case DISCOVERY_FINDING:
Andre Guedes0e05bba2013-04-30 15:29:33 -03003608 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
3609 hci_req_add(&req, HCI_OP_INQUIRY_CANCEL, 0, NULL);
3610 } else {
3611 cancel_delayed_work(&hdev->le_scan_disable);
3612
Andre Guedesb1efcc22014-02-26 20:21:40 -03003613 hci_req_add_le_scan_disable(&req);
Andre Guedes0e05bba2013-04-30 15:29:33 -03003614 }
Andre Guedesc9ecc482012-03-15 16:52:08 -03003615
Andre Guedese0d9727e2012-03-20 15:15:36 -03003616 break;
3617
3618 case DISCOVERY_RESOLVING:
3619 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003620 NAME_PENDING);
Andre Guedese0d9727e2012-03-20 15:15:36 -03003621 if (!e) {
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003622 mgmt_pending_remove(cmd);
Andre Guedese0d9727e2012-03-20 15:15:36 -03003623 err = cmd_complete(sk, hdev->id,
3624 MGMT_OP_STOP_DISCOVERY, 0,
3625 &mgmt_cp->type,
3626 sizeof(mgmt_cp->type));
3627 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
3628 goto unlock;
3629 }
3630
3631 bacpy(&cp.bdaddr, &e->data.bdaddr);
Andre Guedes0e05bba2013-04-30 15:29:33 -03003632 hci_req_add(&req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp),
3633 &cp);
Andre Guedese0d9727e2012-03-20 15:15:36 -03003634
3635 break;
3636
3637 default:
3638 BT_DBG("unknown discovery state %u", hdev->discovery.state);
Andre Guedes0e05bba2013-04-30 15:29:33 -03003639
3640 mgmt_pending_remove(cmd);
3641 err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
3642 MGMT_STATUS_FAILED, &mgmt_cp->type,
3643 sizeof(mgmt_cp->type));
3644 goto unlock;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003645 }
3646
Andre Guedes0e05bba2013-04-30 15:29:33 -03003647 err = hci_req_run(&req, stop_discovery_complete);
Johan Hedberg14a53662011-04-27 10:29:56 -04003648 if (err < 0)
3649 mgmt_pending_remove(cmd);
Johan Hedbergff9ef572012-01-04 14:23:45 +02003650 else
3651 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
Johan Hedberg14a53662011-04-27 10:29:56 -04003652
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003653unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003654 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04003655 return err;
3656}
3657
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003658static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003659 u16 len)
Johan Hedberg561aafb2012-01-04 13:31:59 +02003660{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003661 struct mgmt_cp_confirm_name *cp = data;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003662 struct inquiry_entry *e;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003663 int err;
3664
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003665 BT_DBG("%s", hdev->name);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003666
Johan Hedberg561aafb2012-01-04 13:31:59 +02003667 hci_dev_lock(hdev);
3668
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003669 if (!hci_discovery_active(hdev)) {
Lukasz Rymanowskid3a25412014-02-27 16:47:28 +01003670 err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
3671 MGMT_STATUS_FAILED, &cp->addr,
3672 sizeof(cp->addr));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003673 goto failed;
3674 }
3675
Johan Hedberga198e7b2012-02-17 14:27:06 +02003676 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003677 if (!e) {
Lukasz Rymanowskid3a25412014-02-27 16:47:28 +01003678 err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
3679 MGMT_STATUS_INVALID_PARAMS, &cp->addr,
3680 sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02003681 goto failed;
3682 }
3683
3684 if (cp->name_known) {
3685 e->name_state = NAME_KNOWN;
3686 list_del(&e->list);
3687 } else {
3688 e->name_state = NAME_NEEDED;
Johan Hedberga3d4e20a2012-01-09 00:53:02 +02003689 hci_inquiry_cache_update_resolve(hdev, e);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003690 }
3691
Johan Hedberge3846622013-01-09 15:29:33 +02003692 err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr,
3693 sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02003694
3695failed:
3696 hci_dev_unlock(hdev);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003697 return err;
3698}
3699
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003700static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003701 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03003702{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003703 struct mgmt_cp_block_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003704 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03003705 int err;
3706
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003707 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03003708
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003709 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg5d0846d2013-01-20 14:27:22 +02003710 return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
3711 MGMT_STATUS_INVALID_PARAMS,
3712 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003713
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003714 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03003715
Johan Hedberg88c1fe42012-02-09 15:56:11 +02003716 err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type);
Antti Julku7fbec222011-06-15 12:01:15 +03003717 if (err < 0)
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003718 status = MGMT_STATUS_FAILED;
Antti Julku7fbec222011-06-15 12:01:15 +03003719 else
Szymon Janca6785be2012-12-13 15:11:21 +01003720 status = MGMT_STATUS_SUCCESS;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003721
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003722 err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003723 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03003724
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003725 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03003726
3727 return err;
3728}
3729
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003730static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003731 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03003732{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003733 struct mgmt_cp_unblock_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003734 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03003735 int err;
3736
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003737 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03003738
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003739 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg5d0846d2013-01-20 14:27:22 +02003740 return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
3741 MGMT_STATUS_INVALID_PARAMS,
3742 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003743
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003744 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03003745
Johan Hedberg88c1fe42012-02-09 15:56:11 +02003746 err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type);
Antti Julku7fbec222011-06-15 12:01:15 +03003747 if (err < 0)
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003748 status = MGMT_STATUS_INVALID_PARAMS;
Antti Julku7fbec222011-06-15 12:01:15 +03003749 else
Szymon Janca6785be2012-12-13 15:11:21 +01003750 status = MGMT_STATUS_SUCCESS;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02003751
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003752 err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003753 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03003754
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003755 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03003756
3757 return err;
3758}
3759
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003760static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
3761 u16 len)
3762{
3763 struct mgmt_cp_set_device_id *cp = data;
Johan Hedberg890ea892013-03-15 17:06:52 -05003764 struct hci_request req;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003765 int err;
Szymon Jancc72d4b82012-03-16 16:02:57 +01003766 __u16 source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003767
3768 BT_DBG("%s", hdev->name);
3769
Szymon Jancc72d4b82012-03-16 16:02:57 +01003770 source = __le16_to_cpu(cp->source);
3771
3772 if (source > 0x0002)
3773 return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
3774 MGMT_STATUS_INVALID_PARAMS);
3775
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003776 hci_dev_lock(hdev);
3777
Szymon Jancc72d4b82012-03-16 16:02:57 +01003778 hdev->devid_source = source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003779 hdev->devid_vendor = __le16_to_cpu(cp->vendor);
3780 hdev->devid_product = __le16_to_cpu(cp->product);
3781 hdev->devid_version = __le16_to_cpu(cp->version);
3782
3783 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0);
3784
Johan Hedberg890ea892013-03-15 17:06:52 -05003785 hci_req_init(&req, hdev);
3786 update_eir(&req);
3787 hci_req_run(&req, NULL);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07003788
3789 hci_dev_unlock(hdev);
3790
3791 return err;
3792}
3793
Johan Hedberg4375f102013-09-25 13:26:10 +03003794static void set_advertising_complete(struct hci_dev *hdev, u8 status)
3795{
3796 struct cmd_lookup match = { NULL, hdev };
3797
3798 if (status) {
3799 u8 mgmt_err = mgmt_status(status);
3800
3801 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev,
3802 cmd_status_rsp, &mgmt_err);
3803 return;
3804 }
3805
3806 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
3807 &match);
3808
3809 new_settings(hdev, match.sk);
3810
3811 if (match.sk)
3812 sock_put(match.sk);
3813}
3814
Marcel Holtmann21b51872013-10-10 09:47:53 -07003815static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
3816 u16 len)
Johan Hedberg4375f102013-09-25 13:26:10 +03003817{
3818 struct mgmt_mode *cp = data;
3819 struct pending_cmd *cmd;
3820 struct hci_request req;
Johan Hedberge6fe7982013-10-02 15:45:22 +03003821 u8 val, enabled, status;
Johan Hedberg4375f102013-09-25 13:26:10 +03003822 int err;
3823
3824 BT_DBG("request for %s", hdev->name);
3825
Johan Hedberge6fe7982013-10-02 15:45:22 +03003826 status = mgmt_le_support(hdev);
3827 if (status)
Johan Hedberg4375f102013-09-25 13:26:10 +03003828 return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
Johan Hedberge6fe7982013-10-02 15:45:22 +03003829 status);
Johan Hedberg4375f102013-09-25 13:26:10 +03003830
3831 if (cp->val != 0x00 && cp->val != 0x01)
3832 return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
3833 MGMT_STATUS_INVALID_PARAMS);
3834
3835 hci_dev_lock(hdev);
3836
3837 val = !!cp->val;
Johan Hedbergf3d3444a2013-10-05 12:01:04 +02003838 enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags);
Johan Hedberg4375f102013-09-25 13:26:10 +03003839
Johan Hedbergf74ca9b2013-10-08 15:52:18 +02003840 /* The following conditions are ones which mean that we should
3841 * not do any HCI communication but directly send a mgmt
3842 * response to user space (after toggling the flag if
3843 * necessary).
3844 */
3845 if (!hdev_is_powered(hdev) || val == enabled ||
Marcel Holtmannb145edc2013-10-10 09:47:54 -07003846 hci_conn_num(hdev, LE_LINK) > 0) {
Johan Hedberg4375f102013-09-25 13:26:10 +03003847 bool changed = false;
3848
Johan Hedbergf3d3444a2013-10-05 12:01:04 +02003849 if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
3850 change_bit(HCI_ADVERTISING, &hdev->dev_flags);
Johan Hedberg4375f102013-09-25 13:26:10 +03003851 changed = true;
3852 }
3853
3854 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
3855 if (err < 0)
3856 goto unlock;
3857
3858 if (changed)
3859 err = new_settings(hdev, sk);
3860
3861 goto unlock;
3862 }
3863
3864 if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
3865 mgmt_pending_find(MGMT_OP_SET_LE, hdev)) {
3866 err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
3867 MGMT_STATUS_BUSY);
3868 goto unlock;
3869 }
3870
3871 cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
3872 if (!cmd) {
3873 err = -ENOMEM;
3874 goto unlock;
3875 }
3876
3877 hci_req_init(&req, hdev);
3878
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07003879 if (val)
3880 enable_advertising(&req);
3881 else
3882 disable_advertising(&req);
Johan Hedberg4375f102013-09-25 13:26:10 +03003883
3884 err = hci_req_run(&req, set_advertising_complete);
3885 if (err < 0)
3886 mgmt_pending_remove(cmd);
3887
3888unlock:
3889 hci_dev_unlock(hdev);
3890 return err;
3891}
3892
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07003893static int set_static_address(struct sock *sk, struct hci_dev *hdev,
3894 void *data, u16 len)
3895{
3896 struct mgmt_cp_set_static_address *cp = data;
3897 int err;
3898
3899 BT_DBG("%s", hdev->name);
3900
Marcel Holtmann62af4442013-10-02 22:10:32 -07003901 if (!lmp_le_capable(hdev))
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07003902 return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
Marcel Holtmann62af4442013-10-02 22:10:32 -07003903 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07003904
3905 if (hdev_is_powered(hdev))
3906 return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
3907 MGMT_STATUS_REJECTED);
3908
3909 if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
3910 if (!bacmp(&cp->bdaddr, BDADDR_NONE))
3911 return cmd_status(sk, hdev->id,
3912 MGMT_OP_SET_STATIC_ADDRESS,
3913 MGMT_STATUS_INVALID_PARAMS);
3914
3915 /* Two most significant bits shall be set */
3916 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
3917 return cmd_status(sk, hdev->id,
3918 MGMT_OP_SET_STATIC_ADDRESS,
3919 MGMT_STATUS_INVALID_PARAMS);
3920 }
3921
3922 hci_dev_lock(hdev);
3923
3924 bacpy(&hdev->static_addr, &cp->bdaddr);
3925
3926 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0);
3927
3928 hci_dev_unlock(hdev);
3929
3930 return err;
3931}
3932
Marcel Holtmann14b49b92013-10-11 08:23:20 -07003933static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
3934 void *data, u16 len)
3935{
3936 struct mgmt_cp_set_scan_params *cp = data;
3937 __u16 interval, window;
3938 int err;
3939
3940 BT_DBG("%s", hdev->name);
3941
3942 if (!lmp_le_capable(hdev))
3943 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
3944 MGMT_STATUS_NOT_SUPPORTED);
3945
3946 interval = __le16_to_cpu(cp->interval);
3947
3948 if (interval < 0x0004 || interval > 0x4000)
3949 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
3950 MGMT_STATUS_INVALID_PARAMS);
3951
3952 window = __le16_to_cpu(cp->window);
3953
3954 if (window < 0x0004 || window > 0x4000)
3955 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
3956 MGMT_STATUS_INVALID_PARAMS);
3957
Marcel Holtmann899e1072013-10-14 09:55:32 -07003958 if (window > interval)
3959 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
3960 MGMT_STATUS_INVALID_PARAMS);
3961
Marcel Holtmann14b49b92013-10-11 08:23:20 -07003962 hci_dev_lock(hdev);
3963
3964 hdev->le_scan_interval = interval;
3965 hdev->le_scan_window = window;
3966
3967 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0);
3968
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03003969 /* If background scan is running, restart it so new parameters are
3970 * loaded.
3971 */
3972 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) &&
3973 hdev->discovery.state == DISCOVERY_STOPPED) {
3974 struct hci_request req;
3975
3976 hci_req_init(&req, hdev);
3977
3978 hci_req_add_le_scan_disable(&req);
3979 hci_req_add_le_passive_scan(&req);
3980
3981 hci_req_run(&req, NULL);
3982 }
3983
Marcel Holtmann14b49b92013-10-11 08:23:20 -07003984 hci_dev_unlock(hdev);
3985
3986 return err;
3987}
3988
Johan Hedberg33e38b32013-03-15 17:07:05 -05003989static void fast_connectable_complete(struct hci_dev *hdev, u8 status)
3990{
3991 struct pending_cmd *cmd;
3992
3993 BT_DBG("status 0x%02x", status);
3994
3995 hci_dev_lock(hdev);
3996
3997 cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev);
3998 if (!cmd)
3999 goto unlock;
4000
4001 if (status) {
4002 cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4003 mgmt_status(status));
4004 } else {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004005 struct mgmt_mode *cp = cmd->param;
4006
4007 if (cp->val)
4008 set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags);
4009 else
4010 clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags);
4011
Johan Hedberg33e38b32013-03-15 17:07:05 -05004012 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
4013 new_settings(hdev, cmd->sk);
4014 }
4015
4016 mgmt_pending_remove(cmd);
4017
4018unlock:
4019 hci_dev_unlock(hdev);
4020}
4021
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004022static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004023 void *data, u16 len)
Antti Julkuf6422ec2011-06-22 13:11:56 +03004024{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004025 struct mgmt_mode *cp = data;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004026 struct pending_cmd *cmd;
4027 struct hci_request req;
Antti Julkuf6422ec2011-06-22 13:11:56 +03004028 int err;
4029
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004030 BT_DBG("%s", hdev->name);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004031
Johan Hedberg56f87902013-10-02 13:43:13 +03004032 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) ||
4033 hdev->hci_ver < BLUETOOTH_VER_1_2)
Johan Hedberg33c525c2012-10-24 21:11:58 +03004034 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4035 MGMT_STATUS_NOT_SUPPORTED);
4036
Johan Hedberga7e80f22013-01-09 16:05:19 +02004037 if (cp->val != 0x00 && cp->val != 0x01)
4038 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4039 MGMT_STATUS_INVALID_PARAMS);
4040
Johan Hedberg5400c042012-02-21 16:40:33 +02004041 if (!hdev_is_powered(hdev))
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004042 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004043 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5400c042012-02-21 16:40:33 +02004044
4045 if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004046 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004047 MGMT_STATUS_REJECTED);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004048
4049 hci_dev_lock(hdev);
4050
Johan Hedberg05cbf292013-03-15 17:07:07 -05004051 if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) {
4052 err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4053 MGMT_STATUS_BUSY);
4054 goto unlock;
4055 }
4056
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004057 if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) {
4058 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4059 hdev);
4060 goto unlock;
4061 }
4062
Johan Hedberg33e38b32013-03-15 17:07:05 -05004063 cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev,
4064 data, len);
4065 if (!cmd) {
4066 err = -ENOMEM;
4067 goto unlock;
4068 }
4069
4070 hci_req_init(&req, hdev);
4071
Johan Hedberg406d7802013-03-15 17:07:09 -05004072 write_fast_connectable(&req, cp->val);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004073
4074 err = hci_req_run(&req, fast_connectable_complete);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004075 if (err < 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004076 err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004077 MGMT_STATUS_FAILED);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004078 mgmt_pending_remove(cmd);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004079 }
4080
Johan Hedberg33e38b32013-03-15 17:07:05 -05004081unlock:
Antti Julkuf6422ec2011-06-22 13:11:56 +03004082 hci_dev_unlock(hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004083
Antti Julkuf6422ec2011-06-22 13:11:56 +03004084 return err;
4085}
4086
Johan Hedberg67e5a7a2013-10-14 21:15:25 +03004087static void set_bredr_scan(struct hci_request *req)
4088{
4089 struct hci_dev *hdev = req->hdev;
4090 u8 scan = 0;
4091
4092 /* Ensure that fast connectable is disabled. This function will
4093 * not do anything if the page scan parameters are already what
4094 * they should be.
4095 */
4096 write_fast_connectable(req, false);
4097
4098 if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
4099 scan |= SCAN_PAGE;
4100 if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
4101 scan |= SCAN_INQUIRY;
4102
4103 if (scan)
4104 hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
4105}
4106
Johan Hedberg0663ca22013-10-02 13:43:14 +03004107static void set_bredr_complete(struct hci_dev *hdev, u8 status)
4108{
4109 struct pending_cmd *cmd;
4110
4111 BT_DBG("status 0x%02x", status);
4112
4113 hci_dev_lock(hdev);
4114
4115 cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev);
4116 if (!cmd)
4117 goto unlock;
4118
4119 if (status) {
4120 u8 mgmt_err = mgmt_status(status);
4121
4122 /* We need to restore the flag if related HCI commands
4123 * failed.
4124 */
4125 clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
4126
4127 cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
4128 } else {
4129 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
4130 new_settings(hdev, cmd->sk);
4131 }
4132
4133 mgmt_pending_remove(cmd);
4134
4135unlock:
4136 hci_dev_unlock(hdev);
4137}
4138
4139static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
4140{
4141 struct mgmt_mode *cp = data;
4142 struct pending_cmd *cmd;
4143 struct hci_request req;
4144 int err;
4145
4146 BT_DBG("request for %s", hdev->name);
4147
4148 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
4149 return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4150 MGMT_STATUS_NOT_SUPPORTED);
4151
4152 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
4153 return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4154 MGMT_STATUS_REJECTED);
4155
4156 if (cp->val != 0x00 && cp->val != 0x01)
4157 return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4158 MGMT_STATUS_INVALID_PARAMS);
4159
4160 hci_dev_lock(hdev);
4161
4162 if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
4163 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4164 goto unlock;
4165 }
4166
4167 if (!hdev_is_powered(hdev)) {
4168 if (!cp->val) {
Johan Hedberg0663ca22013-10-02 13:43:14 +03004169 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
4170 clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
4171 clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
4172 clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags);
4173 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
4174 }
4175
4176 change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
4177
4178 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4179 if (err < 0)
4180 goto unlock;
4181
4182 err = new_settings(hdev, sk);
4183 goto unlock;
4184 }
4185
4186 /* Reject disabling when powered on */
4187 if (!cp->val) {
4188 err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4189 MGMT_STATUS_REJECTED);
4190 goto unlock;
4191 }
4192
4193 if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) {
4194 err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4195 MGMT_STATUS_BUSY);
4196 goto unlock;
4197 }
4198
4199 cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len);
4200 if (!cmd) {
4201 err = -ENOMEM;
4202 goto unlock;
4203 }
4204
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07004205 /* We need to flip the bit already here so that update_adv_data
Johan Hedberg0663ca22013-10-02 13:43:14 +03004206 * generates the correct flags.
4207 */
4208 set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
4209
4210 hci_req_init(&req, hdev);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004211
4212 if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
4213 set_bredr_scan(&req);
4214
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07004215 /* Since only the advertising data flags will change, there
4216 * is no need to update the scan response data.
4217 */
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07004218 update_adv_data(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004219
Johan Hedberg0663ca22013-10-02 13:43:14 +03004220 err = hci_req_run(&req, set_bredr_complete);
4221 if (err < 0)
4222 mgmt_pending_remove(cmd);
4223
4224unlock:
4225 hci_dev_unlock(hdev);
4226 return err;
4227}
4228
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004229static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
4230 void *data, u16 len)
4231{
4232 struct mgmt_mode *cp = data;
4233 struct pending_cmd *cmd;
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004234 u8 val, status;
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004235 int err;
4236
4237 BT_DBG("request for %s", hdev->name);
4238
4239 status = mgmt_bredr_support(hdev);
4240 if (status)
4241 return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4242 status);
4243
Marcel Holtmann5afeac12014-01-10 02:07:27 -08004244 if (!lmp_sc_capable(hdev) &&
4245 !test_bit(HCI_FORCE_SC, &hdev->dev_flags))
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004246 return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4247 MGMT_STATUS_NOT_SUPPORTED);
4248
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004249 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004250 return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4251 MGMT_STATUS_INVALID_PARAMS);
4252
4253 hci_dev_lock(hdev);
4254
4255 if (!hdev_is_powered(hdev)) {
4256 bool changed;
4257
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004258 if (cp->val) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004259 changed = !test_and_set_bit(HCI_SC_ENABLED,
4260 &hdev->dev_flags);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004261 if (cp->val == 0x02)
4262 set_bit(HCI_SC_ONLY, &hdev->dev_flags);
4263 else
4264 clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
4265 } else {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004266 changed = test_and_clear_bit(HCI_SC_ENABLED,
4267 &hdev->dev_flags);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004268 clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
4269 }
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004270
4271 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
4272 if (err < 0)
4273 goto failed;
4274
4275 if (changed)
4276 err = new_settings(hdev, sk);
4277
4278 goto failed;
4279 }
4280
4281 if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) {
4282 err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4283 MGMT_STATUS_BUSY);
4284 goto failed;
4285 }
4286
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004287 val = !!cp->val;
4288
4289 if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) &&
4290 (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004291 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
4292 goto failed;
4293 }
4294
4295 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len);
4296 if (!cmd) {
4297 err = -ENOMEM;
4298 goto failed;
4299 }
4300
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004301 err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004302 if (err < 0) {
4303 mgmt_pending_remove(cmd);
4304 goto failed;
4305 }
4306
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004307 if (cp->val == 0x02)
4308 set_bit(HCI_SC_ONLY, &hdev->dev_flags);
4309 else
4310 clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
4311
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004312failed:
4313 hci_dev_unlock(hdev);
4314 return err;
4315}
4316
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004317static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
4318 void *data, u16 len)
4319{
4320 struct mgmt_mode *cp = data;
4321 bool changed;
4322 int err;
4323
4324 BT_DBG("request for %s", hdev->name);
4325
4326 if (cp->val != 0x00 && cp->val != 0x01)
4327 return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
4328 MGMT_STATUS_INVALID_PARAMS);
4329
4330 hci_dev_lock(hdev);
4331
4332 if (cp->val)
4333 changed = !test_and_set_bit(HCI_DEBUG_KEYS, &hdev->dev_flags);
4334 else
4335 changed = test_and_clear_bit(HCI_DEBUG_KEYS, &hdev->dev_flags);
4336
4337 err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev);
4338 if (err < 0)
4339 goto unlock;
4340
4341 if (changed)
4342 err = new_settings(hdev, sk);
4343
4344unlock:
4345 hci_dev_unlock(hdev);
4346 return err;
4347}
4348
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004349static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
4350 u16 len)
4351{
4352 struct mgmt_cp_set_privacy *cp = cp_data;
4353 bool changed;
4354 int err;
4355
4356 BT_DBG("request for %s", hdev->name);
4357
4358 if (!lmp_le_capable(hdev))
4359 return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
4360 MGMT_STATUS_NOT_SUPPORTED);
4361
4362 if (cp->privacy != 0x00 && cp->privacy != 0x01)
4363 return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
4364 MGMT_STATUS_INVALID_PARAMS);
4365
4366 if (hdev_is_powered(hdev))
4367 return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
4368 MGMT_STATUS_REJECTED);
4369
4370 hci_dev_lock(hdev);
4371
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02004372 /* If user space supports this command it is also expected to
4373 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
4374 */
4375 set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags);
4376
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004377 if (cp->privacy) {
4378 changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags);
4379 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
4380 set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
4381 } else {
4382 changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags);
4383 memset(hdev->irk, 0, sizeof(hdev->irk));
4384 clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
4385 }
4386
4387 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
4388 if (err < 0)
4389 goto unlock;
4390
4391 if (changed)
4392 err = new_settings(hdev, sk);
4393
4394unlock:
4395 hci_dev_unlock(hdev);
4396 return err;
4397}
4398
Johan Hedberg41edf162014-02-18 10:19:35 +02004399static bool irk_is_valid(struct mgmt_irk_info *irk)
4400{
4401 switch (irk->addr.type) {
4402 case BDADDR_LE_PUBLIC:
4403 return true;
4404
4405 case BDADDR_LE_RANDOM:
4406 /* Two most significant bits shall be set */
4407 if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0)
4408 return false;
4409 return true;
4410 }
4411
4412 return false;
4413}
4414
4415static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
4416 u16 len)
4417{
4418 struct mgmt_cp_load_irks *cp = cp_data;
4419 u16 irk_count, expected_len;
4420 int i, err;
4421
4422 BT_DBG("request for %s", hdev->name);
4423
4424 if (!lmp_le_capable(hdev))
4425 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
4426 MGMT_STATUS_NOT_SUPPORTED);
4427
4428 irk_count = __le16_to_cpu(cp->irk_count);
4429
4430 expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info);
4431 if (expected_len != len) {
4432 BT_ERR("load_irks: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02004433 expected_len, len);
Johan Hedberg41edf162014-02-18 10:19:35 +02004434 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
4435 MGMT_STATUS_INVALID_PARAMS);
4436 }
4437
4438 BT_DBG("%s irk_count %u", hdev->name, irk_count);
4439
4440 for (i = 0; i < irk_count; i++) {
4441 struct mgmt_irk_info *key = &cp->irks[i];
4442
4443 if (!irk_is_valid(key))
4444 return cmd_status(sk, hdev->id,
4445 MGMT_OP_LOAD_IRKS,
4446 MGMT_STATUS_INVALID_PARAMS);
4447 }
4448
4449 hci_dev_lock(hdev);
4450
4451 hci_smp_irks_clear(hdev);
4452
4453 for (i = 0; i < irk_count; i++) {
4454 struct mgmt_irk_info *irk = &cp->irks[i];
4455 u8 addr_type;
4456
4457 if (irk->addr.type == BDADDR_LE_PUBLIC)
4458 addr_type = ADDR_LE_DEV_PUBLIC;
4459 else
4460 addr_type = ADDR_LE_DEV_RANDOM;
4461
4462 hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val,
4463 BDADDR_ANY);
4464 }
4465
4466 set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags);
4467
4468 err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
4469
4470 hci_dev_unlock(hdev);
4471
4472 return err;
4473}
4474
Johan Hedberg3f706b72013-01-20 14:27:16 +02004475static bool ltk_is_valid(struct mgmt_ltk_info *key)
4476{
4477 if (key->master != 0x00 && key->master != 0x01)
4478 return false;
Marcel Holtmann490cb0b2014-02-16 12:59:05 -08004479
4480 switch (key->addr.type) {
4481 case BDADDR_LE_PUBLIC:
4482 return true;
4483
4484 case BDADDR_LE_RANDOM:
4485 /* Two most significant bits shall be set */
4486 if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0)
4487 return false;
4488 return true;
4489 }
4490
4491 return false;
Johan Hedberg3f706b72013-01-20 14:27:16 +02004492}
4493
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004494static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004495 void *cp_data, u16 len)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004496{
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004497 struct mgmt_cp_load_long_term_keys *cp = cp_data;
4498 u16 key_count, expected_len;
Johan Hedberg715a5bf2013-01-09 15:29:34 +02004499 int i, err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004500
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07004501 BT_DBG("request for %s", hdev->name);
4502
4503 if (!lmp_le_capable(hdev))
4504 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
4505 MGMT_STATUS_NOT_SUPPORTED);
4506
Marcel Holtmann1f350c82012-03-12 20:31:08 -07004507 key_count = __le16_to_cpu(cp->key_count);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004508
4509 expected_len = sizeof(*cp) + key_count *
4510 sizeof(struct mgmt_ltk_info);
4511 if (expected_len != len) {
4512 BT_ERR("load_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02004513 expected_len, len);
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004514 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
Johan Hedberge57e6192013-01-20 14:27:14 +02004515 MGMT_STATUS_INVALID_PARAMS);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004516 }
4517
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004518 BT_DBG("%s key_count %u", hdev->name, key_count);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004519
Johan Hedberg54ad6d82013-01-20 14:27:15 +02004520 for (i = 0; i < key_count; i++) {
4521 struct mgmt_ltk_info *key = &cp->keys[i];
4522
Johan Hedberg3f706b72013-01-20 14:27:16 +02004523 if (!ltk_is_valid(key))
Johan Hedberg54ad6d82013-01-20 14:27:15 +02004524 return cmd_status(sk, hdev->id,
4525 MGMT_OP_LOAD_LONG_TERM_KEYS,
4526 MGMT_STATUS_INVALID_PARAMS);
4527 }
4528
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004529 hci_dev_lock(hdev);
4530
4531 hci_smp_ltks_clear(hdev);
4532
4533 for (i = 0; i < key_count; i++) {
4534 struct mgmt_ltk_info *key = &cp->keys[i];
Marcel Holtmann79d95a12013-10-13 03:57:38 -07004535 u8 type, addr_type;
4536
4537 if (key->addr.type == BDADDR_LE_PUBLIC)
4538 addr_type = ADDR_LE_DEV_PUBLIC;
4539 else
4540 addr_type = ADDR_LE_DEV_RANDOM;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004541
4542 if (key->master)
4543 type = HCI_SMP_LTK;
4544 else
4545 type = HCI_SMP_LTK_SLAVE;
4546
Johan Hedberg35d70272014-02-19 14:57:47 +02004547 hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type,
4548 key->type, key->val, key->enc_size, key->ediv,
4549 key->rand);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004550 }
4551
Johan Hedberg715a5bf2013-01-09 15:29:34 +02004552 err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
4553 NULL, 0);
4554
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004555 hci_dev_unlock(hdev);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004556
Johan Hedberg715a5bf2013-01-09 15:29:34 +02004557 return err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03004558}
4559
Andrei Emeltchenko2e3c35e2012-03-14 18:54:15 +02004560static const struct mgmt_handler {
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004561 int (*func) (struct sock *sk, struct hci_dev *hdev, void *data,
4562 u16 data_len);
Johan Hedbergbe22b542012-03-01 22:24:41 +02004563 bool var_len;
4564 size_t data_len;
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02004565} mgmt_handlers[] = {
4566 { NULL }, /* 0x0000 (no command) */
Johan Hedbergbe22b542012-03-01 22:24:41 +02004567 { read_version, false, MGMT_READ_VERSION_SIZE },
4568 { read_commands, false, MGMT_READ_COMMANDS_SIZE },
4569 { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE },
4570 { read_controller_info, false, MGMT_READ_INFO_SIZE },
4571 { set_powered, false, MGMT_SETTING_SIZE },
4572 { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE },
4573 { set_connectable, false, MGMT_SETTING_SIZE },
4574 { set_fast_connectable, false, MGMT_SETTING_SIZE },
4575 { set_pairable, false, MGMT_SETTING_SIZE },
4576 { set_link_security, false, MGMT_SETTING_SIZE },
4577 { set_ssp, false, MGMT_SETTING_SIZE },
4578 { set_hs, false, MGMT_SETTING_SIZE },
4579 { set_le, false, MGMT_SETTING_SIZE },
4580 { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE },
4581 { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE },
4582 { add_uuid, false, MGMT_ADD_UUID_SIZE },
4583 { remove_uuid, false, MGMT_REMOVE_UUID_SIZE },
4584 { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE },
4585 { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE },
4586 { disconnect, false, MGMT_DISCONNECT_SIZE },
4587 { get_connections, false, MGMT_GET_CONNECTIONS_SIZE },
4588 { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE },
4589 { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE },
4590 { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE },
4591 { pair_device, false, MGMT_PAIR_DEVICE_SIZE },
4592 { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE },
4593 { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE },
4594 { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE },
4595 { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
4596 { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE },
4597 { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
4598 { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE },
Marcel Holtmannec109112014-01-10 02:07:30 -08004599 { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE },
Johan Hedbergbe22b542012-03-01 22:24:41 +02004600 { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
4601 { start_discovery, false, MGMT_START_DISCOVERY_SIZE },
4602 { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE },
4603 { confirm_name, false, MGMT_CONFIRM_NAME_SIZE },
4604 { block_device, false, MGMT_BLOCK_DEVICE_SIZE },
4605 { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE },
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004606 { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE },
Johan Hedberg4375f102013-09-25 13:26:10 +03004607 { set_advertising, false, MGMT_SETTING_SIZE },
Johan Hedberg0663ca22013-10-02 13:43:14 +03004608 { set_bredr, false, MGMT_SETTING_SIZE },
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004609 { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE },
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004610 { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE },
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004611 { set_secure_conn, false, MGMT_SETTING_SIZE },
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08004612 { set_debug_keys, false, MGMT_SETTING_SIZE },
Johan Hedberg62b04cd2014-02-23 19:42:27 +02004613 { set_privacy, false, MGMT_SET_PRIVACY_SIZE },
Johan Hedberg41edf162014-02-18 10:19:35 +02004614 { load_irks, true, MGMT_LOAD_IRKS_SIZE },
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02004615};
4616
4617
Johan Hedberg03811012010-12-08 00:21:06 +02004618int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
4619{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004620 void *buf;
4621 u8 *cp;
Johan Hedberg03811012010-12-08 00:21:06 +02004622 struct mgmt_hdr *hdr;
Szymon Janc4e51eae2011-02-25 19:05:48 +01004623 u16 opcode, index, len;
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004624 struct hci_dev *hdev = NULL;
Andrei Emeltchenko2e3c35e2012-03-14 18:54:15 +02004625 const struct mgmt_handler *handler;
Johan Hedberg03811012010-12-08 00:21:06 +02004626 int err;
4627
4628 BT_DBG("got %zu bytes", msglen);
4629
4630 if (msglen < sizeof(*hdr))
4631 return -EINVAL;
4632
Gustavo F. Padovane63a15e2011-04-04 18:56:53 -03004633 buf = kmalloc(msglen, GFP_KERNEL);
Johan Hedberg03811012010-12-08 00:21:06 +02004634 if (!buf)
4635 return -ENOMEM;
4636
4637 if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) {
4638 err = -EFAULT;
4639 goto done;
4640 }
4641
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004642 hdr = buf;
Marcel Holtmann1f350c82012-03-12 20:31:08 -07004643 opcode = __le16_to_cpu(hdr->opcode);
4644 index = __le16_to_cpu(hdr->index);
4645 len = __le16_to_cpu(hdr->len);
Johan Hedberg03811012010-12-08 00:21:06 +02004646
4647 if (len != msglen - sizeof(*hdr)) {
4648 err = -EINVAL;
4649 goto done;
4650 }
4651
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02004652 if (index != MGMT_INDEX_NONE) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004653 hdev = hci_dev_get(index);
4654 if (!hdev) {
4655 err = cmd_status(sk, index, opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004656 MGMT_STATUS_INVALID_INDEX);
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004657 goto done;
4658 }
Marcel Holtmann0736cfa2013-08-26 21:40:51 -07004659
Johan Hedbergcebf4cf2013-10-10 18:06:04 +02004660 if (test_bit(HCI_SETUP, &hdev->dev_flags) ||
4661 test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
Marcel Holtmann0736cfa2013-08-26 21:40:51 -07004662 err = cmd_status(sk, index, opcode,
4663 MGMT_STATUS_INVALID_INDEX);
4664 goto done;
4665 }
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004666 }
4667
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02004668 if (opcode >= ARRAY_SIZE(mgmt_handlers) ||
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03004669 mgmt_handlers[opcode].func == NULL) {
Johan Hedberg03811012010-12-08 00:21:06 +02004670 BT_DBG("Unknown op %u", opcode);
Johan Hedbergca69b792011-11-11 18:10:00 +02004671 err = cmd_status(sk, index, opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004672 MGMT_STATUS_UNKNOWN_COMMAND);
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02004673 goto done;
Johan Hedberg03811012010-12-08 00:21:06 +02004674 }
4675
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02004676 if ((hdev && opcode < MGMT_OP_READ_INFO) ||
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03004677 (!hdev && opcode >= MGMT_OP_READ_INFO)) {
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02004678 err = cmd_status(sk, index, opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004679 MGMT_STATUS_INVALID_INDEX);
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02004680 goto done;
4681 }
4682
Johan Hedbergbe22b542012-03-01 22:24:41 +02004683 handler = &mgmt_handlers[opcode];
4684
4685 if ((handler->var_len && len < handler->data_len) ||
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03004686 (!handler->var_len && len != handler->data_len)) {
Johan Hedbergbe22b542012-03-01 22:24:41 +02004687 err = cmd_status(sk, index, opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004688 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergbe22b542012-03-01 22:24:41 +02004689 goto done;
4690 }
4691
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02004692 if (hdev)
4693 mgmt_init_hdev(sk, hdev);
4694
4695 cp = buf + sizeof(*hdr);
4696
Johan Hedbergbe22b542012-03-01 22:24:41 +02004697 err = handler->func(sk, hdev, cp, len);
Johan Hedberge41d8b42010-12-13 21:07:03 +02004698 if (err < 0)
4699 goto done;
4700
Johan Hedberg03811012010-12-08 00:21:06 +02004701 err = msglen;
4702
4703done:
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004704 if (hdev)
4705 hci_dev_put(hdev);
4706
Johan Hedberg03811012010-12-08 00:21:06 +02004707 kfree(buf);
4708 return err;
4709}
Johan Hedbergc71e97b2010-12-13 21:07:07 +02004710
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07004711void mgmt_index_added(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02004712{
Marcel Holtmann1514b892013-10-06 08:25:01 -07004713 if (hdev->dev_type != HCI_BREDR)
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07004714 return;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +03004715
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07004716 mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL);
Johan Hedbergc71e97b2010-12-13 21:07:07 +02004717}
4718
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07004719void mgmt_index_removed(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02004720{
Johan Hedberg5f159032012-03-02 03:13:19 +02004721 u8 status = MGMT_STATUS_INVALID_INDEX;
Johan Hedbergb24752f2011-11-03 14:40:33 +02004722
Marcel Holtmann1514b892013-10-06 08:25:01 -07004723 if (hdev->dev_type != HCI_BREDR)
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07004724 return;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +03004725
Johan Hedberg744cf192011-11-08 20:40:14 +02004726 mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status);
Johan Hedbergb24752f2011-11-03 14:40:33 +02004727
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07004728 mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02004729}
4730
Andre Guedes6046dc32014-02-26 20:21:51 -03004731/* This function requires the caller holds hdev->lock */
4732static void restart_le_auto_conns(struct hci_dev *hdev)
4733{
4734 struct hci_conn_params *p;
4735
4736 list_for_each_entry(p, &hdev->le_conn_params, list) {
4737 if (p->auto_connect == HCI_AUTO_CONN_ALWAYS)
4738 hci_pend_le_conn_add(hdev, &p->addr, p->addr_type);
4739 }
4740}
4741
Johan Hedberg229ab392013-03-15 17:06:53 -05004742static void powered_complete(struct hci_dev *hdev, u8 status)
4743{
4744 struct cmd_lookup match = { NULL, hdev };
4745
4746 BT_DBG("status 0x%02x", status);
4747
4748 hci_dev_lock(hdev);
4749
Andre Guedes6046dc32014-02-26 20:21:51 -03004750 restart_le_auto_conns(hdev);
4751
Johan Hedberg229ab392013-03-15 17:06:53 -05004752 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
4753
4754 new_settings(hdev, match.sk);
4755
4756 hci_dev_unlock(hdev);
4757
4758 if (match.sk)
4759 sock_put(match.sk);
4760}
4761
Johan Hedberg70da6242013-03-15 17:06:51 -05004762static int powered_update_hci(struct hci_dev *hdev)
4763{
Johan Hedberg890ea892013-03-15 17:06:52 -05004764 struct hci_request req;
Johan Hedberg70da6242013-03-15 17:06:51 -05004765 u8 link_sec;
4766
Johan Hedberg890ea892013-03-15 17:06:52 -05004767 hci_req_init(&req, hdev);
4768
Johan Hedberg70da6242013-03-15 17:06:51 -05004769 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) &&
4770 !lmp_host_ssp_capable(hdev)) {
4771 u8 ssp = 1;
4772
Johan Hedberg890ea892013-03-15 17:06:52 -05004773 hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp);
Johan Hedberg70da6242013-03-15 17:06:51 -05004774 }
4775
Johan Hedbergc73eee92013-04-19 18:35:21 +03004776 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) &&
4777 lmp_bredr_capable(hdev)) {
Johan Hedberg70da6242013-03-15 17:06:51 -05004778 struct hci_cp_write_le_host_supported cp;
4779
4780 cp.le = 1;
4781 cp.simul = lmp_le_br_capable(hdev);
4782
4783 /* Check first if we already have the right
4784 * host state (host features set)
4785 */
4786 if (cp.le != lmp_host_le_capable(hdev) ||
4787 cp.simul != lmp_host_le_br_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05004788 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED,
4789 sizeof(cp), &cp);
Johan Hedberg70da6242013-03-15 17:06:51 -05004790 }
4791
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004792 if (lmp_le_capable(hdev)) {
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07004793 /* Make sure the controller has a good default for
4794 * advertising data. This also applies to the case
4795 * where BR/EDR was toggled during the AUTO_OFF phase.
4796 */
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07004797 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07004798 update_adv_data(&req);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07004799 update_scan_rsp_data(&req);
4800 }
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07004801
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07004802 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
4803 enable_advertising(&req);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03004804 }
4805
Johan Hedberg70da6242013-03-15 17:06:51 -05004806 link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
4807 if (link_sec != test_bit(HCI_AUTH, &hdev->flags))
Johan Hedberg890ea892013-03-15 17:06:52 -05004808 hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE,
4809 sizeof(link_sec), &link_sec);
Johan Hedberg70da6242013-03-15 17:06:51 -05004810
4811 if (lmp_bredr_capable(hdev)) {
Johan Hedberg56f87902013-10-02 13:43:13 +03004812 if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
4813 set_bredr_scan(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05004814 update_class(&req);
Johan Hedberg13928972013-03-15 17:07:00 -05004815 update_name(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05004816 update_eir(&req);
Johan Hedberg70da6242013-03-15 17:06:51 -05004817 }
4818
Johan Hedberg229ab392013-03-15 17:06:53 -05004819 return hci_req_run(&req, powered_complete);
Johan Hedberg70da6242013-03-15 17:06:51 -05004820}
4821
Johan Hedberg744cf192011-11-08 20:40:14 +02004822int mgmt_powered(struct hci_dev *hdev, u8 powered)
Johan Hedberg5add6af2010-12-16 10:00:37 +02004823{
Johan Hedberg76a7f3a2012-02-17 00:34:40 +02004824 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg229ab392013-03-15 17:06:53 -05004825 u8 status_not_powered = MGMT_STATUS_NOT_POWERED;
4826 u8 zero_cod[] = { 0, 0, 0 };
Johan Hedberg7bb895d2012-02-17 01:20:00 +02004827 int err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02004828
Johan Hedberg5e5282b2012-02-21 16:01:30 +02004829 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
4830 return 0;
4831
Johan Hedberg5e5282b2012-02-21 16:01:30 +02004832 if (powered) {
Johan Hedberg229ab392013-03-15 17:06:53 -05004833 if (powered_update_hci(hdev) == 0)
4834 return 0;
Johan Hedbergfe038882013-01-16 16:15:34 +02004835
Johan Hedberg229ab392013-03-15 17:06:53 -05004836 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp,
4837 &match);
4838 goto new_settings;
Johan Hedbergb24752f2011-11-03 14:40:33 +02004839 }
4840
Johan Hedberg229ab392013-03-15 17:06:53 -05004841 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
4842 mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered);
4843
4844 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0)
4845 mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
4846 zero_cod, sizeof(zero_cod), NULL);
4847
4848new_settings:
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02004849 err = new_settings(hdev, match.sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02004850
4851 if (match.sk)
4852 sock_put(match.sk);
4853
Johan Hedberg7bb895d2012-02-17 01:20:00 +02004854 return err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02004855}
Johan Hedberg73f22f62010-12-29 16:00:25 +02004856
Marcel Holtmann3eec7052013-10-06 23:55:46 -07004857void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
Johan Hedberg96570ff2013-05-29 09:51:29 +03004858{
4859 struct pending_cmd *cmd;
4860 u8 status;
4861
4862 cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev);
4863 if (!cmd)
Marcel Holtmann3eec7052013-10-06 23:55:46 -07004864 return;
Johan Hedberg96570ff2013-05-29 09:51:29 +03004865
4866 if (err == -ERFKILL)
4867 status = MGMT_STATUS_RFKILLED;
4868 else
4869 status = MGMT_STATUS_FAILED;
4870
Marcel Holtmann3eec7052013-10-06 23:55:46 -07004871 cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
Johan Hedberg96570ff2013-05-29 09:51:29 +03004872
4873 mgmt_pending_remove(cmd);
Johan Hedberg96570ff2013-05-29 09:51:29 +03004874}
4875
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07004876void mgmt_discoverable_timeout(struct hci_dev *hdev)
4877{
4878 struct hci_request req;
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07004879
4880 hci_dev_lock(hdev);
4881
4882 /* When discoverable timeout triggers, then just make sure
4883 * the limited discoverable flag is cleared. Even in the case
4884 * of a timeout triggered from general discoverable, it is
4885 * safe to unconditionally clear the flag.
4886 */
4887 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
Johan Hedberg9a43e252013-10-20 19:00:07 +03004888 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07004889
4890 hci_req_init(&req, hdev);
Johan Hedberg4b580612013-10-19 23:38:21 +03004891 if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
4892 u8 scan = SCAN_PAGE;
4893 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE,
4894 sizeof(scan), &scan);
4895 }
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07004896 update_class(&req);
Johan Hedberg9a43e252013-10-20 19:00:07 +03004897 update_adv_data(&req);
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07004898 hci_req_run(&req, NULL);
4899
4900 hdev->discov_timeout = 0;
4901
Johan Hedberg9a43e252013-10-20 19:00:07 +03004902 new_settings(hdev, NULL);
4903
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07004904 hci_dev_unlock(hdev);
4905}
4906
Marcel Holtmann86a75642013-10-15 06:33:54 -07004907void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable)
Johan Hedberg73f22f62010-12-29 16:00:25 +02004908{
Marcel Holtmann86a75642013-10-15 06:33:54 -07004909 bool changed;
Johan Hedberg73f22f62010-12-29 16:00:25 +02004910
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03004911 /* Nothing needed here if there's a pending command since that
4912 * commands request completion callback takes care of everything
4913 * necessary.
4914 */
4915 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev))
Marcel Holtmann86a75642013-10-15 06:33:54 -07004916 return;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03004917
Johan Hedbergbd107992014-02-24 14:52:19 +02004918 /* Powering off may clear the scan mode - don't let that interfere */
4919 if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev))
4920 return;
4921
Johan Hedberg9a43e252013-10-20 19:00:07 +03004922 if (discoverable) {
Marcel Holtmann86a75642013-10-15 06:33:54 -07004923 changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
Johan Hedberg9a43e252013-10-20 19:00:07 +03004924 } else {
4925 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
Marcel Holtmann86a75642013-10-15 06:33:54 -07004926 changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
Johan Hedberg9a43e252013-10-20 19:00:07 +03004927 }
Johan Hedberg73f22f62010-12-29 16:00:25 +02004928
Johan Hedberg9a43e252013-10-20 19:00:07 +03004929 if (changed) {
4930 struct hci_request req;
4931
4932 /* In case this change in discoverable was triggered by
4933 * a disabling of connectable there could be a need to
4934 * update the advertising flags.
4935 */
4936 hci_req_init(&req, hdev);
4937 update_adv_data(&req);
4938 hci_req_run(&req, NULL);
4939
Marcel Holtmann86a75642013-10-15 06:33:54 -07004940 new_settings(hdev, NULL);
Johan Hedberg9a43e252013-10-20 19:00:07 +03004941 }
Johan Hedberg73f22f62010-12-29 16:00:25 +02004942}
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02004943
Marcel Holtmanna3309162013-10-15 06:33:55 -07004944void mgmt_connectable(struct hci_dev *hdev, u8 connectable)
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02004945{
Marcel Holtmanna3309162013-10-15 06:33:55 -07004946 bool changed;
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02004947
Johan Hedbergd7b856f2013-10-14 16:20:04 +03004948 /* Nothing needed here if there's a pending command since that
4949 * commands request completion callback takes care of everything
4950 * necessary.
4951 */
4952 if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev))
Marcel Holtmanna3309162013-10-15 06:33:55 -07004953 return;
Johan Hedbergd7b856f2013-10-14 16:20:04 +03004954
Johan Hedbergce3f24c2014-02-24 14:52:20 +02004955 /* Powering off may clear the scan mode - don't let that interfere */
4956 if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev))
4957 return;
4958
Marcel Holtmanna3309162013-10-15 06:33:55 -07004959 if (connectable)
4960 changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
4961 else
4962 changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02004963
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02004964 if (changed)
Marcel Holtmanna3309162013-10-15 06:33:55 -07004965 new_settings(hdev, NULL);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02004966}
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004967
Johan Hedberg778b2352014-02-24 14:52:17 +02004968void mgmt_advertising(struct hci_dev *hdev, u8 advertising)
4969{
Johan Hedberg7c4cfab2014-02-24 14:52:21 +02004970 /* Powering off may stop advertising - don't let that interfere */
4971 if (!advertising && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev))
4972 return;
4973
Johan Hedberg778b2352014-02-24 14:52:17 +02004974 if (advertising)
4975 set_bit(HCI_ADVERTISING, &hdev->dev_flags);
4976 else
4977 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
4978}
4979
Marcel Holtmann4796e8a2013-10-15 06:33:56 -07004980void mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status)
Johan Hedberg2d7cee52011-11-07 22:16:03 +02004981{
Johan Hedbergca69b792011-11-11 18:10:00 +02004982 u8 mgmt_err = mgmt_status(status);
4983
Johan Hedberg2d7cee52011-11-07 22:16:03 +02004984 if (scan & SCAN_PAGE)
Johan Hedberg744cf192011-11-08 20:40:14 +02004985 mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004986 cmd_status_rsp, &mgmt_err);
Johan Hedberg2d7cee52011-11-07 22:16:03 +02004987
4988 if (scan & SCAN_INQUIRY)
Johan Hedberg744cf192011-11-08 20:40:14 +02004989 mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004990 cmd_status_rsp, &mgmt_err);
Johan Hedberg2d7cee52011-11-07 22:16:03 +02004991}
4992
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07004993void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
4994 bool persistent)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004995{
Johan Hedberg86742e12011-11-07 23:13:38 +02004996 struct mgmt_ev_new_link_key ev;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004997
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03004998 memset(&ev, 0, sizeof(ev));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004999
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03005000 ev.store_hint = persistent;
Johan Hedbergd753fdc2012-02-17 14:06:34 +02005001 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03005002 ev.key.addr.type = BDADDR_BREDR;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03005003 ev.key.type = key->type;
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03005004 memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03005005 ev.key.pin_len = key->pin_len;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02005006
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07005007 mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02005008}
Johan Hedbergf7520542011-01-20 12:34:39 +02005009
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07005010void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005011{
5012 struct mgmt_ev_new_long_term_key ev;
5013
5014 memset(&ev, 0, sizeof(ev));
5015
Marcel Holtmann5192d302014-02-19 17:11:58 -08005016 /* Devices using resolvable or non-resolvable random addresses
5017 * without providing an indentity resolving key don't require
5018 * to store long term keys. Their addresses will change the
5019 * next time around.
5020 *
5021 * Only when a remote device provides an identity address
5022 * make sure the long term key is stored. If the remote
5023 * identity is known, the long term keys are internally
5024 * mapped to the identity address. So allow static random
5025 * and public addresses here.
5026 */
Johan Hedbergba74b662014-02-19 14:57:45 +02005027 if (key->bdaddr_type == ADDR_LE_DEV_RANDOM &&
5028 (key->bdaddr.b[5] & 0xc0) != 0xc0)
5029 ev.store_hint = 0x00;
5030 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07005031 ev.store_hint = persistent;
Johan Hedbergba74b662014-02-19 14:57:45 +02005032
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005033 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03005034 ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
Marcel Holtmannd40f3ee2014-01-31 18:42:17 -08005035 ev.key.type = key->authenticated;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005036 ev.key.enc_size = key->enc_size;
5037 ev.key.ediv = key->ediv;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08005038 ev.key.rand = key->rand;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005039
5040 if (key->type == HCI_SMP_LTK)
5041 ev.key.master = 1;
5042
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005043 memcpy(ev.key.val, key->val, sizeof(key->val));
5044
Marcel Holtmann083368f2013-10-15 14:26:29 -07005045 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005046}
5047
Johan Hedberg95fbac82014-02-19 15:18:31 +02005048void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk)
5049{
5050 struct mgmt_ev_new_irk ev;
5051
5052 memset(&ev, 0, sizeof(ev));
5053
Marcel Holtmannbab6d1e2014-02-19 11:51:54 -08005054 /* For identity resolving keys from devices that are already
5055 * using a public address or static random address, do not
5056 * ask for storing this key. The identity resolving key really
5057 * is only mandatory for devices using resovlable random
5058 * addresses.
5059 *
5060 * Storing all identity resolving keys has the downside that
5061 * they will be also loaded on next boot of they system. More
5062 * identity resolving keys, means more time during scanning is
5063 * needed to actually resolve these addresses.
5064 */
5065 if (bacmp(&irk->rpa, BDADDR_ANY))
5066 ev.store_hint = 0x01;
5067 else
5068 ev.store_hint = 0x00;
5069
Johan Hedberg95fbac82014-02-19 15:18:31 +02005070 bacpy(&ev.rpa, &irk->rpa);
5071 bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr);
5072 ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type);
5073 memcpy(ev.irk.val, irk->val, sizeof(irk->val));
5074
5075 mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
5076}
5077
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07005078void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
5079 bool persistent)
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07005080{
5081 struct mgmt_ev_new_csrk ev;
5082
5083 memset(&ev, 0, sizeof(ev));
5084
5085 /* Devices using resolvable or non-resolvable random addresses
5086 * without providing an indentity resolving key don't require
5087 * to store signature resolving keys. Their addresses will change
5088 * the next time around.
5089 *
5090 * Only when a remote device provides an identity address
5091 * make sure the signature resolving key is stored. So allow
5092 * static random and public addresses here.
5093 */
5094 if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM &&
5095 (csrk->bdaddr.b[5] & 0xc0) != 0xc0)
5096 ev.store_hint = 0x00;
5097 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07005098 ev.store_hint = persistent;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07005099
5100 bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
5101 ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
5102 ev.key.master = csrk->master;
5103 memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
5104
5105 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
5106}
5107
Marcel Holtmann94933992013-10-15 10:26:39 -07005108static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
5109 u8 data_len)
5110{
5111 eir[eir_len++] = sizeof(type) + data_len;
5112 eir[eir_len++] = type;
5113 memcpy(&eir[eir_len], data, data_len);
5114 eir_len += data_len;
5115
5116 return eir_len;
5117}
5118
Marcel Holtmannecd90ae2013-10-06 23:55:49 -07005119void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
5120 u8 addr_type, u32 flags, u8 *name, u8 name_len,
5121 u8 *dev_class)
Johan Hedbergf7520542011-01-20 12:34:39 +02005122{
Johan Hedbergb644ba32012-01-17 21:48:47 +02005123 char buf[512];
5124 struct mgmt_ev_device_connected *ev = (void *) buf;
5125 u16 eir_len = 0;
Johan Hedbergf7520542011-01-20 12:34:39 +02005126
Johan Hedbergb644ba32012-01-17 21:48:47 +02005127 bacpy(&ev->addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03005128 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergf7520542011-01-20 12:34:39 +02005129
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02005130 ev->flags = __cpu_to_le32(flags);
Johan Hedberg08c79b62012-02-23 22:31:51 +02005131
Johan Hedbergb644ba32012-01-17 21:48:47 +02005132 if (name_len > 0)
5133 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005134 name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02005135
5136 if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0)
Brian Gix53156382012-03-09 14:07:03 -08005137 eir_len = eir_append_data(ev->eir, eir_len,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005138 EIR_CLASS_OF_DEV, dev_class, 3);
Johan Hedbergb644ba32012-01-17 21:48:47 +02005139
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02005140 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02005141
Marcel Holtmannecd90ae2013-10-06 23:55:49 -07005142 mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf,
5143 sizeof(*ev) + eir_len, NULL);
Johan Hedbergf7520542011-01-20 12:34:39 +02005144}
5145
Johan Hedberg8962ee72011-01-20 12:40:27 +02005146static void disconnect_rsp(struct pending_cmd *cmd, void *data)
5147{
Szymon Jancc68fb7f2011-03-22 13:12:19 +01005148 struct mgmt_cp_disconnect *cp = cmd->param;
Johan Hedberg8962ee72011-01-20 12:40:27 +02005149 struct sock **sk = data;
Johan Hedberga38528f2011-01-22 06:46:43 +02005150 struct mgmt_rp_disconnect rp;
Johan Hedberg8962ee72011-01-20 12:40:27 +02005151
Johan Hedberg88c3df12012-02-09 14:27:38 +02005152 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5153 rp.addr.type = cp->addr.type;
Johan Hedberg8962ee72011-01-20 12:40:27 +02005154
Johan Hedbergaee9b212012-02-18 15:07:59 +02005155 cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005156 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02005157
5158 *sk = cmd->sk;
5159 sock_hold(*sk);
5160
Johan Hedberga664b5b2011-02-19 12:06:02 -03005161 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02005162}
5163
Johan Hedberg124f6e32012-02-09 13:50:12 +02005164static void unpair_device_rsp(struct pending_cmd *cmd, void *data)
Johan Hedberga8a1d192011-11-10 15:54:38 +02005165{
Johan Hedbergb1078ad2012-02-09 17:21:16 +02005166 struct hci_dev *hdev = data;
Johan Hedberg124f6e32012-02-09 13:50:12 +02005167 struct mgmt_cp_unpair_device *cp = cmd->param;
5168 struct mgmt_rp_unpair_device rp;
Johan Hedberga8a1d192011-11-10 15:54:38 +02005169
5170 memset(&rp, 0, sizeof(rp));
Johan Hedberg124f6e32012-02-09 13:50:12 +02005171 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5172 rp.addr.type = cp->addr.type;
Johan Hedberga8a1d192011-11-10 15:54:38 +02005173
Johan Hedbergb1078ad2012-02-09 17:21:16 +02005174 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
5175
Johan Hedbergaee9b212012-02-18 15:07:59 +02005176 cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp));
Johan Hedberga8a1d192011-11-10 15:54:38 +02005177
5178 mgmt_pending_remove(cmd);
5179}
5180
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07005181void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02005182 u8 link_type, u8 addr_type, u8 reason,
5183 bool mgmt_connected)
Johan Hedbergf7520542011-01-20 12:34:39 +02005184{
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02005185 struct mgmt_ev_device_disconnected ev;
Johan Hedberg8b064a32014-02-24 14:52:22 +02005186 struct pending_cmd *power_off;
Johan Hedberg8962ee72011-01-20 12:40:27 +02005187 struct sock *sk = NULL;
Johan Hedberg8962ee72011-01-20 12:40:27 +02005188
Johan Hedberg8b064a32014-02-24 14:52:22 +02005189 power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev);
5190 if (power_off) {
5191 struct mgmt_mode *cp = power_off->param;
5192
5193 /* The connection is still in hci_conn_hash so test for 1
5194 * instead of 0 to know if this is the last one.
5195 */
Johan Hedberga3172b72014-02-28 09:33:44 +02005196 if (!cp->val && hci_conn_count(hdev) == 1) {
5197 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02005198 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberga3172b72014-02-28 09:33:44 +02005199 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02005200 }
5201
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02005202 if (!mgmt_connected)
5203 return;
5204
Andre Guedes57eb7762013-10-30 19:01:41 -03005205 if (link_type != ACL_LINK && link_type != LE_LINK)
5206 return;
5207
Johan Hedberg744cf192011-11-08 20:40:14 +02005208 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
Johan Hedbergf7520542011-01-20 12:34:39 +02005209
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02005210 bacpy(&ev.addr.bdaddr, bdaddr);
5211 ev.addr.type = link_to_bdaddr(link_type, addr_type);
5212 ev.reason = reason;
Johan Hedbergf7520542011-01-20 12:34:39 +02005213
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07005214 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02005215
5216 if (sk)
Szymon Jancd97dcb62012-03-16 16:02:56 +01005217 sock_put(sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02005218
Johan Hedberg124f6e32012-02-09 13:50:12 +02005219 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005220 hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02005221}
5222
Marcel Holtmann78929242013-10-06 23:55:47 -07005223void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
5224 u8 link_type, u8 addr_type, u8 status)
Johan Hedberg8962ee72011-01-20 12:40:27 +02005225{
Andre Guedes3655bba2013-10-30 19:01:40 -03005226 u8 bdaddr_type = link_to_bdaddr(link_type, addr_type);
5227 struct mgmt_cp_disconnect *cp;
Johan Hedberg88c3df12012-02-09 14:27:38 +02005228 struct mgmt_rp_disconnect rp;
Johan Hedberg8962ee72011-01-20 12:40:27 +02005229 struct pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02005230
Jefferson Delfes36a75f12012-09-18 13:36:54 -04005231 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
5232 hdev);
5233
Johan Hedberg2e58ef32011-11-08 20:40:15 +02005234 cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02005235 if (!cmd)
Marcel Holtmann78929242013-10-06 23:55:47 -07005236 return;
Johan Hedberg8962ee72011-01-20 12:40:27 +02005237
Andre Guedes3655bba2013-10-30 19:01:40 -03005238 cp = cmd->param;
5239
5240 if (bacmp(bdaddr, &cp->addr.bdaddr))
5241 return;
5242
5243 if (cp->addr.type != bdaddr_type)
5244 return;
5245
Johan Hedberg88c3df12012-02-09 14:27:38 +02005246 bacpy(&rp.addr.bdaddr, bdaddr);
Andre Guedes3655bba2013-10-30 19:01:40 -03005247 rp.addr.type = bdaddr_type;
Johan Hedberg37d9ef72011-11-10 15:54:39 +02005248
Marcel Holtmann78929242013-10-06 23:55:47 -07005249 cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT,
5250 mgmt_status(status), &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02005251
Johan Hedberga664b5b2011-02-19 12:06:02 -03005252 mgmt_pending_remove(cmd);
Johan Hedbergf7520542011-01-20 12:34:39 +02005253}
Johan Hedberg17d5c042011-01-22 06:09:08 +02005254
Marcel Holtmann445608d2013-10-06 23:55:48 -07005255void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
5256 u8 addr_type, u8 status)
Johan Hedberg17d5c042011-01-22 06:09:08 +02005257{
5258 struct mgmt_ev_connect_failed ev;
Johan Hedbergc9910d02014-02-27 14:35:12 +02005259 struct pending_cmd *power_off;
5260
5261 power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev);
5262 if (power_off) {
5263 struct mgmt_mode *cp = power_off->param;
5264
5265 /* The connection is still in hci_conn_hash so test for 1
5266 * instead of 0 to know if this is the last one.
5267 */
Johan Hedberga3172b72014-02-28 09:33:44 +02005268 if (!cp->val && hci_conn_count(hdev) == 1) {
5269 cancel_delayed_work(&hdev->power_off);
Johan Hedbergc9910d02014-02-27 14:35:12 +02005270 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberga3172b72014-02-28 09:33:44 +02005271 }
Johan Hedbergc9910d02014-02-27 14:35:12 +02005272 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02005273
Johan Hedberg4c659c32011-11-07 23:13:39 +02005274 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03005275 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergca69b792011-11-11 18:10:00 +02005276 ev.status = mgmt_status(status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02005277
Marcel Holtmann445608d2013-10-06 23:55:48 -07005278 mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg17d5c042011-01-22 06:09:08 +02005279}
Johan Hedberg980e1a52011-01-22 06:10:07 +02005280
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07005281void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
Johan Hedberg980e1a52011-01-22 06:10:07 +02005282{
5283 struct mgmt_ev_pin_code_request ev;
5284
Johan Hedbergd8457692012-02-17 14:24:57 +02005285 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03005286 ev.addr.type = BDADDR_BREDR;
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02005287 ev.secure = secure;
Johan Hedberg980e1a52011-01-22 06:10:07 +02005288
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07005289 mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg980e1a52011-01-22 06:10:07 +02005290}
5291
Marcel Holtmanne669cf82013-10-15 14:26:21 -07005292void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
5293 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02005294{
5295 struct pending_cmd *cmd;
Johan Hedbergac56fb12011-02-19 12:05:59 -03005296 struct mgmt_rp_pin_code_reply rp;
Johan Hedberg980e1a52011-01-22 06:10:07 +02005297
Johan Hedberg2e58ef32011-11-08 20:40:15 +02005298 cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02005299 if (!cmd)
Marcel Holtmanne669cf82013-10-15 14:26:21 -07005300 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02005301
Johan Hedbergd8457692012-02-17 14:24:57 +02005302 bacpy(&rp.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03005303 rp.addr.type = BDADDR_BREDR;
Johan Hedbergac56fb12011-02-19 12:05:59 -03005304
Marcel Holtmanne669cf82013-10-15 14:26:21 -07005305 cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
5306 mgmt_status(status), &rp, sizeof(rp));
Johan Hedberg980e1a52011-01-22 06:10:07 +02005307
Johan Hedberga664b5b2011-02-19 12:06:02 -03005308 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02005309}
5310
Marcel Holtmann3eb38522013-10-15 14:26:22 -07005311void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
5312 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02005313{
5314 struct pending_cmd *cmd;
Johan Hedbergac56fb12011-02-19 12:05:59 -03005315 struct mgmt_rp_pin_code_reply rp;
Johan Hedberg980e1a52011-01-22 06:10:07 +02005316
Johan Hedberg2e58ef32011-11-08 20:40:15 +02005317 cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02005318 if (!cmd)
Marcel Holtmann3eb38522013-10-15 14:26:22 -07005319 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02005320
Johan Hedbergd8457692012-02-17 14:24:57 +02005321 bacpy(&rp.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03005322 rp.addr.type = BDADDR_BREDR;
Johan Hedbergac56fb12011-02-19 12:05:59 -03005323
Marcel Holtmann3eb38522013-10-15 14:26:22 -07005324 cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY,
5325 mgmt_status(status), &rp, sizeof(rp));
Johan Hedberg980e1a52011-01-22 06:10:07 +02005326
Johan Hedberga664b5b2011-02-19 12:06:02 -03005327 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02005328}
Johan Hedberga5c29682011-02-19 12:05:57 -03005329
Johan Hedberg744cf192011-11-08 20:40:14 +02005330int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg39adbff2014-03-20 08:18:14 +02005331 u8 link_type, u8 addr_type, u32 value,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005332 u8 confirm_hint)
Johan Hedberga5c29682011-02-19 12:05:57 -03005333{
5334 struct mgmt_ev_user_confirm_request ev;
5335
Johan Hedberg744cf192011-11-08 20:40:14 +02005336 BT_DBG("%s", hdev->name);
Johan Hedberga5c29682011-02-19 12:05:57 -03005337
Johan Hedberg272d90d2012-02-09 15:26:12 +02005338 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03005339 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberg55bc1a32011-04-28 11:28:56 -07005340 ev.confirm_hint = confirm_hint;
Johan Hedberg39adbff2014-03-20 08:18:14 +02005341 ev.value = cpu_to_le32(value);
Johan Hedberga5c29682011-02-19 12:05:57 -03005342
Johan Hedberg744cf192011-11-08 20:40:14 +02005343 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005344 NULL);
Johan Hedberga5c29682011-02-19 12:05:57 -03005345}
5346
Johan Hedberg272d90d2012-02-09 15:26:12 +02005347int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03005348 u8 link_type, u8 addr_type)
Brian Gix604086b2011-11-23 08:28:33 -08005349{
5350 struct mgmt_ev_user_passkey_request ev;
5351
5352 BT_DBG("%s", hdev->name);
5353
Johan Hedberg272d90d2012-02-09 15:26:12 +02005354 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03005355 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Brian Gix604086b2011-11-23 08:28:33 -08005356
5357 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005358 NULL);
Brian Gix604086b2011-11-23 08:28:33 -08005359}
5360
Brian Gix0df4c182011-11-16 13:53:13 -08005361static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03005362 u8 link_type, u8 addr_type, u8 status,
5363 u8 opcode)
Johan Hedberga5c29682011-02-19 12:05:57 -03005364{
5365 struct pending_cmd *cmd;
5366 struct mgmt_rp_user_confirm_reply rp;
5367 int err;
5368
Johan Hedberg2e58ef32011-11-08 20:40:15 +02005369 cmd = mgmt_pending_find(opcode, hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03005370 if (!cmd)
5371 return -ENOENT;
5372
Johan Hedberg272d90d2012-02-09 15:26:12 +02005373 bacpy(&rp.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03005374 rp.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergaee9b212012-02-18 15:07:59 +02005375 err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005376 &rp, sizeof(rp));
Johan Hedberga5c29682011-02-19 12:05:57 -03005377
Johan Hedberga664b5b2011-02-19 12:06:02 -03005378 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03005379
5380 return err;
5381}
5382
Johan Hedberg744cf192011-11-08 20:40:14 +02005383int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005384 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03005385{
Johan Hedberg272d90d2012-02-09 15:26:12 +02005386 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005387 status, MGMT_OP_USER_CONFIRM_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03005388}
5389
Johan Hedberg272d90d2012-02-09 15:26:12 +02005390int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005391 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03005392{
Johan Hedberg272d90d2012-02-09 15:26:12 +02005393 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03005394 status,
5395 MGMT_OP_USER_CONFIRM_NEG_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03005396}
Johan Hedberg2a611692011-02-19 12:06:00 -03005397
Brian Gix604086b2011-11-23 08:28:33 -08005398int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005399 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08005400{
Johan Hedberg272d90d2012-02-09 15:26:12 +02005401 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005402 status, MGMT_OP_USER_PASSKEY_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08005403}
5404
Johan Hedberg272d90d2012-02-09 15:26:12 +02005405int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005406 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08005407{
Johan Hedberg272d90d2012-02-09 15:26:12 +02005408 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03005409 status,
5410 MGMT_OP_USER_PASSKEY_NEG_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08005411}
5412
Johan Hedberg92a25252012-09-06 18:39:26 +03005413int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
5414 u8 link_type, u8 addr_type, u32 passkey,
5415 u8 entered)
5416{
5417 struct mgmt_ev_passkey_notify ev;
5418
5419 BT_DBG("%s", hdev->name);
5420
5421 bacpy(&ev.addr.bdaddr, bdaddr);
5422 ev.addr.type = link_to_bdaddr(link_type, addr_type);
5423 ev.passkey = __cpu_to_le32(passkey);
5424 ev.entered = entered;
5425
5426 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
5427}
5428
Marcel Holtmanne5460992013-10-15 14:26:23 -07005429void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
5430 u8 addr_type, u8 status)
Johan Hedberg2a611692011-02-19 12:06:00 -03005431{
5432 struct mgmt_ev_auth_failed ev;
5433
Johan Hedbergbab73cb2012-02-09 16:07:29 +02005434 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03005435 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergca69b792011-11-11 18:10:00 +02005436 ev.status = mgmt_status(status);
Johan Hedberg2a611692011-02-19 12:06:00 -03005437
Marcel Holtmanne5460992013-10-15 14:26:23 -07005438 mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg2a611692011-02-19 12:06:00 -03005439}
Johan Hedbergb312b1612011-03-16 14:29:37 +02005440
Marcel Holtmann464996a2013-10-15 14:26:24 -07005441void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02005442{
5443 struct cmd_lookup match = { NULL, hdev };
Marcel Holtmann464996a2013-10-15 14:26:24 -07005444 bool changed;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02005445
5446 if (status) {
5447 u8 mgmt_err = mgmt_status(status);
5448 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005449 cmd_status_rsp, &mgmt_err);
Marcel Holtmann464996a2013-10-15 14:26:24 -07005450 return;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02005451 }
5452
Marcel Holtmann464996a2013-10-15 14:26:24 -07005453 if (test_bit(HCI_AUTH, &hdev->flags))
5454 changed = !test_and_set_bit(HCI_LINK_SECURITY,
5455 &hdev->dev_flags);
5456 else
5457 changed = test_and_clear_bit(HCI_LINK_SECURITY,
5458 &hdev->dev_flags);
Johan Hedberg47990ea2012-02-22 11:58:37 +02005459
Johan Hedberg33ef95e2012-02-16 23:56:27 +02005460 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005461 &match);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02005462
Johan Hedberg47990ea2012-02-22 11:58:37 +02005463 if (changed)
Marcel Holtmann464996a2013-10-15 14:26:24 -07005464 new_settings(hdev, match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02005465
5466 if (match.sk)
5467 sock_put(match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02005468}
5469
Johan Hedberg890ea892013-03-15 17:06:52 -05005470static void clear_eir(struct hci_request *req)
Johan Hedbergcacaf522012-02-21 00:52:42 +02005471{
Johan Hedberg890ea892013-03-15 17:06:52 -05005472 struct hci_dev *hdev = req->hdev;
Johan Hedbergcacaf522012-02-21 00:52:42 +02005473 struct hci_cp_write_eir cp;
5474
Johan Hedberg976eb202012-10-24 21:12:01 +03005475 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05005476 return;
Johan Hedbergcacaf522012-02-21 00:52:42 +02005477
Johan Hedbergc80da272012-02-22 15:38:48 +02005478 memset(hdev->eir, 0, sizeof(hdev->eir));
5479
Johan Hedbergcacaf522012-02-21 00:52:42 +02005480 memset(&cp, 0, sizeof(cp));
5481
Johan Hedberg890ea892013-03-15 17:06:52 -05005482 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedbergcacaf522012-02-21 00:52:42 +02005483}
5484
Marcel Holtmann3e248562013-10-15 14:26:25 -07005485void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02005486{
5487 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg890ea892013-03-15 17:06:52 -05005488 struct hci_request req;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02005489 bool changed = false;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02005490
5491 if (status) {
5492 u8 mgmt_err = mgmt_status(status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02005493
5494 if (enable && test_and_clear_bit(HCI_SSP_ENABLED,
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07005495 &hdev->dev_flags)) {
5496 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
Marcel Holtmann3e248562013-10-15 14:26:25 -07005497 new_settings(hdev, NULL);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07005498 }
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02005499
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005500 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
5501 &mgmt_err);
Marcel Holtmann3e248562013-10-15 14:26:25 -07005502 return;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02005503 }
5504
5505 if (enable) {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07005506 changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02005507 } else {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07005508 changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
5509 if (!changed)
5510 changed = test_and_clear_bit(HCI_HS_ENABLED,
5511 &hdev->dev_flags);
5512 else
5513 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02005514 }
5515
5516 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
5517
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02005518 if (changed)
Marcel Holtmann3e248562013-10-15 14:26:25 -07005519 new_settings(hdev, match.sk);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02005520
Johan Hedberg5fc6ebb2012-02-22 15:10:59 +02005521 if (match.sk)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02005522 sock_put(match.sk);
5523
Johan Hedberg890ea892013-03-15 17:06:52 -05005524 hci_req_init(&req, hdev);
5525
Johan Hedberg5fc6ebb2012-02-22 15:10:59 +02005526 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
Johan Hedberg890ea892013-03-15 17:06:52 -05005527 update_eir(&req);
Johan Hedberg5fc6ebb2012-02-22 15:10:59 +02005528 else
Johan Hedberg890ea892013-03-15 17:06:52 -05005529 clear_eir(&req);
5530
5531 hci_req_run(&req, NULL);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02005532}
5533
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005534void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
5535{
5536 struct cmd_lookup match = { NULL, hdev };
5537 bool changed = false;
5538
5539 if (status) {
5540 u8 mgmt_err = mgmt_status(status);
5541
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005542 if (enable) {
5543 if (test_and_clear_bit(HCI_SC_ENABLED,
5544 &hdev->dev_flags))
5545 new_settings(hdev, NULL);
5546 clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
5547 }
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005548
5549 mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev,
5550 cmd_status_rsp, &mgmt_err);
5551 return;
5552 }
5553
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005554 if (enable) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005555 changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005556 } else {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005557 changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005558 clear_bit(HCI_SC_ONLY, &hdev->dev_flags);
5559 }
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005560
5561 mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev,
5562 settings_rsp, &match);
5563
5564 if (changed)
5565 new_settings(hdev, match.sk);
5566
5567 if (match.sk)
5568 sock_put(match.sk);
5569}
5570
Johan Hedberg92da6092013-03-15 17:06:55 -05005571static void sk_lookup(struct pending_cmd *cmd, void *data)
Johan Hedberg90e70452012-02-23 23:09:40 +02005572{
5573 struct cmd_lookup *match = data;
5574
Johan Hedberg90e70452012-02-23 23:09:40 +02005575 if (match->sk == NULL) {
5576 match->sk = cmd->sk;
5577 sock_hold(match->sk);
5578 }
Johan Hedberg90e70452012-02-23 23:09:40 +02005579}
5580
Marcel Holtmann4e1b0242013-10-15 14:26:26 -07005581void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
5582 u8 status)
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01005583{
Johan Hedberg90e70452012-02-23 23:09:40 +02005584 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01005585
Johan Hedberg92da6092013-03-15 17:06:55 -05005586 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
5587 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
5588 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
Johan Hedberg90e70452012-02-23 23:09:40 +02005589
5590 if (!status)
Marcel Holtmann4e1b0242013-10-15 14:26:26 -07005591 mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3,
5592 NULL);
Johan Hedberg90e70452012-02-23 23:09:40 +02005593
5594 if (match.sk)
5595 sock_put(match.sk);
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01005596}
5597
Marcel Holtmann7667da32013-10-15 14:26:27 -07005598void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
Johan Hedbergb312b1612011-03-16 14:29:37 +02005599{
Johan Hedbergb312b1612011-03-16 14:29:37 +02005600 struct mgmt_cp_set_local_name ev;
Johan Hedberg13928972013-03-15 17:07:00 -05005601 struct pending_cmd *cmd;
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02005602
Johan Hedberg13928972013-03-15 17:07:00 -05005603 if (status)
Marcel Holtmann7667da32013-10-15 14:26:27 -07005604 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02005605
5606 memset(&ev, 0, sizeof(ev));
5607 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02005608 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
Johan Hedbergb312b1612011-03-16 14:29:37 +02005609
Johan Hedberg2e58ef32011-11-08 20:40:15 +02005610 cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05005611 if (!cmd) {
5612 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
Johan Hedbergb312b1612011-03-16 14:29:37 +02005613
Johan Hedberg13928972013-03-15 17:07:00 -05005614 /* If this is a HCI command related to powering on the
5615 * HCI dev don't send any mgmt signals.
5616 */
5617 if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev))
Marcel Holtmann7667da32013-10-15 14:26:27 -07005618 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02005619 }
5620
Marcel Holtmann7667da32013-10-15 14:26:27 -07005621 mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
5622 cmd ? cmd->sk : NULL);
Johan Hedbergb312b1612011-03-16 14:29:37 +02005623}
Szymon Jancc35938b2011-03-22 13:12:21 +01005624
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08005625void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192,
5626 u8 *randomizer192, u8 *hash256,
5627 u8 *randomizer256, u8 status)
Szymon Jancc35938b2011-03-22 13:12:21 +01005628{
5629 struct pending_cmd *cmd;
Szymon Jancc35938b2011-03-22 13:12:21 +01005630
Johan Hedberg744cf192011-11-08 20:40:14 +02005631 BT_DBG("%s status %u", hdev->name, status);
Szymon Jancc35938b2011-03-22 13:12:21 +01005632
Johan Hedberg2e58ef32011-11-08 20:40:15 +02005633 cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01005634 if (!cmd)
Marcel Holtmann3edaf092013-10-15 14:26:28 -07005635 return;
Szymon Jancc35938b2011-03-22 13:12:21 +01005636
5637 if (status) {
Marcel Holtmann3edaf092013-10-15 14:26:28 -07005638 cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
5639 mgmt_status(status));
Szymon Jancc35938b2011-03-22 13:12:21 +01005640 } else {
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08005641 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) &&
5642 hash256 && randomizer256) {
5643 struct mgmt_rp_read_local_oob_ext_data rp;
Szymon Jancc35938b2011-03-22 13:12:21 +01005644
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08005645 memcpy(rp.hash192, hash192, sizeof(rp.hash192));
5646 memcpy(rp.randomizer192, randomizer192,
5647 sizeof(rp.randomizer192));
Szymon Jancc35938b2011-03-22 13:12:21 +01005648
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08005649 memcpy(rp.hash256, hash256, sizeof(rp.hash256));
5650 memcpy(rp.randomizer256, randomizer256,
5651 sizeof(rp.randomizer256));
5652
5653 cmd_complete(cmd->sk, hdev->id,
5654 MGMT_OP_READ_LOCAL_OOB_DATA, 0,
5655 &rp, sizeof(rp));
5656 } else {
5657 struct mgmt_rp_read_local_oob_data rp;
5658
5659 memcpy(rp.hash, hash192, sizeof(rp.hash));
5660 memcpy(rp.randomizer, randomizer192,
5661 sizeof(rp.randomizer));
5662
5663 cmd_complete(cmd->sk, hdev->id,
5664 MGMT_OP_READ_LOCAL_OOB_DATA, 0,
5665 &rp, sizeof(rp));
5666 }
Szymon Jancc35938b2011-03-22 13:12:21 +01005667 }
5668
5669 mgmt_pending_remove(cmd);
Szymon Jancc35938b2011-03-22 13:12:21 +01005670}
Johan Hedberge17acd42011-03-30 23:57:16 +03005671
Marcel Holtmann901801b2013-10-06 23:55:51 -07005672void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
Johan Hedberg73cf71d2014-03-25 12:06:19 +02005673 u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name,
5674 u8 ssp, u8 *eir, u16 eir_len, u8 *scan_rsp,
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02005675 u8 scan_rsp_len)
Johan Hedberge17acd42011-03-30 23:57:16 +03005676{
Johan Hedberge319d2e2012-01-15 19:51:59 +02005677 char buf[512];
5678 struct mgmt_ev_device_found *ev = (void *) buf;
Johan Hedberg5cedbb82014-02-18 21:41:37 +02005679 struct smp_irk *irk;
Johan Hedberg1dc06092012-01-15 21:01:23 +02005680 size_t ev_size;
Johan Hedberge17acd42011-03-30 23:57:16 +03005681
Andre Guedes12602d02013-04-30 15:29:40 -03005682 if (!hci_discovery_active(hdev))
Marcel Holtmann901801b2013-10-06 23:55:51 -07005683 return;
Andre Guedes12602d02013-04-30 15:29:40 -03005684
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02005685 /* Make sure that the buffer is big enough. The 5 extra bytes
5686 * are for the potential CoD field.
5687 */
5688 if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf))
Marcel Holtmann901801b2013-10-06 23:55:51 -07005689 return;
Andre Guedes7d262f82012-01-10 18:20:49 -03005690
Johan Hedberg1dc06092012-01-15 21:01:23 +02005691 memset(buf, 0, sizeof(buf));
5692
Johan Hedberg5cedbb82014-02-18 21:41:37 +02005693 irk = hci_get_irk(hdev, bdaddr, addr_type);
5694 if (irk) {
5695 bacpy(&ev->addr.bdaddr, &irk->bdaddr);
5696 ev->addr.type = link_to_bdaddr(link_type, irk->addr_type);
5697 } else {
5698 bacpy(&ev->addr.bdaddr, bdaddr);
5699 ev->addr.type = link_to_bdaddr(link_type, addr_type);
5700 }
5701
Johan Hedberge319d2e2012-01-15 19:51:59 +02005702 ev->rssi = rssi;
Johan Hedberg9a395a82012-02-23 00:00:32 +02005703 if (cfm_name)
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005704 ev->flags |= cpu_to_le32(MGMT_DEV_FOUND_CONFIRM_NAME);
Johan Hedberg388fc8f2012-02-23 00:38:59 +02005705 if (!ssp)
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005706 ev->flags |= cpu_to_le32(MGMT_DEV_FOUND_LEGACY_PAIRING);
Johan Hedberge17acd42011-03-30 23:57:16 +03005707
Johan Hedberg1dc06092012-01-15 21:01:23 +02005708 if (eir_len > 0)
Johan Hedberge319d2e2012-01-15 19:51:59 +02005709 memcpy(ev->eir, eir, eir_len);
Johan Hedberge17acd42011-03-30 23:57:16 +03005710
Johan Hedberg1dc06092012-01-15 21:01:23 +02005711 if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV))
5712 eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005713 dev_class, 3);
Johan Hedberg1dc06092012-01-15 21:01:23 +02005714
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02005715 if (scan_rsp_len > 0)
5716 memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len);
5717
5718 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
5719 ev_size = sizeof(*ev) + eir_len + scan_rsp_len;
Andre Guedesf8523592011-09-09 18:56:26 -03005720
Marcel Holtmann901801b2013-10-06 23:55:51 -07005721 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL);
Johan Hedberge17acd42011-03-30 23:57:16 +03005722}
Johan Hedberga88a9652011-03-30 13:18:12 +03005723
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07005724void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
5725 u8 addr_type, s8 rssi, u8 *name, u8 name_len)
Johan Hedberga88a9652011-03-30 13:18:12 +03005726{
Johan Hedbergb644ba32012-01-17 21:48:47 +02005727 struct mgmt_ev_device_found *ev;
5728 char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2];
5729 u16 eir_len;
Johan Hedberga88a9652011-03-30 13:18:12 +03005730
Johan Hedbergb644ba32012-01-17 21:48:47 +02005731 ev = (struct mgmt_ev_device_found *) buf;
Johan Hedberga88a9652011-03-30 13:18:12 +03005732
Johan Hedbergb644ba32012-01-17 21:48:47 +02005733 memset(buf, 0, sizeof(buf));
Johan Hedberga88a9652011-03-30 13:18:12 +03005734
Johan Hedbergb644ba32012-01-17 21:48:47 +02005735 bacpy(&ev->addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03005736 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergb644ba32012-01-17 21:48:47 +02005737 ev->rssi = rssi;
5738
5739 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005740 name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02005741
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02005742 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02005743
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07005744 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL);
Johan Hedberga88a9652011-03-30 13:18:12 +03005745}
Johan Hedberg314b2382011-04-27 10:29:57 -04005746
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07005747void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
Johan Hedberg314b2382011-04-27 10:29:57 -04005748{
Johan Hedbergf963e8e2012-02-20 23:30:44 +02005749 struct mgmt_ev_discovering ev;
Johan Hedberg164a6e72011-11-01 17:06:44 +02005750 struct pending_cmd *cmd;
5751
Andre Guedes343fb142011-11-22 17:14:19 -03005752 BT_DBG("%s discovering %u", hdev->name, discovering);
5753
Johan Hedberg164a6e72011-11-01 17:06:44 +02005754 if (discovering)
Johan Hedberg2e58ef32011-11-08 20:40:15 +02005755 cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev);
Johan Hedberg164a6e72011-11-01 17:06:44 +02005756 else
Johan Hedberg2e58ef32011-11-08 20:40:15 +02005757 cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
Johan Hedberg164a6e72011-11-01 17:06:44 +02005758
5759 if (cmd != NULL) {
Johan Hedbergf808e162012-02-19 12:52:07 +02005760 u8 type = hdev->discovery.type;
5761
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005762 cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type,
5763 sizeof(type));
Johan Hedberg164a6e72011-11-01 17:06:44 +02005764 mgmt_pending_remove(cmd);
5765 }
5766
Johan Hedbergf963e8e2012-02-20 23:30:44 +02005767 memset(&ev, 0, sizeof(ev));
5768 ev.type = hdev->discovery.type;
5769 ev.discovering = discovering;
5770
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07005771 mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg314b2382011-04-27 10:29:57 -04005772}
Antti Julku5e762442011-08-25 16:48:02 +03005773
Johan Hedberg88c1fe42012-02-09 15:56:11 +02005774int mgmt_device_blocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
Antti Julku5e762442011-08-25 16:48:02 +03005775{
5776 struct pending_cmd *cmd;
5777 struct mgmt_ev_device_blocked ev;
5778
Johan Hedberg2e58ef32011-11-08 20:40:15 +02005779 cmd = mgmt_pending_find(MGMT_OP_BLOCK_DEVICE, hdev);
Antti Julku5e762442011-08-25 16:48:02 +03005780
Johan Hedberg88c1fe42012-02-09 15:56:11 +02005781 bacpy(&ev.addr.bdaddr, bdaddr);
5782 ev.addr.type = type;
Antti Julku5e762442011-08-25 16:48:02 +03005783
Johan Hedberg744cf192011-11-08 20:40:14 +02005784 return mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005785 cmd ? cmd->sk : NULL);
Antti Julku5e762442011-08-25 16:48:02 +03005786}
5787
Johan Hedberg88c1fe42012-02-09 15:56:11 +02005788int mgmt_device_unblocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
Antti Julku5e762442011-08-25 16:48:02 +03005789{
5790 struct pending_cmd *cmd;
5791 struct mgmt_ev_device_unblocked ev;
5792
Johan Hedberg2e58ef32011-11-08 20:40:15 +02005793 cmd = mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE, hdev);
Antti Julku5e762442011-08-25 16:48:02 +03005794
Johan Hedberg88c1fe42012-02-09 15:56:11 +02005795 bacpy(&ev.addr.bdaddr, bdaddr);
5796 ev.addr.type = type;
Antti Julku5e762442011-08-25 16:48:02 +03005797
Johan Hedberg744cf192011-11-08 20:40:14 +02005798 return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005799 cmd ? cmd->sk : NULL);
Antti Julku5e762442011-08-25 16:48:02 +03005800}
Marcel Holtmann5976e602013-10-06 04:08:14 -07005801
5802static void adv_enable_complete(struct hci_dev *hdev, u8 status)
5803{
5804 BT_DBG("%s status %u", hdev->name, status);
5805
5806 /* Clear the advertising mgmt setting if we failed to re-enable it */
5807 if (status) {
5808 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
Marcel Holtmanna6d811e2013-10-06 04:11:12 -07005809 new_settings(hdev, NULL);
Marcel Holtmann5976e602013-10-06 04:08:14 -07005810 }
5811}
5812
5813void mgmt_reenable_advertising(struct hci_dev *hdev)
5814{
5815 struct hci_request req;
5816
Marcel Holtmannb145edc2013-10-10 09:47:54 -07005817 if (hci_conn_num(hdev, LE_LINK) > 0)
Marcel Holtmann5976e602013-10-06 04:08:14 -07005818 return;
5819
5820 if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags))
5821 return;
5822
5823 hci_req_init(&req, hdev);
5824 enable_advertising(&req);
5825
5826 /* If this fails we have no option but to let user space know
5827 * that we've disabled advertising.
5828 */
5829 if (hci_req_run(&req, adv_enable_complete) < 0) {
5830 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
Marcel Holtmanna6d811e2013-10-06 04:11:12 -07005831 new_settings(hdev, NULL);
Marcel Holtmann5976e602013-10-06 04:08:14 -07005832 }
5833}