forked from pandax381/microps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp.c
422 lines (382 loc) · 10.3 KB
/
udp.c
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include "platform.h"
#include "util.h"
#include "net.h"
#include "ip.h"
#include "udp.h"
#define UDP_PCB_SIZE 16
#define UDP_PCB_STATE_FREE 0
#define UDP_PCB_STATE_OPEN 1
#define UDP_PCB_STATE_CLOSING 2
/* see https://tools.ietf.org/html/rfc6335 */
#define UDP_SOURCE_PORT_MIN 49152
#define UDP_SOURCE_PORT_MAX 65535
struct pseudo_hdr {
uint32_t src;
uint32_t dst;
uint8_t zero;
uint8_t protocol;
uint16_t len;
};
struct udp_hdr {
uint16_t src;
uint16_t dst;
uint16_t len;
uint16_t sum;
};
struct udp_pcb {
int state;
struct ip_endpoint local;
struct queue_head queue; /* receive queue */
struct sched_ctx ctx;
};
/* NOTE: the data follows immediately after the structure */
struct udp_queue_entry {
struct ip_endpoint foreign;
uint16_t len;
};
static mutex_t mutex = MUTEX_INITIALIZER;
static struct udp_pcb pcbs[UDP_PCB_SIZE];
static void
udp_dump(const uint8_t *data, size_t len)
{
struct udp_hdr *hdr;
flockfile(stderr);
hdr = (struct udp_hdr *)data;
fprintf(stderr, " src: %u\n", ntoh16(hdr->src));
fprintf(stderr, " dst: %u\n", ntoh16(hdr->dst));
fprintf(stderr, " len: %u\n", ntoh16(hdr->len));
fprintf(stderr, " sum: 0x%04x\n", ntoh16(hdr->sum));
#ifdef HEXDUMP
hexdump(stderr, data, len);
#endif
funlockfile(stderr);
}
/*
* UDP Protocol Control Block (PCB)
*
* NOTE: UDP PCB functions must be called after mutex locked
*/
static struct udp_pcb *
udp_pcb_alloc(void)
{
struct udp_pcb *pcb;
for (pcb = pcbs; pcb < tailof(pcbs); pcb++) {
if (pcb->state == UDP_PCB_STATE_FREE) {
pcb->state = UDP_PCB_STATE_OPEN;
sched_ctx_init(&pcb->ctx);
return pcb;
}
}
return NULL;
}
static void
udp_pcb_release(struct udp_pcb *pcb)
{
struct queue_entry *entry;
pcb->state = UDP_PCB_STATE_CLOSING;
if (sched_ctx_destroy(&pcb->ctx) == -1) {
sched_wakeup(&pcb->ctx);
return;
}
pcb->state = UDP_PCB_STATE_FREE;
pcb->local.addr = IP_ADDR_ANY;
pcb->local.port = 0;
while ((entry = queue_pop(&pcb->queue)) != NULL) {
memory_free(entry);
}
}
static struct udp_pcb *
udp_pcb_select(ip_addr_t addr, uint16_t port)
{
struct udp_pcb *pcb;
for (pcb = pcbs; pcb < tailof(pcbs); pcb++) {
if (pcb->state == UDP_PCB_STATE_OPEN) {
if ((pcb->local.addr == IP_ADDR_ANY || pcb->local.addr == addr) && pcb->local.port == port) {
return pcb;
}
}
}
return NULL;
}
static struct udp_pcb *
udp_pcb_get(int id)
{
struct udp_pcb *pcb;
if (id < 0 || id >= (int)countof(pcbs)) {
/* out of range */
return NULL;
}
pcb = &pcbs[id];
if (pcb->state != UDP_PCB_STATE_OPEN) {
return NULL;
}
return pcb;
}
static int
udp_pcb_id(struct udp_pcb *pcb)
{
return indexof(pcbs, pcb);
}
static void
udp_input(const uint8_t *data, size_t len, ip_addr_t src, ip_addr_t dst, struct ip_iface *iface)
{
struct pseudo_hdr pseudo;
uint16_t psum = 0;
struct udp_hdr *hdr;
char addr1[IP_ADDR_STR_LEN];
char addr2[IP_ADDR_STR_LEN];
struct udp_pcb *pcb;
struct udp_queue_entry *entry;
if (len < sizeof(*hdr)) {
errorf("too short");
return;
}
hdr = (struct udp_hdr *)data;
if (len != ntoh16(hdr->len)) { /* just to make sure */
errorf("length error: len=%zu, hdr->len=%u", len, ntoh16(hdr->len));
return;
}
pseudo.src = src;
pseudo.dst = dst;
pseudo.zero = 0;
pseudo.protocol = IP_PROTOCOL_UDP;
pseudo.len = hton16(len);
psum = ~cksum16((uint16_t *)&pseudo, sizeof(pseudo), 0);
if (cksum16((uint16_t *)hdr, len, psum) != 0) {
errorf("checksum error: sum=0x%04x, verify=0x%04x", ntoh16(hdr->sum), ntoh16(cksum16((uint16_t *)hdr, len, -hdr->sum + psum)));
return;
}
debugf("%s:%d => %s:%d, len=%zu (payload=%zu)",
ip_addr_ntop(src, addr1, sizeof(addr1)), ntoh16(hdr->src),
ip_addr_ntop(dst, addr2, sizeof(addr2)), ntoh16(hdr->dst),
len, len - sizeof(*hdr));
udp_dump(data, len);
mutex_lock(&mutex);
pcb = udp_pcb_select(dst, hdr->dst);
if (!pcb) {
/* port is not in use */
mutex_unlock(&mutex);
return;
}
entry = memory_alloc(sizeof(*entry) + (len - sizeof(*hdr)));
if (!entry) {
mutex_unlock(&mutex);
errorf("memory_alloc() failure");
return;
}
entry->foreign.addr = src;
entry->foreign.port = hdr->src;
entry->len = len - sizeof(*hdr);
memcpy(entry + 1, hdr + 1, entry->len);
if (!queue_push(&pcb->queue, entry)) {
mutex_unlock(&mutex);
errorf("queue_push() failure");
return;
}
sched_wakeup(&pcb->ctx);
mutex_unlock(&mutex);
}
ssize_t
udp_output(struct ip_endpoint *src, struct ip_endpoint *dst, const uint8_t *data, size_t len)
{
uint8_t buf[IP_PAYLOAD_SIZE_MAX];
struct udp_hdr *hdr;
struct pseudo_hdr pseudo;
uint16_t total, psum = 0;
char ep1[IP_ENDPOINT_STR_LEN];
char ep2[IP_ENDPOINT_STR_LEN];
if (len > IP_PAYLOAD_SIZE_MAX - sizeof(*hdr)) {
errorf("too long");
return -1;
}
hdr = (struct udp_hdr *)buf;
hdr->src = src->port;
hdr->dst = dst->port;
total = sizeof(*hdr) + len;
hdr->len = hton16(total);
hdr->sum = 0;
memcpy(hdr + 1, data, len);
pseudo.src = src->addr;
pseudo.dst = dst->addr;
pseudo.zero = 0;
pseudo.protocol = IP_PROTOCOL_UDP;
pseudo.len = hton16(total);
psum = ~cksum16((uint16_t *)&pseudo, sizeof(pseudo), 0);
hdr->sum = cksum16((uint16_t *)hdr, total, psum);
debugf("%s => %s, len=%u (payload=%zu)",
ip_endpoint_ntop(src, ep1, sizeof(ep1)), ip_endpoint_ntop(dst, ep2, sizeof(ep2)), total, len);
udp_dump((uint8_t *)hdr, total);
if (ip_output(IP_PROTOCOL_UDP, (uint8_t *)hdr, total, src->addr, dst->addr) == -1) {
errorf("ip_output() failure");
return -1;
}
return len;
}
static void
event_handler(void *arg)
{
struct udp_pcb *pcb;
mutex_lock(&mutex);
for (pcb = pcbs; pcb < tailof(pcbs); pcb++) {
if (pcb->state == UDP_PCB_STATE_OPEN) {
sched_interrupt(&pcb->ctx);
}
}
mutex_unlock(&mutex);
}
int
udp_init(void)
{
if (ip_protocol_register("UDP", IP_PROTOCOL_UDP, udp_input) == -1) {
errorf("ip_protocol_register() failure");
return -1;
}
net_event_subscribe(event_handler, NULL);
return 0;
}
/*
* UDP User Commands
*/
int
udp_open(void)
{
struct udp_pcb *pcb;
int id;
mutex_lock(&mutex);
pcb = udp_pcb_alloc();
if (!pcb) {
errorf("udp_pcb_alloc() failure");
mutex_unlock(&mutex);
return -1;
}
id = udp_pcb_id(pcb);
mutex_unlock(&mutex);
return id;
}
int
udp_close(int id)
{
struct udp_pcb *pcb;
mutex_lock(&mutex);
pcb = udp_pcb_get(id);
if (!pcb) {
errorf("pcb not found, id=%d", id);
mutex_unlock(&mutex);
return -1;
}
udp_pcb_release(pcb);
mutex_unlock(&mutex);
return 0;
}
int
udp_bind(int id, struct ip_endpoint *local)
{
struct udp_pcb *pcb, *exist;
char ep1[IP_ENDPOINT_STR_LEN];
char ep2[IP_ENDPOINT_STR_LEN];
mutex_lock(&mutex);
pcb = udp_pcb_get(id);
if (!pcb) {
errorf("pcb not found, id=%d", id);
mutex_unlock(&mutex);
return -1;
}
exist = udp_pcb_select(local->addr, local->port);
if (exist) {
errorf("already in use, id=%d, want=%s, exist=%s",
id, ip_endpoint_ntop(local, ep1, sizeof(ep1)), ip_endpoint_ntop(&exist->local, ep2, sizeof(ep2)));
mutex_unlock(&mutex);
return -1;
}
pcb->local = *local;
debugf("bound, id=%d, local=%s", id, ip_endpoint_ntop(&pcb->local, ep1, sizeof(ep1)));
mutex_unlock(&mutex);
return 0;
}
ssize_t
udp_sendto(int id, uint8_t *data, size_t len, struct ip_endpoint *foreign)
{
struct udp_pcb *pcb;
struct ip_endpoint local;
struct ip_iface *iface;
char addr[IP_ADDR_STR_LEN];
uint32_t p;
mutex_lock(&mutex);
pcb = udp_pcb_get(id);
if (!pcb) {
errorf("pcb not found, id=%d", id);
mutex_unlock(&mutex);
return -1;
}
local.addr = pcb->local.addr;
if (local.addr == IP_ADDR_ANY) {
iface = ip_route_get_iface(foreign->addr);
if (!iface) {
errorf("iface not found that can reach foreign address, addr=%s",
ip_addr_ntop(foreign->addr, addr, sizeof(addr)));
mutex_unlock(&mutex);
return -1;
}
local.addr = iface->unicast;
debugf("select local address, addr=%s", ip_addr_ntop(local.addr, addr, sizeof(addr)));
}
if (!pcb->local.port) {
for (p = UDP_SOURCE_PORT_MIN; p <= UDP_SOURCE_PORT_MAX; p++) {
if (!udp_pcb_select(local.addr, hton16(p))) {
pcb->local.port = hton16(p);
debugf("dynamic assign local port, port=%d", p);
break;
}
}
if (!pcb->local.port) {
debugf("failed to dynamic assign local port, addr=%s", ip_addr_ntop(local.addr, addr, sizeof(addr)));
mutex_unlock(&mutex);
return -1;
}
}
local.port = pcb->local.port;
mutex_unlock(&mutex);
return udp_output(&local, foreign, data, len);
}
ssize_t
udp_recvfrom(int id, uint8_t *buf, size_t size, struct ip_endpoint *foreign)
{
struct udp_pcb *pcb;
struct udp_queue_entry *entry;
ssize_t len;
mutex_lock(&mutex);
pcb = udp_pcb_get(id);
if (!pcb) {
errorf("pcb not found, id=%d", id);
mutex_unlock(&mutex);
return -1;
}
while (!(entry = queue_pop(&pcb->queue))) {
if (sched_sleep(&pcb->ctx, &mutex, NULL) == -1) {
debugf("interrupted");
mutex_unlock(&mutex);
errno = EINTR;
return -1;
}
if (pcb->state == UDP_PCB_STATE_CLOSING) {
debugf("closed");
udp_pcb_release(pcb);
mutex_unlock(&mutex);
return -1;
}
}
mutex_unlock(&mutex);
if (foreign) {
*foreign = entry->foreign;
}
len = MIN(size, entry->len); /* truncate */
memcpy(buf, entry + 1, len);
memory_free(entry);
return len;
}