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);
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);
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);
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);
1347 "%s: failed to init socket for interface, retrying soon", ptv->
iface);
1350 AFPPeersListReachedInc();
1354 AFPSynchronizeStart(ptv, &discarded_pkts);
1357 #ifdef PACKET_STATISTICS
1358 struct tpacket_stats kstats;
1359 socklen_t
len =
sizeof (
struct tpacket_stats);
1360 if (getsockopt(ptv->
socket, SOL_PACKET, PACKET_STATISTICS,
1361 &kstats, &
len) > -1) {
1363 SCLogDebug(
"(%s) Kernel socket startup: Packets %" PRIu32
1364 ", dropped %" PRIu32
"",
1366 kstats.tp_packets, kstats.tp_drops);
1367 pkts = kstats.tp_packets - discarded_pkts - kstats.tp_drops;
1375 fds.events = POLLIN;
1392 r = AFPTryReopen(ptv);
1412 (fds.revents & (POLLHUP|POLLRDHUP|POLLERR|POLLNVAL))) {
1414 if (fds.revents & (POLLHUP | POLLRDHUP)) {
1417 }
else if (fds.revents & POLLERR) {
1420 if (recv(ptv->
socket, &c,
sizeof c, MSG_PEEK) != -1)
1425 }
else if (fds.revents & POLLNVAL) {
1432 r = AFPReadFunc(ptv);
1436 current_time = time(NULL);
1437 if (current_time != last_dump) {
1438 AFPDumpCounters(ptv);
1439 last_dump = current_time;
1451 AFPDumpCounters(ptv);
1457 current_time = time(NULL);
1458 if (current_time != last_dump) {
1459 AFPDumpCounters(ptv);
1460 last_dump = current_time;
1463 TmThreadsCaptureHandleTimeout(
tv, NULL);
1465 }
else if ((r < 0) && (errno != EINTR)) {
1474 AFPDumpCounters(ptv);
1479 static int AFPGetDevFlags(
int fd,
const char *ifname)
1483 memset(&ifr, 0,
sizeof(ifr));
1484 strlcpy(ifr.ifr_name, ifname,
sizeof(ifr.ifr_name));
1486 if (ioctl(fd, SIOCGIFFLAGS, &ifr) == -1) {
1487 SCLogError(
"%s: failed to get interface flags: %s", ifname, strerror(errno));
1491 return ifr.ifr_flags;
1495 static int AFPGetIfnumByDev(
int fd,
const char *ifname,
int verbose)
1499 memset(&ifr, 0,
sizeof(ifr));
1500 strlcpy(ifr.ifr_name, ifname,
sizeof(ifr.ifr_name));
1502 if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
1504 SCLogError(
"%s: failed to find interface: %s", ifname, strerror(errno));
1508 return ifr.ifr_ifindex;
1511 static int AFPGetDevLinktype(
int fd,
const char *ifname)
1515 memset(&ifr, 0,
sizeof(ifr));
1516 strlcpy(ifr.ifr_name, ifname,
sizeof(ifr.ifr_name));
1518 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) {
1519 SCLogError(
"%s: failed to find interface type: %s", ifname, strerror(errno));
1523 switch (ifr.ifr_hwaddr.sa_family) {
1524 case ARPHRD_LOOPBACK:
1530 return ifr.ifr_hwaddr.sa_family;
1538 int fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1540 SCLogError(
"%s: failed to create AF_PACKET socket: %s", ifname, strerror(errno));
1544 ltype = AFPGetDevLinktype(fd, ifname);
1552 static int AFPComputeRingParams(
AFPThreadVars *ptv,
int order)
1578 int tp_hdrlen =
sizeof(
struct tpacket_hdr);
1589 SCLogWarning(
"%s: unable to get MTU, setting snaplen default of 1514", ptv->
iface);
1596 ptv->
req.
v2.tp_frame_size = TPACKET_ALIGN(snaplen +TPACKET_ALIGN(TPACKET_ALIGN(tp_hdrlen) +
sizeof(
struct sockaddr_ll) + ETH_HLEN) - ETH_HLEN);
1597 ptv->
req.
v2.tp_block_size = getpagesize() << order;
1598 int frames_per_block = ptv->
req.
v2.tp_block_size / ptv->
req.
v2.tp_frame_size;
1599 if (frames_per_block == 0) {
1604 ptv->
req.
v2.tp_block_nr = ptv->
req.
v2.tp_frame_nr / frames_per_block + 1;
1606 ptv->
req.
v2.tp_frame_nr = ptv->
req.
v2.tp_block_nr * frames_per_block;
1607 SCLogPerf(
"%s: rx ring: block_size=%d block_nr=%d frame_size=%d frame_nr=%d", ptv->
iface,
1608 ptv->
req.
v2.tp_block_size, ptv->
req.
v2.tp_block_nr, ptv->
req.
v2.tp_frame_size,
1609 ptv->
req.
v2.tp_frame_nr);
1613 static int AFPComputeRingParamsWithBlockSize(
AFPThreadVars *ptv,
unsigned int block_size)
1639 int tp_hdrlen =
sizeof(
struct tpacket_hdr);
1650 SCLogWarning(
"%s: unable to get MTU, setting snaplen default of 1514", ptv->
iface);
1657 ptv->
req.
v2.tp_frame_size = TPACKET_ALIGN(
1659 TPACKET_ALIGN(TPACKET_ALIGN(tp_hdrlen) +
sizeof(
struct sockaddr_ll) + ETH_HLEN) -
1661 ptv->
req.
v2.tp_block_size = block_size;
1662 int frames_per_block = ptv->
req.
v2.tp_block_size / ptv->
req.
v2.tp_frame_size;
1663 if (frames_per_block == 0) {
1668 ptv->
req.
v2.tp_block_nr = ptv->
req.
v2.tp_frame_nr / frames_per_block + 1;
1670 ptv->
req.
v2.tp_frame_nr = ptv->
req.
v2.tp_block_nr * frames_per_block;
1671 SCLogPerf(
"%s: rx ring: block_size=%d block_nr=%d frame_size=%d frame_nr=%d", ptv->
iface,
1672 ptv->
req.
v2.tp_block_size, ptv->
req.
v2.tp_block_nr, ptv->
req.
v2.tp_frame_size,
1673 ptv->
req.
v2.tp_frame_nr);
1680 ptv->
req.
v3.tp_frame_size = 2048;
1681 int frames_per_block = 0;
1682 int tp_hdrlen =
sizeof(
struct tpacket3_hdr);
1688 SCLogWarning(
"%s: unable to get MTU, setting snaplen default of 1514", ptv->
iface);
1694 ptv->
req.
v3.tp_frame_size = TPACKET_ALIGN(snaplen +TPACKET_ALIGN(TPACKET_ALIGN(tp_hdrlen) +
sizeof(
struct sockaddr_ll) + ETH_HLEN) - ETH_HLEN);
1695 frames_per_block = ptv->
req.
v3.tp_block_size / ptv->
req.
v3.tp_frame_size;
1697 if (frames_per_block == 0) {
1698 SCLogError(
"%s: block size is too small, it should be at least %d", ptv->
iface,
1699 ptv->
req.
v3.tp_frame_size);
1704 ptv->
req.
v3.tp_frame_nr = ptv->
req.
v3.tp_block_nr * frames_per_block;
1706 ptv->
req.
v3.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH;
1707 SCLogPerf(
"%s: rx ring params: block_size=%d block_nr=%d frame_size=%d frame_nr=%d (mem: %d)",
1709 ptv->
req.
v3.tp_frame_size, ptv->
req.
v3.tp_frame_nr,
1710 ptv->
req.
v3.tp_block_size * ptv->
req.
v3.tp_block_nr);
1717 unsigned int len =
sizeof(val), i;
1726 if (getsockopt(ptv->
socket, SOL_PACKET, PACKET_HDRLEN, &val, &
len) < 0) {
1727 if (errno == ENOPROTOOPT) {
1729 SCLogError(
"%s: kernel too old for TPACKET_V3 (need 3.2+)", devname);
1731 SCLogError(
"%s: kernel too old (need 2.6.27+)", devname);
1734 SCLogError(
"%s: failed to retrieve packet header len", devname);
1742 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_VERSION, &val,
1744 SCLogError(
"%s: failed to activate TPACKET_V2/TPACKET_V3 on packet socket: %s", devname,
1749 #ifdef HAVE_HW_TIMESTAMPING
1751 int req = SOF_TIMESTAMPING_RAW_HARDWARE;
1752 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_TIMESTAMP, (
void *)&req,
sizeof(req)) < 0) {
1753 SCLogWarning(
"%s: failed to activate hardware timestamping on packet socket: %s",
1754 devname, strerror(errno));
1756 SCLogConfig(
"%s: hardware timestamping enabled", devname);
1759 SCLogConfig(
"%s: hardware timestamping disabled", devname);
1766 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_RESERVE, (
void *)&reserve,
sizeof(reserve)) <
1768 SCLogError(
"%s: failed to activate reserve on packet socket: %s", devname, strerror(errno));
1774 if (AFPComputeRingParamsV3(ptv) != 1) {
1777 r = setsockopt(ptv->
socket, SOL_PACKET, PACKET_RX_RING,
1780 SCLogError(
"%s: failed to allocate RX Ring: %s", devname, strerror(errno));
1786 if (AFPComputeRingParamsWithBlockSize(ptv, ptv->
v2_block_size) != 1) {
1787 SCLogError(
"%s: ring parameters are incorrect. Please file a bug report", devname);
1792 ptv->
socket, SOL_PACKET, PACKET_RX_RING, (
void *)&ptv->
req,
sizeof(ptv->
req));
1795 if (errno == ENOMEM) {
1796 SCLogError(
"%s: memory issue with ring parameters", devname);
1799 SCLogError(
"%s: failed to setup RX Ring: %s", devname, strerror(errno));
1805 if (AFPComputeRingParams(ptv, order) != 1) {
1807 "%s: ring parameters are incorrect. Please file a bug report", devname);
1811 r = setsockopt(ptv->
socket, SOL_PACKET, PACKET_RX_RING, (
void *)&ptv->
req,
1815 if (errno == ENOMEM) {
1816 SCLogWarning(
"%s: memory issue with ring parameters. Retrying", devname);
1819 SCLogError(
"%s: failed to setup RX Ring: %s", devname, strerror(errno));
1826 SCLogError(
"%s: failed to setup RX Ring (order 0 failed)", devname);
1838 mmap_flag = MAP_SHARED;
1840 mmap_flag |= MAP_LOCKED;
1842 mmap_flag, ptv->
socket, 0);
1844 SCLogError(
"%s: failed to mmap: %s", devname, strerror(errno));
1850 SCLogError(
"%s: failed to alloc ring: %s", devname, strerror(errno));
1853 for (i = 0; i < ptv->
req.
v3.tp_block_nr; ++i) {
1855 ptv->
ring.
v3[i].iov_len = ptv->
req.
v3.tp_block_size;
1860 if (ptv->
ring.
v2 == NULL) {
1861 SCLogError(
"%s: failed to alloc ring: %s", devname, strerror(errno));
1866 for (i = 0; i < ptv->
req.
v2.tp_block_nr; ++i) {
1867 void *base = &(ptv->
ring_buf[i * ptv->
req.
v2.tp_block_size]);
1871 base += ptv->
req.
v2.tp_frame_size;
1895 #ifdef HAVE_PACKET_FANOUT
1896 int fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1901 uint32_t option = (mode << 16) | cluster_id;
1902 int r = setsockopt(fd, SOL_PACKET,
PACKET_FANOUT,(
void *)&option,
sizeof(option));
1906 SCLogError(
"fanout not supported by kernel: "
1907 "Kernel too old or cluster-id %d already in use.",
1917 #ifdef HAVE_PACKET_EBPF
1921 int pfd = ptv->ebpf_lb_fd;
1923 SCLogError(
"Fanout file descriptor is invalid");
1927 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_FANOUT_DATA, &pfd,
sizeof(pfd))) {
1938 int pfd = ptv->ebpf_filter_fd;
1940 SCLogError(
"Filter file descriptor is invalid");
1944 if (setsockopt(ptv->
socket, SOL_SOCKET, SO_ATTACH_BPF, &pfd,
sizeof(pfd))) {
1945 SCLogError(
"Error setting ebpf: %s", strerror(errno));
1948 SCLogInfo(
"Activated eBPF filter on socket");
1954 static int AFPCreateSocket(
AFPThreadVars *ptv,
char *devname,
int verbose)
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));
2080 ret = AFPSetupRing(ptv, devname);
2084 SCLogDebug(
"Using interface '%s' via socket %d", (
char *)devname, ptv->
socket);
2088 TmEcode rc = AFPSetBPFFilter(ptv);
2119 struct bpf_program filter;
2120 struct sock_fprog fcode;
2123 #ifdef HAVE_PACKET_EBPF
2124 if (ptv->ebpf_filter_fd != -1) {
2125 return SetEbpfFilter(ptv);
2134 char errbuf[PCAP_ERRBUF_SIZE];
2141 errbuf,
sizeof(errbuf)) == -1) {
2146 if (filter.bf_len > USHRT_MAX) {
2149 fcode.len = (
unsigned short)filter.bf_len;
2150 fcode.filter = (
struct sock_filter*)filter.bf_insns;
2152 rc = setsockopt(ptv->
socket, SOL_SOCKET, SO_ATTACH_FILTER, &fcode,
sizeof(fcode));
2156 SCLogError(
"%s: failed to attach filter: %s", ptv->
iface, strerror(errno));
2163 #ifdef HAVE_PACKET_EBPF
2171 static int AFPInsertHalfFlow(
int mapd,
void *key,
unsigned int nr_cpus)
2173 BPF_DECLARE_PERCPU(
struct pair, value, nr_cpus);
2182 for (i = 0; i < nr_cpus; i++) {
2183 BPF_PERCPU(value, i).packets = 0;
2184 BPF_PERCPU(value, i).bytes = 0;
2186 if (bpf_map_update_elem(mapd, key, value, BPF_NOEXIST) != 0) {
2199 SCLogError(
"Can't update eBPF map: %s (%d)", strerror(errno), errno);
2206 static int AFPSetFlowStorage(
Packet *
p,
int map_fd,
void *key0,
void* key1,
2218 EBPFBypassData *eb =
SCCalloc(1,
sizeof(EBPFBypassData));
2220 EBPFDeleteKey(map_fd, key0);
2221 EBPFDeleteKey(map_fd, key1);
2230 eb->cpus_count =
p->afp_v.nr_cpus;
2235 EBPFDeleteKey(map_fd, key0);
2236 EBPFDeleteKey(map_fd, key1);
2248 #define FlowKeyIpFill(k, p) \
2250 (k)->ip_proto = ((p)->proto == IPPROTO_TCP) ? 1 : 0; \
2251 (k)->vlan0 = (p)->vlan_id[0]; \
2252 (k)->vlan1 = (p)->vlan_id[1]; \
2269 static int AFPBypassCallback(
Packet *
p)
2271 SCLogDebug(
"Calling af_packet callback function");
2273 if (!(PacketIsTCP(
p) || PacketIsUDP(
p))) {
2280 if (
p->
flow == NULL) {
2286 if (PacketIsTunnel(
p)) {
2290 if (PacketIsIPv4(
p)) {
2292 if (
p->afp_v.v4_map_fd == -1) {
2295 struct flowv4_keys *keys[2];
2296 keys[0] =
SCCalloc(1,
sizeof(
struct flowv4_keys));
2297 if (keys[0] == NULL) {
2302 FlowKeyIpFill(keys[0],
p);
2303 keys[0]->port16[0] =
p->
sp;
2304 keys[0]->port16[1] =
p->
dp;
2305 if (AFPInsertHalfFlow(
p->afp_v.v4_map_fd, keys[0],
2306 p->afp_v.nr_cpus) == 0) {
2311 keys[1]=
SCCalloc(1,
sizeof(
struct flowv4_keys));
2312 if (keys[1] == NULL) {
2313 EBPFDeleteKey(
p->afp_v.v4_map_fd, keys[0]);
2320 keys[1]->port16[0] =
p->
dp;
2321 keys[1]->port16[1] =
p->
sp;
2322 FlowKeyIpFill(keys[1],
p);
2323 if (AFPInsertHalfFlow(
p->afp_v.v4_map_fd, keys[1],
2324 p->afp_v.nr_cpus) == 0) {
2325 EBPFDeleteKey(
p->afp_v.v4_map_fd, keys[0]);
2331 EBPFUpdateFlow(
p->
flow,
p, NULL);
2332 return AFPSetFlowStorage(
p,
p->afp_v.v4_map_fd, keys[0], keys[1], AF_INET);
2335 if (PacketIsIPv6(
p) && ((
p->
proto == IPPROTO_TCP) || (
p->
proto == IPPROTO_UDP))) {
2337 if (
p->afp_v.v6_map_fd == -1) {
2341 struct flowv6_keys *keys[2];
2342 keys[0] =
SCCalloc(1,
sizeof(
struct flowv6_keys));
2343 if (keys[0] == NULL) {
2347 for (i = 0; i < 4; i++) {
2351 keys[0]->port16[0] =
p->
sp;
2352 keys[0]->port16[1] =
p->
dp;
2353 FlowKeyIpFill(keys[0],
p);
2354 if (AFPInsertHalfFlow(
p->afp_v.v6_map_fd, keys[0],
2355 p->afp_v.nr_cpus) == 0) {
2360 keys[1]=
SCCalloc(1,
sizeof(
struct flowv6_keys));
2361 if (keys[1] == NULL) {
2362 EBPFDeleteKey(
p->afp_v.v6_map_fd, keys[0]);
2367 for (i = 0; i < 4; i++) {
2371 keys[1]->port16[0] =
p->
dp;
2372 keys[1]->port16[1] =
p->
sp;
2373 FlowKeyIpFill(keys[1],
p);
2374 if (AFPInsertHalfFlow(
p->afp_v.v6_map_fd, keys[1],
2375 p->afp_v.nr_cpus) == 0) {
2376 EBPFDeleteKey(
p->afp_v.v6_map_fd, keys[0]);
2383 EBPFUpdateFlow(
p->
flow,
p, NULL);
2384 return AFPSetFlowStorage(
p,
p->afp_v.v6_map_fd, keys[0], keys[1], AF_INET6);
2400 static int AFPXDPBypassCallback(
Packet *
p)
2402 SCLogDebug(
"Calling af_packet callback function");
2404 if (!(PacketIsTCP(
p) || PacketIsUDP(
p))) {
2411 if (
p->
flow == NULL) {
2417 if (PacketIsTunnel(
p)) {
2421 if (PacketIsIPv4(
p)) {
2422 struct flowv4_keys *keys[2];
2423 keys[0]=
SCCalloc(1,
sizeof(
struct flowv4_keys));
2424 if (keys[0] == NULL) {
2428 if (
p->afp_v.v4_map_fd == -1) {
2432 keys[0]->src =
p->
src.addr_data32[0];
2433 keys[0]->dst =
p->
dst.addr_data32[0];
2436 keys[0]->port16[0] = htons(
p->
sp);
2437 keys[0]->port16[1] = htons(
p->
dp);
2438 FlowKeyIpFill(keys[0],
p);
2439 if (AFPInsertHalfFlow(
p->afp_v.v4_map_fd, keys[0],
2440 p->afp_v.nr_cpus) == 0) {
2445 keys[1]=
SCCalloc(1,
sizeof(
struct flowv4_keys));
2446 if (keys[1] == NULL) {
2447 EBPFDeleteKey(
p->afp_v.v4_map_fd, keys[0]);
2452 keys[1]->src =
p->
dst.addr_data32[0];
2453 keys[1]->dst =
p->
src.addr_data32[0];
2454 keys[1]->port16[0] = htons(
p->
dp);
2455 keys[1]->port16[1] = htons(
p->
sp);
2456 FlowKeyIpFill(keys[1],
p);
2457 if (AFPInsertHalfFlow(
p->afp_v.v4_map_fd, keys[1],
2458 p->afp_v.nr_cpus) == 0) {
2459 EBPFDeleteKey(
p->afp_v.v4_map_fd, keys[0]);
2465 return AFPSetFlowStorage(
p,
p->afp_v.v4_map_fd, keys[0], keys[1], AF_INET);
2468 if (PacketIsIPv6(
p) && ((
p->
proto == IPPROTO_TCP) || (
p->
proto == IPPROTO_UDP))) {
2470 if (
p->afp_v.v6_map_fd == -1) {
2474 struct flowv6_keys *keys[2];
2475 keys[0] =
SCCalloc(1,
sizeof(
struct flowv6_keys));
2476 if (keys[0] == NULL) {
2480 for (i = 0; i < 4; i++) {
2484 keys[0]->port16[0] = htons(
p->
sp);
2485 keys[0]->port16[1] = htons(
p->
dp);
2486 FlowKeyIpFill(keys[0],
p);
2487 if (AFPInsertHalfFlow(
p->afp_v.v6_map_fd, keys[0],
2488 p->afp_v.nr_cpus) == 0) {
2493 keys[1]=
SCCalloc(1,
sizeof(
struct flowv6_keys));
2494 if (keys[1] == NULL) {
2495 EBPFDeleteKey(
p->afp_v.v6_map_fd, keys[0]);
2500 for (i = 0; i < 4; i++) {
2504 keys[1]->port16[0] = htons(
p->
dp);
2505 keys[1]->port16[1] = htons(
p->
sp);
2506 FlowKeyIpFill(keys[1],
p);
2507 if (AFPInsertHalfFlow(
p->afp_v.v6_map_fd, keys[1],
2508 p->afp_v.nr_cpus) == 0) {
2509 EBPFDeleteKey(
p->afp_v.v6_map_fd, keys[0]);
2515 return AFPSetFlowStorage(
p,
p->afp_v.v6_map_fd, keys[0], keys[1], AF_INET6);
2520 bool g_flowv4_ok =
true;
2521 bool g_flowv6_ok =
true;
2539 if (initdata == NULL) {
2573 #ifdef HAVE_PACKET_FANOUT
2588 #ifdef HAVE_PACKET_EBPF
2591 ptv->xdp_mode = afpconfig->
xdp_mode;
2595 ptv->v4_map_fd = EBPFGetMapFDByName(ptv->
iface,
"flow_table_v4");
2596 if (ptv->v4_map_fd == -1) {
2598 SCLogError(
"Can't find eBPF map fd for '%s'",
"flow_table_v4");
2599 g_flowv4_ok =
false;
2602 ptv->v6_map_fd = EBPFGetMapFDByName(ptv->
iface,
"flow_table_v6");
2603 if (ptv->v6_map_fd == -1) {
2605 SCLogError(
"Can't find eBPF map fd for '%s'",
"flow_table_v6");
2606 g_flowv6_ok =
false;
2610 ptv->ebpf_t_config = afpconfig->ebpf_t_config;
2613 #ifdef PACKET_STATISTICS
2639 SCLogWarning(
"Enabling a BPF filter in IPS mode result"
2640 " in dropping all non matching packets.");
2651 *data = (
void *)ptv;
2670 void ReceiveAFPThreadExitStats(
ThreadVars *
tv,
void *data)
2675 #ifdef PACKET_STATISTICS
2676 AFPDumpCounters(ptv);
2694 #ifdef HAVE_PACKET_XDP
2695 if ((ptv->ebpf_t_config.
flags & EBPF_XDP_CODE) &&
2696 (!(ptv->ebpf_t_config.
flags & EBPF_PINNED_MAPS))) {
2697 EBPFSetupXDP(ptv->
iface, -1, ptv->xdp_mode);
2723 static void UpdateRawDataForVLANHdr(
Packet *
p)
2725 if (
p->afp_v.vlan_tci != 0) {
2731 *(uint16_t *)(pstart + 2 * ETH_ALEN) = htons(0x8100);
2732 *(uint16_t *)(pstart + 2 * ETH_ALEN + 2) = htons(
p->afp_v.vlan_tci);
2755 const bool afp_vlan_hdr =
p->
vlan_idx != 0;
2768 UpdateRawDataForVLANHdr(
p);
2785 *data = (
void *)
dtv;