Skip to content

Commit 4b6e228

Browse files
Vudentzholtmann
authored andcommitted
Bluetooth: Auto tune if input MTU is set to 0
This enables the code to set the input MTU using the underline link packet types when set to 0, previously this would likely be rejected by the remote peer since it would be bellow the minimal of 48 for BR/EDR or 23 for LE, that way it shall be safe to use 0 without causing any side effects. This is convenient for the likes of A2DP transport, see: https://habr.com/en/post/456182/ Signed-off-by: Luiz Augusto von Dentz <[email protected]> Signed-off-by: Marcel Holtmann <[email protected]>
1 parent 1efd927 commit 4b6e228

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

net/bluetooth/l2cap_core.c

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,9 @@ static void l2cap_le_connect(struct l2cap_chan *chan)
12891289
if (test_and_set_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags))
12901290
return;
12911291

1292+
if (!chan->imtu)
1293+
chan->imtu = chan->conn->mtu;
1294+
12921295
l2cap_le_flowctl_init(chan, 0);
12931296

12941297
req.psm = chan->psm;
@@ -3226,6 +3229,49 @@ static inline void l2cap_txwin_setup(struct l2cap_chan *chan)
32263229
chan->ack_win = chan->tx_win;
32273230
}
32283231

3232+
static void l2cap_mtu_auto(struct l2cap_chan *chan)
3233+
{
3234+
struct hci_conn *conn = chan->conn->hcon;
3235+
3236+
chan->imtu = L2CAP_DEFAULT_MIN_MTU;
3237+
3238+
/* The 2-DH1 packet has between 2 and 56 information bytes
3239+
* (including the 2-byte payload header)
3240+
*/
3241+
if (!(conn->pkt_type & HCI_2DH1))
3242+
chan->imtu = 54;
3243+
3244+
/* The 3-DH1 packet has between 2 and 85 information bytes
3245+
* (including the 2-byte payload header)
3246+
*/
3247+
if (!(conn->pkt_type & HCI_3DH1))
3248+
chan->imtu = 83;
3249+
3250+
/* The 2-DH3 packet has between 2 and 369 information bytes
3251+
* (including the 2-byte payload header)
3252+
*/
3253+
if (!(conn->pkt_type & HCI_2DH3))
3254+
chan->imtu = 367;
3255+
3256+
/* The 3-DH3 packet has between 2 and 554 information bytes
3257+
* (including the 2-byte payload header)
3258+
*/
3259+
if (!(conn->pkt_type & HCI_3DH3))
3260+
chan->imtu = 552;
3261+
3262+
/* The 2-DH5 packet has between 2 and 681 information bytes
3263+
* (including the 2-byte payload header)
3264+
*/
3265+
if (!(conn->pkt_type & HCI_2DH5))
3266+
chan->imtu = 679;
3267+
3268+
/* The 3-DH5 packet has between 2 and 1023 information bytes
3269+
* (including the 2-byte payload header)
3270+
*/
3271+
if (!(conn->pkt_type & HCI_3DH5))
3272+
chan->imtu = 1021;
3273+
}
3274+
32293275
static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data, size_t data_size)
32303276
{
32313277
struct l2cap_conf_req *req = data;
@@ -3255,8 +3301,12 @@ static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data, size_t data
32553301
}
32563302

32573303
done:
3258-
if (chan->imtu != L2CAP_DEFAULT_MTU)
3259-
l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu, endptr - ptr);
3304+
if (chan->imtu != L2CAP_DEFAULT_MTU) {
3305+
if (!chan->imtu)
3306+
l2cap_mtu_auto(chan);
3307+
l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu,
3308+
endptr - ptr);
3309+
}
32603310

32613311
switch (chan->mode) {
32623312
case L2CAP_MODE_BASIC:

0 commit comments

Comments
 (0)