blob: 9038118d37a3f32c5d42340fec49c91189a90283 [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
Johan Hedbergca69b792011-11-11 18:10:00 +020027#include <linux/kernel.h>
Szymon Janc72359752011-02-17 14:16:32 +010028#include <linux/uaccess.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040029#include <linux/module.h>
Johan Hedberg03811012010-12-08 00:21:06 +020030#include <asm/unaligned.h>
31
32#include <net/bluetooth/bluetooth.h>
33#include <net/bluetooth/hci_core.h>
34#include <net/bluetooth/mgmt.h>
Brian Gix5fe57d92011-12-21 16:12:13 -080035#include <net/bluetooth/smp.h>
Johan Hedberg03811012010-12-08 00:21:06 +020036
Marcel Holtmannd7b7e792012-02-20 21:47:49 +010037bool enable_hs;
38bool enable_le;
39
Johan Hedberg2da9c552012-02-17 14:39:28 +020040#define MGMT_VERSION 1
Marcel Holtmann053262d2012-03-27 18:49:02 +020041#define MGMT_REVISION 1
Johan Hedberg02d98122010-12-13 21:07:04 +020042
Johan Hedberge70bb2e2012-02-13 16:59:33 +020043static const u16 mgmt_commands[] = {
44 MGMT_OP_READ_INDEX_LIST,
45 MGMT_OP_READ_INFO,
46 MGMT_OP_SET_POWERED,
47 MGMT_OP_SET_DISCOVERABLE,
48 MGMT_OP_SET_CONNECTABLE,
49 MGMT_OP_SET_FAST_CONNECTABLE,
50 MGMT_OP_SET_PAIRABLE,
51 MGMT_OP_SET_LINK_SECURITY,
52 MGMT_OP_SET_SSP,
53 MGMT_OP_SET_HS,
54 MGMT_OP_SET_LE,
55 MGMT_OP_SET_DEV_CLASS,
56 MGMT_OP_SET_LOCAL_NAME,
57 MGMT_OP_ADD_UUID,
58 MGMT_OP_REMOVE_UUID,
59 MGMT_OP_LOAD_LINK_KEYS,
60 MGMT_OP_LOAD_LONG_TERM_KEYS,
61 MGMT_OP_DISCONNECT,
62 MGMT_OP_GET_CONNECTIONS,
63 MGMT_OP_PIN_CODE_REPLY,
64 MGMT_OP_PIN_CODE_NEG_REPLY,
65 MGMT_OP_SET_IO_CAPABILITY,
66 MGMT_OP_PAIR_DEVICE,
67 MGMT_OP_CANCEL_PAIR_DEVICE,
68 MGMT_OP_UNPAIR_DEVICE,
69 MGMT_OP_USER_CONFIRM_REPLY,
70 MGMT_OP_USER_CONFIRM_NEG_REPLY,
71 MGMT_OP_USER_PASSKEY_REPLY,
72 MGMT_OP_USER_PASSKEY_NEG_REPLY,
73 MGMT_OP_READ_LOCAL_OOB_DATA,
74 MGMT_OP_ADD_REMOTE_OOB_DATA,
75 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
76 MGMT_OP_START_DISCOVERY,
77 MGMT_OP_STOP_DISCOVERY,
78 MGMT_OP_CONFIRM_NAME,
79 MGMT_OP_BLOCK_DEVICE,
80 MGMT_OP_UNBLOCK_DEVICE,
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -070081 MGMT_OP_SET_DEVICE_ID,
Johan Hedberge70bb2e2012-02-13 16:59:33 +020082};
83
84static const u16 mgmt_events[] = {
85 MGMT_EV_CONTROLLER_ERROR,
86 MGMT_EV_INDEX_ADDED,
87 MGMT_EV_INDEX_REMOVED,
88 MGMT_EV_NEW_SETTINGS,
89 MGMT_EV_CLASS_OF_DEV_CHANGED,
90 MGMT_EV_LOCAL_NAME_CHANGED,
91 MGMT_EV_NEW_LINK_KEY,
92 MGMT_EV_NEW_LONG_TERM_KEY,
93 MGMT_EV_DEVICE_CONNECTED,
94 MGMT_EV_DEVICE_DISCONNECTED,
95 MGMT_EV_CONNECT_FAILED,
96 MGMT_EV_PIN_CODE_REQUEST,
97 MGMT_EV_USER_CONFIRM_REQUEST,
98 MGMT_EV_USER_PASSKEY_REQUEST,
99 MGMT_EV_AUTH_FAILED,
100 MGMT_EV_DEVICE_FOUND,
101 MGMT_EV_DISCOVERING,
102 MGMT_EV_DEVICE_BLOCKED,
103 MGMT_EV_DEVICE_UNBLOCKED,
104 MGMT_EV_DEVICE_UNPAIRED,
105};
106
Andre Guedes3fd24152012-02-03 17:48:01 -0300107/*
108 * These LE scan and inquiry parameters were chosen according to LE General
109 * Discovery Procedure specification.
110 */
111#define LE_SCAN_TYPE 0x01
112#define LE_SCAN_WIN 0x12
113#define LE_SCAN_INT 0x12
114#define LE_SCAN_TIMEOUT_LE_ONLY 10240 /* TGAP(gen_disc_scan_min) */
Andre Guedes5e0452c2012-02-17 20:39:38 -0300115#define LE_SCAN_TIMEOUT_BREDR_LE 5120 /* TGAP(100)/2 */
Andre Guedes3fd24152012-02-03 17:48:01 -0300116
Andre Guedese8777522012-02-03 17:48:02 -0300117#define INQUIRY_LEN_BREDR 0x08 /* TGAP(100) */
Andre Guedes5e0452c2012-02-17 20:39:38 -0300118#define INQUIRY_LEN_BREDR_LE 0x04 /* TGAP(100)/2 */
Andre Guedes2519a1f2011-11-07 11:45:24 -0300119
Marcel Holtmann17b02e62012-03-01 14:32:37 -0800120#define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)
Johan Hedberg7d785252011-12-15 00:47:39 +0200121
Johan Hedberg4b34ee782012-02-21 14:13:02 +0200122#define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \
123 !test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
124
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200125struct pending_cmd {
126 struct list_head list;
Johan Hedbergfc2f4b12011-11-09 13:58:56 +0200127 u16 opcode;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200128 int index;
Szymon Jancc68fb7f2011-03-22 13:12:19 +0100129 void *param;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200130 struct sock *sk;
Johan Hedberge9a416b2011-02-19 12:05:56 -0300131 void *user_data;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200132};
133
Johan Hedbergca69b792011-11-11 18:10:00 +0200134/* HCI to MGMT error code conversion table */
135static u8 mgmt_status_table[] = {
136 MGMT_STATUS_SUCCESS,
137 MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */
138 MGMT_STATUS_NOT_CONNECTED, /* No Connection */
139 MGMT_STATUS_FAILED, /* Hardware Failure */
140 MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */
141 MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */
142 MGMT_STATUS_NOT_PAIRED, /* PIN or Key Missing */
143 MGMT_STATUS_NO_RESOURCES, /* Memory Full */
144 MGMT_STATUS_TIMEOUT, /* Connection Timeout */
145 MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */
146 MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */
147 MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */
148 MGMT_STATUS_BUSY, /* Command Disallowed */
149 MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */
150 MGMT_STATUS_REJECTED, /* Rejected Security */
151 MGMT_STATUS_REJECTED, /* Rejected Personal */
152 MGMT_STATUS_TIMEOUT, /* Host Timeout */
153 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */
154 MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */
155 MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */
156 MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */
157 MGMT_STATUS_DISCONNECTED, /* OE Power Off */
158 MGMT_STATUS_DISCONNECTED, /* Connection Terminated */
159 MGMT_STATUS_BUSY, /* Repeated Attempts */
160 MGMT_STATUS_REJECTED, /* Pairing Not Allowed */
161 MGMT_STATUS_FAILED, /* Unknown LMP PDU */
162 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */
163 MGMT_STATUS_REJECTED, /* SCO Offset Rejected */
164 MGMT_STATUS_REJECTED, /* SCO Interval Rejected */
165 MGMT_STATUS_REJECTED, /* Air Mode Rejected */
166 MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */
167 MGMT_STATUS_FAILED, /* Unspecified Error */
168 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */
169 MGMT_STATUS_FAILED, /* Role Change Not Allowed */
170 MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */
171 MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */
172 MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */
173 MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */
174 MGMT_STATUS_FAILED, /* Unit Link Key Used */
175 MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */
176 MGMT_STATUS_TIMEOUT, /* Instant Passed */
177 MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */
178 MGMT_STATUS_FAILED, /* Transaction Collision */
179 MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */
180 MGMT_STATUS_REJECTED, /* QoS Rejected */
181 MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */
182 MGMT_STATUS_REJECTED, /* Insufficient Security */
183 MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */
184 MGMT_STATUS_BUSY, /* Role Switch Pending */
185 MGMT_STATUS_FAILED, /* Slot Violation */
186 MGMT_STATUS_FAILED, /* Role Switch Failed */
187 MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */
188 MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */
189 MGMT_STATUS_BUSY, /* Host Busy Pairing */
190 MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */
191 MGMT_STATUS_BUSY, /* Controller Busy */
192 MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */
193 MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */
194 MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */
195 MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */
196 MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */
197};
198
199static u8 mgmt_status(u8 hci_status)
200{
201 if (hci_status < ARRAY_SIZE(mgmt_status_table))
202 return mgmt_status_table[hci_status];
203
204 return MGMT_STATUS_FAILED;
205}
206
Szymon Janc4e51eae2011-02-25 19:05:48 +0100207static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status)
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200208{
209 struct sk_buff *skb;
210 struct mgmt_hdr *hdr;
211 struct mgmt_ev_cmd_status *ev;
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300212 int err;
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200213
Szymon Janc34eb5252011-02-28 14:10:08 +0100214 BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status);
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200215
216 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_ATOMIC);
217 if (!skb)
218 return -ENOMEM;
219
220 hdr = (void *) skb_put(skb, sizeof(*hdr));
221
222 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS);
Szymon Janc4e51eae2011-02-25 19:05:48 +0100223 hdr->index = cpu_to_le16(index);
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200224 hdr->len = cpu_to_le16(sizeof(*ev));
225
226 ev = (void *) skb_put(skb, sizeof(*ev));
227 ev->status = status;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200228 ev->opcode = cpu_to_le16(cmd);
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200229
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300230 err = sock_queue_rcv_skb(sk, skb);
231 if (err < 0)
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200232 kfree_skb(skb);
233
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300234 return err;
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200235}
236
Johan Hedbergaee9b2182012-02-18 15:07:59 +0200237static int cmd_complete(struct sock *sk, u16 index, u16 cmd, u8 status,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300238 void *rp, size_t rp_len)
Johan Hedberg02d98122010-12-13 21:07:04 +0200239{
240 struct sk_buff *skb;
241 struct mgmt_hdr *hdr;
242 struct mgmt_ev_cmd_complete *ev;
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300243 int err;
Johan Hedberg02d98122010-12-13 21:07:04 +0200244
245 BT_DBG("sock %p", sk);
246
Johan Hedberga38528f2011-01-22 06:46:43 +0200247 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_ATOMIC);
Johan Hedberg02d98122010-12-13 21:07:04 +0200248 if (!skb)
249 return -ENOMEM;
250
251 hdr = (void *) skb_put(skb, sizeof(*hdr));
Johan Hedberga38528f2011-01-22 06:46:43 +0200252
Johan Hedberg02d98122010-12-13 21:07:04 +0200253 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE);
Szymon Janc4e51eae2011-02-25 19:05:48 +0100254 hdr->index = cpu_to_le16(index);
Johan Hedberga38528f2011-01-22 06:46:43 +0200255 hdr->len = cpu_to_le16(sizeof(*ev) + rp_len);
Johan Hedberg02d98122010-12-13 21:07:04 +0200256
Johan Hedberga38528f2011-01-22 06:46:43 +0200257 ev = (void *) skb_put(skb, sizeof(*ev) + rp_len);
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200258 ev->opcode = cpu_to_le16(cmd);
Johan Hedbergaee9b2182012-02-18 15:07:59 +0200259 ev->status = status;
Szymon Janc8020c162011-02-28 14:09:50 +0100260
261 if (rp)
262 memcpy(ev->data, rp, rp_len);
Johan Hedberg02d98122010-12-13 21:07:04 +0200263
Gustavo F. Padovan56b7d132011-10-14 19:20:01 -0300264 err = sock_queue_rcv_skb(sk, skb);
265 if (err < 0)
Johan Hedberg02d98122010-12-13 21:07:04 +0200266 kfree_skb(skb);
267
Marcel Holtmanne5f0e152012-02-22 11:59:01 +0100268 return err;
Johan Hedberg02d98122010-12-13 21:07:04 +0200269}
270
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300271static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
272 u16 data_len)
Johan Hedberga38528f2011-01-22 06:46:43 +0200273{
274 struct mgmt_rp_read_version rp;
275
276 BT_DBG("sock %p", sk);
277
278 rp.version = MGMT_VERSION;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200279 rp.revision = __constant_cpu_to_le16(MGMT_REVISION);
Johan Hedberga38528f2011-01-22 06:46:43 +0200280
Johan Hedbergaee9b2182012-02-18 15:07:59 +0200281 return cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, &rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300282 sizeof(rp));
Johan Hedberga38528f2011-01-22 06:46:43 +0200283}
284
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300285static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
286 u16 data_len)
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200287{
288 struct mgmt_rp_read_commands *rp;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200289 const u16 num_commands = ARRAY_SIZE(mgmt_commands);
290 const u16 num_events = ARRAY_SIZE(mgmt_events);
Andrei Emeltchenko2e3c35e2012-03-14 18:54:15 +0200291 __le16 *opcode;
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200292 size_t rp_size;
293 int i, err;
294
295 BT_DBG("sock %p", sk);
296
297 rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16));
298
299 rp = kmalloc(rp_size, GFP_KERNEL);
300 if (!rp)
301 return -ENOMEM;
302
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200303 rp->num_commands = __constant_cpu_to_le16(num_commands);
304 rp->num_events = __constant_cpu_to_le16(num_events);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200305
306 for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++)
307 put_unaligned_le16(mgmt_commands[i], opcode);
308
309 for (i = 0; i < num_events; i++, opcode++)
310 put_unaligned_le16(mgmt_events[i], opcode);
311
Johan Hedbergaee9b2182012-02-18 15:07:59 +0200312 err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300313 rp_size);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200314 kfree(rp);
315
316 return err;
317}
318
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300319static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
320 u16 data_len)
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200321{
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200322 struct mgmt_rp_read_index_list *rp;
323 struct list_head *p;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200324 struct hci_dev *d;
Johan Hedberga38528f2011-01-22 06:46:43 +0200325 size_t rp_len;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200326 u16 count;
Johan Hedberga38528f2011-01-22 06:46:43 +0200327 int i, err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200328
329 BT_DBG("sock %p", sk);
330
331 read_lock(&hci_dev_list_lock);
332
333 count = 0;
334 list_for_each(p, &hci_dev_list) {
335 count++;
336 }
337
Johan Hedberga38528f2011-01-22 06:46:43 +0200338 rp_len = sizeof(*rp) + (2 * count);
339 rp = kmalloc(rp_len, GFP_ATOMIC);
340 if (!rp) {
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100341 read_unlock(&hci_dev_list_lock);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200342 return -ENOMEM;
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100343 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200344
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200345 rp->num_controllers = cpu_to_le16(count);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200346
347 i = 0;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200348 list_for_each_entry(d, &hci_dev_list, list) {
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200349 if (test_bit(HCI_SETUP, &d->dev_flags))
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200350 continue;
351
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200352 rp->index[i++] = cpu_to_le16(d->id);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200353 BT_DBG("Added hci%u", d->id);
354 }
355
356 read_unlock(&hci_dev_list_lock);
357
Johan Hedbergaee9b2182012-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;
371 settings |= MGMT_SETTING_CONNECTABLE;
372 settings |= MGMT_SETTING_FAST_CONNECTABLE;
373 settings |= MGMT_SETTING_DISCOVERABLE;
374 settings |= MGMT_SETTING_PAIRABLE;
Johan Hedberg03811012010-12-08 00:21:06 +0200375
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200376 if (hdev->features[6] & LMP_SIMPLE_PAIR)
377 settings |= MGMT_SETTING_SSP;
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200378
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200379 if (!(hdev->features[4] & LMP_NO_BREDR)) {
380 settings |= MGMT_SETTING_BREDR;
381 settings |= MGMT_SETTING_LINK_SECURITY;
382 }
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200383
Marcel Holtmannd7b7e792012-02-20 21:47:49 +0100384 if (enable_hs)
385 settings |= MGMT_SETTING_HS;
386
387 if (enable_le) {
388 if (hdev->features[4] & LMP_LE)
389 settings |= MGMT_SETTING_LE;
390 }
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200391
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200392 return settings;
393}
Johan Hedbergebc99fe2011-01-04 11:54:26 +0200394
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200395static u32 get_current_settings(struct hci_dev *hdev)
396{
397 u32 settings = 0;
Johan Hedbergdc4fe302011-03-16 14:29:36 +0200398
Johan Hedbergf1f0eb02012-02-21 17:15:41 +0200399 if (hdev_is_powered(hdev))
Marcel Holtmannf0d4b782012-02-21 12:14:25 +0100400 settings |= MGMT_SETTING_POWERED;
401
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200402 if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200403 settings |= MGMT_SETTING_CONNECTABLE;
404
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200405 if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200406 settings |= MGMT_SETTING_DISCOVERABLE;
407
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200408 if (test_bit(HCI_PAIRABLE, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200409 settings |= MGMT_SETTING_PAIRABLE;
410
411 if (!(hdev->features[4] & LMP_NO_BREDR))
412 settings |= MGMT_SETTING_BREDR;
413
Johan Hedberg06199cf2012-02-22 16:37:11 +0200414 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200415 settings |= MGMT_SETTING_LE;
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200416
Johan Hedberg47990ea2012-02-22 11:58:37 +0200417 if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200418 settings |= MGMT_SETTING_LINK_SECURITY;
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200419
Johan Hedberg84bde9d2012-01-25 14:21:06 +0200420 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200421 settings |= MGMT_SETTING_SSP;
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200422
Johan Hedberg6d80dfd2012-02-20 23:50:38 +0200423 if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags))
424 settings |= MGMT_SETTING_HS;
425
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200426 return settings;
Johan Hedbergc542a062011-01-26 13:11:03 +0200427}
428
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300429#define PNP_INFO_SVCLASS_ID 0x1200
430
431static u8 bluetooth_base_uuid[] = {
432 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
433 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
434};
435
436static u16 get_uuid16(u8 *uuid128)
437{
438 u32 val;
439 int i;
440
441 for (i = 0; i < 12; i++) {
442 if (bluetooth_base_uuid[i] != uuid128[i])
443 return 0;
444 }
445
Andrei Emeltchenko3e9fb6d2012-03-20 10:32:25 +0200446 val = get_unaligned_le32(&uuid128[12]);
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300447 if (val > 0xffff)
448 return 0;
449
450 return (u16) val;
451}
452
453static void create_eir(struct hci_dev *hdev, u8 *data)
454{
455 u8 *ptr = data;
456 u16 eir_len = 0;
457 u16 uuid16_list[HCI_MAX_EIR_LENGTH / sizeof(u16)];
458 int i, truncated = 0;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200459 struct bt_uuid *uuid;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300460 size_t name_len;
461
462 name_len = strlen(hdev->dev_name);
463
464 if (name_len > 0) {
465 /* EIR Data type */
466 if (name_len > 48) {
467 name_len = 48;
468 ptr[1] = EIR_NAME_SHORT;
469 } else
470 ptr[1] = EIR_NAME_COMPLETE;
471
472 /* EIR Data length */
473 ptr[0] = name_len + 1;
474
475 memcpy(ptr + 2, hdev->dev_name, name_len);
476
477 eir_len += (name_len + 2);
478 ptr += (name_len + 2);
479 }
480
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700481 if (hdev->inq_tx_power) {
482 ptr[0] = 2;
483 ptr[1] = EIR_TX_POWER;
484 ptr[2] = (u8) hdev->inq_tx_power;
485
486 eir_len += 3;
487 ptr += 3;
488 }
489
Marcel Holtmann2b9be132012-03-11 19:32:12 -0700490 if (hdev->devid_source > 0) {
491 ptr[0] = 9;
492 ptr[1] = EIR_DEVICE_ID;
493
494 put_unaligned_le16(hdev->devid_source, ptr + 2);
495 put_unaligned_le16(hdev->devid_vendor, ptr + 4);
496 put_unaligned_le16(hdev->devid_product, ptr + 6);
497 put_unaligned_le16(hdev->devid_version, ptr + 8);
498
499 eir_len += 10;
500 ptr += 10;
501 }
502
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300503 memset(uuid16_list, 0, sizeof(uuid16_list));
504
505 /* Group all UUID16 types */
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200506 list_for_each_entry(uuid, &hdev->uuids, list) {
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300507 u16 uuid16;
508
509 uuid16 = get_uuid16(uuid->uuid);
510 if (uuid16 == 0)
511 return;
512
513 if (uuid16 < 0x1100)
514 continue;
515
516 if (uuid16 == PNP_INFO_SVCLASS_ID)
517 continue;
518
519 /* Stop if not enough space to put next UUID */
520 if (eir_len + 2 + sizeof(u16) > HCI_MAX_EIR_LENGTH) {
521 truncated = 1;
522 break;
523 }
524
525 /* Check for duplicates */
526 for (i = 0; uuid16_list[i] != 0; i++)
527 if (uuid16_list[i] == uuid16)
528 break;
529
530 if (uuid16_list[i] == 0) {
531 uuid16_list[i] = uuid16;
532 eir_len += sizeof(u16);
533 }
534 }
535
536 if (uuid16_list[0] != 0) {
537 u8 *length = ptr;
538
539 /* EIR Data type */
540 ptr[1] = truncated ? EIR_UUID16_SOME : EIR_UUID16_ALL;
541
542 ptr += 2;
543 eir_len += 2;
544
545 for (i = 0; uuid16_list[i] != 0; i++) {
546 *ptr++ = (uuid16_list[i] & 0x00ff);
547 *ptr++ = (uuid16_list[i] & 0xff00) >> 8;
548 }
549
550 /* EIR Data length */
551 *length = (i * sizeof(u16)) + 1;
552 }
553}
554
555static int update_eir(struct hci_dev *hdev)
556{
557 struct hci_cp_write_eir cp;
558
Johan Hedberg504c8dc2012-02-23 13:30:41 +0200559 if (!hdev_is_powered(hdev))
Johan Hedberg7770c4a2012-02-22 22:06:38 +0200560 return 0;
561
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300562 if (!(hdev->features[6] & LMP_EXT_INQ))
563 return 0;
564
Johan Hedberg84bde9d2012-01-25 14:21:06 +0200565 if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300566 return 0;
567
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200568 if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300569 return 0;
570
571 memset(&cp, 0, sizeof(cp));
572
573 create_eir(hdev, cp.data);
574
575 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
576 return 0;
577
578 memcpy(hdev->eir, cp.data, sizeof(cp.data));
579
580 return hci_send_cmd(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
581}
582
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200583static u8 get_service_classes(struct hci_dev *hdev)
584{
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -0300585 struct bt_uuid *uuid;
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200586 u8 val = 0;
587
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -0300588 list_for_each_entry(uuid, &hdev->uuids, list)
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200589 val |= uuid->svc_hint;
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200590
591 return val;
592}
593
594static int update_class(struct hci_dev *hdev)
595{
596 u8 cod[3];
Johan Hedbergc95f0ba2012-02-23 22:54:38 +0200597 int err;
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200598
599 BT_DBG("%s", hdev->name);
600
Johan Hedberg504c8dc2012-02-23 13:30:41 +0200601 if (!hdev_is_powered(hdev))
Johan Hedberg7770c4a2012-02-22 22:06:38 +0200602 return 0;
603
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200604 if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200605 return 0;
606
607 cod[0] = hdev->minor_class;
608 cod[1] = hdev->major_class;
609 cod[2] = get_service_classes(hdev);
610
611 if (memcmp(cod, hdev->dev_class, 3) == 0)
612 return 0;
613
Johan Hedbergc95f0ba2012-02-23 22:54:38 +0200614 err = hci_send_cmd(hdev, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
615 if (err == 0)
616 set_bit(HCI_PENDING_CLASS, &hdev->dev_flags);
617
618 return err;
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200619}
620
Johan Hedberg7d785252011-12-15 00:47:39 +0200621static void service_cache_off(struct work_struct *work)
622{
623 struct hci_dev *hdev = container_of(work, struct hci_dev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300624 service_cache.work);
Johan Hedberg7d785252011-12-15 00:47:39 +0200625
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200626 if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
Johan Hedberg7d785252011-12-15 00:47:39 +0200627 return;
628
629 hci_dev_lock(hdev);
630
631 update_eir(hdev);
632 update_class(hdev);
633
634 hci_dev_unlock(hdev);
635}
636
Johan Hedberg6a919082012-02-28 06:17:26 +0200637static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
Johan Hedberg7d785252011-12-15 00:47:39 +0200638{
Johan Hedberg4f87da82012-03-02 19:55:56 +0200639 if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg6a919082012-02-28 06:17:26 +0200640 return;
641
Johan Hedberg4f87da82012-03-02 19:55:56 +0200642 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
Johan Hedberg7d785252011-12-15 00:47:39 +0200643
Johan Hedberg4f87da82012-03-02 19:55:56 +0200644 /* Non-mgmt controlled devices get this bit set
645 * implicitly so that pairing works for them, however
646 * for mgmt we require user-space to explicitly enable
647 * it
648 */
649 clear_bit(HCI_PAIRABLE, &hdev->dev_flags);
Johan Hedberg7d785252011-12-15 00:47:39 +0200650}
651
Johan Hedberg0f4e68c2012-02-28 17:18:30 +0200652static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300653 void *data, u16 data_len)
Johan Hedberg03811012010-12-08 00:21:06 +0200654{
655 struct mgmt_rp_read_info rp;
Johan Hedberg03811012010-12-08 00:21:06 +0200656
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200657 BT_DBG("sock %p %s", sk, hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +0200658
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300659 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +0200660
Johan Hedberg03811012010-12-08 00:21:06 +0200661 memset(&rp, 0, sizeof(rp));
662
Johan Hedberg03811012010-12-08 00:21:06 +0200663 bacpy(&rp.bdaddr, &hdev->bdaddr);
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200664
665 rp.version = hdev->hci_ver;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200666 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200667
668 rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
669 rp.current_settings = cpu_to_le32(get_current_settings(hdev));
670
671 memcpy(rp.dev_class, hdev->dev_class, 3);
Johan Hedberg03811012010-12-08 00:21:06 +0200672
673 memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
Johan Hedberg27fcc362012-02-22 21:46:22 +0200674 memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
Johan Hedberg03811012010-12-08 00:21:06 +0200675
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300676 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +0200677
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200678 return cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300679 sizeof(rp));
Johan Hedberg03811012010-12-08 00:21:06 +0200680}
681
682static void mgmt_pending_free(struct pending_cmd *cmd)
683{
684 sock_put(cmd->sk);
685 kfree(cmd->param);
686 kfree(cmd);
687}
688
689static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300690 struct hci_dev *hdev, void *data,
691 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +0200692{
693 struct pending_cmd *cmd;
694
695 cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC);
696 if (!cmd)
697 return NULL;
698
699 cmd->opcode = opcode;
700 cmd->index = hdev->id;
701
702 cmd->param = kmalloc(len, GFP_ATOMIC);
703 if (!cmd->param) {
704 kfree(cmd);
705 return NULL;
706 }
707
708 if (data)
709 memcpy(cmd->param, data, len);
710
711 cmd->sk = sk;
712 sock_hold(sk);
713
714 list_add(&cmd->list, &hdev->mgmt_pending);
715
716 return cmd;
717}
718
719static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300720 void (*cb)(struct pending_cmd *cmd, void *data),
721 void *data)
Johan Hedberg03811012010-12-08 00:21:06 +0200722{
723 struct list_head *p, *n;
724
725 list_for_each_safe(p, n, &hdev->mgmt_pending) {
726 struct pending_cmd *cmd;
727
728 cmd = list_entry(p, struct pending_cmd, list);
729
730 if (opcode > 0 && cmd->opcode != opcode)
731 continue;
732
733 cb(cmd, data);
734 }
735}
736
737static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev)
738{
739 struct pending_cmd *cmd;
740
741 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
742 if (cmd->opcode == opcode)
743 return cmd;
744 }
745
746 return NULL;
747}
748
749static void mgmt_pending_remove(struct pending_cmd *cmd)
750{
751 list_del(&cmd->list);
752 mgmt_pending_free(cmd);
753}
754
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200755static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
Johan Hedberg86805702011-11-11 16:18:52 +0200756{
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200757 __le32 settings = cpu_to_le32(get_current_settings(hdev));
Johan Hedberg86805702011-11-11 16:18:52 +0200758
Johan Hedbergaee9b2182012-02-18 15:07:59 +0200759 return cmd_complete(sk, hdev->id, opcode, 0, &settings,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300760 sizeof(settings));
Johan Hedberg86805702011-11-11 16:18:52 +0200761}
762
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200763static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300764 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +0200765{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -0300766 struct mgmt_mode *cp = data;
Johan Hedberg03811012010-12-08 00:21:06 +0200767 struct pending_cmd *cmd;
Johan Hedberg4b34ee782012-02-21 14:13:02 +0200768 int err;
Johan Hedberg03811012010-12-08 00:21:06 +0200769
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200770 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +0200771
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300772 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +0200773
Marcel Holtmannf0d4b782012-02-21 12:14:25 +0100774 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) {
775 cancel_delayed_work(&hdev->power_off);
776
777 if (cp->val) {
778 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
779 mgmt_powered(hdev, 1);
780 goto failed;
781 }
782 }
783
Johan Hedberg4b34ee782012-02-21 14:13:02 +0200784 if (!!cp->val == hdev_is_powered(hdev)) {
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200785 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
Johan Hedberg03811012010-12-08 00:21:06 +0200786 goto failed;
787 }
788
789 if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200790 err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300791 MGMT_STATUS_BUSY);
Johan Hedberg03811012010-12-08 00:21:06 +0200792 goto failed;
793 }
794
795 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
796 if (!cmd) {
797 err = -ENOMEM;
798 goto failed;
799 }
800
801 if (cp->val)
Gustavo F. Padovan7f971042011-12-18 12:40:32 -0200802 schedule_work(&hdev->power_on);
Johan Hedberg03811012010-12-08 00:21:06 +0200803 else
Gustavo F. Padovan80b7ab32011-12-17 14:52:27 -0200804 schedule_work(&hdev->power_off.work);
Johan Hedberg03811012010-12-08 00:21:06 +0200805
806 err = 0;
807
808failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300809 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +0200810 return err;
811}
812
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300813static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len,
814 struct sock *skip_sk)
Johan Hedbergbeadb2b2012-02-21 16:55:31 +0200815{
816 struct sk_buff *skb;
817 struct mgmt_hdr *hdr;
818
819 skb = alloc_skb(sizeof(*hdr) + data_len, GFP_ATOMIC);
820 if (!skb)
821 return -ENOMEM;
822
823 hdr = (void *) skb_put(skb, sizeof(*hdr));
824 hdr->opcode = cpu_to_le16(event);
825 if (hdev)
826 hdr->index = cpu_to_le16(hdev->id);
827 else
828 hdr->index = cpu_to_le16(MGMT_INDEX_NONE);
829 hdr->len = cpu_to_le16(data_len);
830
831 if (data)
832 memcpy(skb_put(skb, data_len), data, data_len);
833
Marcel Holtmann97e0bde2012-02-22 13:49:28 +0100834 /* Time stamp */
835 __net_timestamp(skb);
836
Johan Hedbergbeadb2b2012-02-21 16:55:31 +0200837 hci_send_to_control(skb, skip_sk);
838 kfree_skb(skb);
839
840 return 0;
841}
842
843static int new_settings(struct hci_dev *hdev, struct sock *skip)
844{
845 __le32 ev;
846
847 ev = cpu_to_le32(get_current_settings(hdev));
848
849 return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip);
850}
851
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200852static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300853 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +0200854{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -0300855 struct mgmt_cp_set_discoverable *cp = data;
Johan Hedberg03811012010-12-08 00:21:06 +0200856 struct pending_cmd *cmd;
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200857 u16 timeout;
Johan Hedberg03811012010-12-08 00:21:06 +0200858 u8 scan;
859 int err;
860
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200861 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +0200862
Marcel Holtmann1f350c82012-03-12 20:31:08 -0700863 timeout = __le16_to_cpu(cp->timeout);
Marcel Holtmann24c54a92012-02-22 18:06:34 +0100864 if (!cp->val && timeout > 0)
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200865 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300866 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200867
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300868 hci_dev_lock(hdev);
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200869
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200870 if (!hdev_is_powered(hdev) && timeout > 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200871 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300872 MGMT_STATUS_NOT_POWERED);
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200873 goto failed;
874 }
875
876 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
877 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200878 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300879 MGMT_STATUS_BUSY);
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200880 goto failed;
881 }
882
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200883 if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200884 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300885 MGMT_STATUS_REJECTED);
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200886 goto failed;
887 }
888
889 if (!hdev_is_powered(hdev)) {
Johan Hedberg0224d2f2012-02-21 19:40:05 +0200890 bool changed = false;
891
892 if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) {
893 change_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
894 changed = true;
895 }
896
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200897 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg0224d2f2012-02-21 19:40:05 +0200898 if (err < 0)
899 goto failed;
900
901 if (changed)
902 err = new_settings(hdev, sk);
903
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200904 goto failed;
905 }
906
907 if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) {
Marcel Holtmann955638e2012-02-22 18:21:00 +0100908 if (hdev->discov_timeout > 0) {
909 cancel_delayed_work(&hdev->discov_off);
910 hdev->discov_timeout = 0;
911 }
912
913 if (cp->val && timeout > 0) {
914 hdev->discov_timeout = timeout;
915 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
916 msecs_to_jiffies(hdev->discov_timeout * 1000));
917 }
918
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200919 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergf7b64e62010-12-13 21:07:06 +0200920 goto failed;
921 }
922
923 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
924 if (!cmd) {
925 err = -ENOMEM;
926 goto failed;
927 }
928
929 scan = SCAN_PAGE;
930
931 if (cp->val)
932 scan |= SCAN_INQUIRY;
933 else
934 cancel_delayed_work(&hdev->discov_off);
935
936 err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
937 if (err < 0)
938 mgmt_pending_remove(cmd);
939
Johan Hedberg03811012010-12-08 00:21:06 +0200940 if (cp->val)
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200941 hdev->discov_timeout = timeout;
Johan Hedberg03811012010-12-08 00:21:06 +0200942
943failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300944 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +0200945 return err;
946}
947
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200948static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300949 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +0200950{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -0300951 struct mgmt_mode *cp = data;
Johan Hedberg03811012010-12-08 00:21:06 +0200952 struct pending_cmd *cmd;
953 u8 scan;
954 int err;
955
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200956 BT_DBG("request for %s", hdev->name);
Johan Hedberge41d8b42010-12-13 21:07:03 +0200957
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300958 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200959
Johan Hedberg4b34ee782012-02-21 14:13:02 +0200960 if (!hdev_is_powered(hdev)) {
Johan Hedberg0224d2f2012-02-21 19:40:05 +0200961 bool changed = false;
962
963 if (!!cp->val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
964 changed = true;
965
Andrei Emeltchenko6bf0e462012-02-22 13:21:16 +0200966 if (cp->val) {
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200967 set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
Andrei Emeltchenko6bf0e462012-02-22 13:21:16 +0200968 } else {
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200969 clear_bit(HCI_CONNECTABLE, &hdev->dev_flags);
970 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
971 }
Johan Hedberg0224d2f2012-02-21 19:40:05 +0200972
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200973 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
Johan Hedberg0224d2f2012-02-21 19:40:05 +0200974 if (err < 0)
975 goto failed;
976
977 if (changed)
978 err = new_settings(hdev, sk);
979
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200980 goto failed;
981 }
982
983 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
984 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +0200985 err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300986 MGMT_STATUS_BUSY);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200987 goto failed;
988 }
989
Johan Hedberg5e5282b2012-02-21 16:01:30 +0200990 if (!!cp->val == test_bit(HCI_PSCAN, &hdev->flags)) {
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200991 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +0200992 goto failed;
993 }
994
995 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
996 if (!cmd) {
997 err = -ENOMEM;
998 goto failed;
999 }
1000
Andrei Emeltchenko6bf0e462012-02-22 13:21:16 +02001001 if (cp->val) {
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001002 scan = SCAN_PAGE;
Andrei Emeltchenko6bf0e462012-02-22 13:21:16 +02001003 } else {
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001004 scan = 0;
1005
Johan Hedbergdf2c6c52012-02-21 19:15:49 +02001006 if (test_bit(HCI_ISCAN, &hdev->flags) &&
1007 hdev->discov_timeout > 0)
1008 cancel_delayed_work(&hdev->discov_off);
1009 }
1010
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001011 err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1012 if (err < 0)
1013 mgmt_pending_remove(cmd);
1014
1015failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001016 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001017 return err;
1018}
1019
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001020static int set_pairable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001021 u16 len)
Johan Hedberg73f22f62010-12-29 16:00:25 +02001022{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001023 struct mgmt_mode *cp = data;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001024 int err;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001025
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001026 BT_DBG("request for %s", hdev->name);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001027
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001028 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001029
1030 if (cp->val)
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001031 set_bit(HCI_PAIRABLE, &hdev->dev_flags);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001032 else
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001033 clear_bit(HCI_PAIRABLE, &hdev->dev_flags);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001034
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001035 err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001036 if (err < 0)
1037 goto failed;
1038
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001039 err = new_settings(hdev, sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001040
1041failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001042 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001043 return err;
1044}
Johan Hedberg72a734e2010-12-30 00:38:22 +02001045
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001046static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
1047 u16 len)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001048{
1049 struct mgmt_mode *cp = data;
1050 struct pending_cmd *cmd;
Johan Hedberg816a11d2012-02-26 13:04:52 +02001051 u8 val;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001052 int err;
1053
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001054 BT_DBG("request for %s", hdev->name);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001055
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001056 hci_dev_lock(hdev);
1057
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001058 if (!hdev_is_powered(hdev)) {
Johan Hedberg47990ea2012-02-22 11:58:37 +02001059 bool changed = false;
1060
1061 if (!!cp->val != test_bit(HCI_LINK_SECURITY,
1062 &hdev->dev_flags)) {
1063 change_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
1064 changed = true;
1065 }
1066
1067 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1068 if (err < 0)
1069 goto failed;
1070
1071 if (changed)
1072 err = new_settings(hdev, sk);
1073
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001074 goto failed;
1075 }
1076
1077 if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001078 err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001079 MGMT_STATUS_BUSY);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001080 goto failed;
1081 }
1082
1083 val = !!cp->val;
1084
1085 if (test_bit(HCI_AUTH, &hdev->flags) == val) {
1086 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1087 goto failed;
1088 }
1089
1090 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
1091 if (!cmd) {
1092 err = -ENOMEM;
1093 goto failed;
1094 }
1095
1096 err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
1097 if (err < 0) {
1098 mgmt_pending_remove(cmd);
1099 goto failed;
1100 }
1101
1102failed:
1103 hci_dev_unlock(hdev);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001104 return err;
1105}
1106
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001107static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001108{
1109 struct mgmt_mode *cp = data;
1110 struct pending_cmd *cmd;
Johan Hedberg816a11d2012-02-26 13:04:52 +02001111 u8 val;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001112 int err;
1113
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001114 BT_DBG("request for %s", hdev->name);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001115
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001116 hci_dev_lock(hdev);
1117
Johan Hedberg6c8f12c2012-02-22 16:35:26 +02001118 if (!(hdev->features[6] & LMP_SIMPLE_PAIR)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001119 err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001120 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg6c8f12c2012-02-22 16:35:26 +02001121 goto failed;
1122 }
1123
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02001124 val = !!cp->val;
1125
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001126 if (!hdev_is_powered(hdev)) {
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02001127 bool changed = false;
1128
1129 if (val != test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
1130 change_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
1131 changed = true;
1132 }
1133
1134 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
1135 if (err < 0)
1136 goto failed;
1137
1138 if (changed)
1139 err = new_settings(hdev, sk);
1140
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001141 goto failed;
1142 }
1143
1144 if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev)) {
Szymon Jancd97dcb62012-03-16 16:02:56 +01001145 err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1146 MGMT_STATUS_BUSY);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001147 goto failed;
1148 }
1149
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001150 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) == val) {
1151 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
1152 goto failed;
1153 }
1154
1155 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
1156 if (!cmd) {
1157 err = -ENOMEM;
1158 goto failed;
1159 }
1160
1161 err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, sizeof(val), &val);
1162 if (err < 0) {
1163 mgmt_pending_remove(cmd);
1164 goto failed;
1165 }
1166
1167failed:
1168 hci_dev_unlock(hdev);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001169 return err;
1170}
1171
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001172static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001173{
1174 struct mgmt_mode *cp = data;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001175
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001176 BT_DBG("request for %s", hdev->name);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001177
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001178 if (!enable_hs)
1179 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001180 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001181
1182 if (cp->val)
1183 set_bit(HCI_HS_ENABLED, &hdev->dev_flags);
1184 else
1185 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
1186
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001187 return send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001188}
1189
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001190static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg06199cf2012-02-22 16:37:11 +02001191{
1192 struct mgmt_mode *cp = data;
1193 struct hci_cp_write_le_host_supported hci_cp;
1194 struct pending_cmd *cmd;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001195 int err;
Johan Hedberg0b60eba2012-02-28 00:57:24 +02001196 u8 val, enabled;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001197
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001198 BT_DBG("request for %s", hdev->name);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001199
Johan Hedberg1de028c2012-02-29 19:55:35 -08001200 hci_dev_lock(hdev);
1201
Johan Hedberg06199cf2012-02-22 16:37:11 +02001202 if (!enable_le || !(hdev->features[4] & LMP_LE)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001203 err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001204 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg1de028c2012-02-29 19:55:35 -08001205 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001206 }
1207
1208 val = !!cp->val;
Johan Hedberg0b60eba2012-02-28 00:57:24 +02001209 enabled = !!(hdev->host_features[0] & LMP_HOST_LE);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001210
Johan Hedberg0b60eba2012-02-28 00:57:24 +02001211 if (!hdev_is_powered(hdev) || val == enabled) {
Johan Hedberg06199cf2012-02-22 16:37:11 +02001212 bool changed = false;
1213
1214 if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
1215 change_bit(HCI_LE_ENABLED, &hdev->dev_flags);
1216 changed = true;
1217 }
1218
1219 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
1220 if (err < 0)
Johan Hedberg1de028c2012-02-29 19:55:35 -08001221 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001222
1223 if (changed)
1224 err = new_settings(hdev, sk);
1225
Johan Hedberg1de028c2012-02-29 19:55:35 -08001226 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001227 }
1228
1229 if (mgmt_pending_find(MGMT_OP_SET_LE, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001230 err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001231 MGMT_STATUS_BUSY);
Johan Hedberg1de028c2012-02-29 19:55:35 -08001232 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001233 }
1234
1235 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
1236 if (!cmd) {
1237 err = -ENOMEM;
Johan Hedberg1de028c2012-02-29 19:55:35 -08001238 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001239 }
1240
1241 memset(&hci_cp, 0, sizeof(hci_cp));
1242
1243 if (val) {
1244 hci_cp.le = val;
1245 hci_cp.simul = !!(hdev->features[6] & LMP_SIMUL_LE_BR);
1246 }
1247
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001248 err = hci_send_cmd(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp),
1249 &hci_cp);
Syam Sidhardhan0c01bc42012-04-12 20:33:21 +05301250 if (err < 0)
Johan Hedberg06199cf2012-02-22 16:37:11 +02001251 mgmt_pending_remove(cmd);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001252
Johan Hedberg1de028c2012-02-29 19:55:35 -08001253unlock:
1254 hci_dev_unlock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001255 return err;
1256}
1257
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001258static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001259{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001260 struct mgmt_cp_add_uuid *cp = data;
Johan Hedberg90e70452012-02-23 23:09:40 +02001261 struct pending_cmd *cmd;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001262 struct bt_uuid *uuid;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001263 int err;
1264
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001265 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001266
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001267 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001268
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02001269 if (test_bit(HCI_PENDING_CLASS, &hdev->dev_flags)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001270 err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001271 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02001272 goto failed;
1273 }
1274
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001275 uuid = kmalloc(sizeof(*uuid), GFP_ATOMIC);
1276 if (!uuid) {
1277 err = -ENOMEM;
1278 goto failed;
1279 }
1280
1281 memcpy(uuid->uuid, cp->uuid, 16);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001282 uuid->svc_hint = cp->svc_hint;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001283
1284 list_add(&uuid->list, &hdev->uuids);
1285
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001286 err = update_class(hdev);
1287 if (err < 0)
1288 goto failed;
1289
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001290 err = update_eir(hdev);
1291 if (err < 0)
1292 goto failed;
1293
Johan Hedberg90e70452012-02-23 23:09:40 +02001294 if (!test_bit(HCI_PENDING_CLASS, &hdev->dev_flags)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001295 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001296 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02001297 goto failed;
1298 }
1299
1300 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len);
Syam Sidhardhan0c01bc42012-04-12 20:33:21 +05301301 if (!cmd)
Johan Hedberg90e70452012-02-23 23:09:40 +02001302 err = -ENOMEM;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001303
1304failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001305 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001306 return err;
1307}
1308
Johan Hedberg24b78d02012-02-23 23:24:30 +02001309static bool enable_service_cache(struct hci_dev *hdev)
1310{
1311 if (!hdev_is_powered(hdev))
1312 return false;
1313
1314 if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) {
Marcel Holtmann17b02e62012-03-01 14:32:37 -08001315 schedule_delayed_work(&hdev->service_cache, CACHE_TIMEOUT);
Johan Hedberg24b78d02012-02-23 23:24:30 +02001316 return true;
1317 }
1318
1319 return false;
1320}
1321
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001322static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
1323 u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001324{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001325 struct mgmt_cp_remove_uuid *cp = data;
Johan Hedberg90e70452012-02-23 23:09:40 +02001326 struct pending_cmd *cmd;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001327 struct list_head *p, *n;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001328 u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001329 int err, found;
1330
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001331 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001332
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001333 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001334
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02001335 if (test_bit(HCI_PENDING_CLASS, &hdev->dev_flags)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001336 err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001337 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02001338 goto unlock;
1339 }
1340
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001341 if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
1342 err = hci_uuids_clear(hdev);
Johan Hedberg4004b6d2012-02-23 21:30:12 +02001343
Johan Hedberg24b78d02012-02-23 23:24:30 +02001344 if (enable_service_cache(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001345 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001346 0, hdev->dev_class, 3);
Johan Hedberg24b78d02012-02-23 23:24:30 +02001347 goto unlock;
1348 }
Johan Hedberg4004b6d2012-02-23 21:30:12 +02001349
Johan Hedberg9246a862012-02-23 21:33:16 +02001350 goto update_class;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001351 }
1352
1353 found = 0;
1354
1355 list_for_each_safe(p, n, &hdev->uuids) {
1356 struct bt_uuid *match = list_entry(p, struct bt_uuid, list);
1357
1358 if (memcmp(match->uuid, cp->uuid, 16) != 0)
1359 continue;
1360
1361 list_del(&match->list);
1362 found++;
1363 }
1364
1365 if (found == 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001366 err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001367 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001368 goto unlock;
1369 }
1370
Johan Hedberg9246a862012-02-23 21:33:16 +02001371update_class:
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001372 err = update_class(hdev);
1373 if (err < 0)
1374 goto unlock;
1375
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001376 err = update_eir(hdev);
1377 if (err < 0)
1378 goto unlock;
1379
Johan Hedberg90e70452012-02-23 23:09:40 +02001380 if (!test_bit(HCI_PENDING_CLASS, &hdev->dev_flags)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001381 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001382 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02001383 goto unlock;
1384 }
1385
1386 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
Syam Sidhardhan0c01bc42012-04-12 20:33:21 +05301387 if (!cmd)
Johan Hedberg90e70452012-02-23 23:09:40 +02001388 err = -ENOMEM;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001389
1390unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001391 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02001392 return err;
1393}
1394
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001395static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001396 u16 len)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001397{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001398 struct mgmt_cp_set_dev_class *cp = data;
Johan Hedberg90e70452012-02-23 23:09:40 +02001399 struct pending_cmd *cmd;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001400 int err;
1401
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001402 BT_DBG("request for %s", hdev->name);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001403
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001404 hci_dev_lock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001405
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02001406 if (test_bit(HCI_PENDING_CLASS, &hdev->dev_flags)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001407 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001408 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02001409 goto unlock;
1410 }
1411
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001412 hdev->major_class = cp->major;
1413 hdev->minor_class = cp->minor;
1414
Johan Hedberg932f5ff2012-02-22 22:11:32 +02001415 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001416 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001417 hdev->dev_class, 3);
Johan Hedberg932f5ff2012-02-22 22:11:32 +02001418 goto unlock;
1419 }
1420
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001421 if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) {
Johan Hedberg7d785252011-12-15 00:47:39 +02001422 hci_dev_unlock(hdev);
1423 cancel_delayed_work_sync(&hdev->service_cache);
1424 hci_dev_lock(hdev);
Johan Hedberg14c0b602011-12-15 00:47:37 +02001425 update_eir(hdev);
Johan Hedberg7d785252011-12-15 00:47:39 +02001426 }
Johan Hedberg14c0b602011-12-15 00:47:37 +02001427
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001428 err = update_class(hdev);
Johan Hedberg90e70452012-02-23 23:09:40 +02001429 if (err < 0)
1430 goto unlock;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001431
Johan Hedberg90e70452012-02-23 23:09:40 +02001432 if (!test_bit(HCI_PENDING_CLASS, &hdev->dev_flags)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001433 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001434 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02001435 goto unlock;
1436 }
1437
1438 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
Syam Sidhardhan0c01bc42012-04-12 20:33:21 +05301439 if (!cmd)
Johan Hedberg90e70452012-02-23 23:09:40 +02001440 err = -ENOMEM;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001441
Johan Hedbergb5235a62012-02-21 14:32:24 +02001442unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001443 hci_dev_unlock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001444 return err;
1445}
1446
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001447static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
1448 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001449{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001450 struct mgmt_cp_load_link_keys *cp = data;
Szymon Janc4e51eae2011-02-25 19:05:48 +01001451 u16 key_count, expected_len;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03001452 int i;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001453
Marcel Holtmann1f350c82012-03-12 20:31:08 -07001454 key_count = __le16_to_cpu(cp->key_count);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001455
Johan Hedberg86742e12011-11-07 23:13:38 +02001456 expected_len = sizeof(*cp) + key_count *
1457 sizeof(struct mgmt_link_key_info);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03001458 if (expected_len != len) {
Johan Hedberg86742e12011-11-07 23:13:38 +02001459 BT_ERR("load_link_keys: expected %u bytes, got %u bytes",
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03001460 len, expected_len);
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001461 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001462 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001463 }
1464
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001465 BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys,
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001466 key_count);
1467
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001468 hci_dev_lock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001469
1470 hci_link_keys_clear(hdev);
1471
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001472 set_bit(HCI_LINK_KEYS, &hdev->dev_flags);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001473
1474 if (cp->debug_keys)
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001475 set_bit(HCI_DEBUG_KEYS, &hdev->dev_flags);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001476 else
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001477 clear_bit(HCI_DEBUG_KEYS, &hdev->dev_flags);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001478
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03001479 for (i = 0; i < key_count; i++) {
Johan Hedberg86742e12011-11-07 23:13:38 +02001480 struct mgmt_link_key_info *key = &cp->keys[i];
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001481
Johan Hedbergd753fdc2012-02-17 14:06:34 +02001482 hci_add_link_key(hdev, NULL, 0, &key->addr.bdaddr, key->val,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001483 key->type, key->pin_len);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001484 }
1485
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001486 cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
Johan Hedberg0e5f8752011-11-11 16:18:54 +02001487
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001488 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001489
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03001490 return 0;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001491}
1492
Johan Hedbergb1078ad2012-02-09 17:21:16 +02001493static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001494 u8 addr_type, struct sock *skip_sk)
Johan Hedbergb1078ad2012-02-09 17:21:16 +02001495{
1496 struct mgmt_ev_device_unpaired ev;
1497
1498 bacpy(&ev.addr.bdaddr, bdaddr);
1499 ev.addr.type = addr_type;
1500
1501 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001502 skip_sk);
Johan Hedbergb1078ad2012-02-09 17:21:16 +02001503}
1504
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001505static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001506 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001507{
Johan Hedberg124f6e32012-02-09 13:50:12 +02001508 struct mgmt_cp_unpair_device *cp = data;
1509 struct mgmt_rp_unpair_device rp;
Johan Hedberga8a1d192011-11-10 15:54:38 +02001510 struct hci_cp_disconnect dc;
1511 struct pending_cmd *cmd;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001512 struct hci_conn *conn;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001513 int err;
1514
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001515 hci_dev_lock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001516
Johan Hedberga8a1d192011-11-10 15:54:38 +02001517 memset(&rp, 0, sizeof(rp));
Johan Hedberg124f6e32012-02-09 13:50:12 +02001518 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
1519 rp.addr.type = cp->addr.type;
Johan Hedberga8a1d192011-11-10 15:54:38 +02001520
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02001521 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001522 err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001523 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp));
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02001524 goto unlock;
1525 }
1526
Andre Guedes591f47f2012-04-24 21:02:49 -03001527 if (cp->addr.type == BDADDR_BREDR)
Johan Hedberg124f6e32012-02-09 13:50:12 +02001528 err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
1529 else
1530 err = hci_remove_ltk(hdev, &cp->addr.bdaddr);
Vinicius Costa Gomesb0dbfb42012-02-02 21:08:03 -03001531
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001532 if (err < 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001533 err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001534 MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001535 goto unlock;
1536 }
1537
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02001538 if (cp->disconnect) {
Andre Guedes591f47f2012-04-24 21:02:49 -03001539 if (cp->addr.type == BDADDR_BREDR)
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02001540 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
1541 &cp->addr.bdaddr);
1542 else
1543 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK,
1544 &cp->addr.bdaddr);
1545 } else {
1546 conn = NULL;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001547 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001548
Johan Hedberga8a1d192011-11-10 15:54:38 +02001549 if (!conn) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001550 err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001551 &rp, sizeof(rp));
Johan Hedbergb1078ad2012-02-09 17:21:16 +02001552 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
Johan Hedberga8a1d192011-11-10 15:54:38 +02001553 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001554 }
1555
Johan Hedberg124f6e32012-02-09 13:50:12 +02001556 cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001557 sizeof(*cp));
Johan Hedberga8a1d192011-11-10 15:54:38 +02001558 if (!cmd) {
1559 err = -ENOMEM;
1560 goto unlock;
1561 }
1562
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02001563 dc.handle = cpu_to_le16(conn->handle);
Johan Hedberga8a1d192011-11-10 15:54:38 +02001564 dc.reason = 0x13; /* Remote User Terminated Connection */
1565 err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc);
1566 if (err < 0)
1567 mgmt_pending_remove(cmd);
1568
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001569unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001570 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001571 return err;
1572}
1573
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001574static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001575 u16 len)
Johan Hedberg8962ee72011-01-20 12:40:27 +02001576{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001577 struct mgmt_cp_disconnect *cp = data;
Johan Hedberg8962ee72011-01-20 12:40:27 +02001578 struct hci_cp_disconnect dc;
Johan Hedberg366a0332011-02-19 12:05:55 -03001579 struct pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02001580 struct hci_conn *conn;
Johan Hedberg8962ee72011-01-20 12:40:27 +02001581 int err;
1582
1583 BT_DBG("");
1584
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001585 hci_dev_lock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02001586
1587 if (!test_bit(HCI_UP, &hdev->flags)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001588 err = cmd_status(sk, hdev->id, MGMT_OP_DISCONNECT,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001589 MGMT_STATUS_NOT_POWERED);
Johan Hedberg8962ee72011-01-20 12:40:27 +02001590 goto failed;
1591 }
1592
Johan Hedberg2e58ef32011-11-08 20:40:15 +02001593 if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001594 err = cmd_status(sk, hdev->id, MGMT_OP_DISCONNECT,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001595 MGMT_STATUS_BUSY);
Johan Hedberg8962ee72011-01-20 12:40:27 +02001596 goto failed;
1597 }
1598
Andre Guedes591f47f2012-04-24 21:02:49 -03001599 if (cp->addr.type == BDADDR_BREDR)
Johan Hedberg88c3df12012-02-09 14:27:38 +02001600 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
1601 else
1602 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
Vinicius Costa Gomes365227e2011-05-06 18:41:44 -03001603
Johan Hedberg8962ee72011-01-20 12:40:27 +02001604 if (!conn) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001605 err = cmd_status(sk, hdev->id, MGMT_OP_DISCONNECT,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001606 MGMT_STATUS_NOT_CONNECTED);
Johan Hedberg8962ee72011-01-20 12:40:27 +02001607 goto failed;
1608 }
1609
Johan Hedberg2e58ef32011-11-08 20:40:15 +02001610 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03001611 if (!cmd) {
1612 err = -ENOMEM;
Johan Hedberg8962ee72011-01-20 12:40:27 +02001613 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03001614 }
Johan Hedberg8962ee72011-01-20 12:40:27 +02001615
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02001616 dc.handle = cpu_to_le16(conn->handle);
Johan Hedberg8962ee72011-01-20 12:40:27 +02001617 dc.reason = 0x13; /* Remote User Terminated Connection */
1618
1619 err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc);
1620 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03001621 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02001622
1623failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001624 hci_dev_unlock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02001625 return err;
1626}
1627
Andre Guedes57c14772012-04-24 21:02:50 -03001628static u8 link_to_bdaddr(u8 link_type, u8 addr_type)
Johan Hedberg4c659c32011-11-07 23:13:39 +02001629{
1630 switch (link_type) {
1631 case LE_LINK:
Johan Hedberg48264f02011-11-09 13:58:58 +02001632 switch (addr_type) {
1633 case ADDR_LE_DEV_PUBLIC:
Andre Guedes591f47f2012-04-24 21:02:49 -03001634 return BDADDR_LE_PUBLIC;
Andre Guedes0ed09142012-04-03 08:46:54 -03001635
Johan Hedberg48264f02011-11-09 13:58:58 +02001636 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03001637 /* Fallback to LE Random address type */
Andre Guedes591f47f2012-04-24 21:02:49 -03001638 return BDADDR_LE_RANDOM;
Johan Hedberg48264f02011-11-09 13:58:58 +02001639 }
Andre Guedes0ed09142012-04-03 08:46:54 -03001640
Johan Hedberg4c659c32011-11-07 23:13:39 +02001641 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03001642 /* Fallback to BR/EDR type */
Andre Guedes591f47f2012-04-24 21:02:49 -03001643 return BDADDR_BREDR;
Johan Hedberg4c659c32011-11-07 23:13:39 +02001644 }
1645}
1646
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001647static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
1648 u16 data_len)
Johan Hedberg2784eb42011-01-21 13:56:35 +02001649{
Johan Hedberg2784eb42011-01-21 13:56:35 +02001650 struct mgmt_rp_get_connections *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001651 struct hci_conn *c;
Johan Hedberga38528f2011-01-22 06:46:43 +02001652 size_t rp_len;
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02001653 int err;
1654 u16 i;
Johan Hedberg2784eb42011-01-21 13:56:35 +02001655
1656 BT_DBG("");
1657
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001658 hci_dev_lock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02001659
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02001660 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001661 err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001662 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02001663 goto unlock;
1664 }
1665
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02001666 i = 0;
Johan Hedbergb644ba32012-01-17 21:48:47 +02001667 list_for_each_entry(c, &hdev->conn_hash.list, list) {
1668 if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02001669 i++;
Johan Hedberg2784eb42011-01-21 13:56:35 +02001670 }
1671
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02001672 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Johan Hedberga38528f2011-01-22 06:46:43 +02001673 rp = kmalloc(rp_len, GFP_ATOMIC);
1674 if (!rp) {
Johan Hedberg2784eb42011-01-21 13:56:35 +02001675 err = -ENOMEM;
1676 goto unlock;
1677 }
1678
Johan Hedberg2784eb42011-01-21 13:56:35 +02001679 i = 0;
Johan Hedberg4c659c32011-11-07 23:13:39 +02001680 list_for_each_entry(c, &hdev->conn_hash.list, list) {
Johan Hedbergb644ba32012-01-17 21:48:47 +02001681 if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
1682 continue;
Johan Hedberg4c659c32011-11-07 23:13:39 +02001683 bacpy(&rp->addr[i].bdaddr, &c->dst);
Andre Guedes57c14772012-04-24 21:02:50 -03001684 rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type);
Andre Guedes0ed09142012-04-03 08:46:54 -03001685 if (c->type == SCO_LINK || c->type == ESCO_LINK)
Johan Hedberg4c659c32011-11-07 23:13:39 +02001686 continue;
1687 i++;
1688 }
1689
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02001690 rp->conn_count = cpu_to_le16(i);
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02001691
Johan Hedberg4c659c32011-11-07 23:13:39 +02001692 /* Recalculate length in case of filtered SCO connections, etc */
1693 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Johan Hedberg2784eb42011-01-21 13:56:35 +02001694
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001695 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001696 rp_len);
Johan Hedberg2784eb42011-01-21 13:56:35 +02001697
Johan Hedberga38528f2011-01-22 06:46:43 +02001698 kfree(rp);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02001699
1700unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001701 hci_dev_unlock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02001702 return err;
1703}
1704
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001705static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001706 struct mgmt_cp_pin_code_neg_reply *cp)
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02001707{
1708 struct pending_cmd *cmd;
1709 int err;
1710
Johan Hedberg2e58ef32011-11-08 20:40:15 +02001711 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001712 sizeof(*cp));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02001713 if (!cmd)
1714 return -ENOMEM;
1715
Johan Hedbergd8457692012-02-17 14:24:57 +02001716 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001717 sizeof(cp->addr.bdaddr), &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02001718 if (err < 0)
1719 mgmt_pending_remove(cmd);
1720
1721 return err;
1722}
1723
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001724static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001725 u16 len)
Johan Hedberg980e1a52011-01-22 06:10:07 +02001726{
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02001727 struct hci_conn *conn;
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001728 struct mgmt_cp_pin_code_reply *cp = data;
Johan Hedberg980e1a52011-01-22 06:10:07 +02001729 struct hci_cp_pin_code_reply reply;
Johan Hedberg366a0332011-02-19 12:05:55 -03001730 struct pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02001731 int err;
1732
1733 BT_DBG("");
1734
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001735 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001736
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001737 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001738 err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001739 MGMT_STATUS_NOT_POWERED);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001740 goto failed;
1741 }
1742
Johan Hedbergd8457692012-02-17 14:24:57 +02001743 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02001744 if (!conn) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001745 err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001746 MGMT_STATUS_NOT_CONNECTED);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02001747 goto failed;
1748 }
1749
1750 if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) {
Johan Hedbergd8457692012-02-17 14:24:57 +02001751 struct mgmt_cp_pin_code_neg_reply ncp;
1752
1753 memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02001754
1755 BT_ERR("PIN code is not 16 bytes long");
1756
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001757 err = send_pin_code_neg_reply(sk, hdev, &ncp);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02001758 if (err >= 0)
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001759 err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001760 MGMT_STATUS_INVALID_PARAMS);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02001761
1762 goto failed;
1763 }
1764
Gustavo F. Padovan00abfe42012-03-01 00:37:10 -03001765 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03001766 if (!cmd) {
1767 err = -ENOMEM;
Johan Hedberg980e1a52011-01-22 06:10:07 +02001768 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03001769 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02001770
Johan Hedbergd8457692012-02-17 14:24:57 +02001771 bacpy(&reply.bdaddr, &cp->addr.bdaddr);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001772 reply.pin_len = cp->pin_len;
Waldemar Rymarkiewicz24718ca2011-06-01 17:28:47 +02001773 memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code));
Johan Hedberg980e1a52011-01-22 06:10:07 +02001774
1775 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
1776 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03001777 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001778
1779failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001780 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001781 return err;
1782}
1783
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001784static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001785 void *data, u16 len)
Johan Hedberg980e1a52011-01-22 06:10:07 +02001786{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001787 struct mgmt_cp_pin_code_neg_reply *cp = data;
Johan Hedberg980e1a52011-01-22 06:10:07 +02001788 int err;
1789
1790 BT_DBG("");
1791
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001792 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001793
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001794 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001795 err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001796 MGMT_STATUS_NOT_POWERED);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001797 goto failed;
1798 }
1799
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001800 err = send_pin_code_neg_reply(sk, hdev, cp);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001801
1802failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001803 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001804 return err;
1805}
1806
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001807static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
1808 u16 len)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02001809{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001810 struct mgmt_cp_set_io_capability *cp = data;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02001811
1812 BT_DBG("");
1813
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001814 hci_dev_lock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02001815
1816 hdev->io_capability = cp->io_capability;
1817
1818 BT_DBG("%s IO capability set to 0x%02x", hdev->name,
Szymon Jancb8534e02011-03-01 16:55:34 +01001819 hdev->io_capability);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02001820
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001821 hci_dev_unlock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02001822
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001823 return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL,
1824 0);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02001825}
1826
Johan Hedberge9a416b2011-02-19 12:05:56 -03001827static inline struct pending_cmd *find_pairing(struct hci_conn *conn)
1828{
1829 struct hci_dev *hdev = conn->hdev;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001830 struct pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03001831
Johan Hedberg2e58ef32011-11-08 20:40:15 +02001832 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
Johan Hedberge9a416b2011-02-19 12:05:56 -03001833 if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
1834 continue;
1835
Johan Hedberge9a416b2011-02-19 12:05:56 -03001836 if (cmd->user_data != conn)
1837 continue;
1838
1839 return cmd;
1840 }
1841
1842 return NULL;
1843}
1844
1845static void pairing_complete(struct pending_cmd *cmd, u8 status)
1846{
1847 struct mgmt_rp_pair_device rp;
1848 struct hci_conn *conn = cmd->user_data;
1849
Johan Hedbergba4e5642011-11-11 00:07:34 +02001850 bacpy(&rp.addr.bdaddr, &conn->dst);
Andre Guedes57c14772012-04-24 21:02:50 -03001851 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedberge9a416b2011-02-19 12:05:56 -03001852
Johan Hedbergaee9b2182012-02-18 15:07:59 +02001853 cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001854 &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03001855
1856 /* So we don't get further callbacks for this connection */
1857 conn->connect_cfm_cb = NULL;
1858 conn->security_cfm_cb = NULL;
1859 conn->disconn_cfm_cb = NULL;
1860
1861 hci_conn_put(conn);
1862
Johan Hedberga664b5b2011-02-19 12:06:02 -03001863 mgmt_pending_remove(cmd);
Johan Hedberge9a416b2011-02-19 12:05:56 -03001864}
1865
1866static void pairing_complete_cb(struct hci_conn *conn, u8 status)
1867{
1868 struct pending_cmd *cmd;
1869
1870 BT_DBG("status %u", status);
1871
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001872 cmd = find_pairing(conn);
1873 if (!cmd)
1874 BT_DBG("Unable to find a pending command");
1875 else
Johan Hedberge2113262012-02-18 15:20:03 +02001876 pairing_complete(cmd, mgmt_status(status));
Johan Hedberge9a416b2011-02-19 12:05:56 -03001877}
1878
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001879static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001880 u16 len)
Johan Hedberge9a416b2011-02-19 12:05:56 -03001881{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001882 struct mgmt_cp_pair_device *cp = data;
Johan Hedberg1425acb2011-11-11 00:07:35 +02001883 struct mgmt_rp_pair_device rp;
Johan Hedberge9a416b2011-02-19 12:05:56 -03001884 struct pending_cmd *cmd;
1885 u8 sec_level, auth_type;
1886 struct hci_conn *conn;
Johan Hedberge9a416b2011-02-19 12:05:56 -03001887 int err;
1888
1889 BT_DBG("");
1890
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001891 hci_dev_lock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03001892
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02001893 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001894 err = cmd_status(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001895 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02001896 goto unlock;
1897 }
1898
Vinicius Costa Gomesc908df32011-09-02 14:51:22 -03001899 sec_level = BT_SECURITY_MEDIUM;
1900 if (cp->io_cap == 0x03)
Johan Hedberge9a416b2011-02-19 12:05:56 -03001901 auth_type = HCI_AT_DEDICATED_BONDING;
Vinicius Costa Gomesc908df32011-09-02 14:51:22 -03001902 else
Johan Hedberge9a416b2011-02-19 12:05:56 -03001903 auth_type = HCI_AT_DEDICATED_BONDING_MITM;
Johan Hedberge9a416b2011-02-19 12:05:56 -03001904
Andre Guedes591f47f2012-04-24 21:02:49 -03001905 if (cp->addr.type == BDADDR_BREDR)
Andre Guedesb12f62c2012-04-24 21:02:54 -03001906 conn = hci_connect(hdev, ACL_LINK, &cp->addr.bdaddr,
1907 cp->addr.type, sec_level, auth_type);
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03001908 else
Andre Guedesb12f62c2012-04-24 21:02:54 -03001909 conn = hci_connect(hdev, LE_LINK, &cp->addr.bdaddr,
1910 cp->addr.type, sec_level, auth_type);
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03001911
Johan Hedberg1425acb2011-11-11 00:07:35 +02001912 memset(&rp, 0, sizeof(rp));
1913 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
1914 rp.addr.type = cp->addr.type;
1915
Ville Tervo30e76272011-02-22 16:10:53 -03001916 if (IS_ERR(conn)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001917 err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001918 MGMT_STATUS_CONNECT_FAILED, &rp,
1919 sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03001920 goto unlock;
1921 }
1922
1923 if (conn->connect_cfm_cb) {
1924 hci_conn_put(conn);
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001925 err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001926 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03001927 goto unlock;
1928 }
1929
Johan Hedberg2e58ef32011-11-08 20:40:15 +02001930 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
Johan Hedberge9a416b2011-02-19 12:05:56 -03001931 if (!cmd) {
1932 err = -ENOMEM;
1933 hci_conn_put(conn);
1934 goto unlock;
1935 }
1936
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03001937 /* For LE, just connecting isn't a proof that the pairing finished */
Andre Guedes591f47f2012-04-24 21:02:49 -03001938 if (cp->addr.type == BDADDR_BREDR)
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03001939 conn->connect_cfm_cb = pairing_complete_cb;
1940
Johan Hedberge9a416b2011-02-19 12:05:56 -03001941 conn->security_cfm_cb = pairing_complete_cb;
1942 conn->disconn_cfm_cb = pairing_complete_cb;
1943 conn->io_capability = cp->io_cap;
1944 cmd->user_data = conn;
1945
1946 if (conn->state == BT_CONNECTED &&
1947 hci_conn_security(conn, sec_level, auth_type))
1948 pairing_complete(cmd, 0);
1949
1950 err = 0;
1951
1952unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001953 hci_dev_unlock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03001954 return err;
1955}
1956
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001957static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
1958 u16 len)
Johan Hedberg28424702012-02-02 04:02:29 +02001959{
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02001960 struct mgmt_addr_info *addr = data;
Johan Hedberg28424702012-02-02 04:02:29 +02001961 struct pending_cmd *cmd;
1962 struct hci_conn *conn;
1963 int err;
1964
1965 BT_DBG("");
1966
Johan Hedberg28424702012-02-02 04:02:29 +02001967 hci_dev_lock(hdev);
1968
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02001969 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001970 err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001971 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02001972 goto unlock;
1973 }
1974
Johan Hedberg28424702012-02-02 04:02:29 +02001975 cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev);
1976 if (!cmd) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001977 err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001978 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02001979 goto unlock;
1980 }
1981
1982 conn = cmd->user_data;
1983
1984 if (bacmp(&addr->bdaddr, &conn->dst) != 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001985 err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001986 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02001987 goto unlock;
1988 }
1989
1990 pairing_complete(cmd, MGMT_STATUS_CANCELLED);
1991
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001992 err = cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001993 addr, sizeof(*addr));
Johan Hedberg28424702012-02-02 04:02:29 +02001994unlock:
1995 hci_dev_unlock(hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02001996 return err;
1997}
1998
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001999static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002000 bdaddr_t *bdaddr, u8 type, u16 mgmt_op,
2001 u16 hci_op, __le32 passkey)
Johan Hedberga5c29682011-02-19 12:05:57 -03002002{
Johan Hedberga5c29682011-02-19 12:05:57 -03002003 struct pending_cmd *cmd;
Brian Gix0df4c182011-11-16 13:53:13 -08002004 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03002005 int err;
2006
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002007 hci_dev_lock(hdev);
Johan Hedberg08ba5382011-03-16 14:29:34 +02002008
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002009 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002010 err = cmd_status(sk, hdev->id, mgmt_op,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002011 MGMT_STATUS_NOT_POWERED);
Brian Gix0df4c182011-11-16 13:53:13 -08002012 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03002013 }
2014
Andre Guedes591f47f2012-04-24 21:02:49 -03002015 if (type == BDADDR_BREDR)
Johan Hedberg272d90d2012-02-09 15:26:12 +02002016 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, bdaddr);
2017 else
Brian Gix47c15e22011-11-16 13:53:14 -08002018 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, bdaddr);
Brian Gix47c15e22011-11-16 13:53:14 -08002019
Johan Hedberg272d90d2012-02-09 15:26:12 +02002020 if (!conn) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002021 err = cmd_status(sk, hdev->id, mgmt_op,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002022 MGMT_STATUS_NOT_CONNECTED);
Johan Hedberg272d90d2012-02-09 15:26:12 +02002023 goto done;
2024 }
2025
Andre Guedes591f47f2012-04-24 21:02:49 -03002026 if (type == BDADDR_LE_PUBLIC || type == BDADDR_LE_RANDOM) {
Brian Gix47c15e22011-11-16 13:53:14 -08002027 /* Continue with pairing via SMP */
Brian Gix5fe57d92011-12-21 16:12:13 -08002028 err = smp_user_confirm_reply(conn, mgmt_op, passkey);
Brian Gix47c15e22011-11-16 13:53:14 -08002029
Brian Gix5fe57d92011-12-21 16:12:13 -08002030 if (!err)
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002031 err = cmd_status(sk, hdev->id, mgmt_op,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002032 MGMT_STATUS_SUCCESS);
Brian Gix5fe57d92011-12-21 16:12:13 -08002033 else
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002034 err = cmd_status(sk, hdev->id, mgmt_op,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002035 MGMT_STATUS_FAILED);
Brian Gix5fe57d92011-12-21 16:12:13 -08002036
Brian Gix47c15e22011-11-16 13:53:14 -08002037 goto done;
2038 }
2039
Brian Gix0df4c182011-11-16 13:53:13 -08002040 cmd = mgmt_pending_add(sk, mgmt_op, hdev, bdaddr, sizeof(*bdaddr));
Johan Hedberga5c29682011-02-19 12:05:57 -03002041 if (!cmd) {
2042 err = -ENOMEM;
Brian Gix0df4c182011-11-16 13:53:13 -08002043 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03002044 }
2045
Brian Gix0df4c182011-11-16 13:53:13 -08002046 /* Continue with pairing via HCI */
Brian Gix604086b2011-11-23 08:28:33 -08002047 if (hci_op == HCI_OP_USER_PASSKEY_REPLY) {
2048 struct hci_cp_user_passkey_reply cp;
2049
2050 bacpy(&cp.bdaddr, bdaddr);
2051 cp.passkey = passkey;
2052 err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
2053 } else
2054 err = hci_send_cmd(hdev, hci_op, sizeof(*bdaddr), bdaddr);
2055
Johan Hedberga664b5b2011-02-19 12:06:02 -03002056 if (err < 0)
2057 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03002058
Brian Gix0df4c182011-11-16 13:53:13 -08002059done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002060 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03002061 return err;
2062}
2063
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002064static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
2065 u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08002066{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002067 struct mgmt_cp_user_confirm_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08002068
2069 BT_DBG("");
2070
2071 if (len != sizeof(*cp))
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002072 return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002073 MGMT_STATUS_INVALID_PARAMS);
Brian Gix0df4c182011-11-16 13:53:13 -08002074
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002075 return user_pairing_resp(sk, hdev, &cp->addr.bdaddr, cp->addr.type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002076 MGMT_OP_USER_CONFIRM_REPLY,
2077 HCI_OP_USER_CONFIRM_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08002078}
2079
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002080static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002081 void *data, u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08002082{
Johan Hedbergc9c26592011-12-15 00:47:41 +02002083 struct mgmt_cp_user_confirm_neg_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08002084
2085 BT_DBG("");
2086
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002087 return user_pairing_resp(sk, hdev, &cp->addr.bdaddr, cp->addr.type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002088 MGMT_OP_USER_CONFIRM_NEG_REPLY,
2089 HCI_OP_USER_CONFIRM_NEG_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08002090}
2091
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002092static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
2093 u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08002094{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002095 struct mgmt_cp_user_passkey_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08002096
2097 BT_DBG("");
2098
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002099 return user_pairing_resp(sk, hdev, &cp->addr.bdaddr, cp->addr.type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002100 MGMT_OP_USER_PASSKEY_REPLY,
2101 HCI_OP_USER_PASSKEY_REPLY, cp->passkey);
Brian Gix604086b2011-11-23 08:28:33 -08002102}
2103
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002104static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002105 void *data, u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08002106{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002107 struct mgmt_cp_user_passkey_neg_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08002108
2109 BT_DBG("");
2110
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002111 return user_pairing_resp(sk, hdev, &cp->addr.bdaddr, cp->addr.type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002112 MGMT_OP_USER_PASSKEY_NEG_REPLY,
2113 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
Brian Gix604086b2011-11-23 08:28:33 -08002114}
2115
Johan Hedberg2b4bf392012-03-03 00:19:06 +02002116static int update_name(struct hci_dev *hdev, const char *name)
2117{
2118 struct hci_cp_write_local_name cp;
2119
2120 memcpy(cp.name, name, sizeof(cp.name));
2121
2122 return hci_send_cmd(hdev, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp);
2123}
2124
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002125static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002126 u16 len)
Johan Hedbergb312b1612011-03-16 14:29:37 +02002127{
Johan Hedberg2b4bf392012-03-03 00:19:06 +02002128 struct mgmt_cp_set_local_name *cp = data;
Johan Hedbergb312b1612011-03-16 14:29:37 +02002129 struct pending_cmd *cmd;
2130 int err;
2131
2132 BT_DBG("");
2133
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002134 hci_dev_lock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02002135
Johan Hedberg2b4bf392012-03-03 00:19:06 +02002136 memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02002137
Johan Hedbergb5235a62012-02-21 14:32:24 +02002138 if (!hdev_is_powered(hdev)) {
Johan Hedberg2b4bf392012-03-03 00:19:06 +02002139 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02002140
2141 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002142 data, len);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02002143 if (err < 0)
2144 goto failed;
2145
2146 err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002147 sk);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02002148
Johan Hedbergb5235a62012-02-21 14:32:24 +02002149 goto failed;
2150 }
2151
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02002152 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
Johan Hedbergb312b1612011-03-16 14:29:37 +02002153 if (!cmd) {
2154 err = -ENOMEM;
2155 goto failed;
2156 }
2157
Johan Hedberg2b4bf392012-03-03 00:19:06 +02002158 err = update_name(hdev, cp->name);
Johan Hedbergb312b1612011-03-16 14:29:37 +02002159 if (err < 0)
2160 mgmt_pending_remove(cmd);
2161
2162failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002163 hci_dev_unlock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02002164 return err;
2165}
2166
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02002167static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002168 void *data, u16 data_len)
Szymon Jancc35938b2011-03-22 13:12:21 +01002169{
Szymon Jancc35938b2011-03-22 13:12:21 +01002170 struct pending_cmd *cmd;
2171 int err;
2172
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002173 BT_DBG("%s", hdev->name);
Szymon Jancc35938b2011-03-22 13:12:21 +01002174
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002175 hci_dev_lock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01002176
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002177 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002178 err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002179 MGMT_STATUS_NOT_POWERED);
Szymon Jancc35938b2011-03-22 13:12:21 +01002180 goto unlock;
2181 }
2182
2183 if (!(hdev->features[6] & LMP_SIMPLE_PAIR)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002184 err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002185 MGMT_STATUS_NOT_SUPPORTED);
Szymon Jancc35938b2011-03-22 13:12:21 +01002186 goto unlock;
2187 }
2188
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002189 if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002190 err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002191 MGMT_STATUS_BUSY);
Szymon Jancc35938b2011-03-22 13:12:21 +01002192 goto unlock;
2193 }
2194
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002195 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
Szymon Jancc35938b2011-03-22 13:12:21 +01002196 if (!cmd) {
2197 err = -ENOMEM;
2198 goto unlock;
2199 }
2200
2201 err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
2202 if (err < 0)
2203 mgmt_pending_remove(cmd);
2204
2205unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002206 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01002207 return err;
2208}
2209
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002210static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002211 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01002212{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002213 struct mgmt_cp_add_remote_oob_data *cp = data;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02002214 u8 status;
Szymon Janc2763eda2011-03-22 13:12:22 +01002215 int err;
2216
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002217 BT_DBG("%s ", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01002218
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002219 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01002220
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002221 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002222 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002223 MGMT_STATUS_NOT_POWERED, &cp->addr,
2224 sizeof(cp->addr));
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002225 goto unlock;
2226 }
2227
Johan Hedberg664ce4c2012-02-09 15:44:09 +02002228 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, cp->hash,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002229 cp->randomizer);
Szymon Janc2763eda2011-03-22 13:12:22 +01002230 if (err < 0)
Johan Hedbergbf1e3542012-02-19 13:16:14 +02002231 status = MGMT_STATUS_FAILED;
Szymon Janc2763eda2011-03-22 13:12:22 +01002232 else
Johan Hedbergbf1e3542012-02-19 13:16:14 +02002233 status = 0;
2234
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002235 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, status,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002236 &cp->addr, sizeof(cp->addr));
Szymon Janc2763eda2011-03-22 13:12:22 +01002237
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002238unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002239 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01002240 return err;
2241}
2242
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002243static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002244 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01002245{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002246 struct mgmt_cp_remove_remote_oob_data *cp = data;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02002247 u8 status;
Szymon Janc2763eda2011-03-22 13:12:22 +01002248 int err;
2249
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002250 BT_DBG("%s", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01002251
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002252 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01002253
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002254 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002255 err = cmd_complete(sk, hdev->id,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002256 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
2257 MGMT_STATUS_NOT_POWERED, &cp->addr,
2258 sizeof(cp->addr));
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002259 goto unlock;
2260 }
2261
Johan Hedberg664ce4c2012-02-09 15:44:09 +02002262 err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr);
Szymon Janc2763eda2011-03-22 13:12:22 +01002263 if (err < 0)
Johan Hedbergbf1e3542012-02-19 13:16:14 +02002264 status = MGMT_STATUS_INVALID_PARAMS;
Szymon Janc2763eda2011-03-22 13:12:22 +01002265 else
Johan Hedbergbf1e3542012-02-19 13:16:14 +02002266 status = 0;
2267
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002268 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002269 status, &cp->addr, sizeof(cp->addr));
Szymon Janc2763eda2011-03-22 13:12:22 +01002270
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002271unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002272 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01002273 return err;
2274}
2275
Andre Guedes5e0452c2012-02-17 20:39:38 -03002276int mgmt_interleaved_discovery(struct hci_dev *hdev)
2277{
2278 int err;
2279
2280 BT_DBG("%s", hdev->name);
2281
2282 hci_dev_lock(hdev);
2283
2284 err = hci_do_inquiry(hdev, INQUIRY_LEN_BREDR_LE);
2285 if (err < 0)
2286 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2287
2288 hci_dev_unlock(hdev);
2289
2290 return err;
2291}
2292
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002293static int start_discovery(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002294 void *data, u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04002295{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002296 struct mgmt_cp_start_discovery *cp = data;
Johan Hedberg14a53662011-04-27 10:29:56 -04002297 struct pending_cmd *cmd;
Johan Hedberg14a53662011-04-27 10:29:56 -04002298 int err;
2299
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002300 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04002301
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002302 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04002303
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002304 if (!hdev_is_powered(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002305 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002306 MGMT_STATUS_NOT_POWERED);
Johan Hedbergbd2d1332011-11-07 23:13:37 +02002307 goto failed;
2308 }
2309
Andre Guedes642be6c2012-03-21 00:03:37 -03002310 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) {
2311 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
2312 MGMT_STATUS_BUSY);
2313 goto failed;
2314 }
2315
Johan Hedbergff9ef572012-01-04 14:23:45 +02002316 if (hdev->discovery.state != DISCOVERY_STOPPED) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002317 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002318 MGMT_STATUS_BUSY);
Johan Hedbergff9ef572012-01-04 14:23:45 +02002319 goto failed;
2320 }
2321
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002322 cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0);
Johan Hedberg14a53662011-04-27 10:29:56 -04002323 if (!cmd) {
2324 err = -ENOMEM;
2325 goto failed;
2326 }
2327
Andre Guedes4aab14e2012-02-17 20:39:36 -03002328 hdev->discovery.type = cp->type;
2329
2330 switch (hdev->discovery.type) {
Andre Guedesf39799f2012-02-17 20:39:35 -03002331 case DISCOV_TYPE_BREDR:
Andre Guedes8b901292012-02-23 18:09:27 -03002332 if (lmp_bredr_capable(hdev))
2333 err = hci_do_inquiry(hdev, INQUIRY_LEN_BREDR);
2334 else
2335 err = -ENOTSUPP;
Andre Guedesf39799f2012-02-17 20:39:35 -03002336 break;
2337
2338 case DISCOV_TYPE_LE:
Andre Guedes8b901292012-02-23 18:09:27 -03002339 if (lmp_host_le_capable(hdev))
2340 err = hci_le_scan(hdev, LE_SCAN_TYPE, LE_SCAN_INT,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002341 LE_SCAN_WIN, LE_SCAN_TIMEOUT_LE_ONLY);
Andre Guedes8b901292012-02-23 18:09:27 -03002342 else
2343 err = -ENOTSUPP;
Andre Guedesf39799f2012-02-17 20:39:35 -03002344 break;
2345
Andre Guedes5e0452c2012-02-17 20:39:38 -03002346 case DISCOV_TYPE_INTERLEAVED:
Andre Guedes426c1892012-02-24 11:41:04 -03002347 if (lmp_host_le_capable(hdev) && lmp_bredr_capable(hdev))
2348 err = hci_le_scan(hdev, LE_SCAN_TYPE, LE_SCAN_INT,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002349 LE_SCAN_WIN,
2350 LE_SCAN_TIMEOUT_BREDR_LE);
Andre Guedes426c1892012-02-24 11:41:04 -03002351 else
2352 err = -ENOTSUPP;
Andre Guedes5e0452c2012-02-17 20:39:38 -03002353 break;
2354
Andre Guedesf39799f2012-02-17 20:39:35 -03002355 default:
Andre Guedes3fd24152012-02-03 17:48:01 -03002356 err = -EINVAL;
Andre Guedesf39799f2012-02-17 20:39:35 -03002357 }
Andre Guedes3fd24152012-02-03 17:48:01 -03002358
Johan Hedberg14a53662011-04-27 10:29:56 -04002359 if (err < 0)
2360 mgmt_pending_remove(cmd);
Johan Hedbergff9ef572012-01-04 14:23:45 +02002361 else
2362 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
Johan Hedberg14a53662011-04-27 10:29:56 -04002363
2364failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002365 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04002366 return err;
2367}
2368
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002369static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002370 u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04002371{
Johan Hedbergd9306502012-02-20 23:25:18 +02002372 struct mgmt_cp_stop_discovery *mgmt_cp = data;
Johan Hedberg14a53662011-04-27 10:29:56 -04002373 struct pending_cmd *cmd;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002374 struct hci_cp_remote_name_req_cancel cp;
2375 struct inquiry_entry *e;
Johan Hedberg14a53662011-04-27 10:29:56 -04002376 int err;
2377
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002378 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04002379
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002380 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04002381
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002382 if (!hci_discovery_active(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002383 err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002384 MGMT_STATUS_REJECTED, &mgmt_cp->type,
2385 sizeof(mgmt_cp->type));
Johan Hedbergd9306502012-02-20 23:25:18 +02002386 goto unlock;
2387 }
2388
2389 if (hdev->discovery.type != mgmt_cp->type) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002390 err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002391 MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type,
2392 sizeof(mgmt_cp->type));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002393 goto unlock;
Johan Hedbergff9ef572012-01-04 14:23:45 +02002394 }
2395
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002396 cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0);
Johan Hedberg14a53662011-04-27 10:29:56 -04002397 if (!cmd) {
2398 err = -ENOMEM;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002399 goto unlock;
Johan Hedberg14a53662011-04-27 10:29:56 -04002400 }
2401
Andre Guedese0d9727e2012-03-20 15:15:36 -03002402 switch (hdev->discovery.state) {
2403 case DISCOVERY_FINDING:
Andre Guedesc9ecc482012-03-15 16:52:08 -03002404 if (test_bit(HCI_INQUIRY, &hdev->flags))
2405 err = hci_cancel_inquiry(hdev);
2406 else
2407 err = hci_cancel_le_scan(hdev);
2408
Andre Guedese0d9727e2012-03-20 15:15:36 -03002409 break;
2410
2411 case DISCOVERY_RESOLVING:
2412 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
2413 NAME_PENDING);
2414 if (!e) {
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002415 mgmt_pending_remove(cmd);
Andre Guedese0d9727e2012-03-20 15:15:36 -03002416 err = cmd_complete(sk, hdev->id,
2417 MGMT_OP_STOP_DISCOVERY, 0,
2418 &mgmt_cp->type,
2419 sizeof(mgmt_cp->type));
2420 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2421 goto unlock;
2422 }
2423
2424 bacpy(&cp.bdaddr, &e->data.bdaddr);
2425 err = hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
2426 sizeof(cp), &cp);
2427
2428 break;
2429
2430 default:
2431 BT_DBG("unknown discovery state %u", hdev->discovery.state);
2432 err = -EFAULT;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002433 }
2434
Johan Hedberg14a53662011-04-27 10:29:56 -04002435 if (err < 0)
2436 mgmt_pending_remove(cmd);
Johan Hedbergff9ef572012-01-04 14:23:45 +02002437 else
2438 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
Johan Hedberg14a53662011-04-27 10:29:56 -04002439
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002440unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002441 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04002442 return err;
2443}
2444
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002445static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002446 u16 len)
Johan Hedberg561aafb2012-01-04 13:31:59 +02002447{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002448 struct mgmt_cp_confirm_name *cp = data;
Johan Hedberg561aafb2012-01-04 13:31:59 +02002449 struct inquiry_entry *e;
Johan Hedberg561aafb2012-01-04 13:31:59 +02002450 int err;
2451
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002452 BT_DBG("%s", hdev->name);
Johan Hedberg561aafb2012-01-04 13:31:59 +02002453
Johan Hedberg561aafb2012-01-04 13:31:59 +02002454 hci_dev_lock(hdev);
2455
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002456 if (!hci_discovery_active(hdev)) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002457 err = cmd_status(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002458 MGMT_STATUS_FAILED);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002459 goto failed;
2460 }
2461
Johan Hedberga198e7b2012-02-17 14:27:06 +02002462 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
Johan Hedberg561aafb2012-01-04 13:31:59 +02002463 if (!e) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002464 err = cmd_status(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002465 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg561aafb2012-01-04 13:31:59 +02002466 goto failed;
2467 }
2468
2469 if (cp->name_known) {
2470 e->name_state = NAME_KNOWN;
2471 list_del(&e->list);
2472 } else {
2473 e->name_state = NAME_NEEDED;
Johan Hedberga3d4e202012-01-09 00:53:02 +02002474 hci_inquiry_cache_update_resolve(hdev, e);
Johan Hedberg561aafb2012-01-04 13:31:59 +02002475 }
2476
2477 err = 0;
2478
2479failed:
2480 hci_dev_unlock(hdev);
Johan Hedberg561aafb2012-01-04 13:31:59 +02002481 return err;
2482}
2483
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002484static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002485 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03002486{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002487 struct mgmt_cp_block_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02002488 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03002489 int err;
2490
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002491 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03002492
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002493 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03002494
Johan Hedberg88c1fe42012-02-09 15:56:11 +02002495 err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type);
Antti Julku7fbec222011-06-15 12:01:15 +03002496 if (err < 0)
Johan Hedbergf0eeea82012-02-19 12:58:54 +02002497 status = MGMT_STATUS_FAILED;
Antti Julku7fbec222011-06-15 12:01:15 +03002498 else
Johan Hedbergf0eeea82012-02-19 12:58:54 +02002499 status = 0;
2500
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002501 err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002502 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03002503
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002504 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03002505
2506 return err;
2507}
2508
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002509static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002510 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03002511{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002512 struct mgmt_cp_unblock_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02002513 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03002514 int err;
2515
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002516 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03002517
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002518 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03002519
Johan Hedberg88c1fe42012-02-09 15:56:11 +02002520 err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type);
Antti Julku7fbec222011-06-15 12:01:15 +03002521 if (err < 0)
Johan Hedbergf0eeea82012-02-19 12:58:54 +02002522 status = MGMT_STATUS_INVALID_PARAMS;
Antti Julku7fbec222011-06-15 12:01:15 +03002523 else
Johan Hedbergf0eeea82012-02-19 12:58:54 +02002524 status = 0;
2525
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002526 err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002527 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03002528
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002529 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03002530
2531 return err;
2532}
2533
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07002534static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
2535 u16 len)
2536{
2537 struct mgmt_cp_set_device_id *cp = data;
2538 int err;
Szymon Jancc72d4b82012-03-16 16:02:57 +01002539 __u16 source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07002540
2541 BT_DBG("%s", hdev->name);
2542
Szymon Jancc72d4b82012-03-16 16:02:57 +01002543 source = __le16_to_cpu(cp->source);
2544
2545 if (source > 0x0002)
2546 return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
2547 MGMT_STATUS_INVALID_PARAMS);
2548
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07002549 hci_dev_lock(hdev);
2550
Szymon Jancc72d4b82012-03-16 16:02:57 +01002551 hdev->devid_source = source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07002552 hdev->devid_vendor = __le16_to_cpu(cp->vendor);
2553 hdev->devid_product = __le16_to_cpu(cp->product);
2554 hdev->devid_version = __le16_to_cpu(cp->version);
2555
2556 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0);
2557
2558 update_eir(hdev);
2559
2560 hci_dev_unlock(hdev);
2561
2562 return err;
2563}
2564
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002565static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002566 void *data, u16 len)
Antti Julkuf6422ec2011-06-22 13:11:56 +03002567{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002568 struct mgmt_mode *cp = data;
Antti Julkuf6422ec2011-06-22 13:11:56 +03002569 struct hci_cp_write_page_scan_activity acp;
2570 u8 type;
2571 int err;
2572
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002573 BT_DBG("%s", hdev->name);
Antti Julkuf6422ec2011-06-22 13:11:56 +03002574
Johan Hedberg5400c042012-02-21 16:40:33 +02002575 if (!hdev_is_powered(hdev))
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002576 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002577 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5400c042012-02-21 16:40:33 +02002578
2579 if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002580 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002581 MGMT_STATUS_REJECTED);
Antti Julkuf6422ec2011-06-22 13:11:56 +03002582
2583 hci_dev_lock(hdev);
2584
Johan Hedbergf7c68692011-12-15 00:47:36 +02002585 if (cp->val) {
Antti Julkuf6422ec2011-06-22 13:11:56 +03002586 type = PAGE_SCAN_TYPE_INTERLACED;
Andrei Emeltchenko76ec9de2012-03-12 12:13:11 +02002587
2588 /* 22.5 msec page scan interval */
2589 acp.interval = __constant_cpu_to_le16(0x0024);
Antti Julkuf6422ec2011-06-22 13:11:56 +03002590 } else {
2591 type = PAGE_SCAN_TYPE_STANDARD; /* default */
Andrei Emeltchenko76ec9de2012-03-12 12:13:11 +02002592
2593 /* default 1.28 sec page scan */
2594 acp.interval = __constant_cpu_to_le16(0x0800);
Antti Julkuf6422ec2011-06-22 13:11:56 +03002595 }
2596
Andrei Emeltchenko76ec9de2012-03-12 12:13:11 +02002597 /* default 11.25 msec page scan window */
2598 acp.window = __constant_cpu_to_le16(0x0012);
Antti Julkuf6422ec2011-06-22 13:11:56 +03002599
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002600 err = hci_send_cmd(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, sizeof(acp),
2601 &acp);
Antti Julkuf6422ec2011-06-22 13:11:56 +03002602 if (err < 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002603 err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002604 MGMT_STATUS_FAILED);
Antti Julkuf6422ec2011-06-22 13:11:56 +03002605 goto done;
2606 }
2607
2608 err = hci_send_cmd(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type);
2609 if (err < 0) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002610 err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002611 MGMT_STATUS_FAILED);
Antti Julkuf6422ec2011-06-22 13:11:56 +03002612 goto done;
2613 }
2614
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002615 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002616 NULL, 0);
Antti Julkuf6422ec2011-06-22 13:11:56 +03002617done:
2618 hci_dev_unlock(hdev);
Antti Julkuf6422ec2011-06-22 13:11:56 +03002619 return err;
2620}
2621
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002622static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002623 void *cp_data, u16 len)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03002624{
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03002625 struct mgmt_cp_load_long_term_keys *cp = cp_data;
2626 u16 key_count, expected_len;
2627 int i;
2628
Marcel Holtmann1f350c82012-03-12 20:31:08 -07002629 key_count = __le16_to_cpu(cp->key_count);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03002630
2631 expected_len = sizeof(*cp) + key_count *
2632 sizeof(struct mgmt_ltk_info);
2633 if (expected_len != len) {
2634 BT_ERR("load_keys: expected %u bytes, got %u bytes",
2635 len, expected_len);
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002636 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002637 EINVAL);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03002638 }
2639
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002640 BT_DBG("%s key_count %u", hdev->name, key_count);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03002641
2642 hci_dev_lock(hdev);
2643
2644 hci_smp_ltks_clear(hdev);
2645
2646 for (i = 0; i < key_count; i++) {
2647 struct mgmt_ltk_info *key = &cp->keys[i];
2648 u8 type;
2649
2650 if (key->master)
2651 type = HCI_SMP_LTK;
2652 else
2653 type = HCI_SMP_LTK_SLAVE;
2654
Hemant Gupta4596fde2012-04-16 14:57:40 +05302655 hci_add_ltk(hdev, &key->addr.bdaddr,
Andre Guedes378b5b72012-04-24 21:02:51 -03002656 bdaddr_to_le(key->addr.type),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002657 type, 0, key->authenticated, key->val,
2658 key->enc_size, key->ediv, key->rand);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03002659 }
2660
2661 hci_dev_unlock(hdev);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03002662
2663 return 0;
2664}
2665
Andrei Emeltchenko2e3c35e2012-03-14 18:54:15 +02002666static const struct mgmt_handler {
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002667 int (*func) (struct sock *sk, struct hci_dev *hdev, void *data,
2668 u16 data_len);
Johan Hedbergbe22b542012-03-01 22:24:41 +02002669 bool var_len;
2670 size_t data_len;
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02002671} mgmt_handlers[] = {
2672 { NULL }, /* 0x0000 (no command) */
Johan Hedbergbe22b542012-03-01 22:24:41 +02002673 { read_version, false, MGMT_READ_VERSION_SIZE },
2674 { read_commands, false, MGMT_READ_COMMANDS_SIZE },
2675 { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE },
2676 { read_controller_info, false, MGMT_READ_INFO_SIZE },
2677 { set_powered, false, MGMT_SETTING_SIZE },
2678 { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE },
2679 { set_connectable, false, MGMT_SETTING_SIZE },
2680 { set_fast_connectable, false, MGMT_SETTING_SIZE },
2681 { set_pairable, false, MGMT_SETTING_SIZE },
2682 { set_link_security, false, MGMT_SETTING_SIZE },
2683 { set_ssp, false, MGMT_SETTING_SIZE },
2684 { set_hs, false, MGMT_SETTING_SIZE },
2685 { set_le, false, MGMT_SETTING_SIZE },
2686 { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE },
2687 { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE },
2688 { add_uuid, false, MGMT_ADD_UUID_SIZE },
2689 { remove_uuid, false, MGMT_REMOVE_UUID_SIZE },
2690 { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE },
2691 { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE },
2692 { disconnect, false, MGMT_DISCONNECT_SIZE },
2693 { get_connections, false, MGMT_GET_CONNECTIONS_SIZE },
2694 { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE },
2695 { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE },
2696 { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE },
2697 { pair_device, false, MGMT_PAIR_DEVICE_SIZE },
2698 { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE },
2699 { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE },
2700 { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE },
2701 { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
2702 { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE },
2703 { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
2704 { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE },
2705 { add_remote_oob_data, false, MGMT_ADD_REMOTE_OOB_DATA_SIZE },
2706 { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
2707 { start_discovery, false, MGMT_START_DISCOVERY_SIZE },
2708 { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE },
2709 { confirm_name, false, MGMT_CONFIRM_NAME_SIZE },
2710 { block_device, false, MGMT_BLOCK_DEVICE_SIZE },
2711 { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE },
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07002712 { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE },
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02002713};
2714
2715
Johan Hedberg03811012010-12-08 00:21:06 +02002716int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
2717{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002718 void *buf;
2719 u8 *cp;
Johan Hedberg03811012010-12-08 00:21:06 +02002720 struct mgmt_hdr *hdr;
Szymon Janc4e51eae2011-02-25 19:05:48 +01002721 u16 opcode, index, len;
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002722 struct hci_dev *hdev = NULL;
Andrei Emeltchenko2e3c35e2012-03-14 18:54:15 +02002723 const struct mgmt_handler *handler;
Johan Hedberg03811012010-12-08 00:21:06 +02002724 int err;
2725
2726 BT_DBG("got %zu bytes", msglen);
2727
2728 if (msglen < sizeof(*hdr))
2729 return -EINVAL;
2730
Gustavo F. Padovane63a15e2011-04-04 18:56:53 -03002731 buf = kmalloc(msglen, GFP_KERNEL);
Johan Hedberg03811012010-12-08 00:21:06 +02002732 if (!buf)
2733 return -ENOMEM;
2734
2735 if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) {
2736 err = -EFAULT;
2737 goto done;
2738 }
2739
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002740 hdr = buf;
Marcel Holtmann1f350c82012-03-12 20:31:08 -07002741 opcode = __le16_to_cpu(hdr->opcode);
2742 index = __le16_to_cpu(hdr->index);
2743 len = __le16_to_cpu(hdr->len);
Johan Hedberg03811012010-12-08 00:21:06 +02002744
2745 if (len != msglen - sizeof(*hdr)) {
2746 err = -EINVAL;
2747 goto done;
2748 }
2749
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02002750 if (index != MGMT_INDEX_NONE) {
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002751 hdev = hci_dev_get(index);
2752 if (!hdev) {
2753 err = cmd_status(sk, index, opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002754 MGMT_STATUS_INVALID_INDEX);
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002755 goto done;
2756 }
2757 }
2758
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02002759 if (opcode >= ARRAY_SIZE(mgmt_handlers) ||
2760 mgmt_handlers[opcode].func == NULL) {
Johan Hedberg03811012010-12-08 00:21:06 +02002761 BT_DBG("Unknown op %u", opcode);
Johan Hedbergca69b792011-11-11 18:10:00 +02002762 err = cmd_status(sk, index, opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002763 MGMT_STATUS_UNKNOWN_COMMAND);
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02002764 goto done;
Johan Hedberg03811012010-12-08 00:21:06 +02002765 }
2766
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02002767 if ((hdev && opcode < MGMT_OP_READ_INFO) ||
2768 (!hdev && opcode >= MGMT_OP_READ_INFO)) {
2769 err = cmd_status(sk, index, opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002770 MGMT_STATUS_INVALID_INDEX);
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02002771 goto done;
2772 }
2773
Johan Hedbergbe22b542012-03-01 22:24:41 +02002774 handler = &mgmt_handlers[opcode];
2775
2776 if ((handler->var_len && len < handler->data_len) ||
2777 (!handler->var_len && len != handler->data_len)) {
2778 err = cmd_status(sk, index, opcode,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002779 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergbe22b542012-03-01 22:24:41 +02002780 goto done;
2781 }
2782
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02002783 if (hdev)
2784 mgmt_init_hdev(sk, hdev);
2785
2786 cp = buf + sizeof(*hdr);
2787
Johan Hedbergbe22b542012-03-01 22:24:41 +02002788 err = handler->func(sk, hdev, cp, len);
Johan Hedberge41d8b42010-12-13 21:07:03 +02002789 if (err < 0)
2790 goto done;
2791
Johan Hedberg03811012010-12-08 00:21:06 +02002792 err = msglen;
2793
2794done:
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002795 if (hdev)
2796 hci_dev_put(hdev);
2797
Johan Hedberg03811012010-12-08 00:21:06 +02002798 kfree(buf);
2799 return err;
2800}
Johan Hedbergc71e97b2010-12-13 21:07:07 +02002801
Johan Hedbergb24752f2011-11-03 14:40:33 +02002802static void cmd_status_rsp(struct pending_cmd *cmd, void *data)
2803{
2804 u8 *status = data;
2805
2806 cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
2807 mgmt_pending_remove(cmd);
2808}
2809
Johan Hedberg744cf192011-11-08 20:40:14 +02002810int mgmt_index_added(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02002811{
Johan Hedberg744cf192011-11-08 20:40:14 +02002812 return mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL);
Johan Hedbergc71e97b2010-12-13 21:07:07 +02002813}
2814
Johan Hedberg744cf192011-11-08 20:40:14 +02002815int mgmt_index_removed(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02002816{
Johan Hedberg5f159032012-03-02 03:13:19 +02002817 u8 status = MGMT_STATUS_INVALID_INDEX;
Johan Hedbergb24752f2011-11-03 14:40:33 +02002818
Johan Hedberg744cf192011-11-08 20:40:14 +02002819 mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status);
Johan Hedbergb24752f2011-11-03 14:40:33 +02002820
Johan Hedberg744cf192011-11-08 20:40:14 +02002821 return mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002822}
2823
Johan Hedberg73f22f62010-12-29 16:00:25 +02002824struct cmd_lookup {
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002825 struct sock *sk;
Johan Hedberg69ab39e2011-12-15 00:47:35 +02002826 struct hci_dev *hdev;
Johan Hedberg90e70452012-02-23 23:09:40 +02002827 u8 mgmt_status;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002828};
2829
Johan Hedberg69ab39e2011-12-15 00:47:35 +02002830static void settings_rsp(struct pending_cmd *cmd, void *data)
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002831{
Johan Hedberg73f22f62010-12-29 16:00:25 +02002832 struct cmd_lookup *match = data;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002833
Johan Hedberg69ab39e2011-12-15 00:47:35 +02002834 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002835
2836 list_del(&cmd->list);
2837
2838 if (match->sk == NULL) {
2839 match->sk = cmd->sk;
2840 sock_hold(match->sk);
2841 }
2842
2843 mgmt_pending_free(cmd);
Johan Hedbergc71e97b2010-12-13 21:07:07 +02002844}
Johan Hedberg5add6af2010-12-16 10:00:37 +02002845
Johan Hedberg744cf192011-11-08 20:40:14 +02002846int mgmt_powered(struct hci_dev *hdev, u8 powered)
Johan Hedberg5add6af2010-12-16 10:00:37 +02002847{
Johan Hedberg76a7f3a2012-02-17 00:34:40 +02002848 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg7bb895d2012-02-17 01:20:00 +02002849 int err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02002850
Johan Hedberg5e5282b2012-02-21 16:01:30 +02002851 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
2852 return 0;
2853
Johan Hedberg69ab39e2011-12-15 00:47:35 +02002854 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
Johan Hedberg5add6af2010-12-16 10:00:37 +02002855
Johan Hedberg5e5282b2012-02-21 16:01:30 +02002856 if (powered) {
2857 u8 scan = 0;
2858
2859 if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
2860 scan |= SCAN_PAGE;
2861 if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
2862 scan |= SCAN_INQUIRY;
2863
2864 if (scan)
2865 hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
Johan Hedberg504c8dc2012-02-23 13:30:41 +02002866
2867 update_class(hdev);
Johan Hedberg2b4bf392012-03-03 00:19:06 +02002868 update_name(hdev, hdev->dev_name);
Johan Hedberg504c8dc2012-02-23 13:30:41 +02002869 update_eir(hdev);
Johan Hedberg5e5282b2012-02-21 16:01:30 +02002870 } else {
Johan Hedbergd4f68522012-03-02 03:07:07 +02002871 u8 status = MGMT_STATUS_NOT_POWERED;
Johan Hedberg744cf192011-11-08 20:40:14 +02002872 mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status);
Johan Hedbergb24752f2011-11-03 14:40:33 +02002873 }
2874
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02002875 err = new_settings(hdev, match.sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002876
2877 if (match.sk)
2878 sock_put(match.sk);
2879
Johan Hedberg7bb895d2012-02-17 01:20:00 +02002880 return err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02002881}
Johan Hedberg73f22f62010-12-29 16:00:25 +02002882
Johan Hedberg744cf192011-11-08 20:40:14 +02002883int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable)
Johan Hedberg73f22f62010-12-29 16:00:25 +02002884{
Johan Hedberg76a7f3a2012-02-17 00:34:40 +02002885 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg5e5282b2012-02-21 16:01:30 +02002886 bool changed = false;
2887 int err = 0;
Johan Hedberg73f22f62010-12-29 16:00:25 +02002888
Johan Hedberg5e5282b2012-02-21 16:01:30 +02002889 if (discoverable) {
2890 if (!test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
2891 changed = true;
2892 } else {
2893 if (test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
2894 changed = true;
2895 }
Johan Hedberg73f22f62010-12-29 16:00:25 +02002896
Johan Hedberged9b5f22012-02-21 20:47:06 +02002897 mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, settings_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002898 &match);
Johan Hedberged9b5f22012-02-21 20:47:06 +02002899
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02002900 if (changed)
2901 err = new_settings(hdev, match.sk);
Johan Hedberg5e5282b2012-02-21 16:01:30 +02002902
Johan Hedberg73f22f62010-12-29 16:00:25 +02002903 if (match.sk)
2904 sock_put(match.sk);
2905
Johan Hedberg7bb895d2012-02-17 01:20:00 +02002906 return err;
Johan Hedberg73f22f62010-12-29 16:00:25 +02002907}
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02002908
Johan Hedberg744cf192011-11-08 20:40:14 +02002909int mgmt_connectable(struct hci_dev *hdev, u8 connectable)
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02002910{
Johan Hedberg76a7f3a2012-02-17 00:34:40 +02002911 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg5e5282b2012-02-21 16:01:30 +02002912 bool changed = false;
2913 int err = 0;
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02002914
Johan Hedberg5e5282b2012-02-21 16:01:30 +02002915 if (connectable) {
2916 if (!test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags))
2917 changed = true;
2918 } else {
2919 if (test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags))
2920 changed = true;
2921 }
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02002922
Johan Hedberged9b5f22012-02-21 20:47:06 +02002923 mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, settings_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002924 &match);
Johan Hedberged9b5f22012-02-21 20:47:06 +02002925
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02002926 if (changed)
2927 err = new_settings(hdev, match.sk);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02002928
2929 if (match.sk)
2930 sock_put(match.sk);
2931
Johan Hedberg7bb895d2012-02-17 01:20:00 +02002932 return err;
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02002933}
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002934
Johan Hedberg744cf192011-11-08 20:40:14 +02002935int mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status)
Johan Hedberg2d7cee52011-11-07 22:16:03 +02002936{
Johan Hedbergca69b792011-11-11 18:10:00 +02002937 u8 mgmt_err = mgmt_status(status);
2938
Johan Hedberg2d7cee52011-11-07 22:16:03 +02002939 if (scan & SCAN_PAGE)
Johan Hedberg744cf192011-11-08 20:40:14 +02002940 mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002941 cmd_status_rsp, &mgmt_err);
Johan Hedberg2d7cee52011-11-07 22:16:03 +02002942
2943 if (scan & SCAN_INQUIRY)
Johan Hedberg744cf192011-11-08 20:40:14 +02002944 mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002945 cmd_status_rsp, &mgmt_err);
Johan Hedberg2d7cee52011-11-07 22:16:03 +02002946
2947 return 0;
2948}
2949
Vishal Agarwal745c0ce2012-04-13 17:43:22 +05302950int mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, bool persistent)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002951{
Johan Hedberg86742e12011-11-07 23:13:38 +02002952 struct mgmt_ev_new_link_key ev;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002953
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002954 memset(&ev, 0, sizeof(ev));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002955
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002956 ev.store_hint = persistent;
Johan Hedbergd753fdc2012-02-17 14:06:34 +02002957 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03002958 ev.key.addr.type = BDADDR_BREDR;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002959 ev.key.type = key->type;
2960 memcpy(ev.key.val, key->val, 16);
2961 ev.key.pin_len = key->pin_len;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002962
Johan Hedberg744cf192011-11-08 20:40:14 +02002963 return mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002964}
Johan Hedbergf7520542011-01-20 12:34:39 +02002965
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03002966int mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, u8 persistent)
2967{
2968 struct mgmt_ev_new_long_term_key ev;
2969
2970 memset(&ev, 0, sizeof(ev));
2971
2972 ev.store_hint = persistent;
2973 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03002974 ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03002975 ev.key.authenticated = key->authenticated;
2976 ev.key.enc_size = key->enc_size;
2977 ev.key.ediv = key->ediv;
2978
2979 if (key->type == HCI_SMP_LTK)
2980 ev.key.master = 1;
2981
2982 memcpy(ev.key.rand, key->rand, sizeof(key->rand));
2983 memcpy(ev.key.val, key->val, sizeof(key->val));
2984
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002985 return mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev),
2986 NULL);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03002987}
2988
Johan Hedbergafc747a2012-01-15 18:11:07 +02002989int mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002990 u8 addr_type, u32 flags, u8 *name, u8 name_len,
2991 u8 *dev_class)
Johan Hedbergf7520542011-01-20 12:34:39 +02002992{
Johan Hedbergb644ba32012-01-17 21:48:47 +02002993 char buf[512];
2994 struct mgmt_ev_device_connected *ev = (void *) buf;
2995 u16 eir_len = 0;
Johan Hedbergf7520542011-01-20 12:34:39 +02002996
Johan Hedbergb644ba32012-01-17 21:48:47 +02002997 bacpy(&ev->addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03002998 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergf7520542011-01-20 12:34:39 +02002999
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02003000 ev->flags = __cpu_to_le32(flags);
Johan Hedberg08c79b62012-02-23 22:31:51 +02003001
Johan Hedbergb644ba32012-01-17 21:48:47 +02003002 if (name_len > 0)
3003 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003004 name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003005
3006 if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0)
Brian Gix53156382012-03-09 14:07:03 -08003007 eir_len = eir_append_data(ev->eir, eir_len,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003008 EIR_CLASS_OF_DEV, dev_class, 3);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003009
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02003010 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003011
3012 return mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003013 sizeof(*ev) + eir_len, NULL);
Johan Hedbergf7520542011-01-20 12:34:39 +02003014}
3015
Johan Hedberg8962ee72011-01-20 12:40:27 +02003016static void disconnect_rsp(struct pending_cmd *cmd, void *data)
3017{
Szymon Jancc68fb7f2011-03-22 13:12:19 +01003018 struct mgmt_cp_disconnect *cp = cmd->param;
Johan Hedberg8962ee72011-01-20 12:40:27 +02003019 struct sock **sk = data;
Johan Hedberga38528f2011-01-22 06:46:43 +02003020 struct mgmt_rp_disconnect rp;
Johan Hedberg8962ee72011-01-20 12:40:27 +02003021
Johan Hedberg88c3df12012-02-09 14:27:38 +02003022 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3023 rp.addr.type = cp->addr.type;
Johan Hedberg8962ee72011-01-20 12:40:27 +02003024
Johan Hedbergaee9b2182012-02-18 15:07:59 +02003025 cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003026 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02003027
3028 *sk = cmd->sk;
3029 sock_hold(*sk);
3030
Johan Hedberga664b5b2011-02-19 12:06:02 -03003031 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003032}
3033
Johan Hedberg124f6e32012-02-09 13:50:12 +02003034static void unpair_device_rsp(struct pending_cmd *cmd, void *data)
Johan Hedberga8a1d192011-11-10 15:54:38 +02003035{
Johan Hedbergb1078ad2012-02-09 17:21:16 +02003036 struct hci_dev *hdev = data;
Johan Hedberg124f6e32012-02-09 13:50:12 +02003037 struct mgmt_cp_unpair_device *cp = cmd->param;
3038 struct mgmt_rp_unpair_device rp;
Johan Hedberga8a1d192011-11-10 15:54:38 +02003039
3040 memset(&rp, 0, sizeof(rp));
Johan Hedberg124f6e32012-02-09 13:50:12 +02003041 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3042 rp.addr.type = cp->addr.type;
Johan Hedberga8a1d192011-11-10 15:54:38 +02003043
Johan Hedbergb1078ad2012-02-09 17:21:16 +02003044 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
3045
Johan Hedbergaee9b2182012-02-18 15:07:59 +02003046 cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp));
Johan Hedberga8a1d192011-11-10 15:54:38 +02003047
3048 mgmt_pending_remove(cmd);
3049}
3050
Johan Hedbergafc747a2012-01-15 18:11:07 +02003051int mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003052 u8 link_type, u8 addr_type)
Johan Hedbergf7520542011-01-20 12:34:39 +02003053{
Johan Hedberg4c659c32011-11-07 23:13:39 +02003054 struct mgmt_addr_info ev;
Johan Hedberg8962ee72011-01-20 12:40:27 +02003055 struct sock *sk = NULL;
3056 int err;
3057
Johan Hedberg744cf192011-11-08 20:40:14 +02003058 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
Johan Hedbergf7520542011-01-20 12:34:39 +02003059
Johan Hedbergf7520542011-01-20 12:34:39 +02003060 bacpy(&ev.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03003061 ev.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergf7520542011-01-20 12:34:39 +02003062
Johan Hedbergafc747a2012-01-15 18:11:07 +02003063 err = mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003064 sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003065
3066 if (sk)
Szymon Jancd97dcb62012-03-16 16:02:56 +01003067 sock_put(sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003068
Johan Hedberg124f6e32012-02-09 13:50:12 +02003069 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003070 hdev);
Johan Hedberga8a1d192011-11-10 15:54:38 +02003071
Johan Hedberg8962ee72011-01-20 12:40:27 +02003072 return err;
3073}
3074
Johan Hedberg88c3df12012-02-09 14:27:38 +02003075int mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003076 u8 link_type, u8 addr_type, u8 status)
Johan Hedberg8962ee72011-01-20 12:40:27 +02003077{
Johan Hedberg88c3df12012-02-09 14:27:38 +02003078 struct mgmt_rp_disconnect rp;
Johan Hedberg8962ee72011-01-20 12:40:27 +02003079 struct pending_cmd *cmd;
3080 int err;
3081
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003082 cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003083 if (!cmd)
3084 return -ENOENT;
3085
Johan Hedberg88c3df12012-02-09 14:27:38 +02003086 bacpy(&rp.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03003087 rp.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberg37d9ef72011-11-10 15:54:39 +02003088
Johan Hedberg88c3df12012-02-09 14:27:38 +02003089 err = cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003090 mgmt_status(status), &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02003091
Johan Hedberga664b5b2011-02-19 12:06:02 -03003092 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003093
Johan Hedbergb1078ad2012-02-09 17:21:16 +02003094 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
3095 hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02003096 return err;
Johan Hedbergf7520542011-01-20 12:34:39 +02003097}
Johan Hedberg17d5c042011-01-22 06:09:08 +02003098
Johan Hedberg48264f02011-11-09 13:58:58 +02003099int mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003100 u8 addr_type, u8 status)
Johan Hedberg17d5c042011-01-22 06:09:08 +02003101{
3102 struct mgmt_ev_connect_failed ev;
3103
Johan Hedberg4c659c32011-11-07 23:13:39 +02003104 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03003105 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergca69b792011-11-11 18:10:00 +02003106 ev.status = mgmt_status(status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02003107
Johan Hedberg744cf192011-11-08 20:40:14 +02003108 return mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg17d5c042011-01-22 06:09:08 +02003109}
Johan Hedberg980e1a52011-01-22 06:10:07 +02003110
Johan Hedberg744cf192011-11-08 20:40:14 +02003111int mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
Johan Hedberg980e1a52011-01-22 06:10:07 +02003112{
3113 struct mgmt_ev_pin_code_request ev;
3114
Johan Hedbergd8457692012-02-17 14:24:57 +02003115 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03003116 ev.addr.type = BDADDR_BREDR;
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02003117 ev.secure = secure;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003118
Johan Hedberg744cf192011-11-08 20:40:14 +02003119 return mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003120 NULL);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003121}
3122
Johan Hedberg744cf192011-11-08 20:40:14 +02003123int mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003124 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02003125{
3126 struct pending_cmd *cmd;
Johan Hedbergac56fb12011-02-19 12:05:59 -03003127 struct mgmt_rp_pin_code_reply rp;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003128 int err;
3129
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003130 cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003131 if (!cmd)
3132 return -ENOENT;
3133
Johan Hedbergd8457692012-02-17 14:24:57 +02003134 bacpy(&rp.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03003135 rp.addr.type = BDADDR_BREDR;
Johan Hedbergac56fb12011-02-19 12:05:59 -03003136
Johan Hedbergaee9b2182012-02-18 15:07:59 +02003137 err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003138 mgmt_status(status), &rp, sizeof(rp));
Johan Hedberg980e1a52011-01-22 06:10:07 +02003139
Johan Hedberga664b5b2011-02-19 12:06:02 -03003140 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003141
3142 return err;
3143}
3144
Johan Hedberg744cf192011-11-08 20:40:14 +02003145int mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003146 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02003147{
3148 struct pending_cmd *cmd;
Johan Hedbergac56fb12011-02-19 12:05:59 -03003149 struct mgmt_rp_pin_code_reply rp;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003150 int err;
3151
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003152 cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003153 if (!cmd)
3154 return -ENOENT;
3155
Johan Hedbergd8457692012-02-17 14:24:57 +02003156 bacpy(&rp.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03003157 rp.addr.type = BDADDR_BREDR;
Johan Hedbergac56fb12011-02-19 12:05:59 -03003158
Johan Hedbergaee9b2182012-02-18 15:07:59 +02003159 err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003160 mgmt_status(status), &rp, sizeof(rp));
Johan Hedberg980e1a52011-01-22 06:10:07 +02003161
Johan Hedberga664b5b2011-02-19 12:06:02 -03003162 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003163
3164 return err;
3165}
Johan Hedberga5c29682011-02-19 12:05:57 -03003166
Johan Hedberg744cf192011-11-08 20:40:14 +02003167int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003168 u8 link_type, u8 addr_type, __le32 value,
3169 u8 confirm_hint)
Johan Hedberga5c29682011-02-19 12:05:57 -03003170{
3171 struct mgmt_ev_user_confirm_request ev;
3172
Johan Hedberg744cf192011-11-08 20:40:14 +02003173 BT_DBG("%s", hdev->name);
Johan Hedberga5c29682011-02-19 12:05:57 -03003174
Johan Hedberg272d90d2012-02-09 15:26:12 +02003175 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03003176 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003177 ev.confirm_hint = confirm_hint;
Andrei Emeltchenko78e8098e2012-03-09 13:00:50 +02003178 ev.value = value;
Johan Hedberga5c29682011-02-19 12:05:57 -03003179
Johan Hedberg744cf192011-11-08 20:40:14 +02003180 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003181 NULL);
Johan Hedberga5c29682011-02-19 12:05:57 -03003182}
3183
Johan Hedberg272d90d2012-02-09 15:26:12 +02003184int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
3185 u8 link_type, u8 addr_type)
Brian Gix604086b2011-11-23 08:28:33 -08003186{
3187 struct mgmt_ev_user_passkey_request ev;
3188
3189 BT_DBG("%s", hdev->name);
3190
Johan Hedberg272d90d2012-02-09 15:26:12 +02003191 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03003192 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Brian Gix604086b2011-11-23 08:28:33 -08003193
3194 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003195 NULL);
Brian Gix604086b2011-11-23 08:28:33 -08003196}
3197
Brian Gix0df4c182011-11-16 13:53:13 -08003198static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg272d90d2012-02-09 15:26:12 +02003199 u8 link_type, u8 addr_type, u8 status,
3200 u8 opcode)
Johan Hedberga5c29682011-02-19 12:05:57 -03003201{
3202 struct pending_cmd *cmd;
3203 struct mgmt_rp_user_confirm_reply rp;
3204 int err;
3205
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003206 cmd = mgmt_pending_find(opcode, hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03003207 if (!cmd)
3208 return -ENOENT;
3209
Johan Hedberg272d90d2012-02-09 15:26:12 +02003210 bacpy(&rp.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03003211 rp.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergaee9b2182012-02-18 15:07:59 +02003212 err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003213 &rp, sizeof(rp));
Johan Hedberga5c29682011-02-19 12:05:57 -03003214
Johan Hedberga664b5b2011-02-19 12:06:02 -03003215 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03003216
3217 return err;
3218}
3219
Johan Hedberg744cf192011-11-08 20:40:14 +02003220int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003221 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03003222{
Johan Hedberg272d90d2012-02-09 15:26:12 +02003223 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003224 status, MGMT_OP_USER_CONFIRM_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03003225}
3226
Johan Hedberg272d90d2012-02-09 15:26:12 +02003227int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003228 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03003229{
Johan Hedberg272d90d2012-02-09 15:26:12 +02003230 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003231 status, MGMT_OP_USER_CONFIRM_NEG_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03003232}
Johan Hedberg2a611692011-02-19 12:06:00 -03003233
Brian Gix604086b2011-11-23 08:28:33 -08003234int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003235 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08003236{
Johan Hedberg272d90d2012-02-09 15:26:12 +02003237 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003238 status, MGMT_OP_USER_PASSKEY_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08003239}
3240
Johan Hedberg272d90d2012-02-09 15:26:12 +02003241int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003242 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08003243{
Johan Hedberg272d90d2012-02-09 15:26:12 +02003244 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003245 status, MGMT_OP_USER_PASSKEY_NEG_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08003246}
3247
Johan Hedbergbab73cb2012-02-09 16:07:29 +02003248int mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003249 u8 addr_type, u8 status)
Johan Hedberg2a611692011-02-19 12:06:00 -03003250{
3251 struct mgmt_ev_auth_failed ev;
3252
Johan Hedbergbab73cb2012-02-09 16:07:29 +02003253 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03003254 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergca69b792011-11-11 18:10:00 +02003255 ev.status = mgmt_status(status);
Johan Hedberg2a611692011-02-19 12:06:00 -03003256
Johan Hedberg744cf192011-11-08 20:40:14 +02003257 return mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg2a611692011-02-19 12:06:00 -03003258}
Johan Hedbergb312b1612011-03-16 14:29:37 +02003259
Johan Hedberg33ef95e2012-02-16 23:56:27 +02003260int mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
3261{
3262 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg47990ea2012-02-22 11:58:37 +02003263 bool changed = false;
3264 int err = 0;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02003265
3266 if (status) {
3267 u8 mgmt_err = mgmt_status(status);
3268 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003269 cmd_status_rsp, &mgmt_err);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02003270 return 0;
3271 }
3272
Johan Hedberg47990ea2012-02-22 11:58:37 +02003273 if (test_bit(HCI_AUTH, &hdev->flags)) {
3274 if (!test_and_set_bit(HCI_LINK_SECURITY, &hdev->dev_flags))
3275 changed = true;
3276 } else {
3277 if (test_and_clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags))
3278 changed = true;
3279 }
3280
Johan Hedberg33ef95e2012-02-16 23:56:27 +02003281 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003282 &match);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02003283
Johan Hedberg47990ea2012-02-22 11:58:37 +02003284 if (changed)
3285 err = new_settings(hdev, match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02003286
3287 if (match.sk)
3288 sock_put(match.sk);
3289
3290 return err;
3291}
3292
Johan Hedbergcacaf522012-02-21 00:52:42 +02003293static int clear_eir(struct hci_dev *hdev)
3294{
3295 struct hci_cp_write_eir cp;
3296
3297 if (!(hdev->features[6] & LMP_EXT_INQ))
3298 return 0;
3299
Johan Hedbergc80da272012-02-22 15:38:48 +02003300 memset(hdev->eir, 0, sizeof(hdev->eir));
3301
Johan Hedbergcacaf522012-02-21 00:52:42 +02003302 memset(&cp, 0, sizeof(cp));
3303
3304 return hci_send_cmd(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
3305}
3306
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02003307int mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02003308{
3309 struct cmd_lookup match = { NULL, hdev };
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02003310 bool changed = false;
3311 int err = 0;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02003312
3313 if (status) {
3314 u8 mgmt_err = mgmt_status(status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02003315
3316 if (enable && test_and_clear_bit(HCI_SSP_ENABLED,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003317 &hdev->dev_flags))
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02003318 err = new_settings(hdev, NULL);
3319
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003320 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
3321 &mgmt_err);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02003322
3323 return err;
3324 }
3325
3326 if (enable) {
3327 if (!test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
3328 changed = true;
3329 } else {
3330 if (test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
3331 changed = true;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02003332 }
3333
3334 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
3335
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02003336 if (changed)
3337 err = new_settings(hdev, match.sk);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02003338
Johan Hedberg5fc6ebb2012-02-22 15:10:59 +02003339 if (match.sk)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02003340 sock_put(match.sk);
3341
Johan Hedberg5fc6ebb2012-02-22 15:10:59 +02003342 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
3343 update_eir(hdev);
3344 else
3345 clear_eir(hdev);
Johan Hedbergcacaf522012-02-21 00:52:42 +02003346
Johan Hedberged2c4ee2012-02-17 00:56:28 +02003347 return err;
3348}
3349
Johan Hedberg90e70452012-02-23 23:09:40 +02003350static void class_rsp(struct pending_cmd *cmd, void *data)
3351{
3352 struct cmd_lookup *match = data;
3353
3354 cmd_complete(cmd->sk, cmd->index, cmd->opcode, match->mgmt_status,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003355 match->hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02003356
3357 list_del(&cmd->list);
3358
3359 if (match->sk == NULL) {
3360 match->sk = cmd->sk;
3361 sock_hold(match->sk);
3362 }
3363
3364 mgmt_pending_free(cmd);
3365}
3366
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01003367int mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003368 u8 status)
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01003369{
Johan Hedberg90e70452012-02-23 23:09:40 +02003370 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
3371 int err = 0;
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01003372
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02003373 clear_bit(HCI_PENDING_CLASS, &hdev->dev_flags);
3374
Johan Hedberg90e70452012-02-23 23:09:40 +02003375 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, class_rsp, &match);
3376 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, class_rsp, &match);
3377 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, class_rsp, &match);
3378
3379 if (!status)
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003380 err = mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class,
3381 3, NULL);
Johan Hedberg90e70452012-02-23 23:09:40 +02003382
3383 if (match.sk)
3384 sock_put(match.sk);
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01003385
3386 return err;
3387}
3388
Johan Hedberg744cf192011-11-08 20:40:14 +02003389int mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
Johan Hedbergb312b1612011-03-16 14:29:37 +02003390{
3391 struct pending_cmd *cmd;
3392 struct mgmt_cp_set_local_name ev;
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003393 bool changed = false;
3394 int err = 0;
3395
3396 if (memcmp(name, hdev->dev_name, sizeof(hdev->dev_name)) != 0) {
3397 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
3398 changed = true;
3399 }
Johan Hedbergb312b1612011-03-16 14:29:37 +02003400
3401 memset(&ev, 0, sizeof(ev));
3402 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003403 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003404
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003405 cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003406 if (!cmd)
3407 goto send_event;
3408
Johan Hedberg7bdaae42012-02-22 21:39:58 +02003409 /* Always assume that either the short or the complete name has
3410 * changed if there was a pending mgmt command */
3411 changed = true;
3412
Johan Hedbergb312b1612011-03-16 14:29:37 +02003413 if (status) {
Johan Hedberg744cf192011-11-08 20:40:14 +02003414 err = cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003415 mgmt_status(status));
Johan Hedbergb312b1612011-03-16 14:29:37 +02003416 goto failed;
3417 }
3418
Johan Hedbergaee9b2182012-02-18 15:07:59 +02003419 err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, &ev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003420 sizeof(ev));
Johan Hedbergb312b1612011-03-16 14:29:37 +02003421 if (err < 0)
3422 goto failed;
3423
3424send_event:
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003425 if (changed)
3426 err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003427 sizeof(ev), cmd ? cmd->sk : NULL);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003428
Johan Hedbergf51d5b22012-02-22 18:17:32 +02003429 update_eir(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003430
3431failed:
3432 if (cmd)
3433 mgmt_pending_remove(cmd);
3434 return err;
3435}
Szymon Jancc35938b2011-03-22 13:12:21 +01003436
Johan Hedberg744cf192011-11-08 20:40:14 +02003437int mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003438 u8 *randomizer, u8 status)
Szymon Jancc35938b2011-03-22 13:12:21 +01003439{
3440 struct pending_cmd *cmd;
3441 int err;
3442
Johan Hedberg744cf192011-11-08 20:40:14 +02003443 BT_DBG("%s status %u", hdev->name, status);
Szymon Jancc35938b2011-03-22 13:12:21 +01003444
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003445 cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003446 if (!cmd)
3447 return -ENOENT;
3448
3449 if (status) {
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003450 err = cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3451 mgmt_status(status));
Szymon Jancc35938b2011-03-22 13:12:21 +01003452 } else {
3453 struct mgmt_rp_read_local_oob_data rp;
3454
3455 memcpy(rp.hash, hash, sizeof(rp.hash));
3456 memcpy(rp.randomizer, randomizer, sizeof(rp.randomizer));
3457
Johan Hedberg744cf192011-11-08 20:40:14 +02003458 err = cmd_complete(cmd->sk, hdev->id,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003459 MGMT_OP_READ_LOCAL_OOB_DATA, 0, &rp,
3460 sizeof(rp));
Szymon Jancc35938b2011-03-22 13:12:21 +01003461 }
3462
3463 mgmt_pending_remove(cmd);
3464
3465 return err;
3466}
Johan Hedberge17acd42011-03-30 23:57:16 +03003467
Johan Hedberg06199cf2012-02-22 16:37:11 +02003468int mgmt_le_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
3469{
3470 struct cmd_lookup match = { NULL, hdev };
3471 bool changed = false;
3472 int err = 0;
3473
3474 if (status) {
3475 u8 mgmt_err = mgmt_status(status);
3476
3477 if (enable && test_and_clear_bit(HCI_LE_ENABLED,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003478 &hdev->dev_flags))
Szymon Jancd97dcb62012-03-16 16:02:56 +01003479 err = new_settings(hdev, NULL);
Johan Hedberg06199cf2012-02-22 16:37:11 +02003480
Szymon Jancd97dcb62012-03-16 16:02:56 +01003481 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
3482 &mgmt_err);
Johan Hedberg06199cf2012-02-22 16:37:11 +02003483
3484 return err;
3485 }
3486
3487 if (enable) {
3488 if (!test_and_set_bit(HCI_LE_ENABLED, &hdev->dev_flags))
3489 changed = true;
3490 } else {
3491 if (test_and_clear_bit(HCI_LE_ENABLED, &hdev->dev_flags))
3492 changed = true;
3493 }
3494
3495 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
3496
3497 if (changed)
3498 err = new_settings(hdev, match.sk);
3499
3500 if (match.sk)
3501 sock_put(match.sk);
3502
3503 return err;
3504}
3505
Johan Hedberg48264f02011-11-09 13:58:58 +02003506int mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003507 u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name, u8
3508 ssp, u8 *eir, u16 eir_len)
Johan Hedberge17acd42011-03-30 23:57:16 +03003509{
Johan Hedberge319d2e2012-01-15 19:51:59 +02003510 char buf[512];
3511 struct mgmt_ev_device_found *ev = (void *) buf;
Johan Hedberg1dc06092012-01-15 21:01:23 +02003512 size_t ev_size;
Johan Hedberge17acd42011-03-30 23:57:16 +03003513
Johan Hedberg1dc06092012-01-15 21:01:23 +02003514 /* Leave 5 bytes for a potential CoD field */
3515 if (sizeof(*ev) + eir_len + 5 > sizeof(buf))
Andre Guedes7d262f82012-01-10 18:20:49 -03003516 return -EINVAL;
3517
Johan Hedberg1dc06092012-01-15 21:01:23 +02003518 memset(buf, 0, sizeof(buf));
3519
Johan Hedberge319d2e2012-01-15 19:51:59 +02003520 bacpy(&ev->addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03003521 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberge319d2e2012-01-15 19:51:59 +02003522 ev->rssi = rssi;
Johan Hedberg9a395a82012-02-23 00:00:32 +02003523 if (cfm_name)
3524 ev->flags[0] |= MGMT_DEV_FOUND_CONFIRM_NAME;
Johan Hedberg388fc8f2012-02-23 00:38:59 +02003525 if (!ssp)
3526 ev->flags[0] |= MGMT_DEV_FOUND_LEGACY_PAIRING;
Johan Hedberge17acd42011-03-30 23:57:16 +03003527
Johan Hedberg1dc06092012-01-15 21:01:23 +02003528 if (eir_len > 0)
Johan Hedberge319d2e2012-01-15 19:51:59 +02003529 memcpy(ev->eir, eir, eir_len);
Johan Hedberge17acd42011-03-30 23:57:16 +03003530
Johan Hedberg1dc06092012-01-15 21:01:23 +02003531 if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV))
3532 eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003533 dev_class, 3);
Johan Hedberg1dc06092012-01-15 21:01:23 +02003534
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02003535 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedberg1dc06092012-01-15 21:01:23 +02003536
3537 ev_size = sizeof(*ev) + eir_len;
Andre Guedesf8523592011-09-09 18:56:26 -03003538
Johan Hedberge319d2e2012-01-15 19:51:59 +02003539 return mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL);
Johan Hedberge17acd42011-03-30 23:57:16 +03003540}
Johan Hedberga88a9652011-03-30 13:18:12 +03003541
Johan Hedbergb644ba32012-01-17 21:48:47 +02003542int mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003543 u8 addr_type, s8 rssi, u8 *name, u8 name_len)
Johan Hedberga88a9652011-03-30 13:18:12 +03003544{
Johan Hedbergb644ba32012-01-17 21:48:47 +02003545 struct mgmt_ev_device_found *ev;
3546 char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2];
3547 u16 eir_len;
Johan Hedberga88a9652011-03-30 13:18:12 +03003548
Johan Hedbergb644ba32012-01-17 21:48:47 +02003549 ev = (struct mgmt_ev_device_found *) buf;
Johan Hedberga88a9652011-03-30 13:18:12 +03003550
Johan Hedbergb644ba32012-01-17 21:48:47 +02003551 memset(buf, 0, sizeof(buf));
Johan Hedberga88a9652011-03-30 13:18:12 +03003552
Johan Hedbergb644ba32012-01-17 21:48:47 +02003553 bacpy(&ev->addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03003554 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003555 ev->rssi = rssi;
3556
3557 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003558 name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003559
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02003560 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003561
Johan Hedberg053c7e02012-02-04 00:06:00 +02003562 return mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003563 sizeof(*ev) + eir_len, NULL);
Johan Hedberga88a9652011-03-30 13:18:12 +03003564}
Johan Hedberg314b2382011-04-27 10:29:57 -04003565
Andre Guedes7a135102011-11-09 17:14:25 -03003566int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status)
Johan Hedberg164a6e72011-11-01 17:06:44 +02003567{
3568 struct pending_cmd *cmd;
Johan Hedbergf808e162012-02-19 12:52:07 +02003569 u8 type;
Johan Hedberg164a6e72011-11-01 17:06:44 +02003570 int err;
3571
Andre Guedes203159d2012-02-13 15:41:01 -03003572 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
3573
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003574 cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev);
Johan Hedberg164a6e72011-11-01 17:06:44 +02003575 if (!cmd)
3576 return -ENOENT;
3577
Johan Hedbergf808e162012-02-19 12:52:07 +02003578 type = hdev->discovery.type;
3579
3580 err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003581 &type, sizeof(type));
Johan Hedberg164a6e72011-11-01 17:06:44 +02003582 mgmt_pending_remove(cmd);
3583
3584 return err;
3585}
3586
Andre Guedese6d465c2011-11-09 17:14:26 -03003587int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status)
3588{
3589 struct pending_cmd *cmd;
3590 int err;
3591
3592 cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
3593 if (!cmd)
3594 return -ENOENT;
3595
Johan Hedbergd9306502012-02-20 23:25:18 +02003596 err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003597 &hdev->discovery.type, sizeof(hdev->discovery.type));
Johan Hedberg03811012010-12-08 00:21:06 +02003598 mgmt_pending_remove(cmd);
3599
3600 return err;
3601}
Johan Hedberg314b2382011-04-27 10:29:57 -04003602
Johan Hedberg744cf192011-11-08 20:40:14 +02003603int mgmt_discovering(struct hci_dev *hdev, u8 discovering)
Johan Hedberg314b2382011-04-27 10:29:57 -04003604{
Johan Hedbergf963e8e2012-02-20 23:30:44 +02003605 struct mgmt_ev_discovering ev;
Johan Hedberg164a6e72011-11-01 17:06:44 +02003606 struct pending_cmd *cmd;
3607
Andre Guedes343fb142011-11-22 17:14:19 -03003608 BT_DBG("%s discovering %u", hdev->name, discovering);
3609
Johan Hedberg164a6e72011-11-01 17:06:44 +02003610 if (discovering)
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003611 cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev);
Johan Hedberg164a6e72011-11-01 17:06:44 +02003612 else
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003613 cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
Johan Hedberg164a6e72011-11-01 17:06:44 +02003614
3615 if (cmd != NULL) {
Johan Hedbergf808e162012-02-19 12:52:07 +02003616 u8 type = hdev->discovery.type;
3617
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003618 cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type,
3619 sizeof(type));
Johan Hedberg164a6e72011-11-01 17:06:44 +02003620 mgmt_pending_remove(cmd);
3621 }
3622
Johan Hedbergf963e8e2012-02-20 23:30:44 +02003623 memset(&ev, 0, sizeof(ev));
3624 ev.type = hdev->discovery.type;
3625 ev.discovering = discovering;
3626
3627 return mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg314b2382011-04-27 10:29:57 -04003628}
Antti Julku5e762442011-08-25 16:48:02 +03003629
Johan Hedberg88c1fe42012-02-09 15:56:11 +02003630int mgmt_device_blocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
Antti Julku5e762442011-08-25 16:48:02 +03003631{
3632 struct pending_cmd *cmd;
3633 struct mgmt_ev_device_blocked ev;
3634
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003635 cmd = mgmt_pending_find(MGMT_OP_BLOCK_DEVICE, hdev);
Antti Julku5e762442011-08-25 16:48:02 +03003636
Johan Hedberg88c1fe42012-02-09 15:56:11 +02003637 bacpy(&ev.addr.bdaddr, bdaddr);
3638 ev.addr.type = type;
Antti Julku5e762442011-08-25 16:48:02 +03003639
Johan Hedberg744cf192011-11-08 20:40:14 +02003640 return mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003641 cmd ? cmd->sk : NULL);
Antti Julku5e762442011-08-25 16:48:02 +03003642}
3643
Johan Hedberg88c1fe42012-02-09 15:56:11 +02003644int mgmt_device_unblocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
Antti Julku5e762442011-08-25 16:48:02 +03003645{
3646 struct pending_cmd *cmd;
3647 struct mgmt_ev_device_unblocked ev;
3648
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003649 cmd = mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE, hdev);
Antti Julku5e762442011-08-25 16:48:02 +03003650
Johan Hedberg88c1fe42012-02-09 15:56:11 +02003651 bacpy(&ev.addr.bdaddr, bdaddr);
3652 ev.addr.type = type;
Antti Julku5e762442011-08-25 16:48:02 +03003653
Johan Hedberg744cf192011-11-08 20:40:14 +02003654 return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003655 cmd ? cmd->sk : NULL);
Antti Julku5e762442011-08-25 16:48:02 +03003656}
Marcel Holtmannd7b7e792012-02-20 21:47:49 +01003657
3658module_param(enable_hs, bool, 0644);
3659MODULE_PARM_DESC(enable_hs, "Enable High Speed support");
3660
3661module_param(enable_le, bool, 0644);
3662MODULE_PARM_DESC(enable_le, "Enable Low Energy support");