53 #ifndef HAVE_LIBNET_INIT_CONST
54 #define LIBNET_INIT_CAST (char *)
56 #define LIBNET_INIT_CAST
60 const char *g_reject_dev = NULL;
61 uint16_t g_reject_dev_mtu = 0;
69 thread_local libnet_t *t_c = NULL;
70 thread_local
int t_inject_mode = -1;
72 typedef struct Libnet11Packet_ {
74 uint16_t window, dsize;
79 struct libnet_in6_addr src6, dst6;
86 static inline libnet_t *GetCtx(
const Packet *p,
int injection_type)
93 bool store_ctx =
false;
94 const char *devname = NULL;
97 if (g_reject_dev != NULL) {
99 injection_type = t_inject_mode = LIBNET_LINK;
100 devname = g_reject_dev;
107 char ebuf[LIBNET_ERRBUF_SIZE];
108 libnet_t *c = libnet_init(injection_type, LIBNET_INIT_CAST devname, ebuf);
118 static inline void ClearCtx(libnet_t *c)
121 libnet_clear_packet(c);
146 lpacket->ack =
TCP_GET_SEQ(p) + lpacket->dsize + 1;
169 static inline int BuildTCP(libnet_t *c, Libnet11Packet *lpacket)
172 if ((libnet_build_tcp(
187 SCLogError(
"libnet_build_tcp %s", libnet_geterror(c));
193 static inline int BuildIPv4(libnet_t *c, Libnet11Packet *lpacket,
const uint8_t
proto)
195 if ((libnet_build_ipv4(
210 SCLogError(
"libnet_build_ipv4 %s", libnet_geterror(c));
216 static inline int BuildIPv6(libnet_t *c, Libnet11Packet *lpacket,
const uint8_t
proto)
218 if ((libnet_build_ipv6(
231 SCLogError(
"libnet_build_ipv6 %s", libnet_geterror(c));
241 lpacket->smac = p->
ethh->eth_dst;
242 lpacket->dmac = p->
ethh->eth_src;
246 lpacket->smac = p->
ethh->eth_src;
247 lpacket->dmac = p->
ethh->eth_dst;
252 static inline int BuildEthernet(libnet_t *c, Libnet11Packet *lpacket, uint16_t
proto)
254 if ((libnet_build_ethernet(lpacket->dmac,lpacket->smac,
proto , NULL, 0, c, 0)) < 0) {
255 SCLogError(
"libnet_build_ethernet %s", libnet_geterror(c));
261 static inline int BuildEthernetVLAN(libnet_t *c, Libnet11Packet *lpacket, uint16_t
proto, uint16_t vlan_id)
263 if (libnet_build_802_1q(lpacket->dmac, lpacket->smac, ETHERTYPE_VLAN, 0, 0, vlan_id,
proto,
268 SCLogError(
"libnet_build_802_1q %s", libnet_geterror(c));
276 Libnet11Packet lpacket;
288 libnet_t *c = GetCtx(p, LIBNET_RAW4);
292 lpacket.len = LIBNET_IPV4_H + LIBNET_TCP_H;
309 SetupTCP(p, &lpacket, dir);
311 if (BuildTCP(c, &lpacket) < 0)
314 if (BuildIPv4(c, &lpacket, IPPROTO_TCP) < 0)
317 if (t_inject_mode == LIBNET_LINK) {
318 SetupEthernet(p, &lpacket, dir);
329 result = libnet_write(c);
331 SCLogError(
"libnet_write failed: %s", libnet_geterror(c));
342 Libnet11Packet lpacket;
354 lpacket.len = LIBNET_IPV4_H +
MIN(8,iplen);
356 lpacket.dsize = lpacket.len - (LIBNET_IPV4_H + LIBNET_ICMPV4_H);
358 libnet_t *c = GetCtx(p, LIBNET_RAW4);
378 if ((libnet_build_icmpv4_unreach(
387 SCLogError(
"libnet_build_icmpv4_unreach %s", libnet_geterror(c));
391 if (BuildIPv4(c, &lpacket, IPPROTO_ICMP) < 0)
394 if (t_inject_mode == LIBNET_LINK) {
395 SetupEthernet(p, &lpacket, dir);
406 result = libnet_write(c);
408 SCLogError(
"libnet_write_raw_ipv4 failed: %s", libnet_geterror(c));
419 Libnet11Packet lpacket;
431 libnet_t *c = GetCtx(p, LIBNET_RAW6);
435 lpacket.len = LIBNET_TCP_H;
452 SetupTCP(p, &lpacket, dir);
454 BuildTCP(c, &lpacket);
456 if (BuildIPv6(c, &lpacket, IPPROTO_TCP) < 0)
459 if (t_inject_mode == LIBNET_LINK) {
460 SetupEthernet(p, &lpacket, dir);
470 result = libnet_write(c);
472 SCLogError(
"libnet_write failed: %s", libnet_geterror(c));
481 #ifdef HAVE_LIBNET_ICMPV6_UNREACH
484 Libnet11Packet lpacket;
498 lpacket.dsize = lpacket.len - LIBNET_ICMPV6_H;
500 libnet_t *c = GetCtx(p, LIBNET_RAW6);
520 if ((libnet_build_icmpv6_unreach(
529 SCLogError(
"libnet_build_icmpv6_unreach %s", libnet_geterror(c));
533 if (BuildIPv6(c, &lpacket, IPPROTO_ICMPV6) < 0)
536 if (t_inject_mode == LIBNET_LINK) {
537 SetupEthernet(p, &lpacket, dir);
547 result = libnet_write(c);
549 SCLogError(
"libnet_write_raw_ipv6 failed: %s", libnet_geterror(c));
562 SCLogError(
"Libnet ICMPv6 based rejects are disabled."
563 "Usually this means that you don't have a patched libnet installed,"
564 " or configure couldn't find it.");
574 SCLogError(
"Libnet based rejects are disabled."
575 "Usually this means that you don't have libnet installed,"
576 " or configure couldn't find it.");
582 SCLogError(
"Libnet based rejects are disabled."
583 "Usually this means that you don't have libnet installed,"
584 " or configure couldn't find it.");
590 SCLogError(
"Libnet based rejects are disabled."
591 "Usually this means that you don't have libnet installed,"
592 " or configure couldn't find it.");
598 SCLogError(
"Libnet based rejects are disabled."
599 "Usually this means that you don't have libnet installed,"
600 " or configure couldn't find it.");