tcp.c 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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. break;
  287. default:
  288. DBG ( "TCP_INVALID state %d\n", conn->tcp_state );
  289. return;
  290. }
  291. }
  292. void tcp_trans ( struct tcp_connection *conn, int nxt_state ) {
  293. /* Remember the last state */
  294. conn->tcp_lstate = conn->tcp_state;
  295. conn->tcp_state = nxt_state;
  296. printf ( "Transition from %s to %s\n", tcp_states[conn->tcp_lstate], tcp_states[conn->tcp_state] );
  297. /* TODO: Check if this check is required */
  298. if ( conn->tcp_lstate == conn->tcp_state ||
  299. conn->tcp_state == TCP_INVALID ) {
  300. conn->tcp_flags = 0;
  301. return;
  302. }
  303. tcp_set_flags ( conn );
  304. }
  305. /**
  306. * Dump TCP header
  307. *
  308. * @v tcphdr TCP header
  309. */
  310. void tcp_dump ( struct tcp_header *tcphdr ) {
  311. /*
  312. DBG ( "TCP header at %p+%d\n", tcphdr, sizeof ( *tcphdr ) );
  313. DBG ( "\tSource port = %d, Destination port = %d\n",
  314. ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ) );
  315. DBG ( "\tSequence Number = %ld, Acknowledgement Number = %ld\n",
  316. ntohl ( tcphdr->seq ), ntohl ( tcphdr->ack ) );
  317. DBG ( "\tHeader length (/4) = %hd, Flags [..RAPUSF]= %#x\n",
  318. ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ),
  319. ( tcphdr->flags & TCP_MASK_FLAGS ) );
  320. DBG ( "\tAdvertised window = %ld, Checksum = %x, Urgent Pointer = %d\n",
  321. ntohs ( tcphdr->win ), tcphdr->csum, ntohs ( tcphdr->urg ) );
  322. */
  323. DBG ( "TCP %p at %p src:%d dest:%d seq:%lld ack:%lld hlen:%hd flags:%#hx\n",
  324. &tcp_protocol, tcphdr, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ), ntohl ( tcphdr->seq ),
  325. ntohl ( tcphdr->ack ), ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ), ( tcphdr->flags & TCP_MASK_FLAGS ) );
  326. }
  327. /**
  328. * Initialize a TCP connection
  329. *
  330. * @v conn TCP connection
  331. *
  332. * This function assigns initial values to some fields in the connection
  333. * structure. The application should call tcp_init_conn after creating a new
  334. * connection before calling any other "tcp_*" function.
  335. *
  336. * struct tcp_connection my_conn;
  337. * tcp_init_conn ( &my_conn );
  338. * ...
  339. */
  340. void tcp_init_conn ( struct tcp_connection *conn ) {
  341. conn->local_port = 0;
  342. conn->tcp_state = TCP_CLOSED;
  343. conn->tcp_lstate = TCP_INVALID;
  344. conn->tx_pkb = NULL;
  345. conn->tcp_op = NULL;
  346. }
  347. /** Retry timer
  348. *
  349. * @v timer Retry timer
  350. * @v over Failure indicator
  351. */
  352. void tcp_expired ( struct retry_timer *timer, int over ) {
  353. struct tcp_connection *conn;
  354. conn = ( struct tcp_connection * ) container_of ( timer,
  355. struct tcp_connection, timer );
  356. DBG ( "Timer expired in %s\n", tcp_states[conn->tcp_state] );
  357. switch ( conn->tcp_state ) {
  358. case TCP_SYN_SENT:
  359. if ( over ) {
  360. tcp_trans ( conn, TCP_CLOSED );
  361. stop_timer ( &conn->timer );
  362. DBG ( "Timeout! Connection closed\n" );
  363. return;
  364. }
  365. goto send_tcp_nomsg;
  366. case TCP_SYN_RCVD:
  367. if ( over ) {
  368. tcp_trans ( conn, TCP_CLOSED );
  369. stop_timer ( &conn->timer );
  370. goto send_tcp_nomsg;
  371. }
  372. goto send_tcp_nomsg;
  373. case TCP_ESTABLISHED:
  374. if ( conn->tcp_lstate == TCP_SYN_SENT ) {
  375. goto send_tcp_nomsg;
  376. }
  377. break;
  378. case TCP_CLOSE_WAIT:
  379. if ( conn->tcp_lstate == TCP_ESTABLISHED ) {
  380. goto send_tcp_nomsg;
  381. }
  382. break;
  383. case TCP_FIN_WAIT_1:
  384. case TCP_FIN_WAIT_2:
  385. goto send_tcp_nomsg;
  386. case TCP_CLOSING:
  387. case TCP_LAST_ACK:
  388. if ( conn->tcp_lstate == TCP_CLOSE_WAIT ) {
  389. goto send_tcp_nomsg;
  390. }
  391. return;
  392. case TCP_TIME_WAIT:
  393. tcp_trans ( conn, TCP_CLOSED );
  394. stop_timer ( &conn->timer );
  395. return;
  396. }
  397. /* Retransmit the data */
  398. tcp_set_flags ( conn );
  399. tcp_senddata ( conn );
  400. return;
  401. send_tcp_nomsg:
  402. // free_pkb ( conn->tx_pkb );
  403. conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
  404. pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
  405. tcp_set_flags ( conn );
  406. int rc;
  407. if ( ( rc = tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN ) ) != 0 ) {
  408. DBG ( "Error sending TCP message (rc = %d)\n", rc );
  409. }
  410. return;
  411. }
  412. /**
  413. * Connect to a remote server
  414. *
  415. * @v conn TCP connection
  416. * @v peer Remote socket address
  417. *
  418. * This function initiates a TCP connection to the socket address specified in
  419. * peer. It sends a SYN packet to peer. When the connection is established, the
  420. * TCP stack calls the connected() callback function.
  421. */
  422. int tcp_connectto ( struct tcp_connection *conn,
  423. struct sockaddr_tcpip *peer ) {
  424. int rc;
  425. /* A connection can only be established from the CLOSED state */
  426. if ( conn->tcp_state != TCP_CLOSED ) {
  427. DBG ( "Error opening connection: Invalid state %s\n",
  428. tcp_states[conn->tcp_state] );
  429. return -EISCONN;
  430. }
  431. /* Add the connection to the set of listening connections */
  432. if ( ( rc = tcp_listen ( conn, conn->local_port ) ) != 0 ) {
  433. return rc;
  434. }
  435. memcpy ( &conn->peer, peer, sizeof ( conn->peer ) );
  436. /* Initialize the TCP timer */
  437. conn->timer.expired = tcp_expired;
  438. /* Send a SYN packet and transition to TCP_SYN_SENT */
  439. conn->snd_una = ( ( ( uint32_t ) random() ) << 16 ) & random();
  440. tcp_trans ( conn, TCP_SYN_SENT );
  441. /* Allocate space for the packet */
  442. free_pkb ( conn->tx_pkb );
  443. conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
  444. pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
  445. conn->rcv_win = MAX_PKB_LEN - MAX_HDR_LEN; /* TODO: Is this OK? */
  446. return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
  447. }
  448. int tcp_connect ( struct tcp_connection *conn ) {
  449. return tcp_connectto ( conn, &conn->peer );
  450. }
  451. /**
  452. * Close the connection
  453. *
  454. * @v conn
  455. *
  456. * This function sends a FIN packet to the remote end of the connection. When
  457. * the remote end of the connection ACKs the FIN (FIN consumes one byte on the
  458. * snd stream), the stack invokes the closed() callback function.
  459. */
  460. int tcp_close ( struct tcp_connection *conn ) {
  461. /* A connection can only be closed if it is a connected state */
  462. switch ( conn->tcp_state ) {
  463. case TCP_SYN_RCVD:
  464. case TCP_ESTABLISHED:
  465. tcp_trans ( conn, TCP_FIN_WAIT_1 );
  466. conn->tcp_op->closed ( conn, CONN_SNDCLOSE ); /* TODO: Check! */
  467. /* FIN consumes one byte on the snd stream */
  468. // conn->snd_una++;
  469. goto send_tcp_nomsg;
  470. case TCP_SYN_SENT:
  471. case TCP_LISTEN:
  472. /**
  473. * Since the connection does not expect any packets from the
  474. * remote end, it can be removed from the set of listening
  475. * connections.
  476. */
  477. list_del ( &conn->list );
  478. tcp_trans ( conn, TCP_CLOSED );
  479. conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
  480. return 0;
  481. case TCP_CLOSE_WAIT:
  482. tcp_trans ( conn, TCP_LAST_ACK );
  483. conn->tcp_op->closed ( conn, CONN_SNDCLOSE ); /* TODO: Check! */
  484. /* FIN consumes one byte on the snd stream */
  485. // conn->snd_una++;
  486. goto send_tcp_nomsg;
  487. default:
  488. DBG ( "tcp_close(): Invalid state %s\n",
  489. tcp_states[conn->tcp_state] );
  490. return -EPROTO;
  491. }
  492. send_tcp_nomsg:
  493. free_pkb ( conn->tx_pkb );
  494. conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
  495. conn->tcp_flags = TCP_FIN;
  496. pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
  497. return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
  498. }
  499. /**
  500. * Listen for a packet
  501. *
  502. * @v conn TCP connection
  503. * @v port Local port, in network byte order
  504. *
  505. * This function adds the connection to a list of registered tcp connections. If
  506. * the local port is 0, the connection is assigned the lowest available port
  507. * between MIN_TCP_PORT and 65535.
  508. */
  509. int tcp_listen ( struct tcp_connection *conn, uint16_t port ) {
  510. struct tcp_connection *cconn;
  511. if ( port != 0 ) {
  512. list_for_each_entry ( cconn, &tcp_conns, list ) {
  513. if ( cconn->local_port == port ) {
  514. DBG ( "Error listening to %d\n",
  515. ntohs ( port ) );
  516. return -EISCONN;
  517. }
  518. }
  519. /* Add the connection to the list of registered connections */
  520. conn->local_port = port;
  521. list_add ( &conn->list, &tcp_conns );
  522. return 0;
  523. }
  524. /* Assigning lowest port not supported */
  525. DBG ( "Assigning lowest port not implemented\n");
  526. return -ENOSYS;
  527. }
  528. /**
  529. * Send data
  530. *
  531. * @v conn TCP connection
  532. *
  533. * This function allocates space to the transmit buffer and invokes the
  534. * senddata() callback function. It passes the allocated buffer to senddata().
  535. * The applicaion may use this space to write it's data.
  536. */
  537. int tcp_senddata ( struct tcp_connection *conn ) {
  538. /* The connection must be in a state in which the user can send data */
  539. switch ( conn->tcp_state ) {
  540. case TCP_LISTEN:
  541. tcp_trans ( conn, TCP_SYN_SENT );
  542. conn->snd_una = ( ( ( uint32_t ) random() ) << 16 ) & random();
  543. break;
  544. case TCP_ESTABLISHED:
  545. case TCP_CLOSE_WAIT:
  546. break;
  547. default:
  548. DBG ( "tcp_senddata: Invalid state %s\n",
  549. tcp_states[conn->tcp_state] );
  550. return -EPROTO;
  551. }
  552. /* Allocate space to the TX buffer */
  553. free_pkb ( conn->tx_pkb );
  554. conn->tx_pkb = alloc_pkb ( MAX_PKB_LEN );
  555. if ( !conn->tx_pkb ) {
  556. DBG ( "Insufficient memory\n" );
  557. return -ENOMEM;
  558. }
  559. pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
  560. /* Set the advertised window */
  561. conn->rcv_win = pkb_available ( conn->tx_pkb );
  562. /* Call the senddata() call back function */
  563. conn->tcp_op->senddata ( conn, conn->tx_pkb->data,
  564. pkb_available ( conn->tx_pkb ) );
  565. return 0;
  566. }
  567. /**
  568. * Transmit data
  569. *
  570. * @v conn TCP connection
  571. * @v data Data to be sent
  572. * @v len Length of the data
  573. *
  574. * This function sends data to the peer socket address
  575. */
  576. int tcp_send ( struct tcp_connection *conn, const void *data, size_t len ) {
  577. struct sockaddr_tcpip *peer = &conn->peer;
  578. struct pk_buff *pkb = conn->tx_pkb;
  579. int slen;
  580. /* Determine the amount of data to be sent */
  581. slen = len < conn->snd_win ? len : conn->snd_win;
  582. /* Copy payload */
  583. memmove ( pkb_put ( pkb, slen ), data, slen );
  584. /* Fill up the TCP header */
  585. struct tcp_header *tcphdr = pkb_push ( pkb, sizeof ( *tcphdr ) );
  586. /* Source port, assumed to be in network byte order in conn */
  587. tcphdr->src = conn->local_port;
  588. /* Destination port, assumed to be in network byte order in peer */
  589. tcphdr->dest = peer->st_port;
  590. tcphdr->seq = htonl ( conn->snd_una );
  591. tcphdr->ack = htonl ( conn->rcv_nxt );
  592. /* Header length, = 0x50 (without TCP options) */
  593. tcphdr->hlen = ( uint8_t ) ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
  594. /* Copy TCP flags, and then reset the variable */
  595. tcphdr->flags = conn->tcp_flags;
  596. conn->tcp_flags = 0;
  597. /* Advertised window, in network byte order */
  598. tcphdr->win = htons ( conn->rcv_win );
  599. /* Set urgent pointer to 0 */
  600. tcphdr->urg = 0;
  601. /* Calculate and store partial checksum, in host byte order */
  602. tcphdr->csum = 0;
  603. tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
  604. /* Dump the TCP header */
  605. tcp_dump ( tcphdr );
  606. /* Start the timer */
  607. if ( ( conn->tcp_state == TCP_ESTABLISHED && conn->tcp_lstate == TCP_SYN_SENT ) ||
  608. ( conn->tcp_state == TCP_LISTEN && conn->tcp_lstate == TCP_SYN_RCVD ) ||
  609. ( conn->tcp_state == TCP_CLOSED && conn->tcp_lstate == TCP_SYN_RCVD ) ) {
  610. // Don't start the timer
  611. } else {
  612. start_timer ( &conn->timer );
  613. }
  614. /* Transmit packet */
  615. return tcpip_tx ( pkb, &tcp_protocol, peer );
  616. }
  617. /**
  618. * Process received packet
  619. *
  620. * @v pkb Packet buffer
  621. * @v partial Partial checksum
  622. */
  623. static int tcp_rx ( struct pk_buff *pkb,
  624. struct sockaddr_tcpip *st_src __unused,
  625. struct sockaddr_tcpip *st_dest __unused ) {
  626. struct tcp_connection *conn;
  627. struct tcp_header *tcphdr;
  628. uint32_t acked, toack;
  629. int hlen;
  630. int add = 0;
  631. /* Sanity check */
  632. if ( pkb_len ( pkb ) < sizeof ( *tcphdr ) ) {
  633. DBG ( "Packet too short (%d bytes)\n", pkb_len ( pkb ) );
  634. return -EINVAL;
  635. }
  636. /* Process TCP header */
  637. tcphdr = pkb->data;
  638. tcp_dump ( tcphdr );
  639. /* Verify header length */
  640. hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
  641. if ( hlen != sizeof ( *tcphdr ) ) {
  642. if ( hlen == sizeof ( *tcphdr ) + 4 ) {
  643. DBG ( "TCP options sent\n" );
  644. add = 4;
  645. } else {
  646. DBG ( "Bad header length (%d bytes)\n", hlen );
  647. return -EINVAL;
  648. }
  649. }
  650. /* TODO: Verify checksum */
  651. /* Demux TCP connection */
  652. list_for_each_entry ( conn, &tcp_conns, list ) {
  653. if ( tcphdr->dest == conn->local_port ) {
  654. goto found_conn;
  655. }
  656. }
  657. DBG ( "No connection found on port %d\n", ntohs ( tcphdr->dest ) );
  658. return 0;
  659. found_conn:
  660. /* Stop the timer */
  661. stop_timer ( &conn->timer );
  662. /* Set the advertised window */
  663. conn->snd_win = tcphdr->win;
  664. /* TCP State Machine */
  665. uint8_t out_flags = 0;
  666. conn->tcp_lstate = conn->tcp_state;
  667. switch ( conn->tcp_state ) {
  668. case TCP_CLOSED:
  669. DBG ( "tcp_rx(): Invalid state %s\n",
  670. tcp_states[conn->tcp_state] );
  671. return -EINVAL;
  672. case TCP_LISTEN:
  673. if ( tcphdr->flags & TCP_SYN ) {
  674. tcp_trans ( conn, TCP_SYN_RCVD );
  675. /* Synchronize the sequence numbers */
  676. conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
  677. out_flags |= TCP_ACK;
  678. /* Set the sequence number for the snd stream */
  679. conn->snd_una = ( ( ( uint32_t ) random() ) << 16 );
  680. conn->snd_una &= random();
  681. out_flags |= TCP_SYN;
  682. /* Send a SYN,ACK packet */
  683. goto send_tcp_nomsg;
  684. }
  685. /* Unexpected packet */
  686. goto unexpected;
  687. case TCP_SYN_SENT:
  688. if ( tcphdr->flags & TCP_SYN ) {
  689. /* Synchronize the sequence number in rcv stream */
  690. conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
  691. out_flags |= TCP_ACK;
  692. if ( tcphdr->flags & TCP_ACK ) {
  693. tcp_trans ( conn, TCP_ESTABLISHED );
  694. /**
  695. * Process ACK of SYN. This does not invoke the
  696. * acked() callback function.
  697. */
  698. conn->snd_una = ntohl ( tcphdr->ack );
  699. conn->tcp_op->connected ( conn );
  700. tcp_senddata ( conn );
  701. } else {
  702. tcp_trans ( conn, TCP_SYN_RCVD );
  703. out_flags |= TCP_SYN;
  704. }
  705. /* Send SYN,ACK or ACK packet */
  706. goto send_tcp_nomsg;
  707. }
  708. /* Unexpected packet */
  709. goto unexpected;
  710. case TCP_SYN_RCVD:
  711. if ( tcphdr->flags & TCP_RST ) {
  712. tcp_trans ( conn, TCP_LISTEN );
  713. conn->tcp_op->closed ( conn, CONN_RESTART );
  714. return 0;
  715. }
  716. if ( tcphdr->flags & TCP_ACK ) {
  717. tcp_trans ( conn, TCP_ESTABLISHED );
  718. /**
  719. * Process ACK of SYN. It neither invokes the callback
  720. * function nor does it send an ACK.
  721. */
  722. conn->snd_una = tcphdr->ack - 1;
  723. conn->tcp_op->connected ( conn );
  724. tcp_senddata ( conn );
  725. return 0;
  726. }
  727. /* Unexpected packet */
  728. goto unexpected;
  729. case TCP_ESTABLISHED:
  730. if ( tcphdr->flags & TCP_FIN ) {
  731. tcp_trans ( conn, TCP_CLOSE_WAIT );
  732. /* FIN consumes one byte */
  733. conn->rcv_nxt++;
  734. out_flags |= TCP_ACK;
  735. /* Send an acknowledgement */
  736. goto send_tcp_nomsg;
  737. }
  738. /* Packet might contain data */
  739. break;
  740. case TCP_FIN_WAIT_1:
  741. if ( tcphdr->flags & TCP_FIN ) {
  742. conn->rcv_nxt++;
  743. out_flags |= TCP_ACK;
  744. conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
  745. if ( tcphdr->flags & TCP_ACK ) {
  746. tcp_trans ( conn, TCP_TIME_WAIT );
  747. } else {
  748. tcp_trans ( conn, TCP_CLOSING );
  749. }
  750. /* Send an acknowledgement */
  751. goto send_tcp_nomsg;
  752. }
  753. if ( tcphdr->flags & TCP_ACK ) {
  754. tcp_trans ( conn, TCP_FIN_WAIT_2 );
  755. }
  756. /* Packet might contain data */
  757. break;
  758. case TCP_FIN_WAIT_2:
  759. if ( tcphdr->flags & TCP_FIN ) {
  760. tcp_trans ( conn, TCP_TIME_WAIT );
  761. /* FIN consumes one byte */
  762. conn->rcv_nxt++;
  763. out_flags |= TCP_ACK;
  764. goto send_tcp_nomsg;
  765. }
  766. /* Packet might contain data */
  767. break;
  768. case TCP_CLOSING:
  769. if ( tcphdr->flags & TCP_ACK ) {
  770. tcp_trans ( conn, TCP_TIME_WAIT );
  771. start_timer ( &conn->timer );
  772. return 0;
  773. }
  774. /* Unexpected packet */
  775. goto unexpected;
  776. case TCP_TIME_WAIT:
  777. /* Unexpected packet */
  778. goto unexpected;
  779. case TCP_CLOSE_WAIT:
  780. /* Packet could acknowledge data */
  781. break;
  782. case TCP_LAST_ACK:
  783. if ( tcphdr->flags & TCP_ACK ) {
  784. tcp_trans ( conn, TCP_CLOSED );
  785. return 0;
  786. }
  787. /* Unexpected packet */
  788. goto unexpected;
  789. }
  790. /**
  791. * Any packet reaching this point either contains new data or
  792. * acknowledges previously transmitted data.
  793. */
  794. assert ( ( tcphdr->flags & TCP_ACK ) ||
  795. pkb_len ( pkb ) > sizeof ( *tcphdr ) );
  796. /* Check for new data */
  797. toack = pkb_len ( pkb ) - hlen;
  798. if ( toack > 0 ) {
  799. /* Check if expected sequence number */
  800. if ( conn->rcv_nxt == ntohl ( tcphdr->seq ) ) {
  801. conn->rcv_nxt += toack;
  802. conn->tcp_op->newdata ( conn, pkb->data + sizeof ( *tcphdr ) + add, toack );
  803. }
  804. /* Acknowledge new data */
  805. out_flags |= TCP_ACK;
  806. if ( !( tcphdr->flags & TCP_ACK ) ) {
  807. goto send_tcp_nomsg;
  808. }
  809. }
  810. /* Process ACK */
  811. if ( tcphdr->flags & TCP_ACK ) {
  812. acked = ntohl ( tcphdr->ack ) - conn->snd_una;
  813. if ( acked < 0 ) { /* TODO: Replace all uint32_t arith */
  814. DBG ( "Previously ACKed (%d)\n", tcphdr->ack );
  815. return 0;
  816. }
  817. /* Advance snd stream */
  818. conn->snd_una += acked;
  819. /* Set the ACK flag */
  820. conn->tcp_flags |= TCP_ACK;
  821. /* Invoke the acked() callback function */
  822. conn->tcp_op->acked ( conn, acked );
  823. /* Invoke the senddata() callback function */
  824. tcp_senddata ( conn );
  825. }
  826. return 0;
  827. send_tcp_nomsg:
  828. free_pkb ( conn->tx_pkb );
  829. conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
  830. pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
  831. int rc;
  832. if ( ( rc = tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN ) ) != 0 ) {
  833. DBG ( "Error sending TCP message (rc = %d)\n", rc );
  834. }
  835. return 0;
  836. unexpected:
  837. DBG ( "Unexpected packet received in %s with flags = %#hx\n",
  838. tcp_states[conn->tcp_state], tcphdr->flags & TCP_MASK_FLAGS );
  839. tcp_close ( conn );
  840. free_pkb ( conn->tx_pkb );
  841. return -EINVAL;
  842. }
  843. /** TCP protocol */
  844. struct tcpip_protocol tcp_protocol = {
  845. .name = "TCP",
  846. .rx = tcp_rx,
  847. .tcpip_proto = IP_TCP,
  848. .csum_offset = 16,
  849. };
  850. TCPIP_PROTOCOL ( tcp_protocol );
  851. #endif /* USE_UIP */