summaryrefslogtreecommitdiff
path: root/src/user/app/netstack/arp.c
blob: 3a1c8da233e35d8695d8591dcfaa939af2ca55b8 (plain)
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
#include "proto.h"
#include "util.h"
#include <assert.h>
#include <camellia/syscalls.h>
#include <string.h>

enum {
	HdrType   = 0,
		HdrTypeEther = 1,
	ProtoType = 2,
	HdrALen   = 4,
	ProtoALen = 5,
	Operation = 6,
		OpReq   = 1,
		OpReply = 2,
};

struct arpc {
	struct arpc *next;
	uint32_t ip;
	mac_t mac;
};
static struct arpc *arpcache;
static void arpcache_put(uint32_t ip, mac_t mac);

void arp_parse(const uint8_t *buf, size_t len) {
	if (len < Operation + 2) return;
	uint16_t htype = nget16(buf + HdrType);
	uint16_t ptype = nget16(buf + ProtoType);
	uint16_t op = nget16(buf + Operation);

	if (!(htype == HdrTypeEther && ptype == ET_IPv4)) return;
	enum { /* only valid for this combination of header + proto */
		SrcMAC =  8,
		SrcIP  = 14,
		DstMAC = 18,
		DstIP  = 24,
	};
	if (len < DstIP + 4) return;
	arpcache_put(nget32(buf + SrcIP), *(mac_t*)buf + SrcMAC);

	if (op == OpReq) {
		uint32_t daddr = nget32(buf + DstIP);
		if (daddr == state.ip) {
			uint8_t *pkt = ether_start(30, (struct ethernet){
				.dst = (void*)(buf + SrcMAC),
				.type = ET_ARP,
			});
			nput16(pkt + HdrType, HdrTypeEther);
			nput16(pkt + ProtoType, ET_IPv4);
			pkt[HdrALen] = 6;
			pkt[ProtoALen] = 4;
			nput16(pkt + Operation, OpReply);
			memcpy(pkt + SrcMAC, state.mac, 6);
			nput32(pkt + SrcIP, state.ip);
			memcpy(pkt + DstMAC, buf + SrcMAC, 10); /* sender's MAC and IP */
			ether_finish(pkt);
		}
	}
}

void arp_request(uint32_t ip) {
	enum {
		SrcMAC =  8,
		SrcIP  = 14,
		DstMAC = 18,
		DstIP  = 24,
	};
	uint8_t *pkt = ether_start(28, (struct ethernet){
		.src = &state.mac,
		.dst = &MAC_BROADCAST,
		.type = ET_ARP,
	});
	nput16(pkt + HdrType, HdrTypeEther);
	nput16(pkt + ProtoType, ET_IPv4);
	pkt[HdrALen] = 6;
	pkt[ProtoALen] = 4;
	nput16(pkt + Operation, OpReq);
	memcpy(pkt + SrcMAC, state.mac, 6);
	nput32(pkt + SrcIP, state.ip);
	memcpy(pkt + DstMAC, &MAC_BROADCAST, 6);
	nput32(pkt + DstIP, ip);
	ether_finish(pkt);
}

static void arpcache_put(uint32_t ip, mac_t mac) {
	for (struct arpc *iter = arpcache; iter; iter = iter->next) {
		if (memcmp(iter->mac, mac, 6) == 0) {
			if (iter->ip == ip) return; /* cache entry correct */
			else break; /* cache entry needs updating */
		}
	}
	struct arpc *e = malloc(sizeof *e);
	e->next = arpcache;
	e->ip = ip;
	memcpy(e->mac, mac, 6);
	arpcache = e;
}

int arpcache_get(uint32_t ip, mac_t *mac) {
	for (struct arpc *iter = arpcache; iter; iter = iter->next) {
		if (iter->ip == ip) {
			if (mac) memcpy(mac, iter->mac, 6);
			return 0;
		}
	}
	return -1;
}

void arp_fsread(hid_t h, long offset) {
	const char *fmt = "%08x\t%02x:%02x:%02x:%02x:%02x:%02x\n";
	long linelen = snprintf(NULL, 0, fmt, 0, 1, 2, 3, 4, 5, 6) + 1;
	char buf[28];
	assert(linelen <= (long)sizeof(buf));
	if (offset < 0) goto err;

	struct arpc *cur = arpcache;
	if (!cur) goto err;
	for (; linelen <= offset; offset -= linelen) {
		cur = cur->next;
		if (!cur) goto err;
	}
	assert(0 <= offset && offset < linelen);

	snprintf(buf, sizeof buf, fmt, cur->ip,
		cur->mac[0],
		cur->mac[1],
		cur->mac[2],
		cur->mac[3],
		cur->mac[4],
		cur->mac[5]);
	_sys_fs_respond(h, buf + offset, linelen - offset, 0);
	return;
err:
	_sys_fs_respond(h, NULL, -1, 0);
}

long arp_fswrite(const char *buf, long len, long offset) {
	if (offset != -1) return -1;
	uint32_t ip;
	char tmp[16];
	size_t iplen = len < 15 ? len : 15;
	memcpy(tmp, buf, iplen);
	tmp[iplen] = '\0';
	if (ip_parse(tmp, &ip) < 0) {
		return -1;
	} else {
		arp_request(ip);
		return len;
	}
}