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
49 struct 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 
88 typedef struct AFPIfaceConfig_
89 {
91  /* number of threads */
92  int threads;
93  /* socket buffer size */
95  /* ring size in number of packets */
96  int ring_size;
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 */
107  int promisc;
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 
133 typedef 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  */
153 typedef 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 
196 void TmModuleReceiveAFPRegister (void);
197 void TmModuleDecodeAFPRegister (void);
198 
201 void AFPPeersListClean(void);
202 int AFPGetLinkType(const char *ifname);
203 
204 int AFPIsFanoutSupported(uint16_t cluster_id);
205 
206 #endif /* SURICATA_SOURCE_AFP_H */
AFPIfaceConfig_::promisc
int promisc
Definition: source-af-packet.h:107
AFPIfaceConfig_::checksum_mode
ChecksumValidationMode checksum_mode
Definition: source-af-packet.h:111
AFPIfaceConfig_::xdp_mode
uint8_t xdp_mode
Definition: source-af-packet.h:119
AFPPeer_::TAILQ_ENTRY
TAILQ_ENTRY(AFPPeer_) next
AFPPeer_::turn
int turn
Definition: source-af-packet.h:140
AFPPeer_::peer
struct AFPPeer_ * peer
Definition: source-af-packet.h:142
AFPPacketVars
struct AFPPacketVars_ AFPPacketVars
per packet AF_PACKET vars
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
AFPIfaceConfig_::ebpf_filter_fd
int ebpf_filter_fd
Definition: source-af-packet.h:116
AFPPeer_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(int, sock_usage)
AFPPeersListInit
TmEcode AFPPeersListInit(void)
Init the global list of AFPPeer.
Definition: source-af-packet.c:449
AFPIfaceConfig_::threads
int threads
Definition: source-af-packet.h:92
AFPIfaceConfig_::ring_size
int ring_size
Definition: source-af-packet.h:96
AFPPacketVars_::copy_mode
uint8_t copy_mode
Definition: source-af-packet.h:161
TmModuleDecodeAFPRegister
void TmModuleDecodeAFPRegister(void)
Registration Function for DecodeAFP.
Definition: source-af-packet.c:599
AFPIfaceConfig_::block_timeout
int block_timeout
Definition: source-af-packet.h:100
AFPPeer
struct AFPPeer_ AFPPeer
AFPPeer_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(int, socket)
AFPPeer_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(int, if_idx)
AFPIfaceConfig_::out_iface
const char * out_iface
Definition: source-af-packet.h:120
AFPPacketVars_::peer
AFPPeer * peer
Definition: source-af-packet.h:156
AFPIfaceConfig_::flags
unsigned int flags
Definition: source-af-packet.h:109
AFPIfaceConfig_::xdp_filter_fd
int xdp_filter_fd
Definition: source-af-packet.h:118
AFPPeersListClean
void AFPPeersListClean(void)
Clean the global peers list.
Definition: source-af-packet.c:581
AFPGetLinkType
int AFPGetLinkType(const char *ifname)
Definition: source-af-packet.c:1523
AFPPeer_::sock_protect
SCMutex sock_protect
Definition: source-af-packet.h:139
AFPIfaceConfig
struct AFPIfaceConfig_ AFPIfaceConfig
AFPPeersListCheck
TmEcode AFPPeersListCheck(void)
Check that all AFPPeer got a peer.
Definition: source-af-packet.c:466
AFPIfaceConfig_::cluster_type
int cluster_type
Definition: source-af-packet.h:105
AFPIfaceConfig_::cluster_id
uint16_t cluster_id
Definition: source-af-packet.h:104
AFPIfaceConfig_::block_size
int block_size
Definition: source-af-packet.h:98
AFPPeer_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint8_t, state)
AFPIfaceConfig_::ebpf_lb_fd
int ebpf_lb_fd
Definition: source-af-packet.h:114
AFPIfaceConfig_::ebpf_filter_file
const char * ebpf_filter_file
Definition: source-af-packet.h:115
TmModuleReceiveAFPRegister
void TmModuleReceiveAFPRegister(void)
Registration Function for RecieveAFP.
Definition: source-af-packet.c:381
AFPPacketVars_::vlan_tci
uint16_t vlan_tci
Definition: source-af-packet.h:162
AFPIfaceConfig_::DerefFunc
void(* DerefFunc)(void *)
Definition: source-af-packet.h:125
AFPPeer_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint64_t, send_errors)
AFPPeer_::flags
int flags
Definition: source-af-packet.h:138
TmEcode
TmEcode
Definition: tm-threads-common.h:79
queue.h
AFPPeer_
Definition: source-af-packet.h:133
flags
uint8_t flags
Definition: decode-gre.h:0
ChecksumValidationMode
ChecksumValidationMode
Definition: decode.h:41
AFPIfaceConfig_::xdp_filter_file
const char * xdp_filter_file
Definition: source-af-packet.h:117
AFPIfaceConfig_::v2_block_size
int v2_block_size
Definition: source-af-packet.h:102
AFPPacketVars_::relptr
void * relptr
Definition: source-af-packet.h:155
AFPIfaceConfig_::buffer_size
int buffer_size
Definition: source-af-packet.h:94
AFP_IFACE_NAME_LENGTH
#define AFP_IFACE_NAME_LENGTH
Definition: source-af-packet.h:72
AFPIfaceConfig_::iface
char iface[AFP_IFACE_NAME_LENGTH]
Definition: source-af-packet.h:90
AFPIfaceConfig_::copy_mode
uint8_t copy_mode
Definition: source-af-packet.h:110
AFPPacketVars_
per packet AF_PACKET vars
Definition: source-af-packet.h:154
AFPIfaceConfig_
Definition: source-af-packet.h:89
AFPIfaceConfig_::bpf_filter
const char * bpf_filter
Definition: source-af-packet.h:112
AFPPeer_::iface
char iface[AFP_IFACE_NAME_LENGTH]
Definition: source-af-packet.h:144
AFPIfaceConfig_::ebpf_lb_file
const char * ebpf_lb_file
Definition: source-af-packet.h:113
AFPIfaceConfig_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(unsigned int, ref)
AFPPacketVars_::mpeer
AFPPeer * mpeer
Definition: source-af-packet.h:160
SCMutex
#define SCMutex
Definition: threads-debug.h:114
AFPIsFanoutSupported
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...
Definition: source-af-packet.c:1877