tcp.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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 6
  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. flags = net_irq_enter();
  433. r = tcp_send_segment(c, TCP_SYN, c->snd_nxt, 0, 0, 0, 1);
  434. net_irq_exit(flags);
  435. if (r != 0) {
  436. client_free(cl);
  437. return -3;
  438. }
  439. while ((int)(get_ticks_ms() - deadline) < 0) {
  440. net_pump(8);
  441. if (c->state == TCP_STATE_ESTABLISHED)
  442. return 0;
  443. if (c->state == TCP_STATE_CLOSED) {
  444. r = cl->err ? cl->err : -2;
  445. client_free(cl);
  446. return r;
  447. }
  448. process_sleep_ms(1);
  449. }
  450. }
  451. client_free(cl);
  452. return -1;
  453. }
  454. int tcp_client_send(unsigned int pid, const void *buf, unsigned int len)
  455. {
  456. struct tcp_client *cl = client_find(pid);
  457. struct tcp_conn *c;
  458. unsigned int tries;
  459. if (!cl)
  460. return -4;
  461. c = &cl->c;
  462. if (c->state != TCP_STATE_ESTABLISHED)
  463. return -4;
  464. if (len > c->peer_mss)
  465. return -3;
  466. for (tries = 0; tries < TCP_RETRIES; tries++) {
  467. unsigned int seq = c->snd_nxt;
  468. unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
  469. unsigned int flags;
  470. int r;
  471. flags = net_irq_enter();
  472. r = tcp_send_segment(c, TCP_PSH | TCP_ACK, seq, c->rcv_nxt,
  473. (const unsigned char *)buf, len, 0);
  474. net_irq_exit(flags);
  475. if (r != 0)
  476. return -3;
  477. c->snd_nxt += len;
  478. while ((int)(get_ticks_ms() - deadline) < 0) {
  479. net_pump(8);
  480. if (c->state == TCP_STATE_CLOSED)
  481. return cl->err ? cl->err : -2;
  482. if ((int)(c->snd_una - c->snd_nxt) >= 0)
  483. return 0;
  484. process_sleep_ms(1);
  485. }
  486. /* Retransmit the whole segment; a duplicate is harmless. */
  487. }
  488. return -1;
  489. }
  490. int tcp_client_recv(unsigned int pid, void *vbuf, unsigned int max,
  491. unsigned int deadline_ms)
  492. {
  493. struct tcp_client *cl = client_find(pid);
  494. struct tcp_conn *c;
  495. unsigned char *buf = (unsigned char *)vbuf;
  496. if (!cl)
  497. return -4;
  498. c = &cl->c;
  499. for (;;) {
  500. if (rxq_len(c)) {
  501. unsigned int n = rxq_len(c);
  502. unsigned int flags;
  503. if (n > max)
  504. n = max;
  505. rxq_get(c, buf, n);
  506. flags = net_irq_enter();
  507. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  508. 0, 0, 0);
  509. net_irq_exit(flags);
  510. return (int)n;
  511. }
  512. if (c->peer_fin)
  513. return 0;
  514. if (c->state == TCP_STATE_CLOSED)
  515. return cl->err ? cl->err : -2;
  516. if ((int)(get_ticks_ms() - deadline_ms) >= 0)
  517. return -1;
  518. net_pump(8);
  519. if (!rxq_len(c) && c->state != TCP_STATE_CLOSED)
  520. process_sleep_ms(1);
  521. }
  522. }
  523. void tcp_client_close(unsigned int pid)
  524. {
  525. struct tcp_client *cl = client_find(pid);
  526. struct tcp_conn *c;
  527. if (!cl)
  528. return;
  529. c = &cl->c;
  530. if (c->state == TCP_STATE_ESTABLISHED) {
  531. unsigned int deadline = get_ticks_ms() + 500;
  532. unsigned int flags;
  533. flags = net_irq_enter();
  534. tcp_send_segment(c, TCP_FIN | TCP_ACK, c->snd_nxt,
  535. c->rcv_nxt, 0, 0, 0);
  536. net_irq_exit(flags);
  537. c->snd_nxt++;
  538. c->state = TCP_STATE_FIN_WAIT;
  539. while ((int)(get_ticks_ms() - deadline) < 0) {
  540. net_pump(4);
  541. if (c->peer_fin || c->state == TCP_STATE_CLOSED)
  542. break;
  543. process_sleep_ms(1);
  544. }
  545. }
  546. client_free(cl);
  547. }
  548. int tcp_client_peer_mss(unsigned int pid)
  549. {
  550. struct tcp_client *cl = client_find(pid);
  551. return cl ? (int)cl->c.peer_mss : -1;
  552. }
  553. void tcp_client_reap(unsigned int pid)
  554. {
  555. unsigned int i;
  556. for (i = 0; i < TCP_CLIENT_MAX; i++) {
  557. struct tcp_client *cl = &clients[i];
  558. struct tcp_conn *c = &cl->c;
  559. if (!cl->used || cl->owner != pid)
  560. continue;
  561. if (c->state != TCP_STATE_CLOSED) {
  562. unsigned int flags = net_irq_enter();
  563. tcp_send_segment(c, TCP_RST | TCP_ACK, c->snd_nxt,
  564. c->rcv_nxt, 0, 0, 0);
  565. net_irq_exit(flags);
  566. }
  567. client_free(cl);
  568. }
  569. }
  570. /* Generic receive used by the WebDAV server, which owns the raw NIC lease
  571. * and therefore polls the device directly instead of net_pump(). */
  572. int tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
  573. unsigned int deadline_ms)
  574. {
  575. const struct netdev *nd = netdev_active();
  576. unsigned char *rxbuf = netdev_rx_scratch();
  577. if (!nd || !c || !c->rxq)
  578. return -2;
  579. for (;;) {
  580. struct tcp_seg s;
  581. if (rxq_len(c)) {
  582. unsigned int n = rxq_len(c);
  583. if (n > maxlen)
  584. n = maxlen;
  585. rxq_get(c, out, n);
  586. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  587. 0, 0, 0);
  588. return (int)n;
  589. }
  590. if (c->peer_fin)
  591. return 0;
  592. if ((int)(get_ticks_ms() - deadline_ms) >= 0)
  593. return -1;
  594. {
  595. int len = nd->rx(rxbuf, NETDEV_FRAME_MAX);
  596. if (len < 0)
  597. continue;
  598. if (!tcp_parse(rxbuf, (unsigned int)len, c->dst_ip,
  599. c->dport, c->sport, &s)) {
  600. if (srv_port)
  601. tcp_srv_input(rxbuf, (unsigned int)len);
  602. continue;
  603. }
  604. }
  605. if ((unsigned)(s.ack - c->snd_una) <=
  606. (unsigned)(c->snd_nxt - c->snd_una))
  607. c->snd_una = s.ack;
  608. if (c->need_wnd && (s.flags & TCP_ACK)) {
  609. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  610. 0, 0, 0);
  611. c->need_wnd = 0;
  612. }
  613. if (tcp_rx_data(c, &s) == -2)
  614. return -2;
  615. }
  616. }
  617. /* ---- Server side ----
  618. *
  619. * One listening port, one active connection, two slots that hold a
  620. * handshake-complete connection until the command gets around to accepting
  621. * it. A closed connection lingers for a few seconds so the peer's FIN/ACK
  622. * is absorbed instead of answered with RST (libsoup treats a reset after a
  623. * complete response as an error). */
  624. #define TCP_SRV_SLOTS 4 /* 1 active + 3 backlog/closing */
  625. #define TCP_SRV_CLOSE_MS 2000
  626. #define TCP_SRV_BACKLOG_MS 30000
  627. enum {
  628. SRV_FREE = 0,
  629. SRV_BACKLOG,
  630. SRV_ACTIVE,
  631. SRV_CLOSING
  632. };
  633. #define SRV_PRE_MAX 1024
  634. struct tcp_srv {
  635. struct tcp_conn c;
  636. int used;
  637. int state;
  638. unsigned int last_ms;
  639. unsigned char pre[SRV_PRE_MAX]; /* first request, held until accept */
  640. unsigned int pre_len;
  641. };
  642. static struct tcp_srv srv[TCP_SRV_SLOTS];
  643. static int srv_active = -1;
  644. /* Only one server connection is active at a time, so the listener shares one
  645. * receive queue instead of embedding a 4 KB buffer in every slot. */
  646. static unsigned char srv_rxq[TCP_RXQ_SIZE];
  647. struct tcp_rx {
  648. unsigned int seq, ack;
  649. unsigned char flags;
  650. const unsigned char *data;
  651. unsigned int len;
  652. const unsigned char *tcp;
  653. unsigned int doff;
  654. unsigned char src_mac[6];
  655. unsigned char src_ip[4];
  656. unsigned int sport, dport;
  657. };
  658. /* Parse any IPv4/TCP frame addressed to us (no connection match). */
  659. static int tcp_parse_any(const unsigned char *f, unsigned int flen,
  660. struct tcp_rx *s)
  661. {
  662. const unsigned char *ip = f + 14;
  663. const unsigned char *tcp;
  664. unsigned char my_ip[4];
  665. unsigned int i, ihl, ip_len, doff;
  666. if (flen < 14 + 20)
  667. return 0;
  668. if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
  669. return 0;
  670. if ((ip[0] >> 4) != 4)
  671. return 0;
  672. ihl = (unsigned int)(ip[0] & 0x0F) * 4;
  673. if (ihl < 20 || ip[9] != IPPROTO_TCP)
  674. return 0;
  675. if ((ip[6] & 0x3F) != 0 || ip[7] != 0)
  676. return 0;
  677. ip_len = rd16(ip + 2);
  678. if (ip_len < ihl + 20 || ip_len > flen - 14)
  679. return 0;
  680. net_get_ip(my_ip);
  681. for (i = 0; i < 4; i++)
  682. if (ip[16 + i] != my_ip[i])
  683. return 0;
  684. tcp = ip + ihl;
  685. doff = (unsigned int)(tcp[12] >> 4) * 4;
  686. if (doff < 20 || ihl + doff > ip_len)
  687. return 0;
  688. for (i = 0; i < 6; i++)
  689. s->src_mac[i] = f[6 + i];
  690. for (i = 0; i < 4; i++)
  691. s->src_ip[i] = ip[12 + i];
  692. s->sport = rd16(tcp);
  693. s->dport = rd16(tcp + 2);
  694. s->seq = rd32(tcp + 4);
  695. s->ack = rd32(tcp + 8);
  696. s->flags = tcp[13];
  697. s->data = tcp + doff;
  698. s->len = ip_len - ihl - doff;
  699. s->tcp = tcp;
  700. s->doff = doff;
  701. return 1;
  702. }
  703. static int srv_match(const struct tcp_conn *c, const struct tcp_rx *s)
  704. {
  705. unsigned int i;
  706. if (s->sport != c->dport || s->dport != c->sport)
  707. return 0;
  708. for (i = 0; i < 4; i++)
  709. if (s->src_ip[i] != c->dst_ip[i])
  710. return 0;
  711. return 1;
  712. }
  713. static void srv_clear(int i)
  714. {
  715. srv[i].used = 0;
  716. srv[i].state = SRV_FREE;
  717. srv[i].pre_len = 0;
  718. }
  719. static void srv_expire(void)
  720. {
  721. unsigned int now = get_ticks_ms();
  722. int i;
  723. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  724. if (!srv[i].used)
  725. continue;
  726. if (srv[i].state == SRV_CLOSING &&
  727. now - srv[i].last_ms >= TCP_SRV_CLOSE_MS) {
  728. srv_clear(i);
  729. if (srv_active == i)
  730. srv_active = -1;
  731. } else if (srv[i].state == SRV_BACKLOG &&
  732. now - srv[i].last_ms >= TCP_SRV_BACKLOG_MS) {
  733. srv_clear(i);
  734. }
  735. }
  736. }
  737. static unsigned int srv_isn(void)
  738. {
  739. return (get_ticks_ms() * 1103515245u) ^ 0x5EED0000u;
  740. }
  741. static void srv_resend_synack(struct tcp_srv *e)
  742. {
  743. tcp_send_segment_wnd(&e->c, TCP_SYN | TCP_ACK, e->c.snd_una - 1,
  744. e->c.rcv_nxt, 0, 0, 1, 0);
  745. }
  746. /* Build a connection from a SYN and answer SYN-ACK. */
  747. static int srv_new(const struct tcp_rx *s)
  748. {
  749. struct tcp_srv *e;
  750. unsigned int mss = 536, i;
  751. int slot = -1;
  752. /* Retransmitted SYN: answer with the same ISN. */
  753. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  754. if (!srv[i].used || srv[i].state == SRV_CLOSING)
  755. continue;
  756. if (srv_match(&srv[i].c, s)) {
  757. srv_resend_synack(&srv[i]);
  758. return 1;
  759. }
  760. }
  761. srv_expire();
  762. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  763. /* A connection that already sent its response is finished
  764. * as far as new clients are concerned: reuse the slot rather
  765. * than make the next SYN wait for the linger to expire. */
  766. if (!srv[i].used || srv[i].state == SRV_CLOSING) {
  767. slot = (int)i;
  768. break;
  769. }
  770. }
  771. if (slot < 0)
  772. return 1; /* no room: drop, the client retries */
  773. e = &srv[slot];
  774. for (i = 0; i < 6; i++)
  775. e->c.dst_mac[i] = s->src_mac[i];
  776. for (i = 0; i < 4; i++)
  777. e->c.dst_ip[i] = s->src_ip[i];
  778. e->c.sport = srv_port;
  779. e->c.dport = s->sport;
  780. e->c.snd_una = e->c.snd_nxt = srv_isn();
  781. e->c.rcv_nxt = s->seq + 1;
  782. e->c.peer_fin = 0;
  783. e->c.need_wnd = 0;
  784. e->c.peer_mss = 536;
  785. e->c.peer_wnd = rd16(s->tcp + 14);
  786. if (!e->c.peer_wnd)
  787. e->c.peer_wnd = 4096;
  788. if (tcp_parse_mss(s->tcp, s->doff, &mss) &&
  789. mss >= 64 && mss <= TCP_MSS_OURS)
  790. e->c.peer_mss = mss;
  791. e->used = 1;
  792. e->state = SRV_BACKLOG;
  793. e->last_ms = get_ticks_ms();
  794. e->pre_len = 0;
  795. tcp_send_segment_wnd(&e->c, TCP_SYN | TCP_ACK, e->c.snd_nxt,
  796. e->c.rcv_nxt, 0, 0, 1, 0);
  797. /* The SYN consumed one sequence number. */
  798. e->c.snd_nxt++;
  799. e->c.snd_una = e->c.snd_nxt;
  800. return 1;
  801. }
  802. static void srv_send_rst(const struct tcp_rx *s)
  803. {
  804. struct tcp_conn c;
  805. unsigned int ack = s->seq + s->len;
  806. unsigned int i;
  807. if (s->flags & TCP_SYN)
  808. ack++;
  809. if (s->flags & TCP_FIN)
  810. ack++;
  811. for (i = 0; i < 6; i++)
  812. c.dst_mac[i] = s->src_mac[i];
  813. for (i = 0; i < 4; i++)
  814. c.dst_ip[i] = s->src_ip[i];
  815. c.sport = s->dport;
  816. c.dport = s->sport;
  817. c.snd_una = c.snd_nxt = s->ack;
  818. c.rcv_nxt = ack;
  819. c.peer_mss = 536;
  820. c.peer_fin = 0;
  821. tcp_send_segment_wnd(&c, TCP_RST | TCP_ACK, s->ack, ack, 0, 0, 0, 0);
  822. }
  823. static void srv_rx_data(struct tcp_conn *c, const struct tcp_rx *r)
  824. {
  825. struct tcp_seg s;
  826. s.seq = r->seq;
  827. s.ack = r->ack;
  828. s.flags = r->flags;
  829. s.data = r->data;
  830. s.len = r->len;
  831. s.tcp = r->tcp;
  832. s.doff = r->doff;
  833. tcp_rx_data(c, &s);
  834. }
  835. void tcp_srv_init(unsigned int port)
  836. {
  837. int i;
  838. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  839. srv[i].used = 0;
  840. srv[i].state = SRV_FREE;
  841. tcp_conn_init(&srv[i].c, srv_rxq, sizeof(srv_rxq));
  842. }
  843. srv_active = -1;
  844. srv_port = port;
  845. }
  846. void tcp_srv_reset(void)
  847. {
  848. tcp_srv_init(0);
  849. }
  850. int tcp_srv_input(const unsigned char *f, unsigned int len)
  851. {
  852. struct tcp_rx s;
  853. int i;
  854. if (!srv_port || !tcp_parse_any(f, len, &s))
  855. return 0;
  856. /* The accepted connection. */
  857. if (srv_active >= 0 && srv_match(&srv[srv_active].c, &s)) {
  858. struct tcp_conn *c = &srv[srv_active].c;
  859. if (c->need_wnd && (s.flags & TCP_ACK)) {
  860. tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
  861. 0, 0, 0);
  862. c->need_wnd = 0;
  863. }
  864. if (s.flags & TCP_RST) {
  865. srv_clear(srv_active);
  866. srv_active = -1;
  867. return 1;
  868. }
  869. if ((unsigned)(s.ack - c->snd_una) <=
  870. (unsigned)(c->snd_nxt - c->snd_una))
  871. c->snd_una = s.ack;
  872. srv_rx_data(c, &s);
  873. srv[srv_active].last_ms = get_ticks_ms();
  874. return 1;
  875. }
  876. /* A pending, closing or duplicate connection. */
  877. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  878. if (!srv[i].used || !srv_match(&srv[i].c, &s))
  879. continue;
  880. if (srv[i].state == SRV_CLOSING) {
  881. if (s.flags & TCP_FIN) {
  882. srv[i].c.rcv_nxt++;
  883. tcp_send_segment(&srv[i].c, TCP_ACK,
  884. srv[i].c.snd_nxt,
  885. srv[i].c.rcv_nxt, 0, 0, 0);
  886. }
  887. if (s.flags & TCP_RST)
  888. srv_clear(i);
  889. return 1;
  890. }
  891. if (s.flags & TCP_RST) {
  892. srv_clear(i);
  893. return 1;
  894. }
  895. if ((s.flags & (TCP_SYN | TCP_ACK | TCP_RST)) == TCP_SYN) {
  896. srv_resend_synack(&srv[i]);
  897. return 1;
  898. }
  899. if ((unsigned)(s.ack - srv[i].c.snd_una) <=
  900. (unsigned)(srv[i].c.snd_nxt - srv[i].c.snd_una))
  901. srv[i].c.snd_una = s.ack;
  902. /* Hold the first request (small HTTP headers) until the
  903. * command accepts this connection. ACK it so the client does
  904. * not have to wait for an RTO; if it does not fit, leave it
  905. * unacknowledged and the client retransmits after accept. */
  906. if (s.len && s.seq == srv[i].c.rcv_nxt &&
  907. s.len <= SRV_PRE_MAX - srv[i].pre_len) {
  908. unsigned int k;
  909. for (k = 0; k < s.len; k++)
  910. srv[i].pre[srv[i].pre_len + k] = s.data[k];
  911. srv[i].pre_len += s.len;
  912. srv[i].c.rcv_nxt += s.len;
  913. if ((s.flags & TCP_FIN) &&
  914. s.seq + s.len == srv[i].c.rcv_nxt) {
  915. srv[i].c.peer_fin = 1;
  916. srv[i].c.rcv_nxt++;
  917. }
  918. /* Keep the queued connection's window closed so it
  919. * does not send a body we have nowhere to put. */
  920. tcp_send_segment_wnd(&srv[i].c, TCP_ACK,
  921. srv[i].c.snd_nxt, srv[i].c.rcv_nxt,
  922. 0, 0, 0, 0);
  923. }
  924. srv[i].last_ms = get_ticks_ms();
  925. return 1;
  926. }
  927. /* A new connection. */
  928. if ((s.flags & (TCP_SYN | TCP_ACK | TCP_RST)) == TCP_SYN &&
  929. s.dport == srv_port)
  930. return srv_new(&s);
  931. /* TCP for us on a port we do not serve: refuse it. */
  932. srv_send_rst(&s);
  933. return 1;
  934. }
  935. struct tcp_conn *tcp_srv_accept(void)
  936. {
  937. int i, best = -1;
  938. if (srv_active >= 0)
  939. return &srv[srv_active].c;
  940. srv_expire();
  941. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  942. if (!srv[i].used || srv[i].state != SRV_BACKLOG)
  943. continue;
  944. if (best < 0 ||
  945. (int)(srv[i].last_ms - srv[best].last_ms) < 0)
  946. best = i;
  947. }
  948. if (best < 0)
  949. return 0;
  950. srv[best].state = SRV_ACTIVE;
  951. srv_active = best;
  952. srv[best].c.rxq_head = srv[best].c.rxq_tail = 0;
  953. if (srv[best].pre_len) {
  954. rxq_put(&srv[best].c, srv[best].pre, srv[best].pre_len);
  955. srv[best].pre_len = 0;
  956. }
  957. /* The SYN-ACK advertised a zero window while this connection waited
  958. * for the command; open it now with the real queue space. */
  959. tcp_send_segment(&srv[best].c, TCP_ACK, srv[best].c.snd_nxt,
  960. srv[best].c.rcv_nxt, 0, 0, 0);
  961. /* If this update raced ahead of the client's final ACK it may have
  962. * been discarded; repeat it when that ACK reaches us. */
  963. srv[best].c.need_wnd = 1;
  964. return &srv[best].c;
  965. }
  966. /* Send up to four segments before waiting for the cumulative ACK. Linux
  967. * delays the ACK of a lone small segment by up to 40 ms; a burst makes the
  968. * peer acknowledge every second segment immediately, which is the difference
  969. * between ~35 KB/s and a few MB/s on a LAN. The peer's advertised window
  970. * caps the burst. */
  971. int tcp_srv_send(struct tcp_conn *c, const void *data, unsigned int len)
  972. {
  973. const struct netdev *nd = netdev_active();
  974. unsigned char *rxbuf = netdev_rx_scratch();
  975. const unsigned char *p = data;
  976. unsigned int mss;
  977. if (!c || !nd)
  978. return -3;
  979. mss = c->peer_mss;
  980. if (mss < 64)
  981. mss = 64;
  982. while (len) {
  983. struct {
  984. unsigned int seq;
  985. unsigned int len;
  986. const unsigned char *data;
  987. } seg[4];
  988. unsigned int nseg = 0, off = 0, total = 0, cap, wnd;
  989. unsigned int tries, i;
  990. wnd = c->peer_wnd ? c->peer_wnd : 4096;
  991. cap = mss * 4;
  992. if (cap > wnd)
  993. cap = wnd;
  994. if (cap < mss)
  995. cap = mss;
  996. while (off < len && nseg < 4 && total < cap) {
  997. unsigned int n = len - off;
  998. if (n > mss)
  999. n = mss;
  1000. if (total + n > cap)
  1001. n = cap - total;
  1002. if (!n)
  1003. break;
  1004. seg[nseg].seq = c->snd_nxt;
  1005. seg[nseg].len = n;
  1006. seg[nseg].data = p + off;
  1007. if (tcp_send_segment(c, TCP_PSH | TCP_ACK, c->snd_nxt,
  1008. c->rcv_nxt, p + off, n, 0) != 0)
  1009. return -3;
  1010. c->snd_nxt += n;
  1011. off += n;
  1012. total += n;
  1013. nseg++;
  1014. }
  1015. for (tries = 0; ; tries++) {
  1016. unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
  1017. while ((int)(get_ticks_ms() - deadline) < 0) {
  1018. struct tcp_seg s;
  1019. int rlen = nd->rx(rxbuf, NETDEV_FRAME_MAX);
  1020. if (rlen < 0)
  1021. continue;
  1022. if (!tcp_parse(rxbuf, (unsigned int)rlen,
  1023. c->dst_ip, c->dport, c->sport, &s)) {
  1024. if (srv_port)
  1025. tcp_srv_input(rxbuf,
  1026. (unsigned int)rlen);
  1027. continue;
  1028. }
  1029. if (s.flags & TCP_RST)
  1030. return -2;
  1031. if ((unsigned)(s.ack - c->snd_una) <=
  1032. (unsigned)(c->snd_nxt - c->snd_una))
  1033. c->snd_una = s.ack;
  1034. c->peer_wnd = rd16(s.tcp + 14);
  1035. if ((int)(s.ack - c->snd_nxt) >= 0)
  1036. goto burst_done;
  1037. tcp_rx_data(c, &s);
  1038. }
  1039. if (tries >= TCP_RETRIES)
  1040. return -1;
  1041. /* Retransmit the still-unacknowledged part of this
  1042. * burst (duplicates are harmless). */
  1043. for (i = 0; i < nseg; i++) {
  1044. if ((int)(seg[i].seq + seg[i].len -
  1045. c->snd_una) <= 0)
  1046. continue;
  1047. tcp_send_segment(c, TCP_PSH | TCP_ACK,
  1048. seg[i].seq, c->rcv_nxt,
  1049. seg[i].data, seg[i].len, 0);
  1050. }
  1051. }
  1052. burst_done:
  1053. p += off;
  1054. len -= off;
  1055. }
  1056. return 0;
  1057. }
  1058. void tcp_srv_close(struct tcp_conn *c)
  1059. {
  1060. int i;
  1061. if (!c)
  1062. return;
  1063. for (i = 0; i < TCP_SRV_SLOTS; i++) {
  1064. if (!srv[i].used || &srv[i].c != c)
  1065. continue;
  1066. tcp_send_segment(c, TCP_FIN | TCP_ACK, c->snd_nxt, c->rcv_nxt,
  1067. 0, 0, 0);
  1068. c->snd_nxt++;
  1069. srv[i].state = SRV_CLOSING;
  1070. srv[i].last_ms = get_ticks_ms();
  1071. if (srv_active == i)
  1072. srv_active = -1;
  1073. return;
  1074. }
  1075. }