-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipv6.cpp
194 lines (143 loc) · 5.59 KB
/
ipv6.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
// (C) 2020-2022 by folkert van heusden <[email protected]>, released under Apache License v2.0
#include <assert.h>
#include <chrono>
#include <optional>
#include <stdint.h>
#include <string>
#include <string.h>
#include <arpa/inet.h>
#include "icmp.h"
#include "ipv6.h"
#include "log.h"
#include "phys.h"
#include "router.h"
#include "str.h"
#include "utils.h"
ipv6::ipv6(stats *const s, ndp *const indp, const any_addr & myip, router *const r, const int n_threads) : network_layer(s, "ipv6", r), indp(indp), myip(myip)
{
ip_n_pkt = s->register_stat("ip_n_pkt", "1.3.6.1.2.1.4.3");
ip_n_disc = s->register_stat("ip_n_discards", "1.3.6.1.2.1.4.8");
ip_n_del = s->register_stat("ip_n_delivers", "1.3.6.1.2.1.4.9");
ip_n_out_req = s->register_stat("ip_n_out_req", "1.3.6.1.2.1.4.10");
ip_n_out_disc = s->register_stat("ip_n_out_req", "1.3.6.1.2.1.4.11");
ipv6_n_pkt = s->register_stat("ipv6_n_pkt");
ipv6_not_me = s->register_stat("ipv6_not_me");
ipv6_ttl_ex = s->register_stat("ipv6_ttl_ex");
ipv6_unk_prot = s->register_stat("ipv6_unk_prot");
ipv6_n_tx = s->register_stat("ipv6_n_tx");
ipv6_tx_err = s->register_stat("ipv6_tx_err");
assert(myip.get_family() == any_addr::ipv6);
for(int i=0; i<n_threads; i++)
ths.push_back(new std::thread(std::ref(*this)));
}
ipv6::~ipv6()
{
for(auto & th : ths) {
th->join();
delete th;
}
}
bool ipv6::transmit_packet(const std::optional<any_addr> & dst_mac, const any_addr & dst_ip, const any_addr & src_ip, const uint8_t protocol, const uint8_t *payload, const size_t pl_size, const uint8_t *const header_template)
{
stats_inc_counter(ipv6_n_tx);
stats_inc_counter(ip_n_out_req);
size_t out_size = 40 + pl_size;
uint8_t *out = new uint8_t[out_size];
out[0] = 0x60; // IPv6
// not sure if this should be stored per session
uint32_t flow_label = 0;
get_random((uint8_t *)&flow_label, sizeof flow_label);
out[1] = (flow_label >> 16) & 0x0f;
out[2] = flow_label >> 8;
out[3] = flow_label;
out[4] = pl_size >> 8;
out[5] = pl_size;
out[6] = protocol;
out[7] = 255; // basically ignored according to wikipedia?
src_ip.get(&out[8], 16);
dst_ip.get(&out[24], 16);
if (pl_size)
memcpy(&out[40], payload, pl_size);
bool rc = r->route_packet(dst_mac, 0x86dd, dst_ip, { }, src_ip, out, out_size);
delete [] out;
return rc;
}
void ipv6::operator()()
{
set_thread_name("myip-ipv6");
for(;;) {
auto po = pkts->get();
if (!po.has_value())
break;
packet *pkt = po.value().p;
stats_inc_counter(ip_n_pkt);
const uint8_t *const p = pkt->get_data();
int size = pkt->get_size();
if (size < 40) {
DOLOG(ll_info, "IPv6: not an IPv6 packet (size: %d)\n", size);
stats_inc_counter(ip_n_disc);
delete pkt;
continue;
}
const uint8_t *const payload_header = &p[0];
//const uint32_t flow_label = ((payload_header[1] & 15) << 16) | (payload_header[2] << 8) | payload_header[3];
uint8_t version = payload_header[0] >> 4;
if (version != 0x06) {
CDOLOG(ll_info, pkt->get_log_prefix().c_str(), "not an IPv6 packet (version: %d)\n", version);
stats_inc_counter(ip_n_disc);
delete pkt;
continue;
}
stats_inc_counter(ipv6_n_pkt);
any_addr pkt_dst(any_addr::ipv6, &payload_header[24]);
any_addr pkt_src(any_addr::ipv6, &payload_header[8]);
pkt->add_to_log_prefix(myformat("[IPv6:%s]", pkt_src.to_str().c_str()));
CDOLOG(ll_debug, pkt->get_log_prefix().c_str(), "packet %s => %s\n", pkt_src.to_str().c_str(), pkt_dst.to_str().c_str());
bool link_local_scope_multicast_adress = pkt_dst[0] == 0xff && pkt_dst[1] == 0x02;
if (pkt_dst != myip && !link_local_scope_multicast_adress) {
CDOLOG(ll_debug, pkt->get_log_prefix().c_str(), "packet (%s) not for me (=%s)\n", pkt_src.to_str().c_str(), myip.to_str().c_str());
delete pkt;
stats_inc_counter(ipv6_not_me);
stats_inc_counter(ip_n_disc);
continue;
}
if (pkt->get_is_forwarded() == false)
indp->update_cache(pkt->get_src_addr(), pkt_src, po.value().interface);
int ip_size = (payload_header[4] << 8) | payload_header[5];
if (ip_size > size) {
CDOLOG(ll_info, pkt->get_log_prefix().c_str(), "packet is bigger on the inside (%d) than on the outside (%d)\n", ip_size, size);
ip_size = size;
}
uint8_t protocol = payload_header[6];
const uint8_t *nh = &payload_header[40], *const eh = &payload_header[size - ip_size];
while(eh - nh >= 8) {
protocol = nh[0];
nh += (nh[1] + 1) * 8;
}
int header_size = nh - payload_header;
CDOLOG(ll_debug, pkt->get_log_prefix().c_str(), "total packet size: %d, IP header says: %d, header size: %d\n", size, ip_size, header_size);
if (ip_size > size) {
CDOLOG(ll_info, pkt->get_log_prefix().c_str(), "size (%d) > Ethernet size (%d)\n", ip_size, size);
delete pkt;
stats_inc_counter(ip_n_disc);
continue;
}
// adjust size indication to what IP-header says; Ethernet adds padding for small packets (< 60 bytes)
size = ip_size;
const uint8_t *payload_data = &payload_header[header_size];
int payload_size = size;
auto it = prot_map.find(protocol);
if (it == prot_map.end()) {
CDOLOG(ll_debug, pkt->get_log_prefix().c_str(), "dropping packet %02x (= unknown protocol) and size %d\n", protocol, size);
delete pkt;
stats_inc_counter(ipv6_unk_prot);
stats_inc_counter(ip_n_disc);
continue;
}
CDOLOG(ll_debug, pkt->get_log_prefix().c_str(), "queing packet protocol %02x and size %d\n", protocol, payload_size);
packet *ip_p = new packet(pkt->get_recv_ts(), pkt->get_src_mac_addr(), pkt_src, pkt_dst, payload_data, payload_size, payload_header, header_size, pkt->get_log_prefix());
it->second->queue_packet(ip_p);
stats_inc_counter(ip_n_del);
delete pkt;
}
}