1818#include <linux/sched.h>
1919#include <linux/if_arp.h>
2020#include <linux/timer.h>
21+ #include <linux/rtnetlink.h>
2122#include <net/caif/caif_layer.h>
2223#include <net/caif/caif_hsi.h>
2324
@@ -348,8 +349,7 @@ static void cfhsi_tx_done_cb(struct cfhsi_drv *drv)
348349 cfhsi_tx_done (cfhsi );
349350}
350351
351- static int cfhsi_rx_desc (struct cfhsi_desc * desc , struct cfhsi * cfhsi ,
352- bool * dump )
352+ static int cfhsi_rx_desc (struct cfhsi_desc * desc , struct cfhsi * cfhsi )
353353{
354354 int xfer_sz = 0 ;
355355 int nfrms = 0 ;
@@ -360,8 +360,7 @@ static int cfhsi_rx_desc(struct cfhsi_desc *desc, struct cfhsi *cfhsi,
360360 (desc -> offset > CFHSI_MAX_EMB_FRM_SZ )) {
361361 dev_err (& cfhsi -> ndev -> dev , "%s: Invalid descriptor.\n" ,
362362 __func__ );
363- * dump = true;
364- return 0 ;
363+ return - EPROTO ;
365364 }
366365
367366 /* Check for embedded CAIF frame. */
@@ -379,6 +378,12 @@ static int cfhsi_rx_desc(struct cfhsi_desc *desc, struct cfhsi *cfhsi,
379378 len |= ((* (pfrm + 1 )) << 8 ) & 0xFF00 ;
380379 len += 2 ; /* Add FCS fields. */
381380
381+ /* Sanity check length of CAIF frame. */
382+ if (unlikely (len > CFHSI_MAX_CAIF_FRAME_SZ )) {
383+ dev_err (& cfhsi -> ndev -> dev , "%s: Invalid length.\n" ,
384+ __func__ );
385+ return - EPROTO ;
386+ }
382387
383388 /* Allocate SKB (OK even in IRQ context). */
384389 skb = alloc_skb (len + 1 , GFP_ATOMIC );
@@ -423,18 +428,16 @@ static int cfhsi_rx_desc(struct cfhsi_desc *desc, struct cfhsi *cfhsi,
423428 if (desc -> header & CFHSI_PIGGY_DESC )
424429 xfer_sz += CFHSI_DESC_SZ ;
425430
426- if (xfer_sz % 4 ) {
431+ if (( xfer_sz % 4 ) || ( xfer_sz > ( CFHSI_BUF_SZ_RX - CFHSI_DESC_SZ )) ) {
427432 dev_err (& cfhsi -> ndev -> dev ,
428433 "%s: Invalid payload len: %d, ignored.\n" ,
429434 __func__ , xfer_sz );
430- xfer_sz = 0 ;
431- * dump = true;
435+ return - EPROTO ;
432436 }
433437 return xfer_sz ;
434438}
435439
436- static int cfhsi_rx_pld (struct cfhsi_desc * desc , struct cfhsi * cfhsi ,
437- bool * dump )
440+ static int cfhsi_rx_pld (struct cfhsi_desc * desc , struct cfhsi * cfhsi )
438441{
439442 int rx_sz = 0 ;
440443 int nfrms = 0 ;
@@ -446,8 +449,7 @@ static int cfhsi_rx_pld(struct cfhsi_desc *desc, struct cfhsi *cfhsi,
446449 (desc -> offset > CFHSI_MAX_EMB_FRM_SZ ))) {
447450 dev_err (& cfhsi -> ndev -> dev , "%s: Invalid descriptor.\n" ,
448451 __func__ );
449- * dump = true;
450- return - EINVAL ;
452+ return - EPROTO ;
451453 }
452454
453455 /* Set frame pointer to start of payload. */
@@ -469,13 +471,6 @@ static int cfhsi_rx_pld(struct cfhsi_desc *desc, struct cfhsi *cfhsi,
469471 u8 * pcffrm = NULL ;
470472 int len = 0 ;
471473
472- if (WARN_ON (desc -> cffrm_len [nfrms ] > CFHSI_MAX_PAYLOAD_SZ )) {
473- dev_err (& cfhsi -> ndev -> dev , "%s: Invalid payload.\n" ,
474- __func__ );
475- * dump = true;
476- return - EINVAL ;
477- }
478-
479474 /* CAIF frame starts after head padding. */
480475 pcffrm = pfrm + * pfrm + 1 ;
481476
@@ -484,6 +479,13 @@ static int cfhsi_rx_pld(struct cfhsi_desc *desc, struct cfhsi *cfhsi,
484479 len |= ((* (pcffrm + 1 )) << 8 ) & 0xFF00 ;
485480 len += 2 ; /* Add FCS fields. */
486481
482+ /* Sanity check length of CAIF frames. */
483+ if (unlikely (len > CFHSI_MAX_CAIF_FRAME_SZ )) {
484+ dev_err (& cfhsi -> ndev -> dev , "%s: Invalid length.\n" ,
485+ __func__ );
486+ return - EPROTO ;
487+ }
488+
487489 /* Allocate SKB (OK even in IRQ context). */
488490 skb = alloc_skb (len + 1 , GFP_ATOMIC );
489491 if (!skb ) {
@@ -528,7 +530,6 @@ static void cfhsi_rx_done(struct cfhsi *cfhsi)
528530 int res ;
529531 int desc_pld_len = 0 ;
530532 struct cfhsi_desc * desc = NULL ;
531- bool dump = false;
532533
533534 desc = (struct cfhsi_desc * )cfhsi -> rx_buf ;
534535
@@ -544,16 +545,20 @@ static void cfhsi_rx_done(struct cfhsi *cfhsi)
544545 spin_unlock_bh (& cfhsi -> lock );
545546
546547 if (cfhsi -> rx_state .state == CFHSI_RX_STATE_DESC ) {
547- desc_pld_len = cfhsi_rx_desc (desc , cfhsi , & dump );
548+ desc_pld_len = cfhsi_rx_desc (desc , cfhsi );
548549 if (desc_pld_len == - ENOMEM )
549550 goto restart ;
551+ if (desc_pld_len == - EPROTO )
552+ goto out_of_sync ;
550553 } else {
551554 int pld_len ;
552555
553556 if (!cfhsi -> rx_state .piggy_desc ) {
554- pld_len = cfhsi_rx_pld (desc , cfhsi , & dump );
557+ pld_len = cfhsi_rx_pld (desc , cfhsi );
555558 if (pld_len == - ENOMEM )
556559 goto restart ;
560+ if (pld_len == - EPROTO )
561+ goto out_of_sync ;
557562 cfhsi -> rx_state .pld_len = pld_len ;
558563 } else {
559564 pld_len = cfhsi -> rx_state .pld_len ;
@@ -567,7 +572,7 @@ static void cfhsi_rx_done(struct cfhsi *cfhsi)
567572 cfhsi -> rx_state .piggy_desc = true;
568573
569574 /* Extract piggy-backed descriptor. */
570- desc_pld_len = cfhsi_rx_desc (piggy_desc , cfhsi , & dump );
575+ desc_pld_len = cfhsi_rx_desc (piggy_desc , cfhsi );
571576 if (desc_pld_len == - ENOMEM )
572577 goto restart ;
573578
@@ -577,15 +582,10 @@ static void cfhsi_rx_done(struct cfhsi *cfhsi)
577582 */
578583 memcpy ((u8 * )desc , (u8 * )piggy_desc ,
579584 CFHSI_DESC_SHORT_SZ );
580- }
581- }
582585
583- if (unlikely (dump )) {
584- size_t rx_offset = cfhsi -> rx_ptr - cfhsi -> rx_buf ;
585- dev_err (& cfhsi -> ndev -> dev , "%s: RX offset: %u.\n" ,
586- __func__ , (unsigned ) rx_offset );
587- print_hex_dump_bytes ("--> " , DUMP_PREFIX_NONE ,
588- cfhsi -> rx_buf , cfhsi -> rx_len + rx_offset );
586+ if (desc_pld_len == - EPROTO )
587+ goto out_of_sync ;
588+ }
589589 }
590590
591591 memset (& cfhsi -> rx_state , 0 , sizeof (cfhsi -> rx_state ));
@@ -622,6 +622,13 @@ static void cfhsi_rx_done(struct cfhsi *cfhsi)
622622 BUG ();
623623 }
624624 mod_timer (& cfhsi -> rx_slowpath_timer , jiffies + 1 );
625+ return ;
626+
627+ out_of_sync :
628+ dev_err (& cfhsi -> ndev -> dev , "%s: Out of sync.\n" , __func__ );
629+ print_hex_dump_bytes ("--> " , DUMP_PREFIX_NONE ,
630+ cfhsi -> rx_buf , CFHSI_DESC_SZ );
631+ schedule_work (& cfhsi -> out_of_sync_work );
625632}
626633
627634static void cfhsi_rx_slowpath (unsigned long arg )
@@ -804,6 +811,17 @@ static void cfhsi_wake_down(struct work_struct *work)
804811
805812}
806813
814+ static void cfhsi_out_of_sync (struct work_struct * work )
815+ {
816+ struct cfhsi * cfhsi = NULL ;
817+
818+ cfhsi = container_of (work , struct cfhsi , out_of_sync_work );
819+
820+ rtnl_lock ();
821+ dev_close (cfhsi -> ndev );
822+ rtnl_unlock ();
823+ }
824+
807825static void cfhsi_wake_up_cb (struct cfhsi_drv * drv )
808826{
809827 struct cfhsi * cfhsi = NULL ;
@@ -1023,6 +1041,7 @@ int cfhsi_probe(struct platform_device *pdev)
10231041 /* Initialize the work queues. */
10241042 INIT_WORK (& cfhsi -> wake_up_work , cfhsi_wake_up );
10251043 INIT_WORK (& cfhsi -> wake_down_work , cfhsi_wake_down );
1044+ INIT_WORK (& cfhsi -> out_of_sync_work , cfhsi_out_of_sync );
10261045
10271046 /* Clear all bit fields. */
10281047 clear_bit (CFHSI_WAKE_UP_ACK , & cfhsi -> bits );
0 commit comments