tcp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. /* Payload of a segment larger than the caller's buffer is held here, so no
  21. * bytes are lost; at most one segment can be outstanding. */
  22. static unsigned char hold_buf[TCP_MSS_OURS];
  23. static unsigned int hold_len;
  24. static unsigned int hold_pos;
  25. static unsigned int rd16(const unsigned char *p)
  26. {
  27. return ((unsigned int)p[0] << 8) | p[1];
  28. }
  29. static void wr16(unsigned char *p, unsigned int v)
  30. {
  31. p[0] = (unsigned char)(v >> 8);
  32. p[1] = (unsigned char)v;
  33. }
  34. static unsigned int rd32(const unsigned char *p)
  35. {
  36. return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) |
  37. ((unsigned int)p[2] << 8) | p[3];
  38. }
  39. static void wr32(unsigned char *p, unsigned int v)
  40. {
  41. p[0] = (unsigned char)(v >> 24);
  42. p[1] = (unsigned char)(v >> 16);
  43. p[2] = (unsigned char)(v >> 8);
  44. p[3] = (unsigned char)v;
  45. }
  46. /* Build and transmit one segment. `seq` is the sequence of the first byte
  47. * (for SYN/FIN that is the flag's own sequence). Returns 0 / -1 (tx). */
  48. static int tcp_send_segment(struct tcp_conn *c, unsigned char flags,
  49. unsigned int seq, unsigned int ack,
  50. const unsigned char *payload, unsigned int len,
  51. int with_mss)
  52. {
  53. const struct netdev *nd = netdev_active();
  54. unsigned char my_mac[6], my_ip[4];
  55. unsigned char *ip = tx_frame + 14;
  56. unsigned char *tcp = ip + 20;
  57. unsigned short cs;
  58. unsigned int total, optlen = 0, i;
  59. if (!nd)
  60. return -1;
  61. nd->get_mac(my_mac);
  62. net_get_ip(my_ip);
  63. /* Ethernet. */
  64. for (i = 0; i < 6; i++)
  65. tx_frame[i] = c->dst_mac[i];
  66. for (i = 0; i < 6; i++)
  67. tx_frame[6 + i] = my_mac[i];
  68. tx_frame[12] = 0x08;
  69. tx_frame[13] = 0x00;
  70. /* IPv4 header (IHL 5, no options). */
  71. total = 20 + 20 + (with_mss ? 4 : 0) + len;
  72. for (i = 0; i < 20; i++)
  73. ip[i] = 0;
  74. ip[0] = 0x45;
  75. wr16(ip + 2, total);
  76. ip[8] = 64; /* TTL */
  77. ip[9] = IPPROTO_TCP;
  78. for (i = 0; i < 4; i++)
  79. ip[12 + i] = my_ip[i];
  80. for (i = 0; i < 4; i++)
  81. ip[16 + i] = c->dst_ip[i];
  82. cs = net_checksum(ip, 20);
  83. wr16(ip + 10, cs);
  84. /* TCP header (+ MSS option on SYN). */
  85. if (with_mss) {
  86. tcp[20] = 2; /* kind: MSS */
  87. tcp[21] = 4; /* length */
  88. wr16(tcp + 22, TCP_MSS_OURS);
  89. optlen = 4;
  90. }
  91. wr16(tcp + 0, c->sport);
  92. wr16(tcp + 2, c->dport);
  93. wr32(tcp + 4, seq);
  94. wr32(tcp + 8, ack);
  95. tcp[12] = (unsigned char)((5 + optlen / 4) << 4);
  96. tcp[13] = flags;
  97. wr16(tcp + 14, TCP_WINDOW);
  98. wr16(tcp + 16, 0); /* checksum */
  99. wr16(tcp + 18, 0); /* urgent pointer */
  100. for (i = 0; i < len; i++)
  101. tcp[20 + optlen + i] = payload[i];
  102. cs = net_checksum_tcp(my_ip, c->dst_ip, tcp, 20 + optlen + len);
  103. wr16(tcp + 16, cs);
  104. return nd->tx(tx_frame, 14 + total);
  105. }
  106. /* One received segment. */
  107. struct tcp_seg {
  108. unsigned int seq, ack;
  109. unsigned char flags;
  110. const unsigned char *data;
  111. unsigned int len;
  112. const unsigned char *tcp; /* for option parsing */
  113. unsigned int doff;
  114. };
  115. /* Parse one frame as a TCP segment from the peer. Returns 1 on match.
  116. * Incoming checksums are not validated (same as the UDP path). */
  117. static int tcp_parse(const unsigned char *f, unsigned int flen,
  118. const unsigned char *peer_ip,
  119. unsigned int peer_port, unsigned int local_port,
  120. struct tcp_seg *s)
  121. {
  122. const unsigned char *ip = f + 14;
  123. const unsigned char *tcp;
  124. unsigned char my_ip[4];
  125. unsigned int i, ihl, ip_len, doff;
  126. if (flen < 14 + 20)
  127. return 0;
  128. if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
  129. return 0;
  130. if ((ip[0] >> 4) != 4)
  131. return 0;
  132. ihl = (unsigned int)(ip[0] & 0x0F) * 4;
  133. if (ihl < 20 || ip[9] != IPPROTO_TCP)
  134. return 0;
  135. /* Fragments are not reassembled: MF or a nonzero offset. */
  136. if ((ip[6] & 0x3F) != 0 || ip[7] != 0)
  137. return 0;
  138. ip_len = rd16(ip + 2);
  139. if (ip_len < ihl + 20 || ip_len > flen - 14)
  140. return 0;
  141. net_get_ip(my_ip);
  142. for (i = 0; i < 4; i++) {
  143. if (ip[12 + i] != peer_ip[i])
  144. return 0;
  145. if (ip[16 + i] != my_ip[i])
  146. return 0;
  147. }
  148. tcp = ip + ihl;
  149. if (rd16(tcp + 0) != peer_port || rd16(tcp + 2) != local_port)
  150. return 0;
  151. doff = (unsigned int)(tcp[12] >> 4) * 4;
  152. if (doff < 20 || ihl + doff > ip_len)
  153. return 0;
  154. s->seq = rd32(tcp + 4);
  155. s->ack = rd32(tcp + 8);
  156. s->flags = tcp[13];
  157. s->data = tcp + doff;
  158. s->len = ip_len - ihl - doff;
  159. s->tcp = tcp;
  160. s->doff = doff;
  161. return 1;
  162. }
  163. /* Parse the peer's MSS option (default stays when absent); 1 if present. */
  164. static int tcp_parse_mss(const unsigned char *tcp, unsigned int doff,
  165. unsigned int *mss)
  166. {
  167. unsigned int o = 20;
  168. while (o < doff) {
  169. unsigned char kind = tcp[o];
  170. if (kind == 0) /* end of options */
  171. return 0;
  172. if (kind == 1) { /* NOP */
  173. o++;
  174. continue;
  175. }
  176. if (o + 1 >= doff)
  177. return 0;
  178. if (tcp[o + 1] < 2 || o + tcp[o + 1] > doff)
  179. return 0;
  180. if (kind == 2 && tcp[o + 1] == 4) {
  181. *mss = rd16(tcp + o + 2);
  182. return 1;
  183. }
  184. o += tcp[o + 1];
  185. }
  186. return 0;
  187. }
  188. int tcp_connect(const unsigned char *dst_mac, const unsigned char *dst_ip,
  189. unsigned int dport, struct tcp_conn *c)
  190. {
  191. const struct netdev *nd = netdev_active();
  192. unsigned char *rxbuf = netdev_rx_scratch();
  193. unsigned int tries, i;
  194. if (!nd || !net_is_configured())
  195. return -3;
  196. for (i = 0; i < 6; i++)
  197. c->dst_mac[i] = dst_mac[i];
  198. for (i = 0; i < 4; i++)
  199. c->dst_ip[i] = dst_ip[i];
  200. c->dport = dport;
  201. c->sport = 0xC000u | (get_ticks_ms() & 0x3FFFu);
  202. c->snd_una = c->snd_nxt = (get_ticks_ms() << 16) ^ 0x5EED0000u;
  203. c->rcv_nxt = 0;
  204. c->peer_mss = 536;
  205. c->peer_fin = 0;
  206. hold_len = hold_pos = 0;
  207. for (tries = 0; tries < TCP_RETRIES; tries++) {
  208. unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
  209. if (tcp_send_segment(c, TCP_SYN, c->snd_nxt, 0, 0, 0, 1) != 0)
  210. return -3;
  211. while ((int)(get_ticks_ms() - deadline) < 0) {
  212. struct tcp_seg s;
  213. int len = nd->rx(rxbuf, NETDEV_FRAME_MAX);
  214. if (len < 0)
  215. continue;
  216. if (!tcp_parse(rxbuf, (unsigned int)len, c->dst_ip,
  217. c->dport, c->sport, &s))
  218. continue;
  219. if (s.flags & TCP_RST)
  220. return -2;
  221. if ((s.flags & (TCP_SYN | TCP_ACK)) ==
  222. (TCP_SYN | TCP_ACK) && s.ack == c->snd_nxt + 1) {
  223. unsigned int mss;
  224. c->rcv_nxt = s.seq + 1;
  225. c->snd_nxt++;
  226. c->snd_una = c->snd_nxt;
  227. if (tcp_parse_mss(s.tcp, s.doff, &mss) &&
  228. mss >= 64 && mss <= TCP_MSS_OURS)
  229. c->peer_mss = mss;
  230. if (tcp_send_segment(c, TCP_ACK, c->snd_nxt,
  231. c->rcv_nxt, 0, 0, 0) != 0)
  232. return -3;
  233. return 0;
  234. }
  235. }
  236. }
  237. return -1;
  238. }
  239. int tcp_send(struct tcp_conn *c, const void *data, unsigned int len)
  240. {
  241. const struct netdev *nd = netdev_active();
  242. unsigned char *rxbuf = netdev_rx_scratch();
  243. unsigned int tries;
  244. if (!nd || len > c->peer_mss)
  245. return -3;
  246. for (tries = 0; tries < TCP_RETRIES; tries++) {
  247. unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
  248. if (tcp_send_segment(c, TCP_PSH | TCP_ACK, c->snd_nxt,
  249. c->rcv_nxt, (const unsigned char *)data,
  250. len, 0) != 0)
  251. return -3;
  252. while ((int)(get_ticks_ms() - deadline) < 0) {
  253. struct tcp_seg s;
  254. int rlen = nd->rx(rxbuf, NETDEV_FRAME_MAX);
  255. if (rlen < 0)
  256. continue;
  257. if (!tcp_parse(rxbuf, (unsigned int)rlen, c->dst_ip,
  258. c->dport, c->sport, &s))
  259. continue;
  260. if (s.flags & TCP_RST)
  261. return -2;
  262. if (s.ack == c->snd_nxt + len) {
  263. c->snd_una = s.ack;
  264. c->snd_nxt = s.ack;
  265. return 0;
  266. }
  267. /* Data before the request is fully acked: ACK it and
  268. * drop it; the peer retransmits if it cares. */
  269. if (s.len) {
  270. if (s.seq == c->rcv_nxt)
  271. c->rcv_nxt += s.len;
  272. tcp_send_segment(c, TCP_ACK, c->snd_nxt,
  273. c->rcv_nxt, 0, 0, 0);
  274. }
  275. }
  276. }
  277. return -1;
  278. }
  279. int tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
  280. unsigned int deadline_ms)
  281. {
  282. const struct netdev *nd = netdev_active();
  283. unsigned char *rxbuf = netdev_rx_scratch();
  284. if (!nd)
  285. return -2;
  286. /* Drain a partially consumed segment first. */
  287. if (hold_pos < hold_len) {
  288. unsigned int n = hold_len - hold_pos;
  289. unsigned int i;
  290. if (n > maxlen)
  291. n = maxlen;
  292. for (i = 0; i < n; i++)
  293. out[i] = hold_buf[hold_pos + i];
  294. hold_pos += n;
  295. return (int)n;
  296. }
  297. if (c->peer_fin)
  298. return 0;
  299. while ((int)(get_ticks_ms() - deadline_ms) < 0) {
  300. struct tcp_seg s;
  301. int len = nd->rx(rxbuf, NETDEV_FRAME_MAX);
  302. if (len < 0)
  303. continue;
  304. if (!tcp_parse(rxbuf, (unsigned int)len, c->dst_ip,
  305. c->dport, c->sport, &s))
  306. continue;
  307. if (s.flags & TCP_RST)
  308. return -2;
  309. /* Track our send window (nothing pending here, but keeps
  310. * snd_una meaningful for the ACKs we emit). */
  311. if ((unsigned)(s.ack - c->snd_una) <=
  312. (unsigned)(c->snd_nxt - c->snd_una))
  313. c->snd_una = s.ack;
  314. if (s.len) {
  315. if (s.len > sizeof(hold_buf)) /* not ours to store */
  316. continue;
  317. if (s.seq == c->rcv_nxt) {
  318. unsigned int take = s.len < maxlen ? s.len : maxlen;
  319. unsigned int i;
  320. for (i = 0; i < take; i++)
  321. out[i] = s.data[i];
  322. c->rcv_nxt += s.len;
  323. if (s.len > take) { /* hold the rest */
  324. for (i = take; i < s.len; i++)
  325. hold_buf[i - take] = s.data[i];
  326. hold_len = s.len - take;
  327. hold_pos = 0;
  328. }
  329. /* A FIN riding on the data consumes the next
  330. * sequence number after the payload. */
  331. if ((s.flags & TCP_FIN) && !c->peer_fin) {
  332. c->peer_fin = 1;
  333. c->rcv_nxt++;
  334. }
  335. tcp_send_segment(c, TCP_ACK, c->snd_nxt,
  336. c->rcv_nxt, 0, 0, 0);
  337. return (int)take;
  338. }
  339. /* Duplicate or out-of-order data: re-ACK to resync. */
  340. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  341. 0, 0, 0);
  342. continue;
  343. }
  344. if (s.flags & TCP_FIN) {
  345. if (s.seq == c->rcv_nxt) {
  346. c->rcv_nxt++;
  347. c->peer_fin = 1;
  348. tcp_send_segment(c, TCP_ACK, c->snd_nxt,
  349. c->rcv_nxt, 0, 0, 0);
  350. return 0;
  351. }
  352. }
  353. }
  354. return -1;
  355. }
  356. void tcp_close(struct tcp_conn *c)
  357. {
  358. const struct netdev *nd = netdev_active();
  359. unsigned char *rxbuf = netdev_rx_scratch();
  360. unsigned int fin_seq = c->snd_nxt;
  361. unsigned int tries;
  362. if (!nd)
  363. return;
  364. for (tries = 0; tries < 2; tries++) {
  365. unsigned int deadline = get_ticks_ms() + 500;
  366. struct tcp_seg s;
  367. if (tcp_send_segment(c, TCP_FIN | TCP_ACK, fin_seq,
  368. c->rcv_nxt, 0, 0, 0) != 0)
  369. return;
  370. c->snd_nxt = fin_seq + 1;
  371. while ((int)(get_ticks_ms() - deadline) < 0) {
  372. int len = nd->rx(rxbuf, NETDEV_FRAME_MAX);
  373. if (len < 0)
  374. continue;
  375. if (!tcp_parse(rxbuf, (unsigned int)len, c->dst_ip,
  376. c->dport, c->sport, &s))
  377. continue;
  378. if (s.flags & TCP_RST)
  379. return;
  380. /* Our FIN acked (or their FIN first). */
  381. if ((s.flags & TCP_FIN) && s.seq == c->rcv_nxt) {
  382. c->rcv_nxt++;
  383. c->peer_fin = 1;
  384. tcp_send_segment(c, TCP_ACK, c->snd_nxt,
  385. c->rcv_nxt, 0, 0, 0);
  386. return;
  387. }
  388. if (s.ack == c->snd_nxt)
  389. return;
  390. }
  391. }
  392. }