7 #include <gpxe/process.h>
9 #include <gpxe/netdevice.h>
10 #include <gpxe/pkbuff.h>
13 #include <gpxe/tcpip.h>
20 * The gPXE TCP stack is currently implemented on top of the uIP
21 * protocol stack. This file provides wrappers around uIP so that
22 * higher-level protocol implementations do not need to talk directly
23 * to uIP (which has a somewhat baroque API).
25 * Basic operation is to create a #tcp_connection structure, call
26 * tcp_connect() and then call run_tcpip() in a loop until the
27 * operation has completed. The TCP stack will call the various
28 * methods defined in the #tcp_operations structure in order to send
31 * See hello.c for a trivial example of a TCP protocol using this
41 * When a tcp_operations::senddata() method is called, it is
42 * guaranteed to be able to use this buffer as temporary space for
43 * constructing the data to be sent. For example, code such as
47 * static void my_senddata ( struct tcp_connection *conn, void *buf,
49 * len = snprintf ( buf, len, "FETCH %s\r\n", filename );
50 * tcp_send ( conn, buf + already_sent, len - already_sent );
55 * is allowed, and is probably the best way to deal with
56 * variably-sized data.
58 * Note that you cannot use this simple mechanism if you want to be
59 * able to construct single data blocks of more than #len bytes.
61 static void *tcp_buffer = uip_buf + ( 40 + UIP_LLH_LEN );
63 /** Size of #tcp_buffer */
64 static size_t tcp_buflen = UIP_BUFSIZE - ( 40 + UIP_LLH_LEN );
67 * Open a TCP connection
69 * @v conn TCP connection
71 * This sets up a new TCP connection to the remote host specified in
72 * tcp_connection::sin.
74 void tcp_connect ( struct tcp_connection *conn ) {
75 struct uip_conn *uip_conn;
78 assert ( conn->sin.sin_addr.s_addr != 0 );
79 assert ( conn->sin.sin_port != 0 );
80 assert ( conn->tcp_op != NULL );
81 assert ( sizeof ( uip_conn->appstate ) == sizeof ( conn ) );
83 * ( ( uint32_t * ) ipaddr ) = conn->sin.sin_addr.s_addr;
84 uip_conn = uip_connect ( ipaddr, conn->sin.sin_port );
85 #warning "Use linked lists so that uip_connect() cannot fail"
86 assert ( uip_conn != NULL );
87 *( ( void ** ) uip_conn->appstate ) = conn;
91 * Send data via a TCP connection
93 * @v conn TCP connection
94 * @v data Data to send
95 * @v len Length of data
97 * Data will be automatically limited to the current TCP window size.
99 * If retransmission is required, the connection's
100 * tcp_operations::senddata() method will be called again in order to
101 * regenerate the data.
103 void tcp_send ( struct tcp_connection *conn __unused,
104 const void *data, size_t len ) {
106 assert ( conn = *( ( void ** ) uip_conn->appstate ) );
108 if ( len > tcp_buflen )
110 memmove ( tcp_buffer, data, len );
112 uip_send ( tcp_buffer, len );
116 * Close a TCP connection
118 * @v conn TCP connection
120 void tcp_close ( struct tcp_connection *conn __unused ) {
121 assert ( conn = *( ( void ** ) uip_conn->appstate ) );
126 * uIP TCP application call interface
128 * This is the entry point of gPXE from the point of view of the uIP
129 * protocol stack. This function calls the appropriate methods from
130 * the connection's @tcp_operations table in order to process received
131 * data, transmit new data etc.
133 void uip_tcp_appcall ( void ) {
134 struct tcp_connection *conn = *( ( void ** ) uip_conn->appstate );
135 struct tcp_operations *op = conn->tcp_op;
139 op->closed ( conn, -ECONNABORTED );
140 if ( uip_timedout() )
141 op->closed ( conn, -ETIMEDOUT );
143 op->closed ( conn, 0 );
145 if ( uip_connected() && op->connected )
146 op->connected ( conn );
147 if ( uip_acked() && op->acked )
148 op->acked ( conn, uip_conn->len );
149 if ( uip_newdata() && op->newdata )
150 op->newdata ( conn, ( void * ) uip_appdata, uip_len );
151 if ( ( uip_rexmit() || uip_newdata() || uip_acked() ||
152 uip_connected() || uip_poll() ) && op->senddata )
153 op->senddata ( conn, tcp_buffer, tcp_buflen );
156 /* Present here to allow everything to link. Will go into separate
159 void uip_udp_appcall ( void ) {
163 * Perform periodic processing of all TCP connections
165 * This allows TCP connections to retransmit data if necessary.
167 static void tcp_periodic ( void ) {
171 for ( i = 0 ; i < UIP_CONNS ; i++ ) {
174 pkb = alloc_pkb ( uip_len + MAX_LL_HEADER_LEN);
178 pkb_reserve ( pkb, MAX_LL_HEADER_LEN );
179 pkb_put ( pkb, uip_len );
180 memcpy ( pkb->data, uip_buf, uip_len );
188 * Kick a connection into life
190 * @v conn TCP connection
192 * Call this function when you have new data to send and are not
193 * already being called as part of TCP processing.
195 void tcp_kick ( struct tcp_connection *conn __unused ) {
196 /* Just kick all the connections; this will work for now */
201 * Single-step the TCP stack
203 * @v process TCP process
205 * This calls tcp_periodic() at regular intervals.
207 static void tcp_step ( struct process *process ) {
208 static unsigned long timeout = 0;
210 if ( currticks() > timeout ) {
211 timeout = currticks() + ( TICKS_PER_SEC / 10 );
215 schedule ( process );
218 /** TCP stack process */
219 static struct process tcp_process = {
223 /** Initialise the TCP stack */
224 static void init_tcp ( void ) {
225 schedule ( &tcp_process );
228 INIT_FN ( INIT_PROCESS, init_tcp, NULL, NULL );
233 * List of registered TCP connections
235 static LIST_HEAD ( tcp_conns );
240 static const char *tcp_states[] = {
255 * TCP state transition function
257 * @v conn TCP connection
258 * @v nxt_state Next TCP state
260 void tcp_trans ( struct tcp_connection *conn, int nxt_state ) {
261 /* Remember the last state */
262 conn->tcp_lstate = conn->tcp_state;
263 conn->tcp_state = nxt_state;
265 /* TODO: Check if this check is required */
266 if ( conn->tcp_lstate == conn->tcp_state ||
267 conn->tcp_state == TCP_INVALID ) {
272 /* Set the TCP flags */
273 switch ( conn->tcp_state ) {
275 if ( conn->tcp_lstate == TCP_SYN_RCVD ) {
276 conn->tcp_flags |= TCP_RST;
282 if ( conn->tcp_lstate == TCP_LISTEN ||
283 conn->tcp_lstate == TCP_CLOSED ) {
284 conn->tcp_flags |= TCP_SYN;
288 if ( conn->tcp_lstate == TCP_LISTEN ||
289 conn->tcp_lstate == TCP_SYN_SENT ) {
290 conn->tcp_flags |= ( TCP_SYN | TCP_ACK );
293 case TCP_ESTABLISHED:
294 if ( conn->tcp_lstate == TCP_SYN_SENT ) {
295 conn->tcp_flags |= TCP_ACK;
299 if ( conn->tcp_lstate == TCP_SYN_RCVD ||
300 conn->tcp_lstate == TCP_ESTABLISHED ) {
301 conn->tcp_flags |= TCP_FIN;
307 if ( conn->tcp_lstate == TCP_FIN_WAIT_1 ) {
308 conn->tcp_flags |= TCP_ACK;
312 if ( conn->tcp_lstate == TCP_FIN_WAIT_1 ||
313 conn->tcp_lstate == TCP_FIN_WAIT_2 ) {
314 conn->tcp_flags |= TCP_ACK;
318 if ( conn->tcp_lstate == TCP_ESTABLISHED ) {
319 conn->tcp_flags |= TCP_ACK;
323 if ( conn->tcp_lstate == TCP_CLOSE_WAIT ) {
324 conn->tcp_flags |= TCP_FIN;
328 DBG ( "TCP_INVALID state %d\n", conn->tcp_state );
336 * @v tcphdr TCP header
338 void tcp_dump ( struct tcp_header *tcphdr ) {
339 DBG ( "TCP header at %p+%d\n", tcphdr, sizeof ( *tcphdr ) );
340 DBG ( "\tSource port = %d, Destination port = %d\n",
341 ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ) );
342 DBG ( "\tSequence Number = %ld, Acknowledgement Number = %ld\n",
343 ntohl ( tcphdr->seq ), ntohl ( tcphdr->ack ) );
344 DBG ( "\tHeader length (/4) = %hd, Flags [..RAPUSF]= %#x\n",
345 ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ),
346 ( tcphdr->flags & TCP_MASK_FLAGS ) );
347 DBG ( "\tAdvertised window = %ld, Checksum = %x, Urgent Pointer = %d\n",
348 ntohs ( tcphdr->win ), tcphdr->csum, ntohs ( tcphdr->urg ) );
352 * Initialize a TCP connection
354 * @v conn TCP connection
356 * This function assigns initial values to some fields in the connection
357 * structure. The application should call tcp_init_conn after creating a new
358 * connection before calling any other "tcp_*" function.
360 * struct tcp_connection my_conn;
361 * tcp_init_conn ( &my_conn );
364 void tcp_init_conn ( struct tcp_connection *conn ) {
365 conn->local_port = 0;
366 conn->tcp_state = TCP_CLOSED;
367 conn->tcp_lstate = TCP_INVALID;
373 * Connect to a remote server
375 * @v conn TCP connection
376 * @v peer Remote socket address
378 * This function initiates a TCP connection to the socket address specified in
379 * peer. It sends a SYN packet to peer. When the connection is established, the
380 * TCP stack calls the connected() callback function.
382 int tcp_connectto ( struct tcp_connection *conn,
383 struct sockaddr_tcpip *peer ) {
386 /* A connection can only be established from the CLOSED state */
387 if ( conn->tcp_state != TCP_CLOSED ) {
388 DBG ( "Error opening connection: Invalid state %s\n",
389 tcp_states[conn->tcp_state] );
393 /* Add the connection to the set of listening connections */
394 if ( ( rc = tcp_listen ( conn, conn->local_port ) ) != 0 ) {
397 memcpy ( &conn->peer, peer, sizeof ( conn->peer ) );
399 /* Send a SYN packet and transition to TCP_SYN_SENT */
400 conn->snd_una = ( ( ( uint32_t ) random() ) << 16 ) & random();
401 tcp_trans ( conn, TCP_SYN_SENT );
402 /* Allocate space for the packet */
403 free_pkb ( conn->tx_pkb );
404 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
405 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
406 conn->rcv_win = MAX_PKB_LEN - MAX_HDR_LEN; /* TODO: Is this OK? */
407 return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
410 int tcp_connect ( struct tcp_connection *conn ) {
411 return tcp_connectto ( conn, &conn->peer );
415 * Close the connection
419 * This function sends a FIN packet to the remote end of the connection. When
420 * the remote end of the connection ACKs the FIN (FIN consumes one byte on the
421 * snd stream), the stack invokes the closed() callback function.
423 int tcp_close ( struct tcp_connection *conn ) {
424 /* A connection can only be closed if it is a connected state */
425 switch ( conn->tcp_state ) {
427 case TCP_ESTABLISHED:
428 tcp_trans ( conn, TCP_FIN_WAIT_1 );
429 conn->tcp_op->closed ( conn, CONN_SNDCLOSE ); /* TODO: Check! */
430 /* FIN consumes one byte on the snd stream */
436 * Since the connection does not expect any packets from the
437 * remote end, it can be removed from the set of listening
440 list_del ( &conn->list );
441 tcp_trans ( conn, TCP_CLOSED );
442 conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
445 tcp_trans ( conn, TCP_LAST_ACK );
446 conn->tcp_op->closed ( conn, CONN_SNDCLOSE ); /* TODO: Check! */
447 /* FIN consumes one byte on the snd stream */
451 DBG ( "tcp_close(): Invalid state %s\n",
452 tcp_states[conn->tcp_state] );
457 free_pkb ( conn->tx_pkb );
458 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
459 conn->tcp_flags = TCP_FIN;
460 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
461 return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
466 * Listen for a packet
468 * @v conn TCP connection
469 * @v port Local port, in network byte order
471 * This function adds the connection to a list of registered tcp connections. If
472 * the local port is 0, the connection is assigned the lowest available port
473 * between MIN_TCP_PORT and 65535.
475 int tcp_listen ( struct tcp_connection *conn, uint16_t port ) {
476 struct tcp_connection *cconn;
478 list_for_each_entry ( cconn, &tcp_conns, list ) {
479 if ( cconn->local_port == port ) {
480 DBG ( "Error listening to %d\n",
485 /* Add the connection to the list of registered connections */
486 conn->local_port = port;
487 list_add ( &conn->list, &tcp_conns );
490 /* Assigning lowest port not supported */
491 DBG ( "Assigning lowest port not implemented\n");
498 * @v conn TCP connection
500 * This function allocates space to the transmit buffer and invokes the
501 * senddata() callback function. It passes the allocated buffer to senddata().
502 * The applicaion may use this space to write it's data.
504 int tcp_senddata ( struct tcp_connection *conn ) {
505 /* The connection must be in a state in which the user can send data */
506 switch ( conn->tcp_state ) {
508 tcp_trans ( conn, TCP_SYN_SENT );
509 conn->snd_una = ( ( ( uint32_t ) random() ) << 16 ) & random();
511 case TCP_ESTABLISHED:
515 DBG ( "tcp_senddata: Invalid state %s\n",
516 tcp_states[conn->tcp_state] );
520 /* Allocate space to the TX buffer */
521 free_pkb ( conn->tx_pkb );
522 conn->tx_pkb = alloc_pkb ( MAX_PKB_LEN );
523 if ( !conn->tx_pkb ) {
524 DBG ( "Insufficient memory\n" );
527 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
528 /* Set the advertised window */
529 conn->rcv_win = pkb_available ( conn->tx_pkb );
530 /* Call the senddata() call back function */
531 conn->tcp_op->senddata ( conn, conn->tx_pkb->data,
532 pkb_available ( conn->tx_pkb ) );
539 * @v conn TCP connection
540 * @v data Data to be sent
541 * @v len Length of the data
543 * This function sends data to the peer socket address
545 int tcp_send ( struct tcp_connection *conn, const void *data, size_t len ) {
546 struct sockaddr_tcpip *peer = &conn->peer;
547 struct pk_buff *pkb = conn->tx_pkb;
550 /* Determine the amount of data to be sent */
551 slen = len < conn->snd_win ? len : conn->snd_win;
553 memmove ( pkb_put ( pkb, slen ), data, slen );
555 /* Fill up the TCP header */
556 struct tcp_header *tcphdr = pkb_push ( pkb, sizeof ( *tcphdr ) );
558 /* Source port, assumed to be in network byte order in conn */
559 tcphdr->src = conn->local_port;
560 /* Destination port, assumed to be in network byte order in peer */
561 tcphdr->dest = peer->st_port;
562 tcphdr->seq = htonl ( conn->snd_una );
563 tcphdr->ack = htonl ( conn->rcv_nxt );
564 /* Header length, = 0x50 (without TCP options) */
565 tcphdr->hlen = ( uint8_t ) ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
566 /* Copy TCP flags, and then reset the variable */
567 tcphdr->flags = conn->tcp_flags;
569 /* Advertised window, in network byte order */
570 tcphdr->win = htons ( conn->rcv_win );
571 /* Set urgent pointer to 0 */
573 /* Calculate and store partial checksum, in network byte order */
575 tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
577 /* Dump the TCP header */
580 /* Transmit packet */
581 return tcpip_tx ( pkb, &tcp_protocol, peer );
585 * Process received packet
587 * @v pkb Packet buffer
588 * @v partial Partial checksum
590 static int tcp_rx ( struct pk_buff *pkb,
591 struct sockaddr_tcpip *st_src __unused,
592 struct sockaddr_tcpip *st_dest __unused ) {
593 struct tcp_connection *conn;
594 struct tcp_header *tcphdr;
595 uint32_t acked, toack;
599 if ( pkb_len ( pkb ) < sizeof ( *tcphdr ) ) {
600 DBG ( "Packet too short (%d bytes)\n", pkb_len ( pkb ) );
604 /* Process TCP header */
607 /* Verify header length */
608 hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
609 if ( hlen != sizeof ( *tcphdr ) ) {
610 DBG ( "Bad header length (%d bytes)\n", hlen );
614 /* TODO: Verify checksum */
616 /* Demux TCP connection */
617 list_for_each_entry ( conn, &tcp_conns, list ) {
618 if ( tcphdr->dest == conn->local_port ) {
623 DBG ( "No connection found on port %d\n", ntohs ( tcphdr->dest ) );
627 /* Set the advertised window */
628 conn->snd_win = tcphdr->win;
630 /* TCP State Machine */
631 uint8_t out_flags = 0;
632 conn->tcp_lstate = conn->tcp_state;
633 switch ( conn->tcp_state ) {
635 DBG ( "tcp_rx(): Invalid state %s\n",
636 tcp_states[conn->tcp_state] );
639 if ( tcphdr->flags & TCP_SYN ) {
640 tcp_trans ( conn, TCP_SYN_RCVD );
641 /* Synchronize the sequence numbers */
642 conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
643 out_flags |= TCP_ACK;
645 /* Set the sequence number for the snd stream */
646 conn->snd_una = ( ( ( uint32_t ) random() ) << 16 );
647 conn->snd_una &= random();
648 out_flags |= TCP_SYN;
650 /* Send a SYN,ACK packet */
653 /* Unexpected packet */
656 if ( tcphdr->flags & TCP_SYN ) {
657 /* Synchronize the sequence number in rcv stream */
658 conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
659 out_flags |= TCP_ACK;
661 if ( tcphdr->flags & TCP_ACK ) {
662 tcp_trans ( conn, TCP_ESTABLISHED );
664 * Process ACK of SYN. This does not invoke the
665 * acked() callback function.
667 conn->snd_una = ntohl ( tcphdr->ack );
668 conn->tcp_op->connected ( conn );
670 tcp_trans ( conn, TCP_SYN_RCVD );
671 out_flags |= TCP_SYN;
673 /* Send SYN,ACK or ACK packet */
676 /* Unexpected packet */
679 if ( tcphdr->flags & TCP_RST ) {
680 tcp_trans ( conn, TCP_LISTEN );
681 conn->tcp_op->closed ( conn, CONN_RESTART );
684 if ( tcphdr->flags & TCP_ACK ) {
685 tcp_trans ( conn, TCP_ESTABLISHED );
687 * Process ACK of SYN. It neither invokes the callback
688 * function nor does it send an ACK.
690 conn->snd_una = tcphdr->ack - 1;
691 conn->tcp_op->connected ( conn );
694 /* Unexpected packet */
696 case TCP_ESTABLISHED:
697 if ( tcphdr->flags & TCP_FIN ) {
698 tcp_trans ( conn, TCP_CLOSE_WAIT );
699 /* FIN consumes one byte */
701 out_flags |= TCP_ACK;
702 /* Send an acknowledgement */
705 /* Packet might contain data */
708 if ( tcphdr->flags & TCP_FIN ) {
710 out_flags |= TCP_ACK;
711 conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
713 if ( tcphdr->flags & TCP_ACK ) {
714 tcp_trans ( conn, TCP_TIME_WAIT );
716 tcp_trans ( conn, TCP_CLOSING );
718 /* Send an acknowledgement */
721 if ( tcphdr->flags & TCP_ACK ) {
722 tcp_trans ( conn, TCP_FIN_WAIT_2 );
724 /* Packet might contain data */
727 if ( tcphdr->flags & TCP_FIN ) {
728 tcp_trans ( conn, TCP_TIME_WAIT );
729 /* FIN consumes one byte */
731 out_flags |= TCP_ACK;
734 /* Packet might contain data */
737 if ( tcphdr->flags & TCP_ACK ) {
738 tcp_trans ( conn, TCP_TIME_WAIT );
741 /* Unexpected packet */
744 /* Unexpected packet */
747 /* Packet could acknowledge data */
750 if ( tcphdr->flags & TCP_ACK ) {
751 tcp_trans ( conn, TCP_CLOSED );
754 /* Unexpected packet */
759 * Any packet reaching this point either contains new data or
760 * acknowledges previously transmitted data.
762 assert ( ( tcphdr->flags & TCP_ACK ) ||
763 pkb_len ( pkb ) > sizeof ( *tcphdr ) );
765 /* Check for new data */
766 toack = pkb_len ( pkb ) - hlen;
768 /* Check if expected sequence number */
769 if ( conn->rcv_nxt == ntohl ( tcphdr->seq ) ) {
770 conn->rcv_nxt += toack;
771 conn->tcp_op->newdata ( conn, pkb->data + sizeof ( *tcphdr ), toack );
774 /* Acknowledge new data */
775 out_flags |= TCP_ACK;
776 if ( !( tcphdr->flags & TCP_ACK ) ) {
782 if ( tcphdr->flags & TCP_ACK ) {
783 acked = ntohl ( tcphdr->ack ) - conn->snd_una;
784 if ( acked < 0 ) { /* TODO: Replace all uint32_t arith */
785 DBG ( "Previously ACKed (%d)\n", tcphdr->ack );
788 /* Advance snd stream */
789 conn->snd_una += acked;
790 /* Set the ACK flag */
791 conn->tcp_flags |= TCP_ACK;
792 /* Invoke the acked() callback function */
793 conn->tcp_op->acked ( conn, acked );
794 /* Invoke the senddata() callback function */
795 tcp_senddata ( conn );
800 free_pkb ( conn->tx_pkb );
801 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
802 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
804 if ( ( rc = tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN ) ) != 0 ) {
805 DBG ( "Error sending TCP message (rc = %d)\n", rc );
810 DBG ( "Unexpected packet received in %d state with flags = %hd\n",
811 conn->tcp_state, tcphdr->flags & TCP_MASK_FLAGS );
812 free_pkb ( conn->tx_pkb );
817 struct tcpip_protocol tcp_protocol = {
820 .tcpip_proto = IP_TCP,
824 TCPIP_PROTOCOL ( tcp_protocol );