tcp.c 31 KB

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