Skip to content

Commit 088fac7

Browse files
sjanccarlescufi
authored andcommitted
Bluetooth: Mesh: Fix crash on disconnect
bt_mesh_proxy_role_setup() is called conditionally when peer is connected and gatt_disconnected() is always called. This leads to unbalance in role->conn reference count and crash. Instead of hot-fixing this in gatt_disconnected(), this commit adds proper bt_mesh_proxy_role_cleanup() API that is called by roles implementations if cleanup is needed. Signed-off-by: Szymon Janc <[email protected]>
1 parent 072c8f2 commit 088fac7

File tree

4 files changed

+10
-13
lines changed

4 files changed

+10
-13
lines changed

subsys/bluetooth/mesh/pb_gatt_srv.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ static void gatt_disconnected(struct bt_conn *conn, uint8_t reason)
115115
return;
116116
}
117117

118-
cli = NULL;
118+
if (cli) {
119+
bt_mesh_proxy_role_cleanup(cli);
120+
cli = NULL;
121+
}
119122

120123
bt_mesh_pb_gatt_close(conn);
121124

subsys/bluetooth/mesh/proxy_msg.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,8 @@ struct bt_mesh_proxy_role *bt_mesh_proxy_role_setup(struct bt_conn *conn,
219219
return role;
220220
}
221221

222-
static void gatt_disconnected(struct bt_conn *conn, uint8_t reason)
222+
void bt_mesh_proxy_role_cleanup(struct bt_mesh_proxy_role *role)
223223
{
224-
struct bt_mesh_proxy_role *role;
225-
226-
BT_DBG("conn %p reason 0x%02x", (void *)conn, reason);
227-
228-
role = &roles[bt_conn_index(conn)];
229-
230224
/* If this fails, the work handler exits early, as
231225
* there's no active connection.
232226
*/
@@ -236,7 +230,3 @@ static void gatt_disconnected(struct bt_conn *conn, uint8_t reason)
236230

237231
bt_mesh_adv_update();
238232
}
239-
240-
BT_CONN_CB_DEFINE(conn_callbacks) = {
241-
.disconnected = gatt_disconnected,
242-
};

subsys/bluetooth/mesh/proxy_msg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@ int bt_mesh_proxy_msg_send(struct bt_mesh_proxy_role *role, uint8_t type,
5454
struct bt_mesh_proxy_role *bt_mesh_proxy_role_setup(struct bt_conn *conn,
5555
proxy_send_cb_t send,
5656
proxy_recv_cb_t recv);
57+
void bt_mesh_proxy_role_cleanup(struct bt_mesh_proxy_role *role);
5758

5859
#endif /* ZEPHYR_SUBSYS_BLUETOOTH_MESH_PROXY_MSG_H_ */

subsys/bluetooth/mesh/proxy_srv.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,10 @@ static void gatt_disconnected(struct bt_conn *conn, uint8_t reason)
883883
conn_count--;
884884

885885
client = find_client(conn);
886-
client->cli = NULL;
886+
if (client->cli) {
887+
bt_mesh_proxy_role_cleanup(client->cli);
888+
client->cli = NULL;
889+
}
887890
}
888891

889892
static int proxy_send(struct bt_conn *conn,

0 commit comments

Comments
 (0)