-
Notifications
You must be signed in to change notification settings - Fork 357
/
arp.cpp
246 lines (185 loc) · 6.7 KB
/
arp.cpp
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//#define ARP_DEBUG 1
#ifdef ARP_DEBUG
#define PRINT(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define PRINT(fmt, ...) /* fmt */
#endif
#include <vector>
#include <net/inet>
#include <net/ip4/arp.hpp>
#include <net/ip4/packet_arp.hpp>
#include <statman>
using namespace std::chrono;
namespace net {
// Initialize
Arp::Arp(Stack& inet) noexcept:
requests_rx_ {Statman::get().create(Stat::UINT32, inet.ifname() + ".arp.requests_rx").get_uint32()},
requests_tx_ {Statman::get().create(Stat::UINT32, inet.ifname() + ".arp.requests_tx").get_uint32()},
replies_rx_ {Statman::get().create(Stat::UINT32, inet.ifname() + ".arp.replies_rx").get_uint32()},
replies_tx_ {Statman::get().create(Stat::UINT32, inet.ifname() + ".arp.replies_tx").get_uint32()},
inet_ {inet},
mac_ (inet.link_addr())
{}
void Arp::receive(Packet_ptr pckt) {
PRINT("<ARP handler> got %i bytes of data\n", pckt->size());
header* hdr = reinterpret_cast<header*>(pckt->layer_begin());
/// cache entry
this->cache(hdr->sipaddr, hdr->shwaddr);
/// always try to ship waiting packets when someone talks
auto waiting = waiting_packets_.find(hdr->sipaddr);
if (waiting != waiting_packets_.end()) {
PRINT("<Arp> Had a packet waiting for this IP. Sending\n");
transmit(std::move(waiting->second.pckt), hdr->sipaddr);
waiting_packets_.erase(waiting);
}
switch(hdr->opcode) {
case H_request: {
// Stat increment requests received
requests_rx_++;
PRINT("<Arp> %s is looking for %s\n",
hdr->sipaddr.str().c_str(),
hdr->dipaddr.str().c_str());
if (hdr->dipaddr == inet_.ip_addr()) {
// The packet is for us. Respond.
arp_respond(hdr, inet_.ip_addr());
} else if (proxy_ and proxy_(hdr->dipaddr)){
// The packet is for an IP to which we know a route
arp_respond(hdr, hdr->dipaddr);
} else {
// Drop
PRINT("\t NO MATCH for My IP (%s). DROP!\n",
inet_.ip_addr().str().c_str());
}
break;
}
case H_reply: {
// Stat increment replies received
replies_rx_++;
PRINT("\t ARP REPLY: %s belongs to %s (waiting: %zu)\n",
hdr->sipaddr.str().c_str(), hdr->shwaddr.str().c_str(), waiting_packets_.size());
break;
}
default:
PRINT("\t UNKNOWN OPCODE\n");
break;
} //< switch(hdr->opcode)
}
void Arp::cache(ip4::Addr ip, MAC::Addr mac) {
PRINT("<Arp> Caching IP %s for %s\n", ip.str().c_str(), mac.str().c_str());
auto entry = cache_.find(ip);
if (entry != cache_.end()) {
PRINT("Cached entry found: %s recorded @ %zu. Updating timestamp\n",
entry->second.mac().str().c_str(), entry->second.timestamp());
if (entry->second.mac() != mac) {
cache_.erase(entry);
cache_.emplace(ip, mac);
} else {
entry->second.update();
}
} else {
cache_.emplace(ip, mac); // Insert
if (UNLIKELY(not flush_timer_.is_running())) {
flush_timer_.start(flush_interval_);
}
}
}
void Arp::arp_respond(header* hdr_in, ip4::Addr ack_ip) {
PRINT("\t IP Match. Constructing ARP Reply\n");
// Stat increment replies sent
replies_tx_++;
// Populate ARP-header
auto res = static_unique_ptr_cast<PacketArp>(inet_.create_packet());
res->init(mac_, ack_ip, hdr_in->sipaddr);
res->set_dest_mac(hdr_in->shwaddr);
res->set_opcode(H_reply);
PRINT("\t IP: %s is at My Mac: %s\n",
res->source_ip().str().c_str(), res->source_mac().str().c_str());
MAC::Addr dest = hdr_in->shwaddr;
PRINT("<ARP -> physical> Sending response to %s. Linklayer begin: buf + %li \n",
dest.str().c_str(), res->layer_begin() - res->buf() );
linklayer_out_(std::move(res), dest, Ethertype::ARP);
}
void Arp::transmit(Packet_ptr pckt, ip4::Addr next_hop) {
Expects(pckt->size());
PRINT("<ARP -> physical> Transmitting %u bytes to %s\n",
(uint32_t) pckt->size(), next_hop.str().c_str());
MAC::Addr dest_mac;
if (next_hop == IP4::ADDR_BCAST) {
dest_mac = MAC::BROADCAST;
} else {
#ifdef ARP_PASSTHROUGH
extern MAC::Addr linux_tap_device;
dest_mac = linux_tap_device;
#else
// If we don't have a cached IP, perform address resolution
auto cache_entry = cache_.find(next_hop);
if (UNLIKELY(cache_entry == cache_.end())) {
PRINT("<ARP> No cache entry for IP %s. Resolving. \n", next_hop.to_string().c_str());
await_resolution(std::move(pckt), next_hop);
return;
}
// Get MAC from cache
dest_mac = cache_[next_hop].mac();
#endif
PRINT("<ARP> Found cache entry for IP %s -> %s \n",
next_hop.to_string().c_str(), dest_mac.to_string().c_str());
}
// Move chain to linklayer
linklayer_out_(std::move(pckt), dest_mac, Ethertype::IP4);
}
void Arp::resolve_waiting()
{
PRINT("<Arp> resolve timer doing sweep\n");
for (auto it =waiting_packets_.begin(); it != waiting_packets_.end();){
if (it->second.tries_remaining--) {
arp_resolver_(it->first);
it++;
} else {
it = waiting_packets_.erase(it);
}
}
if (not waiting_packets_.empty())
resolve_timer_.start(1s);
}
void Arp::await_resolution(Packet_ptr pckt, ip4::Addr next_hop) {
auto queue = waiting_packets_.find(next_hop);
PRINT("<ARP await> Waiting for resolution of %s\n", next_hop.str().c_str());
if (queue != waiting_packets_.end()) {
PRINT("\t * Packets already queueing for this IP\n");
queue->second.pckt->chain(std::move(pckt));
} else {
PRINT("\t *This is the first packet going to that IP\n");
waiting_packets_.emplace(std::make_pair(next_hop, Queue_entry{std::move(pckt)}));
// Try resolution immediately
arp_resolver_(next_hop);
// Retry later
resolve_timer_.start(1s);
}
}
void Arp::arp_resolve(ip4::Addr next_hop) {
PRINT("<ARP RESOLVE> %s\n", next_hop.str().c_str());
auto req = static_unique_ptr_cast<PacketArp>(inet_.create_packet());
req->init(mac_, inet_.ip_addr(), next_hop);
req->set_dest_mac(MAC::BROADCAST);
req->set_opcode(H_request);
// Stat increment requests sent
requests_tx_++;
linklayer_out_(std::move(req), MAC::BROADCAST, Ethertype::ARP);
}
void Arp::flush_expired()
{
PRINT("<ARP> Flushing expired entries\n");
std::vector<ip4::Addr> expired;
for (auto ent : cache_) {
if (ent.second.expired()) {
expired.push_back(ent.first);
}
}
for (auto ip : expired) {
cache_.erase(ip);
}
if (not cache_.empty()) {
flush_timer_.start(flush_interval_);
}
}
} //< namespace net