tcp.c 20 KB

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