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

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