/
sender.h
423 lines (382 loc) · 13 KB
/
sender.h
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
/*
*
* Copyright (C) Max <max1976@mail.ru>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef __SENDER_H
#define __SENDER_H
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <errno.h>
#include <stdio.h>
#include <string>
#include <sys/socket.h>
#include <stdlib.h>
#include <errno.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <rte_config.h>
#include <rte_ether.h>
#include <rte_mbuf.h>
#include <rte_ethdev.h>
#include <rte_ip.h>
#include <netinet/ip6.h>
#include <Poco/Logger.h>
#include <Poco/Net/IPAddress.h>
#include <api.h>
#include "cfg.h"
class BSender
{
public:
struct params
{
std::string redirect_url;
std::string code;
bool send_rst_to_server;
int ttl;
int ipv6_hops;
int mtu;
params() : code("302 Moved Temporarily"), send_rst_to_server(false), ttl(250), ipv6_hops(250), mtu(1500) { }
};
BSender(const char *, struct params &prm);
virtual ~BSender();
void HTTPRedirect(int user_port, int dst_port, void *ip_from, void *ip_to, int ip_ver, uint32_t acknum, uint32_t seqnum, int f_psh, const char *redir_url, size_t p_len);
void HTTPForbidden(int user_port, int dst_port, void *ip_from, void *ip_to, int ip_ver, uint32_t acknum, uint32_t seqnum, int f_psh);
virtual void sendPacket(void *ip_from, void *ip_to, int ip_ver, int port_from, int port_to, uint32_t acknum, uint32_t seqnum, const char *dt_buf, size_t dt_len, int f_reset, int f_psh);
void SendRST(int user_port, int dst_port, void *ip_from, void *ip_to, int ip_ver, uint32_t acknum, uint32_t seqnum, int f_psh);
inline int makePacket(void *ip_from, void *ip_to, int ip_ver, int port_from, int port_to, uint32_t acknum, uint32_t seqnum, const char *dt_buf, size_t dt_len, int f_reset, int f_psh, uint8_t *buffer)
{
int pkt_len;
pkt_id++;
// IP header
struct iphdr *iph = (struct iphdr *) buffer;
struct ip6_hdr *iph6 = (struct ip6_hdr *) buffer;
// TCP header
struct tcphdr *tcph = (struct tcphdr *) (buffer + (ip_ver == 4 ? sizeof(struct iphdr) : sizeof(struct ip6_hdr)));
// Data part
uint8_t *data = (uint8_t *)tcph + sizeof(struct tcphdr);
if(dt_buf != nullptr && dt_len != 0)
rte_memcpy(data, dt_buf, dt_len);
if(_logger.getLevel() == Poco::Message::PRIO_DEBUG)
{
Poco::Net::IPAddress ipa(ip_to, ip_ver == 4 ? sizeof(in_addr) : sizeof(in6_addr));
_logger.debug("Trying to send packet to %s port %d", ipa.toString(), port_to);
}
if(ip_ver == 4)
{
// Fill the IPv4 header
iph->ihl = 5;
iph->version = 4;
iph->tos=0;
iph->tot_len = rte_cpu_to_be_16(sizeof(struct iphdr) + sizeof(struct tcphdr) + dt_len);
iph->id = rte_cpu_to_be_16(pkt_id);
iph->frag_off = 0;
iph->ttl = _parameters.ttl;
iph->protocol = IPPROTO_TCP;
iph->check = 0;
iph->saddr = ((in_addr *)ip_from)->s_addr;
iph->daddr = ((in_addr *)ip_to)->s_addr;;
pkt_len = sizeof(struct iphdr) + sizeof(struct tcphdr) + dt_len;
} else {
// IPv6 version (4 bits), Traffic class (8 bits), Flow label (20 bits)
iph6->ip6_flow = htonl ((6 << 28) | (0 << 20) | 0);
// Payload length (16 bits): TCP header + TCP data
iph6->ip6_plen = rte_cpu_to_be_16 (sizeof(struct tcphdr) + dt_len);
// Next header (8 bits): 6 for TCP
iph6->ip6_nxt = IPPROTO_TCP;
// Hop limit (8 bits): default to maximum value
iph6->ip6_hops = 250;
rte_mov16((uint8_t *)&iph6->ip6_src, (uint8_t *)ip_from);
rte_mov16((uint8_t *)&iph6->ip6_dst, (uint8_t *)ip_to);
pkt_len = (sizeof(struct ip6_hdr) + sizeof(struct tcphdr) + dt_len);
}
// TCP Header
tcph->source = port_from;
tcph->dest = port_to;
tcph->seq = acknum;
tcph->doff = 5;
tcph->syn = 0;
tcph->res1 = 0;
tcph->res2 = 0;
tcph->rst = f_reset;
tcph->psh = f_psh;
if(f_reset)
{
tcph->ack = 0;
tcph->ack_seq = 0;
tcph->fin = 0;
tcph->window = rte_cpu_to_be_16(0xEF);
} else {
tcph->ack_seq = seqnum;
tcph->ack = 1;
tcph->fin = 1;
tcph->window = rte_cpu_to_be_16(5885); // TODO get from original packet...
}
tcph->urg = 0;
tcph->check = 0;
tcph->urg_ptr = 0;
if(ip_ver == 4)
tcph->check = rte_ipv4_udptcp_cksum((const ipv4_hdr*)iph, tcph);
else
tcph->check = rte_ipv6_udptcp_cksum((const ipv6_hdr*)iph6, tcph);
return pkt_len;
}
virtual int Send(uint8_t *buffer, int size, void *addr, int addr_size) = 0;
Poco::Logger& _logger;
struct params _parameters;
uint16_t pkt_id;
};
class CSender : public BSender
{
public:
CSender(struct BSender::params &prm);
~CSender();
int Send(uint8_t *buffer, int size, void *addr, int addr_size);
private:
int s;
int s6;
};
class DSender : public BSender
{
public:
DSender(struct BSender::params &prm, uint8_t port, uint8_t *mac, uint8_t *to_mac, struct rte_mempool *mp);
~DSender();
int Send(uint8_t *buffer, int size, void *addr, int addr_size);
private:
uint8_t _port;
struct ether_hdr _eth_hdr;
struct rte_mempool *_mp;
};
class WorkerThread;
class ESender : public BSender
{
public:
struct nparams
{
struct params params;
uint8_t *mac;
uint8_t *to_mac;
int answer_duplication;
struct rte_mempool *clone_pool;
};
ESender(struct nparams ¶ms, uint8_t port, struct rte_mempool *mp, WorkerThread *wt, bool keep_l2_hdr = false);
~ESender();
void sendPacket(void *ip_from, void *ip_to, int ip_ver, int port_from, int port_to, uint32_t acknum, uint32_t seqnum, const char *dt_buf, size_t dt_len, int f_reset, int f_psh);
int Send(uint8_t *buffer, int size, void *addr, int addr_size)
{
return size;
}
inline int makeSwapPacketIPv4(dpi_pkt_infos_t *pkt_infos, uint32_t acknum, uint32_t seqnum, const char *dt_buf, size_t dt_len, uint8_t f_reset, bool f_psh, struct rte_mbuf *m, bool to_server = false)
{
int pkt_len;
const uint8_t *pkt = pkt_infos->pkt;
struct ipv4_hdr *ipv4_header = (struct ipv4_hdr *)pkt;
struct tcphdr *tcph_orig = (struct tcphdr *)(pkt + sizeof(struct ipv4_hdr));
// ethernet header
std::size_t l2_hdr_size = _keep_l2_hdr ? (pkt_infos->pkt - pkt_infos->l2_pkt) : sizeof(struct ether_hdr);
struct ether_hdr *eth_hdr = (struct ether_hdr *) rte_pktmbuf_append(m, l2_hdr_size);
if(_keep_l2_hdr)
{
rte_memcpy(eth_hdr, pkt_infos->l2_pkt, l2_hdr_size);
if(!to_server)
{
struct ether_addr addr;
ether_addr_copy(ð_hdr->d_addr, &addr);
ether_addr_copy(ð_hdr->s_addr, ð_hdr->d_addr);
ether_addr_copy(&addr, ð_hdr->s_addr);
}
} else {
*eth_hdr = _eth_hdr;
}
// IP header
struct iphdr *iph = (struct iphdr *) rte_pktmbuf_append(m, sizeof(struct iphdr));
// TCP header
struct tcphdr *tcph = (struct tcphdr *) rte_pktmbuf_append(m, sizeof(struct tcphdr));
// Data part
if(dt_buf != nullptr && dt_len != 0)
{
uint8_t *data = (uint8_t *) rte_pktmbuf_append(m, dt_len);
rte_memcpy(data, dt_buf, dt_len);
}
if(_logger.getLevel() == Poco::Message::PRIO_DEBUG)
{
Poco::Net::IPAddress ipa(&ipv4_header->src_addr, sizeof(in_addr) );
_logger.debug("Trying to send packet to %s port %d", ipa.toString(), (int) rte_be_to_cpu_16(tcph_orig->source));
}
// Fill the IPv4 header
iph->ihl = 5;
iph->version = 4;
iph->tos = 0;
iph->tot_len = rte_cpu_to_be_16(sizeof(struct iphdr) + sizeof(struct tcphdr) + dt_len);
iph->id = rte_cpu_to_be_16(pkt_id++);
iph->frag_off = 0;
iph->ttl = _parameters.ttl;
iph->protocol = IPPROTO_TCP;
iph->check = 0;
if(to_server)
{
iph->saddr = ipv4_header->src_addr;
iph->daddr = ipv4_header->dst_addr;
} else {
iph->saddr = ipv4_header->dst_addr;
iph->daddr = ipv4_header->src_addr;
}
pkt_len = sizeof(struct iphdr) + sizeof(struct tcphdr) + dt_len;
// TCP Header
tcph->source = tcph_orig->dest;
tcph->dest = tcph_orig->source;
tcph->seq = acknum;
tcph->doff = 5;
tcph->syn = 0;
tcph->res1 = 0;
tcph->res2 = 0;
tcph->rst = f_reset;
tcph->psh = f_psh;
if(f_reset)
{
if(f_reset & 2)
{
tcph->ack = 1;
tcph->ack_seq = seqnum;
} else {
tcph->ack = 0;
tcph->ack_seq = 0;
}
tcph->fin = 0;
tcph->window = 0;
} else {
tcph->window = tcph_orig->window;
tcph->ack_seq = seqnum;
tcph->ack = 1;
tcph->fin = 0;
}
tcph->urg = 0;
tcph->check = 0;
tcph->urg_ptr = 0;
tcph->check = rte_ipv4_udptcp_cksum((const ipv4_hdr*)iph, tcph);
iph->check = rte_ipv4_cksum((const ipv4_hdr*)iph);
return pkt_len;
}
inline int makeSwapPacketIPv6(dpi_pkt_infos_t *pkt_infos, uint32_t acknum, uint32_t seqnum, const char *dt_buf, size_t dt_len, uint8_t f_reset, bool f_psh, struct rte_mbuf *m, bool to_server = false)
{
int pkt_len;
const uint8_t *pkt = pkt_infos->pkt;
struct ipv6_hdr *ipv6_header = (struct ipv6_hdr *)pkt;
struct tcphdr *tcph_orig = (struct tcphdr *)(pkt + sizeof(struct ipv6_hdr));
// ethernet header
std::size_t l2_hdr_size = _keep_l2_hdr ? (pkt_infos->pkt - pkt_infos->l2_pkt) : sizeof(struct ether_hdr);
struct ether_hdr *eth_hdr = (struct ether_hdr *) rte_pktmbuf_append(m, l2_hdr_size);
if(_keep_l2_hdr)
{
rte_memcpy(eth_hdr, pkt_infos->l2_pkt, l2_hdr_size);
if(!to_server)
{
struct ether_addr addr;
ether_addr_copy(ð_hdr->d_addr, &addr);
ether_addr_copy(ð_hdr->s_addr, ð_hdr->d_addr);
ether_addr_copy(&addr, ð_hdr->s_addr);
}
} else {
*eth_hdr = _eth_hdr_ipv6;
}
// IP header
struct ip6_hdr *iph6 = (struct ip6_hdr *) rte_pktmbuf_append(m, sizeof(struct ip6_hdr));
// TCP header
struct tcphdr *tcph = (struct tcphdr *) rte_pktmbuf_append(m, sizeof(struct tcphdr));
// Data part
if(dt_buf != nullptr && dt_len != 0)
{
uint8_t *data = (uint8_t *) rte_pktmbuf_append(m, dt_len);
rte_memcpy(data, dt_buf, dt_len);
}
if(_logger.getLevel() == Poco::Message::PRIO_DEBUG)
{
Poco::Net::IPAddress ipa(&ipv6_header->src_addr, sizeof(in6_addr) );
_logger.debug("Trying to send packet to %s port %d", ipa.toString(), (int) rte_be_to_cpu_16(tcph_orig->source));
}
// IPv6 version (4 bits), Traffic class (8 bits), Flow label (20 bits)
iph6->ip6_flow = htonl ((6 << 28) | (0 << 20) | 0);
// Payload length (16 bits): TCP header + TCP data
iph6->ip6_plen = rte_cpu_to_be_16 (sizeof(struct tcphdr) + dt_len);
// Next header (8 bits): 6 for TCP
iph6->ip6_nxt = IPPROTO_TCP;
// Hop limit (8 bits): default to maximum value
iph6->ip6_hops = 250;
if(to_server)
{
rte_mov16((uint8_t *)&iph6->ip6_src, (uint8_t *)&ipv6_header->src_addr);
rte_mov16((uint8_t *)&iph6->ip6_dst, (uint8_t *)&ipv6_header->dst_addr);
} else {
rte_mov16((uint8_t *)&iph6->ip6_src, (uint8_t *)&ipv6_header->dst_addr);
rte_mov16((uint8_t *)&iph6->ip6_dst, (uint8_t *)&ipv6_header->src_addr);
}
pkt_len = (sizeof(struct ip6_hdr) + sizeof(struct tcphdr) + dt_len);
// TCP Header
tcph->source = tcph_orig->dest;
tcph->dest = tcph_orig->source;
tcph->seq = acknum;
tcph->doff = 5;
tcph->syn = 0;
tcph->res1 = 0;
tcph->res2 = 0;
tcph->rst = f_reset;
tcph->psh = f_psh;
tcph->window = tcph_orig->window;
if(f_reset)
{
if(f_reset & 2)
{
tcph->ack = 1;
tcph->ack_seq = seqnum;
} else {
tcph->ack = 0;
tcph->ack_seq = 0;
}
tcph->fin = 0;
} else {
tcph->ack_seq = seqnum;
tcph->ack = 1;
tcph->fin = 1;
}
tcph->urg = 0;
tcph->check = 0;
tcph->urg_ptr = 0;
tcph->check = rte_ipv6_udptcp_cksum((const ipv6_hdr*)iph6, tcph);
return pkt_len;
}
void sendPacketIPv4(dpi_pkt_infos_t *pkt_infos, uint32_t acknum, uint32_t seqnum, const char *dt_buf, size_t dt_len, uint8_t f_reset, bool f_psh, bool to_server = false);
void SendRSTIPv4(dpi_pkt_infos_t *pkt_infos, uint32_t acknum, uint32_t seqnum, bool use_ack = false);
void HTTPRedirectIPv4(dpi_pkt_infos_t *pkt_infos, uint32_t acknum, uint32_t seqnum, bool f_psh, const char *redir_url, size_t r_len);
void HTTPForbiddenIPv4(dpi_pkt_infos_t *pkt_infos, uint32_t acknum, uint32_t seqnum, bool f_psh);
void sendPacketIPv6(dpi_pkt_infos_t *pkt_infos, uint32_t acknum, uint32_t seqnum, const char *dt_buf, size_t dt_len, uint8_t f_reset, bool f_psh, bool to_server = false);
void HTTPRedirectIPv6(dpi_pkt_infos_t *pkt_infos, uint32_t acknum, uint32_t seqnum, bool f_psh, const char *redir_url, size_t r_len);
void SendRSTIPv6(dpi_pkt_infos_t *pkt_infos, uint32_t acknum, uint32_t seqnum, bool use_ack = false);
void HTTPForbiddenIPv6(dpi_pkt_infos_t *pkt_infos, uint32_t acknum, uint32_t seqnum, bool f_psh);
private:
uint8_t _port;
struct ether_hdr _eth_hdr;
struct ether_hdr _eth_hdr_ipv6;
struct rte_mempool *_mp;
struct rte_mempool *_clone_pool;
WorkerThread *_wt;
int _answer_duplication;
bool _keep_l2_hdr;
};
#endif