4 * Copyright (C) 2012 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 * @(#)ip_state.h 1.3 1/12/96 (C) 1995 Darren Reed
10 * Id: ip_state.h,v 2.68.2.10 2007/10/16 09:33:24 darrenr Exp $
12 #ifndef __IP_STATE_H__
13 #define __IP_STATE_H__
15 #if defined(__STDC__) || defined(__GNUC__) || defined(_AIX51)
16 # define SIOCDELST _IOW('r', 61, struct ipfobj)
18 # define SIOCDELST _IOW(r, 61, struct ipfobj)
24 # define IPSTATE_SIZE 5737
27 # define IPSTATE_MAX 4013 /* Maximum number of states held */
30 #define PAIRS(s1,d1,s2,d2) ((((s1) == (s2)) && ((d1) == (d2))) ||\
31 (((s1) == (d2)) && ((d1) == (s2))))
32 #define IPPAIR(s1,d1,s2,d2) PAIRS((s1).s_addr, (d1).s_addr, \
33 (s2).s_addr, (d2).s_addr)
36 typedef struct ipstate {
38 struct ipstate *is_next;
39 struct ipstate **is_pnext;
40 struct ipstate *is_hnext;
41 struct ipstate **is_phnext;
42 struct ipstate **is_me;
46 struct ipftq *is_tqehead[2];
47 struct ipscan *is_isc;
50 U_QUAD_T is_icmppkts[4];
51 struct ipftqent is_sti;
53 int is_ref; /* reference count */
59 u_char is_p; /* Protocol */
64 u_32_t is_opt[2]; /* packet options set */
65 u_32_t is_optmsk[2]; /* " " mask */
66 u_short is_sec; /* security options set */
67 u_short is_secmsk; /* " " mask */
68 u_short is_auth; /* authentication options set */
69 u_short is_authmsk; /* " " mask */
78 u_32_t is_rulen; /* rule number when created */
83 char is_group[FR_GROUPLEN];
85 char is_ifname[4][LIFNAMSIZ];
88 #define is_die is_sti.tqe_die
89 #define is_state is_sti.tqe_state
90 #define is_touched is_sti.tqe_touched
91 #define is_saddr is_src.in4.s_addr
92 #define is_daddr is_dst.in4.s_addr
93 #define is_icmp is_ps.is_ics
94 #define is_type is_icmp.ici_type
95 #define is_tcp is_ps.is_ts
96 #define is_udp is_ps.is_us
97 #define is_send is_tcp.ts_data[0].td_end
98 #define is_dend is_tcp.ts_data[1].td_end
99 #define is_maxswin is_tcp.ts_data[0].td_maxwin
100 #define is_maxdwin is_tcp.ts_data[1].td_maxwin
101 #define is_maxsend is_tcp.ts_data[0].td_maxend
102 #define is_maxdend is_tcp.ts_data[1].td_maxend
103 #define is_swinscale is_tcp.ts_data[0].td_winscale
104 #define is_dwinscale is_tcp.ts_data[1].td_winscale
105 #define is_swinflags is_tcp.ts_data[0].td_winflags
106 #define is_dwinflags is_tcp.ts_data[1].td_winflags
107 #define is_sport is_tcp.ts_sport
108 #define is_dport is_tcp.ts_dport
109 #define is_ifpin is_ifp[0]
110 #define is_ifpout is_ifp[2]
111 #define is_gre is_ps.is_ug
112 #define is_call is_gre.gs_call
114 #define IS_WSPORT SI_W_SPORT /* 0x00100 */
115 #define IS_WDPORT SI_W_DPORT /* 0x00200 */
116 #define IS_WSADDR SI_W_SADDR /* 0x00400 */
117 #define IS_WDADDR SI_W_DADDR /* 0x00800 */
118 #define IS_NEWFR SI_NEWFR /* 0x01000 */
119 #define IS_CLONE SI_CLONE /* 0x02000 */
120 #define IS_CLONED SI_CLONED /* 0x04000 */
121 #define IS_TCPFSM 0x10000
122 #define IS_STRICT 0x20000
123 #define IS_ISNSYN 0x40000
124 #define IS_ISNACK 0x80000
125 #define IS_STATESYNC 0x100000
126 #define IS_LOOSE 0x200000
128 * IS_SC flags are for scan-operations that need to be recognised in state.
130 #define IS_SC_CLIENT 0x10000000
131 #define IS_SC_SERVER 0x20000000
132 #define IS_SC_MATCHC 0x40000000
133 #define IS_SC_MATCHS 0x80000000
134 #define IS_SC_MATCHALL (IS_SC_MATCHC|IS_SC_MATCHC)
135 #define IS_SC_ALL (IS_SC_MATCHC|IS_SC_MATCHC|IS_SC_CLIENT|IS_SC_SERVER)
138 * Flags that can be passed into ipf_addstate
140 #define IS_INHERITED 0x0fffff00
142 #define TH_OPENING (TH_SYN|TH_ACK)
145 * Bits 0 - 3 are use as a mask with the current packet's bits to check for
146 * whether it is short, tcp/udp, a fragment or the presence of IP options.
147 * Bits 4 - 7 are set from the initial packet and contain what the packet
148 * anded with bits 0-3 must match.
149 * Bits 8,9 are used to indicate wildcard source/destination port matching.
150 * Bits 10,11 are reserved for other wildcard flag compatibility.
151 * Bits 12,13 are for scaning.
154 typedef struct ipstate_save {
156 struct ipstate ips_is;
157 struct frentry ips_fr;
160 #define ips_rule ips_is.is_rule
163 typedef struct ipslog {
164 U_QUAD_T isl_pkts[4];
165 U_QUAD_T isl_bytes[4];
171 u_short isl_filler[2];
172 u_short isl_ports[2];
180 char isl_group[FR_GROUPLEN];
183 #define isl_sport isl_ps.isl_ports[0]
184 #define isl_dport isl_ps.isl_ports[1]
185 #define isl_itype isl_ps.isl_icmp
189 #define ISL_STATECHANGE 2
190 #define ISL_EXPIRE 0xffff
191 #define ISL_FLUSH 0xfffe
192 #define ISL_REMOVE 0xfffd
193 #define ISL_INTERMEDIATE 0xfffc
194 #define ISL_KILLED 0xfffb
195 #define ISL_ORPHAN 0xfffa
196 #define ISL_UNLOAD 0xfff9
199 typedef struct ips_stat {
201 u_int iss_active_proto[256];
204 u_long iss_add_locked;
206 u_long iss_bucket_full;
207 u_long iss_check_bad;
208 u_long iss_check_miss;
209 u_long iss_check_nattag;
210 u_long iss_check_notag;
211 u_long iss_clone_nomem;
215 u_long iss_flush_all;
216 u_long iss_flush_closing;
217 u_long iss_flush_queue;
218 u_long iss_flush_state;
219 u_long iss_flush_timeout;
221 u_long iss_icmp6_icmperr;
222 u_long iss_icmp6_miss;
223 u_long iss_icmp6_notinfo;
224 u_long iss_icmp6_notquery;
226 u_long iss_icmp_banned;
227 u_long iss_icmp_headblock;
228 u_long iss_icmp_hits;
229 u_long iss_icmp_icmperr;
230 u_long iss_icmp_miss;
231 u_long iss_icmp_notquery;
232 u_long iss_icmp_short;
233 u_long iss_icmp_toomany;
238 u_long iss_lookup_badifp;
239 u_long iss_lookup_badport;
240 u_long iss_lookup_miss;
243 u_long iss_max_track;
244 u_long iss_miss_mask;
248 u_long iss_proto[256];
249 u_long iss_scan_block;
250 u_long iss_state_max;
251 u_long iss_state_size;
252 u_long iss_states[IPF_TCP_NSTATES];
253 ipstate_t **iss_table;
254 u_long iss_tcp_closing;
256 u_long iss_tcp_rstadd;
257 u_long iss_tcp_toosmall;
258 u_long iss_tcp_badopt;
260 u_long iss_tcp_strict;
265 u_int *iss_bucketlen;
269 typedef struct ipf_state_softc_s {
270 ipfmutex_t ipf_stinsert;
271 int ipf_state_logging;
273 int ipf_state_doflush;
274 u_int ipf_state_inited;
276 u_int ipf_state_maxbucket;
277 u_int ipf_state_size;
278 u_int ipf_state_wm_freq;
279 u_int ipf_state_wm_high;
280 u_int ipf_state_wm_low;
281 u_int ipf_state_wm_last;
282 u_long *ipf_state_seed;
283 ipstate_t *ipf_state_list;
284 ipstate_t **ipf_state_table;
285 ipftuneable_t *ipf_state_tune;
286 ipftq_t *ipf_state_usertq;
287 ipftq_t ipf_state_pending;
288 ipftq_t ipf_state_deletetq;
289 ipftq_t ipf_state_udptq;
290 ipftq_t ipf_state_udpacktq;
291 ipftq_t ipf_state_iptq;
292 ipftq_t ipf_state_icmptq;
293 ipftq_t ipf_state_icmpacktq;
294 ipftq_t ipf_state_tcptq[IPF_TCP_NSTATES];
295 ips_stat_t ipf_state_stats;
300 extern void ipf_state_dump __P((ipf_main_softc_t *, void *));
302 extern int ipf_tcp_age __P((struct ipftqent *, struct fr_info *,
303 struct ipftq *, int, int));
304 extern int ipf_tcpinwindow __P((struct fr_info *, struct tcpdata *,
305 struct tcpdata *, tcphdr_t *, int));
307 extern int ipf_state_add __P((ipf_main_softc_t *, fr_info_t *,
308 ipstate_t **, u_int));
309 extern frentry_t *ipf_state_check __P((struct fr_info *, u_32_t *));
310 extern void ipf_state_deref __P((ipf_main_softc_t *, ipstate_t **));
311 extern void ipf_state_expire __P((ipf_main_softc_t *));
312 extern int ipf_state_flush __P((ipf_main_softc_t *, int, int));
313 extern ipstate_t *ipf_state_lookup __P((fr_info_t *, tcphdr_t *, ipftq_t **));
314 extern int ipf_state_init __P((void));
315 extern int ipf_state_insert __P((ipf_main_softc_t *, struct ipstate *, int));
316 extern int ipf_state_ioctl __P((ipf_main_softc_t *, caddr_t, ioctlcmd_t, int, int, void *));
317 extern void ipf_state_log __P((ipf_main_softc_t *, struct ipstate *, u_int));
318 extern int ipf_state_matchflush __P((ipf_main_softc_t *, caddr_t));
319 extern int ipf_state_rehash __P((ipf_main_softc_t *, ipftuneable_t *, ipftuneval_t *));
320 extern void ipf_state_setqueue __P((ipf_main_softc_t *, ipstate_t *, int));
321 extern void ipf_state_setpending __P((ipf_main_softc_t *, ipstate_t *));
322 extern int ipf_state_settimeout __P((struct ipf_main_softc_s *, ipftuneable_t *, ipftuneval_t *));
323 extern void ipf_state_sync __P((ipf_main_softc_t *, void *));
324 extern void ipf_state_update __P((fr_info_t *, ipstate_t *));
326 extern void ipf_sttab_init __P((ipf_main_softc_t *, struct ipftq *));
327 extern void ipf_sttab_destroy __P((struct ipftq *));
328 extern void ipf_state_setlock __P((void *, int));
329 extern int ipf_state_main_load __P((void));
330 extern int ipf_state_main_unload __P((void));
331 extern void *ipf_state_soft_create __P((ipf_main_softc_t *));
332 extern void ipf_state_soft_destroy __P((ipf_main_softc_t *, void *));
333 extern int ipf_state_soft_init __P((ipf_main_softc_t *, void *));
334 extern int ipf_state_soft_fini __P((ipf_main_softc_t *, void *));
335 extern ipftq_t *ipf_state_add_tq __P((ipf_main_softc_t *, int));
337 #endif /* __IP_STATE_H__ */