Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

tcp.c 37KB

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