tcp.c 29 KB

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