-
Notifications
You must be signed in to change notification settings - Fork 4
/
net.c
144 lines (125 loc) · 3.09 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
#include "net.h"
#include <stdint.h>
#include <stdlib.h>
#include "util.h"
struct netdev_driver {
struct netdev_driver *next;
uint16_t type;
uint16_t mtu;
uint16_t flags;
uint16_t hlen;
uint16_t alen;
struct netdev_ops *ops;
};
struct netdev_proto {
struct netdev_proto *next;
uint16_t type;
void (*handler)(uint8_t *packet, size_t plen, struct netdev *dev);
};
static struct netdev_driver *drivers = NULL;
static struct netdev_proto *protos = NULL;
static struct netdev *devices = NULL;
int netdev_driver_register(struct netdev_def *def) {
struct netdev_driver *entry;
for (entry = drivers; entry; entry = entry->next) {
if (entry->type == def->type) {
return -1;
}
}
entry = malloc(sizeof(struct netdev_driver));
if (!entry) {
return -1;
}
entry->next = drivers;
entry->type = def->type;
entry->mtu = def->mtu;
entry->flags = def->flags;
entry->hlen = def->hlen;
entry->alen = def->alen;
entry->ops = def->ops;
drivers = entry;
return 0;
}
int netdev_proto_register(unsigned short type,
void (*handler)(uint8_t *packet, size_t plen,
struct netdev *dev)) {
struct netdev_proto *entry;
// check this proto is already registered
for (entry = protos; entry; entry = entry->next) {
if (entry->type == type) {
return -1;
}
}
entry = malloc(sizeof(struct netdev_proto));
if (!entry) {
return -1;
}
entry->next = protos;
entry->type = type;
entry->handler = handler;
protos = entry;
return 0;
}
static void netdev_rx_handler(struct netdev *dev, uint16_t type,
uint8_t *packet, size_t plen) {
struct netdev_proto *entry;
for (entry = protos; entry; entry = entry->next) {
if (hton16(entry->type) == type) {
entry->handler(packet, plen, dev);
}
}
}
struct netdev *netdev_root(void) {
return devices;
}
struct netdev *netdev_alloc(uint16_t type) {
struct netdev_driver *driver;
struct netdev *dev;
// find driver matches type
for (driver = drivers; driver; driver = driver->next) {
if (driver->type == type) {
break;
}
}
if (!driver) {
return NULL;
}
dev = malloc(sizeof(struct netdev));
if (!dev) {
return NULL;
}
dev->next = devices;
dev->ifs = NULL;
dev->type = driver->type;
dev->mtu = driver->mtu;
dev->flags = driver->flags;
dev->hlen = driver->hlen;
dev->alen = driver->alen;
dev->rx_handler = netdev_rx_handler;
dev->ops = driver->ops;
devices = dev;
return dev;
}
int netdev_add_netif(struct netdev *dev, struct netif *netif) {
struct netif *entry;
// check netif is already registered to this netdev
for (entry = dev->ifs; entry; entry = entry->next) {
if (entry->family == netif->family) {
return -1;
}
}
// set netif
netif->next = dev->ifs;
netif->dev = dev;
dev->ifs = netif;
return 0;
}
struct netif *netdev_get_netif(struct netdev *dev, int family) {
struct netif *entry;
for (entry = dev->ifs; entry; entry = entry->next) {
if (entry->family == family) {
return entry;
}
}
return NULL;
}