@@ -626,21 +626,15 @@ static int tcp_hdr_len(struct net_buf *buf)
626626 return 4 * (hdr -> offset >> 4 );
627627}
628628
629- /* This is called when we receive data after the connection has been
630- * established. The core TCP logic is located here.
631- */
632- static enum net_verdict tcp_established (struct net_conn * conn ,
629+ static enum net_verdict tcp_passive_close (struct net_conn * conn ,
633630 struct net_buf * buf ,
634631 void * user_data )
635632{
636633 struct net_context * context = (struct net_context * )user_data ;
637- struct net_tcp_hdr * hdr ;
638- enum net_verdict ret ;
639634
640635 NET_ASSERT (context && context -> tcp );
641636
642637 switch (context -> tcp -> state ) {
643- case NET_TCP_ESTABLISHED :
644638 case NET_TCP_CLOSE_WAIT :
645639 case NET_TCP_LAST_ACK :
646640 break ;
@@ -650,36 +644,65 @@ static enum net_verdict tcp_established(struct net_conn *conn,
650644 return NET_DROP ;
651645 }
652646
653- net_tcp_print_recv_info ("DATA" , buf , NET_TCP_BUF (buf )-> src_port );
647+ net_tcp_print_recv_info ("PASSCLOSE" , buf , NET_TCP_BUF (buf )-> src_port );
648+
649+ if (context -> tcp -> state == NET_TCP_LAST_ACK &&
650+ NET_TCP_FLAGS (buf ) & NET_TCP_ACK ) {
651+ net_context_put (context );
652+ }
653+
654+ return NET_DROP ;
655+ }
654656
655- hdr = (void * )net_nbuf_tcp_data (buf );
657+ /* This is called when we receive data after the connection has been
658+ * established. The core TCP logic is located here.
659+ */
660+ static enum net_verdict tcp_established (struct net_conn * conn ,
661+ struct net_buf * buf ,
662+ void * user_data )
663+ {
664+ struct net_context * context = (struct net_context * )user_data ;
665+ enum net_verdict ret ;
666+
667+ NET_ASSERT (context && context -> tcp );
668+
669+ if (context -> tcp -> state != NET_TCP_ESTABLISHED ) {
670+ NET_DBG ("Context %p in wrong state %d" ,
671+ context , context -> tcp -> state );
672+ return NET_DROP ;
673+ }
674+
675+ net_tcp_print_recv_info ("DATA" , buf , NET_TCP_BUF (buf )-> src_port );
656676
657677 if (NET_TCP_FLAGS (buf ) & NET_TCP_FIN ) {
658- /* Sending a FIN and ACK in the CLOSE_WAIT state will
659- * transition to LAST_ACK state
678+ /* Sending an ACK in the CLOSE_WAIT state will transition to
679+ * LAST_ACK state
660680 */
661681 net_tcp_change_state (context -> tcp , NET_TCP_CLOSE_WAIT );
662- send_fin_ack (context , & conn -> remote_addr );
663- return NET_DROP ;
664- }
665- if (NET_TCP_FLAGS (buf ) & NET_TCP_ACK ) {
666- if (context -> tcp -> state == NET_TCP_LAST_ACK ) {
667- net_context_put (context );
682+ net_conn_change_callback (context -> conn_handler ,
683+ tcp_passive_close , context );
684+
685+ ret = NET_DROP ;
686+
687+ context -> tcp -> send_ack =
688+ sys_get_be32 (NET_TCP_BUF (buf )-> seq ) + 1 ;
689+ } else {
690+ struct net_tcp_hdr * hdr = (void * )net_nbuf_tcp_data (buf );
691+
692+ if (sys_get_be32 (hdr -> seq ) - context -> tcp -> send_ack ) {
693+ /* Don't try to reorder packets. If it doesn't
694+ * match the next segment exactly, drop and wait for
695+ * retransmit
696+ */
668697 return NET_DROP ;
669698 }
670- }
671- if (sys_get_be32 (hdr -> seq ) - context -> tcp -> send_ack ) {
672- /* Don't try to reorder packets. If it doesn't match
673- * the next segment exactly, drop and wait for
674- * retransmit
675- */
676- return NET_DROP ;
677- }
678699
679- ret = packet_received (conn , buf , user_data );
700+ ret = packet_received (conn , buf , user_data );
701+
702+ context -> tcp -> send_ack += net_buf_frags_len (buf )
703+ - net_nbuf_ip_hdr_len (buf ) - tcp_hdr_len (buf );
704+ }
680705
681- context -> tcp -> send_ack += net_buf_frags_len (buf )
682- - net_nbuf_ip_hdr_len (buf ) - tcp_hdr_len (buf );
683706 send_ack (context , & conn -> remote_addr );
684707
685708 return ret ;
0 commit comments