tcp.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /* Minimal TCP client: see tcp.h. */
  2. #include "io.h"
  3. #include "timer.h"
  4. #include "netdev.h"
  5. #include "net.h"
  6. #include "tcp.h"
  7. /* TCP flags. */
  8. #define TCP_FIN 0x01
  9. #define TCP_SYN 0x02
  10. #define TCP_RST 0x04
  11. #define TCP_PSH 0x08
  12. #define TCP_ACK 0x10
  13. #define TCP_MSS_OURS 1460
  14. #define TCP_WINDOW 4096
  15. #define TCP_RETRY_MS 500
  16. #define TCP_RETRIES 3
  17. /* Listening port for the server side (0 = not listening). Declared here
  18. * because the shared receive loop feeds non-matching frames to the listener. */
  19. static unsigned int srv_port;
  20. /* Frame buffers are file scope: a 4 KB process stack cannot hold them. The
  21. * receive side uses the shared frame scratch (netdev_rx_scratch()). */
  22. static unsigned char tx_frame[14 + 20 + 20 + TCP_MSS_OURS];
  23. /* Receive queue: in-order payload accepted from the peer but not yet handed
  24. * to the application. Data can arrive while the app is inside tcp_send()
  25. * (a TLS server sends session tickets right after the handshake), so it must
  26. * be buffered, never ACKed and dropped. A segment that does not fit is left
  27. * unacknowledged and the peer retransmits it. */
  28. #define TCP_RXQ_SIZE 8192
  29. static unsigned char rxq[TCP_RXQ_SIZE];
  30. static unsigned int rxq_head, rxq_tail;
  31. static unsigned int rxq_len(void)
  32. {
  33. return (rxq_head - rxq_tail) & (TCP_RXQ_SIZE - 1);
  34. }
  35. static unsigned int rxq_space(void)
  36. {
  37. return TCP_RXQ_SIZE - 1 - rxq_len();
  38. }
  39. static void rxq_put(const unsigned char *p, unsigned int n)
  40. {
  41. while (n--) {
  42. rxq[rxq_head] = *p++;
  43. rxq_head = (rxq_head + 1) & (TCP_RXQ_SIZE - 1);
  44. }
  45. }
  46. static void rxq_get(unsigned char *p, unsigned int n)
  47. {
  48. while (n--) {
  49. *p++ = rxq[rxq_tail];
  50. rxq_tail = (rxq_tail + 1) & (TCP_RXQ_SIZE - 1);
  51. }
  52. }
  53. static unsigned int rd16(const unsigned char *p)
  54. {
  55. return ((unsigned int)p[0] << 8) | p[1];
  56. }
  57. static void wr16(unsigned char *p, unsigned int v)
  58. {
  59. p[0] = (unsigned char)(v >> 8);
  60. p[1] = (unsigned char)v;
  61. }
  62. static unsigned int rd32(const unsigned char *p)
  63. {
  64. return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) |
  65. ((unsigned int)p[2] << 8) | p[3];
  66. }
  67. static void wr32(unsigned char *p, unsigned int v)
  68. {
  69. p[0] = (unsigned char)(v >> 24);
  70. p[1] = (unsigned char)(v >> 16);
  71. p[2] = (unsigned char)(v >> 8);
  72. p[3] = (unsigned char)v;
  73. }
  74. /* Build and transmit one segment. `seq` is the sequence of the first byte
  75. * (for SYN/FIN that is the flag's own sequence). Returns 0 / -1 (tx). */
  76. static int tcp_send_segment_wnd(struct tcp_conn *c, unsigned char flags,
  77. unsigned int seq, unsigned int ack,
  78. const unsigned char *payload, unsigned int len,
  79. int with_mss, unsigned int wnd)
  80. {
  81. const struct netdev *nd = netdev_active();
  82. unsigned char my_mac[6], my_ip[4];
  83. unsigned char *ip = tx_frame + 14;
  84. unsigned char *tcp = ip + 20;
  85. unsigned short cs;
  86. unsigned int total, optlen = 0, i;
  87. if (!nd)
  88. return -1;
  89. nd->get_mac(my_mac);
  90. net_get_ip(my_ip);
  91. /* Ethernet. */
  92. for (i = 0; i < 6; i++)
  93. tx_frame[i] = c->dst_mac[i];
  94. for (i = 0; i < 6; i++)
  95. tx_frame[6 + i] = my_mac[i];
  96. tx_frame[12] = 0x08;
  97. tx_frame[13] = 0x00;
  98. /* IPv4 header (IHL 5, no options). */
  99. total = 20 + 20 + (with_mss ? 4 : 0) + len;
  100. for (i = 0; i < 20; i++)
  101. ip[i] = 0;
  102. ip[0] = 0x45;
  103. wr16(ip + 2, total);
  104. ip[8] = 64; /* TTL */
  105. ip[9] = IPPROTO_TCP;
  106. for (i = 0; i < 4; i++)
  107. ip[12 + i] = my_ip[i];
  108. for (i = 0; i < 4; i++)
  109. ip[16 + i] = c->dst_ip[i];
  110. cs = net_checksum(ip, 20);
  111. wr16(ip + 10, cs);
  112. /* TCP header (+ MSS option on SYN). */
  113. if (with_mss) {
  114. tcp[20] = 2; /* kind: MSS */
  115. tcp[21] = 4; /* length */
  116. wr16(tcp + 22, TCP_MSS_OURS);
  117. optlen = 4;
  118. }
  119. wr16(tcp + 0, c->sport);
  120. wr16(tcp + 2, c->dport);
  121. wr32(tcp + 4, seq);
  122. wr32(tcp + 8, ack);
  123. tcp[12] = (unsigned char)((5 + optlen / 4) << 4);
  124. tcp[13] = flags;
  125. wr16(tcp + 14, wnd > 0xFFFFu ? 0xFFFFu : wnd);
  126. wr16(tcp + 16, 0); /* checksum */
  127. wr16(tcp + 18, 0); /* urgent pointer */
  128. for (i = 0; i < len; i++)
  129. tcp[20 + optlen + i] = payload[i];
  130. cs = net_checksum_tcp(my_ip, c->dst_ip, tcp, 20 + optlen + len);
  131. wr16(tcp + 16, cs);
  132. return nd->tx(tx_frame, 14 + total);
  133. }
  134. static int tcp_send_segment(struct tcp_conn *c, unsigned char flags,
  135. unsigned int seq, unsigned int ack,
  136. const unsigned char *payload, unsigned int len,
  137. int with_mss)
  138. {
  139. unsigned int wnd = with_mss ? TCP_WINDOW : rxq_space();
  140. return tcp_send_segment_wnd(c, flags, seq, ack, payload, len,
  141. with_mss, wnd);
  142. }
  143. /* One received segment. */
  144. struct tcp_seg {
  145. unsigned int seq, ack;
  146. unsigned char flags;
  147. const unsigned char *data;
  148. unsigned int len;
  149. const unsigned char *tcp; /* for option parsing */
  150. unsigned int doff;
  151. };
  152. /* Parse one frame as a TCP segment from the peer. Returns 1 on match.
  153. * Incoming checksums are not validated (same as the UDP path). */
  154. static int tcp_parse(const unsigned char *f, unsigned int flen,
  155. const unsigned char *peer_ip,
  156. unsigned int peer_port, unsigned int local_port,
  157. struct tcp_seg *s)
  158. {
  159. const unsigned char *ip = f + 14;
  160. const unsigned char *tcp;
  161. unsigned char my_ip[4];
  162. unsigned int i, ihl, ip_len, doff;
  163. if (flen < 14 + 20)
  164. return 0;
  165. if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
  166. return 0;
  167. if ((ip[0] >> 4) != 4)
  168. return 0;
  169. ihl = (unsigned int)(ip[0] & 0x0F) * 4;
  170. if (ihl < 20 || ip[9] != IPPROTO_TCP)
  171. return 0;
  172. /* Fragments are not reassembled: MF or a nonzero offset. */
  173. if ((ip[6] & 0x3F) != 0 || ip[7] != 0)
  174. return 0;
  175. ip_len = rd16(ip + 2);
  176. if (ip_len < ihl + 20 || ip_len > flen - 14)
  177. return 0;
  178. net_get_ip(my_ip);
  179. for (i = 0; i < 4; i++) {
  180. if (ip[12 + i] != peer_ip[i])
  181. return 0;
  182. if (ip[16 + i] != my_ip[i])
  183. return 0;
  184. }
  185. tcp = ip + ihl;
  186. if (rd16(tcp + 0) != peer_port || rd16(tcp + 2) != local_port)
  187. return 0;
  188. doff = (unsigned int)(tcp[12] >> 4) * 4;
  189. if (doff < 20 || ihl + doff > ip_len)
  190. return 0;
  191. s->seq = rd32(tcp + 4);
  192. s->ack = rd32(tcp + 8);
  193. s->flags = tcp[13];
  194. s->data = tcp + doff;
  195. s->len = ip_len - ihl - doff;
  196. s->tcp = tcp;
  197. s->doff = doff;
  198. return 1;
  199. }
  200. /* Parse the peer's MSS option (default stays when absent); 1 if present. */
  201. static int tcp_parse_mss(const unsigned char *tcp, unsigned int doff,
  202. unsigned int *mss)
  203. {
  204. unsigned int o = 20;
  205. while (o < doff) {
  206. unsigned char kind = tcp[o];
  207. if (kind == 0) /* end of options */
  208. return 0;
  209. if (kind == 1) { /* NOP */
  210. o++;
  211. continue;
  212. }
  213. if (o + 1 >= doff)
  214. return 0;
  215. if (tcp[o + 1] < 2 || o + tcp[o + 1] > doff)
  216. return 0;
  217. if (kind == 2 && tcp[o + 1] == 4) {
  218. *mss = rd16(tcp + o + 2);
  219. return 1;
  220. }
  221. o += tcp[o + 1];
  222. }
  223. return 0;
  224. }
  225. /* Take an incoming segment's payload/FIN into the receive queue. Returns
  226. * -2 on RST, 0 otherwise. Duplicate/out-of-order data is re-ACKed to resync;
  227. * a segment that does not fit the queue is not ACKed at all. */
  228. static int tcp_rx_data(struct tcp_conn *c, const struct tcp_seg *s)
  229. {
  230. if (s->flags & TCP_RST)
  231. return -2;
  232. if (s->tcp)
  233. c->peer_wnd = rd16(s->tcp + 14);
  234. if (s->len) {
  235. if (s->seq == c->rcv_nxt && s->len <= rxq_space()) {
  236. rxq_put(s->data, s->len);
  237. c->rcv_nxt += s->len;
  238. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  239. 0, 0, 0);
  240. } else if (s->seq != c->rcv_nxt) {
  241. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  242. 0, 0, 0);
  243. }
  244. }
  245. if ((s->flags & TCP_FIN) && !c->peer_fin &&
  246. s->seq + s->len == c->rcv_nxt) {
  247. c->peer_fin = 1;
  248. c->rcv_nxt++;
  249. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  250. 0, 0, 0);
  251. }
  252. return 0;
  253. }
  254. int tcp_connect(const unsigned char *dst_mac, const unsigned char *dst_ip,
  255. unsigned int dport, struct tcp_conn *c)
  256. {
  257. const struct netdev *nd = netdev_active();
  258. unsigned char *rxbuf = netdev_rx_scratch();
  259. unsigned int tries, i;
  260. if (!nd || !net_is_configured())
  261. return -3;
  262. for (i = 0; i < 6; i++)
  263. c->dst_mac[i] = dst_mac[i];
  264. for (i = 0; i < 4; i++)
  265. c->dst_ip[i] = dst_ip[i];
  266. c->dport = dport;
  267. c->sport = 0xC000u | (get_ticks_ms() & 0x3FFFu);
  268. c->snd_una = c->snd_nxt = (get_ticks_ms() << 16) ^ 0x5EED0000u;
  269. c->rcv_nxt = 0;
  270. c->peer_mss = 536;
  271. c->peer_wnd = 4096;
  272. c->peer_fin = 0;
  273. c->need_wnd = 0;
  274. rxq_head = rxq_tail = 0;
  275. for (tries = 0; tries < TCP_RETRIES; tries++) {
  276. unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
  277. if (tcp_send_segment(c, TCP_SYN, c->snd_nxt, 0, 0, 0, 1) != 0)
  278. return -3;
  279. while ((int)(get_ticks_ms() - deadline) < 0) {
  280. struct tcp_seg s;
  281. int len = nd->rx(rxbuf, NETDEV_FRAME_MAX);
  282. if (len < 0)
  283. continue;
  284. if (!tcp_parse(rxbuf, (unsigned int)len, c->dst_ip,
  285. c->dport, c->sport, &s))
  286. continue;
  287. if (s.flags & TCP_RST)
  288. return -2;
  289. if ((s.flags & (TCP_SYN | TCP_ACK)) ==
  290. (TCP_SYN | TCP_ACK) && s.ack == c->snd_nxt + 1) {
  291. unsigned int mss;
  292. c->rcv_nxt = s.seq + 1;
  293. c->snd_nxt++;
  294. c->snd_una = c->snd_nxt;
  295. c->peer_wnd = rd16(s.tcp + 14);
  296. if (tcp_parse_mss(s.tcp, s.doff, &mss) &&
  297. mss >= 64 && mss <= TCP_MSS_OURS)
  298. c->peer_mss = mss;
  299. if (tcp_send_segment(c, TCP_ACK, c->snd_nxt,
  300. c->rcv_nxt, 0, 0, 0) != 0)
  301. return -3;
  302. return 0;
  303. }
  304. }
  305. }
  306. return -1;
  307. }
  308. int tcp_send(struct tcp_conn *c, const void *data, unsigned int len)
  309. {
  310. const struct netdev *nd = netdev_active();
  311. unsigned char *rxbuf = netdev_rx_scratch();
  312. unsigned int tries;
  313. if (!nd || len > c->peer_mss)
  314. return -3;
  315. for (tries = 0; tries < TCP_RETRIES; tries++) {
  316. unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
  317. if (tcp_send_segment(c, TCP_PSH | TCP_ACK, c->snd_nxt,
  318. c->rcv_nxt, (const unsigned char *)data,
  319. len, 0) != 0)
  320. return -3;
  321. while ((int)(get_ticks_ms() - deadline) < 0) {
  322. struct tcp_seg s;
  323. int rlen = nd->rx(rxbuf, NETDEV_FRAME_MAX);
  324. if (rlen < 0)
  325. continue;
  326. if (!tcp_parse(rxbuf, (unsigned int)rlen, c->dst_ip,
  327. c->dport, c->sport, &s))
  328. continue;
  329. if (s.flags & TCP_RST)
  330. return -2;
  331. if (s.ack == c->snd_nxt + len) {
  332. c->snd_una = s.ack;
  333. c->snd_nxt = s.ack;
  334. return 0;
  335. }
  336. /* Data arriving while we are sending: buffer it for
  337. * tcp_recv (never ACK and drop). */
  338. if (tcp_rx_data(c, &s) == -2)
  339. return -2;
  340. }
  341. }
  342. return -1;
  343. }
  344. int tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
  345. unsigned int deadline_ms)
  346. {
  347. const struct netdev *nd = netdev_active();
  348. unsigned char *rxbuf = netdev_rx_scratch();
  349. if (!nd)
  350. return -2;
  351. /* Deliver what is already queued (may span several segments). */
  352. for (;;) {
  353. struct tcp_seg s;
  354. if (rxq_len()) {
  355. unsigned int n = rxq_len();
  356. if (n > maxlen)
  357. n = maxlen;
  358. rxq_get(out, n);
  359. /* Window update: ACKs sent while the queue was full may
  360. * have advertised zero, and without this the peer would
  361. * only recover on its next zero-window probe. */
  362. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  363. 0, 0, 0);
  364. return (int)n;
  365. }
  366. if (c->peer_fin)
  367. return 0;
  368. if ((int)(get_ticks_ms() - deadline_ms) >= 0)
  369. return -1;
  370. {
  371. int len = nd->rx(rxbuf, NETDEV_FRAME_MAX);
  372. if (len < 0)
  373. continue;
  374. if (!tcp_parse(rxbuf, (unsigned int)len, c->dst_ip,
  375. c->dport, c->sport, &s)) {
  376. /* A new connection (or its first data) while
  377. * this one is being served. */
  378. if (srv_port)
  379. tcp_srv_input(rxbuf, (unsigned int)len);
  380. continue;
  381. }
  382. }
  383. /* Track our send window (nothing pending here, but keeps
  384. * snd_una meaningful for the ACKs we emit). */
  385. if ((unsigned)(s.ack - c->snd_una) <=
  386. (unsigned)(c->snd_nxt - c->snd_una))
  387. c->snd_una = s.ack;
  388. if (c->need_wnd && (s.flags & TCP_ACK)) {
  389. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  390. 0, 0, 0);
  391. c->need_wnd = 0;
  392. }
  393. if (tcp_rx_data(c, &s) == -2)
  394. return -2;
  395. }
  396. }
  397. void tcp_close(struct tcp_conn *c)
  398. {
  399. const struct netdev *nd = netdev_active();
  400. unsigned char *rxbuf = netdev_rx_scratch();
  401. unsigned int fin_seq = c->snd_nxt;
  402. unsigned int tries;
  403. if (!nd)
  404. return;
  405. for (tries = 0; tries < 2; tries++) {
  406. unsigned int deadline = get_ticks_ms() + 500;
  407. struct tcp_seg s;
  408. if (tcp_send_segment(c, TCP_FIN | TCP_ACK, fin_seq,
  409. c->rcv_nxt, 0, 0, 0) != 0)
  410. return;
  411. c->snd_nxt = fin_seq + 1;
  412. while ((int)(get_ticks_ms() - deadline) < 0) {
  413. int len = nd->rx(rxbuf, NETDEV_FRAME_MAX);
  414. if (len < 0)
  415. continue;
  416. if (!tcp_parse(rxbuf, (unsigned int)len, c->dst_ip,
  417. c->dport, c->sport, &s))
  418. continue;
  419. if (s.flags & TCP_RST)
  420. return;
  421. /* Our FIN acked (or their FIN first). */
  422. if ((s.flags & TCP_FIN) && s.seq == c->rcv_nxt) {
  423. c->rcv_nxt++;
  424. c->peer_fin = 1;
  425. tcp_send_segment(c, TCP_ACK, c->snd_nxt,
  426. c->rcv_nxt, 0, 0, 0);
  427. return;
  428. }
  429. if (s.ack == c->snd_nxt)
  430. return;
  431. }
  432. }
  433. }
  434. /* ---- Server side ----
  435. *
  436. * One listening port, one active connection, two slots that hold a
  437. * handshake-complete connection until the command gets around to accepting
  438. * it. A closed connection lingers for a few seconds so the peer's FIN/ACK
  439. * is absorbed instead of answered with RST (libsoup treats a reset after a
  440. * complete response as an error). */
  441. #define TCP_SRV_SLOTS 4 /* 1 active + 3 backlog/closing */
  442. #define TCP_SRV_CLOSE_MS 2000
  443. #define TCP_SRV_BACKLOG_MS 30000
  444. enum {
  445. SRV_FREE = 0,
  446. SRV_BACKLOG,
  447. SRV_ACTIVE,
  448. SRV_CLOSING
  449. };
  450. #define SRV_PRE_MAX 1024
  451. struct tcp_srv {
  452. struct tcp_conn c;
  453. int used;
  454. int state;
  455. unsigned int last_ms;
  456. unsigned char pre[SRV_PRE_MAX]; /* first request, held until accept */
  457. unsigned int pre_len;
  458. };
  459. static struct tcp_srv srv[TCP_SRV_SLOTS];
  460. static int srv_active = -1;
  461. struct tcp_rx {
  462. unsigned int seq, ack;
  463. unsigned char flags;
  464. const unsigned char *data;
  465. unsigned int len;
  466. const unsigned char *tcp;
  467. unsigned int doff;
  468. unsigned char src_mac[6];
  469. unsigned char src_ip[4];
  470. unsigned int sport, dport;
  471. };
  472. /* Parse any IPv4/TCP frame addressed to us (no connection match). */
  473. static int tcp_parse_any(const unsigned char *f, unsigned int flen,
  474. struct tcp_rx *s)
  475. {
  476. const unsigned char *ip = f + 14;
  477. const unsigned char *tcp;
  478. unsigned char my_ip[4];
  479. unsigned int i, ihl, ip_len, doff;
  480. if (flen < 14 + 20)
  481. return 0;
  482. if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
  483. return 0;
  484. if ((ip[0] >> 4) != 4)
  485. return 0;
  486. ihl = (unsigned int)(ip[0] & 0x0F) * 4;
  487. if (ihl < 20 || ip[9] != IPPROTO_TCP)
  488. return 0;
  489. if ((ip[6] & 0x3F) != 0 || ip[7] != 0)
  490. return 0;
  491. ip_len = rd16(ip + 2);
  492. if (ip_len < ihl + 20 || ip_len > flen - 14)
  493. return 0;
  494. net_get_ip(my_ip);
  495. for (i = 0; i < 4; i++)
  496. if (ip[16 + i] != my_ip[i])
  497. return 0;
  498. tcp = ip + ihl;
  499. doff = (unsigned int)(tcp[12] >> 4) * 4;
  500. if (doff < 20 || ihl + doff > ip_len)
  501. return 0;
  502. for (i = 0; i < 6; i++)
  503. s->src_mac[i] = f[6 + i];
  504. for (i = 0; i < 4; i++)
  505. s->src_ip[i] = ip[12 + i];
  506. s->sport = rd16(tcp);
  507. s->dport = rd16(tcp + 2);
  508. s->seq = rd32(tcp + 4);
  509. s->ack = rd32(tcp + 8);
  510. s->flags = tcp[13];
  511. s->data = tcp + doff;
  512. s->len = ip_len - ihl - doff;
  513. s->tcp = tcp;
  514. s->doff = doff;
  515. return 1;
  516. }
  517. static int srv_match(const struct tcp_conn *c, const struct tcp_rx *s)
  518. {
  519. unsigned int i;
  520. if (s->sport != c->dport || s->dport != c->sport)
  521. return 0;
  522. for (i = 0; i < 4; i++)
  523. if (s->src_ip[i] != c->dst_ip[i])
  524. return 0;
  525. return 1;
  526. }
  527. static void srv_clear(int i)
  528. {
  529. srv[i].used = 0;
  530. srv[i].state = SRV_FREE;
  531. srv[i].pre_len = 0;
  532. }
  533. static void srv_expire(void)
  534. {
  535. unsigned int now = get_ticks_ms();
  536. int i;
  537. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  538. if (!srv[i].used)
  539. continue;
  540. if (srv[i].state == SRV_CLOSING &&
  541. now - srv[i].last_ms >= TCP_SRV_CLOSE_MS) {
  542. srv_clear(i);
  543. if (srv_active == i)
  544. srv_active = -1;
  545. } else if (srv[i].state == SRV_BACKLOG &&
  546. now - srv[i].last_ms >= TCP_SRV_BACKLOG_MS) {
  547. srv_clear(i);
  548. }
  549. }
  550. }
  551. static unsigned int srv_isn(void)
  552. {
  553. return (get_ticks_ms() * 1103515245u) ^ 0x5EED0000u;
  554. }
  555. static void srv_resend_synack(struct tcp_srv *e)
  556. {
  557. tcp_send_segment_wnd(&e->c, TCP_SYN | TCP_ACK, e->c.snd_una - 1,
  558. e->c.rcv_nxt, 0, 0, 1, 0);
  559. }
  560. /* Build a connection from a SYN and answer SYN-ACK. */
  561. static int srv_new(const struct tcp_rx *s)
  562. {
  563. struct tcp_srv *e;
  564. unsigned int mss = 536, i;
  565. int slot = -1;
  566. /* Retransmitted SYN: answer with the same ISN. */
  567. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  568. if (!srv[i].used || srv[i].state == SRV_CLOSING)
  569. continue;
  570. if (srv_match(&srv[i].c, s)) {
  571. srv_resend_synack(&srv[i]);
  572. return 1;
  573. }
  574. }
  575. srv_expire();
  576. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  577. /* A connection that already sent its response is finished
  578. * as far as new clients are concerned: reuse the slot rather
  579. * than make the next SYN wait for the linger to expire. */
  580. if (!srv[i].used || srv[i].state == SRV_CLOSING) {
  581. slot = (int)i;
  582. break;
  583. }
  584. }
  585. if (slot < 0)
  586. return 1; /* no room: drop, the client retries */
  587. e = &srv[slot];
  588. for (i = 0; i < 6; i++)
  589. e->c.dst_mac[i] = s->src_mac[i];
  590. for (i = 0; i < 4; i++)
  591. e->c.dst_ip[i] = s->src_ip[i];
  592. e->c.sport = srv_port;
  593. e->c.dport = s->sport;
  594. e->c.snd_una = e->c.snd_nxt = srv_isn();
  595. e->c.rcv_nxt = s->seq + 1;
  596. e->c.peer_fin = 0;
  597. e->c.need_wnd = 0;
  598. e->c.peer_mss = 536;
  599. e->c.peer_wnd = rd16(s->tcp + 14);
  600. if (!e->c.peer_wnd)
  601. e->c.peer_wnd = 4096;
  602. if (tcp_parse_mss(s->tcp, s->doff, &mss) &&
  603. mss >= 64 && mss <= TCP_MSS_OURS)
  604. e->c.peer_mss = mss;
  605. e->used = 1;
  606. e->state = SRV_BACKLOG;
  607. e->last_ms = get_ticks_ms();
  608. e->pre_len = 0;
  609. tcp_send_segment_wnd(&e->c, TCP_SYN | TCP_ACK, e->c.snd_nxt,
  610. e->c.rcv_nxt, 0, 0, 1, 0);
  611. /* The SYN consumed one sequence number. */
  612. e->c.snd_nxt++;
  613. e->c.snd_una = e->c.snd_nxt;
  614. return 1;
  615. }
  616. static void srv_send_rst(const struct tcp_rx *s)
  617. {
  618. struct tcp_conn c;
  619. unsigned int ack = s->seq + s->len;
  620. unsigned int i;
  621. if (s->flags & TCP_SYN)
  622. ack++;
  623. if (s->flags & TCP_FIN)
  624. ack++;
  625. for (i = 0; i < 6; i++)
  626. c.dst_mac[i] = s->src_mac[i];
  627. for (i = 0; i < 4; i++)
  628. c.dst_ip[i] = s->src_ip[i];
  629. c.sport = s->dport;
  630. c.dport = s->sport;
  631. c.snd_una = c.snd_nxt = s->ack;
  632. c.rcv_nxt = ack;
  633. c.peer_mss = 536;
  634. c.peer_fin = 0;
  635. tcp_send_segment(&c, TCP_RST | TCP_ACK, s->ack, ack, 0, 0, 0);
  636. }
  637. static void srv_rx_data(struct tcp_conn *c, const struct tcp_rx *r)
  638. {
  639. struct tcp_seg s;
  640. s.seq = r->seq;
  641. s.ack = r->ack;
  642. s.flags = r->flags;
  643. s.data = r->data;
  644. s.len = r->len;
  645. s.tcp = r->tcp;
  646. s.doff = r->doff;
  647. tcp_rx_data(c, &s);
  648. }
  649. void tcp_srv_init(unsigned int port)
  650. {
  651. int i;
  652. for (i = 0; i < TCP_SRV_SLOTS; i++)
  653. srv[i].used = 0, srv[i].state = SRV_FREE;
  654. srv_active = -1;
  655. srv_port = port;
  656. }
  657. void tcp_srv_reset(void)
  658. {
  659. tcp_srv_init(0);
  660. rxq_head = rxq_tail = 0;
  661. }
  662. int tcp_srv_input(const unsigned char *f, unsigned int len)
  663. {
  664. struct tcp_rx s;
  665. int i;
  666. if (!srv_port || !tcp_parse_any(f, len, &s))
  667. return 0;
  668. /* The accepted connection. */
  669. if (srv_active >= 0 && srv_match(&srv[srv_active].c, &s)) {
  670. struct tcp_conn *c = &srv[srv_active].c;
  671. if (c->need_wnd && (s.flags & TCP_ACK)) {
  672. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  673. 0, 0, 0);
  674. c->need_wnd = 0;
  675. }
  676. if (s.flags & TCP_RST) {
  677. srv_clear(srv_active);
  678. srv_active = -1;
  679. return 1;
  680. }
  681. if ((unsigned)(s.ack - c->snd_una) <=
  682. (unsigned)(c->snd_nxt - c->snd_una))
  683. c->snd_una = s.ack;
  684. srv_rx_data(c, &s);
  685. srv[srv_active].last_ms = get_ticks_ms();
  686. return 1;
  687. }
  688. /* A pending, closing or duplicate connection. */
  689. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  690. if (!srv[i].used || !srv_match(&srv[i].c, &s))
  691. continue;
  692. if (srv[i].state == SRV_CLOSING) {
  693. if (s.flags & TCP_FIN) {
  694. srv[i].c.rcv_nxt++;
  695. tcp_send_segment(&srv[i].c, TCP_ACK,
  696. srv[i].c.snd_nxt,
  697. srv[i].c.rcv_nxt, 0, 0, 0);
  698. }
  699. if (s.flags & TCP_RST)
  700. srv_clear(i);
  701. return 1;
  702. }
  703. if (s.flags & TCP_RST) {
  704. srv_clear(i);
  705. return 1;
  706. }
  707. if ((s.flags & (TCP_SYN | TCP_ACK | TCP_RST)) == TCP_SYN) {
  708. srv_resend_synack(&srv[i]);
  709. return 1;
  710. }
  711. if ((unsigned)(s.ack - srv[i].c.snd_una) <=
  712. (unsigned)(srv[i].c.snd_nxt - srv[i].c.snd_una))
  713. srv[i].c.snd_una = s.ack;
  714. /* Hold the first request (small HTTP headers) until the
  715. * command accepts this connection. ACK it so the client does
  716. * not have to wait for an RTO; if it does not fit, leave it
  717. * unacknowledged and the client retransmits after accept. */
  718. if (s.len && s.seq == srv[i].c.rcv_nxt &&
  719. s.len <= SRV_PRE_MAX - srv[i].pre_len) {
  720. unsigned int k;
  721. for (k = 0; k < s.len; k++)
  722. srv[i].pre[srv[i].pre_len + k] = s.data[k];
  723. srv[i].pre_len += s.len;
  724. srv[i].c.rcv_nxt += s.len;
  725. if ((s.flags & TCP_FIN) &&
  726. s.seq + s.len == srv[i].c.rcv_nxt) {
  727. srv[i].c.peer_fin = 1;
  728. srv[i].c.rcv_nxt++;
  729. }
  730. /* Keep the queued connection's window closed so it
  731. * does not send a body we have nowhere to put. */
  732. tcp_send_segment_wnd(&srv[i].c, TCP_ACK,
  733. srv[i].c.snd_nxt, srv[i].c.rcv_nxt,
  734. 0, 0, 0, 0);
  735. }
  736. srv[i].last_ms = get_ticks_ms();
  737. return 1;
  738. }
  739. /* A new connection. */
  740. if ((s.flags & (TCP_SYN | TCP_ACK | TCP_RST)) == TCP_SYN &&
  741. s.dport == srv_port)
  742. return srv_new(&s);
  743. /* TCP for us on a port we do not serve: refuse it. */
  744. srv_send_rst(&s);
  745. return 1;
  746. }
  747. struct tcp_conn *tcp_srv_accept(void)
  748. {
  749. int i, best = -1;
  750. if (srv_active >= 0)
  751. return &srv[srv_active].c;
  752. srv_expire();
  753. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  754. if (!srv[i].used || srv[i].state != SRV_BACKLOG)
  755. continue;
  756. if (best < 0 ||
  757. (int)(srv[i].last_ms - srv[best].last_ms) < 0)
  758. best = i;
  759. }
  760. if (best < 0)
  761. return 0;
  762. srv[best].state = SRV_ACTIVE;
  763. srv_active = best;
  764. rxq_head = rxq_tail = 0;
  765. if (srv[best].pre_len) {
  766. rxq_put(srv[best].pre, srv[best].pre_len);
  767. srv[best].pre_len = 0;
  768. }
  769. /* The SYN-ACK advertised a zero window while this connection waited
  770. * for the command; open it now with the real queue space. */
  771. tcp_send_segment(&srv[best].c, TCP_ACK, srv[best].c.snd_nxt,
  772. srv[best].c.rcv_nxt, 0, 0, 0);
  773. /* If this update raced ahead of the client's final ACK it may have
  774. * been discarded; repeat it when that ACK reaches us. */
  775. srv[best].c.need_wnd = 1;
  776. return &srv[best].c;
  777. }
  778. /* Send up to four segments before waiting for the cumulative ACK. Linux
  779. * delays the ACK of a lone small segment by up to 40 ms; a burst makes the
  780. * peer acknowledge every second segment immediately, which is the difference
  781. * between ~35 KB/s and a few MB/s on a LAN. The peer's advertised window
  782. * caps the burst. */
  783. int tcp_srv_send(struct tcp_conn *c, const void *data, unsigned int len)
  784. {
  785. const struct netdev *nd = netdev_active();
  786. unsigned char *rxbuf = netdev_rx_scratch();
  787. const unsigned char *p = data;
  788. unsigned int mss;
  789. if (!c || !nd)
  790. return -3;
  791. mss = c->peer_mss;
  792. if (mss < 64)
  793. mss = 64;
  794. while (len) {
  795. struct {
  796. unsigned int seq;
  797. unsigned int len;
  798. const unsigned char *data;
  799. } seg[4];
  800. unsigned int nseg = 0, off = 0, total = 0, cap, wnd;
  801. unsigned int tries, i;
  802. wnd = c->peer_wnd ? c->peer_wnd : 4096;
  803. cap = mss * 4;
  804. if (cap > wnd)
  805. cap = wnd;
  806. if (cap < mss)
  807. cap = mss;
  808. while (off < len && nseg < 4 && total < cap) {
  809. unsigned int n = len - off;
  810. if (n > mss)
  811. n = mss;
  812. if (total + n > cap)
  813. n = cap - total;
  814. if (!n)
  815. break;
  816. seg[nseg].seq = c->snd_nxt;
  817. seg[nseg].len = n;
  818. seg[nseg].data = p + off;
  819. if (tcp_send_segment(c, TCP_PSH | TCP_ACK, c->snd_nxt,
  820. c->rcv_nxt, p + off, n, 0) != 0)
  821. return -3;
  822. c->snd_nxt += n;
  823. off += n;
  824. total += n;
  825. nseg++;
  826. }
  827. for (tries = 0; ; tries++) {
  828. unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
  829. while ((int)(get_ticks_ms() - deadline) < 0) {
  830. struct tcp_seg s;
  831. int rlen = nd->rx(rxbuf, NETDEV_FRAME_MAX);
  832. if (rlen < 0)
  833. continue;
  834. if (!tcp_parse(rxbuf, (unsigned int)rlen,
  835. c->dst_ip, c->dport, c->sport, &s)) {
  836. if (srv_port)
  837. tcp_srv_input(rxbuf,
  838. (unsigned int)rlen);
  839. continue;
  840. }
  841. if (s.flags & TCP_RST)
  842. return -2;
  843. if ((unsigned)(s.ack - c->snd_una) <=
  844. (unsigned)(c->snd_nxt - c->snd_una))
  845. c->snd_una = s.ack;
  846. c->peer_wnd = rd16(s.tcp + 14);
  847. if ((int)(s.ack - c->snd_nxt) >= 0)
  848. goto burst_done;
  849. tcp_rx_data(c, &s);
  850. }
  851. if (tries >= TCP_RETRIES)
  852. return -1;
  853. /* Retransmit the still-unacknowledged part of this
  854. * burst (duplicates are harmless). */
  855. for (i = 0; i < nseg; i++) {
  856. if ((int)(seg[i].seq + seg[i].len -
  857. c->snd_una) <= 0)
  858. continue;
  859. tcp_send_segment(c, TCP_PSH | TCP_ACK,
  860. seg[i].seq, c->rcv_nxt,
  861. seg[i].data, seg[i].len, 0);
  862. }
  863. }
  864. burst_done:
  865. p += off;
  866. len -= off;
  867. }
  868. return 0;
  869. }
  870. void tcp_srv_close(struct tcp_conn *c)
  871. {
  872. int i;
  873. if (!c)
  874. return;
  875. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  876. if (!srv[i].used || &srv[i].c != c)
  877. continue;
  878. tcp_send_segment(c, TCP_FIN | TCP_ACK, c->snd_nxt, c->rcv_nxt,
  879. 0, 0, 0);
  880. c->snd_nxt++;
  881. srv[i].state = SRV_CLOSING;
  882. srv[i].last_ms = get_ticks_ms();
  883. if (srv_active == i)
  884. srv_active = -1;
  885. return;
  886. }
  887. }