8 #include <gpxe/process.h>
10 #include <gpxe/netdevice.h>
11 #include <gpxe/pkbuff.h>
14 #include <gpxe/tcpip.h>
15 #include <gpxe/retry.h>
22 * The gPXE TCP stack is currently implemented on top of the uIP
23 * protocol stack. This file provides wrappers around uIP so that
24 * higher-level protocol implementations do not need to talk directly
25 * to uIP (which has a somewhat baroque API).
27 * Basic operation is to create a #tcp_connection structure, call
28 * tcp_connect() and then call run_tcpip() in a loop until the
29 * operation has completed. The TCP stack will call the various
30 * methods defined in the #tcp_operations structure in order to send
33 * See hello.c for a trivial example of a TCP protocol using this
43 * When a tcp_operations::senddata() method is called, it is
44 * guaranteed to be able to use this buffer as temporary space for
45 * constructing the data to be sent. For example, code such as
49 * static void my_senddata ( struct tcp_connection *conn, void *buf,
51 * len = snprintf ( buf, len, "FETCH %s\r\n", filename );
52 * tcp_send ( conn, buf + already_sent, len - already_sent );
57 * is allowed, and is probably the best way to deal with
58 * variably-sized data.
60 * Note that you cannot use this simple mechanism if you want to be
61 * able to construct single data blocks of more than #len bytes.
63 static void *tcp_buffer = uip_buf + ( 40 + UIP_LLH_LEN );
65 /** Size of #tcp_buffer */
66 static size_t tcp_buflen = UIP_BUFSIZE - ( 40 + UIP_LLH_LEN );
69 * Open a TCP connection
71 * @v conn TCP connection
73 * This sets up a new TCP connection to the remote host specified in
74 * tcp_connection::sin.
76 void tcp_connect ( struct tcp_connection *conn ) {
77 struct uip_conn *uip_conn;
80 assert ( conn->sin.sin_addr.s_addr != 0 );
81 assert ( conn->sin.sin_port != 0 );
82 assert ( conn->tcp_op != NULL );
83 assert ( sizeof ( uip_conn->appstate ) == sizeof ( conn ) );
85 * ( ( uint32_t * ) ipaddr ) = conn->sin.sin_addr.s_addr;
86 uip_conn = uip_connect ( ipaddr, conn->sin.sin_port );
87 #warning "Use linked lists so that uip_connect() cannot fail"
88 assert ( uip_conn != NULL );
89 *( ( void ** ) uip_conn->appstate ) = conn;
93 * Send data via a TCP connection
95 * @v conn TCP connection
96 * @v data Data to send
97 * @v len Length of data
99 * Data will be automatically limited to the current TCP window size.
101 * If retransmission is required, the connection's
102 * tcp_operations::senddata() method will be called again in order to
103 * regenerate the data.
105 void tcp_send ( struct tcp_connection *conn __unused,
106 const void *data, size_t len ) {
108 assert ( conn = *( ( void ** ) uip_conn->appstate ) );
110 if ( len > tcp_buflen )
112 memmove ( tcp_buffer, data, len );
114 uip_send ( tcp_buffer, len );
118 * Close a TCP connection
120 * @v conn TCP connection
122 void tcp_close ( struct tcp_connection *conn __unused ) {
123 assert ( conn = *( ( void ** ) uip_conn->appstate ) );
128 * uIP TCP application call interface
130 * This is the entry point of gPXE from the point of view of the uIP
131 * protocol stack. This function calls the appropriate methods from
132 * the connection's @tcp_operations table in order to process received
133 * data, transmit new data etc.
135 void uip_tcp_appcall ( void ) {
136 struct tcp_connection *conn = *( ( void ** ) uip_conn->appstate );
137 struct tcp_operations *op = conn->tcp_op;
141 op->closed ( conn, -ECONNABORTED );
142 if ( uip_timedout() )
143 op->closed ( conn, -ETIMEDOUT );
145 op->closed ( conn, 0 );
147 if ( uip_connected() && op->connected )
148 op->connected ( conn );
149 if ( uip_acked() && op->acked )
150 op->acked ( conn, uip_conn->len );
151 if ( uip_newdata() && op->newdata )
152 op->newdata ( conn, ( void * ) uip_appdata, uip_len );
153 if ( ( uip_rexmit() || uip_newdata() || uip_acked() ||
154 uip_connected() || uip_poll() ) && op->senddata )
155 op->senddata ( conn, tcp_buffer, tcp_buflen );
158 /* Present here to allow everything to link. Will go into separate
161 void uip_udp_appcall ( void ) {
165 * Perform periodic processing of all TCP connections
167 * This allows TCP connections to retransmit data if necessary.
169 static void tcp_periodic ( void ) {
173 for ( i = 0 ; i < UIP_CONNS ; i++ ) {
176 pkb = alloc_pkb ( uip_len + MAX_LL_HEADER_LEN);
180 pkb_reserve ( pkb, MAX_LL_HEADER_LEN );
181 pkb_put ( pkb, uip_len );
182 memcpy ( pkb->data, uip_buf, uip_len );
190 * Kick a connection into life
192 * @v conn TCP connection
194 * Call this function when you have new data to send and are not
195 * already being called as part of TCP processing.
197 void tcp_kick ( struct tcp_connection *conn __unused ) {
198 /* Just kick all the connections; this will work for now */
203 * Single-step the TCP stack
205 * @v process TCP process
207 * This calls tcp_periodic() at regular intervals.
209 static void tcp_step ( struct process *process ) {
210 static unsigned long timeout = 0;
212 if ( currticks() > timeout ) {
213 timeout = currticks() + ( TICKS_PER_SEC / 10 );
217 schedule ( process );
220 /** TCP stack process */
221 static struct process tcp_process = {
225 /** Initialise the TCP stack */
226 static void init_tcp ( void ) {
227 schedule ( &tcp_process );
230 INIT_FN ( INIT_PROCESS, init_tcp, NULL, NULL );
235 * List of registered TCP connections
237 static LIST_HEAD ( tcp_conns );
242 static const char *tcp_states[] = {
257 * TCP state transition function
259 * @v conn TCP connection
260 * @v nxt_state Next TCP state
262 void tcp_set_flags ( struct tcp_connection *conn ) {
264 /* Set the TCP flags */
265 switch ( conn->tcp_state ) {
267 if ( conn->tcp_lstate == TCP_SYN_RCVD ) {
268 conn->tcp_flags |= TCP_RST;
274 if ( conn->tcp_lstate == TCP_LISTEN ||
275 conn->tcp_lstate == TCP_CLOSED ) {
276 conn->tcp_flags |= TCP_SYN;
280 if ( conn->tcp_lstate == TCP_LISTEN ||
281 conn->tcp_lstate == TCP_SYN_SENT ) {
282 conn->tcp_flags |= ( TCP_SYN | TCP_ACK );
285 case TCP_ESTABLISHED:
286 if ( conn->tcp_lstate == TCP_SYN_SENT ) {
287 conn->tcp_flags |= TCP_ACK;
291 if ( conn->tcp_lstate == TCP_SYN_RCVD ||
292 conn->tcp_lstate == TCP_ESTABLISHED ) {
293 conn->tcp_flags |= TCP_FIN;
299 if ( conn->tcp_lstate == TCP_FIN_WAIT_1 ) {
300 conn->tcp_flags |= TCP_ACK;
304 if ( conn->tcp_lstate == TCP_FIN_WAIT_1 ||
305 conn->tcp_lstate == TCP_FIN_WAIT_2 ) {
306 conn->tcp_flags |= TCP_ACK;
310 if ( conn->tcp_lstate == TCP_ESTABLISHED ) {
311 conn->tcp_flags |= TCP_ACK;
315 if ( conn->tcp_lstate == TCP_CLOSE_WAIT ) {
316 conn->tcp_flags |= TCP_FIN;
318 if ( conn->tcp_lstate == TCP_ESTABLISHED ) {
319 conn->tcp_flags |= ( TCP_FIN | TCP_ACK );
323 DBG ( "TCP_INVALID state %d\n", conn->tcp_state );
328 void tcp_trans ( struct tcp_connection *conn, int nxt_state ) {
329 /* Remember the last state */
330 conn->tcp_lstate = conn->tcp_state;
331 conn->tcp_state = nxt_state;
333 DBG ( "Transition from %s to %s\n", tcp_states[conn->tcp_lstate], tcp_states[conn->tcp_state] );
335 /* TODO: Check if this check is required */
336 if ( conn->tcp_lstate == conn->tcp_state ||
337 conn->tcp_state == TCP_INVALID ) {
341 tcp_set_flags ( conn );
347 * @v tcphdr TCP header
349 void tcp_dump ( struct tcp_header *tcphdr ) {
350 DBG ( "TCP %p src:%d dest:%d seq:%lx ack:%lx hlen:%hd flags:%#hx\n",
351 tcphdr, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ), ntohl ( tcphdr->seq ),
352 ntohl ( tcphdr->ack ), ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ), ( tcphdr->flags & TCP_MASK_FLAGS ) );
356 * Initialize a TCP connection
358 * @v conn TCP connection
360 * This function assigns initial values to some fields in the connection
361 * structure. The application should call tcp_init_conn after creating a new
362 * connection before calling any other "tcp_*" function.
364 * struct tcp_connection my_conn;
365 * tcp_init_conn ( &my_conn );
368 void tcp_init_conn ( struct tcp_connection *conn ) {
369 conn->local_port = 0;
370 conn->tcp_state = TCP_CLOSED;
371 conn->tcp_lstate = TCP_INVALID;
378 * @v timer Retry timer
379 * @v over Failure indicator
381 void tcp_expired ( struct retry_timer *timer, int over ) {
382 struct tcp_connection *conn;
383 conn = ( struct tcp_connection * ) container_of ( timer,
384 struct tcp_connection, timer );
385 DBG ( "Timer expired in %s\n", tcp_states[conn->tcp_state] );
386 switch ( conn->tcp_state ) {
389 tcp_trans ( conn, TCP_CLOSED );
390 DBG ( "Timeout! Connection closed\n" );
396 tcp_trans ( conn, TCP_CLOSED );
400 case TCP_ESTABLISHED:
401 if ( conn->tcp_lstate == TCP_SYN_SENT ) {
406 if ( conn->tcp_lstate == TCP_ESTABLISHED ) {
415 if ( conn->tcp_lstate == TCP_CLOSE_WAIT ) {
420 tcp_trans ( conn, TCP_CLOSED );
423 /* Retransmit the data */
424 tcp_set_flags ( conn );
425 tcp_senddata ( conn );
429 free_pkb ( conn->tx_pkb );
430 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
431 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
432 tcp_set_flags ( conn );
434 if ( ( rc = tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN ) ) != 0 ) {
435 DBG ( "Error sending TCP message (rc = %d)\n", rc );
441 * Connect to a remote server
443 * @v conn TCP connection
444 * @v peer Remote socket address
446 * This function initiates a TCP connection to the socket address specified in
447 * peer. It sends a SYN packet to peer. When the connection is established, the
448 * TCP stack calls the connected() callback function.
450 int tcp_connectto ( struct tcp_connection *conn,
451 struct sockaddr_tcpip *peer ) {
454 /* A connection can only be established from the CLOSED state */
455 if ( conn->tcp_state != TCP_CLOSED ) {
456 DBG ( "Error opening connection: Invalid state %s\n",
457 tcp_states[conn->tcp_state] );
461 /* Add the connection to the set of listening connections */
462 if ( ( rc = tcp_listen ( conn, conn->local_port ) ) != 0 ) {
465 memcpy ( &conn->peer, peer, sizeof ( conn->peer ) );
467 /* Initialize the TCP timer */
468 conn->timer.expired = tcp_expired;
470 /* Send a SYN packet and transition to TCP_SYN_SENT */
471 conn->snd_una = random();
472 tcp_trans ( conn, TCP_SYN_SENT );
473 /* Allocate space for the packet */
474 free_pkb ( conn->tx_pkb );
475 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
476 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
477 conn->rcv_win = MAX_PKB_LEN - MAX_HDR_LEN; /* TODO: Is this OK? */
478 return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
481 int tcp_connect ( struct tcp_connection *conn ) {
482 return tcp_connectto ( conn, &conn->peer );
486 * Close the connection
490 * This function sends a FIN packet to the remote end of the connection. When
491 * the remote end of the connection ACKs the FIN (FIN consumes one byte on the
492 * snd stream), the stack invokes the closed() callback function.
494 int tcp_close ( struct tcp_connection *conn ) {
495 /* A connection can only be closed if it is a connected state */
496 switch ( conn->tcp_state ) {
498 case TCP_ESTABLISHED:
499 tcp_trans ( conn, TCP_FIN_WAIT_1 );
500 if ( conn->tcp_op->closed )
501 conn->tcp_op->closed ( conn, CONN_SNDCLOSE ); /* TODO: Check! */
502 /* FIN consumes one byte on the snd stream */
508 * Since the connection does not expect any packets from the
509 * remote end, it can be removed from the set of listening
512 list_del ( &conn->list );
513 tcp_trans ( conn, TCP_CLOSED );
514 if ( conn->tcp_op->closed )
515 conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
518 tcp_trans ( conn, TCP_LAST_ACK );
519 if ( conn->tcp_op->closed )
520 conn->tcp_op->closed ( conn, CONN_SNDCLOSE ); /* TODO: Check! */
521 /* FIN consumes one byte on the snd stream */
525 DBG ( "tcp_close(): Invalid state %s\n",
526 tcp_states[conn->tcp_state] );
531 free_pkb ( conn->tx_pkb );
532 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
533 conn->tcp_flags = TCP_FIN;
534 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
535 return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
539 * Bind TCP connection to local port
541 * @v conn TCP connection
542 * @v local_port Local port, in network byte order
543 * @ret rc Return status code
545 int tcp_bind ( struct tcp_connection *conn, uint16_t local_port ) {
546 struct tcp_connection *existing;
548 list_for_each_entry ( existing, &tcp_conns, list ) {
549 if ( existing->local_port == local_port )
552 conn->local_port = local_port;
558 * Listen for a packet
560 * @v conn TCP connection
561 * @v local_port Local port, in network byte order
563 * This function adds the connection to a list of registered tcp
564 * connections. If the local port is 0, the connection is assigned an
565 * available port between MIN_TCP_PORT and 65535.
567 int tcp_listen ( struct tcp_connection *conn, uint16_t local_port ) {
568 static uint16_t try_port = 1024;
571 #warning "Fix the port re-use bug"
572 /* If we re-use the same port, the connection should be reset
573 * and a new connection set up. This doesn't happen yet, so
574 * randomise the port to avoid hitting the problem.
578 /* If no port specified, find the first available port */
579 if ( ! local_port ) {
580 for ( ; try_port ; try_port++ ) {
581 if ( try_port < 1024 )
583 if ( tcp_listen ( conn, htons ( try_port ) ) == 0 )
589 /* Attempt bind to local port */
590 if ( ( rc = tcp_bind ( conn, local_port ) ) != 0 )
593 /* Add to TCP connection list */
594 list_add ( &conn->list, &tcp_conns );
595 DBG ( "TCP opened %p on port %d\n", conn, ntohs ( local_port ) );
603 * @v conn TCP connection
605 * This function allocates space to the transmit buffer and invokes the
606 * senddata() callback function. It passes the allocated buffer to senddata().
607 * The applicaion may use this space to write it's data.
609 int tcp_senddata ( struct tcp_connection *conn ) {
610 /* The connection must be in a state in which the user can send data */
611 switch ( conn->tcp_state ) {
613 tcp_trans ( conn, TCP_SYN_SENT );
614 conn->snd_una = random();
616 case TCP_ESTABLISHED:
620 DBG ( "tcp_senddata: Invalid state %s\n",
621 tcp_states[conn->tcp_state] );
625 /* Allocate space to the TX buffer */
626 free_pkb ( conn->tx_pkb );
627 conn->tx_pkb = alloc_pkb ( MAX_PKB_LEN );
628 if ( !conn->tx_pkb ) {
629 DBG ( "Insufficient memory\n" );
632 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
633 /* Set the advertised window */
634 conn->rcv_win = pkb_available ( conn->tx_pkb );
635 /* Call the senddata() call back function */
636 if ( conn->tcp_op->senddata )
637 conn->tcp_op->senddata ( conn, conn->tx_pkb->data,
638 pkb_available ( conn->tx_pkb ) );
639 /* Send pure ACK if senddata() didn't call tcp_send() */
640 if ( conn->tx_pkb ) {
641 tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
649 * @v conn TCP connection
650 * @v data Data to be sent
651 * @v len Length of the data
653 * This function sends data to the peer socket address
655 int tcp_send ( struct tcp_connection *conn, const void *data, size_t len ) {
656 struct sockaddr_tcpip *peer = &conn->peer;
660 /* Take ownership of the TX buffer from the connection */
664 /* Determine the amount of data to be sent */
665 slen = len < conn->snd_win ? len : conn->snd_win;
667 memmove ( pkb_put ( pkb, slen ), data, slen );
669 /* Fill up the TCP header */
670 struct tcp_header *tcphdr = pkb_push ( pkb, sizeof ( *tcphdr ) );
672 /* Source port, assumed to be in network byte order in conn */
673 tcphdr->src = conn->local_port;
674 /* Destination port, assumed to be in network byte order in peer */
675 tcphdr->dest = peer->st_port;
676 tcphdr->seq = htonl ( conn->snd_una );
677 tcphdr->ack = htonl ( conn->rcv_nxt );
678 /* Header length, = 0x50 (without TCP options) */
679 tcphdr->hlen = ( uint8_t ) ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
680 /* Copy TCP flags, and then reset the variable */
681 tcphdr->flags = conn->tcp_flags;
683 /* Advertised window, in network byte order */
684 tcphdr->win = htons ( conn->rcv_win );
685 /* Set urgent pointer to 0 */
687 /* Calculate and store partial checksum, in host byte order */
689 tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
691 /* Dump the TCP header */
694 /* Start the timer */
695 if ( ( conn->tcp_state == TCP_ESTABLISHED && conn->tcp_lstate == TCP_SYN_SENT ) ||
696 ( conn->tcp_state == TCP_LISTEN && conn->tcp_lstate == TCP_SYN_RCVD ) ||
697 ( conn->tcp_state == TCP_CLOSED && conn->tcp_lstate == TCP_SYN_RCVD ) ||
698 ( conn->tcp_state == TCP_ESTABLISHED && ( len == 0 ) ) ) {
699 // Don't start the timer
701 start_timer ( &conn->timer );
704 /* Transmit packet */
705 return tcpip_tx ( pkb, &tcp_protocol, peer );
709 * Process received packet
711 * @v pkb Packet buffer
712 * @v partial Partial checksum
714 static int tcp_rx ( struct pk_buff *pkb,
715 struct sockaddr_tcpip *st_src __unused,
716 struct sockaddr_tcpip *st_dest __unused ) {
717 struct tcp_connection *conn;
718 struct tcp_header *tcphdr;
719 uint32_t acked, toack;
724 if ( pkb_len ( pkb ) < sizeof ( *tcphdr ) ) {
725 DBG ( "Packet too short (%d bytes)\n", pkb_len ( pkb ) );
730 /* Process TCP header */
734 /* Verify header length */
735 hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
736 if ( hlen < sizeof ( *tcphdr ) ) {
737 DBG ( "Bad header length (%d bytes)\n", hlen );
741 /* TODO: Parse TCP options */
742 if ( hlen != sizeof ( *tcphdr ) ) {
743 DBG ( "Ignoring TCP options\n" );
746 /* TODO: Verify checksum */
748 /* Demux TCP connection */
749 list_for_each_entry ( conn, &tcp_conns, list ) {
750 if ( tcphdr->dest == conn->local_port ) {
755 DBG ( "No connection found on port %d\n", ntohs ( tcphdr->dest ) );
761 stop_timer ( &conn->timer );
763 /* Set the advertised window */
764 conn->snd_win = tcphdr->win;
766 /* TCP State Machine */
767 conn->tcp_lstate = conn->tcp_state;
768 switch ( conn->tcp_state ) {
770 DBG ( "tcp_rx(): Invalid state %s\n",
771 tcp_states[conn->tcp_state] );
775 if ( tcphdr->flags & TCP_SYN ) {
776 tcp_trans ( conn, TCP_SYN_RCVD );
777 /* Synchronize the sequence numbers */
778 conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
779 conn->tcp_flags |= TCP_ACK;
781 /* Set the sequence number for the snd stream */
782 conn->snd_una = random();
783 conn->tcp_flags |= TCP_SYN;
785 /* Send a SYN,ACK packet */
788 /* Unexpected packet */
791 if ( tcphdr->flags & TCP_SYN ) {
792 /* Synchronize the sequence number in rcv stream */
793 conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
794 conn->tcp_flags |= TCP_ACK;
796 if ( tcphdr->flags & TCP_ACK ) {
797 tcp_trans ( conn, TCP_ESTABLISHED );
799 * Process ACK of SYN. This does not invoke the
800 * acked() callback function.
802 conn->snd_una = ntohl ( tcphdr->ack );
803 if ( conn->tcp_op->connected )
804 conn->tcp_op->connected ( conn );
805 conn->tcp_flags |= TCP_ACK;
806 tcp_senddata ( conn );
810 tcp_trans ( conn, TCP_SYN_RCVD );
811 conn->tcp_flags |= TCP_SYN;
815 /* Unexpected packet */
818 if ( tcphdr->flags & TCP_RST ) {
819 tcp_trans ( conn, TCP_LISTEN );
820 if ( conn->tcp_op->closed )
821 conn->tcp_op->closed ( conn, CONN_RESTART );
825 if ( tcphdr->flags & TCP_ACK ) {
826 tcp_trans ( conn, TCP_ESTABLISHED );
828 * Process ACK of SYN. It neither invokes the callback
829 * function nor does it send an ACK.
831 conn->snd_una = tcphdr->ack - 1;
832 if ( conn->tcp_op->connected )
833 conn->tcp_op->connected ( conn );
837 /* Unexpected packet */
839 case TCP_ESTABLISHED:
840 if ( tcphdr->flags & TCP_FIN ) {
841 if ( tcphdr->flags & TCP_ACK ) {
842 tcp_trans ( conn, TCP_LAST_ACK );
843 conn->tcp_flags |= TCP_FIN;
845 tcp_trans ( conn, TCP_CLOSE_WAIT );
847 /* FIN consumes one byte */
849 conn->tcp_flags |= TCP_ACK;
850 /* Send the packet */
853 /* Packet might contain data */
856 if ( tcphdr->flags & TCP_FIN ) {
858 conn->tcp_flags |= TCP_ACK;
859 if ( conn->tcp_op->closed )
860 conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
862 if ( tcphdr->flags & TCP_ACK ) {
863 tcp_trans ( conn, TCP_TIME_WAIT );
865 tcp_trans ( conn, TCP_CLOSING );
867 /* Send an acknowledgement */
870 if ( tcphdr->flags & TCP_ACK ) {
871 tcp_trans ( conn, TCP_FIN_WAIT_2 );
873 /* Packet might contain data */
876 if ( tcphdr->flags & TCP_FIN ) {
877 tcp_trans ( conn, TCP_TIME_WAIT );
878 /* FIN consumes one byte */
880 conn->tcp_flags |= TCP_ACK;
883 /* Packet might contain data */
886 if ( tcphdr->flags & TCP_ACK ) {
887 tcp_trans ( conn, TCP_TIME_WAIT );
888 start_timer ( &conn->timer );
892 /* Unexpected packet */
895 /* Unexpected packet */
898 /* Packet could acknowledge data */
901 if ( tcphdr->flags & TCP_ACK ) {
902 tcp_trans ( conn, TCP_CLOSED );
906 /* Unexpected packet */
911 * Any packet reaching this point either contains new data or
912 * acknowledges previously transmitted data.
914 assert ( ( tcphdr->flags & TCP_ACK ) ||
915 pkb_len ( pkb ) > sizeof ( *tcphdr ) );
918 * Check if the received packet ACKs sent data
920 if ( tcphdr->flags & TCP_ACK ) {
921 acked = ntohl ( tcphdr->ack ) - conn->snd_una;
923 /* Packet ACKs previously ACKed data */
924 DBG ( "Previously ACKed data %lx\n",
925 ntohl ( tcphdr->ack ) );
929 /* Invoke the acked() callback */
930 conn->snd_una += acked;
931 if ( conn->tcp_op->acked )
932 conn->tcp_op->acked ( conn, acked );
936 * Check if packet contains new data
938 toack = pkb_len ( pkb ) - hlen;
940 /* Check the sequence number */
941 if ( conn->rcv_nxt == ntohl ( tcphdr->seq ) ) {
942 conn->rcv_nxt += toack;
943 if ( conn->tcp_op->newdata )
944 conn->tcp_op->newdata ( conn, pkb->data + hlen,
947 DBG ( "Unexpected sequence number %lx (wanted %lx)\n",
948 ntohl ( tcphdr->ack ), conn->rcv_nxt );
950 conn->tcp_flags |= TCP_ACK;
956 tcp_senddata ( conn );
960 free_pkb ( conn->tx_pkb );
961 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
962 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
963 if ( ( rc = tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN ) ) != 0 ) {
964 DBG ( "Error sending TCP message (rc = %d)\n", rc );
969 DBG ( "Unexpected packet received in %s with flags = %#hx\n",
970 tcp_states[conn->tcp_state], tcphdr->flags & TCP_MASK_FLAGS );
972 free_pkb ( conn->tx_pkb );
983 struct tcpip_protocol tcp_protocol __tcpip_protocol = {
986 .tcpip_proto = IP_TCP,