Commit | Line | Data |
---|---|---|
0381101f JH |
1 | /* |
2 | BlueZ - Bluetooth protocol stack for Linux | |
ea585ab5 | 3 | |
0381101f | 4 | Copyright (C) 2010 Nokia Corporation |
ea585ab5 | 5 | Copyright (C) 2011-2012 Intel Corporation |
0381101f JH |
6 | |
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 | ||
3a9a231d | 27 | #include <linux/module.h> |
0381101f JH |
28 | #include <asm/unaligned.h> |
29 | ||
30 | #include <net/bluetooth/bluetooth.h> | |
31 | #include <net/bluetooth/hci_core.h> | |
7129069e | 32 | #include <net/bluetooth/hci_sock.h> |
4bc58f51 | 33 | #include <net/bluetooth/l2cap.h> |
0381101f | 34 | #include <net/bluetooth/mgmt.h> |
ac4b7236 | 35 | |
0857dd3b | 36 | #include "hci_request.h" |
ac4b7236 | 37 | #include "smp.h" |
a380b6cf | 38 | #include "mgmt_util.h" |
0381101f | 39 | |
2da9c55c | 40 | #define MGMT_VERSION 1 |
dc4270c0 | 41 | #define MGMT_REVISION 11 |
02d98129 | 42 | |
e70bb2e8 JH |
43 | static 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, | |
b2939475 | 50 | MGMT_OP_SET_BONDABLE, |
e70bb2e8 JH |
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, | |
cdbaccca | 81 | MGMT_OP_SET_DEVICE_ID, |
4375f103 | 82 | MGMT_OP_SET_ADVERTISING, |
0663ca2a | 83 | MGMT_OP_SET_BREDR, |
d13eafce | 84 | MGMT_OP_SET_STATIC_ADDRESS, |
7f72134e | 85 | MGMT_OP_SET_SCAN_PARAMS, |
e98d2ce2 | 86 | MGMT_OP_SET_SECURE_CONN, |
4e39ac81 | 87 | MGMT_OP_SET_DEBUG_KEYS, |
62b04cd1 | 88 | MGMT_OP_SET_PRIVACY, |
41edf160 | 89 | MGMT_OP_LOAD_IRKS, |
dd983808 | 90 | MGMT_OP_GET_CONN_INFO, |
95868426 | 91 | MGMT_OP_GET_CLOCK_INFO, |
2faade53 MH |
92 | MGMT_OP_ADD_DEVICE, |
93 | MGMT_OP_REMOVE_DEVICE, | |
a26f3dcf | 94 | MGMT_OP_LOAD_CONN_PARAM, |
73d1df2a | 95 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
9fc3bfb6 | 96 | MGMT_OP_READ_CONFIG_INFO, |
dbece37a | 97 | MGMT_OP_SET_EXTERNAL_CONFIG, |
9713c17b | 98 | MGMT_OP_SET_PUBLIC_ADDRESS, |
66ea9427 | 99 | MGMT_OP_START_SERVICE_DISCOVERY, |
4f0f155c | 100 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, |
96f1474a | 101 | MGMT_OP_READ_EXT_INDEX_LIST, |
d3d5305b | 102 | MGMT_OP_READ_ADV_FEATURES, |
24b4f38f | 103 | MGMT_OP_ADD_ADVERTISING, |
da929335 | 104 | MGMT_OP_REMOVE_ADVERTISING, |
40b25fe5 | 105 | MGMT_OP_GET_ADV_SIZE_INFO, |
78b781ca | 106 | MGMT_OP_START_LIMITED_DISCOVERY, |
e70bb2e8 JH |
107 | }; |
108 | ||
109 | static const u16 mgmt_events[] = { | |
110 | MGMT_EV_CONTROLLER_ERROR, | |
111 | MGMT_EV_INDEX_ADDED, | |
112 | MGMT_EV_INDEX_REMOVED, | |
113 | MGMT_EV_NEW_SETTINGS, | |
114 | MGMT_EV_CLASS_OF_DEV_CHANGED, | |
115 | MGMT_EV_LOCAL_NAME_CHANGED, | |
116 | MGMT_EV_NEW_LINK_KEY, | |
117 | MGMT_EV_NEW_LONG_TERM_KEY, | |
118 | MGMT_EV_DEVICE_CONNECTED, | |
119 | MGMT_EV_DEVICE_DISCONNECTED, | |
120 | MGMT_EV_CONNECT_FAILED, | |
121 | MGMT_EV_PIN_CODE_REQUEST, | |
122 | MGMT_EV_USER_CONFIRM_REQUEST, | |
123 | MGMT_EV_USER_PASSKEY_REQUEST, | |
124 | MGMT_EV_AUTH_FAILED, | |
125 | MGMT_EV_DEVICE_FOUND, | |
126 | MGMT_EV_DISCOVERING, | |
127 | MGMT_EV_DEVICE_BLOCKED, | |
128 | MGMT_EV_DEVICE_UNBLOCKED, | |
129 | MGMT_EV_DEVICE_UNPAIRED, | |
92a25256 | 130 | MGMT_EV_PASSKEY_NOTIFY, |
1b60ef21 | 131 | MGMT_EV_NEW_IRK, |
7ee4ea36 | 132 | MGMT_EV_NEW_CSRK, |
8afef092 MH |
133 | MGMT_EV_DEVICE_ADDED, |
134 | MGMT_EV_DEVICE_REMOVED, | |
ffb5a827 | 135 | MGMT_EV_NEW_CONN_PARAM, |
0602a8ad | 136 | MGMT_EV_UNCONF_INDEX_ADDED, |
edd3896b | 137 | MGMT_EV_UNCONF_INDEX_REMOVED, |
f4537c04 | 138 | MGMT_EV_NEW_CONFIG_OPTIONS, |
ced85549 MH |
139 | MGMT_EV_EXT_INDEX_ADDED, |
140 | MGMT_EV_EXT_INDEX_REMOVED, | |
72000df2 | 141 | MGMT_EV_LOCAL_OOB_DATA_UPDATED, |
24b4f38f AU |
142 | MGMT_EV_ADVERTISING_ADDED, |
143 | MGMT_EV_ADVERTISING_REMOVED, | |
e70bb2e8 JH |
144 | }; |
145 | ||
99c679ac MH |
146 | static const u16 mgmt_untrusted_commands[] = { |
147 | MGMT_OP_READ_INDEX_LIST, | |
148 | MGMT_OP_READ_INFO, | |
149 | MGMT_OP_READ_UNCONF_INDEX_LIST, | |
150 | MGMT_OP_READ_CONFIG_INFO, | |
151 | MGMT_OP_READ_EXT_INDEX_LIST, | |
152 | }; | |
153 | ||
154 | static const u16 mgmt_untrusted_events[] = { | |
155 | MGMT_EV_INDEX_ADDED, | |
156 | MGMT_EV_INDEX_REMOVED, | |
157 | MGMT_EV_NEW_SETTINGS, | |
158 | MGMT_EV_CLASS_OF_DEV_CHANGED, | |
159 | MGMT_EV_LOCAL_NAME_CHANGED, | |
160 | MGMT_EV_UNCONF_INDEX_ADDED, | |
161 | MGMT_EV_UNCONF_INDEX_REMOVED, | |
162 | MGMT_EV_NEW_CONFIG_OPTIONS, | |
163 | MGMT_EV_EXT_INDEX_ADDED, | |
164 | MGMT_EV_EXT_INDEX_REMOVED, | |
165 | }; | |
166 | ||
17b02e62 | 167 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
7d78525d | 168 | |
d25b78e2 JH |
169 | #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \ |
170 | "\x00\x00\x00\x00\x00\x00\x00\x00" | |
171 | ||
ca69b795 JH |
172 | /* HCI to MGMT error code conversion table */ |
173 | static u8 mgmt_status_table[] = { | |
174 | MGMT_STATUS_SUCCESS, | |
175 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ | |
176 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ | |
177 | MGMT_STATUS_FAILED, /* Hardware Failure */ | |
178 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ | |
179 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ | |
eadd663a | 180 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
ca69b795 JH |
181 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
182 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ | |
183 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ | |
184 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ | |
185 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ | |
186 | MGMT_STATUS_BUSY, /* Command Disallowed */ | |
187 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ | |
188 | MGMT_STATUS_REJECTED, /* Rejected Security */ | |
189 | MGMT_STATUS_REJECTED, /* Rejected Personal */ | |
190 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ | |
191 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ | |
192 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ | |
193 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ | |
194 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ | |
195 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ | |
196 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ | |
197 | MGMT_STATUS_BUSY, /* Repeated Attempts */ | |
198 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ | |
199 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ | |
200 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ | |
201 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ | |
202 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ | |
203 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ | |
204 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ | |
205 | MGMT_STATUS_FAILED, /* Unspecified Error */ | |
206 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ | |
207 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ | |
208 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ | |
209 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ | |
210 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ | |
211 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ | |
212 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ | |
213 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ | |
214 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ | |
215 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ | |
216 | MGMT_STATUS_FAILED, /* Transaction Collision */ | |
217 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ | |
218 | MGMT_STATUS_REJECTED, /* QoS Rejected */ | |
219 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ | |
220 | MGMT_STATUS_REJECTED, /* Insufficient Security */ | |
221 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ | |
222 | MGMT_STATUS_BUSY, /* Role Switch Pending */ | |
223 | MGMT_STATUS_FAILED, /* Slot Violation */ | |
224 | MGMT_STATUS_FAILED, /* Role Switch Failed */ | |
225 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ | |
226 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ | |
227 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ | |
228 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ | |
229 | MGMT_STATUS_BUSY, /* Controller Busy */ | |
230 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ | |
231 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ | |
232 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ | |
233 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ | |
234 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ | |
235 | }; | |
236 | ||
237 | static u8 mgmt_status(u8 hci_status) | |
238 | { | |
239 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) | |
240 | return mgmt_status_table[hci_status]; | |
241 | ||
242 | return MGMT_STATUS_FAILED; | |
243 | } | |
244 | ||
c08b1a1d MH |
245 | static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data, |
246 | u16 len, int flag) | |
f9207338 | 247 | { |
c08b1a1d MH |
248 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
249 | flag, NULL); | |
f9207338 MH |
250 | } |
251 | ||
72000df2 MH |
252 | static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data, |
253 | u16 len, int flag, struct sock *skip_sk) | |
254 | { | |
255 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, | |
256 | flag, skip_sk); | |
257 | } | |
258 | ||
f6b7712e MH |
259 | static int mgmt_generic_event(u16 event, struct hci_dev *hdev, void *data, |
260 | u16 len, struct sock *skip_sk) | |
261 | { | |
262 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, | |
263 | HCI_MGMT_GENERIC_EVENTS, skip_sk); | |
264 | } | |
265 | ||
7a00ff44 JH |
266 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len, |
267 | struct sock *skip_sk) | |
268 | { | |
269 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, | |
c08b1a1d | 270 | HCI_SOCK_TRUSTED, skip_sk); |
7a00ff44 JH |
271 | } |
272 | ||
85813a7e JH |
273 | static u8 le_addr_type(u8 mgmt_addr_type) |
274 | { | |
275 | if (mgmt_addr_type == BDADDR_LE_PUBLIC) | |
276 | return ADDR_LE_DEV_PUBLIC; | |
277 | else | |
278 | return ADDR_LE_DEV_RANDOM; | |
279 | } | |
280 | ||
04124681 GP |
281 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
282 | u16 data_len) | |
a38528f1 JH |
283 | { |
284 | struct mgmt_rp_read_version rp; | |
285 | ||
286 | BT_DBG("sock %p", sk); | |
287 | ||
288 | rp.version = MGMT_VERSION; | |
dcf4adbf | 289 | rp.revision = cpu_to_le16(MGMT_REVISION); |
a38528f1 | 290 | |
2a1afb5a JH |
291 | return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, |
292 | &rp, sizeof(rp)); | |
a38528f1 JH |
293 | } |
294 | ||
04124681 GP |
295 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
296 | u16 data_len) | |
e70bb2e8 JH |
297 | { |
298 | struct mgmt_rp_read_commands *rp; | |
99c679ac | 299 | u16 num_commands, num_events; |
e70bb2e8 JH |
300 | size_t rp_size; |
301 | int i, err; | |
302 | ||
303 | BT_DBG("sock %p", sk); | |
304 | ||
99c679ac MH |
305 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
306 | num_commands = ARRAY_SIZE(mgmt_commands); | |
307 | num_events = ARRAY_SIZE(mgmt_events); | |
308 | } else { | |
309 | num_commands = ARRAY_SIZE(mgmt_untrusted_commands); | |
310 | num_events = ARRAY_SIZE(mgmt_untrusted_events); | |
311 | } | |
312 | ||
e70bb2e8 JH |
313 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
314 | ||
315 | rp = kmalloc(rp_size, GFP_KERNEL); | |
316 | if (!rp) | |
317 | return -ENOMEM; | |
318 | ||
dcf4adbf JP |
319 | rp->num_commands = cpu_to_le16(num_commands); |
320 | rp->num_events = cpu_to_le16(num_events); | |
e70bb2e8 | 321 | |
99c679ac MH |
322 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
323 | __le16 *opcode = rp->opcodes; | |
324 | ||
325 | for (i = 0; i < num_commands; i++, opcode++) | |
326 | put_unaligned_le16(mgmt_commands[i], opcode); | |
e70bb2e8 | 327 | |
99c679ac MH |
328 | for (i = 0; i < num_events; i++, opcode++) |
329 | put_unaligned_le16(mgmt_events[i], opcode); | |
330 | } else { | |
331 | __le16 *opcode = rp->opcodes; | |
332 | ||
333 | for (i = 0; i < num_commands; i++, opcode++) | |
334 | put_unaligned_le16(mgmt_untrusted_commands[i], opcode); | |
335 | ||
336 | for (i = 0; i < num_events; i++, opcode++) | |
337 | put_unaligned_le16(mgmt_untrusted_events[i], opcode); | |
338 | } | |
e70bb2e8 | 339 | |
2a1afb5a JH |
340 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, |
341 | rp, rp_size); | |
e70bb2e8 JH |
342 | kfree(rp); |
343 | ||
344 | return err; | |
345 | } | |
346 | ||
04124681 GP |
347 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
348 | u16 data_len) | |
faba42eb | 349 | { |
faba42eb | 350 | struct mgmt_rp_read_index_list *rp; |
8035ded4 | 351 | struct hci_dev *d; |
a38528f1 | 352 | size_t rp_len; |
faba42eb | 353 | u16 count; |
476e44cb | 354 | int err; |
faba42eb JH |
355 | |
356 | BT_DBG("sock %p", sk); | |
357 | ||
358 | read_lock(&hci_dev_list_lock); | |
359 | ||
360 | count = 0; | |
bb4b2a9a | 361 | list_for_each_entry(d, &hci_dev_list, list) { |
73d1df2a | 362 | if (d->dev_type == HCI_BREDR && |
d7a5a11d | 363 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
1514b892 | 364 | count++; |
faba42eb JH |
365 | } |
366 | ||
a38528f1 JH |
367 | rp_len = sizeof(*rp) + (2 * count); |
368 | rp = kmalloc(rp_len, GFP_ATOMIC); | |
369 | if (!rp) { | |
b2c60d42 | 370 | read_unlock(&hci_dev_list_lock); |
faba42eb | 371 | return -ENOMEM; |
b2c60d42 | 372 | } |
faba42eb | 373 | |
476e44cb | 374 | count = 0; |
8035ded4 | 375 | list_for_each_entry(d, &hci_dev_list, list) { |
d7a5a11d MH |
376 | if (hci_dev_test_flag(d, HCI_SETUP) || |
377 | hci_dev_test_flag(d, HCI_CONFIG) || | |
378 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) | |
ab81cbf9 JH |
379 | continue; |
380 | ||
73d1df2a MH |
381 | /* Devices marked as raw-only are neither configured |
382 | * nor unconfigured controllers. | |
383 | */ | |
384 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) | |
fee746b0 MH |
385 | continue; |
386 | ||
73d1df2a | 387 | if (d->dev_type == HCI_BREDR && |
d7a5a11d | 388 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
1514b892 MH |
389 | rp->index[count++] = cpu_to_le16(d->id); |
390 | BT_DBG("Added hci%u", d->id); | |
391 | } | |
faba42eb JH |
392 | } |
393 | ||
476e44cb JH |
394 | rp->num_controllers = cpu_to_le16(count); |
395 | rp_len = sizeof(*rp) + (2 * count); | |
396 | ||
faba42eb JH |
397 | read_unlock(&hci_dev_list_lock); |
398 | ||
2a1afb5a JH |
399 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, |
400 | 0, rp, rp_len); | |
faba42eb | 401 | |
a38528f1 JH |
402 | kfree(rp); |
403 | ||
404 | return err; | |
faba42eb JH |
405 | } |
406 | ||
73d1df2a MH |
407 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
408 | void *data, u16 data_len) | |
409 | { | |
410 | struct mgmt_rp_read_unconf_index_list *rp; | |
411 | struct hci_dev *d; | |
412 | size_t rp_len; | |
413 | u16 count; | |
414 | int err; | |
415 | ||
416 | BT_DBG("sock %p", sk); | |
417 | ||
418 | read_lock(&hci_dev_list_lock); | |
419 | ||
420 | count = 0; | |
421 | list_for_each_entry(d, &hci_dev_list, list) { | |
422 | if (d->dev_type == HCI_BREDR && | |
d7a5a11d | 423 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
73d1df2a MH |
424 | count++; |
425 | } | |
426 | ||
427 | rp_len = sizeof(*rp) + (2 * count); | |
428 | rp = kmalloc(rp_len, GFP_ATOMIC); | |
429 | if (!rp) { | |
430 | read_unlock(&hci_dev_list_lock); | |
431 | return -ENOMEM; | |
432 | } | |
433 | ||
434 | count = 0; | |
435 | list_for_each_entry(d, &hci_dev_list, list) { | |
d7a5a11d MH |
436 | if (hci_dev_test_flag(d, HCI_SETUP) || |
437 | hci_dev_test_flag(d, HCI_CONFIG) || | |
438 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) | |
73d1df2a MH |
439 | continue; |
440 | ||
441 | /* Devices marked as raw-only are neither configured | |
442 | * nor unconfigured controllers. | |
443 | */ | |
444 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) | |
445 | continue; | |
446 | ||
447 | if (d->dev_type == HCI_BREDR && | |
d7a5a11d | 448 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
73d1df2a MH |
449 | rp->index[count++] = cpu_to_le16(d->id); |
450 | BT_DBG("Added hci%u", d->id); | |
451 | } | |
452 | } | |
453 | ||
454 | rp->num_controllers = cpu_to_le16(count); | |
455 | rp_len = sizeof(*rp) + (2 * count); | |
456 | ||
457 | read_unlock(&hci_dev_list_lock); | |
458 | ||
2a1afb5a JH |
459 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
460 | MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len); | |
73d1df2a MH |
461 | |
462 | kfree(rp); | |
463 | ||
464 | return err; | |
465 | } | |
466 | ||
96f1474a MH |
467 | static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev, |
468 | void *data, u16 data_len) | |
469 | { | |
470 | struct mgmt_rp_read_ext_index_list *rp; | |
471 | struct hci_dev *d; | |
472 | size_t rp_len; | |
473 | u16 count; | |
474 | int err; | |
475 | ||
476 | BT_DBG("sock %p", sk); | |
477 | ||
478 | read_lock(&hci_dev_list_lock); | |
479 | ||
480 | count = 0; | |
481 | list_for_each_entry(d, &hci_dev_list, list) { | |
482 | if (d->dev_type == HCI_BREDR || d->dev_type == HCI_AMP) | |
483 | count++; | |
484 | } | |
485 | ||
486 | rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count); | |
487 | rp = kmalloc(rp_len, GFP_ATOMIC); | |
488 | if (!rp) { | |
489 | read_unlock(&hci_dev_list_lock); | |
490 | return -ENOMEM; | |
491 | } | |
492 | ||
493 | count = 0; | |
494 | list_for_each_entry(d, &hci_dev_list, list) { | |
495 | if (hci_dev_test_flag(d, HCI_SETUP) || | |
496 | hci_dev_test_flag(d, HCI_CONFIG) || | |
497 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) | |
498 | continue; | |
499 | ||
500 | /* Devices marked as raw-only are neither configured | |
501 | * nor unconfigured controllers. | |
502 | */ | |
503 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) | |
504 | continue; | |
505 | ||
506 | if (d->dev_type == HCI_BREDR) { | |
507 | if (hci_dev_test_flag(d, HCI_UNCONFIGURED)) | |
508 | rp->entry[count].type = 0x01; | |
509 | else | |
510 | rp->entry[count].type = 0x00; | |
511 | } else if (d->dev_type == HCI_AMP) { | |
512 | rp->entry[count].type = 0x02; | |
513 | } else { | |
514 | continue; | |
515 | } | |
516 | ||
517 | rp->entry[count].bus = d->bus; | |
518 | rp->entry[count++].index = cpu_to_le16(d->id); | |
519 | BT_DBG("Added hci%u", d->id); | |
520 | } | |
521 | ||
522 | rp->num_controllers = cpu_to_le16(count); | |
523 | rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count); | |
524 | ||
525 | read_unlock(&hci_dev_list_lock); | |
526 | ||
527 | /* If this command is called at least once, then all the | |
528 | * default index and unconfigured index events are disabled | |
529 | * and from now on only extended index events are used. | |
530 | */ | |
531 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS); | |
532 | hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS); | |
533 | hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); | |
534 | ||
535 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, | |
536 | MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, rp_len); | |
537 | ||
538 | kfree(rp); | |
539 | ||
540 | return err; | |
541 | } | |
542 | ||
dbece37a MH |
543 | static bool is_configured(struct hci_dev *hdev) |
544 | { | |
545 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && | |
d7a5a11d | 546 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
dbece37a MH |
547 | return false; |
548 | ||
549 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && | |
550 | !bacmp(&hdev->public_addr, BDADDR_ANY)) | |
551 | return false; | |
552 | ||
553 | return true; | |
554 | } | |
555 | ||
89bc22d2 MH |
556 | static __le32 get_missing_options(struct hci_dev *hdev) |
557 | { | |
558 | u32 options = 0; | |
559 | ||
dbece37a | 560 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
d7a5a11d | 561 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
eb1904f4 MH |
562 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
563 | ||
89bc22d2 MH |
564 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
565 | !bacmp(&hdev->public_addr, BDADDR_ANY)) | |
566 | options |= MGMT_OPTION_PUBLIC_ADDRESS; | |
567 | ||
568 | return cpu_to_le32(options); | |
569 | } | |
570 | ||
f4537c04 MH |
571 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
572 | { | |
573 | __le32 options = get_missing_options(hdev); | |
574 | ||
f6b7712e MH |
575 | return mgmt_generic_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
576 | sizeof(options), skip); | |
f4537c04 MH |
577 | } |
578 | ||
dbece37a MH |
579 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
580 | { | |
581 | __le32 options = get_missing_options(hdev); | |
582 | ||
2a1afb5a JH |
583 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options, |
584 | sizeof(options)); | |
dbece37a MH |
585 | } |
586 | ||
9fc3bfb6 MH |
587 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
588 | void *data, u16 data_len) | |
589 | { | |
590 | struct mgmt_rp_read_config_info rp; | |
89bc22d2 | 591 | u32 options = 0; |
9fc3bfb6 MH |
592 | |
593 | BT_DBG("sock %p %s", sk, hdev->name); | |
594 | ||
595 | hci_dev_lock(hdev); | |
596 | ||
597 | memset(&rp, 0, sizeof(rp)); | |
598 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); | |
89bc22d2 | 599 | |
eb1904f4 MH |
600 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
601 | options |= MGMT_OPTION_EXTERNAL_CONFIG; | |
602 | ||
9fc3bfb6 | 603 | if (hdev->set_bdaddr) |
89bc22d2 MH |
604 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
605 | ||
606 | rp.supported_options = cpu_to_le32(options); | |
607 | rp.missing_options = get_missing_options(hdev); | |
9fc3bfb6 MH |
608 | |
609 | hci_dev_unlock(hdev); | |
610 | ||
2a1afb5a JH |
611 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, |
612 | &rp, sizeof(rp)); | |
9fc3bfb6 MH |
613 | } |
614 | ||
69ab39ea JH |
615 | static u32 get_supported_settings(struct hci_dev *hdev) |
616 | { | |
617 | u32 settings = 0; | |
618 | ||
619 | settings |= MGMT_SETTING_POWERED; | |
b2939475 | 620 | settings |= MGMT_SETTING_BONDABLE; |
b1de97d8 | 621 | settings |= MGMT_SETTING_DEBUG_KEYS; |
3742abfc JH |
622 | settings |= MGMT_SETTING_CONNECTABLE; |
623 | settings |= MGMT_SETTING_DISCOVERABLE; | |
69ab39ea | 624 | |
ed3fa31f | 625 | if (lmp_bredr_capable(hdev)) { |
1a47aee8 JH |
626 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
627 | settings |= MGMT_SETTING_FAST_CONNECTABLE; | |
69ab39ea JH |
628 | settings |= MGMT_SETTING_BREDR; |
629 | settings |= MGMT_SETTING_LINK_SECURITY; | |
a82974c9 MH |
630 | |
631 | if (lmp_ssp_capable(hdev)) { | |
632 | settings |= MGMT_SETTING_SSP; | |
633 | settings |= MGMT_SETTING_HS; | |
634 | } | |
e98d2ce2 | 635 | |
05b3c3e7 | 636 | if (lmp_sc_capable(hdev)) |
e98d2ce2 | 637 | settings |= MGMT_SETTING_SECURE_CONN; |
848566b3 | 638 | } |
d7b7e796 | 639 | |
eeca6f89 | 640 | if (lmp_le_capable(hdev)) { |
9d42820f | 641 | settings |= MGMT_SETTING_LE; |
eeca6f89 | 642 | settings |= MGMT_SETTING_ADVERTISING; |
a3209694 | 643 | settings |= MGMT_SETTING_SECURE_CONN; |
0f4bd942 | 644 | settings |= MGMT_SETTING_PRIVACY; |
93690c22 | 645 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
eeca6f89 | 646 | } |
69ab39ea | 647 | |
eb1904f4 MH |
648 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
649 | hdev->set_bdaddr) | |
9fc3bfb6 MH |
650 | settings |= MGMT_SETTING_CONFIGURATION; |
651 | ||
69ab39ea JH |
652 | return settings; |
653 | } | |
654 | ||
655 | static u32 get_current_settings(struct hci_dev *hdev) | |
656 | { | |
657 | u32 settings = 0; | |
658 | ||
f1f0eb02 | 659 | if (hdev_is_powered(hdev)) |
f0d4b78a MH |
660 | settings |= MGMT_SETTING_POWERED; |
661 | ||
d7a5a11d | 662 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
69ab39ea JH |
663 | settings |= MGMT_SETTING_CONNECTABLE; |
664 | ||
d7a5a11d | 665 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
1a4d3c4b JH |
666 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
667 | ||
d7a5a11d | 668 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
69ab39ea JH |
669 | settings |= MGMT_SETTING_DISCOVERABLE; |
670 | ||
d7a5a11d | 671 | if (hci_dev_test_flag(hdev, HCI_BONDABLE)) |
b2939475 | 672 | settings |= MGMT_SETTING_BONDABLE; |
69ab39ea | 673 | |
d7a5a11d | 674 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
69ab39ea JH |
675 | settings |= MGMT_SETTING_BREDR; |
676 | ||
d7a5a11d | 677 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
69ab39ea JH |
678 | settings |= MGMT_SETTING_LE; |
679 | ||
d7a5a11d | 680 | if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) |
69ab39ea JH |
681 | settings |= MGMT_SETTING_LINK_SECURITY; |
682 | ||
d7a5a11d | 683 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
69ab39ea JH |
684 | settings |= MGMT_SETTING_SSP; |
685 | ||
d7a5a11d | 686 | if (hci_dev_test_flag(hdev, HCI_HS_ENABLED)) |
6d80dfd0 JH |
687 | settings |= MGMT_SETTING_HS; |
688 | ||
d7a5a11d | 689 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
eeca6f89 JH |
690 | settings |= MGMT_SETTING_ADVERTISING; |
691 | ||
d7a5a11d | 692 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) |
e98d2ce2 MH |
693 | settings |= MGMT_SETTING_SECURE_CONN; |
694 | ||
d7a5a11d | 695 | if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) |
b1de97d8 MH |
696 | settings |= MGMT_SETTING_DEBUG_KEYS; |
697 | ||
d7a5a11d | 698 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
0f4bd942 JH |
699 | settings |= MGMT_SETTING_PRIVACY; |
700 | ||
93690c22 MH |
701 | /* The current setting for static address has two purposes. The |
702 | * first is to indicate if the static address will be used and | |
703 | * the second is to indicate if it is actually set. | |
704 | * | |
705 | * This means if the static address is not configured, this flag | |
08dc0e98 | 706 | * will never be set. If the address is configured, then if the |
93690c22 MH |
707 | * address is actually used decides if the flag is set or not. |
708 | * | |
709 | * For single mode LE only controllers and dual-mode controllers | |
710 | * with BR/EDR disabled, the existence of the static address will | |
711 | * be evaluated. | |
712 | */ | |
b7cb93e5 | 713 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
d7a5a11d | 714 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
93690c22 MH |
715 | !bacmp(&hdev->bdaddr, BDADDR_ANY)) { |
716 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) | |
717 | settings |= MGMT_SETTING_STATIC_ADDRESS; | |
718 | } | |
719 | ||
69ab39ea JH |
720 | return settings; |
721 | } | |
722 | ||
333ae95d JH |
723 | static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev) |
724 | { | |
725 | return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev); | |
726 | } | |
727 | ||
333ae95d JH |
728 | static struct mgmt_pending_cmd *pending_find_data(u16 opcode, |
729 | struct hci_dev *hdev, | |
730 | const void *data) | |
731 | { | |
732 | return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data); | |
733 | } | |
734 | ||
f2252570 | 735 | u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev) |
9a43e25f | 736 | { |
3b0602cd | 737 | struct mgmt_pending_cmd *cmd; |
9a43e25f JH |
738 | |
739 | /* If there's a pending mgmt command the flags will not yet have | |
740 | * their final values, so check for this first. | |
741 | */ | |
333ae95d | 742 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
9a43e25f JH |
743 | if (cmd) { |
744 | struct mgmt_mode *cp = cmd->param; | |
745 | if (cp->val == 0x01) | |
746 | return LE_AD_GENERAL; | |
747 | else if (cp->val == 0x02) | |
748 | return LE_AD_LIMITED; | |
749 | } else { | |
d7a5a11d | 750 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) |
9a43e25f | 751 | return LE_AD_LIMITED; |
d7a5a11d | 752 | else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
9a43e25f JH |
753 | return LE_AD_GENERAL; |
754 | } | |
755 | ||
756 | return 0; | |
757 | } | |
758 | ||
f2252570 | 759 | bool mgmt_get_connectable(struct hci_dev *hdev) |
fdf51784 AU |
760 | { |
761 | struct mgmt_pending_cmd *cmd; | |
441ad2d0 | 762 | |
fdf51784 AU |
763 | /* If there's a pending mgmt command the flag will not yet have |
764 | * it's final value, so check for this first. | |
765 | */ | |
766 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); | |
767 | if (cmd) { | |
768 | struct mgmt_mode *cp = cmd->param; | |
441ad2d0 | 769 | |
fdf51784 | 770 | return cp->val; |
441ad2d0 MH |
771 | } |
772 | ||
fdf51784 AU |
773 | return hci_dev_test_flag(hdev, HCI_CONNECTABLE); |
774 | } | |
441ad2d0 | 775 | |
7d78525d JH |
776 | static void service_cache_off(struct work_struct *work) |
777 | { | |
778 | struct hci_dev *hdev = container_of(work, struct hci_dev, | |
04124681 | 779 | service_cache.work); |
890ea898 | 780 | struct hci_request req; |
7d78525d | 781 | |
a69d8927 | 782 | if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) |
7d78525d JH |
783 | return; |
784 | ||
890ea898 JH |
785 | hci_req_init(&req, hdev); |
786 | ||
7d78525d JH |
787 | hci_dev_lock(hdev); |
788 | ||
b1a8917c | 789 | __hci_req_update_eir(&req); |
14bf5eac | 790 | __hci_req_update_class(&req); |
7d78525d JH |
791 | |
792 | hci_dev_unlock(hdev); | |
890ea898 JH |
793 | |
794 | hci_req_run(&req, NULL); | |
7d78525d JH |
795 | } |
796 | ||
d6bfd59c JH |
797 | static void rpa_expired(struct work_struct *work) |
798 | { | |
799 | struct hci_dev *hdev = container_of(work, struct hci_dev, | |
800 | rpa_expired.work); | |
801 | struct hci_request req; | |
802 | ||
803 | BT_DBG(""); | |
804 | ||
a1536da2 | 805 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
d6bfd59c | 806 | |
d7a5a11d | 807 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
d6bfd59c JH |
808 | return; |
809 | ||
810 | /* The generation of a new RPA and programming it into the | |
f2252570 JH |
811 | * controller happens in the hci_req_enable_advertising() |
812 | * function. | |
d6bfd59c | 813 | */ |
d6bfd59c | 814 | hci_req_init(&req, hdev); |
f2252570 | 815 | __hci_req_enable_advertising(&req); |
d6bfd59c JH |
816 | hci_req_run(&req, NULL); |
817 | } | |
818 | ||
6a919082 | 819 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
7d78525d | 820 | { |
238be788 | 821 | if (hci_dev_test_and_set_flag(hdev, HCI_MGMT)) |
6a919082 JH |
822 | return; |
823 | ||
4f87da80 | 824 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
d6bfd59c | 825 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
7d78525d | 826 | |
4f87da80 JH |
827 | /* Non-mgmt controlled devices get this bit set |
828 | * implicitly so that pairing works for them, however | |
829 | * for mgmt we require user-space to explicitly enable | |
830 | * it | |
831 | */ | |
a358dc11 | 832 | hci_dev_clear_flag(hdev, HCI_BONDABLE); |
7d78525d JH |
833 | } |
834 | ||
0f4e68cf | 835 | static int read_controller_info(struct sock *sk, struct hci_dev *hdev, |
04124681 | 836 | void *data, u16 data_len) |
0381101f | 837 | { |
a38528f1 | 838 | struct mgmt_rp_read_info rp; |
f7b64e69 | 839 | |
bdb6d971 | 840 | BT_DBG("sock %p %s", sk, hdev->name); |
f7b64e69 | 841 | |
09fd0de5 | 842 | hci_dev_lock(hdev); |
f7b64e69 | 843 | |
dc4fe30b JH |
844 | memset(&rp, 0, sizeof(rp)); |
845 | ||
69ab39ea | 846 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
f7b64e69 | 847 | |
69ab39ea | 848 | rp.version = hdev->hci_ver; |
eb55ef07 | 849 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
69ab39ea JH |
850 | |
851 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); | |
852 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); | |
f7b64e69 | 853 | |
a38528f1 | 854 | memcpy(rp.dev_class, hdev->dev_class, 3); |
f7b64e69 | 855 | |
dc4fe30b | 856 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
27fcc362 | 857 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
dc4fe30b | 858 | |
09fd0de5 | 859 | hci_dev_unlock(hdev); |
0381101f | 860 | |
2a1afb5a JH |
861 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
862 | sizeof(rp)); | |
0381101f JH |
863 | } |
864 | ||
69ab39ea | 865 | static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
8680570b | 866 | { |
69ab39ea | 867 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
8680570b | 868 | |
2a1afb5a JH |
869 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings, |
870 | sizeof(settings)); | |
8680570b JH |
871 | } |
872 | ||
1904a853 | 873 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
8b064a3a JH |
874 | { |
875 | BT_DBG("%s status 0x%02x", hdev->name, status); | |
876 | ||
a3172b7e JH |
877 | if (hci_conn_count(hdev) == 0) { |
878 | cancel_delayed_work(&hdev->power_off); | |
8b064a3a | 879 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
a3172b7e | 880 | } |
8b064a3a JH |
881 | } |
882 | ||
f2252570 | 883 | void mgmt_advertising_added(struct sock *sk, struct hci_dev *hdev, u8 instance) |
912098a6 AU |
884 | { |
885 | struct mgmt_ev_advertising_added ev; | |
886 | ||
887 | ev.instance = instance; | |
888 | ||
889 | mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk); | |
890 | } | |
891 | ||
f2252570 JH |
892 | void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev, |
893 | u8 instance) | |
912098a6 AU |
894 | { |
895 | struct mgmt_ev_advertising_removed ev; | |
896 | ||
897 | ev.instance = instance; | |
898 | ||
899 | mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk); | |
900 | } | |
901 | ||
7816b820 FG |
902 | static void cancel_adv_timeout(struct hci_dev *hdev) |
903 | { | |
904 | if (hdev->adv_instance_timeout) { | |
905 | hdev->adv_instance_timeout = 0; | |
906 | cancel_delayed_work(&hdev->adv_instance_expire); | |
907 | } | |
908 | } | |
909 | ||
8b064a3a JH |
910 | static int clean_up_hci_state(struct hci_dev *hdev) |
911 | { | |
912 | struct hci_request req; | |
913 | struct hci_conn *conn; | |
23a48093 JH |
914 | bool discov_stopped; |
915 | int err; | |
8b064a3a JH |
916 | |
917 | hci_req_init(&req, hdev); | |
918 | ||
919 | if (test_bit(HCI_ISCAN, &hdev->flags) || | |
920 | test_bit(HCI_PSCAN, &hdev->flags)) { | |
921 | u8 scan = 0x00; | |
922 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); | |
923 | } | |
924 | ||
f2252570 | 925 | hci_req_clear_adv_instance(hdev, NULL, 0x00, false); |
912098a6 | 926 | |
d7a5a11d | 927 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
f2252570 | 928 | __hci_req_disable_advertising(&req); |
8b064a3a | 929 | |
2154d3f4 | 930 | discov_stopped = hci_req_stop_discovery(&req); |
8b064a3a JH |
931 | |
932 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { | |
89e0ccc8 JH |
933 | /* 0x15 == Terminated due to Power Off */ |
934 | __hci_abort_conn(&req, conn, 0x15); | |
8b064a3a JH |
935 | } |
936 | ||
23a48093 JH |
937 | err = hci_req_run(&req, clean_up_hci_complete); |
938 | if (!err && discov_stopped) | |
939 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); | |
940 | ||
941 | return err; | |
8b064a3a JH |
942 | } |
943 | ||
bdb6d971 | 944 | static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data, |
04124681 | 945 | u16 len) |
eec8d2bc | 946 | { |
650f726d | 947 | struct mgmt_mode *cp = data; |
3b0602cd | 948 | struct mgmt_pending_cmd *cmd; |
4b34ee78 | 949 | int err; |
eec8d2bc | 950 | |
bdb6d971 | 951 | BT_DBG("request for %s", hdev->name); |
eec8d2bc | 952 | |
a7e80f25 | 953 | if (cp->val != 0x00 && cp->val != 0x01) |
a69e8375 JH |
954 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
955 | MGMT_STATUS_INVALID_PARAMS); | |
a7e80f25 | 956 | |
09fd0de5 | 957 | hci_dev_lock(hdev); |
eec8d2bc | 958 | |
333ae95d | 959 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) { |
a69e8375 JH |
960 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
961 | MGMT_STATUS_BUSY); | |
87b95ba6 JH |
962 | goto failed; |
963 | } | |
964 | ||
4b34ee78 | 965 | if (!!cp->val == hdev_is_powered(hdev)) { |
69ab39ea | 966 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
eec8d2bc JH |
967 | goto failed; |
968 | } | |
969 | ||
2e58ef3e | 970 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
366a0336 JH |
971 | if (!cmd) { |
972 | err = -ENOMEM; | |
eec8d2bc | 973 | goto failed; |
366a0336 | 974 | } |
eec8d2bc | 975 | |
8b064a3a | 976 | if (cp->val) { |
19202573 | 977 | queue_work(hdev->req_workqueue, &hdev->power_on); |
8b064a3a JH |
978 | err = 0; |
979 | } else { | |
980 | /* Disconnect connections, stop scans, etc */ | |
981 | err = clean_up_hci_state(hdev); | |
a3172b7e JH |
982 | if (!err) |
983 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, | |
984 | HCI_POWER_OFF_TIMEOUT); | |
eec8d2bc | 985 | |
8b064a3a JH |
986 | /* ENODATA means there were no HCI commands queued */ |
987 | if (err == -ENODATA) { | |
a3172b7e | 988 | cancel_delayed_work(&hdev->power_off); |
8b064a3a JH |
989 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
990 | err = 0; | |
991 | } | |
992 | } | |
eec8d2bc JH |
993 | |
994 | failed: | |
09fd0de5 | 995 | hci_dev_unlock(hdev); |
366a0336 | 996 | return err; |
eec8d2bc JH |
997 | } |
998 | ||
beadb2bd JH |
999 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
1000 | { | |
f6b7712e | 1001 | __le32 ev = cpu_to_le32(get_current_settings(hdev)); |
beadb2bd | 1002 | |
f6b7712e MH |
1003 | return mgmt_generic_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, |
1004 | sizeof(ev), skip); | |
beadb2bd JH |
1005 | } |
1006 | ||
91a668b0 JH |
1007 | int mgmt_new_settings(struct hci_dev *hdev) |
1008 | { | |
1009 | return new_settings(hdev, NULL); | |
1010 | } | |
1011 | ||
bd99abdd JH |
1012 | struct cmd_lookup { |
1013 | struct sock *sk; | |
1014 | struct hci_dev *hdev; | |
1015 | u8 mgmt_status; | |
1016 | }; | |
1017 | ||
3b0602cd | 1018 | static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data) |
bd99abdd JH |
1019 | { |
1020 | struct cmd_lookup *match = data; | |
1021 | ||
1022 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); | |
1023 | ||
1024 | list_del(&cmd->list); | |
1025 | ||
1026 | if (match->sk == NULL) { | |
1027 | match->sk = cmd->sk; | |
1028 | sock_hold(match->sk); | |
1029 | } | |
1030 | ||
1031 | mgmt_pending_free(cmd); | |
1032 | } | |
1033 | ||
3b0602cd | 1034 | static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data) |
bd99abdd JH |
1035 | { |
1036 | u8 *status = data; | |
1037 | ||
a69e8375 | 1038 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
bd99abdd JH |
1039 | mgmt_pending_remove(cmd); |
1040 | } | |
1041 | ||
3b0602cd | 1042 | static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data) |
1b9b5ee5 JH |
1043 | { |
1044 | if (cmd->cmd_complete) { | |
1045 | u8 *status = data; | |
1046 | ||
1047 | cmd->cmd_complete(cmd, *status); | |
1048 | mgmt_pending_remove(cmd); | |
1049 | ||
1050 | return; | |
1051 | } | |
1052 | ||
1053 | cmd_status_rsp(cmd, data); | |
1054 | } | |
1055 | ||
3b0602cd | 1056 | static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
f5818c22 | 1057 | { |
2a1afb5a JH |
1058 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
1059 | cmd->param, cmd->param_len); | |
f5818c22 JH |
1060 | } |
1061 | ||
3b0602cd | 1062 | static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
7776d1d8 | 1063 | { |
2a1afb5a JH |
1064 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
1065 | cmd->param, sizeof(struct mgmt_addr_info)); | |
7776d1d8 JH |
1066 | } |
1067 | ||
e6fe7986 JH |
1068 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
1069 | { | |
1070 | if (!lmp_bredr_capable(hdev)) | |
1071 | return MGMT_STATUS_NOT_SUPPORTED; | |
d7a5a11d | 1072 | else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
e6fe7986 JH |
1073 | return MGMT_STATUS_REJECTED; |
1074 | else | |
1075 | return MGMT_STATUS_SUCCESS; | |
1076 | } | |
1077 | ||
1078 | static u8 mgmt_le_support(struct hci_dev *hdev) | |
1079 | { | |
1080 | if (!lmp_le_capable(hdev)) | |
1081 | return MGMT_STATUS_NOT_SUPPORTED; | |
d7a5a11d | 1082 | else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
e6fe7986 JH |
1083 | return MGMT_STATUS_REJECTED; |
1084 | else | |
1085 | return MGMT_STATUS_SUCCESS; | |
1086 | } | |
1087 | ||
aed1a885 | 1088 | void mgmt_set_discoverable_complete(struct hci_dev *hdev, u8 status) |
bfaf8c9f | 1089 | { |
3b0602cd | 1090 | struct mgmt_pending_cmd *cmd; |
bfaf8c9f JH |
1091 | |
1092 | BT_DBG("status 0x%02x", status); | |
1093 | ||
1094 | hci_dev_lock(hdev); | |
1095 | ||
333ae95d | 1096 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
bfaf8c9f JH |
1097 | if (!cmd) |
1098 | goto unlock; | |
1099 | ||
1100 | if (status) { | |
1101 | u8 mgmt_err = mgmt_status(status); | |
a69e8375 | 1102 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
a358dc11 | 1103 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
bfaf8c9f JH |
1104 | goto remove_cmd; |
1105 | } | |
1106 | ||
aed1a885 JH |
1107 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
1108 | hdev->discov_timeout > 0) { | |
1109 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); | |
1110 | queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to); | |
d4462a07 | 1111 | } |
bfaf8c9f JH |
1112 | |
1113 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); | |
aed1a885 | 1114 | new_settings(hdev, cmd->sk); |
970ba524 | 1115 | |
bfaf8c9f JH |
1116 | remove_cmd: |
1117 | mgmt_pending_remove(cmd); | |
1118 | ||
1119 | unlock: | |
1120 | hci_dev_unlock(hdev); | |
1121 | } | |
1122 | ||
bdb6d971 | 1123 | static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data, |
04124681 | 1124 | u16 len) |
73f22f62 | 1125 | { |
650f726d | 1126 | struct mgmt_cp_set_discoverable *cp = data; |
3b0602cd | 1127 | struct mgmt_pending_cmd *cmd; |
5e5282bb | 1128 | u16 timeout; |
73f22f62 JH |
1129 | int err; |
1130 | ||
bdb6d971 | 1131 | BT_DBG("request for %s", hdev->name); |
73f22f62 | 1132 | |
d7a5a11d MH |
1133 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
1134 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) | |
a69e8375 JH |
1135 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
1136 | MGMT_STATUS_REJECTED); | |
33c525c0 | 1137 | |
310a3d48 | 1138 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
a69e8375 JH |
1139 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
1140 | MGMT_STATUS_INVALID_PARAMS); | |
a7e80f25 | 1141 | |
1f350c87 | 1142 | timeout = __le16_to_cpu(cp->timeout); |
310a3d48 MH |
1143 | |
1144 | /* Disabling discoverable requires that no timeout is set, | |
1145 | * and enabling limited discoverable requires a timeout. | |
1146 | */ | |
1147 | if ((cp->val == 0x00 && timeout > 0) || | |
1148 | (cp->val == 0x02 && timeout == 0)) | |
a69e8375 JH |
1149 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
1150 | MGMT_STATUS_INVALID_PARAMS); | |
73f22f62 | 1151 | |
09fd0de5 | 1152 | hci_dev_lock(hdev); |
73f22f62 | 1153 | |
5e5282bb | 1154 | if (!hdev_is_powered(hdev) && timeout > 0) { |
a69e8375 JH |
1155 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
1156 | MGMT_STATUS_NOT_POWERED); | |
73f22f62 JH |
1157 | goto failed; |
1158 | } | |
1159 | ||
333ae95d JH |
1160 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
1161 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { | |
a69e8375 JH |
1162 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
1163 | MGMT_STATUS_BUSY); | |
73f22f62 JH |
1164 | goto failed; |
1165 | } | |
1166 | ||
d7a5a11d | 1167 | if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) { |
a69e8375 JH |
1168 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
1169 | MGMT_STATUS_REJECTED); | |
5e5282bb JH |
1170 | goto failed; |
1171 | } | |
1172 | ||
1173 | if (!hdev_is_powered(hdev)) { | |
0224d2fa JH |
1174 | bool changed = false; |
1175 | ||
310a3d48 MH |
1176 | /* Setting limited discoverable when powered off is |
1177 | * not a valid operation since it requires a timeout | |
1178 | * and so no need to check HCI_LIMITED_DISCOVERABLE. | |
1179 | */ | |
d7a5a11d | 1180 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) { |
ce05d603 | 1181 | hci_dev_change_flag(hdev, HCI_DISCOVERABLE); |
0224d2fa JH |
1182 | changed = true; |
1183 | } | |
1184 | ||
5e5282bb | 1185 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
0224d2fa JH |
1186 | if (err < 0) |
1187 | goto failed; | |
1188 | ||
1189 | if (changed) | |
1190 | err = new_settings(hdev, sk); | |
1191 | ||
5e5282bb JH |
1192 | goto failed; |
1193 | } | |
1194 | ||
310a3d48 MH |
1195 | /* If the current mode is the same, then just update the timeout |
1196 | * value with the new value. And if only the timeout gets updated, | |
1197 | * then no need for any HCI transactions. | |
1198 | */ | |
d7a5a11d MH |
1199 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
1200 | (cp->val == 0x02) == hci_dev_test_flag(hdev, | |
1201 | HCI_LIMITED_DISCOVERABLE)) { | |
36261547 MH |
1202 | cancel_delayed_work(&hdev->discov_off); |
1203 | hdev->discov_timeout = timeout; | |
955638ec | 1204 | |
36261547 MH |
1205 | if (cp->val && hdev->discov_timeout > 0) { |
1206 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); | |
c366f555 JH |
1207 | queue_delayed_work(hdev->req_workqueue, |
1208 | &hdev->discov_off, to); | |
955638ec MH |
1209 | } |
1210 | ||
69ab39ea | 1211 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
73f22f62 JH |
1212 | goto failed; |
1213 | } | |
1214 | ||
2e58ef3e | 1215 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
366a0336 JH |
1216 | if (!cmd) { |
1217 | err = -ENOMEM; | |
73f22f62 | 1218 | goto failed; |
366a0336 | 1219 | } |
73f22f62 | 1220 | |
310a3d48 MH |
1221 | /* Cancel any potential discoverable timeout that might be |
1222 | * still active and store new timeout value. The arming of | |
1223 | * the timeout happens in the complete handler. | |
1224 | */ | |
1225 | cancel_delayed_work(&hdev->discov_off); | |
1226 | hdev->discov_timeout = timeout; | |
1227 | ||
aed1a885 JH |
1228 | if (cp->val) |
1229 | hci_dev_set_flag(hdev, HCI_DISCOVERABLE); | |
1230 | else | |
1231 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); | |
1232 | ||
b456f87c JH |
1233 | /* Limited discoverable mode */ |
1234 | if (cp->val == 0x02) | |
a1536da2 | 1235 | hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
b456f87c | 1236 | else |
a358dc11 | 1237 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
b456f87c | 1238 | |
aed1a885 JH |
1239 | queue_work(hdev->req_workqueue, &hdev->discoverable_update); |
1240 | err = 0; | |
73f22f62 JH |
1241 | |
1242 | failed: | |
09fd0de5 | 1243 | hci_dev_unlock(hdev); |
73f22f62 JH |
1244 | return err; |
1245 | } | |
1246 | ||
53c0ba74 | 1247 | void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status) |
2b76f453 | 1248 | { |
3b0602cd | 1249 | struct mgmt_pending_cmd *cmd; |
2b76f453 JH |
1250 | |
1251 | BT_DBG("status 0x%02x", status); | |
1252 | ||
1253 | hci_dev_lock(hdev); | |
1254 | ||
333ae95d | 1255 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
2b76f453 JH |
1256 | if (!cmd) |
1257 | goto unlock; | |
1258 | ||
37438c1f JH |
1259 | if (status) { |
1260 | u8 mgmt_err = mgmt_status(status); | |
a69e8375 | 1261 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
37438c1f JH |
1262 | goto remove_cmd; |
1263 | } | |
1264 | ||
2b76f453 | 1265 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
53c0ba74 | 1266 | new_settings(hdev, cmd->sk); |
d7b856f9 | 1267 | |
37438c1f | 1268 | remove_cmd: |
2b76f453 JH |
1269 | mgmt_pending_remove(cmd); |
1270 | ||
1271 | unlock: | |
1272 | hci_dev_unlock(hdev); | |
1273 | } | |
1274 | ||
e8ba3a1f JH |
1275 | static int set_connectable_update_settings(struct hci_dev *hdev, |
1276 | struct sock *sk, u8 val) | |
1277 | { | |
1278 | bool changed = false; | |
1279 | int err; | |
1280 | ||
d7a5a11d | 1281 | if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
e8ba3a1f JH |
1282 | changed = true; |
1283 | ||
1284 | if (val) { | |
a1536da2 | 1285 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
e8ba3a1f | 1286 | } else { |
a358dc11 MH |
1287 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
1288 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); | |
e8ba3a1f JH |
1289 | } |
1290 | ||
1291 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); | |
1292 | if (err < 0) | |
1293 | return err; | |
1294 | ||
562064e6 | 1295 | if (changed) { |
01b1cb87 | 1296 | hci_req_update_scan(hdev); |
562064e6 | 1297 | hci_update_background_scan(hdev); |
e8ba3a1f | 1298 | return new_settings(hdev, sk); |
562064e6 | 1299 | } |
e8ba3a1f JH |
1300 | |
1301 | return 0; | |
1302 | } | |
1303 | ||
bdb6d971 | 1304 | static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data, |
04124681 | 1305 | u16 len) |
9fbcbb45 | 1306 | { |
650f726d | 1307 | struct mgmt_mode *cp = data; |
3b0602cd | 1308 | struct mgmt_pending_cmd *cmd; |
9fbcbb45 JH |
1309 | int err; |
1310 | ||
bdb6d971 | 1311 | BT_DBG("request for %s", hdev->name); |
9fbcbb45 | 1312 | |
d7a5a11d MH |
1313 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
1314 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) | |
a69e8375 JH |
1315 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
1316 | MGMT_STATUS_REJECTED); | |
33c525c0 | 1317 | |
a7e80f25 | 1318 | if (cp->val != 0x00 && cp->val != 0x01) |
a69e8375 JH |
1319 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
1320 | MGMT_STATUS_INVALID_PARAMS); | |
a7e80f25 | 1321 | |
09fd0de5 | 1322 | hci_dev_lock(hdev); |
9fbcbb45 | 1323 | |
4b34ee78 | 1324 | if (!hdev_is_powered(hdev)) { |
e8ba3a1f | 1325 | err = set_connectable_update_settings(hdev, sk, cp->val); |
9fbcbb45 JH |
1326 | goto failed; |
1327 | } | |
1328 | ||
333ae95d JH |
1329 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
1330 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { | |
a69e8375 JH |
1331 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
1332 | MGMT_STATUS_BUSY); | |
9fbcbb45 JH |
1333 | goto failed; |
1334 | } | |
1335 | ||
2e58ef3e | 1336 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
366a0336 JH |
1337 | if (!cmd) { |
1338 | err = -ENOMEM; | |
9fbcbb45 | 1339 | goto failed; |
366a0336 | 1340 | } |
9fbcbb45 | 1341 | |
53c0ba74 JH |
1342 | if (cp->val) { |
1343 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); | |
1344 | } else { | |
1345 | if (hdev->discov_timeout > 0) | |
1346 | cancel_delayed_work(&hdev->discov_off); | |
2b76f453 | 1347 | |
53c0ba74 JH |
1348 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
1349 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); | |
1350 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); | |
9b74246f | 1351 | } |
2b76f453 | 1352 | |
53c0ba74 JH |
1353 | queue_work(hdev->req_workqueue, &hdev->connectable_update); |
1354 | err = 0; | |
9fbcbb45 JH |
1355 | |
1356 | failed: | |
09fd0de5 | 1357 | hci_dev_unlock(hdev); |
9fbcbb45 JH |
1358 | return err; |
1359 | } | |
1360 | ||
b2939475 | 1361 | static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data, |
04124681 | 1362 | u16 len) |
c542a06c | 1363 | { |
650f726d | 1364 | struct mgmt_mode *cp = data; |
55594356 | 1365 | bool changed; |
c542a06c JH |
1366 | int err; |
1367 | ||
bdb6d971 | 1368 | BT_DBG("request for %s", hdev->name); |
c542a06c | 1369 | |
a7e80f25 | 1370 | if (cp->val != 0x00 && cp->val != 0x01) |
a69e8375 JH |
1371 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, |
1372 | MGMT_STATUS_INVALID_PARAMS); | |
a7e80f25 | 1373 | |
09fd0de5 | 1374 | hci_dev_lock(hdev); |
c542a06c JH |
1375 | |
1376 | if (cp->val) | |
238be788 | 1377 | changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE); |
c542a06c | 1378 | else |
a69d8927 | 1379 | changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE); |
c542a06c | 1380 | |
b2939475 | 1381 | err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); |
c542a06c | 1382 | if (err < 0) |
55594356 | 1383 | goto unlock; |
c542a06c | 1384 | |
55594356 MH |
1385 | if (changed) |
1386 | err = new_settings(hdev, sk); | |
c542a06c | 1387 | |
55594356 | 1388 | unlock: |
09fd0de5 | 1389 | hci_dev_unlock(hdev); |
c542a06c JH |
1390 | return err; |
1391 | } | |
1392 | ||
04124681 GP |
1393 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
1394 | u16 len) | |
33ef95ed JH |
1395 | { |
1396 | struct mgmt_mode *cp = data; | |
3b0602cd | 1397 | struct mgmt_pending_cmd *cmd; |
e6fe7986 | 1398 | u8 val, status; |
33ef95ed JH |
1399 | int err; |
1400 | ||
bdb6d971 | 1401 | BT_DBG("request for %s", hdev->name); |
33ef95ed | 1402 | |
e6fe7986 JH |
1403 | status = mgmt_bredr_support(hdev); |
1404 | if (status) | |
a69e8375 JH |
1405 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
1406 | status); | |
33c525c0 | 1407 | |
a7e80f25 | 1408 | if (cp->val != 0x00 && cp->val != 0x01) |
a69e8375 JH |
1409 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
1410 | MGMT_STATUS_INVALID_PARAMS); | |
a7e80f25 | 1411 | |
33ef95ed JH |
1412 | hci_dev_lock(hdev); |
1413 | ||
4b34ee78 | 1414 | if (!hdev_is_powered(hdev)) { |
47990ea0 JH |
1415 | bool changed = false; |
1416 | ||
d7a5a11d | 1417 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { |
ce05d603 | 1418 | hci_dev_change_flag(hdev, HCI_LINK_SECURITY); |
47990ea0 JH |
1419 | changed = true; |
1420 | } | |
1421 | ||
1422 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); | |
1423 | if (err < 0) | |
1424 | goto failed; | |
1425 | ||
1426 | if (changed) | |
1427 | err = new_settings(hdev, sk); | |
1428 | ||
33ef95ed JH |
1429 | goto failed; |
1430 | } | |
1431 | ||
333ae95d | 1432 | if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
a69e8375 JH |
1433 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
1434 | MGMT_STATUS_BUSY); | |
33ef95ed JH |
1435 | goto failed; |
1436 | } | |
1437 | ||
1438 | val = !!cp->val; | |
1439 | ||
1440 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { | |
1441 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); | |
1442 | goto failed; | |
1443 | } | |
1444 | ||
1445 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); | |
1446 | if (!cmd) { | |
1447 | err = -ENOMEM; | |
1448 | goto failed; | |
1449 | } | |
1450 | ||
1451 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); | |
1452 | if (err < 0) { | |
1453 | mgmt_pending_remove(cmd); | |
1454 | goto failed; | |
1455 | } | |
1456 | ||
1457 | failed: | |
1458 | hci_dev_unlock(hdev); | |
33ef95ed JH |
1459 | return err; |
1460 | } | |
1461 | ||
bdb6d971 | 1462 | static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
ed2c4ee3 JH |
1463 | { |
1464 | struct mgmt_mode *cp = data; | |
3b0602cd | 1465 | struct mgmt_pending_cmd *cmd; |
72ef0c1a | 1466 | u8 status; |
ed2c4ee3 JH |
1467 | int err; |
1468 | ||
bdb6d971 | 1469 | BT_DBG("request for %s", hdev->name); |
ed2c4ee3 | 1470 | |
cdba5281 MH |
1471 | status = mgmt_bredr_support(hdev); |
1472 | if (status) | |
a69e8375 | 1473 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
cdba5281 | 1474 | |
13ecd8b6 | 1475 | if (!lmp_ssp_capable(hdev)) |
a69e8375 JH |
1476 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
1477 | MGMT_STATUS_NOT_SUPPORTED); | |
ed2c4ee3 | 1478 | |
a7e80f25 | 1479 | if (cp->val != 0x00 && cp->val != 0x01) |
a69e8375 JH |
1480 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
1481 | MGMT_STATUS_INVALID_PARAMS); | |
a7e80f25 | 1482 | |
13ecd8b6 | 1483 | hci_dev_lock(hdev); |
6c8f12c1 | 1484 | |
4b34ee78 | 1485 | if (!hdev_is_powered(hdev)) { |
9ecb3e24 | 1486 | bool changed; |
c0ecddc2 | 1487 | |
9ecb3e24 | 1488 | if (cp->val) { |
238be788 MH |
1489 | changed = !hci_dev_test_and_set_flag(hdev, |
1490 | HCI_SSP_ENABLED); | |
9ecb3e24 | 1491 | } else { |
a69d8927 MH |
1492 | changed = hci_dev_test_and_clear_flag(hdev, |
1493 | HCI_SSP_ENABLED); | |
9ecb3e24 | 1494 | if (!changed) |
a69d8927 MH |
1495 | changed = hci_dev_test_and_clear_flag(hdev, |
1496 | HCI_HS_ENABLED); | |
9ecb3e24 | 1497 | else |
a358dc11 | 1498 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
c0ecddc2 JH |
1499 | } |
1500 | ||
1501 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); | |
1502 | if (err < 0) | |
1503 | goto failed; | |
1504 | ||
1505 | if (changed) | |
1506 | err = new_settings(hdev, sk); | |
1507 | ||
ed2c4ee3 JH |
1508 | goto failed; |
1509 | } | |
1510 | ||
333ae95d | 1511 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
a69e8375 JH |
1512 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
1513 | MGMT_STATUS_BUSY); | |
ed2c4ee3 JH |
1514 | goto failed; |
1515 | } | |
1516 | ||
d7a5a11d | 1517 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
ed2c4ee3 JH |
1518 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
1519 | goto failed; | |
1520 | } | |
1521 | ||
1522 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); | |
1523 | if (!cmd) { | |
1524 | err = -ENOMEM; | |
1525 | goto failed; | |
1526 | } | |
1527 | ||
d7a5a11d | 1528 | if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
3769972b JH |
1529 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
1530 | sizeof(cp->val), &cp->val); | |
1531 | ||
72ef0c1a | 1532 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val); |
ed2c4ee3 JH |
1533 | if (err < 0) { |
1534 | mgmt_pending_remove(cmd); | |
1535 | goto failed; | |
1536 | } | |
1537 | ||
1538 | failed: | |
1539 | hci_dev_unlock(hdev); | |
ed2c4ee3 JH |
1540 | return err; |
1541 | } | |
1542 | ||
bdb6d971 | 1543 | static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
6d80dfd0 JH |
1544 | { |
1545 | struct mgmt_mode *cp = data; | |
ee392693 | 1546 | bool changed; |
e6fe7986 | 1547 | u8 status; |
ee392693 | 1548 | int err; |
6d80dfd0 | 1549 | |
bdb6d971 | 1550 | BT_DBG("request for %s", hdev->name); |
6d80dfd0 | 1551 | |
e6fe7986 JH |
1552 | status = mgmt_bredr_support(hdev); |
1553 | if (status) | |
a69e8375 | 1554 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
6d80dfd0 | 1555 | |
9ecb3e24 | 1556 | if (!lmp_ssp_capable(hdev)) |
a69e8375 JH |
1557 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
1558 | MGMT_STATUS_NOT_SUPPORTED); | |
9ecb3e24 | 1559 | |
d7a5a11d | 1560 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
a69e8375 JH |
1561 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
1562 | MGMT_STATUS_REJECTED); | |
9ecb3e24 | 1563 | |
a7e80f25 | 1564 | if (cp->val != 0x00 && cp->val != 0x01) |
a69e8375 JH |
1565 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
1566 | MGMT_STATUS_INVALID_PARAMS); | |
a7e80f25 | 1567 | |
ee392693 MH |
1568 | hci_dev_lock(hdev); |
1569 | ||
333ae95d | 1570 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
a69e8375 JH |
1571 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
1572 | MGMT_STATUS_BUSY); | |
a2cb01de JH |
1573 | goto unlock; |
1574 | } | |
1575 | ||
a0cdf960 | 1576 | if (cp->val) { |
238be788 | 1577 | changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED); |
a0cdf960 MH |
1578 | } else { |
1579 | if (hdev_is_powered(hdev)) { | |
a69e8375 JH |
1580 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
1581 | MGMT_STATUS_REJECTED); | |
a0cdf960 MH |
1582 | goto unlock; |
1583 | } | |
1584 | ||
a69d8927 | 1585 | changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED); |
a0cdf960 | 1586 | } |
ee392693 MH |
1587 | |
1588 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); | |
1589 | if (err < 0) | |
1590 | goto unlock; | |
1591 | ||
1592 | if (changed) | |
1593 | err = new_settings(hdev, sk); | |
6d80dfd0 | 1594 | |
ee392693 MH |
1595 | unlock: |
1596 | hci_dev_unlock(hdev); | |
1597 | return err; | |
6d80dfd0 JH |
1598 | } |
1599 | ||
1904a853 | 1600 | static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
416a4ae5 JH |
1601 | { |
1602 | struct cmd_lookup match = { NULL, hdev }; | |
1603 | ||
3ad67582 JK |
1604 | hci_dev_lock(hdev); |
1605 | ||
416a4ae5 JH |
1606 | if (status) { |
1607 | u8 mgmt_err = mgmt_status(status); | |
1608 | ||
1609 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, | |
1610 | &mgmt_err); | |
3ad67582 | 1611 | goto unlock; |
416a4ae5 JH |
1612 | } |
1613 | ||
1614 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); | |
1615 | ||
1616 | new_settings(hdev, match.sk); | |
1617 | ||
1618 | if (match.sk) | |
1619 | sock_put(match.sk); | |
441ad2d0 MH |
1620 | |
1621 | /* Make sure the controller has a good default for | |
1622 | * advertising data. Restrict the update to when LE | |
1623 | * has actually been enabled. During power on, the | |
1624 | * update in powered_update_hci will take care of it. | |
1625 | */ | |
d7a5a11d | 1626 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
441ad2d0 MH |
1627 | struct hci_request req; |
1628 | ||
441ad2d0 | 1629 | hci_req_init(&req, hdev); |
cab054ab JH |
1630 | __hci_req_update_adv_data(&req, 0x00); |
1631 | __hci_req_update_scan_rsp_data(&req, 0x00); | |
441ad2d0 | 1632 | hci_req_run(&req, NULL); |
2e93e53b | 1633 | hci_update_background_scan(hdev); |
441ad2d0 | 1634 | } |
3ad67582 JK |
1635 | |
1636 | unlock: | |
1637 | hci_dev_unlock(hdev); | |
416a4ae5 JH |
1638 | } |
1639 | ||
bdb6d971 | 1640 | static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
06199cf8 JH |
1641 | { |
1642 | struct mgmt_mode *cp = data; | |
1643 | struct hci_cp_write_le_host_supported hci_cp; | |
3b0602cd | 1644 | struct mgmt_pending_cmd *cmd; |
416a4ae5 | 1645 | struct hci_request req; |
06199cf8 | 1646 | int err; |
0b60eba1 | 1647 | u8 val, enabled; |
06199cf8 | 1648 | |
bdb6d971 | 1649 | BT_DBG("request for %s", hdev->name); |
06199cf8 | 1650 | |
13ecd8b6 | 1651 | if (!lmp_le_capable(hdev)) |
a69e8375 JH |
1652 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
1653 | MGMT_STATUS_NOT_SUPPORTED); | |
1de028ce | 1654 | |
a7e80f25 | 1655 | if (cp->val != 0x00 && cp->val != 0x01) |
a69e8375 JH |
1656 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
1657 | MGMT_STATUS_INVALID_PARAMS); | |
a7e80f25 | 1658 | |
e7844ee5 MH |
1659 | /* Bluetooth single mode LE only controllers or dual-mode |
1660 | * controllers configured as LE only devices, do not allow | |
1661 | * switching LE off. These have either LE enabled explicitly | |
1662 | * or BR/EDR has been previously switched off. | |
1663 | * | |
1664 | * When trying to enable an already enabled LE, then gracefully | |
1665 | * send a positive response. Trying to disable it however will | |
1666 | * result into rejection. | |
1667 | */ | |
1668 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { | |
1669 | if (cp->val == 0x01) | |
1670 | return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); | |
1671 | ||
a69e8375 JH |
1672 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
1673 | MGMT_STATUS_REJECTED); | |
e7844ee5 | 1674 | } |
c73eee91 | 1675 | |
13ecd8b6 | 1676 | hci_dev_lock(hdev); |
06199cf8 JH |
1677 | |
1678 | val = !!cp->val; | |
ffa88e02 | 1679 | enabled = lmp_host_le_capable(hdev); |
06199cf8 | 1680 | |
847818d9 | 1681 | if (!val) |
f2252570 | 1682 | hci_req_clear_adv_instance(hdev, NULL, 0x00, true); |
847818d9 | 1683 | |
0b60eba1 | 1684 | if (!hdev_is_powered(hdev) || val == enabled) { |
06199cf8 JH |
1685 | bool changed = false; |
1686 | ||
d7a5a11d | 1687 | if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
ce05d603 | 1688 | hci_dev_change_flag(hdev, HCI_LE_ENABLED); |
06199cf8 JH |
1689 | changed = true; |
1690 | } | |
1691 | ||
d7a5a11d | 1692 | if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
a358dc11 | 1693 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
eeca6f89 JH |
1694 | changed = true; |
1695 | } | |
1696 | ||
06199cf8 JH |
1697 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
1698 | if (err < 0) | |
1de028ce | 1699 | goto unlock; |
06199cf8 JH |
1700 | |
1701 | if (changed) | |
1702 | err = new_settings(hdev, sk); | |
1703 | ||
1de028ce | 1704 | goto unlock; |
06199cf8 JH |
1705 | } |
1706 | ||
333ae95d JH |
1707 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
1708 | pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { | |
a69e8375 JH |
1709 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
1710 | MGMT_STATUS_BUSY); | |
1de028ce | 1711 | goto unlock; |
06199cf8 JH |
1712 | } |
1713 | ||
1714 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); | |
1715 | if (!cmd) { | |
1716 | err = -ENOMEM; | |
1de028ce | 1717 | goto unlock; |
06199cf8 JH |
1718 | } |
1719 | ||
441ad2d0 MH |
1720 | hci_req_init(&req, hdev); |
1721 | ||
06199cf8 JH |
1722 | memset(&hci_cp, 0, sizeof(hci_cp)); |
1723 | ||
1724 | if (val) { | |
1725 | hci_cp.le = val; | |
32226e4f | 1726 | hci_cp.simul = 0x00; |
441ad2d0 | 1727 | } else { |
d7a5a11d | 1728 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
f2252570 | 1729 | __hci_req_disable_advertising(&req); |
06199cf8 JH |
1730 | } |
1731 | ||
416a4ae5 JH |
1732 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
1733 | &hci_cp); | |
1734 | ||
1735 | err = hci_req_run(&req, le_enable_complete); | |
0c01bc48 | 1736 | if (err < 0) |
06199cf8 | 1737 | mgmt_pending_remove(cmd); |
06199cf8 | 1738 | |
1de028ce JH |
1739 | unlock: |
1740 | hci_dev_unlock(hdev); | |
06199cf8 JH |
1741 | return err; |
1742 | } | |
1743 | ||
0cab9c80 JH |
1744 | /* This is a helper function to test for pending mgmt commands that can |
1745 | * cause CoD or EIR HCI commands. We can only allow one such pending | |
1746 | * mgmt command at a time since otherwise we cannot easily track what | |
1747 | * the current values are, will be, and based on that calculate if a new | |
1748 | * HCI command needs to be sent and if yes with what value. | |
1749 | */ | |
1750 | static bool pending_eir_or_class(struct hci_dev *hdev) | |
1751 | { | |
3b0602cd | 1752 | struct mgmt_pending_cmd *cmd; |
0cab9c80 JH |
1753 | |
1754 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { | |
1755 | switch (cmd->opcode) { | |
1756 | case MGMT_OP_ADD_UUID: | |
1757 | case MGMT_OP_REMOVE_UUID: | |
1758 | case MGMT_OP_SET_DEV_CLASS: | |
1759 | case MGMT_OP_SET_POWERED: | |
1760 | return true; | |
1761 | } | |
1762 | } | |
1763 | ||
1764 | return false; | |
1765 | } | |
1766 | ||
83be8eca JH |
1767 | static const u8 bluetooth_base_uuid[] = { |
1768 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, | |
1769 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
1770 | }; | |
1771 | ||
1772 | static u8 get_uuid_size(const u8 *uuid) | |
1773 | { | |
1774 | u32 val; | |
1775 | ||
1776 | if (memcmp(uuid, bluetooth_base_uuid, 12)) | |
1777 | return 128; | |
1778 | ||
1779 | val = get_unaligned_le32(&uuid[12]); | |
1780 | if (val > 0xffff) | |
1781 | return 32; | |
1782 | ||
1783 | return 16; | |
1784 | } | |
1785 | ||
92da6097 JH |
1786 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
1787 | { | |
3b0602cd | 1788 | struct mgmt_pending_cmd *cmd; |
92da6097 JH |
1789 | |
1790 | hci_dev_lock(hdev); | |
1791 | ||
333ae95d | 1792 | cmd = pending_find(mgmt_op, hdev); |
92da6097 JH |
1793 | if (!cmd) |
1794 | goto unlock; | |
1795 | ||
2a1afb5a JH |
1796 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
1797 | mgmt_status(status), hdev->dev_class, 3); | |
92da6097 JH |
1798 | |
1799 | mgmt_pending_remove(cmd); | |
1800 | ||
1801 | unlock: | |
1802 | hci_dev_unlock(hdev); | |
1803 | } | |
1804 | ||
1904a853 | 1805 | static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
92da6097 JH |
1806 | { |
1807 | BT_DBG("status 0x%02x", status); | |
1808 | ||
1809 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); | |
1810 | } | |
1811 | ||
bdb6d971 | 1812 | static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
2aeb9a1a | 1813 | { |
650f726d | 1814 | struct mgmt_cp_add_uuid *cp = data; |
3b0602cd | 1815 | struct mgmt_pending_cmd *cmd; |
890ea898 | 1816 | struct hci_request req; |
2aeb9a1a | 1817 | struct bt_uuid *uuid; |
2aeb9a1a JH |
1818 | int err; |
1819 | ||
bdb6d971 | 1820 | BT_DBG("request for %s", hdev->name); |
2aeb9a1a | 1821 | |
09fd0de5 | 1822 | hci_dev_lock(hdev); |
2aeb9a1a | 1823 | |
0cab9c80 | 1824 | if (pending_eir_or_class(hdev)) { |
a69e8375 JH |
1825 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
1826 | MGMT_STATUS_BUSY); | |
c95f0ba7 JH |
1827 | goto failed; |
1828 | } | |
1829 | ||
92c4c204 | 1830 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
2aeb9a1a JH |
1831 | if (!uuid) { |
1832 | err = -ENOMEM; | |
1833 | goto failed; | |
1834 | } | |
1835 | ||
1836 | memcpy(uuid->uuid, cp->uuid, 16); | |
1aff6f09 | 1837 | uuid->svc_hint = cp->svc_hint; |
83be8eca | 1838 | uuid->size = get_uuid_size(cp->uuid); |
2aeb9a1a | 1839 | |
de66aa63 | 1840 | list_add_tail(&uuid->list, &hdev->uuids); |
2aeb9a1a | 1841 | |
890ea898 | 1842 | hci_req_init(&req, hdev); |
1aff6f09 | 1843 | |
14bf5eac | 1844 | __hci_req_update_class(&req); |
b1a8917c | 1845 | __hci_req_update_eir(&req); |
890ea898 | 1846 | |
92da6097 JH |
1847 | err = hci_req_run(&req, add_uuid_complete); |
1848 | if (err < 0) { | |
1849 | if (err != -ENODATA) | |
1850 | goto failed; | |
80a1e1db | 1851 | |
2a1afb5a JH |
1852 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
1853 | hdev->dev_class, 3); | |
90e70454 JH |
1854 | goto failed; |
1855 | } | |
1856 | ||
1857 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); | |
890ea898 | 1858 | if (!cmd) { |
90e70454 | 1859 | err = -ENOMEM; |
890ea898 JH |
1860 | goto failed; |
1861 | } | |
1862 | ||
1863 | err = 0; | |
2aeb9a1a JH |
1864 | |
1865 | failed: | |
09fd0de5 | 1866 | hci_dev_unlock(hdev); |
2aeb9a1a JH |
1867 | return err; |
1868 | } | |
1869 | ||
24b78d0f JH |
1870 | static bool enable_service_cache(struct hci_dev *hdev) |
1871 | { | |
1872 | if (!hdev_is_powered(hdev)) | |
1873 | return false; | |
1874 | ||
238be788 | 1875 | if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) { |
46818ed5 JH |
1876 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
1877 | CACHE_TIMEOUT); | |
24b78d0f JH |
1878 | return true; |
1879 | } | |
1880 | ||
1881 | return false; | |
1882 | } | |
1883 | ||
1904a853 | 1884 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
92da6097 JH |
1885 | { |
1886 | BT_DBG("status 0x%02x", status); | |
1887 | ||
1888 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); | |
1889 | } | |
1890 | ||
bdb6d971 | 1891 | static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data, |
8ce8e2b5 | 1892 | u16 len) |
2aeb9a1a | 1893 | { |
650f726d | 1894 | struct mgmt_cp_remove_uuid *cp = data; |
3b0602cd | 1895 | struct mgmt_pending_cmd *cmd; |
056341c8 | 1896 | struct bt_uuid *match, *tmp; |
2aeb9a1a | 1897 | u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
890ea898 | 1898 | struct hci_request req; |
2aeb9a1a JH |
1899 | int err, found; |
1900 | ||
bdb6d971 | 1901 | BT_DBG("request for %s", hdev->name); |
2aeb9a1a | 1902 | |
09fd0de5 | 1903 | hci_dev_lock(hdev); |
2aeb9a1a | 1904 | |
0cab9c80 | 1905 | if (pending_eir_or_class(hdev)) { |
a69e8375 JH |
1906 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
1907 | MGMT_STATUS_BUSY); | |
c95f0ba7 JH |
1908 | goto unlock; |
1909 | } | |
1910 | ||
2aeb9a1a | 1911 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
35f7498a | 1912 | hci_uuids_clear(hdev); |
4004b6d9 | 1913 | |
24b78d0f | 1914 | if (enable_service_cache(hdev)) { |
2a1afb5a JH |
1915 | err = mgmt_cmd_complete(sk, hdev->id, |
1916 | MGMT_OP_REMOVE_UUID, | |
1917 | 0, hdev->dev_class, 3); | |
24b78d0f JH |
1918 | goto unlock; |
1919 | } | |
4004b6d9 | 1920 | |
9246a869 | 1921 | goto update_class; |
2aeb9a1a JH |
1922 | } |
1923 | ||
1924 | found = 0; | |
1925 | ||
056341c8 | 1926 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
2aeb9a1a JH |
1927 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
1928 | continue; | |
1929 | ||
1930 | list_del(&match->list); | |
482049f7 | 1931 | kfree(match); |
2aeb9a1a JH |
1932 | found++; |
1933 | } | |
1934 | ||
1935 | if (found == 0) { | |
a69e8375 JH |
1936 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
1937 | MGMT_STATUS_INVALID_PARAMS); | |
2aeb9a1a JH |
1938 | goto unlock; |
1939 | } | |
1940 | ||
9246a869 | 1941 | update_class: |
890ea898 | 1942 | hci_req_init(&req, hdev); |
1aff6f09 | 1943 | |
14bf5eac | 1944 | __hci_req_update_class(&req); |
b1a8917c | 1945 | __hci_req_update_eir(&req); |
890ea898 | 1946 | |
92da6097 JH |
1947 | err = hci_req_run(&req, remove_uuid_complete); |
1948 | if (err < 0) { | |
1949 | if (err != -ENODATA) | |
1950 | goto unlock; | |
80a1e1db | 1951 | |
2a1afb5a JH |
1952 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
1953 | hdev->dev_class, 3); | |
90e70454 JH |
1954 | goto unlock; |
1955 | } | |
1956 | ||
1957 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); | |
890ea898 | 1958 | if (!cmd) { |
90e70454 | 1959 | err = -ENOMEM; |
890ea898 JH |
1960 | goto unlock; |
1961 | } | |
1962 | ||
1963 | err = 0; | |
2aeb9a1a JH |
1964 | |
1965 | unlock: | |
09fd0de5 | 1966 | hci_dev_unlock(hdev); |
2aeb9a1a JH |
1967 | return err; |
1968 | } | |
1969 | ||
1904a853 | 1970 | static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
92da6097 JH |
1971 | { |
1972 | BT_DBG("status 0x%02x", status); | |
1973 | ||
1974 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); | |
1975 | } | |
1976 | ||
bdb6d971 | 1977 | static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data, |
04124681 | 1978 | u16 len) |
1aff6f09 | 1979 | { |
650f726d | 1980 | struct mgmt_cp_set_dev_class *cp = data; |
3b0602cd | 1981 | struct mgmt_pending_cmd *cmd; |
890ea898 | 1982 | struct hci_request req; |
1aff6f09 JH |
1983 | int err; |
1984 | ||
bdb6d971 | 1985 | BT_DBG("request for %s", hdev->name); |
1aff6f09 | 1986 | |
6203fc98 | 1987 | if (!lmp_bredr_capable(hdev)) |
a69e8375 JH |
1988 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
1989 | MGMT_STATUS_NOT_SUPPORTED); | |
1aff6f09 | 1990 | |
0cab9c80 | 1991 | hci_dev_lock(hdev); |
ee98f473 | 1992 | |
0cab9c80 | 1993 | if (pending_eir_or_class(hdev)) { |
a69e8375 JH |
1994 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
1995 | MGMT_STATUS_BUSY); | |
0cab9c80 JH |
1996 | goto unlock; |
1997 | } | |
c95f0ba7 | 1998 | |
0cab9c80 | 1999 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
a69e8375 JH |
2000 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
2001 | MGMT_STATUS_INVALID_PARAMS); | |
0cab9c80 JH |
2002 | goto unlock; |
2003 | } | |
575b3a02 | 2004 | |
932f5ff5 JH |
2005 | hdev->major_class = cp->major; |
2006 | hdev->minor_class = cp->minor; | |
2007 | ||
b5235a65 | 2008 | if (!hdev_is_powered(hdev)) { |
2a1afb5a JH |
2009 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
2010 | hdev->dev_class, 3); | |
b5235a65 JH |
2011 | goto unlock; |
2012 | } | |
2013 | ||
890ea898 JH |
2014 | hci_req_init(&req, hdev); |
2015 | ||
a69d8927 | 2016 | if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) { |
7d78525d JH |
2017 | hci_dev_unlock(hdev); |
2018 | cancel_delayed_work_sync(&hdev->service_cache); | |
2019 | hci_dev_lock(hdev); | |
b1a8917c | 2020 | __hci_req_update_eir(&req); |
7d78525d | 2021 | } |
14c0b608 | 2022 | |
14bf5eac | 2023 | __hci_req_update_class(&req); |
890ea898 | 2024 | |
92da6097 JH |
2025 | err = hci_req_run(&req, set_class_complete); |
2026 | if (err < 0) { | |
2027 | if (err != -ENODATA) | |
2028 | goto unlock; | |
1aff6f09 | 2029 | |
2a1afb5a JH |
2030 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
2031 | hdev->dev_class, 3); | |
90e70454 JH |
2032 | goto unlock; |
2033 | } | |
2034 | ||
2035 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len); | |
890ea898 | 2036 | if (!cmd) { |
90e70454 | 2037 | err = -ENOMEM; |
890ea898 JH |
2038 | goto unlock; |
2039 | } | |
2040 | ||
2041 | err = 0; | |
1aff6f09 | 2042 | |
b5235a65 | 2043 | unlock: |
09fd0de5 | 2044 | hci_dev_unlock(hdev); |
1aff6f09 JH |
2045 | return err; |
2046 | } | |
2047 | ||
bdb6d971 | 2048 | static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data, |
8ce8e2b5 | 2049 | u16 len) |
55ed8ca1 | 2050 | { |
650f726d | 2051 | struct mgmt_cp_load_link_keys *cp = data; |
ba1d6936 JH |
2052 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
2053 | sizeof(struct mgmt_link_key_info)); | |
4e51eae9 | 2054 | u16 key_count, expected_len; |
b1de97d8 | 2055 | bool changed; |
a492cd52 | 2056 | int i; |
55ed8ca1 | 2057 | |
9060d5cf MH |
2058 | BT_DBG("request for %s", hdev->name); |
2059 | ||
2060 | if (!lmp_bredr_capable(hdev)) | |
a69e8375 JH |
2061 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
2062 | MGMT_STATUS_NOT_SUPPORTED); | |
9060d5cf | 2063 | |
1f350c87 | 2064 | key_count = __le16_to_cpu(cp->key_count); |
ba1d6936 JH |
2065 | if (key_count > max_key_count) { |
2066 | BT_ERR("load_link_keys: too big key_count value %u", | |
2067 | key_count); | |
a69e8375 JH |
2068 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
2069 | MGMT_STATUS_INVALID_PARAMS); | |
ba1d6936 | 2070 | } |
55ed8ca1 | 2071 | |
86742e1e JH |
2072 | expected_len = sizeof(*cp) + key_count * |
2073 | sizeof(struct mgmt_link_key_info); | |
a492cd52 | 2074 | if (expected_len != len) { |
86742e1e | 2075 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
2606ecbc | 2076 | expected_len, len); |
a69e8375 JH |
2077 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
2078 | MGMT_STATUS_INVALID_PARAMS); | |
55ed8ca1 JH |
2079 | } |
2080 | ||
4ae14301 | 2081 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
a69e8375 JH |
2082 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
2083 | MGMT_STATUS_INVALID_PARAMS); | |
4ae14301 | 2084 | |
bdb6d971 | 2085 | BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys, |
8ce8e2b5 | 2086 | key_count); |
55ed8ca1 | 2087 | |
4ee71b20 JH |
2088 | for (i = 0; i < key_count; i++) { |
2089 | struct mgmt_link_key_info *key = &cp->keys[i]; | |
2090 | ||
8e991132 | 2091 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
a69e8375 JH |
2092 | return mgmt_cmd_status(sk, hdev->id, |
2093 | MGMT_OP_LOAD_LINK_KEYS, | |
2094 | MGMT_STATUS_INVALID_PARAMS); | |
4ee71b20 JH |
2095 | } |
2096 | ||
09fd0de5 | 2097 | hci_dev_lock(hdev); |
55ed8ca1 JH |
2098 | |
2099 | hci_link_keys_clear(hdev); | |
2100 | ||
55ed8ca1 | 2101 | if (cp->debug_keys) |
238be788 | 2102 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
55ed8ca1 | 2103 | else |
a69d8927 MH |
2104 | changed = hci_dev_test_and_clear_flag(hdev, |
2105 | HCI_KEEP_DEBUG_KEYS); | |
b1de97d8 MH |
2106 | |
2107 | if (changed) | |
2108 | new_settings(hdev, NULL); | |
55ed8ca1 | 2109 | |
a492cd52 | 2110 | for (i = 0; i < key_count; i++) { |
86742e1e | 2111 | struct mgmt_link_key_info *key = &cp->keys[i]; |
55ed8ca1 | 2112 | |
58e9293c JH |
2113 | /* Always ignore debug keys and require a new pairing if |
2114 | * the user wants to use them. | |
2115 | */ | |
2116 | if (key->type == HCI_LK_DEBUG_COMBINATION) | |
2117 | continue; | |
2118 | ||
7652ff6a JH |
2119 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
2120 | key->type, key->pin_len, NULL); | |
55ed8ca1 JH |
2121 | } |
2122 | ||
2a1afb5a | 2123 | mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0); |
0e5f875a | 2124 | |
09fd0de5 | 2125 | hci_dev_unlock(hdev); |
55ed8ca1 | 2126 | |
a492cd52 | 2127 | return 0; |
55ed8ca1 JH |
2128 | } |
2129 | ||
b1078ad0 | 2130 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
04124681 | 2131 | u8 addr_type, struct sock *skip_sk) |
b1078ad0 JH |
2132 | { |
2133 | struct mgmt_ev_device_unpaired ev; | |
2134 | ||
2135 | bacpy(&ev.addr.bdaddr, bdaddr); | |
2136 | ev.addr.type = addr_type; | |
2137 | ||
2138 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), | |
04124681 | 2139 | skip_sk); |
b1078ad0 JH |
2140 | } |
2141 | ||
bdb6d971 | 2142 | static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
04124681 | 2143 | u16 len) |
55ed8ca1 | 2144 | { |
124f6e35 JH |
2145 | struct mgmt_cp_unpair_device *cp = data; |
2146 | struct mgmt_rp_unpair_device rp; | |
fc64361a | 2147 | struct hci_conn_params *params; |
3b0602cd | 2148 | struct mgmt_pending_cmd *cmd; |
55ed8ca1 | 2149 | struct hci_conn *conn; |
ec182f03 | 2150 | u8 addr_type; |
55ed8ca1 JH |
2151 | int err; |
2152 | ||
a8a1d19e | 2153 | memset(&rp, 0, sizeof(rp)); |
124f6e35 JH |
2154 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
2155 | rp.addr.type = cp->addr.type; | |
a8a1d19e | 2156 | |
4ee71b20 | 2157 | if (!bdaddr_type_is_valid(cp->addr.type)) |
2a1afb5a JH |
2158 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
2159 | MGMT_STATUS_INVALID_PARAMS, | |
2160 | &rp, sizeof(rp)); | |
4ee71b20 | 2161 | |
118da70b | 2162 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
2a1afb5a JH |
2163 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
2164 | MGMT_STATUS_INVALID_PARAMS, | |
2165 | &rp, sizeof(rp)); | |
118da70b | 2166 | |
4ee71b20 JH |
2167 | hci_dev_lock(hdev); |
2168 | ||
86a8cfc6 | 2169 | if (!hdev_is_powered(hdev)) { |
2a1afb5a JH |
2170 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
2171 | MGMT_STATUS_NOT_POWERED, &rp, | |
2172 | sizeof(rp)); | |
86a8cfc6 JH |
2173 | goto unlock; |
2174 | } | |
2175 | ||
e0b2b27e | 2176 | if (cp->addr.type == BDADDR_BREDR) { |
89cbb063 AA |
2177 | /* If disconnection is requested, then look up the |
2178 | * connection. If the remote device is connected, it | |
2179 | * will be later used to terminate the link. | |
2180 | * | |
2181 | * Setting it to NULL explicitly will cause no | |
2182 | * termination of the link. | |
2183 | */ | |
2184 | if (cp->disconnect) | |
2185 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, | |
2186 | &cp->addr.bdaddr); | |
2187 | else | |
2188 | conn = NULL; | |
2189 | ||
124f6e35 | 2190 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
ec182f03 JH |
2191 | if (err < 0) { |
2192 | err = mgmt_cmd_complete(sk, hdev->id, | |
2193 | MGMT_OP_UNPAIR_DEVICE, | |
2194 | MGMT_STATUS_NOT_PAIRED, &rp, | |
2195 | sizeof(rp)); | |
2196 | goto unlock; | |
89cbb063 AA |
2197 | } |
2198 | ||
ec182f03 | 2199 | goto done; |
e0b2b27e | 2200 | } |
b0dbfb46 | 2201 | |
ec182f03 JH |
2202 | /* LE address type */ |
2203 | addr_type = le_addr_type(cp->addr.type); | |
2204 | ||
2205 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); | |
2206 | ||
2207 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); | |
55ed8ca1 | 2208 | if (err < 0) { |
2a1afb5a JH |
2209 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
2210 | MGMT_STATUS_NOT_PAIRED, &rp, | |
2211 | sizeof(rp)); | |
55ed8ca1 JH |
2212 | goto unlock; |
2213 | } | |
2214 | ||
ec182f03 JH |
2215 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type); |
2216 | if (!conn) { | |
2217 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); | |
2218 | goto done; | |
2219 | } | |
2220 | ||
c81d555a JH |
2221 | /* Abort any ongoing SMP pairing */ |
2222 | smp_cancel_pairing(conn); | |
2223 | ||
ec182f03 JH |
2224 | /* Defer clearing up the connection parameters until closing to |
2225 | * give a chance of keeping them if a repairing happens. | |
2226 | */ | |
2227 | set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); | |
2228 | ||
fc64361a JH |
2229 | /* Disable auto-connection parameters if present */ |
2230 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type); | |
2231 | if (params) { | |
2232 | if (params->explicit_connect) | |
2233 | params->auto_connect = HCI_AUTO_CONN_EXPLICIT; | |
2234 | else | |
2235 | params->auto_connect = HCI_AUTO_CONN_DISABLED; | |
2236 | } | |
2237 | ||
ec182f03 JH |
2238 | /* If disconnection is not requested, then clear the connection |
2239 | * variable so that the link is not terminated. | |
2240 | */ | |
2241 | if (!cp->disconnect) | |
2242 | conn = NULL; | |
2243 | ||
2244 | done: | |
89cbb063 AA |
2245 | /* If the connection variable is set, then termination of the |
2246 | * link is requested. | |
2247 | */ | |
a8a1d19e | 2248 | if (!conn) { |
2a1afb5a JH |
2249 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
2250 | &rp, sizeof(rp)); | |
b1078ad0 | 2251 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
a8a1d19e JH |
2252 | goto unlock; |
2253 | } | |
55ed8ca1 | 2254 | |
124f6e35 | 2255 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
04124681 | 2256 | sizeof(*cp)); |
a8a1d19e JH |
2257 | if (!cmd) { |
2258 | err = -ENOMEM; | |
2259 | goto unlock; | |
55ed8ca1 JH |
2260 | } |
2261 | ||
d8b7b1e4 JH |
2262 | cmd->cmd_complete = addr_cmd_complete; |
2263 | ||
89e0ccc8 | 2264 | err = hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
a8a1d19e JH |
2265 | if (err < 0) |
2266 | mgmt_pending_remove(cmd); | |
2267 | ||
55ed8ca1 | 2268 | unlock: |
09fd0de5 | 2269 | hci_dev_unlock(hdev); |
55ed8ca1 JH |
2270 | return err; |
2271 | } | |
2272 | ||
bdb6d971 | 2273 | static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data, |
04124681 | 2274 | u16 len) |
8962ee74 | 2275 | { |
650f726d | 2276 | struct mgmt_cp_disconnect *cp = data; |
06a63b19 | 2277 | struct mgmt_rp_disconnect rp; |
3b0602cd | 2278 | struct mgmt_pending_cmd *cmd; |
8962ee74 | 2279 | struct hci_conn *conn; |
8962ee74 JH |
2280 | int err; |
2281 | ||
2282 | BT_DBG(""); | |
2283 | ||
06a63b19 JH |
2284 | memset(&rp, 0, sizeof(rp)); |
2285 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); | |
2286 | rp.addr.type = cp->addr.type; | |
2287 | ||
4ee71b20 | 2288 | if (!bdaddr_type_is_valid(cp->addr.type)) |
2a1afb5a JH |
2289 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
2290 | MGMT_STATUS_INVALID_PARAMS, | |
2291 | &rp, sizeof(rp)); | |
4ee71b20 | 2292 | |
09fd0de5 | 2293 | hci_dev_lock(hdev); |
8962ee74 JH |
2294 | |
2295 | if (!test_bit(HCI_UP, &hdev->flags)) { | |
2a1afb5a JH |
2296 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
2297 | MGMT_STATUS_NOT_POWERED, &rp, | |
2298 | sizeof(rp)); | |
8962ee74 JH |
2299 | goto failed; |
2300 | } | |
2301 | ||
333ae95d | 2302 | if (pending_find(MGMT_OP_DISCONNECT, hdev)) { |
2a1afb5a JH |
2303 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
2304 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); | |
8962ee74 JH |
2305 | goto failed; |
2306 | } | |
2307 | ||
591f47f3 | 2308 | if (cp->addr.type == BDADDR_BREDR) |
8fc9ced3 GP |
2309 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
2310 | &cp->addr.bdaddr); | |
88c3df13 | 2311 | else |
9d4c1cc1 JH |
2312 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, |
2313 | le_addr_type(cp->addr.type)); | |
365227e5 | 2314 | |
f960727e | 2315 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
2a1afb5a JH |
2316 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
2317 | MGMT_STATUS_NOT_CONNECTED, &rp, | |
2318 | sizeof(rp)); | |
8962ee74 JH |
2319 | goto failed; |
2320 | } | |
2321 | ||
2e58ef3e | 2322 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
366a0336 JH |
2323 | if (!cmd) { |
2324 | err = -ENOMEM; | |
8962ee74 | 2325 | goto failed; |
366a0336 | 2326 | } |
8962ee74 | 2327 | |
f5818c22 JH |
2328 | cmd->cmd_complete = generic_cmd_complete; |
2329 | ||
e3f2f92a | 2330 | err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM); |
8962ee74 | 2331 | if (err < 0) |
a664b5bc | 2332 | mgmt_pending_remove(cmd); |
8962ee74 JH |
2333 | |
2334 | failed: | |
09fd0de5 | 2335 | hci_dev_unlock(hdev); |
8962ee74 JH |
2336 | return err; |
2337 | } | |
2338 | ||
57c1477c | 2339 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
4c659c39 JH |
2340 | { |
2341 | switch (link_type) { | |
2342 | case LE_LINK: | |
48264f06 JH |
2343 | switch (addr_type) { |
2344 | case ADDR_LE_DEV_PUBLIC: | |
591f47f3 | 2345 | return BDADDR_LE_PUBLIC; |
0ed09148 | 2346 | |
48264f06 | 2347 | default: |
0ed09148 | 2348 | /* Fallback to LE Random address type */ |
591f47f3 | 2349 | return BDADDR_LE_RANDOM; |
48264f06 | 2350 | } |
0ed09148 | 2351 | |
4c659c39 | 2352 | default: |
0ed09148 | 2353 | /* Fallback to BR/EDR type */ |
591f47f3 | 2354 | return BDADDR_BREDR; |
4c659c39 JH |
2355 | } |
2356 | } | |
2357 | ||
04124681 GP |
2358 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
2359 | u16 data_len) | |
2784eb41 | 2360 | { |
2784eb41 | 2361 | struct mgmt_rp_get_connections *rp; |
8035ded4 | 2362 | struct hci_conn *c; |
a38528f1 | 2363 | size_t rp_len; |
60fc5fb6 JH |
2364 | int err; |
2365 | u16 i; | |
2784eb41 JH |
2366 | |
2367 | BT_DBG(""); | |
2368 | ||
09fd0de5 | 2369 | hci_dev_lock(hdev); |
2784eb41 | 2370 | |
5f97c1df | 2371 | if (!hdev_is_powered(hdev)) { |
a69e8375 JH |
2372 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
2373 | MGMT_STATUS_NOT_POWERED); | |
5f97c1df JH |
2374 | goto unlock; |
2375 | } | |
2376 | ||
60fc5fb6 | 2377 | i = 0; |
b644ba33 JH |
2378 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
2379 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) | |
60fc5fb6 | 2380 | i++; |
2784eb41 JH |
2381 | } |
2382 | ||
60fc5fb6 | 2383 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
92c4c204 | 2384 | rp = kmalloc(rp_len, GFP_KERNEL); |
a38528f1 | 2385 | if (!rp) { |
2784eb41 JH |
2386 | err = -ENOMEM; |
2387 | goto unlock; | |
2388 | } | |
2389 | ||
2784eb41 | 2390 | i = 0; |
4c659c39 | 2391 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
b644ba33 JH |
2392 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
2393 | continue; | |
4c659c39 | 2394 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
57c1477c | 2395 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
0ed09148 | 2396 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
4c659c39 JH |
2397 | continue; |
2398 | i++; | |
2399 | } | |
2400 | ||
eb55ef07 | 2401 | rp->conn_count = cpu_to_le16(i); |
60fc5fb6 | 2402 | |
4c659c39 JH |
2403 | /* Recalculate length in case of filtered SCO connections, etc */ |
2404 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); | |
2784eb41 | 2405 | |
2a1afb5a JH |
2406 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp, |
2407 | rp_len); | |
2784eb41 | 2408 | |
a38528f1 | 2409 | kfree(rp); |
5f97c1df JH |
2410 | |
2411 | unlock: | |
09fd0de5 | 2412 | hci_dev_unlock(hdev); |
2784eb41 JH |
2413 | return err; |
2414 | } | |
2415 | ||
bdb6d971 | 2416 | static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
04124681 | 2417 | struct mgmt_cp_pin_code_neg_reply *cp) |
96d97a67 | 2418 | { |
3b0602cd | 2419 | struct mgmt_pending_cmd *cmd; |
96d97a67 WR |
2420 | int err; |
2421 | ||
2e58ef3e | 2422 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp, |
04124681 | 2423 | sizeof(*cp)); |
96d97a67 WR |
2424 | if (!cmd) |
2425 | return -ENOMEM; | |
2426 | ||
d8457698 | 2427 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
04124681 | 2428 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
96d97a67 WR |
2429 | if (err < 0) |
2430 | mgmt_pending_remove(cmd); | |
2431 | ||
2432 | return err; | |
2433 | } | |
2434 | ||
bdb6d971 | 2435 | static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
04124681 | 2436 | u16 len) |
980e1a53 | 2437 | { |
96d97a67 | 2438 | struct hci_conn *conn; |
650f726d | 2439 | struct mgmt_cp_pin_code_reply *cp = data; |
980e1a53 | 2440 | struct hci_cp_pin_code_reply reply; |
3b0602cd | 2441 | struct mgmt_pending_cmd *cmd; |
980e1a53 JH |
2442 | int err; |
2443 | ||
2444 | BT_DBG(""); | |
2445 | ||
09fd0de5 | 2446 | hci_dev_lock(hdev); |
980e1a53 | 2447 | |
4b34ee78 | 2448 | if (!hdev_is_powered(hdev)) { |
a69e8375 JH |
2449 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
2450 | MGMT_STATUS_NOT_POWERED); | |
980e1a53 JH |
2451 | goto failed; |
2452 | } | |
2453 | ||
d8457698 | 2454 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
96d97a67 | 2455 | if (!conn) { |
a69e8375 JH |
2456 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
2457 | MGMT_STATUS_NOT_CONNECTED); | |
96d97a67 WR |
2458 | goto failed; |
2459 | } | |
2460 | ||
2461 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { | |
d8457698 JH |
2462 | struct mgmt_cp_pin_code_neg_reply ncp; |
2463 | ||
2464 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); | |
96d97a67 WR |
2465 | |
2466 | BT_ERR("PIN code is not 16 bytes long"); | |
2467 | ||
bdb6d971 | 2468 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
96d97a67 | 2469 | if (err >= 0) |
a69e8375 JH |
2470 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
2471 | MGMT_STATUS_INVALID_PARAMS); | |
96d97a67 WR |
2472 | |
2473 | goto failed; | |
2474 | } | |
2475 | ||
00abfe44 | 2476 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len); |
366a0336 JH |
2477 | if (!cmd) { |
2478 | err = -ENOMEM; | |
980e1a53 | 2479 | goto failed; |
366a0336 | 2480 | } |
980e1a53 | 2481 | |
7776d1d8 JH |
2482 | cmd->cmd_complete = addr_cmd_complete; |
2483 | ||
d8457698 | 2484 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
980e1a53 | 2485 | reply.pin_len = cp->pin_len; |
24718ca5 | 2486 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
980e1a53 JH |
2487 | |
2488 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); | |
2489 | if (err < 0) | |
a664b5bc | 2490 | mgmt_pending_remove(cmd); |
980e1a53 JH |
2491 | |
2492 | failed: | |
09fd0de5 | 2493 | hci_dev_unlock(hdev); |
980e1a53 JH |
2494 | return err; |
2495 | } | |
2496 | ||
04124681 GP |
2497 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
2498 | u16 len) | |
17fa4b9d | 2499 | { |
650f726d | 2500 | struct mgmt_cp_set_io_capability *cp = data; |
17fa4b9d JH |
2501 | |
2502 | BT_DBG(""); | |
2503 | ||
4ec86d4c | 2504 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
2a1afb5a JH |
2505 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
2506 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); | |
4ec86d4c | 2507 | |
09fd0de5 | 2508 | hci_dev_lock(hdev); |
17fa4b9d JH |
2509 | |
2510 | hdev->io_capability = cp->io_capability; | |
2511 | ||
2512 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, | |
8ce8e2b5 | 2513 | hdev->io_capability); |
17fa4b9d | 2514 | |
09fd0de5 | 2515 | hci_dev_unlock(hdev); |
17fa4b9d | 2516 | |
2a1afb5a JH |
2517 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, |
2518 | NULL, 0); | |
17fa4b9d JH |
2519 | } |
2520 | ||
3b0602cd | 2521 | static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn) |
e9a416b5 JH |
2522 | { |
2523 | struct hci_dev *hdev = conn->hdev; | |
3b0602cd | 2524 | struct mgmt_pending_cmd *cmd; |
e9a416b5 | 2525 | |
2e58ef3e | 2526 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
e9a416b5 JH |
2527 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
2528 | continue; | |
2529 | ||
e9a416b5 JH |
2530 | if (cmd->user_data != conn) |
2531 | continue; | |
2532 | ||
2533 | return cmd; | |
2534 | } | |
2535 | ||
2536 | return NULL; | |
2537 | } | |
2538 | ||
3b0602cd | 2539 | static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status) |
e9a416b5 JH |
2540 | { |
2541 | struct mgmt_rp_pair_device rp; | |
2542 | struct hci_conn *conn = cmd->user_data; | |
9df74653 | 2543 | int err; |
e9a416b5 | 2544 | |
61b1a7fb JH |
2545 | bacpy(&rp.addr.bdaddr, &conn->dst); |
2546 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); | |
e9a416b5 | 2547 | |
2a1afb5a JH |
2548 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, |
2549 | status, &rp, sizeof(rp)); | |
e9a416b5 JH |
2550 | |
2551 | /* So we don't get further callbacks for this connection */ | |
2552 | conn->connect_cfm_cb = NULL; | |
2553 | conn->security_cfm_cb = NULL; | |
2554 | conn->disconn_cfm_cb = NULL; | |
2555 | ||
76a68ba0 | 2556 | hci_conn_drop(conn); |
89cbb063 AA |
2557 | |
2558 | /* The device is paired so there is no need to remove | |
2559 | * its connection parameters anymore. | |
2560 | */ | |
2561 | clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); | |
15013aeb JH |
2562 | |
2563 | hci_conn_put(conn); | |
9df74653 JH |
2564 | |
2565 | return err; | |
e9a416b5 JH |
2566 | } |
2567 | ||
f4a407be JH |
2568 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
2569 | { | |
2570 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; | |
3b0602cd | 2571 | struct mgmt_pending_cmd *cmd; |
f4a407be JH |
2572 | |
2573 | cmd = find_pairing(conn); | |
a511b35b | 2574 | if (cmd) { |
04ab2749 | 2575 | cmd->cmd_complete(cmd, status); |
a511b35b JH |
2576 | mgmt_pending_remove(cmd); |
2577 | } | |
f4a407be JH |
2578 | } |
2579 | ||
e9a416b5 JH |
2580 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
2581 | { | |
3b0602cd | 2582 | struct mgmt_pending_cmd *cmd; |
e9a416b5 JH |
2583 | |
2584 | BT_DBG("status %u", status); | |
2585 | ||
2586 | cmd = find_pairing(conn); | |
a511b35b | 2587 | if (!cmd) { |
e9a416b5 | 2588 | BT_DBG("Unable to find a pending command"); |
a511b35b JH |
2589 | return; |
2590 | } | |
2591 | ||
2592 | cmd->cmd_complete(cmd, mgmt_status(status)); | |
2593 | mgmt_pending_remove(cmd); | |
e9a416b5 JH |
2594 | } |
2595 | ||
f4a407be | 2596 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
4c47d739 | 2597 | { |
3b0602cd | 2598 | struct mgmt_pending_cmd *cmd; |
4c47d739 VA |
2599 | |
2600 | BT_DBG("status %u", status); | |
2601 | ||
2602 | if (!status) | |
2603 | return; | |
2604 | ||
2605 | cmd = find_pairing(conn); | |
a511b35b | 2606 | if (!cmd) { |
4c47d739 | 2607 | BT_DBG("Unable to find a pending command"); |
a511b35b JH |
2608 | return; |
2609 | } | |
2610 | ||
2611 | cmd->cmd_complete(cmd, mgmt_status(status)); | |
2612 | mgmt_pending_remove(cmd); | |
4c47d739 VA |
2613 | } |
2614 | ||
bdb6d971 | 2615 | static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
04124681 | 2616 | u16 len) |
e9a416b5 | 2617 | { |
650f726d | 2618 | struct mgmt_cp_pair_device *cp = data; |
1425acb7 | 2619 | struct mgmt_rp_pair_device rp; |
3b0602cd | 2620 | struct mgmt_pending_cmd *cmd; |
e9a416b5 JH |
2621 | u8 sec_level, auth_type; |
2622 | struct hci_conn *conn; | |
e9a416b5 JH |
2623 | int err; |
2624 | ||
2625 | BT_DBG(""); | |
2626 | ||
f950a30e SJ |
2627 | memset(&rp, 0, sizeof(rp)); |
2628 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); | |
2629 | rp.addr.type = cp->addr.type; | |
2630 | ||
4ee71b20 | 2631 | if (!bdaddr_type_is_valid(cp->addr.type)) |
2a1afb5a JH |
2632 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
2633 | MGMT_STATUS_INVALID_PARAMS, | |
2634 | &rp, sizeof(rp)); | |
4ee71b20 | 2635 | |
4ec86d4c | 2636 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
2a1afb5a JH |
2637 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
2638 | MGMT_STATUS_INVALID_PARAMS, | |
2639 | &rp, sizeof(rp)); | |
4ec86d4c | 2640 | |
09fd0de5 | 2641 | hci_dev_lock(hdev); |
e9a416b5 | 2642 | |
5f97c1df | 2643 | if (!hdev_is_powered(hdev)) { |
2a1afb5a JH |
2644 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
2645 | MGMT_STATUS_NOT_POWERED, &rp, | |
2646 | sizeof(rp)); | |
5f97c1df JH |
2647 | goto unlock; |
2648 | } | |
2649 | ||
55e76b38 JH |
2650 | if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) { |
2651 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, | |
2652 | MGMT_STATUS_ALREADY_PAIRED, &rp, | |
2653 | sizeof(rp)); | |
2654 | goto unlock; | |
2655 | } | |
2656 | ||
c908df36 | 2657 | sec_level = BT_SECURITY_MEDIUM; |
6fd6b915 | 2658 | auth_type = HCI_AT_DEDICATED_BONDING; |
e9a416b5 | 2659 | |
6f77d8c7 | 2660 | if (cp->addr.type == BDADDR_BREDR) { |
04a6c589 AG |
2661 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
2662 | auth_type); | |
6f77d8c7 | 2663 | } else { |
85813a7e | 2664 | u8 addr_type = le_addr_type(cp->addr.type); |
5157b8a5 | 2665 | struct hci_conn_params *p; |
6f77d8c7 | 2666 | |
7c264b10 MH |
2667 | /* When pairing a new device, it is expected to remember |
2668 | * this device for future connections. Adding the connection | |
2669 | * parameter information ahead of time allows tracking | |
2670 | * of the slave preferred values and will speed up any | |
2671 | * further connection establishment. | |
2672 | * | |
2673 | * If connection parameters already exist, then they | |
2674 | * will be kept and this function does nothing. | |
2675 | */ | |
5157b8a5 JP |
2676 | p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
2677 | ||
2678 | if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT) | |
2679 | p->auto_connect = HCI_AUTO_CONN_DISABLED; | |
7c264b10 | 2680 | |
fa142220 JP |
2681 | conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, |
2682 | addr_type, sec_level, | |
0ad06aa6 | 2683 | HCI_LE_CONN_TIMEOUT); |
6f77d8c7 | 2684 | } |
7a512d01 | 2685 | |
30e76272 | 2686 | if (IS_ERR(conn)) { |
489dc48e AK |
2687 | int status; |
2688 | ||
2689 | if (PTR_ERR(conn) == -EBUSY) | |
2690 | status = MGMT_STATUS_BUSY; | |
faa81030 LR |
2691 | else if (PTR_ERR(conn) == -EOPNOTSUPP) |
2692 | status = MGMT_STATUS_NOT_SUPPORTED; | |
2693 | else if (PTR_ERR(conn) == -ECONNREFUSED) | |
2694 | status = MGMT_STATUS_REJECTED; | |
489dc48e AK |
2695 | else |
2696 | status = MGMT_STATUS_CONNECT_FAILED; | |
2697 | ||
2a1afb5a JH |
2698 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
2699 | status, &rp, sizeof(rp)); | |
e9a416b5 JH |
2700 | goto unlock; |
2701 | } | |
2702 | ||
2703 | if (conn->connect_cfm_cb) { | |
76a68ba0 | 2704 | hci_conn_drop(conn); |
2a1afb5a JH |
2705 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
2706 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); | |
e9a416b5 JH |
2707 | goto unlock; |
2708 | } | |
2709 | ||
2e58ef3e | 2710 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
e9a416b5 JH |
2711 | if (!cmd) { |
2712 | err = -ENOMEM; | |
76a68ba0 | 2713 | hci_conn_drop(conn); |
e9a416b5 JH |
2714 | goto unlock; |
2715 | } | |
2716 | ||
04ab2749 JH |
2717 | cmd->cmd_complete = pairing_complete; |
2718 | ||
7a512d01 | 2719 | /* For LE, just connecting isn't a proof that the pairing finished */ |
f4a407be | 2720 | if (cp->addr.type == BDADDR_BREDR) { |
7a512d01 | 2721 | conn->connect_cfm_cb = pairing_complete_cb; |
f4a407be JH |
2722 | conn->security_cfm_cb = pairing_complete_cb; |
2723 | conn->disconn_cfm_cb = pairing_complete_cb; | |
2724 | } else { | |
2725 | conn->connect_cfm_cb = le_pairing_complete_cb; | |
2726 | conn->security_cfm_cb = le_pairing_complete_cb; | |
2727 | conn->disconn_cfm_cb = le_pairing_complete_cb; | |
2728 | } | |
7a512d01 | 2729 | |
e9a416b5 | 2730 | conn->io_capability = cp->io_cap; |
f8aaf9b6 | 2731 | cmd->user_data = hci_conn_get(conn); |
e9a416b5 | 2732 | |
6f78fd4b | 2733 | if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) && |
a511b35b JH |
2734 | hci_conn_security(conn, sec_level, auth_type, true)) { |
2735 | cmd->cmd_complete(cmd, 0); | |
2736 | mgmt_pending_remove(cmd); | |
2737 | } | |
e9a416b5 JH |
2738 | |
2739 | err = 0; | |
2740 | ||
2741 | unlock: | |
09fd0de5 | 2742 | hci_dev_unlock(hdev); |
e9a416b5 JH |
2743 | return err; |
2744 | } | |
2745 | ||
04124681 GP |
2746 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
2747 | u16 len) | |
28424707 | 2748 | { |
0f4e68cf | 2749 | struct mgmt_addr_info *addr = data; |
3b0602cd | 2750 | struct mgmt_pending_cmd *cmd; |
28424707 JH |
2751 | struct hci_conn *conn; |
2752 | int err; | |
2753 | ||
2754 | BT_DBG(""); | |
2755 | ||
28424707 JH |
2756 | hci_dev_lock(hdev); |
2757 | ||
5f97c1df | 2758 | if (!hdev_is_powered(hdev)) { |
a69e8375 JH |
2759 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
2760 | MGMT_STATUS_NOT_POWERED); | |
5f97c1df JH |
2761 | goto unlock; |
2762 | } | |
2763 | ||
333ae95d | 2764 | cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
28424707 | 2765 | if (!cmd) { |
a69e8375 JH |
2766 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
2767 | MGMT_STATUS_INVALID_PARAMS); | |
28424707 JH |
2768 | goto unlock; |
2769 | } | |
2770 | ||
2771 | conn = cmd->user_data; | |
2772 | ||
2773 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { | |
a69e8375 JH |
2774 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
2775 | MGMT_STATUS_INVALID_PARAMS); | |
28424707 JH |
2776 | goto unlock; |
2777 | } | |
2778 | ||
a511b35b JH |
2779 | cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED); |
2780 | mgmt_pending_remove(cmd); | |
28424707 | 2781 | |
2a1afb5a JH |
2782 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, |
2783 | addr, sizeof(*addr)); | |
28424707 JH |
2784 | unlock: |
2785 | hci_dev_unlock(hdev); | |
28424707 JH |
2786 | return err; |
2787 | } | |
2788 | ||
bdb6d971 | 2789 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
1707c60e | 2790 | struct mgmt_addr_info *addr, u16 mgmt_op, |
04124681 | 2791 | u16 hci_op, __le32 passkey) |
a5c29683 | 2792 | { |
3b0602cd | 2793 | struct mgmt_pending_cmd *cmd; |
0df4c185 | 2794 | struct hci_conn *conn; |
a5c29683 JH |
2795 | int err; |
2796 | ||
09fd0de5 | 2797 | hci_dev_lock(hdev); |
08ba5382 | 2798 | |
4b34ee78 | 2799 | if (!hdev_is_powered(hdev)) { |
2a1afb5a JH |
2800 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
2801 | MGMT_STATUS_NOT_POWERED, addr, | |
2802 | sizeof(*addr)); | |
0df4c185 | 2803 | goto done; |
a5c29683 JH |
2804 | } |
2805 | ||
1707c60e JH |
2806 | if (addr->type == BDADDR_BREDR) |
2807 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); | |
272d90df | 2808 | else |
9d4c1cc1 JH |
2809 | conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr, |
2810 | le_addr_type(addr->type)); | |
272d90df JH |
2811 | |
2812 | if (!conn) { | |
2a1afb5a JH |
2813 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
2814 | MGMT_STATUS_NOT_CONNECTED, addr, | |
2815 | sizeof(*addr)); | |
272d90df JH |
2816 | goto done; |
2817 | } | |
47c15e2b | 2818 | |
1707c60e | 2819 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
5fe57d9e | 2820 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
5fe57d9e | 2821 | if (!err) |
2a1afb5a JH |
2822 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
2823 | MGMT_STATUS_SUCCESS, addr, | |
2824 | sizeof(*addr)); | |
5fe57d9e | 2825 | else |
2a1afb5a JH |
2826 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
2827 | MGMT_STATUS_FAILED, addr, | |
2828 | sizeof(*addr)); | |
47c15e2b | 2829 | |
47c15e2b BG |
2830 | goto done; |
2831 | } | |
2832 | ||
1707c60e | 2833 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
a5c29683 JH |
2834 | if (!cmd) { |
2835 | err = -ENOMEM; | |
0df4c185 | 2836 | goto done; |
a5c29683 JH |
2837 | } |
2838 | ||
7776d1d8 JH |
2839 | cmd->cmd_complete = addr_cmd_complete; |
2840 | ||
0df4c185 | 2841 | /* Continue with pairing via HCI */ |
604086b7 BG |
2842 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
2843 | struct hci_cp_user_passkey_reply cp; | |
2844 | ||
1707c60e | 2845 | bacpy(&cp.bdaddr, &addr->bdaddr); |
604086b7 BG |
2846 | cp.passkey = passkey; |
2847 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); | |
2848 | } else | |
1707c60e JH |
2849 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
2850 | &addr->bdaddr); | |
604086b7 | 2851 | |
a664b5bc JH |
2852 | if (err < 0) |
2853 | mgmt_pending_remove(cmd); | |
a5c29683 | 2854 | |
0df4c185 | 2855 | done: |
09fd0de5 | 2856 | hci_dev_unlock(hdev); |
a5c29683 JH |
2857 | return err; |
2858 | } | |
2859 | ||
afeb019d JK |
2860 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
2861 | void *data, u16 len) | |
2862 | { | |
2863 | struct mgmt_cp_pin_code_neg_reply *cp = data; | |
2864 | ||
2865 | BT_DBG(""); | |
2866 | ||
1707c60e | 2867 | return user_pairing_resp(sk, hdev, &cp->addr, |
afeb019d JK |
2868 | MGMT_OP_PIN_CODE_NEG_REPLY, |
2869 | HCI_OP_PIN_CODE_NEG_REPLY, 0); | |
2870 | } | |
2871 | ||
04124681 GP |
2872 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
2873 | u16 len) | |
0df4c185 | 2874 | { |
650f726d | 2875 | struct mgmt_cp_user_confirm_reply *cp = data; |
0df4c185 BG |
2876 | |
2877 | BT_DBG(""); | |
2878 | ||
2879 | if (len != sizeof(*cp)) | |
a69e8375 JH |
2880 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
2881 | MGMT_STATUS_INVALID_PARAMS); | |
0df4c185 | 2882 | |
1707c60e | 2883 | return user_pairing_resp(sk, hdev, &cp->addr, |
04124681 GP |
2884 | MGMT_OP_USER_CONFIRM_REPLY, |
2885 | HCI_OP_USER_CONFIRM_REPLY, 0); | |
0df4c185 BG |
2886 | } |
2887 | ||
bdb6d971 | 2888 | static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev, |
04124681 | 2889 | void *data, u16 len) |
0df4c185 | 2890 | { |
c9c2659f | 2891 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
0df4c185 BG |
2892 | |
2893 | BT_DBG(""); | |
2894 | ||
1707c60e | 2895 | return user_pairing_resp(sk, hdev, &cp->addr, |
04124681 GP |
2896 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
2897 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); | |
0df4c185 BG |
2898 | } |
2899 | ||
04124681 GP |
2900 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
2901 | u16 len) | |
604086b7 | 2902 | { |
650f726d | 2903 | struct mgmt_cp_user_passkey_reply *cp = data; |
604086b7 BG |
2904 | |
2905 | BT_DBG(""); | |
2906 | ||
1707c60e | 2907 | return user_pairing_resp(sk, hdev, &cp->addr, |
04124681 GP |
2908 | MGMT_OP_USER_PASSKEY_REPLY, |
2909 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); | |
604086b7 BG |
2910 | } |
2911 | ||
bdb6d971 | 2912 | static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev, |
04124681 | 2913 | void *data, u16 len) |
604086b7 | 2914 | { |
650f726d | 2915 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
604086b7 BG |
2916 | |
2917 | BT_DBG(""); | |
2918 | ||
1707c60e | 2919 | return user_pairing_resp(sk, hdev, &cp->addr, |
04124681 GP |
2920 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
2921 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); | |
604086b7 BG |
2922 | } |
2923 | ||
1904a853 | 2924 | static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
13928971 JH |
2925 | { |
2926 | struct mgmt_cp_set_local_name *cp; | |
3b0602cd | 2927 | struct mgmt_pending_cmd *cmd; |
13928971 JH |
2928 | |
2929 | BT_DBG("status 0x%02x", status); | |
2930 | ||
2931 | hci_dev_lock(hdev); | |
2932 | ||
333ae95d | 2933 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
13928971 JH |
2934 | if (!cmd) |
2935 | goto unlock; | |
2936 | ||
2937 | cp = cmd->param; | |
2938 | ||
2939 | if (status) | |
a69e8375 JH |
2940 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
2941 | mgmt_status(status)); | |
13928971 | 2942 | else |
2a1afb5a JH |
2943 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
2944 | cp, sizeof(*cp)); | |
13928971 JH |
2945 | |
2946 | mgmt_pending_remove(cmd); | |
2947 | ||
2948 | unlock: | |
2949 | hci_dev_unlock(hdev); | |
2950 | } | |
2951 | ||
bdb6d971 | 2952 | static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data, |
04124681 | 2953 | u16 len) |
b312b161 | 2954 | { |
2b4bf397 | 2955 | struct mgmt_cp_set_local_name *cp = data; |
3b0602cd | 2956 | struct mgmt_pending_cmd *cmd; |
890ea898 | 2957 | struct hci_request req; |
b312b161 JH |
2958 | int err; |
2959 | ||
2960 | BT_DBG(""); | |
2961 | ||
09fd0de5 | 2962 | hci_dev_lock(hdev); |
b312b161 | 2963 | |
b3f2ca94 JH |
2964 | /* If the old values are the same as the new ones just return a |
2965 | * direct command complete event. | |
2966 | */ | |
2967 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && | |
2968 | !memcmp(hdev->short_name, cp->short_name, | |
2969 | sizeof(hdev->short_name))) { | |
2a1afb5a JH |
2970 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
2971 | data, len); | |
b3f2ca94 JH |
2972 | goto failed; |
2973 | } | |
2974 | ||
2b4bf397 | 2975 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
28cc7bde | 2976 | |
b5235a65 | 2977 | if (!hdev_is_powered(hdev)) { |
2b4bf397 | 2978 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
28cc7bde | 2979 | |
2a1afb5a JH |
2980 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
2981 | data, len); | |
28cc7bde JH |
2982 | if (err < 0) |
2983 | goto failed; | |
2984 | ||
f6b7712e MH |
2985 | err = mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, |
2986 | data, len, sk); | |
28cc7bde | 2987 | |
b5235a65 JH |
2988 | goto failed; |
2989 | } | |
2990 | ||
28cc7bde | 2991 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len); |
b312b161 JH |
2992 | if (!cmd) { |
2993 | err = -ENOMEM; | |
2994 | goto failed; | |
2995 | } | |
2996 | ||
13928971 JH |
2997 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
2998 | ||
890ea898 | 2999 | hci_req_init(&req, hdev); |
3f985050 JH |
3000 | |
3001 | if (lmp_bredr_capable(hdev)) { | |
00cf5040 | 3002 | __hci_req_update_name(&req); |
b1a8917c | 3003 | __hci_req_update_eir(&req); |
3f985050 JH |
3004 | } |
3005 | ||
7a5f4990 MH |
3006 | /* The name is stored in the scan response data and so |
3007 | * no need to udpate the advertising data here. | |
3008 | */ | |
3f985050 | 3009 | if (lmp_le_capable(hdev)) |
cab054ab | 3010 | __hci_req_update_scan_rsp_data(&req, hdev->cur_adv_instance); |
3f985050 | 3011 | |
13928971 | 3012 | err = hci_req_run(&req, set_name_complete); |
b312b161 JH |
3013 | if (err < 0) |
3014 | mgmt_pending_remove(cmd); | |
3015 | ||
3016 | failed: | |
09fd0de5 | 3017 | hci_dev_unlock(hdev); |
b312b161 JH |
3018 | return err; |
3019 | } | |
3020 | ||
1b9441f8 JH |
3021 | static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status, |
3022 | u16 opcode, struct sk_buff *skb) | |
3023 | { | |
3024 | struct mgmt_rp_read_local_oob_data mgmt_rp; | |
3025 | size_t rp_size = sizeof(mgmt_rp); | |
3026 | struct mgmt_pending_cmd *cmd; | |
3027 | ||
3028 | BT_DBG("%s status %u", hdev->name, status); | |
3029 | ||
3030 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); | |
3031 | if (!cmd) | |
3032 | return; | |
3033 | ||
3034 | if (status || !skb) { | |
3035 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, | |
3036 | status ? mgmt_status(status) : MGMT_STATUS_FAILED); | |
3037 | goto remove; | |
3038 | } | |
3039 | ||
3040 | memset(&mgmt_rp, 0, sizeof(mgmt_rp)); | |
3041 | ||
3042 | if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { | |
3043 | struct hci_rp_read_local_oob_data *rp = (void *) skb->data; | |
3044 | ||
3045 | if (skb->len < sizeof(*rp)) { | |
3046 | mgmt_cmd_status(cmd->sk, hdev->id, | |
3047 | MGMT_OP_READ_LOCAL_OOB_DATA, | |
3048 | MGMT_STATUS_FAILED); | |
3049 | goto remove; | |
3050 | } | |
3051 | ||
3052 | memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash)); | |
3053 | memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand)); | |
3054 | ||
3055 | rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256); | |
3056 | } else { | |
3057 | struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data; | |
3058 | ||
3059 | if (skb->len < sizeof(*rp)) { | |
3060 | mgmt_cmd_status(cmd->sk, hdev->id, | |
3061 | MGMT_OP_READ_LOCAL_OOB_DATA, | |
3062 | MGMT_STATUS_FAILED); | |
3063 | goto remove; | |
3064 | } | |
3065 | ||
3066 | memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192)); | |
3067 | memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192)); | |
3068 | ||
3069 | memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256)); | |
3070 | memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256)); | |
3071 | } | |
3072 | ||
3073 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, | |
3074 | MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size); | |
3075 | ||
3076 | remove: | |
3077 | mgmt_pending_remove(cmd); | |
3078 | } | |
3079 | ||
0f4e68cf | 3080 | static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev, |
04124681 | 3081 | void *data, u16 data_len) |
c35938b2 | 3082 | { |
3b0602cd | 3083 | struct mgmt_pending_cmd *cmd; |
1b9441f8 | 3084 | struct hci_request req; |
c35938b2 SJ |
3085 | int err; |
3086 | ||
bdb6d971 | 3087 | BT_DBG("%s", hdev->name); |
c35938b2 | 3088 | |
09fd0de5 | 3089 | hci_dev_lock(hdev); |
c35938b2 | 3090 | |
4b34ee78 | 3091 | if (!hdev_is_powered(hdev)) { |
a69e8375 JH |
3092 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
3093 | MGMT_STATUS_NOT_POWERED); | |
c35938b2 SJ |
3094 | goto unlock; |
3095 | } | |
3096 | ||
9a1a1996 | 3097 | if (!lmp_ssp_capable(hdev)) { |
a69e8375 JH |
3098 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
3099 | MGMT_STATUS_NOT_SUPPORTED); | |
c35938b2 SJ |
3100 | goto unlock; |
3101 | } | |
3102 | ||
333ae95d | 3103 | if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
a69e8375 JH |
3104 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
3105 | MGMT_STATUS_BUSY); | |
c35938b2 SJ |
3106 | goto unlock; |
3107 | } | |
3108 | ||
2e58ef3e | 3109 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0); |
c35938b2 SJ |
3110 | if (!cmd) { |
3111 | err = -ENOMEM; | |
3112 | goto unlock; | |
3113 | } | |
3114 | ||
1b9441f8 JH |
3115 | hci_req_init(&req, hdev); |
3116 | ||
710f11c0 | 3117 | if (bredr_sc_enabled(hdev)) |
1b9441f8 | 3118 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
4d2d2796 | 3119 | else |
1b9441f8 | 3120 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
4d2d2796 | 3121 | |
1b9441f8 | 3122 | err = hci_req_run_skb(&req, read_local_oob_data_complete); |
c35938b2 SJ |
3123 | if (err < 0) |
3124 | mgmt_pending_remove(cmd); | |
3125 | ||
3126 | unlock: | |
09fd0de5 | 3127 | hci_dev_unlock(hdev); |
c35938b2 SJ |
3128 | return err; |
3129 | } | |
3130 | ||
bdb6d971 | 3131 | static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev, |
04124681 | 3132 | void *data, u16 len) |
2763eda6 | 3133 | { |
5d57e796 | 3134 | struct mgmt_addr_info *addr = data; |
2763eda6 SJ |
3135 | int err; |
3136 | ||
bdb6d971 | 3137 | BT_DBG("%s ", hdev->name); |
2763eda6 | 3138 | |
5d57e796 | 3139 | if (!bdaddr_type_is_valid(addr->type)) |
2a1afb5a JH |
3140 | return mgmt_cmd_complete(sk, hdev->id, |
3141 | MGMT_OP_ADD_REMOTE_OOB_DATA, | |
3142 | MGMT_STATUS_INVALID_PARAMS, | |
3143 | addr, sizeof(*addr)); | |
5d57e796 | 3144 | |
09fd0de5 | 3145 | hci_dev_lock(hdev); |
2763eda6 | 3146 | |
ec109113 MH |
3147 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
3148 | struct mgmt_cp_add_remote_oob_data *cp = data; | |
3149 | u8 status; | |
bf1e3541 | 3150 | |
c19a495c | 3151 | if (cp->addr.type != BDADDR_BREDR) { |
2a1afb5a JH |
3152 | err = mgmt_cmd_complete(sk, hdev->id, |
3153 | MGMT_OP_ADD_REMOTE_OOB_DATA, | |
3154 | MGMT_STATUS_INVALID_PARAMS, | |
3155 | &cp->addr, sizeof(cp->addr)); | |
c19a495c JH |
3156 | goto unlock; |
3157 | } | |
3158 | ||
ec109113 | 3159 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
6928a924 JH |
3160 | cp->addr.type, cp->hash, |
3161 | cp->rand, NULL, NULL); | |
ec109113 MH |
3162 | if (err < 0) |
3163 | status = MGMT_STATUS_FAILED; | |
3164 | else | |
3165 | status = MGMT_STATUS_SUCCESS; | |
3166 | ||
2a1afb5a JH |
3167 | err = mgmt_cmd_complete(sk, hdev->id, |
3168 | MGMT_OP_ADD_REMOTE_OOB_DATA, status, | |
3169 | &cp->addr, sizeof(cp->addr)); | |
ec109113 MH |
3170 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
3171 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; | |
41bcfd50 | 3172 | u8 *rand192, *hash192, *rand256, *hash256; |
ec109113 MH |
3173 | u8 status; |
3174 | ||
86df9200 | 3175 | if (bdaddr_type_is_le(cp->addr.type)) { |
d25b78e2 JH |
3176 | /* Enforce zero-valued 192-bit parameters as |
3177 | * long as legacy SMP OOB isn't implemented. | |
3178 | */ | |
3179 | if (memcmp(cp->rand192, ZERO_KEY, 16) || | |
3180 | memcmp(cp->hash192, ZERO_KEY, 16)) { | |
2a1afb5a JH |
3181 | err = mgmt_cmd_complete(sk, hdev->id, |
3182 | MGMT_OP_ADD_REMOTE_OOB_DATA, | |
3183 | MGMT_STATUS_INVALID_PARAMS, | |
3184 | addr, sizeof(*addr)); | |
d25b78e2 JH |
3185 | goto unlock; |
3186 | } | |
3187 | ||
86df9200 JH |
3188 | rand192 = NULL; |
3189 | hash192 = NULL; | |
3190 | } else { | |
41bcfd50 MH |
3191 | /* In case one of the P-192 values is set to zero, |
3192 | * then just disable OOB data for P-192. | |
3193 | */ | |
3194 | if (!memcmp(cp->rand192, ZERO_KEY, 16) || | |
3195 | !memcmp(cp->hash192, ZERO_KEY, 16)) { | |
3196 | rand192 = NULL; | |
3197 | hash192 = NULL; | |
3198 | } else { | |
3199 | rand192 = cp->rand192; | |
3200 | hash192 = cp->hash192; | |
3201 | } | |
3202 | } | |
3203 | ||
3204 | /* In case one of the P-256 values is set to zero, then just | |
3205 | * disable OOB data for P-256. | |
3206 | */ | |
3207 | if (!memcmp(cp->rand256, ZERO_KEY, 16) || | |
3208 | !memcmp(cp->hash256, ZERO_KEY, 16)) { | |
3209 | rand256 = NULL; | |
3210 | hash256 = NULL; | |
3211 | } else { | |
3212 | rand256 = cp->rand256; | |
3213 | hash256 = cp->hash256; | |
86df9200 JH |
3214 | } |
3215 | ||
81328d5c | 3216 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
86df9200 | 3217 | cp->addr.type, hash192, rand192, |
41bcfd50 | 3218 | hash256, rand256); |
ec109113 MH |
3219 | if (err < 0) |
3220 | status = MGMT_STATUS_FAILED; | |
3221 | else | |
3222 | status = MGMT_STATUS_SUCCESS; | |
3223 | ||
2a1afb5a JH |
3224 | err = mgmt_cmd_complete(sk, hdev->id, |
3225 | MGMT_OP_ADD_REMOTE_OOB_DATA, | |
3226 | status, &cp->addr, sizeof(cp->addr)); | |
ec109113 MH |
3227 | } else { |
3228 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); | |
a69e8375 JH |
3229 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
3230 | MGMT_STATUS_INVALID_PARAMS); | |
ec109113 | 3231 | } |
2763eda6 | 3232 | |
c19a495c | 3233 | unlock: |
09fd0de5 | 3234 | hci_dev_unlock(hdev); |
2763eda6 SJ |
3235 | return err; |
3236 | } | |
3237 | ||
bdb6d971 | 3238 | static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev, |
8ce8e2b5 | 3239 | void *data, u16 len) |
2763eda6 | 3240 | { |
650f726d | 3241 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
bf1e3541 | 3242 | u8 status; |
2763eda6 SJ |
3243 | int err; |
3244 | ||
bdb6d971 | 3245 | BT_DBG("%s", hdev->name); |
2763eda6 | 3246 | |
c19a495c | 3247 | if (cp->addr.type != BDADDR_BREDR) |
2a1afb5a JH |
3248 | return mgmt_cmd_complete(sk, hdev->id, |
3249 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, | |
3250 | MGMT_STATUS_INVALID_PARAMS, | |
3251 | &cp->addr, sizeof(cp->addr)); | |
c19a495c | 3252 | |
09fd0de5 | 3253 | hci_dev_lock(hdev); |
2763eda6 | 3254 | |
eedbd581 JH |
3255 | if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
3256 | hci_remote_oob_data_clear(hdev); | |
3257 | status = MGMT_STATUS_SUCCESS; | |
3258 | goto done; | |
3259 | } | |
3260 | ||
6928a924 | 3261 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type); |
2763eda6 | 3262 | if (err < 0) |
bf1e3541 | 3263 | status = MGMT_STATUS_INVALID_PARAMS; |
2763eda6 | 3264 | else |
a6785be2 | 3265 | status = MGMT_STATUS_SUCCESS; |
bf1e3541 | 3266 | |
eedbd581 | 3267 | done: |
2a1afb5a JH |
3268 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
3269 | status, &cp->addr, sizeof(cp->addr)); | |
2763eda6 | 3270 | |
09fd0de5 | 3271 | hci_dev_unlock(hdev); |
2763eda6 SJ |
3272 | return err; |
3273 | } | |
3274 | ||
e68f072b | 3275 | void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status) |
7c307720 | 3276 | { |
3b0602cd | 3277 | struct mgmt_pending_cmd *cmd; |
ae55f598 | 3278 | |
7c307720 AG |
3279 | BT_DBG("status %d", status); |
3280 | ||
11e6e25d | 3281 | hci_dev_lock(hdev); |
41dc2bd6 | 3282 | |
333ae95d | 3283 | cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev); |
66ea9427 | 3284 | if (!cmd) |
333ae95d | 3285 | cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev); |
66ea9427 | 3286 | |
78b781ca JH |
3287 | if (!cmd) |
3288 | cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev); | |
3289 | ||
11e6e25d | 3290 | if (cmd) { |
2922a94f | 3291 | cmd->cmd_complete(cmd, mgmt_status(status)); |
11e6e25d MH |
3292 | mgmt_pending_remove(cmd); |
3293 | } | |
7c307720 | 3294 | |
11e6e25d | 3295 | hci_dev_unlock(hdev); |
7c307720 AG |
3296 | } |
3297 | ||
591752af JH |
3298 | static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type, |
3299 | uint8_t *mgmt_status) | |
3300 | { | |
3301 | switch (type) { | |
3302 | case DISCOV_TYPE_LE: | |
3303 | *mgmt_status = mgmt_le_support(hdev); | |
3304 | if (*mgmt_status) | |
3305 | return false; | |
3306 | break; | |
3307 | case DISCOV_TYPE_INTERLEAVED: | |
3308 | *mgmt_status = mgmt_le_support(hdev); | |
3309 | if (*mgmt_status) | |
3310 | return false; | |
3311 | /* Intentional fall-through */ | |
3312 | case DISCOV_TYPE_BREDR: | |
3313 | *mgmt_status = mgmt_bredr_support(hdev); | |
3314 | if (*mgmt_status) | |
3315 | return false; | |
3316 | break; | |
3317 | default: | |
3318 | *mgmt_status = MGMT_STATUS_INVALID_PARAMS; | |
3319 | return false; | |
3320 | } | |
3321 | ||
3322 | return true; | |
3323 | } | |
3324 | ||
78b781ca JH |
3325 | static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev, |
3326 | u16 op, void *data, u16 len) | |
14a53664 | 3327 | { |
650f726d | 3328 | struct mgmt_cp_start_discovery *cp = data; |
3b0602cd | 3329 | struct mgmt_pending_cmd *cmd; |
8019044d | 3330 | u8 status; |
14a53664 JH |
3331 | int err; |
3332 | ||
bdb6d971 | 3333 | BT_DBG("%s", hdev->name); |
14a53664 | 3334 | |
09fd0de5 | 3335 | hci_dev_lock(hdev); |
14a53664 | 3336 | |
4b34ee78 | 3337 | if (!hdev_is_powered(hdev)) { |
78b781ca | 3338 | err = mgmt_cmd_complete(sk, hdev->id, op, |
2a1afb5a JH |
3339 | MGMT_STATUS_NOT_POWERED, |
3340 | &cp->type, sizeof(cp->type)); | |
bd2d1334 JH |
3341 | goto failed; |
3342 | } | |
3343 | ||
f5a969f2 | 3344 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
d7a5a11d | 3345 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
78b781ca JH |
3346 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
3347 | &cp->type, sizeof(cp->type)); | |
642be6c7 AG |
3348 | goto failed; |
3349 | } | |
3350 | ||
591752af | 3351 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
78b781ca JH |
3352 | err = mgmt_cmd_complete(sk, hdev->id, op, status, |
3353 | &cp->type, sizeof(cp->type)); | |
591752af JH |
3354 | goto failed; |
3355 | } | |
3356 | ||
22078800 MH |
3357 | /* Clear the discovery filter first to free any previously |
3358 | * allocated memory for the UUID list. | |
3359 | */ | |
3360 | hci_discovery_filter_clear(hdev); | |
3361 | ||
4aab14e5 | 3362 | hdev->discovery.type = cp->type; |
da25cf6a | 3363 | hdev->discovery.report_invalid_rssi = false; |
78b781ca JH |
3364 | if (op == MGMT_OP_START_LIMITED_DISCOVERY) |
3365 | hdev->discovery.limited = true; | |
3366 | else | |
3367 | hdev->discovery.limited = false; | |
4aab14e5 | 3368 | |
78b781ca | 3369 | cmd = mgmt_pending_add(sk, op, hdev, data, len); |
e68f072b JH |
3370 | if (!cmd) { |
3371 | err = -ENOMEM; | |
04106755 | 3372 | goto failed; |
f39799f5 | 3373 | } |
04106755 | 3374 | |
e68f072b | 3375 | cmd->cmd_complete = generic_cmd_complete; |
7c307720 | 3376 | |
f5a969f2 | 3377 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
e68f072b JH |
3378 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
3379 | err = 0; | |
7c307720 | 3380 | |
14a53664 | 3381 | failed: |
09fd0de5 | 3382 | hci_dev_unlock(hdev); |
14a53664 JH |
3383 | return err; |
3384 | } | |
f39799f5 | 3385 | |
78b781ca JH |
3386 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
3387 | void *data, u16 len) | |
3388 | { | |
3389 | return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY, | |
3390 | data, len); | |
3391 | } | |
3392 | ||
3393 | static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev, | |
3394 | void *data, u16 len) | |
3395 | { | |
3396 | return start_discovery_internal(sk, hdev, | |
3397 | MGMT_OP_START_LIMITED_DISCOVERY, | |
3398 | data, len); | |
3399 | } | |
3400 | ||
3b0602cd JH |
3401 | static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd, |
3402 | u8 status) | |
2922a94f | 3403 | { |
2a1afb5a JH |
3404 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
3405 | cmd->param, 1); | |
2922a94f | 3406 | } |
04106755 | 3407 | |
66ea9427 JP |
3408 | static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, |
3409 | void *data, u16 len) | |
3410 | { | |
3411 | struct mgmt_cp_start_service_discovery *cp = data; | |
3b0602cd | 3412 | struct mgmt_pending_cmd *cmd; |
66ea9427 JP |
3413 | const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16); |
3414 | u16 uuid_count, expected_len; | |
3415 | u8 status; | |
3416 | int err; | |
04106755 | 3417 | |
66ea9427 | 3418 | BT_DBG("%s", hdev->name); |
e8bb6b97 | 3419 | |
66ea9427 | 3420 | hci_dev_lock(hdev); |
7c307720 | 3421 | |
66ea9427 | 3422 | if (!hdev_is_powered(hdev)) { |
2a1afb5a JH |
3423 | err = mgmt_cmd_complete(sk, hdev->id, |
3424 | MGMT_OP_START_SERVICE_DISCOVERY, | |
3425 | MGMT_STATUS_NOT_POWERED, | |
3426 | &cp->type, sizeof(cp->type)); | |
66ea9427 JP |
3427 | goto failed; |
3428 | } | |
7c307720 | 3429 | |
66ea9427 | 3430 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
d7a5a11d | 3431 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
2a1afb5a JH |
3432 | err = mgmt_cmd_complete(sk, hdev->id, |
3433 | MGMT_OP_START_SERVICE_DISCOVERY, | |
3434 | MGMT_STATUS_BUSY, &cp->type, | |
3435 | sizeof(cp->type)); | |
66ea9427 JP |
3436 | goto failed; |
3437 | } | |
d9483943 | 3438 | |
66ea9427 JP |
3439 | uuid_count = __le16_to_cpu(cp->uuid_count); |
3440 | if (uuid_count > max_uuid_count) { | |
3441 | BT_ERR("service_discovery: too big uuid_count value %u", | |
3442 | uuid_count); | |
2a1afb5a JH |
3443 | err = mgmt_cmd_complete(sk, hdev->id, |
3444 | MGMT_OP_START_SERVICE_DISCOVERY, | |
3445 | MGMT_STATUS_INVALID_PARAMS, &cp->type, | |
3446 | sizeof(cp->type)); | |
66ea9427 JP |
3447 | goto failed; |
3448 | } | |
3449 | ||
3450 | expected_len = sizeof(*cp) + uuid_count * 16; | |
3451 | if (expected_len != len) { | |
3452 | BT_ERR("service_discovery: expected %u bytes, got %u bytes", | |
3453 | expected_len, len); | |
2a1afb5a JH |
3454 | err = mgmt_cmd_complete(sk, hdev->id, |
3455 | MGMT_OP_START_SERVICE_DISCOVERY, | |
3456 | MGMT_STATUS_INVALID_PARAMS, &cp->type, | |
3457 | sizeof(cp->type)); | |
66ea9427 JP |
3458 | goto failed; |
3459 | } | |
3460 | ||
591752af JH |
3461 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
3462 | err = mgmt_cmd_complete(sk, hdev->id, | |
3463 | MGMT_OP_START_SERVICE_DISCOVERY, | |
3464 | status, &cp->type, sizeof(cp->type)); | |
3465 | goto failed; | |
3466 | } | |
3467 | ||
66ea9427 | 3468 | cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY, |
2922a94f | 3469 | hdev, data, len); |
66ea9427 JP |
3470 | if (!cmd) { |
3471 | err = -ENOMEM; | |
3472 | goto failed; | |
3473 | } | |
3474 | ||
2922a94f JH |
3475 | cmd->cmd_complete = service_discovery_cmd_complete; |
3476 | ||
22078800 MH |
3477 | /* Clear the discovery filter first to free any previously |
3478 | * allocated memory for the UUID list. | |
3479 | */ | |
3480 | hci_discovery_filter_clear(hdev); | |
3481 | ||
82f8b651 | 3482 | hdev->discovery.result_filtering = true; |
66ea9427 JP |
3483 | hdev->discovery.type = cp->type; |
3484 | hdev->discovery.rssi = cp->rssi; | |
3485 | hdev->discovery.uuid_count = uuid_count; | |
3486 | ||
3487 | if (uuid_count > 0) { | |
3488 | hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16, | |
3489 | GFP_KERNEL); | |
3490 | if (!hdev->discovery.uuids) { | |
2a1afb5a JH |
3491 | err = mgmt_cmd_complete(sk, hdev->id, |
3492 | MGMT_OP_START_SERVICE_DISCOVERY, | |
3493 | MGMT_STATUS_FAILED, | |
3494 | &cp->type, sizeof(cp->type)); | |
d9483943 JH |
3495 | mgmt_pending_remove(cmd); |
3496 | goto failed; | |
3497 | } | |
66ea9427 | 3498 | } |
d9483943 | 3499 | |
66ea9427 | 3500 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
e68f072b JH |
3501 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
3502 | err = 0; | |
14a53664 JH |
3503 | |
3504 | failed: | |
09fd0de5 | 3505 | hci_dev_unlock(hdev); |
14a53664 JH |
3506 | return err; |
3507 | } | |
3508 | ||
2154d3f4 | 3509 | void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status) |
1183fdca | 3510 | { |
3b0602cd | 3511 | struct mgmt_pending_cmd *cmd; |
1183fdca | 3512 | |
0e05bba6 AG |
3513 | BT_DBG("status %d", status); |
3514 | ||
3515 | hci_dev_lock(hdev); | |
3516 | ||
333ae95d | 3517 | cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
11e6e25d | 3518 | if (cmd) { |
2922a94f | 3519 | cmd->cmd_complete(cmd, mgmt_status(status)); |
11e6e25d | 3520 | mgmt_pending_remove(cmd); |
0e05bba6 AG |
3521 | } |
3522 | ||
0e05bba6 AG |
3523 | hci_dev_unlock(hdev); |
3524 | } | |
3525 | ||
bdb6d971 | 3526 | static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data, |
04124681 | 3527 | u16 len) |
14a53664 | 3528 | { |
d930650b | 3529 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
3b0602cd | 3530 | struct mgmt_pending_cmd *cmd; |
14a53664 JH |
3531 | int err; |
3532 | ||
bdb6d971 | 3533 | BT_DBG("%s", hdev->name); |
14a53664 | 3534 | |
09fd0de5 | 3535 | hci_dev_lock(hdev); |
14a53664 | 3536 | |
30dc78e1 | 3537 | if (!hci_discovery_active(hdev)) { |
2a1afb5a JH |
3538 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
3539 | MGMT_STATUS_REJECTED, &mgmt_cp->type, | |
3540 | sizeof(mgmt_cp->type)); | |
d930650b JH |
3541 | goto unlock; |
3542 | } | |
3543 | ||
3544 | if (hdev->discovery.type != mgmt_cp->type) { | |
2a1afb5a JH |
3545 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
3546 | MGMT_STATUS_INVALID_PARAMS, | |
3547 | &mgmt_cp->type, sizeof(mgmt_cp->type)); | |
30dc78e1 | 3548 | goto unlock; |
ff9ef578 JH |
3549 | } |
3550 | ||
2922a94f | 3551 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len); |
14a53664 JH |
3552 | if (!cmd) { |
3553 | err = -ENOMEM; | |
30dc78e1 JH |
3554 | goto unlock; |
3555 | } | |
3556 | ||
2922a94f JH |
3557 | cmd->cmd_complete = generic_cmd_complete; |
3558 | ||
2154d3f4 JH |
3559 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
3560 | queue_work(hdev->req_workqueue, &hdev->discov_update); | |
3561 | err = 0; | |
14a53664 | 3562 | |
30dc78e1 | 3563 | unlock: |
09fd0de5 | 3564 | hci_dev_unlock(hdev); |
14a53664 JH |
3565 | return err; |
3566 | } | |
3567 | ||
bdb6d971 | 3568 | static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data, |
04124681 | 3569 | u16 len) |
561aafbc | 3570 | { |
650f726d | 3571 | struct mgmt_cp_confirm_name *cp = data; |
561aafbc | 3572 | struct inquiry_entry *e; |
561aafbc JH |
3573 | int err; |
3574 | ||
bdb6d971 | 3575 | BT_DBG("%s", hdev->name); |
561aafbc | 3576 | |
561aafbc JH |
3577 | hci_dev_lock(hdev); |
3578 | ||
30dc78e1 | 3579 | if (!hci_discovery_active(hdev)) { |
2a1afb5a JH |
3580 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
3581 | MGMT_STATUS_FAILED, &cp->addr, | |
3582 | sizeof(cp->addr)); | |
30dc78e1 JH |
3583 | goto failed; |
3584 | } | |
3585 | ||
a198e7b1 | 3586 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
561aafbc | 3587 | if (!e) { |
2a1afb5a JH |
3588 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
3589 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, | |
3590 | sizeof(cp->addr)); | |
561aafbc JH |
3591 | goto failed; |
3592 | } | |
3593 | ||
3594 | if (cp->name_known) { | |
3595 | e->name_state = NAME_KNOWN; | |
3596 | list_del(&e->list); | |
3597 | } else { | |
3598 | e->name_state = NAME_NEEDED; | |
a3d4e20a | 3599 | hci_inquiry_cache_update_resolve(hdev, e); |
561aafbc JH |
3600 | } |
3601 | ||
2a1afb5a JH |
3602 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, |
3603 | &cp->addr, sizeof(cp->addr)); | |
561aafbc JH |
3604 | |
3605 | failed: | |
3606 | hci_dev_unlock(hdev); | |
561aafbc JH |
3607 | return err; |
3608 | } | |
3609 | ||
bdb6d971 | 3610 | static int block_device(struct sock *sk, struct hci_dev *hdev, void *data, |
04124681 | 3611 | u16 len) |
7fbec224 | 3612 | { |
650f726d | 3613 | struct mgmt_cp_block_device *cp = data; |
f0eeea8b | 3614 | u8 status; |
7fbec224 AJ |
3615 | int err; |
3616 | ||
bdb6d971 | 3617 | BT_DBG("%s", hdev->name); |
7fbec224 | 3618 | |
4ee71b20 | 3619 | if (!bdaddr_type_is_valid(cp->addr.type)) |
2a1afb5a JH |
3620 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
3621 | MGMT_STATUS_INVALID_PARAMS, | |
3622 | &cp->addr, sizeof(cp->addr)); | |
4ee71b20 | 3623 | |
09fd0de5 | 3624 | hci_dev_lock(hdev); |
5e762444 | 3625 | |
dcc36c16 JH |
3626 | err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr, |
3627 | cp->addr.type); | |
2a8357f2 | 3628 | if (err < 0) { |
f0eeea8b | 3629 | status = MGMT_STATUS_FAILED; |
2a8357f2 JH |
3630 | goto done; |
3631 | } | |
3632 | ||
3633 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), | |
3634 | sk); | |
3635 | status = MGMT_STATUS_SUCCESS; | |
f0eeea8b | 3636 | |
2a8357f2 | 3637 | done: |
2a1afb5a JH |
3638 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
3639 | &cp->addr, sizeof(cp->addr)); | |
5e762444 | 3640 | |
09fd0de5 | 3641 | hci_dev_unlock(hdev); |
7fbec224 AJ |
3642 | |
3643 | return err; | |
3644 | } | |
3645 | ||
bdb6d971 | 3646 | static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data, |
04124681 | 3647 | u16 len) |
7fbec224 | 3648 | { |
650f726d | 3649 | struct mgmt_cp_unblock_device *cp = data; |
f0eeea8b | 3650 | u8 status; |
7fbec224 AJ |
3651 | int err; |
3652 | ||
bdb6d971 | 3653 | BT_DBG("%s", hdev->name); |
7fbec224 | 3654 | |
4ee71b20 | 3655 | if (!bdaddr_type_is_valid(cp->addr.type)) |
2a1afb5a JH |
3656 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
3657 | MGMT_STATUS_INVALID_PARAMS, | |
3658 | &cp->addr, sizeof(cp->addr)); | |
4ee71b20 | 3659 | |
09fd0de5 | 3660 | hci_dev_lock(hdev); |
5e762444 | 3661 | |
dcc36c16 JH |
3662 | err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr, |
3663 | cp->addr.type); | |
2a8357f2 | 3664 | if (err < 0) { |
f0eeea8b | 3665 | status = MGMT_STATUS_INVALID_PARAMS; |
2a8357f2 JH |
3666 | goto done; |
3667 | } | |
3668 | ||
3669 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), | |
3670 | sk); | |
3671 | status = MGMT_STATUS_SUCCESS; | |
f0eeea8b | 3672 | |
2a8357f2 | 3673 | done: |
2a1afb5a JH |
3674 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
3675 | &cp->addr, sizeof(cp->addr)); | |
5e762444 | 3676 | |
09fd0de5 | 3677 | hci_dev_unlock(hdev); |
7fbec224 AJ |
3678 | |
3679 | return err; | |
3680 | } | |
3681 | ||
cdbaccca MH |
3682 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
3683 | u16 len) | |
3684 | { | |
3685 | struct mgmt_cp_set_device_id *cp = data; | |
890ea898 | 3686 | struct hci_request req; |
cdbaccca | 3687 | int err; |
c72d4b8a | 3688 | __u16 source; |
cdbaccca MH |
3689 | |
3690 | BT_DBG("%s", hdev->name); | |
3691 | ||
c72d4b8a SJ |
3692 | source = __le16_to_cpu(cp->source); |
3693 | ||
3694 | if (source > 0x0002) | |
a69e8375 JH |
3695 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
3696 | MGMT_STATUS_INVALID_PARAMS); | |
c72d4b8a | 3697 | |
cdbaccca MH |
3698 | hci_dev_lock(hdev); |
3699 | ||
c72d4b8a | 3700 | hdev->devid_source = source; |
cdbaccca MH |
3701 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
3702 | hdev->devid_product = __le16_to_cpu(cp->product); | |
3703 | hdev->devid_version = __le16_to_cpu(cp->version); | |
3704 | ||
2a1afb5a JH |
3705 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, |
3706 | NULL, 0); | |
cdbaccca | 3707 | |
890ea898 | 3708 | hci_req_init(&req, hdev); |
b1a8917c | 3709 | __hci_req_update_eir(&req); |
890ea898 | 3710 | hci_req_run(&req, NULL); |
cdbaccca MH |
3711 | |
3712 | hci_dev_unlock(hdev); | |
3713 | ||
3714 | return err; | |
3715 | } | |
3716 | ||
24b4f38f AU |
3717 | static void enable_advertising_instance(struct hci_dev *hdev, u8 status, |
3718 | u16 opcode) | |
3719 | { | |
3720 | BT_DBG("status %d", status); | |
3721 | } | |
3722 | ||
1904a853 MH |
3723 | static void set_advertising_complete(struct hci_dev *hdev, u8 status, |
3724 | u16 opcode) | |
4375f103 JH |
3725 | { |
3726 | struct cmd_lookup match = { NULL, hdev }; | |
24b4f38f | 3727 | struct hci_request req; |
7816b820 FG |
3728 | u8 instance; |
3729 | struct adv_info *adv_instance; | |
3730 | int err; | |
4375f103 | 3731 | |
3ad67582 JK |
3732 | hci_dev_lock(hdev); |
3733 | ||
4375f103 JH |
3734 | if (status) { |
3735 | u8 mgmt_err = mgmt_status(status); | |
3736 | ||
3737 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, | |
3738 | cmd_status_rsp, &mgmt_err); | |
3ad67582 | 3739 | goto unlock; |
4375f103 JH |
3740 | } |
3741 | ||
d7a5a11d | 3742 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
a1536da2 | 3743 | hci_dev_set_flag(hdev, HCI_ADVERTISING); |
c93bd150 | 3744 | else |
a358dc11 | 3745 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
c93bd150 | 3746 | |
4375f103 JH |
3747 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
3748 | &match); | |
3749 | ||
3750 | new_settings(hdev, match.sk); | |
3751 | ||
3752 | if (match.sk) | |
3753 | sock_put(match.sk); | |
3ad67582 | 3754 | |
24b4f38f | 3755 | /* If "Set Advertising" was just disabled and instance advertising was |
7816b820 | 3756 | * set up earlier, then re-enable multi-instance advertising. |
24b4f38f AU |
3757 | */ |
3758 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || | |
7816b820 | 3759 | list_empty(&hdev->adv_instances)) |
24b4f38f AU |
3760 | goto unlock; |
3761 | ||
7816b820 FG |
3762 | instance = hdev->cur_adv_instance; |
3763 | if (!instance) { | |
3764 | adv_instance = list_first_entry_or_null(&hdev->adv_instances, | |
3765 | struct adv_info, list); | |
3766 | if (!adv_instance) | |
3767 | goto unlock; | |
3768 | ||
3769 | instance = adv_instance->instance; | |
3770 | } | |
3771 | ||
24b4f38f AU |
3772 | hci_req_init(&req, hdev); |
3773 | ||
f2252570 | 3774 | err = __hci_req_schedule_adv_instance(&req, instance, true); |
7816b820 FG |
3775 | |
3776 | if (!err) | |
3777 | err = hci_req_run(&req, enable_advertising_instance); | |
24b4f38f | 3778 | |
7816b820 | 3779 | if (err) |
24b4f38f AU |
3780 | BT_ERR("Failed to re-configure advertising"); |
3781 | ||
3ad67582 JK |
3782 | unlock: |
3783 | hci_dev_unlock(hdev); | |
4375f103 JH |
3784 | } |
3785 | ||
21b5187f MH |
3786 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
3787 | u16 len) | |
4375f103 JH |
3788 | { |
3789 | struct mgmt_mode *cp = data; | |
3b0602cd | 3790 | struct mgmt_pending_cmd *cmd; |
4375f103 | 3791 | struct hci_request req; |
cc91cb04 | 3792 | u8 val, status; |
4375f103 JH |
3793 | int err; |
3794 | ||
3795 | BT_DBG("request for %s", hdev->name); | |
3796 | ||
e6fe7986 JH |
3797 | status = mgmt_le_support(hdev); |
3798 | if (status) | |
a69e8375 JH |
3799 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
3800 | status); | |
4375f103 | 3801 | |
cc91cb04 | 3802 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
a69e8375 JH |
3803 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
3804 | MGMT_STATUS_INVALID_PARAMS); | |
4375f103 JH |
3805 | |
3806 | hci_dev_lock(hdev); | |
3807 | ||
3808 | val = !!cp->val; | |
4375f103 | 3809 | |
f74ca9b8 JH |
3810 | /* The following conditions are ones which mean that we should |
3811 | * not do any HCI communication but directly send a mgmt | |
3812 | * response to user space (after toggling the flag if | |
3813 | * necessary). | |
3814 | */ | |
cc91cb04 | 3815 | if (!hdev_is_powered(hdev) || |
d7a5a11d MH |
3816 | (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
3817 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) || | |
e8bb6b97 | 3818 | hci_conn_num(hdev, LE_LINK) > 0 || |
d7a5a11d | 3819 | (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
e8bb6b97 | 3820 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
cc91cb04 | 3821 | bool changed; |
4375f103 | 3822 | |
cc91cb04 | 3823 | if (cp->val) { |
cab054ab | 3824 | hdev->cur_adv_instance = 0x00; |
238be788 | 3825 | changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING); |
cc91cb04 | 3826 | if (cp->val == 0x02) |
a1536da2 | 3827 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
cc91cb04 | 3828 | else |
a358dc11 | 3829 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
cc91cb04 | 3830 | } else { |
a69d8927 | 3831 | changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING); |
a358dc11 | 3832 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
4375f103 JH |
3833 | } |
3834 | ||
3835 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); | |
3836 | if (err < 0) | |
3837 | goto unlock; | |
3838 | ||
3839 | if (changed) | |
3840 | err = new_settings(hdev, sk); | |
3841 | ||
3842 | goto unlock; | |
3843 | } | |
3844 | ||
333ae95d JH |
3845 | if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
3846 | pending_find(MGMT_OP_SET_LE, hdev)) { | |
a69e8375 JH |
3847 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
3848 | MGMT_STATUS_BUSY); | |
4375f103 JH |
3849 | goto unlock; |
3850 | } | |
3851 | ||
3852 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); | |
3853 | if (!cmd) { | |
3854 | err = -ENOMEM; | |
3855 | goto unlock; | |
3856 | } | |
3857 | ||
3858 | hci_req_init(&req, hdev); | |
3859 | ||
cc91cb04 | 3860 | if (cp->val == 0x02) |
a1536da2 | 3861 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
cc91cb04 | 3862 | else |
a358dc11 | 3863 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
cc91cb04 | 3864 | |
7816b820 FG |
3865 | cancel_adv_timeout(hdev); |
3866 | ||
24b4f38f | 3867 | if (val) { |
7816b820 FG |
3868 | /* Switch to instance "0" for the Set Advertising setting. |
3869 | * We cannot use update_[adv|scan_rsp]_data() here as the | |
3870 | * HCI_ADVERTISING flag is not yet set. | |
3871 | */ | |
cab054ab | 3872 | hdev->cur_adv_instance = 0x00; |
f2252570 JH |
3873 | __hci_req_update_adv_data(&req, 0x00); |
3874 | __hci_req_update_scan_rsp_data(&req, 0x00); | |
3875 | __hci_req_enable_advertising(&req); | |
24b4f38f | 3876 | } else { |
f2252570 | 3877 | __hci_req_disable_advertising(&req); |
24b4f38f | 3878 | } |
4375f103 JH |
3879 | |
3880 | err = hci_req_run(&req, set_advertising_complete); | |
3881 | if (err < 0) | |
3882 | mgmt_pending_remove(cmd); | |
3883 | ||
3884 | unlock: | |
3885 | hci_dev_unlock(hdev); | |
3886 | return err; | |
3887 | } | |
3888 | ||
d13eafce MH |
3889 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
3890 | void *data, u16 len) | |
3891 | { | |
3892 | struct mgmt_cp_set_static_address *cp = data; | |
3893 | int err; | |
3894 | ||
3895 | BT_DBG("%s", hdev->name); | |
3896 | ||
62af4443 | 3897 | if (!lmp_le_capable(hdev)) |
a69e8375 JH |
3898 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
3899 | MGMT_STATUS_NOT_SUPPORTED); | |
d13eafce MH |
3900 | |
3901 | if (hdev_is_powered(hdev)) | |
a69e8375 JH |
3902 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
3903 | MGMT_STATUS_REJECTED); | |
d13eafce MH |
3904 | |
3905 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { | |
3906 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) | |
a69e8375 JH |
3907 | return mgmt_cmd_status(sk, hdev->id, |
3908 | MGMT_OP_SET_STATIC_ADDRESS, | |
3909 | MGMT_STATUS_INVALID_PARAMS); | |
d13eafce MH |
3910 | |
3911 | /* Two most significant bits shall be set */ | |
3912 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) | |
a69e8375 JH |
3913 | return mgmt_cmd_status(sk, hdev->id, |
3914 | MGMT_OP_SET_STATIC_ADDRESS, | |
3915 | MGMT_STATUS_INVALID_PARAMS); | |
d13eafce MH |
3916 | } |
3917 | ||
3918 | hci_dev_lock(hdev); | |
3919 | ||
3920 | bacpy(&hdev->static_addr, &cp->bdaddr); | |
3921 | ||
93690c22 MH |
3922 | err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev); |
3923 | if (err < 0) | |
3924 | goto unlock; | |
3925 | ||
3926 | err = new_settings(hdev, sk); | |
d13eafce | 3927 | |
93690c22 | 3928 | unlock: |
d13eafce | 3929 | hci_dev_unlock(hdev); |
d13eafce MH |
3930 | return err; |
3931 | } | |
3932 | ||
14b49b9a MH |
3933 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
3934 | void *data, u16 len) | |
3935 | { | |
3936 | struct mgmt_cp_set_scan_params *cp = data; | |
3937 | __u16 interval, window; | |
3938 | int err; | |
3939 | ||
3940 | BT_DBG("%s", hdev->name); | |
3941 | ||
3942 | if (!lmp_le_capable(hdev)) | |
a69e8375 JH |
3943 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
3944 | MGMT_STATUS_NOT_SUPPORTED); | |
14b49b9a MH |
3945 | |
3946 | interval = __le16_to_cpu(cp->interval); | |
3947 | ||
3948 | if (interval < 0x0004 || interval > 0x4000) | |
a69e8375 JH |
3949 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
3950 | MGMT_STATUS_INVALID_PARAMS); | |
14b49b9a MH |
3951 | |
3952 | window = __le16_to_cpu(cp->window); | |
3953 | ||
3954 | if (window < 0x0004 || window > 0x4000) | |
a69e8375 JH |
3955 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
3956 | MGMT_STATUS_INVALID_PARAMS); | |
14b49b9a | 3957 | |
899e1075 | 3958 | if (window > interval) |
a69e8375 JH |
3959 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
3960 | MGMT_STATUS_INVALID_PARAMS); | |
899e1075 | 3961 | |
14b49b9a MH |
3962 | hci_dev_lock(hdev); |
3963 | ||
3964 | hdev->le_scan_interval = interval; | |
3965 | hdev->le_scan_window = window; | |
3966 | ||
2a1afb5a JH |
3967 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, |
3968 | NULL, 0); | |
14b49b9a | 3969 | |
dd2ef8e2 AG |
3970 | /* If background scan is running, restart it so new parameters are |
3971 | * loaded. | |
3972 | */ | |
d7a5a11d | 3973 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
dd2ef8e2 AG |
3974 | hdev->discovery.state == DISCOVERY_STOPPED) { |
3975 | struct hci_request req; | |
3976 | ||
3977 | hci_req_init(&req, hdev); | |
3978 | ||
3979 | hci_req_add_le_scan_disable(&req); | |
3980 | hci_req_add_le_passive_scan(&req); | |
3981 | ||
3982 | hci_req_run(&req, NULL); | |
3983 | } | |
3984 | ||
14b49b9a MH |
3985 | hci_dev_unlock(hdev); |
3986 | ||
3987 | return err; | |
3988 | } | |
3989 | ||
1904a853 MH |
3990 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status, |
3991 | u16 opcode) | |
33e38b3e | 3992 | { |
3b0602cd | 3993 | struct mgmt_pending_cmd *cmd; |
33e38b3e JH |
3994 | |
3995 | BT_DBG("status 0x%02x", status); | |
3996 | ||
3997 | hci_dev_lock(hdev); | |
3998 | ||
333ae95d | 3999 | cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
33e38b3e JH |
4000 | if (!cmd) |
4001 | goto unlock; | |
4002 | ||
4003 | if (status) { | |
a69e8375 JH |
4004 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
4005 | mgmt_status(status)); | |
33e38b3e | 4006 | } else { |
1a4d3c4b JH |
4007 | struct mgmt_mode *cp = cmd->param; |
4008 | ||
4009 | if (cp->val) | |
a1536da2 | 4010 | hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE); |
1a4d3c4b | 4011 | else |
a358dc11 | 4012 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
1a4d3c4b | 4013 | |
33e38b3e JH |
4014 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
4015 | new_settings(hdev, cmd->sk); | |
4016 | } | |
4017 | ||
4018 | mgmt_pending_remove(cmd); | |
4019 | ||
4020 | unlock: | |
4021 | hci_dev_unlock(hdev); | |
4022 | } | |
4023 | ||
bdb6d971 | 4024 | static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev, |
04124681 | 4025 | void *data, u16 len) |
f6422ec6 | 4026 | { |
650f726d | 4027 | struct mgmt_mode *cp = data; |
3b0602cd | 4028 | struct mgmt_pending_cmd *cmd; |
33e38b3e | 4029 | struct hci_request req; |
f6422ec6 AJ |
4030 | int err; |
4031 | ||
bdb6d971 | 4032 | BT_DBG("%s", hdev->name); |
f6422ec6 | 4033 | |
d7a5a11d | 4034 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
56f87901 | 4035 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
a69e8375 JH |
4036 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
4037 | MGMT_STATUS_NOT_SUPPORTED); | |
33c525c0 | 4038 | |
a7e80f25 | 4039 | if (cp->val != 0x00 && cp->val != 0x01) |
a69e8375 JH |
4040 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
4041 | MGMT_STATUS_INVALID_PARAMS); | |
a7e80f25 | 4042 | |
f6422ec6 AJ |
4043 | hci_dev_lock(hdev); |
4044 | ||
333ae95d | 4045 | if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
a69e8375 JH |
4046 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
4047 | MGMT_STATUS_BUSY); | |
05cbf29f JH |
4048 | goto unlock; |
4049 | } | |
4050 | ||
d7a5a11d | 4051 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) { |
1a4d3c4b JH |
4052 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
4053 | hdev); | |
4054 | goto unlock; | |
4055 | } | |
4056 | ||
406ef2a6 | 4057 | if (!hdev_is_powered(hdev)) { |
ce05d603 | 4058 | hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE); |
406ef2a6 JH |
4059 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
4060 | hdev); | |
4061 | new_settings(hdev, sk); | |
4062 | goto unlock; | |
4063 | } | |
4064 | ||
33e38b3e JH |
4065 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
4066 | data, len); | |
4067 | if (!cmd) { | |
4068 | err = -ENOMEM; | |
4069 | goto unlock; | |
f6422ec6 AJ |
4070 | } |
4071 | ||
33e38b3e JH |
4072 | hci_req_init(&req, hdev); |
4073 | ||
bf943cbf | 4074 | __hci_req_write_fast_connectable(&req, cp->val); |
33e38b3e JH |
4075 | |
4076 | err = hci_req_run(&req, fast_connectable_complete); | |
f6422ec6 | 4077 | if (err < 0) { |
a69e8375 JH |
4078 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
4079 | MGMT_STATUS_FAILED); | |
33e38b3e | 4080 | mgmt_pending_remove(cmd); |
f6422ec6 AJ |
4081 | } |
4082 | ||
33e38b3e | 4083 | unlock: |
f6422ec6 | 4084 | hci_dev_unlock(hdev); |
33e38b3e | 4085 | |
f6422ec6 AJ |
4086 | return err; |
4087 | } | |
4088 | ||
1904a853 | 4089 | static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
0663ca2a | 4090 | { |
3b0602cd | 4091 | struct mgmt_pending_cmd *cmd; |
0663ca2a JH |
4092 | |
4093 | BT_DBG("status 0x%02x", status); | |
4094 | ||
4095 | hci_dev_lock(hdev); | |
4096 | ||
333ae95d | 4097 | cmd = pending_find(MGMT_OP_SET_BREDR, hdev); |
0663ca2a JH |
4098 | if (!cmd) |
4099 | goto unlock; | |
4100 | ||
4101 | if (status) { | |
4102 | u8 mgmt_err = mgmt_status(status); | |
4103 | ||
4104 | /* We need to restore the flag if related HCI commands | |
4105 | * failed. | |
4106 | */ | |
a358dc11 | 4107 | hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); |
0663ca2a | 4108 | |
a69e8375 | 4109 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
0663ca2a JH |
4110 | } else { |
4111 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); | |
4112 | new_settings(hdev, cmd->sk); | |
4113 | } | |
4114 | ||
4115 | mgmt_pending_remove(cmd); | |
4116 | ||
4117 | unlock: | |
4118 | hci_dev_unlock(hdev); | |
4119 | } | |
4120 | ||
4121 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) | |
4122 | { | |
4123 | struct mgmt_mode *cp = data; | |
3b0602cd | 4124 | struct mgmt_pending_cmd *cmd; |
0663ca2a JH |
4125 | struct hci_request req; |
4126 | int err; | |
4127 | ||
4128 | BT_DBG("request for %s", hdev->name); | |
4129 | ||
4130 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) | |
a69e8375 JH |
4131 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
4132 | MGMT_STATUS_NOT_SUPPORTED); | |
0663ca2a | 4133 | |
d7a5a11d | 4134 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
a69e8375 JH |
4135 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
4136 | MGMT_STATUS_REJECTED); | |
0663ca2a JH |
4137 | |
4138 | if (cp->val != 0x00 && cp->val != 0x01) | |
a69e8375 JH |
4139 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
4140 | MGMT_STATUS_INVALID_PARAMS); | |
0663ca2a JH |
4141 | |
4142 | hci_dev_lock(hdev); | |
4143 | ||
d7a5a11d | 4144 | if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
0663ca2a JH |
4145 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
4146 | goto unlock; | |
4147 | } | |
4148 | ||
4149 | if (!hdev_is_powered(hdev)) { | |
4150 | if (!cp->val) { | |
a358dc11 MH |
4151 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
4152 | hci_dev_clear_flag(hdev, HCI_SSP_ENABLED); | |
4153 | hci_dev_clear_flag(hdev, HCI_LINK_SECURITY); | |
4154 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); | |
4155 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); | |
0663ca2a JH |
4156 | } |
4157 | ||
ce05d603 | 4158 | hci_dev_change_flag(hdev, HCI_BREDR_ENABLED); |
0663ca2a JH |
4159 | |
4160 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); | |
4161 | if (err < 0) | |
4162 | goto unlock; | |
4163 | ||
4164 | err = new_settings(hdev, sk); | |
4165 | goto unlock; | |
4166 | } | |
4167 | ||
4168 | /* Reject disabling when powered on */ | |
4169 | if (!cp->val) { | |
a69e8375 JH |
4170 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
4171 | MGMT_STATUS_REJECTED); | |
0663ca2a | 4172 | goto unlock; |
111e4bcc MH |
4173 | } else { |
4174 | /* When configuring a dual-mode controller to operate | |
4175 | * with LE only and using a static address, then switching | |
4176 | * BR/EDR back on is not allowed. | |
4177 | * | |
4178 | * Dual-mode controllers shall operate with the public | |
4179 | * address as its identity address for BR/EDR and LE. So | |
4180 | * reject the attempt to create an invalid configuration. | |
3a5486e1 MH |
4181 | * |
4182 | * The same restrictions applies when secure connections | |
4183 | * has been enabled. For BR/EDR this is a controller feature | |
4184 | * while for LE it is a host stack feature. This means that | |
4185 | * switching BR/EDR back on when secure connections has been | |
4186 | * enabled is not a supported transaction. | |
111e4bcc | 4187 | */ |
d7a5a11d | 4188 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
3a5486e1 | 4189 | (bacmp(&hdev->static_addr, BDADDR_ANY) || |
d7a5a11d | 4190 | hci_dev_test_flag(hdev, HCI_SC_ENABLED))) { |
a69e8375 JH |
4191 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
4192 | MGMT_STATUS_REJECTED); | |
111e4bcc MH |
4193 | goto unlock; |
4194 | } | |
0663ca2a JH |
4195 | } |
4196 | ||
333ae95d | 4197 | if (pending_find(MGMT_OP_SET_BREDR, hdev)) { |
a69e8375 JH |
4198 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
4199 | MGMT_STATUS_BUSY); | |
0663ca2a JH |
4200 | goto unlock; |
4201 | } | |
4202 | ||
4203 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); | |
4204 | if (!cmd) { | |
4205 | err = -ENOMEM; | |
4206 | goto unlock; | |
4207 | } | |
4208 | ||
f2252570 JH |
4209 | /* We need to flip the bit already here so that |
4210 | * hci_req_update_adv_data generates the correct flags. | |
0663ca2a | 4211 | */ |
a1536da2 | 4212 | hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); |
0663ca2a JH |
4213 | |
4214 | hci_req_init(&req, hdev); | |
aa8af46e | 4215 | |
bf943cbf | 4216 | __hci_req_write_fast_connectable(&req, false); |
01b1cb87 | 4217 | __hci_req_update_scan(&req); |
aa8af46e | 4218 | |
f14d8f64 MH |
4219 | /* Since only the advertising data flags will change, there |
4220 | * is no need to update the scan response data. | |
4221 | */ | |
cab054ab | 4222 | __hci_req_update_adv_data(&req, hdev->cur_adv_instance); |
aa8af46e | 4223 | |
0663ca2a JH |
4224 | err = hci_req_run(&req, set_bredr_complete); |
4225 | if (err < 0) | |
4226 | mgmt_pending_remove(cmd); | |
4227 | ||
4228 | unlock: | |
4229 | hci_dev_unlock(hdev); | |
4230 | return err; | |
4231 | } | |
4232 | ||
a1443f5a JH |
4233 | static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
4234 | { | |
3b0602cd | 4235 | struct mgmt_pending_cmd *cmd; |
a1443f5a JH |
4236 | struct mgmt_mode *cp; |
4237 | ||
4238 | BT_DBG("%s status %u", hdev->name, status); | |
4239 | ||
4240 | hci_dev_lock(hdev); | |
4241 | ||
333ae95d | 4242 | cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev); |
a1443f5a JH |
4243 | if (!cmd) |
4244 | goto unlock; | |
4245 | ||
4246 | if (status) { | |
a69e8375 JH |
4247 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
4248 | mgmt_status(status)); | |
a1443f5a JH |
4249 | goto remove; |
4250 | } | |
4251 | ||
4252 | cp = cmd->param; | |
4253 | ||
4254 | switch (cp->val) { | |
4255 | case 0x00: | |
a358dc11 MH |
4256 | hci_dev_clear_flag(hdev, HCI_SC_ENABLED); |
4257 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); | |
a1443f5a JH |
4258 | break; |
4259 | case 0x01: | |
a1536da2 | 4260 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
a358dc11 | 4261 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
a1443f5a JH |
4262 | break; |
4263 | case 0x02: | |
a1536da2 MH |
4264 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
4265 | hci_dev_set_flag(hdev, HCI_SC_ONLY); | |
a1443f5a JH |
4266 | break; |
4267 | } | |
4268 | ||
4269 | send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev); | |
4270 | new_settings(hdev, cmd->sk); | |
4271 | ||
4272 | remove: | |
4273 | mgmt_pending_remove(cmd); | |
4274 | unlock: | |
4275 | hci_dev_unlock(hdev); | |
4276 | } | |
4277 | ||
eac83dc6 MH |
4278 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
4279 | void *data, u16 len) | |
4280 | { | |
4281 | struct mgmt_mode *cp = data; | |
3b0602cd | 4282 | struct mgmt_pending_cmd *cmd; |
a1443f5a | 4283 | struct hci_request req; |
a3209694 | 4284 | u8 val; |
eac83dc6 MH |
4285 | int err; |
4286 | ||
4287 | BT_DBG("request for %s", hdev->name); | |
4288 | ||
05b3c3e7 | 4289 | if (!lmp_sc_capable(hdev) && |
d7a5a11d | 4290 | !hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
a69e8375 JH |
4291 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
4292 | MGMT_STATUS_NOT_SUPPORTED); | |
eac83dc6 | 4293 | |
d7a5a11d | 4294 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
59200286 | 4295 | lmp_sc_capable(hdev) && |
d7a5a11d | 4296 | !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
a69e8375 JH |
4297 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
4298 | MGMT_STATUS_REJECTED); | |
ed93ec69 | 4299 | |
0ab04a9c | 4300 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
a69e8375 | 4301 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
eac83dc6 MH |
4302 | MGMT_STATUS_INVALID_PARAMS); |
4303 | ||
4304 | hci_dev_lock(hdev); | |
4305 | ||
05b3c3e7 | 4306 | if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) || |
d7a5a11d | 4307 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
eac83dc6 MH |
4308 | bool changed; |
4309 | ||
0ab04a9c | 4310 | if (cp->val) { |
238be788 MH |
4311 | changed = !hci_dev_test_and_set_flag(hdev, |
4312 | HCI_SC_ENABLED); | |
0ab04a9c | 4313 | if (cp->val == 0x02) |
a1536da2 | 4314 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
0ab04a9c | 4315 | else |
a358dc11 | 4316 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
0ab04a9c | 4317 | } else { |
a69d8927 MH |
4318 | changed = hci_dev_test_and_clear_flag(hdev, |
4319 | HCI_SC_ENABLED); | |
a358dc11 | 4320 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
0ab04a9c | 4321 | } |
eac83dc6 MH |
4322 | |
4323 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); | |
4324 | if (err < 0) | |
4325 | goto failed; | |
4326 | ||
4327 | if (changed) | |
4328 | err = new_settings(hdev, sk); | |
4329 | ||
4330 | goto failed; | |
4331 | } | |
4332 | ||
333ae95d | 4333 | if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
a69e8375 JH |
4334 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
4335 | MGMT_STATUS_BUSY); | |
eac83dc6 MH |
4336 | goto failed; |
4337 | } | |
4338 | ||
0ab04a9c MH |
4339 | val = !!cp->val; |
4340 | ||
d7a5a11d MH |
4341 | if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
4342 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) { | |
eac83dc6 MH |
4343 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
4344 | goto failed; | |
4345 | } | |
4346 | ||
4347 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); | |
4348 | if (!cmd) { | |
4349 | err = -ENOMEM; | |
4350 | goto failed; | |
4351 | } | |
4352 | ||
a1443f5a JH |
4353 | hci_req_init(&req, hdev); |
4354 | hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val); | |
4355 | err = hci_req_run(&req, sc_enable_complete); | |
eac83dc6 MH |
4356 | if (err < 0) { |
4357 | mgmt_pending_remove(cmd); | |
4358 | goto failed; | |
4359 | } | |
4360 | ||
4361 | failed: | |
4362 | hci_dev_unlock(hdev); | |
4363 | return err; | |
4364 | } | |
4365 | ||
4e39ac81 MH |
4366 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
4367 | void *data, u16 len) | |
4368 | { | |
4369 | struct mgmt_mode *cp = data; | |
b9710979 | 4370 | bool changed, use_changed; |
4e39ac81 MH |
4371 | int err; |
4372 | ||
4373 | BT_DBG("request for %s", hdev->name); | |
4374 | ||
b9710979 | 4375 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
a69e8375 JH |
4376 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
4377 | MGMT_STATUS_INVALID_PARAMS); | |
4e39ac81 MH |
4378 | |
4379 | hci_dev_lock(hdev); | |
4380 | ||
4381 | if (cp->val) | |
238be788 | 4382 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
4e39ac81 | 4383 | else |
a69d8927 MH |
4384 | changed = hci_dev_test_and_clear_flag(hdev, |
4385 | HCI_KEEP_DEBUG_KEYS); | |
4e39ac81 | 4386 | |
b9710979 | 4387 | if (cp->val == 0x02) |
238be788 MH |
4388 | use_changed = !hci_dev_test_and_set_flag(hdev, |
4389 | HCI_USE_DEBUG_KEYS); | |
b9710979 | 4390 | else |
a69d8927 MH |
4391 | use_changed = hci_dev_test_and_clear_flag(hdev, |
4392 | HCI_USE_DEBUG_KEYS); | |
b9710979 JH |
4393 | |
4394 | if (hdev_is_powered(hdev) && use_changed && | |
d7a5a11d | 4395 | hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
b9710979 JH |
4396 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
4397 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, | |
4398 | sizeof(mode), &mode); | |
4399 | } | |
4400 | ||
4e39ac81 MH |
4401 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
4402 | if (err < 0) | |
4403 | goto unlock; | |
4404 | ||
4405 | if (changed) | |
4406 | err = new_settings(hdev, sk); | |
4407 | ||
4408 | unlock: | |
4409 | hci_dev_unlock(hdev); | |
4410 | return err; | |
4411 | } | |
4412 | ||
62b04cd1 JH |
4413 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
4414 | u16 len) | |
4415 | { | |
4416 | struct mgmt_cp_set_privacy *cp = cp_data; | |
4417 | bool changed; | |
4418 | int err; | |
4419 | ||
4420 | BT_DBG("request for %s", hdev->name); | |
4421 | ||
4422 | if (!lmp_le_capable(hdev)) | |
a69e8375 JH |
4423 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
4424 | MGMT_STATUS_NOT_SUPPORTED); | |
62b04cd1 JH |
4425 | |
4426 | if (cp->privacy != 0x00 && cp->privacy != 0x01) | |
a69e8375 JH |
4427 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
4428 | MGMT_STATUS_INVALID_PARAMS); | |
62b04cd1 JH |
4429 | |
4430 | if (hdev_is_powered(hdev)) | |
a69e8375 JH |
4431 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
4432 | MGMT_STATUS_REJECTED); | |
62b04cd1 JH |
4433 | |
4434 | hci_dev_lock(hdev); | |
4435 | ||
c21c0ea0 JH |
4436 | /* If user space supports this command it is also expected to |
4437 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. | |
4438 | */ | |
a1536da2 | 4439 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
c21c0ea0 | 4440 | |
62b04cd1 | 4441 | if (cp->privacy) { |
238be788 | 4442 | changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY); |
62b04cd1 | 4443 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
a1536da2 | 4444 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
62b04cd1 | 4445 | } else { |
a69d8927 | 4446 | changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY); |
62b04cd1 | 4447 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
a358dc11 | 4448 | hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED); |
62b04cd1 JH |
4449 | } |
4450 | ||
4451 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); | |
4452 | if (err < 0) | |
4453 | goto unlock; | |
4454 | ||
4455 | if (changed) | |
4456 | err = new_settings(hdev, sk); | |
4457 | ||
4458 | unlock: | |
4459 | hci_dev_unlock(hdev); | |
4460 | return err; | |
4461 | } | |
4462 | ||
41edf160 JH |
4463 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
4464 | { | |
4465 | switch (irk->addr.type) { | |
4466 | case BDADDR_LE_PUBLIC: | |
4467 | return true; | |
4468 | ||
4469 | case BDADDR_LE_RANDOM: | |
4470 | /* Two most significant bits shall be set */ | |
4471 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) | |
4472 | return false; | |
4473 | return true; | |
4474 | } | |
4475 | ||
4476 | return false; | |
4477 | } | |
4478 | ||
4479 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, | |
4480 | u16 len) | |
4481 | { | |
4482 | struct mgmt_cp_load_irks *cp = cp_data; | |
ba1d6936 JH |
4483 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
4484 | sizeof(struct mgmt_irk_info)); | |
41edf160 JH |
4485 | u16 irk_count, expected_len; |
4486 | int i, err; | |
4487 | ||
4488 | BT_DBG("request for %s", hdev->name); | |
4489 | ||
4490 | if (!lmp_le_capable(hdev)) | |
a69e8375 JH |
4491 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
4492 | MGMT_STATUS_NOT_SUPPORTED); | |
41edf160 JH |
4493 | |
4494 | irk_count = __le16_to_cpu(cp->irk_count); | |
ba1d6936 JH |
4495 | if (irk_count > max_irk_count) { |
4496 | BT_ERR("load_irks: too big irk_count value %u", irk_count); | |
a69e8375 JH |
4497 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
4498 | MGMT_STATUS_INVALID_PARAMS); | |
ba1d6936 | 4499 | } |
41edf160 JH |
4500 | |
4501 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); | |
4502 | if (expected_len != len) { | |
4503 | BT_ERR("load_irks: expected %u bytes, got %u bytes", | |
2606ecbc | 4504 | expected_len, len); |
a69e8375 JH |
4505 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
4506 | MGMT_STATUS_INVALID_PARAMS); | |
41edf160 JH |
4507 | } |
4508 | ||
4509 | BT_DBG("%s irk_count %u", hdev->name, irk_count); | |
4510 | ||
4511 | for (i = 0; i < irk_count; i++) { | |
4512 | struct mgmt_irk_info *key = &cp->irks[i]; | |
4513 | ||
4514 | if (!irk_is_valid(key)) | |
a69e8375 JH |
4515 | return mgmt_cmd_status(sk, hdev->id, |
4516 | MGMT_OP_LOAD_IRKS, | |
4517 | MGMT_STATUS_INVALID_PARAMS); | |
41edf160 JH |
4518 | } |
4519 | ||
4520 | hci_dev_lock(hdev); | |
4521 | ||
4522 | hci_smp_irks_clear(hdev); | |
4523 | ||
4524 | for (i = 0; i < irk_count; i++) { | |
4525 | struct mgmt_irk_info *irk = &cp->irks[i]; | |
41edf160 | 4526 | |
85813a7e JH |
4527 | hci_add_irk(hdev, &irk->addr.bdaddr, |
4528 | le_addr_type(irk->addr.type), irk->val, | |
41edf160 JH |
4529 | BDADDR_ANY); |
4530 | } | |
4531 | ||
a1536da2 | 4532 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
41edf160 | 4533 | |
2a1afb5a | 4534 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
41edf160 JH |
4535 | |
4536 | hci_dev_unlock(hdev); | |
4537 | ||
4538 | return err; | |
4539 | } | |
4540 | ||
3f706b72 JH |
4541 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
4542 | { | |
4543 | if (key->master != 0x00 && key->master != 0x01) | |
4544 | return false; | |
490cb0b3 MH |
4545 | |
4546 | switch (key->addr.type) { | |
4547 | case BDADDR_LE_PUBLIC: | |
4548 | return true; | |
4549 | ||
4550 | case BDADDR_LE_RANDOM: | |
4551 | /* Two most significant bits shall be set */ | |
4552 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) | |
4553 | return false; | |
4554 | return true; | |
4555 | } | |
4556 | ||
4557 | return false; | |
3f706b72 JH |
4558 | } |
4559 | ||
bdb6d971 | 4560 | static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev, |
04124681 | 4561 | void *cp_data, u16 len) |
346af67b | 4562 | { |
346af67b | 4563 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
ba1d6936 JH |
4564 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
4565 | sizeof(struct mgmt_ltk_info)); | |
346af67b | 4566 | u16 key_count, expected_len; |
715a5bf2 | 4567 | int i, err; |
346af67b | 4568 | |
cf99ba13 MH |
4569 | BT_DBG("request for %s", hdev->name); |
4570 | ||
4571 | if (!lmp_le_capable(hdev)) | |
a69e8375 JH |
4572 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
4573 | MGMT_STATUS_NOT_SUPPORTED); | |
cf99ba13 | 4574 | |
1f350c87 | 4575 | key_count = __le16_to_cpu(cp->key_count); |
ba1d6936 JH |
4576 | if (key_count > max_key_count) { |
4577 | BT_ERR("load_ltks: too big key_count value %u", key_count); | |
a69e8375 JH |
4578 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
4579 | MGMT_STATUS_INVALID_PARAMS); | |
ba1d6936 | 4580 | } |
346af67b VCG |
4581 | |
4582 | expected_len = sizeof(*cp) + key_count * | |
4583 | sizeof(struct mgmt_ltk_info); | |
4584 | if (expected_len != len) { | |
4585 | BT_ERR("load_keys: expected %u bytes, got %u bytes", | |
2606ecbc | 4586 | expected_len, len); |
a69e8375 JH |
4587 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
4588 | MGMT_STATUS_INVALID_PARAMS); | |
346af67b VCG |
4589 | } |
4590 | ||
bdb6d971 | 4591 | BT_DBG("%s key_count %u", hdev->name, key_count); |
346af67b | 4592 | |
54ad6d8a JH |
4593 | for (i = 0; i < key_count; i++) { |
4594 | struct mgmt_ltk_info *key = &cp->keys[i]; | |
4595 | ||
3f706b72 | 4596 | if (!ltk_is_valid(key)) |
a69e8375 JH |
4597 | return mgmt_cmd_status(sk, hdev->id, |
4598 | MGMT_OP_LOAD_LONG_TERM_KEYS, | |
4599 | MGMT_STATUS_INVALID_PARAMS); | |
54ad6d8a JH |
4600 | } |
4601 | ||
346af67b VCG |
4602 | hci_dev_lock(hdev); |
4603 | ||
4604 | hci_smp_ltks_clear(hdev); | |
4605 | ||
4606 | for (i = 0; i < key_count; i++) { | |
4607 | struct mgmt_ltk_info *key = &cp->keys[i]; | |
85813a7e | 4608 | u8 type, authenticated; |
346af67b | 4609 | |
61b43357 JH |
4610 | switch (key->type) { |
4611 | case MGMT_LTK_UNAUTHENTICATED: | |
d7b25450 | 4612 | authenticated = 0x00; |
23fb8de3 | 4613 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
61b43357 JH |
4614 | break; |
4615 | case MGMT_LTK_AUTHENTICATED: | |
d7b25450 | 4616 | authenticated = 0x01; |
23fb8de3 JH |
4617 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
4618 | break; | |
4619 | case MGMT_LTK_P256_UNAUTH: | |
4620 | authenticated = 0x00; | |
4621 | type = SMP_LTK_P256; | |
61b43357 | 4622 | break; |
23fb8de3 JH |
4623 | case MGMT_LTK_P256_AUTH: |
4624 | authenticated = 0x01; | |
4625 | type = SMP_LTK_P256; | |
61b43357 | 4626 | break; |
23fb8de3 JH |
4627 | case MGMT_LTK_P256_DEBUG: |
4628 | authenticated = 0x00; | |
4629 | type = SMP_LTK_P256_DEBUG; | |
61b43357 JH |
4630 | default: |
4631 | continue; | |
4632 | } | |
d7b25450 | 4633 | |
85813a7e JH |
4634 | hci_add_ltk(hdev, &key->addr.bdaddr, |
4635 | le_addr_type(key->addr.type), type, authenticated, | |
4636 | key->val, key->enc_size, key->ediv, key->rand); | |
346af67b VCG |
4637 | } |
4638 | ||
2a1afb5a | 4639 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
715a5bf2 JH |
4640 | NULL, 0); |
4641 | ||
346af67b | 4642 | hci_dev_unlock(hdev); |
346af67b | 4643 | |
715a5bf2 | 4644 | return err; |
346af67b VCG |
4645 | } |
4646 | ||
3b0602cd | 4647 | static int conn_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
dd983808 | 4648 | { |
dd983808 | 4649 | struct hci_conn *conn = cmd->user_data; |
9981bdb0 | 4650 | struct mgmt_rp_get_conn_info rp; |
9df74653 | 4651 | int err; |
dd983808 | 4652 | |
9981bdb0 | 4653 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
dd983808 | 4654 | |
9981bdb0 | 4655 | if (status == MGMT_STATUS_SUCCESS) { |
dd983808 | 4656 | rp.rssi = conn->rssi; |
9981bdb0 JH |
4657 | rp.tx_power = conn->tx_power; |
4658 | rp.max_tx_power = conn->max_tx_power; | |
4659 | } else { | |
4660 | rp.rssi = HCI_RSSI_INVALID; | |
4661 | rp.tx_power = HCI_TX_POWER_INVALID; | |
4662 | rp.max_tx_power = HCI_TX_POWER_INVALID; | |
dd983808 AK |
4663 | } |
4664 | ||
2a1afb5a JH |
4665 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
4666 | status, &rp, sizeof(rp)); | |
dd983808 AK |
4667 | |
4668 | hci_conn_drop(conn); | |
f8aaf9b6 | 4669 | hci_conn_put(conn); |
9df74653 JH |
4670 | |
4671 | return err; | |
dd983808 AK |
4672 | } |
4673 | ||
1904a853 MH |
4674 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status, |
4675 | u16 opcode) | |
dd983808 AK |
4676 | { |
4677 | struct hci_cp_read_rssi *cp; | |
3b0602cd | 4678 | struct mgmt_pending_cmd *cmd; |
dd983808 | 4679 | struct hci_conn *conn; |
dd983808 | 4680 | u16 handle; |
9981bdb0 | 4681 | u8 status; |
dd983808 | 4682 | |
9981bdb0 | 4683 | BT_DBG("status 0x%02x", hci_status); |
dd983808 AK |
4684 | |
4685 | hci_dev_lock(hdev); | |
4686 | ||
dd983808 AK |
4687 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
4688 | * Level so we check which one was last sent to retrieve connection | |
4689 | * handle. Both commands have handle as first parameter so it's safe to | |
4690 | * cast data on the same command struct. | |
4691 | * | |
4692 | * First command sent is always Read RSSI and we fail only if it fails. | |
4693 | * In other case we simply override error to indicate success as we | |
4694 | * already remembered if TX power value is actually valid. | |
4695 | */ | |
4696 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); | |
4697 | if (!cp) { | |
4698 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); | |
9981bdb0 JH |
4699 | status = MGMT_STATUS_SUCCESS; |
4700 | } else { | |
4701 | status = mgmt_status(hci_status); | |
dd983808 AK |
4702 | } |
4703 | ||
4704 | if (!cp) { | |
9981bdb0 | 4705 | BT_ERR("invalid sent_cmd in conn_info response"); |
dd983808 AK |
4706 | goto unlock; |
4707 | } | |
4708 | ||
4709 | handle = __le16_to_cpu(cp->handle); | |
4710 | conn = hci_conn_hash_lookup_handle(hdev, handle); | |
4711 | if (!conn) { | |
9981bdb0 | 4712 | BT_ERR("unknown handle (%d) in conn_info response", handle); |
dd983808 AK |
4713 | goto unlock; |
4714 | } | |
4715 | ||
333ae95d | 4716 | cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn); |
9981bdb0 JH |
4717 | if (!cmd) |
4718 | goto unlock; | |
dd983808 | 4719 | |
9981bdb0 JH |
4720 | cmd->cmd_complete(cmd, status); |
4721 | mgmt_pending_remove(cmd); | |
dd983808 AK |
4722 | |
4723 | unlock: | |
4724 | hci_dev_unlock(hdev); | |
4725 | } | |
4726 | ||
4727 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, | |
4728 | u16 len) | |
4729 | { | |
4730 | struct mgmt_cp_get_conn_info *cp = data; | |
4731 | struct mgmt_rp_get_conn_info rp; | |
4732 | struct hci_conn *conn; | |
4733 | unsigned long conn_info_age; | |
4734 | int err = 0; | |
4735 | ||
4736 | BT_DBG("%s", hdev->name); | |
4737 | ||
4738 | memset(&rp, 0, sizeof(rp)); | |
4739 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); | |
4740 | rp.addr.type = cp->addr.type; | |
4741 | ||
4742 | if (!bdaddr_type_is_valid(cp->addr.type)) | |
2a1afb5a JH |
4743 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
4744 | MGMT_STATUS_INVALID_PARAMS, | |
4745 | &rp, sizeof(rp)); | |
dd983808 AK |
4746 | |
4747 | hci_dev_lock(hdev); | |
4748 | ||
4749 | if (!hdev_is_powered(hdev)) { | |
2a1afb5a JH |
4750 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
4751 | MGMT_STATUS_NOT_POWERED, &rp, | |
4752 | sizeof(rp)); | |
dd983808 AK |
4753 | goto unlock; |
4754 | } | |
4755 | ||
4756 | if (cp->addr.type == BDADDR_BREDR) | |
4757 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, | |
4758 | &cp->addr.bdaddr); | |
4759 | else | |
4760 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); | |
4761 | ||
4762 | if (!conn || conn->state != BT_CONNECTED) { | |
2a1afb5a JH |
4763 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
4764 | MGMT_STATUS_NOT_CONNECTED, &rp, | |
4765 | sizeof(rp)); | |
dd983808 AK |
4766 | goto unlock; |
4767 | } | |
4768 | ||
333ae95d | 4769 | if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) { |
2a1afb5a JH |
4770 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
4771 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); | |
9981bdb0 JH |
4772 | goto unlock; |
4773 | } | |
4774 | ||
dd983808 AK |
4775 | /* To avoid client trying to guess when to poll again for information we |
4776 | * calculate conn info age as random value between min/max set in hdev. | |
4777 | */ | |
4778 | conn_info_age = hdev->conn_info_min_age + | |
4779 | prandom_u32_max(hdev->conn_info_max_age - | |
4780 | hdev->conn_info_min_age); | |
4781 | ||
4782 | /* Query controller to refresh cached values if they are too old or were | |
4783 | * never read. | |
4784 | */ | |
f4e2dd53 AK |
4785 | if (time_after(jiffies, conn->conn_info_timestamp + |
4786 | msecs_to_jiffies(conn_info_age)) || | |
dd983808 AK |
4787 | !conn->conn_info_timestamp) { |
4788 | struct hci_request req; | |
4789 | struct hci_cp_read_tx_power req_txp_cp; | |
4790 | struct hci_cp_read_rssi req_rssi_cp; | |
3b0602cd | 4791 | struct mgmt_pending_cmd *cmd; |
dd983808 AK |
4792 | |
4793 | hci_req_init(&req, hdev); | |
4794 | req_rssi_cp.handle = cpu_to_le16(conn->handle); | |
4795 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), | |
4796 | &req_rssi_cp); | |
4797 | ||
f7faab0c AK |
4798 | /* For LE links TX power does not change thus we don't need to |
4799 | * query for it once value is known. | |
4800 | */ | |
4801 | if (!bdaddr_type_is_le(cp->addr.type) || | |
4802 | conn->tx_power == HCI_TX_POWER_INVALID) { | |
4803 | req_txp_cp.handle = cpu_to_le16(conn->handle); | |
4804 | req_txp_cp.type = 0x00; | |
4805 | hci_req_add(&req, HCI_OP_READ_TX_POWER, | |
4806 | sizeof(req_txp_cp), &req_txp_cp); | |
4807 | } | |
dd983808 | 4808 | |
eed5daf3 AK |
4809 | /* Max TX power needs to be read only once per connection */ |
4810 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { | |
4811 | req_txp_cp.handle = cpu_to_le16(conn->handle); | |
4812 | req_txp_cp.type = 0x01; | |
4813 | hci_req_add(&req, HCI_OP_READ_TX_POWER, | |
4814 | sizeof(req_txp_cp), &req_txp_cp); | |
4815 | } | |
4816 | ||
dd983808 AK |
4817 | err = hci_req_run(&req, conn_info_refresh_complete); |
4818 | if (err < 0) | |
4819 | goto unlock; | |
4820 | ||
4821 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, | |
4822 | data, len); | |
4823 | if (!cmd) { | |
4824 | err = -ENOMEM; | |
4825 | goto unlock; | |
4826 | } | |
4827 | ||
4828 | hci_conn_hold(conn); | |
f8aaf9b6 | 4829 | cmd->user_data = hci_conn_get(conn); |
9981bdb0 | 4830 | cmd->cmd_complete = conn_info_cmd_complete; |
dd983808 AK |
4831 | |
4832 | conn->conn_info_timestamp = jiffies; | |
4833 | } else { | |
4834 | /* Cache is valid, just reply with values cached in hci_conn */ | |
4835 | rp.rssi = conn->rssi; | |
4836 | rp.tx_power = conn->tx_power; | |
eed5daf3 | 4837 | rp.max_tx_power = conn->max_tx_power; |
dd983808 | 4838 | |
2a1afb5a JH |
4839 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
4840 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); | |
dd983808 AK |
4841 | } |
4842 | ||
4843 | unlock: | |
4844 | hci_dev_unlock(hdev); | |
4845 | return err; | |
4846 | } | |
4847 | ||
3b0602cd | 4848 | static int clock_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
95868426 | 4849 | { |
69487371 | 4850 | struct hci_conn *conn = cmd->user_data; |
95868426 | 4851 | struct mgmt_rp_get_clock_info rp; |
69487371 | 4852 | struct hci_dev *hdev; |
9df74653 | 4853 | int err; |
69487371 JH |
4854 | |
4855 | memset(&rp, 0, sizeof(rp)); | |
4856 | memcpy(&rp.addr, &cmd->param, sizeof(rp.addr)); | |
4857 | ||
4858 | if (status) | |
4859 | goto complete; | |
4860 | ||
4861 | hdev = hci_dev_get(cmd->index); | |
4862 | if (hdev) { | |
4863 | rp.local_clock = cpu_to_le32(hdev->clock); | |
4864 | hci_dev_put(hdev); | |
4865 | } | |
4866 | ||
4867 | if (conn) { | |
4868 | rp.piconet_clock = cpu_to_le32(conn->clock); | |
4869 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); | |
4870 | } | |
4871 | ||
4872 | complete: | |
2a1afb5a JH |
4873 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp, |
4874 | sizeof(rp)); | |
69487371 JH |
4875 | |
4876 | if (conn) { | |
4877 | hci_conn_drop(conn); | |
4878 | hci_conn_put(conn); | |
4879 | } | |
9df74653 JH |
4880 | |
4881 | return err; | |
69487371 JH |
4882 | } |
4883 | ||
1904a853 | 4884 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
69487371 | 4885 | { |
95868426 | 4886 | struct hci_cp_read_clock *hci_cp; |
3b0602cd | 4887 | struct mgmt_pending_cmd *cmd; |
95868426 JH |
4888 | struct hci_conn *conn; |
4889 | ||
4890 | BT_DBG("%s status %u", hdev->name, status); | |
4891 | ||
4892 | hci_dev_lock(hdev); | |
4893 | ||
4894 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); | |
4895 | if (!hci_cp) | |
4896 | goto unlock; | |
4897 | ||
4898 | if (hci_cp->which) { | |
4899 | u16 handle = __le16_to_cpu(hci_cp->handle); | |
4900 | conn = hci_conn_hash_lookup_handle(hdev, handle); | |
4901 | } else { | |
4902 | conn = NULL; | |
4903 | } | |
4904 | ||
333ae95d | 4905 | cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
95868426 JH |
4906 | if (!cmd) |
4907 | goto unlock; | |
4908 | ||
69487371 | 4909 | cmd->cmd_complete(cmd, mgmt_status(status)); |
95868426 | 4910 | mgmt_pending_remove(cmd); |
95868426 JH |
4911 | |
4912 | unlock: | |
4913 | hci_dev_unlock(hdev); | |
4914 | } | |
4915 | ||
4916 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, | |
4917 | u16 len) | |
4918 | { | |
4919 | struct mgmt_cp_get_clock_info *cp = data; | |
4920 | struct mgmt_rp_get_clock_info rp; | |
4921 | struct hci_cp_read_clock hci_cp; | |
3b0602cd | 4922 | struct mgmt_pending_cmd *cmd; |
95868426 JH |
4923 | struct hci_request req; |
4924 | struct hci_conn *conn; | |
4925 | int err; | |
4926 | ||
4927 | BT_DBG("%s", hdev->name); | |
4928 | ||
4929 | memset(&rp, 0, sizeof(rp)); | |
4930 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); | |
4931 | rp.addr.type = cp->addr.type; | |
4932 | ||
4933 | if (cp->addr.type != BDADDR_BREDR) | |
2a1afb5a JH |
4934 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
4935 | MGMT_STATUS_INVALID_PARAMS, | |
4936 | &rp, sizeof(rp)); | |
95868426 JH |
4937 | |
4938 | hci_dev_lock(hdev); | |
4939 | ||
4940 | if (!hdev_is_powered(hdev)) { | |
2a1afb5a JH |
4941 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
4942 | MGMT_STATUS_NOT_POWERED, &rp, | |
4943 | sizeof(rp)); | |
95868426 JH |
4944 | goto unlock; |
4945 | } | |
4946 | ||
4947 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { | |
4948 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, | |
4949 | &cp->addr.bdaddr); | |
4950 | if (!conn || conn->state != BT_CONNECTED) { | |
2a1afb5a JH |
4951 | err = mgmt_cmd_complete(sk, hdev->id, |
4952 | MGMT_OP_GET_CLOCK_INFO, | |
4953 | MGMT_STATUS_NOT_CONNECTED, | |
4954 | &rp, sizeof(rp)); | |
95868426 JH |
4955 | goto unlock; |
4956 | } | |
4957 | } else { | |
4958 | conn = NULL; | |
4959 | } | |
4960 | ||
4961 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); | |
4962 | if (!cmd) { | |
4963 | err = -ENOMEM; | |
4964 | goto unlock; | |
4965 | } | |
4966 | ||
69487371 JH |
4967 | cmd->cmd_complete = clock_info_cmd_complete; |
4968 | ||
95868426 JH |
4969 | hci_req_init(&req, hdev); |
4970 | ||
4971 | memset(&hci_cp, 0, sizeof(hci_cp)); | |
4972 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); | |
4973 | ||
4974 | if (conn) { | |
4975 | hci_conn_hold(conn); | |
f8aaf9b6 | 4976 | cmd->user_data = hci_conn_get(conn); |
95868426 JH |
4977 | |
4978 | hci_cp.handle = cpu_to_le16(conn->handle); | |
4979 | hci_cp.which = 0x01; /* Piconet clock */ | |
4980 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); | |
4981 | } | |
4982 | ||
4983 | err = hci_req_run(&req, get_clock_info_complete); | |
4984 | if (err < 0) | |
4985 | mgmt_pending_remove(cmd); | |
4986 | ||
4987 | unlock: | |
4988 | hci_dev_unlock(hdev); | |
4989 | return err; | |
4990 | } | |
4991 | ||
5a154e6f JH |
4992 | static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) |
4993 | { | |
4994 | struct hci_conn *conn; | |
4995 | ||
4996 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); | |
4997 | if (!conn) | |
4998 | return false; | |
4999 | ||
5000 | if (conn->dst_type != type) | |
5001 | return false; | |
5002 | ||
5003 | if (conn->state != BT_CONNECTED) | |
5004 | return false; | |
5005 | ||
5006 | return true; | |
5007 | } | |
5008 | ||
5009 | /* This function requires the caller holds hdev->lock */ | |
51d7a94d | 5010 | static int hci_conn_params_set(struct hci_dev *hdev, bdaddr_t *addr, |
5a154e6f JH |
5011 | u8 addr_type, u8 auto_connect) |
5012 | { | |
5a154e6f JH |
5013 | struct hci_conn_params *params; |
5014 | ||
5015 | params = hci_conn_params_add(hdev, addr, addr_type); | |
5016 | if (!params) | |
5017 | return -EIO; | |
5018 | ||
5019 | if (params->auto_connect == auto_connect) | |
5020 | return 0; | |
5021 | ||
5022 | list_del_init(¶ms->action); | |
5023 | ||
5024 | switch (auto_connect) { | |
5025 | case HCI_AUTO_CONN_DISABLED: | |
5026 | case HCI_AUTO_CONN_LINK_LOSS: | |
28a667c9 JP |
5027 | /* If auto connect is being disabled when we're trying to |
5028 | * connect to device, keep connecting. | |
5029 | */ | |
5030 | if (params->explicit_connect) | |
5031 | list_add(¶ms->action, &hdev->pend_le_conns); | |
5a154e6f JH |
5032 | break; |
5033 | case HCI_AUTO_CONN_REPORT: | |
49c50922 JH |
5034 | if (params->explicit_connect) |
5035 | list_add(¶ms->action, &hdev->pend_le_conns); | |
5036 | else | |
5037 | list_add(¶ms->action, &hdev->pend_le_reports); | |
5a154e6f JH |
5038 | break; |
5039 | case HCI_AUTO_CONN_DIRECT: | |
5040 | case HCI_AUTO_CONN_ALWAYS: | |
51d7a94d | 5041 | if (!is_connected(hdev, addr, addr_type)) |
5a154e6f | 5042 | list_add(¶ms->action, &hdev->pend_le_conns); |
5a154e6f JH |
5043 | break; |
5044 | } | |
5045 | ||
5046 | params->auto_connect = auto_connect; | |
5047 | ||
5048 | BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type, | |
5049 | auto_connect); | |
5050 | ||
5051 | return 0; | |
5052 | } | |
5053 | ||
8afef092 MH |
5054 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
5055 | bdaddr_t *bdaddr, u8 type, u8 action) | |
5056 | { | |
5057 | struct mgmt_ev_device_added ev; | |
5058 | ||
5059 | bacpy(&ev.addr.bdaddr, bdaddr); | |
5060 | ev.addr.type = type; | |
5061 | ev.action = action; | |
5062 | ||
5063 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); | |
5064 | } | |
5065 | ||
2faade53 MH |
5066 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
5067 | void *data, u16 len) | |
5068 | { | |
5069 | struct mgmt_cp_add_device *cp = data; | |
5070 | u8 auto_conn, addr_type; | |
5071 | int err; | |
5072 | ||
5073 | BT_DBG("%s", hdev->name); | |
5074 | ||
6659358e | 5075 | if (!bdaddr_type_is_valid(cp->addr.type) || |
2faade53 | 5076 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
2a1afb5a JH |
5077 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
5078 | MGMT_STATUS_INVALID_PARAMS, | |
5079 | &cp->addr, sizeof(cp->addr)); | |
2faade53 | 5080 | |
4b9e7e75 | 5081 | if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) |
2a1afb5a JH |
5082 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
5083 | MGMT_STATUS_INVALID_PARAMS, | |
5084 | &cp->addr, sizeof(cp->addr)); | |
2faade53 MH |
5085 | |
5086 | hci_dev_lock(hdev); | |
5087 | ||
6659358e | 5088 | if (cp->addr.type == BDADDR_BREDR) { |
4b9e7e75 | 5089 | /* Only incoming connections action is supported for now */ |
6659358e | 5090 | if (cp->action != 0x01) { |
51d7a94d JH |
5091 | err = mgmt_cmd_complete(sk, hdev->id, |
5092 | MGMT_OP_ADD_DEVICE, | |
5093 | MGMT_STATUS_INVALID_PARAMS, | |
5094 | &cp->addr, sizeof(cp->addr)); | |
6659358e JH |
5095 | goto unlock; |
5096 | } | |
5097 | ||
5098 | err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr, | |
5099 | cp->addr.type); | |
5100 | if (err) | |
5101 | goto unlock; | |
a397407f | 5102 | |
01b1cb87 | 5103 | hci_req_update_scan(hdev); |
a397407f | 5104 | |
6659358e JH |
5105 | goto added; |
5106 | } | |
5107 | ||
85813a7e | 5108 | addr_type = le_addr_type(cp->addr.type); |
2faade53 | 5109 | |
4b9e7e75 | 5110 | if (cp->action == 0x02) |
2faade53 | 5111 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
4b9e7e75 MH |
5112 | else if (cp->action == 0x01) |
5113 | auto_conn = HCI_AUTO_CONN_DIRECT; | |
2faade53 | 5114 | else |
a3451d27 | 5115 | auto_conn = HCI_AUTO_CONN_REPORT; |
2faade53 | 5116 | |
9a0a8a8e JP |
5117 | /* Kernel internally uses conn_params with resolvable private |
5118 | * address, but Add Device allows only identity addresses. | |
5119 | * Make sure it is enforced before calling | |
5120 | * hci_conn_params_lookup. | |
5121 | */ | |
5122 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { | |
51d7a94d JH |
5123 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
5124 | MGMT_STATUS_INVALID_PARAMS, | |
5125 | &cp->addr, sizeof(cp->addr)); | |
9a0a8a8e JP |
5126 | goto unlock; |
5127 | } | |
5128 | ||
bf5b3c8b MH |
5129 | /* If the connection parameters don't exist for this device, |
5130 | * they will be created and configured with defaults. | |
5131 | */ | |
51d7a94d | 5132 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
d06b50ce | 5133 | auto_conn) < 0) { |
51d7a94d JH |
5134 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
5135 | MGMT_STATUS_FAILED, &cp->addr, | |
5136 | sizeof(cp->addr)); | |
2faade53 MH |
5137 | goto unlock; |
5138 | } | |
5139 | ||
51d7a94d JH |
5140 | hci_update_background_scan(hdev); |
5141 | ||
6659358e | 5142 | added: |
8afef092 MH |
5143 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
5144 | ||
51d7a94d JH |
5145 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
5146 | MGMT_STATUS_SUCCESS, &cp->addr, | |
5147 | sizeof(cp->addr)); | |
2faade53 MH |
5148 | |
5149 | unlock: | |
5150 | hci_dev_unlock(hdev); | |
5151 | return err; | |
5152 | } | |
5153 | ||
8afef092 MH |
5154 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
5155 | bdaddr_t *bdaddr, u8 type) | |
5156 | { | |
5157 | struct mgmt_ev_device_removed ev; | |
5158 | ||
5159 | bacpy(&ev.addr.bdaddr, bdaddr); | |
5160 | ev.addr.type = type; | |
5161 | ||
5162 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); | |
5163 | } | |
5164 | ||
2faade53 MH |
5165 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
5166 | void *data, u16 len) | |
5167 | { | |
5168 | struct mgmt_cp_remove_device *cp = data; | |
5169 | int err; | |
5170 | ||
5171 | BT_DBG("%s", hdev->name); | |
5172 | ||
5173 | hci_dev_lock(hdev); | |
5174 | ||
5175 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { | |
c71593dd | 5176 | struct hci_conn_params *params; |
2faade53 MH |
5177 | u8 addr_type; |
5178 | ||
6659358e | 5179 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
51d7a94d JH |
5180 | err = mgmt_cmd_complete(sk, hdev->id, |
5181 | MGMT_OP_REMOVE_DEVICE, | |
5182 | MGMT_STATUS_INVALID_PARAMS, | |
5183 | &cp->addr, sizeof(cp->addr)); | |
2faade53 MH |
5184 | goto unlock; |
5185 | } | |
5186 | ||
6659358e JH |
5187 | if (cp->addr.type == BDADDR_BREDR) { |
5188 | err = hci_bdaddr_list_del(&hdev->whitelist, | |
5189 | &cp->addr.bdaddr, | |
5190 | cp->addr.type); | |
5191 | if (err) { | |
51d7a94d JH |
5192 | err = mgmt_cmd_complete(sk, hdev->id, |
5193 | MGMT_OP_REMOVE_DEVICE, | |
5194 | MGMT_STATUS_INVALID_PARAMS, | |
5195 | &cp->addr, | |
5196 | sizeof(cp->addr)); | |
6659358e JH |
5197 | goto unlock; |
5198 | } | |
5199 | ||
01b1cb87 | 5200 | hci_req_update_scan(hdev); |
a397407f | 5201 | |
6659358e JH |
5202 | device_removed(sk, hdev, &cp->addr.bdaddr, |
5203 | cp->addr.type); | |
5204 | goto complete; | |
5205 | } | |
5206 | ||
85813a7e | 5207 | addr_type = le_addr_type(cp->addr.type); |
2faade53 | 5208 | |
9a0a8a8e JP |
5209 | /* Kernel internally uses conn_params with resolvable private |
5210 | * address, but Remove Device allows only identity addresses. | |
5211 | * Make sure it is enforced before calling | |
5212 | * hci_conn_params_lookup. | |
5213 | */ | |
5214 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { | |
51d7a94d JH |
5215 | err = mgmt_cmd_complete(sk, hdev->id, |
5216 | MGMT_OP_REMOVE_DEVICE, | |
5217 | MGMT_STATUS_INVALID_PARAMS, | |
5218 | &cp->addr, sizeof(cp->addr)); | |
9a0a8a8e JP |
5219 | goto unlock; |
5220 | } | |
5221 | ||
c71593dd JH |
5222 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
5223 | addr_type); | |
5224 | if (!params) { | |
51d7a94d JH |
5225 | err = mgmt_cmd_complete(sk, hdev->id, |
5226 | MGMT_OP_REMOVE_DEVICE, | |
5227 | MGMT_STATUS_INVALID_PARAMS, | |
5228 | &cp->addr, sizeof(cp->addr)); | |
c71593dd JH |
5229 | goto unlock; |
5230 | } | |
5231 | ||
679d2b6f JH |
5232 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED || |
5233 | params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { | |
51d7a94d JH |
5234 | err = mgmt_cmd_complete(sk, hdev->id, |
5235 | MGMT_OP_REMOVE_DEVICE, | |
5236 | MGMT_STATUS_INVALID_PARAMS, | |
5237 | &cp->addr, sizeof(cp->addr)); | |
c71593dd JH |
5238 | goto unlock; |
5239 | } | |
5240 | ||
d1dbf12e | 5241 | list_del(¶ms->action); |
c71593dd JH |
5242 | list_del(¶ms->list); |
5243 | kfree(params); | |
51d7a94d | 5244 | hci_update_background_scan(hdev); |
8afef092 MH |
5245 | |
5246 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); | |
2faade53 | 5247 | } else { |
19de0825 | 5248 | struct hci_conn_params *p, *tmp; |
6659358e | 5249 | struct bdaddr_list *b, *btmp; |
19de0825 | 5250 | |
2faade53 | 5251 | if (cp->addr.type) { |
51d7a94d JH |
5252 | err = mgmt_cmd_complete(sk, hdev->id, |
5253 | MGMT_OP_REMOVE_DEVICE, | |
5254 | MGMT_STATUS_INVALID_PARAMS, | |
5255 | &cp->addr, sizeof(cp->addr)); | |
2faade53 MH |
5256 | goto unlock; |
5257 | } | |
5258 | ||
6659358e JH |
5259 | list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) { |
5260 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); | |
5261 | list_del(&b->list); | |
5262 | kfree(b); | |
5263 | } | |
5264 | ||
01b1cb87 | 5265 | hci_req_update_scan(hdev); |
a397407f | 5266 | |
19de0825 JH |
5267 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
5268 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) | |
5269 | continue; | |
5270 | device_removed(sk, hdev, &p->addr, p->addr_type); | |
679d2b6f JH |
5271 | if (p->explicit_connect) { |
5272 | p->auto_connect = HCI_AUTO_CONN_EXPLICIT; | |
5273 | continue; | |
5274 | } | |
19de0825 JH |
5275 | list_del(&p->action); |
5276 | list_del(&p->list); | |
5277 | kfree(p); | |
5278 | } | |
5279 | ||
5280 | BT_DBG("All LE connection parameters were removed"); | |
5281 | ||
51d7a94d | 5282 | hci_update_background_scan(hdev); |
2faade53 MH |
5283 | } |
5284 | ||
6659358e | 5285 | complete: |
51d7a94d JH |
5286 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
5287 | MGMT_STATUS_SUCCESS, &cp->addr, | |
5288 | sizeof(cp->addr)); | |
2faade53 MH |
5289 | unlock: |
5290 | hci_dev_unlock(hdev); | |
5291 | return err; | |
5292 | } | |
5293 | ||
a26f3dcf JH |
5294 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
5295 | u16 len) | |
5296 | { | |
5297 | struct mgmt_cp_load_conn_param *cp = data; | |
ba1d6936 JH |
5298 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
5299 | sizeof(struct mgmt_conn_param)); | |
a26f3dcf JH |
5300 | u16 param_count, expected_len; |
5301 | int i; | |
5302 | ||
5303 | if (!lmp_le_capable(hdev)) | |
a69e8375 JH |
5304 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
5305 | MGMT_STATUS_NOT_SUPPORTED); | |
a26f3dcf JH |
5306 | |
5307 | param_count = __le16_to_cpu(cp->param_count); | |
ba1d6936 JH |
5308 | if (param_count > max_param_count) { |
5309 | BT_ERR("load_conn_param: too big param_count value %u", | |
5310 | param_count); | |
a69e8375 JH |
5311 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
5312 | MGMT_STATUS_INVALID_PARAMS); | |
ba1d6936 | 5313 | } |
a26f3dcf JH |
5314 | |
5315 | expected_len = sizeof(*cp) + param_count * | |
5316 | sizeof(struct mgmt_conn_param); | |
5317 | if (expected_len != len) { | |
5318 | BT_ERR("load_conn_param: expected %u bytes, got %u bytes", | |
5319 | expected_len, len); | |
a69e8375 JH |
5320 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
5321 | MGMT_STATUS_INVALID_PARAMS); | |
a26f3dcf JH |
5322 | } |
5323 | ||
5324 | BT_DBG("%s param_count %u", hdev->name, param_count); | |
5325 | ||
5326 | hci_dev_lock(hdev); | |
5327 | ||
5328 | hci_conn_params_clear_disabled(hdev); | |
5329 | ||
5330 | for (i = 0; i < param_count; i++) { | |
5331 | struct mgmt_conn_param *param = &cp->params[i]; | |
5332 | struct hci_conn_params *hci_param; | |
5333 | u16 min, max, latency, timeout; | |
5334 | u8 addr_type; | |
5335 | ||
5336 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, | |
5337 | param->addr.type); | |
5338 | ||
5339 | if (param->addr.type == BDADDR_LE_PUBLIC) { | |
5340 | addr_type = ADDR_LE_DEV_PUBLIC; | |
5341 | } else if (param->addr.type == BDADDR_LE_RANDOM) { | |
5342 | addr_type = ADDR_LE_DEV_RANDOM; | |
5343 | } else { | |
5344 | BT_ERR("Ignoring invalid connection parameters"); | |
5345 | continue; | |
5346 | } | |
5347 | ||
5348 | min = le16_to_cpu(param->min_interval); | |
5349 | max = le16_to_cpu(param->max_interval); | |
5350 | latency = le16_to_cpu(param->latency); | |
5351 | timeout = le16_to_cpu(param->timeout); | |
5352 | ||
5353 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", | |
5354 | min, max, latency, timeout); | |
5355 | ||
5356 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { | |
5357 | BT_ERR("Ignoring invalid connection parameters"); | |
5358 | continue; | |
5359 | } | |
5360 | ||
5361 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, | |
5362 | addr_type); | |
5363 | if (!hci_param) { | |
5364 | BT_ERR("Failed to add connection parameters"); | |
5365 | continue; | |
5366 | } | |
5367 | ||
5368 | hci_param->conn_min_interval = min; | |
5369 | hci_param->conn_max_interval = max; | |
5370 | hci_param->conn_latency = latency; | |
5371 | hci_param->supervision_timeout = timeout; | |
5372 | } | |
5373 | ||
5374 | hci_dev_unlock(hdev); | |
5375 | ||
2a1afb5a JH |
5376 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, |
5377 | NULL, 0); | |
a26f3dcf JH |
5378 | } |
5379 | ||
dbece37a MH |
5380 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
5381 | void *data, u16 len) | |
5382 | { | |
5383 | struct mgmt_cp_set_external_config *cp = data; | |
5384 | bool changed; | |
5385 | int err; | |
5386 | ||
5387 | BT_DBG("%s", hdev->name); | |
5388 | ||
5389 | if (hdev_is_powered(hdev)) | |
a69e8375 JH |
5390 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
5391 | MGMT_STATUS_REJECTED); | |
dbece37a MH |
5392 | |
5393 | if (cp->config != 0x00 && cp->config != 0x01) | |
a69e8375 JH |
5394 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
5395 | MGMT_STATUS_INVALID_PARAMS); | |
dbece37a MH |
5396 | |
5397 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) | |
a69e8375 JH |
5398 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
5399 | MGMT_STATUS_NOT_SUPPORTED); | |
dbece37a MH |
5400 | |
5401 | hci_dev_lock(hdev); | |
5402 | ||
5403 | if (cp->config) | |
238be788 | 5404 | changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED); |
dbece37a | 5405 | else |
a69d8927 | 5406 | changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED); |
dbece37a MH |
5407 | |
5408 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); | |
5409 | if (err < 0) | |
5410 | goto unlock; | |
5411 | ||
5412 | if (!changed) | |
5413 | goto unlock; | |
5414 | ||
f4537c04 MH |
5415 | err = new_options(hdev, sk); |
5416 | ||
d7a5a11d | 5417 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) { |
dbece37a | 5418 | mgmt_index_removed(hdev); |
d603b76b | 5419 | |
516018a9 | 5420 | if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) { |
a1536da2 MH |
5421 | hci_dev_set_flag(hdev, HCI_CONFIG); |
5422 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); | |
d603b76b MH |
5423 | |
5424 | queue_work(hdev->req_workqueue, &hdev->power_on); | |
5425 | } else { | |
5ea234d3 | 5426 | set_bit(HCI_RAW, &hdev->flags); |
d603b76b MH |
5427 | mgmt_index_added(hdev); |
5428 | } | |
dbece37a MH |
5429 | } |
5430 | ||
5431 | unlock: | |
5432 | hci_dev_unlock(hdev); | |
5433 | return err; | |
5434 | } | |
5435 | ||
9713c17b MH |
5436 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
5437 | void *data, u16 len) | |
5438 | { | |
5439 | struct mgmt_cp_set_public_address *cp = data; | |
5440 | bool changed; | |
5441 | int err; | |
5442 | ||
5443 | BT_DBG("%s", hdev->name); | |
5444 | ||
5445 | if (hdev_is_powered(hdev)) | |
a69e8375 JH |
5446 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
5447 | MGMT_STATUS_REJECTED); | |
9713c17b MH |
5448 | |
5449 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) | |
a69e8375 JH |
5450 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
5451 | MGMT_STATUS_INVALID_PARAMS); | |
9713c17b MH |
5452 | |
5453 | if (!hdev->set_bdaddr) | |
a69e8375 JH |
5454 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
5455 | MGMT_STATUS_NOT_SUPPORTED); | |
9713c17b MH |
5456 | |
5457 | hci_dev_lock(hdev); | |
5458 | ||
5459 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); | |
5460 | bacpy(&hdev->public_addr, &cp->bdaddr); | |
5461 | ||
5462 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); | |
5463 | if (err < 0) | |
5464 | goto unlock; | |
5465 | ||
5466 | if (!changed) | |
5467 | goto unlock; | |
5468 | ||
d7a5a11d | 5469 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
9713c17b MH |
5470 | err = new_options(hdev, sk); |
5471 | ||
5472 | if (is_configured(hdev)) { | |
5473 | mgmt_index_removed(hdev); | |
5474 | ||
a358dc11 | 5475 | hci_dev_clear_flag(hdev, HCI_UNCONFIGURED); |
9713c17b | 5476 | |
a1536da2 MH |
5477 | hci_dev_set_flag(hdev, HCI_CONFIG); |
5478 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); | |
9713c17b MH |
5479 | |
5480 | queue_work(hdev->req_workqueue, &hdev->power_on); | |
5481 | } | |
5482 | ||
5483 | unlock: | |
5484 | hci_dev_unlock(hdev); | |
5485 | return err; | |
5486 | } | |
5487 | ||
bea41609 MH |
5488 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
5489 | u8 data_len) | |
5490 | { | |
5491 | eir[eir_len++] = sizeof(type) + data_len; | |
5492 | eir[eir_len++] = type; | |
5493 | memcpy(&eir[eir_len], data, data_len); | |
5494 | eir_len += data_len; | |
5495 | ||
5496 | return eir_len; | |
5497 | } | |
5498 | ||
40f66c05 JH |
5499 | static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status, |
5500 | u16 opcode, struct sk_buff *skb) | |
5501 | { | |
5502 | const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp; | |
5503 | struct mgmt_rp_read_local_oob_ext_data *mgmt_rp; | |
5504 | u8 *h192, *r192, *h256, *r256; | |
5505 | struct mgmt_pending_cmd *cmd; | |
5506 | u16 eir_len; | |
5507 | int err; | |
5508 | ||
5509 | BT_DBG("%s status %u", hdev->name, status); | |
5510 | ||
5511 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev); | |
5512 | if (!cmd) | |
5513 | return; | |
5514 | ||
5515 | mgmt_cp = cmd->param; | |
5516 | ||
5517 | if (status) { | |
5518 | status = mgmt_status(status); | |
5519 | eir_len = 0; | |
5520 | ||
5521 | h192 = NULL; | |
5522 | r192 = NULL; | |
5523 | h256 = NULL; | |
5524 | r256 = NULL; | |
5525 | } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { | |
5526 | struct hci_rp_read_local_oob_data *rp; | |
5527 | ||
5528 | if (skb->len != sizeof(*rp)) { | |
5529 | status = MGMT_STATUS_FAILED; | |
5530 | eir_len = 0; | |
5531 | } else { | |
5532 | status = MGMT_STATUS_SUCCESS; | |
5533 | rp = (void *)skb->data; | |
5534 | ||
5535 | eir_len = 5 + 18 + 18; | |
5536 | h192 = rp->hash; | |
5537 | r192 = rp->rand; | |
5538 | h256 = NULL; | |
5539 | r256 = NULL; | |
5540 | } | |
5541 | } else { | |
5542 | struct hci_rp_read_local_oob_ext_data *rp; | |
5543 | ||
5544 | if (skb->len != sizeof(*rp)) { | |
5545 | status = MGMT_STATUS_FAILED; | |
5546 | eir_len = 0; | |
5547 | } else { | |
5548 | status = MGMT_STATUS_SUCCESS; | |
5549 | rp = (void *)skb->data; | |
5550 | ||
5551 | if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) { | |
5552 | eir_len = 5 + 18 + 18; | |
5553 | h192 = NULL; | |
5554 | r192 = NULL; | |
5555 | } else { | |
5556 | eir_len = 5 + 18 + 18 + 18 + 18; | |
5557 | h192 = rp->hash192; | |
5558 | r192 = rp->rand192; | |
5559 | } | |
5560 | ||
5561 | h256 = rp->hash256; | |
5562 | r256 = rp->rand256; | |
5563 | } | |
5564 | } | |
5565 | ||
5566 | mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL); | |
5567 | if (!mgmt_rp) | |
5568 | goto done; | |
5569 | ||
5570 | if (status) | |
5571 | goto send_rsp; | |
5572 | ||
5573 | eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV, | |
5574 | hdev->dev_class, 3); | |
5575 | ||
5576 | if (h192 && r192) { | |
5577 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, | |
5578 | EIR_SSP_HASH_C192, h192, 16); | |
5579 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, | |
5580 | EIR_SSP_RAND_R192, r192, 16); | |
5581 | } | |
5582 | ||
5583 | if (h256 && r256) { | |
5584 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, | |
5585 | EIR_SSP_HASH_C256, h256, 16); | |
5586 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, | |
5587 | EIR_SSP_RAND_R256, r256, 16); | |
5588 | } | |
5589 | ||
5590 | send_rsp: | |
5591 | mgmt_rp->type = mgmt_cp->type; | |
5592 | mgmt_rp->eir_len = cpu_to_le16(eir_len); | |
5593 | ||
5594 | err = mgmt_cmd_complete(cmd->sk, hdev->id, | |
5595 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status, | |
5596 | mgmt_rp, sizeof(*mgmt_rp) + eir_len); | |
5597 | if (err < 0 || status) | |
5598 | goto done; | |
5599 | ||
5600 | hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS); | |
5601 | ||
5602 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, | |
5603 | mgmt_rp, sizeof(*mgmt_rp) + eir_len, | |
5604 | HCI_MGMT_OOB_DATA_EVENTS, cmd->sk); | |
5605 | done: | |
5606 | kfree(mgmt_rp); | |
5607 | mgmt_pending_remove(cmd); | |
5608 | } | |
5609 | ||
5610 | static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk, | |
5611 | struct mgmt_cp_read_local_oob_ext_data *cp) | |
5612 | { | |
5613 | struct mgmt_pending_cmd *cmd; | |
5614 | struct hci_request req; | |
5615 | int err; | |
5616 | ||
5617 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev, | |
5618 | cp, sizeof(*cp)); | |
5619 | if (!cmd) | |
5620 | return -ENOMEM; | |
5621 | ||
5622 | hci_req_init(&req, hdev); | |
5623 | ||
5624 | if (bredr_sc_enabled(hdev)) | |
5625 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); | |
5626 | else | |
5627 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); | |
5628 | ||
5629 | err = hci_req_run_skb(&req, read_local_oob_ext_data_complete); | |
5630 | if (err < 0) { | |
5631 | mgmt_pending_remove(cmd); | |
5632 | return err; | |
5633 | } | |
5634 | ||
5635 | return 0; | |
5636 | } | |
5637 | ||
4f0f155c MH |
5638 | static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev, |
5639 | void *data, u16 data_len) | |
5640 | { | |
5641 | struct mgmt_cp_read_local_oob_ext_data *cp = data; | |
5642 | struct mgmt_rp_read_local_oob_ext_data *rp; | |
5643 | size_t rp_len; | |
5644 | u16 eir_len; | |
0821a2c5 | 5645 | u8 status, flags, role, addr[7], hash[16], rand[16]; |
4f0f155c MH |
5646 | int err; |
5647 | ||
5648 | BT_DBG("%s", hdev->name); | |
5649 | ||
57b0d3e8 MH |
5650 | if (hdev_is_powered(hdev)) { |
5651 | switch (cp->type) { | |
5652 | case BIT(BDADDR_BREDR): | |
5653 | status = mgmt_bredr_support(hdev); | |
5654 | if (status) | |
5655 | eir_len = 0; | |
5656 | else | |
5657 | eir_len = 5; | |
5658 | break; | |
5659 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): | |
5660 | status = mgmt_le_support(hdev); | |
5661 | if (status) | |
5662 | eir_len = 0; | |
5663 | else | |
5664 | eir_len = 9 + 3 + 18 + 18 + 3; | |
5665 | break; | |
5666 | default: | |
5667 | status = MGMT_STATUS_INVALID_PARAMS; | |
5668 | eir_len = 0; | |
5669 | break; | |
5670 | } | |
5671 | } else { | |
5672 | status = MGMT_STATUS_NOT_POWERED; | |
5673 | eir_len = 0; | |
4f0f155c MH |
5674 | } |
5675 | ||
4f0f155c MH |
5676 | rp_len = sizeof(*rp) + eir_len; |
5677 | rp = kmalloc(rp_len, GFP_ATOMIC); | |
efcd8c98 | 5678 | if (!rp) |
4f0f155c | 5679 | return -ENOMEM; |
efcd8c98 | 5680 | |
57b0d3e8 MH |
5681 | if (status) |
5682 | goto complete; | |
5683 | ||
efcd8c98 | 5684 | hci_dev_lock(hdev); |
4f0f155c MH |
5685 | |
5686 | eir_len = 0; | |
5687 | switch (cp->type) { | |
5688 | case BIT(BDADDR_BREDR): | |
40f66c05 JH |
5689 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
5690 | err = read_local_ssp_oob_req(hdev, sk, cp); | |
5691 | hci_dev_unlock(hdev); | |
5692 | if (!err) | |
5693 | goto done; | |
5694 | ||
5695 | status = MGMT_STATUS_FAILED; | |
5696 | goto complete; | |
5697 | } else { | |
5698 | eir_len = eir_append_data(rp->eir, eir_len, | |
5699 | EIR_CLASS_OF_DEV, | |
5700 | hdev->dev_class, 3); | |
5701 | } | |
4f0f155c MH |
5702 | break; |
5703 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): | |
5082a599 MH |
5704 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
5705 | smp_generate_oob(hdev, hash, rand) < 0) { | |
0821a2c5 | 5706 | hci_dev_unlock(hdev); |
57b0d3e8 MH |
5707 | status = MGMT_STATUS_FAILED; |
5708 | goto complete; | |
0821a2c5 MH |
5709 | } |
5710 | ||
e213568a MH |
5711 | /* This should return the active RPA, but since the RPA |
5712 | * is only programmed on demand, it is really hard to fill | |
5713 | * this in at the moment. For now disallow retrieving | |
5714 | * local out-of-band data when privacy is in use. | |
5715 | * | |
5716 | * Returning the identity address will not help here since | |
5717 | * pairing happens before the identity resolving key is | |
5718 | * known and thus the connection establishment happens | |
5719 | * based on the RPA and not the identity address. | |
5720 | */ | |
4f0f155c | 5721 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) { |
e213568a MH |
5722 | hci_dev_unlock(hdev); |
5723 | status = MGMT_STATUS_REJECTED; | |
5724 | goto complete; | |
5725 | } | |
5726 | ||
5727 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || | |
5728 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || | |
5729 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && | |
5730 | bacmp(&hdev->static_addr, BDADDR_ANY))) { | |
4f0f155c MH |
5731 | memcpy(addr, &hdev->static_addr, 6); |
5732 | addr[6] = 0x01; | |
5733 | } else { | |
5734 | memcpy(addr, &hdev->bdaddr, 6); | |
5735 | addr[6] = 0x00; | |
5736 | } | |
5737 | ||
5738 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR, | |
5739 | addr, sizeof(addr)); | |
5740 | ||
5741 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) | |
5742 | role = 0x02; | |
5743 | else | |
5744 | role = 0x01; | |
5745 | ||
5746 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE, | |
5747 | &role, sizeof(role)); | |
5748 | ||
5082a599 MH |
5749 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) { |
5750 | eir_len = eir_append_data(rp->eir, eir_len, | |
5751 | EIR_LE_SC_CONFIRM, | |
5752 | hash, sizeof(hash)); | |
0821a2c5 | 5753 | |
5082a599 MH |
5754 | eir_len = eir_append_data(rp->eir, eir_len, |
5755 | EIR_LE_SC_RANDOM, | |
5756 | rand, sizeof(rand)); | |
5757 | } | |
0821a2c5 | 5758 | |
f2252570 | 5759 | flags = mgmt_get_adv_discov_flags(hdev); |
4f0f155c MH |
5760 | |
5761 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) | |
5762 | flags |= LE_AD_NO_BREDR; | |
5763 | ||
5764 | eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS, | |
5765 | &flags, sizeof(flags)); | |
5766 | break; | |
5767 | } | |
5768 | ||
4f0f155c MH |
5769 | hci_dev_unlock(hdev); |
5770 | ||
72000df2 MH |
5771 | hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS); |
5772 | ||
57b0d3e8 MH |
5773 | status = MGMT_STATUS_SUCCESS; |
5774 | ||
5775 | complete: | |
efcd8c98 MH |
5776 | rp->type = cp->type; |
5777 | rp->eir_len = cpu_to_le16(eir_len); | |
5778 | ||
4f0f155c | 5779 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, |
57b0d3e8 MH |
5780 | status, rp, sizeof(*rp) + eir_len); |
5781 | if (err < 0 || status) | |
72000df2 MH |
5782 | goto done; |
5783 | ||
5784 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, | |
5785 | rp, sizeof(*rp) + eir_len, | |
5786 | HCI_MGMT_OOB_DATA_EVENTS, sk); | |
4f0f155c | 5787 | |
0821a2c5 | 5788 | done: |
4f0f155c MH |
5789 | kfree(rp); |
5790 | ||
5791 | return err; | |
5792 | } | |
5793 | ||
089fa8c0 AU |
5794 | static u32 get_supported_adv_flags(struct hci_dev *hdev) |
5795 | { | |
5796 | u32 flags = 0; | |
5797 | ||
5798 | flags |= MGMT_ADV_FLAG_CONNECTABLE; | |
5799 | flags |= MGMT_ADV_FLAG_DISCOV; | |
5800 | flags |= MGMT_ADV_FLAG_LIMITED_DISCOV; | |
5801 | flags |= MGMT_ADV_FLAG_MANAGED_FLAGS; | |
5802 | ||
5803 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) | |
5804 | flags |= MGMT_ADV_FLAG_TX_POWER; | |
5805 | ||
5806 | return flags; | |
5807 | } | |
5808 | ||
d3d5305b MH |
5809 | static int read_adv_features(struct sock *sk, struct hci_dev *hdev, |
5810 | void *data, u16 data_len) | |
5811 | { | |
5812 | struct mgmt_rp_read_adv_features *rp; | |
5813 | size_t rp_len; | |
02c04afe | 5814 | int err; |
286e0c83 | 5815 | struct adv_info *adv_instance; |
089fa8c0 | 5816 | u32 supported_flags; |
02c04afe | 5817 | u8 *instance; |
d3d5305b MH |
5818 | |
5819 | BT_DBG("%s", hdev->name); | |
5820 | ||
089fa8c0 AU |
5821 | if (!lmp_le_capable(hdev)) |
5822 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, | |
5823 | MGMT_STATUS_REJECTED); | |
5824 | ||
d3d5305b MH |
5825 | hci_dev_lock(hdev); |
5826 | ||
02c04afe | 5827 | rp_len = sizeof(*rp) + hdev->adv_instance_cnt; |
d3d5305b MH |
5828 | rp = kmalloc(rp_len, GFP_ATOMIC); |
5829 | if (!rp) { | |
5830 | hci_dev_unlock(hdev); | |
5831 | return -ENOMEM; | |
5832 | } | |
5833 | ||
089fa8c0 AU |
5834 | supported_flags = get_supported_adv_flags(hdev); |
5835 | ||
5836 | rp->supported_flags = cpu_to_le32(supported_flags); | |
dc5d82a9 MH |
5837 | rp->max_adv_data_len = HCI_MAX_AD_LENGTH; |
5838 | rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH; | |
d2609b34 | 5839 | rp->max_instances = HCI_MAX_ADV_INSTANCES; |
02c04afe | 5840 | rp->num_instances = hdev->adv_instance_cnt; |
24b4f38f | 5841 | |
02c04afe JH |
5842 | instance = rp->instance; |
5843 | list_for_each_entry(adv_instance, &hdev->adv_instances, list) { | |
5844 | *instance = adv_instance->instance; | |
5845 | instance++; | |
24b4f38f | 5846 | } |
d3d5305b MH |
5847 | |
5848 | hci_dev_unlock(hdev); | |
5849 | ||
5850 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, | |
5851 | MGMT_STATUS_SUCCESS, rp, rp_len); | |
5852 | ||
5853 | kfree(rp); | |
5854 | ||
5855 | return err; | |
5856 | } | |
5857 | ||
4117ed70 | 5858 | static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data, |
b44133ff | 5859 | u8 len, bool is_adv_data) |
24b4f38f | 5860 | { |
4117ed70 | 5861 | u8 max_len = HCI_MAX_AD_LENGTH; |
24b4f38f | 5862 | int i, cur_len; |
b44133ff | 5863 | bool flags_managed = false; |
5507e358 | 5864 | bool tx_power_managed = false; |
24b4f38f | 5865 | |
31a3248d MH |
5866 | if (is_adv_data) { |
5867 | if (adv_flags & (MGMT_ADV_FLAG_DISCOV | | |
5868 | MGMT_ADV_FLAG_LIMITED_DISCOV | | |
5869 | MGMT_ADV_FLAG_MANAGED_FLAGS)) { | |
5870 | flags_managed = true; | |
5871 | max_len -= 3; | |
5872 | } | |
24b4f38f | 5873 | |
31a3248d MH |
5874 | if (adv_flags & MGMT_ADV_FLAG_TX_POWER) { |
5875 | tx_power_managed = true; | |
5876 | max_len -= 3; | |
5877 | } | |
5507e358 AU |
5878 | } |
5879 | ||
4117ed70 | 5880 | if (len > max_len) |
24b4f38f AU |
5881 | return false; |
5882 | ||
4117ed70 AU |
5883 | /* Make sure that the data is correctly formatted. */ |
5884 | for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) { | |
5885 | cur_len = data[i]; | |
24b4f38f | 5886 | |
b44133ff AU |
5887 | if (flags_managed && data[i + 1] == EIR_FLAGS) |
5888 | return false; | |
5889 | ||
5507e358 AU |
5890 | if (tx_power_managed && data[i + 1] == EIR_TX_POWER) |
5891 | return false; | |
5892 | ||
24b4f38f AU |
5893 | /* If the current field length would exceed the total data |
5894 | * length, then it's invalid. | |
5895 | */ | |
4117ed70 | 5896 | if (i + cur_len >= len) |
24b4f38f AU |
5897 | return false; |
5898 | } | |
5899 | ||
5900 | return true; | |
5901 | } | |
5902 | ||
24b4f38f AU |
5903 | static void add_advertising_complete(struct hci_dev *hdev, u8 status, |
5904 | u16 opcode) | |
5905 | { | |
5906 | struct mgmt_pending_cmd *cmd; | |
fffd38bc | 5907 | struct mgmt_cp_add_advertising *cp; |
24b4f38f | 5908 | struct mgmt_rp_add_advertising rp; |
fffd38bc FG |
5909 | struct adv_info *adv_instance, *n; |
5910 | u8 instance; | |
24b4f38f AU |
5911 | |
5912 | BT_DBG("status %d", status); | |
5913 | ||
5914 | hci_dev_lock(hdev); | |
5915 | ||
5916 | cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev); | |
5917 | ||
fffd38bc FG |
5918 | list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) { |
5919 | if (!adv_instance->pending) | |
5920 | continue; | |
5921 | ||
5922 | if (!status) { | |
5923 | adv_instance->pending = false; | |
5924 | continue; | |
5925 | } | |
5926 | ||
5927 | instance = adv_instance->instance; | |
5928 | ||
5929 | if (hdev->cur_adv_instance == instance) | |
5930 | cancel_adv_timeout(hdev); | |
5931 | ||
5932 | hci_remove_adv_instance(hdev, instance); | |
f2252570 | 5933 | mgmt_advertising_removed(cmd ? cmd->sk : NULL, hdev, instance); |
24b4f38f AU |
5934 | } |
5935 | ||
5936 | if (!cmd) | |
5937 | goto unlock; | |
5938 | ||
fffd38bc FG |
5939 | cp = cmd->param; |
5940 | rp.instance = cp->instance; | |
24b4f38f AU |
5941 | |
5942 | if (status) | |
5943 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, | |
5944 | mgmt_status(status)); | |
5945 | else | |
5946 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, | |
5947 | mgmt_status(status), &rp, sizeof(rp)); | |
5948 | ||
5949 | mgmt_pending_remove(cmd); | |
5950 | ||
5951 | unlock: | |
5952 | hci_dev_unlock(hdev); | |
5953 | } | |
5954 | ||
5955 | static int add_advertising(struct sock *sk, struct hci_dev *hdev, | |
5956 | void *data, u16 data_len) | |
5957 | { | |
5958 | struct mgmt_cp_add_advertising *cp = data; | |
5959 | struct mgmt_rp_add_advertising rp; | |
5960 | u32 flags; | |
089fa8c0 | 5961 | u32 supported_flags; |
24b4f38f | 5962 | u8 status; |
fffd38bc FG |
5963 | u16 timeout, duration; |
5964 | unsigned int prev_instance_cnt = hdev->adv_instance_cnt; | |
5965 | u8 schedule_instance = 0; | |
5966 | struct adv_info *next_instance; | |
24b4f38f AU |
5967 | int err; |
5968 | struct mgmt_pending_cmd *cmd; | |
5969 | struct hci_request req; | |
5970 | ||
5971 | BT_DBG("%s", hdev->name); | |
5972 | ||
5973 | status = mgmt_le_support(hdev); | |
5974 | if (status) | |
5975 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, | |
5976 | status); | |
5977 | ||
ceff86af MH |
5978 | if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES) |
5979 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, | |
5980 | MGMT_STATUS_INVALID_PARAMS); | |
5981 | ||
24b4f38f | 5982 | flags = __le32_to_cpu(cp->flags); |
912098a6 | 5983 | timeout = __le16_to_cpu(cp->timeout); |
fffd38bc | 5984 | duration = __le16_to_cpu(cp->duration); |
24b4f38f | 5985 | |
fffd38bc FG |
5986 | /* The current implementation only supports a subset of the specified |
5987 | * flags. | |
089fa8c0 AU |
5988 | */ |
5989 | supported_flags = get_supported_adv_flags(hdev); | |
fffd38bc | 5990 | if (flags & ~supported_flags) |
24b4f38f AU |
5991 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
5992 | MGMT_STATUS_INVALID_PARAMS); | |
5993 | ||
5994 | hci_dev_lock(hdev); | |
5995 | ||
912098a6 AU |
5996 | if (timeout && !hdev_is_powered(hdev)) { |
5997 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, | |
5998 | MGMT_STATUS_REJECTED); | |
5999 | goto unlock; | |
6000 | } | |
6001 | ||
24b4f38f | 6002 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
da929335 | 6003 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
24b4f38f AU |
6004 | pending_find(MGMT_OP_SET_LE, hdev)) { |
6005 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, | |
6006 | MGMT_STATUS_BUSY); | |
6007 | goto unlock; | |
6008 | } | |
6009 | ||
b44133ff | 6010 | if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) || |
4117ed70 | 6011 | !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len, |
b44133ff | 6012 | cp->scan_rsp_len, false)) { |
24b4f38f AU |
6013 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
6014 | MGMT_STATUS_INVALID_PARAMS); | |
6015 | goto unlock; | |
6016 | } | |
6017 | ||
fffd38bc FG |
6018 | err = hci_add_adv_instance(hdev, cp->instance, flags, |
6019 | cp->adv_data_len, cp->data, | |
6020 | cp->scan_rsp_len, | |
6021 | cp->data + cp->adv_data_len, | |
6022 | timeout, duration); | |
6023 | if (err < 0) { | |
6024 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, | |
6025 | MGMT_STATUS_FAILED); | |
6026 | goto unlock; | |
6027 | } | |
24b4f38f | 6028 | |
fffd38bc FG |
6029 | /* Only trigger an advertising added event if a new instance was |
6030 | * actually added. | |
6031 | */ | |
6032 | if (hdev->adv_instance_cnt > prev_instance_cnt) | |
f2252570 | 6033 | mgmt_advertising_added(sk, hdev, cp->instance); |
912098a6 | 6034 | |
fffd38bc FG |
6035 | if (hdev->cur_adv_instance == cp->instance) { |
6036 | /* If the currently advertised instance is being changed then | |
6037 | * cancel the current advertising and schedule the next | |
6038 | * instance. If there is only one instance then the overridden | |
6039 | * advertising data will be visible right away. | |
6040 | */ | |
6041 | cancel_adv_timeout(hdev); | |
912098a6 | 6042 | |
fffd38bc FG |
6043 | next_instance = hci_get_next_instance(hdev, cp->instance); |
6044 | if (next_instance) | |
6045 | schedule_instance = next_instance->instance; | |
6046 | } else if (!hdev->adv_instance_timeout) { | |
6047 | /* Immediately advertise the new instance if no other | |
6048 | * instance is currently being advertised. | |
6049 | */ | |
6050 | schedule_instance = cp->instance; | |
6051 | } | |
24b4f38f | 6052 | |
fffd38bc FG |
6053 | /* If the HCI_ADVERTISING flag is set or the device isn't powered or |
6054 | * there is no instance to be advertised then we have no HCI | |
6055 | * communication to make. Simply return. | |
24b4f38f AU |
6056 | */ |
6057 | if (!hdev_is_powered(hdev) || | |
fffd38bc FG |
6058 | hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
6059 | !schedule_instance) { | |
6060 | rp.instance = cp->instance; | |
24b4f38f AU |
6061 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
6062 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); | |
6063 | goto unlock; | |
6064 | } | |
6065 | ||
6066 | /* We're good to go, update advertising data, parameters, and start | |
6067 | * advertising. | |
6068 | */ | |
6069 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data, | |
6070 | data_len); | |
6071 | if (!cmd) { | |
6072 | err = -ENOMEM; | |
6073 | goto unlock; | |
6074 | } | |
6075 | ||
6076 | hci_req_init(&req, hdev); | |
6077 | ||
f2252570 | 6078 | err = __hci_req_schedule_adv_instance(&req, schedule_instance, true); |
fffd38bc FG |
6079 | |
6080 | if (!err) | |
6081 | err = hci_req_run(&req, add_advertising_complete); | |
24b4f38f | 6082 | |
24b4f38f AU |
6083 | if (err < 0) |
6084 | mgmt_pending_remove(cmd); | |
6085 | ||
6086 | unlock: | |
6087 | hci_dev_unlock(hdev); | |
6088 | ||
6089 | return err; | |
6090 | } | |
6091 | ||
da929335 AU |
6092 | static void remove_advertising_complete(struct hci_dev *hdev, u8 status, |
6093 | u16 opcode) | |
6094 | { | |
6095 | struct mgmt_pending_cmd *cmd; | |
01948331 | 6096 | struct mgmt_cp_remove_advertising *cp; |
da929335 AU |
6097 | struct mgmt_rp_remove_advertising rp; |
6098 | ||
6099 | BT_DBG("status %d", status); | |
6100 | ||
6101 | hci_dev_lock(hdev); | |
6102 | ||
6103 | /* A failure status here only means that we failed to disable | |
6104 | * advertising. Otherwise, the advertising instance has been removed, | |
6105 | * so report success. | |
6106 | */ | |
6107 | cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev); | |
6108 | if (!cmd) | |
6109 | goto unlock; | |
6110 | ||
01948331 FG |
6111 | cp = cmd->param; |
6112 | rp.instance = cp->instance; | |
da929335 AU |
6113 | |
6114 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS, | |
6115 | &rp, sizeof(rp)); | |
6116 | mgmt_pending_remove(cmd); | |
6117 | ||
6118 | unlock: | |
6119 | hci_dev_unlock(hdev); | |
6120 | } | |
6121 | ||
6122 | static int remove_advertising(struct sock *sk, struct hci_dev *hdev, | |
6123 | void *data, u16 data_len) | |
6124 | { | |
6125 | struct mgmt_cp_remove_advertising *cp = data; | |
6126 | struct mgmt_rp_remove_advertising rp; | |
da929335 AU |
6127 | struct mgmt_pending_cmd *cmd; |
6128 | struct hci_request req; | |
952497b1 | 6129 | int err; |
da929335 AU |
6130 | |
6131 | BT_DBG("%s", hdev->name); | |
6132 | ||
da929335 AU |
6133 | hci_dev_lock(hdev); |
6134 | ||
952497b1 | 6135 | if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) { |
01948331 FG |
6136 | err = mgmt_cmd_status(sk, hdev->id, |
6137 | MGMT_OP_REMOVE_ADVERTISING, | |
6138 | MGMT_STATUS_INVALID_PARAMS); | |
6139 | goto unlock; | |
6140 | } | |
6141 | ||
da929335 AU |
6142 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
6143 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || | |
6144 | pending_find(MGMT_OP_SET_LE, hdev)) { | |
6145 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, | |
6146 | MGMT_STATUS_BUSY); | |
6147 | goto unlock; | |
6148 | } | |
6149 | ||
17fd08ff | 6150 | if (list_empty(&hdev->adv_instances)) { |
da929335 AU |
6151 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
6152 | MGMT_STATUS_INVALID_PARAMS); | |
6153 | goto unlock; | |
6154 | } | |
6155 | ||
01948331 | 6156 | hci_req_init(&req, hdev); |
da929335 | 6157 | |
f2252570 | 6158 | hci_req_clear_adv_instance(hdev, &req, cp->instance, true); |
da929335 | 6159 | |
01948331 | 6160 | if (list_empty(&hdev->adv_instances)) |
f2252570 | 6161 | __hci_req_disable_advertising(&req); |
da929335 | 6162 | |
01948331 FG |
6163 | /* If no HCI commands have been collected so far or the HCI_ADVERTISING |
6164 | * flag is set or the device isn't powered then we have no HCI | |
6165 | * communication to make. Simply return. | |
da929335 | 6166 | */ |
01948331 FG |
6167 | if (skb_queue_empty(&req.cmd_q) || |
6168 | !hdev_is_powered(hdev) || | |
da929335 | 6169 | hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
01948331 | 6170 | rp.instance = cp->instance; |
da929335 AU |
6171 | err = mgmt_cmd_complete(sk, hdev->id, |
6172 | MGMT_OP_REMOVE_ADVERTISING, | |
6173 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); | |
6174 | goto unlock; | |
6175 | } | |
6176 | ||
6177 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data, | |
6178 | data_len); | |
6179 | if (!cmd) { | |
6180 | err = -ENOMEM; | |
6181 | goto unlock; | |
6182 | } | |
6183 | ||
da929335 AU |
6184 | err = hci_req_run(&req, remove_advertising_complete); |
6185 | if (err < 0) | |
6186 | mgmt_pending_remove(cmd); | |
6187 | ||
6188 | unlock: | |
6189 | hci_dev_unlock(hdev); | |
6190 | ||
6191 | return err; | |
6192 | } | |
6193 | ||
40b25fe5 MH |
6194 | static u8 tlv_data_max_len(u32 adv_flags, bool is_adv_data) |
6195 | { | |
6196 | u8 max_len = HCI_MAX_AD_LENGTH; | |
6197 | ||
6198 | if (is_adv_data) { | |
6199 | if (adv_flags & (MGMT_ADV_FLAG_DISCOV | | |
6200 | MGMT_ADV_FLAG_LIMITED_DISCOV | | |
6201 | MGMT_ADV_FLAG_MANAGED_FLAGS)) | |
6202 | max_len -= 3; | |
6203 | ||
6204 | if (adv_flags & MGMT_ADV_FLAG_TX_POWER) | |
6205 | max_len -= 3; | |
6206 | } | |
6207 | ||
6208 | return max_len; | |
6209 | } | |
6210 | ||
6211 | static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev, | |
6212 | void *data, u16 data_len) | |
6213 | { | |
6214 | struct mgmt_cp_get_adv_size_info *cp = data; | |
6215 | struct mgmt_rp_get_adv_size_info rp; | |
6216 | u32 flags, supported_flags; | |
6217 | int err; | |
6218 | ||
6219 | BT_DBG("%s", hdev->name); | |
6220 | ||
6221 | if (!lmp_le_capable(hdev)) | |
6222 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, | |
6223 | MGMT_STATUS_REJECTED); | |
6224 | ||
6225 | if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES) | |
6226 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, | |
6227 | MGMT_STATUS_INVALID_PARAMS); | |
6228 | ||
6229 | flags = __le32_to_cpu(cp->flags); | |
6230 | ||
6231 | /* The current implementation only supports a subset of the specified | |
6232 | * flags. | |
6233 | */ | |
6234 | supported_flags = get_supported_adv_flags(hdev); | |
6235 | if (flags & ~supported_flags) | |
6236 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, | |
6237 | MGMT_STATUS_INVALID_PARAMS); | |
6238 | ||
6239 | rp.instance = cp->instance; | |
6240 | rp.flags = cp->flags; | |
6241 | rp.max_adv_data_len = tlv_data_max_len(flags, true); | |
6242 | rp.max_scan_rsp_len = tlv_data_max_len(flags, false); | |
6243 | ||
6244 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, | |
6245 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); | |
6246 | ||
6247 | return err; | |
6248 | } | |
6249 | ||
6d785aa3 | 6250 | static const struct hci_mgmt_handler mgmt_handlers[] = { |
0f4e68cf | 6251 | { NULL }, /* 0x0000 (no command) */ |
b9a245fb | 6252 | { read_version, MGMT_READ_VERSION_SIZE, |
c91041dc MH |
6253 | HCI_MGMT_NO_HDEV | |
6254 | HCI_MGMT_UNTRUSTED }, | |
b9a245fb | 6255 | { read_commands, MGMT_READ_COMMANDS_SIZE, |
c91041dc MH |
6256 | HCI_MGMT_NO_HDEV | |
6257 | HCI_MGMT_UNTRUSTED }, | |
b9a245fb | 6258 | { read_index_list, MGMT_READ_INDEX_LIST_SIZE, |
c91041dc MH |
6259 | HCI_MGMT_NO_HDEV | |
6260 | HCI_MGMT_UNTRUSTED }, | |
6261 | { read_controller_info, MGMT_READ_INFO_SIZE, | |
6262 | HCI_MGMT_UNTRUSTED }, | |
7aea8616 MH |
6263 | { set_powered, MGMT_SETTING_SIZE }, |
6264 | { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE }, | |
6265 | { set_connectable, MGMT_SETTING_SIZE }, | |
6266 | { set_fast_connectable, MGMT_SETTING_SIZE }, | |
6267 | { set_bondable, MGMT_SETTING_SIZE }, | |
6268 | { set_link_security, MGMT_SETTING_SIZE }, | |
6269 | { set_ssp, MGMT_SETTING_SIZE }, | |
6270 | { set_hs, MGMT_SETTING_SIZE }, | |
6271 | { set_le, MGMT_SETTING_SIZE }, | |
6272 | { set_dev_class, MGMT_SET_DEV_CLASS_SIZE }, | |
6273 | { set_local_name, MGMT_SET_LOCAL_NAME_SIZE }, | |
6274 | { add_uuid, MGMT_ADD_UUID_SIZE }, | |
6275 | { remove_uuid, MGMT_REMOVE_UUID_SIZE }, | |
b9a245fb JH |
6276 | { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE, |
6277 | HCI_MGMT_VAR_LEN }, | |
6278 | { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE, | |
6279 | HCI_MGMT_VAR_LEN }, | |
7aea8616 MH |
6280 | { disconnect, MGMT_DISCONNECT_SIZE }, |
6281 | { get_connections, MGMT_GET_CONNECTIONS_SIZE }, | |
6282 | { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE }, | |
6283 | { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE }, | |
6284 | { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE }, | |
6285 | { pair_device, MGMT_PAIR_DEVICE_SIZE }, | |
6286 | { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE }, | |
6287 | { unpair_device, MGMT_UNPAIR_DEVICE_SIZE }, | |
6288 | { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE }, | |
6289 | { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, | |
6290 | { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE }, | |
6291 | { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, | |
b9a245fb JH |
6292 | { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
6293 | { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE, | |
6294 | HCI_MGMT_VAR_LEN }, | |
7aea8616 MH |
6295 | { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
6296 | { start_discovery, MGMT_START_DISCOVERY_SIZE }, | |
6297 | { stop_discovery, MGMT_STOP_DISCOVERY_SIZE }, | |
6298 | { confirm_name, MGMT_CONFIRM_NAME_SIZE }, | |
6299 | { block_device, MGMT_BLOCK_DEVICE_SIZE }, | |
6300 | { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE }, | |
6301 | { set_device_id, MGMT_SET_DEVICE_ID_SIZE }, | |
6302 | { set_advertising, MGMT_SETTING_SIZE }, | |
6303 | { set_bredr, MGMT_SETTING_SIZE }, | |
6304 | { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE }, | |
6305 | { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE }, | |
6306 | { set_secure_conn, MGMT_SETTING_SIZE }, | |
6307 | { set_debug_keys, MGMT_SETTING_SIZE }, | |
6308 | { set_privacy, MGMT_SET_PRIVACY_SIZE }, | |
b9a245fb JH |
6309 | { load_irks, MGMT_LOAD_IRKS_SIZE, |
6310 | HCI_MGMT_VAR_LEN }, | |
7aea8616 MH |
6311 | { get_conn_info, MGMT_GET_CONN_INFO_SIZE }, |
6312 | { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE }, | |
6313 | { add_device, MGMT_ADD_DEVICE_SIZE }, | |
6314 | { remove_device, MGMT_REMOVE_DEVICE_SIZE }, | |
b9a245fb JH |
6315 | { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE, |
6316 | HCI_MGMT_VAR_LEN }, | |
6317 | { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE, | |
c91041dc MH |
6318 | HCI_MGMT_NO_HDEV | |
6319 | HCI_MGMT_UNTRUSTED }, | |
b9a245fb | 6320 | { read_config_info, MGMT_READ_CONFIG_INFO_SIZE, |
c91041dc MH |
6321 | HCI_MGMT_UNCONFIGURED | |
6322 | HCI_MGMT_UNTRUSTED }, | |
b9a245fb JH |
6323 | { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE, |
6324 | HCI_MGMT_UNCONFIGURED }, | |
6325 | { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE, | |
6326 | HCI_MGMT_UNCONFIGURED }, | |
6327 | { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE, | |
6328 | HCI_MGMT_VAR_LEN }, | |
4f0f155c | 6329 | { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE }, |
96f1474a | 6330 | { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE, |
c91041dc MH |
6331 | HCI_MGMT_NO_HDEV | |
6332 | HCI_MGMT_UNTRUSTED }, | |
d3d5305b | 6333 | { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE }, |
24b4f38f AU |
6334 | { add_advertising, MGMT_ADD_ADVERTISING_SIZE, |
6335 | HCI_MGMT_VAR_LEN }, | |
da929335 | 6336 | { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE }, |
40b25fe5 | 6337 | { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE }, |
78b781ca | 6338 | { start_limited_discovery, MGMT_START_DISCOVERY_SIZE }, |
0f4e68cf JH |
6339 | }; |
6340 | ||
bf6b56db | 6341 | void mgmt_index_added(struct hci_dev *hdev) |
c71e97bf | 6342 | { |
ced85549 | 6343 | struct mgmt_ev_ext_index ev; |
bb4b2a9a | 6344 | |
0602a8ad MH |
6345 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
6346 | return; | |
6347 | ||
f9207338 MH |
6348 | switch (hdev->dev_type) { |
6349 | case HCI_BREDR: | |
6350 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { | |
6351 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, | |
6352 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); | |
ced85549 | 6353 | ev.type = 0x01; |
f9207338 MH |
6354 | } else { |
6355 | mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, | |
6356 | HCI_MGMT_INDEX_EVENTS); | |
ced85549 | 6357 | ev.type = 0x00; |
f9207338 MH |
6358 | } |
6359 | break; | |
ced85549 MH |
6360 | case HCI_AMP: |
6361 | ev.type = 0x02; | |
6362 | break; | |
6363 | default: | |
6364 | return; | |
f9207338 | 6365 | } |
ced85549 MH |
6366 | |
6367 | ev.bus = hdev->bus; | |
6368 | ||
6369 | mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev), | |
6370 | HCI_MGMT_EXT_INDEX_EVENTS); | |
c71e97bf JH |
6371 | } |
6372 | ||
bf6b56db | 6373 | void mgmt_index_removed(struct hci_dev *hdev) |
c71e97bf | 6374 | { |
ced85549 | 6375 | struct mgmt_ev_ext_index ev; |
5f159032 | 6376 | u8 status = MGMT_STATUS_INVALID_INDEX; |
b24752fe | 6377 | |
0602a8ad MH |
6378 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
6379 | return; | |
6380 | ||
f9207338 MH |
6381 | switch (hdev->dev_type) { |
6382 | case HCI_BREDR: | |
6383 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); | |
b24752fe | 6384 | |
f9207338 MH |
6385 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
6386 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, | |
6387 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); | |
ced85549 | 6388 | ev.type = 0x01; |
f9207338 MH |
6389 | } else { |
6390 | mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, | |
6391 | HCI_MGMT_INDEX_EVENTS); | |
ced85549 | 6392 | ev.type = 0x00; |
f9207338 MH |
6393 | } |
6394 | break; | |
ced85549 MH |
6395 | case HCI_AMP: |
6396 | ev.type = 0x02; | |
6397 | break; | |
6398 | default: | |
6399 | return; | |
f9207338 | 6400 | } |
ced85549 MH |
6401 | |
6402 | ev.bus = hdev->bus; | |
6403 | ||
6404 | mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev), | |
6405 | HCI_MGMT_EXT_INDEX_EVENTS); | |
eec8d2bc JH |
6406 | } |
6407 | ||
6046dc3e | 6408 | /* This function requires the caller holds hdev->lock */ |
af02dd44 | 6409 | static void restart_le_actions(struct hci_dev *hdev) |
6046dc3e AG |
6410 | { |
6411 | struct hci_conn_params *p; | |
6412 | ||
6413 | list_for_each_entry(p, &hdev->le_conn_params, list) { | |
d7347f3c JH |
6414 | /* Needed for AUTO_OFF case where might not "really" |
6415 | * have been powered off. | |
6416 | */ | |
6417 | list_del_init(&p->action); | |
6418 | ||
6419 | switch (p->auto_connect) { | |
4b9e7e75 | 6420 | case HCI_AUTO_CONN_DIRECT: |
d7347f3c JH |
6421 | case HCI_AUTO_CONN_ALWAYS: |
6422 | list_add(&p->action, &hdev->pend_le_conns); | |
6423 | break; | |
6424 | case HCI_AUTO_CONN_REPORT: | |
6425 | list_add(&p->action, &hdev->pend_le_reports); | |
6426 | break; | |
6427 | default: | |
6428 | break; | |
c83ed19d | 6429 | } |
6046dc3e AG |
6430 | } |
6431 | } | |
6432 | ||
2ff13894 | 6433 | void mgmt_power_on(struct hci_dev *hdev, int err) |
229ab39c JH |
6434 | { |
6435 | struct cmd_lookup match = { NULL, hdev }; | |
6436 | ||
2ff13894 | 6437 | BT_DBG("err %d", err); |
229ab39c | 6438 | |
2ff13894 JH |
6439 | hci_dev_lock(hdev); |
6440 | ||
6441 | if (!err) { | |
af02dd44 JH |
6442 | restart_le_actions(hdev); |
6443 | hci_update_background_scan(hdev); | |
162a3bac MH |
6444 | } |
6445 | ||
229ab39c JH |
6446 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
6447 | ||
6448 | new_settings(hdev, match.sk); | |
6449 | ||
229ab39c JH |
6450 | if (match.sk) |
6451 | sock_put(match.sk); | |
229ab39c | 6452 | |
2ff13894 | 6453 | hci_dev_unlock(hdev); |
70da6243 | 6454 | } |
562fcc24 | 6455 | |
2ff13894 | 6456 | void __mgmt_power_off(struct hci_dev *hdev) |
70da6243 JH |
6457 | { |
6458 | struct cmd_lookup match = { NULL, hdev }; | |
9845904f | 6459 | u8 status, zero_cod[] = { 0, 0, 0 }; |
b24752fe | 6460 | |
229ab39c | 6461 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
9845904f JH |
6462 | |
6463 | /* If the power off is because of hdev unregistration let | |
6464 | * use the appropriate INVALID_INDEX status. Otherwise use | |
6465 | * NOT_POWERED. We cover both scenarios here since later in | |
6466 | * mgmt_index_removed() any hci_conn callbacks will have already | |
6467 | * been triggered, potentially causing misleading DISCONNECTED | |
6468 | * status responses. | |
6469 | */ | |
d7a5a11d | 6470 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) |
9845904f JH |
6471 | status = MGMT_STATUS_INVALID_INDEX; |
6472 | else | |
6473 | status = MGMT_STATUS_NOT_POWERED; | |
6474 | ||
6475 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); | |
229ab39c JH |
6476 | |
6477 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) | |
f6b7712e MH |
6478 | mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
6479 | zero_cod, sizeof(zero_cod), NULL); | |
229ab39c | 6480 | |
2ff13894 | 6481 | new_settings(hdev, match.sk); |
eec8d2bc JH |
6482 | |
6483 | if (match.sk) | |
6484 | sock_put(match.sk); | |
5add6af8 | 6485 | } |
73f22f62 | 6486 | |
3eec705e | 6487 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
96570ffc | 6488 | { |
3b0602cd | 6489 | struct mgmt_pending_cmd *cmd; |
96570ffc JH |
6490 | u8 status; |
6491 | ||
333ae95d | 6492 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
96570ffc | 6493 | if (!cmd) |
3eec705e | 6494 | return; |
96570ffc JH |
6495 | |
6496 | if (err == -ERFKILL) | |
6497 | status = MGMT_STATUS_RFKILLED; | |
6498 | else | |
6499 | status = MGMT_STATUS_FAILED; | |
6500 | ||
a69e8375 | 6501 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
96570ffc JH |
6502 | |
6503 | mgmt_pending_remove(cmd); | |
96570ffc JH |
6504 | } |
6505 | ||
dc4a5ee2 MH |
6506 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
6507 | bool persistent) | |
55ed8ca1 | 6508 | { |
86742e1e | 6509 | struct mgmt_ev_new_link_key ev; |
55ed8ca1 | 6510 | |
a492cd52 | 6511 | memset(&ev, 0, sizeof(ev)); |
55ed8ca1 | 6512 | |
a492cd52 | 6513 | ev.store_hint = persistent; |
d753fdc4 | 6514 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
591f47f3 | 6515 | ev.key.addr.type = BDADDR_BREDR; |
a492cd52 | 6516 | ev.key.type = key->type; |
9b3b4460 | 6517 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
a492cd52 | 6518 | ev.key.pin_len = key->pin_len; |
55ed8ca1 | 6519 | |
dc4a5ee2 | 6520 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
55ed8ca1 | 6521 | } |
f7520543 | 6522 | |
d7b25450 JH |
6523 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
6524 | { | |
23fb8de3 JH |
6525 | switch (ltk->type) { |
6526 | case SMP_LTK: | |
6527 | case SMP_LTK_SLAVE: | |
6528 | if (ltk->authenticated) | |
6529 | return MGMT_LTK_AUTHENTICATED; | |
6530 | return MGMT_LTK_UNAUTHENTICATED; | |
6531 | case SMP_LTK_P256: | |
6532 | if (ltk->authenticated) | |
6533 | return MGMT_LTK_P256_AUTH; | |
6534 | return MGMT_LTK_P256_UNAUTH; | |
6535 | case SMP_LTK_P256_DEBUG: | |
6536 | return MGMT_LTK_P256_DEBUG; | |
6537 | } | |
d7b25450 JH |
6538 | |
6539 | return MGMT_LTK_UNAUTHENTICATED; | |
6540 | } | |
6541 | ||
53ac6ab6 | 6542 | void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent) |
346af67b VCG |
6543 | { |
6544 | struct mgmt_ev_new_long_term_key ev; | |
6545 | ||
6546 | memset(&ev, 0, sizeof(ev)); | |
6547 | ||
5192d301 | 6548 | /* Devices using resolvable or non-resolvable random addresses |
f72186d2 | 6549 | * without providing an identity resolving key don't require |
5192d301 MH |
6550 | * to store long term keys. Their addresses will change the |
6551 | * next time around. | |
6552 | * | |
6553 | * Only when a remote device provides an identity address | |
6554 | * make sure the long term key is stored. If the remote | |
6555 | * identity is known, the long term keys are internally | |
6556 | * mapped to the identity address. So allow static random | |
6557 | * and public addresses here. | |
6558 | */ | |
ba74b666 JH |
6559 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
6560 | (key->bdaddr.b[5] & 0xc0) != 0xc0) | |
6561 | ev.store_hint = 0x00; | |
6562 | else | |
53ac6ab6 | 6563 | ev.store_hint = persistent; |
ba74b666 | 6564 | |
346af67b | 6565 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
57c1477c | 6566 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
d7b25450 | 6567 | ev.key.type = mgmt_ltk_type(key); |
346af67b VCG |
6568 | ev.key.enc_size = key->enc_size; |
6569 | ev.key.ediv = key->ediv; | |
fe39c7b2 | 6570 | ev.key.rand = key->rand; |
346af67b | 6571 | |
2ceba539 | 6572 | if (key->type == SMP_LTK) |
346af67b VCG |
6573 | ev.key.master = 1; |
6574 | ||
1fc62c52 JH |
6575 | /* Make sure we copy only the significant bytes based on the |
6576 | * encryption key size, and set the rest of the value to zeroes. | |
6577 | */ | |
cb92205b | 6578 | memcpy(ev.key.val, key->val, key->enc_size); |
1fc62c52 JH |
6579 | memset(ev.key.val + key->enc_size, 0, |
6580 | sizeof(ev.key.val) - key->enc_size); | |
346af67b | 6581 | |
083368f7 | 6582 | mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL); |
346af67b VCG |
6583 | } |
6584 | ||
cad20c27 | 6585 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent) |
95fbac8a JH |
6586 | { |
6587 | struct mgmt_ev_new_irk ev; | |
6588 | ||
6589 | memset(&ev, 0, sizeof(ev)); | |
6590 | ||
cad20c27 | 6591 | ev.store_hint = persistent; |
bab6d1e5 | 6592 | |
95fbac8a JH |
6593 | bacpy(&ev.rpa, &irk->rpa); |
6594 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); | |
6595 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); | |
6596 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); | |
6597 | ||
6598 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); | |
6599 | } | |
6600 | ||
53ac6ab6 MH |
6601 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
6602 | bool persistent) | |
7ee4ea36 MH |
6603 | { |
6604 | struct mgmt_ev_new_csrk ev; | |
6605 | ||
6606 | memset(&ev, 0, sizeof(ev)); | |
6607 | ||
6608 | /* Devices using resolvable or non-resolvable random addresses | |
f72186d2 | 6609 | * without providing an identity resolving key don't require |
7ee4ea36 MH |
6610 | * to store signature resolving keys. Their addresses will change |
6611 | * the next time around. | |
6612 | * | |
6613 | * Only when a remote device provides an identity address | |
6614 | * make sure the signature resolving key is stored. So allow | |
6615 | * static random and public addresses here. | |
6616 | */ | |
6617 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && | |
6618 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) | |
6619 | ev.store_hint = 0x00; | |
6620 | else | |
53ac6ab6 | 6621 | ev.store_hint = persistent; |
7ee4ea36 MH |
6622 | |
6623 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); | |
6624 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); | |
4cd3928a | 6625 | ev.key.type = csrk->type; |
7ee4ea36 MH |
6626 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
6627 | ||
6628 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); | |
6629 | } | |
6630 | ||
ffb5a827 | 6631 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
f4869e2a JH |
6632 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
6633 | u16 max_interval, u16 latency, u16 timeout) | |
ffb5a827 AG |
6634 | { |
6635 | struct mgmt_ev_new_conn_param ev; | |
6636 | ||
c103aea6 JH |
6637 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
6638 | return; | |
6639 | ||
ffb5a827 AG |
6640 | memset(&ev, 0, sizeof(ev)); |
6641 | bacpy(&ev.addr.bdaddr, bdaddr); | |
6642 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); | |
f4869e2a | 6643 | ev.store_hint = store_hint; |
ffb5a827 AG |
6644 | ev.min_interval = cpu_to_le16(min_interval); |
6645 | ev.max_interval = cpu_to_le16(max_interval); | |
6646 | ev.latency = cpu_to_le16(latency); | |
6647 | ev.timeout = cpu_to_le16(timeout); | |
6648 | ||
6649 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); | |
6650 | } | |
6651 | ||
48ec92fa AA |
6652 | void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, |
6653 | u32 flags, u8 *name, u8 name_len) | |
f7520543 | 6654 | { |
b644ba33 JH |
6655 | char buf[512]; |
6656 | struct mgmt_ev_device_connected *ev = (void *) buf; | |
6657 | u16 eir_len = 0; | |
f7520543 | 6658 | |
48ec92fa AA |
6659 | bacpy(&ev->addr.bdaddr, &conn->dst); |
6660 | ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); | |
f7520543 | 6661 | |
c95f0ba7 | 6662 | ev->flags = __cpu_to_le32(flags); |
08c79b61 | 6663 | |
fd45ada9 AA |
6664 | /* We must ensure that the EIR Data fields are ordered and |
6665 | * unique. Keep it simple for now and avoid the problem by not | |
6666 | * adding any BR/EDR data to the LE adv. | |
6667 | */ | |
6668 | if (conn->le_adv_data_len > 0) { | |
6669 | memcpy(&ev->eir[eir_len], | |
6670 | conn->le_adv_data, conn->le_adv_data_len); | |
6671 | eir_len = conn->le_adv_data_len; | |
6672 | } else { | |
6673 | if (name_len > 0) | |
6674 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, | |
6675 | name, name_len); | |
b644ba33 | 6676 | |
ddbea5cf | 6677 | if (memcmp(conn->dev_class, "\0\0\0", 3) != 0) |
fd45ada9 AA |
6678 | eir_len = eir_append_data(ev->eir, eir_len, |
6679 | EIR_CLASS_OF_DEV, | |
6680 | conn->dev_class, 3); | |
6681 | } | |
b644ba33 | 6682 | |
eb55ef07 | 6683 | ev->eir_len = cpu_to_le16(eir_len); |
b644ba33 | 6684 | |
ecd90ae7 MH |
6685 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
6686 | sizeof(*ev) + eir_len, NULL); | |
f7520543 JH |
6687 | } |
6688 | ||
3b0602cd | 6689 | static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data) |
8962ee74 | 6690 | { |
8962ee74 | 6691 | struct sock **sk = data; |
8962ee74 | 6692 | |
f5818c22 | 6693 | cmd->cmd_complete(cmd, 0); |
8962ee74 JH |
6694 | |
6695 | *sk = cmd->sk; | |
6696 | sock_hold(*sk); | |
6697 | ||
a664b5bc | 6698 | mgmt_pending_remove(cmd); |
8962ee74 JH |
6699 | } |
6700 | ||
3b0602cd | 6701 | static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data) |
a8a1d19e | 6702 | { |
b1078ad0 | 6703 | struct hci_dev *hdev = data; |
124f6e35 | 6704 | struct mgmt_cp_unpair_device *cp = cmd->param; |
a8a1d19e | 6705 | |
b1078ad0 JH |
6706 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
6707 | ||
d8b7b1e4 | 6708 | cmd->cmd_complete(cmd, 0); |
a8a1d19e JH |
6709 | mgmt_pending_remove(cmd); |
6710 | } | |
6711 | ||
84c61d92 JH |
6712 | bool mgmt_powering_down(struct hci_dev *hdev) |
6713 | { | |
3b0602cd | 6714 | struct mgmt_pending_cmd *cmd; |
84c61d92 JH |
6715 | struct mgmt_mode *cp; |
6716 | ||
333ae95d | 6717 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
84c61d92 JH |
6718 | if (!cmd) |
6719 | return false; | |
6720 | ||
6721 | cp = cmd->param; | |
6722 | if (!cp->val) | |
6723 | return true; | |
6724 | ||
6725 | return false; | |
6726 | } | |
6727 | ||
9b80ec5e | 6728 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
12d4a3b2 JH |
6729 | u8 link_type, u8 addr_type, u8 reason, |
6730 | bool mgmt_connected) | |
f7520543 | 6731 | { |
f0d6a0ea | 6732 | struct mgmt_ev_device_disconnected ev; |
8962ee74 | 6733 | struct sock *sk = NULL; |
8962ee74 | 6734 | |
84c61d92 JH |
6735 | /* The connection is still in hci_conn_hash so test for 1 |
6736 | * instead of 0 to know if this is the last one. | |
6737 | */ | |
6738 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { | |
6739 | cancel_delayed_work(&hdev->power_off); | |
6740 | queue_work(hdev->req_workqueue, &hdev->power_off.work); | |
8b064a3a JH |
6741 | } |
6742 | ||
12d4a3b2 JH |
6743 | if (!mgmt_connected) |
6744 | return; | |
6745 | ||
57eb776f AG |
6746 | if (link_type != ACL_LINK && link_type != LE_LINK) |
6747 | return; | |
6748 | ||
744cf19e | 6749 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
f7520543 | 6750 | |
f0d6a0ea MA |
6751 | bacpy(&ev.addr.bdaddr, bdaddr); |
6752 | ev.addr.type = link_to_bdaddr(link_type, addr_type); | |
6753 | ev.reason = reason; | |
f7520543 | 6754 | |
9b80ec5e | 6755 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
8962ee74 JH |
6756 | |
6757 | if (sk) | |
d97dcb66 | 6758 | sock_put(sk); |
8962ee74 | 6759 | |
124f6e35 | 6760 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
04124681 | 6761 | hdev); |
8962ee74 JH |
6762 | } |
6763 | ||
7892924c MH |
6764 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
6765 | u8 link_type, u8 addr_type, u8 status) | |
8962ee74 | 6766 | { |
3655bba8 AG |
6767 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
6768 | struct mgmt_cp_disconnect *cp; | |
3b0602cd | 6769 | struct mgmt_pending_cmd *cmd; |
8962ee74 | 6770 | |
36a75f1b JD |
6771 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
6772 | hdev); | |
6773 | ||
333ae95d | 6774 | cmd = pending_find(MGMT_OP_DISCONNECT, hdev); |
8962ee74 | 6775 | if (!cmd) |
7892924c | 6776 | return; |
8962ee74 | 6777 | |
3655bba8 AG |
6778 | cp = cmd->param; |
6779 | ||
6780 | if (bacmp(bdaddr, &cp->addr.bdaddr)) | |
6781 | return; | |
6782 | ||
6783 | if (cp->addr.type != bdaddr_type) | |
6784 | return; | |
6785 | ||
f5818c22 | 6786 | cmd->cmd_complete(cmd, mgmt_status(status)); |
a664b5bc | 6787 | mgmt_pending_remove(cmd); |
f7520543 | 6788 | } |
17d5c04c | 6789 | |
445608d0 MH |
6790 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
6791 | u8 addr_type, u8 status) | |
17d5c04c JH |
6792 | { |
6793 | struct mgmt_ev_connect_failed ev; | |
c9910d0f | 6794 | |
84c61d92 JH |
6795 | /* The connection is still in hci_conn_hash so test for 1 |
6796 | * instead of 0 to know if this is the last one. | |
6797 | */ | |
6798 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { | |
6799 | cancel_delayed_work(&hdev->power_off); | |
6800 | queue_work(hdev->req_workqueue, &hdev->power_off.work); | |
c9910d0f | 6801 | } |
17d5c04c | 6802 | |
4c659c39 | 6803 | bacpy(&ev.addr.bdaddr, bdaddr); |
57c1477c | 6804 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
ca69b795 | 6805 | ev.status = mgmt_status(status); |
17d5c04c | 6806 | |
445608d0 | 6807 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
17d5c04c | 6808 | } |
980e1a53 | 6809 | |
ce0e4a0d | 6810 | void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure) |
980e1a53 JH |
6811 | { |
6812 | struct mgmt_ev_pin_code_request ev; | |
6813 | ||
d8457698 | 6814 | bacpy(&ev.addr.bdaddr, bdaddr); |
591f47f3 | 6815 | ev.addr.type = BDADDR_BREDR; |
a770bb5a | 6816 | ev.secure = secure; |
980e1a53 | 6817 | |
ce0e4a0d | 6818 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
980e1a53 JH |
6819 | } |
6820 | ||
e669cf80 MH |
6821 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
6822 | u8 status) | |
980e1a53 | 6823 | { |
3b0602cd | 6824 | struct mgmt_pending_cmd *cmd; |
980e1a53 | 6825 | |
333ae95d | 6826 | cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
980e1a53 | 6827 | if (!cmd) |
e669cf80 | 6828 | return; |
980e1a53 | 6829 | |
7776d1d8 | 6830 | cmd->cmd_complete(cmd, mgmt_status(status)); |
a664b5bc | 6831 | mgmt_pending_remove(cmd); |
980e1a53 JH |
6832 | } |
6833 | ||
3eb38528 MH |
6834 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
6835 | u8 status) | |
980e1a53 | 6836 | { |
3b0602cd | 6837 | struct mgmt_pending_cmd *cmd; |
980e1a53 | 6838 | |
333ae95d | 6839 | cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
980e1a53 | 6840 | if (!cmd) |
3eb38528 | 6841 | return; |
980e1a53 | 6842 | |
7776d1d8 | 6843 | cmd->cmd_complete(cmd, mgmt_status(status)); |
a664b5bc | 6844 | mgmt_pending_remove(cmd); |
980e1a53 | 6845 | } |
a5c29683 | 6846 | |
744cf19e | 6847 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
39adbffe | 6848 | u8 link_type, u8 addr_type, u32 value, |
04124681 | 6849 | u8 confirm_hint) |
a5c29683 JH |
6850 | { |
6851 | struct mgmt_ev_user_confirm_request ev; | |
6852 | ||
744cf19e | 6853 | BT_DBG("%s", hdev->name); |
a5c29683 | 6854 | |
272d90df | 6855 | bacpy(&ev.addr.bdaddr, bdaddr); |
57c1477c | 6856 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
55bc1a37 | 6857 | ev.confirm_hint = confirm_hint; |
39adbffe | 6858 | ev.value = cpu_to_le32(value); |
a5c29683 | 6859 | |
744cf19e | 6860 | return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev), |
04124681 | 6861 | NULL); |
a5c29683 JH |
6862 | } |
6863 | ||
272d90df | 6864 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
8ce8e2b5 | 6865 | u8 link_type, u8 addr_type) |
604086b7 BG |
6866 | { |
6867 | struct mgmt_ev_user_passkey_request ev; | |
6868 | ||
6869 | BT_DBG("%s", hdev->name); | |
6870 | ||
272d90df | 6871 | bacpy(&ev.addr.bdaddr, bdaddr); |
57c1477c | 6872 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
604086b7 BG |
6873 | |
6874 | return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev), | |
04124681 | 6875 | NULL); |
604086b7 BG |
6876 | } |
6877 | ||
0df4c185 | 6878 | static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
8ce8e2b5 GP |
6879 | u8 link_type, u8 addr_type, u8 status, |
6880 | u8 opcode) | |
a5c29683 | 6881 | { |
3b0602cd | 6882 | struct mgmt_pending_cmd *cmd; |
a5c29683 | 6883 | |
333ae95d | 6884 | cmd = pending_find(opcode, hdev); |
a5c29683 JH |
6885 | if (!cmd) |
6886 | return -ENOENT; | |
6887 | ||
7776d1d8 | 6888 | cmd->cmd_complete(cmd, mgmt_status(status)); |
a664b5bc | 6889 | mgmt_pending_remove(cmd); |
a5c29683 | 6890 | |
7776d1d8 | 6891 | return 0; |
a5c29683 JH |
6892 | } |
6893 | ||
744cf19e | 6894 | int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
04124681 | 6895 | u8 link_type, u8 addr_type, u8 status) |
a5c29683 | 6896 | { |
272d90df | 6897 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
04124681 | 6898 | status, MGMT_OP_USER_CONFIRM_REPLY); |
a5c29683 JH |
6899 | } |
6900 | ||
272d90df | 6901 | int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
04124681 | 6902 | u8 link_type, u8 addr_type, u8 status) |
a5c29683 | 6903 | { |
272d90df | 6904 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
8fc9ced3 GP |
6905 | status, |
6906 | MGMT_OP_USER_CONFIRM_NEG_REPLY); | |
a5c29683 | 6907 | } |
2a611692 | 6908 | |
604086b7 | 6909 | int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
04124681 | 6910 | u8 link_type, u8 addr_type, u8 status) |
604086b7 | 6911 | { |
272d90df | 6912 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
04124681 | 6913 | status, MGMT_OP_USER_PASSKEY_REPLY); |
604086b7 BG |
6914 | } |
6915 | ||
272d90df | 6916 | int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
04124681 | 6917 | u8 link_type, u8 addr_type, u8 status) |
604086b7 | 6918 | { |
272d90df | 6919 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
8fc9ced3 GP |
6920 | status, |
6921 | MGMT_OP_USER_PASSKEY_NEG_REPLY); | |
604086b7 BG |
6922 | } |
6923 | ||
92a25256 JH |
6924 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
6925 | u8 link_type, u8 addr_type, u32 passkey, | |
6926 | u8 entered) | |
6927 | { | |
6928 | struct mgmt_ev_passkey_notify ev; | |
6929 | ||
6930 | BT_DBG("%s", hdev->name); | |
6931 | ||
6932 | bacpy(&ev.addr.bdaddr, bdaddr); | |
6933 | ev.addr.type = link_to_bdaddr(link_type, addr_type); | |
6934 | ev.passkey = __cpu_to_le32(passkey); | |
6935 | ev.entered = entered; | |
6936 | ||
6937 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); | |
6938 | } | |
6939 | ||
e1e930f5 | 6940 | void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) |
2a611692 JH |
6941 | { |
6942 | struct mgmt_ev_auth_failed ev; | |
3b0602cd | 6943 | struct mgmt_pending_cmd *cmd; |
e1e930f5 | 6944 | u8 status = mgmt_status(hci_status); |
2a611692 | 6945 | |
e1e930f5 JH |
6946 | bacpy(&ev.addr.bdaddr, &conn->dst); |
6947 | ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); | |
6948 | ev.status = status; | |
2a611692 | 6949 | |
e1e930f5 JH |
6950 | cmd = find_pairing(conn); |
6951 | ||
6952 | mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev), | |
6953 | cmd ? cmd->sk : NULL); | |
6954 | ||
a511b35b JH |
6955 | if (cmd) { |
6956 | cmd->cmd_complete(cmd, status); | |
6957 | mgmt_pending_remove(cmd); | |
6958 | } | |
2a611692 | 6959 | } |
b312b161 | 6960 | |
464996ae | 6961 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
33ef95ed JH |
6962 | { |
6963 | struct cmd_lookup match = { NULL, hdev }; | |
464996ae | 6964 | bool changed; |
33ef95ed JH |
6965 | |
6966 | if (status) { | |
6967 | u8 mgmt_err = mgmt_status(status); | |
6968 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, | |
04124681 | 6969 | cmd_status_rsp, &mgmt_err); |
464996ae | 6970 | return; |
33ef95ed JH |
6971 | } |
6972 | ||
464996ae | 6973 | if (test_bit(HCI_AUTH, &hdev->flags)) |
238be788 | 6974 | changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY); |
464996ae | 6975 | else |
a69d8927 | 6976 | changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY); |
47990ea0 | 6977 | |
33ef95ed | 6978 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
04124681 | 6979 | &match); |
33ef95ed | 6980 | |
47990ea0 | 6981 | if (changed) |
464996ae | 6982 | new_settings(hdev, match.sk); |
33ef95ed JH |
6983 | |
6984 | if (match.sk) | |
6985 | sock_put(match.sk); | |
33ef95ed JH |
6986 | } |
6987 | ||
890ea898 | 6988 | static void clear_eir(struct hci_request *req) |
cacaf52f | 6989 | { |
890ea898 | 6990 | struct hci_dev *hdev = req->hdev; |
cacaf52f JH |
6991 | struct hci_cp_write_eir cp; |
6992 | ||
976eb20e | 6993 | if (!lmp_ext_inq_capable(hdev)) |
890ea898 | 6994 | return; |
cacaf52f | 6995 | |
c80da27e JH |
6996 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
6997 | ||
cacaf52f JH |
6998 | memset(&cp, 0, sizeof(cp)); |
6999 | ||
890ea898 | 7000 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
cacaf52f JH |
7001 | } |
7002 | ||
3e248560 | 7003 | void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
ed2c4ee3 JH |
7004 | { |
7005 | struct cmd_lookup match = { NULL, hdev }; | |
890ea898 | 7006 | struct hci_request req; |
c0ecddc2 | 7007 | bool changed = false; |
ed2c4ee3 JH |
7008 | |
7009 | if (status) { | |
7010 | u8 mgmt_err = mgmt_status(status); | |
c0ecddc2 | 7011 | |
a69d8927 MH |
7012 | if (enable && hci_dev_test_and_clear_flag(hdev, |
7013 | HCI_SSP_ENABLED)) { | |
a358dc11 | 7014 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
3e248560 | 7015 | new_settings(hdev, NULL); |
9ecb3e24 | 7016 | } |
c0ecddc2 | 7017 | |
04124681 GP |
7018 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
7019 | &mgmt_err); | |
3e248560 | 7020 | return; |
c0ecddc2 JH |
7021 | } |
7022 | ||
7023 | if (enable) { | |
238be788 | 7024 | changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED); |
c0ecddc2 | 7025 | } else { |
a69d8927 | 7026 | changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED); |
9ecb3e24 | 7027 | if (!changed) |
a69d8927 MH |
7028 | changed = hci_dev_test_and_clear_flag(hdev, |
7029 | HCI_HS_ENABLED); | |
9ecb3e24 | 7030 | else |
a358dc11 | 7031 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
ed2c4ee3 JH |
7032 | } |
7033 | ||
7034 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); | |
7035 | ||
c0ecddc2 | 7036 | if (changed) |
3e248560 | 7037 | new_settings(hdev, match.sk); |
ed2c4ee3 | 7038 | |
5fc6ebb1 | 7039 | if (match.sk) |
ed2c4ee3 JH |
7040 | sock_put(match.sk); |
7041 | ||
890ea898 JH |
7042 | hci_req_init(&req, hdev); |
7043 | ||
d7a5a11d MH |
7044 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
7045 | if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) | |
3769972b JH |
7046 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
7047 | sizeof(enable), &enable); | |
b1a8917c | 7048 | __hci_req_update_eir(&req); |
3769972b | 7049 | } else { |
890ea898 | 7050 | clear_eir(&req); |
3769972b | 7051 | } |
890ea898 JH |
7052 | |
7053 | hci_req_run(&req, NULL); | |
ed2c4ee3 JH |
7054 | } |
7055 | ||
3b0602cd | 7056 | static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data) |
90e70454 JH |
7057 | { |
7058 | struct cmd_lookup *match = data; | |
7059 | ||
90e70454 JH |
7060 | if (match->sk == NULL) { |
7061 | match->sk = cmd->sk; | |
7062 | sock_hold(match->sk); | |
7063 | } | |
90e70454 JH |
7064 | } |
7065 | ||
4e1b0245 MH |
7066 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
7067 | u8 status) | |
7f9a903c | 7068 | { |
90e70454 | 7069 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
7f9a903c | 7070 | |
92da6097 JH |
7071 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
7072 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); | |
7073 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); | |
90e70454 JH |
7074 | |
7075 | if (!status) | |
f6b7712e MH |
7076 | mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
7077 | dev_class, 3, NULL); | |
90e70454 JH |
7078 | |
7079 | if (match.sk) | |
7080 | sock_put(match.sk); | |
7f9a903c MH |
7081 | } |
7082 | ||
7667da34 | 7083 | void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status) |
b312b161 | 7084 | { |
b312b161 | 7085 | struct mgmt_cp_set_local_name ev; |
3b0602cd | 7086 | struct mgmt_pending_cmd *cmd; |
28cc7bde | 7087 | |
13928971 | 7088 | if (status) |
7667da34 | 7089 | return; |
b312b161 JH |
7090 | |
7091 | memset(&ev, 0, sizeof(ev)); | |
7092 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); | |
28cc7bde | 7093 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
b312b161 | 7094 | |
333ae95d | 7095 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
13928971 JH |
7096 | if (!cmd) { |
7097 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); | |
28cc7bde | 7098 | |
13928971 JH |
7099 | /* If this is a HCI command related to powering on the |
7100 | * HCI dev don't send any mgmt signals. | |
7101 | */ | |
333ae95d | 7102 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) |
7667da34 | 7103 | return; |
890ea898 | 7104 | } |
b312b161 | 7105 | |
f6b7712e MH |
7106 | mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
7107 | cmd ? cmd->sk : NULL); | |
b312b161 | 7108 | } |
c35938b2 | 7109 | |
799ce93d JP |
7110 | static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16]) |
7111 | { | |
7112 | int i; | |
7113 | ||
7114 | for (i = 0; i < uuid_count; i++) { | |
7115 | if (!memcmp(uuid, uuids[i], 16)) | |
7116 | return true; | |
7117 | } | |
7118 | ||
7119 | return false; | |
7120 | } | |
7121 | ||
b487b9ce MH |
7122 | static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) |
7123 | { | |
799ce93d JP |
7124 | u16 parsed = 0; |
7125 | ||
7126 | while (parsed < eir_len) { | |
7127 | u8 field_len = eir[0]; | |
7128 | u8 uuid[16]; | |
7129 | int i; | |
7130 | ||
7131 | if (field_len == 0) | |
7132 | break; | |
7133 | ||
7134 | if (eir_len - parsed < field_len + 1) | |
7135 | break; | |
7136 | ||
7137 | switch (eir[1]) { | |
7138 | case EIR_UUID16_ALL: | |
7139 | case EIR_UUID16_SOME: | |
7140 | for (i = 0; i + 3 <= field_len; i += 2) { | |
189f6ad2 | 7141 | memcpy(uuid, bluetooth_base_uuid, 16); |
799ce93d JP |
7142 | uuid[13] = eir[i + 3]; |
7143 | uuid[12] = eir[i + 2]; | |
7144 | if (has_uuid(uuid, uuid_count, uuids)) | |
7145 | return true; | |
7146 | } | |
7147 | break; | |
7148 | case EIR_UUID32_ALL: | |
7149 | case EIR_UUID32_SOME: | |
7150 | for (i = 0; i + 5 <= field_len; i += 4) { | |
189f6ad2 | 7151 | memcpy(uuid, bluetooth_base_uuid, 16); |
799ce93d JP |
7152 | uuid[15] = eir[i + 5]; |
7153 | uuid[14] = eir[i + 4]; | |
7154 | uuid[13] = eir[i + 3]; | |
7155 | uuid[12] = eir[i + 2]; | |
7156 | if (has_uuid(uuid, uuid_count, uuids)) | |
7157 | return true; | |
7158 | } | |
7159 | break; | |
7160 | case EIR_UUID128_ALL: | |
7161 | case EIR_UUID128_SOME: | |
7162 | for (i = 0; i + 17 <= field_len; i += 16) { | |
7163 | memcpy(uuid, eir + i + 2, 16); | |
7164 | if (has_uuid(uuid, uuid_count, uuids)) | |
7165 | return true; | |
7166 | } | |
7167 | break; | |
7168 | } | |
7169 | ||
7170 | parsed += field_len + 1; | |
7171 | eir += field_len + 1; | |
7172 | } | |
7173 | ||
b487b9ce MH |
7174 | return false; |
7175 | } | |
7176 | ||
4b0e0ced JP |
7177 | static void restart_le_scan(struct hci_dev *hdev) |
7178 | { | |
7179 | /* If controller is not scanning we are done. */ | |
d7a5a11d | 7180 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
4b0e0ced JP |
7181 | return; |
7182 | ||
7183 | if (time_after(jiffies + DISCOV_LE_RESTART_DELAY, | |
7184 | hdev->discovery.scan_start + | |
7185 | hdev->discovery.scan_duration)) | |
7186 | return; | |
7187 | ||
7c1fbed2 | 7188 | queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart, |
4b0e0ced JP |
7189 | DISCOV_LE_RESTART_DELAY); |
7190 | } | |
7191 | ||
48f86b7f JP |
7192 | static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir, |
7193 | u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) | |
e17acd40 | 7194 | { |
48f86b7f JP |
7195 | /* If a RSSI threshold has been specified, and |
7196 | * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with | |
7197 | * a RSSI smaller than the RSSI threshold will be dropped. If the quirk | |
7198 | * is set, let it through for further processing, as we might need to | |
7199 | * restart the scan. | |
efb2513f MH |
7200 | * |
7201 | * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry, | |
7202 | * the results are also dropped. | |
bda157a4 MH |
7203 | */ |
7204 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && | |
4b0e0ced JP |
7205 | (rssi == HCI_RSSI_INVALID || |
7206 | (rssi < hdev->discovery.rssi && | |
7207 | !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)))) | |
48f86b7f | 7208 | return false; |
efb2513f | 7209 | |
2976cdeb JP |
7210 | if (hdev->discovery.uuid_count != 0) { |
7211 | /* If a list of UUIDs is provided in filter, results with no | |
7212 | * matching UUID should be dropped. | |
b487b9ce | 7213 | */ |
2976cdeb JP |
7214 | if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count, |
7215 | hdev->discovery.uuids) && | |
7216 | !eir_has_uuids(scan_rsp, scan_rsp_len, | |
7217 | hdev->discovery.uuid_count, | |
7218 | hdev->discovery.uuids)) | |
7219 | return false; | |
b487b9ce | 7220 | } |
5d2e9fad | 7221 | |
2976cdeb JP |
7222 | /* If duplicate filtering does not report RSSI changes, then restart |
7223 | * scanning to ensure updated result with updated RSSI values. | |
4b0e0ced | 7224 | */ |
2976cdeb JP |
7225 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) { |
7226 | restart_le_scan(hdev); | |
7227 | ||
7228 | /* Validate RSSI value against the RSSI threshold once more. */ | |
7229 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && | |
7230 | rssi < hdev->discovery.rssi) | |
7231 | return false; | |
7232 | } | |
48f86b7f JP |
7233 | |
7234 | return true; | |
7235 | } | |
7236 | ||
7237 | void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, | |
7238 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, | |
7239 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) | |
7240 | { | |
7241 | char buf[512]; | |
7242 | struct mgmt_ev_device_found *ev = (void *)buf; | |
7243 | size_t ev_size; | |
7244 | ||
7245 | /* Don't send events for a non-kernel initiated discovery. With | |
7246 | * LE one exception is if we have pend_le_reports > 0 in which | |
7247 | * case we're doing passive scanning and want these events. | |
7248 | */ | |
7249 | if (!hci_discovery_active(hdev)) { | |
7250 | if (link_type == ACL_LINK) | |
7251 | return; | |
7252 | if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports)) | |
7253 | return; | |
7254 | } | |
7255 | ||
82f8b651 | 7256 | if (hdev->discovery.result_filtering) { |
48f86b7f JP |
7257 | /* We are using service discovery */ |
7258 | if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp, | |
7259 | scan_rsp_len)) | |
7260 | return; | |
7261 | } | |
7262 | ||
78b781ca JH |
7263 | if (hdev->discovery.limited) { |
7264 | /* Check for limited discoverable bit */ | |
7265 | if (dev_class) { | |
7266 | if (!(dev_class[1] & 0x20)) | |
7267 | return; | |
7268 | } else { | |
7269 | u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL); | |
7270 | if (!flags || !(flags[0] & LE_AD_LIMITED)) | |
7271 | return; | |
7272 | } | |
7273 | } | |
7274 | ||
48f86b7f JP |
7275 | /* Make sure that the buffer is big enough. The 5 extra bytes |
7276 | * are for the potential CoD field. | |
7277 | */ | |
7278 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) | |
4b0e0ced JP |
7279 | return; |
7280 | ||
48f86b7f JP |
7281 | memset(buf, 0, sizeof(buf)); |
7282 | ||
7283 | /* In case of device discovery with BR/EDR devices (pre 1.2), the | |
7284 | * RSSI value was reported as 0 when not available. This behavior | |
7285 | * is kept when using device discovery. This is required for full | |
7286 | * backwards compatibility with the API. | |
7287 | * | |
7288 | * However when using service discovery, the value 127 will be | |
7289 | * returned when the RSSI is not available. | |
7290 | */ | |
7291 | if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi && | |
7292 | link_type == ACL_LINK) | |
7293 | rssi = 0; | |
7294 | ||
7295 | bacpy(&ev->addr.bdaddr, bdaddr); | |
7296 | ev->addr.type = link_to_bdaddr(link_type, addr_type); | |
7297 | ev->rssi = rssi; | |
7298 | ev->flags = cpu_to_le32(flags); | |
7299 | ||
7300 | if (eir_len > 0) | |
7301 | /* Copy EIR or advertising data into event */ | |
7302 | memcpy(ev->eir, eir, eir_len); | |
7303 | ||
0d3b7f64 JH |
7304 | if (dev_class && !eir_get_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
7305 | NULL)) | |
48f86b7f JP |
7306 | eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
7307 | dev_class, 3); | |
7308 | ||
7309 | if (scan_rsp_len > 0) | |
7310 | /* Append scan response data to event */ | |
7311 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); | |
7312 | ||
5d2e9fad JH |
7313 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
7314 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; | |
f8523598 | 7315 | |
901801b9 | 7316 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
e17acd40 | 7317 | } |
a88a9652 | 7318 | |
9cf12aee MH |
7319 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
7320 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) | |
a88a9652 | 7321 | { |
b644ba33 JH |
7322 | struct mgmt_ev_device_found *ev; |
7323 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; | |
7324 | u16 eir_len; | |
a88a9652 | 7325 | |
b644ba33 | 7326 | ev = (struct mgmt_ev_device_found *) buf; |
a88a9652 | 7327 | |
b644ba33 JH |
7328 | memset(buf, 0, sizeof(buf)); |
7329 | ||
7330 | bacpy(&ev->addr.bdaddr, bdaddr); | |
57c1477c | 7331 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
b644ba33 JH |
7332 | ev->rssi = rssi; |
7333 | ||
7334 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name, | |
04124681 | 7335 | name_len); |
b644ba33 | 7336 | |
eb55ef07 | 7337 | ev->eir_len = cpu_to_le16(eir_len); |
a88a9652 | 7338 | |
9cf12aee | 7339 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL); |
a88a9652 | 7340 | } |
314b2381 | 7341 | |
2f1e063b | 7342 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
314b2381 | 7343 | { |
f963e8e9 | 7344 | struct mgmt_ev_discovering ev; |
164a6e78 | 7345 | |
343fb145 AG |
7346 | BT_DBG("%s discovering %u", hdev->name, discovering); |
7347 | ||
f963e8e9 JH |
7348 | memset(&ev, 0, sizeof(ev)); |
7349 | ev.type = hdev->discovery.type; | |
7350 | ev.discovering = discovering; | |
7351 | ||
2f1e063b | 7352 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
314b2381 | 7353 | } |
5e762444 | 7354 | |
6d785aa3 JH |
7355 | static struct hci_mgmt_chan chan = { |
7356 | .channel = HCI_CHANNEL_CONTROL, | |
7357 | .handler_count = ARRAY_SIZE(mgmt_handlers), | |
7358 | .handlers = mgmt_handlers, | |
88b94ce9 | 7359 | .hdev_init = mgmt_init_hdev, |
6d785aa3 JH |
7360 | }; |
7361 | ||
7362 | int mgmt_init(void) | |
7363 | { | |
7364 | return hci_mgmt_chan_register(&chan); | |
7365 | } | |
7366 | ||
7367 | void mgmt_exit(void) | |
7368 | { | |
7369 | hci_mgmt_chan_unregister(&chan); | |
7370 | } |