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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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. if ( conn->tcp_op->closed )
  455. conn->tcp_op->closed ( conn, CONN_SNDCLOSE ); /* TODO: Check! */
  456. /* FIN consumes one byte on the snd stream */
  457. // conn->snd_una++;
  458. goto send_tcp_nomsg;
  459. case TCP_SYN_SENT:
  460. case TCP_LISTEN:
  461. /**
  462. * Since the connection does not expect any packets from the
  463. * remote end, it can be removed from the set of listening
  464. * connections.
  465. */
  466. list_del ( &conn->list );
  467. tcp_trans ( conn, TCP_CLOSED );
  468. if ( conn->tcp_op->closed )
  469. conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
  470. return 0;
  471. case TCP_CLOSE_WAIT:
  472. tcp_trans ( conn, TCP_LAST_ACK );
  473. if ( conn->tcp_op->closed )
  474. conn->tcp_op->closed ( conn, CONN_SNDCLOSE ); /* TODO: Check! */
  475. /* FIN consumes one byte on the snd stream */
  476. // conn->snd_una++;
  477. goto send_tcp_nomsg;
  478. default:
  479. DBG ( "tcp_close(): Invalid state %s\n",
  480. tcp_states[conn->tcp_state] );
  481. return -EPROTO;
  482. }
  483. send_tcp_nomsg:
  484. free_pkb ( conn->tx_pkb );
  485. conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
  486. conn->tcp_flags = TCP_FIN;
  487. pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
  488. return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
  489. }
  490. /**
  491. * Bind TCP connection to local port
  492. *
  493. * @v conn TCP connection
  494. * @v local_port Local port, in network byte order
  495. * @ret rc Return status code
  496. */
  497. int tcp_bind ( struct tcp_connection *conn, uint16_t local_port ) {
  498. struct tcp_connection *existing;
  499. list_for_each_entry ( existing, &tcp_conns, list ) {
  500. if ( existing->local_port == local_port )
  501. return -EADDRINUSE;
  502. }
  503. conn->local_port = local_port;
  504. return 0;
  505. }
  506. /**
  507. * Listen for a packet
  508. *
  509. * @v conn TCP connection
  510. * @v local_port Local port, in network byte order
  511. *
  512. * This function adds the connection to a list of registered tcp
  513. * connections. If the local port is 0, the connection is assigned an
  514. * available port between MIN_TCP_PORT and 65535.
  515. */
  516. int tcp_listen ( struct tcp_connection *conn, uint16_t local_port ) {
  517. static uint16_t try_port = 1024;
  518. int rc;
  519. #warning "Fix the port re-use bug"
  520. /* If we re-use the same port, the connection should be reset
  521. * and a new connection set up. This doesn't happen yet, so
  522. * randomise the port to avoid hitting the problem.
  523. */
  524. try_port = random();
  525. /* If no port specified, find the first available port */
  526. if ( ! local_port ) {
  527. for ( ; try_port ; try_port++ ) {
  528. if ( try_port < 1024 )
  529. continue;
  530. if ( tcp_listen ( conn, htons ( try_port ) ) == 0 )
  531. return 0;
  532. }
  533. return -EADDRINUSE;
  534. }
  535. /* Attempt bind to local port */
  536. if ( ( rc = tcp_bind ( conn, local_port ) ) != 0 )
  537. return rc;
  538. /* Add to TCP connection list */
  539. list_add ( &conn->list, &tcp_conns );
  540. DBG ( "TCP opened %p on port %d\n", conn, ntohs ( local_port ) );
  541. return 0;
  542. }
  543. /**
  544. * Send data
  545. *
  546. * @v conn TCP connection
  547. *
  548. * This function allocates space to the transmit buffer and invokes the
  549. * senddata() callback function. It passes the allocated buffer to senddata().
  550. * The applicaion may use this space to write it's data.
  551. */
  552. int tcp_senddata ( struct tcp_connection *conn ) {
  553. /* The connection must be in a state in which the user can send data */
  554. switch ( conn->tcp_state ) {
  555. case TCP_LISTEN:
  556. tcp_trans ( conn, TCP_SYN_SENT );
  557. conn->snd_una = random();
  558. break;
  559. case TCP_ESTABLISHED:
  560. case TCP_CLOSE_WAIT:
  561. break;
  562. default:
  563. DBG ( "tcp_senddata: Invalid state %s\n",
  564. tcp_states[conn->tcp_state] );
  565. return -EPROTO;
  566. }
  567. /* Allocate space to the TX buffer */
  568. free_pkb ( conn->tx_pkb );
  569. conn->tx_pkb = alloc_pkb ( MAX_PKB_LEN );
  570. if ( !conn->tx_pkb ) {
  571. DBG ( "Insufficient memory\n" );
  572. return -ENOMEM;
  573. }
  574. pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
  575. /* Set the advertised window */
  576. conn->rcv_win = pkb_available ( conn->tx_pkb );
  577. /* Call the senddata() call back function */
  578. if ( conn->tcp_op->senddata )
  579. conn->tcp_op->senddata ( conn, conn->tx_pkb->data,
  580. pkb_available ( conn->tx_pkb ) );
  581. /* Send pure ACK if senddata() didn't call tcp_send() */
  582. if ( conn->tx_pkb ) {
  583. tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
  584. }
  585. return 0;
  586. }
  587. /**
  588. * Transmit data
  589. *
  590. * @v conn TCP connection
  591. * @v data Data to be sent
  592. * @v len Length of the data
  593. *
  594. * This function sends data to the peer socket address
  595. */
  596. int tcp_send ( struct tcp_connection *conn, const void *data, size_t len ) {
  597. struct sockaddr_tcpip *peer = &conn->peer;
  598. struct pk_buff *pkb;
  599. int slen;
  600. /* Take ownership of the TX buffer from the connection */
  601. pkb = conn->tx_pkb;
  602. conn->tx_pkb = NULL;
  603. /* Determine the amount of data to be sent */
  604. slen = len < conn->snd_win ? len : conn->snd_win;
  605. /* Copy payload */
  606. memmove ( pkb_put ( pkb, slen ), data, slen );
  607. /* Fill up the TCP header */
  608. struct tcp_header *tcphdr = pkb_push ( pkb, sizeof ( *tcphdr ) );
  609. /* Source port, assumed to be in network byte order in conn */
  610. tcphdr->src = conn->local_port;
  611. /* Destination port, assumed to be in network byte order in peer */
  612. tcphdr->dest = peer->st_port;
  613. tcphdr->seq = htonl ( conn->snd_una );
  614. tcphdr->ack = htonl ( conn->rcv_nxt );
  615. /* Header length, = 0x50 (without TCP options) */
  616. tcphdr->hlen = ( uint8_t ) ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
  617. /* Copy TCP flags, and then reset the variable */
  618. tcphdr->flags = conn->tcp_flags;
  619. conn->tcp_flags = 0;
  620. /* Advertised window, in network byte order */
  621. tcphdr->win = htons ( conn->rcv_win );
  622. /* Set urgent pointer to 0 */
  623. tcphdr->urg = 0;
  624. /* Calculate and store partial checksum, in host byte order */
  625. tcphdr->csum = 0;
  626. tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
  627. /* Dump the TCP header */
  628. tcp_dump ( tcphdr );
  629. /* Start the timer */
  630. if ( ( conn->tcp_state == TCP_ESTABLISHED && conn->tcp_lstate == TCP_SYN_SENT ) ||
  631. ( conn->tcp_state == TCP_LISTEN && conn->tcp_lstate == TCP_SYN_RCVD ) ||
  632. ( conn->tcp_state == TCP_CLOSED && conn->tcp_lstate == TCP_SYN_RCVD ) ||
  633. ( conn->tcp_state == TCP_ESTABLISHED && ( len == 0 ) ) ) {
  634. // Don't start the timer
  635. } else {
  636. start_timer ( &conn->timer );
  637. }
  638. /* Transmit packet */
  639. return tcpip_tx ( pkb, &tcp_protocol, peer );
  640. }
  641. /**
  642. * Process received packet
  643. *
  644. * @v pkb Packet buffer
  645. * @v partial Partial checksum
  646. */
  647. static int tcp_rx ( struct pk_buff *pkb,
  648. struct sockaddr_tcpip *st_src __unused,
  649. struct sockaddr_tcpip *st_dest __unused ) {
  650. struct tcp_connection *conn;
  651. struct tcp_header *tcphdr;
  652. uint32_t acked, toack;
  653. int hlen;
  654. int rc;
  655. /* Sanity check */
  656. if ( pkb_len ( pkb ) < sizeof ( *tcphdr ) ) {
  657. DBG ( "Packet too short (%d bytes)\n", pkb_len ( pkb ) );
  658. rc = -EINVAL;
  659. goto done;
  660. }
  661. /* Process TCP header */
  662. tcphdr = pkb->data;
  663. tcp_dump ( tcphdr );
  664. /* Verify header length */
  665. hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
  666. if ( hlen < sizeof ( *tcphdr ) ) {
  667. DBG ( "Bad header length (%d bytes)\n", hlen );
  668. rc = -EINVAL;
  669. goto done;
  670. }
  671. /* TODO: Parse TCP options */
  672. if ( hlen != sizeof ( *tcphdr ) ) {
  673. DBG ( "Ignoring TCP options\n" );
  674. }
  675. /* TODO: Verify checksum */
  676. /* Demux TCP connection */
  677. list_for_each_entry ( conn, &tcp_conns, list ) {
  678. if ( tcphdr->dest == conn->local_port ) {
  679. goto found_conn;
  680. }
  681. }
  682. DBG ( "No connection found on port %d\n", ntohs ( tcphdr->dest ) );
  683. rc = 0;
  684. goto done;
  685. found_conn:
  686. /* Stop the timer */
  687. stop_timer ( &conn->timer );
  688. /* Set the advertised window */
  689. conn->snd_win = tcphdr->win;
  690. /* TCP State Machine */
  691. conn->tcp_lstate = conn->tcp_state;
  692. switch ( conn->tcp_state ) {
  693. case TCP_CLOSED:
  694. DBG ( "tcp_rx(): Invalid state %s\n",
  695. tcp_states[conn->tcp_state] );
  696. rc = -EINVAL;
  697. goto done;
  698. case TCP_LISTEN:
  699. if ( tcphdr->flags & TCP_SYN ) {
  700. tcp_trans ( conn, TCP_SYN_RCVD );
  701. /* Synchronize the sequence numbers */
  702. conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
  703. conn->tcp_flags |= TCP_ACK;
  704. /* Set the sequence number for the snd stream */
  705. conn->snd_una = random();
  706. conn->tcp_flags |= TCP_SYN;
  707. /* Send a SYN,ACK packet */
  708. goto send_tcp_nomsg;
  709. }
  710. /* Unexpected packet */
  711. goto unexpected;
  712. case TCP_SYN_SENT:
  713. if ( tcphdr->flags & TCP_SYN ) {
  714. /* Synchronize the sequence number in rcv stream */
  715. conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
  716. conn->tcp_flags |= TCP_ACK;
  717. if ( tcphdr->flags & TCP_ACK ) {
  718. tcp_trans ( conn, TCP_ESTABLISHED );
  719. /**
  720. * Process ACK of SYN. This does not invoke the
  721. * acked() callback function.
  722. */
  723. conn->snd_una = ntohl ( tcphdr->ack );
  724. if ( conn->tcp_op->connected )
  725. conn->tcp_op->connected ( conn );
  726. conn->tcp_flags |= TCP_ACK;
  727. tcp_senddata ( conn );
  728. rc = 0;
  729. goto done;
  730. } else {
  731. tcp_trans ( conn, TCP_SYN_RCVD );
  732. conn->tcp_flags |= TCP_SYN;
  733. goto send_tcp_nomsg;
  734. }
  735. }
  736. /* Unexpected packet */
  737. goto unexpected;
  738. case TCP_SYN_RCVD:
  739. if ( tcphdr->flags & TCP_RST ) {
  740. tcp_trans ( conn, TCP_LISTEN );
  741. if ( conn->tcp_op->closed )
  742. conn->tcp_op->closed ( conn, CONN_RESTART );
  743. rc = 0;
  744. goto done;
  745. }
  746. if ( tcphdr->flags & TCP_ACK ) {
  747. tcp_trans ( conn, TCP_ESTABLISHED );
  748. /**
  749. * Process ACK of SYN. It neither invokes the callback
  750. * function nor does it send an ACK.
  751. */
  752. conn->snd_una = tcphdr->ack - 1;
  753. if ( conn->tcp_op->connected )
  754. conn->tcp_op->connected ( conn );
  755. rc = 0;
  756. goto done;
  757. }
  758. /* Unexpected packet */
  759. goto unexpected;
  760. case TCP_ESTABLISHED:
  761. if ( tcphdr->flags & TCP_FIN ) {
  762. if ( tcphdr->flags & TCP_ACK ) {
  763. tcp_trans ( conn, TCP_LAST_ACK );
  764. conn->tcp_flags |= TCP_FIN;
  765. } else {
  766. tcp_trans ( conn, TCP_CLOSE_WAIT );
  767. }
  768. /* FIN consumes one byte */
  769. conn->rcv_nxt++;
  770. conn->tcp_flags |= TCP_ACK;
  771. /* Send the packet */
  772. goto send_tcp_nomsg;
  773. }
  774. /* Packet might contain data */
  775. break;
  776. case TCP_FIN_WAIT_1:
  777. if ( tcphdr->flags & TCP_FIN ) {
  778. conn->rcv_nxt++;
  779. conn->tcp_flags |= TCP_ACK;
  780. if ( conn->tcp_op->closed )
  781. conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
  782. if ( tcphdr->flags & TCP_ACK ) {
  783. tcp_trans ( conn, TCP_TIME_WAIT );
  784. } else {
  785. tcp_trans ( conn, TCP_CLOSING );
  786. }
  787. /* Send an acknowledgement */
  788. goto send_tcp_nomsg;
  789. }
  790. if ( tcphdr->flags & TCP_ACK ) {
  791. tcp_trans ( conn, TCP_FIN_WAIT_2 );
  792. }
  793. /* Packet might contain data */
  794. break;
  795. case TCP_FIN_WAIT_2:
  796. if ( tcphdr->flags & TCP_FIN ) {
  797. tcp_trans ( conn, TCP_TIME_WAIT );
  798. /* FIN consumes one byte */
  799. conn->rcv_nxt++;
  800. conn->tcp_flags |= TCP_ACK;
  801. goto send_tcp_nomsg;
  802. }
  803. /* Packet might contain data */
  804. break;
  805. case TCP_CLOSING:
  806. if ( tcphdr->flags & TCP_ACK ) {
  807. tcp_trans ( conn, TCP_TIME_WAIT );
  808. start_timer ( &conn->timer );
  809. rc = 0;
  810. goto done;
  811. }
  812. /* Unexpected packet */
  813. goto unexpected;
  814. case TCP_TIME_WAIT:
  815. /* Unexpected packet */
  816. goto unexpected;
  817. case TCP_CLOSE_WAIT:
  818. /* Packet could acknowledge data */
  819. break;
  820. case TCP_LAST_ACK:
  821. if ( tcphdr->flags & TCP_ACK ) {
  822. tcp_trans ( conn, TCP_CLOSED );
  823. rc = 0;
  824. goto done;
  825. }
  826. /* Unexpected packet */
  827. goto unexpected;
  828. }
  829. /**
  830. * Any packet reaching this point either contains new data or
  831. * acknowledges previously transmitted data.
  832. */
  833. assert ( ( tcphdr->flags & TCP_ACK ) ||
  834. pkb_len ( pkb ) > sizeof ( *tcphdr ) );
  835. /**
  836. * Check if the received packet ACKs sent data
  837. */
  838. if ( tcphdr->flags & TCP_ACK ) {
  839. acked = ntohl ( tcphdr->ack ) - conn->snd_una;
  840. if ( acked < 0 ) {
  841. /* Packet ACKs previously ACKed data */
  842. DBG ( "Previously ACKed data %lx\n",
  843. ntohl ( tcphdr->ack ) );
  844. rc = 0;
  845. goto done;
  846. }
  847. /* Invoke the acked() callback */
  848. conn->snd_una += acked;
  849. if ( conn->tcp_op->acked )
  850. conn->tcp_op->acked ( conn, acked );
  851. }
  852. /**
  853. * Check if packet contains new data
  854. */
  855. toack = pkb_len ( pkb ) - hlen;
  856. if ( toack >= 0 ) {
  857. /* Check the sequence number */
  858. if ( conn->rcv_nxt == ntohl ( tcphdr->seq ) ) {
  859. conn->rcv_nxt += toack;
  860. if ( conn->tcp_op->newdata )
  861. conn->tcp_op->newdata ( conn, pkb->data + hlen,
  862. toack );
  863. } else {
  864. DBG ( "Unexpected sequence number %lx (wanted %lx)\n",
  865. ntohl ( tcphdr->ack ), conn->rcv_nxt );
  866. }
  867. conn->tcp_flags |= TCP_ACK;
  868. }
  869. /**
  870. * Send data
  871. */
  872. tcp_senddata ( conn );
  873. return 0;
  874. send_tcp_nomsg:
  875. free_pkb ( conn->tx_pkb );
  876. conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
  877. pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
  878. if ( ( rc = tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN ) ) != 0 ) {
  879. DBG ( "Error sending TCP message (rc = %d)\n", rc );
  880. }
  881. goto done;
  882. unexpected:
  883. DBG ( "Unexpected packet received in %s with flags = %#hx\n",
  884. tcp_states[conn->tcp_state], tcphdr->flags & TCP_MASK_FLAGS );
  885. tcp_close ( conn );
  886. free_pkb ( conn->tx_pkb );
  887. conn->tx_pkb = NULL;
  888. rc = -EINVAL;
  889. goto done;
  890. done:
  891. free_pkb ( pkb );
  892. return rc;
  893. }
  894. /** TCP protocol */
  895. struct tcpip_protocol tcp_protocol __tcpip_protocol = {
  896. .name = "TCP",
  897. .rx = tcp_rx,
  898. .tcpip_proto = IP_TCP,
  899. .csum_offset = 16,
  900. };
  901. #endif /* USE_UIP */