You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tcp.c 47KB

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