55 #ifndef HAVE_LIBNET_INIT_CONST
56 #define LIBNET_INIT_CAST (char *)
58 #define LIBNET_INIT_CAST
62 const char *g_reject_dev = NULL;
63 uint16_t g_reject_dev_mtu = 0;
71 thread_local libnet_t *t_c = NULL;
72 thread_local
int t_inject_mode = -1;
74 typedef struct Libnet11Packet_ {
76 uint16_t window, dsize;
81 struct libnet_in6_addr src6, dst6;
88 static inline libnet_t *GetCtx(
const Packet *p,
int injection_type)
95 bool store_ctx =
false;
96 const char *devname = NULL;
99 if (g_reject_dev != NULL) {
101 injection_type = t_inject_mode = LIBNET_LINK;
102 devname = g_reject_dev;
109 char ebuf[LIBNET_ERRBUF_SIZE];
110 libnet_t *c = libnet_init(injection_type, LIBNET_INIT_CAST devname, ebuf);
120 static inline void ClearCtx(libnet_t *c)
123 libnet_clear_packet(c);
148 lpacket->ack =
TCP_GET_SEQ(p) + lpacket->dsize + 1;
171 static inline int BuildTCP(libnet_t *c, Libnet11Packet *lpacket)
174 if ((libnet_build_tcp(
189 SCLogError(
"libnet_build_tcp %s", libnet_geterror(c));
195 static inline int BuildIPv4(libnet_t *c, Libnet11Packet *lpacket,
const uint8_t
proto)
197 if ((libnet_build_ipv4(
212 SCLogError(
"libnet_build_ipv4 %s", libnet_geterror(c));
218 static inline int BuildIPv6(libnet_t *c, Libnet11Packet *lpacket,
const uint8_t
proto)
220 if ((libnet_build_ipv6(
233 SCLogError(
"libnet_build_ipv6 %s", libnet_geterror(c));
243 lpacket->smac = p->
ethh->eth_dst;
244 lpacket->dmac = p->
ethh->eth_src;
248 lpacket->smac = p->
ethh->eth_src;
249 lpacket->dmac = p->
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(
266 lpacket->dmac, lpacket->smac, ETHERTYPE_VLAN,
267 0x000, 0x000, vlan_id,
proto,
273 SCLogError(
"libnet_build_802_1q %s", libnet_geterror(c));
281 Libnet11Packet lpacket;
293 libnet_t *c = GetCtx(p, LIBNET_RAW4);
297 lpacket.len = LIBNET_IPV4_H + LIBNET_TCP_H;
314 SetupTCP(p, &lpacket, dir);
316 if (BuildTCP(c, &lpacket) < 0)
319 if (BuildIPv4(c, &lpacket, IPPROTO_TCP) < 0)
322 if (t_inject_mode == LIBNET_LINK) {
323 SetupEthernet(p, &lpacket, dir);
334 result = libnet_write(c);
336 SCLogError(
"libnet_write failed: %s", libnet_geterror(c));
347 Libnet11Packet lpacket;
359 lpacket.len = LIBNET_IPV4_H +
MIN(8,iplen);
361 lpacket.dsize = lpacket.len - (LIBNET_IPV4_H + LIBNET_ICMPV4_H);
363 libnet_t *c = GetCtx(p, LIBNET_RAW4);
383 if ((libnet_build_icmpv4_unreach(
392 SCLogError(
"libnet_build_icmpv4_unreach %s", libnet_geterror(c));
396 if (BuildIPv4(c, &lpacket, IPPROTO_ICMP) < 0)
399 if (t_inject_mode == LIBNET_LINK) {
400 SetupEthernet(p, &lpacket, dir);
411 result = libnet_write(c);
413 SCLogError(
"libnet_write_raw_ipv4 failed: %s", libnet_geterror(c));
424 Libnet11Packet lpacket;
436 libnet_t *c = GetCtx(p, LIBNET_RAW6);
440 lpacket.len = LIBNET_IPV6_H + LIBNET_TCP_H;
457 SetupTCP(p, &lpacket, dir);
459 BuildTCP(c, &lpacket);
461 if (BuildIPv6(c, &lpacket, IPPROTO_ICMP) < 0)
464 if (t_inject_mode == LIBNET_LINK) {
465 SetupEthernet(p, &lpacket, dir);
475 result = libnet_write(c);
477 SCLogError(
"libnet_write failed: %s", libnet_geterror(c));
486 #ifdef HAVE_LIBNET_ICMPV6_UNREACH
489 Libnet11Packet lpacket;
503 lpacket.dsize = lpacket.len - LIBNET_ICMPV6_H;
505 libnet_t *c = GetCtx(p, LIBNET_RAW6);
525 if ((libnet_build_icmpv6_unreach(
534 SCLogError(
"libnet_build_icmpv6_unreach %s", libnet_geterror(c));
538 if (BuildIPv6(c, &lpacket, IPPROTO_ICMPV6) < 0)
541 if (t_inject_mode == LIBNET_LINK) {
542 SetupEthernet(p, &lpacket, dir);
552 result = libnet_write(c);
554 SCLogError(
"libnet_write_raw_ipv6 failed: %s", libnet_geterror(c));
567 SCLogError(
"Libnet ICMPv6 based rejects are disabled."
568 "Usually this means that you don't have a patched libnet installed,"
569 " or configure couldn't find it.");
579 SCLogError(
"Libnet based rejects are disabled."
580 "Usually this means that you don't have libnet installed,"
581 " or configure couldn't find it.");
587 SCLogError(
"Libnet based rejects are disabled."
588 "Usually this means that you don't have libnet installed,"
589 " or configure couldn't find it.");
595 SCLogError(
"Libnet based rejects are disabled."
596 "Usually this means that you don't have libnet installed,"
597 " or configure couldn't find it.");
603 SCLogError(
"Libnet based rejects are disabled."
604 "Usually this means that you don't have libnet installed,"
605 " or configure couldn't find it.");