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 if (!PacketIsEthernet(
p))
254 const EthernetHdr *ethh = PacketGetEthernet(
p);
257 lpacket->smac = ethh->eth_dst;
258 lpacket->dmac = ethh->eth_src;
262 lpacket->smac = ethh->eth_src;
263 lpacket->dmac = ethh->eth_dst;
268 static inline int BuildEthernet(libnet_t *c, Libnet11Packet *lpacket, uint16_t
proto)
270 if ((libnet_build_ethernet(lpacket->dmac,lpacket->smac,
proto , NULL, 0, c, 0)) < 0) {
271 SCLogError(
"libnet_build_ethernet %s", libnet_geterror(c));
277 static inline int BuildEthernetVLAN(libnet_t *c, Libnet11Packet *lpacket, uint16_t
proto, uint16_t vlan_id)
279 if (libnet_build_802_1q(lpacket->dmac, lpacket->smac, ETHERTYPE_VLAN, 0, 0, vlan_id,
proto,
284 SCLogError(
"libnet_build_802_1q %s", libnet_geterror(c));
292 Libnet11Packet lpacket;
304 libnet_t *c = GetCtx(
p, LIBNET_RAW4, dir);
308 lpacket.len = LIBNET_IPV4_H + LIBNET_TCP_H;
325 SetupTCP(
p, &lpacket, dir);
327 if (BuildTCP(c, &lpacket) < 0)
330 if (BuildIPv4(c, &lpacket, IPPROTO_TCP) < 0)
333 if (t_inject_mode == LIBNET_LINK && PacketIsEthernet(
p)) {
334 SetupEthernet(
p, &lpacket, dir);
345 result = libnet_write(c);
347 SCLogError(
"libnet_write failed: %s", libnet_geterror(c));
358 const IPV4Hdr *ip4h = PacketGetIPv4(
p);
359 Libnet11Packet lpacket;
371 lpacket.len = LIBNET_IPV4_H +
MIN(8,iplen);
373 lpacket.dsize = lpacket.len - (LIBNET_IPV4_H + LIBNET_ICMPV4_H);
375 libnet_t *c = GetCtx(
p, LIBNET_RAW4, dir);
403 SCLogError(
"libnet_build_icmpv4_unreach %s", libnet_geterror(c));
407 if (BuildIPv4(c, &lpacket, IPPROTO_ICMP) < 0)
410 if (t_inject_mode == LIBNET_LINK && PacketIsEthernet(
p)) {
411 SetupEthernet(
p, &lpacket, dir);
422 result = libnet_write(c);
424 SCLogError(
"libnet_write_raw_ipv4 failed: %s", libnet_geterror(c));
435 Libnet11Packet lpacket;
447 libnet_t *c = GetCtx(
p, LIBNET_RAW6, dir);
451 lpacket.len = LIBNET_TCP_H;
468 SetupTCP(
p, &lpacket, dir);
470 BuildTCP(c, &lpacket);
472 if (BuildIPv6(c, &lpacket, IPPROTO_TCP) < 0)
475 if (t_inject_mode == LIBNET_LINK && PacketIsEthernet(
p)) {
476 SetupEthernet(
p, &lpacket, dir);
486 result = libnet_write(c);
488 SCLogError(
"libnet_write failed: %s", libnet_geterror(c));
497 #ifdef HAVE_LIBNET_ICMPV6_UNREACH
500 const IPV6Hdr *ip6h = PacketGetIPv6(
p);
501 Libnet11Packet lpacket;
515 lpacket.dsize = lpacket.len - LIBNET_ICMPV6_H;
517 libnet_t *c = GetCtx(
p, LIBNET_RAW6, dir);
545 SCLogError(
"libnet_build_icmpv6_unreach %s", libnet_geterror(c));
549 if (BuildIPv6(c, &lpacket, IPPROTO_ICMPV6) < 0)
552 if (t_inject_mode == LIBNET_LINK && PacketIsEthernet(
p)) {
553 SetupEthernet(
p, &lpacket, dir);
563 result = libnet_write(c);
565 SCLogError(
"libnet_write_raw_ipv6 failed: %s", libnet_geterror(c));
578 SCLogError(
"Libnet ICMPv6 based rejects are disabled."
579 "Usually this means that you don't have a patched libnet installed,"
580 " 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.");
606 SCLogError(
"Libnet based rejects are disabled."
607 "Usually this means that you don't have libnet installed,"
608 " or configure couldn't find it.");
614 SCLogError(
"Libnet based rejects are disabled."
615 "Usually this means that you don't have libnet installed,"
616 " or configure couldn't find it.");