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