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[2] = { NULL, 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;
83 const uint8_t *smac, *dmac;
93 bool store_ctx =
false;
94 const char *devname = NULL;
95 if (g_reject_dev != NULL) {
97 injection_type = t_inject_mode = LIBNET_LINK;
98 devname = g_reject_dev;
100 SCLogDebug(
"dedicated dev: devname %s", devname);
103 devname = dev ? dev->
dev : NULL;
108 devname = dev ? dev->
dev : NULL;
115 char ebuf[LIBNET_ERRBUF_SIZE];
116 libnet_t *c = libnet_init(injection_type, LIBNET_INIT_CAST devname, ebuf);
129 libnet_clear_packet(c);
137 libnet_destroy(t_c[0]);
141 libnet_destroy(t_c[1]);
148 const TCPHdr *tcph = PacketGetTCP(p);
182 static inline int BuildTCP(libnet_t *c, Libnet11Packet *lpacket)
185 if ((libnet_build_tcp(
200 SCLogError(
"libnet_build_tcp %s", libnet_geterror(c));
206 static inline int BuildIPv4(libnet_t *c, Libnet11Packet *lpacket,
const uint8_t
proto)
208 if ((libnet_build_ipv4(
223 SCLogError(
"libnet_build_ipv4 %s", libnet_geterror(c));
229 static inline int BuildIPv6(libnet_t *c, Libnet11Packet *lpacket,
const uint8_t
proto)
231 if ((libnet_build_ipv6(
244 SCLogError(
"libnet_build_ipv6 %s", libnet_geterror(c));
252 const EthernetHdr *ethh = PacketGetEthernet(p);
255 lpacket->smac = ethh->eth_dst;
256 lpacket->dmac = ethh->eth_src;
260 lpacket->smac = ethh->eth_src;
261 lpacket->dmac = ethh->eth_dst;
266 static inline int BuildEthernet(libnet_t *c, Libnet11Packet *lpacket, uint16_t
proto)
268 if ((libnet_build_ethernet(lpacket->dmac,lpacket->smac,
proto , NULL, 0, c, 0)) < 0) {
269 SCLogError(
"libnet_build_ethernet %s", libnet_geterror(c));
275 static inline int BuildEthernetVLAN(libnet_t *c, Libnet11Packet *lpacket, uint16_t
proto, uint16_t vlan_id)
277 if (libnet_build_802_1q(lpacket->dmac, lpacket->smac, ETHERTYPE_VLAN, 0, 0, vlan_id,
proto,
282 SCLogError(
"libnet_build_802_1q %s", libnet_geterror(c));
290 Libnet11Packet lpacket;
302 libnet_t *c = GetCtx(p, LIBNET_RAW4, dir);
306 lpacket.len = LIBNET_IPV4_H + LIBNET_TCP_H;
323 SetupTCP(p, &lpacket, dir);
325 if (BuildTCP(c, &lpacket) < 0)
328 if (BuildIPv4(c, &lpacket, IPPROTO_TCP) < 0)
331 if (t_inject_mode == LIBNET_LINK) {
332 SetupEthernet(p, &lpacket, dir);
343 result = libnet_write(c);
345 SCLogError(
"libnet_write failed: %s", libnet_geterror(c));
356 const IPV4Hdr *ip4h = PacketGetIPv4(p);
357 Libnet11Packet lpacket;
369 lpacket.len = LIBNET_IPV4_H +
MIN(8,iplen);
371 lpacket.dsize = lpacket.len - (LIBNET_IPV4_H + LIBNET_ICMPV4_H);
373 libnet_t *c = GetCtx(p, LIBNET_RAW4, dir);
401 SCLogError(
"libnet_build_icmpv4_unreach %s", libnet_geterror(c));
405 if (BuildIPv4(c, &lpacket, IPPROTO_ICMP) < 0)
408 if (t_inject_mode == LIBNET_LINK) {
409 SetupEthernet(p, &lpacket, dir);
420 result = libnet_write(c);
422 SCLogError(
"libnet_write_raw_ipv4 failed: %s", libnet_geterror(c));
433 Libnet11Packet lpacket;
445 libnet_t *c = GetCtx(p, LIBNET_RAW6, dir);
449 lpacket.len = LIBNET_TCP_H;
466 SetupTCP(p, &lpacket, dir);
468 BuildTCP(c, &lpacket);
470 if (BuildIPv6(c, &lpacket, IPPROTO_TCP) < 0)
473 if (t_inject_mode == LIBNET_LINK) {
474 SetupEthernet(p, &lpacket, dir);
484 result = libnet_write(c);
486 SCLogError(
"libnet_write failed: %s", libnet_geterror(c));
495 #ifdef HAVE_LIBNET_ICMPV6_UNREACH
498 const IPV6Hdr *ip6h = PacketGetIPv6(p);
499 Libnet11Packet lpacket;
513 lpacket.dsize = lpacket.len - LIBNET_ICMPV6_H;
515 libnet_t *c = GetCtx(p, LIBNET_RAW6, dir);
543 SCLogError(
"libnet_build_icmpv6_unreach %s", libnet_geterror(c));
547 if (BuildIPv6(c, &lpacket, IPPROTO_ICMPV6) < 0)
550 if (t_inject_mode == LIBNET_LINK) {
551 SetupEthernet(p, &lpacket, dir);
561 result = libnet_write(c);
563 SCLogError(
"libnet_write_raw_ipv6 failed: %s", libnet_geterror(c));
576 SCLogError(
"Libnet ICMPv6 based rejects are disabled."
577 "Usually this means that you don't have a patched libnet installed,"
578 " or configure couldn't find it.");
588 SCLogError(
"Libnet based rejects are disabled."
589 "Usually this means that you don't have libnet installed,"
590 " or configure couldn't find it.");
596 SCLogError(
"Libnet based rejects are disabled."
597 "Usually this means that you don't have libnet installed,"
598 " or configure couldn't find it.");
604 SCLogError(
"Libnet based rejects are disabled."
605 "Usually this means that you don't have libnet installed,"
606 " or configure couldn't find it.");
612 SCLogError(
"Libnet based rejects are disabled."
613 "Usually this means that you don't have libnet installed,"
614 " or configure couldn't find it.");