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;
83 const uint8_t *smac, *dmac;
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);
136 const TCPHdr *tcph = PacketGetTCP(p);
170 static inline int BuildTCP(libnet_t *c, Libnet11Packet *lpacket)
173 if ((libnet_build_tcp(
188 SCLogError(
"libnet_build_tcp %s", libnet_geterror(c));
194 static inline int BuildIPv4(libnet_t *c, Libnet11Packet *lpacket,
const uint8_t
proto)
196 if ((libnet_build_ipv4(
211 SCLogError(
"libnet_build_ipv4 %s", libnet_geterror(c));
217 static inline int BuildIPv6(libnet_t *c, Libnet11Packet *lpacket,
const uint8_t
proto)
219 if ((libnet_build_ipv6(
232 SCLogError(
"libnet_build_ipv6 %s", libnet_geterror(c));
240 const EthernetHdr *ethh = PacketGetEthernet(p);
243 lpacket->smac = ethh->eth_dst;
244 lpacket->dmac = ethh->eth_src;
248 lpacket->smac = ethh->eth_src;
249 lpacket->dmac = ethh->eth_dst;
254 static inline int BuildEthernet(libnet_t *c, Libnet11Packet *lpacket, uint16_t
proto)
256 if ((libnet_build_ethernet(lpacket->dmac,lpacket->smac,
proto , NULL, 0, c, 0)) < 0) {
257 SCLogError(
"libnet_build_ethernet %s", libnet_geterror(c));
263 static inline int BuildEthernetVLAN(libnet_t *c, Libnet11Packet *lpacket, uint16_t
proto, uint16_t vlan_id)
265 if (libnet_build_802_1q(lpacket->dmac, lpacket->smac, ETHERTYPE_VLAN, 0, 0, vlan_id,
proto,
270 SCLogError(
"libnet_build_802_1q %s", libnet_geterror(c));
278 Libnet11Packet lpacket;
290 libnet_t *c = GetCtx(p, LIBNET_RAW4);
294 lpacket.len = LIBNET_IPV4_H + LIBNET_TCP_H;
311 SetupTCP(p, &lpacket, dir);
313 if (BuildTCP(c, &lpacket) < 0)
316 if (BuildIPv4(c, &lpacket, IPPROTO_TCP) < 0)
319 if (t_inject_mode == LIBNET_LINK) {
320 SetupEthernet(p, &lpacket, dir);
331 result = libnet_write(c);
333 SCLogError(
"libnet_write failed: %s", libnet_geterror(c));
344 const IPV4Hdr *ip4h = PacketGetIPv4(p);
345 Libnet11Packet lpacket;
357 lpacket.len = LIBNET_IPV4_H +
MIN(8,iplen);
359 lpacket.dsize = lpacket.len - (LIBNET_IPV4_H + LIBNET_ICMPV4_H);
361 libnet_t *c = GetCtx(p, LIBNET_RAW4);
389 SCLogError(
"libnet_build_icmpv4_unreach %s", libnet_geterror(c));
393 if (BuildIPv4(c, &lpacket, IPPROTO_ICMP) < 0)
396 if (t_inject_mode == LIBNET_LINK) {
397 SetupEthernet(p, &lpacket, dir);
408 result = libnet_write(c);
410 SCLogError(
"libnet_write_raw_ipv4 failed: %s", libnet_geterror(c));
421 Libnet11Packet lpacket;
433 libnet_t *c = GetCtx(p, LIBNET_RAW6);
437 lpacket.len = LIBNET_TCP_H;
454 SetupTCP(p, &lpacket, dir);
456 BuildTCP(c, &lpacket);
458 if (BuildIPv6(c, &lpacket, IPPROTO_TCP) < 0)
461 if (t_inject_mode == LIBNET_LINK) {
462 SetupEthernet(p, &lpacket, dir);
472 result = libnet_write(c);
474 SCLogError(
"libnet_write failed: %s", libnet_geterror(c));
483 #ifdef HAVE_LIBNET_ICMPV6_UNREACH
486 const IPV6Hdr *ip6h = PacketGetIPv6(p);
487 Libnet11Packet lpacket;
501 lpacket.dsize = lpacket.len - LIBNET_ICMPV6_H;
503 libnet_t *c = GetCtx(p, LIBNET_RAW6);
531 SCLogError(
"libnet_build_icmpv6_unreach %s", libnet_geterror(c));
535 if (BuildIPv6(c, &lpacket, IPPROTO_ICMPV6) < 0)
538 if (t_inject_mode == LIBNET_LINK) {
539 SetupEthernet(p, &lpacket, dir);
549 result = libnet_write(c);
551 SCLogError(
"libnet_write_raw_ipv6 failed: %s", libnet_geterror(c));
564 SCLogError(
"Libnet ICMPv6 based rejects are disabled."
565 "Usually this means that you don't have a patched libnet installed,"
566 " or configure couldn't find it.");
576 SCLogError(
"Libnet based rejects are disabled."
577 "Usually this means that you don't have libnet installed,"
578 " or configure couldn't find it.");
584 SCLogError(
"Libnet based rejects are disabled."
585 "Usually this means that you don't have libnet installed,"
586 " or configure couldn't find it.");
592 SCLogError(
"Libnet based rejects are disabled."
593 "Usually this means that you don't have libnet installed,"
594 " or configure couldn't find it.");
600 SCLogError(
"Libnet based rejects are disabled."
601 "Usually this means that you don't have libnet installed,"
602 " or configure couldn't find it.");