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

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