You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tcp.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <byteswap.h>
  5. #include <latch.h>
  6. #include <errno.h>
  7. #include <gpxe/process.h>
  8. #include <gpxe/init.h>
  9. #include <gpxe/netdevice.h>
  10. #include <gpxe/pkbuff.h>
  11. #include <gpxe/ip.h>
  12. #include <gpxe/tcp.h>
  13. #include <gpxe/tcpip.h>
  14. #include "uip/uip.h"
  15. /** @file
  16. *
  17. * TCP protocol
  18. *
  19. * The gPXE TCP stack is currently implemented on top of the uIP
  20. * protocol stack. This file provides wrappers around uIP so that
  21. * higher-level protocol implementations do not need to talk directly
  22. * to uIP (which has a somewhat baroque API).
  23. *
  24. * Basic operation is to create a #tcp_connection structure, call
  25. * tcp_connect() and then call run_tcpip() in a loop until the
  26. * operation has completed. The TCP stack will call the various
  27. * methods defined in the #tcp_operations structure in order to send
  28. * and receive data.
  29. *
  30. * See hello.c for a trivial example of a TCP protocol using this
  31. * API.
  32. *
  33. */
  34. #if USE_UIP
  35. /**
  36. * TCP transmit buffer
  37. *
  38. * When a tcp_operations::senddata() method is called, it is
  39. * guaranteed to be able to use this buffer as temporary space for
  40. * constructing the data to be sent. For example, code such as
  41. *
  42. * @code
  43. *
  44. * static void my_senddata ( struct tcp_connection *conn, void *buf,
  45. * size_t len ) {
  46. * len = snprintf ( buf, len, "FETCH %s\r\n", filename );
  47. * tcp_send ( conn, buf + already_sent, len - already_sent );
  48. * }
  49. *
  50. * @endcode
  51. *
  52. * is allowed, and is probably the best way to deal with
  53. * variably-sized data.
  54. *
  55. * Note that you cannot use this simple mechanism if you want to be
  56. * able to construct single data blocks of more than #len bytes.
  57. */
  58. static void *tcp_buffer = uip_buf + ( 40 + UIP_LLH_LEN );
  59. /** Size of #tcp_buffer */
  60. static size_t tcp_buflen = UIP_BUFSIZE - ( 40 + UIP_LLH_LEN );
  61. /**
  62. * Open a TCP connection
  63. *
  64. * @v conn TCP connection
  65. *
  66. * This sets up a new TCP connection to the remote host specified in
  67. * tcp_connection::sin.
  68. */
  69. void tcp_connect ( struct tcp_connection *conn ) {
  70. struct uip_conn *uip_conn;
  71. u16_t ipaddr[2];
  72. assert ( conn->sin.sin_addr.s_addr != 0 );
  73. assert ( conn->sin.sin_port != 0 );
  74. assert ( conn->tcp_op != NULL );
  75. assert ( sizeof ( uip_conn->appstate ) == sizeof ( conn ) );
  76. * ( ( uint32_t * ) ipaddr ) = conn->sin.sin_addr.s_addr;
  77. uip_conn = uip_connect ( ipaddr, conn->sin.sin_port );
  78. #warning "Use linked lists so that uip_connect() cannot fail"
  79. assert ( uip_conn != NULL );
  80. *( ( void ** ) uip_conn->appstate ) = conn;
  81. }
  82. /**
  83. * Send data via a TCP connection
  84. *
  85. * @v conn TCP connection
  86. * @v data Data to send
  87. * @v len Length of data
  88. *
  89. * Data will be automatically limited to the current TCP window size.
  90. *
  91. * If retransmission is required, the connection's
  92. * tcp_operations::senddata() method will be called again in order to
  93. * regenerate the data.
  94. */
  95. void tcp_send ( struct tcp_connection *conn __unused,
  96. const void *data, size_t len ) {
  97. assert ( conn = *( ( void ** ) uip_conn->appstate ) );
  98. if ( len > tcp_buflen )
  99. len = tcp_buflen;
  100. memmove ( tcp_buffer, data, len );
  101. uip_send ( tcp_buffer, len );
  102. }
  103. /**
  104. * Close a TCP connection
  105. *
  106. * @v conn TCP connection
  107. */
  108. void tcp_close ( struct tcp_connection *conn __unused ) {
  109. assert ( conn = *( ( void ** ) uip_conn->appstate ) );
  110. uip_close();
  111. }
  112. /**
  113. * uIP TCP application call interface
  114. *
  115. * This is the entry point of gPXE from the point of view of the uIP
  116. * protocol stack. This function calls the appropriate methods from
  117. * the connection's @tcp_operations table in order to process received
  118. * data, transmit new data etc.
  119. */
  120. void uip_tcp_appcall ( void ) {
  121. struct tcp_connection *conn = *( ( void ** ) uip_conn->appstate );
  122. struct tcp_operations *op = conn->tcp_op;
  123. if ( op->closed ) {
  124. if ( uip_aborted() )
  125. op->closed ( conn, -ECONNABORTED );
  126. if ( uip_timedout() )
  127. op->closed ( conn, -ETIMEDOUT );
  128. if ( uip_closed() )
  129. op->closed ( conn, 0 );
  130. }
  131. if ( uip_connected() && op->connected )
  132. op->connected ( conn );
  133. if ( uip_acked() && op->acked )
  134. op->acked ( conn, uip_conn->len );
  135. if ( uip_newdata() && op->newdata )
  136. op->newdata ( conn, ( void * ) uip_appdata, uip_len );
  137. if ( ( uip_rexmit() || uip_newdata() || uip_acked() ||
  138. uip_connected() || uip_poll() ) && op->senddata )
  139. op->senddata ( conn, tcp_buffer, tcp_buflen );
  140. }
  141. /* Present here to allow everything to link. Will go into separate
  142. * udp.c file
  143. */
  144. void uip_udp_appcall ( void ) {
  145. }
  146. /**
  147. * Perform periodic processing of all TCP connections
  148. *
  149. * This allows TCP connections to retransmit data if necessary.
  150. */
  151. static void tcp_periodic ( void ) {
  152. struct pk_buff *pkb;
  153. int i;
  154. for ( i = 0 ; i < UIP_CONNS ; i++ ) {
  155. uip_periodic ( i );
  156. if ( uip_len > 0 ) {
  157. pkb = alloc_pkb ( uip_len + MAX_LL_HEADER_LEN);
  158. if ( ! pkb )
  159. continue;
  160. pkb_reserve ( pkb, MAX_LL_HEADER_LEN );
  161. pkb_put ( pkb, uip_len );
  162. memcpy ( pkb->data, uip_buf, uip_len );
  163. ipv4_uip_tx ( pkb );
  164. }
  165. }
  166. }
  167. /**
  168. * Kick a connection into life
  169. *
  170. * @v conn TCP connection
  171. *
  172. * Call this function when you have new data to send and are not
  173. * already being called as part of TCP processing.
  174. */
  175. void tcp_kick ( struct tcp_connection *conn __unused ) {
  176. /* Just kick all the connections; this will work for now */
  177. tcp_periodic();
  178. }
  179. /**
  180. * Single-step the TCP stack
  181. *
  182. * @v process TCP process
  183. *
  184. * This calls tcp_periodic() at regular intervals.
  185. */
  186. static void tcp_step ( struct process *process ) {
  187. static unsigned long timeout = 0;
  188. if ( currticks() > timeout ) {
  189. timeout = currticks() + ( TICKS_PER_SEC / 10 );
  190. tcp_periodic ();
  191. }
  192. schedule ( process );
  193. }
  194. /** TCP stack process */
  195. static struct process tcp_process = {
  196. .step = tcp_step,
  197. };
  198. /** Initialise the TCP stack */
  199. static void init_tcp ( void ) {
  200. schedule ( &tcp_process );
  201. }
  202. INIT_FN ( INIT_PROCESS, init_tcp, NULL, NULL );
  203. #else
  204. /**
  205. * List of registered TCP connections
  206. */
  207. static LIST_HEAD ( tcp_conns );
  208. /**
  209. * List of TCP states
  210. */
  211. static const char *tcp_states[] = {
  212. "CLOSED",
  213. "LISTEN",
  214. "SYN_SENT",
  215. "SYN_RCVD",
  216. "ESTABLISHED",
  217. "FIN_WAIT_1",
  218. "FIN_WAIT_2",
  219. "CLOSING",
  220. "TIME_WAIT",
  221. "CLOSE_WAIT",
  222. "LAST_ACK",
  223. "INVALID" };
  224. /**
  225. * TCP state transition function
  226. *
  227. * @v conn TCP connection
  228. * @v nxt_state Next TCP state
  229. */
  230. void tcp_trans ( struct tcp_connection *conn, int nxt_state ) {
  231. /* Remember the last state */
  232. conn->tcp_lstate = conn->tcp_state;
  233. conn->tcp_state = nxt_state;
  234. /* TODO: Check if this check is required */
  235. if ( conn->tcp_lstate == conn->tcp_state ||
  236. conn->tcp_state == TCP_INVALID ) {
  237. conn->tcp_flags = 0;
  238. return;
  239. }
  240. /* Set the TCP flags */
  241. switch ( conn->tcp_state ) {
  242. case TCP_CLOSED:
  243. if ( conn->tcp_lstate == TCP_SYN_RCVD ) {
  244. conn->tcp_flags |= TCP_RST;
  245. }
  246. break;
  247. case TCP_LISTEN:
  248. break;
  249. case TCP_SYN_SENT:
  250. if ( conn->tcp_lstate == TCP_LISTEN ||
  251. conn->tcp_lstate == TCP_CLOSED ) {
  252. conn->tcp_flags |= TCP_SYN;
  253. }
  254. break;
  255. case TCP_SYN_RCVD:
  256. if ( conn->tcp_lstate == TCP_LISTEN ||
  257. conn->tcp_lstate == TCP_SYN_SENT ) {
  258. conn->tcp_flags |= ( TCP_SYN | TCP_ACK );
  259. }
  260. break;
  261. case TCP_ESTABLISHED:
  262. if ( conn->tcp_lstate == TCP_SYN_SENT ) {
  263. conn->tcp_flags |= TCP_ACK;
  264. }
  265. break;
  266. case TCP_FIN_WAIT_1:
  267. if ( conn->tcp_lstate == TCP_SYN_RCVD ||
  268. conn->tcp_lstate == TCP_ESTABLISHED ) {
  269. conn->tcp_flags |= TCP_FIN;
  270. }
  271. break;
  272. case TCP_FIN_WAIT_2:
  273. break;
  274. case TCP_CLOSING:
  275. if ( conn->tcp_lstate == TCP_FIN_WAIT_1 ) {
  276. conn->tcp_flags |= TCP_ACK;
  277. }
  278. break;
  279. case TCP_TIME_WAIT:
  280. if ( conn->tcp_lstate == TCP_FIN_WAIT_1 ||
  281. conn->tcp_lstate == TCP_FIN_WAIT_2 ) {
  282. conn->tcp_flags |= TCP_ACK;
  283. }
  284. break;
  285. case TCP_CLOSE_WAIT:
  286. if ( conn->tcp_lstate == TCP_ESTABLISHED ) {
  287. conn->tcp_flags |= TCP_ACK;
  288. }
  289. break;
  290. case TCP_LAST_ACK:
  291. if ( conn->tcp_lstate == TCP_CLOSE_WAIT ) {
  292. conn->tcp_flags |= TCP_FIN;
  293. }
  294. break;
  295. default:
  296. DBG ( "TCP_INVALID state %d\n", conn->tcp_state );
  297. return;
  298. }
  299. }
  300. /**
  301. * Dump TCP header
  302. *
  303. * @v tcphdr TCP header
  304. */
  305. void tcp_dump ( struct tcp_header *tcphdr ) {
  306. DBG ( "TCP header at %p+%d\n", tcphdr, sizeof ( *tcphdr ) );
  307. DBG ( "\tSource port = %d, Destination port = %d\n",
  308. ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ) );
  309. DBG ( "\tSequence Number = %ld, Acknowledgement Number = %ld\n",
  310. ntohl ( tcphdr->seq ), ntohl ( tcphdr->ack ) );
  311. DBG ( "\tHeader length (/4) = %hd, Flags [..RAPUSF]= %#x\n",
  312. ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ),
  313. ( tcphdr->flags & TCP_MASK_FLAGS ) );
  314. DBG ( "\tAdvertised window = %ld, Checksum = %x, Urgent Pointer = %d\n",
  315. ntohs ( tcphdr->win ), tcphdr->csum, ntohs ( tcphdr->urg ) );
  316. }
  317. /**
  318. * Initialize a TCP connection
  319. *
  320. * @v conn TCP connection
  321. *
  322. * This function assigns initial values to some fields in the connection
  323. * structure. The application should call tcp_init_conn after creating a new
  324. * connection before calling any other "tcp_*" function.
  325. *
  326. * struct tcp_connection my_conn;
  327. * tcp_init_conn ( &my_conn );
  328. * ...
  329. */
  330. void tcp_init_conn ( struct tcp_connection *conn ) {
  331. conn->local_port = 0;
  332. conn->tcp_state = TCP_CLOSED;
  333. conn->tcp_lstate = TCP_INVALID;
  334. conn->tx_pkb = NULL;
  335. conn->tcp_op = NULL;
  336. }
  337. /**
  338. * Connect to a remote server
  339. *
  340. * @v conn TCP connection
  341. * @v peer Remote socket address
  342. *
  343. * This function initiates a TCP connection to the socket address specified in
  344. * peer. It sends a SYN packet to peer. When the connection is established, the
  345. * TCP stack calls the connected() callback function.
  346. */
  347. int tcp_connectto ( struct tcp_connection *conn,
  348. struct sockaddr_tcpip *peer ) {
  349. int rc;
  350. /* A connection can only be established from the CLOSED state */
  351. if ( conn->tcp_state != TCP_CLOSED ) {
  352. DBG ( "Error opening connection: Invalid state %s\n",
  353. tcp_states[conn->tcp_state] );
  354. return -EISCONN;
  355. }
  356. /* Add the connection to the set of listening connections */
  357. if ( ( rc = tcp_listen ( conn, conn->local_port ) ) != 0 ) {
  358. return rc;
  359. }
  360. memcpy ( &conn->peer, peer, sizeof ( conn->peer ) );
  361. /* Send a SYN packet and transition to TCP_SYN_SENT */
  362. conn->snd_una = ( ( ( uint32_t ) random() ) << 16 ) & random();
  363. tcp_trans ( conn, TCP_SYN_SENT );
  364. /* Allocate space for the packet */
  365. free_pkb ( conn->tx_pkb );
  366. conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
  367. pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
  368. conn->rcv_win = MAX_PKB_LEN - MAX_HDR_LEN; /* TODO: Is this OK? */
  369. return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
  370. }
  371. int tcp_connect ( struct tcp_connection *conn ) {
  372. return tcp_connectto ( conn, &conn->peer );
  373. }
  374. /**
  375. * Close the connection
  376. *
  377. * @v conn
  378. *
  379. * This function sends a FIN packet to the remote end of the connection. When
  380. * the remote end of the connection ACKs the FIN (FIN consumes one byte on the
  381. * snd stream), the stack invokes the closed() callback function.
  382. */
  383. int tcp_close ( struct tcp_connection *conn ) {
  384. /* A connection can only be closed if it is a connected state */
  385. switch ( conn->tcp_state ) {
  386. case TCP_SYN_RCVD:
  387. case TCP_ESTABLISHED:
  388. tcp_trans ( conn, TCP_FIN_WAIT_1 );
  389. conn->tcp_op->closed ( conn, CONN_SNDCLOSE ); /* TODO: Check! */
  390. /* FIN consumes one byte on the snd stream */
  391. // conn->snd_una++;
  392. goto send_tcp_nomsg;
  393. case TCP_SYN_SENT:
  394. case TCP_LISTEN:
  395. /**
  396. * Since the connection does not expect any packets from the
  397. * remote end, it can be removed from the set of listening
  398. * connections.
  399. */
  400. list_del ( &conn->list );
  401. tcp_trans ( conn, TCP_CLOSED );
  402. conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
  403. return 0;
  404. case TCP_CLOSE_WAIT:
  405. tcp_trans ( conn, TCP_LAST_ACK );
  406. conn->tcp_op->closed ( conn, CONN_SNDCLOSE ); /* TODO: Check! */
  407. /* FIN consumes one byte on the snd stream */
  408. // conn->snd_una++;
  409. goto send_tcp_nomsg;
  410. default:
  411. DBG ( "tcp_close(): Invalid state %s\n",
  412. tcp_states[conn->tcp_state] );
  413. return -EPROTO;
  414. }
  415. send_tcp_nomsg:
  416. free_pkb ( conn->tx_pkb );
  417. conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
  418. conn->tcp_flags = TCP_FIN;
  419. pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
  420. return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
  421. }
  422. /**
  423. * Listen for a packet
  424. *
  425. * @v conn TCP connection
  426. * @v port Local port, in network byte order
  427. *
  428. * This function adds the connection to a list of registered tcp connections. If
  429. * the local port is 0, the connection is assigned the lowest available port
  430. * between MIN_TCP_PORT and 65535.
  431. */
  432. int tcp_listen ( struct tcp_connection *conn, uint16_t port ) {
  433. struct tcp_connection *cconn;
  434. if ( port != 0 ) {
  435. list_for_each_entry ( cconn, &tcp_conns, list ) {
  436. if ( cconn->local_port == port ) {
  437. DBG ( "Error listening to %d\n",
  438. ntohs ( port ) );
  439. return -EISCONN;
  440. }
  441. }
  442. /* Add the connection to the list of registered connections */
  443. conn->local_port = port;
  444. list_add ( &conn->list, &tcp_conns );
  445. return 0;
  446. }
  447. /* Assigning lowest port not supported */
  448. DBG ( "Assigning lowest port not implemented\n");
  449. return -ENOSYS;
  450. }
  451. /**
  452. * Send data
  453. *
  454. * @v conn TCP connection
  455. *
  456. * This function allocates space to the transmit buffer and invokes the
  457. * senddata() callback function. It passes the allocated buffer to senddata().
  458. * The applicaion may use this space to write it's data.
  459. */
  460. int tcp_senddata ( struct tcp_connection *conn ) {
  461. /* The connection must be in a state in which the user can send data */
  462. switch ( conn->tcp_state ) {
  463. case TCP_LISTEN:
  464. tcp_trans ( conn, TCP_SYN_SENT );
  465. conn->snd_una = ( ( ( uint32_t ) random() ) << 16 ) & random();
  466. break;
  467. case TCP_ESTABLISHED:
  468. case TCP_CLOSE_WAIT:
  469. break;
  470. default:
  471. DBG ( "tcp_senddata: Invalid state %s\n",
  472. tcp_states[conn->tcp_state] );
  473. return -EPROTO;
  474. }
  475. /* Allocate space to the TX buffer */
  476. free_pkb ( conn->tx_pkb );
  477. conn->tx_pkb = alloc_pkb ( MAX_PKB_LEN );
  478. if ( !conn->tx_pkb ) {
  479. DBG ( "Insufficient memory\n" );
  480. return -ENOMEM;
  481. }
  482. pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
  483. /* Set the advertised window */
  484. conn->rcv_win = pkb_available ( conn->tx_pkb );
  485. /* Call the senddata() call back function */
  486. conn->tcp_op->senddata ( conn, conn->tx_pkb->data,
  487. pkb_available ( conn->tx_pkb ) );
  488. return 0;
  489. }
  490. /**
  491. * Transmit data
  492. *
  493. * @v conn TCP connection
  494. * @v data Data to be sent
  495. * @v len Length of the data
  496. *
  497. * This function sends data to the peer socket address
  498. */
  499. int tcp_send ( struct tcp_connection *conn, const void *data, size_t len ) {
  500. struct sockaddr_tcpip *peer = &conn->peer;
  501. struct pk_buff *pkb = conn->tx_pkb;
  502. int slen;
  503. /* Determine the amount of data to be sent */
  504. slen = len < conn->snd_win ? len : conn->snd_win;
  505. /* Copy payload */
  506. memmove ( pkb_put ( pkb, slen ), data, slen );
  507. /* Fill up the TCP header */
  508. struct tcp_header *tcphdr = pkb_push ( pkb, sizeof ( *tcphdr ) );
  509. /* Source port, assumed to be in network byte order in conn */
  510. tcphdr->src = conn->local_port;
  511. /* Destination port, assumed to be in network byte order in peer */
  512. tcphdr->dest = peer->st_port;
  513. tcphdr->seq = htonl ( conn->snd_una );
  514. tcphdr->ack = htonl ( conn->rcv_nxt );
  515. /* Header length, = 0x50 (without TCP options) */
  516. tcphdr->hlen = ( uint8_t ) ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
  517. /* Copy TCP flags, and then reset the variable */
  518. tcphdr->flags = conn->tcp_flags;
  519. conn->tcp_flags = 0;
  520. /* Advertised window, in network byte order */
  521. tcphdr->win = htons ( conn->rcv_win );
  522. /* Set urgent pointer to 0 */
  523. tcphdr->urg = 0;
  524. /* Calculate and store partial checksum, in host byte order */
  525. tcphdr->csum = 0;
  526. tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
  527. /* Dump the TCP header */
  528. tcp_dump ( tcphdr );
  529. /* Transmit packet */
  530. return tcpip_tx ( pkb, &tcp_protocol, peer );
  531. }
  532. /**
  533. * Process received packet
  534. *
  535. * @v pkb Packet buffer
  536. * @v partial Partial checksum
  537. */
  538. static int tcp_rx ( struct pk_buff *pkb,
  539. struct sockaddr_tcpip *st_src __unused,
  540. struct sockaddr_tcpip *st_dest __unused ) {
  541. struct tcp_connection *conn;
  542. struct tcp_header *tcphdr;
  543. uint32_t acked, toack;
  544. int hlen;
  545. /* Sanity check */
  546. if ( pkb_len ( pkb ) < sizeof ( *tcphdr ) ) {
  547. DBG ( "Packet too short (%d bytes)\n", pkb_len ( pkb ) );
  548. return -EINVAL;
  549. }
  550. /* Process TCP header */
  551. tcphdr = pkb->data;
  552. /* Verify header length */
  553. hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
  554. if ( hlen != sizeof ( *tcphdr ) ) {
  555. DBG ( "Bad header length (%d bytes)\n", hlen );
  556. return -EINVAL;
  557. }
  558. /* TODO: Verify checksum */
  559. /* Demux TCP connection */
  560. list_for_each_entry ( conn, &tcp_conns, list ) {
  561. if ( tcphdr->dest == conn->local_port ) {
  562. goto found_conn;
  563. }
  564. }
  565. DBG ( "No connection found on port %d\n", ntohs ( tcphdr->dest ) );
  566. return 0;
  567. found_conn:
  568. /* Set the advertised window */
  569. conn->snd_win = tcphdr->win;
  570. /* TCP State Machine */
  571. uint8_t out_flags = 0;
  572. conn->tcp_lstate = conn->tcp_state;
  573. switch ( conn->tcp_state ) {
  574. case TCP_CLOSED:
  575. DBG ( "tcp_rx(): Invalid state %s\n",
  576. tcp_states[conn->tcp_state] );
  577. return -EINVAL;
  578. case TCP_LISTEN:
  579. if ( tcphdr->flags & TCP_SYN ) {
  580. tcp_trans ( conn, TCP_SYN_RCVD );
  581. /* Synchronize the sequence numbers */
  582. conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
  583. out_flags |= TCP_ACK;
  584. /* Set the sequence number for the snd stream */
  585. conn->snd_una = ( ( ( uint32_t ) random() ) << 16 );
  586. conn->snd_una &= random();
  587. out_flags |= TCP_SYN;
  588. /* Send a SYN,ACK packet */
  589. goto send_tcp_nomsg;
  590. }
  591. /* Unexpected packet */
  592. goto unexpected;
  593. case TCP_SYN_SENT:
  594. if ( tcphdr->flags & TCP_SYN ) {
  595. /* Synchronize the sequence number in rcv stream */
  596. conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
  597. out_flags |= TCP_ACK;
  598. if ( tcphdr->flags & TCP_ACK ) {
  599. tcp_trans ( conn, TCP_ESTABLISHED );
  600. /**
  601. * Process ACK of SYN. This does not invoke the
  602. * acked() callback function.
  603. */
  604. conn->snd_una = ntohl ( tcphdr->ack );
  605. conn->tcp_op->connected ( conn );
  606. } else {
  607. tcp_trans ( conn, TCP_SYN_RCVD );
  608. out_flags |= TCP_SYN;
  609. }
  610. /* Send SYN,ACK or ACK packet */
  611. goto send_tcp_nomsg;
  612. }
  613. /* Unexpected packet */
  614. goto unexpected;
  615. case TCP_SYN_RCVD:
  616. if ( tcphdr->flags & TCP_RST ) {
  617. tcp_trans ( conn, TCP_LISTEN );
  618. conn->tcp_op->closed ( conn, CONN_RESTART );
  619. return 0;
  620. }
  621. if ( tcphdr->flags & TCP_ACK ) {
  622. tcp_trans ( conn, TCP_ESTABLISHED );
  623. /**
  624. * Process ACK of SYN. It neither invokes the callback
  625. * function nor does it send an ACK.
  626. */
  627. conn->snd_una = tcphdr->ack - 1;
  628. conn->tcp_op->connected ( conn );
  629. return 0;
  630. }
  631. /* Unexpected packet */
  632. goto unexpected;
  633. case TCP_ESTABLISHED:
  634. if ( tcphdr->flags & TCP_FIN ) {
  635. tcp_trans ( conn, TCP_CLOSE_WAIT );
  636. /* FIN consumes one byte */
  637. conn->rcv_nxt++;
  638. out_flags |= TCP_ACK;
  639. /* Send an acknowledgement */
  640. goto send_tcp_nomsg;
  641. }
  642. /* Packet might contain data */
  643. break;
  644. case TCP_FIN_WAIT_1:
  645. if ( tcphdr->flags & TCP_FIN ) {
  646. conn->rcv_nxt++;
  647. out_flags |= TCP_ACK;
  648. conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
  649. if ( tcphdr->flags & TCP_ACK ) {
  650. tcp_trans ( conn, TCP_TIME_WAIT );
  651. } else {
  652. tcp_trans ( conn, TCP_CLOSING );
  653. }
  654. /* Send an acknowledgement */
  655. goto send_tcp_nomsg;
  656. }
  657. if ( tcphdr->flags & TCP_ACK ) {
  658. tcp_trans ( conn, TCP_FIN_WAIT_2 );
  659. }
  660. /* Packet might contain data */
  661. break;
  662. case TCP_FIN_WAIT_2:
  663. if ( tcphdr->flags & TCP_FIN ) {
  664. tcp_trans ( conn, TCP_TIME_WAIT );
  665. /* FIN consumes one byte */
  666. conn->rcv_nxt++;
  667. out_flags |= TCP_ACK;
  668. goto send_tcp_nomsg;
  669. }
  670. /* Packet might contain data */
  671. break;
  672. case TCP_CLOSING:
  673. if ( tcphdr->flags & TCP_ACK ) {
  674. tcp_trans ( conn, TCP_TIME_WAIT );
  675. return 0;
  676. }
  677. /* Unexpected packet */
  678. goto unexpected;
  679. case TCP_TIME_WAIT:
  680. /* Unexpected packet */
  681. goto unexpected;
  682. case TCP_CLOSE_WAIT:
  683. /* Packet could acknowledge data */
  684. break;
  685. case TCP_LAST_ACK:
  686. if ( tcphdr->flags & TCP_ACK ) {
  687. tcp_trans ( conn, TCP_CLOSED );
  688. return 0;
  689. }
  690. /* Unexpected packet */
  691. goto unexpected;
  692. }
  693. /**
  694. * Any packet reaching this point either contains new data or
  695. * acknowledges previously transmitted data.
  696. */
  697. assert ( ( tcphdr->flags & TCP_ACK ) ||
  698. pkb_len ( pkb ) > sizeof ( *tcphdr ) );
  699. /* Check for new data */
  700. toack = pkb_len ( pkb ) - hlen;
  701. if ( toack > 0 ) {
  702. /* Check if expected sequence number */
  703. if ( conn->rcv_nxt == ntohl ( tcphdr->seq ) ) {
  704. conn->rcv_nxt += toack;
  705. conn->tcp_op->newdata ( conn, pkb->data + sizeof ( *tcphdr ), toack );
  706. }
  707. /* Acknowledge new data */
  708. out_flags |= TCP_ACK;
  709. if ( !( tcphdr->flags & TCP_ACK ) ) {
  710. goto send_tcp_nomsg;
  711. }
  712. }
  713. /* Process ACK */
  714. if ( tcphdr->flags & TCP_ACK ) {
  715. acked = ntohl ( tcphdr->ack ) - conn->snd_una;
  716. if ( acked < 0 ) { /* TODO: Replace all uint32_t arith */
  717. DBG ( "Previously ACKed (%d)\n", tcphdr->ack );
  718. return 0;
  719. }
  720. /* Advance snd stream */
  721. conn->snd_una += acked;
  722. /* Set the ACK flag */
  723. conn->tcp_flags |= TCP_ACK;
  724. /* Invoke the acked() callback function */
  725. conn->tcp_op->acked ( conn, acked );
  726. /* Invoke the senddata() callback function */
  727. tcp_senddata ( conn );
  728. }
  729. return 0;
  730. send_tcp_nomsg:
  731. free_pkb ( conn->tx_pkb );
  732. conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
  733. pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
  734. int rc;
  735. if ( ( rc = tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN ) ) != 0 ) {
  736. DBG ( "Error sending TCP message (rc = %d)\n", rc );
  737. }
  738. return 0;
  739. unexpected:
  740. DBG ( "Unexpected packet received in %d state with flags = %hd\n",
  741. conn->tcp_state, tcphdr->flags & TCP_MASK_FLAGS );
  742. free_pkb ( conn->tx_pkb );
  743. return -EINVAL;
  744. }
  745. /** TCP protocol */
  746. struct tcpip_protocol tcp_protocol = {
  747. .name = "TCP",
  748. .rx = tcp_rx,
  749. .tcpip_proto = IP_TCP,
  750. .csum_offset = 16,
  751. };
  752. TCPIP_PROTOCOL ( tcp_protocol );
  753. #endif /* USE_UIP */