-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharp.c
223 lines (189 loc) · 4.71 KB
/
arp.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
#include <stddef.h>
#include <stdio.h>
#include "netsniff.h"
#include "netif.h"
#include "etherif.h"
#include "mbuf.h"
#include "arp.h"
#include "unet.h"
#define ARP_NR_MAX_ENTRIES 10
static unsigned char zeromac[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
static unsigned char broadcastmac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
static inline void memswap(void *__restrict dst, void *__restrict src,
void *__restrict tmp, size_t n);
static void arp_request(struct netif *ifp, ipv4_t *sip, ipv4_t *tip,
hwaddr_t *ether);
struct arpentry {
uint16_t ae_st; /* ARP entry state, one of: */
#define ARP_E_FREE 0
#define ARP_E_INCOMPLETE 1
#define ARP_E_COMPLETE 2
hwaddr_t ae_ha;
ipv4_t ae_ip;
struct mbuf *ae_wq_head;
};
static struct arpentry arp_tab[ARP_NR_MAX_ENTRIES];
struct arpentry *arp_lookup(ipv4_t ip)
{
struct arpentry *ae;
for (ae = &arp_tab[0]; ae < &arp_tab[ARP_NR_MAX_ENTRIES]; ae++) {
if (ip.addr == ae->ae_ip.addr)
return ae;
}
return NULL;
}
struct arpentry *arp_newentry(ipv4_t addr)
{
struct arpentry *ae;
for (ae = &arp_tab[0]; ae < &arp_tab[ARP_NR_MAX_ENTRIES]; ae++) {
if (ae->ae_st == ARP_E_FREE)
goto got_it;
}
ae = &arp_tab[0];
got_it:
ae->ae_ip = addr;
ae->ae_st = ARP_E_INCOMPLETE;
return ae;
}
static int __arp_resolve(ipv4_t dstaddr, struct mbuf *m, hwaddr_t *dsthw)
{
struct arpentry *ae;
struct mbuf **last;
if ((ae = arp_lookup(dstaddr))) {
switch (ae->ae_st) {
case ARP_E_COMPLETE:
memcpy(dsthw, &ae->ae_ha, HWADDR_LEN);
return 1;
case ARP_E_INCOMPLETE:
break;
case ARP_E_FREE:
ae->ae_st = ARP_E_INCOMPLETE;
}
} else {
ae = arp_newentry(dstaddr);
}
if (m) {
last = mb_lastpp(&ae->ae_wq_head);
*last = m;
}
return 0;
}
int arp_resolve(struct netif *ifp, ipv4_t dstaddr,
struct mbuf *m, hwaddr_t *dsthw)
{
/* Cache hit */
if (__arp_resolve(dstaddr, m, dsthw))
return 1;
arp_request(ifp, &ifp->ipaddr, &dstaddr, &ifp->hwaddr);
return 0;
}
void
arp_print(struct arphdr *hdr)
{
#ifdef DEBUG
char sip[sizeof("255.255.255.255")];
char tip[sizeof(sip)];
char sha[sizeof("ff:ff:ff:ff:ff:ff")];
char tha[sizeof(sha)];
pr_dbg("ARP packet (%zu) bytes\n"
"ar_hrd: %hu\n"
"ar_pro: %hu\n"
"ar_hln: %u\n"
"ar_pln: %u\n"
"ar_op: %hu\n"
"ar_sha: %s\n"
"ar_spa: %s\n"
"ar_tha: %s\n"
"ar_tpa: %s\n\n",
arp_hdr_len(hdr),
ntohs(hdr->ar_hrd),
ntohs(hdr->ar_pro),
hdr->ar_hln,
hdr->ar_pln,
ntohs(hdr->ar_op),
macstr(sha, ar_sha(hdr)),
inet_ntop(AF_INET, ar_spa(hdr), sip, 32),
macstr(tha, ar_tha(hdr)),
inet_ntop(AF_INET, ar_tpa(hdr), tip, 32));
#endif
}
static void
arp_reply(struct netif *rcvif, struct arphdr *req)
{
struct mbuf *mbuf;
struct arphdr *resp;
mbuf = mb_alloc();
if (!mbuf)
return;
mb_init(mbuf);
mb_reserve(mbuf, ETH_HLEN);
resp = mb_put(mbuf, ARP4_HDR_LEN);
memcpy(resp, req, offsetof(typeof(*resp), ar_op));
resp->ar_op = htons(ARPOP_REPLY);
memcpy(ar_sha(resp), &rcvif->hwaddr, HWADDR_LEN);
memcpy(ar_spa(resp), &rcvif->ipaddr, sizeof(rcvif->ipaddr));
memcpy(ar_tha(resp), ar_sha(req), HWADDR_LEN);
memcpy(ar_tpa(resp), ar_spa(req), sizeof(rcvif->ipaddr));
pr_dbg("Response\n");
arp_print(resp);
eth_output(rcvif, mbuf, ar_tha(resp), ETH_P_ARP);
}
void
arp_recv(struct netif *rcvif, struct mbuf *m)
{
struct arphdr *hdr;
struct arpentry *ent;
ipv4_t ip;
hdr = arp_hdr(mb_htrim(m, sizeof(*hdr)));
switch (hdr->ar_op) {
case htons(ARPOP_REQUEST):
arp_print(hdr);
arp_reply(rcvif, hdr);
break;
case htons(ARPOP_REPLY):
ip.addr = *(uint32_t *)ar_spa(hdr);
ent = arp_lookup(ip);
if (!ent)
break;
memcpy(&ent->ae_ha, ar_sha(hdr), HWADDR_LEN);
if (ent->ae_wq_head) {
eth_output(rcvif, ent->ae_wq_head, ent->ae_ha.data,
ETH_P_IP);
ent->ae_wq_head = NULL;
}
ent->ae_st = ARP_E_COMPLETE;
break;
}
/* XXX is here the right place to release? */
mb_chain_free(m);
}
static void
arp_request(struct netif *ifp, ipv4_t *sip, ipv4_t *tip, hwaddr_t *ether)
{
struct mbuf *m;
struct arphdr *ap;
if ((m = mb_alloc()) == NULL)
return;
mb_reserve(m, ETH_HLEN);
ap = mb_put(m, ARP4_HDR_LEN);
ap->ar_hrd = htons(ARPHRD_ETHER);
ap->ar_pro = htons(ETH_P_IP);
ap->ar_hln = ETH_ALEN;
ap->ar_pln = 4;
ap->ar_op = htons(ARPOP_REQUEST);
memcpy(ar_sha(ap), ether, HWADDR_LEN);
memcpy(ar_spa(ap), sip, sizeof(*sip));
memcpy(ar_tha(ap), zeromac, HWADDR_LEN);
memcpy(ar_tpa(ap), tip, sizeof(*tip));
pr_dbg("Request\n");
arp_print(ap);
eth_output(ifp, m, broadcastmac, ETH_P_ARP);
}
static inline void
memswap(void *__restrict dst, void *__restrict src,
void *__restrict tmp, size_t n)
{
memcpy(tmp, dst, n);
memcpy(dst, src, n);
memcpy(src, tmp, n);
}