^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) BlueZ - Bluetooth protocol stack for Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) it under the terms of the GNU General Public License version 2 as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) published by the Free Software Foundation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) SOFTWARE IS DISCLAIMED.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* Bluetooth HCI connection handling. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <net/bluetooth/bluetooth.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <net/bluetooth/hci_core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <net/bluetooth/l2cap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include "hci_request.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include "smp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include "a2mp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct sco_param {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) u16 pkt_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) u16 max_latency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) u8 retrans_effort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static const struct sco_param esco_param_cvsd[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000a, 0x01 }, /* S3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) { EDR_ESCO_MASK & ~ESCO_2EV3, 0x0007, 0x01 }, /* S2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) { EDR_ESCO_MASK | ESCO_EV3, 0x0007, 0x01 }, /* S1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0x01 }, /* D1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0x01 }, /* D0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static const struct sco_param sco_param_cvsd[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0xff }, /* D1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0xff }, /* D0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static const struct sco_param esco_param_msbc[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d, 0x02 }, /* T2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) { EDR_ESCO_MASK | ESCO_EV3, 0x0008, 0x02 }, /* T1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /* This function requires the caller holds hdev->lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static void hci_connect_le_scan_cleanup(struct hci_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct hci_conn_params *params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct smp_irk *irk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) bdaddr_t *bdaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) u8 bdaddr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) bdaddr = &conn->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) bdaddr_type = conn->dst_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /* Check if we need to convert to identity address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (irk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) bdaddr = &irk->bdaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) bdaddr_type = irk->addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) params = hci_pend_le_action_lookup(&hdev->pend_le_conns, bdaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) bdaddr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!params || !params->explicit_connect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /* The connection attempt was doing scan for new RPA, and is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * in scan phase. If params are not associated with any other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * autoconnect action, remove them completely. If they are, just unmark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * them as waiting for connection, by clearing explicit_connect field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) params->explicit_connect = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) list_del_init(¶ms->action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) switch (params->auto_connect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) case HCI_AUTO_CONN_EXPLICIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) hci_conn_params_del(hdev, bdaddr, bdaddr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* return instead of break to avoid duplicate scan update */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) case HCI_AUTO_CONN_DIRECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) case HCI_AUTO_CONN_ALWAYS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) list_add(¶ms->action, &hdev->pend_le_conns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) case HCI_AUTO_CONN_REPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) list_add(¶ms->action, &hdev->pend_le_reports);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) hci_update_background_scan(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static void hci_conn_cleanup(struct hci_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (test_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) hci_conn_params_del(conn->hdev, &conn->dst, conn->dst_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) hci_chan_list_flush(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) hci_conn_hash_del(hdev, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (conn->type == SCO_LINK || conn->type == ESCO_LINK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) switch (conn->setting & SCO_AIRMODE_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) case SCO_AIRMODE_CVSD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) case SCO_AIRMODE_TRANSP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (hdev->notify)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) hdev->notify(hdev, HCI_NOTIFY_DISABLE_SCO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (hdev->notify)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) hci_conn_del_sysfs(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) debugfs_remove_recursive(conn->debugfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) hci_dev_put(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) hci_conn_put(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static void le_scan_cleanup(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct hci_conn *conn = container_of(work, struct hci_conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) le_scan_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct hci_conn *c = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) BT_DBG("%s hcon %p", hdev->name, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) hci_dev_lock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) /* Check that the hci_conn is still around */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) list_for_each_entry_rcu(c, &hdev->conn_hash.list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (c == conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (c == conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) hci_connect_le_scan_cleanup(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) hci_conn_cleanup(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) hci_dev_unlock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) hci_dev_put(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) hci_conn_put(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static void hci_connect_le_scan_remove(struct hci_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) BT_DBG("%s hcon %p", conn->hdev->name, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /* We can't call hci_conn_del/hci_conn_cleanup here since that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * could deadlock with another hci_conn_del() call that's holding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * hci_dev_lock and doing cancel_delayed_work_sync(&conn->disc_work).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * Instead, grab temporary extra references to the hci_dev and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * hci_conn and perform the necessary cleanup in a separate work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) hci_dev_hold(conn->hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) hci_conn_get(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* Even though we hold a reference to the hdev, many other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * things might get cleaned up meanwhile, including the hdev's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * own workqueue, so we can't use that for scheduling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) schedule_work(&conn->le_scan_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static void hci_acl_create_connection(struct hci_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct inquiry_entry *ie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct hci_cp_create_conn cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) BT_DBG("hcon %p", conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) conn->state = BT_CONNECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) conn->out = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) conn->role = HCI_ROLE_MASTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) conn->attempt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) conn->link_policy = hdev->link_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) memset(&cp, 0, sizeof(cp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) bacpy(&cp.bdaddr, &conn->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) cp.pscan_rep_mode = 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (ie) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) cp.pscan_rep_mode = ie->data.pscan_rep_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) cp.pscan_mode = ie->data.pscan_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) cp.clock_offset = ie->data.clock_offset |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) cpu_to_le16(0x8000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) memcpy(conn->dev_class, ie->data.dev_class, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) cp.pkt_type = cpu_to_le16(conn->pkt_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) cp.role_switch = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) cp.role_switch = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) int hci_disconnect(struct hci_conn *conn, __u8 reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) BT_DBG("hcon %p", conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /* When we are master of an established connection and it enters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * the disconnect timeout, then go ahead and try to read the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * current clock offset. Processing of the result is done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * within the event handling and hci_clock_offset_evt function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (conn->type == ACL_LINK && conn->role == HCI_ROLE_MASTER &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) (conn->state == BT_CONNECTED || conn->state == BT_CONFIG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) struct hci_cp_read_clock_offset clkoff_cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) clkoff_cp.handle = cpu_to_le16(conn->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, sizeof(clkoff_cp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) &clkoff_cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return hci_abort_conn(conn, reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static void hci_add_sco(struct hci_conn *conn, __u16 handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct hci_cp_add_sco cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) BT_DBG("hcon %p", conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) conn->state = BT_CONNECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) conn->out = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) conn->attempt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) cp.handle = cpu_to_le16(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) cp.pkt_type = cpu_to_le16(conn->pkt_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) bool hci_setup_sync(struct hci_conn *conn, __u16 handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct hci_cp_setup_sync_conn cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) const struct sco_param *param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) BT_DBG("hcon %p", conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) conn->state = BT_CONNECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) conn->out = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) conn->attempt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) cp.handle = cpu_to_le16(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) cp.tx_bandwidth = cpu_to_le32(0x00001f40);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) cp.rx_bandwidth = cpu_to_le32(0x00001f40);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) cp.voice_setting = cpu_to_le16(conn->setting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) switch (conn->setting & SCO_AIRMODE_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) case SCO_AIRMODE_TRANSP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (conn->attempt > ARRAY_SIZE(esco_param_msbc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) param = &esco_param_msbc[conn->attempt - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) case SCO_AIRMODE_CVSD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (lmp_esco_capable(conn->link)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (conn->attempt > ARRAY_SIZE(esco_param_cvsd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) param = &esco_param_cvsd[conn->attempt - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) param = &sco_param_cvsd[conn->attempt - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) cp.retrans_effort = param->retrans_effort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) cp.pkt_type = __cpu_to_le16(param->pkt_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) cp.max_latency = __cpu_to_le16(param->max_latency);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) u16 to_multiplier)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) struct hci_conn_params *params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct hci_cp_le_conn_update cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) hci_dev_lock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (params) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) params->conn_min_interval = min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) params->conn_max_interval = max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) params->conn_latency = latency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) params->supervision_timeout = to_multiplier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) hci_dev_unlock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) memset(&cp, 0, sizeof(cp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) cp.handle = cpu_to_le16(conn->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) cp.conn_interval_min = cpu_to_le16(min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) cp.conn_interval_max = cpu_to_le16(max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) cp.conn_latency = cpu_to_le16(latency);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) cp.supervision_timeout = cpu_to_le16(to_multiplier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) cp.min_ce_len = cpu_to_le16(0x0000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) cp.max_ce_len = cpu_to_le16(0x0000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) return 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) __u8 ltk[16], __u8 key_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) struct hci_cp_le_start_enc cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) BT_DBG("hcon %p", conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) memset(&cp, 0, sizeof(cp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) cp.handle = cpu_to_le16(conn->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) cp.rand = rand;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) cp.ediv = ediv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) memcpy(cp.ltk, ltk, key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) /* Device _must_ be locked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) void hci_sco_setup(struct hci_conn *conn, __u8 status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) struct hci_conn *sco = conn->link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (!sco)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) BT_DBG("hcon %p", conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (!status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) if (lmp_esco_capable(conn->hdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) hci_setup_sync(sco, conn->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) hci_add_sco(sco, conn->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) hci_connect_cfm(sco, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) hci_conn_del(sco);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static void hci_conn_timeout(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) struct hci_conn *conn = container_of(work, struct hci_conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) disc_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) int refcnt = atomic_read(&conn->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) BT_DBG("hcon %p state %s", conn, state_to_string(conn->state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) WARN_ON(refcnt < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) /* FIXME: It was observed that in pairing failed scenario, refcnt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * drops below 0. Probably this is because l2cap_conn_del calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * l2cap_chan_del for each channel, and inside l2cap_chan_del conn is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * dropped. After that loop hci_chan_del is called which also drops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * conn. For now make sure that ACL is alive if refcnt is higher then 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * otherwise drop it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (refcnt > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) /* LE connections in scanning state need special handling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (conn->state == BT_CONNECT && conn->type == LE_LINK &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) test_bit(HCI_CONN_SCANNING, &conn->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) hci_connect_le_scan_remove(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) hci_abort_conn(conn, hci_proto_disconn_ind(conn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) /* Enter sniff mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) static void hci_conn_idle(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) struct hci_conn *conn = container_of(work, struct hci_conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) idle_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) BT_DBG("hcon %p mode %d", conn, conn->mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) struct hci_cp_sniff_subrate cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) cp.handle = cpu_to_le16(conn->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) cp.max_latency = cpu_to_le16(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) cp.min_remote_timeout = cpu_to_le16(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) cp.min_local_timeout = cpu_to_le16(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) struct hci_cp_sniff_mode cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) cp.handle = cpu_to_le16(conn->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) cp.attempt = cpu_to_le16(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) cp.timeout = cpu_to_le16(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) static void hci_conn_auto_accept(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) struct hci_conn *conn = container_of(work, struct hci_conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) auto_accept_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) &conn->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) static void le_disable_advertising(struct hci_dev *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (ext_adv_capable(hdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) struct hci_cp_le_set_ext_adv_enable cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) cp.enable = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) cp.num_of_sets = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) hci_send_cmd(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, sizeof(cp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) u8 enable = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) &enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) static void le_conn_timeout(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct hci_conn *conn = container_of(work, struct hci_conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) le_conn_timeout.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) BT_DBG("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) /* We could end up here due to having done directed advertising,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * so clean up the state if necessary. This should however only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * happen with broken hardware or if low duty cycle was used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * (which doesn't have a timeout of its own).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (conn->role == HCI_ROLE_SLAVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) /* Disable LE Advertising */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) le_disable_advertising(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) hci_dev_lock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) hci_le_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) hci_dev_unlock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) u8 role)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) struct hci_conn *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) BT_DBG("%s dst %pMR", hdev->name, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) conn = kzalloc(sizeof(*conn), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (!conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) bacpy(&conn->dst, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) bacpy(&conn->src, &hdev->bdaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) conn->hdev = hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) conn->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) conn->role = role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) conn->mode = HCI_CM_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) conn->state = BT_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) conn->auth_type = HCI_AT_GENERAL_BONDING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) conn->io_capability = hdev->io_capability;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) conn->remote_auth = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) conn->key_type = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) conn->rssi = HCI_RSSI_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) conn->tx_power = HCI_TX_POWER_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) conn->max_tx_power = HCI_TX_POWER_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) conn->disc_timeout = HCI_DISCONN_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) /* Set Default Authenticated payload timeout to 30s */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) conn->auth_payload_timeout = DEFAULT_AUTH_PAYLOAD_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (conn->role == HCI_ROLE_MASTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) conn->out = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) case ACL_LINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) case LE_LINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) /* conn->src should reflect the local identity address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) case SCO_LINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (lmp_esco_capable(hdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) (hdev->esco_type & EDR_ESCO_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) case ESCO_LINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) skb_queue_head_init(&conn->data_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) INIT_LIST_HEAD(&conn->chan_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) INIT_WORK(&conn->le_scan_cleanup, le_scan_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) atomic_set(&conn->refcnt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) hci_dev_hold(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) hci_conn_hash_add(hdev, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) /* The SCO and eSCO connections will only be notified when their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) * setup has been completed. This is different to ACL links which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) * can be notified right away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) if (conn->type != SCO_LINK && conn->type != ESCO_LINK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (hdev->notify)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) hci_conn_init_sysfs(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) return conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) int hci_conn_del(struct hci_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) cancel_delayed_work_sync(&conn->disc_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) cancel_delayed_work_sync(&conn->auto_accept_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) cancel_delayed_work_sync(&conn->idle_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if (conn->type == ACL_LINK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) struct hci_conn *sco = conn->link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (sco)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) sco->link = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) /* Unacked frames */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) hdev->acl_cnt += conn->sent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) } else if (conn->type == LE_LINK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) cancel_delayed_work(&conn->le_conn_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) if (hdev->le_pkts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) hdev->le_cnt += conn->sent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) hdev->acl_cnt += conn->sent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) struct hci_conn *acl = conn->link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) if (acl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) acl->link = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) hci_conn_drop(acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) if (conn->amp_mgr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) amp_mgr_put(conn->amp_mgr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) skb_queue_purge(&conn->data_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) /* Remove the connection from the list and cleanup its remaining
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) * state. This is a separate function since for some cases like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) * BT_CONNECT_SCAN we *only* want the cleanup part without the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) * rest of hci_conn_del.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) hci_conn_cleanup(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, uint8_t src_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) int use_src = bacmp(src, BDADDR_ANY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) struct hci_dev *hdev = NULL, *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) BT_DBG("%pMR -> %pMR", src, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) read_lock(&hci_dev_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) list_for_each_entry(d, &hci_dev_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) if (!test_bit(HCI_UP, &d->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) hci_dev_test_flag(d, HCI_USER_CHANNEL) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) d->dev_type != HCI_PRIMARY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) /* Simple routing:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) * No source address - find interface with bdaddr != dst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) * Source address - find interface with bdaddr == src
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) if (use_src) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) bdaddr_t id_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) u8 id_addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) if (src_type == BDADDR_BREDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) if (!lmp_bredr_capable(d))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) bacpy(&id_addr, &d->bdaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) id_addr_type = BDADDR_BREDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) if (!lmp_le_capable(d))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) hci_copy_identity_address(d, &id_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) &id_addr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) /* Convert from HCI to three-value type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) if (id_addr_type == ADDR_LE_DEV_PUBLIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) id_addr_type = BDADDR_LE_PUBLIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) id_addr_type = BDADDR_LE_RANDOM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) if (!bacmp(&id_addr, src) && id_addr_type == src_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) hdev = d; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) if (bacmp(&d->bdaddr, dst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) hdev = d; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) if (hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) hdev = hci_dev_hold(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) read_unlock(&hci_dev_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) return hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) EXPORT_SYMBOL(hci_get_route);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) /* This function requires the caller holds hdev->lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) void hci_le_conn_failed(struct hci_conn *conn, u8 status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) struct hci_conn_params *params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) conn->dst_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) if (params && params->conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) hci_conn_drop(params->conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) hci_conn_put(params->conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) params->conn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) conn->state = BT_CLOSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) /* If the status indicates successful cancellation of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) * the attempt (i.e. Unkown Connection Id) there's no point of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) * notifying failure since we'll go back to keep trying to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) * connect. The only exception is explicit connect requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) * where a timeout + cancel does indicate an actual failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) if (status != HCI_ERROR_UNKNOWN_CONN_ID ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) (params && params->explicit_connect))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) mgmt_connect_failed(hdev, &conn->dst, conn->type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) conn->dst_type, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) hci_connect_cfm(conn, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) hci_conn_del(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) /* Since we may have temporarily stopped the background scanning in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) * favor of connection establishment, we should restart it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) hci_update_background_scan(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) /* Re-enable advertising in case this was a failed connection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) * attempt as a peripheral.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) hci_req_reenable_advertising(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) static void create_le_conn_complete(struct hci_dev *hdev, u8 status, u16 opcode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) struct hci_conn *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) hci_dev_lock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) conn = hci_lookup_le_connect(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) if (!status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) hci_connect_le_scan_cleanup(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) bt_dev_err(hdev, "request failed to create LE connection: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) "status 0x%2.2x", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) if (!conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) hci_le_conn_failed(conn, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) hci_dev_unlock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) static bool conn_use_rpa(struct hci_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) return hci_dev_test_flag(hdev, HCI_PRIVACY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) static void set_ext_conn_params(struct hci_conn *conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) struct hci_cp_le_ext_conn_param *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) memset(p, 0, sizeof(*p));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) p->conn_latency = cpu_to_le16(conn->le_conn_latency);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) p->min_ce_len = cpu_to_le16(0x0000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) p->max_ce_len = cpu_to_le16(0x0000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) static void hci_req_add_le_create_conn(struct hci_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) struct hci_conn *conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) bdaddr_t *direct_rpa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) u8 own_addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) /* If direct address was provided we use it instead of current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) * address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) if (direct_rpa) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) if (bacmp(&req->hdev->random_addr, direct_rpa))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) direct_rpa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) /* direct address is always RPA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) own_addr_type = ADDR_LE_DEV_RANDOM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) /* Update random address, but set require_privacy to false so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) * that we never connect with an non-resolvable address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) if (hci_update_random_address(req, false, conn_use_rpa(conn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) &own_addr_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) if (use_ext_conn(hdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) struct hci_cp_le_ext_create_conn *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) struct hci_cp_le_ext_conn_param *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) u8 data[sizeof(*cp) + sizeof(*p) * 3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) u32 plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) cp = (void *) data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) p = (void *) cp->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) memset(cp, 0, sizeof(*cp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) bacpy(&cp->peer_addr, &conn->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) cp->peer_addr_type = conn->dst_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) cp->own_addr_type = own_addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) plen = sizeof(*cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) if (scan_1m(hdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) cp->phys |= LE_SCAN_PHY_1M;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) set_ext_conn_params(conn, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) plen += sizeof(*p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) if (scan_2m(hdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) cp->phys |= LE_SCAN_PHY_2M;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) set_ext_conn_params(conn, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) plen += sizeof(*p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) if (scan_coded(hdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) cp->phys |= LE_SCAN_PHY_CODED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) set_ext_conn_params(conn, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) plen += sizeof(*p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) hci_req_add(req, HCI_OP_LE_EXT_CREATE_CONN, plen, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) struct hci_cp_le_create_conn cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) memset(&cp, 0, sizeof(cp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) bacpy(&cp.peer_addr, &conn->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) cp.peer_addr_type = conn->dst_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) cp.own_address_type = own_addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) cp.min_ce_len = cpu_to_le16(0x0000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) cp.max_ce_len = cpu_to_le16(0x0000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) hci_req_add(req, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) conn->state = BT_CONNECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) clear_bit(HCI_CONN_SCANNING, &conn->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) static void hci_req_directed_advertising(struct hci_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) struct hci_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) struct hci_dev *hdev = req->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) u8 own_addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) u8 enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) if (ext_adv_capable(hdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) struct hci_cp_le_set_ext_adv_params cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) bdaddr_t random_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) /* Set require_privacy to false so that the remote device has a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) * chance of identifying us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) if (hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) &own_addr_type, &random_addr) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) memset(&cp, 0, sizeof(cp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) cp.own_addr_type = own_addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) cp.channel_map = hdev->le_adv_channel_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) cp.tx_power = HCI_TX_POWER_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) cp.primary_phy = HCI_ADV_PHY_1M;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) cp.secondary_phy = HCI_ADV_PHY_1M;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) cp.handle = 0; /* Use instance 0 for directed adv */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) cp.own_addr_type = own_addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) cp.peer_addr_type = conn->dst_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) bacpy(&cp.peer_addr, &conn->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) /* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) * does not supports advertising data when the advertising set already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) * contains some, the controller shall return erroc code 'Invalid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) * HCI Command Parameters(0x12).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) * So it is required to remove adv set for handle 0x00. since we use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) * instance 0 for directed adv.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) __hci_req_remove_ext_adv_instance(req, cp.handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) hci_req_add(req, HCI_OP_LE_SET_EXT_ADV_PARAMS, sizeof(cp), &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) if (own_addr_type == ADDR_LE_DEV_RANDOM &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) bacmp(&random_addr, BDADDR_ANY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) bacmp(&random_addr, &hdev->random_addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) struct hci_cp_le_set_adv_set_rand_addr cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) memset(&cp, 0, sizeof(cp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) cp.handle = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) bacpy(&cp.bdaddr, &random_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) hci_req_add(req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) sizeof(cp), &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) __hci_req_enable_ext_advertising(req, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) struct hci_cp_le_set_adv_param cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) /* Clear the HCI_LE_ADV bit temporarily so that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) * hci_update_random_address knows that it's safe to go ahead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) * and write a new random address. The flag will be set back on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) * as soon as the SET_ADV_ENABLE HCI command completes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) hci_dev_clear_flag(hdev, HCI_LE_ADV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) /* Set require_privacy to false so that the remote device has a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) * chance of identifying us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) if (hci_update_random_address(req, false, conn_use_rpa(conn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) &own_addr_type) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) memset(&cp, 0, sizeof(cp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) /* Some controllers might reject command if intervals are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) * within range for undirected advertising.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) * BCM20702A0 is known to be affected by this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) cp.min_interval = cpu_to_le16(0x0020);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) cp.max_interval = cpu_to_le16(0x0020);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) cp.type = LE_ADV_DIRECT_IND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) cp.own_address_type = own_addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) cp.direct_addr_type = conn->dst_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) bacpy(&cp.direct_addr, &conn->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) cp.channel_map = hdev->le_adv_channel_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) enable = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) &enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) conn->state = BT_CONNECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) u8 dst_type, u8 sec_level, u16 conn_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) u8 role, bdaddr_t *direct_rpa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) struct hci_conn_params *params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) struct hci_conn *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) struct smp_irk *irk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) struct hci_request req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) /* This ensures that during disable le_scan address resolution
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) * will not be disabled if it is followed by le_create_conn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) bool rpa_le_conn = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) /* Let's make sure that le is enabled.*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) if (lmp_le_capable(hdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) return ERR_PTR(-ECONNREFUSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) return ERR_PTR(-EOPNOTSUPP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) /* Since the controller supports only one LE connection attempt at a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) * time, we return -EBUSY if there is any connection attempt running.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) if (hci_lookup_le_connect(hdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) return ERR_PTR(-EBUSY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) /* If there's already a connection object but it's not in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) * scanning state it means it must already be established, in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) * which case we can't do anything else except report a failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) * to connect.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) if (conn && !test_bit(HCI_CONN_SCANNING, &conn->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) return ERR_PTR(-EBUSY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) /* When given an identity address with existing identity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) * resolving key, the connection needs to be established
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) * to a resolvable random address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) * Storing the resolvable random address is required here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) * to handle connection failures. The address will later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) * be resolved back into the original identity address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) * from the connect request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) irk = hci_find_irk_by_addr(hdev, dst, dst_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) if (irk && bacmp(&irk->rpa, BDADDR_ANY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) dst = &irk->rpa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) dst_type = ADDR_LE_DEV_RANDOM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) if (conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) bacpy(&conn->dst, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) conn = hci_conn_add(hdev, LE_LINK, dst, role);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) if (!conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) hci_conn_hold(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) conn->pending_sec_level = sec_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) conn->dst_type = dst_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) conn->sec_level = BT_SECURITY_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) conn->conn_timeout = conn_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) hci_req_init(&req, hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) /* Disable advertising if we're active. For master role
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) * connections most controllers will refuse to connect if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) * advertising is enabled, and for slave role connections we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) * anyway have to disable it in order to start directed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) * advertising.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) if (hci_dev_test_flag(hdev, HCI_LE_ADV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) __hci_req_disable_advertising(&req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) /* If requested to connect as slave use directed advertising */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) if (conn->role == HCI_ROLE_SLAVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) /* If we're active scanning most controllers are unable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) * to initiate advertising. Simply reject the attempt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) hdev->le_scan_type == LE_SCAN_ACTIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) hci_req_purge(&req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) hci_conn_del(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) return ERR_PTR(-EBUSY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) hci_req_directed_advertising(&req, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) goto create_conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) if (params) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) conn->le_conn_min_interval = params->conn_min_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) conn->le_conn_max_interval = params->conn_max_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) conn->le_conn_latency = params->conn_latency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) conn->le_supv_timeout = params->supervision_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) conn->le_conn_min_interval = hdev->le_conn_min_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) conn->le_conn_max_interval = hdev->le_conn_max_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) conn->le_conn_latency = hdev->le_conn_latency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) conn->le_supv_timeout = hdev->le_supv_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) /* If controller is scanning, we stop it since some controllers are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) * not able to scan and connect at the same time. Also set the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) * handler for scan disabling knows to set the correct discovery
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) * state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) hci_req_add_le_scan_disable(&req, rpa_le_conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) hci_req_add_le_create_conn(&req, conn, direct_rpa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) create_conn:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) err = hci_req_run(&req, create_le_conn_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) hci_conn_del(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) return conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) struct hci_conn *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) conn = hci_conn_hash_lookup_le(hdev, addr, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) if (!conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) if (conn->state != BT_CONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) /* This function requires the caller holds hdev->lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) static int hci_explicit_conn_params_set(struct hci_dev *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) bdaddr_t *addr, u8 addr_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) struct hci_conn_params *params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) if (is_connected(hdev, addr, addr_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) return -EISCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) params = hci_conn_params_lookup(hdev, addr, addr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) if (!params) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) params = hci_conn_params_add(hdev, addr, addr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) if (!params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) /* If we created new params, mark them to be deleted in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) * hci_connect_le_scan_cleanup. It's different case than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) * existing disabled params, those will stay after cleanup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) /* We're trying to connect, so make sure params are at pend_le_conns */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) params->auto_connect == HCI_AUTO_CONN_REPORT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) list_del_init(¶ms->action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) list_add(¶ms->action, &hdev->pend_le_conns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) params->explicit_connect = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) params->auto_connect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) /* This function requires the caller holds hdev->lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) u8 dst_type, u8 sec_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) u16 conn_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) enum conn_reasons conn_reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) struct hci_conn *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) /* Let's make sure that le is enabled.*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) if (lmp_le_capable(hdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) return ERR_PTR(-ECONNREFUSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) return ERR_PTR(-EOPNOTSUPP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) /* Some devices send ATT messages as soon as the physical link is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) * established. To be able to handle these ATT messages, the user-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) * space first establishes the connection and then starts the pairing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) * process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) * So if a hci_conn object already exists for the following connection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) * attempt, we simply update pending_sec_level and auth_type fields
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) * and return the object found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) if (conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) if (conn->pending_sec_level < sec_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) conn->pending_sec_level = sec_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) BT_DBG("requesting refresh of dst_addr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) conn = hci_conn_add(hdev, LE_LINK, dst, HCI_ROLE_MASTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) if (!conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) if (hci_explicit_conn_params_set(hdev, dst, dst_type) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) hci_conn_del(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) return ERR_PTR(-EBUSY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) conn->state = BT_CONNECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) set_bit(HCI_CONN_SCANNING, &conn->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) conn->dst_type = dst_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) conn->sec_level = BT_SECURITY_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) conn->pending_sec_level = sec_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) conn->conn_timeout = conn_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) conn->conn_reason = conn_reason;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) hci_update_background_scan(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) hci_conn_hold(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) return conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) u8 sec_level, u8 auth_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) enum conn_reasons conn_reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) struct hci_conn *acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) if (lmp_bredr_capable(hdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) return ERR_PTR(-ECONNREFUSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) return ERR_PTR(-EOPNOTSUPP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) if (!acl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) acl = hci_conn_add(hdev, ACL_LINK, dst, HCI_ROLE_MASTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) if (!acl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) hci_conn_hold(acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) acl->conn_reason = conn_reason;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) acl->sec_level = BT_SECURITY_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) acl->pending_sec_level = sec_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) acl->auth_type = auth_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) hci_acl_create_connection(acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) return acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) __u16 setting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) struct hci_conn *acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) struct hci_conn *sco;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) CONN_REASON_SCO_CONNECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) if (IS_ERR(acl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) return acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) sco = hci_conn_hash_lookup_ba(hdev, type, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) if (!sco) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) sco = hci_conn_add(hdev, type, dst, HCI_ROLE_MASTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) if (!sco) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) hci_conn_drop(acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) acl->link = sco;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) sco->link = acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) hci_conn_hold(sco);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) sco->setting = setting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) if (acl->state == BT_CONNECTED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) set_bit(HCI_CONN_POWER_SAVE, &acl->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) /* defer SCO setup until mode change completed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) return sco;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) hci_sco_setup(acl, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) return sco;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) /* Check link security requirement */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) int hci_conn_check_link_mode(struct hci_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) BT_DBG("hcon %p", conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) /* In Secure Connections Only mode, it is required that Secure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) * Connections is used and the link is encrypted with AES-CCM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) * using a P-256 authenticated combination key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) if (hci_dev_test_flag(conn->hdev, HCI_SC_ONLY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) if (!hci_conn_sc_enabled(conn) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) !test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) conn->key_type != HCI_LK_AUTH_COMBINATION_P256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) /* AES encryption is required for Level 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) * BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 3, Part C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) * page 1319:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) * 128-bit equivalent strength for link and encryption keys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) * required using FIPS approved algorithms (E0 not allowed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) * SAFER+ not allowed, and P-192 not allowed; encryption key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) * not shortened)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) if (conn->sec_level == BT_SECURITY_FIPS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) !test_bit(HCI_CONN_AES_CCM, &conn->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) bt_dev_err(conn->hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) "Invalid security: Missing AES-CCM usage");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) if (hci_conn_ssp_enabled(conn) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) !test_bit(HCI_CONN_ENCRYPT, &conn->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) /* Authenticate remote device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) BT_DBG("hcon %p", conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) if (conn->pending_sec_level > sec_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) sec_level = conn->pending_sec_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) if (sec_level > conn->sec_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) conn->pending_sec_level = sec_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) else if (test_bit(HCI_CONN_AUTH, &conn->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) /* Make sure we preserve an existing MITM requirement*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) auth_type |= (conn->auth_type & 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) conn->auth_type = auth_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) struct hci_cp_auth_requested cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) cp.handle = cpu_to_le16(conn->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) sizeof(cp), &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) /* If we're already encrypted set the REAUTH_PEND flag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) * otherwise set the ENCRYPT_PEND.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) set_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) /* Encrypt the link */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) static void hci_conn_encrypt(struct hci_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) BT_DBG("hcon %p", conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) struct hci_cp_set_conn_encrypt cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) cp.handle = cpu_to_le16(conn->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) cp.encrypt = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) /* Enable security */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) bool initiator)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) BT_DBG("hcon %p", conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) if (conn->type == LE_LINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) return smp_conn_security(conn, sec_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) /* For sdp we don't need the link key. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) if (sec_level == BT_SECURITY_SDP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) /* For non 2.1 devices and low security level we don't need the link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) key. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) /* For other security levels we need the link key. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) if (!test_bit(HCI_CONN_AUTH, &conn->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) goto auth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) /* An authenticated FIPS approved combination key has sufficient
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) * security for security level 4. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) sec_level == BT_SECURITY_FIPS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) goto encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) /* An authenticated combination key has sufficient security for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) security level 3. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) if ((conn->key_type == HCI_LK_AUTH_COMBINATION_P192 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) conn->key_type == HCI_LK_AUTH_COMBINATION_P256) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) sec_level == BT_SECURITY_HIGH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) goto encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) /* An unauthenticated combination key has sufficient security for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) security level 1 and 2. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) if ((conn->key_type == HCI_LK_UNAUTH_COMBINATION_P192 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) conn->key_type == HCI_LK_UNAUTH_COMBINATION_P256) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) goto encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) /* A combination key has always sufficient security for the security
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) levels 1 or 2. High security level requires the combination key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) is generated using maximum PIN code length (16).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) For pre 2.1 units. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) if (conn->key_type == HCI_LK_COMBINATION &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) conn->pin_length == 16))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) goto encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) auth:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) if (initiator)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) if (!hci_conn_auth(conn, sec_level, auth_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) encrypt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) /* Ensure that the encryption key size has been read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) * otherwise stall the upper layer responses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) if (!conn->enc_key_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) /* Nothing else needed, all requirements are met */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) hci_conn_encrypt(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) EXPORT_SYMBOL(hci_conn_security);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) /* Check secure link requirement */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) BT_DBG("hcon %p", conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) /* Accept if non-secure or higher security level is required */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) if (sec_level != BT_SECURITY_HIGH && sec_level != BT_SECURITY_FIPS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) /* Accept if secure or higher security level is already present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) if (conn->sec_level == BT_SECURITY_HIGH ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) conn->sec_level == BT_SECURITY_FIPS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) /* Reject not secure link */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) EXPORT_SYMBOL(hci_conn_check_secure);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) /* Switch role */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) BT_DBG("hcon %p", conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) if (role == conn->role)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) struct hci_cp_switch_role cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) bacpy(&cp.bdaddr, &conn->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) cp.role = role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) EXPORT_SYMBOL(hci_conn_switch_role);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) /* Enter active mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) BT_DBG("hcon %p mode %d", conn, conn->mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) if (conn->mode != HCI_CM_SNIFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) goto timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) goto timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) struct hci_cp_exit_sniff_mode cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) cp.handle = cpu_to_le16(conn->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) timer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) if (hdev->idle_timeout > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) queue_delayed_work(hdev->workqueue, &conn->idle_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) msecs_to_jiffies(hdev->idle_timeout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) /* Drop all connection on the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) void hci_conn_hash_flush(struct hci_dev *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) struct hci_conn_hash *h = &hdev->conn_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) struct hci_conn *c, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) BT_DBG("hdev %s", hdev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) list_for_each_entry_safe(c, n, &h->list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) c->state = BT_CLOSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) hci_disconn_cfm(c, HCI_ERROR_LOCAL_HOST_TERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) hci_conn_del(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) /* Check pending connect attempts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) void hci_conn_check_pending(struct hci_dev *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) struct hci_conn *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) BT_DBG("hdev %s", hdev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) hci_dev_lock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) if (conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) hci_acl_create_connection(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) hci_dev_unlock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) static u32 get_link_mode(struct hci_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) u32 link_mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) if (conn->role == HCI_ROLE_MASTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) link_mode |= HCI_LM_MASTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) link_mode |= HCI_LM_ENCRYPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) if (test_bit(HCI_CONN_AUTH, &conn->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) link_mode |= HCI_LM_AUTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) if (test_bit(HCI_CONN_SECURE, &conn->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) link_mode |= HCI_LM_SECURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) if (test_bit(HCI_CONN_FIPS, &conn->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) link_mode |= HCI_LM_FIPS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) return link_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) int hci_get_conn_list(void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) struct hci_conn *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) struct hci_conn_list_req req, *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) struct hci_conn_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) struct hci_dev *hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) int n = 0, size, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) if (copy_from_user(&req, arg, sizeof(req)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) size = sizeof(req) + req.conn_num * sizeof(*ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) cl = kmalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) if (!cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) hdev = hci_dev_get(req.dev_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) if (!hdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) kfree(cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) ci = cl->conn_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) hci_dev_lock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) list_for_each_entry(c, &hdev->conn_hash.list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) bacpy(&(ci + n)->bdaddr, &c->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) (ci + n)->handle = c->handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) (ci + n)->type = c->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) (ci + n)->out = c->out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) (ci + n)->state = c->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) (ci + n)->link_mode = get_link_mode(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) if (++n >= req.conn_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) hci_dev_unlock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) cl->dev_id = hdev->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) cl->conn_num = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) size = sizeof(req) + n * sizeof(*ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) hci_dev_put(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) err = copy_to_user(arg, cl, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) kfree(cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) return err ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) struct hci_conn_info_req req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) struct hci_conn_info ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) struct hci_conn *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) char __user *ptr = arg + sizeof(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) if (copy_from_user(&req, arg, sizeof(req)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) hci_dev_lock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) if (conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) bacpy(&ci.bdaddr, &conn->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) ci.handle = conn->handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) ci.type = conn->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) ci.out = conn->out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) ci.state = conn->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) ci.link_mode = get_link_mode(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) hci_dev_unlock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) if (!conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) struct hci_auth_info_req req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) struct hci_conn *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) if (copy_from_user(&req, arg, sizeof(req)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) hci_dev_lock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) if (conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) req.type = conn->auth_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) hci_dev_unlock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) if (!conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) struct hci_chan *hci_chan_create(struct hci_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) struct hci_chan *chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) BT_DBG("%s hcon %p", hdev->name, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) if (test_bit(HCI_CONN_DROP, &conn->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) BT_DBG("Refusing to create new hci_chan");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) chan = kzalloc(sizeof(*chan), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) if (!chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) chan->conn = hci_conn_get(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) skb_queue_head_init(&chan->data_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) chan->state = BT_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) list_add_rcu(&chan->list, &conn->chan_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) return chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) void hci_chan_del(struct hci_chan *chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) struct hci_conn *conn = chan->conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) list_del_rcu(&chan->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) /* Prevent new hci_chan's to be created for this hci_conn */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) set_bit(HCI_CONN_DROP, &conn->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) hci_conn_put(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) skb_queue_purge(&chan->data_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) kfree(chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) void hci_chan_list_flush(struct hci_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) struct hci_chan *chan, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) BT_DBG("hcon %p", conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) list_for_each_entry_safe(chan, n, &conn->chan_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) hci_chan_del(chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) static struct hci_chan *__hci_chan_lookup_handle(struct hci_conn *hcon,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) __u16 handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) struct hci_chan *hchan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) list_for_each_entry(hchan, &hcon->chan_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) if (hchan->handle == handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) return hchan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) struct hci_conn_hash *h = &hdev->conn_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) struct hci_conn *hcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) struct hci_chan *hchan = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) list_for_each_entry_rcu(hcon, &h->list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) hchan = __hci_chan_lookup_handle(hcon, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) if (hchan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) return hchan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) u32 hci_conn_get_phy(struct hci_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) u32 phys = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) /* BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 2, Part B page 471:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) * Table 6.2: Packets defined for synchronous, asynchronous, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) * CSB logical transport types.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) switch (conn->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) case SCO_LINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) /* SCO logical transport (1 Mb/s):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) * HV1, HV2, HV3 and DV.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) phys |= BT_PHY_BR_1M_1SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) case ACL_LINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) /* ACL logical transport (1 Mb/s) ptt=0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) * DH1, DM3, DH3, DM5 and DH5.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) phys |= BT_PHY_BR_1M_1SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) if (conn->pkt_type & (HCI_DM3 | HCI_DH3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) phys |= BT_PHY_BR_1M_3SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) if (conn->pkt_type & (HCI_DM5 | HCI_DH5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) phys |= BT_PHY_BR_1M_5SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) /* ACL logical transport (2 Mb/s) ptt=1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) * 2-DH1, 2-DH3 and 2-DH5.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) if (!(conn->pkt_type & HCI_2DH1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) phys |= BT_PHY_EDR_2M_1SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) if (!(conn->pkt_type & HCI_2DH3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) phys |= BT_PHY_EDR_2M_3SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) if (!(conn->pkt_type & HCI_2DH5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) phys |= BT_PHY_EDR_2M_5SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) /* ACL logical transport (3 Mb/s) ptt=1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) * 3-DH1, 3-DH3 and 3-DH5.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) if (!(conn->pkt_type & HCI_3DH1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) phys |= BT_PHY_EDR_3M_1SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) if (!(conn->pkt_type & HCI_3DH3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) phys |= BT_PHY_EDR_3M_3SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) if (!(conn->pkt_type & HCI_3DH5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) phys |= BT_PHY_EDR_3M_5SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) case ESCO_LINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) /* eSCO logical transport (1 Mb/s): EV3, EV4 and EV5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) phys |= BT_PHY_BR_1M_1SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) if (!(conn->pkt_type & (ESCO_EV4 | ESCO_EV5)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) phys |= BT_PHY_BR_1M_3SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) /* eSCO logical transport (2 Mb/s): 2-EV3, 2-EV5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) if (!(conn->pkt_type & ESCO_2EV3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) phys |= BT_PHY_EDR_2M_1SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) if (!(conn->pkt_type & ESCO_2EV5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) phys |= BT_PHY_EDR_2M_3SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) /* eSCO logical transport (3 Mb/s): 3-EV3, 3-EV5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) if (!(conn->pkt_type & ESCO_3EV3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) phys |= BT_PHY_EDR_3M_1SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) if (!(conn->pkt_type & ESCO_3EV5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) phys |= BT_PHY_EDR_3M_3SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) case LE_LINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) if (conn->le_tx_phy & HCI_LE_SET_PHY_1M)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) phys |= BT_PHY_LE_1M_TX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) if (conn->le_rx_phy & HCI_LE_SET_PHY_1M)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) phys |= BT_PHY_LE_1M_RX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) if (conn->le_tx_phy & HCI_LE_SET_PHY_2M)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) phys |= BT_PHY_LE_2M_TX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) if (conn->le_rx_phy & HCI_LE_SET_PHY_2M)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) phys |= BT_PHY_LE_2M_RX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) if (conn->le_tx_phy & HCI_LE_SET_PHY_CODED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) phys |= BT_PHY_LE_CODED_TX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) if (conn->le_rx_phy & HCI_LE_SET_PHY_CODED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) phys |= BT_PHY_LE_CODED_RX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) return phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) }