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