blob: 6ccd9de87cd6029959f94cf4ab2e987b1183d952 [file] [log] [blame]
Marcel Holtmann6a98e382021-10-27 16:58:38 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2021 Intel Corporation
6 */
7
8#include <net/bluetooth/bluetooth.h>
9#include <net/bluetooth/hci_core.h>
10#include <net/bluetooth/mgmt.h>
11
12#include "hci_request.h"
13#include "smp.h"
Luiz Augusto von Dentz161510c2021-10-27 16:58:39 -070014#include "eir.h"
Marcel Holtmann6a98e382021-10-27 16:58:38 -070015
16static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
17 struct sk_buff *skb)
18{
19 bt_dev_dbg(hdev, "result 0x%2.2x", result);
20
21 if (hdev->req_status != HCI_REQ_PEND)
22 return;
23
24 hdev->req_result = result;
25 hdev->req_status = HCI_REQ_DONE;
26
Luiz Augusto von Dentzcba6b752021-10-27 16:58:40 -070027 if (skb) {
28 struct sock *sk = hci_skb_sk(skb);
29
30 /* Drop sk reference if set */
31 if (sk)
32 sock_put(sk);
33
34 hdev->req_skb = skb_get(skb);
35 }
36
Marcel Holtmann6a98e382021-10-27 16:58:38 -070037 wake_up_interruptible(&hdev->req_wait_q);
38}
39
40static struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode,
41 u32 plen, const void *param,
42 struct sock *sk)
43{
44 int len = HCI_COMMAND_HDR_SIZE + plen;
45 struct hci_command_hdr *hdr;
46 struct sk_buff *skb;
47
48 skb = bt_skb_alloc(len, GFP_ATOMIC);
49 if (!skb)
50 return NULL;
51
52 hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
53 hdr->opcode = cpu_to_le16(opcode);
54 hdr->plen = plen;
55
56 if (plen)
57 skb_put_data(skb, param, plen);
58
59 bt_dev_dbg(hdev, "skb len %d", skb->len);
60
61 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
62 hci_skb_opcode(skb) = opcode;
63
Luiz Augusto von Dentzcba6b752021-10-27 16:58:40 -070064 /* Grab a reference if command needs to be associated with a sock (e.g.
65 * likely mgmt socket that initiated the command).
66 */
67 if (sk) {
68 hci_skb_sk(skb) = sk;
69 sock_hold(sk);
70 }
71
Marcel Holtmann6a98e382021-10-27 16:58:38 -070072 return skb;
73}
74
75static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen,
76 const void *param, u8 event, struct sock *sk)
77{
78 struct hci_dev *hdev = req->hdev;
79 struct sk_buff *skb;
80
81 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
82
83 /* If an error occurred during request building, there is no point in
84 * queueing the HCI command. We can simply return.
85 */
86 if (req->err)
87 return;
88
89 skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk);
90 if (!skb) {
91 bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
92 opcode);
93 req->err = -ENOMEM;
94 return;
95 }
96
97 if (skb_queue_empty(&req->cmd_q))
98 bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
99
100 bt_cb(skb)->hci.req_event = event;
101
102 skb_queue_tail(&req->cmd_q, skb);
103}
104
105static int hci_cmd_sync_run(struct hci_request *req)
106{
107 struct hci_dev *hdev = req->hdev;
108 struct sk_buff *skb;
109 unsigned long flags;
110
111 bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q));
112
113 /* If an error occurred during request building, remove all HCI
114 * commands queued on the HCI request queue.
115 */
116 if (req->err) {
117 skb_queue_purge(&req->cmd_q);
118 return req->err;
119 }
120
121 /* Do not allow empty requests */
122 if (skb_queue_empty(&req->cmd_q))
123 return -ENODATA;
124
125 skb = skb_peek_tail(&req->cmd_q);
126 bt_cb(skb)->hci.req_complete_skb = hci_cmd_sync_complete;
127 bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
128
129 spin_lock_irqsave(&hdev->cmd_q.lock, flags);
130 skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
131 spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
132
133 queue_work(hdev->workqueue, &hdev->cmd_work);
134
135 return 0;
136}
137
138/* This function requires the caller holds hdev->req_lock. */
139struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
140 const void *param, u8 event, u32 timeout,
141 struct sock *sk)
142{
143 struct hci_request req;
144 struct sk_buff *skb;
145 int err = 0;
146
147 bt_dev_dbg(hdev, "");
148
149 hci_req_init(&req, hdev);
150
151 hci_cmd_sync_add(&req, opcode, plen, param, event, sk);
152
153 hdev->req_status = HCI_REQ_PEND;
154
155 err = hci_cmd_sync_run(&req);
156 if (err < 0)
157 return ERR_PTR(err);
158
159 err = wait_event_interruptible_timeout(hdev->req_wait_q,
160 hdev->req_status != HCI_REQ_PEND,
161 timeout);
162
163 if (err == -ERESTARTSYS)
164 return ERR_PTR(-EINTR);
165
166 switch (hdev->req_status) {
167 case HCI_REQ_DONE:
168 err = -bt_to_errno(hdev->req_result);
169 break;
170
171 case HCI_REQ_CANCELED:
172 err = -hdev->req_result;
173 break;
174
175 default:
176 err = -ETIMEDOUT;
177 break;
178 }
179
180 hdev->req_status = 0;
181 hdev->req_result = 0;
182 skb = hdev->req_skb;
183 hdev->req_skb = NULL;
184
185 bt_dev_dbg(hdev, "end: err %d", err);
186
187 if (err < 0) {
188 kfree_skb(skb);
189 return ERR_PTR(err);
190 }
191
192 if (!skb)
193 return ERR_PTR(-ENODATA);
194
195 return skb;
196}
197EXPORT_SYMBOL(__hci_cmd_sync_sk);
198
199/* This function requires the caller holds hdev->req_lock. */
200struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
201 const void *param, u32 timeout)
202{
203 return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL);
204}
205EXPORT_SYMBOL(__hci_cmd_sync);
206
207/* Send HCI command and wait for command complete event */
208struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
209 const void *param, u32 timeout)
210{
211 struct sk_buff *skb;
212
213 if (!test_bit(HCI_UP, &hdev->flags))
214 return ERR_PTR(-ENETDOWN);
215
216 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
217
218 hci_req_sync_lock(hdev);
219 skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout);
220 hci_req_sync_unlock(hdev);
221
222 return skb;
223}
224EXPORT_SYMBOL(hci_cmd_sync);
225
226/* This function requires the caller holds hdev->req_lock. */
227struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
228 const void *param, u8 event, u32 timeout)
229{
230 return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout,
231 NULL);
232}
233EXPORT_SYMBOL(__hci_cmd_sync_ev);
234
235/* This function requires the caller holds hdev->req_lock. */
236int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
237 const void *param, u8 event, u32 timeout,
238 struct sock *sk)
239{
240 struct sk_buff *skb;
241 u8 status;
242
243 skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk);
244 if (IS_ERR_OR_NULL(skb)) {
245 bt_dev_err(hdev, "Opcode 0x%4x failed: %ld", opcode,
246 PTR_ERR(skb));
247 return PTR_ERR(skb);
248 }
249
250 status = skb->data[0];
251
252 kfree_skb(skb);
253
254 return status;
255}
256EXPORT_SYMBOL(__hci_cmd_sync_status_sk);
257
258int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
259 const void *param, u32 timeout)
260{
261 return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout,
262 NULL);
263}
264EXPORT_SYMBOL(__hci_cmd_sync_status);
265
266static void hci_cmd_sync_work(struct work_struct *work)
267{
268 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work);
269 struct hci_cmd_sync_work_entry *entry;
270 hci_cmd_sync_work_func_t func;
271 hci_cmd_sync_work_destroy_t destroy;
272 void *data;
273
274 bt_dev_dbg(hdev, "");
275
276 mutex_lock(&hdev->cmd_sync_work_lock);
277 entry = list_first_entry(&hdev->cmd_sync_work_list,
278 struct hci_cmd_sync_work_entry, list);
279 if (entry) {
280 list_del(&entry->list);
281 func = entry->func;
282 data = entry->data;
283 destroy = entry->destroy;
284 kfree(entry);
285 } else {
286 func = NULL;
287 data = NULL;
288 destroy = NULL;
289 }
290 mutex_unlock(&hdev->cmd_sync_work_lock);
291
292 if (func) {
293 int err;
294
295 hci_req_sync_lock(hdev);
296
297 err = func(hdev, data);
298
299 if (destroy)
300 destroy(hdev, data, err);
301
302 hci_req_sync_unlock(hdev);
303 }
304}
305
306void hci_cmd_sync_init(struct hci_dev *hdev)
307{
308 INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
309 INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
310 mutex_init(&hdev->cmd_sync_work_lock);
311}
312
313void hci_cmd_sync_clear(struct hci_dev *hdev)
314{
315 struct hci_cmd_sync_work_entry *entry, *tmp;
316
317 cancel_work_sync(&hdev->cmd_sync_work);
318
319 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
320 if (entry->destroy)
321 entry->destroy(hdev, entry->data, -ECANCELED);
322
323 list_del(&entry->list);
324 kfree(entry);
325 }
326}
327
328int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
329 void *data, hci_cmd_sync_work_destroy_t destroy)
330{
331 struct hci_cmd_sync_work_entry *entry;
332
333 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
334 if (!entry)
335 return -ENOMEM;
336
337 entry->func = func;
338 entry->data = data;
339 entry->destroy = destroy;
340
341 mutex_lock(&hdev->cmd_sync_work_lock);
342 list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
343 mutex_unlock(&hdev->cmd_sync_work_lock);
344
345 queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
346
347 return 0;
348}
349EXPORT_SYMBOL(hci_cmd_sync_queue);
Luiz Augusto von Dentz161510c2021-10-27 16:58:39 -0700350
351int hci_update_eir_sync(struct hci_dev *hdev)
352{
353 struct hci_cp_write_eir cp;
354
355 bt_dev_dbg(hdev, "");
356
357 if (!hdev_is_powered(hdev))
358 return 0;
359
360 if (!lmp_ext_inq_capable(hdev))
361 return 0;
362
363 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
364 return 0;
365
366 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
367 return 0;
368
369 memset(&cp, 0, sizeof(cp));
370
371 eir_create(hdev, cp.data);
372
373 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
374 return 0;
375
376 memcpy(hdev->eir, cp.data, sizeof(cp.data));
377
378 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
379 HCI_CMD_TIMEOUT);
380}
381
382static u8 get_service_classes(struct hci_dev *hdev)
383{
384 struct bt_uuid *uuid;
385 u8 val = 0;
386
387 list_for_each_entry(uuid, &hdev->uuids, list)
388 val |= uuid->svc_hint;
389
390 return val;
391}
392
393int hci_update_class_sync(struct hci_dev *hdev)
394{
395 u8 cod[3];
396
397 bt_dev_dbg(hdev, "");
398
399 if (!hdev_is_powered(hdev))
400 return 0;
401
402 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
403 return 0;
404
405 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
406 return 0;
407
408 cod[0] = hdev->minor_class;
409 cod[1] = hdev->major_class;
410 cod[2] = get_service_classes(hdev);
411
412 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
413 cod[1] |= 0x20;
414
415 if (memcmp(cod, hdev->dev_class, 3) == 0)
416 return 0;
417
418 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
419 sizeof(cod), cod, HCI_CMD_TIMEOUT);
420}
Luiz Augusto von Dentzcba6b752021-10-27 16:58:40 -0700421
422static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
423{
424 /* If there is no connection we are OK to advertise. */
425 if (hci_conn_num(hdev, LE_LINK) == 0)
426 return true;
427
428 /* Check le_states if there is any connection in peripheral role. */
429 if (hdev->conn_hash.le_num_peripheral > 0) {
430 /* Peripheral connection state and non connectable mode
431 * bit 20.
432 */
433 if (!connectable && !(hdev->le_states[2] & 0x10))
434 return false;
435
436 /* Peripheral connection state and connectable mode bit 38
437 * and scannable bit 21.
438 */
439 if (connectable && (!(hdev->le_states[4] & 0x40) ||
440 !(hdev->le_states[2] & 0x20)))
441 return false;
442 }
443
444 /* Check le_states if there is any connection in central role. */
445 if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
446 /* Central connection state and non connectable mode bit 18. */
447 if (!connectable && !(hdev->le_states[2] & 0x02))
448 return false;
449
450 /* Central connection state and connectable mode bit 35 and
451 * scannable 19.
452 */
453 if (connectable && (!(hdev->le_states[4] & 0x08) ||
454 !(hdev->le_states[2] & 0x08)))
455 return false;
456 }
457
458 return true;
459}
460
461static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
462{
463 /* If privacy is not enabled don't use RPA */
464 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
465 return false;
466
467 /* If basic privacy mode is enabled use RPA */
468 if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
469 return true;
470
471 /* If limited privacy mode is enabled don't use RPA if we're
472 * both discoverable and bondable.
473 */
474 if ((flags & MGMT_ADV_FLAG_DISCOV) &&
475 hci_dev_test_flag(hdev, HCI_BONDABLE))
476 return false;
477
478 /* We're neither bondable nor discoverable in the limited
479 * privacy mode, therefore use RPA.
480 */
481 return true;
482}
483
484static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
485{
486 /* If we're advertising or initiating an LE connection we can't
487 * go ahead and change the random address at this time. This is
488 * because the eventual initiator address used for the
489 * subsequently created connection will be undefined (some
490 * controllers use the new address and others the one we had
491 * when the operation started).
492 *
493 * In this kind of scenario skip the update and let the random
494 * address be updated at the next cycle.
495 */
496 if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
497 hci_lookup_le_connect(hdev)) {
498 bt_dev_dbg(hdev, "Deferring random address update");
499 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
500 return 0;
501 }
502
503 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
504 6, rpa, HCI_CMD_TIMEOUT);
505}
506
507int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
508 bool rpa, u8 *own_addr_type)
509{
510 int err;
511
512 /* If privacy is enabled use a resolvable private address. If
513 * current RPA has expired or there is something else than
514 * the current RPA in use, then generate a new one.
515 */
516 if (rpa) {
517 /* If Controller supports LL Privacy use own address type is
518 * 0x03
519 */
520 if (use_ll_privacy(hdev) &&
521 hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY))
522 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
523 else
524 *own_addr_type = ADDR_LE_DEV_RANDOM;
525
526 /* Check if RPA is valid */
527 if (rpa_valid(hdev))
528 return 0;
529
530 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
531 if (err < 0) {
532 bt_dev_err(hdev, "failed to generate new RPA");
533 return err;
534 }
535
536 err = hci_set_random_addr_sync(hdev, &hdev->rpa);
537 if (err)
538 return err;
539
540 return 0;
541 }
542
543 /* In case of required privacy without resolvable private address,
544 * use an non-resolvable private address. This is useful for active
545 * scanning and non-connectable advertising.
546 */
547 if (require_privacy) {
548 bdaddr_t nrpa;
549
550 while (true) {
551 /* The non-resolvable private address is generated
552 * from random six bytes with the two most significant
553 * bits cleared.
554 */
555 get_random_bytes(&nrpa, 6);
556 nrpa.b[5] &= 0x3f;
557
558 /* The non-resolvable private address shall not be
559 * equal to the public address.
560 */
561 if (bacmp(&hdev->bdaddr, &nrpa))
562 break;
563 }
564
565 *own_addr_type = ADDR_LE_DEV_RANDOM;
566
567 return hci_set_random_addr_sync(hdev, &nrpa);
568 }
569
570 /* If forcing static address is in use or there is no public
571 * address use the static address as random address (but skip
572 * the HCI command if the current random address is already the
573 * static one.
574 *
575 * In case BR/EDR has been disabled on a dual-mode controller
576 * and a static address has been configured, then use that
577 * address instead of the public BR/EDR address.
578 */
579 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
580 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
581 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
582 bacmp(&hdev->static_addr, BDADDR_ANY))) {
583 *own_addr_type = ADDR_LE_DEV_RANDOM;
584 if (bacmp(&hdev->static_addr, &hdev->random_addr))
585 return hci_set_random_addr_sync(hdev,
586 &hdev->static_addr);
587 return 0;
588 }
589
590 /* Neither privacy nor static address is being used so use a
591 * public address.
592 */
593 *own_addr_type = ADDR_LE_DEV_PUBLIC;
594
595 return 0;
596}
597
598static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
599{
600 struct hci_cp_le_set_ext_adv_enable *cp;
601 struct hci_cp_ext_adv_set *set;
602 u8 data[sizeof(*cp) + sizeof(*set) * 1];
603 u8 size;
604
605 /* If request specifies an instance that doesn't exist, fail */
606 if (instance > 0) {
607 struct adv_info *adv;
608
609 adv = hci_find_adv_instance(hdev, instance);
610 if (!adv)
611 return -EINVAL;
612
613 /* If not enabled there is nothing to do */
614 if (!adv->enabled)
615 return 0;
616 }
617
618 memset(data, 0, sizeof(data));
619
620 cp = (void *)data;
621 set = (void *)cp->data;
622
623 /* Instance 0x00 indicates all advertising instances will be disabled */
624 cp->num_of_sets = !!instance;
625 cp->enable = 0x00;
626
627 set->handle = instance;
628
629 size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets;
630
631 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
632 size, data, HCI_CMD_TIMEOUT);
633}
634
635static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
636 bdaddr_t *random_addr)
637{
638 struct hci_cp_le_set_adv_set_rand_addr cp;
639 int err;
640
641 if (!instance) {
642 /* Instance 0x00 doesn't have an adv_info, instead it uses
643 * hdev->random_addr to track its address so whenever it needs
644 * to be updated this also set the random address since
645 * hdev->random_addr is shared with scan state machine.
646 */
647 err = hci_set_random_addr_sync(hdev, random_addr);
648 if (err)
649 return err;
650 }
651
652 memset(&cp, 0, sizeof(cp));
653
654 cp.handle = instance;
655 bacpy(&cp.bdaddr, random_addr);
656
657 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
658 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
659}
660
661int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
662{
663 struct hci_cp_le_set_ext_adv_params cp;
664 bool connectable;
665 u32 flags;
666 bdaddr_t random_addr;
667 u8 own_addr_type;
668 int err;
669 struct adv_info *adv;
670 bool secondary_adv;
671
672 if (instance > 0) {
673 adv = hci_find_adv_instance(hdev, instance);
674 if (!adv)
675 return -EINVAL;
676 } else {
677 adv = NULL;
678 }
679
680 /* Updating parameters of an active instance will return a
681 * Command Disallowed error, so we must first disable the
682 * instance if it is active.
683 */
684 if (adv && !adv->pending) {
685 err = hci_disable_ext_adv_instance_sync(hdev, instance);
686 if (err)
687 return err;
688 }
689
690 flags = hci_adv_instance_flags(hdev, instance);
691
692 /* If the "connectable" instance flag was not set, then choose between
693 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
694 */
695 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
696 mgmt_get_connectable(hdev);
697
698 if (!is_advertising_allowed(hdev, connectable))
699 return -EPERM;
700
701 /* Set require_privacy to true only when non-connectable
702 * advertising is used. In that case it is fine to use a
703 * non-resolvable private address.
704 */
705 err = hci_get_random_address(hdev, !connectable,
706 adv_use_rpa(hdev, flags), adv,
707 &own_addr_type, &random_addr);
708 if (err < 0)
709 return err;
710
711 memset(&cp, 0, sizeof(cp));
712
713 if (adv) {
714 hci_cpu_to_le24(adv->min_interval, cp.min_interval);
715 hci_cpu_to_le24(adv->max_interval, cp.max_interval);
716 cp.tx_power = adv->tx_power;
717 } else {
718 hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
719 hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
720 cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
721 }
722
723 secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
724
725 if (connectable) {
726 if (secondary_adv)
727 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
728 else
729 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
730 } else if (hci_adv_instance_is_scannable(hdev, instance) ||
731 (flags & MGMT_ADV_PARAM_SCAN_RSP)) {
732 if (secondary_adv)
733 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
734 else
735 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
736 } else {
737 if (secondary_adv)
738 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
739 else
740 cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
741 }
742
743 cp.own_addr_type = own_addr_type;
744 cp.channel_map = hdev->le_adv_channel_map;
745 cp.handle = instance;
746
747 if (flags & MGMT_ADV_FLAG_SEC_2M) {
748 cp.primary_phy = HCI_ADV_PHY_1M;
749 cp.secondary_phy = HCI_ADV_PHY_2M;
750 } else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
751 cp.primary_phy = HCI_ADV_PHY_CODED;
752 cp.secondary_phy = HCI_ADV_PHY_CODED;
753 } else {
754 /* In all other cases use 1M */
755 cp.primary_phy = HCI_ADV_PHY_1M;
756 cp.secondary_phy = HCI_ADV_PHY_1M;
757 }
758
759 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
760 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
761 if (err)
762 return err;
763
764 if ((own_addr_type == ADDR_LE_DEV_RANDOM ||
765 own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
766 bacmp(&random_addr, BDADDR_ANY)) {
767 /* Check if random address need to be updated */
768 if (adv) {
769 if (!bacmp(&random_addr, &adv->random_addr))
770 return 0;
771 } else {
772 if (!bacmp(&random_addr, &hdev->random_addr))
773 return 0;
774 }
775
776 return hci_set_adv_set_random_addr_sync(hdev, instance,
777 &random_addr);
778 }
779
780 return 0;
781}
782
783static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
784{
785 struct {
786 struct hci_cp_le_set_ext_scan_rsp_data cp;
787 u8 data[HCI_MAX_EXT_AD_LENGTH];
788 } pdu;
789 u8 len;
790
791 memset(&pdu, 0, sizeof(pdu));
792
793 len = eir_create_scan_rsp(hdev, instance, pdu.data);
794
795 if (hdev->scan_rsp_data_len == len &&
796 !memcmp(pdu.data, hdev->scan_rsp_data, len))
797 return 0;
798
799 memcpy(hdev->scan_rsp_data, pdu.data, len);
800 hdev->scan_rsp_data_len = len;
801
802 pdu.cp.handle = instance;
803 pdu.cp.length = len;
804 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
805 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
806
807 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
808 sizeof(pdu.cp) + len, &pdu.cp,
809 HCI_CMD_TIMEOUT);
810}
811
812static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
813{
814 struct hci_cp_le_set_scan_rsp_data cp;
815 u8 len;
816
817 memset(&cp, 0, sizeof(cp));
818
819 len = eir_create_scan_rsp(hdev, instance, cp.data);
820
821 if (hdev->scan_rsp_data_len == len &&
822 !memcmp(cp.data, hdev->scan_rsp_data, len))
823 return 0;
824
825 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
826 hdev->scan_rsp_data_len = len;
827
828 cp.length = len;
829
830 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
831 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
832}
833
834int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
835{
836 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
837 return 0;
838
839 if (ext_adv_capable(hdev))
840 return hci_set_ext_scan_rsp_data_sync(hdev, instance);
841
842 return __hci_set_scan_rsp_data_sync(hdev, instance);
843}
844
845int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
846{
847 struct hci_cp_le_set_ext_adv_enable *cp;
848 struct hci_cp_ext_adv_set *set;
849 u8 data[sizeof(*cp) + sizeof(*set) * 1];
850 struct adv_info *adv;
851
852 if (instance > 0) {
853 adv = hci_find_adv_instance(hdev, instance);
854 if (!adv)
855 return -EINVAL;
856 /* If already enabled there is nothing to do */
857 if (adv->enabled)
858 return 0;
859 } else {
860 adv = NULL;
861 }
862
863 cp = (void *)data;
864 set = (void *)cp->data;
865
866 memset(cp, 0, sizeof(*cp));
867
868 cp->enable = 0x01;
869 cp->num_of_sets = 0x01;
870
871 memset(set, 0, sizeof(*set));
872
873 set->handle = instance;
874
875 /* Set duration per instance since controller is responsible for
876 * scheduling it.
877 */
878 if (adv && adv->duration) {
879 u16 duration = adv->timeout * MSEC_PER_SEC;
880
881 /* Time = N * 10 ms */
882 set->duration = cpu_to_le16(duration / 10);
883 }
884
885 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
886 sizeof(*cp) +
887 sizeof(*set) * cp->num_of_sets,
888 data, HCI_CMD_TIMEOUT);
889}
890
891int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
892{
893 int err;
894
895 err = hci_setup_ext_adv_instance_sync(hdev, instance);
896 if (err)
897 return err;
898
899 err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
900 if (err)
901 return err;
902
903 return hci_enable_ext_advertising_sync(hdev, instance);
904}
905
906static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
907{
908 int err;
909
910 if (ext_adv_capable(hdev))
911 return hci_start_ext_adv_sync(hdev, instance);
912
913 err = hci_update_adv_data_sync(hdev, instance);
914 if (err)
915 return err;
916
917 err = hci_update_scan_rsp_data_sync(hdev, instance);
918 if (err)
919 return err;
920
921 return hci_enable_advertising_sync(hdev);
922}
923
924int hci_enable_advertising_sync(struct hci_dev *hdev)
925{
926 struct adv_info *adv_instance;
927 struct hci_cp_le_set_adv_param cp;
928 u8 own_addr_type, enable = 0x01;
929 bool connectable;
930 u16 adv_min_interval, adv_max_interval;
931 u32 flags;
932 u8 status;
933
934 flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
935 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
936
937 /* If the "connectable" instance flag was not set, then choose between
938 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
939 */
940 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
941 mgmt_get_connectable(hdev);
942
943 if (!is_advertising_allowed(hdev, connectable))
944 return -EINVAL;
945
946 if (hci_dev_test_flag(hdev, HCI_LE_ADV)) {
947 status = hci_disable_advertising_sync(hdev);
948 if (status)
949 return status;
950 }
951
952 /* Clear the HCI_LE_ADV bit temporarily so that the
953 * hci_update_random_address knows that it's safe to go ahead
954 * and write a new random address. The flag will be set back on
955 * as soon as the SET_ADV_ENABLE HCI command completes.
956 */
957 hci_dev_clear_flag(hdev, HCI_LE_ADV);
958
959 /* Set require_privacy to true only when non-connectable
960 * advertising is used. In that case it is fine to use a
961 * non-resolvable private address.
962 */
963 status = hci_update_random_address_sync(hdev, !connectable,
964 adv_use_rpa(hdev, flags),
965 &own_addr_type);
966 if (status)
967 return status;
968
969 memset(&cp, 0, sizeof(cp));
970
971 if (adv_instance) {
972 adv_min_interval = adv_instance->min_interval;
973 adv_max_interval = adv_instance->max_interval;
974 } else {
975 adv_min_interval = hdev->le_adv_min_interval;
976 adv_max_interval = hdev->le_adv_max_interval;
977 }
978
979 if (connectable) {
980 cp.type = LE_ADV_IND;
981 } else {
982 if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
983 cp.type = LE_ADV_SCAN_IND;
984 else
985 cp.type = LE_ADV_NONCONN_IND;
986
987 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
988 hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
989 adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
990 adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
991 }
992 }
993
994 cp.min_interval = cpu_to_le16(adv_min_interval);
995 cp.max_interval = cpu_to_le16(adv_max_interval);
996 cp.own_address_type = own_addr_type;
997 cp.channel_map = hdev->le_adv_channel_map;
998
999 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1000 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1001 if (status)
1002 return status;
1003
1004 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1005 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1006}
1007
1008static int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1009 struct sock *sk)
1010{
1011 int err;
1012
1013 if (!ext_adv_capable(hdev))
1014 return 0;
1015
1016 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1017 if (err)
1018 return err;
1019
1020 /* If request specifies an instance that doesn't exist, fail */
1021 if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1022 return -EINVAL;
1023
1024 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1025 sizeof(instance), &instance, 0,
1026 HCI_CMD_TIMEOUT, sk);
1027}
1028
1029static void cancel_adv_timeout(struct hci_dev *hdev)
1030{
1031 if (hdev->adv_instance_timeout) {
1032 hdev->adv_instance_timeout = 0;
1033 cancel_delayed_work(&hdev->adv_instance_expire);
1034 }
1035}
1036
1037static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1038{
1039 struct {
1040 struct hci_cp_le_set_ext_adv_data cp;
1041 u8 data[HCI_MAX_EXT_AD_LENGTH];
1042 } pdu;
1043 u8 len;
1044
1045 memset(&pdu, 0, sizeof(pdu));
1046
1047 len = eir_create_adv_data(hdev, instance, pdu.data);
1048
1049 /* There's nothing to do if the data hasn't changed */
1050 if (hdev->adv_data_len == len &&
1051 memcmp(pdu.data, hdev->adv_data, len) == 0)
1052 return 0;
1053
1054 memcpy(hdev->adv_data, pdu.data, len);
1055 hdev->adv_data_len = len;
1056
1057 pdu.cp.length = len;
1058 pdu.cp.handle = instance;
1059 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1060 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1061
1062 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1063 sizeof(pdu.cp) + len, &pdu.cp,
1064 HCI_CMD_TIMEOUT);
1065}
1066
1067static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1068{
1069 struct hci_cp_le_set_adv_data cp;
1070 u8 len;
1071
1072 memset(&cp, 0, sizeof(cp));
1073
1074 len = eir_create_adv_data(hdev, instance, cp.data);
1075
1076 /* There's nothing to do if the data hasn't changed */
1077 if (hdev->adv_data_len == len &&
1078 memcmp(cp.data, hdev->adv_data, len) == 0)
1079 return 0;
1080
1081 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1082 hdev->adv_data_len = len;
1083
1084 cp.length = len;
1085
1086 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1087 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1088}
1089
1090int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1091{
1092 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1093 return 0;
1094
1095 if (ext_adv_capable(hdev))
1096 return hci_set_ext_adv_data_sync(hdev, instance);
1097
1098 return hci_set_adv_data_sync(hdev, instance);
1099}
1100
1101int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1102 bool force)
1103{
1104 struct adv_info *adv = NULL;
1105 u16 timeout;
1106
1107 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
1108 list_empty(&hdev->adv_instances))
1109 return -EPERM;
1110
1111 if (hdev->adv_instance_timeout)
1112 return -EBUSY;
1113
1114 adv = hci_find_adv_instance(hdev, instance);
1115 if (!adv)
1116 return -ENOENT;
1117
1118 /* A zero timeout means unlimited advertising. As long as there is
1119 * only one instance, duration should be ignored. We still set a timeout
1120 * in case further instances are being added later on.
1121 *
1122 * If the remaining lifetime of the instance is more than the duration
1123 * then the timeout corresponds to the duration, otherwise it will be
1124 * reduced to the remaining instance lifetime.
1125 */
1126 if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
1127 timeout = adv->duration;
1128 else
1129 timeout = adv->remaining_time;
1130
1131 /* The remaining time is being reduced unless the instance is being
1132 * advertised without time limit.
1133 */
1134 if (adv->timeout)
1135 adv->remaining_time = adv->remaining_time - timeout;
1136
1137 /* Only use work for scheduling instances with legacy advertising */
1138 if (!ext_adv_capable(hdev)) {
1139 hdev->adv_instance_timeout = timeout;
1140 queue_delayed_work(hdev->req_workqueue,
1141 &hdev->adv_instance_expire,
1142 msecs_to_jiffies(timeout * 1000));
1143 }
1144
1145 /* If we're just re-scheduling the same instance again then do not
1146 * execute any HCI commands. This happens when a single instance is
1147 * being advertised.
1148 */
1149 if (!force && hdev->cur_adv_instance == instance &&
1150 hci_dev_test_flag(hdev, HCI_LE_ADV))
1151 return 0;
1152
1153 hdev->cur_adv_instance = instance;
1154
1155 return hci_start_adv_sync(hdev, instance);
1156}
1157
1158static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1159{
1160 int err;
1161
1162 if (!ext_adv_capable(hdev))
1163 return 0;
1164
1165 /* Disable instance 0x00 to disable all instances */
1166 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1167 if (err)
1168 return err;
1169
1170 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1171 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1172}
1173
1174static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1175{
1176 struct adv_info *adv, *n;
1177
1178 if (ext_adv_capable(hdev))
1179 /* Remove all existing sets */
1180 return hci_clear_adv_sets_sync(hdev, sk);
1181
1182 /* This is safe as long as there is no command send while the lock is
1183 * held.
1184 */
1185 hci_dev_lock(hdev);
1186
1187 /* Cleanup non-ext instances */
1188 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1189 u8 instance = adv->instance;
1190 int err;
1191
1192 if (!(force || adv->timeout))
1193 continue;
1194
1195 err = hci_remove_adv_instance(hdev, instance);
1196 if (!err)
1197 mgmt_advertising_removed(sk, hdev, instance);
1198 }
1199
1200 hci_dev_unlock(hdev);
1201
1202 return 0;
1203}
1204
1205static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1206 struct sock *sk)
1207{
1208 int err;
1209
1210 /* If we use extended advertising, instance has to be removed first. */
1211 if (ext_adv_capable(hdev))
1212 return hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1213
1214 /* This is safe as long as there is no command send while the lock is
1215 * held.
1216 */
1217 hci_dev_lock(hdev);
1218
1219 err = hci_remove_adv_instance(hdev, instance);
1220 if (!err)
1221 mgmt_advertising_removed(sk, hdev, instance);
1222
1223 hci_dev_unlock(hdev);
1224
1225 return err;
1226}
1227
1228/* For a single instance:
1229 * - force == true: The instance will be removed even when its remaining
1230 * lifetime is not zero.
1231 * - force == false: the instance will be deactivated but kept stored unless
1232 * the remaining lifetime is zero.
1233 *
1234 * For instance == 0x00:
1235 * - force == true: All instances will be removed regardless of their timeout
1236 * setting.
1237 * - force == false: Only instances that have a timeout will be removed.
1238 */
1239int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1240 u8 instance, bool force)
1241{
1242 struct adv_info *next = NULL;
1243 int err;
1244
1245 /* Cancel any timeout concerning the removed instance(s). */
1246 if (!instance || hdev->cur_adv_instance == instance)
1247 cancel_adv_timeout(hdev);
1248
1249 /* Get the next instance to advertise BEFORE we remove
1250 * the current one. This can be the same instance again
1251 * if there is only one instance.
1252 */
1253 if (hdev->cur_adv_instance == instance)
1254 next = hci_get_next_instance(hdev, instance);
1255
1256 if (!instance) {
1257 err = hci_clear_adv_sync(hdev, sk, force);
1258 if (err)
1259 return err;
1260 } else {
1261 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1262
1263 if (force || (adv && adv->timeout && !adv->remaining_time)) {
1264 /* Don't advertise a removed instance. */
1265 if (next && next->instance == instance)
1266 next = NULL;
1267
1268 err = hci_remove_adv_sync(hdev, instance, sk);
1269 if (err)
1270 return err;
1271 }
1272 }
1273
1274 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1275 return 0;
1276
1277 if (next && !ext_adv_capable(hdev))
1278 hci_schedule_adv_instance_sync(hdev, next->instance, false);
1279
1280 return 0;
1281}
1282
1283int hci_disable_advertising_sync(struct hci_dev *hdev)
1284{
1285 u8 enable = 0x00;
1286
1287 if (ext_adv_capable(hdev))
1288 return hci_disable_ext_adv_instance_sync(hdev, 0x00);
1289
1290 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1291 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1292}