tcp.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. /* Minimal TCP client: see tcp.h. */
  2. #include "io.h"
  3. #include "timer.h"
  4. #include "mm.h"
  5. #include "netcore.h"
  6. #include "netdev.h"
  7. #include "net.h"
  8. #include "process.h"
  9. #include "tcp.h"
  10. /* TCP flags. */
  11. #define TCP_FIN 0x01
  12. #define TCP_SYN 0x02
  13. #define TCP_RST 0x04
  14. #define TCP_PSH 0x08
  15. #define TCP_ACK 0x10
  16. #define TCP_MSS_OURS 1460
  17. #define TCP_WINDOW 4096 /* fallback only; clients use rxq_mask */
  18. #define TCP_RETRY_MS 500
  19. #define TCP_RETRIES 3
  20. /* Client sockets: one per process, plus a little headroom. Each connection
  21. * gets a 64 KB receive queue from the frame allocator, which fills the
  22. * unscaled 16-bit window. Less is used when the allocator cannot find a
  23. * contiguous run. 64 KB / RTT instead of 4 KB / RTT is the difference
  24. * between ~130 KB/s and ~2 MB/s at a 30 ms round trip. */
  25. #define TCP_CLIENT_MAX 12
  26. #define TCP_RXQ_PAGES 16
  27. #define TCP_SRV_RXQ_SIZE 4096
  28. /* What all client connections together may have in flight towards us.
  29. * The NICs are polled, and a small RX ring (64 descriptors on the
  30. * RTL8111/8168) overflows when several fast senders each fill a 64 KB
  31. * window at once -- the browser's four parallel module downloads on the
  32. * ION. Every overflow costs the peer a retransmission timeout, because
  33. * out-of-order segments are not kept, so the loads stalled for minutes.
  34. * The budget (~32 full frames) is split evenly between the open client
  35. * connections, with a floor of four segments each; a single download
  36. * keeps ~45 KB. */
  37. #define TCP_RX_BUDGET (32u * TCP_MSS_OURS)
  38. #define TCP_RX_FLOOR (4u * TCP_MSS_OURS)
  39. static unsigned int tcp_rx_share(void);
  40. /* Listening port for the server side (0 = not listening). Declared here
  41. * because the shared receive loop feeds non-matching frames to the listener. */
  42. static unsigned int srv_port;
  43. /* Frame buffers are file scope: a 4 KB process stack cannot hold them. The
  44. * receive side uses the shared frame scratch (netdev_rx_scratch()). */
  45. static unsigned char tx_frame[14 + 20 + 20 + TCP_MSS_OURS];
  46. /* In-order payload accepted from the peer but not yet handed to the
  47. * application. Every connection owns a queue, so one socket's data can
  48. * never be read or reset by another. Data can arrive while the app is inside
  49. * a send (a TLS server sends session tickets right after the handshake), so
  50. * it must be buffered, never ACKed and dropped. A segment that does not fit
  51. * is left unacknowledged and the peer retransmits it. */
  52. void tcp_conn_init(struct tcp_conn *c, unsigned char *buf, unsigned int size)
  53. {
  54. c->rxq = buf;
  55. c->rxq_mask = size - 1;
  56. c->rxq_head = c->rxq_tail = 0;
  57. }
  58. static unsigned int rxq_len(const struct tcp_conn *c)
  59. {
  60. return (c->rxq_head - c->rxq_tail) & c->rxq_mask;
  61. }
  62. static unsigned int rxq_space(const struct tcp_conn *c)
  63. {
  64. return c->rxq_mask - rxq_len(c);
  65. }
  66. static void rxq_put(struct tcp_conn *c, const unsigned char *p,
  67. unsigned int n)
  68. {
  69. while (n--) {
  70. c->rxq[c->rxq_head] = *p++;
  71. c->rxq_head = (c->rxq_head + 1) & c->rxq_mask;
  72. }
  73. }
  74. static void rxq_get(struct tcp_conn *c, unsigned char *p, unsigned int n)
  75. {
  76. while (n--) {
  77. *p++ = c->rxq[c->rxq_tail];
  78. c->rxq_tail = (c->rxq_tail + 1) & c->rxq_mask;
  79. }
  80. }
  81. static unsigned int rd16(const unsigned char *p)
  82. {
  83. return ((unsigned int)p[0] << 8) | p[1];
  84. }
  85. static void wr16(unsigned char *p, unsigned int v)
  86. {
  87. p[0] = (unsigned char)(v >> 8);
  88. p[1] = (unsigned char)v;
  89. }
  90. static unsigned int rd32(const unsigned char *p)
  91. {
  92. return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) |
  93. ((unsigned int)p[2] << 8) | p[3];
  94. }
  95. static void wr32(unsigned char *p, unsigned int v)
  96. {
  97. p[0] = (unsigned char)(v >> 24);
  98. p[1] = (unsigned char)(v >> 16);
  99. p[2] = (unsigned char)(v >> 8);
  100. p[3] = (unsigned char)v;
  101. }
  102. /* Build and transmit one segment. `seq` is the sequence of the first byte
  103. * (for SYN/FIN that is the flag's own sequence). Returns 0 / -1 (tx). */
  104. static int tcp_send_segment_wnd(struct tcp_conn *c, unsigned char flags,
  105. unsigned int seq, unsigned int ack,
  106. const unsigned char *payload, unsigned int len,
  107. int with_mss, unsigned int wnd)
  108. {
  109. const struct netdev *nd = netdev_active();
  110. unsigned char my_mac[6], my_ip[4];
  111. unsigned char *ip = tx_frame + 14;
  112. unsigned char *tcp = ip + 20;
  113. unsigned short cs;
  114. unsigned int total, optlen = 0, i;
  115. if (!nd)
  116. return -1;
  117. nd->get_mac(my_mac);
  118. net_get_ip(my_ip);
  119. /* Ethernet. */
  120. for (i = 0; i < 6; i++)
  121. tx_frame[i] = c->dst_mac[i];
  122. for (i = 0; i < 6; i++)
  123. tx_frame[6 + i] = my_mac[i];
  124. tx_frame[12] = 0x08;
  125. tx_frame[13] = 0x00;
  126. /* IPv4 header (IHL 5, no options). */
  127. total = 20 + 20 + (with_mss ? 4 : 0) + len;
  128. for (i = 0; i < 20; i++)
  129. ip[i] = 0;
  130. ip[0] = 0x45;
  131. wr16(ip + 2, total);
  132. ip[8] = 64; /* TTL */
  133. ip[9] = IPPROTO_TCP;
  134. for (i = 0; i < 4; i++)
  135. ip[12 + i] = my_ip[i];
  136. for (i = 0; i < 4; i++)
  137. ip[16 + i] = c->dst_ip[i];
  138. cs = net_checksum(ip, 20);
  139. wr16(ip + 10, cs);
  140. /* TCP header (+ MSS option on SYN). */
  141. if (with_mss) {
  142. tcp[20] = 2; /* kind: MSS */
  143. tcp[21] = 4; /* length */
  144. wr16(tcp + 22, TCP_MSS_OURS);
  145. optlen = 4;
  146. }
  147. wr16(tcp + 0, c->sport);
  148. wr16(tcp + 2, c->dport);
  149. wr32(tcp + 4, seq);
  150. wr32(tcp + 8, ack);
  151. tcp[12] = (unsigned char)((5 + optlen / 4) << 4);
  152. tcp[13] = flags;
  153. wr16(tcp + 14, wnd > 0xFFFFu ? 0xFFFFu : wnd);
  154. wr16(tcp + 16, 0); /* checksum */
  155. wr16(tcp + 18, 0); /* urgent pointer */
  156. for (i = 0; i < len; i++)
  157. tcp[20 + optlen + i] = payload[i];
  158. cs = net_checksum_tcp(my_ip, c->dst_ip, tcp, 20 + optlen + len);
  159. wr16(tcp + 16, cs);
  160. return netdev_tx_frame(nd, tx_frame, 14 + total);
  161. }
  162. static int tcp_send_segment(struct tcp_conn *c, unsigned char flags,
  163. unsigned int seq, unsigned int ack,
  164. const unsigned char *payload, unsigned int len,
  165. int with_mss)
  166. {
  167. /* The SYN advertises the receive queue size; ACKs advertise the space
  168. * currently left in it. Both fit the 16-bit field because the queue
  169. * is at most 64 KB. */
  170. unsigned int wnd = with_mss ? (c->rxq_mask ? c->rxq_mask : TCP_WINDOW)
  171. : rxq_space(c);
  172. unsigned int share = tcp_rx_share();
  173. if (wnd > share)
  174. wnd = share;
  175. return tcp_send_segment_wnd(c, flags, seq, ack, payload, len,
  176. with_mss, wnd);
  177. }
  178. /* One received segment. */
  179. struct tcp_seg {
  180. unsigned int seq, ack;
  181. unsigned char flags;
  182. const unsigned char *data;
  183. unsigned int len;
  184. const unsigned char *tcp; /* for option parsing */
  185. unsigned int doff;
  186. };
  187. /* Parse one frame as a TCP segment from the peer. Returns 1 on match.
  188. * Incoming checksums are not validated (same as the UDP path). */
  189. static int tcp_parse(const unsigned char *f, unsigned int flen,
  190. const unsigned char *peer_ip,
  191. unsigned int peer_port, unsigned int local_port,
  192. struct tcp_seg *s)
  193. {
  194. const unsigned char *ip = f + 14;
  195. const unsigned char *tcp;
  196. unsigned char my_ip[4];
  197. unsigned int i, ihl, ip_len, doff;
  198. if (flen < 14 + 20)
  199. return 0;
  200. if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
  201. return 0;
  202. if ((ip[0] >> 4) != 4)
  203. return 0;
  204. ihl = (unsigned int)(ip[0] & 0x0F) * 4;
  205. if (ihl < 20 || ip[9] != IPPROTO_TCP)
  206. return 0;
  207. /* Fragments are not reassembled: MF or a nonzero offset. */
  208. if ((ip[6] & 0x3F) != 0 || ip[7] != 0)
  209. return 0;
  210. ip_len = rd16(ip + 2);
  211. if (ip_len < ihl + 20 || ip_len > flen - 14)
  212. return 0;
  213. net_get_ip(my_ip);
  214. for (i = 0; i < 4; i++) {
  215. if (ip[12 + i] != peer_ip[i])
  216. return 0;
  217. if (ip[16 + i] != my_ip[i])
  218. return 0;
  219. }
  220. tcp = ip + ihl;
  221. if (rd16(tcp + 0) != peer_port || rd16(tcp + 2) != local_port)
  222. return 0;
  223. doff = (unsigned int)(tcp[12] >> 4) * 4;
  224. if (doff < 20 || ihl + doff > ip_len)
  225. return 0;
  226. s->seq = rd32(tcp + 4);
  227. s->ack = rd32(tcp + 8);
  228. s->flags = tcp[13];
  229. s->data = tcp + doff;
  230. s->len = ip_len - ihl - doff;
  231. s->tcp = tcp;
  232. s->doff = doff;
  233. return 1;
  234. }
  235. /* Parse the peer's MSS option (default stays when absent); 1 if present. */
  236. static int tcp_parse_mss(const unsigned char *tcp, unsigned int doff,
  237. unsigned int *mss)
  238. {
  239. unsigned int o = 20;
  240. while (o < doff) {
  241. unsigned char kind = tcp[o];
  242. if (kind == 0) /* end of options */
  243. return 0;
  244. if (kind == 1) { /* NOP */
  245. o++;
  246. continue;
  247. }
  248. if (o + 1 >= doff)
  249. return 0;
  250. if (tcp[o + 1] < 2 || o + tcp[o + 1] > doff)
  251. return 0;
  252. if (kind == 2 && tcp[o + 1] == 4) {
  253. *mss = rd16(tcp + o + 2);
  254. return 1;
  255. }
  256. o += tcp[o + 1];
  257. }
  258. return 0;
  259. }
  260. /* Take an incoming segment's payload/FIN into the receive queue. Returns
  261. * -2 on RST, 0 otherwise. Duplicate/out-of-order data is re-ACKed to resync;
  262. * a segment that does not fit the queue is not ACKed at all. */
  263. static int tcp_rx_data(struct tcp_conn *c, const struct tcp_seg *s)
  264. {
  265. if (s->flags & TCP_RST)
  266. return -2;
  267. if (s->tcp)
  268. c->peer_wnd = rd16(s->tcp + 14);
  269. if (s->len) {
  270. if (s->seq == c->rcv_nxt && s->len <= rxq_space(c)) {
  271. rxq_put(c, s->data, s->len);
  272. c->rcv_nxt += s->len;
  273. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  274. 0, 0, 0);
  275. } else if (s->seq != c->rcv_nxt) {
  276. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  277. 0, 0, 0);
  278. }
  279. }
  280. if ((s->flags & TCP_FIN) && !c->peer_fin &&
  281. s->seq + s->len == c->rcv_nxt) {
  282. c->peer_fin = 1;
  283. c->rcv_nxt++;
  284. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  285. 0, 0, 0);
  286. }
  287. return 0;
  288. }
  289. /* ---- Client table ---- */
  290. struct tcp_client {
  291. struct tcp_conn c;
  292. unsigned int owner;
  293. int used;
  294. int err; /* last reset reason while a connect waits */
  295. };
  296. static struct tcp_client clients[TCP_CLIENT_MAX];
  297. static unsigned int tcp_rx_share(void)
  298. {
  299. unsigned int i, n = 0, share;
  300. for (i = 0; i < TCP_CLIENT_MAX; i++)
  301. if (clients[i].used)
  302. n++;
  303. share = TCP_RX_BUDGET / (n ? n : 1);
  304. return share < TCP_RX_FLOOR ? TCP_RX_FLOOR : share;
  305. }
  306. static unsigned int next_sport = 0xC000;
  307. static struct tcp_client *client_find(unsigned int pid)
  308. {
  309. unsigned int i;
  310. for (i = 0; i < TCP_CLIENT_MAX; i++)
  311. if (clients[i].used && clients[i].owner == pid)
  312. return &clients[i];
  313. return 0;
  314. }
  315. int tcp_client_count(void)
  316. {
  317. unsigned int i, n = 0;
  318. for (i = 0; i < TCP_CLIENT_MAX; i++)
  319. if (clients[i].used)
  320. n++;
  321. return (int)n;
  322. }
  323. static int ip4_eq(const unsigned char *a, const unsigned char *b)
  324. {
  325. return a[0] == b[0] && a[1] == b[1] &&
  326. a[2] == b[2] && a[3] == b[3];
  327. }
  328. static unsigned int alloc_sport(const unsigned char *dst_ip,
  329. unsigned int dport)
  330. {
  331. unsigned int i, guard = 0;
  332. for (;;) {
  333. unsigned int sport = next_sport++;
  334. int used = 0;
  335. if (next_sport < 0xC000 || next_sport > 0xFFFE)
  336. next_sport = 0xC000;
  337. for (i = 0; i < TCP_CLIENT_MAX; i++) {
  338. if (!clients[i].used)
  339. continue;
  340. if (clients[i].c.sport == sport &&
  341. clients[i].c.dport == dport &&
  342. ip4_eq(clients[i].c.dst_ip, dst_ip)) {
  343. used = 1;
  344. break;
  345. }
  346. }
  347. if (!used || ++guard > 0x3FFF)
  348. return sport;
  349. }
  350. }
  351. static struct tcp_client *client_alloc(unsigned int pid)
  352. {
  353. static const unsigned int pages[] = {
  354. TCP_RXQ_PAGES, TCP_RXQ_PAGES / 2, TCP_RXQ_PAGES / 4
  355. };
  356. unsigned int i, j;
  357. for (i = 0; i < TCP_CLIENT_MAX; i++) {
  358. struct tcp_client *cl = &clients[i];
  359. unsigned char *q = 0;
  360. unsigned int bytes = 0;
  361. if (cl->used)
  362. continue;
  363. for (j = 0; j < sizeof(pages) / sizeof(pages[0]); j++) {
  364. q = (unsigned char *)mm_alloc_pages(pages[j]);
  365. if (q) {
  366. bytes = pages[j] * PAGE_SIZE;
  367. break;
  368. }
  369. }
  370. if (!q)
  371. return 0;
  372. tcp_conn_init(&cl->c, q, bytes);
  373. cl->used = 1;
  374. cl->owner = pid;
  375. cl->err = 0;
  376. return cl;
  377. }
  378. return 0;
  379. }
  380. static void client_free(struct tcp_client *cl)
  381. {
  382. if (cl->c.rxq) {
  383. mm_free_range((unsigned int)cl->c.rxq, cl->c.rxq_mask + 1);
  384. cl->c.rxq = 0;
  385. cl->c.rxq_mask = 0;
  386. }
  387. cl->used = 0;
  388. cl->owner = 0;
  389. cl->err = 0;
  390. cl->c.state = TCP_STATE_CLOSED;
  391. }
  392. /* Feed one frame to the client table. See tcp.h. */
  393. int tcp_client_input(const unsigned char *frame, unsigned int len)
  394. {
  395. unsigned int i;
  396. for (i = 0; i < TCP_CLIENT_MAX; i++) {
  397. struct tcp_client *cl = &clients[i];
  398. struct tcp_conn *c = &cl->c;
  399. struct tcp_seg s;
  400. if (!cl->used)
  401. continue;
  402. if (!tcp_parse(frame, len, c->dst_ip, c->dport, c->sport, &s))
  403. continue;
  404. if (s.flags & TCP_RST) {
  405. c->state = TCP_STATE_CLOSED;
  406. cl->err = -2;
  407. return 1;
  408. }
  409. if (c->state == TCP_STATE_SYN_SENT) {
  410. if ((s.flags & (TCP_SYN | TCP_ACK)) ==
  411. (TCP_SYN | TCP_ACK) &&
  412. s.ack == c->snd_nxt + 1) {
  413. unsigned int mss;
  414. c->rcv_nxt = s.seq + 1;
  415. c->snd_nxt++;
  416. c->snd_una = c->snd_nxt;
  417. c->peer_wnd = rd16(s.tcp + 14);
  418. if (tcp_parse_mss(s.tcp, s.doff, &mss) &&
  419. mss >= 64 && mss <= TCP_MSS_OURS)
  420. c->peer_mss = mss;
  421. tcp_send_segment(c, TCP_ACK, c->snd_nxt,
  422. c->rcv_nxt, 0, 0, 0);
  423. c->state = TCP_STATE_ESTABLISHED;
  424. cl->err = 0;
  425. }
  426. return 1;
  427. }
  428. if (c->state != TCP_STATE_ESTABLISHED &&
  429. c->state != TCP_STATE_FIN_WAIT)
  430. return 1;
  431. if (c->need_wnd && (s.flags & TCP_ACK)) {
  432. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  433. 0, 0, 0);
  434. c->need_wnd = 0;
  435. }
  436. if ((unsigned)(s.ack - c->snd_una) <=
  437. (unsigned)(c->snd_nxt - c->snd_una))
  438. c->snd_una = s.ack;
  439. c->peer_wnd = rd16(s.tcp + 14);
  440. if (tcp_rx_data(c, &s) == -2) {
  441. c->state = TCP_STATE_CLOSED;
  442. cl->err = -2;
  443. }
  444. return 1;
  445. }
  446. return 0;
  447. }
  448. int tcp_client_connect(unsigned int pid, const unsigned char mac[6],
  449. const unsigned char ip[4], unsigned int port)
  450. {
  451. const struct netdev *nd = netdev_active();
  452. struct tcp_client *cl;
  453. struct tcp_conn *c;
  454. unsigned int tries, i;
  455. if (client_find(pid))
  456. return -4;
  457. if (netdev_in_use())
  458. return -4;
  459. if (!nd || !net_is_configured())
  460. return -3;
  461. cl = client_alloc(pid);
  462. if (!cl)
  463. return -4;
  464. c = &cl->c;
  465. for (i = 0; i < 6; i++)
  466. c->dst_mac[i] = mac[i];
  467. for (i = 0; i < 4; i++)
  468. c->dst_ip[i] = ip[i];
  469. c->dport = port;
  470. c->sport = alloc_sport(ip, port);
  471. c->snd_una = c->snd_nxt =
  472. (get_ticks_ms() << 16) ^ (c->sport << 8) ^ 0x5EED0000u;
  473. c->rcv_nxt = 0;
  474. c->peer_mss = 536;
  475. c->peer_wnd = 4096;
  476. c->peer_fin = 0;
  477. c->need_wnd = 0;
  478. c->state = TCP_STATE_SYN_SENT;
  479. cl->err = 0;
  480. for (tries = 0; tries < TCP_RETRIES; tries++) {
  481. unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
  482. unsigned int flags;
  483. int r;
  484. /* A SYN-ACK may have arrived just after the previous
  485. * deadline check; do not send a second SYN on an open
  486. * connection. */
  487. if (c->state == TCP_STATE_ESTABLISHED)
  488. return 0;
  489. if (c->state == TCP_STATE_CLOSED) {
  490. r = cl->err ? cl->err : -2;
  491. client_free(cl);
  492. return r;
  493. }
  494. flags = net_irq_enter();
  495. r = tcp_send_segment(c, TCP_SYN, c->snd_nxt, 0, 0, 0, 1);
  496. net_irq_exit(flags);
  497. if (r != 0) {
  498. client_free(cl);
  499. return -3;
  500. }
  501. while ((int)(get_ticks_ms() - deadline) < 0) {
  502. net_pump_burst(64);
  503. if (c->state == TCP_STATE_ESTABLISHED)
  504. return 0;
  505. if (c->state == TCP_STATE_CLOSED) {
  506. r = cl->err ? cl->err : -2;
  507. client_free(cl);
  508. return r;
  509. }
  510. process_sleep_ms(1);
  511. }
  512. }
  513. if (c->state == TCP_STATE_ESTABLISHED)
  514. return 0;
  515. if (c->state == TCP_STATE_CLOSED) {
  516. int err = cl->err ? cl->err : -2;
  517. client_free(cl);
  518. return err;
  519. }
  520. client_free(cl);
  521. return -1;
  522. }
  523. int tcp_client_send(unsigned int pid, const void *buf, unsigned int len)
  524. {
  525. struct tcp_client *cl = client_find(pid);
  526. struct tcp_conn *c;
  527. unsigned int seq;
  528. unsigned int tries;
  529. if (!cl)
  530. return -4;
  531. c = &cl->c;
  532. if (c->state != TCP_STATE_ESTABLISHED)
  533. return -4;
  534. if (len > c->peer_mss)
  535. return -3;
  536. seq = c->snd_nxt;
  537. c->snd_nxt = seq + len;
  538. for (tries = 0; tries < TCP_RETRIES; tries++) {
  539. unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
  540. unsigned int flags;
  541. int r;
  542. /* An ACK may have arrived just after the previous deadline
  543. * check; never resend the payload at a fresh sequence. */
  544. if ((int)(c->snd_una - (seq + len)) >= 0)
  545. return 0;
  546. flags = net_irq_enter();
  547. r = tcp_send_segment(c, TCP_PSH | TCP_ACK, seq, c->rcv_nxt,
  548. (const unsigned char *)buf, len, 0);
  549. net_irq_exit(flags);
  550. if (r != 0) {
  551. c->state = TCP_STATE_CLOSED;
  552. cl->err = -3;
  553. return -3;
  554. }
  555. while ((int)(get_ticks_ms() - deadline) < 0) {
  556. net_pump_burst(64);
  557. if (c->state == TCP_STATE_CLOSED)
  558. return cl->err ? cl->err : -2;
  559. if ((int)(c->snd_una - (seq + len)) >= 0)
  560. return 0;
  561. process_sleep_ms(1);
  562. }
  563. /* Retransmit the whole segment at the same sequence. */
  564. }
  565. c->state = TCP_STATE_CLOSED;
  566. cl->err = -1;
  567. return -1;
  568. }
  569. int tcp_client_recv(unsigned int pid, void *vbuf, unsigned int max,
  570. unsigned int deadline_ms)
  571. {
  572. struct tcp_client *cl = client_find(pid);
  573. struct tcp_conn *c;
  574. unsigned char *buf = (unsigned char *)vbuf;
  575. if (!cl)
  576. return -4;
  577. c = &cl->c;
  578. for (;;) {
  579. if (rxq_len(c)) {
  580. unsigned int n = rxq_len(c);
  581. unsigned int flags;
  582. if (n > max)
  583. n = max;
  584. rxq_get(c, buf, n);
  585. flags = net_irq_enter();
  586. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  587. 0, 0, 0);
  588. net_irq_exit(flags);
  589. return (int)n;
  590. }
  591. if (c->peer_fin)
  592. return 0;
  593. if (c->state == TCP_STATE_CLOSED)
  594. return cl->err ? cl->err : -2;
  595. if ((int)(get_ticks_ms() - deadline_ms) >= 0)
  596. return -1;
  597. net_pump_burst(64);
  598. if (!rxq_len(c) && c->state != TCP_STATE_CLOSED)
  599. process_sleep_ms(1);
  600. }
  601. }
  602. void tcp_client_close(unsigned int pid)
  603. {
  604. struct tcp_client *cl = client_find(pid);
  605. struct tcp_conn *c;
  606. if (!cl)
  607. return;
  608. c = &cl->c;
  609. /* Send our FIN and free the slot at once. Every send is synchronous,
  610. * so all our data is already acknowledged; waiting for the peer's FIN
  611. * only cost a round trip (up to 500 ms) per closed connection, which
  612. * every redirect hop and helper teardown paid. A late FIN/ACK
  613. * matches no client (source ports rotate) and is dropped. */
  614. if (c->state == TCP_STATE_ESTABLISHED) {
  615. unsigned int flags;
  616. flags = net_irq_enter();
  617. tcp_send_segment(c, TCP_FIN | TCP_ACK, c->snd_nxt,
  618. c->rcv_nxt, 0, 0, 0);
  619. net_irq_exit(flags);
  620. }
  621. client_free(cl);
  622. }
  623. int tcp_client_peer_mss(unsigned int pid)
  624. {
  625. struct tcp_client *cl = client_find(pid);
  626. return cl ? (int)cl->c.peer_mss : -1;
  627. }
  628. void tcp_client_reap(unsigned int pid)
  629. {
  630. unsigned int i;
  631. for (i = 0; i < TCP_CLIENT_MAX; i++) {
  632. struct tcp_client *cl = &clients[i];
  633. struct tcp_conn *c = &cl->c;
  634. if (!cl->used || cl->owner != pid)
  635. continue;
  636. if (c->state != TCP_STATE_CLOSED) {
  637. unsigned int flags = net_irq_enter();
  638. tcp_send_segment(c, TCP_RST | TCP_ACK, c->snd_nxt,
  639. c->rcv_nxt, 0, 0, 0);
  640. net_irq_exit(flags);
  641. }
  642. client_free(cl);
  643. }
  644. }
  645. /* Generic receive used by the WebDAV server, which owns the raw NIC lease
  646. * and therefore polls the device directly instead of net_pump(). */
  647. int tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
  648. unsigned int deadline_ms)
  649. {
  650. const struct netdev *nd = netdev_active();
  651. unsigned char *rxbuf = netdev_rx_scratch();
  652. if (!nd || !c || !c->rxq)
  653. return -2;
  654. for (;;) {
  655. struct tcp_seg s;
  656. if (rxq_len(c)) {
  657. unsigned int n = rxq_len(c);
  658. if (n > maxlen)
  659. n = maxlen;
  660. rxq_get(c, out, n);
  661. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  662. 0, 0, 0);
  663. return (int)n;
  664. }
  665. if (c->peer_fin)
  666. return 0;
  667. if ((int)(get_ticks_ms() - deadline_ms) >= 0)
  668. return -1;
  669. {
  670. int len = netdev_rx_frame(nd, rxbuf, NETDEV_FRAME_MAX);
  671. if (len < 0)
  672. continue;
  673. if (!tcp_parse(rxbuf, (unsigned int)len, c->dst_ip,
  674. c->dport, c->sport, &s)) {
  675. if (srv_port)
  676. tcp_srv_input(rxbuf, (unsigned int)len);
  677. continue;
  678. }
  679. }
  680. if ((unsigned)(s.ack - c->snd_una) <=
  681. (unsigned)(c->snd_nxt - c->snd_una))
  682. c->snd_una = s.ack;
  683. if (c->need_wnd && (s.flags & TCP_ACK)) {
  684. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  685. 0, 0, 0);
  686. c->need_wnd = 0;
  687. }
  688. if (tcp_rx_data(c, &s) == -2)
  689. return -2;
  690. }
  691. }
  692. /* ---- Server side ----
  693. *
  694. * One listening port, one active connection, two slots that hold a
  695. * handshake-complete connection until the command gets around to accepting
  696. * it. A closed connection lingers for a few seconds so the peer's FIN/ACK
  697. * is absorbed instead of answered with RST (libsoup treats a reset after a
  698. * complete response as an error). */
  699. #define TCP_SRV_SLOTS 4 /* 1 active + 3 backlog/closing */
  700. #define TCP_SRV_CLOSE_MS 2000
  701. #define TCP_SRV_BACKLOG_MS 30000
  702. enum {
  703. SRV_FREE = 0,
  704. SRV_BACKLOG,
  705. SRV_ACTIVE,
  706. SRV_CLOSING
  707. };
  708. #define SRV_PRE_MAX 1024
  709. struct tcp_srv {
  710. struct tcp_conn c;
  711. int used;
  712. int state;
  713. unsigned int last_ms;
  714. unsigned char pre[SRV_PRE_MAX]; /* first request, held until accept */
  715. unsigned int pre_len;
  716. };
  717. static struct tcp_srv srv[TCP_SRV_SLOTS];
  718. static int srv_active = -1;
  719. /* Only one server connection is active at a time, so the listener shares one
  720. * receive queue instead of embedding a 4 KB buffer in every slot. */
  721. static unsigned char srv_rxq[TCP_SRV_RXQ_SIZE];
  722. struct tcp_rx {
  723. unsigned int seq, ack;
  724. unsigned char flags;
  725. const unsigned char *data;
  726. unsigned int len;
  727. const unsigned char *tcp;
  728. unsigned int doff;
  729. unsigned char src_mac[6];
  730. unsigned char src_ip[4];
  731. unsigned int sport, dport;
  732. };
  733. /* Parse any IPv4/TCP frame addressed to us (no connection match). */
  734. static int tcp_parse_any(const unsigned char *f, unsigned int flen,
  735. struct tcp_rx *s)
  736. {
  737. const unsigned char *ip = f + 14;
  738. const unsigned char *tcp;
  739. unsigned char my_ip[4];
  740. unsigned int i, ihl, ip_len, doff;
  741. if (flen < 14 + 20)
  742. return 0;
  743. if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
  744. return 0;
  745. if ((ip[0] >> 4) != 4)
  746. return 0;
  747. ihl = (unsigned int)(ip[0] & 0x0F) * 4;
  748. if (ihl < 20 || ip[9] != IPPROTO_TCP)
  749. return 0;
  750. if ((ip[6] & 0x3F) != 0 || ip[7] != 0)
  751. return 0;
  752. ip_len = rd16(ip + 2);
  753. if (ip_len < ihl + 20 || ip_len > flen - 14)
  754. return 0;
  755. net_get_ip(my_ip);
  756. for (i = 0; i < 4; i++)
  757. if (ip[16 + i] != my_ip[i])
  758. return 0;
  759. tcp = ip + ihl;
  760. doff = (unsigned int)(tcp[12] >> 4) * 4;
  761. if (doff < 20 || ihl + doff > ip_len)
  762. return 0;
  763. for (i = 0; i < 6; i++)
  764. s->src_mac[i] = f[6 + i];
  765. for (i = 0; i < 4; i++)
  766. s->src_ip[i] = ip[12 + i];
  767. s->sport = rd16(tcp);
  768. s->dport = rd16(tcp + 2);
  769. s->seq = rd32(tcp + 4);
  770. s->ack = rd32(tcp + 8);
  771. s->flags = tcp[13];
  772. s->data = tcp + doff;
  773. s->len = ip_len - ihl - doff;
  774. s->tcp = tcp;
  775. s->doff = doff;
  776. return 1;
  777. }
  778. static int srv_match(const struct tcp_conn *c, const struct tcp_rx *s)
  779. {
  780. unsigned int i;
  781. if (s->sport != c->dport || s->dport != c->sport)
  782. return 0;
  783. for (i = 0; i < 4; i++)
  784. if (s->src_ip[i] != c->dst_ip[i])
  785. return 0;
  786. return 1;
  787. }
  788. static void srv_clear(int i)
  789. {
  790. srv[i].used = 0;
  791. srv[i].state = SRV_FREE;
  792. srv[i].pre_len = 0;
  793. }
  794. static void srv_expire(void)
  795. {
  796. unsigned int now = get_ticks_ms();
  797. int i;
  798. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  799. if (!srv[i].used)
  800. continue;
  801. if (srv[i].state == SRV_CLOSING &&
  802. now - srv[i].last_ms >= TCP_SRV_CLOSE_MS) {
  803. srv_clear(i);
  804. if (srv_active == i)
  805. srv_active = -1;
  806. } else if (srv[i].state == SRV_BACKLOG &&
  807. now - srv[i].last_ms >= TCP_SRV_BACKLOG_MS) {
  808. srv_clear(i);
  809. }
  810. }
  811. }
  812. static unsigned int srv_isn(void)
  813. {
  814. return (get_ticks_ms() * 1103515245u) ^ 0x5EED0000u;
  815. }
  816. static void srv_resend_synack(struct tcp_srv *e)
  817. {
  818. tcp_send_segment_wnd(&e->c, TCP_SYN | TCP_ACK, e->c.snd_una - 1,
  819. e->c.rcv_nxt, 0, 0, 1, 0);
  820. }
  821. /* Build a connection from a SYN and answer SYN-ACK. */
  822. static int srv_new(const struct tcp_rx *s)
  823. {
  824. struct tcp_srv *e;
  825. unsigned int mss = 536, i;
  826. int slot = -1;
  827. /* Retransmitted SYN: answer with the same ISN. */
  828. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  829. if (!srv[i].used || srv[i].state == SRV_CLOSING)
  830. continue;
  831. if (srv_match(&srv[i].c, s)) {
  832. srv_resend_synack(&srv[i]);
  833. return 1;
  834. }
  835. }
  836. srv_expire();
  837. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  838. /* A connection that already sent its response is finished
  839. * as far as new clients are concerned: reuse the slot rather
  840. * than make the next SYN wait for the linger to expire. */
  841. if (!srv[i].used || srv[i].state == SRV_CLOSING) {
  842. slot = (int)i;
  843. break;
  844. }
  845. }
  846. if (slot < 0)
  847. return 1; /* no room: drop, the client retries */
  848. e = &srv[slot];
  849. for (i = 0; i < 6; i++)
  850. e->c.dst_mac[i] = s->src_mac[i];
  851. for (i = 0; i < 4; i++)
  852. e->c.dst_ip[i] = s->src_ip[i];
  853. e->c.sport = srv_port;
  854. e->c.dport = s->sport;
  855. e->c.snd_una = e->c.snd_nxt = srv_isn();
  856. e->c.rcv_nxt = s->seq + 1;
  857. e->c.peer_fin = 0;
  858. e->c.need_wnd = 0;
  859. e->c.peer_mss = 536;
  860. e->c.peer_wnd = rd16(s->tcp + 14);
  861. if (!e->c.peer_wnd)
  862. e->c.peer_wnd = 4096;
  863. if (tcp_parse_mss(s->tcp, s->doff, &mss) &&
  864. mss >= 64 && mss <= TCP_MSS_OURS)
  865. e->c.peer_mss = mss;
  866. e->used = 1;
  867. e->state = SRV_BACKLOG;
  868. e->last_ms = get_ticks_ms();
  869. e->pre_len = 0;
  870. tcp_send_segment_wnd(&e->c, TCP_SYN | TCP_ACK, e->c.snd_nxt,
  871. e->c.rcv_nxt, 0, 0, 1, 0);
  872. /* The SYN consumed one sequence number. */
  873. e->c.snd_nxt++;
  874. e->c.snd_una = e->c.snd_nxt;
  875. return 1;
  876. }
  877. static void srv_send_rst(const struct tcp_rx *s)
  878. {
  879. struct tcp_conn c;
  880. unsigned int ack = s->seq + s->len;
  881. unsigned int i;
  882. if (s->flags & TCP_SYN)
  883. ack++;
  884. if (s->flags & TCP_FIN)
  885. ack++;
  886. for (i = 0; i < 6; i++)
  887. c.dst_mac[i] = s->src_mac[i];
  888. for (i = 0; i < 4; i++)
  889. c.dst_ip[i] = s->src_ip[i];
  890. c.sport = s->dport;
  891. c.dport = s->sport;
  892. c.snd_una = c.snd_nxt = s->ack;
  893. c.rcv_nxt = ack;
  894. c.peer_mss = 536;
  895. c.peer_fin = 0;
  896. tcp_send_segment_wnd(&c, TCP_RST | TCP_ACK, s->ack, ack, 0, 0, 0, 0);
  897. }
  898. static void srv_rx_data(struct tcp_conn *c, const struct tcp_rx *r)
  899. {
  900. struct tcp_seg s;
  901. s.seq = r->seq;
  902. s.ack = r->ack;
  903. s.flags = r->flags;
  904. s.data = r->data;
  905. s.len = r->len;
  906. s.tcp = r->tcp;
  907. s.doff = r->doff;
  908. tcp_rx_data(c, &s);
  909. }
  910. void tcp_srv_init(unsigned int port)
  911. {
  912. int i;
  913. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  914. srv[i].used = 0;
  915. srv[i].state = SRV_FREE;
  916. tcp_conn_init(&srv[i].c, srv_rxq, sizeof(srv_rxq));
  917. }
  918. srv_active = -1;
  919. srv_port = port;
  920. }
  921. void tcp_srv_reset(void)
  922. {
  923. tcp_srv_init(0);
  924. }
  925. int tcp_srv_input(const unsigned char *f, unsigned int len)
  926. {
  927. struct tcp_rx s;
  928. int i;
  929. if (!srv_port || !tcp_parse_any(f, len, &s))
  930. return 0;
  931. /* The accepted connection. */
  932. if (srv_active >= 0 && srv_match(&srv[srv_active].c, &s)) {
  933. struct tcp_conn *c = &srv[srv_active].c;
  934. if (c->need_wnd && (s.flags & TCP_ACK)) {
  935. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  936. 0, 0, 0);
  937. c->need_wnd = 0;
  938. }
  939. if (s.flags & TCP_RST) {
  940. srv_clear(srv_active);
  941. srv_active = -1;
  942. return 1;
  943. }
  944. if ((unsigned)(s.ack - c->snd_una) <=
  945. (unsigned)(c->snd_nxt - c->snd_una))
  946. c->snd_una = s.ack;
  947. srv_rx_data(c, &s);
  948. srv[srv_active].last_ms = get_ticks_ms();
  949. return 1;
  950. }
  951. /* A pending, closing or duplicate connection. */
  952. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  953. if (!srv[i].used || !srv_match(&srv[i].c, &s))
  954. continue;
  955. if (srv[i].state == SRV_CLOSING) {
  956. if (s.flags & TCP_FIN) {
  957. srv[i].c.rcv_nxt++;
  958. tcp_send_segment(&srv[i].c, TCP_ACK,
  959. srv[i].c.snd_nxt,
  960. srv[i].c.rcv_nxt, 0, 0, 0);
  961. }
  962. if (s.flags & TCP_RST)
  963. srv_clear(i);
  964. return 1;
  965. }
  966. if (s.flags & TCP_RST) {
  967. srv_clear(i);
  968. return 1;
  969. }
  970. if ((s.flags & (TCP_SYN | TCP_ACK | TCP_RST)) == TCP_SYN) {
  971. srv_resend_synack(&srv[i]);
  972. return 1;
  973. }
  974. if ((unsigned)(s.ack - srv[i].c.snd_una) <=
  975. (unsigned)(srv[i].c.snd_nxt - srv[i].c.snd_una))
  976. srv[i].c.snd_una = s.ack;
  977. /* Hold the first request (small HTTP headers) until the
  978. * command accepts this connection. ACK it so the client does
  979. * not have to wait for an RTO; if it does not fit, leave it
  980. * unacknowledged and the client retransmits after accept. */
  981. if (s.len && s.seq == srv[i].c.rcv_nxt &&
  982. s.len <= SRV_PRE_MAX - srv[i].pre_len) {
  983. unsigned int k;
  984. for (k = 0; k < s.len; k++)
  985. srv[i].pre[srv[i].pre_len + k] = s.data[k];
  986. srv[i].pre_len += s.len;
  987. srv[i].c.rcv_nxt += s.len;
  988. if ((s.flags & TCP_FIN) &&
  989. s.seq + s.len == srv[i].c.rcv_nxt) {
  990. srv[i].c.peer_fin = 1;
  991. srv[i].c.rcv_nxt++;
  992. }
  993. /* Keep the queued connection's window closed so it
  994. * does not send a body we have nowhere to put. */
  995. tcp_send_segment_wnd(&srv[i].c, TCP_ACK,
  996. srv[i].c.snd_nxt, srv[i].c.rcv_nxt,
  997. 0, 0, 0, 0);
  998. }
  999. srv[i].last_ms = get_ticks_ms();
  1000. return 1;
  1001. }
  1002. /* A new connection. */
  1003. if ((s.flags & (TCP_SYN | TCP_ACK | TCP_RST)) == TCP_SYN &&
  1004. s.dport == srv_port)
  1005. return srv_new(&s);
  1006. /* TCP for us on a port we do not serve: refuse it. */
  1007. srv_send_rst(&s);
  1008. return 1;
  1009. }
  1010. struct tcp_conn *tcp_srv_accept(void)
  1011. {
  1012. int i, best = -1;
  1013. if (srv_active >= 0)
  1014. return &srv[srv_active].c;
  1015. srv_expire();
  1016. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  1017. if (!srv[i].used || srv[i].state != SRV_BACKLOG)
  1018. continue;
  1019. if (best < 0 ||
  1020. (int)(srv[i].last_ms - srv[best].last_ms) < 0)
  1021. best = i;
  1022. }
  1023. if (best < 0)
  1024. return 0;
  1025. srv[best].state = SRV_ACTIVE;
  1026. srv_active = best;
  1027. srv[best].c.rxq_head = srv[best].c.rxq_tail = 0;
  1028. if (srv[best].pre_len) {
  1029. rxq_put(&srv[best].c, srv[best].pre, srv[best].pre_len);
  1030. srv[best].pre_len = 0;
  1031. }
  1032. /* The SYN-ACK advertised a zero window while this connection waited
  1033. * for the command; open it now with the real queue space. */
  1034. tcp_send_segment(&srv[best].c, TCP_ACK, srv[best].c.snd_nxt,
  1035. srv[best].c.rcv_nxt, 0, 0, 0);
  1036. /* If this update raced ahead of the client's final ACK it may have
  1037. * been discarded; repeat it when that ACK reaches us. */
  1038. srv[best].c.need_wnd = 1;
  1039. return &srv[best].c;
  1040. }
  1041. /* Send up to four segments before waiting for the cumulative ACK. Linux
  1042. * delays the ACK of a lone small segment by up to 40 ms; a burst makes the
  1043. * peer acknowledge every second segment immediately, which is the difference
  1044. * between ~35 KB/s and a few MB/s on a LAN. The peer's advertised window
  1045. * caps the burst. */
  1046. int tcp_srv_send(struct tcp_conn *c, const void *data, unsigned int len)
  1047. {
  1048. const struct netdev *nd = netdev_active();
  1049. unsigned char *rxbuf = netdev_rx_scratch();
  1050. const unsigned char *p = data;
  1051. unsigned int mss;
  1052. if (!c || !nd)
  1053. return -3;
  1054. mss = c->peer_mss;
  1055. if (mss < 64)
  1056. mss = 64;
  1057. while (len) {
  1058. struct {
  1059. unsigned int seq;
  1060. unsigned int len;
  1061. const unsigned char *data;
  1062. } seg[4];
  1063. unsigned int nseg = 0, off = 0, total = 0, cap, wnd;
  1064. unsigned int tries, i;
  1065. wnd = c->peer_wnd ? c->peer_wnd : 4096;
  1066. cap = mss * 4;
  1067. if (cap > wnd)
  1068. cap = wnd;
  1069. if (cap < mss)
  1070. cap = mss;
  1071. while (off < len && nseg < 4 && total < cap) {
  1072. unsigned int n = len - off;
  1073. if (n > mss)
  1074. n = mss;
  1075. if (total + n > cap)
  1076. n = cap - total;
  1077. if (!n)
  1078. break;
  1079. seg[nseg].seq = c->snd_nxt;
  1080. seg[nseg].len = n;
  1081. seg[nseg].data = p + off;
  1082. if (tcp_send_segment(c, TCP_PSH | TCP_ACK, c->snd_nxt,
  1083. c->rcv_nxt, p + off, n, 0) != 0)
  1084. return -3;
  1085. c->snd_nxt += n;
  1086. off += n;
  1087. total += n;
  1088. nseg++;
  1089. }
  1090. for (tries = 0; ; tries++) {
  1091. unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
  1092. while ((int)(get_ticks_ms() - deadline) < 0) {
  1093. struct tcp_seg s;
  1094. int rlen = netdev_rx_frame(nd, rxbuf, NETDEV_FRAME_MAX);
  1095. if (rlen < 0)
  1096. continue;
  1097. if (!tcp_parse(rxbuf, (unsigned int)rlen,
  1098. c->dst_ip, c->dport, c->sport, &s)) {
  1099. if (srv_port)
  1100. tcp_srv_input(rxbuf,
  1101. (unsigned int)rlen);
  1102. continue;
  1103. }
  1104. if (s.flags & TCP_RST)
  1105. return -2;
  1106. if ((unsigned)(s.ack - c->snd_una) <=
  1107. (unsigned)(c->snd_nxt - c->snd_una))
  1108. c->snd_una = s.ack;
  1109. c->peer_wnd = rd16(s.tcp + 14);
  1110. if ((int)(s.ack - c->snd_nxt) >= 0)
  1111. goto burst_done;
  1112. tcp_rx_data(c, &s);
  1113. }
  1114. if (tries >= TCP_RETRIES)
  1115. return -1;
  1116. /* Retransmit the still-unacknowledged part of this
  1117. * burst (duplicates are harmless). */
  1118. for (i = 0; i < nseg; i++) {
  1119. if ((int)(seg[i].seq + seg[i].len -
  1120. c->snd_una) <= 0)
  1121. continue;
  1122. tcp_send_segment(c, TCP_PSH | TCP_ACK,
  1123. seg[i].seq, c->rcv_nxt,
  1124. seg[i].data, seg[i].len, 0);
  1125. }
  1126. }
  1127. burst_done:
  1128. p += off;
  1129. len -= off;
  1130. }
  1131. return 0;
  1132. }
  1133. void tcp_srv_close(struct tcp_conn *c)
  1134. {
  1135. int i;
  1136. if (!c)
  1137. return;
  1138. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  1139. if (!srv[i].used || &srv[i].c != c)
  1140. continue;
  1141. tcp_send_segment(c, TCP_FIN | TCP_ACK, c->snd_nxt, c->rcv_nxt,
  1142. 0, 0, 0);
  1143. c->snd_nxt++;
  1144. srv[i].state = SRV_CLOSING;
  1145. srv[i].last_ms = get_ticks_ms();
  1146. if (srv_active == i)
  1147. srv_active = -1;
  1148. return;
  1149. }
  1150. }