1 /* Copyright (C) 2011,2012 Open Information Security Foundation
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
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.
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
21 * \author Eric Leblond <eric@regit.org>
24 #ifndef __SOURCE_AFP_H__
25 #define __SOURCE_AFP_H__
27 #ifndef HAVE_PACKET_FANOUT /* not defined if linux/if_packet.h trying to force */
28 #define HAVE_PACKET_FANOUT 1
30 #define PACKET_FANOUT 18
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
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 */
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
;
58 #define AFP_NEED_PEER (1 << 0)
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)
68 #define AFP_COPY_MODE_NONE 0
69 #define AFP_COPY_MODE_TAP 1
70 #define AFP_COPY_MODE_IPS 2
72 #define AFP_IFACE_NAME_LENGTH 48
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 3
80 typedef struct AFPIfaceConfig_
82 char iface
[AFP_IFACE_NAME_LENGTH
];
83 /* number of threads */
85 /* socket buffer size */
87 /* ring size in number of packets */
89 /* block size for tpacket_v3 in */
91 /* block timeout for tpacket_v3 in milliseconds */
98 /* misc use flags including ring mode */
101 ChecksumValidationMode checksum_mode
;
102 const char *bpf_filter
;
103 const char *ebpf_lb_file
;
105 const char *ebpf_filter_file
;
107 const char *xdp_filter_file
;
110 const char *out_iface
;
111 #ifdef HAVE_PACKET_EBPF
112 struct ebpf_timeout_config ebpf_t_config
;
114 SC_ATOMIC_DECLARE(unsigned int, ref
);
115 void (*DerefFunc
)(void *);
123 typedef struct AFPPeer_
{
124 SC_ATOMIC_DECLARE(int, socket
);
125 SC_ATOMIC_DECLARE(int, sock_usage
);
126 SC_ATOMIC_DECLARE(int, if_idx
);
127 SC_ATOMIC_DECLARE(uint64_t, send_errors
);
129 SCMutex sock_protect
;
130 int turn
; /**< Field used to store initialisation order. */
131 SC_ATOMIC_DECLARE(uint8_t, state
);
132 struct AFPPeer_
*peer
;
133 TAILQ_ENTRY(AFPPeer_
) next
;
134 char iface
[AFP_IFACE_NAME_LENGTH
];
138 * \brief per packet AF_PACKET vars
140 * This structure is used y the release data system and is cleaned
141 * up by the AFPV_CLEANUP macro below.
143 typedef struct AFPPacketVars_
146 AFPPeer
*peer
; /**< Sending peer for IPS/TAP mode */
147 /** Pointer to ::AFPPeer used for capture. Field is used to be able
148 * to do reference counting.
153 #ifdef HAVE_PACKET_EBPF
156 unsigned int nr_cpus
;
160 #ifdef HAVE_PACKET_EBPF
161 #define AFPV_CLEANUP(afpv) \
163 (afpv)->relptr = NULL; \
164 (afpv)->copy_mode = 0; \
165 (afpv)->vlan_tci = 0; \
166 (afpv)->peer = NULL; \
167 (afpv)->mpeer = NULL; \
168 (afpv)->v4_map_fd = -1; \
169 (afpv)->v6_map_fd = -1; \
172 #define AFPV_CLEANUP(afpv) \
174 (afpv)->relptr = NULL; \
175 (afpv)->copy_mode = 0; \
176 (afpv)->vlan_tci = 0; \
177 (afpv)->peer = NULL; \
178 (afpv)->mpeer = NULL; \
186 void TmModuleReceiveAFPRegister (void);
187 void TmModuleDecodeAFPRegister (void);
189 TmEcode
AFPPeersListInit(void);
190 TmEcode
AFPPeersListCheck(void);
191 void AFPPeersListClean(void);
192 int AFPGetLinkType(const char *ifname
);
194 int AFPIsFanoutSupported(uint16_t cluster_id
);
196 #endif /* __SOURCE_AFP_H__ */