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.68.2.5 2005/08/11 19:58:04 darrenr Exp $
   8  *
   9  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  10  * Use is subject to license terms.
  11  * Copyright 2019 Joyent, Inc.
  12  */
  13 
  14 #ifndef __IP_STATE_H__
  15 #define __IP_STATE_H__
  16 
  17 #include <sys/uuid.h>
  18 
  19 #if defined(__STDC__) || defined(__GNUC__) || defined(_AIX51)
  20 # define        SIOCDELST       _IOW('r', 61, struct ipfobj)
  21 #else
  22 # define        SIOCDELST       _IOW(r, 61, struct ipfobj)
  23 #endif
  24 
  25 struct ipscan;
  26 
  27 #ifndef IPSTATE_SIZE
  28 # define        IPSTATE_SIZE    5737
  29 #endif
  30 #ifndef IPSTATE_MAX
  31 # define        IPSTATE_MAX     50000   /* Maximum number of states held */
  32 #endif
  33 
  34 #define PAIRS(s1,d1,s2,d2)      ((((s1) == (s2)) && ((d1) == (d2))) ||\
  35                                  (((s1) == (d2)) && ((d1) == (s2))))
  36 #define IPPAIR(s1,d1,s2,d2)     PAIRS((s1).s_addr, (d1).s_addr, \
  37                                       (s2).s_addr, (d2).s_addr)
  38 
  39 
  40 typedef struct ipstate {
  41         ipfmutex_t      is_lock;
  42         struct  ipstate *is_next;
  43         struct  ipstate **is_pnext;
  44         struct  ipstate *is_hnext;
  45         struct  ipstate **is_phnext;
  46         struct  ipstate **is_me;
  47         void            *is_ifp[4];
  48         void            *is_sync;
  49         struct nat      *is_nat[2];
  50         frentry_t       *is_rule;
  51         struct  ipftq   *is_tqehead[2];
  52         struct  ipscan  *is_isc;
  53         U_QUAD_T        is_pkts[4];
  54         U_QUAD_T        is_bytes[4];
  55         U_QUAD_T        is_icmppkts[4];
  56         struct  ipftqent is_sti;
  57         u_int   is_frage[2];
  58         int     is_ref;                 /* reference count */
  59         int     is_isninc[2];
  60         u_short is_sumd[2];
  61         i6addr_t        is_src;
  62         i6addr_t        is_dst;
  63         u_int   is_pass;
  64         u_char  is_p;                   /* Protocol */
  65         u_char  is_v;
  66         u_32_t  is_hv;
  67         u_32_t  is_tag;
  68         u_32_t  is_opt[2];              /* packet options set */
  69                                         /* in both directions */
  70         u_32_t  is_optmsk[2];           /*    "      "    mask */
  71                                         /* in both directions */
  72         uuid_t  is_uuid;
  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                 icmpinfo_t      is_ics;
  79                 tcpinfo_t       is_ts;
  80                 udpinfo_t       is_us;
  81                 greinfo_t       is_ug;
  82         } is_ps;
  83         u_32_t  is_flags;
  84         int     is_flx[2][2];
  85         u_32_t  is_rulen;               /* rule number when created */
  86         u_32_t  is_s0[2];
  87         u_short is_smsk[2];
  88         char    is_group[FR_GROUPLEN];
  89         char    is_sbuf[2][16];
  90         char    is_ifname[4][LIFNAMSIZ];
  91 } ipstate_t;
  92 
  93 #define is_die          is_sti.tqe_die
  94 #define is_state        is_sti.tqe_state
  95 #define is_saddr        is_src.in4.s_addr
  96 #define is_daddr        is_dst.in4.s_addr
  97 #define is_icmp         is_ps.is_ics
  98 #define is_type         is_icmp.ici_type
  99 #define is_code         is_icmp.ici_code
 100 #define is_tcp          is_ps.is_ts
 101 #define is_udp          is_ps.is_us
 102 #define is_send         is_tcp.ts_data[0].td_end
 103 #define is_dend         is_tcp.ts_data[1].td_end
 104 #define is_maxswin      is_tcp.ts_data[0].td_maxwin
 105 #define is_maxdwin      is_tcp.ts_data[1].td_maxwin
 106 #define is_maxsend      is_tcp.ts_data[0].td_maxend
 107 #define is_maxdend      is_tcp.ts_data[1].td_maxend
 108 #define is_swinscale    is_tcp.ts_data[0].td_winscale
 109 #define is_dwinscale    is_tcp.ts_data[1].td_winscale
 110 #define is_swinflags    is_tcp.ts_data[0].td_winflags
 111 #define is_dwinflags    is_tcp.ts_data[1].td_winflags
 112 #define is_sport        is_tcp.ts_sport
 113 #define is_dport        is_tcp.ts_dport
 114 #define is_ifpin        is_ifp[0]
 115 #define is_ifpout       is_ifp[2]
 116 #define is_gre          is_ps.is_ug
 117 #define is_call         is_gre.gs_call
 118 
 119 #define IS_WSPORT       SI_W_SPORT      /* 0x00100 */
 120 #define IS_WDPORT       SI_W_DPORT      /* 0x00200 */
 121 #define IS_WSADDR       SI_W_SADDR      /* 0x00400 */
 122 #define IS_WDADDR       SI_W_DADDR      /* 0x00800 */
 123 #define IS_NEWFR        SI_NEWFR        /* 0x01000 */
 124 #define IS_CLONE        SI_CLONE        /* 0x02000 */
 125 #define IS_CLONED       SI_CLONED       /* 0x04000 */
 126 #define IS_TCPFSM                          0x10000
 127 #define IS_STRICT                          0x20000
 128 #define IS_ISNSYN                          0x40000
 129 #define IS_ISNACK                          0x80000
 130 #define IS_STATESYNC                       0x100000
 131 /*
 132  * IS_SC flags are for scan-operations that need to be recognised in state.
 133  */
 134 #define IS_SC_CLIENT                    0x10000000
 135 #define IS_SC_SERVER                    0x20000000
 136 #define IS_SC_MATCHC                    0x40000000
 137 #define IS_SC_MATCHS                    0x80000000
 138 #define IS_SC_MATCHALL  (IS_SC_MATCHC|IS_SC_MATCHC)
 139 #define IS_SC_ALL       (IS_SC_MATCHC|IS_SC_MATCHC|IS_SC_CLIENT|IS_SC_SERVER)
 140 
 141 /*
 142  * Flags that can be passed into fr_addstate
 143  */
 144 #define IS_INHERITED                    0x0fffff00
 145 
 146 #define TH_OPENING      (TH_SYN|TH_ACK)
 147 /*
 148  * is_flags:
 149  * Bits 0 - 3 are use as a mask with the current packet's bits to check for
 150  * whether it is short, tcp/udp, a fragment or the presence of IP options.
 151  * Bits 4 - 7 are set from the initial packet and contain what the packet
 152  * anded with bits 0-3 must match.
 153  * Bits 8,9 are used to indicate wildcard source/destination port matching.
 154  * Bits 10,11 are reserved for other wildcard flag compatibility.
 155  * Bits 12,13 are for scaning.
 156  */
 157 
 158 typedef struct  ipstate_save    {
 159         void    *ips_next;
 160         struct  ipstate ips_is;
 161         struct  frentry ips_fr;
 162 } ipstate_save_t;
 163 
 164 #define ips_rule        ips_is.is_rule
 165 
 166 
 167 typedef struct  ipslog  {
 168         U_QUAD_T        isl_pkts[4];
 169         U_QUAD_T        isl_bytes[4];
 170         i6addr_t        isl_src;
 171         i6addr_t        isl_dst;
 172         u_32_t  isl_tag;
 173         u_short isl_type;
 174         union {
 175                 u_short isl_filler[2];
 176                 u_short isl_ports[2];
 177                 u_short isl_icmp;
 178         } isl_ps;
 179         u_char  isl_v;
 180         u_char  isl_p;
 181         u_char  isl_flags;
 182         u_char  isl_state[2];
 183         u_32_t  isl_rulen;
 184         char    isl_group[FR_GROUPLEN];
 185 } ipslog_t;
 186 
 187 #define isl_sport       isl_ps.isl_ports[0]
 188 #define isl_dport       isl_ps.isl_ports[1]
 189 #define isl_itype       isl_ps.isl_icmp
 190 
 191 #define ISL_NEW                 0
 192 #define ISL_CLONE               1
 193 #define ISL_EXPIRE              0xffff
 194 #define ISL_FLUSH               0xfffe
 195 #define ISL_REMOVE              0xfffd
 196 #define ISL_INTERMEDIATE        0xfffc
 197 #define ISL_KILLED              0xfffb
 198 #define ISL_ORPHAN              0xfffa
 199 
 200 
 201 typedef struct  ips_stat {
 202         u_long  iss_hits;
 203         u_long  iss_miss;
 204         u_long  iss_max;
 205         u_long  iss_maxref;
 206         u_long  iss_tcp;
 207         u_long  iss_udp;
 208         u_long  iss_icmp;
 209         u_long  iss_nomem;
 210         u_long  iss_expire;
 211         u_long  iss_fin;
 212         u_long  iss_active;
 213         u_long  iss_logged;
 214         u_long  iss_logfail;
 215         u_long  iss_inuse;
 216         u_long  iss_wild;
 217         u_long  iss_killed;
 218         u_long  iss_ticks;
 219         u_long  iss_bucketfull;
 220         int     iss_statesize;
 221         int     iss_statemax;
 222         ipstate_t **iss_table;
 223         ipstate_t *iss_list;
 224         u_long  *iss_bucketlen;
 225         u_int   iss_orphans;
 226 } ips_stat_t;
 227 
 228 typedef struct port_pair {
 229         uint16_t        pp_sport;
 230         uint16_t        pp_dport;
 231 } port_pair_t;
 232 
 233 extern  int     fr_stateinit __P((ipf_stack_t *));
 234 extern  ipstate_t *fr_addstate __P((fr_info_t *, ipstate_t **, u_int));
 235 extern  frentry_t *fr_checkstate __P((struct fr_info *, u_32_t *));
 236 extern  ipstate_t *fr_stlookup __P((fr_info_t *, tcphdr_t *, ipftq_t **));
 237 extern  void    fr_statesync __P((int, int, void *, char *, ipf_stack_t *));
 238 extern  void    fr_timeoutstate __P((ipf_stack_t *));
 239 extern  int     fr_tcp_age __P((struct ipftqent *, struct fr_info *,
 240                                 struct ipftq *, int));
 241 extern  int     fr_tcpinwindow __P((struct fr_info *, struct tcpdata *,
 242                                     struct tcpdata *, tcphdr_t *, int));
 243 extern  void    fr_stateunload __P((ipf_stack_t *));
 244 extern  void    ipstate_log __P((struct ipstate *, u_int, ipf_stack_t *));
 245 extern  int     fr_state_ioctl __P((caddr_t, ioctlcmd_t, int, int, void *, ipf_stack_t *));
 246 extern  void    fr_stinsert __P((struct ipstate *, int, ipf_stack_t *));
 247 extern  void    fr_sttab_init __P((struct ipftq *, ipf_stack_t *));
 248 extern  void    fr_sttab_destroy __P((struct ipftq *));
 249 extern  void    fr_updatestate __P((fr_info_t *, ipstate_t *, ipftq_t *));
 250 extern  void    fr_statederef __P((ipstate_t **, ipf_stack_t *));
 251 extern  void    fr_setstatequeue __P((ipstate_t *, int, ipf_stack_t *));
 252 extern  int     fr_delstate __P((ipstate_t *, int, ipf_stack_t *));
 253 #if SOLARIS2 >= 10
 254 extern  void    fr_stateifindexsync __P((void *, void *, ipf_stack_t *));
 255 #endif
 256 
 257 #endif /* __IP_STATE_H__ */