Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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/tcpip.h>
  16. #include <ipxe/tcp.h>
  17. /** @file
  18. *
  19. * TCP protocol
  20. *
  21. */
  22. FILE_LICENCE ( GPL2_OR_LATER );
  23. /** A TCP connection */
  24. struct tcp_connection {
  25. /** Reference counter */
  26. struct refcnt refcnt;
  27. /** List of TCP connections */
  28. struct list_head list;
  29. /** Flags */
  30. unsigned int flags;
  31. /** Data transfer interface */
  32. struct interface xfer;
  33. /** Remote socket address */
  34. struct sockaddr_tcpip peer;
  35. /** Local port */
  36. unsigned int local_port;
  37. /** Current TCP state */
  38. unsigned int tcp_state;
  39. /** Previous TCP state
  40. *
  41. * Maintained only for debug messages
  42. */
  43. unsigned int prev_tcp_state;
  44. /** Current sequence number
  45. *
  46. * Equivalent to SND.UNA in RFC 793 terminology.
  47. */
  48. uint32_t snd_seq;
  49. /** Unacknowledged sequence count
  50. *
  51. * Equivalent to (SND.NXT-SND.UNA) in RFC 793 terminology.
  52. */
  53. uint32_t snd_sent;
  54. /** Send window
  55. *
  56. * Equivalent to SND.WND in RFC 793 terminology
  57. */
  58. uint32_t snd_win;
  59. /** Current acknowledgement number
  60. *
  61. * Equivalent to RCV.NXT in RFC 793 terminology.
  62. */
  63. uint32_t rcv_ack;
  64. /** Receive window
  65. *
  66. * Equivalent to RCV.WND in RFC 793 terminology.
  67. */
  68. uint32_t rcv_win;
  69. /** Most recent received timestamp
  70. *
  71. * Equivalent to TS.Recent in RFC 1323 terminology.
  72. */
  73. uint32_t ts_recent;
  74. /** Transmit queue */
  75. struct list_head tx_queue;
  76. /** Receive queue */
  77. struct list_head rx_queue;
  78. /** Retransmission timer */
  79. struct retry_timer timer;
  80. /** Shutdown (TIME_WAIT) timer */
  81. struct retry_timer wait;
  82. };
  83. /** TCP flags */
  84. enum tcp_flags {
  85. /** TCP data transfer interface has been closed */
  86. TCP_XFER_CLOSED = 0x0001,
  87. /** TCP timestamps are enabled */
  88. TCP_TS_ENABLED = 0x0002,
  89. /** TCP acknowledgement is pending */
  90. TCP_ACK_PENDING = 0x0004,
  91. };
  92. /** TCP internal header
  93. *
  94. * This is the header that replaces the TCP header for packets
  95. * enqueued on the receive queue.
  96. */
  97. struct tcp_rx_queued_header {
  98. /** SEQ value, in host-endian order
  99. *
  100. * This represents the SEQ value at the time the packet is
  101. * enqueued, and so excludes the SYN, if present.
  102. */
  103. uint32_t seq;
  104. /** Flags
  105. *
  106. * Only FIN is valid within this flags byte; all other flags
  107. * have already been processed by the time the packet is
  108. * enqueued.
  109. */
  110. uint8_t flags;
  111. /** Reserved */
  112. uint8_t reserved[3];
  113. };
  114. /**
  115. * List of registered TCP connections
  116. */
  117. static LIST_HEAD ( tcp_conns );
  118. /* Forward declarations */
  119. static struct interface_descriptor tcp_xfer_desc;
  120. static void tcp_expired ( struct retry_timer *timer, int over );
  121. static void tcp_wait_expired ( struct retry_timer *timer, int over );
  122. static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
  123. uint32_t win );
  124. /**
  125. * Name TCP state
  126. *
  127. * @v state TCP state
  128. * @ret name Name of TCP state
  129. */
  130. static inline __attribute__ (( always_inline )) const char *
  131. tcp_state ( int state ) {
  132. switch ( state ) {
  133. case TCP_CLOSED: return "CLOSED";
  134. case TCP_LISTEN: return "LISTEN";
  135. case TCP_SYN_SENT: return "SYN_SENT";
  136. case TCP_SYN_RCVD: return "SYN_RCVD";
  137. case TCP_ESTABLISHED: return "ESTABLISHED";
  138. case TCP_FIN_WAIT_1: return "FIN_WAIT_1";
  139. case TCP_FIN_WAIT_2: return "FIN_WAIT_2";
  140. case TCP_CLOSING_OR_LAST_ACK: return "CLOSING/LAST_ACK";
  141. case TCP_TIME_WAIT: return "TIME_WAIT";
  142. case TCP_CLOSE_WAIT: return "CLOSE_WAIT";
  143. default: return "INVALID";
  144. }
  145. }
  146. /**
  147. * Dump TCP state transition
  148. *
  149. * @v tcp TCP connection
  150. */
  151. static inline __attribute__ (( always_inline )) void
  152. tcp_dump_state ( struct tcp_connection *tcp ) {
  153. if ( tcp->tcp_state != tcp->prev_tcp_state ) {
  154. DBGC ( tcp, "TCP %p transitioned from %s to %s\n", tcp,
  155. tcp_state ( tcp->prev_tcp_state ),
  156. tcp_state ( tcp->tcp_state ) );
  157. }
  158. tcp->prev_tcp_state = tcp->tcp_state;
  159. }
  160. /**
  161. * Dump TCP flags
  162. *
  163. * @v flags TCP flags
  164. */
  165. static inline __attribute__ (( always_inline )) void
  166. tcp_dump_flags ( struct tcp_connection *tcp, unsigned int flags ) {
  167. if ( flags & TCP_RST )
  168. DBGC2 ( tcp, " RST" );
  169. if ( flags & TCP_SYN )
  170. DBGC2 ( tcp, " SYN" );
  171. if ( flags & TCP_PSH )
  172. DBGC2 ( tcp, " PSH" );
  173. if ( flags & TCP_FIN )
  174. DBGC2 ( tcp, " FIN" );
  175. if ( flags & TCP_ACK )
  176. DBGC2 ( tcp, " ACK" );
  177. }
  178. /***************************************************************************
  179. *
  180. * Open and close
  181. *
  182. ***************************************************************************
  183. */
  184. /**
  185. * Bind TCP connection to local port
  186. *
  187. * @v tcp TCP connection
  188. * @v port Local port number
  189. * @ret rc Return status code
  190. *
  191. * If the port is 0, the connection is assigned an available port
  192. * between 1024 and 65535.
  193. */
  194. static int tcp_bind ( struct tcp_connection *tcp, unsigned int port ) {
  195. struct tcp_connection *existing;
  196. uint16_t try_port;
  197. unsigned int i;
  198. /* If no port is specified, find an available port */
  199. if ( ! port ) {
  200. try_port = random();
  201. for ( i = 0 ; i < 65536 ; i++ ) {
  202. try_port++;
  203. if ( try_port < 1024 )
  204. continue;
  205. if ( tcp_bind ( tcp, try_port ) == 0 )
  206. return 0;
  207. }
  208. DBGC ( tcp, "TCP %p could not bind: no free ports\n", tcp );
  209. return -EADDRINUSE;
  210. }
  211. /* Attempt bind to local port */
  212. list_for_each_entry ( existing, &tcp_conns, list ) {
  213. if ( existing->local_port == port ) {
  214. DBGC ( tcp, "TCP %p could not bind: port %d in use\n",
  215. tcp, port );
  216. return -EADDRINUSE;
  217. }
  218. }
  219. tcp->local_port = port;
  220. DBGC ( tcp, "TCP %p bound to port %d\n", tcp, port );
  221. return 0;
  222. }
  223. /**
  224. * Open a TCP connection
  225. *
  226. * @v xfer Data transfer interface
  227. * @v peer Peer socket address
  228. * @v local Local socket address, or NULL
  229. * @ret rc Return status code
  230. */
  231. static int tcp_open ( struct interface *xfer, struct sockaddr *peer,
  232. struct sockaddr *local ) {
  233. struct sockaddr_tcpip *st_peer = ( struct sockaddr_tcpip * ) peer;
  234. struct sockaddr_tcpip *st_local = ( struct sockaddr_tcpip * ) local;
  235. struct tcp_connection *tcp;
  236. unsigned int bind_port;
  237. int rc;
  238. /* Allocate and initialise structure */
  239. tcp = zalloc ( sizeof ( *tcp ) );
  240. if ( ! tcp )
  241. return -ENOMEM;
  242. DBGC ( tcp, "TCP %p allocated\n", tcp );
  243. ref_init ( &tcp->refcnt, NULL );
  244. intf_init ( &tcp->xfer, &tcp_xfer_desc, &tcp->refcnt );
  245. timer_init ( &tcp->timer, tcp_expired, &tcp->refcnt );
  246. timer_init ( &tcp->wait, tcp_wait_expired, &tcp->refcnt );
  247. tcp->prev_tcp_state = TCP_CLOSED;
  248. tcp->tcp_state = TCP_STATE_SENT ( TCP_SYN );
  249. tcp_dump_state ( tcp );
  250. tcp->snd_seq = random();
  251. INIT_LIST_HEAD ( &tcp->tx_queue );
  252. INIT_LIST_HEAD ( &tcp->rx_queue );
  253. memcpy ( &tcp->peer, st_peer, sizeof ( tcp->peer ) );
  254. /* Bind to local port */
  255. bind_port = ( st_local ? ntohs ( st_local->st_port ) : 0 );
  256. if ( ( rc = tcp_bind ( tcp, bind_port ) ) != 0 )
  257. goto err;
  258. /* Start timer to initiate SYN */
  259. start_timer_nodelay ( &tcp->timer );
  260. /* Attach parent interface, transfer reference to connection
  261. * list and return
  262. */
  263. intf_plug_plug ( &tcp->xfer, xfer );
  264. list_add ( &tcp->list, &tcp_conns );
  265. return 0;
  266. err:
  267. ref_put ( &tcp->refcnt );
  268. return rc;
  269. }
  270. /**
  271. * Close TCP connection
  272. *
  273. * @v tcp TCP connection
  274. * @v rc Reason for close
  275. *
  276. * Closes the data transfer interface. If the TCP state machine is in
  277. * a suitable state, the connection will be deleted.
  278. */
  279. static void tcp_close ( struct tcp_connection *tcp, int rc ) {
  280. struct io_buffer *iobuf;
  281. struct io_buffer *tmp;
  282. /* Close data transfer interface */
  283. intf_shutdown ( &tcp->xfer, rc );
  284. tcp->flags |= TCP_XFER_CLOSED;
  285. /* If we are in CLOSED, or have otherwise not yet received a
  286. * SYN (i.e. we are in LISTEN or SYN_SENT), just delete the
  287. * connection.
  288. */
  289. if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
  290. /* Transition to CLOSED for the sake of debugging messages */
  291. tcp->tcp_state = TCP_CLOSED;
  292. tcp_dump_state ( tcp );
  293. /* Free any unprocessed I/O buffers */
  294. list_for_each_entry_safe ( iobuf, tmp, &tcp->rx_queue, list ) {
  295. list_del ( &iobuf->list );
  296. free_iob ( iobuf );
  297. }
  298. /* Free any unsent I/O buffers */
  299. list_for_each_entry_safe ( iobuf, tmp, &tcp->tx_queue, list ) {
  300. list_del ( &iobuf->list );
  301. free_iob ( iobuf );
  302. }
  303. /* Remove from list and drop reference */
  304. stop_timer ( &tcp->timer );
  305. list_del ( &tcp->list );
  306. ref_put ( &tcp->refcnt );
  307. DBGC ( tcp, "TCP %p connection deleted\n", tcp );
  308. return;
  309. }
  310. /* If we have not had our SYN acknowledged (i.e. we are in
  311. * SYN_RCVD), pretend that it has been acknowledged so that we
  312. * can send a FIN without breaking things.
  313. */
  314. if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
  315. tcp_rx_ack ( tcp, ( tcp->snd_seq + 1 ), 0 );
  316. /* If we have no data remaining to send, start sending FIN */
  317. if ( list_empty ( &tcp->tx_queue ) ) {
  318. tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
  319. tcp_dump_state ( tcp );
  320. }
  321. }
  322. /***************************************************************************
  323. *
  324. * Transmit data path
  325. *
  326. ***************************************************************************
  327. */
  328. /**
  329. * Calculate transmission window
  330. *
  331. * @v tcp TCP connection
  332. * @ret len Maximum length that can be sent in a single packet
  333. */
  334. static size_t tcp_xmit_win ( struct tcp_connection *tcp ) {
  335. size_t len;
  336. /* Not ready if we're not in a suitable connection state */
  337. if ( ! TCP_CAN_SEND_DATA ( tcp->tcp_state ) )
  338. return 0;
  339. /* Length is the minimum of the receiver's window and the path MTU */
  340. len = tcp->snd_win;
  341. if ( len > TCP_PATH_MTU )
  342. len = TCP_PATH_MTU;
  343. return len;
  344. }
  345. /**
  346. * Process TCP transmit queue
  347. *
  348. * @v tcp TCP connection
  349. * @v max_len Maximum length to process
  350. * @v dest I/O buffer to fill with data, or NULL
  351. * @v remove Remove data from queue
  352. * @ret len Length of data processed
  353. *
  354. * This processes at most @c max_len bytes from the TCP connection's
  355. * transmit queue. Data will be copied into the @c dest I/O buffer
  356. * (if provided) and, if @c remove is true, removed from the transmit
  357. * queue.
  358. */
  359. static size_t tcp_process_tx_queue ( struct tcp_connection *tcp, size_t max_len,
  360. struct io_buffer *dest, int remove ) {
  361. struct io_buffer *iobuf;
  362. struct io_buffer *tmp;
  363. size_t frag_len;
  364. size_t len = 0;
  365. list_for_each_entry_safe ( iobuf, tmp, &tcp->tx_queue, list ) {
  366. frag_len = iob_len ( iobuf );
  367. if ( frag_len > max_len )
  368. frag_len = max_len;
  369. if ( dest ) {
  370. memcpy ( iob_put ( dest, frag_len ), iobuf->data,
  371. frag_len );
  372. }
  373. if ( remove ) {
  374. iob_pull ( iobuf, frag_len );
  375. if ( ! iob_len ( iobuf ) ) {
  376. list_del ( &iobuf->list );
  377. free_iob ( iobuf );
  378. }
  379. }
  380. len += frag_len;
  381. max_len -= frag_len;
  382. }
  383. return len;
  384. }
  385. /**
  386. * Transmit any outstanding data
  387. *
  388. * @v tcp TCP connection
  389. *
  390. * Transmits any outstanding data on the connection.
  391. *
  392. * Note that even if an error is returned, the retransmission timer
  393. * will have been started if necessary, and so the stack will
  394. * eventually attempt to retransmit the failed packet.
  395. */
  396. static int tcp_xmit ( struct tcp_connection *tcp ) {
  397. struct io_buffer *iobuf;
  398. struct tcp_header *tcphdr;
  399. struct tcp_mss_option *mssopt;
  400. struct tcp_timestamp_padded_option *tsopt;
  401. void *payload;
  402. unsigned int flags;
  403. size_t len = 0;
  404. uint32_t seq_len;
  405. uint32_t app_win;
  406. uint32_t max_rcv_win;
  407. int rc;
  408. /* If retransmission timer is already running, do nothing */
  409. if ( timer_running ( &tcp->timer ) )
  410. return 0;
  411. /* Calculate both the actual (payload) and sequence space
  412. * lengths that we wish to transmit.
  413. */
  414. if ( TCP_CAN_SEND_DATA ( tcp->tcp_state ) ) {
  415. len = tcp_process_tx_queue ( tcp, tcp_xmit_win ( tcp ),
  416. NULL, 0 );
  417. }
  418. seq_len = len;
  419. flags = TCP_FLAGS_SENDING ( tcp->tcp_state );
  420. if ( flags & ( TCP_SYN | TCP_FIN ) ) {
  421. /* SYN or FIN consume one byte, and we can never send both */
  422. assert ( ! ( ( flags & TCP_SYN ) && ( flags & TCP_FIN ) ) );
  423. seq_len++;
  424. }
  425. tcp->snd_sent = seq_len;
  426. /* If we have nothing to transmit, stop now */
  427. if ( ( seq_len == 0 ) && ! ( tcp->flags & TCP_ACK_PENDING ) )
  428. return 0;
  429. /* If we are transmitting anything that requires
  430. * acknowledgement (i.e. consumes sequence space), start the
  431. * retransmission timer. Do this before attempting to
  432. * allocate the I/O buffer, in case allocation itself fails.
  433. */
  434. if ( seq_len )
  435. start_timer ( &tcp->timer );
  436. /* Allocate I/O buffer */
  437. iobuf = alloc_iob ( len + MAX_HDR_LEN );
  438. if ( ! iobuf ) {
  439. DBGC ( tcp, "TCP %p could not allocate iobuf for %08x..%08x "
  440. "%08x\n", tcp, tcp->snd_seq, ( tcp->snd_seq + seq_len ),
  441. tcp->rcv_ack );
  442. return -ENOMEM;
  443. }
  444. iob_reserve ( iobuf, MAX_HDR_LEN );
  445. /* Fill data payload from transmit queue */
  446. tcp_process_tx_queue ( tcp, len, iobuf, 0 );
  447. /* Expand receive window if possible */
  448. max_rcv_win = ( ( freemem * 3 ) / 4 );
  449. if ( max_rcv_win > TCP_MAX_WINDOW_SIZE )
  450. max_rcv_win = TCP_MAX_WINDOW_SIZE;
  451. app_win = xfer_window ( &tcp->xfer );
  452. if ( max_rcv_win > app_win )
  453. max_rcv_win = app_win;
  454. max_rcv_win &= ~0x03; /* Keep everything dword-aligned */
  455. if ( tcp->rcv_win < max_rcv_win )
  456. tcp->rcv_win = max_rcv_win;
  457. /* Fill up the TCP header */
  458. payload = iobuf->data;
  459. if ( flags & TCP_SYN ) {
  460. mssopt = iob_push ( iobuf, sizeof ( *mssopt ) );
  461. mssopt->kind = TCP_OPTION_MSS;
  462. mssopt->length = sizeof ( *mssopt );
  463. mssopt->mss = htons ( TCP_MSS );
  464. }
  465. if ( ( flags & TCP_SYN ) || ( tcp->flags & TCP_TS_ENABLED ) ) {
  466. tsopt = iob_push ( iobuf, sizeof ( *tsopt ) );
  467. memset ( tsopt->nop, TCP_OPTION_NOP, sizeof ( tsopt->nop ) );
  468. tsopt->tsopt.kind = TCP_OPTION_TS;
  469. tsopt->tsopt.length = sizeof ( tsopt->tsopt );
  470. tsopt->tsopt.tsval = htonl ( currticks() );
  471. tsopt->tsopt.tsecr = htonl ( tcp->ts_recent );
  472. }
  473. if ( ! ( flags & TCP_SYN ) )
  474. flags |= TCP_PSH;
  475. tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
  476. memset ( tcphdr, 0, sizeof ( *tcphdr ) );
  477. tcphdr->src = htons ( tcp->local_port );
  478. tcphdr->dest = tcp->peer.st_port;
  479. tcphdr->seq = htonl ( tcp->snd_seq );
  480. tcphdr->ack = htonl ( tcp->rcv_ack );
  481. tcphdr->hlen = ( ( payload - iobuf->data ) << 2 );
  482. tcphdr->flags = flags;
  483. tcphdr->win = htons ( tcp->rcv_win );
  484. tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
  485. /* Dump header */
  486. DBGC2 ( tcp, "TCP %p TX %d->%d %08x..%08x %08x %4zd",
  487. tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
  488. ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) + seq_len ),
  489. ntohl ( tcphdr->ack ), len );
  490. tcp_dump_flags ( tcp, tcphdr->flags );
  491. DBGC2 ( tcp, "\n" );
  492. /* Transmit packet */
  493. if ( ( rc = tcpip_tx ( iobuf, &tcp_protocol, NULL, &tcp->peer, NULL,
  494. &tcphdr->csum ) ) != 0 ) {
  495. DBGC ( tcp, "TCP %p could not transmit %08x..%08x %08x: %s\n",
  496. tcp, tcp->snd_seq, ( tcp->snd_seq + tcp->snd_sent ),
  497. tcp->rcv_ack, strerror ( rc ) );
  498. return rc;
  499. }
  500. /* Clear ACK-pending flag */
  501. tcp->flags &= ~TCP_ACK_PENDING;
  502. return 0;
  503. }
  504. /**
  505. * Retransmission timer expired
  506. *
  507. * @v timer Retransmission timer
  508. * @v over Failure indicator
  509. */
  510. static void tcp_expired ( struct retry_timer *timer, int over ) {
  511. struct tcp_connection *tcp =
  512. container_of ( timer, struct tcp_connection, timer );
  513. DBGC ( tcp, "TCP %p timer %s in %s for %08x..%08x %08x\n", tcp,
  514. ( over ? "expired" : "fired" ), tcp_state ( tcp->tcp_state ),
  515. tcp->snd_seq, ( tcp->snd_seq + tcp->snd_sent ), tcp->rcv_ack );
  516. assert ( ( tcp->tcp_state == TCP_SYN_SENT ) ||
  517. ( tcp->tcp_state == TCP_SYN_RCVD ) ||
  518. ( tcp->tcp_state == TCP_ESTABLISHED ) ||
  519. ( tcp->tcp_state == TCP_FIN_WAIT_1 ) ||
  520. ( tcp->tcp_state == TCP_CLOSE_WAIT ) ||
  521. ( tcp->tcp_state == TCP_CLOSING_OR_LAST_ACK ) );
  522. if ( over ) {
  523. /* If we have finally timed out and given up,
  524. * terminate the connection
  525. */
  526. tcp->tcp_state = TCP_CLOSED;
  527. tcp_dump_state ( tcp );
  528. tcp_close ( tcp, -ETIMEDOUT );
  529. } else {
  530. /* Otherwise, retransmit the packet */
  531. tcp_xmit ( tcp );
  532. }
  533. }
  534. /**
  535. * Shutdown timer expired
  536. *
  537. * @v timer Shutdown timer
  538. * @v over Failure indicator
  539. */
  540. static void tcp_wait_expired ( struct retry_timer *timer, int over __unused ) {
  541. struct tcp_connection *tcp =
  542. container_of ( timer, struct tcp_connection, wait );
  543. assert ( tcp->tcp_state == TCP_TIME_WAIT );
  544. DBGC ( tcp, "TCP %p wait complete in %s for %08x..%08x %08x\n", tcp,
  545. tcp_state ( tcp->tcp_state ), tcp->snd_seq,
  546. ( tcp->snd_seq + tcp->snd_sent ), tcp->rcv_ack );
  547. tcp->tcp_state = TCP_CLOSED;
  548. tcp_dump_state ( tcp );
  549. tcp_close ( tcp, 0 );
  550. }
  551. /**
  552. * Send RST response to incoming packet
  553. *
  554. * @v in_tcphdr TCP header of incoming packet
  555. * @ret rc Return status code
  556. */
  557. static int tcp_xmit_reset ( struct tcp_connection *tcp,
  558. struct sockaddr_tcpip *st_dest,
  559. struct tcp_header *in_tcphdr ) {
  560. struct io_buffer *iobuf;
  561. struct tcp_header *tcphdr;
  562. int rc;
  563. /* Allocate space for dataless TX buffer */
  564. iobuf = alloc_iob ( MAX_HDR_LEN );
  565. if ( ! iobuf ) {
  566. DBGC ( tcp, "TCP %p could not allocate iobuf for RST "
  567. "%08x..%08x %08x\n", tcp, ntohl ( in_tcphdr->ack ),
  568. ntohl ( in_tcphdr->ack ), ntohl ( in_tcphdr->seq ) );
  569. return -ENOMEM;
  570. }
  571. iob_reserve ( iobuf, MAX_HDR_LEN );
  572. /* Construct RST response */
  573. tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
  574. memset ( tcphdr, 0, sizeof ( *tcphdr ) );
  575. tcphdr->src = in_tcphdr->dest;
  576. tcphdr->dest = in_tcphdr->src;
  577. tcphdr->seq = in_tcphdr->ack;
  578. tcphdr->ack = in_tcphdr->seq;
  579. tcphdr->hlen = ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
  580. tcphdr->flags = ( TCP_RST | TCP_ACK );
  581. tcphdr->win = htons ( TCP_MAX_WINDOW_SIZE );
  582. tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
  583. /* Dump header */
  584. DBGC2 ( tcp, "TCP %p TX %d->%d %08x..%08x %08x %4d",
  585. tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
  586. ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) ),
  587. ntohl ( tcphdr->ack ), 0 );
  588. tcp_dump_flags ( tcp, tcphdr->flags );
  589. DBGC2 ( tcp, "\n" );
  590. /* Transmit packet */
  591. if ( ( rc = tcpip_tx ( iobuf, &tcp_protocol, NULL, st_dest,
  592. NULL, &tcphdr->csum ) ) != 0 ) {
  593. DBGC ( tcp, "TCP %p could not transmit RST %08x..%08x %08x: "
  594. "%s\n", tcp, ntohl ( in_tcphdr->ack ),
  595. ntohl ( in_tcphdr->ack ), ntohl ( in_tcphdr->seq ),
  596. strerror ( rc ) );
  597. return rc;
  598. }
  599. return 0;
  600. }
  601. /***************************************************************************
  602. *
  603. * Receive data path
  604. *
  605. ***************************************************************************
  606. */
  607. /**
  608. * Identify TCP connection by local port number
  609. *
  610. * @v local_port Local port
  611. * @ret tcp TCP connection, or NULL
  612. */
  613. static struct tcp_connection * tcp_demux ( unsigned int local_port ) {
  614. struct tcp_connection *tcp;
  615. list_for_each_entry ( tcp, &tcp_conns, list ) {
  616. if ( tcp->local_port == local_port )
  617. return tcp;
  618. }
  619. return NULL;
  620. }
  621. /**
  622. * Parse TCP received options
  623. *
  624. * @v tcp TCP connection
  625. * @v data Raw options data
  626. * @v len Raw options length
  627. * @v options Options structure to fill in
  628. */
  629. static void tcp_rx_opts ( struct tcp_connection *tcp, const void *data,
  630. size_t len, struct tcp_options *options ) {
  631. const void *end = ( data + len );
  632. const struct tcp_option *option;
  633. unsigned int kind;
  634. memset ( options, 0, sizeof ( *options ) );
  635. while ( data < end ) {
  636. option = data;
  637. kind = option->kind;
  638. if ( kind == TCP_OPTION_END )
  639. return;
  640. if ( kind == TCP_OPTION_NOP ) {
  641. data++;
  642. continue;
  643. }
  644. switch ( kind ) {
  645. case TCP_OPTION_MSS:
  646. options->mssopt = data;
  647. break;
  648. case TCP_OPTION_TS:
  649. options->tsopt = data;
  650. break;
  651. default:
  652. DBGC ( tcp, "TCP %p received unknown option %d\n",
  653. tcp, kind );
  654. break;
  655. }
  656. data += option->length;
  657. }
  658. }
  659. /**
  660. * Consume received sequence space
  661. *
  662. * @v tcp TCP connection
  663. * @v seq_len Sequence space length to consume
  664. */
  665. static void tcp_rx_seq ( struct tcp_connection *tcp, uint32_t seq_len ) {
  666. tcp->rcv_ack += seq_len;
  667. if ( tcp->rcv_win > seq_len ) {
  668. tcp->rcv_win -= seq_len;
  669. } else {
  670. tcp->rcv_win = 0;
  671. }
  672. tcp->flags |= TCP_ACK_PENDING;
  673. }
  674. /**
  675. * Handle TCP received SYN
  676. *
  677. * @v tcp TCP connection
  678. * @v seq SEQ value (in host-endian order)
  679. * @v options TCP options
  680. * @ret rc Return status code
  681. */
  682. static int tcp_rx_syn ( struct tcp_connection *tcp, uint32_t seq,
  683. struct tcp_options *options ) {
  684. /* Synchronise sequence numbers on first SYN */
  685. if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
  686. tcp->rcv_ack = seq;
  687. if ( options->tsopt )
  688. tcp->flags |= TCP_TS_ENABLED;
  689. }
  690. /* Ignore duplicate SYN */
  691. if ( seq != tcp->rcv_ack )
  692. return 0;
  693. /* Acknowledge SYN */
  694. tcp_rx_seq ( tcp, 1 );
  695. /* Mark SYN as received and start sending ACKs with each packet */
  696. tcp->tcp_state |= ( TCP_STATE_SENT ( TCP_ACK ) |
  697. TCP_STATE_RCVD ( TCP_SYN ) );
  698. return 0;
  699. }
  700. /**
  701. * Handle TCP received ACK
  702. *
  703. * @v tcp TCP connection
  704. * @v ack ACK value (in host-endian order)
  705. * @v win WIN value (in host-endian order)
  706. * @ret rc Return status code
  707. */
  708. static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
  709. uint32_t win ) {
  710. uint32_t ack_len = ( ack - tcp->snd_seq );
  711. size_t len;
  712. unsigned int acked_flags;
  713. /* Check for out-of-range or old duplicate ACKs */
  714. if ( ack_len > tcp->snd_sent ) {
  715. DBGC ( tcp, "TCP %p received ACK for %08x..%08x, "
  716. "sent only %08x..%08x\n", tcp, tcp->snd_seq,
  717. ( tcp->snd_seq + ack_len ), tcp->snd_seq,
  718. ( tcp->snd_seq + tcp->snd_sent ) );
  719. if ( TCP_HAS_BEEN_ESTABLISHED ( tcp->tcp_state ) ) {
  720. /* Just ignore what might be old duplicate ACKs */
  721. return 0;
  722. } else {
  723. /* Send RST if an out-of-range ACK is received
  724. * on a not-yet-established connection, as per
  725. * RFC 793.
  726. */
  727. return -EINVAL;
  728. }
  729. }
  730. /* Ignore ACKs that don't actually acknowledge any new data.
  731. * (In particular, do not stop the retransmission timer; this
  732. * avoids creating a sorceror's apprentice syndrome when a
  733. * duplicate ACK is received and we still have data in our
  734. * transmit queue.)
  735. */
  736. if ( ack_len == 0 )
  737. return 0;
  738. /* Stop the retransmission timer */
  739. stop_timer ( &tcp->timer );
  740. /* Determine acknowledged flags and data length */
  741. len = ack_len;
  742. acked_flags = ( TCP_FLAGS_SENDING ( tcp->tcp_state ) &
  743. ( TCP_SYN | TCP_FIN ) );
  744. if ( acked_flags )
  745. len--;
  746. /* Update SEQ and sent counters, and window size */
  747. tcp->snd_seq = ack;
  748. tcp->snd_sent = 0;
  749. tcp->snd_win = win;
  750. /* Remove any acknowledged data from transmit queue */
  751. tcp_process_tx_queue ( tcp, len, NULL, 1 );
  752. /* Mark SYN/FIN as acknowledged if applicable. */
  753. if ( acked_flags )
  754. tcp->tcp_state |= TCP_STATE_ACKED ( acked_flags );
  755. /* Start sending FIN if we've had all possible data ACKed */
  756. if ( list_empty ( &tcp->tx_queue ) && ( tcp->flags & TCP_XFER_CLOSED ) )
  757. tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
  758. return 0;
  759. }
  760. /**
  761. * Handle TCP received data
  762. *
  763. * @v tcp TCP connection
  764. * @v seq SEQ value (in host-endian order)
  765. * @v iobuf I/O buffer
  766. * @ret rc Return status code
  767. *
  768. * This function takes ownership of the I/O buffer.
  769. */
  770. static int tcp_rx_data ( struct tcp_connection *tcp, uint32_t seq,
  771. struct io_buffer *iobuf ) {
  772. uint32_t already_rcvd;
  773. uint32_t len;
  774. int rc;
  775. /* Ignore duplicate or out-of-order data */
  776. already_rcvd = ( tcp->rcv_ack - seq );
  777. len = iob_len ( iobuf );
  778. if ( already_rcvd >= len ) {
  779. free_iob ( iobuf );
  780. return 0;
  781. }
  782. iob_pull ( iobuf, already_rcvd );
  783. len -= already_rcvd;
  784. /* Acknowledge new data */
  785. tcp_rx_seq ( tcp, len );
  786. /* Deliver data to application */
  787. if ( ( rc = xfer_deliver_iob ( &tcp->xfer, iobuf ) ) != 0 ) {
  788. DBGC ( tcp, "TCP %p could not deliver %08x..%08x: %s\n",
  789. tcp, seq, ( seq + len ), strerror ( rc ) );
  790. return rc;
  791. }
  792. return 0;
  793. }
  794. /**
  795. * Handle TCP received FIN
  796. *
  797. * @v tcp TCP connection
  798. * @v seq SEQ value (in host-endian order)
  799. * @ret rc Return status code
  800. */
  801. static int tcp_rx_fin ( struct tcp_connection *tcp, uint32_t seq ) {
  802. /* Ignore duplicate or out-of-order FIN */
  803. if ( seq != tcp->rcv_ack )
  804. return 0;
  805. /* Acknowledge FIN */
  806. tcp_rx_seq ( tcp, 1 );
  807. /* Mark FIN as received */
  808. tcp->tcp_state |= TCP_STATE_RCVD ( TCP_FIN );
  809. /* Close connection */
  810. tcp_close ( tcp, 0 );
  811. return 0;
  812. }
  813. /**
  814. * Handle TCP received RST
  815. *
  816. * @v tcp TCP connection
  817. * @v seq SEQ value (in host-endian order)
  818. * @ret rc Return status code
  819. */
  820. static int tcp_rx_rst ( struct tcp_connection *tcp, uint32_t seq ) {
  821. /* Accept RST only if it falls within the window. If we have
  822. * not yet received a SYN, then we have no window to test
  823. * against, so fall back to checking that our SYN has been
  824. * ACKed.
  825. */
  826. if ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) {
  827. if ( ! tcp_in_window ( seq, tcp->rcv_ack, tcp->rcv_win ) )
  828. return 0;
  829. } else {
  830. if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
  831. return 0;
  832. }
  833. /* Abort connection */
  834. tcp->tcp_state = TCP_CLOSED;
  835. tcp_dump_state ( tcp );
  836. tcp_close ( tcp, -ECONNRESET );
  837. DBGC ( tcp, "TCP %p connection reset by peer\n", tcp );
  838. return -ECONNRESET;
  839. }
  840. /**
  841. * Enqueue received TCP packet
  842. *
  843. * @v tcp TCP connection
  844. * @v seq SEQ value (in host-endian order)
  845. * @v flags TCP flags
  846. * @v iobuf I/O buffer
  847. */
  848. static void tcp_rx_enqueue ( struct tcp_connection *tcp, uint32_t seq,
  849. uint8_t flags, struct io_buffer *iobuf ) {
  850. struct tcp_rx_queued_header *tcpqhdr;
  851. struct io_buffer *queued;
  852. size_t len;
  853. uint32_t seq_len;
  854. /* Calculate remaining flags and sequence length. Note that
  855. * SYN, if present, has already been processed by this point.
  856. */
  857. flags &= TCP_FIN;
  858. len = iob_len ( iobuf );
  859. seq_len = ( len + ( flags ? 1 : 0 ) );
  860. /* Discard immediately (to save memory) if:
  861. *
  862. * a) we have not yet received a SYN (and so have no defined
  863. * receive window), or
  864. * b) the packet lies entirely outside the receive window, or
  865. * c) there is no further content to process.
  866. */
  867. if ( ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) ||
  868. ( tcp_cmp ( seq, tcp->rcv_ack + tcp->rcv_win ) >= 0 ) ||
  869. ( tcp_cmp ( seq + seq_len, tcp->rcv_ack ) < 0 ) ||
  870. ( seq_len == 0 ) ) {
  871. free_iob ( iobuf );
  872. return;
  873. }
  874. /* Add internal header */
  875. tcpqhdr = iob_push ( iobuf, sizeof ( *tcpqhdr ) );
  876. tcpqhdr->seq = seq;
  877. tcpqhdr->flags = flags;
  878. /* Add to RX queue */
  879. list_for_each_entry ( queued, &tcp->rx_queue, list ) {
  880. tcpqhdr = queued->data;
  881. if ( tcp_cmp ( seq, tcpqhdr->seq ) < 0 )
  882. break;
  883. }
  884. list_add_tail ( &iobuf->list, &queued->list );
  885. }
  886. /**
  887. * Process receive queue
  888. *
  889. * @v tcp TCP connection
  890. */
  891. static void tcp_process_rx_queue ( struct tcp_connection *tcp ) {
  892. struct io_buffer *iobuf;
  893. struct tcp_rx_queued_header *tcpqhdr;
  894. uint32_t seq;
  895. unsigned int flags;
  896. size_t len;
  897. /* Process all applicable received buffers. Note that we
  898. * cannot use list_for_each_entry() to iterate over the RX
  899. * queue, since tcp_discard() may remove packets from the RX
  900. * queue while we are processing.
  901. */
  902. while ( ! list_empty ( &tcp->rx_queue ) ) {
  903. list_for_each_entry ( iobuf, &tcp->rx_queue, list )
  904. break;
  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. };