forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 14
/
net.c
153 lines (132 loc) · 3.1 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
// Copyright (c) 2012-2020 YAMAMOTO Masaya
// SPDX-License-Identifier: MIT
#include "types.h"
#include "defs.h"
#include "net.h"
#include "ip.h"
struct netproto {
struct netproto *next;
uint16_t type;
void (*handler)(uint8_t *packet, size_t plen, struct netdev *dev);
};
static struct netdev *devices;
static struct netproto *protocols;
struct netdev *
netdev_root(void)
{
return devices;
}
struct netdev *
netdev_alloc(void (*setup)(struct netdev *))
{
struct netdev *dev;
static unsigned int index = 0;
dev = (struct netdev *)kalloc();
if (!dev) {
return NULL;
}
memset(dev, 0, sizeof(struct netdev));
dev->index = index++;
snprintf(dev->name, sizeof(dev->name), "net%d", dev->index);
setup(dev);
return dev;
}
int
netdev_register(struct netdev *dev)
{
cprintf("[net] netdev_register: <%s>\n", dev->name);
dev->next = devices;
devices = dev;
return 0;
}
struct netdev *
netdev_by_index(int index)
{
struct netdev *dev;
for (dev = devices; dev; dev = dev->next)
if (dev->index == index)
return dev;
return NULL;
}
struct netdev *
netdev_by_name(const char *name)
{
struct netdev *dev;
for (dev = devices; dev; dev = dev->next)
if (strcmp(dev->name, name) == 0)
return dev;
return NULL;
}
void
netdev_receive(struct netdev *dev, uint16_t type, uint8_t *packet, unsigned int plen)
{
struct netproto *entry;
#ifdef DEBUG
cprintf("[net] netdev_receive: dev=%s, type=%04x, packet=%p, plen=%u\n", dev->name, type, packet, plen);
#endif
for (entry = protocols; entry; entry = entry->next) {
if (hton16(entry->type) == type) {
entry->handler(packet, plen, dev);
return;
}
}
}
int
netdev_add_netif(struct netdev *dev, struct netif *netif)
{
struct netif *entry;
for (entry = dev->ifs; entry; entry = entry->next) {
if (entry->family == netif->family) {
return -1;
}
}
#ifdef DEBUG
if (netif->family == NETIF_FAMILY_IPV4) {
char addr[IP_ADDR_STR_LEN];
cprintf("[net] Add <%s> to <%s>\n", ip_addr_ntop(&((struct netif_ip *)netif)->unicast, addr, sizeof(addr)), dev->name);
}
#endif
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;
}
int
netproto_register(unsigned short type, void (*handler)(uint8_t *packet, size_t plen, struct netdev *dev))
{
struct netproto *entry;
for (entry = protocols; entry; entry = entry->next) {
if (entry->type == type) {
return -1;
}
}
entry = (struct netproto *)kalloc();
if (!entry) {
return -1;
}
entry->next = protocols;
entry->type = type;
entry->handler = handler;
protocols = entry;
return 0;
}
void
netinit(void)
{
arp_init();
ip_init();
icmp_init();
udp_init();
tcp_init();
}