forked from mfontanini/libtins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp.cpp
425 lines (374 loc) · 13 KB
/
tcp.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
* Copyright (c) 2017, Matias Fontanini
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <cstring>
#include <tins/tcp.h>
#include <tins/ip.h>
#include <tins/ipv6.h>
#include <tins/constants.h>
#include <tins/rawpdu.h>
#include <tins/exceptions.h>
#include <tins/memory_helpers.h>
#include <tins/utils/checksum_utils.h>
using std::vector;
using std::pair;
using Tins::Memory::InputMemoryStream;
using Tins::Memory::OutputMemoryStream;
namespace Tins {
const uint16_t TCP::DEFAULT_WINDOW = 32678;
PDU::metadata TCP::extract_metadata(const uint8_t *buffer, uint32_t total_sz) {
if (TINS_UNLIKELY(total_sz < sizeof(tcp_header))) {
throw malformed_packet();
}
const tcp_header* header = (const tcp_header*)buffer;
return metadata(header->doff * 4, pdu_flag, PDU::UNKNOWN);
}
TCP::TCP(uint16_t dport, uint16_t sport)
: header_() {
this->dport(dport);
this->sport(sport);
data_offset(sizeof(tcp_header) / sizeof(uint32_t));
window(DEFAULT_WINDOW);
}
TCP::TCP(const uint8_t* buffer, uint32_t total_sz) {
InputMemoryStream stream(buffer, total_sz);
stream.read(header_);
// Check that we have at least the amount of bytes we need and not less
if (TINS_UNLIKELY(data_offset() * sizeof(uint32_t) > total_sz ||
data_offset() * sizeof(uint32_t) < sizeof(tcp_header))) {
throw malformed_packet();
}
const uint8_t* header_end = buffer + (data_offset() * sizeof(uint32_t));
if (stream.pointer() < header_end) {
// Estimate about 4 bytes per option and reserver that so we avoid doing
// multiple reallocations on the vector
options_.reserve((header_end - stream.pointer()) / sizeof(uint32_t));
}
while (stream.pointer() < header_end) {
const OptionTypes option_type = (OptionTypes)stream.read<uint8_t>();
if (option_type == EOL) {
stream.skip(header_end - stream.pointer());
break;
}
else if (option_type == NOP) {
#if TINS_IS_CXX11
add_option(option_type, 0);
#else
add_option(option(option_type, 0));
#endif // TINS_IS_CXX11
}
else {
// Extract the length
uint32_t len = stream.read<uint8_t>();
const uint8_t* data_start = stream.pointer();
// We need to subtract the option type and length from the size
if (TINS_UNLIKELY(len < sizeof(uint8_t) << 1)) {
throw malformed_packet();
}
len -= (sizeof(uint8_t) << 1);
// Make sure we have enough bytes for the advertised option payload length
if (TINS_UNLIKELY(data_start + len > header_end)) {
throw malformed_packet();
}
// If we're using C++11, use the variadic template overload
#if TINS_IS_CXX11
add_option(option_type, data_start, data_start + len);
#else
add_option(option(option_type, data_start, data_start + len));
#endif // TINS_IS_CXX11
// Skip the option's payload
stream.skip(len);
}
}
// If we still have any bytes left
if (stream) {
inner_pdu(new RawPDU(stream.pointer(), stream.size()));
}
}
void TCP::dport(uint16_t new_dport) {
header_.dport = Endian::host_to_be(new_dport);
}
void TCP::sport(uint16_t new_sport) {
header_.sport = Endian::host_to_be(new_sport);
}
void TCP::seq(uint32_t new_seq) {
header_.seq = Endian::host_to_be(new_seq);
}
void TCP::ack_seq(uint32_t new_ack_seq) {
header_.ack_seq = Endian::host_to_be(new_ack_seq);
}
void TCP::window(uint16_t new_window) {
header_.window = Endian::host_to_be(new_window);
}
void TCP::checksum(uint16_t new_check) {
header_.check = Endian::host_to_be(new_check);
}
void TCP::urg_ptr(uint16_t new_urg_ptr) {
header_.urg_ptr = Endian::host_to_be(new_urg_ptr);
}
void TCP::data_offset(small_uint<4> new_doff) {
this->header_.doff = new_doff;
}
void TCP::mss(uint16_t value) {
value = Endian::host_to_be(value);
add_option(option(MSS, 2, (uint8_t*)&value));
}
uint16_t TCP::mss() const {
return generic_search<uint16_t>(MSS);
}
void TCP::winscale(uint8_t value) {
add_option(option(WSCALE, 1, &value));
}
uint8_t TCP::winscale() const {
return generic_search<uint8_t>(WSCALE);
}
void TCP::sack_permitted() {
add_option(option(SACK_OK, 0));
}
bool TCP::has_sack_permitted() const {
return search_option(SACK_OK) != NULL;
}
void TCP::sack(const sack_type& edges) {
vector<uint8_t> value(edges.size() * sizeof(uint32_t));
if (edges.size()) {
OutputMemoryStream stream(value);
for (sack_type::const_iterator it = edges.begin(); it != edges.end(); ++it) {
stream.write_be(*it);
}
}
add_option(option(SACK, (uint8_t)value.size(), &value[0]));
}
TCP::sack_type TCP::sack() const {
const option* opt = search_option(SACK);
if (!opt) {
throw option_not_found();
}
return opt->to<sack_type>();
}
void TCP::timestamp(uint32_t value, uint32_t reply) {
uint64_t buffer = (uint64_t(value) << 32) | reply;
buffer = Endian::host_to_be(buffer);
add_option(option(TSOPT, 8, (uint8_t*)&buffer));
}
pair<uint32_t, uint32_t> TCP::timestamp() const {
const option* opt = search_option(TSOPT);
if (!opt) {
throw option_not_found();
}
return opt->to<pair<uint32_t, uint32_t> >();
}
void TCP::altchecksum(AltChecksums value) {
uint8_t int_value = value;
add_option(option(ALTCHK, 1, &int_value));
}
TCP::AltChecksums TCP::altchecksum() const {
return static_cast<AltChecksums>(generic_search<uint8_t>(ALTCHK));
}
small_uint<1> TCP::get_flag(Flags tcp_flag) const {
switch (tcp_flag) {
case FIN:
return header_.flags.fin;
break;
case SYN:
return header_.flags.syn;
break;
case RST:
return header_.flags.rst;
break;
case PSH:
return header_.flags.psh;
break;
case ACK:
return header_.flags.ack;
break;
case URG:
return header_.flags.urg;
break;
case ECE:
return header_.flags.ece;
break;
case CWR:
return header_.flags.cwr;
break;
default:
return 0;
break;
};
}
small_uint<12> TCP::flags() const {
return (header_.res1 << 8) | header_.flags_8;
}
bool TCP::has_flags(small_uint<12> check_flags) const {
return (flags() & check_flags) == check_flags;
}
void TCP::set_flag(Flags tcp_flag, small_uint<1> value) {
switch (tcp_flag) {
case FIN:
header_.flags.fin = value;
break;
case SYN:
header_.flags.syn = value;
break;
case RST:
header_.flags.rst = value;
break;
case PSH:
header_.flags.psh = value;
break;
case ACK:
header_.flags.ack = value;
break;
case URG:
header_.flags.urg = value;
break;
case ECE:
header_.flags.ece = value;
break;
case CWR:
header_.flags.cwr = value;
break;
};
}
void TCP::flags(small_uint<12> value) {
header_.res1 = (value >> 8) & 0x0f;
header_.flags_8 = value & 0xff;
}
void TCP::add_option(const option& opt) {
options_.push_back(opt);
}
uint32_t TCP::header_size() const {
return sizeof(header_) + pad_options_size(calculate_options_size());
}
void TCP::write_serialization(uint8_t* buffer, uint32_t total_sz) {
OutputMemoryStream stream(buffer, total_sz);
const uint32_t options_size = calculate_options_size();
const uint32_t total_options_size = pad_options_size(options_size);
// Set checksum to 0, we'll calculate it at the end
checksum(0);
header_.doff = (sizeof(tcp_header) + total_options_size) / sizeof(uint32_t);
stream.write(header_);
for (options_type::const_iterator it = options_.begin(); it != options_.end(); ++it) {
write_option(*it, stream);
}
if (options_size < total_options_size) {
const uint16_t padding = total_options_size - options_size;
stream.fill(padding, 0);
}
uint32_t check = 0;
const PDU* parent = parent_pdu();
if (const Tins::IP* ip_packet = tins_cast<const Tins::IP*>(parent)) {
check = Utils::pseudoheader_checksum(
ip_packet->src_addr(),
ip_packet->dst_addr(),
size(),
Constants::IP::PROTO_TCP
) + Utils::sum_range(buffer, buffer + total_sz);
}
else if (const Tins::IPv6* ipv6_packet = tins_cast<const Tins::IPv6*>(parent)) {
check = Utils::pseudoheader_checksum(
ipv6_packet->src_addr(),
ipv6_packet->dst_addr(),
size(),
Constants::IP::PROTO_TCP
) + Utils::sum_range(buffer, buffer + total_sz);
}
else {
return;
}
// Convert this 32-bit value into a 16-bit value
while (check >> 16) {
check = (check & 0xffff) + (check >> 16);
}
checksum(Endian::host_to_be<uint16_t>(~check));
((tcp_header*)buffer)->check = header_.check;
}
const TCP::option* TCP::search_option(OptionTypes type) const {
// Search for the iterator. If we found something, return it, otherwise return nullptr.
options_type::const_iterator iter = search_option_iterator(type);
return (iter != options_.end()) ? &*iter : 0;
}
TCP::options_type::const_iterator TCP::search_option_iterator(OptionTypes type) const {
return Internals::find_option_const<option>(options_, type);
}
TCP::options_type::iterator TCP::search_option_iterator(OptionTypes type) {
return Internals::find_option<option>(options_, type);
}
/* options */
void TCP::write_option(const option& opt, OutputMemoryStream& stream) {
stream.write<uint8_t>(opt.option());
// Only do this for non EOL nor NOP options
if (opt.option() > 1) {
uint8_t length = opt.length_field();
// Only add the identifier and size field sizes if the length
// field hasn't been spoofed.
if (opt.length_field() == opt.data_size()) {
length += (sizeof(uint8_t) << 1);
}
stream.write(length);
stream.write(opt.data_ptr(), opt.data_size());
}
}
uint32_t TCP::calculate_options_size() const {
uint32_t options_size = 0;
for (options_type::const_iterator iter = options_.begin(); iter != options_.end(); ++iter) {
const option& opt = *iter;
options_size += sizeof(uint8_t);
// SACK_OK contains length but not data
if (opt.data_size() || opt.option() == SACK_OK) {
options_size += sizeof(uint8_t);
options_size += static_cast<uint16_t>(opt.data_size());
}
}
return options_size;
}
uint32_t TCP::pad_options_size(uint32_t size) const {
uint8_t padding = size & 3;
return padding ? (size - padding + 4) : size;
}
bool TCP::remove_option(OptionTypes type) {
options_type::iterator iter = search_option_iterator(type);
if (iter == options_.end()) {
return false;
}
options_.erase(iter);
return true;
}
bool TCP::matches_response(const uint8_t* ptr, uint32_t total_sz) const {
if (total_sz < sizeof(header_)) {
return false;
}
const tcp_header* tcp_ptr = (const tcp_header*)ptr;
if (tcp_ptr->sport == header_.dport && tcp_ptr->dport == header_.sport) {
const uint32_t data_offset = tcp_ptr->doff * sizeof(uint32_t);
uint32_t sz = (total_sz < data_offset) ? total_sz : data_offset;
return inner_pdu() ? inner_pdu()->matches_response(ptr + sz, total_sz - sz) : true;
}
else
return false;
}
} // Tins