1/*	$FreeBSD: releng/10.3/sys/contrib/ipfilter/netinet/ip_state.h 255332 2013-09-06 23:11:19Z cy $	*/
2
3/*
4 * Copyright (C) 2012 by Darren Reed.
5 *
6 * See the IPFILTER.LICENCE file for details on licencing.
7 *
8 * @(#)ip_state.h	1.3 1/12/96 (C) 1995 Darren Reed
9 * $FreeBSD: releng/10.3/sys/contrib/ipfilter/netinet/ip_state.h 255332 2013-09-06 23:11:19Z cy $
10 * Id: ip_state.h,v 2.68.2.10 2007/10/16 09:33:24 darrenr Exp $
11 */
12#ifndef	__IP_STATE_H__
13#define	__IP_STATE_H__
14
15#if defined(__STDC__) || defined(__GNUC__) || defined(_AIX51)
16# define	SIOCDELST	_IOW('r', 61, struct ipfobj)
17#else
18# define	SIOCDELST	_IOW(r, 61, struct ipfobj)
19#endif
20
21struct ipscan;
22
23#ifndef	IPSTATE_SIZE
24# define	IPSTATE_SIZE	5737
25#endif
26#ifndef	IPSTATE_MAX
27# define	IPSTATE_MAX	4013	/* Maximum number of states held */
28#endif
29
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)
34
35
36typedef struct ipstate {
37	ipfmutex_t	is_lock;
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;
43	void		*is_ifp[4];
44	void		*is_sync;
45	frentry_t	*is_rule;
46	struct	ipftq	*is_tqehead[2];
47	struct	ipscan	*is_isc;
48	U_QUAD_T	is_pkts[4];
49	U_QUAD_T	is_bytes[4];
50	U_QUAD_T	is_icmppkts[4];
51	struct	ipftqent is_sti;
52	u_int	is_frage[2];
53	int	is_ref;			/* reference count */
54	int	is_isninc[2];
55	u_short	is_sumd[2];
56	i6addr_t	is_src;
57	i6addr_t	is_dst;
58	u_int	is_pass;
59	u_char	is_p;			/* Protocol */
60	u_char	is_v;
61	int	is_family;
62	u_32_t	is_hv;
63	u_32_t	is_tag;
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 */
70	union {
71		icmpinfo_t	is_ics;
72		tcpinfo_t	is_ts;
73		udpinfo_t	is_us;
74		greinfo_t	is_ug;
75	} is_ps;
76	u_32_t	is_flags;
77	int	is_flx[2][2];
78	u_32_t	is_rulen;		/* rule number when created */
79	u_32_t	is_s0[2];
80	u_short	is_smsk[2];
81	frdest_t	is_dif;
82	frdest_t	is_tifs[2];
83	char	is_group[FR_GROUPLEN];
84	char	is_sbuf[2][16];
85	char	is_ifname[4][LIFNAMSIZ];
86} ipstate_t;
87
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
113
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
127/*
128 * IS_SC flags are for scan-operations that need to be recognised in state.
129 */
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)
136
137/*
138 * Flags that can be passed into ipf_addstate
139 */
140#define	IS_INHERITED			0x0fffff00
141
142#define	TH_OPENING	(TH_SYN|TH_ACK)
143/*
144 * is_flags:
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.
152 */
153
154typedef	struct	ipstate_save	{
155	void	*ips_next;
156	struct	ipstate	ips_is;
157	struct	frentry	ips_fr;
158} ipstate_save_t;
159
160#define	ips_rule	ips_is.is_rule
161
162
163typedef	struct	ipslog	{
164	U_QUAD_T	isl_pkts[4];
165	U_QUAD_T	isl_bytes[4];
166	i6addr_t	isl_src;
167	i6addr_t	isl_dst;
168	u_32_t	isl_tag;
169	u_short	isl_type;
170	union {
171		u_short	isl_filler[2];
172		u_short	isl_ports[2];
173		u_short	isl_icmp;
174	} isl_ps;
175	u_char	isl_v;
176	u_char	isl_p;
177	u_char	isl_flags;
178	u_char	isl_state[2];
179	u_32_t	isl_rulen;
180	char	isl_group[FR_GROUPLEN];
181} ipslog_t;
182
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
186
187#define	ISL_NEW			0
188#define	ISL_CLONE		1
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
197
198
199typedef	struct	ips_stat {
200	u_int	iss_active;
201	u_int	iss_active_proto[256];
202	u_long	iss_add_bad;
203	u_long	iss_add_dup;
204	u_long	iss_add_locked;
205	u_long	iss_add_oow;
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;
212	u_long	iss_cloned;
213	u_long	iss_expire;
214	u_long	iss_fin;
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;
220	u_long	iss_hits;
221	u_long	iss_icmp6_icmperr;
222	u_long	iss_icmp6_miss;
223	u_long	iss_icmp6_notinfo;
224	u_long	iss_icmp6_notquery;
225	u_long	iss_icmp_bad;
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;
234	u_int	iss_inuse;
235	ipstate_t *iss_list;
236	u_long	iss_log_fail;
237	u_long	iss_log_ok;
238	u_long	iss_lookup_badifp;
239	u_long	iss_lookup_badport;
240	u_long	iss_lookup_miss;
241	u_long	iss_max;
242	u_long	iss_max_ref;
243	u_long	iss_max_track;
244	u_long	iss_miss_mask;
245	u_long	iss_nomem;
246	u_long	iss_oow;
247	u_long	iss_orphan;
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;
255	u_long	iss_tcp_oow;
256	u_long	iss_tcp_rstadd;
257	u_long	iss_tcp_toosmall;
258	u_long	iss_tcp_badopt;
259	u_long	iss_tcp_fsm;
260	u_long	iss_tcp_strict;
261	ipftq_t	*iss_tcptab;
262	u_int	iss_ticks;
263	u_long	iss_wild;
264	u_long	iss_winsack;
265	u_int	*iss_bucketlen;
266} ips_stat_t;
267
268
269typedef struct ipf_state_softc_s {
270	ipfmutex_t	ipf_stinsert;
271	int		ipf_state_logging;
272	int		ipf_state_lock;
273	int		ipf_state_doflush;
274	u_int		ipf_state_inited;
275	u_int		ipf_state_max;
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;
296} ipf_state_softc_t;
297
298
299#ifndef _KERNEL
300extern	void	ipf_state_dump __P((ipf_main_softc_t *, void *));
301#endif
302extern	int	ipf_tcp_age __P((struct ipftqent *, struct fr_info *,
303				struct ipftq *, int, int));
304extern	int	ipf_tcpinwindow __P((struct fr_info *, struct tcpdata *,
305				    struct tcpdata *, tcphdr_t *, int));
306
307extern	int	ipf_state_add __P((ipf_main_softc_t *, fr_info_t *,
308				   ipstate_t **, u_int));
309extern	frentry_t *ipf_state_check __P((struct fr_info *, u_32_t *));
310extern	void	ipf_state_deref __P((ipf_main_softc_t *, ipstate_t **));
311extern	void	ipf_state_expire __P((ipf_main_softc_t *));
312extern	int	ipf_state_flush __P((ipf_main_softc_t *, int, int));
313extern	ipstate_t *ipf_state_lookup __P((fr_info_t *, tcphdr_t *, ipftq_t **));
314extern	int	ipf_state_init __P((void));
315extern	int	ipf_state_insert __P((ipf_main_softc_t *, struct ipstate *, int));
316extern	int	ipf_state_ioctl __P((ipf_main_softc_t *, caddr_t, ioctlcmd_t, int, int, void *));
317extern	void	ipf_state_log __P((ipf_main_softc_t *, struct ipstate *, u_int));
318extern	int	ipf_state_matchflush __P((ipf_main_softc_t *, caddr_t));
319extern	int	ipf_state_rehash __P((ipf_main_softc_t *, ipftuneable_t *, ipftuneval_t *));
320extern	void	ipf_state_setqueue __P((ipf_main_softc_t *, ipstate_t *, int));
321extern	void	ipf_state_setpending __P((ipf_main_softc_t *, ipstate_t *));
322extern	int	ipf_state_settimeout __P((struct ipf_main_softc_s *, ipftuneable_t *, ipftuneval_t *));
323extern	void	ipf_state_sync __P((ipf_main_softc_t *, void *));
324extern	void	ipf_state_update __P((fr_info_t *, ipstate_t *));
325
326extern	void	ipf_sttab_init __P((ipf_main_softc_t *, struct ipftq *));
327extern	void	ipf_sttab_destroy __P((struct ipftq *));
328extern	void	ipf_state_setlock __P((void *, int));
329extern	int	ipf_state_main_load __P((void));
330extern	int	ipf_state_main_unload __P((void));
331extern	void	*ipf_state_soft_create __P((ipf_main_softc_t *));
332extern	void	ipf_state_soft_destroy __P((ipf_main_softc_t *, void *));
333extern	int	ipf_state_soft_init __P((ipf_main_softc_t *, void *));
334extern	int	ipf_state_soft_fini __P((ipf_main_softc_t *, void *));
335extern	ipftq_t	*ipf_state_add_tq __P((ipf_main_softc_t *, int));
336
337#endif /* __IP_STATE_H__ */
338