-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIpLink.cpp
More file actions
408 lines (362 loc) · 9.67 KB
/
IpLink.cpp
File metadata and controls
408 lines (362 loc) · 9.67 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
extern "C" {
#include "hexdump.h"
#include "checksum.h"
}
#include <iostream>
#include <iomanip>
#include <arpa/inet.h>
#include "format_si.hpp"
#include "IpLink.hpp"
namespace IpLink {
using namespace Linux;
/* Frame types */
static constexpr std::uint8_t ft_keepalive = 0x01;
static constexpr std::uint8_t ft_ip_packet = 0x02;
void IpLink::verbose_hexdump(const char *title, const void *buf, size_t len)
{
if (config.verbose) {
hexdump(title, buf, len);
}
}
void IpLink::update_meter()
{
auto rx_total = stats.get_uart_rx_bytes();
auto tx_total = stats.get_uart_tx_bytes();
rx_meter.write(rx_total);
tx_meter.write(tx_total);
if (rx_meter.size() < 2 || tx_meter.size() < 2) {
return;
}
auto rx_rate = rx_meter.rate();
auto tx_rate = tx_meter.rate();
std::cerr << "\r\x1b[K";
std::cerr << " [rx:" << format_si(rx_total, "B", 3) << " @ " << format_si(rx_rate, "B/s", 3) << "]";
std::cerr << " [tx:" << format_si(tx_total, "B", 3) << " @ " << format_si(tx_rate, "B/s", 3) << "]";
}
void IpLink::set_tun_updown(bool value)
{
if (value == tun_up) {
return;
}
tun.set_up(value);
if (value) {
std::cout << "[tun up]" << std::endl;
} else {
std::cout << "[tun down]" << std::endl;
}
tun_up = value;
rebind_tun_events();
}
void IpLink::peer_state_changed(bool value)
{
if (value == is_connected) {
return;
}
if (value) {
std::cout << "[peer connected]" << std::endl;
is_connected = true;
} else {
std::cout << "[peer disconnected]" << std::endl;
is_connected = false;
uart_rx_buf.clear();
uart_tx_buf.clear();
}
is_connected = value;
if (config.updown) {
set_tun_updown(value);
}
}
void IpLink::update_timer(Linux::TimerFD& timer, unsigned delay)
{
if (delay == 0) {
return;
}
constexpr auto billion = 1'000'000'000L;
constexpr auto million = 1'000'000L;
constexpr auto thousand = 1'000L;
Linux::TimerFD::TimeSpec deadline;
clock_gettime(Linux::Clock::monotonic, &deadline);
deadline.tv_sec += delay / thousand;
deadline.tv_nsec += delay % thousand * million;
if (deadline.tv_nsec >= billion) {
deadline.tv_nsec -= billion;
deadline.tv_sec++;
}
timer.set_absolute(deadline, true);
}
void IpLink::reset_send_ka_timer()
{
send_ka.try_read_tick_count();
update_timer(send_ka, config.keepalive_interval);
}
void IpLink::reset_recv_ka_timer()
{
recv_ka.try_read_tick_count();
update_timer(recv_ka, config.keepalive_interval);
}
void IpLink::rebind_serial_events()
{
epfd.rebind(uart,
(uart_rx_buf.empty() ? Events::event_in : Events::event_none) |
(!uart_tx_buf.empty() ? Events::event_out : Events::event_none));
}
void IpLink::rebind_tun_events()
{
epfd.rebind(tun,
(tun_up && uart_tx_buf.empty() ? Events::event_in : Events::event_none) |
(tun_up && !uart_rx_buf.empty() ? Events::event_out : Events::event_none));
}
void IpLink::rebind_events()
{
rebind_tun_events();
rebind_serial_events();
}
void IpLink::send_keepalive()
{
write_packet(ft_keepalive, &ft_keepalive, 1);
rebind_serial_events();
on_sent_keepalive();
}
void IpLink::on_sent_keepalive()
{
reset_send_ka_timer();
verbose_hexdump("[keepalive]", NULL, 0);
}
void IpLink::on_received_keepalive()
{
peer_state_changed(true);
missed_keepalives = 0;
reset_recv_ka_timer();
}
void IpLink::on_missed_keepalive()
{
if (missed_keepalives < config.keepalive_limit && ++missed_keepalives == config.keepalive_limit) {
peer_state_changed(false);
}
}
void IpLink::on_signal(Events events)
{
if (events & Events::event_in) {
const auto ssi = sfd.take_signal();
switch (ssi.ssi_signo) {
case SIGINT:
case SIGTERM:
case SIGQUIT:
terminating = true;
break;
case SIGUSR1:
stats.print(std::cout);
break;
}
}
}
void IpLink::on_update_meter(Events events)
{
if (events & Events::event_in) {
meter_timer.read_tick_count();
update_meter();
}
}
void IpLink::on_send_ka_timer(Events events)
{
if (events & Events::event_in) {
send_ka.read_tick_count();
send_keepalive();
}
}
void IpLink::on_recv_ka_timer(Events events)
{
if (events & Events::event_in) {
recv_ka.read_tick_count();
on_missed_keepalive();
reset_recv_ka_timer();
}
}
void IpLink::on_serial(Events events)
{
if (events & Events::event_in) {
on_serial_readable();
}
if (events & Events::event_out) {
on_serial_writable();
}
rebind_events();
}
void IpLink::on_tun(Events events)
{
if (events & Events::event_in) {
on_tun_readable();
}
if (events & Events::event_out) {
on_tun_writable();
}
rebind_events();
}
void IpLink::on_serial_readable()
{
buffer.resize(1 << 16);
uart.read(buffer);
stats.inc_uart_rx_bytes(buffer.size());
uart_rx_buf.splice(uart_rx_buf.end(), decoder.decode(buffer));
on_received_keepalive();
}
void IpLink::on_serial_writable()
{
/*
* Take data from queue and send it, remove sent data
* from queue
*/
const auto block_size = std::min<std::size_t>(1 << 16, uart_tx_buf.size());
buffer.resize(block_size);
const auto block_begin = uart_tx_buf.begin();
const auto block_end = block_begin + block_size;
std::copy(block_begin, block_end, buffer.begin());
const auto sent_length = uart.write(buffer.data(), buffer.size());
stats.inc_uart_tx_bytes(sent_length);
const auto sent_end = block_begin + sent_length;
uart_tx_buf.erase(block_begin, sent_end);
/* Reset keepalive timer since we've just sent data */
if (sent_length > 0) {
on_sent_keepalive();
}
}
void IpLink::on_tun_readable()
{
const auto frame = tun.recv();
if (tun_up) {
stats.inc_tun_rx_frames(1);
stats.inc_tun_rx_bytes(frame.size - sizeof(struct tun_frame_info));
write_packet(ft_ip_packet, frame.buffer, frame.size);
verbose_hexdump("TUN ==> UART", frame.buffer, frame.size);
} else {
stats.inc_tun_rx_ignored_frames(1);
stats.inc_tun_rx_ignored_bytes(frame.size - sizeof(struct tun_frame_info));
}
}
void IpLink::write_packet(std::uint8_t frame_type, const void *data, size_t size)
{
auto oit = std::back_inserter(uart_tx_buf);
oit = encoder.open(oit);
/* Write packet type */
oit = encoder.write(&frame_type, 1, oit);
/* Write payload */
oit = encoder.write(data, size, oit);
/* Write checksum */
const std::uint32_t cs = htonl(calc_checksum(data, size) ^ frame_type);
oit = encoder.write(&cs, sizeof(cs), oit);
oit = encoder.close(oit);
}
std::tuple<std::uint8_t, void *, size_t> IpLink::read_packet()
{
/* Get packet from queue */
buffer = std::move(uart_rx_buf.front());
uart_rx_buf.pop_front();
/* Validate packet */
auto p = static_cast<std::uint8_t *>(buffer.data());
auto size = buffer.size();
if (size < 5) {
std::cerr << "TOOSMALL: " << size << std::endl;
verbose_hexdump("UART =!> TUN [invalid length]", buffer.data(), buffer.size());
stats.inc_uart_rx_errors(1);
return { 0, nullptr, 0 };
}
std::uint8_t frame_type = *p;
/* Verify checksum */
std::uint32_t cs_expect = ntohl(*static_cast<std::uint32_t *>(static_cast<void *>(&p[size - 4])));
p++;
size -= 5;
std::uint32_t cs_actual = calc_checksum(p, size) ^ frame_type;
if (cs_expect != cs_actual) {
std::cerr << "CSFAIL: " << std::hex << cs_expect << " != " << cs_actual << std::dec << std::endl;
verbose_hexdump("UART =!> TUN [checksum fail]", buffer.data(), buffer.size());
stats.inc_uart_rx_errors(1);
return { 0, nullptr, 0 };
}
return { frame_type, p, size };
}
void IpLink::on_tun_writable()
{
std::uint8_t frame_type;
void *data;
std::size_t size;
std::tie(frame_type, data, size) = read_packet();
if (data == nullptr) {
return;
}
if (frame_type == ft_keepalive) {
on_received_keepalive();
} else if (frame_type == ft_ip_packet) {
if (size < 20 + sizeof(struct tun_frame_info)) {
stats.inc_uart_rx_errors(1);
std::cerr << "TOOSMALLIP: " << size << std::endl;
verbose_hexdump("UART =!> TUN [invalid IP packet length]", data, size);
return;
}
on_received_keepalive();
Frame frame(data, size);
tun.send(frame);
stats.inc_tun_tx_frames(1);
stats.inc_tun_tx_bytes(frame.size - sizeof(struct tun_frame_info));
verbose_hexdump("UART ==> TUN", frame.buffer, frame.size);
} else {
stats.inc_uart_rx_errors(1);
std::cerr << "INVALIDTYPE: " << frame_type << std::endl;
verbose_hexdump("UART =!> TUN [invalid type]", data, size);
return;
}
}
static constexpr Linux::Flags flags = Linux::close_on_exec | Linux::non_blocking;
#define bind_handler(method) \
[this] (auto events) { \
method(events); \
}
IpLink::IpLink(const Config& config) :
config(config),
sfd({ sig_int, sig_term, sig_quit, sig_usr1 }, true, flags),
meter_timer(Linux::Clock::monotonic, flags),
send_ka(Linux::Clock::monotonic, flags),
recv_ka(Linux::Clock::monotonic, flags),
uart(config.uart, config.baud, flags),
tun(config.ifname, flags),
epfd(Flags::close_on_exec),
decoder(sizeof(struct tun_frame_info) + config.mtu)
{
tun.set_point_to_point(true);
tun.set_mtu(config.mtu);
tun.set_addr(config.addr.get_address(), config.addr.get_mask());
// tun.set_route(remote_addr, 1, remote_addr, link_mask);
epfd.bind(sfd, bind_handler(on_signal), Events::event_in);
epfd.bind(meter_timer, bind_handler(on_update_meter), Events::event_in);
epfd.bind(send_ka, bind_handler(on_send_ka_timer), Events::event_in);
epfd.bind(recv_ka, bind_handler(on_recv_ka_timer), Events::event_in);
epfd.bind(uart, bind_handler(on_serial), Events::event_in);
epfd.bind(tun, bind_handler(on_tun), Events::event_in);
if (!config.updown) {
set_tun_updown(true);
}
}
void IpLink::run()
{
if (config.meter) {
rx_meter = { 15, 0.5 };
tx_meter = { 15, 0.5 };
Linux::TimerFD::TimeSpec now;
now.tv_sec = 0;
now.tv_nsec = 1;
Linux::TimerFD::TimeSpec interval;
interval.tv_sec = 0;
interval.tv_nsec = 500000000;
meter_timer.set_periodic(now, interval);
}
reset_send_ka_timer();
reset_recv_ka_timer();
send_keepalive();
rebind_events();
while (!terminating) {
epfd.wait();
}
if (config.meter) {
std::cerr << std::endl;
}
}
}