tcp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. wr16(tcp + 14, TCP_WINDOW);
  123. wr16(tcp + 16, 0); /* checksum */
  124. wr16(tcp + 18, 0); /* urgent pointer */
  125. for (i = 0; i < len; i++)
  126. tcp[20 + optlen + i] = payload[i];
  127. cs = net_checksum_tcp(my_ip, c->dst_ip, tcp, 20 + optlen + len);
  128. wr16(tcp + 16, cs);
  129. return nd->tx(tx_frame, 14 + total);
  130. }
  131. /* One received segment. */
  132. struct tcp_seg {
  133. unsigned int seq, ack;
  134. unsigned char flags;
  135. const unsigned char *data;
  136. unsigned int len;
  137. const unsigned char *tcp; /* for option parsing */
  138. unsigned int doff;
  139. };
  140. /* Parse one frame as a TCP segment from the peer. Returns 1 on match.
  141. * Incoming checksums are not validated (same as the UDP path). */
  142. static int tcp_parse(const unsigned char *f, unsigned int flen,
  143. const unsigned char *peer_ip,
  144. unsigned int peer_port, unsigned int local_port,
  145. struct tcp_seg *s)
  146. {
  147. const unsigned char *ip = f + 14;
  148. const unsigned char *tcp;
  149. unsigned char my_ip[4];
  150. unsigned int i, ihl, ip_len, doff;
  151. if (flen < 14 + 20)
  152. return 0;
  153. if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
  154. return 0;
  155. if ((ip[0] >> 4) != 4)
  156. return 0;
  157. ihl = (unsigned int)(ip[0] & 0x0F) * 4;
  158. if (ihl < 20 || ip[9] != IPPROTO_TCP)
  159. return 0;
  160. /* Fragments are not reassembled: MF or a nonzero offset. */
  161. if ((ip[6] & 0x3F) != 0 || ip[7] != 0)
  162. return 0;
  163. ip_len = rd16(ip + 2);
  164. if (ip_len < ihl + 20 || ip_len > flen - 14)
  165. return 0;
  166. net_get_ip(my_ip);
  167. for (i = 0; i < 4; i++) {
  168. if (ip[12 + i] != peer_ip[i])
  169. return 0;
  170. if (ip[16 + i] != my_ip[i])
  171. return 0;
  172. }
  173. tcp = ip + ihl;
  174. if (rd16(tcp + 0) != peer_port || rd16(tcp + 2) != local_port)
  175. return 0;
  176. doff = (unsigned int)(tcp[12] >> 4) * 4;
  177. if (doff < 20 || ihl + doff > ip_len)
  178. return 0;
  179. s->seq = rd32(tcp + 4);
  180. s->ack = rd32(tcp + 8);
  181. s->flags = tcp[13];
  182. s->data = tcp + doff;
  183. s->len = ip_len - ihl - doff;
  184. s->tcp = tcp;
  185. s->doff = doff;
  186. return 1;
  187. }
  188. /* Parse the peer's MSS option (default stays when absent); 1 if present. */
  189. static int tcp_parse_mss(const unsigned char *tcp, unsigned int doff,
  190. unsigned int *mss)
  191. {
  192. unsigned int o = 20;
  193. while (o < doff) {
  194. unsigned char kind = tcp[o];
  195. if (kind == 0) /* end of options */
  196. return 0;
  197. if (kind == 1) { /* NOP */
  198. o++;
  199. continue;
  200. }
  201. if (o + 1 >= doff)
  202. return 0;
  203. if (tcp[o + 1] < 2 || o + tcp[o + 1] > doff)
  204. return 0;
  205. if (kind == 2 && tcp[o + 1] == 4) {
  206. *mss = rd16(tcp + o + 2);
  207. return 1;
  208. }
  209. o += tcp[o + 1];
  210. }
  211. return 0;
  212. }
  213. /* Take an incoming segment's payload/FIN into the receive queue. Returns
  214. * -2 on RST, 0 otherwise. Duplicate/out-of-order data is re-ACKed to resync;
  215. * a segment that does not fit the queue is not ACKed at all. */
  216. static int tcp_rx_data(struct tcp_conn *c, const struct tcp_seg *s)
  217. {
  218. if (s->flags & TCP_RST)
  219. return -2;
  220. if (s->len) {
  221. if (s->seq == c->rcv_nxt && s->len <= rxq_space()) {
  222. rxq_put(s->data, s->len);
  223. c->rcv_nxt += s->len;
  224. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  225. 0, 0, 0);
  226. } else if (s->seq != c->rcv_nxt) {
  227. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  228. 0, 0, 0);
  229. }
  230. }
  231. if ((s->flags & TCP_FIN) && !c->peer_fin &&
  232. s->seq + s->len == c->rcv_nxt) {
  233. c->peer_fin = 1;
  234. c->rcv_nxt++;
  235. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  236. 0, 0, 0);
  237. }
  238. return 0;
  239. }
  240. int tcp_connect(const unsigned char *dst_mac, const unsigned char *dst_ip,
  241. unsigned int dport, struct tcp_conn *c)
  242. {
  243. const struct netdev *nd = netdev_active();
  244. unsigned char *rxbuf = netdev_rx_scratch();
  245. unsigned int tries, i;
  246. if (!nd || !net_is_configured())
  247. return -3;
  248. for (i = 0; i < 6; i++)
  249. c->dst_mac[i] = dst_mac[i];
  250. for (i = 0; i < 4; i++)
  251. c->dst_ip[i] = dst_ip[i];
  252. c->dport = dport;
  253. c->sport = 0xC000u | (get_ticks_ms() & 0x3FFFu);
  254. c->snd_una = c->snd_nxt = (get_ticks_ms() << 16) ^ 0x5EED0000u;
  255. c->rcv_nxt = 0;
  256. c->peer_mss = 536;
  257. c->peer_fin = 0;
  258. rxq_head = rxq_tail = 0;
  259. for (tries = 0; tries < TCP_RETRIES; tries++) {
  260. unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
  261. if (tcp_send_segment(c, TCP_SYN, c->snd_nxt, 0, 0, 0, 1) != 0)
  262. return -3;
  263. while ((int)(get_ticks_ms() - deadline) < 0) {
  264. struct tcp_seg s;
  265. int len = nd->rx(rxbuf, NETDEV_FRAME_MAX);
  266. if (len < 0)
  267. continue;
  268. if (!tcp_parse(rxbuf, (unsigned int)len, c->dst_ip,
  269. c->dport, c->sport, &s))
  270. continue;
  271. if (s.flags & TCP_RST)
  272. return -2;
  273. if ((s.flags & (TCP_SYN | TCP_ACK)) ==
  274. (TCP_SYN | TCP_ACK) && s.ack == c->snd_nxt + 1) {
  275. unsigned int mss;
  276. c->rcv_nxt = s.seq + 1;
  277. c->snd_nxt++;
  278. c->snd_una = c->snd_nxt;
  279. if (tcp_parse_mss(s.tcp, s.doff, &mss) &&
  280. mss >= 64 && mss <= TCP_MSS_OURS)
  281. c->peer_mss = mss;
  282. if (tcp_send_segment(c, TCP_ACK, c->snd_nxt,
  283. c->rcv_nxt, 0, 0, 0) != 0)
  284. return -3;
  285. return 0;
  286. }
  287. }
  288. }
  289. return -1;
  290. }
  291. int tcp_send(struct tcp_conn *c, const void *data, unsigned int len)
  292. {
  293. const struct netdev *nd = netdev_active();
  294. unsigned char *rxbuf = netdev_rx_scratch();
  295. unsigned int tries;
  296. if (!nd || len > c->peer_mss)
  297. return -3;
  298. for (tries = 0; tries < TCP_RETRIES; tries++) {
  299. unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
  300. if (tcp_send_segment(c, TCP_PSH | TCP_ACK, c->snd_nxt,
  301. c->rcv_nxt, (const unsigned char *)data,
  302. len, 0) != 0)
  303. return -3;
  304. while ((int)(get_ticks_ms() - deadline) < 0) {
  305. struct tcp_seg s;
  306. int rlen = nd->rx(rxbuf, NETDEV_FRAME_MAX);
  307. if (rlen < 0)
  308. continue;
  309. if (!tcp_parse(rxbuf, (unsigned int)rlen, c->dst_ip,
  310. c->dport, c->sport, &s))
  311. continue;
  312. if (s.flags & TCP_RST)
  313. return -2;
  314. if (s.ack == c->snd_nxt + len) {
  315. c->snd_una = s.ack;
  316. c->snd_nxt = s.ack;
  317. return 0;
  318. }
  319. /* Data arriving while we are sending: buffer it for
  320. * tcp_recv (never ACK and drop). */
  321. if (tcp_rx_data(c, &s) == -2)
  322. return -2;
  323. }
  324. }
  325. return -1;
  326. }
  327. int tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
  328. unsigned int deadline_ms)
  329. {
  330. const struct netdev *nd = netdev_active();
  331. unsigned char *rxbuf = netdev_rx_scratch();
  332. if (!nd)
  333. return -2;
  334. /* Deliver what is already queued (may span several segments). */
  335. for (;;) {
  336. struct tcp_seg s;
  337. if (rxq_len()) {
  338. unsigned int n = rxq_len();
  339. if (n > maxlen)
  340. n = maxlen;
  341. rxq_get(out, n);
  342. return (int)n;
  343. }
  344. if (c->peer_fin)
  345. return 0;
  346. if ((int)(get_ticks_ms() - deadline_ms) >= 0)
  347. return -1;
  348. {
  349. int len = nd->rx(rxbuf, NETDEV_FRAME_MAX);
  350. if (len < 0)
  351. continue;
  352. if (!tcp_parse(rxbuf, (unsigned int)len, c->dst_ip,
  353. c->dport, c->sport, &s))
  354. continue;
  355. }
  356. /* Track our send window (nothing pending here, but keeps
  357. * snd_una meaningful for the ACKs we emit). */
  358. if ((unsigned)(s.ack - c->snd_una) <=
  359. (unsigned)(c->snd_nxt - c->snd_una))
  360. c->snd_una = s.ack;
  361. if (tcp_rx_data(c, &s) == -2)
  362. return -2;
  363. }
  364. }
  365. void tcp_close(struct tcp_conn *c)
  366. {
  367. const struct netdev *nd = netdev_active();
  368. unsigned char *rxbuf = netdev_rx_scratch();
  369. unsigned int fin_seq = c->snd_nxt;
  370. unsigned int tries;
  371. if (!nd)
  372. return;
  373. for (tries = 0; tries < 2; tries++) {
  374. unsigned int deadline = get_ticks_ms() + 500;
  375. struct tcp_seg s;
  376. if (tcp_send_segment(c, TCP_FIN | TCP_ACK, fin_seq,
  377. c->rcv_nxt, 0, 0, 0) != 0)
  378. return;
  379. c->snd_nxt = fin_seq + 1;
  380. while ((int)(get_ticks_ms() - deadline) < 0) {
  381. int len = nd->rx(rxbuf, NETDEV_FRAME_MAX);
  382. if (len < 0)
  383. continue;
  384. if (!tcp_parse(rxbuf, (unsigned int)len, c->dst_ip,
  385. c->dport, c->sport, &s))
  386. continue;
  387. if (s.flags & TCP_RST)
  388. return;
  389. /* Our FIN acked (or their FIN first). */
  390. if ((s.flags & TCP_FIN) && s.seq == c->rcv_nxt) {
  391. c->rcv_nxt++;
  392. c->peer_fin = 1;
  393. tcp_send_segment(c, TCP_ACK, c->snd_nxt,
  394. c->rcv_nxt, 0, 0, 0);
  395. return;
  396. }
  397. if (s.ack == c->snd_nxt)
  398. return;
  399. }
  400. }
  401. }