@@ -1494,10 +1494,13 @@ static int rfcomm_recv(struct bt_l2cap_chan *chan, struct net_buf *buf)
14941494{
14951495 struct bt_rfcomm_session * session = RFCOMM_SESSION (chan );
14961496 struct bt_rfcomm_hdr * hdr = (void * )buf -> data ;
1497+ struct bt_rfcomm_hdr_ext * hdr_ext = (void * )buf -> data ;
14971498 uint8_t dlci , frame_type , fcs , fcs_len ;
1499+ uint16_t msg_len ;
1500+ uint16_t hdr_len ;
14981501
14991502 /* Need to consider FCS also*/
1500- if (buf -> len < (sizeof (* hdr ) + 1 )) {
1503+ if (buf -> len < (sizeof (* hdr ) + sizeof ( fcs ) )) {
15011504 LOG_ERR ("Too small RFCOMM Frame" );
15021505 return 0 ;
15031506 }
@@ -1507,19 +1510,28 @@ static int rfcomm_recv(struct bt_l2cap_chan *chan, struct net_buf *buf)
15071510
15081511 LOG_DBG ("session %p dlci %x type %x" , session , dlci , frame_type );
15091512
1510- fcs_len = (frame_type == BT_RFCOMM_UIH ) ? BT_RFCOMM_FCS_LEN_UIH :
1511- BT_RFCOMM_FCS_LEN_NON_UIH ;
1512- fcs = * (net_buf_tail (buf ) - 1 );
1513+ if (BT_RFCOMM_LEN_EXTENDED (hdr -> length )) {
1514+ msg_len = BT_RFCOMM_GET_LEN_EXTENDED (hdr_ext -> hdr .length , hdr_ext -> second_length );
1515+ hdr_len = sizeof (* hdr_ext );
1516+ } else {
1517+ msg_len = BT_RFCOMM_GET_LEN (hdr -> length );
1518+ hdr_len = sizeof (* hdr );
1519+ }
1520+
1521+ if (buf -> len < (hdr_len + msg_len + sizeof (fcs ))) {
1522+ LOG_ERR ("Too small RFCOMM information (%d < %d)" , buf -> len ,
1523+ hdr_len + msg_len + sizeof (fcs ));
1524+ return 0 ;
1525+ }
1526+
1527+ fcs_len = (frame_type == BT_RFCOMM_UIH ) ? BT_RFCOMM_FCS_LEN_UIH : hdr_len ;
1528+ fcs = * (net_buf_tail (buf ) - sizeof (fcs ));
15131529 if (!rfcomm_check_fcs (fcs_len , buf -> data , fcs )) {
15141530 LOG_ERR ("FCS check failed" );
15151531 return 0 ;
15161532 }
15171533
1518- if (BT_RFCOMM_LEN_EXTENDED (hdr -> length )) {
1519- net_buf_pull (buf , sizeof (* hdr ) + 1 );
1520- } else {
1521- net_buf_pull (buf , sizeof (* hdr ));
1522- }
1534+ net_buf_pull (buf , hdr_len );
15231535
15241536 switch (frame_type ) {
15251537 case BT_RFCOMM_SABM :
@@ -1529,8 +1541,7 @@ static int rfcomm_recv(struct bt_l2cap_chan *chan, struct net_buf *buf)
15291541 if (!dlci ) {
15301542 rfcomm_handle_msg (session , buf );
15311543 } else {
1532- rfcomm_handle_data (session , buf , dlci ,
1533- BT_RFCOMM_GET_PF (hdr -> control ));
1544+ rfcomm_handle_data (session , buf , dlci , BT_RFCOMM_GET_PF (hdr -> control ));
15341545 }
15351546 break ;
15361547 case BT_RFCOMM_DISC :
0 commit comments