-
Notifications
You must be signed in to change notification settings - Fork 389
/
net.c
396 lines (355 loc) · 9.41 KB
/
net.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include "platform.h"
#include "util.h"
#include "net.h"
struct net_protocol {
struct net_protocol *next;
char name[16];
uint16_t type;
struct queue_head queue; /* input queue */
void (*handler)(const uint8_t *data, size_t len, struct net_device *dev);
};
/* NOTE: the data follows immediately after the structure */
struct net_protocol_queue_entry {
struct net_device *dev;
size_t len;
};
struct net_timer {
struct net_timer *next;
char name[16];
struct timeval interval;
struct timeval last;
void (*handler)(void);
};
struct net_event {
struct net_event *next;
void (*handler)(void *arg);
void *arg;
};
/* NOTE: if you want to add/delete the entries after net_run(), you need to protect these lists with a mutex. */
static struct net_device *devices;
static struct net_protocol *protocols;
static struct net_timer *timers;
static struct net_event *events;
struct net_device *
net_device_alloc(void (*setup)(struct net_device *dev))
{
struct net_device *dev;
dev = memory_alloc(sizeof(*dev));
if (!dev) {
errorf("memory_alloc() failure");
return NULL;
}
if (setup) {
setup(dev);
}
return dev;
}
/* NOTE: must not be call after net_run() */
int
net_device_register(struct net_device *dev)
{
static unsigned int index = 0;
dev->index = index++;
snprintf(dev->name, sizeof(dev->name), "net%d", dev->index);
dev->next = devices;
devices = dev;
infof("registered, dev=%s, type=0x%04x", dev->name, dev->type);
return 0;
}
static int
net_device_open(struct net_device *dev)
{
if (NET_DEVICE_IS_UP(dev)) {
errorf("already opened, dev=%s", dev->name);
return -1;
}
if (dev->ops->open) {
if (dev->ops->open(dev) == -1) {
errorf("failure, dev=%s", dev->name);
return -1;
}
}
dev->flags |= NET_DEVICE_FLAG_UP;
infof("dev=%s, state=%s", dev->name, NET_DEVICE_STATE(dev));
return 0;
}
static int
net_device_close(struct net_device *dev)
{
if (!NET_DEVICE_IS_UP(dev)) {
errorf("not opened, dev=%s", dev->name);
return -1;
}
if (dev->ops->close) {
if (dev->ops->close(dev) == -1) {
errorf("failure, dev=%s", dev->name);
return -1;
}
}
dev->flags &= ~NET_DEVICE_FLAG_UP;
infof("dev=%s, state=%s", dev->name, NET_DEVICE_STATE(dev));
return 0;
}
/* NOTE: must not be call after net_run() */
int
net_device_add_iface(struct net_device *dev, struct net_iface *iface)
{
struct net_iface *entry;
for (entry = dev->ifaces; entry; entry = entry->next) {
if (entry->family == iface->family) {
errorf("already exists, dev=%s, family=%d", dev->name, entry->family);
return -1;
}
}
iface->next = dev->ifaces;
iface->dev = dev;
dev->ifaces = iface;
return 0;
}
struct net_iface *
net_device_get_iface(struct net_device *dev, int family)
{
struct net_iface *entry;
for (entry = dev->ifaces; entry; entry = entry->next) {
if (entry->family == family) {
break;
}
}
return entry;
}
int
net_device_output(struct net_device *dev, uint16_t type, const uint8_t *data, size_t len, const void *dst)
{
if (!NET_DEVICE_IS_UP(dev)) {
errorf("not opened, dev=%s", dev->name);
return -1;
}
if (len > dev->mtu) {
errorf("too long, dev=%s, mtu=%u, len=%zu", dev->name, dev->mtu, len);
return -1;
}
debugf("dev=%s, type=%s(0x%04x), len=%zu", dev->name, net_protocol_name(type), type, len);
debugdump(data, len);
if (dev->ops->transmit(dev, type, data, len, dst) == -1) {
errorf("device transmit failure, dev=%s, len=%zu", dev->name, len);
return -1;
}
return 0;
}
int
net_input_handler(uint16_t type, const uint8_t *data, size_t len, struct net_device *dev)
{
struct net_protocol *proto;
struct net_protocol_queue_entry *entry;
for (proto = protocols; proto; proto = proto->next) {
if (proto->type == type) {
entry = memory_alloc(sizeof(*entry) + len);
if (!entry) {
errorf("memory_alloc() failure");
return -1;
}
entry->dev = dev;
entry->len = len;
memcpy(entry+1, data, len);
if (!queue_push(&proto->queue, entry)) {
errorf("queue_push() failure");
memory_free(entry);
return -1;
}
debugf("queue pushed (num:%u), dev=%s, type=%s(0x%04x), len=%zd", proto->queue.num, dev->name, proto->name, type, len);
debugdump(data, len);
raise_softirq();
return 0;
}
}
/* unsupported protocol */
return 0;
}
/* NOTE: must not be call after net_run() */
int
net_protocol_register(const char *name, uint16_t type, void (*handler)(const uint8_t *data, size_t len, struct net_device *dev))
{
struct net_protocol *proto;
for (proto = protocols; proto; proto = proto->next) {
if (type == proto->type) {
errorf("already registered, type=%s(0x%04x), exist=%s(0x%04x)", name, type, proto->name, proto->type);
return -1;
}
}
proto = memory_alloc(sizeof(*proto));
if (!proto) {
errorf("memory_alloc() failure");
return -1;
}
strncpy(proto->name, name, sizeof(proto->name)-1);
proto->type = type;
proto->handler = handler;
proto->next = protocols;
protocols = proto;
infof("registered, type=%s(0x%04x)", proto->name, type);
return 0;
}
char *
net_protocol_name(uint16_t type)
{
struct net_protocol *entry;
for (entry = protocols; entry; entry = entry->next) {
if (entry->type == type) {
return entry->name;
}
}
return "UNKNOWN";
}
int
net_protocol_handler(void)
{
struct net_protocol *proto;
struct net_protocol_queue_entry *entry;
unsigned int num;
for (proto = protocols; proto; proto = proto->next) {
while (1) {
entry = queue_pop(&proto->queue);
if (!entry) {
break;
}
num = proto->queue.num;
debugf("queue popped (num:%u), dev=%s, type=0x%04x, len=%zd", num, entry->dev->name, proto->type, entry->len);
debugdump((uint8_t *)(entry+1), entry->len);
proto->handler((uint8_t *)(entry+1), entry->len, entry->dev);
free(entry);
}
}
return 0;
}
/* NOTE: must not be call after net_run() */
int
net_timer_register(const char *name, struct timeval interval, void (*handler)(void))
{
struct net_timer *timer;
timer = memory_alloc(sizeof(*timer));
if (!timer) {
errorf("memory_alloc() failure");
return -1;
}
strncpy(timer->name, name, sizeof(timer->name)-1);
timer->interval = interval;
gettimeofday(&timer->last, NULL);
timer->handler = handler;
timer->next = timers;
timers = timer;
infof("registered: %s interval={%ld, %ld}", timer->name, interval.tv_sec, interval.tv_usec);
return 0;
}
int
net_timer_handler(void)
{
struct net_timer *timer;
struct timeval now, diff;
for (timer = timers; timer; timer = timer->next) {
gettimeofday(&now, NULL);
timersub(&now, &timer->last, &diff);
if (timercmp(&timer->interval, &diff, <) != 0) { /* true (!0) or false (0) */
timer->handler();
timer->last = now;
}
}
return 0;
}
int
net_interrupt(void)
{
/* getpid(2) and kill(2) are signal safety functions. see signal-safety(7). */
return kill(getpid(), SIGUSR2);
}
/* NOTE: must not be call after net_run() */
int
net_event_subscribe(void (*handler)(void *arg), void *arg)
{
struct net_event *event;
event = memory_alloc(sizeof(*event));
if (!event) {
errorf("memory_alloc() failure");
return -1;
}
event->handler = handler;
event->arg = arg;
event->next = events;
events = event;
return 0;
}
int
net_event_handler(void)
{
struct net_event *event;
for (event = events; event; event = event->next) {
event->handler(event->arg);
}
return 0;
}
int
net_run(void)
{
struct net_device *dev;
if (intr_run() == -1) {
errorf("intr_run() failure");
return -1;
}
debugf("open all devices...");
for (dev = devices; dev; dev = dev->next) {
net_device_open(dev);
}
debugf("running...");
return 0;
}
void
net_shutdown(void)
{
struct net_device *dev;
debugf("close all devices...");
for (dev = devices; dev; dev = dev->next) {
net_device_close(dev);
}
debugf("shutdown");
}
#include "arp.h"
#include "ip.h"
#include "icmp.h"
#include "udp.h"
#include "tcp.h"
int
net_init(void)
{
if (intr_init() == -1) {
errorf("intr_init() failure");
return -1;
}
if (arp_init() == -1) {
errorf("arp_init() failure");
return -1;
}
if (ip_init() == -1) {
errorf("ip_init() failure");
return -1;
}
if (icmp_init() == -1) {
errorf("icmp_init() failure");
return -1;
}
if (udp_init() == -1) {
errorf("udp_init() failure");
return -1;
}
if (tcp_init() == -1) {
errorf("tcp_init() failure");
return -1;
}
infof("initialized");
return 0;
}