-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathip.c
More file actions
173 lines (154 loc) · 3.92 KB
/
Copy pathip.c
File metadata and controls
173 lines (154 loc) · 3.92 KB
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
#include <arpa/inet.h>
#include <string.h>
#include "console.h"
#include "net.h"
#include "icmp.h"
#include "udp.h"
#include "ip.h"
/**
* Computes the checksum of the buffer having length
* DOCS: https://tools.ietf.org/html/rfc1071
*/
uint16_t ip_csum(void *buf, int len)
{
uint32_t sum = 0;
uint16_t *ptr = buf;
while (len > 1) {
sum += *ptr++;
len -= 2;
}
if (len > 0)
sum += *(uint8_t *) ptr;
while (sum >> 16)
sum = (sum & 0xFFFF) + (sum >> 16);
return ~sum;
}
/**
* An ugly function to dump an IP header
*/
void ipv4_hdr_dump(ipv4_hdr_t *iph)
{
kprintf("version: %d\n", iph->version);
kprintf("ihl: %d (%d bytes)\n", iph->ihl, iph->ihl * 4);
kprintf("tos: %d\n", iph->tos);
kprintf("total length: %d bytes\n", iph->len);
kprintf("id: %#x\n", iph->id);
kprintf("flags: %#x\n", iph->flags);
kprintf("fragment offset: %#x (%#x bytes)\n",
iph->frag_offs, iph->frag_offs << 3);
kprintf("ttl: %d\n", iph->ttl);
kprintf("protocol: %d\n", iph->proto);
kprintf("csum: %#x %s\n", iph->csum);
kprintf("saddr: ");
print_ip(iph->saddr);
kprintf("\n");
kprintf("daddr: ");
print_ip(iph->daddr);
kprintf("\n");
}
/**
* Gets the length of the IP header.
* buf is the start of the IP header, in network format.
*/
int iphlen(void *buf)
{
uint8_t hlen = *(uint8_t *)buf;
if (is_bige)
hlen = (hlen >> 4);
return (hlen & 0x07) << 2;
}
/**
* Converts from network format IP header into internal representation
*/
static ipv4_hdr_t *buf2ipv4(void *buf)
{
ipv4_hdr_t *iph = buf;
if (!is_bige) {
uint8_t ihl = iph->version;
iph->version = iph->ihl;
iph->ihl = ihl;
uint16_t frg = ntohs(*((uint16_t *)iph + 3));
iph->flags = (frg >> 13) & 0x7;
iph->frag_offs = frg & 0x1FFF;
}
iph->len = ntohs(iph->len);
iph->id = ntohs(iph->id);
iph->csum = ntohs(iph->csum);
iph->saddr = ntohl(iph->saddr);
iph->daddr = ntohl(iph->daddr);
return iph;
}
/**
* Converts an IP header to network format
*/
void *ipv42buf(ipv4_hdr_t *iph)
{
if (!is_bige) {
uint8_t ihl = iph->version;
iph->version = iph->ihl;
iph->ihl = ihl;
uint16_t *ptr = (uint16_t *)iph + 3;
uint16_t val = *ptr;
// XXX more tests here!
*ptr = htons(((val & 0x7) << 13) | ((val & ~0x7) >> 3));
}
iph->len = ntohs(iph->len);
iph->id = ntohs(iph->id);
iph->csum = ntohs(iph->csum);
iph->saddr = ntohl(iph->saddr);
iph->daddr = ntohl(iph->daddr);
return iph;
}
void *ip_data(ipv4_hdr_t *iph)
{
return ((uint8_t *)iph + (iph->ihl * 4));
}
/**
* Process an IP packet.
* Expects buf->data to be in network format.
*/
int ip_process(struct net_buf *buf)
{
int i;
eth_hdr_t *eth = buf2eth(buf->data);
uint16_t csum = ip_csum(eth->data, iphlen(eth->data));
//hexdump(eth->data, buf->size - sizeof(*eth));
ipv4_hdr_t *iph = buf2ipv4(eth->data);
struct netif *netif = &netifs[buf->nic];
int ip_found = 0;
if (iph->version != IPV4)
return -1;
if (iph->ihl < 5)
return -1;
if (iph->ttl == 0)
return -1;
if (csum != 0)
return -1;
/* Is this packet for us? */
if (iph->daddr == 0xFFFFFFFF) {
ip_found = 1;
} else {
for (i = 0; i < IFACE_MAX_IPS; i++)
if (netif->ips[i] == iph->daddr) {
ip_found = 1;
break;
}
}
if (!ip_found) {
net_dbg("packet not maching local ip or bcast: %#x\n", iph->daddr);
return -1;
}
switch (iph->proto) {
case IP_P_ICMP:
icmpv4_process(buf);
break;
case IP_P_UDP:
udp_process(buf);
break;
case IP_P_TCP:
default:
kprintf("Unsupported IP header protocol: %d\n", iph->proto);
break;
}
return 0;
}