From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796388916; b=AMPTprB8DguDO9hTaYurrvw4xMjuhXwbjOLuS7zUTJCnBo+i+J424hsV2R4YVmmnjxBF xJKWyW6/X0t6wxdbPK7YsUaAxgnQQVrPY/ET6MuJMk1aUaneUSM3CMbVNmROzfkpzNvHb IUoTvBPjS3eCHRaAveyiycQ+nQruA/pLVIm1XyduCnuAhuOneZJroqIs+Ysy7L/2m3RLB ClmHSf4stuXjekzw/b+VGJkhCHJzeRP3B7zawsE7M1tyawpoS3z813ZUYHgiNgGXTKJab h69BtPD5PgERSYphAahl0W2US9L2omS8NR5Tly/j1m790iKcWMNDkwwU9wEAPM8K3LA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388916; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=fX0WYNcdpPblCxYCTMvRTE25/b+fzQw5dNsQwbJ1XGE=; b=4eSxt+4AydBifzwfLfwoqW/0vueqa/Gc+jd++OGexlb3f0GVvdQ7/C9YnqK0Cz8a/iSh oun3Gzf4XWQg3YzyF5j/P9+7/6roH/OqVyX/E0RY5d0tYW3S46Uk4U6oc/xd17+rjNX+H OJ9VnEc/e6jE2GnxQOIoSH4BxwNJnfcj8EhogGhLoM2DNWj2nbQQPIuyVqZOzSxtbQc2w H+K60CO/HIj/1thSgE2fDlgjbdGkOrE0CtFgDSDa9/DGlLmL9cGdy5TgnC4yrSBvGT3QM vuhUV7V6FMkBjfR3qndIwQgIFi5a9HG8L87l1i33yLVnqnsSzHNhFtehlh0eC52qj9w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=kMkaWgpt; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5CF0A606A32C; Fri, 4 Dec 2026 12:55:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=kMkaWgpt Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6C4B31F0092C; Fri, 4 Dec 2026 12:55:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796388915; bh=fX0WYNcdpPblCxYCTMvRTE25/b+fzQw5dNsQwbJ1XGE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=kMkaWgptv/E3tKx7Ep9OehfJuGoHT11WN8yjnYvt0OM9Y2/liZ/taaPdnQpmljqR2 HUpN9pFk/ctkeoT69P8EKmrBGsFFpFdZPAqDWEg3azUkFXNe9ZjG1RbFSR2YeUJTzj yx+pjmA1oVa3R2xOrb9iblK+x7GfewlCmOoffl60= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Sashiko , Luiz Augusto von Dentz , Sasha Levin Subject: [PATCH 6.1 0797/1059] Bluetooth: mgmt: Fix mesh transmission locking and lifetime Date: Fri, 4 Dec 2026 19:05:28 +0900 Message-ID: <20261204100704.673664840@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Luiz Augusto von Dentz [ Upstream commit 1d7a527788584e4a88ccc532ed73bfdacd0b1e74 ] The mesh_pending list is modified under hdev->lock by Mesh Send, but the cmd_sync callbacks and Mesh Send Cancel add/remove entries and touch hdev->adv_instances without it, which corrupts the list and the advertising instances. In addition the queued mesh_send_sync entry holds a raw pointer to the mesh_tx which a cancel of the transmission can free while the entry is running, leading to use-after-free. Fix this by: - taking hdev->lock around every mesh_pending/adv_instances access in the cmd_sync callbacks and Mesh Send Cancel, but never across HCI commands; - queueing the cmd_sync entries with the transmission handle and looking it up under the lock instead of passing the pointer; - always sending the first transmission, tracked by HCI_MESH_SENDING which is only cleared together with the removal of the transmission so Mesh Send can't start another one in between, and making sure none is left stranded if queueing fails; - cancelling a transmission by dequeuing its entry, and the done entry if it is the one in progress, stopping the done timer, which is queued with hdev->lock held so a cancel can't miss it; - not taking hdev->lock from callbacks invoked with -ECANCELED since those run with the cmd_sync lock held, the transmission is removed by whoever cancelled the entry; - flushing the transmissions left, dequeuing the entries still queued for them and clearing HCI_MESH_SENDING, when the controller is closed (also when auto powered off) or the index is removed, as nothing is going to send them and the stale entries would act on the ones started after the controller is powered on again, and not accepting Mesh Send while the controller is not powered so none can be left behind; - not reporting the completion of a transmission that failed to be queued by Mesh Send itself since userspace wasn't told about its handle; - not reusing the handle of a transmission still pending when the handle counter wraps around, now that it is what identifies a transmission. Reported-by: Sashiko Link: https://sashiko.dev/#/bug/linux-4ab89465-fc27-4d91-a870-a8ed49b22e91 Link: https://sashiko.dev/#/bug/linux-ab941894-dbe1-49da-8bac-c52efda6eaee Link: https://sashiko.dev/#/bug/linux-4016f774-7ac7-4b8a-916f-587ec9d597d4 Fixes: b338d91703fa ("Bluetooth: Implement support for Mesh") Assisted-by: OpenCode:claude-fable-5.2 Signed-off-by: Luiz Augusto von Dentz Signed-off-by: Sasha Levin --- include/net/bluetooth/hci_core.h | 1 + net/bluetooth/hci_sync.c | 5 + net/bluetooth/mgmt.c | 301 +++++++++++++++++++++++++------ net/bluetooth/mgmt_util.c | 23 ++- 4 files changed, 267 insertions(+), 63 deletions(-) diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h index 9e7d7ad97be9e..1bac0d11cf9e4 100644 --- a/include/net/bluetooth/hci_core.h +++ b/include/net/bluetooth/hci_core.h @@ -2090,6 +2090,7 @@ void mgmt_index_removed(struct hci_dev *hdev); void mgmt_set_powered_failed(struct hci_dev *hdev, int err); void mgmt_power_on(struct hci_dev *hdev, int err); void __mgmt_power_off(struct hci_dev *hdev); +void mgmt_mesh_flush(struct hci_dev *hdev); void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, bool persistent); void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c index d596adedded96..c80466d49114d 100644 --- a/net/bluetooth/hci_sync.c +++ b/net/bluetooth/hci_sync.c @@ -5426,6 +5426,11 @@ int hci_dev_close_sync(struct hci_dev *hdev) auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF); + /* Nothing is going to send the mesh transmissions left, whether or not + * mgmt is told about the controller being powered off. + */ + mgmt_mesh_flush(hdev); + if (!auto_off && !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && hci_dev_test_flag(hdev, HCI_MGMT)) __mgmt_power_off(hdev); diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c index cc6d76b073abf..6a113b659fc82 100644 --- a/net/bluetooth/mgmt.c +++ b/net/bluetooth/mgmt.c @@ -1051,6 +1051,7 @@ static void discov_off(struct work_struct *work) static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev); +/* This function requires the caller holds hdev->lock */ static void mesh_send_complete(struct hci_dev *hdev, struct mgmt_mesh_tx *mesh_tx, bool silent) { @@ -1063,37 +1064,95 @@ static void mesh_send_complete(struct hci_dev *hdev, mgmt_mesh_remove(mesh_tx); } -static int mesh_send_done_sync(struct hci_dev *hdev, void *data) +/* The cmd_sync entries identify the transmission by handle rather than by + * pointer since the mesh_tx can be removed while the entry is queued or + * running. + */ +#define MESH_TX_DATA(_handle) ((void *)(unsigned long)(_handle)) +#define MESH_TX_HANDLE(_data) ((u8)(unsigned long)(_data)) + +static int mesh_send_sync(struct hci_dev *hdev, void *data); +static void mesh_send_start_complete(struct hci_dev *hdev, void *data, int err); + +/* Start sending the first transmission unless one is already in progress, + * HCI_MESH_SENDING is set while the first transmission is being sent. + * Completion is not reported for @new, the handle of a transmission userspace + * wasn't told about yet. + * + * This function requires the caller holds hdev->lock. + */ +static void __mesh_next(struct hci_dev *hdev, u8 new) { struct mgmt_mesh_tx *mesh_tx; + int err; - hci_dev_clear_flag(hdev, HCI_MESH_SENDING); - if (list_empty(&hdev->adv_instances)) - hci_disable_advertising_sync(hdev); - mesh_tx = mgmt_mesh_next(hdev, NULL); - - if (mesh_tx) - mesh_send_complete(hdev, mesh_tx, false); + while (!hci_dev_test_flag(hdev, HCI_MESH_SENDING)) { + mesh_tx = mgmt_mesh_next(hdev, NULL); + if (!mesh_tx) + return; - return 0; + err = hci_cmd_sync_queue(hdev, mesh_send_sync, + MESH_TX_DATA(mesh_tx->handle), + mesh_send_start_complete); + if (err < 0) + mesh_send_complete(hdev, mesh_tx, + mesh_tx->handle == new); + else + hci_dev_set_flag(hdev, HCI_MESH_SENDING); + } } -static int mesh_send_sync(struct hci_dev *hdev, void *data); -static void mesh_send_start_complete(struct hci_dev *hdev, void *data, int err); static void mesh_next(struct hci_dev *hdev, void *data, int err) { - struct mgmt_mesh_tx *mesh_tx = mgmt_mesh_next(hdev, NULL); - - if (!mesh_tx) + /* The entry is being cancelled with the cmd_sync lock held, which + * can't be taken before hdev->lock. + */ + if (err == -ECANCELED) return; - err = hci_cmd_sync_queue(hdev, mesh_send_sync, mesh_tx, - mesh_send_start_complete); + hci_dev_lock(hdev); + __mesh_next(hdev, 0); + hci_dev_unlock(hdev); +} - if (err < 0) +static int mesh_send_done_sync(struct hci_dev *hdev, void *data) +{ + struct mgmt_mesh_tx *mesh_tx; + u8 handle; + bool disable; + + hci_dev_lock(hdev); + + mesh_tx = mgmt_mesh_next(hdev, NULL); + if (!mesh_tx || !hci_dev_test_flag(hdev, HCI_MESH_SENDING)) { + hci_dev_unlock(hdev); + return 0; + } + + handle = mesh_tx->handle; + disable = list_empty(&hdev->adv_instances); + + hci_dev_unlock(hdev); + + if (disable) + hci_disable_advertising_sync(hdev); + + hci_dev_lock(hdev); + + /* The transmission can be cancelled meanwhile, in which case whoever + * did so already moved on to the next one. HCI_MESH_SENDING is only + * cleared with the transmission removed so Mesh Send doesn't start + * another one in between. + */ + mesh_tx = mgmt_mesh_find(hdev, handle); + if (mesh_tx) { mesh_send_complete(hdev, mesh_tx, false); - else - hci_dev_set_flag(hdev, HCI_MESH_SENDING); + hci_dev_clear_flag(hdev, HCI_MESH_SENDING); + } + + hci_dev_unlock(hdev); + + return 0; } static void mesh_send_done(struct work_struct *work) @@ -1107,6 +1166,33 @@ static void mesh_send_done(struct work_struct *work) hci_cmd_sync_queue(hdev, mesh_send_done_sync, NULL, mesh_next); } +/* Nothing is going to send the transmissions left, e.g. when the controller + * is powered off. + * + * This function requires the caller holds hdev->lock. + */ +static void mesh_flush(struct hci_dev *hdev, bool silent) +{ + struct mgmt_mesh_tx *mesh_tx; + + /* Stop the timer first, it checks HCI_MESH_SENDING without hdev->lock + * so once it is clear and the work is not running anymore nothing can + * queue mesh_send_done_sync behind the dequeue below. + */ + hci_dev_clear_flag(hdev, HCI_MESH_SENDING); + cancel_delayed_work_sync(&hdev->mesh_send_done); + + /* Entries still queued would otherwise run after the controller is + * powered on again and act on the transmissions started by then. The + * destroy callbacks don't take hdev->lock when cancelled. + */ + hci_cmd_sync_dequeue(hdev, mesh_send_sync, NULL, NULL); + hci_cmd_sync_dequeue(hdev, mesh_send_done_sync, NULL, NULL); + + while ((mesh_tx = mgmt_mesh_next(hdev, NULL))) + mesh_send_complete(hdev, mesh_tx, silent); +} + static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) { if (hci_dev_test_flag(hdev, HCI_MGMT)) @@ -2206,38 +2292,71 @@ static int set_mesh(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) static void mesh_send_start_complete(struct hci_dev *hdev, void *data, int err) { - struct mgmt_mesh_tx *mesh_tx = data; - struct mgmt_cp_mesh_send *send = (void *)mesh_tx->param; - unsigned long mesh_send_interval; + struct mgmt_mesh_tx *mesh_tx; + struct mgmt_cp_mesh_send *send; u8 mgmt_err = mgmt_status(err); + /* The entry is being cancelled with the cmd_sync lock held, which + * can't be taken before hdev->lock, so leave the removal of the + * transmission, and HCI_MESH_SENDING, to whoever cancelled it or to + * mgmt_power_off() if the controller is going down. + */ + if (err == -ECANCELED) + return; + + hci_dev_lock(hdev); + + /* Cancelled while the entry was running, whoever did so already moved + * on to the next transmission. + */ + mesh_tx = mgmt_mesh_find(hdev, MESH_TX_HANDLE(data)); + if (!mesh_tx) { + hci_dev_unlock(hdev); + return; + } + /* Report any errors here, but don't report completion */ if (mgmt_err) { - hci_dev_clear_flag(hdev, HCI_MESH_SENDING); /* Send Complete Error Code for handle */ mesh_send_complete(hdev, mesh_tx, false); - if (err != -ECANCELED) - mesh_next(hdev, NULL, 0); + hci_dev_clear_flag(hdev, HCI_MESH_SENDING); + __mesh_next(hdev, 0); + hci_dev_unlock(hdev); return; } - mesh_send_interval = msecs_to_jiffies((send->cnt) * 25); + /* Queued with hdev->lock held so a cancel can't miss it */ + send = (void *)mesh_tx->param; queue_delayed_work(hdev->req_workqueue, &hdev->mesh_send_done, - mesh_send_interval); + msecs_to_jiffies((send->cnt) * 25)); + + hci_dev_unlock(hdev); } static int mesh_send_sync(struct hci_dev *hdev, void *data) { - struct mgmt_mesh_tx *mesh_tx = data; - struct mgmt_cp_mesh_send *send = (void *)mesh_tx->param; + struct mgmt_mesh_tx *mesh_tx; + struct mgmt_cp_mesh_send *send; struct adv_info *adv, *next_instance; u8 instance = hdev->le_num_of_adv_sets + 1; u16 timeout, duration; int err = 0; - if (hdev->le_num_of_adv_sets <= hdev->adv_instance_cnt) + hci_dev_lock(hdev); + + mesh_tx = mgmt_mesh_find(hdev, MESH_TX_HANDLE(data)); + if (!mesh_tx) { + hci_dev_unlock(hdev); + return -ENOENT; + } + + send = (void *)mesh_tx->param; + + if (hdev->le_num_of_adv_sets <= hdev->adv_instance_cnt) { + hci_dev_unlock(hdev); return MGMT_STATUS_BUSY; + } timeout = 1000; duration = send->cnt * INTERVAL_TO_MS(hdev->le_adv_max_interval); @@ -2275,6 +2394,8 @@ static int mesh_send_sync(struct hci_dev *hdev, void *data) instance = 0; } + hci_dev_unlock(hdev); + if (instance) return hci_schedule_adv_instance_sync(hdev, instance, true); @@ -2318,37 +2439,80 @@ static int mesh_features(struct sock *sk, struct hci_dev *hdev, return 0; } +/* Cancel a single transmission, the handle is looked up again after the + * entry has been dequeued since it may be gone or have completed meanwhile. + */ +static void mesh_cancel_tx(struct hci_dev *hdev, u8 handle) +{ + struct mgmt_mesh_tx *mesh_tx; + bool in_progress; + + hci_cmd_sync_dequeue(hdev, mesh_send_sync, MESH_TX_DATA(handle), NULL); + + hci_dev_lock(hdev); + mesh_tx = mgmt_mesh_find(hdev, handle); + in_progress = mesh_tx && hci_dev_test_flag(hdev, HCI_MESH_SENDING) && + mesh_tx == mgmt_mesh_next(hdev, NULL); + hci_dev_unlock(hdev); + + /* The done timer may have fired already and be waiting to complete + * the transmission in progress, which is being cancelled. + */ + if (in_progress) + hci_cmd_sync_dequeue(hdev, mesh_send_done_sync, NULL, NULL); + + hci_dev_lock(hdev); + + mesh_tx = mgmt_mesh_find(hdev, handle); + if (mesh_tx) { + /* If it is the transmission in progress make sure the timer + * doesn't complete the next one in its place. + */ + if (hci_dev_test_flag(hdev, HCI_MESH_SENDING) && + mesh_tx == mgmt_mesh_next(hdev, NULL)) { + cancel_delayed_work(&hdev->mesh_send_done); + hci_dev_clear_flag(hdev, HCI_MESH_SENDING); + } + + mesh_send_complete(hdev, mesh_tx, false); + } + + hci_dev_unlock(hdev); +} + static int send_cancel(struct hci_dev *hdev, void *data) { struct mgmt_pending_cmd *cmd = data; struct mgmt_cp_mesh_send_cancel *cancel = (void *)cmd->param; struct mgmt_mesh_tx *mesh_tx; + u8 handle; if (!cancel->handle) { - do { + for (;;) { + hci_dev_lock(hdev); mesh_tx = mgmt_mesh_next(hdev, cmd->sk); + handle = mesh_tx ? mesh_tx->handle : 0; + hci_dev_unlock(hdev); - if (mesh_tx) { - if (!hci_cmd_sync_dequeue(hdev, mesh_send_sync, - mesh_tx, NULL)) - mesh_send_complete(hdev, mesh_tx, false); - } - } while (mesh_tx); + if (!mesh_tx) + break; + + mesh_cancel_tx(hdev, handle); + } } else { + hci_dev_lock(hdev); mesh_tx = mgmt_mesh_find(hdev, cancel->handle); + handle = mesh_tx && mesh_tx->sk == cmd->sk ? mesh_tx->handle : 0; + hci_dev_unlock(hdev); - if (mesh_tx && mesh_tx->sk == cmd->sk) { - if (!hci_cmd_sync_dequeue(hdev, mesh_send_sync, - mesh_tx, NULL)) - mesh_send_complete(hdev, mesh_tx, false); - } + if (handle) + mesh_cancel_tx(hdev, handle); } mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_MESH_SEND_CANCEL, 0, NULL, 0); - if (!hci_dev_test_flag(hdev, HCI_MESH_SENDING)) - mesh_next(hdev, NULL, 0); + mesh_next(hdev, NULL, 0); return 0; } @@ -2399,7 +2563,6 @@ static int mesh_send(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) struct mgmt_cp_mesh_send *send = data; struct mgmt_rp_mesh_read_features rp; u16 expected_len; - bool sending; int err = 0; if (!lmp_le_capable(hdev) || @@ -2421,6 +2584,15 @@ static int mesh_send(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) hci_dev_lock(hdev); + /* Transmissions are flushed when the controller is closed, which is + * serialized with this by hdev->lock, so none can be left behind. + */ + if (!hdev_is_powered(hdev)) { + err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND, + MGMT_STATUS_NOT_POWERED); + goto done; + } + memset(&rp, 0, sizeof(rp)); rp.max_handles = MESH_HANDLES_MAX; @@ -2432,27 +2604,27 @@ static int mesh_send(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) goto done; } - sending = hci_dev_test_flag(hdev, HCI_MESH_SENDING); mesh_tx = mgmt_mesh_add(sk, hdev, send, len); - - if (!mesh_tx) + if (!mesh_tx) { err = -ENOMEM; - else if (!sending) - err = hci_cmd_sync_queue(hdev, mesh_send_sync, mesh_tx, - mesh_send_start_complete); + } else { + u8 handle = mesh_tx->handle; + + /* The first transmission is the one queued, which may not be + * the one just added. + */ + __mesh_next(hdev, handle); + + /* Queueing failed, the transmission was removed */ + if (!mgmt_mesh_find(hdev, handle)) + err = -EIO; + } if (err < 0) { bt_dev_err(hdev, "Send Mesh Failed %d", err); err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND, MGMT_STATUS_FAILED); - - if (mesh_tx) { - if (sending) - mgmt_mesh_remove(mesh_tx); - } } else { - hci_dev_set_flag(hdev, HCI_MESH_SENDING); - mgmt_cmd_complete(sk, hdev->id, MGMT_OP_MESH_SEND, 0, &mesh_tx->handle, 1); } @@ -9625,7 +9797,8 @@ void mgmt_index_removed(struct hci_dev *hdev) cancel_delayed_work_sync(&hdev->discov_off); cancel_delayed_work_sync(&hdev->service_cache); cancel_delayed_work_sync(&hdev->rpa_expired); - cancel_delayed_work_sync(&hdev->mesh_send_done); + + mesh_flush(hdev, true); } void mgmt_power_on(struct hci_dev *hdev, int err) @@ -9652,6 +9825,16 @@ void mgmt_power_on(struct hci_dev *hdev, int err) hci_dev_unlock(hdev); } +/* This function requires the caller holds hdev->lock */ +void mgmt_mesh_flush(struct hci_dev *hdev) +{ + /* The mesh work is only initialized once mgmt is used */ + if (!hci_dev_test_flag(hdev, HCI_MGMT)) + return; + + mesh_flush(hdev, false); +} + void __mgmt_power_off(struct hci_dev *hdev) { struct cmd_lookup match = { NULL, hdev }; diff --git a/net/bluetooth/mgmt_util.c b/net/bluetooth/mgmt_util.c index 4ba500c377a4c..9fdfbd6df51a8 100644 --- a/net/bluetooth/mgmt_util.c +++ b/net/bluetooth/mgmt_util.c @@ -366,16 +366,31 @@ struct mgmt_mesh_tx *mgmt_mesh_add(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) { struct mgmt_mesh_tx *mesh_tx; + u8 handle = hdev->mesh_send_ref; + int i; + + /* The handle is all that identifies a transmission, so it must not be + * in use by one still pending when the counter wraps around. + */ + for (i = 0; i < 255; i++) { + handle++; + if (!handle) + handle++; + + if (!mgmt_mesh_find(hdev, handle)) + break; + } + + if (i == 255) + return NULL; mesh_tx = kzalloc(sizeof(*mesh_tx), GFP_KERNEL); if (!mesh_tx) return NULL; - hdev->mesh_send_ref++; - if (!hdev->mesh_send_ref) - hdev->mesh_send_ref++; + hdev->mesh_send_ref = handle; - mesh_tx->handle = hdev->mesh_send_ref; + mesh_tx->handle = handle; mesh_tx->index = hdev->id; memcpy(mesh_tx->param, data, len); mesh_tx->param_len = len; -- 2.53.0