| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- /* Minimal TCP client: see tcp.h. */
- #include "io.h"
- #include "timer.h"
- #include "netdev.h"
- #include "net.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
- #define TCP_RETRY_MS 1000
- #define TCP_RETRIES 3
- /* 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];
- /* Payload of a segment larger than the caller's buffer is held here, so no
- * bytes are lost; at most one segment can be outstanding. */
- static unsigned char hold_buf[TCP_MSS_OURS];
- static unsigned int hold_len;
- static unsigned int hold_pos;
- 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(struct tcp_conn *c, unsigned char flags,
- unsigned int seq, unsigned int ack,
- const unsigned char *payload, unsigned int len,
- int with_mss)
- {
- 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, TCP_WINDOW);
- 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);
- }
- /* 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;
- }
- int tcp_connect(const unsigned char *dst_mac, const unsigned char *dst_ip,
- unsigned int dport, struct tcp_conn *c)
- {
- const struct netdev *nd = netdev_active();
- unsigned char *rxbuf = netdev_rx_scratch();
- unsigned int tries, i;
- if (!nd || !net_is_configured())
- return -3;
- for (i = 0; i < 6; i++)
- c->dst_mac[i] = dst_mac[i];
- for (i = 0; i < 4; i++)
- c->dst_ip[i] = dst_ip[i];
- c->dport = dport;
- c->sport = 0xC000u | (get_ticks_ms() & 0x3FFFu);
- c->snd_una = c->snd_nxt = (get_ticks_ms() << 16) ^ 0x5EED0000u;
- c->rcv_nxt = 0;
- c->peer_mss = 536;
- c->peer_fin = 0;
- hold_len = hold_pos = 0;
- for (tries = 0; tries < TCP_RETRIES; tries++) {
- unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
- if (tcp_send_segment(c, TCP_SYN, c->snd_nxt, 0, 0, 0, 1) != 0)
- return -3;
- while ((int)(get_ticks_ms() - deadline) < 0) {
- struct tcp_seg s;
- 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))
- continue;
- if (s.flags & TCP_RST)
- return -2;
- 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;
- if (tcp_parse_mss(s.tcp, s.doff, &mss) &&
- mss >= 64 && mss <= TCP_MSS_OURS)
- c->peer_mss = mss;
- if (tcp_send_segment(c, TCP_ACK, c->snd_nxt,
- c->rcv_nxt, 0, 0, 0) != 0)
- return -3;
- return 0;
- }
- }
- }
- return -1;
- }
- int tcp_send(struct tcp_conn *c, const void *data, unsigned int len)
- {
- const struct netdev *nd = netdev_active();
- unsigned char *rxbuf = netdev_rx_scratch();
- unsigned int tries;
- if (!nd || len > c->peer_mss)
- return -3;
- for (tries = 0; tries < TCP_RETRIES; tries++) {
- unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
- if (tcp_send_segment(c, TCP_PSH | TCP_ACK, c->snd_nxt,
- c->rcv_nxt, (const unsigned char *)data,
- len, 0) != 0)
- return -3;
- 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))
- continue;
- if (s.flags & TCP_RST)
- return -2;
- if (s.ack == c->snd_nxt + len) {
- c->snd_una = s.ack;
- c->snd_nxt = s.ack;
- return 0;
- }
- /* Data before the request is fully acked: ACK it and
- * drop it; the peer retransmits if it cares. */
- if (s.len) {
- if (s.seq == c->rcv_nxt)
- c->rcv_nxt += s.len;
- tcp_send_segment(c, TCP_ACK, c->snd_nxt,
- c->rcv_nxt, 0, 0, 0);
- }
- }
- }
- return -1;
- }
- 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)
- return -2;
- /* Drain a partially consumed segment first. */
- if (hold_pos < hold_len) {
- unsigned int n = hold_len - hold_pos;
- unsigned int i;
- if (n > maxlen)
- n = maxlen;
- for (i = 0; i < n; i++)
- out[i] = hold_buf[hold_pos + i];
- hold_pos += n;
- return (int)n;
- }
- if (c->peer_fin)
- return 0;
- while ((int)(get_ticks_ms() - deadline_ms) < 0) {
- struct tcp_seg s;
- 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))
- continue;
- if (s.flags & TCP_RST)
- return -2;
- /* Track our send window (nothing pending here, but keeps
- * snd_una meaningful for the ACKs we emit). */
- if ((unsigned)(s.ack - c->snd_una) <=
- (unsigned)(c->snd_nxt - c->snd_una))
- c->snd_una = s.ack;
- if (s.len) {
- if (s.len > sizeof(hold_buf)) /* not ours to store */
- continue;
- if (s.seq == c->rcv_nxt) {
- unsigned int take = s.len < maxlen ? s.len : maxlen;
- unsigned int i;
- for (i = 0; i < take; i++)
- out[i] = s.data[i];
- c->rcv_nxt += s.len;
- if (s.len > take) { /* hold the rest */
- for (i = take; i < s.len; i++)
- hold_buf[i - take] = s.data[i];
- hold_len = s.len - take;
- hold_pos = 0;
- }
- /* A FIN riding on the data consumes the next
- * sequence number after the payload. */
- if ((s.flags & TCP_FIN) && !c->peer_fin) {
- c->peer_fin = 1;
- c->rcv_nxt++;
- }
- tcp_send_segment(c, TCP_ACK, c->snd_nxt,
- c->rcv_nxt, 0, 0, 0);
- return (int)take;
- }
- /* Duplicate or out-of-order data: re-ACK to resync. */
- tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
- 0, 0, 0);
- continue;
- }
- if (s.flags & TCP_FIN) {
- if (s.seq == c->rcv_nxt) {
- c->rcv_nxt++;
- c->peer_fin = 1;
- tcp_send_segment(c, TCP_ACK, c->snd_nxt,
- c->rcv_nxt, 0, 0, 0);
- return 0;
- }
- }
- }
- return -1;
- }
- void tcp_close(struct tcp_conn *c)
- {
- const struct netdev *nd = netdev_active();
- unsigned char *rxbuf = netdev_rx_scratch();
- unsigned int fin_seq = c->snd_nxt;
- unsigned int tries;
- if (!nd)
- return;
- for (tries = 0; tries < 2; tries++) {
- unsigned int deadline = get_ticks_ms() + 500;
- struct tcp_seg s;
- if (tcp_send_segment(c, TCP_FIN | TCP_ACK, fin_seq,
- c->rcv_nxt, 0, 0, 0) != 0)
- return;
- c->snd_nxt = fin_seq + 1;
- while ((int)(get_ticks_ms() - deadline) < 0) {
- 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))
- continue;
- if (s.flags & TCP_RST)
- return;
- /* Our FIN acked (or their FIN first). */
- if ((s.flags & TCP_FIN) && s.seq == c->rcv_nxt) {
- c->rcv_nxt++;
- c->peer_fin = 1;
- tcp_send_segment(c, TCP_ACK, c->snd_nxt,
- c->rcv_nxt, 0, 0, 0);
- return;
- }
- if (s.ack == c->snd_nxt)
- return;
- }
- }
- }
|