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 #ifdef HAVE_TPACKET_V3
250 struct tpacket3_hdr *h3;
255 #ifdef HAVE_PACKET_EBPF
256 static int AFPBypassCallback(
Packet *p);
257 static int AFPXDPBypassCallback(
Packet *p);
279 #ifdef HAVE_PACKET_EBPF
340 struct tpacket_req
v2;
341 #ifdef HAVE_TPACKET_V3
342 struct tpacket_req3 v3;
354 #ifdef HAVE_PACKET_EBPF
358 struct ebpf_timeout_config ebpf_t_config;
364 static void ReceiveAFPThreadExitStats(
ThreadVars *,
void *);
373 static int AFPGetIfnumByDev(
int fd,
const char *ifname,
int verbose);
374 static int AFPGetDevFlags(
int fd,
const char *ifname);
375 static int AFPDerefSocket(
AFPPeer* peer);
376 static int AFPRefSocket(
AFPPeer* peer);
427 if (ptv->
mpeer == NULL) {
438 static void AFPPeerClean(
AFPPeer *peer)
470 #define AFP_PEERS_MAX_TRY 4
471 #define AFP_PEERS_WAIT 20000
532 if (out_iface == NULL)
534 if (iface->
mtu != out_iface->
mtu) {
535 SCLogWarning(
"MTU on %s (%d) and %s (%d) are not equal, transmission of packets "
536 "bigger than %d will fail.",
550 static int AFPPeersListWaitTurn(
AFPPeer *peer)
561 static void AFPPeersListReachedInc(
void)
575 static int AFPPeersListStarted(
void)
613 static int AFPCreateSocket(
AFPThreadVars *ptv,
char *devname,
int verbose);
617 #ifdef PACKET_STATISTICS
618 struct tpacket_stats kstats;
619 socklen_t
len =
sizeof (
struct tpacket_stats);
620 if (getsockopt(ptv->
socket, SOL_PACKET, PACKET_STATISTICS,
621 &kstats, &
len) > -1) {
622 SCLogDebug(
"(%s) Kernel: Packets %" PRIu32
", dropped %" PRIu32
"",
624 kstats.tp_packets, kstats.tp_drops);
652 struct sockaddr_ll socket_address;
661 if (!PacketIsEthernet(p)) {
666 const EthernetHdr *ethh = PacketGetEthernet(p);
668 socket_address.sll_ifindex =
SC_ATOMIC_GET(p->afp_v.peer->if_idx);
670 socket_address.sll_halen = ETH_ALEN;
672 memcpy(socket_address.sll_addr, ethh, 6);
680 sizeof(
struct sockaddr_ll)) < 0) {
682 SCLogWarning(
"%s: sending packet failed on socket %d: %s", p->afp_v.peer->iface, socket,
690 static void AFPReleaseDataFromRing(
Packet *p)
697 AFPWritePacket(p, TPACKET_V2);
700 BUG_ON(p->afp_v.relptr == NULL);
703 h.
raw = p->afp_v.relptr;
706 (void)AFPDerefSocket(p->afp_v.mpeer);
711 #ifdef HAVE_TPACKET_V3
712 static void AFPReleasePacketV3(
Packet *p)
719 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);
787 p->
vlan_id[0] = h.
h2->tp_vlan_tci & 0x0fff;
789 p->afp_v.vlan_tci = h.
h2->tp_vlan_tci;
795 p->afp_v.relptr = h.
raw;
797 p->afp_v.mpeer = ptv->
mpeer;
798 AFPRefSocket(ptv->
mpeer);
800 p->afp_v.mpeer = NULL;
825 static inline int AFPReadFromRingWaitForPacket(
AFPThreadVars *ptv)
828 struct timeval start_time;
829 gettimeofday(&start_time, NULL);
830 uint64_t busy_loop_iter = 0;
841 const unsigned int tp_status = h.
h2->tp_status;
845 struct timeval cur_time;
846 memset(&cur_time, 0,
sizeof(cur_time));
847 uint64_t milliseconds =
848 ((cur_time.tv_sec - start_time.tv_sec) * 1000) +
849 (((1000000 + cur_time.tv_usec - start_time.tv_usec) / 1000) - 1000);
850 if (milliseconds > 1000) {
857 if (busy_loop_iter) {
874 static inline bool AFPShouldIgnoreFrame(
AFPThreadVars *ptv,
const struct sockaddr_ll *sll)
876 if (
unlikely(sll->sll_pkttype > 31))
894 bool emergency_flush =
false;
899 if (AFPReadFromRingWaitForPacket(ptv) !=
AFP_READ_OK)
911 const unsigned int tp_status = h.
h2->tp_status;
927 const struct sockaddr_ll *sll =
928 (
const struct sockaddr_ll *)((uint8_t *)h.
h2 +
929 TPACKET_ALIGN(
sizeof(
struct tpacket2_hdr)));
930 if (
unlikely(AFPShouldIgnoreFrame(ptv, sll)))
935 return AFPSuriFailure(ptv, h);
937 AFPReadFromRingSetupPacket(ptv, h, tp_status, p);
940 return AFPSuriFailure(ptv, h);
950 if (emergency_flush) {
951 AFPDumpCounters(ptv);
956 #ifdef HAVE_TPACKET_V3
957 static inline void AFPFlushBlock(
struct tpacket_block_desc *pbd)
962 static inline int AFPParsePacketV3(
AFPThreadVars *ptv,
struct tpacket_block_desc *pbd,
struct tpacket3_hdr *ppd)
970 AFPReadApplyBypass(ptv, p);
978 p->
vlan_id[0] = ppd->hv1.tp_vlan_tci & 0x0fff;
980 p->afp_v.vlan_tci = (uint16_t)ppd->hv1.tp_vlan_tci;
983 (void)
PacketSetData(p, (
unsigned char *)ppd + ppd->tp_mac, ppd->tp_snaplen);
986 p->afp_v.relptr = NULL;
987 p->afp_v.mpeer = NULL;
992 p->
ts = (
SCTime_t){ .
secs = ppd->tp_sec, .usecs = ppd->tp_nsec / 1000 };
993 SCLogDebug(
"pktlen: %" PRIu32
" (pkt %p, pkt data %p)",
1019 static inline int AFPWalkBlock(
AFPThreadVars *ptv,
struct tpacket_block_desc *pbd)
1021 const int num_pkts = pbd->hdr.bh1.num_pkts;
1022 uint8_t *ppd = (uint8_t *)pbd + pbd->hdr.bh1.offset_to_first_pkt;
1024 for (
int i = 0; i < num_pkts; ++i) {
1025 const struct sockaddr_ll *sll =
1026 (
const struct sockaddr_ll *)(ppd + TPACKET_ALIGN(
sizeof(
struct tpacket3_hdr)));
1027 if (
unlikely(AFPShouldIgnoreFrame(ptv, sll))) {
1028 ppd = ppd + ((
struct tpacket3_hdr *)ppd)->tp_next_offset;
1031 int ret = AFPParsePacketV3(ptv, pbd, (
struct tpacket3_hdr *)ppd);
1044 ppd = ppd + ((
struct tpacket3_hdr *)ppd)->tp_next_offset;
1062 #ifdef HAVE_TPACKET_V3
1070 struct tpacket_block_desc *pbd =
1078 int ret = AFPWalkBlock(ptv, pbd);
1100 static int AFPRefSocket(
AFPPeer* peer)
1115 static int AFPDerefSocket(
AFPPeer* peer)
1128 if (ptv->
mpeer != NULL)
1132 #ifdef HAVE_TPACKET_V3
1153 static void AFPSwitchState(
AFPThreadVars *ptv, uint8_t state)
1170 static int AFPReadAndDiscardFromRing(
AFPThreadVars *ptv,
struct timeval *synctv,
1171 uint64_t *discarded_pkts)
1177 #ifdef HAVE_TPACKET_V3
1180 struct tpacket_block_desc *pbd =
1182 *discarded_pkts += pbd->hdr.bh1.num_pkts;
1183 struct tpacket3_hdr *ppd =
1184 (
struct tpacket3_hdr *)((uint8_t *)pbd + pbd->hdr.bh1.offset_to_first_pkt);
1185 if (((time_t)ppd->tp_sec > synctv->tv_sec) ||
1186 ((time_t)ppd->tp_sec == synctv->tv_sec &&
1187 (suseconds_t) (ppd->tp_nsec / 1000) > (suseconds_t)synctv->tv_usec)) {
1200 if (h.
raw == NULL) {
1206 if (((time_t)h.
h2->tp_sec > synctv->tv_sec) ||
1207 ((time_t)h.
h2->tp_sec == synctv->tv_sec &&
1208 (suseconds_t) (h.
h2->tp_nsec / 1000) > synctv->tv_usec)) {
1212 (*discarded_pkts)++;
1231 static int AFPSynchronizeStart(
AFPThreadVars *ptv, uint64_t *discarded_pkts)
1233 struct timeval synctv;
1237 fds.events = POLLIN;
1240 synctv.tv_sec = 0xffffffff;
1241 synctv.tv_usec = 0xffffffff;
1246 (fds.revents & (POLLHUP|POLLRDHUP|POLLERR|POLLNVAL))) {
1248 fds.revents & (POLLHUP | POLLRDHUP | POLLERR | POLLNVAL));
1251 if (AFPPeersListStarted() && synctv.tv_sec == (time_t) 0xffffffff) {
1252 gettimeofday(&synctv, NULL);
1254 r = AFPReadAndDiscardFromRing(ptv, &synctv, discarded_pkts);
1264 }
else if (r == 0 && AFPPeersListStarted()) {
1291 AFPCloseSocket(ptv);
1293 int afp_activate_r = AFPCreateSocket(ptv, ptv->
iface, 0);
1294 if (afp_activate_r != 0) {
1298 return afp_activate_r;
1316 time_t last_dump = 0;
1317 time_t current_time;
1319 uint64_t discarded_pkts = 0;
1324 AFPReadFunc = AFPReadFromRingV3;
1326 AFPReadFunc = AFPReadFromRing;
1331 while (AFPPeersListWaitTurn(ptv->
mpeer)) {
1337 r = AFPCreateSocket(ptv, ptv->
iface, 1);
1345 "%s: failed to init socket for interface, retrying soon", ptv->
iface);
1348 AFPPeersListReachedInc();
1352 AFPSynchronizeStart(ptv, &discarded_pkts);
1355 #ifdef PACKET_STATISTICS
1356 struct tpacket_stats kstats;
1357 socklen_t
len =
sizeof (
struct tpacket_stats);
1358 if (getsockopt(ptv->
socket, SOL_PACKET, PACKET_STATISTICS,
1359 &kstats, &
len) > -1) {
1361 SCLogDebug(
"(%s) Kernel socket startup: Packets %" PRIu32
1362 ", dropped %" PRIu32
"",
1364 kstats.tp_packets, kstats.tp_drops);
1365 pkts = kstats.tp_packets - discarded_pkts - kstats.tp_drops;
1373 fds.events = POLLIN;
1390 r = AFPTryReopen(ptv);
1410 (fds.revents & (POLLHUP|POLLRDHUP|POLLERR|POLLNVAL))) {
1412 if (fds.revents & (POLLHUP | POLLRDHUP)) {
1415 }
else if (fds.revents & POLLERR) {
1418 if (recv(ptv->
socket, &c,
sizeof c, MSG_PEEK) != -1)
1423 }
else if (fds.revents & POLLNVAL) {
1430 r = AFPReadFunc(ptv);
1434 current_time = time(NULL);
1435 if (current_time != last_dump) {
1436 AFPDumpCounters(ptv);
1437 last_dump = current_time;
1449 AFPDumpCounters(ptv);
1455 current_time = time(NULL);
1456 if (current_time != last_dump) {
1457 AFPDumpCounters(ptv);
1458 last_dump = current_time;
1461 TmThreadsCaptureHandleTimeout(
tv, NULL);
1463 }
else if ((r < 0) && (errno != EINTR)) {
1472 AFPDumpCounters(ptv);
1477 static int AFPGetDevFlags(
int fd,
const char *ifname)
1481 memset(&ifr, 0,
sizeof(ifr));
1482 strlcpy(ifr.ifr_name, ifname,
sizeof(ifr.ifr_name));
1484 if (ioctl(fd, SIOCGIFFLAGS, &ifr) == -1) {
1485 SCLogError(
"%s: failed to get interface flags: %s", ifname, strerror(errno));
1489 return ifr.ifr_flags;
1493 static int AFPGetIfnumByDev(
int fd,
const char *ifname,
int verbose)
1497 memset(&ifr, 0,
sizeof(ifr));
1498 strlcpy(ifr.ifr_name, ifname,
sizeof(ifr.ifr_name));
1500 if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
1502 SCLogError(
"%s: failed to find interface: %s", ifname, strerror(errno));
1506 return ifr.ifr_ifindex;
1509 static int AFPGetDevLinktype(
int fd,
const char *ifname)
1513 memset(&ifr, 0,
sizeof(ifr));
1514 strlcpy(ifr.ifr_name, ifname,
sizeof(ifr.ifr_name));
1516 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) {
1517 SCLogError(
"%s: failed to find interface type: %s", ifname, strerror(errno));
1521 switch (ifr.ifr_hwaddr.sa_family) {
1522 case ARPHRD_LOOPBACK:
1528 return ifr.ifr_hwaddr.sa_family;
1536 int fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1538 SCLogError(
"%s: failed to create AF_PACKET socket: %s", ifname, strerror(errno));
1542 ltype = AFPGetDevLinktype(fd, ifname);
1550 static int AFPComputeRingParams(
AFPThreadVars *ptv,
int order)
1576 int tp_hdrlen =
sizeof(
struct tpacket_hdr);
1582 SCLogWarning(
"%s: unable to get MTU, setting snaplen default of 1514", ptv->
iface);
1587 ptv->
req.
v2.tp_frame_size = TPACKET_ALIGN(snaplen +TPACKET_ALIGN(TPACKET_ALIGN(tp_hdrlen) +
sizeof(
struct sockaddr_ll) + ETH_HLEN) - ETH_HLEN);
1588 ptv->
req.
v2.tp_block_size = getpagesize() << order;
1589 int frames_per_block = ptv->
req.
v2.tp_block_size / ptv->
req.
v2.tp_frame_size;
1590 if (frames_per_block == 0) {
1595 ptv->
req.
v2.tp_block_nr = ptv->
req.
v2.tp_frame_nr / frames_per_block + 1;
1597 ptv->
req.
v2.tp_frame_nr = ptv->
req.
v2.tp_block_nr * frames_per_block;
1598 SCLogPerf(
"%s: rx ring: block_size=%d block_nr=%d frame_size=%d frame_nr=%d", ptv->
iface,
1599 ptv->
req.
v2.tp_block_size, ptv->
req.
v2.tp_block_nr, ptv->
req.
v2.tp_frame_size,
1600 ptv->
req.
v2.tp_frame_nr);
1604 #ifdef HAVE_TPACKET_V3
1608 ptv->
req.v3.tp_frame_size = 2048;
1609 int frames_per_block = 0;
1610 int tp_hdrlen =
sizeof(
struct tpacket3_hdr);
1616 SCLogWarning(
"%s: unable to get MTU, setting snaplen default of 1514", ptv->
iface);
1621 ptv->
req.v3.tp_frame_size = TPACKET_ALIGN(snaplen +TPACKET_ALIGN(TPACKET_ALIGN(tp_hdrlen) +
sizeof(
struct sockaddr_ll) + ETH_HLEN) - ETH_HLEN);
1622 frames_per_block = ptv->
req.v3.tp_block_size / ptv->
req.v3.tp_frame_size;
1624 if (frames_per_block == 0) {
1625 SCLogError(
"%s: block size is too small, it should be at least %d", ptv->
iface,
1626 ptv->
req.v3.tp_frame_size);
1629 ptv->
req.v3.tp_block_nr = ptv->
ring_size / frames_per_block + 1;
1631 ptv->
req.v3.tp_frame_nr = ptv->
req.v3.tp_block_nr * frames_per_block;
1633 ptv->
req.v3.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH;
1634 SCLogPerf(
"%s: rx ring params: block_size=%d block_nr=%d frame_size=%d frame_nr=%d (mem: %d)",
1635 ptv->
iface, ptv->
req.v3.tp_block_size, ptv->
req.v3.tp_block_nr,
1636 ptv->
req.v3.tp_frame_size, ptv->
req.v3.tp_frame_nr,
1637 ptv->
req.v3.tp_block_size * ptv->
req.v3.tp_block_nr);
1645 unsigned int len =
sizeof(val), i;
1649 #ifdef HAVE_TPACKET_V3
1657 if (getsockopt(ptv->
socket, SOL_PACKET, PACKET_HDRLEN, &val, &
len) < 0) {
1658 if (errno == ENOPROTOOPT) {
1660 SCLogError(
"%s: kernel too old for TPACKET_V3 (need 3.2+)", devname);
1662 SCLogError(
"%s: kernel too old (need 2.6.27+)", devname);
1665 SCLogError(
"%s: failed to retrieve packet header len", devname);
1670 #ifdef HAVE_TPACKET_V3
1675 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_VERSION, &val,
1677 SCLogError(
"%s: failed to activate TPACKET_V2/TPACKET_V3 on packet socket: %s", devname,
1682 #ifdef HAVE_HW_TIMESTAMPING
1683 int req = SOF_TIMESTAMPING_RAW_HARDWARE;
1684 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_TIMESTAMP, (
void *) &req,
1686 SCLogWarning(
"%s: failed to activate hardware timestamping on packet socket: %s", devname,
1694 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_RESERVE, (
void *)&reserve,
sizeof(reserve)) <
1696 SCLogError(
"%s: failed to activate reserve on packet socket: %s", devname, strerror(errno));
1701 #ifdef HAVE_TPACKET_V3
1703 if (AFPComputeRingParamsV3(ptv) != 1) {
1706 r = setsockopt(ptv->
socket, SOL_PACKET, PACKET_RX_RING,
1707 (
void *) &ptv->
req.v3,
sizeof(ptv->
req.v3));
1709 SCLogError(
"%s: failed to allocate RX Ring: %s", devname, strerror(errno));
1715 if (AFPComputeRingParams(ptv, order) != 1) {
1716 SCLogError(
"%s: ring parameters are incorrect. Please file a bug report", devname);
1720 r = setsockopt(ptv->
socket, SOL_PACKET, PACKET_RX_RING,
1721 (
void *) &ptv->
req,
sizeof(ptv->
req));
1724 if (errno == ENOMEM) {
1725 SCLogWarning(
"%s: memory issue with ring parameters. Retrying", devname);
1728 SCLogError(
"%s: failed to setup RX Ring: %s", devname, strerror(errno));
1735 SCLogError(
"%s: failed to setup RX Ring (order 0 failed)", devname);
1738 #ifdef HAVE_TPACKET_V3
1743 #ifdef HAVE_TPACKET_V3
1749 #ifdef HAVE_TPACKET_V3
1752 mmap_flag = MAP_SHARED;
1754 mmap_flag |= MAP_LOCKED;
1756 mmap_flag, ptv->
socket, 0);
1758 SCLogError(
"%s: failed to mmap: %s", devname, strerror(errno));
1761 #ifdef HAVE_TPACKET_V3
1765 SCLogError(
"%s: failed to alloc ring: %s", devname, strerror(errno));
1768 for (i = 0; i < ptv->
req.v3.tp_block_nr; ++i) {
1770 ptv->
ring.
v3[i].iov_len = ptv->
req.v3.tp_block_size;
1776 if (ptv->
ring.
v2 == NULL) {
1777 SCLogError(
"%s: failed to alloc ring: %s", devname, strerror(errno));
1782 for (i = 0; i < ptv->
req.
v2.tp_block_nr; ++i) {
1783 void *base = &(ptv->
ring_buf[i * ptv->
req.
v2.tp_block_size]);
1787 base += ptv->
req.
v2.tp_frame_size;
1791 #ifdef HAVE_TPACKET_V3
1813 #ifdef HAVE_PACKET_FANOUT
1814 int fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1819 uint32_t option = (mode << 16) | cluster_id;
1820 int r = setsockopt(fd, SOL_PACKET,
PACKET_FANOUT,(
void *)&option,
sizeof(option));
1824 SCLogError(
"fanout not supported by kernel: "
1825 "Kernel too old or cluster-id %d already in use.",
1835 #ifdef HAVE_PACKET_EBPF
1839 int pfd = ptv->ebpf_lb_fd;
1841 SCLogError(
"Fanout file descriptor is invalid");
1845 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_FANOUT_DATA, &pfd,
sizeof(pfd))) {
1856 int pfd = ptv->ebpf_filter_fd;
1858 SCLogError(
"Filter file descriptor is invalid");
1862 if (setsockopt(ptv->
socket, SOL_SOCKET, SO_ATTACH_BPF, &pfd,
sizeof(pfd))) {
1863 SCLogError(
"Error setting ebpf: %s", strerror(errno));
1866 SCLogInfo(
"Activated eBPF filter on socket");
1872 static int AFPCreateSocket(
AFPThreadVars *ptv,
char *devname,
int verbose)
1876 struct packet_mreq sock_params;
1877 struct sockaddr_ll bind_address;
1881 ptv->
socket = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1883 SCLogError(
"%s: failed to create socket: %s", devname, strerror(errno));
1887 if_idx = AFPGetIfnumByDev(ptv->
socket, devname, verbose);
1893 memset(&bind_address, 0,
sizeof(bind_address));
1894 bind_address.sll_family = AF_PACKET;
1895 bind_address.sll_protocol = htons(ETH_P_ALL);
1896 bind_address.sll_ifindex = if_idx;
1897 if (bind_address.sll_ifindex == -1) {
1904 int if_flags = AFPGetDevFlags(ptv->
socket, ptv->
iface);
1905 if (if_flags == -1) {
1911 }
else if ((if_flags & (IFF_UP | IFF_RUNNING)) == 0) {
1920 if (if_flags & IFF_LOOPBACK)
1925 memset(&sock_params, 0,
sizeof(sock_params));
1926 sock_params.mr_type = PACKET_MR_PROMISC;
1927 sock_params.mr_ifindex = bind_address.sll_ifindex;
1928 r = setsockopt(ptv->
socket, SOL_PACKET, PACKET_ADD_MEMBERSHIP,(
void *)&sock_params,
sizeof(sock_params));
1930 SCLogError(
"%s: failed to set promisc mode: %s", devname, strerror(errno));
1937 if (setsockopt(ptv->
socket, SOL_PACKET, PACKET_AUXDATA, &val,
1938 sizeof(val)) == -1 && errno != ENOPROTOOPT) {
1940 "%s: 'kernel' checksum mode not supported, falling back to full mode", devname);
1951 if (setsockopt(ptv->
socket, SOL_SOCKET, SO_RCVBUF,
1960 r = bind(ptv->
socket, (
struct sockaddr *)&bind_address,
sizeof(bind_address));
1963 if (errno == ENETDOWN) {
1964 SCLogWarning(
"%s: failed to bind socket, iface is down", devname);
1966 SCLogWarning(
"%s: failed to bind socket: %s", devname, strerror(errno));
1974 #ifdef HAVE_PACKET_FANOUT
1979 uint32_t option = (mode << 16) | (
id & 0xffff);
1982 SCLogError(
"%s: failed to set fanout mode: %s", devname, strerror(errno));
1988 #ifdef HAVE_PACKET_EBPF
1990 r = SockFanoutSeteBPF(ptv);
1992 SCLogError(
"%s: failed to set eBPF: %s", devname, strerror(errno));
1998 ret = AFPSetupRing(ptv, devname);
2002 SCLogDebug(
"Using interface '%s' via socket %d", (
char *)devname, ptv->
socket);
2006 TmEcode rc = AFPSetBPFFilter(ptv);
2037 struct bpf_program filter;
2038 struct sock_fprog fcode;
2041 #ifdef HAVE_PACKET_EBPF
2042 if (ptv->ebpf_filter_fd != -1) {
2043 return SetEbpfFilter(ptv);
2052 char errbuf[PCAP_ERRBUF_SIZE];
2060 sizeof(errbuf)) == -1) {
2065 if (filter.bf_len > USHRT_MAX) {
2068 fcode.len = (
unsigned short)filter.bf_len;
2069 fcode.filter = (
struct sock_filter*)filter.bf_insns;
2071 rc = setsockopt(ptv->
socket, SOL_SOCKET, SO_ATTACH_FILTER, &fcode,
sizeof(fcode));
2075 SCLogError(
"%s: failed to attach filter: %s", ptv->
iface, strerror(errno));
2082 #ifdef HAVE_PACKET_EBPF
2090 static int AFPInsertHalfFlow(
int mapd,
void *key,
unsigned int nr_cpus)
2092 BPF_DECLARE_PERCPU(
struct pair, value, nr_cpus);
2101 for (i = 0; i < nr_cpus; i++) {
2102 BPF_PERCPU(value, i).packets = 0;
2103 BPF_PERCPU(value, i).bytes = 0;
2105 if (bpf_map_update_elem(mapd, key, value, BPF_NOEXIST) != 0) {
2118 SCLogError(
"Can't update eBPF map: %s (%d)", strerror(errno), errno);
2125 static int AFPSetFlowStorage(
Packet *p,
int map_fd,
void *key0,
void* key1,
2136 EBPFBypassData *eb =
SCCalloc(1,
sizeof(EBPFBypassData));
2138 EBPFDeleteKey(map_fd, key0);
2139 EBPFDeleteKey(map_fd, key1);
2148 eb->cpus_count = p->afp_v.nr_cpus;
2153 EBPFDeleteKey(map_fd, key0);
2154 EBPFDeleteKey(map_fd, key1);
2180 static int AFPBypassCallback(
Packet *p)
2182 SCLogDebug(
"Calling af_packet callback function");
2184 if (!(PacketIsTCP(p) || PacketIsUDP(p))) {
2191 if (p->
flow == NULL) {
2197 if (PacketIsTunnel(p)) {
2200 if (PacketIsIPv4(p)) {
2202 if (p->afp_v.v4_map_fd == -1) {
2205 struct flowv4_keys *keys[2];
2206 keys[0] =
SCCalloc(1,
sizeof(
struct flowv4_keys));
2207 if (keys[0] == NULL) {
2212 keys[0]->port16[0] = p->
sp;
2213 keys[0]->port16[1] = p->
dp;
2214 keys[0]->vlan0 = p->
vlan_id[0];
2215 keys[0]->vlan1 = p->
vlan_id[1];
2216 keys[0]->vlan2 = p->
vlan_id[2];
2218 if (p->
proto == IPPROTO_TCP) {
2219 keys[0]->ip_proto = 1;
2221 keys[0]->ip_proto = 0;
2223 if (AFPInsertHalfFlow(p->afp_v.v4_map_fd, keys[0],
2224 p->afp_v.nr_cpus) == 0) {
2229 keys[1]=
SCCalloc(1,
sizeof(
struct flowv4_keys));
2230 if (keys[1] == NULL) {
2231 EBPFDeleteKey(p->afp_v.v4_map_fd, keys[0]);
2238 keys[1]->port16[0] = p->
dp;
2239 keys[1]->port16[1] = p->
sp;
2240 keys[1]->vlan0 = p->
vlan_id[0];
2241 keys[1]->vlan1 = p->
vlan_id[1];
2242 keys[1]->vlan2 = p->
vlan_id[2];
2244 keys[1]->ip_proto = keys[0]->ip_proto;
2245 if (AFPInsertHalfFlow(p->afp_v.v4_map_fd, keys[1],
2246 p->afp_v.nr_cpus) == 0) {
2247 EBPFDeleteKey(p->afp_v.v4_map_fd, keys[0]);
2253 EBPFUpdateFlow(p->
flow, p, NULL);
2254 return AFPSetFlowStorage(p, p->afp_v.v4_map_fd, keys[0], keys[1], AF_INET);
2257 if (PacketIsIPv6(p) && ((p->
proto == IPPROTO_TCP) || (p->
proto == IPPROTO_UDP))) {
2259 if (p->afp_v.v6_map_fd == -1) {
2263 struct flowv6_keys *keys[2];
2264 keys[0] =
SCCalloc(1,
sizeof(
struct flowv6_keys));
2265 if (keys[0] == NULL) {
2269 for (i = 0; i < 4; i++) {
2273 keys[0]->port16[0] = p->
sp;
2274 keys[0]->port16[1] = p->
dp;
2275 keys[0]->vlan0 = p->
vlan_id[0];
2276 keys[0]->vlan1 = p->
vlan_id[1];
2277 keys[0]->vlan2 = p->
vlan_id[2];
2279 if (p->
proto == IPPROTO_TCP) {
2280 keys[0]->ip_proto = 1;
2282 keys[0]->ip_proto = 0;
2284 if (AFPInsertHalfFlow(p->afp_v.v6_map_fd, keys[0],
2285 p->afp_v.nr_cpus) == 0) {
2290 keys[1]=
SCCalloc(1,
sizeof(
struct flowv6_keys));
2291 if (keys[1] == NULL) {
2292 EBPFDeleteKey(p->afp_v.v6_map_fd, keys[0]);
2297 for (i = 0; i < 4; i++) {
2301 keys[1]->port16[0] = p->
dp;
2302 keys[1]->port16[1] = p->
sp;
2303 keys[1]->vlan0 = p->
vlan_id[0];
2304 keys[1]->vlan1 = p->
vlan_id[1];
2305 keys[1]->vlan2 = p->
vlan_id[2];
2307 keys[1]->ip_proto = keys[0]->ip_proto;
2308 if (AFPInsertHalfFlow(p->afp_v.v6_map_fd, keys[1],
2309 p->afp_v.nr_cpus) == 0) {
2310 EBPFDeleteKey(p->afp_v.v6_map_fd, keys[0]);
2317 EBPFUpdateFlow(p->
flow, p, NULL);
2318 return AFPSetFlowStorage(p, p->afp_v.v6_map_fd, keys[0], keys[1], AF_INET6);
2334 static int AFPXDPBypassCallback(
Packet *p)
2336 SCLogDebug(
"Calling af_packet callback function");
2338 if (!(PacketIsTCP(p) || PacketIsUDP(p))) {
2345 if (p->
flow == NULL) {
2351 if (PacketIsTunnel(p)) {
2354 if (PacketIsIPv4(p)) {
2355 struct flowv4_keys *keys[2];
2356 keys[0]=
SCCalloc(1,
sizeof(
struct flowv4_keys));
2357 if (keys[0] == NULL) {
2361 if (p->afp_v.v4_map_fd == -1) {
2365 keys[0]->src = p->
src.addr_data32[0];
2366 keys[0]->dst = p->
dst.addr_data32[0];
2369 keys[0]->port16[0] = htons(p->
sp);
2370 keys[0]->port16[1] = htons(p->
dp);
2371 keys[0]->vlan0 = p->
vlan_id[0];
2372 keys[0]->vlan1 = p->
vlan_id[1];
2373 keys[0]->vlan2 = p->
vlan_id[2];
2374 if (p->
proto == IPPROTO_TCP) {
2375 keys[0]->ip_proto = 1;
2377 keys[0]->ip_proto = 0;
2379 if (AFPInsertHalfFlow(p->afp_v.v4_map_fd, keys[0],
2380 p->afp_v.nr_cpus) == 0) {
2385 keys[1]=
SCCalloc(1,
sizeof(
struct flowv4_keys));
2386 if (keys[1] == NULL) {
2387 EBPFDeleteKey(p->afp_v.v4_map_fd, keys[0]);
2392 keys[1]->src = p->
dst.addr_data32[0];
2393 keys[1]->dst = p->
src.addr_data32[0];
2394 keys[1]->port16[0] = htons(p->
dp);
2395 keys[1]->port16[1] = htons(p->
sp);
2396 keys[1]->vlan0 = p->
vlan_id[0];
2397 keys[1]->vlan1 = p->
vlan_id[1];
2398 keys[1]->vlan2 = p->
vlan_id[2];
2399 keys[1]->ip_proto = keys[0]->ip_proto;
2400 if (AFPInsertHalfFlow(p->afp_v.v4_map_fd, keys[1],
2401 p->afp_v.nr_cpus) == 0) {
2402 EBPFDeleteKey(p->afp_v.v4_map_fd, keys[0]);
2408 return AFPSetFlowStorage(p, p->afp_v.v4_map_fd, keys[0], keys[1], AF_INET);
2411 if (PacketIsIPv6(p) && ((p->
proto == IPPROTO_TCP) || (p->
proto == IPPROTO_UDP))) {
2413 if (p->afp_v.v6_map_fd == -1) {
2417 struct flowv6_keys *keys[2];
2418 keys[0] =
SCCalloc(1,
sizeof(
struct flowv6_keys));
2419 if (keys[0] == NULL) {
2423 for (i = 0; i < 4; i++) {
2427 keys[0]->port16[0] = htons(p->
sp);
2428 keys[0]->port16[1] = htons(p->
dp);
2429 keys[0]->vlan0 = p->
vlan_id[0];
2430 keys[0]->vlan1 = p->
vlan_id[1];
2431 keys[0]->vlan2 = p->
vlan_id[2];
2432 if (p->
proto == IPPROTO_TCP) {
2433 keys[0]->ip_proto = 1;
2435 keys[0]->ip_proto = 0;
2437 if (AFPInsertHalfFlow(p->afp_v.v6_map_fd, keys[0],
2438 p->afp_v.nr_cpus) == 0) {
2443 keys[1]=
SCCalloc(1,
sizeof(
struct flowv6_keys));
2444 if (keys[1] == NULL) {
2445 EBPFDeleteKey(p->afp_v.v6_map_fd, keys[0]);
2450 for (i = 0; i < 4; i++) {
2454 keys[1]->port16[0] = htons(p->
dp);
2455 keys[1]->port16[1] = htons(p->
sp);
2456 keys[1]->vlan0 = p->
vlan_id[0];
2457 keys[1]->vlan1 = p->
vlan_id[1];
2458 keys[1]->vlan2 = p->
vlan_id[2];
2459 keys[1]->ip_proto = keys[0]->ip_proto;
2460 if (AFPInsertHalfFlow(p->afp_v.v6_map_fd, keys[1],
2461 p->afp_v.nr_cpus) == 0) {
2462 EBPFDeleteKey(p->afp_v.v6_map_fd, keys[0]);
2468 return AFPSetFlowStorage(p, p->afp_v.v6_map_fd, keys[0], keys[1], AF_INET6);
2473 bool g_flowv4_ok =
true;
2474 bool g_flowv6_ok =
true;
2492 if (initdata == NULL) {
2525 #ifdef HAVE_PACKET_FANOUT
2540 #ifdef HAVE_PACKET_EBPF
2543 ptv->xdp_mode = afpconfig->
xdp_mode;
2547 ptv->v4_map_fd = EBPFGetMapFDByName(ptv->
iface,
"flow_table_v4");
2548 if (ptv->v4_map_fd == -1) {
2549 if (g_flowv4_ok ==
false) {
2550 SCLogError(
"Can't find eBPF map fd for '%s'",
"flow_table_v4");
2554 ptv->v6_map_fd = EBPFGetMapFDByName(ptv->
iface,
"flow_table_v6");
2555 if (ptv->v6_map_fd == -1) {
2557 SCLogError(
"Can't find eBPF map fd for '%s'",
"flow_table_v6");
2558 g_flowv6_ok =
false;
2562 ptv->ebpf_t_config = afpconfig->ebpf_t_config;
2565 #ifdef PACKET_STATISTICS
2589 SCLogWarning(
"Enabling a BPF filter in IPS mode result"
2590 " in dropping all non matching packets.");
2601 *data = (
void *)ptv;
2620 void ReceiveAFPThreadExitStats(
ThreadVars *
tv,
void *data)
2625 #ifdef PACKET_STATISTICS
2626 AFPDumpCounters(ptv);
2644 #ifdef HAVE_PACKET_XDP
2645 if ((ptv->ebpf_t_config.
flags & EBPF_XDP_CODE) &&
2646 (!(ptv->ebpf_t_config.
flags & EBPF_PINNED_MAPS))) {
2647 EBPFSetupXDP(ptv->
iface, -1, ptv->xdp_mode);
2673 static void UpdateRawDataForVLANHdr(
Packet *p)
2675 if (p->afp_v.vlan_tci != 0) {
2681 *(uint16_t *)(pstart + 2 * ETH_ALEN) = htons(0x8100);
2682 *(uint16_t *)(pstart + 2 * ETH_ALEN + 2) = htons(p->afp_v.vlan_tci);
2705 const bool afp_vlan_hdr = p->
vlan_idx != 0;
2718 UpdateRawDataForVLANHdr(p);
2735 *data = (
void *)
dtv;