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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
|
/* Copyright (C) 2007-2023 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* 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
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* \file
*
* \author Victor Julien <victor@inliniac.net>
* \author William Metcalf <william.metcalf@gmail.com>
*
* RespondRejectLibnet11 used to send out libnet based
* TCP resets and ICMP unreachables.
*
* \todo calculate TTL base on average from stream tracking
* \todo come up with a way for users to specify icmp unreachable type
* \todo Possibly default to port unreachable for UDP traffic this seems
* to be the default in flexresp and iptables
*/
#include "suricata-common.h"
#include "suricata.h"
#include "decode.h"
#include "decode-ipv4.h"
#include "decode-tcp.h"
#include "decode-sctp.h"
#include "decode-udp.h"
#include "packet-queue.h"
#include "threads.h"
#include "threadvars.h"
#include "tm-queuehandlers.h"
#include "tm-threads.h"
#include "action-globals.h"
#include "respond-reject.h"
#include "respond-reject-libnet11.h"
#include "util-device.h"
#ifdef HAVE_LIBNET11
#ifndef HAVE_LIBNET_INIT_CONST
#define LIBNET_INIT_CAST (char *)
#else
#define LIBNET_INIT_CAST
#endif
/* Globally configured device to use for sending resets in IDS mode. */
const char *g_reject_dev = NULL;
uint16_t g_reject_dev_mtu = 0;
/** set to true in main if we're setting caps. We need it here if we're using
* reject rules as libnet 1.1 is not compatible with caps. */
extern int sc_set_caps;
#include <libnet.h>
thread_local libnet_t *t_c = NULL;
thread_local int t_inject_mode = -1;
typedef struct Libnet11Packet_ {
uint32_t ack, seq;
uint16_t window, dsize;
uint8_t ttl;
uint16_t id;
uint32_t flow;
uint8_t class;
struct libnet_in6_addr src6, dst6;
uint32_t src4, dst4;
uint16_t sp, dp;
uint16_t len;
uint8_t *smac, *dmac;
} Libnet11Packet;
static inline libnet_t *GetCtx(const Packet *p, int injection_type)
{
/* fast path: use cache ctx */
if (t_c)
return t_c;
/* slow path: setup a new ctx */
bool store_ctx = false;
const char *devname = NULL;
extern uint8_t host_mode;
if (IS_SURI_HOST_MODE_SNIFFER_ONLY(host_mode)) {
if (g_reject_dev != NULL) {
if (p->datalink == LINKTYPE_ETHERNET)
injection_type = t_inject_mode = LIBNET_LINK;
devname = g_reject_dev;
store_ctx = true;
} else {
devname = p->livedev ? p->livedev->dev : NULL;
}
}
char ebuf[LIBNET_ERRBUF_SIZE];
libnet_t *c = libnet_init(injection_type, LIBNET_INIT_CAST devname, ebuf);
if (c == NULL) {
SCLogError("libnet_init failed: %s", ebuf);
}
if (store_ctx) {
t_c = c;
}
return c;
}
static inline void ClearCtx(libnet_t *c)
{
if (t_c == c)
libnet_clear_packet(c);
else
libnet_destroy(c);
}
void FreeCachedCtx(void)
{
if (t_c) {
libnet_destroy(t_c);
t_c = NULL;
}
}
static inline void SetupTCP(Packet *p, Libnet11Packet *lpacket, enum RejectDirection dir)
{
switch (dir) {
case REJECT_DIR_SRC:
SCLogDebug("sending a tcp reset to src");
/* We follow http://tools.ietf.org/html/rfc793#section-3.4 :
* If packet has no ACK, the seq number is 0 and the ACK is built
* the normal way. If packet has a ACK, the seq of the RST packet
* is equal to the ACK of incoming packet and the ACK is build
* using packet sequence number and size of the data. */
if (TCP_GET_ACK(p) == 0) {
lpacket->seq = 0;
lpacket->ack = TCP_GET_SEQ(p) + lpacket->dsize + 1;
} else {
lpacket->seq = TCP_GET_ACK(p);
lpacket->ack = TCP_GET_SEQ(p) + lpacket->dsize;
}
lpacket->sp = TCP_GET_DST_PORT(p);
lpacket->dp = TCP_GET_SRC_PORT(p);
break;
case REJECT_DIR_DST:
default:
SCLogDebug("sending a tcp reset to dst");
lpacket->seq = TCP_GET_SEQ(p);
lpacket->ack = TCP_GET_ACK(p);
lpacket->sp = TCP_GET_SRC_PORT(p);
lpacket->dp = TCP_GET_DST_PORT(p);
break;
}
lpacket->window = TCP_GET_WINDOW(p);
//lpacket.seq += lpacket.dsize;
}
static inline int BuildTCP(libnet_t *c, Libnet11Packet *lpacket)
{
/* build the package */
if ((libnet_build_tcp(
lpacket->sp, /* source port */
lpacket->dp, /* dst port */
lpacket->seq, /* seq number */
lpacket->ack, /* ack number */
TH_RST|TH_ACK, /* flags */
lpacket->window, /* window size */
0, /* checksum */
0, /* urgent flag */
LIBNET_TCP_H, /* header length */
NULL, /* payload */
0, /* payload length */
c, /* libnet context */
0)) < 0) /* libnet ptag */
{
SCLogError("libnet_build_tcp %s", libnet_geterror(c));
return -1;
}
return 0;
}
static inline int BuildIPv4(libnet_t *c, Libnet11Packet *lpacket, const uint8_t proto)
{
if ((libnet_build_ipv4(
lpacket->len, /* entire packet length */
0, /* tos */
lpacket->id, /* ID */
0, /* fragmentation flags and offset */
lpacket->ttl, /* TTL */
proto, /* protocol */
0, /* checksum */
lpacket->src4, /* source address */
lpacket->dst4, /* destination address */
NULL, /* pointer to packet data (or NULL) */
0, /* payload length */
c, /* libnet context pointer */
0)) < 0) /* packet id */
{
SCLogError("libnet_build_ipv4 %s", libnet_geterror(c));
return -1;
}
return 0;
}
static inline int BuildIPv6(libnet_t *c, Libnet11Packet *lpacket, const uint8_t proto)
{
if ((libnet_build_ipv6(
lpacket->class, /* traffic class */
lpacket->flow, /* Flow label */
lpacket->len, /* payload length */
proto, /* next header */
lpacket->ttl, /* TTL */
lpacket->src6, /* source address */
lpacket->dst6, /* destination address */
NULL, /* pointer to packet data (or NULL) */
0, /* payload length */
c, /* libnet context pointer */
0)) < 0) /* packet id */
{
SCLogError("libnet_build_ipv6 %s", libnet_geterror(c));
return -1;
}
return 0;
}
static inline void SetupEthernet(Packet *p, Libnet11Packet *lpacket, enum RejectDirection dir)
{
switch (dir) {
case REJECT_DIR_SRC:
lpacket->smac = p->ethh->eth_dst;
lpacket->dmac = p->ethh->eth_src;
break;
case REJECT_DIR_DST:
default:
lpacket->smac = p->ethh->eth_src;
lpacket->dmac = p->ethh->eth_dst;
break;
}
}
static inline int BuildEthernet(libnet_t *c, Libnet11Packet *lpacket, uint16_t proto)
{
if ((libnet_build_ethernet(lpacket->dmac,lpacket->smac, proto , NULL, 0, c, 0)) < 0) {
SCLogError("libnet_build_ethernet %s", libnet_geterror(c));
return -1;
}
return 0;
}
static inline int BuildEthernetVLAN(libnet_t *c, Libnet11Packet *lpacket, uint16_t proto, uint16_t vlan_id)
{
if (libnet_build_802_1q(lpacket->dmac, lpacket->smac, ETHERTYPE_VLAN, 0, 0, vlan_id, proto,
NULL, /* payload */
0, /* payload size */
c, /* libnet handle */
0) < 0) {
SCLogError("libnet_build_802_1q %s", libnet_geterror(c));
return -1;
}
return 0;
}
int RejectSendLibnet11IPv4TCP(ThreadVars *tv, Packet *p, void *data, enum RejectDirection dir)
{
Libnet11Packet lpacket;
int result;
/* fill in struct defaults */
lpacket.ttl = 0;
lpacket.id = 0;
lpacket.flow = 0;
lpacket.class = 0;
if (p->tcph == NULL)
return 1;
libnet_t *c = GetCtx(p, LIBNET_RAW4);
if (c == NULL)
return 1;
lpacket.len = LIBNET_IPV4_H + LIBNET_TCP_H;
lpacket.dsize = p->payload_len;
switch (dir) {
case REJECT_DIR_SRC:
lpacket.src4 = GET_IPV4_DST_ADDR_U32(p);
lpacket.dst4 = GET_IPV4_SRC_ADDR_U32(p);
break;
case REJECT_DIR_DST:
default:
lpacket.src4 = GET_IPV4_SRC_ADDR_U32(p);
lpacket.dst4 = GET_IPV4_DST_ADDR_U32(p);
break;
}
/* TODO come up with ttl calc function */
lpacket.ttl = 64;
SetupTCP(p, &lpacket, dir);
if (BuildTCP(c, &lpacket) < 0)
goto cleanup;
if (BuildIPv4(c, &lpacket, IPPROTO_TCP) < 0)
goto cleanup;
if (t_inject_mode == LIBNET_LINK) {
SetupEthernet(p, &lpacket, dir);
if (p->vlan_idx == 1) {
if (BuildEthernetVLAN(c, &lpacket, ETHERNET_TYPE_IP, p->vlan_id[0]) < 0)
goto cleanup;
} else {
if (BuildEthernet(c, &lpacket, ETHERNET_TYPE_IP) < 0)
goto cleanup;
}
}
result = libnet_write(c);
if (result == -1) {
SCLogError("libnet_write failed: %s", libnet_geterror(c));
goto cleanup;
}
cleanup:
ClearCtx(c);
return 0;
}
int RejectSendLibnet11IPv4ICMP(ThreadVars *tv, Packet *p, void *data, enum RejectDirection dir)
{
Libnet11Packet lpacket;
int result;
/* fill in struct defaults */
lpacket.ttl = 0;
lpacket.id = 0;
lpacket.flow = 0;
lpacket.class = 0;
const uint16_t iplen = IPV4_GET_IPLEN(p);
if (g_reject_dev_mtu >= ETHERNET_HEADER_LEN + LIBNET_IPV4_H + 8) {
lpacket.len = MIN(g_reject_dev_mtu - ETHERNET_HEADER_LEN, (LIBNET_IPV4_H + iplen));
} else {
lpacket.len = LIBNET_IPV4_H + MIN(8,iplen); // 8 bytes is the minimum we have to attach
}
lpacket.dsize = lpacket.len - (LIBNET_IPV4_H + LIBNET_ICMPV4_H);
libnet_t *c = GetCtx(p, LIBNET_RAW4);
if (c == NULL)
return 1;
switch (dir) {
case REJECT_DIR_SRC:
lpacket.src4 = GET_IPV4_DST_ADDR_U32(p);
lpacket.dst4 = GET_IPV4_SRC_ADDR_U32(p);
break;
case REJECT_DIR_DST:
default:
lpacket.src4 = GET_IPV4_SRC_ADDR_U32(p);
lpacket.dst4 = GET_IPV4_DST_ADDR_U32(p);
break;
}
/* TODO come up with ttl calc function */
lpacket.ttl = 64;
/* build the package */
if ((libnet_build_icmpv4_unreach(
ICMP_DEST_UNREACH, /* type */
ICMP_HOST_ANO, /* code */
0, /* checksum */
(uint8_t *)p->ip4h, /* payload */
lpacket.dsize, /* payload length */
c, /* libnet context */
0)) < 0) /* libnet ptag */
{
SCLogError("libnet_build_icmpv4_unreach %s", libnet_geterror(c));
goto cleanup;
}
if (BuildIPv4(c, &lpacket, IPPROTO_ICMP) < 0)
goto cleanup;
if (t_inject_mode == LIBNET_LINK) {
SetupEthernet(p, &lpacket, dir);
if (p->vlan_idx == 1) {
if (BuildEthernetVLAN(c, &lpacket, ETHERNET_TYPE_IP, p->vlan_id[0]) < 0)
goto cleanup;
} else {
if (BuildEthernet(c, &lpacket, ETHERNET_TYPE_IP) < 0)
goto cleanup;
}
}
result = libnet_write(c);
if (result == -1) {
SCLogError("libnet_write_raw_ipv4 failed: %s", libnet_geterror(c));
goto cleanup;
}
cleanup:
ClearCtx(c);
return 0;
}
int RejectSendLibnet11IPv6TCP(ThreadVars *tv, Packet *p, void *data, enum RejectDirection dir)
{
Libnet11Packet lpacket;
int result;
/* fill in struct defaults */
lpacket.ttl = 0;
lpacket.id = 0;
lpacket.flow = 0;
lpacket.class = 0;
if (p->tcph == NULL)
return 1;
libnet_t *c = GetCtx(p, LIBNET_RAW6);
if (c == NULL)
return 1;
lpacket.len = LIBNET_TCP_H;
lpacket.dsize = p->payload_len;
switch (dir) {
case REJECT_DIR_SRC:
memcpy(lpacket.src6.libnet_s6_addr, GET_IPV6_DST_ADDR(p), 16);
memcpy(lpacket.dst6.libnet_s6_addr, GET_IPV6_SRC_ADDR(p), 16);
break;
case REJECT_DIR_DST:
default:
memcpy(lpacket.src6.libnet_s6_addr, GET_IPV6_SRC_ADDR(p), 16);
memcpy(lpacket.dst6.libnet_s6_addr, GET_IPV6_DST_ADDR(p), 16);
break;
}
/* TODO come up with ttl calc function */
lpacket.ttl = 64;
SetupTCP(p, &lpacket, dir);
BuildTCP(c, &lpacket);
if (BuildIPv6(c, &lpacket, IPPROTO_TCP) < 0)
goto cleanup;
if (t_inject_mode == LIBNET_LINK) {
SetupEthernet(p, &lpacket, dir);
if (p->vlan_idx == 1) {
if (BuildEthernetVLAN(c, &lpacket, ETHERNET_TYPE_IPV6, p->vlan_id[0]) < 0)
goto cleanup;
} else {
if (BuildEthernet(c, &lpacket, ETHERNET_TYPE_IPV6) < 0)
goto cleanup;
}
}
result = libnet_write(c);
if (result == -1) {
SCLogError("libnet_write failed: %s", libnet_geterror(c));
goto cleanup;
}
cleanup:
ClearCtx(c);
return 0;
}
#ifdef HAVE_LIBNET_ICMPV6_UNREACH
int RejectSendLibnet11IPv6ICMP(ThreadVars *tv, Packet *p, void *data, enum RejectDirection dir)
{
Libnet11Packet lpacket;
int result;
/* fill in struct defaults */
lpacket.ttl = 0;
lpacket.id = 0;
lpacket.flow = 0;
lpacket.class = 0;
const uint16_t iplen = IPV6_GET_PLEN(p);
if (g_reject_dev_mtu >= ETHERNET_HEADER_LEN + IPV6_HEADER_LEN + 8) {
lpacket.len = IPV6_HEADER_LEN + MIN(g_reject_dev_mtu - ETHERNET_HEADER_LEN, iplen);
} else {
lpacket.len = IPV6_HEADER_LEN + MIN(8, iplen);
}
lpacket.dsize = lpacket.len - LIBNET_ICMPV6_H;
libnet_t *c = GetCtx(p, LIBNET_RAW6);
if (c == NULL)
return 1;
switch (dir) {
case REJECT_DIR_SRC:
memcpy(lpacket.src6.libnet_s6_addr, GET_IPV6_DST_ADDR(p), 16);
memcpy(lpacket.dst6.libnet_s6_addr, GET_IPV6_SRC_ADDR(p), 16);
break;
case REJECT_DIR_DST:
default:
memcpy(lpacket.src6.libnet_s6_addr, GET_IPV6_SRC_ADDR(p), 16);
memcpy(lpacket.dst6.libnet_s6_addr, GET_IPV6_DST_ADDR(p), 16);
break;
}
/* TODO come up with ttl calc function */
lpacket.ttl = 64;
/* build the package */
if ((libnet_build_icmpv6_unreach(
ICMP6_DST_UNREACH, /* type */
ICMP6_DST_UNREACH_ADMIN, /* code */
0, /* checksum */
(uint8_t *)p->ip6h, /* payload */
lpacket.dsize, /* payload length */
c, /* libnet context */
0)) < 0) /* libnet ptag */
{
SCLogError("libnet_build_icmpv6_unreach %s", libnet_geterror(c));
goto cleanup;
}
if (BuildIPv6(c, &lpacket, IPPROTO_ICMPV6) < 0)
goto cleanup;
if (t_inject_mode == LIBNET_LINK) {
SetupEthernet(p, &lpacket, dir);
if (p->vlan_idx == 1) {
if (BuildEthernetVLAN(c, &lpacket, ETHERNET_TYPE_IPV6, p->vlan_id[0]) < 0)
goto cleanup;
} else {
if (BuildEthernet(c, &lpacket, ETHERNET_TYPE_IPV6) < 0)
goto cleanup;
}
}
result = libnet_write(c);
if (result == -1) {
SCLogError("libnet_write_raw_ipv6 failed: %s", libnet_geterror(c));
goto cleanup;
}
cleanup:
ClearCtx(c);
return 0;
}
#else /* HAVE_LIBNET_ICMPV6_UNREACH */
int RejectSendLibnet11IPv6ICMP(ThreadVars *tv, Packet *p, void *data, enum RejectDirection dir)
{
SCLogError("Libnet ICMPv6 based rejects are disabled."
"Usually this means that you don't have a patched libnet installed,"
" or configure couldn't find it.");
return 0;
}
#endif /* HAVE_LIBNET_ICMPV6_UNREACH */
#else
int RejectSendLibnet11IPv4TCP(ThreadVars *tv, Packet *p, void *data, enum RejectDirection dir)
{
SCLogError("Libnet based rejects are disabled."
"Usually this means that you don't have libnet installed,"
" or configure couldn't find it.");
return 0;
}
int RejectSendLibnet11IPv4ICMP(ThreadVars *tv, Packet *p, void *data, enum RejectDirection dir)
{
SCLogError("Libnet based rejects are disabled."
"Usually this means that you don't have libnet installed,"
" or configure couldn't find it.");
return 0;
}
int RejectSendLibnet11IPv6TCP(ThreadVars *tv, Packet *p, void *data, enum RejectDirection dir)
{
SCLogError("Libnet based rejects are disabled."
"Usually this means that you don't have libnet installed,"
" or configure couldn't find it.");
return 0;
}
int RejectSendLibnet11IPv6ICMP(ThreadVars *tv, Packet *p, void *data, enum RejectDirection dir)
{
SCLogError("Libnet based rejects are disabled."
"Usually this means that you don't have libnet installed,"
" or configure couldn't find it.");
return 0;
}
void FreeCachedCtx(void)
{
SCLogDebug("no libhnet support");
}
#endif /* HAVE_LIBNET11 */
|