diff --git a/drivers/bluetooth/btintel.c b/drivers/bluetooth/btintel.c index 929018428c19..4cc5273fb175 100644 --- a/drivers/bluetooth/btintel.c +++ b/drivers/bluetooth/btintel.c @@ -1511,11 +1511,8 @@ static int btintel_read_debug_features(struct hci_dev *hdev, */ skb = __hci_cmd_sync(hdev, 0xfca6, sizeof(page_no), &page_no, HCI_INIT_TIMEOUT); - if (IS_ERR(skb)) { - bt_dev_err(hdev, "Reading supported features failed (%ld)", - PTR_ERR(skb)); + if (IS_ERR(skb)) return PTR_ERR(skb); - } if (skb->len != (sizeof(features->page1) + 3)) { bt_dev_err(hdev, "Supported features event size mismatch"); @@ -1632,8 +1629,10 @@ int btintel_set_quality_report(struct hci_dev *hdev, bool enable) * supported, need to load the additional DDC config to enable. */ err = btintel_read_debug_features(hdev, &features); - if (err) + if (err) { + bt_dev_err(hdev, "Reading supported features failed (%d)", err); return err; + } /* Set or reset the debug features. */ if (enable) @@ -1684,7 +1683,7 @@ static int btintel_register_devcoredump_support(struct hci_dev *hdev) err = btintel_read_debug_features(hdev, &features); if (err) { - bt_dev_info(hdev, "Error reading debug features"); + bt_dev_dbg(hdev, "Reading supported features failed (%d)", err); return err; } diff --git a/drivers/bluetooth/btintel_pcie.h b/drivers/bluetooth/btintel_pcie.h index 3030b4e8b750..5f315ebb4123 100644 --- a/drivers/bluetooth/btintel_pcie.h +++ b/drivers/bluetooth/btintel_pcie.h @@ -72,7 +72,7 @@ #define BTINTEL_PCIE_DBGC_CUR_DBGBUFF_STATUS (BTINTEL_PCIE_DBGC_BASE_ADDR + 0x1C) #define BTINTEL_PCIE_DBGC_DBGBUFF_WRAP_ARND (BTINTEL_PCIE_DBGC_BASE_ADDR + 0x2C) -#define BTINTEL_PCIE_DBGC_BASE_ADDR_SCP (0xf0d5d500) +#define BTINTEL_PCIE_DBGC_BASE_ADDR_SCP (0xf0d5d400) #define BTINTEL_PCIE_DBGC_CUR_DBGBUFF_STATUS_SCP (BTINTEL_PCIE_DBGC_BASE_ADDR_SCP + 0x1C) #define BTINTEL_PCIE_DBGC_DBGBUFF_WRAP_ARND_SCP (BTINTEL_PCIE_DBGC_BASE_ADDR_SCP + 0x2C) #define BTINTEL_PCIE_DBGC_ALLOCATION_OFFSET 0x100 diff --git a/drivers/bluetooth/btqca.c b/drivers/bluetooth/btqca.c index 60735b70d89a..9f54e33e4250 100644 --- a/drivers/bluetooth/btqca.c +++ b/drivers/bluetooth/btqca.c @@ -983,6 +983,7 @@ int qca_uart_setup(struct hci_dev *hdev, uint8_t baudrate, switch (soc_type) { case QCA_WCN3991: case QCA_WCN6750: + case QCA_QCA2066: case QCA_WCN6855: case QCA_WCN7850: /* get fw build info */ diff --git a/drivers/bluetooth/hci_mrvl.c b/drivers/bluetooth/hci_mrvl.c index 57e2d80827df..3e542956be1a 100644 --- a/drivers/bluetooth/hci_mrvl.c +++ b/drivers/bluetooth/hci_mrvl.c @@ -57,7 +57,6 @@ struct hci_mrvl_pkt { static int mrvl_open(struct hci_uart *hu) { struct mrvl_data *mrvl; - int ret; BT_DBG("hu %p", hu); @@ -75,17 +74,7 @@ static int mrvl_open(struct hci_uart *hu) hu->priv = mrvl; - if (hu->serdev) { - ret = serdev_device_open(hu->serdev); - if (ret) - goto err; - } - return 0; -err: - kfree(mrvl); - - return ret; } static int mrvl_close(struct hci_uart *hu) @@ -94,9 +83,6 @@ static int mrvl_close(struct hci_uart *hu) BT_DBG("hu %p", hu); - if (hu->serdev) - serdev_device_close(hu->serdev); - skb_queue_purge(&mrvl->txq); skb_queue_purge(&mrvl->rawq); kfree_skb(mrvl->rx_skb); diff --git a/drivers/bluetooth/hci_qca.c b/drivers/bluetooth/hci_qca.c index 4cebd0a9998d..158f294c1890 100644 --- a/drivers/bluetooth/hci_qca.c +++ b/drivers/bluetooth/hci_qca.c @@ -1132,6 +1132,7 @@ static void qca_controller_memdump(struct work_struct *work) if (!test_bit(QCA_MEMDUMP_COLLECTION, &qca->flags)) { bt_dev_err(hu->hdev, "QCA: Discarding other packets"); kfree(qca_memdump); + qca->qca_memdump = NULL; kfree_skb(skb); mutex_unlock(&qca->hci_memdump_lock); return; @@ -1386,6 +1387,7 @@ static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate) case QCA_WCN3991: case QCA_WCN3998: case QCA_WCN6750: + case QCA_QCA2066: case QCA_WCN6855: case QCA_WCN7850: usleep_range(1000, 10000); @@ -1473,6 +1475,7 @@ static int qca_check_speeds(struct hci_uart *hu) case QCA_WCN3991: case QCA_WCN3998: case QCA_WCN6750: + case QCA_QCA2066: case QCA_WCN6855: case QCA_WCN7850: if (!qca_get_speed(hu, QCA_INIT_SPEED) && @@ -1516,6 +1519,7 @@ static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type) case QCA_WCN3991: case QCA_WCN3998: case QCA_WCN6750: + case QCA_QCA2066: case QCA_WCN6855: case QCA_WCN7850: hci_uart_set_flow_control(hu, true); @@ -1551,6 +1555,7 @@ error: case QCA_WCN3991: case QCA_WCN3998: case QCA_WCN6750: + case QCA_QCA2066: case QCA_WCN6855: case QCA_WCN7850: hci_uart_set_flow_control(hu, false); @@ -1750,6 +1755,27 @@ static bool qca_wakeup(struct hci_dev *hdev) return wakeup; } +static void qca_port_close(struct hci_uart *hu) +{ + /* HCI_UART_PROTO_READY tracks whether the port is open, so a port + * left closed by a failed qca_port_open() is not closed again by + * hci_uart_close() or hci_uart_unregister_device(). + */ + if (test_and_clear_bit(HCI_UART_PROTO_READY, &hu->flags)) + serdev_device_close(hu->serdev); +} + +static int qca_port_open(struct hci_uart *hu) +{ + int ret; + + ret = serdev_device_open(hu->serdev); + if (!ret) + set_bit(HCI_UART_PROTO_READY, &hu->flags); + + return ret; +} + static int qca_port_reopen(struct hci_uart *hu) { int ret; @@ -1759,8 +1785,8 @@ static int qca_port_reopen(struct hci_uart *hu) * Without this, we will have RTS and CTS synchronization * issues. */ - serdev_device_close(hu->serdev); - ret = serdev_device_open(hu->serdev); + qca_port_close(hu); + ret = qca_port_open(hu); if (ret) { bt_dev_err(hu->hdev, "failed to open port"); return ret; @@ -1784,12 +1810,12 @@ static int qca_regulator_init(struct hci_uart *hu) qcadev = serdev_device_get_drvdata(hu->serdev); if (!qcadev->bt_power->vregs_on) { - serdev_device_close(hu->serdev); + qca_port_close(hu); ret = qca_regulator_enable(qcadev); if (ret) return ret; - ret = serdev_device_open(hu->serdev); + ret = qca_port_open(hu); if (ret) { bt_dev_err(hu->hdev, "failed to open port"); return ret; @@ -1867,6 +1893,7 @@ static int qca_power_on(struct hci_dev *hdev) case QCA_WCN3991: case QCA_WCN3998: case QCA_WCN6750: + case QCA_QCA2066: case QCA_WCN6855: case QCA_WCN7850: ret = qca_regulator_init(hu); @@ -1989,6 +2016,7 @@ retry: case QCA_WCN3991: case QCA_WCN3998: case QCA_WCN6750: + case QCA_QCA2066: case QCA_WCN6855: case QCA_WCN7850: if (qcadev && qcadev->bdaddr_property_broken) @@ -2022,6 +2050,7 @@ retry: case QCA_WCN3991: case QCA_WCN3998: case QCA_WCN6750: + case QCA_QCA2066: case QCA_WCN6855: case QCA_WCN7850: break; @@ -2065,8 +2094,8 @@ out: if (retries < MAX_INIT_RETRIES) { bt_dev_warn(hdev, "Retry BT power ON:%d", retries); if (hu->serdev) { - serdev_device_close(hu->serdev); - ret = serdev_device_open(hu->serdev); + qca_port_close(hu); + ret = qca_port_open(hu); if (ret) { bt_dev_err(hdev, "failed to open port"); return ret; @@ -2284,6 +2313,7 @@ static void qca_power_off(struct hci_uart *hu) break; case QCA_WCN6750: + case QCA_QCA2066: case QCA_WCN6855: gpiod_set_value_cansleep(qcadev->bt_en, 0); msleep(100); @@ -2438,6 +2468,7 @@ static int qca_serdev_probe(struct serdev_device *serdev) case QCA_WCN3991: case QCA_WCN3998: case QCA_WCN6750: + case QCA_QCA2066: case QCA_WCN6855: case QCA_WCN7850: qcadev->bt_power = devm_kzalloc_obj(&serdev->dev, @@ -2456,6 +2487,7 @@ static int qca_serdev_probe(struct serdev_device *serdev) case QCA_WCN3991: case QCA_WCN3998: case QCA_WCN6750: + case QCA_QCA2066: case QCA_WCN6855: case QCA_WCN7850: if (!device_property_present(&serdev->dev, "enable-gpios")) { @@ -2499,6 +2531,7 @@ static int qca_serdev_probe(struct serdev_device *serdev) if (!qcadev->bt_en && (data->soc_type == QCA_WCN6750 || + data->soc_type == QCA_QCA2066 || data->soc_type == QCA_WCN6855 || data->soc_type == QCA_WCN7850)) power_ctrl_enabled = false; @@ -2507,6 +2540,7 @@ static int qca_serdev_probe(struct serdev_device *serdev) GPIOD_IN); if (IS_ERR(qcadev->sw_ctrl) && (data->soc_type == QCA_WCN6750 || + data->soc_type == QCA_QCA2066 || data->soc_type == QCA_WCN6855 || data->soc_type == QCA_WCN7850)) { dev_err(&serdev->dev, "failed to acquire SW_CTRL gpio\n"); @@ -2591,6 +2625,7 @@ static void qca_serdev_remove(struct serdev_device *serdev) case QCA_WCN3991: case QCA_WCN3998: case QCA_WCN6750: + case QCA_QCA2066: case QCA_WCN6855: case QCA_WCN7850: if (power->vregs_on) diff --git a/drivers/bluetooth/hci_serdev.c b/drivers/bluetooth/hci_serdev.c index 48ba017e219b..194f11251cc5 100644 --- a/drivers/bluetooth/hci_serdev.c +++ b/drivers/bluetooth/hci_serdev.c @@ -97,15 +97,26 @@ static int hci_uart_flush(struct hci_dev *hdev) BT_DBG("hdev %p serdev %p", hdev, hu->serdev); + /* Stop write_work and hold SENDING so wakeups only set TX_WAKEUP. */ + disable_work_sync(&hu->write_work); + set_bit(HCI_UART_SENDING, &hu->tx_state); + if (hu->tx_skb) { kfree_skb(hu->tx_skb); hu->tx_skb = NULL; } - /* Flush any pending characters in the driver and discipline. */ - serdev_device_write_flush(hu->serdev); + if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) { + /* Flush any pending characters in the driver and discipline. */ + serdev_device_write_flush(hu->serdev); - if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) hu->proto->flush(hu); + } + + /* Resume TX and replay a wakeup recorded while it was stopped. */ + enable_work(&hu->write_work); + clear_bit(HCI_UART_SENDING, &hu->tx_state); + if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state)) + hci_uart_tx_wakeup(hu); return 0; } diff --git a/drivers/bluetooth/hci_vhci.c b/drivers/bluetooth/hci_vhci.c index 8f7db0102b25..7fec02705e58 100644 --- a/drivers/bluetooth/hci_vhci.c +++ b/drivers/bluetooth/hci_vhci.c @@ -524,7 +524,9 @@ static inline ssize_t vhci_get_user(struct vhci_data *data, case HCI_ACLDATA_PKT: case HCI_SCODATA_PKT: case HCI_ISODATA_PKT: + mutex_lock(&data->open_mutex); if (!data->hdev) { + mutex_unlock(&data->open_mutex); kfree_skb(skb); return -ENODEV; } @@ -532,6 +534,7 @@ static inline ssize_t vhci_get_user(struct vhci_data *data, hci_skb_pkt_type(skb) = pkt_type; ret = hci_recv_frame(data->hdev, skb); + mutex_unlock(&data->open_mutex); break; case HCI_VENDOR_PKT: @@ -701,6 +704,8 @@ static int vhci_release(struct inode *inode, struct file *file) if (hdev) { if (!IS_ERR_OR_NULL(hdev->debugfs)) vhci_debugfs_remove(hdev); + /* Drain work queued by the last force_suspend writers. */ + flush_work(&data->suspend_work); hci_unregister_dev(hdev); hci_free_dev(hdev); } diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h index 6fb713d5e4be..fd4da751a24f 100644 --- a/include/net/bluetooth/hci_core.h +++ b/include/net/bluetooth/hci_core.h @@ -1972,6 +1972,7 @@ struct adv_info *hci_add_per_instance(struct hci_dev *hdev, u8 instance, u8 sid, int hci_set_adv_instance_data(struct hci_dev *hdev, u8 instance, u16 adv_data_len, u8 *adv_data, u16 scan_rsp_len, u8 *scan_rsp_data); +void hci_free_adv_instance(struct hci_dev *hdev, struct adv_info *adv_instance); int hci_remove_adv_instance(struct hci_dev *hdev, u8 instance); void hci_adv_instances_set_rpa_expired(struct hci_dev *hdev, bool rpa_expired); u32 hci_adv_instance_flags(struct hci_dev *hdev, u8 instance); diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h index 2a05bda8ab43..bb2839972cf3 100644 --- a/include/net/bluetooth/l2cap.h +++ b/include/net/bluetooth/l2cap.h @@ -636,6 +636,7 @@ struct l2cap_conn { struct sk_buff_head pending_rx; struct work_struct pending_rx_work; + bool pending_rx_blocked __guarded_by(&lock); struct delayed_work id_addr_timer; diff --git a/net/bluetooth/6lowpan.c b/net/bluetooth/6lowpan.c index 8c51c20f6c4b..68ca2f49dadc 100644 --- a/net/bluetooth/6lowpan.c +++ b/net/bluetooth/6lowpan.c @@ -345,6 +345,7 @@ static int recv_pkt(struct sk_buff *skb, struct net_device *dev, return NET_RX_SUCCESS; drop: + kfree_skb(skb); dev->stats.rx_dropped++; return NET_RX_DROP; } @@ -356,21 +357,26 @@ static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) struct lowpan_peer *peer; int err; + /* The skb is always consumed, so never return an error that would + * make the caller free it again. + */ peer = lookup_peer(chan->conn); if (!peer) - return -ENOENT; + goto drop; dev = lookup_dev(chan->conn); if (!dev || !dev->netdev) - return -ENOENT; + goto drop; err = recv_pkt(skb, dev->netdev, peer); - if (err) { + if (err) BT_DBG("recv pkt %d", err); - err = -EAGAIN; - } - return err; + return 0; + +drop: + kfree_skb(skb); + return 0; } static int setup_header(struct sk_buff *skb, struct net_device *netdev, diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c index eebd73988bf6..2fb37e078d73 100644 --- a/net/bluetooth/hci_conn.c +++ b/net/bluetooth/hci_conn.c @@ -1019,6 +1019,15 @@ static struct hci_conn *__hci_conn_add(struct hci_dev *hdev, int type, struct hci_conn *conn; struct smp_irk *irk = NULL; + lockdep_assert_held(&hdev->lock); + + /* hci_dev_close_sync() clears HCI_UP before hci_conn_hash_flush(). + * A connection added after that would never be flushed, so refuse + * it. The caller holds hdev->lock, which the flush also takes. + */ + if (!test_bit(HCI_UP, &hdev->flags)) + return ERR_PTR(-ENETDOWN); + switch (type) { case ACL_LINK: if (!hdev->acl_mtu) @@ -1166,6 +1175,9 @@ static struct hci_conn *__hci_conn_add(struct hci_dev *hdev, int type, hci_dev_hold(hdev); + /* Initialize the device before publishing it to RCU readers. */ + hci_conn_init_sysfs(conn); + hci_conn_hash_add(hdev, conn); /* The SCO and eSCO connections will only be notified when their @@ -1177,13 +1189,13 @@ static struct hci_conn *__hci_conn_add(struct hci_dev *hdev, int type, hdev->notify(hdev, HCI_NOTIFY_CONN_ADD); } - hci_conn_init_sysfs(conn); return conn; } struct hci_conn *hci_conn_add_unset(struct hci_dev *hdev, int type, bdaddr_t *dst, u8 dst_type, u8 role) { + struct hci_conn *conn; int handle; bt_dev_dbg(hdev, "dst %pMR", dst); @@ -1192,7 +1204,11 @@ struct hci_conn *hci_conn_add_unset(struct hci_dev *hdev, int type, if (unlikely(handle < 0)) return ERR_PTR(-ECONNREFUSED); - return __hci_conn_add(hdev, type, dst, dst_type, role, handle); + conn = __hci_conn_add(hdev, type, dst, dst_type, role, handle); + if (IS_ERR(conn)) + ida_free(&hdev->unset_handle_ida, handle); + + return conn; } struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst, diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c index c97aec3caba0..846e6eb2d1bc 100644 --- a/net/bluetooth/hci_core.c +++ b/net/bluetooth/hci_core.c @@ -1610,17 +1610,11 @@ struct adv_info *hci_get_next_instance(struct hci_dev *hdev, u8 instance) } /* This function requires the caller holds hdev->lock */ -int hci_remove_adv_instance(struct hci_dev *hdev, u8 instance) +void hci_free_adv_instance(struct hci_dev *hdev, struct adv_info *adv_instance) { - struct adv_info *adv_instance; + BT_DBG("%s removing %dMR", hdev->name, adv_instance->instance); - adv_instance = hci_find_adv_instance(hdev, instance); - if (!adv_instance) - return -ENOENT; - - BT_DBG("%s removing %dMR", hdev->name, instance); - - if (hdev->cur_adv_instance == instance) { + if (hdev->cur_adv_instance == adv_instance->instance) { if (hdev->adv_instance_timeout) { cancel_delayed_work(&hdev->adv_instance_expire); hdev->adv_instance_timeout = false; @@ -1634,6 +1628,18 @@ int hci_remove_adv_instance(struct hci_dev *hdev, u8 instance) kfree(adv_instance); hdev->adv_instance_cnt--; +} + +/* This function requires the caller holds hdev->lock */ +int hci_remove_adv_instance(struct hci_dev *hdev, u8 instance) +{ + struct adv_info *adv_instance; + + adv_instance = hci_find_adv_instance(hdev, instance); + if (!adv_instance) + return -ENOENT; + + hci_free_adv_instance(hdev, adv_instance); return 0; } diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c index 5e6b25eb5053..10c8b7b8e329 100644 --- a/net/bluetooth/hci_event.c +++ b/net/bluetooth/hci_event.c @@ -6158,6 +6158,26 @@ static void hci_le_pa_sync_lost_evt(struct hci_dev *hdev, void *data, hci_dev_unlock(hdev); } +/* The instance advertising on the set identified by handle. Handle 0x00 may + * be shared by Global Advertising and an instance, e.g. instance 1 when the + * controller supports a single advertising set, in which case the instance + * enabled on it, if any, owns the set (see hci_le_ext_adv_set_enabled()). + */ +static struct adv_info *hci_le_ext_adv_owner(struct hci_dev *hdev, u8 handle) +{ + struct adv_info *adv; + + if (handle) + return hci_find_adv_instance(hdev, handle); + + list_for_each_entry(adv, &hdev->adv_instances, list) { + if (!adv->handle && adv->enabled) + return adv; + } + + return NULL; +} + static void hci_le_ext_adv_term_evt(struct hci_dev *hdev, void *data, struct sk_buff *skb) { @@ -6181,15 +6201,18 @@ static void hci_le_ext_adv_term_evt(struct hci_dev *hdev, void *data, hci_dev_lock(hdev); - adv = hci_find_adv_instance(hdev, ev->handle); + adv = hci_le_ext_adv_owner(hdev, ev->handle); if (ev->status) { + u8 instance; + if (!adv) goto unlock; /* Remove advertising as it has been terminated */ - hci_remove_adv_instance(hdev, ev->handle); - mgmt_advertising_removed(NULL, hdev, ev->handle); + instance = adv->instance; + hci_free_adv_instance(hdev, adv); + mgmt_advertising_removed(NULL, hdev, instance); list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) { if (adv->enabled) @@ -6206,22 +6229,19 @@ static void hci_le_ext_adv_term_evt(struct hci_dev *hdev, void *data, conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->conn_handle)); if (conn) { - /* Store handle in the connection so the correct advertising - * instance can be re-enabled when disconnected. + /* Store the instance in the connection so the correct + * advertising instance can be re-enabled when disconnected. */ - conn->adv_instance = ev->handle; + conn->adv_instance = adv ? adv->instance : ev->handle; if (hdev->adv_addr_type != ADDR_LE_DEV_RANDOM || bacmp(&conn->resp_addr, BDADDR_ANY)) goto unlock; - if (!ev->handle) { - bacpy(&conn->resp_addr, &hdev->random_addr); - goto unlock; - } - if (adv) bacpy(&conn->resp_addr, &adv->random_addr); + else if (!ev->handle) + bacpy(&conn->resp_addr, &hdev->random_addr); } unlock: diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c index 9213e3ef9f26..44ce776890d1 100644 --- a/net/bluetooth/hci_sync.c +++ b/net/bluetooth/hci_sync.c @@ -31,21 +31,23 @@ static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode, hdev->req_result = result; WRITE_ONCE(hdev->req_status, HCI_REQ_DONE); - /* Free the request command so it is not used as response */ + /* Free the request command so it is not used as response, and drop + * the reference hci_cmd_sync_alloc() took on its sk, if set: the + * response skb doesn't carry it. + */ hci_dev_lock(hdev); + if (hdev->req_skb) { + struct sock *sk = hci_skb_sk(hdev->req_skb); + + if (sk) + sock_put(sk); + } kfree_skb(hdev->req_skb); hdev->req_skb = NULL; hci_dev_unlock(hdev); - if (skb) { - struct sock *sk = hci_skb_sk(skb); - - /* Drop sk reference if set */ - if (sk) - sock_put(sk); - + if (skb) hdev->req_rsp = skb_get(skb); - } wake_up_interruptible(&hdev->req_wait_q); } @@ -1258,6 +1260,8 @@ static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance, bdaddr_t *random_addr) { struct hci_cp_le_set_adv_set_rand_addr cp; + struct adv_info *adv; + u8 handle = instance; int err; if (!instance) { @@ -1269,15 +1273,46 @@ static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance, err = hci_set_random_addr_sync(hdev, random_addr); if (err) return err; + } else { + hci_dev_lock(hdev); + adv = hci_find_adv_instance(hdev, instance); + if (adv) + handle = adv->handle; + hci_dev_unlock(hdev); } memset(&cp, 0, sizeof(cp)); - cp.handle = instance; + cp.handle = handle; bacpy(&cp.bdaddr, random_addr); - return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR, - sizeof(cp), &cp, HCI_CMD_TIMEOUT); + err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR, + sizeof(cp), &cp, HCI_CMD_TIMEOUT); + if (err || handle == instance) + return err; + + /* hci_cc_le_set_adv_set_random_addr() records the address of the + * instance numbered as the handle, and leaves handle 0x00 to + * hdev->random_addr, so record it here for an instance using another + * handle: instance 1 on handle 0x00, on a controller supporting a + * single advertising set. + */ + hci_dev_lock(hdev); + + adv = hci_find_adv_instance(hdev, instance); + if (adv) { + bacpy(&adv->random_addr, random_addr); + if (!bacmp(&hdev->rpa, random_addr)) { + adv->rpa_expired = false; + queue_delayed_work(hdev->workqueue, + &adv->rpa_expired_cb, + secs_to_jiffies(hdev->rpa_timeout)); + } + } + + hci_dev_unlock(hdev); + + return 0; } static int @@ -2077,6 +2112,9 @@ int hci_enable_advertising(struct hci_dev *hdev) int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance, struct sock *sk) { + struct adv_info *adv = NULL; + u8 handle = instance; + bool global = false; int err; if (!ext_adv_capable(hdev)) @@ -2086,13 +2124,53 @@ int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance, if (err) return err; - /* If request specifies an instance that doesn't exist, fail */ - if (instance > 0 && !hci_find_adv_instance(hdev, instance)) - return -EINVAL; + if (instance) { + hci_dev_lock(hdev); - return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET, - sizeof(instance), &instance, 0, - HCI_CMD_TIMEOUT, sk); + adv = hci_find_adv_instance(hdev, instance); + if (adv) { + handle = adv->handle; + + /* An instance sharing handle 0x00 with Global + * Advertising doesn't own the set while Global + * Advertising is on, and the set may still be enabled + * for it, which the controller would refuse to remove. + */ + global = !handle && + (hci_dev_test_flag(hdev, HCI_ADVERTISING) || + hci_dev_test_flag(hdev, HCI_LE_ADV_0)); + } + + hci_dev_unlock(hdev); + + /* If request specifies an instance that doesn't exist, fail */ + if (!adv) + return -EINVAL; + } + + if (!global) { + err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET, + sizeof(handle), &handle, 0, + HCI_CMD_TIMEOUT, sk); + if (err) + return err; + } + + /* hci_cc_le_remove_adv_set() removes the instance numbered as the + * handle it sent, so remove here an instance using another handle: + * instance 1 on handle 0x00, on a controller supporting a single + * advertising set. The completion can't tell it from instance 0. + */ + if (handle != instance) { + hci_dev_lock(hdev); + + if (!hci_remove_adv_instance(hdev, instance)) + mgmt_advertising_removed(sk, hdev, instance); + + hci_dev_unlock(hdev); + } + + return 0; } int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason) @@ -5694,6 +5772,7 @@ static int hci_dev_shutdown(struct hci_dev *hdev) int hci_dev_close_sync(struct hci_dev *hdev) { + struct hci_conn *conn; bool auto_off; int err = 0; @@ -5761,6 +5840,22 @@ int hci_dev_close_sync(struct hci_dev *hdev) cancel_delayed_work_sync(&adv_instance->rpa_expired_cb); } + /* A delayed work whose timer was armed before close would queue + * itself while the workqueue is draining. Disable the connection + * works so that hci_conn_drop() and the timers can no longer queue + * them. The connections are deleted below, so nothing re-enables + * them. HCI_UP is clear, so no new connection can be added after + * this walk. + */ + hci_dev_lock(hdev); + list_for_each_entry(conn, &hdev->conn_hash.list, list) { + disable_delayed_work(&conn->disc_work); + disable_delayed_work(&conn->auto_accept_work); + disable_delayed_work(&conn->idle_work); + disable_delayed_work(&conn->le_conn_timeout); + } + hci_dev_unlock(hdev); + /* Avoid potential lockdep warnings from the *_flush() calls by * ensuring the workqueue is empty up front. */ diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 7dcb0586a10b..686e86c04d7e 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -1747,10 +1747,40 @@ static void l2cap_le_conn_ready(struct l2cap_conn *conn) } } -static void l2cap_conn_ready(struct l2cap_conn *conn) +static bool l2cap_is_blocked(struct hci_conn *hcon) + __must_hold(&hcon->hdev->lock) { + /* Since we can't actively block incoming LE connections we must + * at least ensure that we ignore incoming data from them. + */ + return hcon->type == LE_LINK && + hci_bdaddr_list_lookup(&hcon->hdev->reject_list, &hcon->dst, + bdaddr_dst_type(hcon)); +} + +static void l2cap_queue_pending_rx(struct hci_conn *hcon) + __must_hold(&hcon->hdev->lock) +{ + struct l2cap_conn *conn = hcon->l2cap_data; + + mutex_lock(&conn->lock); + + if (!skb_queue_empty(&conn->pending_rx)) { + /* Teardown holds hdev->lock while cancelling the worker. + * Pass the decision under conn->lock to avoid that lock. + */ + conn->pending_rx_blocked = l2cap_is_blocked(hcon); + queue_work(hcon->hdev->workqueue, &conn->pending_rx_work); + } + + mutex_unlock(&conn->lock); +} + +static void l2cap_conn_ready(struct hci_conn *hcon) + __must_hold(&hcon->hdev->lock) +{ + struct l2cap_conn *conn = hcon->l2cap_data; struct l2cap_chan *chan; - struct hci_conn *hcon = conn->hcon; BT_DBG("conn %p", conn); @@ -1782,7 +1812,7 @@ static void l2cap_conn_ready(struct l2cap_conn *conn) if (hcon->type == LE_LINK) l2cap_le_conn_ready(conn); - queue_work(hcon->hdev->workqueue, &conn->pending_rx_work); + l2cap_queue_pending_rx(hcon); } /* Notify sockets that we cannot guaranty reliability anymore */ @@ -5701,6 +5731,16 @@ static inline int l2cap_ecred_conn_rsp(struct l2cap_conn *conn, break; } + /* + * Validate MPS and MTU per BT Core Spec + * Vol 3 Part A Sec 10.2 + */ + if (mtu < L2CAP_ECRED_MIN_MTU || + mps < L2CAP_ECRED_MIN_MPS) { + l2cap_chan_del(chan, ECONNREFUSED); + break; + } + chan->ident = 0; chan->dcid = dcid; chan->omtu = mtu; @@ -6186,6 +6226,7 @@ static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag, static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, struct l2cap_ctrl *control) { + struct sk_buff *sdu; int err = -EINVAL; switch (control->sar) { @@ -6246,15 +6287,25 @@ static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, if (chan->sdu->len != chan->sdu_len) break; - err = chan->ops->recv(chan, chan->sdu); + /* Reassembly complete. Hand the SDU over before the + * callback, for the reason given in l2cap_ecred_data_rcv(): + * the callback takes ownership and may free it, while + * l2cap_chan_rx_avail() -> l2cap_chan_le_send_credits() reads + * chan->sdu->len on the way out - it does so before the mode + * test that makes the rest of that function a no-op here. + * Return directly, since the state this leaves is what the + * trailing error block would otherwise set again. + */ + sdu = chan->sdu; + chan->sdu = NULL; + chan->sdu_last_frag = NULL; + chan->sdu_len = 0; - if (!err) { - /* Reassembly complete */ - chan->sdu = NULL; - chan->sdu_last_frag = NULL; - chan->sdu_len = 0; - } - break; + err = chan->ops->recv(chan, sdu); + if (err) + kfree_skb(sdu); + + return err; } if (err) { @@ -7099,6 +7150,9 @@ static void l2cap_chan_le_send_credits(struct l2cap_chan *chan) pkt.cid = cpu_to_le16(chan->scid); pkt.credits = cpu_to_le16(return_credits); + /* Keeps l2cap_put_ident() from releasing ident before we do */ + lockdep_assert_held(&conn->lock); + ident = l2cap_get_ident(conn); l2cap_send_cmd(conn, ident, L2CAP_LE_CREDITS, sizeof(pkt), &pkt); @@ -7245,12 +7299,26 @@ static int l2cap_ecred_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) skb = NULL; if (chan->sdu->len == chan->sdu_len) { - err = l2cap_ecred_recv(chan, chan->sdu); - if (!err) { - chan->sdu = NULL; - chan->sdu_last_frag = NULL; - chan->sdu_len = 0; - } + struct sk_buff *sdu = chan->sdu; + + /* The receive callback takes ownership of a reassembled SDU and + * may free it, while l2cap_ecred_recv() goes on to update the + * credits - which reads chan->sdu->len. Drop the reassembly + * state before handing the SDU over, and release it here if the + * callback refused it, so that it is still freed exactly once. + * The reassembly state is already clear at that point, so + * return instead of falling into the failed: block, whose + * kfree_skb(chan->sdu) would have nothing left to free. + */ + chan->sdu = NULL; + chan->sdu_last_frag = NULL; + chan->sdu_len = 0; + + err = l2cap_ecred_recv(chan, sdu); + if (err) + kfree_skb(sdu); + + return 0; } failed: @@ -7381,7 +7449,8 @@ free_skb: kfree_skb(skb); } -static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) +static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb, + bool blocked) __must_hold(&conn->lock) { struct l2cap_hdr *lh = (void *) skb->data; @@ -7408,12 +7477,7 @@ static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) return; } - /* Since we can't actively block incoming LE connections we must - * at least ensure that we ignore incoming data from them. - */ - if (hcon->type == LE_LINK && - hci_bdaddr_list_lookup(&hcon->hdev->reject_list, &hcon->dst, - bdaddr_dst_type(hcon))) { + if (blocked) { kfree_skb(skb); return; } @@ -7461,7 +7525,7 @@ static void process_pending_rx(struct work_struct *work) */ while (!test_bit(HCI_CONN_ENC_KEY_SIZE_PEND, conn->hcon->flags) && (skb = skb_dequeue(&conn->pending_rx))) - l2cap_recv_frame(conn, skb); + l2cap_recv_frame(conn, skb, conn->pending_rx_blocked); mutex_unlock(&conn->lock); } @@ -7913,7 +7977,7 @@ next: mutex_unlock(&conn->lock); - l2cap_conn_ready(conn); + l2cap_conn_ready(hcon); } int l2cap_disconn_ind(struct hci_conn *hcon) @@ -8052,8 +8116,7 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) mutex_unlock(&conn->lock); /* Process data held back while the encryption key size was read */ - if (!skb_queue_empty(&conn->pending_rx)) - queue_work(hcon->hdev->workqueue, &conn->pending_rx_work); + l2cap_queue_pending_rx(hcon); } /* Append fragment into frame respecting the maximum len of rx_skb */ @@ -8142,6 +8205,7 @@ int l2cap_recv_acldata(struct hci_dev *hdev, u16 handle, { struct hci_conn *hcon; struct l2cap_conn *conn; + bool blocked; int len; /* Lock hdev for hci_conn, and race on l2cap_data vs. l2cap_conn_del */ @@ -8164,6 +8228,7 @@ int l2cap_recv_acldata(struct hci_dev *hdev, u16 handle, conn = l2cap_conn_add(hcon); conn = l2cap_conn_hold_unless_zero(conn); + blocked = l2cap_is_blocked(hcon); hcon = NULL; hci_dev_unlock(hdev); @@ -8200,7 +8265,7 @@ int l2cap_recv_acldata(struct hci_dev *hdev, u16 handle, if (len == skb->len) { /* Complete frame received */ - l2cap_recv_frame(conn, skb); + l2cap_recv_frame(conn, skb, blocked); goto unlock; } @@ -8224,7 +8289,7 @@ int l2cap_recv_acldata(struct hci_dev *hdev, u16 handle, * setting the socket unreliable. */ skb->len = len; - l2cap_recv_frame(conn, skb); + l2cap_recv_frame(conn, skb, blocked); l2cap_conn_unreliable(conn, ECOMM); goto unlock; } @@ -8274,7 +8339,7 @@ int l2cap_recv_acldata(struct hci_dev *hdev, u16 handle, */ struct sk_buff *rx_skb = conn->rx_skb; conn->rx_skb = NULL; - l2cap_recv_frame(conn, rx_skb); + l2cap_recv_frame(conn, rx_skb, blocked); } break; } diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c index e5c15c083c1a..6b65710845dc 100644 --- a/net/bluetooth/l2cap_sock.c +++ b/net/bluetooth/l2cap_sock.c @@ -1229,11 +1229,11 @@ static void l2cap_publish_rx_avail(struct l2cap_chan *chan) /* Correct available memory by estimated sk_buff overhead. * This is significant due to small transfer sizes. However, accept - * at least one full packet if receive space is non-zero. + * at least one full SDU if receive space is non-zero. */ expected_skbs = DIV_ROUND_UP(avail, chan->mps); skb_overhead = expected_skbs * sizeof(struct sk_buff); - if (skb_overhead < avail) + if (avail - skb_overhead >= chan->imtu + L2CAP_SDULEN_SIZE) l2cap_chan_rx_avail(chan, avail - skb_overhead); else l2cap_chan_rx_avail(chan, -1); @@ -1292,6 +1292,7 @@ static int l2cap_sock_recvmsg(struct socket *sock, struct msghdr *msg, { struct sock *sk = sock->sk; struct l2cap_pinfo *pi = l2cap_pi(sk); + bool have_conn; int err; if (unlikely(flags & MSG_ERRQUEUE)) @@ -1312,9 +1313,12 @@ static int l2cap_sock_recvmsg(struct socket *sock, struct msghdr *msg, pi->chan->mode != L2CAP_MODE_EXT_FLOWCTL) return err; - lock_sock(sk); - + /* Returning credits must be serialized with the RX path */ + have_conn = l2cap_chan_lock_conn(pi->chan); l2cap_publish_rx_avail(pi->chan); + l2cap_chan_unlock_conn(pi->chan, have_conn); + + lock_sock(sk); /* Attempt to put pending rx data in the socket buffer */ while (!list_empty(&pi->rx_busy)) { diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c index fdcc752c6f13..6a617656bfbe 100644 --- a/net/bluetooth/mgmt_config.c +++ b/net/bluetooth/mgmt_config.c @@ -362,6 +362,6 @@ int set_def_runtime_config(struct sock *sk, struct hci_dev *hdev, void *data, { bt_dev_dbg(hdev, "sock %p", sk); - return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEF_SYSTEM_CONFIG, + return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEF_RUNTIME_CONFIG, MGMT_STATUS_INVALID_PARAMS); } diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c index b7015e37e1f1..6ccb38b1d56d 100644 --- a/net/bluetooth/smp.c +++ b/net/bluetooth/smp.c @@ -1898,6 +1898,16 @@ static u8 sc_send_public_key(struct smp_chan *smp) } done: + /* Check if remote and local public keys are the same and debug key is + * not in use. + */ + if (!test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags) && + !test_bit(SMP_FLAG_INITIATOR, &smp->flags) && + !crypto_memneq(smp->remote_pk, smp->local_pk, 32)) { + bt_dev_err(hdev, "Remote and local public keys are identical"); + return SMP_DHKEY_CHECK_FAILED; + } + SMP_DBG("Local Public Key X: %32phN", smp->local_pk); SMP_DBG("Local Public Key Y: %32phN", smp->local_pk + 32); @@ -2749,7 +2759,8 @@ static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb) * not in use. */ if (!test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags) && - !crypto_memneq(key, smp->local_pk, 64)) { + test_bit(SMP_FLAG_INITIATOR, &smp->flags) && + !crypto_memneq(key, smp->local_pk, 32)) { bt_dev_err(hdev, "Remote and local public keys are identical"); return SMP_DHKEY_CHECK_FAILED; }