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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <assert.h>
  5. #include <errno.h>
  6. #include <byteswap.h>
  7. #include <ipxe/timer.h>
  8. #include <ipxe/iobuf.h>
  9. #include <ipxe/malloc.h>
  10. #include <ipxe/retry.h>
  11. #include <ipxe/refcnt.h>
  12. #include <ipxe/xfer.h>
  13. #include <ipxe/open.h>
  14. #include <ipxe/uri.h>
  15. #include <ipxe/netdevice.h>
  16. #include <ipxe/tcpip.h>
  17. #include <ipxe/tcp.h>
  18. /** @file
  19. *
  20. * TCP protocol
  21. *
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER );
  24. /** A TCP connection */
  25. struct tcp_connection {
  26. /** Reference counter */
  27. struct refcnt refcnt;
  28. /** List of TCP connections */
  29. struct list_head list;
  30. /** Flags */
  31. unsigned int flags;
  32. /** Data transfer interface */
  33. struct interface xfer;
  34. /** Remote socket address */
  35. struct sockaddr_tcpip peer;
  36. /** Local port */
  37. unsigned int local_port;
  38. /** Current TCP state */
  39. unsigned int tcp_state;
  40. /** Previous TCP state
  41. *
  42. * Maintained only for debug messages
  43. */
  44. unsigned int prev_tcp_state;
  45. /** Current sequence number
  46. *
  47. * Equivalent to SND.UNA in RFC 793 terminology.
  48. */
  49. uint32_t snd_seq;
  50. /** Unacknowledged sequence count
  51. *
  52. * Equivalent to (SND.NXT-SND.UNA) in RFC 793 terminology.
  53. */
  54. uint32_t snd_sent;
  55. /** Send window
  56. *
  57. * Equivalent to SND.WND in RFC 793 terminology
  58. */
  59. uint32_t snd_win;
  60. /** Current acknowledgement number
  61. *
  62. * Equivalent to RCV.NXT in RFC 793 terminology.
  63. */
  64. uint32_t rcv_ack;
  65. /** Receive window
  66. *
  67. * Equivalent to RCV.WND in RFC 793 terminology.
  68. */
  69. uint32_t rcv_win;
  70. /** Received timestamp value
  71. *
  72. * Updated when a packet is received; copied to ts_recent when
  73. * the window is advanced.
  74. */
  75. uint32_t ts_val;
  76. /** Most recent received timestamp that advanced the window
  77. *
  78. * Equivalent to TS.Recent in RFC 1323 terminology.
  79. */
  80. uint32_t ts_recent;
  81. /** Transmit queue */
  82. struct list_head tx_queue;
  83. /** Receive queue */
  84. struct list_head rx_queue;
  85. /** Retransmission timer */
  86. struct retry_timer timer;
  87. /** Shutdown (TIME_WAIT) timer */
  88. struct retry_timer wait;
  89. };
  90. /** TCP flags */
  91. enum tcp_flags {
  92. /** TCP data transfer interface has been closed */
  93. TCP_XFER_CLOSED = 0x0001,
  94. /** TCP timestamps are enabled */
  95. TCP_TS_ENABLED = 0x0002,
  96. /** TCP acknowledgement is pending */
  97. TCP_ACK_PENDING = 0x0004,
  98. };
  99. /** TCP internal header
  100. *
  101. * This is the header that replaces the TCP header for packets
  102. * enqueued on the receive queue.
  103. */
  104. struct tcp_rx_queued_header {
  105. /** SEQ value, in host-endian order
  106. *
  107. * This represents the SEQ value at the time the packet is
  108. * enqueued, and so excludes the SYN, if present.
  109. */
  110. uint32_t seq;
  111. /** Flags
  112. *
  113. * Only FIN is valid within this flags byte; all other flags
  114. * have already been processed by the time the packet is
  115. * enqueued.
  116. */
  117. uint8_t flags;
  118. /** Reserved */
  119. uint8_t reserved[3];
  120. };
  121. /**
  122. * List of registered TCP connections
  123. */
  124. static LIST_HEAD ( tcp_conns );
  125. /* Forward declarations */
  126. static struct interface_descriptor tcp_xfer_desc;
  127. static void tcp_expired ( struct retry_timer *timer, int over );
  128. static void tcp_wait_expired ( struct retry_timer *timer, int over );
  129. static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
  130. uint32_t win );
  131. /**
  132. * Name TCP state
  133. *
  134. * @v state TCP state
  135. * @ret name Name of TCP state
  136. */
  137. static inline __attribute__ (( always_inline )) const char *
  138. tcp_state ( int state ) {
  139. switch ( state ) {
  140. case TCP_CLOSED: return "CLOSED";
  141. case TCP_LISTEN: return "LISTEN";
  142. case TCP_SYN_SENT: return "SYN_SENT";
  143. case TCP_SYN_RCVD: return "SYN_RCVD";
  144. case TCP_ESTABLISHED: return "ESTABLISHED";
  145. case TCP_FIN_WAIT_1: return "FIN_WAIT_1";
  146. case TCP_FIN_WAIT_2: return "FIN_WAIT_2";
  147. case TCP_CLOSING_OR_LAST_ACK: return "CLOSING/LAST_ACK";
  148. case TCP_TIME_WAIT: return "TIME_WAIT";
  149. case TCP_CLOSE_WAIT: return "CLOSE_WAIT";
  150. default: return "INVALID";
  151. }
  152. }
  153. /**
  154. * Dump TCP state transition
  155. *
  156. * @v tcp TCP connection
  157. */
  158. static inline __attribute__ (( always_inline )) void
  159. tcp_dump_state ( struct tcp_connection *tcp ) {
  160. if ( tcp->tcp_state != tcp->prev_tcp_state ) {
  161. DBGC ( tcp, "TCP %p transitioned from %s to %s\n", tcp,
  162. tcp_state ( tcp->prev_tcp_state ),
  163. tcp_state ( tcp->tcp_state ) );
  164. }
  165. tcp->prev_tcp_state = tcp->tcp_state;
  166. }
  167. /**
  168. * Dump TCP flags
  169. *
  170. * @v flags TCP flags
  171. */
  172. static inline __attribute__ (( always_inline )) void
  173. tcp_dump_flags ( struct tcp_connection *tcp, unsigned int flags ) {
  174. if ( flags & TCP_RST )
  175. DBGC2 ( tcp, " RST" );
  176. if ( flags & TCP_SYN )
  177. DBGC2 ( tcp, " SYN" );
  178. if ( flags & TCP_PSH )
  179. DBGC2 ( tcp, " PSH" );
  180. if ( flags & TCP_FIN )
  181. DBGC2 ( tcp, " FIN" );
  182. if ( flags & TCP_ACK )
  183. DBGC2 ( tcp, " ACK" );
  184. }
  185. /***************************************************************************
  186. *
  187. * Open and close
  188. *
  189. ***************************************************************************
  190. */
  191. /**
  192. * Bind TCP connection to local port
  193. *
  194. * @v tcp TCP connection
  195. * @v port Local port number
  196. * @ret rc Return status code
  197. *
  198. * If the port is 0, the connection is assigned an available port
  199. * between 1024 and 65535.
  200. */
  201. static int tcp_bind ( struct tcp_connection *tcp, unsigned int port ) {
  202. struct tcp_connection *existing;
  203. uint16_t try_port;
  204. unsigned int i;
  205. /* If no port is specified, find an available port */
  206. if ( ! port ) {
  207. try_port = random();
  208. for ( i = 0 ; i < 65536 ; i++ ) {
  209. try_port++;
  210. if ( try_port < 1024 )
  211. continue;
  212. if ( tcp_bind ( tcp, try_port ) == 0 )
  213. return 0;
  214. }
  215. DBGC ( tcp, "TCP %p could not bind: no free ports\n", tcp );
  216. return -EADDRINUSE;
  217. }
  218. /* Attempt bind to local port */
  219. list_for_each_entry ( existing, &tcp_conns, list ) {
  220. if ( existing->local_port == port ) {
  221. DBGC ( tcp, "TCP %p could not bind: port %d in use\n",
  222. tcp, port );
  223. return -EADDRINUSE;
  224. }
  225. }
  226. tcp->local_port = port;
  227. DBGC ( tcp, "TCP %p bound to port %d\n", tcp, port );
  228. return 0;
  229. }
  230. /**
  231. * Open a TCP connection
  232. *
  233. * @v xfer Data transfer interface
  234. * @v peer Peer socket address
  235. * @v local Local socket address, or NULL
  236. * @ret rc Return status code
  237. */
  238. static int tcp_open ( struct interface *xfer, struct sockaddr *peer,
  239. struct sockaddr *local ) {
  240. struct sockaddr_tcpip *st_peer = ( struct sockaddr_tcpip * ) peer;
  241. struct sockaddr_tcpip *st_local = ( struct sockaddr_tcpip * ) local;
  242. struct tcp_connection *tcp;
  243. unsigned int bind_port;
  244. int rc;
  245. /* Allocate and initialise structure */
  246. tcp = zalloc ( sizeof ( *tcp ) );
  247. if ( ! tcp )
  248. return -ENOMEM;
  249. DBGC ( tcp, "TCP %p allocated\n", tcp );
  250. ref_init ( &tcp->refcnt, NULL );
  251. intf_init ( &tcp->xfer, &tcp_xfer_desc, &tcp->refcnt );
  252. timer_init ( &tcp->timer, tcp_expired, &tcp->refcnt );
  253. timer_init ( &tcp->wait, tcp_wait_expired, &tcp->refcnt );
  254. tcp->prev_tcp_state = TCP_CLOSED;
  255. tcp->tcp_state = TCP_STATE_SENT ( TCP_SYN );
  256. tcp_dump_state ( tcp );
  257. tcp->snd_seq = random();
  258. INIT_LIST_HEAD ( &tcp->tx_queue );
  259. INIT_LIST_HEAD ( &tcp->rx_queue );
  260. memcpy ( &tcp->peer, st_peer, sizeof ( tcp->peer ) );
  261. /* Bind to local port */
  262. bind_port = ( st_local ? ntohs ( st_local->st_port ) : 0 );
  263. if ( ( rc = tcp_bind ( tcp, bind_port ) ) != 0 )
  264. goto err;
  265. /* Start timer to initiate SYN */
  266. start_timer_nodelay ( &tcp->timer );
  267. /* Attach parent interface, transfer reference to connection
  268. * list and return
  269. */
  270. intf_plug_plug ( &tcp->xfer, xfer );
  271. list_add ( &tcp->list, &tcp_conns );
  272. return 0;
  273. err:
  274. ref_put ( &tcp->refcnt );
  275. return rc;
  276. }
  277. /**
  278. * Close TCP connection
  279. *
  280. * @v tcp TCP connection
  281. * @v rc Reason for close
  282. *
  283. * Closes the data transfer interface. If the TCP state machine is in
  284. * a suitable state, the connection will be deleted.
  285. */
  286. static void tcp_close ( struct tcp_connection *tcp, int rc ) {
  287. struct io_buffer *iobuf;
  288. struct io_buffer *tmp;
  289. /* Close data transfer interface */
  290. intf_shutdown ( &tcp->xfer, rc );
  291. tcp->flags |= TCP_XFER_CLOSED;
  292. /* If we are in CLOSED, or have otherwise not yet received a
  293. * SYN (i.e. we are in LISTEN or SYN_SENT), just delete the
  294. * connection.
  295. */
  296. if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
  297. /* Transition to CLOSED for the sake of debugging messages */
  298. tcp->tcp_state = TCP_CLOSED;
  299. tcp_dump_state ( tcp );
  300. /* Free any unprocessed I/O buffers */
  301. list_for_each_entry_safe ( iobuf, tmp, &tcp->rx_queue, list ) {
  302. list_del ( &iobuf->list );
  303. free_iob ( iobuf );
  304. }
  305. /* Free any unsent I/O buffers */
  306. list_for_each_entry_safe ( iobuf, tmp, &tcp->tx_queue, list ) {
  307. list_del ( &iobuf->list );
  308. free_iob ( iobuf );
  309. }
  310. /* Remove from list and drop reference */
  311. stop_timer ( &tcp->timer );
  312. list_del ( &tcp->list );
  313. ref_put ( &tcp->refcnt );
  314. DBGC ( tcp, "TCP %p connection deleted\n", tcp );
  315. return;
  316. }
  317. /* If we have not had our SYN acknowledged (i.e. we are in
  318. * SYN_RCVD), pretend that it has been acknowledged so that we
  319. * can send a FIN without breaking things.
  320. */
  321. if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
  322. tcp_rx_ack ( tcp, ( tcp->snd_seq + 1 ), 0 );
  323. /* If we have no data remaining to send, start sending FIN */
  324. if ( list_empty ( &tcp->tx_queue ) ) {
  325. tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
  326. tcp_dump_state ( tcp );
  327. }
  328. }
  329. /***************************************************************************
  330. *
  331. * Transmit data path
  332. *
  333. ***************************************************************************
  334. */
  335. /**
  336. * Calculate transmission window
  337. *
  338. * @v tcp TCP connection
  339. * @ret len Maximum length that can be sent in a single packet
  340. */
  341. static size_t tcp_xmit_win ( struct tcp_connection *tcp ) {
  342. size_t len;
  343. /* Not ready if we're not in a suitable connection state */
  344. if ( ! TCP_CAN_SEND_DATA ( tcp->tcp_state ) )
  345. return 0;
  346. /* Length is the minimum of the receiver's window and the path MTU */
  347. len = tcp->snd_win;
  348. if ( len > TCP_PATH_MTU )
  349. len = TCP_PATH_MTU;
  350. return len;
  351. }
  352. /**
  353. * Check data-transfer flow control window
  354. *
  355. * @v tcp TCP connection
  356. * @ret len Length of window
  357. */
  358. static size_t tcp_xfer_window ( struct tcp_connection *tcp ) {
  359. /* Not ready if data queue is non-empty. This imposes a limit
  360. * of only one unACKed packet in the TX queue at any time; we
  361. * do this to conserve memory usage.
  362. */
  363. if ( ! list_empty ( &tcp->tx_queue ) )
  364. return 0;
  365. /* Return TCP window length */
  366. return tcp_xmit_win ( tcp );
  367. }
  368. /**
  369. * Process TCP transmit queue
  370. *
  371. * @v tcp TCP connection
  372. * @v max_len Maximum length to process
  373. * @v dest I/O buffer to fill with data, or NULL
  374. * @v remove Remove data from queue
  375. * @ret len Length of data processed
  376. *
  377. * This processes at most @c max_len bytes from the TCP connection's
  378. * transmit queue. Data will be copied into the @c dest I/O buffer
  379. * (if provided) and, if @c remove is true, removed from the transmit
  380. * queue.
  381. */
  382. static size_t tcp_process_tx_queue ( struct tcp_connection *tcp, size_t max_len,
  383. struct io_buffer *dest, int remove ) {
  384. struct io_buffer *iobuf;
  385. struct io_buffer *tmp;
  386. size_t frag_len;
  387. size_t len = 0;
  388. list_for_each_entry_safe ( iobuf, tmp, &tcp->tx_queue, list ) {
  389. frag_len = iob_len ( iobuf );
  390. if ( frag_len > max_len )
  391. frag_len = max_len;
  392. if ( dest ) {
  393. memcpy ( iob_put ( dest, frag_len ), iobuf->data,
  394. frag_len );
  395. }
  396. if ( remove ) {
  397. iob_pull ( iobuf, frag_len );
  398. if ( ! iob_len ( iobuf ) ) {
  399. list_del ( &iobuf->list );
  400. free_iob ( iobuf );
  401. }
  402. }
  403. len += frag_len;
  404. max_len -= frag_len;
  405. }
  406. return len;
  407. }
  408. /**
  409. * Transmit any outstanding data
  410. *
  411. * @v tcp TCP connection
  412. *
  413. * Transmits any outstanding data on the connection.
  414. *
  415. * Note that even if an error is returned, the retransmission timer
  416. * will have been started if necessary, and so the stack will
  417. * eventually attempt to retransmit the failed packet.
  418. */
  419. static int tcp_xmit ( struct tcp_connection *tcp ) {
  420. struct io_buffer *iobuf;
  421. struct tcp_header *tcphdr;
  422. struct tcp_mss_option *mssopt;
  423. struct tcp_timestamp_padded_option *tsopt;
  424. void *payload;
  425. unsigned int flags;
  426. size_t len = 0;
  427. uint32_t seq_len;
  428. uint32_t app_win;
  429. uint32_t max_rcv_win;
  430. int rc;
  431. /* If retransmission timer is already running, do nothing */
  432. if ( timer_running ( &tcp->timer ) )
  433. return 0;
  434. /* Calculate both the actual (payload) and sequence space
  435. * lengths that we wish to transmit.
  436. */
  437. if ( TCP_CAN_SEND_DATA ( tcp->tcp_state ) ) {
  438. len = tcp_process_tx_queue ( tcp, tcp_xmit_win ( tcp ),
  439. NULL, 0 );
  440. }
  441. seq_len = len;
  442. flags = TCP_FLAGS_SENDING ( tcp->tcp_state );
  443. if ( flags & ( TCP_SYN | TCP_FIN ) ) {
  444. /* SYN or FIN consume one byte, and we can never send both */
  445. assert ( ! ( ( flags & TCP_SYN ) && ( flags & TCP_FIN ) ) );
  446. seq_len++;
  447. }
  448. tcp->snd_sent = seq_len;
  449. /* If we have nothing to transmit, stop now */
  450. if ( ( seq_len == 0 ) && ! ( tcp->flags & TCP_ACK_PENDING ) )
  451. return 0;
  452. /* If we are transmitting anything that requires
  453. * acknowledgement (i.e. consumes sequence space), start the
  454. * retransmission timer. Do this before attempting to
  455. * allocate the I/O buffer, in case allocation itself fails.
  456. */
  457. if ( seq_len )
  458. start_timer ( &tcp->timer );
  459. /* Allocate I/O buffer */
  460. iobuf = alloc_iob ( len + TCP_MAX_HEADER_LEN );
  461. if ( ! iobuf ) {
  462. DBGC ( tcp, "TCP %p could not allocate iobuf for %08x..%08x "
  463. "%08x\n", tcp, tcp->snd_seq, ( tcp->snd_seq + seq_len ),
  464. tcp->rcv_ack );
  465. return -ENOMEM;
  466. }
  467. iob_reserve ( iobuf, TCP_MAX_HEADER_LEN );
  468. /* Fill data payload from transmit queue */
  469. tcp_process_tx_queue ( tcp, len, iobuf, 0 );
  470. /* Expand receive window if possible */
  471. max_rcv_win = ( ( freemem * 3 ) / 4 );
  472. if ( max_rcv_win > TCP_MAX_WINDOW_SIZE )
  473. max_rcv_win = TCP_MAX_WINDOW_SIZE;
  474. app_win = xfer_window ( &tcp->xfer );
  475. if ( max_rcv_win > app_win )
  476. max_rcv_win = app_win;
  477. max_rcv_win &= ~0x03; /* Keep everything dword-aligned */
  478. if ( tcp->rcv_win < max_rcv_win )
  479. tcp->rcv_win = max_rcv_win;
  480. /* Fill up the TCP header */
  481. payload = iobuf->data;
  482. if ( flags & TCP_SYN ) {
  483. mssopt = iob_push ( iobuf, sizeof ( *mssopt ) );
  484. mssopt->kind = TCP_OPTION_MSS;
  485. mssopt->length = sizeof ( *mssopt );
  486. mssopt->mss = htons ( TCP_MSS );
  487. }
  488. if ( ( flags & TCP_SYN ) || ( tcp->flags & TCP_TS_ENABLED ) ) {
  489. tsopt = iob_push ( iobuf, sizeof ( *tsopt ) );
  490. memset ( tsopt->nop, TCP_OPTION_NOP, sizeof ( tsopt->nop ) );
  491. tsopt->tsopt.kind = TCP_OPTION_TS;
  492. tsopt->tsopt.length = sizeof ( tsopt->tsopt );
  493. tsopt->tsopt.tsval = htonl ( currticks() );
  494. tsopt->tsopt.tsecr = htonl ( tcp->ts_recent );
  495. }
  496. if ( len != 0 )
  497. flags |= TCP_PSH;
  498. tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
  499. memset ( tcphdr, 0, sizeof ( *tcphdr ) );
  500. tcphdr->src = htons ( tcp->local_port );
  501. tcphdr->dest = tcp->peer.st_port;
  502. tcphdr->seq = htonl ( tcp->snd_seq );
  503. tcphdr->ack = htonl ( tcp->rcv_ack );
  504. tcphdr->hlen = ( ( payload - iobuf->data ) << 2 );
  505. tcphdr->flags = flags;
  506. tcphdr->win = htons ( tcp->rcv_win );
  507. tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
  508. /* Dump header */
  509. DBGC2 ( tcp, "TCP %p TX %d->%d %08x..%08x %08x %4zd",
  510. tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
  511. ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) + seq_len ),
  512. ntohl ( tcphdr->ack ), len );
  513. tcp_dump_flags ( tcp, tcphdr->flags );
  514. DBGC2 ( tcp, "\n" );
  515. /* Transmit packet */
  516. if ( ( rc = tcpip_tx ( iobuf, &tcp_protocol, NULL, &tcp->peer, NULL,
  517. &tcphdr->csum ) ) != 0 ) {
  518. DBGC ( tcp, "TCP %p could not transmit %08x..%08x %08x: %s\n",
  519. tcp, tcp->snd_seq, ( tcp->snd_seq + tcp->snd_sent ),
  520. tcp->rcv_ack, strerror ( rc ) );
  521. return rc;
  522. }
  523. /* Clear ACK-pending flag */
  524. tcp->flags &= ~TCP_ACK_PENDING;
  525. return 0;
  526. }
  527. /**
  528. * Retransmission timer expired
  529. *
  530. * @v timer Retransmission timer
  531. * @v over Failure indicator
  532. */
  533. static void tcp_expired ( struct retry_timer *timer, int over ) {
  534. struct tcp_connection *tcp =
  535. container_of ( timer, struct tcp_connection, timer );
  536. DBGC ( tcp, "TCP %p timer %s in %s for %08x..%08x %08x\n", tcp,
  537. ( over ? "expired" : "fired" ), tcp_state ( tcp->tcp_state ),
  538. tcp->snd_seq, ( tcp->snd_seq + tcp->snd_sent ), tcp->rcv_ack );
  539. assert ( ( tcp->tcp_state == TCP_SYN_SENT ) ||
  540. ( tcp->tcp_state == TCP_SYN_RCVD ) ||
  541. ( tcp->tcp_state == TCP_ESTABLISHED ) ||
  542. ( tcp->tcp_state == TCP_FIN_WAIT_1 ) ||
  543. ( tcp->tcp_state == TCP_CLOSE_WAIT ) ||
  544. ( tcp->tcp_state == TCP_CLOSING_OR_LAST_ACK ) );
  545. if ( over ) {
  546. /* If we have finally timed out and given up,
  547. * terminate the connection
  548. */
  549. tcp->tcp_state = TCP_CLOSED;
  550. tcp_dump_state ( tcp );
  551. tcp_close ( tcp, -ETIMEDOUT );
  552. } else {
  553. /* Otherwise, retransmit the packet */
  554. tcp_xmit ( tcp );
  555. }
  556. }
  557. /**
  558. * Shutdown timer expired
  559. *
  560. * @v timer Shutdown timer
  561. * @v over Failure indicator
  562. */
  563. static void tcp_wait_expired ( struct retry_timer *timer, int over __unused ) {
  564. struct tcp_connection *tcp =
  565. container_of ( timer, struct tcp_connection, wait );
  566. assert ( tcp->tcp_state == TCP_TIME_WAIT );
  567. DBGC ( tcp, "TCP %p wait complete in %s for %08x..%08x %08x\n", tcp,
  568. tcp_state ( tcp->tcp_state ), tcp->snd_seq,
  569. ( tcp->snd_seq + tcp->snd_sent ), tcp->rcv_ack );
  570. tcp->tcp_state = TCP_CLOSED;
  571. tcp_dump_state ( tcp );
  572. tcp_close ( tcp, 0 );
  573. }
  574. /**
  575. * Send RST response to incoming packet
  576. *
  577. * @v in_tcphdr TCP header of incoming packet
  578. * @ret rc Return status code
  579. */
  580. static int tcp_xmit_reset ( struct tcp_connection *tcp,
  581. struct sockaddr_tcpip *st_dest,
  582. struct tcp_header *in_tcphdr ) {
  583. struct io_buffer *iobuf;
  584. struct tcp_header *tcphdr;
  585. int rc;
  586. /* Allocate space for dataless TX buffer */
  587. iobuf = alloc_iob ( TCP_MAX_HEADER_LEN );
  588. if ( ! iobuf ) {
  589. DBGC ( tcp, "TCP %p could not allocate iobuf for RST "
  590. "%08x..%08x %08x\n", tcp, ntohl ( in_tcphdr->ack ),
  591. ntohl ( in_tcphdr->ack ), ntohl ( in_tcphdr->seq ) );
  592. return -ENOMEM;
  593. }
  594. iob_reserve ( iobuf, TCP_MAX_HEADER_LEN );
  595. /* Construct RST response */
  596. tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
  597. memset ( tcphdr, 0, sizeof ( *tcphdr ) );
  598. tcphdr->src = in_tcphdr->dest;
  599. tcphdr->dest = in_tcphdr->src;
  600. tcphdr->seq = in_tcphdr->ack;
  601. tcphdr->ack = in_tcphdr->seq;
  602. tcphdr->hlen = ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
  603. tcphdr->flags = ( TCP_RST | TCP_ACK );
  604. tcphdr->win = htons ( TCP_MAX_WINDOW_SIZE );
  605. tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
  606. /* Dump header */
  607. DBGC2 ( tcp, "TCP %p TX %d->%d %08x..%08x %08x %4d",
  608. tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
  609. ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) ),
  610. ntohl ( tcphdr->ack ), 0 );
  611. tcp_dump_flags ( tcp, tcphdr->flags );
  612. DBGC2 ( tcp, "\n" );
  613. /* Transmit packet */
  614. if ( ( rc = tcpip_tx ( iobuf, &tcp_protocol, NULL, st_dest,
  615. NULL, &tcphdr->csum ) ) != 0 ) {
  616. DBGC ( tcp, "TCP %p could not transmit RST %08x..%08x %08x: "
  617. "%s\n", tcp, ntohl ( in_tcphdr->ack ),
  618. ntohl ( in_tcphdr->ack ), ntohl ( in_tcphdr->seq ),
  619. strerror ( rc ) );
  620. return rc;
  621. }
  622. return 0;
  623. }
  624. /***************************************************************************
  625. *
  626. * Receive data path
  627. *
  628. ***************************************************************************
  629. */
  630. /**
  631. * Identify TCP connection by local port number
  632. *
  633. * @v local_port Local port
  634. * @ret tcp TCP connection, or NULL
  635. */
  636. static struct tcp_connection * tcp_demux ( unsigned int local_port ) {
  637. struct tcp_connection *tcp;
  638. list_for_each_entry ( tcp, &tcp_conns, list ) {
  639. if ( tcp->local_port == local_port )
  640. return tcp;
  641. }
  642. return NULL;
  643. }
  644. /**
  645. * Parse TCP received options
  646. *
  647. * @v tcp TCP connection
  648. * @v data Raw options data
  649. * @v len Raw options length
  650. * @v options Options structure to fill in
  651. */
  652. static void tcp_rx_opts ( struct tcp_connection *tcp, const void *data,
  653. size_t len, struct tcp_options *options ) {
  654. const void *end = ( data + len );
  655. const struct tcp_option *option;
  656. unsigned int kind;
  657. memset ( options, 0, sizeof ( *options ) );
  658. while ( data < end ) {
  659. option = data;
  660. kind = option->kind;
  661. if ( kind == TCP_OPTION_END )
  662. return;
  663. if ( kind == TCP_OPTION_NOP ) {
  664. data++;
  665. continue;
  666. }
  667. switch ( kind ) {
  668. case TCP_OPTION_MSS:
  669. options->mssopt = data;
  670. break;
  671. case TCP_OPTION_TS:
  672. options->tsopt = data;
  673. break;
  674. default:
  675. DBGC ( tcp, "TCP %p received unknown option %d\n",
  676. tcp, kind );
  677. break;
  678. }
  679. data += option->length;
  680. }
  681. }
  682. /**
  683. * Consume received sequence space
  684. *
  685. * @v tcp TCP connection
  686. * @v seq_len Sequence space length to consume
  687. */
  688. static void tcp_rx_seq ( struct tcp_connection *tcp, uint32_t seq_len ) {
  689. /* Sanity check */
  690. assert ( seq_len > 0 );
  691. /* Update acknowledgement number */
  692. tcp->rcv_ack += seq_len;
  693. /* Update window */
  694. if ( tcp->rcv_win > seq_len ) {
  695. tcp->rcv_win -= seq_len;
  696. } else {
  697. tcp->rcv_win = 0;
  698. }
  699. /* Update timestamp */
  700. tcp->ts_recent = tcp->ts_val;
  701. /* Mark ACK as pending */
  702. tcp->flags |= TCP_ACK_PENDING;
  703. }
  704. /**
  705. * Handle TCP received SYN
  706. *
  707. * @v tcp TCP connection
  708. * @v seq SEQ value (in host-endian order)
  709. * @v options TCP options
  710. * @ret rc Return status code
  711. */
  712. static int tcp_rx_syn ( struct tcp_connection *tcp, uint32_t seq,
  713. struct tcp_options *options ) {
  714. /* Synchronise sequence numbers on first SYN */
  715. if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
  716. tcp->rcv_ack = seq;
  717. if ( options->tsopt )
  718. tcp->flags |= TCP_TS_ENABLED;
  719. }
  720. /* Ignore duplicate SYN */
  721. if ( seq != tcp->rcv_ack )
  722. return 0;
  723. /* Acknowledge SYN */
  724. tcp_rx_seq ( tcp, 1 );
  725. /* Mark SYN as received and start sending ACKs with each packet */
  726. tcp->tcp_state |= ( TCP_STATE_SENT ( TCP_ACK ) |
  727. TCP_STATE_RCVD ( TCP_SYN ) );
  728. return 0;
  729. }
  730. /**
  731. * Handle TCP received ACK
  732. *
  733. * @v tcp TCP connection
  734. * @v ack ACK value (in host-endian order)
  735. * @v win WIN value (in host-endian order)
  736. * @ret rc Return status code
  737. */
  738. static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
  739. uint32_t win ) {
  740. uint32_t ack_len = ( ack - tcp->snd_seq );
  741. size_t len;
  742. unsigned int acked_flags;
  743. /* Check for out-of-range or old duplicate ACKs */
  744. if ( ack_len > tcp->snd_sent ) {
  745. DBGC ( tcp, "TCP %p received ACK for %08x..%08x, "
  746. "sent only %08x..%08x\n", tcp, tcp->snd_seq,
  747. ( tcp->snd_seq + ack_len ), tcp->snd_seq,
  748. ( tcp->snd_seq + tcp->snd_sent ) );
  749. if ( TCP_HAS_BEEN_ESTABLISHED ( tcp->tcp_state ) ) {
  750. /* Just ignore what might be old duplicate ACKs */
  751. return 0;
  752. } else {
  753. /* Send RST if an out-of-range ACK is received
  754. * on a not-yet-established connection, as per
  755. * RFC 793.
  756. */
  757. return -EINVAL;
  758. }
  759. }
  760. /* Ignore ACKs that don't actually acknowledge any new data.
  761. * (In particular, do not stop the retransmission timer; this
  762. * avoids creating a sorceror's apprentice syndrome when a
  763. * duplicate ACK is received and we still have data in our
  764. * transmit queue.)
  765. */
  766. if ( ack_len == 0 )
  767. return 0;
  768. /* Stop the retransmission timer */
  769. stop_timer ( &tcp->timer );
  770. /* Determine acknowledged flags and data length */
  771. len = ack_len;
  772. acked_flags = ( TCP_FLAGS_SENDING ( tcp->tcp_state ) &
  773. ( TCP_SYN | TCP_FIN ) );
  774. if ( acked_flags )
  775. len--;
  776. /* Update SEQ and sent counters, and window size */
  777. tcp->snd_seq = ack;
  778. tcp->snd_sent = 0;
  779. tcp->snd_win = win;
  780. /* Remove any acknowledged data from transmit queue */
  781. tcp_process_tx_queue ( tcp, len, NULL, 1 );
  782. /* Mark SYN/FIN as acknowledged if applicable. */
  783. if ( acked_flags )
  784. tcp->tcp_state |= TCP_STATE_ACKED ( acked_flags );
  785. /* Start sending FIN if we've had all possible data ACKed */
  786. if ( list_empty ( &tcp->tx_queue ) && ( tcp->flags & TCP_XFER_CLOSED ) )
  787. tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
  788. return 0;
  789. }
  790. /**
  791. * Handle TCP received data
  792. *
  793. * @v tcp TCP connection
  794. * @v seq SEQ value (in host-endian order)
  795. * @v iobuf I/O buffer
  796. * @ret rc Return status code
  797. *
  798. * This function takes ownership of the I/O buffer.
  799. */
  800. static int tcp_rx_data ( struct tcp_connection *tcp, uint32_t seq,
  801. struct io_buffer *iobuf ) {
  802. uint32_t already_rcvd;
  803. uint32_t len;
  804. int rc;
  805. /* Ignore duplicate or out-of-order data */
  806. already_rcvd = ( tcp->rcv_ack - seq );
  807. len = iob_len ( iobuf );
  808. if ( already_rcvd >= len ) {
  809. free_iob ( iobuf );
  810. return 0;
  811. }
  812. iob_pull ( iobuf, already_rcvd );
  813. len -= already_rcvd;
  814. /* Acknowledge new data */
  815. tcp_rx_seq ( tcp, len );
  816. /* Deliver data to application */
  817. if ( ( rc = xfer_deliver_iob ( &tcp->xfer, iobuf ) ) != 0 ) {
  818. DBGC ( tcp, "TCP %p could not deliver %08x..%08x: %s\n",
  819. tcp, seq, ( seq + len ), strerror ( rc ) );
  820. return rc;
  821. }
  822. return 0;
  823. }
  824. /**
  825. * Handle TCP received FIN
  826. *
  827. * @v tcp TCP connection
  828. * @v seq SEQ value (in host-endian order)
  829. * @ret rc Return status code
  830. */
  831. static int tcp_rx_fin ( struct tcp_connection *tcp, uint32_t seq ) {
  832. /* Ignore duplicate or out-of-order FIN */
  833. if ( seq != tcp->rcv_ack )
  834. return 0;
  835. /* Acknowledge FIN */
  836. tcp_rx_seq ( tcp, 1 );
  837. /* Mark FIN as received */
  838. tcp->tcp_state |= TCP_STATE_RCVD ( TCP_FIN );
  839. /* Close connection */
  840. tcp_close ( tcp, 0 );
  841. return 0;
  842. }
  843. /**
  844. * Handle TCP received RST
  845. *
  846. * @v tcp TCP connection
  847. * @v seq SEQ value (in host-endian order)
  848. * @ret rc Return status code
  849. */
  850. static int tcp_rx_rst ( struct tcp_connection *tcp, uint32_t seq ) {
  851. /* Accept RST only if it falls within the window. If we have
  852. * not yet received a SYN, then we have no window to test
  853. * against, so fall back to checking that our SYN has been
  854. * ACKed.
  855. */
  856. if ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) {
  857. if ( ! tcp_in_window ( seq, tcp->rcv_ack, tcp->rcv_win ) )
  858. return 0;
  859. } else {
  860. if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
  861. return 0;
  862. }
  863. /* Abort connection */
  864. tcp->tcp_state = TCP_CLOSED;
  865. tcp_dump_state ( tcp );
  866. tcp_close ( tcp, -ECONNRESET );
  867. DBGC ( tcp, "TCP %p connection reset by peer\n", tcp );
  868. return -ECONNRESET;
  869. }
  870. /**
  871. * Enqueue received TCP packet
  872. *
  873. * @v tcp TCP connection
  874. * @v seq SEQ value (in host-endian order)
  875. * @v flags TCP flags
  876. * @v iobuf I/O buffer
  877. */
  878. static void tcp_rx_enqueue ( struct tcp_connection *tcp, uint32_t seq,
  879. uint8_t flags, struct io_buffer *iobuf ) {
  880. struct tcp_rx_queued_header *tcpqhdr;
  881. struct io_buffer *queued;
  882. size_t len;
  883. uint32_t seq_len;
  884. /* Calculate remaining flags and sequence length. Note that
  885. * SYN, if present, has already been processed by this point.
  886. */
  887. flags &= TCP_FIN;
  888. len = iob_len ( iobuf );
  889. seq_len = ( len + ( flags ? 1 : 0 ) );
  890. /* Discard immediately (to save memory) if:
  891. *
  892. * a) we have not yet received a SYN (and so have no defined
  893. * receive window), or
  894. * b) the packet lies entirely outside the receive window, or
  895. * c) there is no further content to process.
  896. */
  897. if ( ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) ||
  898. ( tcp_cmp ( seq, tcp->rcv_ack + tcp->rcv_win ) >= 0 ) ||
  899. ( tcp_cmp ( seq + seq_len, tcp->rcv_ack ) < 0 ) ||
  900. ( seq_len == 0 ) ) {
  901. free_iob ( iobuf );
  902. return;
  903. }
  904. /* Add internal header */
  905. tcpqhdr = iob_push ( iobuf, sizeof ( *tcpqhdr ) );
  906. tcpqhdr->seq = seq;
  907. tcpqhdr->flags = flags;
  908. /* Add to RX queue */
  909. list_for_each_entry ( queued, &tcp->rx_queue, list ) {
  910. tcpqhdr = queued->data;
  911. if ( tcp_cmp ( seq, tcpqhdr->seq ) < 0 )
  912. break;
  913. }
  914. list_add_tail ( &iobuf->list, &queued->list );
  915. }
  916. /**
  917. * Process receive queue
  918. *
  919. * @v tcp TCP connection
  920. */
  921. static void tcp_process_rx_queue ( struct tcp_connection *tcp ) {
  922. struct io_buffer *iobuf;
  923. struct tcp_rx_queued_header *tcpqhdr;
  924. uint32_t seq;
  925. unsigned int flags;
  926. size_t len;
  927. /* Process all applicable received buffers. Note that we
  928. * cannot use list_for_each_entry() to iterate over the RX
  929. * queue, since tcp_discard() may remove packets from the RX
  930. * queue while we are processing.
  931. */
  932. while ( ( iobuf = list_first_entry ( &tcp->rx_queue, struct io_buffer,
  933. list ) ) ) {
  934. /* Stop processing when we hit the first gap */
  935. tcpqhdr = iobuf->data;
  936. if ( tcp_cmp ( tcpqhdr->seq, tcp->rcv_ack ) > 0 )
  937. break;
  938. /* Strip internal header and remove from RX queue */
  939. list_del ( &iobuf->list );
  940. seq = tcpqhdr->seq;
  941. flags = tcpqhdr->flags;
  942. iob_pull ( iobuf, sizeof ( *tcpqhdr ) );
  943. len = iob_len ( iobuf );
  944. /* Handle new data, if any */
  945. tcp_rx_data ( tcp, seq, iob_disown ( iobuf ) );
  946. seq += len;
  947. /* Handle FIN, if present */
  948. if ( flags & TCP_FIN ) {
  949. tcp_rx_fin ( tcp, seq );
  950. seq++;
  951. }
  952. }
  953. }
  954. /**
  955. * Process received packet
  956. *
  957. * @v iobuf I/O buffer
  958. * @v st_src Partially-filled source address
  959. * @v st_dest Partially-filled destination address
  960. * @v pshdr_csum Pseudo-header checksum
  961. * @ret rc Return status code
  962. */
  963. static int tcp_rx ( struct io_buffer *iobuf,
  964. struct sockaddr_tcpip *st_src,
  965. struct sockaddr_tcpip *st_dest __unused,
  966. uint16_t pshdr_csum ) {
  967. struct tcp_header *tcphdr = iobuf->data;
  968. struct tcp_connection *tcp;
  969. struct tcp_options options;
  970. size_t hlen;
  971. uint16_t csum;
  972. uint32_t seq;
  973. uint32_t ack;
  974. uint32_t win;
  975. unsigned int flags;
  976. size_t len;
  977. uint32_t seq_len;
  978. size_t old_xfer_window;
  979. int rc;
  980. /* Sanity check packet */
  981. if ( iob_len ( iobuf ) < sizeof ( *tcphdr ) ) {
  982. DBG ( "TCP packet too short at %zd bytes (min %zd bytes)\n",
  983. iob_len ( iobuf ), sizeof ( *tcphdr ) );
  984. rc = -EINVAL;
  985. goto discard;
  986. }
  987. hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
  988. if ( hlen < sizeof ( *tcphdr ) ) {
  989. DBG ( "TCP header too short at %zd bytes (min %zd bytes)\n",
  990. hlen, sizeof ( *tcphdr ) );
  991. rc = -EINVAL;
  992. goto discard;
  993. }
  994. if ( hlen > iob_len ( iobuf ) ) {
  995. DBG ( "TCP header too long at %zd bytes (max %zd bytes)\n",
  996. hlen, iob_len ( iobuf ) );
  997. rc = -EINVAL;
  998. goto discard;
  999. }
  1000. csum = tcpip_continue_chksum ( pshdr_csum, iobuf->data,
  1001. iob_len ( iobuf ) );
  1002. if ( csum != 0 ) {
  1003. DBG ( "TCP checksum incorrect (is %04x including checksum "
  1004. "field, should be 0000)\n", csum );
  1005. rc = -EINVAL;
  1006. goto discard;
  1007. }
  1008. /* Parse parameters from header and strip header */
  1009. tcp = tcp_demux ( ntohs ( tcphdr->dest ) );
  1010. seq = ntohl ( tcphdr->seq );
  1011. ack = ntohl ( tcphdr->ack );
  1012. win = ntohs ( tcphdr->win );
  1013. flags = tcphdr->flags;
  1014. tcp_rx_opts ( tcp, ( ( ( void * ) tcphdr ) + sizeof ( *tcphdr ) ),
  1015. ( hlen - sizeof ( *tcphdr ) ), &options );
  1016. if ( options.tsopt )
  1017. tcp->ts_val = ntohl ( options.tsopt->tsval );
  1018. iob_pull ( iobuf, hlen );
  1019. len = iob_len ( iobuf );
  1020. seq_len = ( len + ( ( flags & TCP_SYN ) ? 1 : 0 ) +
  1021. ( ( flags & TCP_FIN ) ? 1 : 0 ) );
  1022. /* Dump header */
  1023. DBGC2 ( tcp, "TCP %p RX %d<-%d %08x %08x..%08x %4zd",
  1024. tcp, ntohs ( tcphdr->dest ), ntohs ( tcphdr->src ),
  1025. ntohl ( tcphdr->ack ), ntohl ( tcphdr->seq ),
  1026. ( ntohl ( tcphdr->seq ) + seq_len ), len );
  1027. tcp_dump_flags ( tcp, tcphdr->flags );
  1028. DBGC2 ( tcp, "\n" );
  1029. /* If no connection was found, send RST */
  1030. if ( ! tcp ) {
  1031. tcp_xmit_reset ( tcp, st_src, tcphdr );
  1032. rc = -ENOTCONN;
  1033. goto discard;
  1034. }
  1035. /* Record old data-transfer window */
  1036. old_xfer_window = tcp_xfer_window ( tcp );
  1037. /* Handle ACK, if present */
  1038. if ( flags & TCP_ACK ) {
  1039. if ( ( rc = tcp_rx_ack ( tcp, ack, win ) ) != 0 ) {
  1040. tcp_xmit_reset ( tcp, st_src, tcphdr );
  1041. goto discard;
  1042. }
  1043. }
  1044. /* Force an ACK if this packet is out of order */
  1045. if ( ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) &&
  1046. ( seq != tcp->rcv_ack ) ) {
  1047. tcp->flags |= TCP_ACK_PENDING;
  1048. }
  1049. /* Handle SYN, if present */
  1050. if ( flags & TCP_SYN ) {
  1051. tcp_rx_syn ( tcp, seq, &options );
  1052. seq++;
  1053. }
  1054. /* Handle RST, if present */
  1055. if ( flags & TCP_RST ) {
  1056. if ( ( rc = tcp_rx_rst ( tcp, seq ) ) != 0 )
  1057. goto discard;
  1058. }
  1059. /* Enqueue received data */
  1060. tcp_rx_enqueue ( tcp, seq, flags, iob_disown ( iobuf ) );
  1061. /* Process receive queue */
  1062. tcp_process_rx_queue ( tcp );
  1063. /* Dump out any state change as a result of the received packet */
  1064. tcp_dump_state ( tcp );
  1065. /* Send out any pending data */
  1066. tcp_xmit ( tcp );
  1067. /* If this packet was the last we expect to receive, set up
  1068. * timer to expire and cause the connection to be freed.
  1069. */
  1070. if ( TCP_CLOSED_GRACEFULLY ( tcp->tcp_state ) ) {
  1071. stop_timer ( &tcp->wait );
  1072. start_timer_fixed ( &tcp->wait, ( 2 * TCP_MSL ) );
  1073. }
  1074. /* Notify application if window has changed */
  1075. if ( tcp_xfer_window ( tcp ) != old_xfer_window )
  1076. xfer_window_changed ( &tcp->xfer );
  1077. return 0;
  1078. discard:
  1079. /* Free received packet */
  1080. free_iob ( iobuf );
  1081. return rc;
  1082. }
  1083. /** TCP protocol */
  1084. struct tcpip_protocol tcp_protocol __tcpip_protocol = {
  1085. .name = "TCP",
  1086. .rx = tcp_rx,
  1087. .tcpip_proto = IP_TCP,
  1088. };
  1089. /**
  1090. * Discard some cached TCP data
  1091. *
  1092. * @ret discarded Number of cached items discarded
  1093. */
  1094. static unsigned int tcp_discard ( void ) {
  1095. struct tcp_connection *tcp;
  1096. struct io_buffer *iobuf;
  1097. unsigned int discarded = 0;
  1098. /* Try to drop one queued RX packet from each connection */
  1099. list_for_each_entry ( tcp, &tcp_conns, list ) {
  1100. list_for_each_entry_reverse ( iobuf, &tcp->rx_queue, list ) {
  1101. list_del ( &iobuf->list );
  1102. free_iob ( iobuf );
  1103. discarded++;
  1104. break;
  1105. }
  1106. }
  1107. return discarded;
  1108. }
  1109. /** TCP cache discarder */
  1110. struct cache_discarder tcp_cache_discarder __cache_discarder = {
  1111. .discard = tcp_discard,
  1112. };
  1113. /***************************************************************************
  1114. *
  1115. * Data transfer interface
  1116. *
  1117. ***************************************************************************
  1118. */
  1119. /**
  1120. * Close interface
  1121. *
  1122. * @v tcp TCP connection
  1123. * @v rc Reason for close
  1124. */
  1125. static void tcp_xfer_close ( struct tcp_connection *tcp, int rc ) {
  1126. /* Close data transfer interface */
  1127. tcp_close ( tcp, rc );
  1128. /* Transmit FIN, if possible */
  1129. tcp_xmit ( tcp );
  1130. }
  1131. /**
  1132. * Deliver datagram as I/O buffer
  1133. *
  1134. * @v tcp TCP connection
  1135. * @v iobuf Datagram I/O buffer
  1136. * @v meta Data transfer metadata
  1137. * @ret rc Return status code
  1138. */
  1139. static int tcp_xfer_deliver ( struct tcp_connection *tcp,
  1140. struct io_buffer *iobuf,
  1141. struct xfer_metadata *meta __unused ) {
  1142. /* Enqueue packet */
  1143. list_add_tail ( &iobuf->list, &tcp->tx_queue );
  1144. /* Transmit data, if possible */
  1145. tcp_xmit ( tcp );
  1146. return 0;
  1147. }
  1148. /** TCP data transfer interface operations */
  1149. static struct interface_operation tcp_xfer_operations[] = {
  1150. INTF_OP ( xfer_deliver, struct tcp_connection *, tcp_xfer_deliver ),
  1151. INTF_OP ( xfer_window, struct tcp_connection *, tcp_xfer_window ),
  1152. INTF_OP ( intf_close, struct tcp_connection *, tcp_xfer_close ),
  1153. };
  1154. /** TCP data transfer interface descriptor */
  1155. static struct interface_descriptor tcp_xfer_desc =
  1156. INTF_DESC ( struct tcp_connection, xfer, tcp_xfer_operations );
  1157. /***************************************************************************
  1158. *
  1159. * Openers
  1160. *
  1161. ***************************************************************************
  1162. */
  1163. /** TCP socket opener */
  1164. struct socket_opener tcp_socket_opener __socket_opener = {
  1165. .semantics = TCP_SOCK_STREAM,
  1166. .family = AF_INET,
  1167. .open = tcp_open,
  1168. };
  1169. /** Linkage hack */
  1170. int tcp_sock_stream = TCP_SOCK_STREAM;
  1171. /**
  1172. * Open TCP URI
  1173. *
  1174. * @v xfer Data transfer interface
  1175. * @v uri URI
  1176. * @ret rc Return status code
  1177. */
  1178. static int tcp_open_uri ( struct interface *xfer, struct uri *uri ) {
  1179. struct sockaddr_tcpip peer;
  1180. /* Sanity check */
  1181. if ( ! uri->host )
  1182. return -EINVAL;
  1183. memset ( &peer, 0, sizeof ( peer ) );
  1184. peer.st_port = htons ( uri_port ( uri, 0 ) );
  1185. return xfer_open_named_socket ( xfer, SOCK_STREAM,
  1186. ( struct sockaddr * ) &peer,
  1187. uri->host, NULL );
  1188. }
  1189. /** TCP URI opener */
  1190. struct uri_opener tcp_uri_opener __uri_opener = {
  1191. .scheme = "tcp",
  1192. .open = tcp_open_uri,
  1193. };