ip_state.h revision 63523
1/*
2 * Copyright (C) 1995-2000 by Darren Reed.
3 *
4 * Redistribution and use in source and binary forms are permitted
5 * provided that this notice is preserved and due credit is given
6 * to the original author and the contributors.
7 *
8 * @(#)ip_state.h	1.3 1/12/96 (C) 1995 Darren Reed
9 * $Id: ip_state.h,v 2.13.2.1 2000/07/08 02:15:35 darrenr Exp $
10 * $FreeBSD: head/sys/contrib/ipfilter/netinet/ip_state.h 63523 2000-07-19 14:02:09Z darrenr $
11 */
12#ifndef	__IP_STATE_H__
13#define	__IP_STATE_H__
14
15#if defined(__STDC__) || defined(__GNUC__)
16# define	SIOCDELST	_IOW('r', 61, struct ipstate *)
17#else
18# define	SIOCDELST	_IOW(r, 61, struct ipstate *)
19#endif
20
21#define	IPSTATE_SIZE	5737
22#define	IPSTATE_MAX	4013	/* Maximum number of states held */
23
24#define	PAIRS(s1,d1,s2,d2)	((((s1) == (s2)) && ((d1) == (d2))) ||\
25				 (((s1) == (d2)) && ((d1) == (s2))))
26#define	IPPAIR(s1,d1,s2,d2)	PAIRS((s1).s_addr, (d1).s_addr, \
27				      (s2).s_addr, (d2).s_addr)
28
29
30typedef struct udpstate {
31	u_short	us_sport;
32	u_short	us_dport;
33} udpstate_t;
34
35typedef struct icmpstate {
36	u_short	ics_id;
37	u_short	ics_seq;
38	u_char	ics_type;
39} icmpstate_t;
40
41typedef	struct	tcpdata	{
42	u_32_t	td_end;
43	u_32_t	td_maxend;
44	u_short	td_maxwin;
45} tcpdata_t;
46
47typedef	struct tcpstate {
48	u_short	ts_sport;
49	u_short	ts_dport;
50	tcpdata_t ts_data[2];
51	u_char	ts_state[2];
52} tcpstate_t;
53
54typedef struct ipstate {
55	struct	ipstate	*is_next;
56	struct	ipstate	**is_pnext;
57	struct	ipstate	*is_hnext;
58	struct	ipstate	**is_phnext;
59	u_long	is_age;
60	u_int	is_pass;
61	U_QUAD_T	is_pkts;
62	U_QUAD_T	is_bytes;
63	void	*is_ifp[2];
64	frentry_t	*is_rule;
65	union	i6addr	is_src;
66	union	i6addr	is_dst;
67	u_char	is_p;			/* Protocol */
68	u_char	is_v;
69	u_int	is_hv;
70	u_32_t	is_flags;
71	u_32_t	is_opt;			/* packet options set */
72	u_32_t	is_optmsk;		/*    "      "    mask */
73	u_short	is_sec;			/* security options set */
74	u_short	is_secmsk;		/*    "        "    mask */
75	u_short	is_auth;		/* authentication options set */
76	u_short	is_authmsk;		/*    "              "    mask */
77	union {
78		icmpstate_t	is_ics;
79		tcpstate_t	is_ts;
80		udpstate_t	is_us;
81	} is_ps;
82	char	is_ifname[2][IFNAMSIZ];
83#if SOLARIS || defined(__sgi)
84	kmutex_t	is_lock;
85#endif
86} ipstate_t;
87
88#define	is_saddr	is_src.in4.s_addr
89#define	is_daddr	is_dst.in4.s_addr
90#define	is_icmp		is_ps.is_ics
91#define	is_type		is_icmp.ics_type
92#define	is_code		is_icmp.ics_code
93#define	is_tcp		is_ps.is_ts
94#define	is_udp		is_ps.is_us
95#define is_send		is_tcp.ts_data[0].td_end
96#define is_dend		is_tcp.ts_data[1].td_end
97#define is_maxswin	is_tcp.ts_data[0].td_maxwin
98#define is_maxdwin	is_tcp.ts_data[1].td_maxwin
99#define is_maxsend	is_tcp.ts_data[0].td_maxend
100#define is_maxdend	is_tcp.ts_data[1].td_maxend
101#define	is_sport	is_tcp.ts_sport
102#define	is_dport	is_tcp.ts_dport
103#define	is_state	is_tcp.ts_state
104#define	is_ifpin	is_ifp[0]
105#define	is_ifpout	is_ifp[1]
106
107#define	TH_OPENING	(TH_SYN|TH_ACK)
108/*
109 * is_flags:
110 * Bits 0 - 3 are use as a mask with the current packet's bits to check for
111 * whether it is short, tcp/udp, a fragment or the presence of IP options.
112 * Bits 4 - 7 are set from the initial packet and contain what the packet
113 * anded with bits 0-3 must match.
114 * Bits 8,9 are used to indicate wildcard source/destination port matching.
115 */
116
117typedef	struct	ipstate_save	{
118	void	*ips_next;
119	struct	ipstate	ips_is;
120	struct	frentry	ips_fr;
121} ipstate_save_t;
122
123#define	ips_rule	ips_is.is_rule
124
125
126typedef	struct	ipslog	{
127	U_QUAD_T	isl_pkts;
128	U_QUAD_T	isl_bytes;
129	union	i6addr	isl_src;
130	union	i6addr	isl_dst;
131	u_short	isl_type;
132	union {
133		u_short	isl_filler[2];
134		u_short	isl_ports[2];
135		u_short	isl_icmp;
136	} isl_ps;
137	u_char	isl_v;
138	u_char	isl_p;
139	u_char	isl_flags;
140	u_char	isl_state[2];
141} ipslog_t;
142
143#define	isl_sport	isl_ps.isl_ports[0]
144#define	isl_dport	isl_ps.isl_ports[1]
145#define	isl_itype	isl_ps.isl_icmp
146
147#define	ISL_NEW		0
148#define	ISL_EXPIRE	0xffff
149#define	ISL_FLUSH	0xfffe
150#define	ISL_REMOVE	0xfffd
151
152
153typedef	struct	ips_stat {
154	u_long	iss_hits;
155	u_long	iss_miss;
156	u_long	iss_max;
157	u_long	iss_tcp;
158	u_long	iss_udp;
159	u_long	iss_icmp;
160	u_long	iss_nomem;
161	u_long	iss_expire;
162	u_long	iss_fin;
163	u_long	iss_active;
164	u_long	iss_logged;
165	u_long	iss_logfail;
166	u_long	iss_inuse;
167	ipstate_t **iss_table;
168	ipstate_t *iss_list;
169} ips_stat_t;
170
171
172extern	u_long	fr_tcpidletimeout;
173extern	u_long	fr_tcpclosewait;
174extern	u_long	fr_tcplastack;
175extern	u_long	fr_tcptimeout;
176extern	u_long	fr_tcpclosed;
177extern	u_long	fr_udptimeout;
178extern	u_long	fr_icmptimeout;
179extern	int	fr_state_lock;
180extern	int	fr_stateinit __P((void));
181extern	int	fr_tcpstate __P((ipstate_t *, fr_info_t *, ip_t *, tcphdr_t *));
182extern	ipstate_t	*fr_addstate __P((ip_t *, fr_info_t *, u_int));
183extern	frentry_t	*fr_checkstate __P((ip_t *, fr_info_t *));
184extern	void	ip_statesync __P((void *));
185extern	void	fr_timeoutstate __P((void));
186extern	void	fr_tcp_age __P((u_long *, u_char *, fr_info_t *, int));
187extern	void	fr_stateunload __P((void));
188extern	void	ipstate_log __P((struct ipstate *, u_int));
189#if defined(__NetBSD__) || defined(__OpenBSD__)
190extern	int	fr_state_ioctl __P((caddr_t, u_long, int));
191#else
192extern	int	fr_state_ioctl __P((caddr_t, int, int));
193#endif
194
195#endif /* __IP_STATE_H__ */
196