| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276 |
- /* Minimal TCP client: see tcp.h. */
- #include "io.h"
- #include "timer.h"
- #include "mm.h"
- #include "netcore.h"
- #include "netdev.h"
- #include "net.h"
- #include "process.h"
- #include "tcp.h"
- /* TCP flags. */
- #define TCP_FIN 0x01
- #define TCP_SYN 0x02
- #define TCP_RST 0x04
- #define TCP_PSH 0x08
- #define TCP_ACK 0x10
- #define TCP_MSS_OURS 1460
- #define TCP_WINDOW 4096 /* fallback only; clients use rxq_mask */
- #define TCP_RETRY_MS 500
- #define TCP_RETRIES 3
- /* Client sockets: one per process, plus a little headroom. Each connection
- * gets a 64 KB receive queue from the frame allocator, which fills the
- * unscaled 16-bit window. Less is used when the allocator cannot find a
- * contiguous run. 64 KB / RTT instead of 4 KB / RTT is the difference
- * between ~130 KB/s and ~2 MB/s at a 30 ms round trip. */
- #define TCP_CLIENT_MAX 12
- #define TCP_RXQ_PAGES 16
- #define TCP_SRV_RXQ_SIZE 4096
- /* Listening port for the server side (0 = not listening). Declared here
- * because the shared receive loop feeds non-matching frames to the listener. */
- static unsigned int srv_port;
- /* Frame buffers are file scope: a 4 KB process stack cannot hold them. The
- * receive side uses the shared frame scratch (netdev_rx_scratch()). */
- static unsigned char tx_frame[14 + 20 + 20 + TCP_MSS_OURS];
- /* In-order payload accepted from the peer but not yet handed to the
- * application. Every connection owns a queue, so one socket's data can
- * never be read or reset by another. Data can arrive while the app is inside
- * a send (a TLS server sends session tickets right after the handshake), so
- * it must be buffered, never ACKed and dropped. A segment that does not fit
- * is left unacknowledged and the peer retransmits it. */
- void tcp_conn_init(struct tcp_conn *c, unsigned char *buf, unsigned int size)
- {
- c->rxq = buf;
- c->rxq_mask = size - 1;
- c->rxq_head = c->rxq_tail = 0;
- }
- static unsigned int rxq_len(const struct tcp_conn *c)
- {
- return (c->rxq_head - c->rxq_tail) & c->rxq_mask;
- }
- static unsigned int rxq_space(const struct tcp_conn *c)
- {
- return c->rxq_mask - rxq_len(c);
- }
- static void rxq_put(struct tcp_conn *c, const unsigned char *p,
- unsigned int n)
- {
- while (n--) {
- c->rxq[c->rxq_head] = *p++;
- c->rxq_head = (c->rxq_head + 1) & c->rxq_mask;
- }
- }
- static void rxq_get(struct tcp_conn *c, unsigned char *p, unsigned int n)
- {
- while (n--) {
- *p++ = c->rxq[c->rxq_tail];
- c->rxq_tail = (c->rxq_tail + 1) & c->rxq_mask;
- }
- }
- static unsigned int rd16(const unsigned char *p)
- {
- return ((unsigned int)p[0] << 8) | p[1];
- }
- static void wr16(unsigned char *p, unsigned int v)
- {
- p[0] = (unsigned char)(v >> 8);
- p[1] = (unsigned char)v;
- }
- static unsigned int rd32(const unsigned char *p)
- {
- return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) |
- ((unsigned int)p[2] << 8) | p[3];
- }
- static void wr32(unsigned char *p, unsigned int v)
- {
- p[0] = (unsigned char)(v >> 24);
- p[1] = (unsigned char)(v >> 16);
- p[2] = (unsigned char)(v >> 8);
- p[3] = (unsigned char)v;
- }
- /* Build and transmit one segment. `seq` is the sequence of the first byte
- * (for SYN/FIN that is the flag's own sequence). Returns 0 / -1 (tx). */
- static int tcp_send_segment_wnd(struct tcp_conn *c, unsigned char flags,
- unsigned int seq, unsigned int ack,
- const unsigned char *payload, unsigned int len,
- int with_mss, unsigned int wnd)
- {
- const struct netdev *nd = netdev_active();
- unsigned char my_mac[6], my_ip[4];
- unsigned char *ip = tx_frame + 14;
- unsigned char *tcp = ip + 20;
- unsigned short cs;
- unsigned int total, optlen = 0, i;
- if (!nd)
- return -1;
- nd->get_mac(my_mac);
- net_get_ip(my_ip);
- /* Ethernet. */
- for (i = 0; i < 6; i++)
- tx_frame[i] = c->dst_mac[i];
- for (i = 0; i < 6; i++)
- tx_frame[6 + i] = my_mac[i];
- tx_frame[12] = 0x08;
- tx_frame[13] = 0x00;
- /* IPv4 header (IHL 5, no options). */
- total = 20 + 20 + (with_mss ? 4 : 0) + len;
- for (i = 0; i < 20; i++)
- ip[i] = 0;
- ip[0] = 0x45;
- wr16(ip + 2, total);
- ip[8] = 64; /* TTL */
- ip[9] = IPPROTO_TCP;
- for (i = 0; i < 4; i++)
- ip[12 + i] = my_ip[i];
- for (i = 0; i < 4; i++)
- ip[16 + i] = c->dst_ip[i];
- cs = net_checksum(ip, 20);
- wr16(ip + 10, cs);
- /* TCP header (+ MSS option on SYN). */
- if (with_mss) {
- tcp[20] = 2; /* kind: MSS */
- tcp[21] = 4; /* length */
- wr16(tcp + 22, TCP_MSS_OURS);
- optlen = 4;
- }
- wr16(tcp + 0, c->sport);
- wr16(tcp + 2, c->dport);
- wr32(tcp + 4, seq);
- wr32(tcp + 8, ack);
- tcp[12] = (unsigned char)((5 + optlen / 4) << 4);
- tcp[13] = flags;
- wr16(tcp + 14, wnd > 0xFFFFu ? 0xFFFFu : wnd);
- wr16(tcp + 16, 0); /* checksum */
- wr16(tcp + 18, 0); /* urgent pointer */
- for (i = 0; i < len; i++)
- tcp[20 + optlen + i] = payload[i];
- cs = net_checksum_tcp(my_ip, c->dst_ip, tcp, 20 + optlen + len);
- wr16(tcp + 16, cs);
- return nd->tx(tx_frame, 14 + total);
- }
- static int tcp_send_segment(struct tcp_conn *c, unsigned char flags,
- unsigned int seq, unsigned int ack,
- const unsigned char *payload, unsigned int len,
- int with_mss)
- {
- /* The SYN advertises the receive queue size; ACKs advertise the space
- * currently left in it. Both fit the 16-bit field because the queue
- * is at most 64 KB. */
- unsigned int wnd = with_mss ? (c->rxq_mask ? c->rxq_mask : TCP_WINDOW)
- : rxq_space(c);
- return tcp_send_segment_wnd(c, flags, seq, ack, payload, len,
- with_mss, wnd);
- }
- /* One received segment. */
- struct tcp_seg {
- unsigned int seq, ack;
- unsigned char flags;
- const unsigned char *data;
- unsigned int len;
- const unsigned char *tcp; /* for option parsing */
- unsigned int doff;
- };
- /* Parse one frame as a TCP segment from the peer. Returns 1 on match.
- * Incoming checksums are not validated (same as the UDP path). */
- static int tcp_parse(const unsigned char *f, unsigned int flen,
- const unsigned char *peer_ip,
- unsigned int peer_port, unsigned int local_port,
- struct tcp_seg *s)
- {
- const unsigned char *ip = f + 14;
- const unsigned char *tcp;
- unsigned char my_ip[4];
- unsigned int i, ihl, ip_len, doff;
- if (flen < 14 + 20)
- return 0;
- if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
- return 0;
- if ((ip[0] >> 4) != 4)
- return 0;
- ihl = (unsigned int)(ip[0] & 0x0F) * 4;
- if (ihl < 20 || ip[9] != IPPROTO_TCP)
- return 0;
- /* Fragments are not reassembled: MF or a nonzero offset. */
- if ((ip[6] & 0x3F) != 0 || ip[7] != 0)
- return 0;
- ip_len = rd16(ip + 2);
- if (ip_len < ihl + 20 || ip_len > flen - 14)
- return 0;
- net_get_ip(my_ip);
- for (i = 0; i < 4; i++) {
- if (ip[12 + i] != peer_ip[i])
- return 0;
- if (ip[16 + i] != my_ip[i])
- return 0;
- }
- tcp = ip + ihl;
- if (rd16(tcp + 0) != peer_port || rd16(tcp + 2) != local_port)
- return 0;
- doff = (unsigned int)(tcp[12] >> 4) * 4;
- if (doff < 20 || ihl + doff > ip_len)
- return 0;
- s->seq = rd32(tcp + 4);
- s->ack = rd32(tcp + 8);
- s->flags = tcp[13];
- s->data = tcp + doff;
- s->len = ip_len - ihl - doff;
- s->tcp = tcp;
- s->doff = doff;
- return 1;
- }
- /* Parse the peer's MSS option (default stays when absent); 1 if present. */
- static int tcp_parse_mss(const unsigned char *tcp, unsigned int doff,
- unsigned int *mss)
- {
- unsigned int o = 20;
- while (o < doff) {
- unsigned char kind = tcp[o];
- if (kind == 0) /* end of options */
- return 0;
- if (kind == 1) { /* NOP */
- o++;
- continue;
- }
- if (o + 1 >= doff)
- return 0;
- if (tcp[o + 1] < 2 || o + tcp[o + 1] > doff)
- return 0;
- if (kind == 2 && tcp[o + 1] == 4) {
- *mss = rd16(tcp + o + 2);
- return 1;
- }
- o += tcp[o + 1];
- }
- return 0;
- }
- /* Take an incoming segment's payload/FIN into the receive queue. Returns
- * -2 on RST, 0 otherwise. Duplicate/out-of-order data is re-ACKed to resync;
- * a segment that does not fit the queue is not ACKed at all. */
- static int tcp_rx_data(struct tcp_conn *c, const struct tcp_seg *s)
- {
- if (s->flags & TCP_RST)
- return -2;
- if (s->tcp)
- c->peer_wnd = rd16(s->tcp + 14);
- if (s->len) {
- if (s->seq == c->rcv_nxt && s->len <= rxq_space(c)) {
- rxq_put(c, s->data, s->len);
- c->rcv_nxt += s->len;
- tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
- 0, 0, 0);
- } else if (s->seq != c->rcv_nxt) {
- tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
- 0, 0, 0);
- }
- }
- if ((s->flags & TCP_FIN) && !c->peer_fin &&
- s->seq + s->len == c->rcv_nxt) {
- c->peer_fin = 1;
- c->rcv_nxt++;
- tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
- 0, 0, 0);
- }
- return 0;
- }
- /* ---- Client table ---- */
- struct tcp_client {
- struct tcp_conn c;
- unsigned int owner;
- int used;
- int err; /* last reset reason while a connect waits */
- };
- static struct tcp_client clients[TCP_CLIENT_MAX];
- static unsigned int next_sport = 0xC000;
- static struct tcp_client *client_find(unsigned int pid)
- {
- unsigned int i;
- for (i = 0; i < TCP_CLIENT_MAX; i++)
- if (clients[i].used && clients[i].owner == pid)
- return &clients[i];
- return 0;
- }
- int tcp_client_count(void)
- {
- unsigned int i, n = 0;
- for (i = 0; i < TCP_CLIENT_MAX; i++)
- if (clients[i].used)
- n++;
- return (int)n;
- }
- static int ip4_eq(const unsigned char *a, const unsigned char *b)
- {
- return a[0] == b[0] && a[1] == b[1] &&
- a[2] == b[2] && a[3] == b[3];
- }
- static unsigned int alloc_sport(const unsigned char *dst_ip,
- unsigned int dport)
- {
- unsigned int i, guard = 0;
- for (;;) {
- unsigned int sport = next_sport++;
- int used = 0;
- if (next_sport < 0xC000 || next_sport > 0xFFFE)
- next_sport = 0xC000;
- for (i = 0; i < TCP_CLIENT_MAX; i++) {
- if (!clients[i].used)
- continue;
- if (clients[i].c.sport == sport &&
- clients[i].c.dport == dport &&
- ip4_eq(clients[i].c.dst_ip, dst_ip)) {
- used = 1;
- break;
- }
- }
- if (!used || ++guard > 0x3FFF)
- return sport;
- }
- }
- static struct tcp_client *client_alloc(unsigned int pid)
- {
- static const unsigned int pages[] = {
- TCP_RXQ_PAGES, TCP_RXQ_PAGES / 2, TCP_RXQ_PAGES / 4
- };
- unsigned int i, j;
- for (i = 0; i < TCP_CLIENT_MAX; i++) {
- struct tcp_client *cl = &clients[i];
- unsigned char *q = 0;
- unsigned int bytes = 0;
- if (cl->used)
- continue;
- for (j = 0; j < sizeof(pages) / sizeof(pages[0]); j++) {
- q = (unsigned char *)mm_alloc_pages(pages[j]);
- if (q) {
- bytes = pages[j] * PAGE_SIZE;
- break;
- }
- }
- if (!q)
- return 0;
- tcp_conn_init(&cl->c, q, bytes);
- cl->used = 1;
- cl->owner = pid;
- cl->err = 0;
- return cl;
- }
- return 0;
- }
- static void client_free(struct tcp_client *cl)
- {
- if (cl->c.rxq) {
- mm_free_range((unsigned int)cl->c.rxq, cl->c.rxq_mask + 1);
- cl->c.rxq = 0;
- cl->c.rxq_mask = 0;
- }
- cl->used = 0;
- cl->owner = 0;
- cl->err = 0;
- cl->c.state = TCP_STATE_CLOSED;
- }
- /* Feed one frame to the client table. See tcp.h. */
- int tcp_client_input(const unsigned char *frame, unsigned int len)
- {
- unsigned int i;
- for (i = 0; i < TCP_CLIENT_MAX; i++) {
- struct tcp_client *cl = &clients[i];
- struct tcp_conn *c = &cl->c;
- struct tcp_seg s;
- if (!cl->used)
- continue;
- if (!tcp_parse(frame, len, c->dst_ip, c->dport, c->sport, &s))
- continue;
- if (s.flags & TCP_RST) {
- c->state = TCP_STATE_CLOSED;
- cl->err = -2;
- return 1;
- }
- if (c->state == TCP_STATE_SYN_SENT) {
- if ((s.flags & (TCP_SYN | TCP_ACK)) ==
- (TCP_SYN | TCP_ACK) &&
- s.ack == c->snd_nxt + 1) {
- unsigned int mss;
- c->rcv_nxt = s.seq + 1;
- c->snd_nxt++;
- c->snd_una = c->snd_nxt;
- c->peer_wnd = rd16(s.tcp + 14);
- if (tcp_parse_mss(s.tcp, s.doff, &mss) &&
- mss >= 64 && mss <= TCP_MSS_OURS)
- c->peer_mss = mss;
- tcp_send_segment(c, TCP_ACK, c->snd_nxt,
- c->rcv_nxt, 0, 0, 0);
- c->state = TCP_STATE_ESTABLISHED;
- cl->err = 0;
- }
- return 1;
- }
- if (c->state != TCP_STATE_ESTABLISHED &&
- c->state != TCP_STATE_FIN_WAIT)
- return 1;
- if (c->need_wnd && (s.flags & TCP_ACK)) {
- tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
- 0, 0, 0);
- c->need_wnd = 0;
- }
- if ((unsigned)(s.ack - c->snd_una) <=
- (unsigned)(c->snd_nxt - c->snd_una))
- c->snd_una = s.ack;
- c->peer_wnd = rd16(s.tcp + 14);
- if (tcp_rx_data(c, &s) == -2) {
- c->state = TCP_STATE_CLOSED;
- cl->err = -2;
- }
- return 1;
- }
- return 0;
- }
- int tcp_client_connect(unsigned int pid, const unsigned char mac[6],
- const unsigned char ip[4], unsigned int port)
- {
- const struct netdev *nd = netdev_active();
- struct tcp_client *cl;
- struct tcp_conn *c;
- unsigned int tries, i;
- if (client_find(pid))
- return -4;
- if (netdev_in_use())
- return -4;
- if (!nd || !net_is_configured())
- return -3;
- cl = client_alloc(pid);
- if (!cl)
- return -4;
- c = &cl->c;
- for (i = 0; i < 6; i++)
- c->dst_mac[i] = mac[i];
- for (i = 0; i < 4; i++)
- c->dst_ip[i] = ip[i];
- c->dport = port;
- c->sport = alloc_sport(ip, port);
- c->snd_una = c->snd_nxt =
- (get_ticks_ms() << 16) ^ (c->sport << 8) ^ 0x5EED0000u;
- c->rcv_nxt = 0;
- c->peer_mss = 536;
- c->peer_wnd = 4096;
- c->peer_fin = 0;
- c->need_wnd = 0;
- c->state = TCP_STATE_SYN_SENT;
- cl->err = 0;
- for (tries = 0; tries < TCP_RETRIES; tries++) {
- unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
- unsigned int flags;
- int r;
- /* A SYN-ACK may have arrived just after the previous
- * deadline check; do not send a second SYN on an open
- * connection. */
- if (c->state == TCP_STATE_ESTABLISHED)
- return 0;
- if (c->state == TCP_STATE_CLOSED) {
- r = cl->err ? cl->err : -2;
- client_free(cl);
- return r;
- }
- flags = net_irq_enter();
- r = tcp_send_segment(c, TCP_SYN, c->snd_nxt, 0, 0, 0, 1);
- net_irq_exit(flags);
- if (r != 0) {
- client_free(cl);
- return -3;
- }
- while ((int)(get_ticks_ms() - deadline) < 0) {
- net_pump(8);
- if (c->state == TCP_STATE_ESTABLISHED)
- return 0;
- if (c->state == TCP_STATE_CLOSED) {
- r = cl->err ? cl->err : -2;
- client_free(cl);
- return r;
- }
- process_sleep_ms(1);
- }
- }
- if (c->state == TCP_STATE_ESTABLISHED)
- return 0;
- if (c->state == TCP_STATE_CLOSED) {
- int err = cl->err ? cl->err : -2;
- client_free(cl);
- return err;
- }
- client_free(cl);
- return -1;
- }
- int tcp_client_send(unsigned int pid, const void *buf, unsigned int len)
- {
- struct tcp_client *cl = client_find(pid);
- struct tcp_conn *c;
- unsigned int seq;
- unsigned int tries;
- if (!cl)
- return -4;
- c = &cl->c;
- if (c->state != TCP_STATE_ESTABLISHED)
- return -4;
- if (len > c->peer_mss)
- return -3;
- seq = c->snd_nxt;
- c->snd_nxt = seq + len;
- for (tries = 0; tries < TCP_RETRIES; tries++) {
- unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
- unsigned int flags;
- int r;
- /* An ACK may have arrived just after the previous deadline
- * check; never resend the payload at a fresh sequence. */
- if ((int)(c->snd_una - (seq + len)) >= 0)
- return 0;
- flags = net_irq_enter();
- r = tcp_send_segment(c, TCP_PSH | TCP_ACK, seq, c->rcv_nxt,
- (const unsigned char *)buf, len, 0);
- net_irq_exit(flags);
- if (r != 0) {
- c->state = TCP_STATE_CLOSED;
- cl->err = -3;
- return -3;
- }
- while ((int)(get_ticks_ms() - deadline) < 0) {
- net_pump(8);
- if (c->state == TCP_STATE_CLOSED)
- return cl->err ? cl->err : -2;
- if ((int)(c->snd_una - (seq + len)) >= 0)
- return 0;
- process_sleep_ms(1);
- }
- /* Retransmit the whole segment at the same sequence. */
- }
- c->state = TCP_STATE_CLOSED;
- cl->err = -1;
- return -1;
- }
- int tcp_client_recv(unsigned int pid, void *vbuf, unsigned int max,
- unsigned int deadline_ms)
- {
- struct tcp_client *cl = client_find(pid);
- struct tcp_conn *c;
- unsigned char *buf = (unsigned char *)vbuf;
- if (!cl)
- return -4;
- c = &cl->c;
- for (;;) {
- if (rxq_len(c)) {
- unsigned int n = rxq_len(c);
- unsigned int flags;
- if (n > max)
- n = max;
- rxq_get(c, buf, n);
- flags = net_irq_enter();
- tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
- 0, 0, 0);
- net_irq_exit(flags);
- return (int)n;
- }
- if (c->peer_fin)
- return 0;
- if (c->state == TCP_STATE_CLOSED)
- return cl->err ? cl->err : -2;
- if ((int)(get_ticks_ms() - deadline_ms) >= 0)
- return -1;
- net_pump(8);
- if (!rxq_len(c) && c->state != TCP_STATE_CLOSED)
- process_sleep_ms(1);
- }
- }
- void tcp_client_close(unsigned int pid)
- {
- struct tcp_client *cl = client_find(pid);
- struct tcp_conn *c;
- if (!cl)
- return;
- c = &cl->c;
- /* Send our FIN and free the slot at once. Every send is synchronous,
- * so all our data is already acknowledged; waiting for the peer's FIN
- * only cost a round trip (up to 500 ms) per closed connection, which
- * every redirect hop and helper teardown paid. A late FIN/ACK
- * matches no client (source ports rotate) and is dropped. */
- if (c->state == TCP_STATE_ESTABLISHED) {
- unsigned int flags;
- flags = net_irq_enter();
- tcp_send_segment(c, TCP_FIN | TCP_ACK, c->snd_nxt,
- c->rcv_nxt, 0, 0, 0);
- net_irq_exit(flags);
- }
- client_free(cl);
- }
- int tcp_client_peer_mss(unsigned int pid)
- {
- struct tcp_client *cl = client_find(pid);
- return cl ? (int)cl->c.peer_mss : -1;
- }
- void tcp_client_reap(unsigned int pid)
- {
- unsigned int i;
- for (i = 0; i < TCP_CLIENT_MAX; i++) {
- struct tcp_client *cl = &clients[i];
- struct tcp_conn *c = &cl->c;
- if (!cl->used || cl->owner != pid)
- continue;
- if (c->state != TCP_STATE_CLOSED) {
- unsigned int flags = net_irq_enter();
- tcp_send_segment(c, TCP_RST | TCP_ACK, c->snd_nxt,
- c->rcv_nxt, 0, 0, 0);
- net_irq_exit(flags);
- }
- client_free(cl);
- }
- }
- /* Generic receive used by the WebDAV server, which owns the raw NIC lease
- * and therefore polls the device directly instead of net_pump(). */
- int tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
- unsigned int deadline_ms)
- {
- const struct netdev *nd = netdev_active();
- unsigned char *rxbuf = netdev_rx_scratch();
- if (!nd || !c || !c->rxq)
- return -2;
- for (;;) {
- struct tcp_seg s;
- if (rxq_len(c)) {
- unsigned int n = rxq_len(c);
- if (n > maxlen)
- n = maxlen;
- rxq_get(c, out, n);
- tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
- 0, 0, 0);
- return (int)n;
- }
- if (c->peer_fin)
- return 0;
- if ((int)(get_ticks_ms() - deadline_ms) >= 0)
- return -1;
- {
- int len = nd->rx(rxbuf, NETDEV_FRAME_MAX);
- if (len < 0)
- continue;
- if (!tcp_parse(rxbuf, (unsigned int)len, c->dst_ip,
- c->dport, c->sport, &s)) {
- if (srv_port)
- tcp_srv_input(rxbuf, (unsigned int)len);
- continue;
- }
- }
- if ((unsigned)(s.ack - c->snd_una) <=
- (unsigned)(c->snd_nxt - c->snd_una))
- c->snd_una = s.ack;
- if (c->need_wnd && (s.flags & TCP_ACK)) {
- tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
- 0, 0, 0);
- c->need_wnd = 0;
- }
- if (tcp_rx_data(c, &s) == -2)
- return -2;
- }
- }
- /* ---- Server side ----
- *
- * One listening port, one active connection, two slots that hold a
- * handshake-complete connection until the command gets around to accepting
- * it. A closed connection lingers for a few seconds so the peer's FIN/ACK
- * is absorbed instead of answered with RST (libsoup treats a reset after a
- * complete response as an error). */
- #define TCP_SRV_SLOTS 4 /* 1 active + 3 backlog/closing */
- #define TCP_SRV_CLOSE_MS 2000
- #define TCP_SRV_BACKLOG_MS 30000
- enum {
- SRV_FREE = 0,
- SRV_BACKLOG,
- SRV_ACTIVE,
- SRV_CLOSING
- };
- #define SRV_PRE_MAX 1024
- struct tcp_srv {
- struct tcp_conn c;
- int used;
- int state;
- unsigned int last_ms;
- unsigned char pre[SRV_PRE_MAX]; /* first request, held until accept */
- unsigned int pre_len;
- };
- static struct tcp_srv srv[TCP_SRV_SLOTS];
- static int srv_active = -1;
- /* Only one server connection is active at a time, so the listener shares one
- * receive queue instead of embedding a 4 KB buffer in every slot. */
- static unsigned char srv_rxq[TCP_SRV_RXQ_SIZE];
- struct tcp_rx {
- unsigned int seq, ack;
- unsigned char flags;
- const unsigned char *data;
- unsigned int len;
- const unsigned char *tcp;
- unsigned int doff;
- unsigned char src_mac[6];
- unsigned char src_ip[4];
- unsigned int sport, dport;
- };
- /* Parse any IPv4/TCP frame addressed to us (no connection match). */
- static int tcp_parse_any(const unsigned char *f, unsigned int flen,
- struct tcp_rx *s)
- {
- const unsigned char *ip = f + 14;
- const unsigned char *tcp;
- unsigned char my_ip[4];
- unsigned int i, ihl, ip_len, doff;
- if (flen < 14 + 20)
- return 0;
- if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
- return 0;
- if ((ip[0] >> 4) != 4)
- return 0;
- ihl = (unsigned int)(ip[0] & 0x0F) * 4;
- if (ihl < 20 || ip[9] != IPPROTO_TCP)
- return 0;
- if ((ip[6] & 0x3F) != 0 || ip[7] != 0)
- return 0;
- ip_len = rd16(ip + 2);
- if (ip_len < ihl + 20 || ip_len > flen - 14)
- return 0;
- net_get_ip(my_ip);
- for (i = 0; i < 4; i++)
- if (ip[16 + i] != my_ip[i])
- return 0;
- tcp = ip + ihl;
- doff = (unsigned int)(tcp[12] >> 4) * 4;
- if (doff < 20 || ihl + doff > ip_len)
- return 0;
- for (i = 0; i < 6; i++)
- s->src_mac[i] = f[6 + i];
- for (i = 0; i < 4; i++)
- s->src_ip[i] = ip[12 + i];
- s->sport = rd16(tcp);
- s->dport = rd16(tcp + 2);
- s->seq = rd32(tcp + 4);
- s->ack = rd32(tcp + 8);
- s->flags = tcp[13];
- s->data = tcp + doff;
- s->len = ip_len - ihl - doff;
- s->tcp = tcp;
- s->doff = doff;
- return 1;
- }
- static int srv_match(const struct tcp_conn *c, const struct tcp_rx *s)
- {
- unsigned int i;
- if (s->sport != c->dport || s->dport != c->sport)
- return 0;
- for (i = 0; i < 4; i++)
- if (s->src_ip[i] != c->dst_ip[i])
- return 0;
- return 1;
- }
- static void srv_clear(int i)
- {
- srv[i].used = 0;
- srv[i].state = SRV_FREE;
- srv[i].pre_len = 0;
- }
- static void srv_expire(void)
- {
- unsigned int now = get_ticks_ms();
- int i;
- for (i = 0; i < TCP_SRV_SLOTS; i++) {
- if (!srv[i].used)
- continue;
- if (srv[i].state == SRV_CLOSING &&
- now - srv[i].last_ms >= TCP_SRV_CLOSE_MS) {
- srv_clear(i);
- if (srv_active == i)
- srv_active = -1;
- } else if (srv[i].state == SRV_BACKLOG &&
- now - srv[i].last_ms >= TCP_SRV_BACKLOG_MS) {
- srv_clear(i);
- }
- }
- }
- static unsigned int srv_isn(void)
- {
- return (get_ticks_ms() * 1103515245u) ^ 0x5EED0000u;
- }
- static void srv_resend_synack(struct tcp_srv *e)
- {
- tcp_send_segment_wnd(&e->c, TCP_SYN | TCP_ACK, e->c.snd_una - 1,
- e->c.rcv_nxt, 0, 0, 1, 0);
- }
- /* Build a connection from a SYN and answer SYN-ACK. */
- static int srv_new(const struct tcp_rx *s)
- {
- struct tcp_srv *e;
- unsigned int mss = 536, i;
- int slot = -1;
- /* Retransmitted SYN: answer with the same ISN. */
- for (i = 0; i < TCP_SRV_SLOTS; i++) {
- if (!srv[i].used || srv[i].state == SRV_CLOSING)
- continue;
- if (srv_match(&srv[i].c, s)) {
- srv_resend_synack(&srv[i]);
- return 1;
- }
- }
- srv_expire();
- for (i = 0; i < TCP_SRV_SLOTS; i++) {
- /* A connection that already sent its response is finished
- * as far as new clients are concerned: reuse the slot rather
- * than make the next SYN wait for the linger to expire. */
- if (!srv[i].used || srv[i].state == SRV_CLOSING) {
- slot = (int)i;
- break;
- }
- }
- if (slot < 0)
- return 1; /* no room: drop, the client retries */
- e = &srv[slot];
- for (i = 0; i < 6; i++)
- e->c.dst_mac[i] = s->src_mac[i];
- for (i = 0; i < 4; i++)
- e->c.dst_ip[i] = s->src_ip[i];
- e->c.sport = srv_port;
- e->c.dport = s->sport;
- e->c.snd_una = e->c.snd_nxt = srv_isn();
- e->c.rcv_nxt = s->seq + 1;
- e->c.peer_fin = 0;
- e->c.need_wnd = 0;
- e->c.peer_mss = 536;
- e->c.peer_wnd = rd16(s->tcp + 14);
- if (!e->c.peer_wnd)
- e->c.peer_wnd = 4096;
- if (tcp_parse_mss(s->tcp, s->doff, &mss) &&
- mss >= 64 && mss <= TCP_MSS_OURS)
- e->c.peer_mss = mss;
- e->used = 1;
- e->state = SRV_BACKLOG;
- e->last_ms = get_ticks_ms();
- e->pre_len = 0;
- tcp_send_segment_wnd(&e->c, TCP_SYN | TCP_ACK, e->c.snd_nxt,
- e->c.rcv_nxt, 0, 0, 1, 0);
- /* The SYN consumed one sequence number. */
- e->c.snd_nxt++;
- e->c.snd_una = e->c.snd_nxt;
- return 1;
- }
- static void srv_send_rst(const struct tcp_rx *s)
- {
- struct tcp_conn c;
- unsigned int ack = s->seq + s->len;
- unsigned int i;
- if (s->flags & TCP_SYN)
- ack++;
- if (s->flags & TCP_FIN)
- ack++;
- for (i = 0; i < 6; i++)
- c.dst_mac[i] = s->src_mac[i];
- for (i = 0; i < 4; i++)
- c.dst_ip[i] = s->src_ip[i];
- c.sport = s->dport;
- c.dport = s->sport;
- c.snd_una = c.snd_nxt = s->ack;
- c.rcv_nxt = ack;
- c.peer_mss = 536;
- c.peer_fin = 0;
- tcp_send_segment_wnd(&c, TCP_RST | TCP_ACK, s->ack, ack, 0, 0, 0, 0);
- }
- static void srv_rx_data(struct tcp_conn *c, const struct tcp_rx *r)
- {
- struct tcp_seg s;
- s.seq = r->seq;
- s.ack = r->ack;
- s.flags = r->flags;
- s.data = r->data;
- s.len = r->len;
- s.tcp = r->tcp;
- s.doff = r->doff;
- tcp_rx_data(c, &s);
- }
- void tcp_srv_init(unsigned int port)
- {
- int i;
- for (i = 0; i < TCP_SRV_SLOTS; i++) {
- srv[i].used = 0;
- srv[i].state = SRV_FREE;
- tcp_conn_init(&srv[i].c, srv_rxq, sizeof(srv_rxq));
- }
- srv_active = -1;
- srv_port = port;
- }
- void tcp_srv_reset(void)
- {
- tcp_srv_init(0);
- }
- int tcp_srv_input(const unsigned char *f, unsigned int len)
- {
- struct tcp_rx s;
- int i;
- if (!srv_port || !tcp_parse_any(f, len, &s))
- return 0;
- /* The accepted connection. */
- if (srv_active >= 0 && srv_match(&srv[srv_active].c, &s)) {
- struct tcp_conn *c = &srv[srv_active].c;
- if (c->need_wnd && (s.flags & TCP_ACK)) {
- tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
- 0, 0, 0);
- c->need_wnd = 0;
- }
- if (s.flags & TCP_RST) {
- srv_clear(srv_active);
- srv_active = -1;
- return 1;
- }
- if ((unsigned)(s.ack - c->snd_una) <=
- (unsigned)(c->snd_nxt - c->snd_una))
- c->snd_una = s.ack;
- srv_rx_data(c, &s);
- srv[srv_active].last_ms = get_ticks_ms();
- return 1;
- }
- /* A pending, closing or duplicate connection. */
- for (i = 0; i < TCP_SRV_SLOTS; i++) {
- if (!srv[i].used || !srv_match(&srv[i].c, &s))
- continue;
- if (srv[i].state == SRV_CLOSING) {
- if (s.flags & TCP_FIN) {
- srv[i].c.rcv_nxt++;
- tcp_send_segment(&srv[i].c, TCP_ACK,
- srv[i].c.snd_nxt,
- srv[i].c.rcv_nxt, 0, 0, 0);
- }
- if (s.flags & TCP_RST)
- srv_clear(i);
- return 1;
- }
- if (s.flags & TCP_RST) {
- srv_clear(i);
- return 1;
- }
- if ((s.flags & (TCP_SYN | TCP_ACK | TCP_RST)) == TCP_SYN) {
- srv_resend_synack(&srv[i]);
- return 1;
- }
- if ((unsigned)(s.ack - srv[i].c.snd_una) <=
- (unsigned)(srv[i].c.snd_nxt - srv[i].c.snd_una))
- srv[i].c.snd_una = s.ack;
- /* Hold the first request (small HTTP headers) until the
- * command accepts this connection. ACK it so the client does
- * not have to wait for an RTO; if it does not fit, leave it
- * unacknowledged and the client retransmits after accept. */
- if (s.len && s.seq == srv[i].c.rcv_nxt &&
- s.len <= SRV_PRE_MAX - srv[i].pre_len) {
- unsigned int k;
- for (k = 0; k < s.len; k++)
- srv[i].pre[srv[i].pre_len + k] = s.data[k];
- srv[i].pre_len += s.len;
- srv[i].c.rcv_nxt += s.len;
- if ((s.flags & TCP_FIN) &&
- s.seq + s.len == srv[i].c.rcv_nxt) {
- srv[i].c.peer_fin = 1;
- srv[i].c.rcv_nxt++;
- }
- /* Keep the queued connection's window closed so it
- * does not send a body we have nowhere to put. */
- tcp_send_segment_wnd(&srv[i].c, TCP_ACK,
- srv[i].c.snd_nxt, srv[i].c.rcv_nxt,
- 0, 0, 0, 0);
- }
- srv[i].last_ms = get_ticks_ms();
- return 1;
- }
- /* A new connection. */
- if ((s.flags & (TCP_SYN | TCP_ACK | TCP_RST)) == TCP_SYN &&
- s.dport == srv_port)
- return srv_new(&s);
- /* TCP for us on a port we do not serve: refuse it. */
- srv_send_rst(&s);
- return 1;
- }
- struct tcp_conn *tcp_srv_accept(void)
- {
- int i, best = -1;
- if (srv_active >= 0)
- return &srv[srv_active].c;
- srv_expire();
- for (i = 0; i < TCP_SRV_SLOTS; i++) {
- if (!srv[i].used || srv[i].state != SRV_BACKLOG)
- continue;
- if (best < 0 ||
- (int)(srv[i].last_ms - srv[best].last_ms) < 0)
- best = i;
- }
- if (best < 0)
- return 0;
- srv[best].state = SRV_ACTIVE;
- srv_active = best;
- srv[best].c.rxq_head = srv[best].c.rxq_tail = 0;
- if (srv[best].pre_len) {
- rxq_put(&srv[best].c, srv[best].pre, srv[best].pre_len);
- srv[best].pre_len = 0;
- }
- /* The SYN-ACK advertised a zero window while this connection waited
- * for the command; open it now with the real queue space. */
- tcp_send_segment(&srv[best].c, TCP_ACK, srv[best].c.snd_nxt,
- srv[best].c.rcv_nxt, 0, 0, 0);
- /* If this update raced ahead of the client's final ACK it may have
- * been discarded; repeat it when that ACK reaches us. */
- srv[best].c.need_wnd = 1;
- return &srv[best].c;
- }
- /* Send up to four segments before waiting for the cumulative ACK. Linux
- * delays the ACK of a lone small segment by up to 40 ms; a burst makes the
- * peer acknowledge every second segment immediately, which is the difference
- * between ~35 KB/s and a few MB/s on a LAN. The peer's advertised window
- * caps the burst. */
- int tcp_srv_send(struct tcp_conn *c, const void *data, unsigned int len)
- {
- const struct netdev *nd = netdev_active();
- unsigned char *rxbuf = netdev_rx_scratch();
- const unsigned char *p = data;
- unsigned int mss;
- if (!c || !nd)
- return -3;
- mss = c->peer_mss;
- if (mss < 64)
- mss = 64;
- while (len) {
- struct {
- unsigned int seq;
- unsigned int len;
- const unsigned char *data;
- } seg[4];
- unsigned int nseg = 0, off = 0, total = 0, cap, wnd;
- unsigned int tries, i;
- wnd = c->peer_wnd ? c->peer_wnd : 4096;
- cap = mss * 4;
- if (cap > wnd)
- cap = wnd;
- if (cap < mss)
- cap = mss;
- while (off < len && nseg < 4 && total < cap) {
- unsigned int n = len - off;
- if (n > mss)
- n = mss;
- if (total + n > cap)
- n = cap - total;
- if (!n)
- break;
- seg[nseg].seq = c->snd_nxt;
- seg[nseg].len = n;
- seg[nseg].data = p + off;
- if (tcp_send_segment(c, TCP_PSH | TCP_ACK, c->snd_nxt,
- c->rcv_nxt, p + off, n, 0) != 0)
- return -3;
- c->snd_nxt += n;
- off += n;
- total += n;
- nseg++;
- }
- for (tries = 0; ; tries++) {
- unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
- while ((int)(get_ticks_ms() - deadline) < 0) {
- struct tcp_seg s;
- int rlen = nd->rx(rxbuf, NETDEV_FRAME_MAX);
- if (rlen < 0)
- continue;
- if (!tcp_parse(rxbuf, (unsigned int)rlen,
- c->dst_ip, c->dport, c->sport, &s)) {
- if (srv_port)
- tcp_srv_input(rxbuf,
- (unsigned int)rlen);
- continue;
- }
- if (s.flags & TCP_RST)
- return -2;
- if ((unsigned)(s.ack - c->snd_una) <=
- (unsigned)(c->snd_nxt - c->snd_una))
- c->snd_una = s.ack;
- c->peer_wnd = rd16(s.tcp + 14);
- if ((int)(s.ack - c->snd_nxt) >= 0)
- goto burst_done;
- tcp_rx_data(c, &s);
- }
- if (tries >= TCP_RETRIES)
- return -1;
- /* Retransmit the still-unacknowledged part of this
- * burst (duplicates are harmless). */
- for (i = 0; i < nseg; i++) {
- if ((int)(seg[i].seq + seg[i].len -
- c->snd_una) <= 0)
- continue;
- tcp_send_segment(c, TCP_PSH | TCP_ACK,
- seg[i].seq, c->rcv_nxt,
- seg[i].data, seg[i].len, 0);
- }
- }
- burst_done:
- p += off;
- len -= off;
- }
- return 0;
- }
- void tcp_srv_close(struct tcp_conn *c)
- {
- int i;
- if (!c)
- return;
- for (i = 0; i < TCP_SRV_SLOTS; i++) {
- if (!srv[i].used || &srv[i].c != c)
- continue;
- tcp_send_segment(c, TCP_FIN | TCP_ACK, c->snd_nxt, c->rcv_nxt,
- 0, 0, 0);
- c->snd_nxt++;
- srv[i].state = SRV_CLOSING;
- srv[i].last_ms = get_ticks_ms();
- if (srv_active == i)
- srv_active = -1;
- return;
- }
- }
|