33 #define SC_PCAP_DONT_INCLUDE_PCAP_H 1
67 #include <sys/ioctl.h>
70 #if HAVE_LINUX_SOCKIOS_H
71 #include <linux/sockios.h>
74 #ifdef HAVE_PACKET_EBPF
75 #define PCAP_DONT_INCLUDE_PCAP_BPF_H 1
76 #include <bpf/libbpf.h>
81 struct bpf_insn *bf_insns;
89 #ifdef HAVE_PCAP_PCAP_H
90 #include <pcap/pcap.h>
95 #if HAVE_LINUX_IF_ETHER_H
96 #include <linux/if_ether.h>
99 #if HAVE_LINUX_IF_PACKET_H
100 #include <linux/if_packet.h>
103 #if HAVE_LINUX_IF_ARP_H
104 #include <linux/if_arp.h>
107 #if HAVE_LINUX_FILTER_H
108 #include <linux/filter.h>
112 #include <sys/mman.h>
115 #ifdef HAVE_HW_TIMESTAMPING
116 #include <linux/net_tstamp.h>
123 #ifndef HAVE_AF_PACKET
157 SCLogError(
"Error creating thread %s: you do not have "
158 "support for AF_PACKET enabled, on Linux host please recompile "
159 "with --enable-af-packet",
166 #define AFP_IFACE_NAME_LENGTH 48
168 #define AFP_STATE_DOWN 0
169 #define AFP_STATE_UP 1
171 #define AFP_RECONNECT_TIMEOUT 500000
172 #define AFP_DOWN_COUNTER_INTERVAL 40
174 #define POLL_TIMEOUT 100
177 #ifndef TP_STATUS_KERNEL
178 #define TP_STATUS_KERNEL 0
180 #ifndef TP_STATUS_USER
181 #define TP_STATUS_USER BIT_U32(0)
183 #ifndef TP_STATUS_COPY
184 #define TP_STATUS_COPY BIT_U32(1)
186 #ifndef TP_STATUS_LOSING
187 #define TP_STATUS_LOSING BIT_U32(2)
189 #ifndef TP_STATUS_CSUMNOTREADY
190 #define TP_STATUS_CSUMNOTREADY BIT_U32(3)
192 #ifndef TP_STATUS_VLAN_VALID
193 #define TP_STATUS_VLAN_VALID BIT_U32(4)
195 #ifndef TP_STATUS_BLK_TMO
196 #define TP_STATUS_BLK_TMO BIT_U32(5)
198 #ifndef TP_STATUS_VLAN_TPID_VALID
199 #define TP_STATUS_VLAN_TPID_VALID BIT_U32(6)
201 #ifndef TP_STATUS_CSUM_VALID
202 #define TP_STATUS_CSUM_VALID BIT_U32(7)
205 #ifndef TP_STATUS_TS_SOFTWARE
206 #define TP_STATUS_TS_SOFTWARE BIT_U32(29)
208 #ifndef TP_STATUS_TS_SYS_HARDWARE
209 #define TP_STATUS_TS_SYS_HARDWARE BIT_U32(30)
211 #ifndef TP_STATUS_TS_RAW_HARDWARE
212 #define TP_STATUS_TS_RAW_HARDWARE BIT_U32(31)
215 #ifndef TP_STATUS_USER_BUSY
227 #define TP_STATUS_USER_BUSY \
228 (uint32_t)((uint32_t)TP_STATUS_TS_SOFTWARE | (uint32_t)TP_STATUS_TS_SYS_HARDWARE | \
229 (uint32_t)TP_STATUS_TS_RAW_HARDWARE)
231 #define FRAME_BUSY(tp_status) \
232 (((uint32_t)(tp_status) & (uint32_t)TP_STATUS_USER_BUSY) == (uint32_t)TP_STATUS_USER_BUSY)
248 struct tpacket2_hdr *
h2;
249 struct tpacket3_hdr *
h3;
253 #ifdef HAVE_PACKET_EBPF
254 static int AFPBypassCallback(
Packet *p);
255 static int AFPXDPBypassCallback(
Packet *p);
277 #ifdef HAVE_PACKET_EBPF
339 struct tpacket_req
v2;
340 struct tpacket_req3
v3;
352 #ifdef HAVE_PACKET_EBPF
356 struct ebpf_timeout_config ebpf_t_config;
362 static void ReceiveAFPThreadExitStats(
ThreadVars *,
void *);
371 static int AFPGetIfnumByDev(
int fd,
const char *ifname,
int verbose);
372 static int AFPGetDevFlags(
int fd,
const char *ifname);
373 static int AFPDerefSocket(
AFPPeer* peer);
374 static int AFPRefSocket(
AFPPeer* peer);
425 if (ptv->
mpeer == NULL) {
436 static void AFPPeerClean(
AFPPeer *peer)
468 #define AFP_PEERS_MAX_TRY 4
469 #define AFP_PEERS_WAIT 20000
530 if (out_iface == NULL)
532 if (iface->
mtu != out_iface->
mtu) {
533 SCLogWarning(
"MTU on %s (%d) and %s (%d) are not equal, transmission of packets "
534 "bigger than %d will fail.",
548 static int AFPPeersListWaitTurn(
AFPPeer *peer)
559 static void AFPPeersListReachedInc(
void)
573 static int AFPPeersListStarted(
void)
610 static int AFPCreateSocket(
AFPThreadVars *ptv,
char *devname,
int verbose,
const bool peer_update);
614 #ifdef PACKET_STATISTICS
615 struct tpacket_stats kstats;
616 socklen_t
len =
sizeof (
struct tpacket_stats);
617 if (getsockopt(ptv->
socket, SOL_PACKET, PACKET_STATISTICS,
618 &kstats, &
len) > -1) {
619 SCLogDebug(
"(%s) Kernel: Packets %" PRIu32
", dropped %" PRIu32
"",
621 kstats.tp_packets, kstats.tp_drops);
650 struct sockaddr_ll socket_address;
659 if (!PacketIsEthernet(p)) {
664 const EthernetHdr *ethh = PacketGetEthernet(p);
666 socket_address.sll_ifindex =
SC_ATOMIC_GET(p->afp_v.peer->if_idx);
668 socket_address.sll_halen = ETH_ALEN;
670 memcpy(socket_address.sll_addr, ethh, 6);
678 sizeof(
struct sockaddr_ll)) < 0) {
680 SCLogWarning(
"%s: sending packet failed on socket %d: %s", p->afp_v.peer->iface, socket,
688 static void AFPReleaseDataFromRing(
Packet *p)
695 AFPWritePacket(p, TPACKET_V2);
698 BUG_ON(p->afp_v.relptr == NULL);
701 h.
raw = p->afp_v.relptr;
704 (void)AFPDerefSocket(p->afp_v.mpeer);
709 static void AFPReleasePacketV3(
Packet *p)
716 AFPWritePacket(p, TPACKET_V3);
721 static void AFPReleasePacket(
Packet *p)
723 AFPReleaseDataFromRing(p);
742 #ifdef HAVE_PACKET_EBPF
745 p->afp_v.v4_map_fd = ptv->v4_map_fd;
746 p->afp_v.v6_map_fd = ptv->v6_map_fd;
747 p->afp_v.nr_cpus = ptv->ebpf_t_config.cpus_count;
751 p->afp_v.v4_map_fd = ptv->v4_map_fd;
752 p->afp_v.v6_map_fd = ptv->v6_map_fd;
753 p->afp_v.nr_cpus = ptv->ebpf_t_config.cpus_count;
761 static void AFPReadFromRingSetupPacket(
774 AFPReadApplyBypass(ptv, p);
776 if (h.
h2->tp_len > h.
h2->tp_snaplen) {
777 SCLogDebug(
"Packet length (%d) > snaplen (%d), truncating", h.
h2->tp_len, h.
h2->tp_snaplen);
784 p->
vlan_id[0] = h.
h2->tp_vlan_tci & 0x0fff;
786 p->afp_v.vlan_tci = h.
h2->tp_vlan_tci;
792 p->afp_v.relptr = h.
raw;
794 p->afp_v.mpeer = ptv->
mpeer;
795 AFPRefSocket(ptv->
mpeer);
797 p->afp_v.mpeer = NULL;
822 static inline int AFPReadFromRingWaitForPacket(
AFPThreadVars *ptv)
825 struct timeval start_time;
826 gettimeofday(&start_time, NULL);
827 int64_t busy_loop_iter = 0;
838 const unsigned int tp_status = h.
h2->tp_status;
842 struct timeval cur_time;
843 memset(&cur_time, 0,
sizeof(cur_time));
844 uint64_t milliseconds =
845 ((cur_time.tv_sec - start_time.tv_sec) * 1000) +
846 (((1000000 + cur_time.tv_usec - start_time.tv_usec) / 1000) - 1000);
847 if (milliseconds > 1000) {
854 if (busy_loop_iter) {
871 static inline bool AFPShouldIgnoreFrame(
AFPThreadVars *ptv,
const struct sockaddr_ll *sll)
873 if (
unlikely(sll->sll_pkttype > 31))
891 bool emergency_flush =
false;
896 if (AFPReadFromRingWaitForPacket(ptv) !=
AFP_READ_OK)
908 const unsigned int tp_status = h.
h2->tp_status;
924 const struct sockaddr_ll *sll =
925 (
const struct sockaddr_ll *)((uint8_t *)h.
h2 +
926 TPACKET_ALIGN(
sizeof(
struct tpacket2_hdr)));
927 if (
unlikely(AFPShouldIgnoreFrame(ptv, sll)))
932 return AFPSuriFailure(ptv, h);
934 AFPReadFromRingSetupPacket(ptv, h, tp_status, p);
937 return AFPSuriFailure(ptv, h);
947 if (emergency_flush) {
948 AFPDumpCounters(ptv);
953 static inline void AFPFlushBlock(
struct tpacket_block_desc *pbd)
958 static inline int AFPParsePacketV3(
AFPThreadVars *ptv,
struct tpacket_block_desc *pbd,
struct tpacket3_hdr *ppd)
966 AFPReadApplyBypass(ptv, p);
974 p->
vlan_id[0] = ppd->hv1.tp_vlan_tci & 0x0fff;
976 p->afp_v.vlan_tci = (uint16_t)ppd->hv1.tp_vlan_tci;
979 if (ppd->tp_len > ppd->tp_snaplen) {
980 SCLogDebug(
"Packet length (%d) > snaplen (%d), truncating", ppd->tp_len, ppd->tp_snaplen);
984 (void)
PacketSetData(p, (
unsigned char *)ppd + ppd->tp_mac, ppd->tp_snaplen);
987 p->afp_v.relptr = NULL;
988 p->afp_v.mpeer = NULL;
993 p->
ts = (
SCTime_t){ .
secs = ppd->tp_sec, .usecs = ppd->tp_nsec / 1000 };
994 SCLogDebug(
"pktlen: %" PRIu32
" (pkt %p, pkt data %p)",
1020 static inline int AFPWalkBlock(
AFPThreadVars *ptv,
struct tpacket_block_desc *pbd)
1022 const int num_pkts = pbd->hdr.bh1.num_pkts;
1023 uint8_t *ppd = (uint8_t *)pbd + pbd->hdr.bh1.offset_to_first_pkt;
1025 for (
int i = 0; i < num_pkts; ++i) {
1026 const struct sockaddr_ll *sll =
1027 (
const struct sockaddr_ll *)(ppd + TPACKET_ALIGN(
sizeof(
struct tpacket3_hdr)));
1028 if (
unlikely(AFPShouldIgnoreFrame(ptv, sll))) {
1029 ppd = ppd + ((
struct tpacket3_hdr *)ppd)->tp_next_offset;
1032 int ret = AFPParsePacketV3(ptv, pbd, (
struct tpacket3_hdr *)ppd);
1045 ppd = ppd + ((
struct tpacket3_hdr *)ppd)->tp_next_offset;
1069 struct tpacket_block_desc *pbd =
1077 int ret = AFPWalkBlock(ptv, pbd);
1098 static int AFPRefSocket(
AFPPeer* peer)
1113 static int AFPDerefSocket(
AFPPeer* peer)
1126 if (ptv->
mpeer != NULL)
1149 static void AFPSwitchState(
AFPThreadVars *ptv, uint8_t state)
1166 static int AFPReadAndDiscardFromRing(
AFPThreadVars *ptv,
struct timeval *synctv,
1167 uint64_t *discarded_pkts)
1175 struct tpacket_block_desc *pbd =
1177 *discarded_pkts += pbd->hdr.bh1.num_pkts;
1178 struct tpacket3_hdr *ppd =
1179 (
struct tpacket3_hdr *)((uint8_t *)pbd + pbd->hdr.bh1.offset_to_first_pkt);
1180 if (((time_t)ppd->tp_sec > synctv->tv_sec) ||
1181 ((time_t)ppd->tp_sec == synctv->tv_sec &&
1182 (suseconds_t) (ppd->tp_nsec / 1000) > (suseconds_t)synctv->tv_usec)) {
1193 if (h.
raw == NULL) {
1199 if (((time_t)h.
h2->tp_sec > synctv->tv_sec) ||
1200 ((time_t)h.
h2->tp_sec == synctv->tv_sec &&
1201 (suseconds_t) (h.
h2->tp_nsec / 1000) > synctv->tv_usec)) {
1205 (*discarded_pkts)++;
1224 static int AFPSynchronizeStart(
AFPThreadVars *ptv, uint64_t *discarded_pkts)
1226 struct timeval synctv;
1230 fds.events = POLLIN;
1233 synctv.tv_sec = 0xffffffff;
1234 synctv.tv_usec = 0xffffffff;
1239 (fds.revents & (POLLHUP|POLLRDHUP|POLLERR|POLLNVAL))) {
1241 fds.revents & (POLLHUP | POLLRDHUP | POLLERR | POLLNVAL));
1244 if (AFPPeersListStarted() && synctv.tv_sec == (time_t) 0xffffffff) {
1245 gettimeofday(&synctv, NULL);
1247 r = AFPReadAndDiscardFromRing(ptv, &synctv, discarded_pkts);
1257 }
else if (r == 0 && AFPPeersListStarted()) {
1284 AFPCloseSocket(ptv);
1286 int afp_activate_r = AFPCreateSocket(ptv, ptv->
iface, 0,
false);
1287 if (afp_activate_r != 0) {
1291 return afp_activate_r;
1309 time_t last_dump = 0;
1310 time_t current_time;
1312 uint64_t discarded_pkts = 0;
1317 AFPReadFunc = AFPReadFromRingV3;
1319 AFPReadFunc = AFPReadFromRing;
1324 while (AFPPeersListWaitTurn(ptv->
mpeer)) {
1330 r = AFPCreateSocket(ptv, ptv->
iface, 1,
true);
1338 "%s: failed to init socket for interface, retrying soon", ptv->
iface);
1344 AFPSynchronizeStart(ptv, &discarded_pkts);
1347 #ifdef PACKET_STATISTICS
1348 struct tpacket_stats kstats;
1349 socklen_t
len =
sizeof (
struct tpacket_stats);
1350 if (getsockopt(ptv->
socket, SOL_PACKET, PACKET_STATISTICS,
1351 &kstats, &
len) > -1) {
1353 SCLogDebug(
"(%s) Kernel socket startup: Packets %" PRIu32
1354 ", dropped %" PRIu32
"",
1356 kstats.tp_packets, kstats.tp_drops);
1357 pkts = kstats.tp_packets - discarded_pkts - kstats.tp_drops;
1365 fds.events = POLLIN;
1382 r = AFPTryReopen(ptv);
1402 (fds.revents & (POLLHUP|POLLRDHUP|POLLERR|POLLNVAL))) {
1404 if (fds.revents & (POLLHUP | POLLRDHUP)) {
1407 }
else if (fds.revents & POLLERR) {
1410 if (recv(ptv->
socket, &c,
sizeof c, MSG_PEEK) != -1)
1415 }
else if (fds.revents & POLLNVAL) {
1422 r = AFPReadFunc(ptv);
1426 current_time = time(NULL);
1427 if (current_time != last_dump) {
1428 AFPDumpCounters(ptv);
1429 last_dump = current_time;
1441 AFPDumpCounters(ptv);
1447 current_time = time(NULL);
1448 if (current_time != last_dump) {
1449 AFPDumpCounters(ptv);
1450 last_dump = current_time;
1453 TmThreadsCaptureHandleTimeout(
tv, NULL);
1455 }
else if ((r < 0) && (errno != EINTR)) {
1464 AFPDumpCounters(ptv);
1469 static int AFPGetDevFlags(
int fd,
const char *ifname)
1473 memset(&ifr, 0,
sizeof(ifr));
1474 strlcpy(ifr.ifr_name, ifname,
sizeof(ifr.ifr_name));
1476 if (ioctl(fd, SIOCGIFFLAGS, &ifr) == -1) {
1477 SCLogError(
"%s: failed to get interface flags: %s", ifname, strerror(errno));
1481 return ifr.ifr_flags;
1485 static int AFPGetIfnumByDev(
int fd,
const char *ifname,
int verbose)
1489 memset(&ifr, 0,
sizeof(ifr));
1490 strlcpy(ifr.ifr_name, ifname,
sizeof(ifr.ifr_name));
1492 if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
1494 SCLogError(
"%s: failed to find interface: %s", ifname, strerror(errno));
1498 return ifr.ifr_ifindex;
1501 static int AFPGetDevLinktype(
int fd,
const char *ifname)
1505 memset(&ifr, 0,
sizeof(ifr));
1506 strlcpy(ifr.ifr_name, ifname,
sizeof(ifr.ifr_name));
1508 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) {
1509 SCLogError(
"%s: failed to find interface type: %s", ifname, strerror(errno));
1513 switch (ifr.ifr_hwaddr.sa_family) {
1514 case ARPHRD_LOOPBACK:
1520 return ifr.ifr_hwaddr.sa_family;
1528 int fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1530 SCLogError(
"%s: failed to create AF_PACKET socket: %s", ifname, strerror(errno));
1534 ltype = AFPGetDevLinktype(fd, ifname);
1542 static int AFPComputeRingParams(
AFPThreadVars *ptv,
int order)
1568 int tp_hdrlen =
sizeof(
struct tpacket_hdr);
1579 SCLogWarning(
"%s: unable to get MTU, setting snaplen default of 1514", ptv->
iface);
1586 ptv->
req.
v2.tp_frame_size = TPACKET_ALIGN(snaplen +TPACKET_ALIGN(TPACKET_ALIGN(tp_hdrlen) +
sizeof(
struct sockaddr_ll) + ETH_HLEN) - ETH_HLEN);
1587 ptv->
req.
v2.tp_block_size = getpagesize() << order;
1588 int frames_per_block = ptv->
req.
v2.tp_block_size / ptv->
req.
v2.tp_frame_size;
1589 if (frames_per_block == 0) {
1594 ptv->
req.
v2.tp_block_nr = ptv->
req.
v2.tp_frame_nr / frames_per_block + 1;
1596 ptv->
req.
v2.tp_frame_nr = ptv->
req.
v2.tp_block_nr * frames_per_block;
1597 SCLogPerf(
"%s: rx ring: block_size=%d block_nr=%d frame_size=%d frame_nr=%d", ptv->
iface,
1598 ptv->
req.
v2.tp_block_size, ptv->
req.
v2.tp_block_nr, ptv->
req.
v2.tp_frame_size,
1599 ptv->
req.
v2.tp_frame_nr);
1603 static int AFPComputeRingParamsWithBlockSize(
AFPThreadVars *ptv,
unsigned int block_size)
1629 int tp_hdrlen =
sizeof(
struct tpacket_hdr);
1640 SCLogWarning(
"%s: unable to get MTU, setting snaplen default of 1514", ptv->
iface);
1647 ptv->
req.
v2.tp_frame_size = TPACKET_ALIGN(
1649 TPACKET_ALIGN(TPACKET_ALIGN(tp_hdrlen) +
sizeof(
struct sockaddr_ll) + ETH_HLEN) -
1651 ptv->
req.
v2.tp_block_size = block_size;
1652 int frames_per_block = ptv->
req.
v2.tp_block_size / ptv->
req.
v2.tp_frame_size;
1653 if (frames_per_block == 0) {
1658 ptv->
req.
v2.tp_block_nr = ptv->
req.
v2.tp_frame_nr / frames_per_block + 1;
1660 ptv->
req.
v2.tp_frame_nr = ptv->
req.
v2.tp_block_nr * frames_per_block;
1661 SCLogPerf(
"%s: rx ring: block_size=%d block_nr=%d frame_size=%d frame_nr=%d", ptv->
iface,
1662 ptv->
req.
v2.tp_block_size, ptv->
req.
v2.tp_block_nr, ptv->
req.
v2.tp_frame_size,
1663 ptv->
req.
v2.tp_frame_nr);
1670 ptv->
req.
v3.tp_frame_size = 2048;
1671 int frames_per_block = 0;
1672 int tp_hdrlen =
sizeof(
struct tpacket3_hdr);
1678 SCLogWarning(
"%s: unable to get MTU, setting snaplen default of 1514", ptv->
iface);
1684 ptv->
req.
v3.tp_frame_size = TPACKET_ALIGN(snaplen +TPACKET_ALIGN(TPACKET_ALIGN(tp_hdrlen) +
sizeof(
struct sockaddr_ll) + ETH_HLEN) - ETH_HLEN);
1685 frames_per_block = ptv->
req.
v3.tp_block_size / ptv->
req.
v3.tp_frame_size;
1687 if (frames_per_block == 0) {
1688 SCLogError(
"%s: block size is too small, it should be at least %d", ptv->
iface,
1689 ptv->
req.
v3.tp_frame_size);
1694 ptv->
req.
v3.tp_frame_nr = ptv->
req.
v3.tp_block_nr * frames_per_block;
1696 ptv->
req.
v3.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH;
1697 SCLogPerf(
"%s: rx ring params: block_size=%d block_nr=%d frame_size=%d frame_nr=%d (mem: %d)",
1699 ptv->
req.
v3.tp_frame_size, ptv->
req.
v3.tp_frame_nr,
1700 ptv->
req.
v3.tp_block_size * ptv->
req.
v3.tp_block_nr);
1707 unsigned int len =
sizeof(val), i;
1716 if (getsockopt(ptv->
socket, SOL_PACKET, PACKET_HDRLEN, &val, &
len) < 0) {
1717 if (errno == ENOPROTOOPT) {
1719 SCLogError(
"%s: kernel too old for TPACKET_V3 (need 3.2+)", devname);
1721 SCLogError(
"%s: kernel too old (need 2.6.27+)", devname);
1724 SCLogError(
"%s: failed to retrieve packet header len", devname);
1732 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_VERSION, &val,
1734 SCLogError(
"%s: failed to activate TPACKET_V2/TPACKET_V3 on packet socket: %s", devname,
1739 #ifdef HAVE_HW_TIMESTAMPING
1741 int req = SOF_TIMESTAMPING_RAW_HARDWARE;
1742 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_TIMESTAMP, (
void *)&req,
sizeof(req)) < 0) {
1743 SCLogWarning(
"%s: failed to activate hardware timestamping on packet socket: %s",
1744 devname, strerror(errno));
1746 SCLogConfig(
"%s: hardware timestamping enabled", devname);
1749 SCLogConfig(
"%s: hardware timestamping disabled", devname);
1756 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_RESERVE, (
void *)&reserve,
sizeof(reserve)) <
1758 SCLogError(
"%s: failed to activate reserve on packet socket: %s", devname, strerror(errno));
1764 if (AFPComputeRingParamsV3(ptv) != 1) {
1767 r = setsockopt(ptv->
socket, SOL_PACKET, PACKET_RX_RING,
1770 SCLogError(
"%s: failed to allocate RX Ring: %s", devname, strerror(errno));
1776 if (AFPComputeRingParamsWithBlockSize(ptv, ptv->
v2_block_size) != 1) {
1777 SCLogError(
"%s: ring parameters are incorrect. Please file a bug report", devname);
1782 ptv->
socket, SOL_PACKET, PACKET_RX_RING, (
void *)&ptv->
req,
sizeof(ptv->
req));
1785 if (errno == ENOMEM) {
1786 SCLogError(
"%s: memory issue with ring parameters", devname);
1789 SCLogError(
"%s: failed to setup RX Ring: %s", devname, strerror(errno));
1795 if (AFPComputeRingParams(ptv, order) != 1) {
1797 "%s: ring parameters are incorrect. Please file a bug report", devname);
1801 r = setsockopt(ptv->
socket, SOL_PACKET, PACKET_RX_RING, (
void *)&ptv->
req,
1805 if (errno == ENOMEM) {
1806 SCLogWarning(
"%s: memory issue with ring parameters. Retrying", devname);
1809 SCLogError(
"%s: failed to setup RX Ring: %s", devname, strerror(errno));
1816 SCLogError(
"%s: failed to setup RX Ring (order 0 failed)", devname);
1828 mmap_flag = MAP_SHARED;
1830 mmap_flag |= MAP_LOCKED;
1832 mmap_flag, ptv->
socket, 0);
1834 SCLogError(
"%s: failed to mmap: %s", devname, strerror(errno));
1840 SCLogError(
"%s: failed to alloc ring: %s", devname, strerror(errno));
1843 for (i = 0; i < ptv->
req.
v3.tp_block_nr; ++i) {
1845 ptv->
ring.
v3[i].iov_len = ptv->
req.
v3.tp_block_size;
1850 if (ptv->
ring.
v2 == NULL) {
1851 SCLogError(
"%s: failed to alloc ring: %s", devname, strerror(errno));
1856 for (i = 0; i < ptv->
req.
v2.tp_block_nr; ++i) {
1857 void *base = &(ptv->
ring_buf[i * ptv->
req.
v2.tp_block_size]);
1861 base += ptv->
req.
v2.tp_frame_size;
1885 #ifdef HAVE_PACKET_FANOUT
1886 int fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1891 uint32_t option = (mode << 16) | cluster_id;
1892 int r = setsockopt(fd, SOL_PACKET,
PACKET_FANOUT,(
void *)&option,
sizeof(option));
1896 SCLogError(
"fanout not supported by kernel: "
1897 "Kernel too old or cluster-id %d already in use.",
1907 #ifdef HAVE_PACKET_EBPF
1911 int pfd = ptv->ebpf_lb_fd;
1913 SCLogError(
"Fanout file descriptor is invalid");
1917 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_FANOUT_DATA, &pfd,
sizeof(pfd))) {
1928 int pfd = ptv->ebpf_filter_fd;
1930 SCLogError(
"Filter file descriptor is invalid");
1934 if (setsockopt(ptv->
socket, SOL_SOCKET, SO_ATTACH_BPF, &pfd,
sizeof(pfd))) {
1935 SCLogError(
"Error setting ebpf: %s", strerror(errno));
1938 SCLogInfo(
"Activated eBPF filter on socket");
1945 static int AFPCreateSocket(
AFPThreadVars *ptv,
char *devname,
int verbose,
const bool peer_update)
1949 struct packet_mreq sock_params;
1950 struct sockaddr_ll bind_address;
1954 ptv->
socket = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1956 SCLogError(
"%s: failed to create socket: %s", devname, strerror(errno));
1960 if_idx = AFPGetIfnumByDev(ptv->
socket, devname, verbose);
1966 memset(&bind_address, 0,
sizeof(bind_address));
1967 bind_address.sll_family = AF_PACKET;
1968 bind_address.sll_protocol = htons(ETH_P_ALL);
1969 bind_address.sll_ifindex = if_idx;
1970 if (bind_address.sll_ifindex == -1) {
1977 int if_flags = AFPGetDevFlags(ptv->
socket, ptv->
iface);
1978 if (if_flags == -1) {
1984 }
else if ((if_flags & (IFF_UP | IFF_RUNNING)) == 0) {
1993 if (if_flags & IFF_LOOPBACK)
1998 memset(&sock_params, 0,
sizeof(sock_params));
1999 sock_params.mr_type = PACKET_MR_PROMISC;
2000 sock_params.mr_ifindex = bind_address.sll_ifindex;
2001 r = setsockopt(ptv->
socket, SOL_PACKET, PACKET_ADD_MEMBERSHIP,(
void *)&sock_params,
sizeof(sock_params));
2003 SCLogError(
"%s: failed to set promisc mode: %s", devname, strerror(errno));
2010 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_AUXDATA, &val,
2011 sizeof(val)) == -1 && errno != ENOPROTOOPT) {
2013 "%s: 'kernel' checksum mode not supported, falling back to full mode", devname);
2024 if (setsockopt(ptv->
socket, SOL_SOCKET, SO_RCVBUF,
2033 r = bind(ptv->
socket, (
struct sockaddr *)&bind_address,
sizeof(bind_address));
2036 if (errno == ENETDOWN) {
2037 SCLogWarning(
"%s: failed to bind socket, iface is down", devname);
2039 SCLogWarning(
"%s: failed to bind socket: %s", devname, strerror(errno));
2047 #ifdef HAVE_PACKET_FANOUT
2052 uint32_t option = (mode << 16) | (
id & 0xffff);
2055 SCLogError(
"%s: failed to set fanout mode: %s", devname, strerror(errno));
2061 #ifdef HAVE_PACKET_EBPF
2063 r = SockFanoutSeteBPF(ptv);
2065 SCLogError(
"%s: failed to set eBPF: %s", devname, strerror(errno));
2072 AFPPeersListReachedInc();
2074 ret = AFPSetupRing(ptv, devname);
2078 SCLogDebug(
"Using interface '%s' via socket %d", (
char *)devname, ptv->
socket);
2082 TmEcode rc = AFPSetBPFFilter(ptv);
2113 struct bpf_program filter;
2114 struct sock_fprog fcode;
2117 #ifdef HAVE_PACKET_EBPF
2118 if (ptv->ebpf_filter_fd != -1) {
2119 return SetEbpfFilter(ptv);
2128 char errbuf[PCAP_ERRBUF_SIZE];
2135 errbuf,
sizeof(errbuf)) == -1) {
2140 if (filter.bf_len > USHRT_MAX) {
2143 fcode.len = (
unsigned short)filter.bf_len;
2144 fcode.filter = (
struct sock_filter*)filter.bf_insns;
2146 rc = setsockopt(ptv->
socket, SOL_SOCKET, SO_ATTACH_FILTER, &fcode,
sizeof(fcode));
2150 SCLogError(
"%s: failed to attach filter: %s", ptv->
iface, strerror(errno));
2157 #ifdef HAVE_PACKET_EBPF
2165 static int AFPInsertHalfFlow(
int mapd,
void *key,
unsigned int nr_cpus)
2167 BPF_DECLARE_PERCPU(
struct pair, value, nr_cpus);
2176 for (i = 0; i < nr_cpus; i++) {
2177 BPF_PERCPU(value, i).packets = 0;
2178 BPF_PERCPU(value, i).bytes = 0;
2180 if (bpf_map_update_elem(mapd, key, value, BPF_NOEXIST) != 0) {
2193 SCLogError(
"Can't update eBPF map: %s (%d)", strerror(errno), errno);
2200 static int AFPSetFlowStorage(
Packet *p,
int map_fd,
void *key0,
void* key1,
2211 EBPFBypassData *eb =
SCCalloc(1,
sizeof(EBPFBypassData));
2213 EBPFDeleteKey(map_fd, key0);
2214 EBPFDeleteKey(map_fd, key1);
2223 eb->cpus_count = p->afp_v.nr_cpus;
2228 EBPFDeleteKey(map_fd, key0);
2229 EBPFDeleteKey(map_fd, key1);
2255 static int AFPBypassCallback(
Packet *p)
2257 SCLogDebug(
"Calling af_packet callback function");
2259 if (!(PacketIsTCP(p) || PacketIsUDP(p))) {
2266 if (p->
flow == NULL) {
2272 if (PacketIsTunnel(p)) {
2275 if (PacketIsIPv4(p)) {
2277 if (p->afp_v.v4_map_fd == -1) {
2280 struct flowv4_keys *keys[2];
2281 keys[0] =
SCCalloc(1,
sizeof(
struct flowv4_keys));
2282 if (keys[0] == NULL) {
2287 keys[0]->port16[0] = p->
sp;
2288 keys[0]->port16[1] = p->
dp;
2289 keys[0]->vlan0 = p->
vlan_id[0];
2290 keys[0]->vlan1 = p->
vlan_id[1];
2291 keys[0]->vlan2 = p->
vlan_id[2];
2293 if (p->
proto == IPPROTO_TCP) {
2294 keys[0]->ip_proto = 1;
2296 keys[0]->ip_proto = 0;
2298 if (AFPInsertHalfFlow(p->afp_v.v4_map_fd, keys[0],
2299 p->afp_v.nr_cpus) == 0) {
2304 keys[1]=
SCCalloc(1,
sizeof(
struct flowv4_keys));
2305 if (keys[1] == NULL) {
2306 EBPFDeleteKey(p->afp_v.v4_map_fd, keys[0]);
2313 keys[1]->port16[0] = p->
dp;
2314 keys[1]->port16[1] = p->
sp;
2315 keys[1]->vlan0 = p->
vlan_id[0];
2316 keys[1]->vlan1 = p->
vlan_id[1];
2317 keys[1]->vlan2 = p->
vlan_id[2];
2319 keys[1]->ip_proto = keys[0]->ip_proto;
2320 if (AFPInsertHalfFlow(p->afp_v.v4_map_fd, keys[1],
2321 p->afp_v.nr_cpus) == 0) {
2322 EBPFDeleteKey(p->afp_v.v4_map_fd, keys[0]);
2328 EBPFUpdateFlow(p->
flow, p, NULL);
2329 return AFPSetFlowStorage(p, p->afp_v.v4_map_fd, keys[0], keys[1], AF_INET);
2332 if (PacketIsIPv6(p) && ((p->
proto == IPPROTO_TCP) || (p->
proto == IPPROTO_UDP))) {
2334 if (p->afp_v.v6_map_fd == -1) {
2338 struct flowv6_keys *keys[2];
2339 keys[0] =
SCCalloc(1,
sizeof(
struct flowv6_keys));
2340 if (keys[0] == NULL) {
2344 for (i = 0; i < 4; i++) {
2348 keys[0]->port16[0] = p->
sp;
2349 keys[0]->port16[1] = p->
dp;
2350 keys[0]->vlan0 = p->
vlan_id[0];
2351 keys[0]->vlan1 = p->
vlan_id[1];
2352 keys[0]->vlan2 = p->
vlan_id[2];
2354 if (p->
proto == IPPROTO_TCP) {
2355 keys[0]->ip_proto = 1;
2357 keys[0]->ip_proto = 0;
2359 if (AFPInsertHalfFlow(p->afp_v.v6_map_fd, keys[0],
2360 p->afp_v.nr_cpus) == 0) {
2365 keys[1]=
SCCalloc(1,
sizeof(
struct flowv6_keys));
2366 if (keys[1] == NULL) {
2367 EBPFDeleteKey(p->afp_v.v6_map_fd, keys[0]);
2372 for (i = 0; i < 4; i++) {
2376 keys[1]->port16[0] = p->
dp;
2377 keys[1]->port16[1] = p->
sp;
2378 keys[1]->vlan0 = p->
vlan_id[0];
2379 keys[1]->vlan1 = p->
vlan_id[1];
2380 keys[1]->vlan2 = p->
vlan_id[2];
2382 keys[1]->ip_proto = keys[0]->ip_proto;
2383 if (AFPInsertHalfFlow(p->afp_v.v6_map_fd, keys[1],
2384 p->afp_v.nr_cpus) == 0) {
2385 EBPFDeleteKey(p->afp_v.v6_map_fd, keys[0]);
2392 EBPFUpdateFlow(p->
flow, p, NULL);
2393 return AFPSetFlowStorage(p, p->afp_v.v6_map_fd, keys[0], keys[1], AF_INET6);
2409 static int AFPXDPBypassCallback(
Packet *p)
2411 SCLogDebug(
"Calling af_packet callback function");
2413 if (!(PacketIsTCP(p) || PacketIsUDP(p))) {
2420 if (p->
flow == NULL) {
2426 if (PacketIsTunnel(p)) {
2429 if (PacketIsIPv4(p)) {
2430 struct flowv4_keys *keys[2];
2431 keys[0]=
SCCalloc(1,
sizeof(
struct flowv4_keys));
2432 if (keys[0] == NULL) {
2436 if (p->afp_v.v4_map_fd == -1) {
2440 keys[0]->src = p->
src.addr_data32[0];
2441 keys[0]->dst = p->
dst.addr_data32[0];
2444 keys[0]->port16[0] = htons(p->
sp);
2445 keys[0]->port16[1] = htons(p->
dp);
2446 keys[0]->vlan0 = p->
vlan_id[0];
2447 keys[0]->vlan1 = p->
vlan_id[1];
2448 keys[0]->vlan2 = p->
vlan_id[2];
2449 if (p->
proto == IPPROTO_TCP) {
2450 keys[0]->ip_proto = 1;
2452 keys[0]->ip_proto = 0;
2454 if (AFPInsertHalfFlow(p->afp_v.v4_map_fd, keys[0],
2455 p->afp_v.nr_cpus) == 0) {
2460 keys[1]=
SCCalloc(1,
sizeof(
struct flowv4_keys));
2461 if (keys[1] == NULL) {
2462 EBPFDeleteKey(p->afp_v.v4_map_fd, keys[0]);
2467 keys[1]->src = p->
dst.addr_data32[0];
2468 keys[1]->dst = p->
src.addr_data32[0];
2469 keys[1]->port16[0] = htons(p->
dp);
2470 keys[1]->port16[1] = htons(p->
sp);
2471 keys[1]->vlan0 = p->
vlan_id[0];
2472 keys[1]->vlan1 = p->
vlan_id[1];
2473 keys[1]->vlan2 = p->
vlan_id[2];
2474 keys[1]->ip_proto = keys[0]->ip_proto;
2475 if (AFPInsertHalfFlow(p->afp_v.v4_map_fd, keys[1],
2476 p->afp_v.nr_cpus) == 0) {
2477 EBPFDeleteKey(p->afp_v.v4_map_fd, keys[0]);
2483 return AFPSetFlowStorage(p, p->afp_v.v4_map_fd, keys[0], keys[1], AF_INET);
2486 if (PacketIsIPv6(p) && ((p->
proto == IPPROTO_TCP) || (p->
proto == IPPROTO_UDP))) {
2488 if (p->afp_v.v6_map_fd == -1) {
2492 struct flowv6_keys *keys[2];
2493 keys[0] =
SCCalloc(1,
sizeof(
struct flowv6_keys));
2494 if (keys[0] == NULL) {
2498 for (i = 0; i < 4; i++) {
2502 keys[0]->port16[0] = htons(p->
sp);
2503 keys[0]->port16[1] = htons(p->
dp);
2504 keys[0]->vlan0 = p->
vlan_id[0];
2505 keys[0]->vlan1 = p->
vlan_id[1];
2506 keys[0]->vlan2 = p->
vlan_id[2];
2507 if (p->
proto == IPPROTO_TCP) {
2508 keys[0]->ip_proto = 1;
2510 keys[0]->ip_proto = 0;
2512 if (AFPInsertHalfFlow(p->afp_v.v6_map_fd, keys[0],
2513 p->afp_v.nr_cpus) == 0) {
2518 keys[1]=
SCCalloc(1,
sizeof(
struct flowv6_keys));
2519 if (keys[1] == NULL) {
2520 EBPFDeleteKey(p->afp_v.v6_map_fd, keys[0]);
2525 for (i = 0; i < 4; i++) {
2529 keys[1]->port16[0] = htons(p->
dp);
2530 keys[1]->port16[1] = htons(p->
sp);
2531 keys[1]->vlan0 = p->
vlan_id[0];
2532 keys[1]->vlan1 = p->
vlan_id[1];
2533 keys[1]->vlan2 = p->
vlan_id[2];
2534 keys[1]->ip_proto = keys[0]->ip_proto;
2535 if (AFPInsertHalfFlow(p->afp_v.v6_map_fd, keys[1],
2536 p->afp_v.nr_cpus) == 0) {
2537 EBPFDeleteKey(p->afp_v.v6_map_fd, keys[0]);
2543 return AFPSetFlowStorage(p, p->afp_v.v6_map_fd, keys[0], keys[1], AF_INET6);
2548 bool g_flowv4_ok =
true;
2549 bool g_flowv6_ok =
true;
2567 if (initdata == NULL) {
2601 #ifdef HAVE_PACKET_FANOUT
2616 #ifdef HAVE_PACKET_EBPF
2619 ptv->xdp_mode = afpconfig->
xdp_mode;
2623 ptv->v4_map_fd = EBPFGetMapFDByName(ptv->
iface,
"flow_table_v4");
2624 if (ptv->v4_map_fd == -1) {
2626 SCLogError(
"Can't find eBPF map fd for '%s'",
"flow_table_v4");
2630 ptv->v6_map_fd = EBPFGetMapFDByName(ptv->
iface,
"flow_table_v6");
2631 if (ptv->v6_map_fd == -1) {
2633 SCLogError(
"Can't find eBPF map fd for '%s'",
"flow_table_v6");
2634 g_flowv6_ok =
false;
2638 ptv->ebpf_t_config = afpconfig->ebpf_t_config;
2641 #ifdef PACKET_STATISTICS
2667 SCLogWarning(
"Enabling a BPF filter in IPS mode result"
2668 " in dropping all non matching packets.");
2679 *data = (
void *)ptv;
2698 void ReceiveAFPThreadExitStats(
ThreadVars *
tv,
void *data)
2703 #ifdef PACKET_STATISTICS
2704 AFPDumpCounters(ptv);
2722 #ifdef HAVE_PACKET_XDP
2723 if ((ptv->ebpf_t_config.
flags & EBPF_XDP_CODE) &&
2724 (!(ptv->ebpf_t_config.
flags & EBPF_PINNED_MAPS))) {
2725 EBPFSetupXDP(ptv->
iface, -1, ptv->xdp_mode);
2751 static void UpdateRawDataForVLANHdr(
Packet *p)
2753 if (p->afp_v.vlan_tci != 0) {
2759 *(uint16_t *)(pstart + 2 * ETH_ALEN) = htons(0x8100);
2760 *(uint16_t *)(pstart + 2 * ETH_ALEN + 2) = htons(p->afp_v.vlan_tci);
2783 const bool afp_vlan_hdr = p->
vlan_idx != 0;
2796 UpdateRawDataForVLANHdr(p);
2813 *data = (
void *)
dtv;