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
534 if (out_iface == NULL)
536 if (iface->
mtu != out_iface->
mtu) {
537 SCLogWarning(
"MTU on %s (%d) and %s (%d) are not equal, transmission of packets "
538 "bigger than %d will fail.",
552 static int AFPPeersListWaitTurn(
AFPPeer *peer)
563 static void AFPPeersListReachedInc(
void)
577 static int AFPPeersListStarted(
void)
614 static int AFPCreateSocket(
AFPThreadVars *ptv,
char *devname,
int verbose,
const bool peer_update);
618 #ifdef PACKET_STATISTICS
619 struct tpacket_stats kstats;
620 socklen_t
len =
sizeof (
struct tpacket_stats);
621 if (getsockopt(ptv->
socket, SOL_PACKET, PACKET_STATISTICS,
622 &kstats, &
len) > -1) {
623 SCLogDebug(
"(%s) Kernel: Packets %" PRIu32
", dropped %" PRIu32
"",
625 kstats.tp_packets, kstats.tp_drops);
654 struct sockaddr_ll socket_address;
663 if (!PacketIsEthernet(p)) {
668 const EthernetHdr *ethh = PacketGetEthernet(p);
670 socket_address.sll_ifindex =
SC_ATOMIC_GET(p->afp_v.peer->if_idx);
672 socket_address.sll_halen = ETH_ALEN;
674 memcpy(socket_address.sll_addr, ethh, 6);
682 sizeof(
struct sockaddr_ll)) < 0) {
684 SCLogWarning(
"%s: sending packet failed on socket %d: %s", p->afp_v.peer->iface, socket,
692 static void AFPReleaseDataFromRing(
Packet *p)
699 AFPWritePacket(p, TPACKET_V2);
702 BUG_ON(p->afp_v.relptr == NULL);
705 h.
raw = p->afp_v.relptr;
708 (void)AFPDerefSocket(p->afp_v.mpeer);
713 static void AFPReleasePacketV3(
Packet *p)
720 AFPWritePacket(p, TPACKET_V3);
725 static void AFPReleasePacket(
Packet *p)
727 AFPReleaseDataFromRing(p);
746 #ifdef HAVE_PACKET_EBPF
749 p->afp_v.v4_map_fd = ptv->v4_map_fd;
750 p->afp_v.v6_map_fd = ptv->v6_map_fd;
751 p->afp_v.nr_cpus = ptv->ebpf_t_config.cpus_count;
755 p->afp_v.v4_map_fd = ptv->v4_map_fd;
756 p->afp_v.v6_map_fd = ptv->v6_map_fd;
757 p->afp_v.nr_cpus = ptv->ebpf_t_config.cpus_count;
765 static void AFPReadFromRingSetupPacket(
778 AFPReadApplyBypass(ptv, p);
780 if (h.
h2->tp_len > h.
h2->tp_snaplen) {
781 SCLogDebug(
"Packet length (%d) > snaplen (%d), truncating", h.
h2->tp_len, h.
h2->tp_snaplen);
788 p->
vlan_id[0] = h.
h2->tp_vlan_tci & 0x0fff;
790 p->afp_v.vlan_tci = h.
h2->tp_vlan_tci;
796 p->afp_v.relptr = h.
raw;
798 p->afp_v.mpeer = ptv->
mpeer;
799 AFPRefSocket(ptv->
mpeer);
801 p->afp_v.mpeer = NULL;
831 static inline int AFPReadFromRingWaitForPacket(
AFPThreadVars *ptv)
834 struct timeval start_time;
835 gettimeofday(&start_time, NULL);
836 int64_t busy_loop_iter = 0;
847 const unsigned int tp_status = h.
h2->tp_status;
851 struct timeval cur_time;
852 memset(&cur_time, 0,
sizeof(cur_time));
853 uint64_t milliseconds =
854 ((cur_time.tv_sec - start_time.tv_sec) * 1000) +
855 (((1000000 + cur_time.tv_usec - start_time.tv_usec) / 1000) - 1000);
856 if (milliseconds > 1000) {
863 if (busy_loop_iter) {
880 static inline bool AFPShouldIgnoreFrame(
AFPThreadVars *ptv,
const struct sockaddr_ll *sll)
882 if (
unlikely(sll->sll_pkttype > 31))
900 bool emergency_flush =
false;
905 if (AFPReadFromRingWaitForPacket(ptv) !=
AFP_READ_OK)
917 const unsigned int tp_status = h.
h2->tp_status;
933 const struct sockaddr_ll *sll =
934 (
const struct sockaddr_ll *)((uint8_t *)h.
h2 +
935 TPACKET_ALIGN(
sizeof(
struct tpacket2_hdr)));
936 if (
unlikely(AFPShouldIgnoreFrame(ptv, sll)))
941 return AFPSuriFailure(ptv, h);
943 AFPReadFromRingSetupPacket(ptv, h, tp_status, p);
946 return AFPSuriFailure(ptv, h);
956 if (emergency_flush) {
957 AFPDumpCounters(ptv);
962 static inline void AFPFlushBlock(
struct tpacket_block_desc *pbd)
967 static inline int AFPParsePacketV3(
AFPThreadVars *ptv,
struct tpacket_block_desc *pbd,
struct tpacket3_hdr *ppd)
975 AFPReadApplyBypass(ptv, p);
983 p->
vlan_id[0] = ppd->hv1.tp_vlan_tci & 0x0fff;
985 p->afp_v.vlan_tci = (uint16_t)ppd->hv1.tp_vlan_tci;
988 if (ppd->tp_len > ppd->tp_snaplen) {
989 SCLogDebug(
"Packet length (%d) > snaplen (%d), truncating", ppd->tp_len, ppd->tp_snaplen);
993 (void)
PacketSetData(p, (
unsigned char *)ppd + ppd->tp_mac, ppd->tp_snaplen);
996 p->afp_v.relptr = NULL;
997 p->afp_v.mpeer = NULL;
1002 p->
ts = (
SCTime_t){ .
secs = ppd->tp_sec, .usecs = ppd->tp_nsec / 1000 };
1003 SCLogDebug(
"pktlen: %" PRIu32
" (pkt %p, pkt data %p)",
1029 static inline int AFPWalkBlock(
AFPThreadVars *ptv,
struct tpacket_block_desc *pbd)
1031 const int num_pkts = pbd->hdr.bh1.num_pkts;
1032 uint8_t *ppd = (uint8_t *)pbd + pbd->hdr.bh1.offset_to_first_pkt;
1034 for (
int i = 0; i < num_pkts; ++i) {
1035 const struct sockaddr_ll *sll =
1036 (
const struct sockaddr_ll *)(ppd + TPACKET_ALIGN(
sizeof(
struct tpacket3_hdr)));
1037 if (
unlikely(AFPShouldIgnoreFrame(ptv, sll))) {
1038 ppd = ppd + ((
struct tpacket3_hdr *)ppd)->tp_next_offset;
1041 int ret = AFPParsePacketV3(ptv, pbd, (
struct tpacket3_hdr *)ppd);
1054 ppd = ppd + ((
struct tpacket3_hdr *)ppd)->tp_next_offset;
1078 struct tpacket_block_desc *pbd =
1086 int ret = AFPWalkBlock(ptv, pbd);
1107 static int AFPRefSocket(
AFPPeer* peer)
1122 static int AFPDerefSocket(
AFPPeer* peer)
1135 if (ptv->
mpeer != NULL)
1158 static void AFPSwitchState(
AFPThreadVars *ptv, uint8_t state)
1175 static int AFPReadAndDiscardFromRing(
AFPThreadVars *ptv,
struct timeval *synctv,
1176 uint64_t *discarded_pkts)
1184 struct tpacket_block_desc *pbd =
1186 *discarded_pkts += pbd->hdr.bh1.num_pkts;
1187 struct tpacket3_hdr *ppd =
1188 (
struct tpacket3_hdr *)((uint8_t *)pbd + pbd->hdr.bh1.offset_to_first_pkt);
1189 if (((time_t)ppd->tp_sec > synctv->tv_sec) ||
1190 ((time_t)ppd->tp_sec == synctv->tv_sec &&
1191 (suseconds_t) (ppd->tp_nsec / 1000) > (suseconds_t)synctv->tv_usec)) {
1202 if (h.
raw == NULL) {
1208 if (((time_t)h.
h2->tp_sec > synctv->tv_sec) ||
1209 ((time_t)h.
h2->tp_sec == synctv->tv_sec &&
1210 (suseconds_t) (h.
h2->tp_nsec / 1000) > synctv->tv_usec)) {
1214 (*discarded_pkts)++;
1233 static int AFPSynchronizeStart(
AFPThreadVars *ptv, uint64_t *discarded_pkts)
1235 struct timeval synctv;
1239 fds.events = POLLIN;
1242 synctv.tv_sec = 0xffffffff;
1243 synctv.tv_usec = 0xffffffff;
1248 (fds.revents & (POLLHUP|POLLRDHUP|POLLERR|POLLNVAL))) {
1250 fds.revents & (POLLHUP | POLLRDHUP | POLLERR | POLLNVAL));
1253 if (AFPPeersListStarted() && synctv.tv_sec == (time_t) 0xffffffff) {
1254 gettimeofday(&synctv, NULL);
1256 r = AFPReadAndDiscardFromRing(ptv, &synctv, discarded_pkts);
1266 }
else if (r == 0 && AFPPeersListStarted()) {
1293 AFPCloseSocket(ptv);
1295 int afp_activate_r = AFPCreateSocket(ptv, ptv->
iface, 0,
false);
1296 if (afp_activate_r != 0) {
1300 return afp_activate_r;
1318 time_t last_dump = 0;
1319 time_t current_time;
1321 uint64_t discarded_pkts = 0;
1326 AFPReadFunc = AFPReadFromRingV3;
1328 AFPReadFunc = AFPReadFromRing;
1333 while (AFPPeersListWaitTurn(ptv->
mpeer)) {
1339 r = AFPCreateSocket(ptv, ptv->
iface, 1,
true);
1347 "%s: failed to init socket for interface, retrying soon", ptv->
iface);
1353 AFPSynchronizeStart(ptv, &discarded_pkts);
1356 #ifdef PACKET_STATISTICS
1357 struct tpacket_stats kstats;
1358 socklen_t
len =
sizeof (
struct tpacket_stats);
1359 if (getsockopt(ptv->
socket, SOL_PACKET, PACKET_STATISTICS,
1360 &kstats, &
len) > -1) {
1362 SCLogDebug(
"(%s) Kernel socket startup: Packets %" PRIu32
1363 ", dropped %" PRIu32
"",
1365 kstats.tp_packets, kstats.tp_drops);
1366 pkts = kstats.tp_packets - discarded_pkts - kstats.tp_drops;
1374 fds.events = POLLIN;
1391 r = AFPTryReopen(ptv);
1411 (fds.revents & (POLLHUP|POLLRDHUP|POLLERR|POLLNVAL))) {
1413 if (fds.revents & (POLLHUP | POLLRDHUP)) {
1416 }
else if (fds.revents & POLLERR) {
1419 if (recv(ptv->
socket, &c,
sizeof c, MSG_PEEK) != -1)
1424 }
else if (fds.revents & POLLNVAL) {
1431 r = AFPReadFunc(ptv);
1435 current_time = time(NULL);
1436 if (current_time != last_dump) {
1437 AFPDumpCounters(ptv);
1438 last_dump = current_time;
1450 AFPDumpCounters(ptv);
1456 current_time = time(NULL);
1457 if (current_time != last_dump) {
1458 AFPDumpCounters(ptv);
1459 last_dump = current_time;
1462 TmThreadsCaptureHandleTimeout(
tv, NULL);
1464 }
else if ((r < 0) && (errno != EINTR)) {
1473 AFPDumpCounters(ptv);
1478 static int AFPGetDevFlags(
int fd,
const char *ifname)
1482 memset(&ifr, 0,
sizeof(ifr));
1483 strlcpy(ifr.ifr_name, ifname,
sizeof(ifr.ifr_name));
1485 if (ioctl(fd, SIOCGIFFLAGS, &ifr) == -1) {
1486 SCLogError(
"%s: failed to get interface flags: %s", ifname, strerror(errno));
1490 return ifr.ifr_flags;
1494 static int AFPGetIfnumByDev(
int fd,
const char *ifname,
int verbose)
1498 memset(&ifr, 0,
sizeof(ifr));
1499 strlcpy(ifr.ifr_name, ifname,
sizeof(ifr.ifr_name));
1501 if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
1503 SCLogError(
"%s: failed to find interface: %s", ifname, strerror(errno));
1507 return ifr.ifr_ifindex;
1510 static int AFPGetDevLinktype(
int fd,
const char *ifname)
1514 memset(&ifr, 0,
sizeof(ifr));
1515 strlcpy(ifr.ifr_name, ifname,
sizeof(ifr.ifr_name));
1517 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) {
1518 SCLogError(
"%s: failed to find interface type: %s", ifname, strerror(errno));
1522 switch (ifr.ifr_hwaddr.sa_family) {
1523 case ARPHRD_LOOPBACK:
1529 return ifr.ifr_hwaddr.sa_family;
1537 int fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1539 SCLogError(
"%s: failed to create AF_PACKET socket: %s", ifname, strerror(errno));
1543 ltype = AFPGetDevLinktype(fd, ifname);
1551 static int AFPComputeRingParams(
AFPThreadVars *ptv,
int order)
1577 int tp_hdrlen =
sizeof(
struct tpacket_hdr);
1588 SCLogWarning(
"%s: unable to get MTU, setting snaplen default of 1514", ptv->
iface);
1595 ptv->
req.
v2.tp_frame_size = TPACKET_ALIGN(snaplen +TPACKET_ALIGN(TPACKET_ALIGN(tp_hdrlen) +
sizeof(
struct sockaddr_ll) + ETH_HLEN) - ETH_HLEN);
1596 ptv->
req.
v2.tp_block_size = getpagesize() << order;
1597 int frames_per_block = ptv->
req.
v2.tp_block_size / ptv->
req.
v2.tp_frame_size;
1598 if (frames_per_block == 0) {
1603 ptv->
req.
v2.tp_block_nr = ptv->
req.
v2.tp_frame_nr / frames_per_block + 1;
1605 ptv->
req.
v2.tp_frame_nr = ptv->
req.
v2.tp_block_nr * frames_per_block;
1606 SCLogPerf(
"%s: rx ring: block_size=%d block_nr=%d frame_size=%d frame_nr=%d", ptv->
iface,
1607 ptv->
req.
v2.tp_block_size, ptv->
req.
v2.tp_block_nr, ptv->
req.
v2.tp_frame_size,
1608 ptv->
req.
v2.tp_frame_nr);
1612 static int AFPComputeRingParamsWithBlockSize(
AFPThreadVars *ptv,
unsigned int block_size)
1638 int tp_hdrlen =
sizeof(
struct tpacket_hdr);
1649 SCLogWarning(
"%s: unable to get MTU, setting snaplen default of 1514", ptv->
iface);
1656 ptv->
req.
v2.tp_frame_size = TPACKET_ALIGN(
1658 TPACKET_ALIGN(TPACKET_ALIGN(tp_hdrlen) +
sizeof(
struct sockaddr_ll) + ETH_HLEN) -
1660 ptv->
req.
v2.tp_block_size = block_size;
1661 int frames_per_block = ptv->
req.
v2.tp_block_size / ptv->
req.
v2.tp_frame_size;
1662 if (frames_per_block == 0) {
1667 ptv->
req.
v2.tp_block_nr = ptv->
req.
v2.tp_frame_nr / frames_per_block + 1;
1669 ptv->
req.
v2.tp_frame_nr = ptv->
req.
v2.tp_block_nr * frames_per_block;
1670 SCLogPerf(
"%s: rx ring: block_size=%d block_nr=%d frame_size=%d frame_nr=%d", ptv->
iface,
1671 ptv->
req.
v2.tp_block_size, ptv->
req.
v2.tp_block_nr, ptv->
req.
v2.tp_frame_size,
1672 ptv->
req.
v2.tp_frame_nr);
1679 ptv->
req.
v3.tp_frame_size = 2048;
1680 int frames_per_block = 0;
1681 int tp_hdrlen =
sizeof(
struct tpacket3_hdr);
1687 SCLogWarning(
"%s: unable to get MTU, setting snaplen default of 1514", ptv->
iface);
1693 ptv->
req.
v3.tp_frame_size = TPACKET_ALIGN(snaplen +TPACKET_ALIGN(TPACKET_ALIGN(tp_hdrlen) +
sizeof(
struct sockaddr_ll) + ETH_HLEN) - ETH_HLEN);
1694 frames_per_block = ptv->
req.
v3.tp_block_size / ptv->
req.
v3.tp_frame_size;
1696 if (frames_per_block == 0) {
1697 SCLogError(
"%s: block size is too small, it should be at least %d", ptv->
iface,
1698 ptv->
req.
v3.tp_frame_size);
1703 ptv->
req.
v3.tp_frame_nr = ptv->
req.
v3.tp_block_nr * frames_per_block;
1705 ptv->
req.
v3.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH;
1706 SCLogPerf(
"%s: rx ring params: block_size=%d block_nr=%d frame_size=%d frame_nr=%d (mem: %d)",
1708 ptv->
req.
v3.tp_frame_size, ptv->
req.
v3.tp_frame_nr,
1709 ptv->
req.
v3.tp_block_size * ptv->
req.
v3.tp_block_nr);
1716 unsigned int len =
sizeof(val), i;
1725 if (getsockopt(ptv->
socket, SOL_PACKET, PACKET_HDRLEN, &val, &
len) < 0) {
1726 if (errno == ENOPROTOOPT) {
1728 SCLogError(
"%s: kernel too old for TPACKET_V3 (need 3.2+)", devname);
1730 SCLogError(
"%s: kernel too old (need 2.6.27+)", devname);
1733 SCLogError(
"%s: failed to retrieve packet header len", devname);
1741 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_VERSION, &val,
1743 SCLogError(
"%s: failed to activate TPACKET_V2/TPACKET_V3 on packet socket: %s", devname,
1748 #ifdef HAVE_HW_TIMESTAMPING
1750 int req = SOF_TIMESTAMPING_RAW_HARDWARE;
1751 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_TIMESTAMP, (
void *)&req,
sizeof(req)) < 0) {
1752 SCLogWarning(
"%s: failed to activate hardware timestamping on packet socket: %s",
1753 devname, strerror(errno));
1755 SCLogConfig(
"%s: hardware timestamping enabled", devname);
1758 SCLogConfig(
"%s: hardware timestamping disabled", devname);
1765 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_RESERVE, (
void *)&reserve,
sizeof(reserve)) <
1767 SCLogError(
"%s: failed to activate reserve on packet socket: %s", devname, strerror(errno));
1773 if (AFPComputeRingParamsV3(ptv) != 1) {
1776 r = setsockopt(ptv->
socket, SOL_PACKET, PACKET_RX_RING,
1779 SCLogError(
"%s: failed to allocate RX Ring: %s", devname, strerror(errno));
1785 if (AFPComputeRingParamsWithBlockSize(ptv, ptv->
v2_block_size) != 1) {
1786 SCLogError(
"%s: ring parameters are incorrect. Please file a bug report", devname);
1791 ptv->
socket, SOL_PACKET, PACKET_RX_RING, (
void *)&ptv->
req,
sizeof(ptv->
req));
1794 if (errno == ENOMEM) {
1795 SCLogError(
"%s: memory issue with ring parameters", devname);
1798 SCLogError(
"%s: failed to setup RX Ring: %s", devname, strerror(errno));
1804 if (AFPComputeRingParams(ptv, order) != 1) {
1806 "%s: ring parameters are incorrect. Please file a bug report", devname);
1810 r = setsockopt(ptv->
socket, SOL_PACKET, PACKET_RX_RING, (
void *)&ptv->
req,
1814 if (errno == ENOMEM) {
1815 SCLogWarning(
"%s: memory issue with ring parameters. Retrying", devname);
1818 SCLogError(
"%s: failed to setup RX Ring: %s", devname, strerror(errno));
1825 SCLogError(
"%s: failed to setup RX Ring (order 0 failed)", devname);
1837 mmap_flag = MAP_SHARED;
1839 mmap_flag |= MAP_LOCKED;
1841 mmap_flag, ptv->
socket, 0);
1843 SCLogError(
"%s: failed to mmap: %s", devname, strerror(errno));
1849 SCLogError(
"%s: failed to alloc ring: %s", devname, strerror(errno));
1852 for (i = 0; i < ptv->
req.
v3.tp_block_nr; ++i) {
1854 ptv->
ring.
v3[i].iov_len = ptv->
req.
v3.tp_block_size;
1859 if (ptv->
ring.
v2 == NULL) {
1860 SCLogError(
"%s: failed to alloc ring: %s", devname, strerror(errno));
1865 for (i = 0; i < ptv->
req.
v2.tp_block_nr; ++i) {
1866 void *base = &(ptv->
ring_buf[i * ptv->
req.
v2.tp_block_size]);
1870 base += ptv->
req.
v2.tp_frame_size;
1894 #ifdef HAVE_PACKET_FANOUT
1895 int fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1900 uint32_t option = (mode << 16) | cluster_id;
1901 int r = setsockopt(fd, SOL_PACKET,
PACKET_FANOUT,(
void *)&option,
sizeof(option));
1905 SCLogError(
"fanout not supported by kernel: "
1906 "Kernel too old or cluster-id %d already in use.",
1916 #ifdef HAVE_PACKET_EBPF
1920 int pfd = ptv->ebpf_lb_fd;
1922 SCLogError(
"Fanout file descriptor is invalid");
1926 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_FANOUT_DATA, &pfd,
sizeof(pfd))) {
1937 int pfd = ptv->ebpf_filter_fd;
1939 SCLogError(
"Filter file descriptor is invalid");
1943 if (setsockopt(ptv->
socket, SOL_SOCKET, SO_ATTACH_BPF, &pfd,
sizeof(pfd))) {
1944 SCLogError(
"Error setting ebpf: %s", strerror(errno));
1947 SCLogInfo(
"Activated eBPF filter on socket");
1954 static int AFPCreateSocket(
AFPThreadVars *ptv,
char *devname,
int verbose,
const bool peer_update)
1958 struct packet_mreq sock_params;
1959 struct sockaddr_ll bind_address;
1963 ptv->
socket = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1965 SCLogError(
"%s: failed to create socket: %s", devname, strerror(errno));
1969 if_idx = AFPGetIfnumByDev(ptv->
socket, devname, verbose);
1975 memset(&bind_address, 0,
sizeof(bind_address));
1976 bind_address.sll_family = AF_PACKET;
1977 bind_address.sll_protocol = htons(ETH_P_ALL);
1978 bind_address.sll_ifindex = if_idx;
1979 if (bind_address.sll_ifindex == -1) {
1986 int if_flags = AFPGetDevFlags(ptv->
socket, ptv->
iface);
1987 if (if_flags == -1) {
1993 }
else if ((if_flags & (IFF_UP | IFF_RUNNING)) == 0) {
2002 if (if_flags & IFF_LOOPBACK)
2007 memset(&sock_params, 0,
sizeof(sock_params));
2008 sock_params.mr_type = PACKET_MR_PROMISC;
2009 sock_params.mr_ifindex = bind_address.sll_ifindex;
2010 r = setsockopt(ptv->
socket, SOL_PACKET, PACKET_ADD_MEMBERSHIP,(
void *)&sock_params,
sizeof(sock_params));
2012 SCLogError(
"%s: failed to set promisc mode: %s", devname, strerror(errno));
2019 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_AUXDATA, &val,
2020 sizeof(val)) == -1 && errno != ENOPROTOOPT) {
2022 "%s: 'kernel' checksum mode not supported, falling back to full mode", devname);
2033 if (setsockopt(ptv->
socket, SOL_SOCKET, SO_RCVBUF,
2042 r = bind(ptv->
socket, (
struct sockaddr *)&bind_address,
sizeof(bind_address));
2045 if (errno == ENETDOWN) {
2046 SCLogWarning(
"%s: failed to bind socket, iface is down", devname);
2048 SCLogWarning(
"%s: failed to bind socket: %s", devname, strerror(errno));
2056 #ifdef HAVE_PACKET_FANOUT
2061 uint32_t option = (mode << 16) | (
id & 0xffff);
2064 SCLogError(
"%s: failed to set fanout mode: %s", devname, strerror(errno));
2070 #ifdef HAVE_PACKET_EBPF
2072 r = SockFanoutSeteBPF(ptv);
2074 SCLogError(
"%s: failed to set eBPF: %s", devname, strerror(errno));
2081 AFPPeersListReachedInc();
2083 ret = AFPSetupRing(ptv, devname);
2087 SCLogDebug(
"Using interface '%s' via socket %d", (
char *)devname, ptv->
socket);
2091 TmEcode rc = AFPSetBPFFilter(ptv);
2122 struct bpf_program filter;
2123 struct sock_fprog fcode;
2126 #ifdef HAVE_PACKET_EBPF
2127 if (ptv->ebpf_filter_fd != -1) {
2128 return SetEbpfFilter(ptv);
2137 char errbuf[PCAP_ERRBUF_SIZE];
2144 errbuf,
sizeof(errbuf)) == -1) {
2149 if (filter.bf_len > USHRT_MAX) {
2152 fcode.len = (
unsigned short)filter.bf_len;
2153 fcode.filter = (
struct sock_filter*)filter.bf_insns;
2155 rc = setsockopt(ptv->
socket, SOL_SOCKET, SO_ATTACH_FILTER, &fcode,
sizeof(fcode));
2159 SCLogError(
"%s: failed to attach filter: %s", ptv->
iface, strerror(errno));
2166 #ifdef HAVE_PACKET_EBPF
2174 static int AFPInsertHalfFlow(
int mapd,
void *key,
unsigned int nr_cpus)
2176 BPF_DECLARE_PERCPU(
struct pair, value, nr_cpus);
2185 for (i = 0; i < nr_cpus; i++) {
2186 BPF_PERCPU(value, i).packets = 0;
2187 BPF_PERCPU(value, i).bytes = 0;
2189 if (bpf_map_update_elem(mapd, key, value, BPF_NOEXIST) != 0) {
2202 SCLogError(
"Can't update eBPF map: %s (%d)", strerror(errno), errno);
2209 static int AFPSetFlowStorage(
Packet *p,
int map_fd,
void *key0,
void* key1,
2221 EBPFBypassData *eb =
SCCalloc(1,
sizeof(EBPFBypassData));
2223 EBPFDeleteKey(map_fd, key0);
2224 EBPFDeleteKey(map_fd, key1);
2233 eb->cpus_count = p->afp_v.nr_cpus;
2238 EBPFDeleteKey(map_fd, key0);
2239 EBPFDeleteKey(map_fd, key1);
2265 static int AFPBypassCallback(
Packet *p)
2267 SCLogDebug(
"Calling af_packet callback function");
2269 if (!(PacketIsTCP(p) || PacketIsUDP(p))) {
2276 if (p->
flow == NULL) {
2282 if (PacketIsTunnel(p)) {
2286 if (PacketIsIPv4(p)) {
2288 if (p->afp_v.v4_map_fd == -1) {
2291 struct flowv4_keys *keys[2];
2292 keys[0] =
SCCalloc(1,
sizeof(
struct flowv4_keys));
2293 if (keys[0] == NULL) {
2298 keys[0]->port16[0] = p->
sp;
2299 keys[0]->port16[1] = p->
dp;
2300 keys[0]->vlan0 = p->
vlan_id[0];
2301 keys[0]->vlan1 = p->
vlan_id[1];
2303 if (p->
proto == IPPROTO_TCP) {
2304 keys[0]->ip_proto = 1;
2306 keys[0]->ip_proto = 0;
2308 if (AFPInsertHalfFlow(p->afp_v.v4_map_fd, keys[0],
2309 p->afp_v.nr_cpus) == 0) {
2314 keys[1]=
SCCalloc(1,
sizeof(
struct flowv4_keys));
2315 if (keys[1] == NULL) {
2316 EBPFDeleteKey(p->afp_v.v4_map_fd, keys[0]);
2323 keys[1]->port16[0] = p->
dp;
2324 keys[1]->port16[1] = p->
sp;
2325 keys[1]->vlan0 = p->
vlan_id[0];
2326 keys[1]->vlan1 = p->
vlan_id[1];
2328 keys[1]->ip_proto = keys[0]->ip_proto;
2329 if (AFPInsertHalfFlow(p->afp_v.v4_map_fd, keys[1],
2330 p->afp_v.nr_cpus) == 0) {
2331 EBPFDeleteKey(p->afp_v.v4_map_fd, keys[0]);
2337 EBPFUpdateFlow(p->
flow, p, NULL);
2338 return AFPSetFlowStorage(p, p->afp_v.v4_map_fd, keys[0], keys[1], AF_INET);
2341 if (PacketIsIPv6(p) && ((p->
proto == IPPROTO_TCP) || (p->
proto == IPPROTO_UDP))) {
2343 if (p->afp_v.v6_map_fd == -1) {
2347 struct flowv6_keys *keys[2];
2348 keys[0] =
SCCalloc(1,
sizeof(
struct flowv6_keys));
2349 if (keys[0] == NULL) {
2353 for (i = 0; i < 4; i++) {
2357 keys[0]->port16[0] = p->
sp;
2358 keys[0]->port16[1] = p->
dp;
2359 keys[0]->vlan0 = p->
vlan_id[0];
2360 keys[0]->vlan1 = p->
vlan_id[1];
2362 if (p->
proto == IPPROTO_TCP) {
2363 keys[0]->ip_proto = 1;
2365 keys[0]->ip_proto = 0;
2367 if (AFPInsertHalfFlow(p->afp_v.v6_map_fd, keys[0],
2368 p->afp_v.nr_cpus) == 0) {
2373 keys[1]=
SCCalloc(1,
sizeof(
struct flowv6_keys));
2374 if (keys[1] == NULL) {
2375 EBPFDeleteKey(p->afp_v.v6_map_fd, keys[0]);
2380 for (i = 0; i < 4; i++) {
2384 keys[1]->port16[0] = p->
dp;
2385 keys[1]->port16[1] = p->
sp;
2386 keys[1]->vlan0 = p->
vlan_id[0];
2387 keys[1]->vlan1 = p->
vlan_id[1];
2389 keys[1]->ip_proto = keys[0]->ip_proto;
2390 if (AFPInsertHalfFlow(p->afp_v.v6_map_fd, keys[1],
2391 p->afp_v.nr_cpus) == 0) {
2392 EBPFDeleteKey(p->afp_v.v6_map_fd, keys[0]);
2399 EBPFUpdateFlow(p->
flow, p, NULL);
2400 return AFPSetFlowStorage(p, p->afp_v.v6_map_fd, keys[0], keys[1], AF_INET6);
2416 static int AFPXDPBypassCallback(
Packet *p)
2418 SCLogDebug(
"Calling af_packet callback function");
2420 if (!(PacketIsTCP(p) || PacketIsUDP(p))) {
2427 if (p->
flow == NULL) {
2433 if (PacketIsTunnel(p)) {
2437 if (PacketIsIPv4(p)) {
2438 struct flowv4_keys *keys[2];
2439 keys[0]=
SCCalloc(1,
sizeof(
struct flowv4_keys));
2440 if (keys[0] == NULL) {
2444 if (p->afp_v.v4_map_fd == -1) {
2448 keys[0]->src = p->
src.addr_data32[0];
2449 keys[0]->dst = p->
dst.addr_data32[0];
2452 keys[0]->port16[0] = htons(p->
sp);
2453 keys[0]->port16[1] = htons(p->
dp);
2454 keys[0]->vlan0 = p->
vlan_id[0];
2455 keys[0]->vlan1 = p->
vlan_id[1];
2456 if (p->
proto == IPPROTO_TCP) {
2457 keys[0]->ip_proto = 1;
2459 keys[0]->ip_proto = 0;
2461 if (AFPInsertHalfFlow(p->afp_v.v4_map_fd, keys[0],
2462 p->afp_v.nr_cpus) == 0) {
2467 keys[1]=
SCCalloc(1,
sizeof(
struct flowv4_keys));
2468 if (keys[1] == NULL) {
2469 EBPFDeleteKey(p->afp_v.v4_map_fd, keys[0]);
2474 keys[1]->src = p->
dst.addr_data32[0];
2475 keys[1]->dst = p->
src.addr_data32[0];
2476 keys[1]->port16[0] = htons(p->
dp);
2477 keys[1]->port16[1] = htons(p->
sp);
2478 keys[1]->vlan0 = p->
vlan_id[0];
2479 keys[1]->vlan1 = p->
vlan_id[1];
2480 keys[1]->ip_proto = keys[0]->ip_proto;
2481 if (AFPInsertHalfFlow(p->afp_v.v4_map_fd, keys[1],
2482 p->afp_v.nr_cpus) == 0) {
2483 EBPFDeleteKey(p->afp_v.v4_map_fd, keys[0]);
2489 return AFPSetFlowStorage(p, p->afp_v.v4_map_fd, keys[0], keys[1], AF_INET);
2492 if (PacketIsIPv6(p) && ((p->
proto == IPPROTO_TCP) || (p->
proto == IPPROTO_UDP))) {
2494 if (p->afp_v.v6_map_fd == -1) {
2498 struct flowv6_keys *keys[2];
2499 keys[0] =
SCCalloc(1,
sizeof(
struct flowv6_keys));
2500 if (keys[0] == NULL) {
2504 for (i = 0; i < 4; i++) {
2508 keys[0]->port16[0] = htons(p->
sp);
2509 keys[0]->port16[1] = htons(p->
dp);
2510 keys[0]->vlan0 = p->
vlan_id[0];
2511 keys[0]->vlan1 = p->
vlan_id[1];
2512 if (p->
proto == IPPROTO_TCP) {
2513 keys[0]->ip_proto = 1;
2515 keys[0]->ip_proto = 0;
2517 if (AFPInsertHalfFlow(p->afp_v.v6_map_fd, keys[0],
2518 p->afp_v.nr_cpus) == 0) {
2523 keys[1]=
SCCalloc(1,
sizeof(
struct flowv6_keys));
2524 if (keys[1] == NULL) {
2525 EBPFDeleteKey(p->afp_v.v6_map_fd, keys[0]);
2530 for (i = 0; i < 4; i++) {
2534 keys[1]->port16[0] = htons(p->
dp);
2535 keys[1]->port16[1] = htons(p->
sp);
2536 keys[1]->vlan0 = p->
vlan_id[0];
2537 keys[1]->vlan1 = p->
vlan_id[1];
2538 keys[1]->ip_proto = keys[0]->ip_proto;
2539 if (AFPInsertHalfFlow(p->afp_v.v6_map_fd, keys[1],
2540 p->afp_v.nr_cpus) == 0) {
2541 EBPFDeleteKey(p->afp_v.v6_map_fd, keys[0]);
2547 return AFPSetFlowStorage(p, p->afp_v.v6_map_fd, keys[0], keys[1], AF_INET6);
2552 bool g_flowv4_ok =
true;
2553 bool g_flowv6_ok =
true;
2571 if (initdata == NULL) {
2605 #ifdef HAVE_PACKET_FANOUT
2620 #ifdef HAVE_PACKET_EBPF
2623 ptv->xdp_mode = afpconfig->
xdp_mode;
2627 ptv->v4_map_fd = EBPFGetMapFDByName(ptv->
iface,
"flow_table_v4");
2628 if (ptv->v4_map_fd == -1) {
2630 SCLogError(
"Can't find eBPF map fd for '%s'",
"flow_table_v4");
2634 ptv->v6_map_fd = EBPFGetMapFDByName(ptv->
iface,
"flow_table_v6");
2635 if (ptv->v6_map_fd == -1) {
2637 SCLogError(
"Can't find eBPF map fd for '%s'",
"flow_table_v6");
2638 g_flowv6_ok =
false;
2642 ptv->ebpf_t_config = afpconfig->ebpf_t_config;
2645 #ifdef PACKET_STATISTICS
2671 SCLogWarning(
"Enabling a BPF filter in IPS mode result"
2672 " in dropping all non matching packets.");
2683 *data = (
void *)ptv;
2702 void ReceiveAFPThreadExitStats(
ThreadVars *
tv,
void *data)
2707 #ifdef PACKET_STATISTICS
2708 AFPDumpCounters(ptv);
2726 #ifdef HAVE_PACKET_XDP
2727 if ((ptv->ebpf_t_config.
flags & EBPF_XDP_CODE) &&
2728 (!(ptv->ebpf_t_config.
flags & EBPF_PINNED_MAPS))) {
2729 EBPFSetupXDP(ptv->
iface, -1, ptv->xdp_mode);
2755 static void UpdateRawDataForVLANHdr(
Packet *p)
2757 if (p->afp_v.vlan_tci != 0) {
2763 *(uint16_t *)(pstart + 2 * ETH_ALEN) = htons(0x8100);
2764 *(uint16_t *)(pstart + 2 * ETH_ALEN + 2) = htons(p->afp_v.vlan_tci);
2787 const bool afp_vlan_hdr = p->
vlan_idx != 0;
2800 UpdateRawDataForVLANHdr(p);
2817 *data = (
void *)
dtv;