1
2/*
3 * Copyright (C) 2012 by Darren Reed.
4 *
5 * See the IPFILTER.LICENCE file for details on licencing.
6 * Id: ip_state.h,v 2.68.2.10 2007/10/16 09:33:24 darrenr Exp $
7 */
8#ifndef	__IP_STATE_H__
9#define	__IP_STATE_H__
10
11# define	SIOCDELST	_IOW('r', 61, struct ipfobj)
12
13struct ipscan;
14
15#ifndef	IPSTATE_SIZE
16# define	IPSTATE_SIZE	5737
17#endif
18#ifndef	IPSTATE_MAX
19# define	IPSTATE_MAX	4013	/* Maximum number of states held */
20#endif
21
22#define	PAIRS(s1,d1,s2,d2)	((((s1) == (s2)) && ((d1) == (d2))) ||\
23				 (((s1) == (d2)) && ((d1) == (s2))))
24#define	IPPAIR(s1,d1,s2,d2)	PAIRS((s1).s_addr, (d1).s_addr, \
25				      (s2).s_addr, (d2).s_addr)
26
27
28typedef struct ipstate {
29	ipfmutex_t	is_lock;
30	struct	ipstate	*is_next;
31	struct	ipstate	**is_pnext;
32	struct	ipstate	*is_hnext;
33	struct	ipstate	**is_phnext;
34	struct	ipstate	**is_me;
35	void		*is_ifp[4];
36	void		*is_sync;
37	frentry_t	*is_rule;
38	struct	ipftq	*is_tqehead[2];
39	struct	ipscan	*is_isc;
40	U_QUAD_T	is_pkts[4];
41	U_QUAD_T	is_bytes[4];
42	U_QUAD_T	is_icmppkts[4];
43	struct	ipftqent is_sti;
44	u_int	is_frage[2];
45	int	is_ref;			/* reference count */
46	int	is_isninc[2];
47	u_short	is_sumd[2];
48	i6addr_t	is_src;
49	i6addr_t	is_dst;
50	u_int	is_pass;
51	u_char	is_p;			/* Protocol */
52	u_char	is_v;
53	int	is_family;
54	u_32_t	is_hv;
55	u_32_t	is_tag;
56	u_32_t	is_opt[2];		/* packet options set */
57	u_32_t	is_optmsk[2];		/*    "      "    mask */
58	u_short	is_sec;			/* security options set */
59	u_short	is_secmsk;		/*    "        "    mask */
60	u_short	is_auth;		/* authentication options set */
61	u_short	is_authmsk;		/*    "              "    mask */
62	union {
63		icmpinfo_t	is_ics;
64		tcpinfo_t	is_ts;
65		udpinfo_t	is_us;
66		greinfo_t	is_ug;
67	} is_ps;
68	u_32_t	is_flags;
69	int	is_flx[2][2];
70	u_32_t	is_rulen;		/* rule number when created */
71	u_32_t	is_s0[2];
72	u_short	is_smsk[2];
73	frdest_t	is_dif;
74	frdest_t	is_tifs[2];
75	char	is_group[FR_GROUPLEN];
76	char	is_sbuf[2][16];
77	char	is_ifname[4][LIFNAMSIZ];
78} ipstate_t;
79
80#define	is_die		is_sti.tqe_die
81#define	is_state	is_sti.tqe_state
82#define	is_touched	is_sti.tqe_touched
83#define	is_saddr	is_src.in4.s_addr
84#define	is_daddr	is_dst.in4.s_addr
85#define	is_icmp		is_ps.is_ics
86#define	is_type		is_icmp.ici_type
87#define	is_tcp		is_ps.is_ts
88#define	is_udp		is_ps.is_us
89#define is_send		is_tcp.ts_data[0].td_end
90#define is_dend		is_tcp.ts_data[1].td_end
91#define is_maxswin	is_tcp.ts_data[0].td_maxwin
92#define is_maxdwin	is_tcp.ts_data[1].td_maxwin
93#define is_maxsend	is_tcp.ts_data[0].td_maxend
94#define is_maxdend	is_tcp.ts_data[1].td_maxend
95#define	is_swinscale	is_tcp.ts_data[0].td_winscale
96#define	is_dwinscale	is_tcp.ts_data[1].td_winscale
97#define	is_swinflags	is_tcp.ts_data[0].td_winflags
98#define	is_dwinflags	is_tcp.ts_data[1].td_winflags
99#define	is_sport	is_tcp.ts_sport
100#define	is_dport	is_tcp.ts_dport
101#define	is_ifpin	is_ifp[0]
102#define	is_ifpout	is_ifp[2]
103#define	is_gre		is_ps.is_ug
104#define	is_call		is_gre.gs_call
105
106#define	IS_WSPORT	SI_W_SPORT	/* 0x00100 */
107#define	IS_WDPORT	SI_W_DPORT	/* 0x00200 */
108#define	IS_WSADDR	SI_W_SADDR	/* 0x00400 */
109#define	IS_WDADDR	SI_W_DADDR	/* 0x00800 */
110#define	IS_NEWFR	SI_NEWFR	/* 0x01000 */
111#define	IS_CLONE	SI_CLONE	/* 0x02000 */
112#define	IS_CLONED	SI_CLONED	/* 0x04000 */
113#define	IS_TCPFSM			   0x10000
114#define	IS_STRICT			   0x20000
115#define	IS_ISNSYN			   0x40000
116#define	IS_ISNACK			   0x80000
117#define	IS_STATESYNC			   0x100000
118#define	IS_LOOSE			   0x200000
119/*
120 * IS_SC flags are for scan-operations that need to be recognised in state.
121 */
122#define	IS_SC_CLIENT			0x10000000
123#define	IS_SC_SERVER			0x20000000
124#define	IS_SC_MATCHC			0x40000000
125#define	IS_SC_MATCHS			0x80000000
126#define	IS_SC_MATCHALL	(IS_SC_MATCHC|IS_SC_MATCHC)
127#define	IS_SC_ALL	(IS_SC_MATCHC|IS_SC_MATCHC|IS_SC_CLIENT|IS_SC_SERVER)
128
129/*
130 * Flags that can be passed into ipf_addstate
131 */
132#define	IS_INHERITED			0x0fffff00
133
134#define	TH_OPENING	(TH_SYN|TH_ACK)
135/*
136 * is_flags:
137 * Bits 0 - 3 are use as a mask with the current packet's bits to check for
138 * whether it is short, tcp/udp, a fragment or the presence of IP options.
139 * Bits 4 - 7 are set from the initial packet and contain what the packet
140 * anded with bits 0-3 must match.
141 * Bits 8,9 are used to indicate wildcard source/destination port matching.
142 * Bits 10,11 are reserved for other wildcard flag compatibility.
143 * Bits 12,13 are for scaning.
144 */
145
146typedef	struct	ipstate_save	{
147	void	*ips_next;
148	struct	ipstate	ips_is;
149	struct	frentry	ips_fr;
150} ipstate_save_t;
151
152#define	ips_rule	ips_is.is_rule
153
154
155typedef	struct	ipslog	{
156	U_QUAD_T	isl_pkts[4];
157	U_QUAD_T	isl_bytes[4];
158	i6addr_t	isl_src;
159	i6addr_t	isl_dst;
160	u_32_t	isl_tag;
161	u_short	isl_type;
162	union {
163		u_short	isl_filler[2];
164		u_short	isl_ports[2];
165		u_short	isl_icmp;
166	} isl_ps;
167	u_char	isl_v;
168	u_char	isl_p;
169	u_char	isl_flags;
170	u_char	isl_state[2];
171	u_32_t	isl_rulen;
172	char	isl_group[FR_GROUPLEN];
173} ipslog_t;
174
175#define	isl_sport	isl_ps.isl_ports[0]
176#define	isl_dport	isl_ps.isl_ports[1]
177#define	isl_itype	isl_ps.isl_icmp
178
179#define	ISL_NEW			0
180#define	ISL_CLONE		1
181#define	ISL_STATECHANGE		2
182#define	ISL_EXPIRE		0xffff
183#define	ISL_FLUSH		0xfffe
184#define	ISL_REMOVE		0xfffd
185#define	ISL_INTERMEDIATE	0xfffc
186#define	ISL_KILLED		0xfffb
187#define	ISL_ORPHAN		0xfffa
188#define	ISL_UNLOAD		0xfff9
189
190
191typedef	struct	ips_stat {
192	u_int	iss_active;
193	u_int	iss_active_proto[256];
194	u_long	iss_add_bad;
195	u_long	iss_add_dup;
196	u_long	iss_add_locked;
197	u_long	iss_add_oow;
198	u_long	iss_bucket_full;
199	u_long	iss_check_bad;
200	u_long	iss_check_miss;
201	u_long	iss_check_nattag;
202	u_long	iss_check_notag;
203	u_long	iss_clone_nomem;
204	u_long	iss_cloned;
205	u_long	iss_expire;
206	u_long	iss_fin;
207	u_long	iss_flush_all;
208	u_long	iss_flush_closing;
209	u_long	iss_flush_queue;
210	u_long	iss_flush_state;
211	u_long	iss_flush_timeout;
212	u_long	iss_hits;
213	u_long	iss_icmp6_icmperr;
214	u_long	iss_icmp6_miss;
215	u_long	iss_icmp6_notinfo;
216	u_long	iss_icmp6_notquery;
217	u_long	iss_icmp_bad;
218	u_long	iss_icmp_banned;
219	u_long	iss_icmp_headblock;
220	u_long	iss_icmp_hits;
221	u_long	iss_icmp_icmperr;
222	u_long	iss_icmp_miss;
223	u_long	iss_icmp_notquery;
224	u_long	iss_icmp_short;
225	u_long	iss_icmp_toomany;
226	u_int	iss_inuse;
227	ipstate_t *iss_list;
228	u_long	iss_log_fail;
229	u_long	iss_log_ok;
230	u_long	iss_lookup_badifp;
231	u_long	iss_lookup_badport;
232	u_long	iss_lookup_miss;
233	u_long	iss_max;
234	u_long	iss_max_ref;
235	u_long	iss_max_track;
236	u_long	iss_miss_mask;
237	u_long	iss_nomem;
238	u_long	iss_oow;
239	u_long	iss_orphan;
240	u_long	iss_proto[256];
241	u_long	iss_scan_block;
242	u_long	iss_state_max;
243	u_long	iss_state_size;
244	u_long	iss_states[IPF_TCP_NSTATES];
245	ipstate_t **iss_table;
246	u_long	iss_tcp_closing;
247	u_long	iss_tcp_oow;
248	u_long	iss_tcp_rstadd;
249	u_long	iss_tcp_toosmall;
250	u_long	iss_tcp_badopt;
251	u_long	iss_tcp_fsm;
252	u_long	iss_tcp_strict;
253	ipftq_t	*iss_tcptab;
254	u_int	iss_ticks;
255	u_long	iss_wild;
256	u_long	iss_winsack;
257	u_int	*iss_bucketlen;
258} ips_stat_t;
259
260
261typedef struct ipf_state_softc_s {
262	ipfmutex_t	ipf_stinsert;
263	int		ipf_state_logging;
264	int		ipf_state_lock;
265	int		ipf_state_doflush;
266	u_int		ipf_state_inited;
267	u_int		ipf_state_max;
268	u_int		ipf_state_maxbucket;
269	u_int		ipf_state_size;
270	u_int		ipf_state_wm_freq;
271	u_int		ipf_state_wm_high;
272	u_int		ipf_state_wm_low;
273	u_int		ipf_state_wm_last;
274	u_long		*ipf_state_seed;
275	ipstate_t	*ipf_state_list;
276	ipstate_t	**ipf_state_table;
277	ipftuneable_t	*ipf_state_tune;
278	ipftq_t		*ipf_state_usertq;
279	ipftq_t		ipf_state_pending;
280	ipftq_t		ipf_state_deletetq;
281	ipftq_t		ipf_state_udptq;
282	ipftq_t		ipf_state_udpacktq;
283	ipftq_t		ipf_state_iptq;
284	ipftq_t		ipf_state_icmptq;
285	ipftq_t		ipf_state_icmpacktq;
286	ipftq_t		ipf_state_tcptq[IPF_TCP_NSTATES];
287	ips_stat_t	ipf_state_stats;
288} ipf_state_softc_t;
289
290
291#ifndef _KERNEL
292extern	void	ipf_state_dump(ipf_main_softc_t *, void *);
293#endif
294extern	int	ipf_tcp_age(struct ipftqent *, struct fr_info *,
295				struct ipftq *, int, int);
296extern	int	ipf_tcpinwindow(struct fr_info *, struct tcpdata *,
297				    struct tcpdata *, tcphdr_t *, int);
298
299extern	int	ipf_state_add(ipf_main_softc_t *, fr_info_t *,
300				   ipstate_t **, u_int);
301extern	frentry_t *ipf_state_check(struct fr_info *, u_32_t *);
302extern	void	ipf_state_deref(ipf_main_softc_t *, ipstate_t **);
303extern	void	ipf_state_expire(ipf_main_softc_t *);
304extern	int	ipf_state_flush(ipf_main_softc_t *, int, int);
305extern	ipstate_t *ipf_state_lookup(fr_info_t *, tcphdr_t *, ipftq_t **);
306extern	int	ipf_state_init(void);
307extern	int	ipf_state_insert(ipf_main_softc_t *, struct ipstate *, int);
308extern	int	ipf_state_ioctl(ipf_main_softc_t *, caddr_t, ioctlcmd_t, int, int, void *);
309extern	void	ipf_state_log(ipf_main_softc_t *, struct ipstate *, u_int);
310extern	int	ipf_state_matchflush(ipf_main_softc_t *, caddr_t);
311extern	int	ipf_state_rehash(ipf_main_softc_t *, ipftuneable_t *, ipftuneval_t *);
312extern	void	ipf_state_setqueue(ipf_main_softc_t *, ipstate_t *, int);
313extern	void	ipf_state_setpending(ipf_main_softc_t *, ipstate_t *);
314extern	int	ipf_state_settimeout(struct ipf_main_softc_s *, ipftuneable_t *, ipftuneval_t *);
315extern	void	ipf_state_sync(ipf_main_softc_t *, void *);
316extern	void	ipf_state_update(fr_info_t *, ipstate_t *);
317
318extern	void	ipf_sttab_init(ipf_main_softc_t *, struct ipftq *);
319extern	void	ipf_sttab_destroy(struct ipftq *);
320extern	void	ipf_state_setlock(void *, int);
321extern	int	ipf_state_main_load(void);
322extern	int	ipf_state_main_unload(void);
323extern	void	*ipf_state_soft_create(ipf_main_softc_t *);
324extern	void	ipf_state_soft_destroy(ipf_main_softc_t *, void *);
325extern	int	ipf_state_soft_init(ipf_main_softc_t *, void *);
326extern	int	ipf_state_soft_fini(ipf_main_softc_t *, void *);
327extern	ipftq_t	*ipf_state_add_tq(ipf_main_softc_t *, int);
328
329#endif /* __IP_STATE_H__ */
330