2 utcp.c -- Userspace TCP
3 Copyright (C) 2014-2017 Guus Sliepen <guus@tinc-vpn.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include "utcp_priv.h"
46 static void timespec_sub(const struct timespec *a, const struct timespec *b, struct timespec *r) {
47 r->tv_sec = a->tv_sec - b->tv_sec;
48 r->tv_nsec = a->tv_nsec - b->tv_nsec;
51 r->tv_sec--, r->tv_nsec += NSEC_PER_SEC;
55 static int32_t timespec_diff_usec(const struct timespec *a, const struct timespec *b) {
56 int64_t diff = (a->tv_sec - b->tv_sec) * 1000000000 + a->tv_sec - b->tv_sec;
60 static bool timespec_lt(const struct timespec *a, const struct timespec *b) {
61 if(a->tv_sec == b->tv_sec) {
62 return a->tv_nsec < b->tv_nsec;
64 return a->tv_sec < b->tv_sec;
68 static void timespec_clear(struct timespec *a) {
72 static bool timespec_isset(const struct timespec *a) {
76 static long CLOCK_GRANULARITY;
78 static inline size_t min(size_t a, size_t b) {
82 static inline size_t max(size_t a, size_t b) {
89 #ifndef UTCP_DEBUG_DATALEN
90 #define UTCP_DEBUG_DATALEN 20
94 #if defined(CLOCK_MONOTONIC_RAW) && defined(__x86_64__)
95 #define UTCP_CLOCK CLOCK_MONOTONIC_RAW
97 #define UTCP_CLOCK CLOCK_MONOTONIC
101 static void debug(struct utcp_connection *c, const char *format, ...) {
106 clock_gettime(CLOCK_REALTIME, &tv);
107 len = snprintf(buf, sizeof(buf), "%ld.%06lu %u:%u ", (long)tv.tv_sec, tv.tv_nsec / 1000, c ? c->src : 0, c ? c->dst : 0);
109 va_start(ap, format);
110 len += vsnprintf(buf + len, sizeof(buf) - len, format, ap);
113 if(len > 0 && (size_t)len < sizeof(buf)) {
114 fwrite(buf, len, 1, stderr);
118 static void print_packet(struct utcp_connection *c, const char *dir, const void *pkt, size_t len) {
121 if(len < sizeof(hdr)) {
122 debug(c, "%s: short packet (%lu bytes)\n", dir, (unsigned long)len);
126 memcpy(&hdr, pkt, sizeof(hdr));
130 if(len > sizeof(hdr)) {
131 datalen = min(len - sizeof(hdr), UTCP_DEBUG_DATALEN);
137 const uint8_t *data = (uint8_t *)pkt + sizeof(hdr);
138 char str[datalen * 2 + 1];
141 for(uint32_t i = 0; i < datalen; i++) {
142 *p++ = "0123456789ABCDEF"[data[i] >> 4];
143 *p++ = "0123456789ABCDEF"[data[i] & 15];
148 debug(c, "%s: len %lu src %u dst %u seq %u ack %u wnd %u aux %x ctl %s%s%s%s data %s\n",
149 dir, (unsigned long)len, hdr.src, hdr.dst, hdr.seq, hdr.ack, hdr.wnd, hdr.aux,
150 hdr.ctl & SYN ? "SYN" : "",
151 hdr.ctl & RST ? "RST" : "",
152 hdr.ctl & FIN ? "FIN" : "",
153 hdr.ctl & ACK ? "ACK" : "",
158 static void debug_cwnd(struct utcp_connection *c) {
159 debug(c, "snd.cwnd %u snd.ssthresh %u\n", c->snd.cwnd, ~c->snd.ssthresh ? c->snd.ssthresh : 0);
162 #define debug(...) do {} while(0)
163 #define print_packet(...) do {} while(0)
164 #define debug_cwnd(...) do {} while(0)
167 static void set_state(struct utcp_connection *c, enum state state) {
170 if(state == ESTABLISHED) {
171 timespec_clear(&c->conn_timeout);
174 debug(c, "state %s\n", strstate[state]);
177 static bool fin_wanted(struct utcp_connection *c, uint32_t seq) {
178 if(seq != c->snd.last) {
193 static bool is_reliable(struct utcp_connection *c) {
194 return c->flags & UTCP_RELIABLE;
197 static int32_t seqdiff(uint32_t a, uint32_t b) {
202 static bool buffer_wraps(struct buffer *buf) {
203 return buf->size - buf->offset < buf->used;
206 static bool buffer_resize(struct buffer *buf, uint32_t newsize) {
207 char *newdata = realloc(buf->data, newsize);
215 if(buffer_wraps(buf)) {
216 // Shift the right part of the buffer until it hits the end of the new buffer.
220 // [345.........|........012]
221 uint32_t tailsize = buf->size - buf->offset;
222 uint32_t newoffset = newsize - tailsize;
223 memmove(buf + newoffset, buf + buf->offset, tailsize);
224 buf->offset = newoffset;
231 // Store data into the buffer
232 static ssize_t buffer_put_at(struct buffer *buf, size_t offset, const void *data, size_t len) {
233 debug(NULL, "buffer_put_at %lu %lu %lu\n", (unsigned long)buf->used, (unsigned long)offset, (unsigned long)len);
235 // Ensure we don't store more than maxsize bytes in total
236 size_t required = offset + len;
238 if(required > buf->maxsize) {
239 if(offset >= buf->maxsize) {
243 len = buf->maxsize - offset;
244 required = buf->maxsize;
247 // Check if we need to resize the buffer
248 if(required > buf->size) {
249 size_t newsize = buf->size;
257 } while(newsize < required);
259 if(newsize > buf->maxsize) {
260 newsize = buf->maxsize;
263 if(!buffer_resize(buf, newsize)) {
268 uint32_t realoffset = buf->offset + offset;
270 if(buf->size - buf->offset < offset) {
271 // The offset wrapped
272 realoffset -= buf->size;
275 if(buf->size - realoffset < len) {
276 // The new chunk of data must be wrapped
277 memcpy(buf->data + realoffset, data, buf->size - realoffset);
278 memcpy(buf->data, (char *)data + buf->size - realoffset, len - (buf->size - realoffset));
280 memcpy(buf->data + realoffset, data, len);
283 if(required > buf->used) {
284 buf->used = required;
290 static ssize_t buffer_put(struct buffer *buf, const void *data, size_t len) {
291 return buffer_put_at(buf, buf->used, data, len);
294 // Copy data from the buffer without removing it.
295 static ssize_t buffer_copy(struct buffer *buf, void *data, size_t offset, size_t len) {
296 // Ensure we don't copy more than is actually stored in the buffer
297 if(offset >= buf->used) {
301 if(buf->used - offset < len) {
302 len = buf->used - offset;
305 uint32_t realoffset = buf->offset + offset;
307 if(buf->size - buf->offset < offset) {
308 // The offset wrapped
309 realoffset -= buf->size;
312 if(buf->size - realoffset < len) {
313 // The data is wrapped
314 memcpy(data, buf->data + realoffset, buf->size - realoffset);
315 memcpy((char *)data + buf->size - realoffset, buf->data, len - (buf->size - realoffset));
317 memcpy(data, buf->data + realoffset, len);
323 // Discard data from the buffer.
324 static ssize_t buffer_discard(struct buffer *buf, size_t len) {
325 if(buf->used < len) {
329 if(buf->size - buf->offset < len) {
330 buf->offset -= buf->size;
339 static bool buffer_set_size(struct buffer *buf, uint32_t minsize, uint32_t maxsize) {
340 if(maxsize < minsize) {
344 buf->maxsize = maxsize;
346 return buf->size >= minsize || buffer_resize(buf, minsize);
349 static void buffer_exit(struct buffer *buf) {
351 memset(buf, 0, sizeof(*buf));
354 static uint32_t buffer_free(const struct buffer *buf) {
355 return buf->maxsize - buf->used;
358 // Connections are stored in a sorted list.
359 // This gives O(log(N)) lookup time, O(N log(N)) insertion time and O(N) deletion time.
361 static int compare(const void *va, const void *vb) {
364 const struct utcp_connection *a = *(struct utcp_connection **)va;
365 const struct utcp_connection *b = *(struct utcp_connection **)vb;
368 assert(a->src && b->src);
370 int c = (int)a->src - (int)b->src;
376 c = (int)a->dst - (int)b->dst;
380 static struct utcp_connection *find_connection(const struct utcp *utcp, uint16_t src, uint16_t dst) {
381 if(!utcp->nconnections) {
385 struct utcp_connection key = {
389 struct utcp_connection **match = bsearch(&keyp, utcp->connections, utcp->nconnections, sizeof(*utcp->connections), compare);
390 return match ? *match : NULL;
393 static void free_connection(struct utcp_connection *c) {
394 struct utcp *utcp = c->utcp;
395 struct utcp_connection **cp = bsearch(&c, utcp->connections, utcp->nconnections, sizeof(*utcp->connections), compare);
399 int i = cp - utcp->connections;
400 memmove(cp, cp + 1, (utcp->nconnections - i - 1) * sizeof(*cp));
401 utcp->nconnections--;
403 buffer_exit(&c->rcvbuf);
404 buffer_exit(&c->sndbuf);
408 static struct utcp_connection *allocate_connection(struct utcp *utcp, uint16_t src, uint16_t dst) {
409 // Check whether this combination of src and dst is free
412 if(find_connection(utcp, src, dst)) {
416 } else { // If src == 0, generate a random port number with the high bit set
417 if(utcp->nconnections >= 32767) {
422 src = rand() | 0x8000;
424 while(find_connection(utcp, src, dst)) {
429 // Allocate memory for the new connection
431 if(utcp->nconnections >= utcp->nallocated) {
432 if(!utcp->nallocated) {
433 utcp->nallocated = 4;
435 utcp->nallocated *= 2;
438 struct utcp_connection **new_array = realloc(utcp->connections, utcp->nallocated * sizeof(*utcp->connections));
444 utcp->connections = new_array;
447 struct utcp_connection *c = calloc(1, sizeof(*c));
453 if(!buffer_set_size(&c->sndbuf, DEFAULT_SNDBUFSIZE, DEFAULT_MAXSNDBUFSIZE)) {
458 if(!buffer_set_size(&c->rcvbuf, DEFAULT_RCVBUFSIZE, DEFAULT_MAXRCVBUFSIZE)) {
459 buffer_exit(&c->sndbuf);
464 // Fill in the details
473 c->snd.una = c->snd.iss;
474 c->snd.nxt = c->snd.iss + 1;
475 c->snd.last = c->snd.nxt;
476 c->snd.cwnd = (utcp->mss > 2190 ? 2 : utcp->mss > 1095 ? 3 : 4) * utcp->mss;
477 c->snd.ssthresh = ~0;
481 // Add it to the sorted list of connections
483 utcp->connections[utcp->nconnections++] = c;
484 qsort(utcp->connections, utcp->nconnections, sizeof(*utcp->connections), compare);
489 static inline uint32_t absdiff(uint32_t a, uint32_t b) {
497 // Update RTT variables. See RFC 6298.
498 static void update_rtt(struct utcp_connection *c, uint32_t rtt) {
500 debug(c, "invalid rtt\n");
504 struct utcp *utcp = c->utcp;
508 utcp->rttvar = rtt / 2;
510 utcp->rttvar = (utcp->rttvar * 3 + absdiff(utcp->srtt, rtt)) / 4;
511 utcp->srtt = (utcp->srtt * 7 + rtt) / 8;
514 utcp->rto = utcp->srtt + max(4 * utcp->rttvar, CLOCK_GRANULARITY);
516 if(utcp->rto > MAX_RTO) {
520 debug(c, "rtt %u srtt %u rttvar %u rto %u\n", rtt, utcp->srtt, utcp->rttvar, utcp->rto);
523 static void start_retransmit_timer(struct utcp_connection *c) {
524 clock_gettime(UTCP_CLOCK, &c->rtrx_timeout);
526 uint32_t rto = c->utcp->rto;
528 while(rto > USEC_PER_SEC) {
529 c->rtrx_timeout.tv_sec++;
533 c->rtrx_timeout.tv_nsec += c->utcp->rto * 1000;
535 if(c->rtrx_timeout.tv_nsec >= NSEC_PER_SEC) {
536 c->rtrx_timeout.tv_nsec -= NSEC_PER_SEC;
537 c->rtrx_timeout.tv_sec++;
540 debug(c, "rtrx_timeout %ld.%06lu\n", c->rtrx_timeout.tv_sec, c->rtrx_timeout.tv_nsec);
543 static void stop_retransmit_timer(struct utcp_connection *c) {
544 timespec_clear(&c->rtrx_timeout);
545 debug(c, "rtrx_timeout cleared\n");
548 struct utcp_connection *utcp_connect_ex(struct utcp *utcp, uint16_t dst, utcp_recv_t recv, void *priv, uint32_t flags) {
549 struct utcp_connection *c = allocate_connection(utcp, 0, dst);
555 assert((flags & ~0x1f) == 0);
566 pkt.hdr.src = c->src;
567 pkt.hdr.dst = c->dst;
568 pkt.hdr.seq = c->snd.iss;
570 pkt.hdr.wnd = c->rcvbuf.maxsize;
572 pkt.hdr.aux = 0x0101;
576 pkt.init[3] = flags & 0x7;
578 set_state(c, SYN_SENT);
580 print_packet(c, "send", &pkt, sizeof(pkt));
581 utcp->send(utcp, &pkt, sizeof(pkt));
583 clock_gettime(UTCP_CLOCK, &c->conn_timeout);
584 c->conn_timeout.tv_sec += utcp->timeout;
586 start_retransmit_timer(c);
591 struct utcp_connection *utcp_connect(struct utcp *utcp, uint16_t dst, utcp_recv_t recv, void *priv) {
592 return utcp_connect_ex(utcp, dst, recv, priv, UTCP_TCP);
595 void utcp_accept(struct utcp_connection *c, utcp_recv_t recv, void *priv) {
596 if(c->reapable || c->state != SYN_RECEIVED) {
597 debug(c, "accept() called on invalid connection in state %s\n", c, strstate[c->state]);
601 debug(c, "accepted %p %p\n", c, recv, priv);
604 set_state(c, ESTABLISHED);
607 static void ack(struct utcp_connection *c, bool sendatleastone) {
608 int32_t left = seqdiff(c->snd.last, c->snd.nxt);
609 int32_t cwndleft = min(c->snd.cwnd, c->snd.wnd) - seqdiff(c->snd.nxt, c->snd.una);
615 } else if(cwndleft < left) {
618 if(!sendatleastone || cwndleft > c->utcp->mss) {
619 left -= left % c->utcp->mss;
623 debug(c, "cwndleft %d left %d\n", cwndleft, left);
625 if(!left && !sendatleastone) {
632 } *pkt = c->utcp->pkt;
634 pkt->hdr.src = c->src;
635 pkt->hdr.dst = c->dst;
636 pkt->hdr.ack = c->rcv.nxt;
637 pkt->hdr.wnd = c->rcvbuf.maxsize;
642 uint32_t seglen = left > c->utcp->mss ? c->utcp->mss : left;
643 pkt->hdr.seq = c->snd.nxt;
645 buffer_copy(&c->sndbuf, pkt->data, seqdiff(c->snd.nxt, c->snd.una), seglen);
647 c->snd.nxt += seglen;
650 if(seglen && fin_wanted(c, c->snd.nxt)) {
655 if(!c->rtt_start.tv_sec) {
656 // Start RTT measurement
657 clock_gettime(UTCP_CLOCK, &c->rtt_start);
658 c->rtt_seq = pkt->hdr.seq + seglen;
659 debug(c, "starting RTT measurement, expecting ack %u\n", c->rtt_seq);
662 print_packet(c, "send", pkt, sizeof(pkt->hdr) + seglen);
663 c->utcp->send(c->utcp, pkt, sizeof(pkt->hdr) + seglen);
667 ssize_t utcp_send(struct utcp_connection *c, const void *data, size_t len) {
669 debug(c, "send() called on closed connection\n");
677 debug(c, "send() called on unconnected connection\n");
692 debug(c, "send() called on closed connection\n");
697 // Exit early if we have nothing to send.
708 // Check if we need to be able to buffer all data
710 if(c->flags & UTCP_NO_PARTIAL) {
711 if(len > buffer_free(&c->sndbuf)) {
712 if(len > c->sndbuf.maxsize) {
722 // Add data to send buffer.
724 if(is_reliable(c) || (c->state != SYN_SENT && c->state != SYN_RECEIVED)) {
725 len = buffer_put(&c->sndbuf, data, len);
741 // Don't send anything yet if the connection has not fully established yet
743 if(c->state == SYN_SENT || c->state == SYN_RECEIVED) {
749 if(!is_reliable(c)) {
750 c->snd.una = c->snd.nxt = c->snd.last;
751 buffer_discard(&c->sndbuf, c->sndbuf.used);
754 if(is_reliable(c) && !timespec_isset(&c->rtrx_timeout)) {
755 start_retransmit_timer(c);
758 if(is_reliable(c) && !timespec_isset(&c->conn_timeout)) {
759 clock_gettime(UTCP_CLOCK, &c->conn_timeout);
760 c->conn_timeout.tv_sec += c->utcp->timeout;
766 static void swap_ports(struct hdr *hdr) {
767 uint16_t tmp = hdr->src;
772 static void fast_retransmit(struct utcp_connection *c) {
773 if(c->state == CLOSED || c->snd.last == c->snd.una) {
774 debug(c, "fast_retransmit() called but nothing to retransmit!\n");
778 struct utcp *utcp = c->utcp;
785 pkt = malloc(c->utcp->mtu);
791 pkt->hdr.src = c->src;
792 pkt->hdr.dst = c->dst;
793 pkt->hdr.wnd = c->rcvbuf.maxsize;
802 // Send unacked data again.
803 pkt->hdr.seq = c->snd.una;
804 pkt->hdr.ack = c->rcv.nxt;
806 uint32_t len = min(seqdiff(c->snd.last, c->snd.una), utcp->mss);
808 if(fin_wanted(c, c->snd.una + len)) {
813 buffer_copy(&c->sndbuf, pkt->data, 0, len);
814 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr) + len);
815 utcp->send(utcp, pkt, sizeof(pkt->hdr) + len);
825 static void retransmit(struct utcp_connection *c) {
826 if(c->state == CLOSED || c->snd.last == c->snd.una) {
827 debug(c, "retransmit() called but nothing to retransmit!\n");
828 stop_retransmit_timer(c);
832 struct utcp *utcp = c->utcp;
837 } *pkt = c->utcp->pkt;
839 pkt->hdr.src = c->src;
840 pkt->hdr.dst = c->dst;
841 pkt->hdr.wnd = c->rcvbuf.maxsize;
846 // Send our SYN again
847 pkt->hdr.seq = c->snd.iss;
850 pkt->hdr.aux = 0x0101;
854 pkt->data[3] = c->flags & 0x7;
855 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr) + 4);
856 utcp->send(utcp, pkt, sizeof(pkt->hdr) + 4);
861 pkt->hdr.seq = c->snd.nxt;
862 pkt->hdr.ack = c->rcv.nxt;
863 pkt->hdr.ctl = SYN | ACK;
864 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr));
865 utcp->send(utcp, pkt, sizeof(pkt->hdr));
873 // Send unacked data again.
874 pkt->hdr.seq = c->snd.una;
875 pkt->hdr.ack = c->rcv.nxt;
877 uint32_t len = min(seqdiff(c->snd.last, c->snd.una), utcp->mss);
879 if(fin_wanted(c, c->snd.una + len)) {
884 // RFC 5681 slow start after timeout
885 uint32_t flightsize = seqdiff(c->snd.nxt, c->snd.una);
886 c->snd.ssthresh = max(flightsize / 2, utcp->mss * 2); // eq. 4
887 c->snd.cwnd = utcp->mss;
890 buffer_copy(&c->sndbuf, pkt->data, 0, len);
891 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr) + len);
892 utcp->send(utcp, pkt, sizeof(pkt->hdr) + len);
894 c->snd.nxt = c->snd.una + len;
901 // We shouldn't need to retransmit anything in this state.
905 stop_retransmit_timer(c);
909 start_retransmit_timer(c);
912 if(utcp->rto > MAX_RTO) {
916 c->rtt_start.tv_sec = 0; // invalidate RTT timer
917 c->dupack = 0; // cancel any ongoing fast recovery
923 /* Update receive buffer and SACK entries after consuming data.
927 * |.....0000..1111111111.....22222......3333|
930 * 0..3 represent the SACK entries. The ^ indicates up to which point we want
931 * to remove data from the receive buffer. The idea is to substract "len"
932 * from the offset of all the SACK entries, and then remove/cut down entries
933 * that are shifted to before the start of the receive buffer.
935 * There are three cases:
936 * - the SACK entry is after ^, in that case just change the offset.
937 * - the SACK entry starts before and ends after ^, so we have to
938 * change both its offset and size.
939 * - the SACK entry is completely before ^, in that case delete it.
941 static void sack_consume(struct utcp_connection *c, size_t len) {
942 debug(c, "sack_consume %lu\n", (unsigned long)len);
944 if(len > c->rcvbuf.used) {
945 debug(c, "all SACK entries consumed\n");
950 buffer_discard(&c->rcvbuf, len);
952 for(int i = 0; i < NSACKS && c->sacks[i].len;) {
953 if(len < c->sacks[i].offset) {
954 c->sacks[i].offset -= len;
956 } else if(len < c->sacks[i].offset + c->sacks[i].len) {
957 c->sacks[i].len -= len - c->sacks[i].offset;
958 c->sacks[i].offset = 0;
962 memmove(&c->sacks[i], &c->sacks[i + 1], (NSACKS - 1 - i) * sizeof(c->sacks)[i]);
963 c->sacks[NSACKS - 1].len = 0;
971 for(int i = 0; i < NSACKS && c->sacks[i].len; i++) {
972 debug(c, "SACK[%d] offset %u len %u\n", i, c->sacks[i].offset, c->sacks[i].len);
976 static void handle_out_of_order(struct utcp_connection *c, uint32_t offset, const void *data, size_t len) {
977 debug(c, "out of order packet, offset %u\n", offset);
978 // Packet loss or reordering occured. Store the data in the buffer.
979 ssize_t rxd = buffer_put_at(&c->rcvbuf, offset, data, len);
981 if(rxd < 0 || (size_t)rxd < len) {
985 // Make note of where we put it.
986 for(int i = 0; i < NSACKS; i++) {
987 if(!c->sacks[i].len) { // nothing to merge, add new entry
988 debug(c, "new SACK entry %d\n", i);
989 c->sacks[i].offset = offset;
990 c->sacks[i].len = rxd;
992 } else if(offset < c->sacks[i].offset) {
993 if(offset + rxd < c->sacks[i].offset) { // insert before
994 if(!c->sacks[NSACKS - 1].len) { // only if room left
995 debug(c, "insert SACK entry at %d\n", i);
996 memmove(&c->sacks[i + 1], &c->sacks[i], (NSACKS - i - 1) * sizeof(c->sacks)[i]);
997 c->sacks[i].offset = offset;
998 c->sacks[i].len = rxd;
1000 debug(c, "SACK entries full, dropping packet\n");
1005 debug(c, "merge with start of SACK entry at %d\n", i);
1006 c->sacks[i].offset = offset;
1009 } else if(offset <= c->sacks[i].offset + c->sacks[i].len) {
1010 if(offset + rxd > c->sacks[i].offset + c->sacks[i].len) { // merge
1011 debug(c, "merge with end of SACK entry at %d\n", i);
1012 c->sacks[i].len = offset + rxd - c->sacks[i].offset;
1013 // TODO: handle potential merge with next entry
1020 for(int i = 0; i < NSACKS && c->sacks[i].len; i++) {
1021 debug(c, "SACK[%d] offset %u len %u\n", i, c->sacks[i].offset, c->sacks[i].len);
1025 static void handle_in_order(struct utcp_connection *c, const void *data, size_t len) {
1026 // Check if we can process out-of-order data now.
1027 if(c->sacks[0].len && len >= c->sacks[0].offset) { // TODO: handle overlap with second SACK
1028 debug(c, "incoming packet len %lu connected with SACK at %u\n", (unsigned long)len, c->sacks[0].offset);
1029 buffer_put_at(&c->rcvbuf, 0, data, len); // TODO: handle return value
1030 len = max(len, c->sacks[0].offset + c->sacks[0].len);
1031 data = c->rcvbuf.data;
1035 ssize_t rxd = c->recv(c, data, len);
1037 if(rxd < 0 || (size_t)rxd != len) {
1038 // TODO: handle the application not accepting all data.
1043 if(c->rcvbuf.used) {
1044 sack_consume(c, len);
1051 static void handle_incoming_data(struct utcp_connection *c, uint32_t seq, const void *data, size_t len) {
1052 if(!is_reliable(c)) {
1053 c->recv(c, data, len);
1054 c->rcv.nxt = seq + len;
1058 uint32_t offset = seqdiff(seq, c->rcv.nxt);
1060 if(offset + len > c->rcvbuf.maxsize) {
1065 handle_out_of_order(c, offset, data, len);
1067 handle_in_order(c, data, len);
1072 ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
1073 const uint8_t *ptr = data;
1089 // Drop packets smaller than the header
1093 if(len < sizeof(hdr)) {
1094 print_packet(NULL, "recv", data, len);
1099 // Make a copy from the potentially unaligned data to a struct hdr
1101 memcpy(&hdr, ptr, sizeof(hdr));
1103 // Try to match the packet to an existing connection
1105 struct utcp_connection *c = find_connection(utcp, hdr.dst, hdr.src);
1106 print_packet(c, "recv", data, len);
1108 // Process the header
1113 // Drop packets with an unknown CTL flag
1115 if(hdr.ctl & ~(SYN | ACK | RST | FIN)) {
1116 print_packet(NULL, "recv", data, len);
1121 // Check for auxiliary headers
1123 const uint8_t *init = NULL;
1125 uint16_t aux = hdr.aux;
1128 size_t auxlen = 4 * (aux >> 8) & 0xf;
1129 uint8_t auxtype = aux & 0xff;
1138 if(!(hdr.ctl & SYN) || auxlen != 4) {
1154 if(!(aux & 0x800)) {
1163 memcpy(&aux, ptr, 2);
1168 bool has_data = len || (hdr.ctl & (SYN | FIN));
1170 // Is it for a new connection?
1173 // Ignore RST packets
1179 // Is it a SYN packet and are we LISTENing?
1181 if(hdr.ctl & SYN && !(hdr.ctl & ACK) && utcp->accept) {
1182 // If we don't want to accept it, send a RST back
1183 if((utcp->pre_accept && !utcp->pre_accept(utcp, hdr.dst))) {
1188 // Try to allocate memory, otherwise send a RST back
1189 c = allocate_connection(utcp, hdr.dst, hdr.src);
1196 // Parse auxilliary information
1203 c->flags = init[3] & 0x7;
1205 c->flags = UTCP_TCP;
1209 // Return SYN+ACK, go to SYN_RECEIVED state
1210 c->snd.wnd = hdr.wnd;
1211 c->rcv.irs = hdr.seq;
1212 c->rcv.nxt = c->rcv.irs + 1;
1213 set_state(c, SYN_RECEIVED);
1220 pkt.hdr.src = c->src;
1221 pkt.hdr.dst = c->dst;
1222 pkt.hdr.ack = c->rcv.irs + 1;
1223 pkt.hdr.seq = c->snd.iss;
1224 pkt.hdr.wnd = c->rcvbuf.maxsize;
1225 pkt.hdr.ctl = SYN | ACK;
1228 pkt.hdr.aux = 0x0101;
1232 pkt.data[3] = c->flags & 0x7;
1233 print_packet(c, "send", &pkt, sizeof(hdr) + 4);
1234 utcp->send(utcp, &pkt, sizeof(hdr) + 4);
1237 print_packet(c, "send", &pkt, sizeof(hdr));
1238 utcp->send(utcp, &pkt, sizeof(hdr));
1241 // No, we don't want your packets, send a RST back
1249 debug(c, "state %s\n", strstate[c->state]);
1251 // In case this is for a CLOSED connection, ignore the packet.
1252 // TODO: make it so incoming packets can never match a CLOSED connection.
1254 if(c->state == CLOSED) {
1255 debug(c, "got packet for closed connection\n");
1259 // It is for an existing connection.
1261 // 1. Drop invalid packets.
1263 // 1a. Drop packets that should not happen in our current state.
1284 // 1b. Discard data that is not in our receive window.
1286 if(is_reliable(c)) {
1289 if(c->state == SYN_SENT) {
1291 } else if(len == 0) {
1292 acceptable = seqdiff(hdr.seq, c->rcv.nxt) >= 0;
1294 int32_t rcv_offset = seqdiff(hdr.seq, c->rcv.nxt);
1296 // cut already accepted front overlapping
1297 if(rcv_offset < 0) {
1298 acceptable = len > (size_t) - rcv_offset;
1303 hdr.seq -= rcv_offset;
1306 acceptable = seqdiff(hdr.seq, c->rcv.nxt) >= 0 && seqdiff(hdr.seq, c->rcv.nxt) + len <= c->rcvbuf.maxsize;
1311 debug(c, "packet not acceptable, %u <= %u + %lu < %u\n", c->rcv.nxt, hdr.seq, (unsigned long)len, c->rcv.nxt + c->rcvbuf.maxsize);
1313 // Ignore unacceptable RST packets.
1318 // Otherwise, continue processing.
1323 int32_t rcv_offset = seqdiff(hdr.seq, c->rcv.nxt);
1326 debug(c, "packet out of order, offset %u bytes", rcv_offset);
1329 if(rcv_offset >= 0) {
1330 c->rcv.nxt = hdr.seq + len;
1336 c->snd.wnd = hdr.wnd; // TODO: move below
1338 // 1c. Drop packets with an invalid ACK.
1339 // ackno should not roll back, and it should also not be bigger than what we ever could have sent
1340 // (= snd.una + c->sndbuf.used).
1342 if(!is_reliable(c)) {
1343 if(hdr.ack != c->snd.last && c->state >= ESTABLISHED) {
1344 hdr.ack = c->snd.una;
1348 if(hdr.ctl & ACK && (seqdiff(hdr.ack, c->snd.last) > 0 || seqdiff(hdr.ack, c->snd.una) < 0)) {
1349 debug(c, "packet ack seqno out of range, %u <= %u < %u\n", c->snd.una, hdr.ack, c->snd.una + c->sndbuf.used);
1351 // Ignore unacceptable RST packets.
1359 // 2. Handle RST packets
1364 if(!(hdr.ctl & ACK)) {
1368 // The peer has refused our connection.
1369 set_state(c, CLOSED);
1370 errno = ECONNREFUSED;
1373 c->recv(c, NULL, 0);
1376 if(c->poll && !c->reapable) {
1387 // We haven't told the application about this connection yet. Silently delete.
1399 // The peer has aborted our connection.
1400 set_state(c, CLOSED);
1404 c->recv(c, NULL, 0);
1407 if(c->poll && !c->reapable) {
1420 // As far as the application is concerned, the connection has already been closed.
1421 // If it has called utcp_close() already, we can immediately free this connection.
1427 // Otherwise, immediately move to the CLOSED state.
1428 set_state(c, CLOSED);
1441 if(!(hdr.ctl & ACK)) {
1446 // 3. Advance snd.una
1448 advanced = seqdiff(hdr.ack, c->snd.una);
1452 if(c->rtt_start.tv_sec) {
1453 if(c->rtt_seq == hdr.ack) {
1454 struct timespec now;
1455 clock_gettime(UTCP_CLOCK, &now);
1456 int32_t diff = timespec_diff_usec(&now, &c->rtt_start);
1457 update_rtt(c, diff);
1458 c->rtt_start.tv_sec = 0;
1459 } else if(c->rtt_seq < hdr.ack) {
1460 debug(c, "cancelling RTT measurement: %u < %u\n", c->rtt_seq, hdr.ack);
1461 c->rtt_start.tv_sec = 0;
1465 int32_t data_acked = advanced;
1473 // TODO: handle FIN as well.
1478 assert(data_acked >= 0);
1481 int32_t bufused = seqdiff(c->snd.last, c->snd.una);
1482 assert(data_acked <= bufused);
1486 buffer_discard(&c->sndbuf, data_acked);
1489 // Also advance snd.nxt if possible
1490 if(seqdiff(c->snd.nxt, hdr.ack) < 0) {
1491 c->snd.nxt = hdr.ack;
1494 c->snd.una = hdr.ack;
1497 if(c->dupack >= 3) {
1498 debug(c, "fast recovery ended\n");
1499 c->snd.cwnd = c->snd.ssthresh;
1505 // Increase the congestion window according to RFC 5681
1506 if(c->snd.cwnd < c->snd.ssthresh) {
1507 c->snd.cwnd += min(advanced, utcp->mss); // eq. 2
1509 c->snd.cwnd += max(1, (utcp->mss * utcp->mss) / c->snd.cwnd); // eq. 3
1512 if(c->snd.cwnd > c->sndbuf.maxsize) {
1513 c->snd.cwnd = c->sndbuf.maxsize;
1518 // Check if we have sent a FIN that is now ACKed.
1521 if(c->snd.una == c->snd.last) {
1522 set_state(c, FIN_WAIT_2);
1528 if(c->snd.una == c->snd.last) {
1529 clock_gettime(UTCP_CLOCK, &c->conn_timeout);
1530 c->conn_timeout.tv_sec += utcp->timeout;
1531 set_state(c, TIME_WAIT);
1540 if(!len && is_reliable(c) && c->snd.una != c->snd.last) {
1542 debug(c, "duplicate ACK %d\n", c->dupack);
1544 if(c->dupack == 3) {
1545 // RFC 5681 fast recovery
1546 debug(c, "fast recovery started\n", c->dupack);
1547 uint32_t flightsize = seqdiff(c->snd.nxt, c->snd.una);
1548 c->snd.ssthresh = max(flightsize / 2, utcp->mss * 2); // eq. 4
1549 c->snd.cwnd = min(c->snd.ssthresh + 3 * utcp->mss, c->sndbuf.maxsize);
1551 if(c->snd.cwnd > c->sndbuf.maxsize) {
1552 c->snd.cwnd = c->sndbuf.maxsize;
1558 } else if(c->dupack > 3) {
1559 c->snd.cwnd += utcp->mss;
1561 if(c->snd.cwnd > c->sndbuf.maxsize) {
1562 c->snd.cwnd = c->sndbuf.maxsize;
1568 // We got an ACK which indicates the other side did get one of our packets.
1569 // Reset the retransmission timer to avoid going to slow start,
1570 // but don't touch the connection timeout.
1571 start_retransmit_timer(c);
1578 if(c->snd.una == c->snd.last) {
1579 stop_retransmit_timer(c);
1580 timespec_clear(&c->conn_timeout);
1581 } else if(is_reliable(c)) {
1582 start_retransmit_timer(c);
1583 clock_gettime(UTCP_CLOCK, &c->conn_timeout);
1584 c->conn_timeout.tv_sec += utcp->timeout;
1589 // 5. Process SYN stuff
1595 // This is a SYNACK. It should always have ACKed the SYN.
1600 c->rcv.irs = hdr.seq;
1601 c->rcv.nxt = hdr.seq;
1605 set_state(c, FIN_WAIT_1);
1607 set_state(c, ESTABLISHED);
1610 // TODO: notify application of this somehow.
1614 // This is a retransmit of a SYN, send back the SYNACK.
1624 // Ehm, no. We should never receive a second SYN.
1634 // SYN counts as one sequence number
1638 // 6. Process new data
1640 if(c->state == SYN_RECEIVED) {
1641 // This is the ACK after the SYNACK. It should always have ACKed the SYNACK.
1646 // Are we still LISTENing?
1648 utcp->accept(c, c->src);
1651 if(c->state != ESTABLISHED) {
1652 set_state(c, CLOSED);
1662 // This should never happen.
1677 // Ehm no, We should never receive more data after a FIN.
1687 handle_incoming_data(c, hdr.seq, ptr, len);
1690 // 7. Process FIN stuff
1692 if((hdr.ctl & FIN) && (!is_reliable(c) || hdr.seq + len == c->rcv.nxt)) {
1696 // This should never happen.
1703 set_state(c, CLOSE_WAIT);
1707 set_state(c, CLOSING);
1711 clock_gettime(UTCP_CLOCK, &c->conn_timeout);
1712 c->conn_timeout.tv_sec += utcp->timeout;
1713 set_state(c, TIME_WAIT);
1720 // Ehm, no. We should never receive a second FIN.
1730 // FIN counts as one sequence number
1734 // Inform the application that the peer closed its end of the connection.
1737 c->recv(c, NULL, 0);
1741 // Now we send something back if:
1742 // - we received data, so we have to send back an ACK
1743 // -> sendatleastone = true
1744 // - or we got an ack, so we should maybe send a bit more data
1745 // -> sendatleastone = false
1747 if(is_reliable(c) || hdr.ctl & SYN || hdr.ctl & FIN) {
1762 hdr.ack = hdr.seq + len;
1764 hdr.ctl = RST | ACK;
1767 print_packet(c, "send", &hdr, sizeof(hdr));
1768 utcp->send(utcp, &hdr, sizeof(hdr));
1773 int utcp_shutdown(struct utcp_connection *c, int dir) {
1774 debug(c, "shutdown %d at %u\n", dir, c ? c->snd.last : 0);
1782 debug(c, "shutdown() called on closed connection\n");
1787 if(!(dir == UTCP_SHUT_RD || dir == UTCP_SHUT_WR || dir == UTCP_SHUT_RDWR)) {
1792 // TCP does not have a provision for stopping incoming packets.
1793 // The best we can do is to just ignore them.
1794 if(dir == UTCP_SHUT_RD || dir == UTCP_SHUT_RDWR) {
1798 // The rest of the code deals with shutting down writes.
1799 if(dir == UTCP_SHUT_RD) {
1803 // Only process shutting down writes once.
1821 set_state(c, FIN_WAIT_1);
1829 set_state(c, CLOSING);
1842 if(!timespec_isset(&c->rtrx_timeout)) {
1843 start_retransmit_timer(c);
1849 static bool reset_connection(struct utcp_connection *c) {
1856 debug(c, "abort() called on closed connection\n");
1873 set_state(c, CLOSED);
1881 set_state(c, CLOSED);
1891 hdr.seq = c->snd.nxt;
1896 print_packet(c, "send", &hdr, sizeof(hdr));
1897 c->utcp->send(c->utcp, &hdr, sizeof(hdr));
1901 // Closes all the opened connections
1902 void utcp_abort_all_connections(struct utcp *utcp) {
1908 for(int i = 0; i < utcp->nconnections; i++) {
1909 struct utcp_connection *c = utcp->connections[i];
1911 if(c->reapable || c->state == CLOSED) {
1915 utcp_recv_t old_recv = c->recv;
1916 utcp_poll_t old_poll = c->poll;
1918 reset_connection(c);
1922 old_recv(c, NULL, 0);
1925 if(old_poll && !c->reapable) {
1934 int utcp_close(struct utcp_connection *c) {
1935 if(utcp_shutdown(c, SHUT_RDWR) && errno != ENOTCONN) {
1945 int utcp_abort(struct utcp_connection *c) {
1946 if(!reset_connection(c)) {
1955 * One call to this function will loop through all connections,
1956 * checking if something needs to be resent or not.
1957 * The return value is the time to the next timeout in milliseconds,
1958 * or maybe a negative value if the timeout is infinite.
1960 struct timespec utcp_timeout(struct utcp *utcp) {
1961 struct timespec now;
1962 clock_gettime(UTCP_CLOCK, &now);
1963 struct timespec next = {now.tv_sec + 3600, now.tv_nsec};
1965 for(int i = 0; i < utcp->nconnections; i++) {
1966 struct utcp_connection *c = utcp->connections[i];
1972 // delete connections that have been utcp_close()d.
1973 if(c->state == CLOSED) {
1975 debug(c, "reaping\n");
1983 if(timespec_isset(&c->conn_timeout) && timespec_lt(&c->conn_timeout, &now)) {
1988 c->recv(c, NULL, 0);
1991 if(c->poll && !c->reapable) {
1998 if(timespec_isset(&c->rtrx_timeout) && timespec_lt(&c->rtrx_timeout, &now)) {
1999 debug(c, "retransmitting after timeout\n");
2004 if((c->state == ESTABLISHED || c->state == CLOSE_WAIT)) {
2005 uint32_t len = buffer_free(&c->sndbuf);
2010 } else if(c->state == CLOSED) {
2015 if(timespec_isset(&c->conn_timeout) && timespec_lt(&c->conn_timeout, &next)) {
2016 next = c->conn_timeout;
2019 if(timespec_isset(&c->rtrx_timeout) && timespec_lt(&c->rtrx_timeout, &next)) {
2020 next = c->rtrx_timeout;
2024 struct timespec diff;
2026 timespec_sub(&next, &now, &diff);
2031 bool utcp_is_active(struct utcp *utcp) {
2036 for(int i = 0; i < utcp->nconnections; i++)
2037 if(utcp->connections[i]->state != CLOSED && utcp->connections[i]->state != TIME_WAIT) {
2044 struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_send_t send, void *priv) {
2050 struct utcp *utcp = calloc(1, sizeof(*utcp));
2056 if(!CLOCK_GRANULARITY) {
2057 struct timespec res;
2058 clock_getres(UTCP_CLOCK, &res);
2059 CLOCK_GRANULARITY = res.tv_sec * NSEC_PER_SEC + res.tv_nsec;
2062 utcp->accept = accept;
2063 utcp->pre_accept = pre_accept;
2066 utcp_set_mtu(utcp, DEFAULT_MTU);
2067 utcp->timeout = DEFAULT_USER_TIMEOUT; // sec
2068 utcp->rto = START_RTO; // usec
2073 void utcp_exit(struct utcp *utcp) {
2078 for(int i = 0; i < utcp->nconnections; i++) {
2079 struct utcp_connection *c = utcp->connections[i];
2083 c->recv(c, NULL, 0);
2086 if(c->poll && !c->reapable) {
2091 buffer_exit(&c->rcvbuf);
2092 buffer_exit(&c->sndbuf);
2096 free(utcp->connections);
2100 uint16_t utcp_get_mtu(struct utcp *utcp) {
2101 return utcp ? utcp->mtu : 0;
2104 uint16_t utcp_get_mss(struct utcp *utcp) {
2105 return utcp ? utcp->mss : 0;
2108 void utcp_set_mtu(struct utcp *utcp, uint16_t mtu) {
2113 if(mtu <= sizeof(struct hdr)) {
2117 if(mtu > utcp->mtu) {
2118 char *new = realloc(utcp->pkt, mtu + sizeof(struct hdr));
2128 utcp->mss = mtu - sizeof(struct hdr);
2131 void utcp_reset_timers(struct utcp *utcp) {
2136 struct timespec now, then;
2138 clock_gettime(UTCP_CLOCK, &now);
2142 then.tv_sec += utcp->timeout;
2144 for(int i = 0; i < utcp->nconnections; i++) {
2145 struct utcp_connection *c = utcp->connections[i];
2151 if(timespec_isset(&c->rtrx_timeout)) {
2152 c->rtrx_timeout = now;
2155 if(timespec_isset(&c->conn_timeout)) {
2156 c->conn_timeout = then;
2159 c->rtt_start.tv_sec = 0;
2162 if(utcp->rto > START_RTO) {
2163 utcp->rto = START_RTO;
2167 int utcp_get_user_timeout(struct utcp *u) {
2168 return u ? u->timeout : 0;
2171 void utcp_set_user_timeout(struct utcp *u, int timeout) {
2173 u->timeout = timeout;
2177 size_t utcp_get_sndbuf(struct utcp_connection *c) {
2178 return c ? c->sndbuf.maxsize : 0;
2181 size_t utcp_get_sndbuf_free(struct utcp_connection *c) {
2191 return buffer_free(&c->sndbuf);
2198 void utcp_set_sndbuf(struct utcp_connection *c, size_t size) {
2203 c->sndbuf.maxsize = size;
2205 if(c->sndbuf.maxsize != size) {
2206 c->sndbuf.maxsize = -1;
2210 size_t utcp_get_rcvbuf(struct utcp_connection *c) {
2211 return c ? c->rcvbuf.maxsize : 0;
2214 size_t utcp_get_rcvbuf_free(struct utcp_connection *c) {
2215 if(c && (c->state == ESTABLISHED || c->state == CLOSE_WAIT)) {
2216 return buffer_free(&c->rcvbuf);
2222 void utcp_set_rcvbuf(struct utcp_connection *c, size_t size) {
2227 c->rcvbuf.maxsize = size;
2229 if(c->rcvbuf.maxsize != size) {
2230 c->rcvbuf.maxsize = -1;
2234 size_t utcp_get_sendq(struct utcp_connection *c) {
2235 return c->sndbuf.used;
2238 size_t utcp_get_recvq(struct utcp_connection *c) {
2239 return c->rcvbuf.used;
2242 bool utcp_get_nodelay(struct utcp_connection *c) {
2243 return c ? c->nodelay : false;
2246 void utcp_set_nodelay(struct utcp_connection *c, bool nodelay) {
2248 c->nodelay = nodelay;
2252 bool utcp_get_keepalive(struct utcp_connection *c) {
2253 return c ? c->keepalive : false;
2256 void utcp_set_keepalive(struct utcp_connection *c, bool keepalive) {
2258 c->keepalive = keepalive;
2262 size_t utcp_get_outq(struct utcp_connection *c) {
2263 return c ? seqdiff(c->snd.nxt, c->snd.una) : 0;
2266 void utcp_set_recv_cb(struct utcp_connection *c, utcp_recv_t recv) {
2272 void utcp_set_poll_cb(struct utcp_connection *c, utcp_poll_t poll) {
2278 void utcp_set_accept_cb(struct utcp *utcp, utcp_accept_t accept, utcp_pre_accept_t pre_accept) {
2280 utcp->accept = accept;
2281 utcp->pre_accept = pre_accept;
2285 void utcp_expect_data(struct utcp_connection *c, bool expect) {
2286 if(!c || c->reapable) {
2290 if(!(c->state == ESTABLISHED || c->state == FIN_WAIT_1 || c->state == FIN_WAIT_2)) {
2295 // If we expect data, start the connection timer.
2296 if(!timespec_isset(&c->conn_timeout)) {
2297 clock_gettime(UTCP_CLOCK, &c->conn_timeout);
2298 c->conn_timeout.tv_sec += c->utcp->timeout;
2301 // If we want to cancel expecting data, only clear the timer when there is no unACKed data.
2302 if(c->snd.una == c->snd.last) {
2303 timespec_clear(&c->conn_timeout);
2308 void utcp_offline(struct utcp *utcp, bool offline) {
2309 struct timespec now;
2310 clock_gettime(UTCP_CLOCK, &now);
2312 for(int i = 0; i < utcp->nconnections; i++) {
2313 struct utcp_connection *c = utcp->connections[i];
2319 utcp_expect_data(c, offline);
2322 if(timespec_isset(&c->rtrx_timeout)) {
2323 c->rtrx_timeout = now;
2326 utcp->connections[i]->rtt_start.tv_sec = 0;
2330 if(!offline && utcp->rto > START_RTO) {
2331 utcp->rto = START_RTO;