Pārlūkot izejas kodu

Corrected bugs in the TCP logic. There might be a few more which need to be taken care of.

tags/v0.9.3
Nikhil Chandru Rao 18 gadus atpakaļ
vecāks
revīzija
8bf38fb400
1 mainītis faili ar 27 papildinājumiem un 25 dzēšanām
  1. 27
    25
      src/net/tcp.c

+ 27
- 25
src/net/tcp.c Parādīt failu

@@ -326,8 +326,6 @@ void tcp_trans ( struct tcp_connection *conn, int nxt_state ) {
326 326
 	conn->tcp_lstate = conn->tcp_state;
327 327
 	conn->tcp_state = nxt_state;
328 328
 
329
-	printf ( "Transition from %s to %s\n", tcp_states[conn->tcp_lstate], tcp_states[conn->tcp_state] );
330
-
331 329
 	/* TODO: Check if this check is required */
332 330
 	if ( conn->tcp_lstate == conn->tcp_state || 
333 331
 	     conn->tcp_state == TCP_INVALID ) {
@@ -343,20 +341,8 @@ void tcp_trans ( struct tcp_connection *conn, int nxt_state ) {
343 341
  * @v tcphdr	TCP header
344 342
  */
345 343
 void tcp_dump ( struct tcp_header *tcphdr ) {
346
-/*
347
-	DBG ( "TCP header at %p+%d\n", tcphdr, sizeof ( *tcphdr ) );
348
-	DBG ( "\tSource port = %d, Destination port = %d\n",
349
-		ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ) );
350
-	DBG ( "\tSequence Number = %ld, Acknowledgement Number = %ld\n",
351
-		ntohl ( tcphdr->seq ), ntohl ( tcphdr->ack ) );
352
-	DBG ( "\tHeader length (/4) = %hd, Flags [..RAPUSF]= %#x\n",
353
-		( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ),
354
-		( tcphdr->flags & TCP_MASK_FLAGS ) );
355
-	DBG ( "\tAdvertised window = %ld, Checksum = %x, Urgent Pointer = %d\n",
356
-		ntohs ( tcphdr->win ), tcphdr->csum, ntohs ( tcphdr->urg ) );
357
-*/
358
-	DBG ( "TCP %p at %p src:%d dest:%d seq:%lld ack:%lld hlen:%hd flags:%#hx\n",
359
-		&tcp_protocol, tcphdr, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ), ntohl ( tcphdr->seq ),
344
+	DBG ( "TCP %p src:%d dest:%d seq:%lld ack:%lld hlen:%hd flags:%#hx\n",
345
+		tcphdr, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ), ntohl ( tcphdr->seq ),
360 346
 		ntohl ( tcphdr->ack ), ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ), ( tcphdr->flags & TCP_MASK_FLAGS ) );
361 347
 }
362 348
 
@@ -437,7 +423,7 @@ void tcp_expired ( struct retry_timer *timer, int over ) {
437 423
 	return;
438 424
 
439 425
   send_tcp_nomsg:
440
-//	free_pkb ( conn->tx_pkb );
426
+	free_pkb ( conn->tx_pkb );
441 427
 	conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
442 428
 	pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
443 429
 	tcp_set_flags ( conn );
@@ -657,7 +643,6 @@ int tcp_send ( struct tcp_connection *conn, const void *data, size_t len ) {
657 643
 	tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
658 644
 	
659 645
 	/* Dump the TCP header */
660
-	tcp_dump ( tcphdr );
661 646
 
662 647
 	/* Start the timer */
663 648
 	if ( ( conn->tcp_state == TCP_ESTABLISHED && conn->tcp_lstate == TCP_SYN_SENT ) ||
@@ -685,7 +670,7 @@ static int tcp_rx ( struct pk_buff *pkb,
685 670
 	struct tcp_header *tcphdr;
686 671
 	uint32_t acked, toack;
687 672
 	int hlen;
688
-	int add = 0;
673
+	int add = 4;
689 674
 
690 675
 	/* Sanity check */
691 676
 	if ( pkb_len ( pkb ) < sizeof ( *tcphdr ) ) {
@@ -709,7 +694,8 @@ static int tcp_rx ( struct pk_buff *pkb,
709 694
 			return -EINVAL;
710 695
 		}
711 696
 	}
712
-	
697
+
698
+
713 699
 	/* TODO: Verify checksum */
714 700
 	
715 701
 	/* Demux TCP connection */
@@ -768,13 +754,14 @@ static int tcp_rx ( struct pk_buff *pkb,
768 754
 				 */
769 755
 				conn->snd_una = ntohl ( tcphdr->ack );
770 756
 				conn->tcp_op->connected ( conn );
757
+				out_flags |= TCP_ACK;
771 758
 				tcp_senddata ( conn );
759
+				return;
772 760
 			} else {
773 761
 				tcp_trans ( conn, TCP_SYN_RCVD );
774 762
 				out_flags |= TCP_SYN;
763
+				goto send_tcp_nomsg;
775 764
 			}
776
-			/* Send SYN,ACK or ACK packet */
777
-			goto send_tcp_nomsg;
778 765
 		}
779 766
 		/* Unexpected packet */
780 767
 		goto unexpected;
@@ -792,12 +779,12 @@ static int tcp_rx ( struct pk_buff *pkb,
792 779
 			 */
793 780
 			conn->snd_una = tcphdr->ack - 1;
794 781
 			conn->tcp_op->connected ( conn );
795
-			tcp_senddata ( conn );
796 782
 			return 0;
797 783
 		}
798 784
 		/* Unexpected packet */
799 785
 		goto unexpected;
800 786
 	case TCP_ESTABLISHED:
787
+#if 0
801 788
 		if ( tcphdr->flags & TCP_FIN ) {
802 789
 			tcp_trans ( conn, TCP_CLOSE_WAIT );
803 790
 			/* FIN consumes one byte */
@@ -806,6 +793,7 @@ static int tcp_rx ( struct pk_buff *pkb,
806 793
 			/* Send an acknowledgement */
807 794
 			goto send_tcp_nomsg;
808 795
 		}
796
+#endif
809 797
 		/* Packet might contain data */
810 798
 		break;
811 799
 	case TCP_FIN_WAIT_1:
@@ -874,6 +862,9 @@ static int tcp_rx ( struct pk_buff *pkb,
874 862
 		if ( conn->rcv_nxt == ntohl ( tcphdr->seq ) ) {
875 863
 			conn->rcv_nxt += toack;
876 864
 			conn->tcp_op->newdata ( conn, pkb->data + sizeof ( *tcphdr ) + add, toack );
865
+		} else {
866
+			printf ( "Unexpected sequence number %ld\n", 
867
+				ntohl ( tcphdr->seq ) );
877 868
 		}
878 869
 
879 870
 		/* Acknowledge new data */
@@ -892,13 +883,24 @@ static int tcp_rx ( struct pk_buff *pkb,
892 883
 		}
893 884
 		/* Advance snd stream */
894 885
 		conn->snd_una += acked;
895
-		/* Set the ACK flag */
896
-		conn->tcp_flags |= TCP_ACK;
897 886
 		/* Invoke the acked() callback function */
898 887
 		conn->tcp_op->acked ( conn, acked );
899 888
 		/* Invoke the senddata() callback function */
900 889
 		tcp_senddata ( conn );
901 890
 	}
891
+
892
+	/* If the connection is in ESTABLISHED and it receives a FIN */
893
+	if ( conn->tcp_state == ESTABLISHED && ( tcphdr->flags & TCP_FIN ) ) {
894
+		tcp_trans ( conn, TCP_CLOSE_WAIT );
895
+		conn->tcp_op->closed ( conn, CONN_SNDCLOSE );	
896
+		conn->rcv_nxt++;
897
+		if ( ! ( tcphdr->flags & TCP_ACK ) ) {
898
+			out_flags |= TCP_ACK;
899
+			/* Send an acknowledgement */
900
+			goto send_tcp_nomsg;
901
+		}
902
+		/* Otherwise, the packet has been ACKed already */
903
+	}
902 904
 	return 0;
903 905
 
904 906
   send_tcp_nomsg:

Notiek ielāde…
Atcelt
Saglabāt