Skip to content

Commit 7e0ef6e

Browse files
finikorgGustavo F. Padovan
authored andcommitted
Bluetooth: EWS: rewrite handling SAR bits
Segmentation and Reassembly (SAR) occupies different windows in standard and extended control fields. Convert hardcoded masks to relative ones and use shift to access SAR bits. Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com> Signed-off-by: Gustavo F. Padovan <padovan@profusion.mobi>
1 parent ab784b7 commit 7e0ef6e

2 files changed

Lines changed: 47 additions & 20 deletions

File tree

‎include/net/bluetooth/l2cap.h‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ struct l2cap_conninfo {
146146
#define L2CAP_SUPER_SREJ 0x03
147147

148148
/* L2CAP Segmentation and Reassembly */
149-
#define L2CAP_SDU_UNSEGMENTED 0x0000
150-
#define L2CAP_SDU_START 0x4000
151-
#define L2CAP_SDU_END 0x8000
152-
#define L2CAP_SDU_CONTINUE 0xC000
149+
#define L2CAP_SAR_UNSEGMENTED 0x00
150+
#define L2CAP_SAR_START 0x01
151+
#define L2CAP_SAR_END 0x02
152+
#define L2CAP_SAR_CONTINUE 0x03
153153

154154
/* L2CAP Command rej. reasons */
155155
#define L2CAP_REJ_NOT_UNDERSTOOD 0x0000
@@ -516,7 +516,34 @@ static inline int l2cap_tx_window_full(struct l2cap_chan *ch)
516516
#define __get_reqseq(ctrl) (((ctrl) & L2CAP_CTRL_REQSEQ) >> 8)
517517
#define __is_iframe(ctrl) (!((ctrl) & L2CAP_CTRL_FRAME_TYPE))
518518
#define __is_sframe(ctrl) ((ctrl) & L2CAP_CTRL_FRAME_TYPE)
519-
#define __is_sar_start(ctrl) (((ctrl) & L2CAP_CTRL_SAR) == L2CAP_SDU_START)
519+
static inline __u8 __get_ctrl_sar(struct l2cap_chan *chan, __u32 ctrl)
520+
{
521+
if (test_bit(FLAG_EXT_CTRL, &chan->flags))
522+
return (ctrl & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT;
523+
else
524+
return (ctrl & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT;
525+
}
526+
527+
static inline __u32 __set_ctrl_sar(struct l2cap_chan *chan, __u32 sar)
528+
{
529+
if (test_bit(FLAG_EXT_CTRL, &chan->flags))
530+
return (sar << L2CAP_EXT_CTRL_SAR_SHIFT) & L2CAP_EXT_CTRL_SAR;
531+
else
532+
return (sar << L2CAP_CTRL_SAR_SHIFT) & L2CAP_CTRL_SAR;
533+
}
534+
535+
static inline bool __is_sar_start(struct l2cap_chan *chan, __u32 ctrl)
536+
{
537+
return __get_ctrl_sar(chan, ctrl) == L2CAP_SAR_START;
538+
}
539+
540+
static inline __u32 __get_sar_mask(struct l2cap_chan *chan)
541+
{
542+
if (test_bit(FLAG_EXT_CTRL, &chan->flags))
543+
return L2CAP_EXT_CTRL_SAR;
544+
else
545+
return L2CAP_CTRL_SAR;
546+
}
520547

521548
static inline __u8 __get_ctrl_super(struct l2cap_chan *chan, __u32 ctrl)
522549
{

‎net/bluetooth/l2cap_core.c‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u8 tx_seq)
13111311
tx_skb = skb_clone(skb, GFP_ATOMIC);
13121312
bt_cb(skb)->retries++;
13131313
control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE);
1314-
control &= L2CAP_CTRL_SAR;
1314+
control &= __get_sar_mask(chan);
13151315

13161316
if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
13171317
control |= L2CAP_CTRL_FINAL;
@@ -1351,7 +1351,7 @@ static int l2cap_ertm_send(struct l2cap_chan *chan)
13511351
bt_cb(skb)->retries++;
13521352

13531353
control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE);
1354-
control &= L2CAP_CTRL_SAR;
1354+
control &= __get_sar_mask(chan);
13551355

13561356
if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
13571357
control |= L2CAP_CTRL_FINAL;
@@ -1582,7 +1582,7 @@ static int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, si
15821582
size_t size = 0;
15831583

15841584
skb_queue_head_init(&sar_queue);
1585-
control = L2CAP_SDU_START;
1585+
control = __set_ctrl_sar(chan, L2CAP_SAR_START);
15861586
skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len);
15871587
if (IS_ERR(skb))
15881588
return PTR_ERR(skb);
@@ -1595,10 +1595,10 @@ static int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, si
15951595
size_t buflen;
15961596

15971597
if (len > chan->remote_mps) {
1598-
control = L2CAP_SDU_CONTINUE;
1598+
control = __set_ctrl_sar(chan, L2CAP_SAR_CONTINUE);
15991599
buflen = chan->remote_mps;
16001600
} else {
1601-
control = L2CAP_SDU_END;
1601+
control = __set_ctrl_sar(chan, L2CAP_SAR_END);
16021602
buflen = len;
16031603
}
16041604

@@ -1654,7 +1654,7 @@ int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
16541654
case L2CAP_MODE_STREAMING:
16551655
/* Entire SDU fits into one PDU */
16561656
if (len <= chan->remote_mps) {
1657-
control = L2CAP_SDU_UNSEGMENTED;
1657+
control = __set_ctrl_sar(chan, L2CAP_SAR_UNSEGMENTED);
16581658
skb = l2cap_create_iframe_pdu(chan, msg, len, control,
16591659
0);
16601660
if (IS_ERR(skb))
@@ -3201,15 +3201,15 @@ static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u1
32013201
{
32023202
int err = -EINVAL;
32033203

3204-
switch (control & L2CAP_CTRL_SAR) {
3205-
case L2CAP_SDU_UNSEGMENTED:
3204+
switch (__get_ctrl_sar(chan, control)) {
3205+
case L2CAP_SAR_UNSEGMENTED:
32063206
if (chan->sdu)
32073207
break;
32083208

32093209
err = chan->ops->recv(chan->data, skb);
32103210
break;
32113211

3212-
case L2CAP_SDU_START:
3212+
case L2CAP_SAR_START:
32133213
if (chan->sdu)
32143214
break;
32153215

@@ -3231,7 +3231,7 @@ static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u1
32313231
err = 0;
32323232
break;
32333233

3234-
case L2CAP_SDU_CONTINUE:
3234+
case L2CAP_SAR_CONTINUE:
32353235
if (!chan->sdu)
32363236
break;
32373237

@@ -3245,7 +3245,7 @@ static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u1
32453245
err = 0;
32463246
break;
32473247

3248-
case L2CAP_SDU_END:
3248+
case L2CAP_SAR_END:
32493249
if (!chan->sdu)
32503250
break;
32513251

@@ -3343,7 +3343,7 @@ static void l2cap_check_srej_gap(struct l2cap_chan *chan, u8 tx_seq)
33433343
break;
33443344

33453345
skb = skb_dequeue(&chan->srej_q);
3346-
control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT;
3346+
control = __set_ctrl_sar(chan, bt_cb(skb)->sar);
33473347
err = l2cap_reassemble_sdu(chan, skb, control);
33483348

33493349
if (err < 0) {
@@ -3398,7 +3398,7 @@ static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u16 rx_cont
33983398
{
33993399
u8 tx_seq = __get_txseq(rx_control);
34003400
u8 req_seq = __get_reqseq(rx_control);
3401-
u8 sar = rx_control >> L2CAP_CTRL_SAR_SHIFT;
3401+
u8 sar = __get_ctrl_sar(chan, rx_control);
34023402
int tx_seq_offset, expected_tx_seq_offset;
34033403
int num_to_ack = (chan->tx_win/6) + 1;
34043404
int err = 0;
@@ -3707,7 +3707,7 @@ static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb)
37073707
if (l2cap_check_fcs(chan, skb))
37083708
goto drop;
37093709

3710-
if (__is_sar_start(control) && __is_iframe(control))
3710+
if (__is_sar_start(chan, control) && __is_iframe(control))
37113711
len -= 2;
37123712

37133713
if (chan->fcs == L2CAP_FCS_CRC16)
@@ -3811,7 +3811,7 @@ static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk
38113811
if (l2cap_check_fcs(chan, skb))
38123812
goto drop;
38133813

3814-
if (__is_sar_start(control))
3814+
if (__is_sar_start(chan, control))
38153815
len -= 2;
38163816

38173817
if (chan->fcs == L2CAP_FCS_CRC16)

0 commit comments

Comments
 (0)