summaryrefslogtreecommitdiff
path: root/src/user/app/netstack/tcp.c
blob: a1b0759f0f81aab743203e7fa17f6b5b6f4523d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* Welcome to spaghetti land.
 * This is anything but production quality. It's throwaway code, meant
 * only to see how networking could fit into the architecture of the
 * system. */
#include "proto.h"
#include "util.h"
#include <assert.h>
#include <shared/container/ring.h>

enum {
	SrcPort = 0,
	DstPort = 2,
	Seq     = 4,
	AckNum  = 8, /* last processed seq + 1 */
	Flags   = 12,
		FlagSize = 0xF000, /* size of header / 4 bytes */
		/* 3 bits: 0, still unused
		 * 3 bits: modern stuff not in the original RFC */
		FlagURG = 0x0020, /* urgent */
		FlagACK = 0x0010,
		FlagPSH = 0x0008, /* force buffer flush */
		FlagRST = 0x0004, /* reset connection */
		FlagSYN = 0x0002, /* first packet; sync sequence numbers */
		FlagFIN = 0x0001, /* last packet */
		FlagAll = 0x003F,
	WinSize  = 14, /* amount of data we're willing to receive */
	Checksum = 16,
	Urgent   = 18,
	MinHdr   = 20,
};

enum tcp_state {
	LISTEN,
	SYN_RCVD,
	LAST_ACK,
	CLOSED,
};

struct tcp_conn {
	uint32_t lip, rip;
	mac_t rmac;
	uint16_t lport, rport;
	struct tcp_conn *next, **link;

	enum tcp_state state;
	uint32_t lack, rack;
	uint32_t lseq;
	bool uclosed; /* did the user close? */

	void (*on_conn)(struct tcp_conn *, void *carg);
	void (*on_recv)(void *carg);
	void (*on_close)(void *carg);
	void *carg;

	ring_t rx;
};
static struct tcp_conn *conns;
static void conns_append(struct tcp_conn *c) {
	c->next = conns;
	if (c->next)
		c->next->link = &c->next;
	c->link = &conns;
	*c->link = c;
}
static void tcpc_sendraw(struct tcp_conn *c, uint16_t flags, const void *buf, size_t len) {
	uint8_t *pkt = malloc(MinHdr + len);
	memset(pkt, 0, MinHdr);

	nput16(pkt + SrcPort, c->lport);
	nput16(pkt + DstPort, c->rport);
	nput32(pkt + Seq, c->lseq);
	c->lseq += len;
	nput32(pkt + AckNum, c->lack);
	flags |= (MinHdr / 4) << 12;
	nput16(pkt + Flags, flags);
	nput16(pkt + WinSize, ring_avail(&c->rx));
	memcpy(pkt + MinHdr, buf, len);
	nput16(pkt + Checksum, ip_checksumphdr(pkt, MinHdr + len, c->lip, c->rip, 6));

	ipv4_send(pkt, MinHdr + len, (struct ipv4){
		.proto = 6,
		.src = c->lip,
		.dst = c->rip,
		.e.dst = &c->rmac,
	});
	free(pkt);
}
void tcp_listen(
	uint16_t port,
	void (*on_conn)(struct tcp_conn *, void *carg),
	void (*on_recv)(void *carg),
	void (*on_close)(void *carg),
	void *carg)
{
	struct tcp_conn *c = malloc(sizeof *c);
	c->lport = port;
	c->lip = state.ip;
	c->state = LISTEN;
	c->on_conn = on_conn;
	c->on_recv = on_recv;
	c->on_close = on_close;
	c->carg = carg;
	// TODO setting the ring size super low loses every nth byte. probably a bug with ring_t itself!
	c->rx = (ring_t){malloc(4096), 4096, 0, 0};
	conns_append(c);
}
size_t tcpc_tryread(struct tcp_conn *c, void *buf, size_t len) {
	if (!buf) return ring_used(&c->rx);
	return ring_get(&c->rx, buf, len);
}
void tcpc_send(struct tcp_conn *c, const void *buf, size_t len) {
	tcpc_sendraw(c, FlagACK | FlagPSH, buf, len);
}
static void tcpc_tryfree(struct tcp_conn *c) {
	if (c->state == CLOSED && c->uclosed) {
		if (c->next) c->next->link = c->link;
		*c->link = c->next;
		free(c->rx.buf);
		free(c);
	}
}
void tcpc_close(struct tcp_conn *c) {
	/* ONLY FOR USE BY THE USER, drops their reference */
	assert(!c->uclosed);
	c->uclosed = true;
	if (c->state != CLOSED && c->state != LAST_ACK && c->state != LISTEN) {
		tcpc_sendraw(c, FlagFIN | FlagACK, NULL, 0);
		c->state = LAST_ACK;
		c->on_conn = NULL;
		c->on_close = NULL;
	}
	tcpc_tryfree(c);
}

void tcp_parse(const uint8_t *buf, size_t len, struct ipv4 ip) {
	if (len < 20) return;
	uint16_t srcport = nget16(buf + SrcPort);
	uint16_t dstport = nget16(buf + DstPort);
	uint32_t seq     = nget32(buf + Seq);
	uint32_t acknum  = nget32(buf + AckNum);
	uint16_t flags   = nget16(buf + Flags);
	uint16_t winsize = nget16(buf + WinSize);
	uint16_t chksum  = nget16(buf + Checksum);
	uint16_t hdrlen = ((flags & FlagSize) >> 12) * 4;
	if (hdrlen > len) return;
	uint16_t payloadlen = len - hdrlen;

	for (struct tcp_conn *iter = conns; iter; iter = iter->next) {
		if (iter->state == CLOSED) continue;
		if (iter->lport != dstport) continue;

		if (iter->state == LISTEN && (flags & FlagAll) == FlagSYN) {
			iter->state = SYN_RCVD;
			iter->rip = ip.src;
			iter->rport = srcport;
			iter->lack = seq + 1;
			memcpy(&iter->rmac, ip.e.src, sizeof(mac_t));
			tcpc_sendraw(iter, FlagSYN | FlagACK, NULL, 0);
			iter->lseq++;
			if (iter->on_conn) iter->on_conn(iter, iter->carg);
			return;
		}

		if (iter->rip == ip.src && iter->rport == srcport) {
			// TODO doesn't handle seq/ack overflows
			if (flags & FlagACK) {
				if (iter->rack < acknum)
					iter->rack = acknum;
				if (iter->state == LAST_ACK) {
					// TODO check if ack has correct number
					iter->state = CLOSED;
					tcpc_tryfree(iter);
					// TODO free (also after a timeout)
					return;
				}
			}
			if (iter->lack != seq && iter->lack - 1 != seq) {
				eprintf("remote seq jumped by %d", seq - iter->lack);
				tcpc_sendraw(iter, FlagACK, NULL, 0);
				return;
			}
			// TODO check if overflows window size
			if (payloadlen) {
				iter->lack = seq + payloadlen;
				ring_put(&iter->rx, buf + hdrlen, payloadlen);
				if (iter->on_recv) iter->on_recv(iter->carg);
				tcpc_sendraw(iter, FlagACK, NULL, 0);
			}
			if (flags & FlagFIN) {
				// TODO should resend the packet until an ACK is received
				// TODO duplicated in tcpc_close
				tcpc_sendraw(iter, FlagFIN | FlagACK, NULL, 0);
				iter->state = LAST_ACK;
				if (iter->on_close) iter->on_close(iter->carg);
				return;
			}
			return;
		}
	}

	uint8_t *pkt = malloc(MinHdr);
	memset(pkt, 0, MinHdr);
	nput16(pkt + SrcPort, dstport);
	nput16(pkt + DstPort, srcport);
	nput32(pkt + Seq, acknum);
	nput32(pkt + AckNum, seq + 1);
	uint16_t pktflags = FlagRST | FlagACK;
	pktflags |= (MinHdr / 4) << 12;
	nput16(pkt + Flags, pktflags);
	nput16(pkt + Checksum, ip_checksumphdr(pkt, MinHdr, ip.src, ip.dst, 6));

	ipv4_send(pkt, MinHdr, (struct ipv4){
		.proto = 6,
		.src = ip.dst,
		.dst = ip.src,
		.e.dst = ip.e.src,
	});
	free(pkt);
}