suricata
source-af-packet.h
Go to the documentation of this file.
1/* Copyright (C) 2011,2012 Open Information Security Foundation
2 *
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
5 * Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18/**
19 * \file
20 *
21 * \author Eric Leblond <eric@regit.org>
22 */
23
24#ifndef SURICATA_SOURCE_AFP_H
25#define SURICATA_SOURCE_AFP_H
26
27#ifndef HAVE_PACKET_FANOUT /* not defined if linux/if_packet.h trying to force */
28#define HAVE_PACKET_FANOUT 1
29
30#define PACKET_FANOUT 18
31
32#define PACKET_FANOUT_HASH 0
33#define PACKET_FANOUT_LB 1
34#define PACKET_FANOUT_CPU 2
35#define PACKET_FANOUT_ROLLOVER 3
36#define PACKET_FANOUT_RND 4
37#define PACKET_FANOUT_QM 5
38
39#define PACKET_FANOUT_FLAG_ROLLOVER 0x1000
40#define PACKET_FANOUT_FLAG_DEFRAG 0x8000
41#else /* HAVE_PACKET_FANOUT */
42#include <linux/if_packet.h>
43#endif /* HAVE_PACKET_FANOUT */
44#include "queue.h"
45
46#ifdef HAVE_PACKET_EBPF
47#define AFP_MODE_XDP_BYPASS 1
48#define AFP_MODE_EBPF_BYPASS 2
49struct ebpf_timeout_config {
50 const char *pinned_maps_name;
51 uint16_t cpus_count;
52 uint8_t mode;
53 uint8_t flags;
54};
55#endif
56
57/* value for flags */
58#define AFP_NEED_PEER (1 << 0)
59// (1<<1) vacant
60#define AFP_SOCK_PROTECT (1<<2)
61#define AFP_EMERGENCY_MODE (1<<3)
62#define AFP_TPACKET_V3 (1<<4)
63#define AFP_VLAN_IN_HEADER (1<<5)
64#define AFP_MMAP_LOCKED (1<<6)
65#define AFP_BYPASS (1<<7)
66#define AFP_XDPBYPASS (1<<8)
67
68#define AFP_COPY_MODE_NONE 0
69#define AFP_COPY_MODE_TAP 1
70#define AFP_COPY_MODE_IPS 2
71
72#define AFP_IFACE_NAME_LENGTH 48
73
74/* In kernel the allocated block size is allocated using the formula
75 * page_size << order. So default value is using the same formula with
76 * an order of 3 which guarantee we have some room in the block compared
77 * to standard frame size */
78#define AFP_BLOCK_SIZE_DEFAULT_ORDER 5
79
80/* Set max packet size to 65561: IP + Ethernet + 3 VLAN tags. */
81#define MAX_PACKET_SIZE 65561
82
83/* Default snaplen to use when defrag enabled. 9k is somewhat
84 * arbitrary but is large enough for the common 9000 jumbo frame plus
85 * some extra headers including tpacket headers. */
86#define DEFAULT_TPACKET_DEFRAG_SNAPLEN 9216
87
88typedef struct AFPIfaceConfig_
89{
91 /* number of threads */
93 /* socket buffer size */
95 /* ring size in number of packets */
97 /* block size for tpacket_v3 in */
99 /* block timeout for tpacket_v3 in milliseconds */
101 /* block size for tpacket v2 */
103 /* cluster param */
104 uint16_t cluster_id;
106 /* promisc mode */
108 /* misc use flags including ring mode */
109 unsigned int flags;
110 uint8_t copy_mode;
112 const char *bpf_filter;
113 const char *ebpf_lb_file;
115 const char *ebpf_filter_file;
117 const char *xdp_filter_file;
119 uint8_t xdp_mode;
120 const char *out_iface;
121#ifdef HAVE_PACKET_EBPF
122 struct ebpf_timeout_config ebpf_t_config;
123#endif
124 SC_ATOMIC_DECLARE(unsigned int, ref);
125 void (*DerefFunc)(void *);
127
128/**
129 * \ingroup afppeers
130 * @{
131 */
132
133typedef struct AFPPeer_ {
134 SC_ATOMIC_DECLARE(int, socket);
135 SC_ATOMIC_DECLARE(int, sock_usage);
136 SC_ATOMIC_DECLARE(int, if_idx);
137 SC_ATOMIC_DECLARE(uint64_t, send_errors);
138 int flags;
140 int turn; /**< Field used to store initialisation order. */
141 SC_ATOMIC_DECLARE(uint8_t, state);
142 struct AFPPeer_ *peer;
146
147/**
148 * \brief per packet AF_PACKET vars
149 *
150 * This structure is used y the release data system and is cleaned
151 * up by the AFPV_CLEANUP macro below.
152 */
153typedef struct AFPPacketVars_
154{
155 void *relptr;
156 AFPPeer *peer; /**< Sending peer for IPS/TAP mode */
157 /** Pointer to ::AFPPeer used for capture. Field is used to be able
158 * to do reference counting.
159 */
161 uint8_t copy_mode;
162 uint16_t vlan_tci;
163#ifdef HAVE_PACKET_EBPF
164 int v4_map_fd;
165 int v6_map_fd;
166 unsigned int nr_cpus;
167#endif
169
170#ifdef HAVE_PACKET_EBPF
171#define AFPV_CLEANUP(afpv) \
172 do { \
173 (afpv)->relptr = NULL; \
174 (afpv)->copy_mode = 0; \
175 (afpv)->vlan_tci = 0; \
176 (afpv)->peer = NULL; \
177 (afpv)->mpeer = NULL; \
178 (afpv)->v4_map_fd = -1; \
179 (afpv)->v6_map_fd = -1; \
180 } while (0)
181#else
182#define AFPV_CLEANUP(afpv) \
183 do { \
184 (afpv)->relptr = NULL; \
185 (afpv)->copy_mode = 0; \
186 (afpv)->vlan_tci = 0; \
187 (afpv)->peer = NULL; \
188 (afpv)->mpeer = NULL; \
189 } while (0)
190#endif
191
192/**
193 * @}
194 */
195
197void TmModuleDecodeAFPRegister (void);
198
201void AFPPeersListClean(void);
202int AFPGetLinkType(const char *ifname);
203
204int AFPIsFanoutSupported(uint16_t cluster_id);
205
206#endif /* SURICATA_SOURCE_AFP_H */
struct HtpBodyChunk_ * next
uint8_t flags
Definition decode-gre.h:0
ChecksumValidationMode
Definition decode.h:42
void TmModuleReceiveAFPRegister(void)
Registration Function for RecieveAFP.
int AFPGetLinkType(const char *ifname)
void TmModuleDecodeAFPRegister(void)
Registration Function for DecodeAFP.
int AFPIsFanoutSupported(uint16_t cluster_id)
test if we can use FANOUT. Older kernels like those in CentOS6 have HAVE_PACKET_FANOUT defined but fa...
void AFPPeersListClean(void)
Clean the global peers list.
TmEcode AFPPeersListInit(void)
Init the global list of AFPPeer.
struct AFPPeer_ AFPPeer
TmEcode AFPPeersListCheck(void)
Check that all AFPPeer got a peer.
struct AFPIfaceConfig_ AFPIfaceConfig
struct AFPPacketVars_ AFPPacketVars
per packet AF_PACKET vars
#define AFP_IFACE_NAME_LENGTH
const char * out_iface
SC_ATOMIC_DECLARE(unsigned int, ref)
void(* DerefFunc)(void *)
char iface[AFP_IFACE_NAME_LENGTH]
ChecksumValidationMode checksum_mode
const char * ebpf_filter_file
const char * xdp_filter_file
const char * bpf_filter
const char * ebpf_lb_file
per packet AF_PACKET vars
TAILQ_ENTRY(AFPPeer_) next
SC_ATOMIC_DECLARE(uint64_t, send_errors)
SC_ATOMIC_DECLARE(uint8_t, state)
SCMutex sock_protect
char iface[AFP_IFACE_NAME_LENGTH]
SC_ATOMIC_DECLARE(int, sock_usage)
SC_ATOMIC_DECLARE(int, socket)
struct AFPPeer_ * peer
SC_ATOMIC_DECLARE(int, if_idx)
#define SCMutex