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 24KB

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