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