1145522Sdarrenr/* $FreeBSD$ */ 2145522Sdarrenr 353642Sguido/* 4255332Scy * Copyright (C) 2012 by Darren Reed. 553642Sguido * 680482Sdarrenr * See the IPFILTER.LICENCE file for details on licencing. 7255332Scy * 8255332Scy * Copyright 2008 Sun Microsystems. 9255332Scy * 10255332Scy * $Id$ 1153642Sguido */ 12145522Sdarrenr#if defined(KERNEL) || defined(_KERNEL) 13145522Sdarrenr# undef KERNEL 14145522Sdarrenr# undef _KERNEL 15145522Sdarrenr# define KERNEL 1 16145522Sdarrenr# define _KERNEL 1 1792685Sdarrenr#endif 1853642Sguido#include <sys/errno.h> 1953642Sguido#include <sys/types.h> 2053642Sguido#include <sys/param.h> 2153642Sguido#include <sys/file.h> 2260854Sdarrenr#if defined(_KERNEL) && defined(__FreeBSD_version) && \ 2360854Sdarrenr (__FreeBSD_version >= 400000) && !defined(KLD_MODULE) 2460854Sdarrenr#include "opt_inet6.h" 2560854Sdarrenr#endif 26145522Sdarrenr#if !defined(_KERNEL) && !defined(__KERNEL__) 2753642Sguido# include <stdio.h> 2853642Sguido# include <stdlib.h> 2953642Sguido# include <string.h> 30145522Sdarrenr# define _KERNEL 31145522Sdarrenr# ifdef __OpenBSD__ 32145522Sdarrenrstruct file; 3353642Sguido# endif 34145522Sdarrenr# include <sys/uio.h> 35145522Sdarrenr# undef _KERNEL 3653642Sguido#endif 37145522Sdarrenr#if defined(_KERNEL) && (__FreeBSD_version >= 220000) 3853642Sguido# include <sys/filio.h> 3953642Sguido# include <sys/fcntl.h> 4053642Sguido#else 4153642Sguido# include <sys/ioctl.h> 4253642Sguido#endif 4353642Sguido#include <sys/time.h> 44145522Sdarrenr#if !defined(linux) 4553642Sguido# include <sys/protosw.h> 4653642Sguido#endif 4753642Sguido#include <sys/socket.h> 48145522Sdarrenr#if defined(_KERNEL) 4953642Sguido# include <sys/systm.h> 50145522Sdarrenr# if !defined(__SVR4) && !defined(__svr4__) 5153642Sguido# include <sys/mbuf.h> 5253642Sguido# endif 53145522Sdarrenr#endif 54145522Sdarrenr#if defined(__SVR4) || defined(__svr4__) 5553642Sguido# include <sys/filio.h> 5653642Sguido# include <sys/byteorder.h> 5753642Sguido# ifdef _KERNEL 5853642Sguido# include <sys/dditypes.h> 5953642Sguido# endif 6053642Sguido# include <sys/stream.h> 6153642Sguido# include <sys/kmem.h> 6253642Sguido#endif 6353642Sguido 6453642Sguido#include <net/if.h> 6553642Sguido#ifdef sun 6653642Sguido# include <net/af.h> 6753642Sguido#endif 6853642Sguido#include <netinet/in.h> 6953642Sguido#include <netinet/in_systm.h> 7053642Sguido#include <netinet/ip.h> 7153642Sguido#include <netinet/tcp.h> 72145522Sdarrenr#if !defined(__hpux) && !defined(linux) 7353642Sguido# include <netinet/tcp_fsm.h> 7453642Sguido#endif 7553642Sguido#include <netinet/udp.h> 7653642Sguido#include <netinet/ip_icmp.h> 77255332Scy#if !defined(_KERNEL) 78255332Scy# include "ipf.h" 79255332Scy#endif 8053642Sguido#include "netinet/ip_compat.h" 8153642Sguido#include "netinet/ip_fil.h" 8253642Sguido#include "netinet/ip_nat.h" 8353642Sguido#include "netinet/ip_frag.h" 8453642Sguido#include "netinet/ip_state.h" 85145522Sdarrenr#include "netinet/ip_proxy.h" 86255332Scy#include "netinet/ip_lookup.h" 87255332Scy#include "netinet/ip_dstlist.h" 88145522Sdarrenr#include "netinet/ip_sync.h" 8960854Sdarrenr#ifdef USE_INET6 9060854Sdarrenr#include <netinet/icmp6.h> 9160854Sdarrenr#endif 92255332Scy#if FREEBSD_GE_REV(300000) 9353642Sguido# include <sys/malloc.h> 94145522Sdarrenr# if defined(_KERNEL) && !defined(IPFILTER_LKM) 9553642Sguido# include <sys/libkern.h> 9653642Sguido# include <sys/systm.h> 9753642Sguido# endif 9853642Sguido#endif 99145522Sdarrenr/* END OF INCLUDES */ 10053642Sguido 101145522Sdarrenr 10280482Sdarrenr#if !defined(lint) 10380482Sdarrenrstatic const char sccsid[] = "@(#)ip_state.c 1.8 6/5/96 (C) 1993-2000 Darren Reed"; 104255332Scystatic const char rcsid[] = "@(#)$Id$"; 10580482Sdarrenr#endif 10680482Sdarrenr 10753642Sguido 108255332Scystatic ipftuneable_t ipf_state_tuneables[] = { 109255332Scy { { (void *)offsetof(ipf_state_softc_t, ipf_state_max) }, 110255332Scy "state_max", 1, 0x7fffffff, 111255332Scy stsizeof(ipf_state_softc_t, ipf_state_max), 112255332Scy 0, NULL, NULL }, 113255332Scy { { (void *)offsetof(ipf_state_softc_t, ipf_state_size) }, 114255332Scy "state_size", 1, 0x7fffffff, 115255332Scy stsizeof(ipf_state_softc_t, ipf_state_size), 116255332Scy 0, NULL, ipf_state_rehash }, 117255332Scy { { (void *)offsetof(ipf_state_softc_t, ipf_state_lock) }, 118255332Scy "state_lock", 0, 1, 119255332Scy stsizeof(ipf_state_softc_t, ipf_state_lock), 120255332Scy IPFT_RDONLY, NULL, NULL }, 121255332Scy { { (void *)offsetof(ipf_state_softc_t, ipf_state_maxbucket) }, 122255332Scy "state_maxbucket", 1, 0x7fffffff, 123255332Scy stsizeof(ipf_state_softc_t, ipf_state_maxbucket), 124255332Scy 0, NULL, NULL }, 125255332Scy { { (void *)offsetof(ipf_state_softc_t, ipf_state_logging) }, 126255332Scy "state_logging",0, 1, 127255332Scy stsizeof(ipf_state_softc_t, ipf_state_logging), 128255332Scy 0, NULL, NULL }, 129255332Scy { { (void *)offsetof(ipf_state_softc_t, ipf_state_wm_high) }, 130255332Scy "state_wm_high",2, 100, 131255332Scy stsizeof(ipf_state_softc_t, ipf_state_wm_high), 132255332Scy 0, NULL, NULL }, 133255332Scy { { (void *)offsetof(ipf_state_softc_t, ipf_state_wm_low) }, 134255332Scy "state_wm_low", 1, 99, 135255332Scy stsizeof(ipf_state_softc_t, ipf_state_wm_low), 136255332Scy 0, NULL, NULL }, 137255332Scy { { (void *)offsetof(ipf_state_softc_t, ipf_state_wm_freq) }, 138255332Scy "state_wm_freq",2, 999999, 139255332Scy stsizeof(ipf_state_softc_t, ipf_state_wm_freq), 140255332Scy 0, NULL, NULL }, 141255332Scy { { NULL }, 142255332Scy NULL, 0, 0, 143255332Scy 0, 144255332Scy 0, NULL, NULL } 145255332Scy}; 146255332Scy 147255332Scy#define SINCL(x) ATOMIC_INCL(softs->x) 148255332Scy#define SBUMP(x) (softs->x)++ 149255332Scy#define SBUMPD(x, y) do { (softs->x.y)++; DT(y); } while (0) 150255332Scy#define SBUMPDX(x, y, z)do { (softs->x.y)++; DT(z); } while (0) 151255332Scy 15260854Sdarrenr#ifdef USE_INET6 153255332Scystatic ipstate_t *ipf_checkicmp6matchingstate __P((fr_info_t *)); 15460854Sdarrenr#endif 155255332Scystatic int ipf_allowstateicmp __P((fr_info_t *, ipstate_t *, i6addr_t *)); 156255332Scystatic ipstate_t *ipf_matchsrcdst __P((fr_info_t *, ipstate_t *, i6addr_t *, 157145522Sdarrenr i6addr_t *, tcphdr_t *, u_32_t)); 158255332Scystatic ipstate_t *ipf_checkicmpmatchingstate __P((fr_info_t *)); 159255332Scystatic int ipf_state_flush_entry __P((ipf_main_softc_t *, void *)); 160255332Scystatic ips_stat_t *ipf_state_stats __P((ipf_main_softc_t *)); 161255332Scystatic int ipf_state_del __P((ipf_main_softc_t *, ipstate_t *, int)); 162255332Scystatic int ipf_state_remove __P((ipf_main_softc_t *, caddr_t)); 163255332Scystatic int ipf_state_match __P((ipstate_t *is1, ipstate_t *is2)); 164255332Scystatic int ipf_state_matchaddresses __P((ipstate_t *is1, ipstate_t *is2)); 165255332Scystatic int ipf_state_matchipv4addrs __P((ipstate_t *is1, ipstate_t *is2)); 166255332Scystatic int ipf_state_matchipv6addrs __P((ipstate_t *is1, ipstate_t *is2)); 167255332Scystatic int ipf_state_matchisps __P((ipstate_t *is1, ipstate_t *is2)); 168255332Scystatic int ipf_state_matchports __P((udpinfo_t *is1, udpinfo_t *is2)); 169255332Scystatic int ipf_state_matcharray __P((ipstate_t *, int *, u_long)); 170255332Scystatic void ipf_ipsmove __P((ipf_state_softc_t *, ipstate_t *, u_int)); 171255332Scystatic int ipf_state_tcp __P((ipf_main_softc_t *, ipf_state_softc_t *, 172255332Scy fr_info_t *, tcphdr_t *, ipstate_t *)); 173255332Scystatic int ipf_tcpoptions __P((ipf_state_softc_t *, fr_info_t *, 174255332Scy tcphdr_t *, tcpdata_t *)); 175255332Scystatic ipstate_t *ipf_state_clone __P((fr_info_t *, tcphdr_t *, ipstate_t *)); 176255332Scystatic void ipf_fixinisn __P((fr_info_t *, ipstate_t *)); 177255332Scystatic void ipf_fixoutisn __P((fr_info_t *, ipstate_t *)); 178255332Scystatic void ipf_checknewisn __P((fr_info_t *, ipstate_t *)); 179255332Scystatic int ipf_state_iter __P((ipf_main_softc_t *, ipftoken_t *, 180255332Scy ipfgeniter_t *, ipfobj_t *)); 181255332Scystatic int ipf_state_gettable __P((ipf_main_softc_t *, ipf_state_softc_t *, 182255332Scy char *)); 183255332Scystatic int ipf_state_tcpinwindow __P((struct fr_info *, struct tcpdata *, 184255332Scy struct tcpdata *, tcphdr_t *, int)); 185145522Sdarrenr 186255332Scystatic int ipf_state_getent __P((ipf_main_softc_t *, ipf_state_softc_t *, 187255332Scy caddr_t)); 188255332Scystatic int ipf_state_putent __P((ipf_main_softc_t *, ipf_state_softc_t *, 189255332Scy caddr_t)); 19053642Sguido 191145522Sdarrenr#define ONE_DAY IPF_TTLVAL(1 * 86400) /* 1 day */ 192145522Sdarrenr#define FIVE_DAYS (5 * ONE_DAY) 193255332Scy#define DOUBLE_HASH(x) (((x) + softs->ipf_state_seed[(x) % \ 194255332Scy softs->ipf_state_size]) % softs->ipf_state_size) 19553642Sguido 196255332Scy 197255332Scy/* ------------------------------------------------------------------------ */ 198255332Scy/* Function: ipf_state_main_load */ 199255332Scy/* Returns: int - 0 == success, -1 == failure */ 200255332Scy/* Parameters: Nil */ 201255332Scy/* */ 202255332Scy/* A null-op function that exists as a placeholder so that the flow in */ 203255332Scy/* other functions is obvious. */ 204255332Scy/* ------------------------------------------------------------------------ */ 205255332Scyint 206255332Scyipf_state_main_load() 207255332Scy{ 208255332Scy return 0; 209255332Scy} 210255332Scy 211255332Scy 212255332Scy/* ------------------------------------------------------------------------ */ 213255332Scy/* Function: ipf_state_main_unload */ 214255332Scy/* Returns: int - 0 == success, -1 == failure */ 215255332Scy/* Parameters: Nil */ 216255332Scy/* */ 217255332Scy/* A null-op function that exists as a placeholder so that the flow in */ 218255332Scy/* other functions is obvious. */ 219255332Scy/* ------------------------------------------------------------------------ */ 220255332Scyint 221255332Scyipf_state_main_unload() 222255332Scy{ 223255332Scy return 0; 224255332Scy} 225255332Scy 226255332Scy 227255332Scy/* ------------------------------------------------------------------------ */ 228255332Scy/* Function: ipf_state_soft_create */ 229255332Scy/* Returns: void * - NULL = failure, else pointer to soft context */ 230255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 231255332Scy/* */ 232255332Scy/* Create a new state soft context structure and populate it with the list */ 233255332Scy/* of tunables and other default settings. */ 234255332Scy/* ------------------------------------------------------------------------ */ 235255332Scyvoid * 236255332Scyipf_state_soft_create(softc) 237255332Scy ipf_main_softc_t *softc; 238255332Scy{ 239255332Scy ipf_state_softc_t *softs; 240255332Scy 241255332Scy KMALLOC(softs, ipf_state_softc_t *); 242255332Scy if (softs == NULL) 243255332Scy return NULL; 244255332Scy 245255332Scy bzero((char *)softs, sizeof(*softs)); 246255332Scy 247255332Scy softs->ipf_state_tune = ipf_tune_array_copy(softs, 248255332Scy sizeof(ipf_state_tuneables), 249255332Scy ipf_state_tuneables); 250255332Scy if (softs->ipf_state_tune == NULL) { 251255332Scy ipf_state_soft_destroy(softc, softs); 252255332Scy return NULL; 253255332Scy } 254255332Scy if (ipf_tune_array_link(softc, softs->ipf_state_tune) == -1) { 255255332Scy ipf_state_soft_destroy(softc, softs); 256255332Scy return NULL; 257255332Scy } 258255332Scy 259145522Sdarrenr#ifdef IPFILTER_LOG 260255332Scy softs->ipf_state_logging = 1; 261145522Sdarrenr#else 262255332Scy softs->ipf_state_logging = 0; 263145522Sdarrenr#endif 264255332Scy softs->ipf_state_size = IPSTATE_SIZE, 265255332Scy softs->ipf_state_maxbucket = 0; 266255332Scy softs->ipf_state_wm_freq = IPF_TTLVAL(10); 267255332Scy softs->ipf_state_max = IPSTATE_MAX; 268255332Scy softs->ipf_state_wm_last = 0; 269255332Scy softs->ipf_state_wm_high = 99; 270255332Scy softs->ipf_state_wm_low = 90; 271255332Scy softs->ipf_state_inited = 0; 272255332Scy softs->ipf_state_lock = 0; 273255332Scy softs->ipf_state_doflush = 0; 27453642Sguido 275255332Scy return softs; 276255332Scy} 27753642Sguido 278255332Scy 279145522Sdarrenr/* ------------------------------------------------------------------------ */ 280255332Scy/* Function: ipf_state_soft_destroy */ 281255332Scy/* Returns: Nil */ 282255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 283255332Scy/* arg(I) - pointer to local context to use */ 284145522Sdarrenr/* */ 285255332Scy/* Undo only what we did in soft create: unlink and free the tunables and */ 286255332Scy/* free the soft context structure itself. */ 287145522Sdarrenr/* ------------------------------------------------------------------------ */ 288255332Scyvoid 289255332Scyipf_state_soft_destroy(softc, arg) 290255332Scy ipf_main_softc_t *softc; 291255332Scy void *arg; 29253642Sguido{ 293255332Scy ipf_state_softc_t *softs = arg; 294255332Scy 295255332Scy if (softs->ipf_state_tune != NULL) { 296255332Scy ipf_tune_array_unlink(softc, softs->ipf_state_tune); 297255332Scy KFREES(softs->ipf_state_tune, sizeof(ipf_state_tuneables)); 298255332Scy softs->ipf_state_tune = NULL; 299255332Scy } 300255332Scy 301255332Scy KFREE(softs); 302255332Scy} 303255332Scy 304255332Scy 305255332Scy/* ------------------------------------------------------------------------ */ 306255332Scy/* Function: ipf_state_soft_init */ 307255332Scy/* Returns: int - 0 == success, -1 == failure */ 308255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 309255332Scy/* arg(I) - pointer to local context to use */ 310255332Scy/* */ 311255332Scy/* Initialise the state soft context structure so it is ready for use. */ 312255332Scy/* This involves: */ 313255332Scy/* - allocating a hash table and zero'ing it out */ 314255332Scy/* - building a secondary table of seeds for double hashing to make it more */ 315255332Scy/* difficult to attempt to attack the hash table itself (for DoS) */ 316255332Scy/* - initialise all of the timeout queues, including a table for TCP, some */ 317255332Scy/* pairs of query/response for UDP and other IP protocols (typically the */ 318255332Scy/* reply queue has a shorter timeout than the query) */ 319255332Scy/* ------------------------------------------------------------------------ */ 320255332Scyint 321255332Scyipf_state_soft_init(softc, arg) 322255332Scy ipf_main_softc_t *softc; 323255332Scy void *arg; 324255332Scy{ 325255332Scy ipf_state_softc_t *softs = arg; 32660854Sdarrenr int i; 32760854Sdarrenr 328255332Scy KMALLOCS(softs->ipf_state_table, 329255332Scy ipstate_t **, softs->ipf_state_size * sizeof(ipstate_t *)); 330255332Scy if (softs->ipf_state_table == NULL) 33153642Sguido return -1; 33260854Sdarrenr 333255332Scy bzero((char *)softs->ipf_state_table, 334255332Scy softs->ipf_state_size * sizeof(ipstate_t *)); 335255332Scy 336255332Scy KMALLOCS(softs->ipf_state_seed, u_long *, 337255332Scy softs->ipf_state_size * sizeof(*softs->ipf_state_seed)); 338255332Scy if (softs->ipf_state_seed == NULL) 339145522Sdarrenr return -2; 340255332Scy 341255332Scy for (i = 0; i < softs->ipf_state_size; i++) { 342145522Sdarrenr /* 343255332Scy * XXX - ipf_state_seed[X] should be a random number of sorts. 344145522Sdarrenr */ 345255332Scy#if FREEBSD_GE_REV(400000) 346255332Scy softs->ipf_state_seed[i] = arc4random(); 347145522Sdarrenr#else 348255332Scy softs->ipf_state_seed[i] = ((u_long)softs->ipf_state_seed + i) * 349255332Scy softs->ipf_state_size; 350255332Scy softs->ipf_state_seed[i] ^= 0xa5a55a5a; 351255332Scy softs->ipf_state_seed[i] *= (u_long)softs->ipf_state_seed; 352255332Scy softs->ipf_state_seed[i] ^= 0x5a5aa5a5; 353255332Scy softs->ipf_state_seed[i] *= softs->ipf_state_max; 354145522Sdarrenr#endif 355145522Sdarrenr } 356145522Sdarrenr 357255332Scy KMALLOCS(softs->ipf_state_stats.iss_bucketlen, u_int *, 358255332Scy softs->ipf_state_size * sizeof(u_int)); 359255332Scy if (softs->ipf_state_stats.iss_bucketlen == NULL) 360255332Scy return -3; 36160854Sdarrenr 362255332Scy bzero((char *)softs->ipf_state_stats.iss_bucketlen, 363255332Scy softs->ipf_state_size * sizeof(u_int)); 364145522Sdarrenr 365255332Scy if (softs->ipf_state_maxbucket == 0) { 366255332Scy for (i = softs->ipf_state_size; i > 0; i >>= 1) 367255332Scy softs->ipf_state_maxbucket++; 368255332Scy softs->ipf_state_maxbucket *= 2; 369145522Sdarrenr } 370145522Sdarrenr 371255332Scy ipf_sttab_init(softc, softs->ipf_state_tcptq); 372255332Scy softs->ipf_state_stats.iss_tcptab = softs->ipf_state_tcptq; 373255332Scy softs->ipf_state_tcptq[IPF_TCP_NSTATES - 1].ifq_next = 374255332Scy &softs->ipf_state_udptq; 375145522Sdarrenr 376255332Scy IPFTQ_INIT(&softs->ipf_state_udptq, softc->ipf_udptimeout, 377255332Scy "ipftq udp tab"); 378255332Scy softs->ipf_state_udptq.ifq_next = &softs->ipf_state_udpacktq; 379145522Sdarrenr 380255332Scy IPFTQ_INIT(&softs->ipf_state_udpacktq, softc->ipf_udpacktimeout, 381255332Scy "ipftq udpack tab"); 382255332Scy softs->ipf_state_udpacktq.ifq_next = &softs->ipf_state_icmptq; 383255332Scy 384255332Scy IPFTQ_INIT(&softs->ipf_state_icmptq, softc->ipf_icmptimeout, 385255332Scy "ipftq icmp tab"); 386255332Scy softs->ipf_state_icmptq.ifq_next = &softs->ipf_state_icmpacktq; 387255332Scy 388255332Scy IPFTQ_INIT(&softs->ipf_state_icmpacktq, softc->ipf_icmpacktimeout, 389255332Scy "ipftq icmpack tab"); 390255332Scy softs->ipf_state_icmpacktq.ifq_next = &softs->ipf_state_iptq; 391255332Scy 392255332Scy IPFTQ_INIT(&softs->ipf_state_iptq, softc->ipf_iptimeout, 393255332Scy "ipftq iptimeout tab"); 394255332Scy softs->ipf_state_iptq.ifq_next = &softs->ipf_state_pending; 395255332Scy 396255332Scy IPFTQ_INIT(&softs->ipf_state_pending, IPF_HZ_DIVIDE, "ipftq pending"); 397255332Scy softs->ipf_state_pending.ifq_next = &softs->ipf_state_deletetq; 398255332Scy 399255332Scy IPFTQ_INIT(&softs->ipf_state_deletetq, 1, "ipftq delete"); 400255332Scy softs->ipf_state_deletetq.ifq_next = NULL; 401255332Scy 402255332Scy MUTEX_INIT(&softs->ipf_stinsert, "ipf state insert mutex"); 403255332Scy 404255332Scy 405255332Scy softs->ipf_state_wm_last = softc->ipf_ticks; 406255332Scy softs->ipf_state_inited = 1; 407255332Scy 40853642Sguido return 0; 40953642Sguido} 41053642Sguido 41153642Sguido 412145522Sdarrenr/* ------------------------------------------------------------------------ */ 413255332Scy/* Function: ipf_state_soft_fini */ 414255332Scy/* Returns: int - 0 = success, -1 = failure */ 415255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 416255332Scy/* arg(I) - pointer to local context to use */ 417145522Sdarrenr/* */ 418145522Sdarrenr/* Release and destroy any resources acquired or initialised so that */ 419145522Sdarrenr/* IPFilter can be unloaded or re-initialised. */ 420145522Sdarrenr/* ------------------------------------------------------------------------ */ 421255332Scyint 422255332Scyipf_state_soft_fini(softc, arg) 423255332Scy ipf_main_softc_t *softc; 424255332Scy void *arg; 42553642Sguido{ 426255332Scy ipf_state_softc_t *softs = arg; 427145522Sdarrenr ipftq_t *ifq, *ifqnext; 428145522Sdarrenr ipstate_t *is; 42953642Sguido 430255332Scy while ((is = softs->ipf_state_list) != NULL) 431255332Scy ipf_state_del(softc, is, ISL_UNLOAD); 43253642Sguido 433145522Sdarrenr /* 434145522Sdarrenr * Proxy timeout queues are not cleaned here because although they 435255332Scy * exist on the state list, appr_unload is called after 436255332Scy * ipf_state_unload and the proxies actually are responsible for them 437255332Scy * being created. Should the proxy timeouts have their own list? 438255332Scy * There's no real justification as this is the only complication. 439145522Sdarrenr */ 440255332Scy for (ifq = softs->ipf_state_usertq; ifq != NULL; ifq = ifqnext) { 441145522Sdarrenr ifqnext = ifq->ifq_next; 442255332Scy 443255332Scy if (ipf_deletetimeoutqueue(ifq) == 0) 444255332Scy ipf_freetimeoutqueue(softc, ifq); 445145522Sdarrenr } 44653642Sguido 447255332Scy softs->ipf_state_stats.iss_inuse = 0; 448255332Scy softs->ipf_state_stats.iss_active = 0; 44953642Sguido 450255332Scy if (softs->ipf_state_inited == 1) { 451255332Scy softs->ipf_state_inited = 0; 452255332Scy ipf_sttab_destroy(softs->ipf_state_tcptq); 453255332Scy MUTEX_DESTROY(&softs->ipf_state_udptq.ifq_lock); 454255332Scy MUTEX_DESTROY(&softs->ipf_state_icmptq.ifq_lock); 455255332Scy MUTEX_DESTROY(&softs->ipf_state_udpacktq.ifq_lock); 456255332Scy MUTEX_DESTROY(&softs->ipf_state_icmpacktq.ifq_lock); 457255332Scy MUTEX_DESTROY(&softs->ipf_state_iptq.ifq_lock); 458255332Scy MUTEX_DESTROY(&softs->ipf_state_deletetq.ifq_lock); 459255332Scy MUTEX_DESTROY(&softs->ipf_state_pending.ifq_lock); 460255332Scy MUTEX_DESTROY(&softs->ipf_stinsert); 461145522Sdarrenr } 462110916Sdarrenr 463255332Scy if (softs->ipf_state_table != NULL) { 464255332Scy KFREES(softs->ipf_state_table, 465255332Scy softs->ipf_state_size * sizeof(*softs->ipf_state_table)); 466255332Scy softs->ipf_state_table = NULL; 467145522Sdarrenr } 46853642Sguido 469255332Scy if (softs->ipf_state_seed != NULL) { 470255332Scy KFREES(softs->ipf_state_seed, 471255332Scy softs->ipf_state_size * sizeof(*softs->ipf_state_seed)); 472255332Scy softs->ipf_state_seed = NULL; 47360854Sdarrenr } 47492685Sdarrenr 475255332Scy if (softs->ipf_state_stats.iss_bucketlen != NULL) { 476255332Scy KFREES(softs->ipf_state_stats.iss_bucketlen, 477255332Scy softs->ipf_state_size * sizeof(u_int)); 478255332Scy softs->ipf_state_stats.iss_bucketlen = NULL; 47992685Sdarrenr } 48092685Sdarrenr 481255332Scy return 0; 482255332Scy} 483145522Sdarrenr 484255332Scy 485255332Scy/* ------------------------------------------------------------------------ */ 486255332Scy/* Function: ipf_state_set_lock */ 487255332Scy/* Returns: Nil */ 488255332Scy/* Parameters: arg(I) - pointer to local context to use */ 489255332Scy/* tmp(I) - new value for lock */ 490255332Scy/* */ 491255332Scy/* Stub function that allows for external manipulation of ipf_state_lock */ 492255332Scy/* ------------------------------------------------------------------------ */ 493255332Scyvoid 494255332Scyipf_state_setlock(arg, tmp) 495255332Scy void *arg; 496255332Scy int tmp; 497255332Scy{ 498255332Scy ipf_state_softc_t *softs = arg; 499255332Scy 500255332Scy softs->ipf_state_lock = tmp; 50153642Sguido} 50253642Sguido 50353642Sguido 504145522Sdarrenr/* ------------------------------------------------------------------------ */ 505255332Scy/* Function: ipf_state_stats */ 506145522Sdarrenr/* Returns: ips_state_t* - pointer to state stats structure */ 507255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 508145522Sdarrenr/* */ 509145522Sdarrenr/* Put all the current numbers and pointers into a single struct and return */ 510145522Sdarrenr/* a pointer to it. */ 511145522Sdarrenr/* ------------------------------------------------------------------------ */ 512255332Scystatic ips_stat_t * 513255332Scyipf_state_stats(softc) 514255332Scy ipf_main_softc_t *softc; 515145522Sdarrenr{ 516255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 517255332Scy ips_stat_t *issp = &softs->ipf_state_stats; 518255332Scy 519255332Scy issp->iss_state_size = softs->ipf_state_size; 520255332Scy issp->iss_state_max = softs->ipf_state_max; 521255332Scy issp->iss_table = softs->ipf_state_table; 522255332Scy issp->iss_list = softs->ipf_state_list; 523255332Scy issp->iss_ticks = softc->ipf_ticks; 524255332Scy 525255332Scy#ifdef IPFILTER_LOGGING 526255332Scy issp->iss_log_ok = ipf_log_logok(softc, IPF_LOGSTATE); 527255332Scy issp->iss_log_fail = ipf_log_failures(softc, IPF_LOGSTATE); 528255332Scy#else 529255332Scy issp->iss_log_ok = 0; 530255332Scy issp->iss_log_fail = 0; 531255332Scy#endif 532255332Scy return issp; 533145522Sdarrenr} 534145522Sdarrenr 535145522Sdarrenr/* ------------------------------------------------------------------------ */ 536255332Scy/* Function: ipf_state_remove */ 537145522Sdarrenr/* Returns: int - 0 == success, != 0 == failure */ 538255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 539255332Scy/* data(I) - pointer to state structure to delete from table */ 540145522Sdarrenr/* */ 541145522Sdarrenr/* Search for a state structure that matches the one passed, according to */ 542145522Sdarrenr/* the IP addresses and other protocol specific information. */ 543145522Sdarrenr/* ------------------------------------------------------------------------ */ 544255332Scystatic int 545255332Scyipf_state_remove(softc, data) 546255332Scy ipf_main_softc_t *softc; 547255332Scy caddr_t data; 54860854Sdarrenr{ 549255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 55060854Sdarrenr ipstate_t *sp, st; 55160854Sdarrenr int error; 55260854Sdarrenr 55360854Sdarrenr sp = &st; 554255332Scy error = ipf_inobj(softc, data, NULL, &st, IPFOBJ_IPSTATE); 55560854Sdarrenr if (error) 55660854Sdarrenr return EFAULT; 55760854Sdarrenr 558255332Scy WRITE_ENTER(&softc->ipf_state); 559255332Scy for (sp = softs->ipf_state_list; sp; sp = sp->is_next) 56060854Sdarrenr if ((sp->is_p == st.is_p) && (sp->is_v == st.is_v) && 561145522Sdarrenr !bcmp((caddr_t)&sp->is_src, (caddr_t)&st.is_src, 56267614Sdarrenr sizeof(st.is_src)) && 563255332Scy !bcmp((caddr_t)&sp->is_dst, (caddr_t)&st.is_dst, 56467614Sdarrenr sizeof(st.is_dst)) && 565145522Sdarrenr !bcmp((caddr_t)&sp->is_ps, (caddr_t)&st.is_ps, 56667614Sdarrenr sizeof(st.is_ps))) { 567255332Scy ipf_state_del(softc, sp, ISL_REMOVE); 568255332Scy RWLOCK_EXIT(&softc->ipf_state); 56960854Sdarrenr return 0; 57060854Sdarrenr } 571255332Scy RWLOCK_EXIT(&softc->ipf_state); 572255332Scy 573255332Scy IPFERROR(100001); 57460854Sdarrenr return ESRCH; 57560854Sdarrenr} 57660854Sdarrenr 57760854Sdarrenr 578145522Sdarrenr/* ------------------------------------------------------------------------ */ 579255332Scy/* Function: ipf_state_ioctl */ 580145522Sdarrenr/* Returns: int - 0 == success, != 0 == failure */ 581255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 582255332Scy/* data(I) - pointer to ioctl data */ 583255332Scy/* cmd(I) - ioctl command integer */ 584255332Scy/* mode(I) - file mode bits used with open */ 585255332Scy/* uid(I) - uid of process making the ioctl call */ 586255332Scy/* ctx(I) - pointer specific to context of the call */ 587145522Sdarrenr/* */ 588145522Sdarrenr/* Processes an ioctl call made to operate on the IP Filter state device. */ 589145522Sdarrenr/* ------------------------------------------------------------------------ */ 590255332Scyint 591255332Scyipf_state_ioctl(softc, data, cmd, mode, uid, ctx) 592255332Scy ipf_main_softc_t *softc; 593255332Scy caddr_t data; 594255332Scy ioctlcmd_t cmd; 595255332Scy int mode, uid; 596255332Scy void *ctx; 59753642Sguido{ 598255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 59960854Sdarrenr int arg, ret, error = 0; 600170268Sdarrenr SPL_INT(s); 60153642Sguido 60253642Sguido switch (cmd) 60353642Sguido { 604145522Sdarrenr /* 605145522Sdarrenr * Delete an entry from the state table. 606145522Sdarrenr */ 60760854Sdarrenr case SIOCDELST : 608255332Scy error = ipf_state_remove(softc, data); 60960854Sdarrenr break; 610170268Sdarrenr 611145522Sdarrenr /* 612145522Sdarrenr * Flush the state table 613145522Sdarrenr */ 61453642Sguido case SIOCIPFFL : 615255332Scy error = BCOPYIN(data, &arg, sizeof(arg)); 616170268Sdarrenr if (error != 0) { 617255332Scy IPFERROR(100002); 618170268Sdarrenr error = EFAULT; 619255332Scy 620170268Sdarrenr } else { 621255332Scy WRITE_ENTER(&softc->ipf_state); 622255332Scy ret = ipf_state_flush(softc, arg, 4); 623255332Scy RWLOCK_EXIT(&softc->ipf_state); 624255332Scy 625255332Scy error = BCOPYOUT(&ret, data, sizeof(ret)); 626255332Scy if (error != 0) { 627255332Scy IPFERROR(100003); 628170268Sdarrenr error = EFAULT; 629255332Scy } 630170268Sdarrenr } 63153642Sguido break; 632170268Sdarrenr 633145522Sdarrenr#ifdef USE_INET6 634110916Sdarrenr case SIOCIPFL6 : 635255332Scy error = BCOPYIN(data, &arg, sizeof(arg)); 636170268Sdarrenr if (error != 0) { 637255332Scy IPFERROR(100004); 638170268Sdarrenr error = EFAULT; 639255332Scy 640170268Sdarrenr } else { 641255332Scy WRITE_ENTER(&softc->ipf_state); 642255332Scy ret = ipf_state_flush(softc, arg, 6); 643255332Scy RWLOCK_EXIT(&softc->ipf_state); 644255332Scy 645255332Scy error = BCOPYOUT(&ret, data, sizeof(ret)); 646255332Scy if (error != 0) { 647255332Scy IPFERROR(100005); 648170268Sdarrenr error = EFAULT; 649255332Scy } 650170268Sdarrenr } 651110916Sdarrenr break; 652110916Sdarrenr#endif 653255332Scy 654255332Scy case SIOCMATCHFLUSH : 655255332Scy WRITE_ENTER(&softc->ipf_state); 656255332Scy error = ipf_state_matchflush(softc, data); 657255332Scy RWLOCK_EXIT(&softc->ipf_state); 658255332Scy break; 659255332Scy 66055929Sguido#ifdef IPFILTER_LOG 661145522Sdarrenr /* 662145522Sdarrenr * Flush the state log. 663145522Sdarrenr */ 66455929Sguido case SIOCIPFFB : 665255332Scy if (!(mode & FWRITE)) { 666255332Scy IPFERROR(100008); 66755929Sguido error = EPERM; 668255332Scy } else { 66960854Sdarrenr int tmp; 67060854Sdarrenr 671255332Scy tmp = ipf_log_clear(softc, IPL_LOGSTATE); 672255332Scy error = BCOPYOUT(&tmp, data, sizeof(tmp)); 673255332Scy if (error != 0) { 674255332Scy IPFERROR(100009); 675170268Sdarrenr error = EFAULT; 676255332Scy } 67760854Sdarrenr } 67855929Sguido break; 679170268Sdarrenr 680145522Sdarrenr /* 681145522Sdarrenr * Turn logging of state information on/off. 682145522Sdarrenr */ 683145522Sdarrenr case SIOCSETLG : 684255332Scy if (!(mode & FWRITE)) { 685255332Scy IPFERROR(100010); 686145522Sdarrenr error = EPERM; 687255332Scy } else { 688255332Scy error = BCOPYIN(data, &softs->ipf_state_logging, 689255332Scy sizeof(softs->ipf_state_logging)); 690255332Scy if (error != 0) { 691255332Scy IPFERROR(100011); 692170268Sdarrenr error = EFAULT; 693255332Scy } 694145522Sdarrenr } 69553642Sguido break; 696170268Sdarrenr 697145522Sdarrenr /* 698145522Sdarrenr * Return the current state of logging. 699145522Sdarrenr */ 700145522Sdarrenr case SIOCGETLG : 701255332Scy error = BCOPYOUT(&softs->ipf_state_logging, data, 702255332Scy sizeof(softs->ipf_state_logging)); 703255332Scy if (error != 0) { 704255332Scy IPFERROR(100012); 705170268Sdarrenr error = EFAULT; 706255332Scy } 707145522Sdarrenr break; 708170268Sdarrenr 709145522Sdarrenr /* 710145522Sdarrenr * Return the number of bytes currently waiting to be read. 711145522Sdarrenr */ 71253642Sguido case FIONREAD : 713255332Scy arg = ipf_log_bytesused(softc, IPL_LOGSTATE); 714255332Scy error = BCOPYOUT(&arg, data, sizeof(arg)); 715255332Scy if (error != 0) { 716255332Scy IPFERROR(100013); 717170268Sdarrenr error = EFAULT; 718255332Scy } 719145522Sdarrenr break; 72053642Sguido#endif 721170268Sdarrenr 722145522Sdarrenr /* 723145522Sdarrenr * Get the current state statistics. 724145522Sdarrenr */ 725145522Sdarrenr case SIOCGETFS : 726255332Scy error = ipf_outobj(softc, data, ipf_state_stats(softc), 727255332Scy IPFOBJ_STATESTAT); 72853642Sguido break; 729170268Sdarrenr 730145522Sdarrenr /* 731145522Sdarrenr * Lock/Unlock the state table. (Locking prevents any changes, which 732145522Sdarrenr * means no packets match). 733145522Sdarrenr */ 73460854Sdarrenr case SIOCSTLCK : 735153876Sguido if (!(mode & FWRITE)) { 736255332Scy IPFERROR(100014); 737153876Sguido error = EPERM; 738153876Sguido } else { 739255332Scy error = ipf_lock(data, &softs->ipf_state_lock); 740153876Sguido } 74160854Sdarrenr break; 742170268Sdarrenr 743145522Sdarrenr /* 744145522Sdarrenr * Add an entry to the current state table. 745145522Sdarrenr */ 74660854Sdarrenr case SIOCSTPUT : 747255332Scy if (!softs->ipf_state_lock || !(mode &FWRITE)) { 748255332Scy IPFERROR(100015); 74960854Sdarrenr error = EACCES; 75060854Sdarrenr break; 75160854Sdarrenr } 752255332Scy error = ipf_state_putent(softc, softs, data); 75360854Sdarrenr break; 754170268Sdarrenr 755145522Sdarrenr /* 756145522Sdarrenr * Get a state table entry. 757145522Sdarrenr */ 75860854Sdarrenr case SIOCSTGET : 759255332Scy if (!softs->ipf_state_lock) { 760255332Scy IPFERROR(100016); 76160854Sdarrenr error = EACCES; 76260854Sdarrenr break; 76360854Sdarrenr } 764255332Scy error = ipf_state_getent(softc, softs, data); 76560854Sdarrenr break; 766170268Sdarrenr 767170268Sdarrenr /* 768170268Sdarrenr * Return a copy of the hash table bucket lengths 769170268Sdarrenr */ 770170268Sdarrenr case SIOCSTAT1 : 771255332Scy error = BCOPYOUT(softs->ipf_state_stats.iss_bucketlen, data, 772255332Scy softs->ipf_state_size * sizeof(u_int)); 773255332Scy if (error != 0) { 774255332Scy IPFERROR(100017); 775170268Sdarrenr error = EFAULT; 776255332Scy } 777170268Sdarrenr break; 778170268Sdarrenr 779170268Sdarrenr case SIOCGENITER : 780170268Sdarrenr { 781170268Sdarrenr ipftoken_t *token; 782170268Sdarrenr ipfgeniter_t iter; 783255332Scy ipfobj_t obj; 784170268Sdarrenr 785255332Scy error = ipf_inobj(softc, data, &obj, &iter, IPFOBJ_GENITER); 786170268Sdarrenr if (error != 0) 787170268Sdarrenr break; 788170268Sdarrenr 789170268Sdarrenr SPL_SCHED(s); 790255332Scy token = ipf_token_find(softc, IPFGENITER_STATE, uid, ctx); 791255332Scy if (token != NULL) { 792255332Scy error = ipf_state_iter(softc, token, &iter, &obj); 793255332Scy WRITE_ENTER(&softc->ipf_tokens); 794255332Scy ipf_token_deref(softc, token); 795255332Scy RWLOCK_EXIT(&softc->ipf_tokens); 796255332Scy } else { 797255332Scy IPFERROR(100018); 798170268Sdarrenr error = ESRCH; 799255332Scy } 800170268Sdarrenr SPL_X(s); 801170268Sdarrenr break; 802170268Sdarrenr } 803170268Sdarrenr 804170268Sdarrenr case SIOCGTABL : 805255332Scy error = ipf_state_gettable(softc, softs, data); 806170268Sdarrenr break; 807170268Sdarrenr 808170268Sdarrenr case SIOCIPFDELTOK : 809255332Scy error = BCOPYIN(data, &arg, sizeof(arg)); 810170268Sdarrenr if (error != 0) { 811255332Scy IPFERROR(100019); 812170268Sdarrenr error = EFAULT; 813170268Sdarrenr } else { 814170268Sdarrenr SPL_SCHED(s); 815255332Scy error = ipf_token_del(softc, arg, uid, ctx); 816170268Sdarrenr SPL_X(s); 817170268Sdarrenr } 818170268Sdarrenr break; 819170268Sdarrenr 820170268Sdarrenr case SIOCGTQTAB : 821255332Scy error = ipf_outobj(softc, data, softs->ipf_state_tcptq, 822255332Scy IPFOBJ_STATETQTAB); 823170268Sdarrenr break; 824170268Sdarrenr 82553642Sguido default : 826255332Scy IPFERROR(100020); 82753642Sguido error = EINVAL; 82853642Sguido break; 82953642Sguido } 83053642Sguido return error; 83153642Sguido} 83253642Sguido 83353642Sguido 834145522Sdarrenr/* ------------------------------------------------------------------------ */ 835255332Scy/* Function: ipf_state_getent */ 836145522Sdarrenr/* Returns: int - 0 == success, != 0 == failure */ 837255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 838255332Scy/* softs(I) - pointer to state context structure */ 839255332Scy/* data(I) - pointer to state structure to retrieve from table*/ 840145522Sdarrenr/* */ 841145522Sdarrenr/* Copy out state information from the kernel to a user space process. If */ 842145522Sdarrenr/* there is a filter rule associated with the state entry, copy that out */ 843145522Sdarrenr/* as well. The entry to copy out is taken from the value of "ips_next" in */ 844145522Sdarrenr/* the struct passed in and if not null and not found in the list of current*/ 845145522Sdarrenr/* state entries, the retrieval fails. */ 846145522Sdarrenr/* ------------------------------------------------------------------------ */ 847255332Scystatic int 848255332Scyipf_state_getent(softc, softs, data) 849255332Scy ipf_main_softc_t *softc; 850255332Scy ipf_state_softc_t *softs; 851255332Scy caddr_t data; 85260854Sdarrenr{ 853145522Sdarrenr ipstate_t *is, *isn; 85492685Sdarrenr ipstate_save_t ips; 85560854Sdarrenr int error; 85660854Sdarrenr 857255332Scy error = ipf_inobj(softc, data, NULL, &ips, IPFOBJ_STATESAVE); 858255332Scy if (error) 859255332Scy return EFAULT; 86060854Sdarrenr 861255332Scy READ_ENTER(&softc->ipf_state); 86260854Sdarrenr isn = ips.ips_next; 863145522Sdarrenr if (isn == NULL) { 864255332Scy isn = softs->ipf_state_list; 86560854Sdarrenr if (isn == NULL) { 866255332Scy if (ips.ips_next == NULL) { 867255332Scy RWLOCK_EXIT(&softc->ipf_state); 868255332Scy IPFERROR(100021); 86960854Sdarrenr return ENOENT; 870255332Scy } 87160854Sdarrenr return 0; 87260854Sdarrenr } 87360854Sdarrenr } else { 87460854Sdarrenr /* 87560854Sdarrenr * Make sure the pointer we're copying from exists in the 87660854Sdarrenr * current list of entries. Security precaution to prevent 87760854Sdarrenr * copying of random kernel data. 87860854Sdarrenr */ 879255332Scy for (is = softs->ipf_state_list; is; is = is->is_next) 88060854Sdarrenr if (is == isn) 88160854Sdarrenr break; 882255332Scy if (!is) { 883255332Scy RWLOCK_EXIT(&softc->ipf_state); 884255332Scy IPFERROR(100022); 88560854Sdarrenr return ESRCH; 886173181Sdarrenr } 88760854Sdarrenr } 88860854Sdarrenr ips.ips_next = isn->is_next; 88960854Sdarrenr bcopy((char *)isn, (char *)&ips.ips_is, sizeof(ips.ips_is)); 890145522Sdarrenr ips.ips_rule = isn->is_rule; 891145522Sdarrenr if (isn->is_rule != NULL) 89260854Sdarrenr bcopy((char *)isn->is_rule, (char *)&ips.ips_fr, 89360854Sdarrenr sizeof(ips.ips_fr)); 894255332Scy RWLOCK_EXIT(&softc->ipf_state); 895255332Scy error = ipf_outobj(softc, data, &ips, IPFOBJ_STATESAVE); 896172776Sdarrenr return error; 89760854Sdarrenr} 89860854Sdarrenr 89960854Sdarrenr 900145522Sdarrenr/* ------------------------------------------------------------------------ */ 901255332Scy/* Function: ipf_state_putent */ 902145522Sdarrenr/* Returns: int - 0 == success, != 0 == failure */ 903255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 904255332Scy/* softs(I) - pointer to state context structure */ 905255332Scy/* data(I) - pointer to state information struct */ 906145522Sdarrenr/* */ 907145522Sdarrenr/* This function implements the SIOCSTPUT ioctl: insert a state entry into */ 908145522Sdarrenr/* the state table. If the state info. includes a pointer to a filter rule */ 909145522Sdarrenr/* then also add in an orphaned rule (will not show up in any "ipfstat -io" */ 910145522Sdarrenr/* output. */ 911145522Sdarrenr/* ------------------------------------------------------------------------ */ 912255332Scyint 913255332Scyipf_state_putent(softc, softs, data) 914255332Scy ipf_main_softc_t *softc; 915255332Scy ipf_state_softc_t *softs; 916255332Scy caddr_t data; 91760854Sdarrenr{ 918145522Sdarrenr ipstate_t *is, *isn; 91992685Sdarrenr ipstate_save_t ips; 92092685Sdarrenr int error, out, i; 92160854Sdarrenr frentry_t *fr; 92292685Sdarrenr char *name; 92360854Sdarrenr 924255332Scy error = ipf_inobj(softc, data, NULL, &ips, IPFOBJ_STATESAVE); 925255332Scy if (error != 0) 926255332Scy return error; 92760854Sdarrenr 92860854Sdarrenr KMALLOC(isn, ipstate_t *); 929255332Scy if (isn == NULL) { 930255332Scy IPFERROR(100023); 93160854Sdarrenr return ENOMEM; 932255332Scy } 93360854Sdarrenr 93460854Sdarrenr bcopy((char *)&ips.ips_is, (char *)isn, sizeof(*isn)); 935145522Sdarrenr bzero((char *)isn, offsetof(struct ipstate, is_pkts)); 936145522Sdarrenr isn->is_sti.tqe_pnext = NULL; 937145522Sdarrenr isn->is_sti.tqe_next = NULL; 938145522Sdarrenr isn->is_sti.tqe_ifq = NULL; 939145522Sdarrenr isn->is_sti.tqe_parent = isn; 940145522Sdarrenr isn->is_ifp[0] = NULL; 941145522Sdarrenr isn->is_ifp[1] = NULL; 942145522Sdarrenr isn->is_ifp[2] = NULL; 943145522Sdarrenr isn->is_ifp[3] = NULL; 944145522Sdarrenr isn->is_sync = NULL; 945145522Sdarrenr fr = ips.ips_rule; 94692685Sdarrenr 947145522Sdarrenr if (fr == NULL) { 948255332Scy int inserr; 949255332Scy 950255332Scy READ_ENTER(&softc->ipf_state); 951255332Scy inserr = ipf_state_insert(softc, isn, 0); 952153876Sguido MUTEX_EXIT(&isn->is_lock); 953255332Scy RWLOCK_EXIT(&softc->ipf_state); 954255332Scy 955255332Scy return inserr; 956145522Sdarrenr } 957145522Sdarrenr 958145522Sdarrenr if (isn->is_flags & SI_NEWFR) { 959145522Sdarrenr KMALLOC(fr, frentry_t *); 960145522Sdarrenr if (fr == NULL) { 961145522Sdarrenr KFREE(isn); 962255332Scy IPFERROR(100024); 963145522Sdarrenr return ENOMEM; 964145522Sdarrenr } 965145522Sdarrenr bcopy((char *)&ips.ips_fr, (char *)fr, sizeof(*fr)); 966145522Sdarrenr out = fr->fr_flags & FR_OUTQUE ? 1 : 0; 967145522Sdarrenr isn->is_rule = fr; 968145522Sdarrenr ips.ips_is.is_rule = fr; 969145522Sdarrenr MUTEX_NUKE(&fr->fr_lock); 970145522Sdarrenr MUTEX_INIT(&fr->fr_lock, "state filter rule lock"); 971145522Sdarrenr 972145522Sdarrenr /* 973145522Sdarrenr * Look up all the interface names in the rule. 974145522Sdarrenr */ 975145522Sdarrenr for (i = 0; i < 4; i++) { 976255332Scy if (fr->fr_ifnames[i] == -1) { 977255332Scy fr->fr_ifas[i] = NULL; 978255332Scy continue; 979255332Scy } 980255332Scy name = fr->fr_names + fr->fr_ifnames[i]; 981255332Scy fr->fr_ifas[i] = ipf_resolvenic(softc, name, 982255332Scy fr->fr_family); 983255332Scy } 984255332Scy 985255332Scy for (i = 0; i < 4; i++) { 986145522Sdarrenr name = isn->is_ifname[i]; 987255332Scy isn->is_ifp[i] = ipf_resolvenic(softc, name, 988255332Scy isn->is_v); 989145522Sdarrenr } 990145522Sdarrenr 991145522Sdarrenr fr->fr_ref = 0; 992145522Sdarrenr fr->fr_dsize = 0; 993145522Sdarrenr fr->fr_data = NULL; 994161356Sguido fr->fr_type = FR_T_NONE; 995145522Sdarrenr 996255332Scy (void) ipf_resolvedest(softc, fr->fr_names, &fr->fr_tifs[0], 997255332Scy fr->fr_family); 998255332Scy (void) ipf_resolvedest(softc, fr->fr_names, &fr->fr_tifs[1], 999255332Scy fr->fr_family); 1000255332Scy (void) ipf_resolvedest(softc, fr->fr_names, &fr->fr_dif, 1001255332Scy fr->fr_family); 1002145522Sdarrenr 1003145522Sdarrenr /* 1004145522Sdarrenr * send a copy back to userland of what we ended up 1005145522Sdarrenr * to allow for verification. 1006145522Sdarrenr */ 1007255332Scy error = ipf_outobj(softc, data, &ips, IPFOBJ_STATESAVE); 1008255332Scy if (error != 0) { 1009145522Sdarrenr KFREE(isn); 1010145522Sdarrenr MUTEX_DESTROY(&fr->fr_lock); 1011145522Sdarrenr KFREE(fr); 1012255332Scy IPFERROR(100025); 1013145522Sdarrenr return EFAULT; 1014145522Sdarrenr } 1015255332Scy READ_ENTER(&softc->ipf_state); 1016255332Scy error = ipf_state_insert(softc, isn, 0); 1017153876Sguido MUTEX_EXIT(&isn->is_lock); 1018255332Scy RWLOCK_EXIT(&softc->ipf_state); 1019145522Sdarrenr 1020145522Sdarrenr } else { 1021255332Scy READ_ENTER(&softc->ipf_state); 1022255332Scy for (is = softs->ipf_state_list; is; is = is->is_next) 1023145522Sdarrenr if (is->is_rule == fr) { 1024255332Scy error = ipf_state_insert(softc, isn, 0); 1025153876Sguido MUTEX_EXIT(&isn->is_lock); 1026145522Sdarrenr break; 102792685Sdarrenr } 102892685Sdarrenr 1029145522Sdarrenr if (is == NULL) { 1030145522Sdarrenr KFREE(isn); 1031145522Sdarrenr isn = NULL; 103260854Sdarrenr } 1033255332Scy RWLOCK_EXIT(&softc->ipf_state); 1034145522Sdarrenr 1035255332Scy if (isn == NULL) { 1036255332Scy IPFERROR(100033); 1037255332Scy error = ESRCH; 1038255332Scy } 103960854Sdarrenr } 1040145522Sdarrenr 1041255332Scy return error; 104260854Sdarrenr} 104360854Sdarrenr 104460854Sdarrenr 1045145522Sdarrenr/* ------------------------------------------------------------------------ */ 1046255332Scy/* Function: ipf_state_insert */ 1047255332Scy/* Returns: int - 0 == success, -1 == failure */ 1048255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 1049255332Scy/* Parameters: is(I) - pointer to state structure */ 1050255332Scy/* rev(I) - flag indicating direction of packet */ 1051145522Sdarrenr/* */ 1052145522Sdarrenr/* Inserts a state structure into the hash table (for lookups) and the list */ 1053145522Sdarrenr/* of state entries (for enumeration). Resolves all of the interface names */ 1054145522Sdarrenr/* to pointers and adjusts running stats for the hash table as appropriate. */ 1055145522Sdarrenr/* */ 1056255332Scy/* This function can fail if the filter rule has had a population policy of */ 1057255332Scy/* IP addresses used with stateful filteirng assigned to it. */ 1058255332Scy/* */ 1059145522Sdarrenr/* Locking: it is assumed that some kind of lock on ipf_state is held. */ 1060255332Scy/* Exits with is_lock initialised and held - *EVEN IF ERROR*. */ 1061145522Sdarrenr/* ------------------------------------------------------------------------ */ 1062255332Scyint 1063255332Scyipf_state_insert(softc, is, rev) 1064255332Scy ipf_main_softc_t *softc; 1065255332Scy ipstate_t *is; 1066255332Scy int rev; 106760854Sdarrenr{ 1068255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 1069145522Sdarrenr frentry_t *fr; 1070145522Sdarrenr u_int hv; 107192685Sdarrenr int i; 107260854Sdarrenr 107392685Sdarrenr /* 107492685Sdarrenr * Look up all the interface names in the state entry. 107592685Sdarrenr */ 107692685Sdarrenr for (i = 0; i < 4; i++) { 1077145522Sdarrenr if (is->is_ifp[i] != NULL) 1078145522Sdarrenr continue; 1079255332Scy is->is_ifp[i] = ipf_resolvenic(softc, is->is_ifname[i], 1080255332Scy is->is_v); 108160854Sdarrenr } 108260854Sdarrenr 1083145522Sdarrenr /* 1084255332Scy * If we could trust is_hv, then the modulous would not be needed, 1085255332Scy * but when running with IPFILTER_SYNC, this stops bad values. 1086145522Sdarrenr */ 1087255332Scy hv = is->is_hv % softs->ipf_state_size; 1088255332Scy /* TRACE is, hv */ 1089145522Sdarrenr is->is_hv = hv; 109092685Sdarrenr 109160854Sdarrenr /* 1092145522Sdarrenr * We need to get both of these locks...the first because it is 1093145522Sdarrenr * possible that once the insert is complete another packet might 1094145522Sdarrenr * come along, match the entry and want to update it. 1095145522Sdarrenr */ 1096255332Scy MUTEX_INIT(&is->is_lock, "ipf state entry"); 1097145522Sdarrenr MUTEX_ENTER(&is->is_lock); 1098255332Scy MUTEX_ENTER(&softs->ipf_stinsert); 1099145522Sdarrenr 1100255332Scy fr = is->is_rule; 1101255332Scy if (fr != NULL) { 1102255332Scy if ((fr->fr_srctrack.ht_max_nodes != 0) && 1103255332Scy (ipf_ht_node_add(softc, &fr->fr_srctrack, 1104255332Scy is->is_family, &is->is_src) == -1)) { 1105255332Scy SBUMPD(ipf_state_stats, iss_max_track); 1106255332Scy MUTEX_EXIT(&softs->ipf_stinsert); 1107255332Scy return -1; 1108255332Scy } 1109255332Scy 1110255332Scy MUTEX_ENTER(&fr->fr_lock); 1111255332Scy fr->fr_ref++; 1112255332Scy MUTEX_EXIT(&fr->fr_lock); 1113255332Scy fr->fr_statecnt++; 1114255332Scy } 1115255332Scy 1116255332Scy if (is->is_flags & (SI_WILDP|SI_WILDA)) { 1117255332Scy DT(iss_wild_plus_one); 1118255332Scy SINCL(ipf_state_stats.iss_wild); 1119255332Scy } 1120255332Scy 1121255332Scy SBUMP(ipf_state_stats.iss_proto[is->is_p]); 1122255332Scy SBUMP(ipf_state_stats.iss_active_proto[is->is_p]); 1123255332Scy 1124145522Sdarrenr /* 112560854Sdarrenr * add into list table. 112660854Sdarrenr */ 1127255332Scy if (softs->ipf_state_list != NULL) 1128255332Scy softs->ipf_state_list->is_pnext = &is->is_next; 1129255332Scy is->is_pnext = &softs->ipf_state_list; 1130255332Scy is->is_next = softs->ipf_state_list; 1131255332Scy softs->ipf_state_list = is; 1132145522Sdarrenr 1133255332Scy if (softs->ipf_state_table[hv] != NULL) 1134255332Scy softs->ipf_state_table[hv]->is_phnext = &is->is_hnext; 113560854Sdarrenr else 1136255332Scy softs->ipf_state_stats.iss_inuse++; 1137255332Scy is->is_phnext = softs->ipf_state_table + hv; 1138255332Scy is->is_hnext = softs->ipf_state_table[hv]; 1139255332Scy softs->ipf_state_table[hv] = is; 1140255332Scy softs->ipf_state_stats.iss_bucketlen[hv]++; 1141255332Scy softs->ipf_state_stats.iss_active++; 1142255332Scy MUTEX_EXIT(&softs->ipf_stinsert); 1143145522Sdarrenr 1144255332Scy ipf_state_setqueue(softc, is, rev); 1145255332Scy 1146255332Scy return 0; 114760854Sdarrenr} 114860854Sdarrenr 114960854Sdarrenr 1150145522Sdarrenr/* ------------------------------------------------------------------------ */ 1151255332Scy/* Function: ipf_state_matchipv4addrs */ 1152255332Scy/* Returns: int - 2 addresses match (strong match), 1 reverse match, */ 1153255332Scy/* 0 no match */ 1154255332Scy/* Parameters: is1, is2 pointers to states we are checking */ 1155255332Scy/* */ 1156255332Scy/* Function matches IPv4 addresses it returns strong match for ICMP proto */ 1157255332Scy/* even there is only reverse match */ 1158255332Scy/* ------------------------------------------------------------------------ */ 1159255332Scystatic int 1160255332Scyipf_state_matchipv4addrs(is1, is2) 1161255332Scy ipstate_t *is1, *is2; 1162255332Scy{ 1163255332Scy int rv; 1164255332Scy 1165255332Scy if (is1->is_saddr == is2->is_saddr && is1->is_daddr == is2->is_daddr) 1166255332Scy rv = 2; 1167255332Scy else if (is1->is_saddr == is2->is_daddr && 1168255332Scy is1->is_daddr == is2->is_saddr) { 1169255332Scy /* force strong match for ICMP protocol */ 1170255332Scy rv = (is1->is_p == IPPROTO_ICMP) ? 2 : 1; 1171255332Scy } 1172255332Scy else 1173255332Scy rv = 0; 1174255332Scy 1175255332Scy return (rv); 1176255332Scy} 1177255332Scy 1178255332Scy 1179255332Scy/* ------------------------------------------------------------------------ */ 1180255332Scy/* Function: ipf_state_matchipv6addrs */ 1181255332Scy/* Returns: int - 2 addresses match (strong match), 1 reverse match, */ 1182255332Scy/* 0 no match */ 1183255332Scy/* Parameters: is1, is2 pointers to states we are checking */ 1184255332Scy/* */ 1185255332Scy/* Function matches IPv6 addresses it returns strong match for ICMP proto */ 1186255332Scy/* even there is only reverse match */ 1187255332Scy/* ------------------------------------------------------------------------ */ 1188255332Scystatic int 1189255332Scyipf_state_matchipv6addrs(is1, is2) 1190255332Scy ipstate_t *is1, *is2; 1191255332Scy{ 1192255332Scy int rv; 1193255332Scy 1194255332Scy if (IP6_EQ(&is1->is_src, &is2->is_src) && 1195255332Scy IP6_EQ(&is1->is_dst, &is2->is_dst)) 1196255332Scy rv = 2; 1197255332Scy else if (IP6_EQ(&is1->is_src, &is2->is_dst) && 1198255332Scy IP6_EQ(&is1->is_dst, &is2->is_src)) { 1199255332Scy /* force strong match for ICMPv6 protocol */ 1200255332Scy rv = (is1->is_p == IPPROTO_ICMPV6) ? 2 : 1; 1201255332Scy } 1202255332Scy else 1203255332Scy rv = 0; 1204255332Scy 1205255332Scy return (rv); 1206255332Scy} 1207255332Scy 1208255332Scy 1209255332Scy/* ------------------------------------------------------------------------ */ 1210255332Scy/* Function: ipf_state_matchaddresses */ 1211255332Scy/* Returns: int - 2 addresses match, 1 reverse match, zero no match */ 1212255332Scy/* Parameters: is1, is2 pointers to states we are checking */ 1213255332Scy/* */ 1214255332Scy/* function retruns true if two pairs of addresses belong to single */ 1215255332Scy/* connection. suppose there are two endpoints: */ 1216255332Scy/* endpoint1 1.1.1.1 */ 1217255332Scy/* endpoint2 1.1.1.2 */ 1218255332Scy/* */ 1219255332Scy/* the state is established by packet flying from .1 to .2 so we see: */ 1220255332Scy/* is1->src = 1.1.1.1 */ 1221255332Scy/* is1->dst = 1.1.1.2 */ 1222255332Scy/* now endpoint 1.1.1.2 sends answer */ 1223255332Scy/* retreives is1 record created by first packat and compares it with is2 */ 1224255332Scy/* temporal record, is2 is initialized as follows: */ 1225255332Scy/* is2->src = 1.1.1.2 */ 1226255332Scy/* is2->dst = 1.1.1.1 */ 1227255332Scy/* in this case 1 will be returned */ 1228255332Scy/* */ 1229255332Scy/* the ipf_matchaddresses() assumes those two records to be same. of course */ 1230255332Scy/* the ipf_matchaddresses() also assume records are same in case you pass */ 1231255332Scy/* identical arguments (i.e. ipf_matchaddress(is1, is1) would return 2 */ 1232255332Scy/* ------------------------------------------------------------------------ */ 1233255332Scystatic int 1234255332Scyipf_state_matchaddresses(is1, is2) 1235255332Scy ipstate_t *is1, *is2; 1236255332Scy{ 1237255332Scy int rv; 1238255332Scy 1239255332Scy if (is1->is_v == 4) { 1240255332Scy rv = ipf_state_matchipv4addrs(is1, is2); 1241255332Scy } 1242255332Scy else { 1243255332Scy rv = ipf_state_matchipv6addrs(is1, is2); 1244255332Scy } 1245255332Scy 1246255332Scy return (rv); 1247255332Scy} 1248255332Scy 1249255332Scy 1250255332Scy/* ------------------------------------------------------------------------ */ 1251255332Scy/* Function: ipf_matchports */ 1252255332Scy/* Returns: int - 2 match, 1 rverse match, 0 no match */ 1253255332Scy/* Parameters: ppairs1, ppairs - src, dst ports we want to match */ 1254255332Scy/* */ 1255255332Scy/* performs the same match for isps members as for addresses */ 1256255332Scy/* ------------------------------------------------------------------------ */ 1257255332Scystatic int 1258255332Scyipf_state_matchports(ppairs1, ppairs2) 1259255332Scy udpinfo_t *ppairs1, *ppairs2; 1260255332Scy{ 1261255332Scy int rv; 1262255332Scy 1263255332Scy if (ppairs1->us_sport == ppairs2->us_sport && 1264255332Scy ppairs1->us_dport == ppairs2->us_dport) 1265255332Scy rv = 2; 1266255332Scy else if (ppairs1->us_sport == ppairs2->us_dport && 1267255332Scy ppairs1->us_dport == ppairs2->us_sport) 1268255332Scy rv = 1; 1269255332Scy else 1270255332Scy rv = 0; 1271255332Scy 1272255332Scy return (rv); 1273255332Scy} 1274255332Scy 1275255332Scy 1276255332Scy/* ------------------------------------------------------------------------ */ 1277255332Scy/* Function: ipf_matchisps */ 1278255332Scy/* Returns: int - nonzero if isps members match, 0 nomatch */ 1279255332Scy/* Parameters: is1, is2 - states we want to match */ 1280255332Scy/* */ 1281255332Scy/* performs the same match for isps members as for addresses */ 1282255332Scy/* ------------------------------------------------------------------------ */ 1283255332Scystatic int 1284255332Scyipf_state_matchisps(is1, is2) 1285255332Scy ipstate_t *is1, *is2; 1286255332Scy{ 1287255332Scy int rv; 1288255332Scy 1289255332Scy if (is1->is_p == is2->is_p) { 1290255332Scy switch (is1->is_p) 1291255332Scy { 1292255332Scy case IPPROTO_TCP : 1293255332Scy case IPPROTO_UDP : 1294255332Scy case IPPROTO_GRE : 1295255332Scy /* greinfo_t can be also interprted as port pair */ 1296255332Scy rv = ipf_state_matchports(&is1->is_ps.is_us, 1297255332Scy &is2->is_ps.is_us); 1298255332Scy break; 1299255332Scy 1300255332Scy case IPPROTO_ICMP : 1301255332Scy case IPPROTO_ICMPV6 : 1302255332Scy /* force strong match for ICMP datagram. */ 1303255332Scy if (bcmp(&is1->is_ps, &is2->is_ps, 1304255332Scy sizeof(icmpinfo_t)) == 0) { 1305255332Scy rv = 2; 1306255332Scy } else { 1307255332Scy rv = 0; 1308255332Scy } 1309255332Scy break; 1310255332Scy 1311255332Scy default: 1312255332Scy rv = 0; 1313255332Scy } 1314255332Scy } else { 1315255332Scy rv = 0; 1316255332Scy } 1317255332Scy 1318255332Scy return (rv); 1319255332Scy} 1320255332Scy 1321255332Scy 1322255332Scy/* ------------------------------------------------------------------------ */ 1323255332Scy/* Function: ipf_state_match */ 1324255332Scy/* Returns: int - nonzero match, zero no match */ 1325255332Scy/* Parameters: is1, is2 - states we want to match */ 1326255332Scy/* */ 1327255332Scy/* ------------------------------------------------------------------------ */ 1328255332Scystatic int 1329255332Scyipf_state_match(is1, is2) 1330255332Scy ipstate_t *is1, *is2; 1331255332Scy{ 1332255332Scy int rv; 1333255332Scy int amatch; 1334255332Scy int pomatch; 1335255332Scy 1336255332Scy if (bcmp(&is1->is_pass, &is2->is_pass, 1337255332Scy offsetof(struct ipstate, is_authmsk) - 1338255332Scy offsetof(struct ipstate, is_pass)) == 0) { 1339255332Scy 1340255332Scy pomatch = ipf_state_matchisps(is1, is2); 1341255332Scy amatch = ipf_state_matchaddresses(is1, is2); 1342255332Scy rv = (amatch != 0) && (amatch == pomatch); 1343255332Scy } else { 1344255332Scy rv = 0; 1345255332Scy } 1346255332Scy 1347255332Scy return (rv); 1348255332Scy} 1349255332Scy 1350255332Scy/* ------------------------------------------------------------------------ */ 1351255332Scy/* Function: ipf_state_add */ 1352255332Scy/* Returns: ipstate_t - 0 = success */ 1353255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 1354255332Scy/* fin(I) - pointer to packet information */ 1355145522Sdarrenr/* stsave(O) - pointer to place to save pointer to created */ 1356145522Sdarrenr/* state structure. */ 1357145522Sdarrenr/* flags(I) - flags to use when creating the structure */ 1358145522Sdarrenr/* */ 1359145522Sdarrenr/* Creates a new IP state structure from the packet information collected. */ 1360145522Sdarrenr/* Inserts it into the state table and appends to the bottom of the active */ 1361145522Sdarrenr/* list. If the capacity of the table has reached the maximum allowed then */ 1362145522Sdarrenr/* the call will fail and a flush is scheduled for the next timeout call. */ 1363161356Sguido/* */ 1364161356Sguido/* NOTE: The use of stsave to point to nat_state will result in memory */ 1365161356Sguido/* corruption. It should only be used to point to objects that will */ 1366161356Sguido/* either outlive this (not expired) or will deref the ip_state_t */ 1367161356Sguido/* when they are deleted. */ 1368145522Sdarrenr/* ------------------------------------------------------------------------ */ 1369255332Scyint 1370255332Scyipf_state_add(softc, fin, stsave, flags) 1371255332Scy ipf_main_softc_t *softc; 1372255332Scy fr_info_t *fin; 1373255332Scy ipstate_t **stsave; 1374255332Scy u_int flags; 137553642Sguido{ 1376255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 1377145522Sdarrenr ipstate_t *is, ips; 137892685Sdarrenr struct icmp *ic; 1379145522Sdarrenr u_int pass, hv; 1380145522Sdarrenr frentry_t *fr; 1381145522Sdarrenr tcphdr_t *tcp; 1382255332Scy frdest_t *fdp; 1383145522Sdarrenr int out; 138453642Sguido 1385255332Scy /* 1386255332Scy * If a packet that was created locally is trying to go out but we 1387255332Scy * do not match here here because of this lock, it is likely that 1388255332Scy * the policy will block it and return network unreachable back up 1389255332Scy * the stack. To mitigate this error, EAGAIN is returned instead, 1390255332Scy * telling the IP stack to try sending this packet again later. 1391255332Scy */ 1392255332Scy if (softs->ipf_state_lock) { 1393255332Scy SBUMPD(ipf_state_stats, iss_add_locked); 1394255332Scy fin->fin_error = EAGAIN; 1395255332Scy return -1; 1396255332Scy } 1397145522Sdarrenr 1398255332Scy if (fin->fin_flx & (FI_SHORT|FI_STATE|FI_FRAGBODY|FI_BAD)) { 1399255332Scy SBUMPD(ipf_state_stats, iss_add_bad); 1400255332Scy return -1; 1401255332Scy } 1402145522Sdarrenr 1403255332Scy if ((fin->fin_flx & FI_OOW) && !(fin->fin_tcpf & TH_SYN)) { 1404255332Scy SBUMPD(ipf_state_stats, iss_add_oow); 1405255332Scy return -1; 1406255332Scy } 1407255332Scy 1408255332Scy if ((softs->ipf_state_stats.iss_active * 100 / softs->ipf_state_max) > 1409255332Scy softs->ipf_state_wm_high) { 1410255332Scy softs->ipf_state_doflush = 1; 1411255332Scy } 1412255332Scy 1413145522Sdarrenr /* 1414145522Sdarrenr * If a "keep state" rule has reached the maximum number of references 1415145522Sdarrenr * to it, then schedule an automatic flush in case we can clear out 1416161356Sguido * some "dead old wood". Note that because the lock isn't held on 1417161356Sguido * fr it is possible that we could overflow. The cost of overflowing 1418161356Sguido * is being ignored here as the number by which it can overflow is 1419161356Sguido * a product of the number of simultaneous threads that could be 1420161356Sguido * executing in here, so a limit of 100 won't result in 200, but could 1421161356Sguido * result in 101 or 102. 1422145522Sdarrenr */ 1423161356Sguido fr = fin->fin_fr; 1424161356Sguido if (fr != NULL) { 1425255332Scy if ((softs->ipf_state_stats.iss_active >= 1426255332Scy softs->ipf_state_max) && (fr->fr_statemax == 0)) { 1427255332Scy SBUMPD(ipf_state_stats, iss_max); 1428255332Scy return 1; 1429161356Sguido } 1430161356Sguido if ((fr->fr_statemax != 0) && 1431161356Sguido (fr->fr_statecnt >= fr->fr_statemax)) { 1432255332Scy SBUMPD(ipf_state_stats, iss_max_ref); 1433255332Scy return 2; 1434161356Sguido } 1435145522Sdarrenr } 1436145522Sdarrenr 1437255332Scy is = &ips; 1438255332Scy if (fr == NULL) { 1439255332Scy pass = softc->ipf_flags; 1440255332Scy is->is_tag = FR_NOLOGTAG; 1441255332Scy } else { 1442255332Scy pass = fr->fr_flags; 1443255332Scy } 1444145522Sdarrenr 1445145522Sdarrenr ic = NULL; 1446145522Sdarrenr tcp = NULL; 144760854Sdarrenr out = fin->fin_out; 144853642Sguido bzero((char *)is, sizeof(*is)); 1449255332Scy is->is_die = 1 + softc->ipf_ticks; 1450255332Scy /* 1451255332Scy * We want to check everything that is a property of this packet, 1452255332Scy * but we don't (automatically) care about it's fragment status as 1453255332Scy * this may change. 1454255332Scy */ 1455255332Scy is->is_pass = pass; 1456255332Scy is->is_v = fin->fin_v; 1457255332Scy is->is_sec = fin->fin_secmsk; 1458255332Scy is->is_secmsk = 0xffff; 1459255332Scy is->is_auth = fin->fin_auth; 1460255332Scy is->is_authmsk = 0xffff; 1461255332Scy is->is_family = fin->fin_family; 1462255332Scy is->is_opt[0] = fin->fin_optmsk; 1463255332Scy is->is_optmsk[0] = 0xffffffff; 1464255332Scy if (is->is_v == 6) { 1465255332Scy is->is_opt[0] &= ~0x8; 1466255332Scy is->is_optmsk[0] &= ~0x8; 1467255332Scy } 1468145522Sdarrenr 146953642Sguido /* 147053642Sguido * Copy and calculate... 147153642Sguido */ 147260854Sdarrenr hv = (is->is_p = fin->fin_fi.fi_p); 147360854Sdarrenr is->is_src = fin->fin_fi.fi_src; 147460854Sdarrenr hv += is->is_saddr; 147560854Sdarrenr is->is_dst = fin->fin_fi.fi_dst; 147660854Sdarrenr hv += is->is_daddr; 147760854Sdarrenr#ifdef USE_INET6 147860854Sdarrenr if (fin->fin_v == 6) { 1479145522Sdarrenr /* 1480145522Sdarrenr * For ICMPv6, we check to see if the destination address is 1481145522Sdarrenr * a multicast address. If it is, do not include it in the 1482145522Sdarrenr * calculation of the hash because the correct reply will come 1483145522Sdarrenr * back from a real address, not a multicast address. 1484145522Sdarrenr */ 148592685Sdarrenr if ((is->is_p == IPPROTO_ICMPV6) && 148692685Sdarrenr IN6_IS_ADDR_MULTICAST(&is->is_dst.in6)) { 148792685Sdarrenr /* 148892685Sdarrenr * So you can do keep state with neighbour discovery. 1489145522Sdarrenr * 1490145522Sdarrenr * Here we could use the address from the neighbour 1491145522Sdarrenr * solicit message to put in the state structure and 1492145522Sdarrenr * we could use that without a wildcard flag too... 149392685Sdarrenr */ 1494145522Sdarrenr flags |= SI_W_DADDR; 149592685Sdarrenr hv -= is->is_daddr; 149692685Sdarrenr } else { 149792685Sdarrenr hv += is->is_dst.i6[1]; 149892685Sdarrenr hv += is->is_dst.i6[2]; 149992685Sdarrenr hv += is->is_dst.i6[3]; 150060854Sdarrenr } 150192685Sdarrenr hv += is->is_src.i6[1]; 150292685Sdarrenr hv += is->is_src.i6[2]; 150392685Sdarrenr hv += is->is_src.i6[3]; 150460854Sdarrenr } 150560854Sdarrenr#endif 1506172776Sdarrenr if ((fin->fin_v == 4) && 1507172776Sdarrenr (fin->fin_flx & (FI_MULTICAST|FI_BROADCAST|FI_MBCAST))) { 1508255332Scy flags |= SI_W_DADDR; 1509255332Scy hv -= is->is_daddr; 1510172776Sdarrenr } 151153642Sguido 151260854Sdarrenr switch (is->is_p) 151353642Sguido { 151460854Sdarrenr#ifdef USE_INET6 151560854Sdarrenr case IPPROTO_ICMPV6 : 1516145522Sdarrenr ic = fin->fin_dp; 151753642Sguido 151853642Sguido switch (ic->icmp_type) 151953642Sguido { 152060854Sdarrenr case ICMP6_ECHO_REQUEST : 1521145522Sdarrenr hv += (is->is_icmp.ici_id = ic->icmp_id); 1522255332Scy /*FALLTHROUGH*/ 152360854Sdarrenr case ICMP6_MEMBERSHIP_QUERY : 152460854Sdarrenr case ND_ROUTER_SOLICIT : 152560854Sdarrenr case ND_NEIGHBOR_SOLICIT : 152692685Sdarrenr case ICMP6_NI_QUERY : 1527145522Sdarrenr is->is_icmp.ici_type = ic->icmp_type; 152860854Sdarrenr break; 152992685Sdarrenr default : 1530255332Scy SBUMPD(ipf_state_stats, iss_icmp6_notquery); 1531255332Scy return -2; 153292685Sdarrenr } 153392685Sdarrenr break; 153460854Sdarrenr#endif 153592685Sdarrenr case IPPROTO_ICMP : 1536145522Sdarrenr ic = fin->fin_dp; 153792685Sdarrenr 153892685Sdarrenr switch (ic->icmp_type) 153992685Sdarrenr { 154060854Sdarrenr case ICMP_ECHO : 154153642Sguido case ICMP_TSTAMP : 154253642Sguido case ICMP_IREQ : 154353642Sguido case ICMP_MASKREQ : 1544145522Sdarrenr is->is_icmp.ici_type = ic->icmp_type; 1545145522Sdarrenr hv += (is->is_icmp.ici_id = ic->icmp_id); 154653642Sguido break; 154753642Sguido default : 1548255332Scy SBUMPD(ipf_state_stats, iss_icmp_notquery); 1549255332Scy return -3; 155053642Sguido } 155153642Sguido break; 1552145522Sdarrenr 1553255332Scy#if 0 1554145522Sdarrenr case IPPROTO_GRE : 1555145522Sdarrenr gre = fin->fin_dp; 1556145522Sdarrenr 1557145522Sdarrenr is->is_gre.gs_flags = gre->gr_flags; 1558145522Sdarrenr is->is_gre.gs_ptype = gre->gr_ptype; 1559145522Sdarrenr if (GRE_REV(is->is_gre.gs_flags) == 1) { 1560145522Sdarrenr is->is_call[0] = fin->fin_data[0]; 1561145522Sdarrenr is->is_call[1] = fin->fin_data[1]; 1562145522Sdarrenr } 1563145522Sdarrenr break; 1564255332Scy#endif 1565145522Sdarrenr 156653642Sguido case IPPROTO_TCP : 1567145522Sdarrenr tcp = fin->fin_dp; 156853642Sguido 1569255332Scy if (tcp->th_flags & TH_RST) { 1570255332Scy SBUMPD(ipf_state_stats, iss_tcp_rstadd); 1571255332Scy return -4; 1572255332Scy } 1573255332Scy 1574255332Scy /* TRACE is, flags, hv */ 1575255332Scy 157653642Sguido /* 157753642Sguido * The endian of the ports doesn't matter, but the ack and 157853642Sguido * sequence numbers do as we do mathematics on them later. 157953642Sguido */ 158092685Sdarrenr is->is_sport = htons(fin->fin_data[0]); 158192685Sdarrenr is->is_dport = htons(fin->fin_data[1]); 1582145522Sdarrenr if ((flags & (SI_W_DPORT|SI_W_SPORT)) == 0) { 158392685Sdarrenr hv += is->is_sport; 158492685Sdarrenr hv += is->is_dport; 158553642Sguido } 1586145522Sdarrenr 1587255332Scy /* TRACE is, flags, hv */ 1588255332Scy 1589145522Sdarrenr /* 1590145522Sdarrenr * If this is a real packet then initialise fields in the 1591145522Sdarrenr * state information structure from the TCP header information. 1592145522Sdarrenr */ 1593145522Sdarrenr 1594145522Sdarrenr is->is_maxdwin = 1; 1595145522Sdarrenr is->is_maxswin = ntohs(tcp->th_win); 1596145522Sdarrenr if (is->is_maxswin == 0) 1597145522Sdarrenr is->is_maxswin = 1; 1598145522Sdarrenr 1599145522Sdarrenr if ((fin->fin_flx & FI_IGNORE) == 0) { 160095563Sdarrenr is->is_send = ntohl(tcp->th_seq) + fin->fin_dlen - 1601145522Sdarrenr (TCP_OFF(tcp) << 2) + 160295563Sdarrenr ((tcp->th_flags & TH_SYN) ? 1 : 0) + 160395563Sdarrenr ((tcp->th_flags & TH_FIN) ? 1 : 0); 160495563Sdarrenr is->is_maxsend = is->is_send; 160595563Sdarrenr 1606145522Sdarrenr /* 1607145522Sdarrenr * Window scale option is only present in 1608145522Sdarrenr * SYN/SYN-ACK packet. 1609145522Sdarrenr */ 1610145522Sdarrenr if ((tcp->th_flags & ~(TH_FIN|TH_ACK|TH_ECNALL)) == 1611145522Sdarrenr TH_SYN && 1612145522Sdarrenr (TCP_OFF(tcp) > (sizeof(tcphdr_t) >> 2))) { 1613255332Scy if (ipf_tcpoptions(softs, fin, tcp, 1614255332Scy &is->is_tcp.ts_data[0]) == -1) 1615153876Sguido fin->fin_flx |= FI_BAD; 161698004Sdarrenr } 161795563Sdarrenr 1618145522Sdarrenr if ((fin->fin_out != 0) && (pass & FR_NEWISN) != 0) { 1619255332Scy ipf_checknewisn(fin, is); 1620255332Scy ipf_fixoutisn(fin, is); 1621145522Sdarrenr } 162295418Sdarrenr 1623145522Sdarrenr if ((tcp->th_flags & TH_OPENING) == TH_SYN) 1624145522Sdarrenr flags |= IS_TCPFSM; 1625145522Sdarrenr else { 1626145522Sdarrenr is->is_maxdwin = is->is_maxswin * 2; 1627145522Sdarrenr is->is_dend = ntohl(tcp->th_ack); 1628145522Sdarrenr is->is_maxdend = ntohl(tcp->th_ack); 1629145522Sdarrenr is->is_maxdwin *= 2; 1630145522Sdarrenr } 1631145522Sdarrenr } 163295418Sdarrenr 163353642Sguido /* 1634255332Scy * If we're creating state for a starting connection, start 1635255332Scy * the timer on it as we'll never see an error if it fails 1636255332Scy * to connect. 163753642Sguido */ 163853642Sguido break; 163992685Sdarrenr 164053642Sguido case IPPROTO_UDP : 1641145522Sdarrenr tcp = fin->fin_dp; 164253642Sguido 164392685Sdarrenr is->is_sport = htons(fin->fin_data[0]); 164492685Sdarrenr is->is_dport = htons(fin->fin_data[1]); 1645145522Sdarrenr if ((flags & (SI_W_DPORT|SI_W_SPORT)) == 0) { 1646145522Sdarrenr hv += tcp->th_dport; 1647145522Sdarrenr hv += tcp->th_sport; 164853642Sguido } 164953642Sguido break; 1650145522Sdarrenr 165153642Sguido default : 165292685Sdarrenr break; 165353642Sguido } 1654145522Sdarrenr hv = DOUBLE_HASH(hv); 1655145522Sdarrenr is->is_hv = hv; 165653642Sguido 1657145522Sdarrenr /* 1658145522Sdarrenr * Look for identical state. 1659145522Sdarrenr */ 1660255332Scy for (is = softs->ipf_state_table[hv % softs->ipf_state_size]; 1661255332Scy is != NULL; is = is->is_hnext) { 1662255332Scy if (ipf_state_match(&ips, is) == 1) 1663145522Sdarrenr break; 1664145522Sdarrenr } 1665255332Scy if (is != NULL) { 1666255332Scy SBUMPD(ipf_state_stats, iss_add_dup); 1667255332Scy return 3; 1668255332Scy } 1669145522Sdarrenr 1670255332Scy if (softs->ipf_state_stats.iss_bucketlen[hv] >= 1671255332Scy softs->ipf_state_maxbucket) { 1672255332Scy SBUMPD(ipf_state_stats, iss_bucket_full); 1673255332Scy return 4; 1674145522Sdarrenr } 167553642Sguido KMALLOC(is, ipstate_t *); 167653642Sguido if (is == NULL) { 1677255332Scy SBUMPD(ipf_state_stats, iss_nomem); 1678255332Scy return 5; 167953642Sguido } 168053642Sguido bcopy((char *)&ips, (char *)is, sizeof(*is)); 1681255332Scy is->is_flags = flags & IS_INHERITED; 1682255332Scy is->is_rulen = fin->fin_rule; 1683255332Scy is->is_rule = fr; 1684255332Scy 1685145522Sdarrenr /* 1686255332Scy * Do not do the modulous here, it is done in ipf_state_insert(). 1687145522Sdarrenr */ 1688145522Sdarrenr if (fr != NULL) { 1689255332Scy ipftq_t *tq; 1690255332Scy 1691255332Scy (void) strncpy(is->is_group, FR_NAME(fr, fr_group), 1692255332Scy FR_GROUPLEN); 1693145522Sdarrenr if (fr->fr_age[0] != 0) { 1694255332Scy tq = ipf_addtimeoutqueue(softc, 1695255332Scy &softs->ipf_state_usertq, 1696255332Scy fr->fr_age[0]); 1697255332Scy is->is_tqehead[0] = tq; 1698145522Sdarrenr is->is_sti.tqe_flags |= TQE_RULEBASED; 1699145522Sdarrenr } 1700145522Sdarrenr if (fr->fr_age[1] != 0) { 1701255332Scy tq = ipf_addtimeoutqueue(softc, 1702255332Scy &softs->ipf_state_usertq, 1703255332Scy fr->fr_age[1]); 1704255332Scy is->is_tqehead[1] = tq; 1705145522Sdarrenr is->is_sti.tqe_flags |= TQE_RULEBASED; 1706145522Sdarrenr } 170792685Sdarrenr 1708145522Sdarrenr is->is_tag = fr->fr_logtag; 1709145522Sdarrenr } 171092685Sdarrenr 1711145522Sdarrenr /* 1712255332Scy * It may seem strange to set is_ref to 2, but if stsave is not NULL 1713255332Scy * then a copy of the pointer is being stored somewhere else and in 1714255332Scy * the end, it will expect to be able to do osmething with it. 1715145522Sdarrenr */ 1716255332Scy is->is_me = stsave; 1717255332Scy if (stsave != NULL) { 1718255332Scy *stsave = is; 1719255332Scy is->is_ref = 2; 1720255332Scy } else { 1721255332Scy is->is_ref = 1; 1722255332Scy } 1723145522Sdarrenr is->is_pkts[0] = 0, is->is_bytes[0] = 0; 1724145522Sdarrenr is->is_pkts[1] = 0, is->is_bytes[1] = 0; 1725145522Sdarrenr is->is_pkts[2] = 0, is->is_bytes[2] = 0; 1726145522Sdarrenr is->is_pkts[3] = 0, is->is_bytes[3] = 0; 1727145522Sdarrenr if ((fin->fin_flx & FI_IGNORE) == 0) { 1728145522Sdarrenr is->is_pkts[out] = 1; 1729255332Scy fin->fin_pktnum = 1; 1730145522Sdarrenr is->is_bytes[out] = fin->fin_plen; 1731145522Sdarrenr is->is_flx[out][0] = fin->fin_flx & FI_CMP; 1732145522Sdarrenr is->is_flx[out][0] &= ~FI_OOW; 173392685Sdarrenr } 1734145522Sdarrenr 1735255332Scy if (pass & FR_STLOOSE) 1736255332Scy is->is_flags |= IS_LOOSE; 1737255332Scy 1738145522Sdarrenr if (pass & FR_STSTRICT) 1739145522Sdarrenr is->is_flags |= IS_STRICT; 1740145522Sdarrenr 1741145522Sdarrenr if (pass & FR_STATESYNC) 1742145522Sdarrenr is->is_flags |= IS_STATESYNC; 1743145522Sdarrenr 1744255332Scy if (pass & FR_LOGFIRST) 1745255332Scy is->is_pass &= ~(FR_LOGFIRST|FR_LOG); 1746255332Scy 1747255332Scy READ_ENTER(&softc->ipf_state); 1748255332Scy 1749255332Scy if (ipf_state_insert(softc, is, fin->fin_rev) == -1) { 1750255332Scy RWLOCK_EXIT(&softc->ipf_state); 1751255332Scy /* 1752255332Scy * This is a bit more manual than it should be but 1753255332Scy * ipf_state_del cannot be called. 1754255332Scy */ 1755255332Scy MUTEX_EXIT(&is->is_lock); 1756255332Scy MUTEX_DESTROY(&is->is_lock); 1757255332Scy if (is->is_tqehead[0] != NULL) { 1758255332Scy if (ipf_deletetimeoutqueue(is->is_tqehead[0]) == 0) 1759255332Scy ipf_freetimeoutqueue(softc, is->is_tqehead[0]); 1760255332Scy is->is_tqehead[0] = NULL; 1761255332Scy } 1762255332Scy if (is->is_tqehead[1] != NULL) { 1763255332Scy if (ipf_deletetimeoutqueue(is->is_tqehead[1]) == 0) 1764255332Scy ipf_freetimeoutqueue(softc, is->is_tqehead[1]); 1765255332Scy is->is_tqehead[1] = NULL; 1766255332Scy } 1767255332Scy KFREE(is); 1768255332Scy return -1; 1769255332Scy } 1770255332Scy 177153642Sguido /* 1772255332Scy * Filling in the interface name is after the insert so that an 1773255332Scy * event (such as add/delete) of an interface that is referenced 1774255332Scy * by this rule will see this state entry. 177553642Sguido */ 1776255332Scy if (fr != NULL) { 1777255332Scy /* 1778255332Scy * The name '-' is special for network interfaces and causes 1779255332Scy * a NULL name to be present, always, allowing packets to 1780255332Scy * match it, regardless of their interface. 1781255332Scy */ 1782255332Scy if ((fin->fin_ifp == NULL) || 1783255332Scy (fr->fr_ifnames[out << 1] != -1 && 1784255332Scy fr->fr_names[fr->fr_ifnames[out << 1] + 0] == '-' && 1785255332Scy fr->fr_names[fr->fr_ifnames[out << 1] + 1] == '\0')) { 1786255332Scy is->is_ifp[out << 1] = fr->fr_ifas[0]; 1787255332Scy strncpy(is->is_ifname[out << 1], 1788255332Scy fr->fr_names + fr->fr_ifnames[0], 1789255332Scy sizeof(fr->fr_ifnames[0])); 1790255332Scy } else { 1791255332Scy is->is_ifp[out << 1] = fin->fin_ifp; 1792255332Scy COPYIFNAME(fin->fin_v, fin->fin_ifp, 1793255332Scy is->is_ifname[out << 1]); 1794255332Scy } 179592685Sdarrenr 1796255332Scy is->is_ifp[(out << 1) + 1] = fr->fr_ifas[1]; 1797255332Scy if (fr->fr_ifnames[1] != -1) { 1798255332Scy strncpy(is->is_ifname[(out << 1) + 1], 1799255332Scy fr->fr_names + fr->fr_ifnames[1], 1800255332Scy sizeof(fr->fr_ifnames[1])); 1801255332Scy } 1802145522Sdarrenr 1803255332Scy is->is_ifp[(1 - out) << 1] = fr->fr_ifas[2]; 1804255332Scy if (fr->fr_ifnames[2] != -1) { 1805255332Scy strncpy(is->is_ifname[((1 - out) << 1)], 1806255332Scy fr->fr_names + fr->fr_ifnames[2], 1807255332Scy sizeof(fr->fr_ifnames[2])); 1808255332Scy } 1809145522Sdarrenr 1810255332Scy is->is_ifp[((1 - out) << 1) + 1] = fr->fr_ifas[3]; 1811255332Scy if (fr->fr_ifnames[3] != -1) { 1812255332Scy strncpy(is->is_ifname[((1 - out) << 1) + 1], 1813255332Scy fr->fr_names + fr->fr_ifnames[3], 1814255332Scy sizeof(fr->fr_ifnames[3])); 1815255332Scy } 1816255332Scy } else { 1817255332Scy if (fin->fin_ifp != NULL) { 1818255332Scy is->is_ifp[out << 1] = fin->fin_ifp; 1819255332Scy COPYIFNAME(fin->fin_v, fin->fin_ifp, 1820255332Scy is->is_ifname[out << 1]); 1821255332Scy } 1822255332Scy } 1823145522Sdarrenr 1824145522Sdarrenr if (fin->fin_p == IPPROTO_TCP) { 1825145522Sdarrenr /* 1826145522Sdarrenr * If we're creating state for a starting connection, start the 1827145522Sdarrenr * timer on it as we'll never see an error if it fails to 1828145522Sdarrenr * connect. 1829145522Sdarrenr */ 1830255332Scy (void) ipf_tcp_age(&is->is_sti, fin, softs->ipf_state_tcptq, 1831255332Scy is->is_flags, 2); 183260854Sdarrenr } 1833255332Scy MUTEX_EXIT(&is->is_lock); 1834145522Sdarrenr if ((is->is_flags & IS_STATESYNC) && ((is->is_flags & SI_CLONE) == 0)) 1835255332Scy is->is_sync = ipf_sync_new(softc, SMC_STATE, fin, is); 1836255332Scy if (softs->ipf_state_logging) 1837255332Scy ipf_state_log(softc, is, ISL_NEW); 1838145522Sdarrenr 1839255332Scy RWLOCK_EXIT(&softc->ipf_state); 1840255332Scy 1841145522Sdarrenr fin->fin_flx |= FI_STATE; 1842145522Sdarrenr if (fin->fin_flx & FI_FRAG) 1843255332Scy (void) ipf_frag_new(softc, fin, pass); 1844145522Sdarrenr 1845255332Scy fdp = &fr->fr_tifs[0]; 1846255332Scy if (fdp->fd_type == FRD_DSTLIST) { 1847255332Scy ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, 1848255332Scy &is->is_tifs[0]); 1849255332Scy } else { 1850255332Scy bcopy(fdp, &is->is_tifs[0], sizeof(*fdp)); 1851255332Scy } 1852255332Scy 1853255332Scy fdp = &fr->fr_tifs[1]; 1854255332Scy if (fdp->fd_type == FRD_DSTLIST) { 1855255332Scy ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, 1856255332Scy &is->is_tifs[1]); 1857255332Scy } else { 1858255332Scy bcopy(fdp, &is->is_tifs[1], sizeof(*fdp)); 1859255332Scy } 1860255332Scy fin->fin_tif = &is->is_tifs[fin->fin_rev]; 1861255332Scy 1862255332Scy fdp = &fr->fr_dif; 1863255332Scy if (fdp->fd_type == FRD_DSTLIST) { 1864255332Scy ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, 1865255332Scy &is->is_dif); 1866255332Scy } else { 1867255332Scy bcopy(fdp, &is->is_dif, sizeof(*fdp)); 1868255332Scy } 1869255332Scy fin->fin_dif = &is->is_dif; 1870255332Scy 1871255332Scy return 0; 187253642Sguido} 187353642Sguido 187453642Sguido 1875145522Sdarrenr/* ------------------------------------------------------------------------ */ 1876255332Scy/* Function: ipf_tcpoptions */ 1877153876Sguido/* Returns: int - 1 == packet matches state entry, 0 == it does not, */ 1878153876Sguido/* -1 == packet has bad TCP options data */ 1879255332Scy/* Parameters: softs(I) - pointer to state context structure */ 1880255332Scy/* fin(I) - pointer to packet information */ 1881145522Sdarrenr/* tcp(I) - pointer to TCP packet header */ 1882145522Sdarrenr/* td(I) - pointer to TCP data held as part of the state */ 1883145522Sdarrenr/* */ 1884145522Sdarrenr/* Look after the TCP header for any options and deal with those that are */ 1885145522Sdarrenr/* present. Record details about those that we recogise. */ 1886145522Sdarrenr/* ------------------------------------------------------------------------ */ 1887255332Scystatic int 1888255332Scyipf_tcpoptions(softs, fin, tcp, td) 1889255332Scy ipf_state_softc_t *softs; 1890255332Scy fr_info_t *fin; 1891255332Scy tcphdr_t *tcp; 1892255332Scy tcpdata_t *td; 189395418Sdarrenr{ 1894145522Sdarrenr int off, mlen, ol, i, len, retval; 1895145522Sdarrenr char buf[64], *s, opt; 1896145522Sdarrenr mb_t *m = NULL; 189753642Sguido 1898153876Sguido len = (TCP_OFF(tcp) << 2); 1899255332Scy if (fin->fin_dlen < len) { 1900255332Scy SBUMPD(ipf_state_stats, iss_tcp_toosmall); 1901145522Sdarrenr return 0; 1902255332Scy } 1903153876Sguido len -= sizeof(*tcp); 190495418Sdarrenr 1905153876Sguido off = fin->fin_plen - fin->fin_dlen + sizeof(*tcp) + fin->fin_ipoff; 1906153876Sguido 1907145522Sdarrenr m = fin->fin_m; 1908145522Sdarrenr mlen = MSGDSIZE(m) - off; 1909145522Sdarrenr if (len > mlen) { 1910145522Sdarrenr len = mlen; 1911145522Sdarrenr retval = 0; 1912145522Sdarrenr } else { 1913145522Sdarrenr retval = 1; 1914145522Sdarrenr } 191595418Sdarrenr 1916145522Sdarrenr COPYDATA(m, off, len, buf); 1917145522Sdarrenr 1918145522Sdarrenr for (s = buf; len > 0; ) { 1919145522Sdarrenr opt = *s; 1920145522Sdarrenr if (opt == TCPOPT_EOL) 192195418Sdarrenr break; 1922145522Sdarrenr else if (opt == TCPOPT_NOP) 1923145522Sdarrenr ol = 1; 1924145522Sdarrenr else { 1925145522Sdarrenr if (len < 2) 192695418Sdarrenr break; 1927145522Sdarrenr ol = (int)*(s + 1); 1928145522Sdarrenr if (ol < 2 || ol > len) 1929145522Sdarrenr break; 1930145522Sdarrenr 1931145522Sdarrenr /* 1932145522Sdarrenr * Extract the TCP options we are interested in out of 1933145522Sdarrenr * the header and store them in the the tcpdata struct. 1934145522Sdarrenr */ 1935145522Sdarrenr switch (opt) 1936145522Sdarrenr { 1937145522Sdarrenr case TCPOPT_WINDOW : 1938145522Sdarrenr if (ol == TCPOLEN_WINDOW) { 1939145522Sdarrenr i = (int)*(s + 2); 1940145522Sdarrenr if (i > TCP_WSCALE_MAX) 1941145522Sdarrenr i = TCP_WSCALE_MAX; 1942145522Sdarrenr else if (i < 0) 1943145522Sdarrenr i = 0; 1944145522Sdarrenr td->td_winscale = i; 1945153876Sguido td->td_winflags |= TCP_WSCALE_SEEN| 1946153876Sguido TCP_WSCALE_FIRST; 1947153876Sguido } else 1948153876Sguido retval = -1; 1949145522Sdarrenr break; 1950145522Sdarrenr case TCPOPT_MAXSEG : 1951145522Sdarrenr /* 1952145522Sdarrenr * So, if we wanted to set the TCP MAXSEG, 1953145522Sdarrenr * it should be done here... 1954145522Sdarrenr */ 1955145522Sdarrenr if (ol == TCPOLEN_MAXSEG) { 1956145522Sdarrenr i = (int)*(s + 2); 1957145522Sdarrenr i <<= 8; 1958145522Sdarrenr i += (int)*(s + 3); 1959145522Sdarrenr td->td_maxseg = i; 1960153876Sguido } else 1961153876Sguido retval = -1; 1962145522Sdarrenr break; 1963153876Sguido case TCPOPT_SACK_PERMITTED : 1964153876Sguido if (ol == TCPOLEN_SACK_PERMITTED) 1965153876Sguido td->td_winflags |= TCP_SACK_PERMIT; 1966153876Sguido else 1967153876Sguido retval = -1; 1968153876Sguido break; 1969145522Sdarrenr } 197095418Sdarrenr } 1971145522Sdarrenr len -= ol; 1972145522Sdarrenr s += ol; 197395418Sdarrenr } 1974255332Scy if (retval == -1) { 1975255332Scy SBUMPD(ipf_state_stats, iss_tcp_badopt); 1976255332Scy } 1977145522Sdarrenr return retval; 197895418Sdarrenr} 197995418Sdarrenr 198095418Sdarrenr 1981145522Sdarrenr/* ------------------------------------------------------------------------ */ 1982255332Scy/* Function: ipf_state_tcp */ 1983145522Sdarrenr/* Returns: int - 1 == packet matches state entry, 0 == it does not */ 1984255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 1985255332Scy/* softs(I) - pointer to state context structure */ 1986255332Scy/* fin(I) - pointer to packet information */ 1987145522Sdarrenr/* tcp(I) - pointer to TCP packet header */ 1988145522Sdarrenr/* is(I) - pointer to master state structure */ 1989145522Sdarrenr/* */ 1990145522Sdarrenr/* Check to see if a packet with TCP headers fits within the TCP window. */ 1991145522Sdarrenr/* Change timeout depending on whether new packet is a SYN-ACK returning */ 1992145522Sdarrenr/* for a SYN or a RST or FIN which indicate time to close up shop. */ 1993145522Sdarrenr/* ------------------------------------------------------------------------ */ 1994255332Scystatic int 1995255332Scyipf_state_tcp(softc, softs, fin, tcp, is) 1996255332Scy ipf_main_softc_t *softc; 1997255332Scy ipf_state_softc_t *softs; 1998255332Scy fr_info_t *fin; 1999255332Scy tcphdr_t *tcp; 2000255332Scy ipstate_t *is; 200153642Sguido{ 200253642Sguido tcpdata_t *fdata, *tdata; 2003255332Scy int source, ret, flags; 200453642Sguido 2005145522Sdarrenr source = !fin->fin_rev; 2006255332Scy if (((is->is_flags & IS_TCPFSM) != 0) && (source == 1) && 2007145522Sdarrenr (ntohs(is->is_sport) != fin->fin_data[0])) 2008145522Sdarrenr source = 0; 2009145522Sdarrenr fdata = &is->is_tcp.ts_data[!source]; 2010145522Sdarrenr tdata = &is->is_tcp.ts_data[source]; 2011145522Sdarrenr 2012145522Sdarrenr MUTEX_ENTER(&is->is_lock); 2013170268Sdarrenr 2014170268Sdarrenr /* 2015170268Sdarrenr * If a SYN packet is received for a connection that is on the way out 2016170268Sdarrenr * but hasn't yet departed then advance this session along the way. 2017170268Sdarrenr */ 2018170268Sdarrenr if ((tcp->th_flags & TH_OPENING) == TH_SYN) { 2019170268Sdarrenr if ((is->is_state[0] > IPF_TCPS_ESTABLISHED) && 2020170268Sdarrenr (is->is_state[1] > IPF_TCPS_ESTABLISHED)) { 2021170268Sdarrenr is->is_state[!source] = IPF_TCPS_CLOSED; 2022255332Scy ipf_movequeue(softc->ipf_ticks, &is->is_sti, 2023255332Scy is->is_sti.tqe_ifq, 2024255332Scy &softs->ipf_state_deletetq); 2025172776Sdarrenr MUTEX_EXIT(&is->is_lock); 2026255332Scy DT1(iss_tcp_closing, ipstate_t *, is); 2027255332Scy SBUMP(ipf_state_stats.iss_tcp_closing); 2028170268Sdarrenr return 0; 2029170268Sdarrenr } 2030170268Sdarrenr } 2031170268Sdarrenr 2032255332Scy if (is->is_flags & IS_LOOSE) 2033255332Scy ret = 1; 2034255332Scy else 2035255332Scy ret = ipf_state_tcpinwindow(fin, fdata, tdata, tcp, 2036255332Scy is->is_flags); 2037172776Sdarrenr if (ret > 0) { 2038145522Sdarrenr /* 2039145522Sdarrenr * Nearing end of connection, start timeout. 2040145522Sdarrenr */ 2041255332Scy ret = ipf_tcp_age(&is->is_sti, fin, softs->ipf_state_tcptq, 2042255332Scy is->is_flags, ret); 2043145522Sdarrenr if (ret == 0) { 2044145522Sdarrenr MUTEX_EXIT(&is->is_lock); 2045255332Scy DT2(iss_tcp_fsm, fr_info_t *, fin, ipstate_t *, is); 2046255332Scy SBUMP(ipf_state_stats.iss_tcp_fsm); 2047145522Sdarrenr return 0; 2048145522Sdarrenr } 2049145522Sdarrenr 2050255332Scy if (softs->ipf_state_logging > 4) 2051255332Scy ipf_state_log(softc, is, ISL_STATECHANGE); 2052255332Scy 2053145522Sdarrenr /* 2054145522Sdarrenr * set s0's as appropriate. Use syn-ack packet as it 2055145522Sdarrenr * contains both pieces of required information. 2056145522Sdarrenr */ 2057145522Sdarrenr /* 2058145522Sdarrenr * Window scale option is only present in SYN/SYN-ACK packet. 2059145522Sdarrenr * Compare with ~TH_FIN to mask out T/TCP setups. 2060145522Sdarrenr */ 2061145522Sdarrenr flags = tcp->th_flags & ~(TH_FIN|TH_ECNALL); 2062145522Sdarrenr if (flags == (TH_SYN|TH_ACK)) { 2063145522Sdarrenr is->is_s0[source] = ntohl(tcp->th_ack); 2064145522Sdarrenr is->is_s0[!source] = ntohl(tcp->th_seq) + 1; 2065165515Sdarrenr if ((TCP_OFF(tcp) > (sizeof(tcphdr_t) >> 2))) { 2066255332Scy if (ipf_tcpoptions(softs, fin, tcp, 2067255332Scy fdata) == -1) 2068153876Sguido fin->fin_flx |= FI_BAD; 2069145522Sdarrenr } 2070145522Sdarrenr if ((fin->fin_out != 0) && (is->is_pass & FR_NEWISN)) 2071255332Scy ipf_checknewisn(fin, is); 2072145522Sdarrenr } else if (flags == TH_SYN) { 2073145522Sdarrenr is->is_s0[source] = ntohl(tcp->th_seq) + 1; 2074153876Sguido if ((TCP_OFF(tcp) > (sizeof(tcphdr_t) >> 2))) { 2075255332Scy if (ipf_tcpoptions(softs, fin, tcp, 2076255332Scy fdata) == -1) 2077153876Sguido fin->fin_flx |= FI_BAD; 2078153876Sguido } 2079145522Sdarrenr 2080145522Sdarrenr if ((fin->fin_out != 0) && (is->is_pass & FR_NEWISN)) 2081255332Scy ipf_checknewisn(fin, is); 2082145522Sdarrenr 2083145522Sdarrenr } 2084145522Sdarrenr ret = 1; 2085170268Sdarrenr } else { 2086255332Scy DT2(iss_tcp_oow, fr_info_t *, fin, ipstate_t *, is); 2087255332Scy SBUMP(ipf_state_stats.iss_tcp_oow); 2088255332Scy ret = 0; 2089170268Sdarrenr } 2090145522Sdarrenr MUTEX_EXIT(&is->is_lock); 2091145522Sdarrenr return ret; 2092145522Sdarrenr} 2093145522Sdarrenr 2094145522Sdarrenr 2095145522Sdarrenr/* ------------------------------------------------------------------------ */ 2096255332Scy/* Function: ipf_checknewisn */ 2097145522Sdarrenr/* Returns: Nil */ 2098145522Sdarrenr/* Parameters: fin(I) - pointer to packet information */ 2099145522Sdarrenr/* is(I) - pointer to master state structure */ 2100145522Sdarrenr/* */ 2101145522Sdarrenr/* Check to see if this TCP connection is expecting and needs a new */ 2102145522Sdarrenr/* sequence number for a particular direction of the connection. */ 2103145522Sdarrenr/* */ 2104145522Sdarrenr/* NOTE: This does not actually change the sequence numbers, only gets new */ 2105145522Sdarrenr/* one ready. */ 2106145522Sdarrenr/* ------------------------------------------------------------------------ */ 2107255332Scystatic void 2108255332Scyipf_checknewisn(fin, is) 2109255332Scy fr_info_t *fin; 2110255332Scy ipstate_t *is; 2111145522Sdarrenr{ 2112145522Sdarrenr u_32_t sumd, old, new; 2113145522Sdarrenr tcphdr_t *tcp; 2114145522Sdarrenr int i; 2115145522Sdarrenr 2116145522Sdarrenr i = fin->fin_rev; 2117145522Sdarrenr tcp = fin->fin_dp; 2118145522Sdarrenr 2119145522Sdarrenr if (((i == 0) && !(is->is_flags & IS_ISNSYN)) || 2120145522Sdarrenr ((i == 1) && !(is->is_flags & IS_ISNACK))) { 2121145522Sdarrenr old = ntohl(tcp->th_seq); 2122255332Scy new = ipf_newisn(fin); 2123145522Sdarrenr is->is_isninc[i] = new - old; 2124145522Sdarrenr CALC_SUMD(old, new, sumd); 2125145522Sdarrenr is->is_sumd[i] = (sumd & 0xffff) + (sumd >> 16); 2126145522Sdarrenr 2127145522Sdarrenr is->is_flags |= ((i == 0) ? IS_ISNSYN : IS_ISNACK); 2128145522Sdarrenr } 2129145522Sdarrenr} 2130145522Sdarrenr 2131145522Sdarrenr 2132145522Sdarrenr/* ------------------------------------------------------------------------ */ 2133255332Scy/* Function: ipf_state_tcpinwindow */ 2134255332Scy/* Returns: int - 1 == packet inside TCP "window", 0 == not inside. */ 2135145522Sdarrenr/* Parameters: fin(I) - pointer to packet information */ 2136145522Sdarrenr/* fdata(I) - pointer to tcp state informatio (forward) */ 2137145522Sdarrenr/* tdata(I) - pointer to tcp state informatio (reverse) */ 2138145522Sdarrenr/* tcp(I) - pointer to TCP packet header */ 2139145522Sdarrenr/* */ 2140145522Sdarrenr/* Given a packet has matched addresses and ports, check to see if it is */ 2141145522Sdarrenr/* within the TCP data window. In a show of generosity, allow packets that */ 2142145522Sdarrenr/* are within the window space behind the current sequence # as well. */ 2143145522Sdarrenr/* ------------------------------------------------------------------------ */ 2144255332Scystatic int 2145255332Scyipf_state_tcpinwindow(fin, fdata, tdata, tcp, flags) 2146255332Scy fr_info_t *fin; 2147255332Scy tcpdata_t *fdata, *tdata; 2148255332Scy tcphdr_t *tcp; 2149255332Scy int flags; 2150145522Sdarrenr{ 2151255332Scy ipf_main_softc_t *softc = fin->fin_main_soft; 2152255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 2153145522Sdarrenr tcp_seq seq, ack, end; 2154145522Sdarrenr int ackskew, tcpflags; 2155145522Sdarrenr u_32_t win, maxwin; 2156153876Sguido int dsize, inseq; 2157145522Sdarrenr 215853642Sguido /* 215953642Sguido * Find difference between last checked packet and this packet. 216053642Sguido */ 2161145522Sdarrenr tcpflags = tcp->th_flags; 216253642Sguido seq = ntohl(tcp->th_seq); 216353642Sguido ack = ntohl(tcp->th_ack); 2164145522Sdarrenr if (tcpflags & TH_SYN) 2165145522Sdarrenr win = ntohs(tcp->th_win); 216695418Sdarrenr else 2167145522Sdarrenr win = ntohs(tcp->th_win) << fdata->td_winscale; 2168153876Sguido 2169153876Sguido /* 2170153876Sguido * A window of 0 produces undesirable behaviour from this function. 2171153876Sguido */ 2172145522Sdarrenr if (win == 0) 2173145522Sdarrenr win = 1; 217495418Sdarrenr 2175153876Sguido dsize = fin->fin_dlen - (TCP_OFF(tcp) << 2) + 2176153876Sguido ((tcpflags & TH_SYN) ? 1 : 0) + ((tcpflags & TH_FIN) ? 1 : 0); 2177153876Sguido 2178145522Sdarrenr /* 2179145522Sdarrenr * if window scaling is present, the scaling is only allowed 2180145522Sdarrenr * for windows not in the first SYN packet. In that packet the 2181145522Sdarrenr * window is 65535 to specify the largest window possible 2182145522Sdarrenr * for receivers not implementing the window scale option. 2183145522Sdarrenr * Currently, we do not assume TTCP here. That means that 2184145522Sdarrenr * if we see a second packet from a host (after the initial 2185145522Sdarrenr * SYN), we can assume that the receiver of the SYN did 2186145522Sdarrenr * already send back the SYN/ACK (and thus that we know if 2187145522Sdarrenr * the receiver also does window scaling) 2188145522Sdarrenr */ 2189145522Sdarrenr if (!(tcpflags & TH_SYN) && (fdata->td_winflags & TCP_WSCALE_FIRST)) { 2190165515Sdarrenr fdata->td_winflags &= ~TCP_WSCALE_FIRST; 2191165515Sdarrenr fdata->td_maxwin = win; 2192145522Sdarrenr } 219395418Sdarrenr 2194153876Sguido end = seq + dsize; 219595418Sdarrenr 219695418Sdarrenr if ((fdata->td_end == 0) && 2197145522Sdarrenr (!(flags & IS_TCPFSM) || 2198145522Sdarrenr ((tcpflags & TH_OPENING) == TH_OPENING))) { 219953642Sguido /* 220053642Sguido * Must be a (outgoing) SYN-ACK in reply to a SYN. 220153642Sguido */ 2202153876Sguido fdata->td_end = end - 1; 220353642Sguido fdata->td_maxwin = 1; 220498004Sdarrenr fdata->td_maxend = end + win; 220553642Sguido } 220653642Sguido 2207145522Sdarrenr if (!(tcpflags & TH_ACK)) { /* Pretend an ack was sent */ 220853642Sguido ack = tdata->td_end; 2209145522Sdarrenr } else if (((tcpflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) && 221053642Sguido (ack == 0)) { 221153642Sguido /* gross hack to get around certain broken tcp stacks */ 221253642Sguido ack = tdata->td_end; 221353642Sguido } 221453642Sguido 221553642Sguido maxwin = tdata->td_maxwin; 221653642Sguido ackskew = tdata->td_end - ack; 221753642Sguido 2218145522Sdarrenr /* 2219145522Sdarrenr * Strict sequencing only allows in-order delivery. 2220145522Sdarrenr */ 2221255332Scy if ((flags & IS_STRICT) != 0) { 2222255332Scy if (seq != fdata->td_end) { 2223255332Scy DT2(iss_tcp_struct, tcpdata_t *, fdata, int, seq); 2224255332Scy SBUMP(ipf_state_stats.iss_tcp_strict); 2225255332Scy fin->fin_flx |= FI_OOW; 2226145522Sdarrenr return 0; 2227145522Sdarrenr } 2228145522Sdarrenr } 2229145522Sdarrenr 2230255332Scy#define SEQ_GE(a,b) ((int)((a) - (b)) >= 0) 2231255332Scy#define SEQ_GT(a,b) ((int)((a) - (b)) > 0) 2232153876Sguido inseq = 0; 2233153876Sguido if ((SEQ_GE(fdata->td_maxend, end)) && 223457096Sguido (SEQ_GE(seq, fdata->td_end - maxwin)) && 223553642Sguido/* XXX what about big packets */ 223653642Sguido#define MAXACKWINDOW 66000 2237165515Sdarrenr (-ackskew <= (MAXACKWINDOW)) && 2238145522Sdarrenr ( ackskew <= (MAXACKWINDOW << fdata->td_winscale))) { 2239153876Sguido inseq = 1; 2240153876Sguido /* 2241153876Sguido * Microsoft Windows will send the next packet to the right of the 2242153876Sguido * window if SACK is in use. 2243153876Sguido */ 2244153876Sguido } else if ((seq == fdata->td_maxend) && (ackskew == 0) && 2245153876Sguido (fdata->td_winflags & TCP_SACK_PERMIT) && 2246153876Sguido (tdata->td_winflags & TCP_SACK_PERMIT)) { 2247255332Scy DT2(iss_sinsack, tcpdata_t *, fdata, int, seq); 2248255332Scy SBUMP(ipf_state_stats.iss_winsack); 2249153876Sguido inseq = 1; 2250161356Sguido /* 2251161356Sguido * Sometimes a TCP RST will be generated with only the ACK field 2252161356Sguido * set to non-zero. 2253161356Sguido */ 2254161356Sguido } else if ((seq == 0) && (tcpflags == (TH_RST|TH_ACK)) && 2255161356Sguido (ackskew >= -1) && (ackskew <= 1)) { 2256161356Sguido inseq = 1; 2257161356Sguido } else if (!(flags & IS_TCPFSM)) { 2258161356Sguido int i; 2259161356Sguido 2260161356Sguido i = (fin->fin_rev << 1) + fin->fin_out; 2261161356Sguido 2262161356Sguido#if 0 2263161356Sguido if (is_pkts[i]0 == 0) { 2264161356Sguido /* 2265161356Sguido * Picking up a connection in the middle, the "next" 2266161356Sguido * packet seen from a direction that is new should be 2267161356Sguido * accepted, even if it appears out of sequence. 2268161356Sguido */ 2269161356Sguido inseq = 1; 2270255332Scy } else 2271161356Sguido#endif 2272161356Sguido if (!(fdata->td_winflags & 2273161356Sguido (TCP_WSCALE_SEEN|TCP_WSCALE_FIRST))) { 2274161356Sguido /* 2275161356Sguido * No TCPFSM and no window scaling, so make some 2276161356Sguido * extra guesses. 2277161356Sguido */ 2278161356Sguido if ((seq == fdata->td_maxend) && (ackskew == 0)) 2279161356Sguido inseq = 1; 2280161356Sguido else if (SEQ_GE(seq + maxwin, fdata->td_end - maxwin)) 2281161356Sguido inseq = 1; 2282161356Sguido } 2283153876Sguido } 2284130886Sdarrenr 2285165515Sdarrenr /* TRACE(inseq, fdata, tdata, seq, end, ack, ackskew, win, maxwin) */ 2286165515Sdarrenr 2287153876Sguido if (inseq) { 2288130886Sdarrenr /* if ackskew < 0 then this should be due to fragmented 228953642Sguido * packets. There is no way to know the length of the 229053642Sguido * total packet in advance. 229153642Sguido * We do know the total length from the fragment cache though. 229253642Sguido * Note however that there might be more sessions with 2293130886Sdarrenr * exactly the same source and destination parameters in the 229453642Sguido * state cache (and source and destination is the only stuff 229553642Sguido * that is saved in the fragment cache). Note further that 229653642Sguido * some TCP connections in the state cache are hashed with 229753642Sguido * sport and dport as well which makes it not worthwhile to 229853642Sguido * look for them. 229953642Sguido * Thus, when ackskew is negative but still seems to belong 230053642Sguido * to this session, we bump up the destinations end value. 230153642Sguido */ 2302145522Sdarrenr if (ackskew < 0) 2303145522Sdarrenr tdata->td_end = ack; 230495418Sdarrenr 2305145522Sdarrenr /* update max window seen */ 2306145522Sdarrenr if (fdata->td_maxwin < win) 2307145522Sdarrenr fdata->td_maxwin = win; 2308145522Sdarrenr if (SEQ_GT(end, fdata->td_end)) 2309145522Sdarrenr fdata->td_end = end; 2310145522Sdarrenr if (SEQ_GE(ack + win, tdata->td_maxend)) 2311145522Sdarrenr tdata->td_maxend = ack + win; 2312145522Sdarrenr return 1; 2313145522Sdarrenr } 2314255332Scy SBUMP(ipf_state_stats.iss_oow); 2315255332Scy fin->fin_flx |= FI_OOW; 2316145522Sdarrenr return 0; 2317145522Sdarrenr} 231895418Sdarrenr 2319145522Sdarrenr 2320145522Sdarrenr/* ------------------------------------------------------------------------ */ 2321255332Scy/* Function: ipf_state_clone */ 2322145522Sdarrenr/* Returns: ipstate_t* - NULL == cloning failed, */ 2323145522Sdarrenr/* else pointer to new state structure */ 2324145522Sdarrenr/* Parameters: fin(I) - pointer to packet information */ 2325145522Sdarrenr/* tcp(I) - pointer to TCP/UDP header */ 2326145522Sdarrenr/* is(I) - pointer to master state structure */ 2327145522Sdarrenr/* */ 2328145522Sdarrenr/* Create a "duplcate" state table entry from the master. */ 2329145522Sdarrenr/* ------------------------------------------------------------------------ */ 2330255332Scystatic ipstate_t * 2331255332Scyipf_state_clone(fin, tcp, is) 2332255332Scy fr_info_t *fin; 2333255332Scy tcphdr_t *tcp; 2334255332Scy ipstate_t *is; 2335145522Sdarrenr{ 2336255332Scy ipf_main_softc_t *softc = fin->fin_main_soft; 2337255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 2338145522Sdarrenr ipstate_t *clone; 2339145522Sdarrenr u_32_t send; 2340145522Sdarrenr 2341255332Scy if (softs->ipf_state_stats.iss_active == softs->ipf_state_max) { 2342255332Scy SBUMPD(ipf_state_stats, iss_max); 2343255332Scy softs->ipf_state_doflush = 1; 2344145522Sdarrenr return NULL; 234553642Sguido } 2346145522Sdarrenr KMALLOC(clone, ipstate_t *); 2347255332Scy if (clone == NULL) { 2348255332Scy SBUMPD(ipf_state_stats, iss_clone_nomem); 2349145522Sdarrenr return NULL; 2350255332Scy } 2351145522Sdarrenr bcopy((char *)is, (char *)clone, sizeof(*clone)); 2352145522Sdarrenr 2353145522Sdarrenr MUTEX_NUKE(&clone->is_lock); 2354255332Scy /* 2355255332Scy * It has not yet been placed on any timeout queue, so make sure 2356255332Scy * all of that data is zero'd out. 2357255332Scy */ 2358255332Scy clone->is_sti.tqe_pnext = NULL; 2359255332Scy clone->is_sti.tqe_next = NULL; 2360255332Scy clone->is_sti.tqe_ifq = NULL; 2361255332Scy clone->is_sti.tqe_parent = clone; 2362145522Sdarrenr 2363255332Scy clone->is_die = ONE_DAY + softc->ipf_ticks; 2364145522Sdarrenr clone->is_state[0] = 0; 2365145522Sdarrenr clone->is_state[1] = 0; 2366145522Sdarrenr send = ntohl(tcp->th_seq) + fin->fin_dlen - (TCP_OFF(tcp) << 2) + 2367145522Sdarrenr ((tcp->th_flags & TH_SYN) ? 1 : 0) + 2368145522Sdarrenr ((tcp->th_flags & TH_FIN) ? 1 : 0); 2369145522Sdarrenr 2370145522Sdarrenr if (fin->fin_rev == 1) { 2371145522Sdarrenr clone->is_dend = send; 2372145522Sdarrenr clone->is_maxdend = send; 2373145522Sdarrenr clone->is_send = 0; 2374145522Sdarrenr clone->is_maxswin = 1; 2375145522Sdarrenr clone->is_maxdwin = ntohs(tcp->th_win); 2376145522Sdarrenr if (clone->is_maxdwin == 0) 2377145522Sdarrenr clone->is_maxdwin = 1; 2378145522Sdarrenr } else { 2379145522Sdarrenr clone->is_send = send; 2380145522Sdarrenr clone->is_maxsend = send; 2381145522Sdarrenr clone->is_dend = 0; 2382145522Sdarrenr clone->is_maxdwin = 1; 2383145522Sdarrenr clone->is_maxswin = ntohs(tcp->th_win); 2384145522Sdarrenr if (clone->is_maxswin == 0) 2385145522Sdarrenr clone->is_maxswin = 1; 2386145522Sdarrenr } 2387145522Sdarrenr 2388145522Sdarrenr clone->is_flags &= ~SI_CLONE; 2389145522Sdarrenr clone->is_flags |= SI_CLONED; 2390255332Scy if (ipf_state_insert(softc, clone, fin->fin_rev) == -1) { 2391255332Scy KFREE(clone); 2392255332Scy return NULL; 2393255332Scy } 2394255332Scy 2395255332Scy clone->is_ref = 1; 2396145522Sdarrenr if (clone->is_p == IPPROTO_TCP) { 2397255332Scy (void) ipf_tcp_age(&clone->is_sti, fin, softs->ipf_state_tcptq, 2398255332Scy clone->is_flags, 2); 2399145522Sdarrenr } 2400145522Sdarrenr MUTEX_EXIT(&clone->is_lock); 2401145522Sdarrenr if (is->is_flags & IS_STATESYNC) 2402255332Scy clone->is_sync = ipf_sync_new(softc, SMC_STATE, fin, clone); 2403255332Scy DT2(iss_clone, ipstate_t *, is, ipstate_t *, clone); 2404255332Scy SBUMP(ipf_state_stats.iss_cloned); 2405145522Sdarrenr return clone; 240653642Sguido} 240753642Sguido 240853642Sguido 2409145522Sdarrenr/* ------------------------------------------------------------------------ */ 2410255332Scy/* Function: ipf_matchsrcdst */ 2411145522Sdarrenr/* Returns: Nil */ 2412255332Scy/* Parameters: fin(I) - pointer to packet information */ 2413255332Scy/* is(I) - pointer to state structure */ 2414255332Scy/* src(I) - pointer to source address */ 2415255332Scy/* dst(I) - pointer to destination address */ 2416255332Scy/* tcp(I) - pointer to TCP/UDP header */ 2417255332Scy/* cmask(I) - mask of FI_* bits to check */ 2418145522Sdarrenr/* */ 2419145522Sdarrenr/* Match a state table entry against an IP packet. The logic below is that */ 2420145522Sdarrenr/* ret gets set to one if the match succeeds, else remains 0. If it is */ 2421145522Sdarrenr/* still 0 after the test. no match. */ 2422145522Sdarrenr/* ------------------------------------------------------------------------ */ 2423255332Scystatic ipstate_t * 2424255332Scyipf_matchsrcdst(fin, is, src, dst, tcp, cmask) 2425255332Scy fr_info_t *fin; 2426255332Scy ipstate_t *is; 2427255332Scy i6addr_t *src, *dst; 2428255332Scy tcphdr_t *tcp; 2429255332Scy u_32_t cmask; 243053642Sguido{ 2431255332Scy ipf_main_softc_t *softc = fin->fin_main_soft; 2432255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 2433145522Sdarrenr int ret = 0, rev, out, flags, flx = 0, idx; 243453642Sguido u_short sp, dp; 2435145522Sdarrenr u_32_t cflx; 243653642Sguido void *ifp; 243753642Sguido 2438255332Scy /* 2439255332Scy * If a connection is about to be deleted, no packets 2440255332Scy * are allowed to match it. 2441255332Scy */ 2442255332Scy if (is->is_sti.tqe_ifq == &softs->ipf_state_deletetq) 2443255332Scy return NULL; 2444255332Scy 2445145522Sdarrenr rev = IP6_NEQ(&is->is_dst, dst); 244653642Sguido ifp = fin->fin_ifp; 244753642Sguido out = fin->fin_out; 2448145522Sdarrenr flags = is->is_flags; 244992685Sdarrenr sp = 0; 245092685Sdarrenr dp = 0; 245153642Sguido 245253642Sguido if (tcp != NULL) { 2453145522Sdarrenr sp = htons(fin->fin_sport); 2454145522Sdarrenr dp = ntohs(fin->fin_dport); 2455145522Sdarrenr } 2456145522Sdarrenr if (!rev) { 2457145522Sdarrenr if (tcp != NULL) { 2458145522Sdarrenr if (!(flags & SI_W_SPORT) && (sp != is->is_sport)) 245980482Sdarrenr rev = 1; 2460145522Sdarrenr else if (!(flags & SI_W_DPORT) && (dp != is->is_dport)) 246180482Sdarrenr rev = 1; 246280482Sdarrenr } 246353642Sguido } 246453642Sguido 246592685Sdarrenr idx = (out << 1) + rev; 246692685Sdarrenr 2467145522Sdarrenr /* 2468145522Sdarrenr * If the interface for this 'direction' is set, make sure it matches. 2469145522Sdarrenr * An interface name that is not set matches any, as does a name of *. 2470145522Sdarrenr */ 2471170268Sdarrenr if ((is->is_ifp[idx] == ifp) || (is->is_ifp[idx] == NULL && 2472170268Sdarrenr (*is->is_ifname[idx] == '\0' || *is->is_ifname[idx] == '-' || 2473170268Sdarrenr *is->is_ifname[idx] == '*'))) 247492685Sdarrenr ret = 1; 247592685Sdarrenr 2476255332Scy if (ret == 0) { 2477255332Scy DT2(iss_lookup_badifp, fr_info_t *, fin, ipstate_t *, is); 2478255332Scy SBUMP(ipf_state_stats.iss_lookup_badifp); 2479255332Scy /* TRACE is, out, rev, idx */ 2480145522Sdarrenr return NULL; 2481255332Scy } 248253642Sguido ret = 0; 248353642Sguido 2484145522Sdarrenr /* 2485145522Sdarrenr * Match addresses and ports. 2486145522Sdarrenr */ 248753642Sguido if (rev == 0) { 2488145522Sdarrenr if ((IP6_EQ(&is->is_dst, dst) || (flags & SI_W_DADDR)) && 2489145522Sdarrenr (IP6_EQ(&is->is_src, src) || (flags & SI_W_SADDR))) { 2490145522Sdarrenr if (tcp) { 2491255332Scy if ((sp == is->is_sport || flags & SI_W_SPORT) 2492255332Scy && 2493145522Sdarrenr (dp == is->is_dport || flags & SI_W_DPORT)) 2494145522Sdarrenr ret = 1; 2495145522Sdarrenr } else { 2496145522Sdarrenr ret = 1; 2497145522Sdarrenr } 249853642Sguido } 249953642Sguido } else { 2500145522Sdarrenr if ((IP6_EQ(&is->is_dst, src) || (flags & SI_W_DADDR)) && 2501145522Sdarrenr (IP6_EQ(&is->is_src, dst) || (flags & SI_W_SADDR))) { 2502145522Sdarrenr if (tcp) { 2503255332Scy if ((dp == is->is_sport || flags & SI_W_SPORT) 2504255332Scy && 2505145522Sdarrenr (sp == is->is_dport || flags & SI_W_DPORT)) 2506145522Sdarrenr ret = 1; 2507145522Sdarrenr } else { 2508145522Sdarrenr ret = 1; 2509145522Sdarrenr } 251053642Sguido } 251153642Sguido } 2512145522Sdarrenr 2513255332Scy if (ret == 0) { 2514255332Scy SBUMP(ipf_state_stats.iss_lookup_badport); 2515255332Scy DT2(iss_lookup_badport, fr_info_t *, fin, ipstate_t *, is); 2516255332Scy /* TRACE rev, is, sp, dp, src, dst */ 2517145522Sdarrenr return NULL; 2518255332Scy } 251953642Sguido 252053642Sguido /* 252153642Sguido * Whether or not this should be here, is questionable, but the aim 252253642Sguido * is to get this out of the main line. 252353642Sguido */ 252453642Sguido if (tcp == NULL) 2525145522Sdarrenr flags = is->is_flags & ~(SI_WILDP|SI_NEWFR|SI_CLONE|SI_CLONED); 252653642Sguido 2527145522Sdarrenr /* 2528145522Sdarrenr * Only one of the source or destination address can be flaged as a 2529145522Sdarrenr * wildcard. Fill in the missing address, if set. 2530145522Sdarrenr * For IPv6, if the address being copied in is multicast, then 2531145522Sdarrenr * don't reset the wild flag - multicast causes it to be set in the 2532145522Sdarrenr * first place! 2533145522Sdarrenr */ 2534145522Sdarrenr if ((flags & (SI_W_SADDR|SI_W_DADDR))) { 2535145522Sdarrenr fr_ip_t *fi = &fin->fin_fi; 253653642Sguido 2537145522Sdarrenr if ((flags & SI_W_SADDR) != 0) { 253892685Sdarrenr if (rev == 0) { 2539255332Scy is->is_src = fi->fi_src; 2540255332Scy is->is_flags &= ~SI_W_SADDR; 254192685Sdarrenr } else { 2542255332Scy if (!(fin->fin_flx & (FI_MULTICAST|FI_MBCAST))){ 2543145522Sdarrenr is->is_src = fi->fi_dst; 2544145522Sdarrenr is->is_flags &= ~SI_W_SADDR; 2545145522Sdarrenr } 254692685Sdarrenr } 2547145522Sdarrenr } else if ((flags & SI_W_DADDR) != 0) { 254892685Sdarrenr if (rev == 0) { 2549255332Scy if (!(fin->fin_flx & (FI_MULTICAST|FI_MBCAST))){ 2550145522Sdarrenr is->is_dst = fi->fi_dst; 2551145522Sdarrenr is->is_flags &= ~SI_W_DADDR; 2552145522Sdarrenr } 255392685Sdarrenr } else { 2554255332Scy is->is_dst = fi->fi_src; 2555255332Scy is->is_flags &= ~SI_W_DADDR; 255692685Sdarrenr } 255792685Sdarrenr } 2558145522Sdarrenr if ((is->is_flags & (SI_WILDA|SI_WILDP)) == 0) { 2559255332Scy ATOMIC_DECL(softs->ipf_state_stats.iss_wild); 2560145522Sdarrenr } 256192685Sdarrenr } 256292685Sdarrenr 2563145522Sdarrenr flx = fin->fin_flx & cmask; 2564145522Sdarrenr cflx = is->is_flx[out][rev]; 2565145522Sdarrenr 2566145522Sdarrenr /* 2567145522Sdarrenr * Match up any flags set from IP options. 2568145522Sdarrenr */ 2569145522Sdarrenr if ((cflx && (flx != (cflx & cmask))) || 2570153876Sguido ((fin->fin_optmsk & is->is_optmsk[rev]) != is->is_opt[rev]) || 2571145522Sdarrenr ((fin->fin_secmsk & is->is_secmsk) != is->is_sec) || 2572255332Scy ((fin->fin_auth & is->is_authmsk) != is->is_auth)) { 2573255332Scy SBUMPD(ipf_state_stats, iss_miss_mask); 2574145522Sdarrenr return NULL; 2575255332Scy } 2576145522Sdarrenr 2577255332Scy if ((fin->fin_flx & FI_IGNORE) != 0) { 2578255332Scy fin->fin_rev = rev; 2579255332Scy return is; 2580255332Scy } 2581255332Scy 2582145522Sdarrenr /* 2583145522Sdarrenr * Only one of the source or destination port can be flagged as a 2584145522Sdarrenr * wildcard. When filling it in, fill in a copy of the matched entry 2585145522Sdarrenr * if it has the cloning flag set. 2586145522Sdarrenr */ 2587145522Sdarrenr if ((flags & (SI_W_SPORT|SI_W_DPORT))) { 2588145522Sdarrenr if ((flags & SI_CLONE) != 0) { 2589153876Sguido ipstate_t *clone; 2590153876Sguido 2591255332Scy clone = ipf_state_clone(fin, tcp, is); 2592153876Sguido if (clone == NULL) 2593145522Sdarrenr return NULL; 2594153876Sguido is = clone; 2595145522Sdarrenr } else { 2596255332Scy ATOMIC_DECL(softs->ipf_state_stats.iss_wild); 2597145522Sdarrenr } 2598145522Sdarrenr 2599145522Sdarrenr if ((flags & SI_W_SPORT) != 0) { 260053642Sguido if (rev == 0) { 260153642Sguido is->is_sport = sp; 2602145522Sdarrenr is->is_send = ntohl(tcp->th_seq); 260353642Sguido } else { 260453642Sguido is->is_sport = dp; 2605145522Sdarrenr is->is_send = ntohl(tcp->th_ack); 260653642Sguido } 260753642Sguido is->is_maxsend = is->is_send + 1; 2608145522Sdarrenr } else if ((flags & SI_W_DPORT) != 0) { 260953642Sguido if (rev == 0) { 261053642Sguido is->is_dport = dp; 2611145522Sdarrenr is->is_dend = ntohl(tcp->th_ack); 261253642Sguido } else { 261353642Sguido is->is_dport = sp; 2614145522Sdarrenr is->is_dend = ntohl(tcp->th_seq); 261553642Sguido } 261653642Sguido is->is_maxdend = is->is_dend + 1; 261753642Sguido } 2618145522Sdarrenr is->is_flags &= ~(SI_W_SPORT|SI_W_DPORT); 2619255332Scy if ((flags & SI_CLONED) && softs->ipf_state_logging) 2620255332Scy ipf_state_log(softc, is, ISL_CLONE); 262153642Sguido } 262253642Sguido 262360854Sdarrenr ret = -1; 262460854Sdarrenr 2625153876Sguido if (is->is_flx[out][rev] == 0) { 2626145522Sdarrenr is->is_flx[out][rev] = flx; 2627255332Scy if (rev == 1 && is->is_optmsk[1] == 0) { 2628255332Scy is->is_opt[1] = fin->fin_optmsk; 2629255332Scy is->is_optmsk[1] = 0xffffffff; 2630255332Scy if (is->is_v == 6) { 2631255332Scy is->is_opt[1] &= ~0x8; 2632255332Scy is->is_optmsk[1] &= ~0x8; 2633255332Scy } 2634153876Sguido } 2635153876Sguido } 2636145522Sdarrenr 2637145522Sdarrenr /* 2638145522Sdarrenr * Check if the interface name for this "direction" is set and if not, 2639145522Sdarrenr * fill it in. 2640145522Sdarrenr */ 264192685Sdarrenr if (is->is_ifp[idx] == NULL && 2642145522Sdarrenr (*is->is_ifname[idx] == '\0' || *is->is_ifname[idx] == '*')) { 2643145522Sdarrenr is->is_ifp[idx] = ifp; 2644255332Scy COPYIFNAME(fin->fin_v, ifp, is->is_ifname[idx]); 264560854Sdarrenr } 264680482Sdarrenr fin->fin_rev = rev; 2647145522Sdarrenr return is; 264853642Sguido} 264953642Sguido 265060854Sdarrenr 2651145522Sdarrenr/* ------------------------------------------------------------------------ */ 2652255332Scy/* Function: ipf_checkicmpmatchingstate */ 2653145522Sdarrenr/* Returns: Nil */ 2654145522Sdarrenr/* Parameters: fin(I) - pointer to packet information */ 2655145522Sdarrenr/* */ 2656145522Sdarrenr/* If we've got an ICMP error message, using the information stored in the */ 2657145522Sdarrenr/* ICMP packet, look for a matching state table entry. */ 2658145522Sdarrenr/* */ 2659145522Sdarrenr/* If we return NULL then no lock on ipf_state is held. */ 2660145522Sdarrenr/* If we return non-null then a read-lock on ipf_state is held. */ 2661145522Sdarrenr/* ------------------------------------------------------------------------ */ 2662255332Scystatic ipstate_t * 2663255332Scyipf_checkicmpmatchingstate(fin) 2664255332Scy fr_info_t *fin; 266553642Sguido{ 2666255332Scy ipf_main_softc_t *softc = fin->fin_main_soft; 2667255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 2668145522Sdarrenr ipstate_t *is, **isp; 2669145522Sdarrenr i6addr_t dst, src; 267053642Sguido struct icmp *ic; 2671145522Sdarrenr u_short savelen; 267264580Sdarrenr icmphdr_t *icmp; 267353642Sguido fr_info_t ofin; 2674145522Sdarrenr tcphdr_t *tcp; 267564580Sdarrenr int type, len; 2676255332Scy u_char pr; 267753642Sguido ip_t *oip; 267855929Sguido u_int hv; 267953642Sguido 268057096Sguido /* 268157096Sguido * Does it at least have the return (basic) IP header ? 2682153876Sguido * Is it an actual recognised ICMP error type? 268353642Sguido * Only a basic IP header (no options) should be with 268453642Sguido * an ICMP error header. 268553642Sguido */ 2686145522Sdarrenr if ((fin->fin_v != 4) || (fin->fin_hlen != sizeof(ip_t)) || 2687153876Sguido (fin->fin_plen < ICMPERR_MINPKTLEN) || 2688255332Scy !(fin->fin_flx & FI_ICMPERR)) { 2689255332Scy SBUMPD(ipf_state_stats, iss_icmp_bad); 269053642Sguido return NULL; 2691255332Scy } 2692145522Sdarrenr ic = fin->fin_dp; 269353642Sguido type = ic->icmp_type; 269453642Sguido 269560854Sdarrenr oip = (ip_t *)((char *)ic + ICMPERR_ICMPHLEN); 2696130886Sdarrenr /* 2697130886Sdarrenr * Check if the at least the old IP header (with options) and 2698130886Sdarrenr * 8 bytes of payload is present. 2699130886Sdarrenr */ 2700255332Scy if (fin->fin_plen < ICMPERR_MAXPKTLEN + ((IP_HL(oip) - 5) << 2)) { 2701255332Scy SBUMPDX(ipf_state_stats, iss_icmp_short, iss_icmp_short_1); 270253642Sguido return NULL; 2703255332Scy } 270455929Sguido 270564580Sdarrenr /* 2706145522Sdarrenr * Sanity Checks. 270764580Sdarrenr */ 270864580Sdarrenr len = fin->fin_dlen - ICMPERR_ICMPHLEN; 2709255332Scy if ((len <= 0) || ((IP_HL(oip) << 2) > len)) { 2710255332Scy DT2(iss_icmp_len, fr_info_t *, fin, struct ip*, oip); 2711255332Scy SBUMPDX(ipf_state_stats, iss_icmp_short, iss_icmp_short_1); 271264580Sdarrenr return NULL; 2713255332Scy } 271464580Sdarrenr 271564580Sdarrenr /* 271664580Sdarrenr * Is the buffer big enough for all of it ? It's the size of the IP 271764580Sdarrenr * header claimed in the encapsulated part which is of concern. It 271864580Sdarrenr * may be too big to be in this buffer but not so big that it's 271964580Sdarrenr * outside the ICMP packet, leading to TCP deref's causing problems. 272064580Sdarrenr * This is possible because we don't know how big oip_hl is when we 2721255332Scy * do the pullup early in ipf_check() and thus can't guarantee it is 272264580Sdarrenr * all here now. 272364580Sdarrenr */ 272464580Sdarrenr#ifdef _KERNEL 272564580Sdarrenr { 272664580Sdarrenr mb_t *m; 272764580Sdarrenr 2728145522Sdarrenr m = fin->fin_m; 2729145522Sdarrenr# if defined(MENTAT) 2730255332Scy if ((char *)oip + len > (char *)m->b_wptr) { 2731255332Scy SBUMPDX(ipf_state_stats, iss_icmp_short, iss_icmp_short_2); 273264580Sdarrenr return NULL; 2733255332Scy } 273464580Sdarrenr# else 2735255332Scy if ((char *)oip + len > (char *)fin->fin_ip + m->m_len) { 2736255332Scy SBUMPDX(ipf_state_stats, iss_icmp_short, iss_icmp_short_3); 273764580Sdarrenr return NULL; 2738255332Scy } 273964580Sdarrenr# endif 274064580Sdarrenr } 274164580Sdarrenr#endif 2742255332Scy 2743170268Sdarrenr bcopy((char *)fin, (char *)&ofin, sizeof(*fin)); 274464580Sdarrenr 274564580Sdarrenr /* 274664580Sdarrenr * in the IPv4 case we must zero the i6addr union otherwise 2747145522Sdarrenr * the IP6_EQ and IP6_NEQ macros produce the wrong results because 274864580Sdarrenr * of the 'junk' in the unused part of the union 274964580Sdarrenr */ 275067614Sdarrenr bzero((char *)&src, sizeof(src)); 275167614Sdarrenr bzero((char *)&dst, sizeof(dst)); 2752145522Sdarrenr 2753130886Sdarrenr /* 2754145522Sdarrenr * we make an fin entry to be able to feed it to 2755145522Sdarrenr * matchsrcdst note that not all fields are encessary 2756145522Sdarrenr * but this is the cleanest way. Note further we fill 2757145522Sdarrenr * in fin_mp such that if someone uses it we'll get 2758255332Scy * a kernel panic. ipf_matchsrcdst does not use this. 2759145522Sdarrenr * 2760130886Sdarrenr * watch out here, as ip is in host order and oip in network 2761130886Sdarrenr * order. Any change we make must be undone afterwards, like 2762255332Scy * oip->ip_len. 2763130886Sdarrenr */ 2764130886Sdarrenr savelen = oip->ip_len; 2765255332Scy oip->ip_len = htons(len); 2766145522Sdarrenr 2767145522Sdarrenr ofin.fin_flx = FI_NOCKSUM; 2768145522Sdarrenr ofin.fin_v = 4; 2769145522Sdarrenr ofin.fin_ip = oip; 2770145522Sdarrenr ofin.fin_m = NULL; /* if dereferenced, panic XXX */ 2771145522Sdarrenr ofin.fin_mp = NULL; /* if dereferenced, panic XXX */ 2772255332Scy (void) ipf_makefrip(IP_HL(oip) << 2, oip, &ofin); 2773145522Sdarrenr ofin.fin_ifp = fin->fin_ifp; 2774145522Sdarrenr ofin.fin_out = !fin->fin_out; 2775255332Scy 2776255332Scy hv = (pr = oip->ip_p); 2777255332Scy src.in4 = oip->ip_src; 2778255332Scy hv += src.in4.s_addr; 2779255332Scy dst.in4 = oip->ip_dst; 2780255332Scy hv += dst.in4.s_addr; 2781255332Scy 2782130886Sdarrenr /* 2783255332Scy * Reset the short and bad flag here because in ipf_matchsrcdst() 2784145522Sdarrenr * the flags for the current packet (fin_flx) are compared against 2785145522Sdarrenr * those for the existing session. 2786130886Sdarrenr */ 2787145522Sdarrenr ofin.fin_flx &= ~(FI_BAD|FI_SHORT); 2788130886Sdarrenr 2789130886Sdarrenr /* 2790255332Scy * Put old values of ip_len back as we don't know 2791255332Scy * if we have to forward the packet or process it again. 2792130886Sdarrenr */ 2793130886Sdarrenr oip->ip_len = savelen; 2794130886Sdarrenr 279592685Sdarrenr switch (oip->ip_p) 279692685Sdarrenr { 279792685Sdarrenr case IPPROTO_ICMP : 279855929Sguido /* 2799130886Sdarrenr * an ICMP error can only be generated as a result of an 280055929Sguido * ICMP query, not as the response on an ICMP error 280155929Sguido * 280255929Sguido * XXX theoretically ICMP_ECHOREP and the other reply's are 280355929Sguido * ICMP query's as well, but adding them here seems strange XXX 280455929Sguido */ 2805255332Scy if ((ofin.fin_flx & FI_ICMPERR) != 0) { 2806255332Scy DT1(iss_icmp_icmperr, fr_info_t *, &ofin); 2807255332Scy SBUMP(ipf_state_stats.iss_icmp_icmperr); 280855929Sguido return NULL; 2809255332Scy } 281055929Sguido 281157096Sguido /* 281255929Sguido * perform a lookup of the ICMP packet in the state table 281355929Sguido */ 2814153876Sguido icmp = (icmphdr_t *)((char *)oip + (IP_HL(oip) << 2)); 281560854Sdarrenr hv += icmp->icmp_id; 2816145522Sdarrenr hv = DOUBLE_HASH(hv); 281755929Sguido 2818255332Scy READ_ENTER(&softc->ipf_state); 2819255332Scy for (isp = &softs->ipf_state_table[hv]; 2820255332Scy ((is = *isp) != NULL); ) { 2821145522Sdarrenr isp = &is->is_hnext; 2822145522Sdarrenr if ((is->is_p != pr) || (is->is_v != 4)) 2823145522Sdarrenr continue; 2824145522Sdarrenr if (is->is_pass & FR_NOICMPERR) 2825145522Sdarrenr continue; 2826255332Scy 2827255332Scy is = ipf_matchsrcdst(&ofin, is, &src, &dst, 2828145522Sdarrenr NULL, FI_ICMPCMP); 2829255332Scy if ((is != NULL) && !ipf_allowstateicmp(fin, is, &src)) 2830145522Sdarrenr return is; 2831145522Sdarrenr } 2832255332Scy RWLOCK_EXIT(&softc->ipf_state); 2833255332Scy SBUMPDX(ipf_state_stats, iss_icmp_miss, iss_icmp_miss_1); 2834145522Sdarrenr return NULL; 283592685Sdarrenr case IPPROTO_TCP : 283692685Sdarrenr case IPPROTO_UDP : 283792685Sdarrenr break; 283892685Sdarrenr default : 2839255332Scy SBUMPDX(ipf_state_stats, iss_icmp_miss, iss_icmp_miss_2); 284055929Sguido return NULL; 284192685Sdarrenr } 284255929Sguido 2843145522Sdarrenr tcp = (tcphdr_t *)((char *)oip + (IP_HL(oip) << 2)); 284453642Sguido 2845255332Scy hv += tcp->th_dport;; 2846255332Scy hv += tcp->th_sport;; 2847145522Sdarrenr hv = DOUBLE_HASH(hv); 2848130886Sdarrenr 2849255332Scy READ_ENTER(&softc->ipf_state); 2850255332Scy for (isp = &softs->ipf_state_table[hv]; ((is = *isp) != NULL); ) { 2851145522Sdarrenr isp = &is->is_hnext; 285253642Sguido /* 285353642Sguido * Only allow this icmp though if the 285453642Sguido * encapsulated packet was allowed through the 285553642Sguido * other way around. Note that the minimal amount 285653642Sguido * of info present does not allow for checking against 2857145522Sdarrenr * tcp internals such as seq and ack numbers. Only the 2858130886Sdarrenr * ports are known to be present and can be even if the 2859130886Sdarrenr * short flag is set. 286053642Sguido */ 286160854Sdarrenr if ((is->is_p == pr) && (is->is_v == 4) && 2862255332Scy (is = ipf_matchsrcdst(&ofin, is, &src, &dst, 2863255332Scy tcp, FI_ICMPCMP))) { 2864255332Scy if (ipf_allowstateicmp(fin, is, &src) == 0) 2865255332Scy return is; 286653642Sguido } 286753642Sguido } 2868255332Scy RWLOCK_EXIT(&softc->ipf_state); 2869255332Scy SBUMPDX(ipf_state_stats, iss_icmp_miss, iss_icmp_miss_3); 2870145522Sdarrenr return NULL; 287153642Sguido} 287253642Sguido 287367614Sdarrenr 2874145522Sdarrenr/* ------------------------------------------------------------------------ */ 2875255332Scy/* Function: ipf_allowstateicmp */ 2876255332Scy/* Returns: int - 1 = packet denied, 0 = packet allowed */ 2877255332Scy/* Parameters: fin(I) - pointer to packet information */ 2878255332Scy/* is(I) - pointer to state table entry */ 2879255332Scy/* src(I) - source address to check permission for */ 2880255332Scy/* */ 2881255332Scy/* For an ICMP packet that has so far matched a state table entry, check if */ 2882255332Scy/* there are any further refinements that might mean we want to block this */ 2883255332Scy/* packet. This code isn't specific to either IPv4 or IPv6. */ 2884255332Scy/* ------------------------------------------------------------------------ */ 2885255332Scystatic int 2886255332Scyipf_allowstateicmp(fin, is, src) 2887255332Scy fr_info_t *fin; 2888255332Scy ipstate_t *is; 2889255332Scy i6addr_t *src; 2890255332Scy{ 2891255332Scy ipf_main_softc_t *softc = fin->fin_main_soft; 2892255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 2893255332Scy frentry_t *savefr; 2894255332Scy frentry_t *fr; 2895255332Scy u_32_t ipass; 2896255332Scy int backward; 2897255332Scy int oi; 2898255332Scy int i; 2899255332Scy 2900255332Scy fr = is->is_rule; 2901255332Scy if (fr != NULL && fr->fr_icmpgrp != NULL) { 2902255332Scy savefr = fin->fin_fr; 2903255332Scy fin->fin_fr = fr->fr_icmpgrp->fg_start; 2904255332Scy 2905255332Scy ipass = ipf_scanlist(fin, softc->ipf_pass); 2906255332Scy fin->fin_fr = savefr; 2907255332Scy if (FR_ISBLOCK(ipass)) { 2908255332Scy SBUMPD(ipf_state_stats, iss_icmp_headblock); 2909255332Scy return 1; 2910255332Scy } 2911255332Scy } 2912255332Scy 2913255332Scy /* 2914255332Scy * i : the index of this packet (the icmp unreachable) 2915255332Scy * oi : the index of the original packet found in the 2916255332Scy * icmp header (i.e. the packet causing this icmp) 2917255332Scy * backward : original packet was backward compared to 2918255332Scy * the state 2919255332Scy */ 2920255332Scy backward = IP6_NEQ(&is->is_src, src); 2921255332Scy fin->fin_rev = !backward; 2922255332Scy i = (!backward << 1) + fin->fin_out; 2923255332Scy oi = (backward << 1) + !fin->fin_out; 2924255332Scy 2925255332Scy if (is->is_pass & FR_NOICMPERR) { 2926255332Scy SBUMPD(ipf_state_stats, iss_icmp_banned); 2927255332Scy return 1; 2928255332Scy } 2929255332Scy if (is->is_icmppkts[i] > is->is_pkts[oi]) { 2930255332Scy SBUMPD(ipf_state_stats, iss_icmp_toomany); 2931255332Scy return 1; 2932255332Scy } 2933255332Scy 2934255332Scy DT2(iss_icmp_hits, fr_info_t *, fin, ipstate_t *, is); 2935255332Scy SBUMP(ipf_state_stats.iss_icmp_hits); 2936255332Scy is->is_icmppkts[i]++; 2937255332Scy 2938255332Scy /* 2939255332Scy * we deliberately do not touch the timeouts 2940255332Scy * for the accompanying state table entry. 2941255332Scy * It remains to be seen if that is correct. XXX 2942255332Scy */ 2943255332Scy return 0; 2944255332Scy} 2945255332Scy 2946255332Scy 2947255332Scy/* ------------------------------------------------------------------------ */ 2948255332Scy/* Function: ipf_ipsmove */ 2949145522Sdarrenr/* Returns: Nil */ 2950145522Sdarrenr/* Parameters: is(I) - pointer to state table entry */ 2951145522Sdarrenr/* hv(I) - new hash value for state table entry */ 2952145522Sdarrenr/* Write Locks: ipf_state */ 2953145522Sdarrenr/* */ 2954145522Sdarrenr/* Move a state entry from one position in the hash table to another. */ 2955145522Sdarrenr/* ------------------------------------------------------------------------ */ 2956255332Scystatic void 2957255332Scyipf_ipsmove(softs, is, hv) 2958255332Scy ipf_state_softc_t *softs; 2959255332Scy ipstate_t *is; 2960255332Scy u_int hv; 296167614Sdarrenr{ 2962145522Sdarrenr ipstate_t **isp; 296367614Sdarrenr u_int hvm; 296467614Sdarrenr 296567614Sdarrenr hvm = is->is_hv; 2966255332Scy 2967255332Scy /* TRACE is, is_hv, hvm */ 2968255332Scy 296967614Sdarrenr /* 297067614Sdarrenr * Remove the hash from the old location... 297167614Sdarrenr */ 2972145522Sdarrenr isp = is->is_phnext; 297367614Sdarrenr if (is->is_hnext) 297467614Sdarrenr is->is_hnext->is_phnext = isp; 297567614Sdarrenr *isp = is->is_hnext; 2976255332Scy if (softs->ipf_state_table[hvm] == NULL) 2977255332Scy softs->ipf_state_stats.iss_inuse--; 2978255332Scy softs->ipf_state_stats.iss_bucketlen[hvm]--; 297967614Sdarrenr 298067614Sdarrenr /* 298167614Sdarrenr * ...and put the hash in the new one. 298267614Sdarrenr */ 2983145522Sdarrenr hvm = DOUBLE_HASH(hv); 298467853Sdarrenr is->is_hv = hvm; 2985255332Scy 2986255332Scy /* TRACE is, hv, is_hv, hvm */ 2987255332Scy 2988255332Scy isp = &softs->ipf_state_table[hvm]; 298967614Sdarrenr if (*isp) 299067614Sdarrenr (*isp)->is_phnext = &is->is_hnext; 299167614Sdarrenr else 2992255332Scy softs->ipf_state_stats.iss_inuse++; 2993255332Scy softs->ipf_state_stats.iss_bucketlen[hvm]++; 299467614Sdarrenr is->is_phnext = isp; 299567614Sdarrenr is->is_hnext = *isp; 299667614Sdarrenr *isp = is; 299767614Sdarrenr} 299867614Sdarrenr 299967614Sdarrenr 3000145522Sdarrenr/* ------------------------------------------------------------------------ */ 3001255332Scy/* Function: ipf_state_lookup */ 3002145522Sdarrenr/* Returns: ipstate_t* - NULL == no matching state found, */ 3003145522Sdarrenr/* else pointer to state information is returned */ 3004255332Scy/* Parameters: fin(I) - pointer to packet information */ 3005255332Scy/* tcp(I) - pointer to TCP/UDP header. */ 3006255332Scy/* ifqp(O) - pointer for storing tailq timeout */ 3007145522Sdarrenr/* */ 3008145522Sdarrenr/* Search the state table for a matching entry to the packet described by */ 3009255332Scy/* the contents of *fin. For certain protocols, when a match is found the */ 3010255332Scy/* timeout queue is also selected and stored in ifpq if it is non-NULL. */ 3011145522Sdarrenr/* */ 3012145522Sdarrenr/* If we return NULL then no lock on ipf_state is held. */ 3013145522Sdarrenr/* If we return non-null then a read-lock on ipf_state is held. */ 3014145522Sdarrenr/* ------------------------------------------------------------------------ */ 3015255332Scyipstate_t * 3016255332Scyipf_state_lookup(fin, tcp, ifqp) 3017255332Scy fr_info_t *fin; 3018255332Scy tcphdr_t *tcp; 3019255332Scy ipftq_t **ifqp; 302053642Sguido{ 3021255332Scy ipf_main_softc_t *softc = fin->fin_main_soft; 3022255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 3023145522Sdarrenr u_int hv, hvm, pr, v, tryagain; 3024145522Sdarrenr ipstate_t *is, **isp; 3025145522Sdarrenr u_short dport, sport; 3026145522Sdarrenr i6addr_t src, dst; 302753642Sguido struct icmp *ic; 3028145522Sdarrenr ipftq_t *ifq; 3029145522Sdarrenr int oow; 303053642Sguido 303153642Sguido is = NULL; 3032145522Sdarrenr ifq = NULL; 3033145522Sdarrenr tcp = fin->fin_dp; 303453642Sguido ic = (struct icmp *)tcp; 303560854Sdarrenr hv = (pr = fin->fin_fi.fi_p); 303660854Sdarrenr src = fin->fin_fi.fi_src; 303760854Sdarrenr dst = fin->fin_fi.fi_dst; 303860854Sdarrenr hv += src.in4.s_addr; 303960854Sdarrenr hv += dst.in4.s_addr; 304053642Sguido 304160854Sdarrenr v = fin->fin_fi.fi_v; 304292685Sdarrenr#ifdef USE_INET6 304392685Sdarrenr if (v == 6) { 3044145522Sdarrenr hv += fin->fin_fi.fi_src.i6[1]; 3045145522Sdarrenr hv += fin->fin_fi.fi_src.i6[2]; 3046145522Sdarrenr hv += fin->fin_fi.fi_src.i6[3]; 304792685Sdarrenr 304892685Sdarrenr if ((fin->fin_p == IPPROTO_ICMPV6) && 304992685Sdarrenr IN6_IS_ADDR_MULTICAST(&fin->fin_fi.fi_dst.in6)) { 305092685Sdarrenr hv -= dst.in4.s_addr; 305192685Sdarrenr } else { 305292685Sdarrenr hv += fin->fin_fi.fi_dst.i6[1]; 305392685Sdarrenr hv += fin->fin_fi.fi_dst.i6[2]; 305492685Sdarrenr hv += fin->fin_fi.fi_dst.i6[3]; 305592685Sdarrenr } 305692685Sdarrenr } 305792685Sdarrenr#endif 3058172776Sdarrenr if ((v == 4) && 3059172776Sdarrenr (fin->fin_flx & (FI_MULTICAST|FI_BROADCAST|FI_MBCAST))) { 3060172776Sdarrenr if (fin->fin_out == 0) { 3061172776Sdarrenr hv -= src.in4.s_addr; 3062172776Sdarrenr } else { 3063172776Sdarrenr hv -= dst.in4.s_addr; 3064172776Sdarrenr } 3065172776Sdarrenr } 306692685Sdarrenr 3067255332Scy /* TRACE fin_saddr, fin_daddr, hv */ 3068255332Scy 3069145522Sdarrenr /* 3070145522Sdarrenr * Search the hash table for matching packet header info. 3071145522Sdarrenr */ 3072145522Sdarrenr switch (pr) 307353642Sguido { 307460854Sdarrenr#ifdef USE_INET6 307560854Sdarrenr case IPPROTO_ICMPV6 : 307692685Sdarrenr tryagain = 0; 307760854Sdarrenr if (v == 6) { 307860854Sdarrenr if ((ic->icmp_type == ICMP6_ECHO_REQUEST) || 307960854Sdarrenr (ic->icmp_type == ICMP6_ECHO_REPLY)) { 308060854Sdarrenr hv += ic->icmp_id; 308160854Sdarrenr } 308260854Sdarrenr } 3083255332Scy READ_ENTER(&softc->ipf_state); 308492685Sdarrenricmp6again: 3085145522Sdarrenr hvm = DOUBLE_HASH(hv); 3086255332Scy for (isp = &softs->ipf_state_table[hvm]; 3087255332Scy ((is = *isp) != NULL); ) { 3088145522Sdarrenr isp = &is->is_hnext; 3089145522Sdarrenr if ((is->is_p != pr) || (is->is_v != v)) 3090145522Sdarrenr continue; 3091255332Scy is = ipf_matchsrcdst(fin, is, &src, &dst, NULL, FI_CMP); 3092145522Sdarrenr if (is != NULL && 3093255332Scy ipf_matchicmpqueryreply(v, &is->is_icmp, 3094145522Sdarrenr ic, fin->fin_rev)) { 3095145522Sdarrenr if (fin->fin_rev) 3096255332Scy ifq = &softs->ipf_state_icmpacktq; 309792685Sdarrenr else 3098255332Scy ifq = &softs->ipf_state_icmptq; 309992685Sdarrenr break; 310092685Sdarrenr } 3101145522Sdarrenr } 310292685Sdarrenr 310392685Sdarrenr if (is != NULL) { 3104145522Sdarrenr if ((tryagain != 0) && !(is->is_flags & SI_W_DADDR)) { 310592685Sdarrenr hv += fin->fin_fi.fi_src.i6[0]; 310692685Sdarrenr hv += fin->fin_fi.fi_src.i6[1]; 310792685Sdarrenr hv += fin->fin_fi.fi_src.i6[2]; 310892685Sdarrenr hv += fin->fin_fi.fi_src.i6[3]; 3109255332Scy ipf_ipsmove(softs, is, hv); 3110255332Scy MUTEX_DOWNGRADE(&softc->ipf_state); 311192685Sdarrenr } 311292685Sdarrenr break; 311392685Sdarrenr } 3114255332Scy RWLOCK_EXIT(&softc->ipf_state); 311592685Sdarrenr 311692685Sdarrenr /* 311792685Sdarrenr * No matching icmp state entry. Perhaps this is a 311892685Sdarrenr * response to another state entry. 3119145522Sdarrenr * 3120145522Sdarrenr * XXX With some ICMP6 packets, the "other" address is already 3121145522Sdarrenr * in the packet, after the ICMP6 header, and this could be 3122145522Sdarrenr * used in place of the multicast address. However, taking 3123145522Sdarrenr * advantage of this requires some significant code changes 3124145522Sdarrenr * to handle the specific types where that is the case. 312592685Sdarrenr */ 3126255332Scy if ((softs->ipf_state_stats.iss_wild != 0) && 3127255332Scy ((fin->fin_flx & FI_NOWILD) == 0) && 3128255332Scy (v == 6) && (tryagain == 0)) { 312992685Sdarrenr hv -= fin->fin_fi.fi_src.i6[0]; 313092685Sdarrenr hv -= fin->fin_fi.fi_src.i6[1]; 313192685Sdarrenr hv -= fin->fin_fi.fi_src.i6[2]; 313292685Sdarrenr hv -= fin->fin_fi.fi_src.i6[3]; 313392685Sdarrenr tryagain = 1; 3134255332Scy WRITE_ENTER(&softc->ipf_state); 313592685Sdarrenr goto icmp6again; 313692685Sdarrenr } 313792685Sdarrenr 3138255332Scy is = ipf_checkicmp6matchingstate(fin); 3139145522Sdarrenr if (is != NULL) 3140145522Sdarrenr return is; 314192685Sdarrenr break; 314260854Sdarrenr#endif 3143145522Sdarrenr 314453642Sguido case IPPROTO_ICMP : 314560854Sdarrenr if (v == 4) { 314655929Sguido hv += ic->icmp_id; 314755929Sguido } 3148145522Sdarrenr hv = DOUBLE_HASH(hv); 3149255332Scy READ_ENTER(&softc->ipf_state); 3150255332Scy for (isp = &softs->ipf_state_table[hv]; 3151255332Scy ((is = *isp) != NULL); ) { 3152145522Sdarrenr isp = &is->is_hnext; 3153145522Sdarrenr if ((is->is_p != pr) || (is->is_v != v)) 3154145522Sdarrenr continue; 3155255332Scy is = ipf_matchsrcdst(fin, is, &src, &dst, NULL, FI_CMP); 3156161356Sguido if ((is != NULL) && 3157161356Sguido (ic->icmp_id == is->is_icmp.ici_id) && 3158255332Scy ipf_matchicmpqueryreply(v, &is->is_icmp, 3159145522Sdarrenr ic, fin->fin_rev)) { 3160145522Sdarrenr if (fin->fin_rev) 3161255332Scy ifq = &softs->ipf_state_icmpacktq; 316280482Sdarrenr else 3163255332Scy ifq = &softs->ipf_state_icmptq; 316453642Sguido break; 316553642Sguido } 3166145522Sdarrenr } 3167145522Sdarrenr if (is == NULL) { 3168255332Scy RWLOCK_EXIT(&softc->ipf_state); 3169145522Sdarrenr } 3170145522Sdarrenr break; 317192685Sdarrenr 317253642Sguido case IPPROTO_TCP : 317367614Sdarrenr case IPPROTO_UDP : 3174145522Sdarrenr ifqp = NULL; 3175145522Sdarrenr sport = htons(fin->fin_data[0]); 3176145522Sdarrenr hv += sport; 3177145522Sdarrenr dport = htons(fin->fin_data[1]); 3178145522Sdarrenr hv += dport; 3179145522Sdarrenr oow = 0; 318053642Sguido tryagain = 0; 3181255332Scy READ_ENTER(&softc->ipf_state); 318267614Sdarrenrretry_tcpudp: 3183145522Sdarrenr hvm = DOUBLE_HASH(hv); 3184255332Scy 3185255332Scy /* TRACE hv, hvm */ 3186255332Scy 3187255332Scy for (isp = &softs->ipf_state_table[hvm]; 3188255332Scy ((is = *isp) != NULL); ) { 3189145522Sdarrenr isp = &is->is_hnext; 3190145522Sdarrenr if ((is->is_p != pr) || (is->is_v != v)) 3191145522Sdarrenr continue; 3192145522Sdarrenr fin->fin_flx &= ~FI_OOW; 3193255332Scy is = ipf_matchsrcdst(fin, is, &src, &dst, tcp, FI_CMP); 3194145522Sdarrenr if (is != NULL) { 3195145522Sdarrenr if (pr == IPPROTO_TCP) { 3196255332Scy if (!ipf_state_tcp(softc, softs, fin, 3197255332Scy tcp, is)) { 3198145522Sdarrenr oow |= fin->fin_flx & FI_OOW; 3199145522Sdarrenr continue; 3200145522Sdarrenr } 320167614Sdarrenr } 320253642Sguido break; 320353642Sguido } 3204145522Sdarrenr } 320567614Sdarrenr if (is != NULL) { 320667614Sdarrenr if (tryagain && 3207145522Sdarrenr !(is->is_flags & (SI_CLONE|SI_WILDP|SI_WILDA))) { 320867614Sdarrenr hv += dport; 320967614Sdarrenr hv += sport; 3210255332Scy ipf_ipsmove(softs, is, hv); 3211255332Scy MUTEX_DOWNGRADE(&softc->ipf_state); 321267614Sdarrenr } 321353642Sguido break; 321467614Sdarrenr } 3215255332Scy RWLOCK_EXIT(&softc->ipf_state); 321695418Sdarrenr 3217255332Scy if ((softs->ipf_state_stats.iss_wild != 0) && 3218255332Scy ((fin->fin_flx & FI_NOWILD) == 0)) { 3219172776Sdarrenr if (tryagain == 0) { 3220172776Sdarrenr hv -= dport; 3221172776Sdarrenr hv -= sport; 3222172776Sdarrenr } else if (tryagain == 1) { 3223172776Sdarrenr hv = fin->fin_fi.fi_p; 3224172776Sdarrenr /* 3225172776Sdarrenr * If we try to pretend this is a reply to a 3226172776Sdarrenr * multicast/broadcast packet then we need to 3227172776Sdarrenr * exclude part of the address from the hash 3228172776Sdarrenr * calculation. 3229172776Sdarrenr */ 3230172776Sdarrenr if (fin->fin_out == 0) { 3231172776Sdarrenr hv += src.in4.s_addr; 3232172776Sdarrenr } else { 3233172776Sdarrenr hv += dst.in4.s_addr; 3234172776Sdarrenr } 3235172776Sdarrenr hv += dport; 3236172776Sdarrenr hv += sport; 3237172776Sdarrenr } 3238172776Sdarrenr tryagain++; 3239172776Sdarrenr if (tryagain <= 2) { 3240255332Scy WRITE_ENTER(&softc->ipf_state); 3241172776Sdarrenr goto retry_tcpudp; 3242172776Sdarrenr } 324353642Sguido } 3244145522Sdarrenr fin->fin_flx |= oow; 324553642Sguido break; 3246145522Sdarrenr 3247145522Sdarrenr#if 0 3248145522Sdarrenr case IPPROTO_GRE : 3249145522Sdarrenr gre = fin->fin_dp; 3250145522Sdarrenr if (GRE_REV(gre->gr_flags) == 1) { 3251145522Sdarrenr hv += gre->gr_call; 3252145522Sdarrenr } 3253145522Sdarrenr /* FALLTHROUGH */ 3254145522Sdarrenr#endif 325553642Sguido default : 3256145522Sdarrenr ifqp = NULL; 3257145522Sdarrenr hvm = DOUBLE_HASH(hv); 3258255332Scy READ_ENTER(&softc->ipf_state); 3259255332Scy for (isp = &softs->ipf_state_table[hvm]; 3260255332Scy ((is = *isp) != NULL); ) { 3261145522Sdarrenr isp = &is->is_hnext; 3262145522Sdarrenr if ((is->is_p != pr) || (is->is_v != v)) 3263145522Sdarrenr continue; 3264255332Scy is = ipf_matchsrcdst(fin, is, &src, &dst, NULL, FI_CMP); 3265145522Sdarrenr if (is != NULL) { 3266255332Scy ifq = &softs->ipf_state_iptq; 326792685Sdarrenr break; 326892685Sdarrenr } 326992685Sdarrenr } 327092685Sdarrenr if (is == NULL) { 3271255332Scy RWLOCK_EXIT(&softc->ipf_state); 327292685Sdarrenr } 327353642Sguido break; 327453642Sguido } 327592685Sdarrenr 3276161356Sguido if (is != NULL) { 3277161356Sguido if (((is->is_sti.tqe_flags & TQE_RULEBASED) != 0) && 3278161356Sguido (is->is_tqehead[fin->fin_rev] != NULL)) 3279161356Sguido ifq = is->is_tqehead[fin->fin_rev]; 3280161356Sguido if (ifq != NULL && ifqp != NULL) 3281161356Sguido *ifqp = ifq; 3282255332Scy } else { 3283255332Scy SBUMP(ipf_state_stats.iss_lookup_miss); 3284161356Sguido } 3285145522Sdarrenr return is; 3286145522Sdarrenr} 3287145522Sdarrenr 3288145522Sdarrenr 3289145522Sdarrenr/* ------------------------------------------------------------------------ */ 3290255332Scy/* Function: ipf_state_check */ 3291145522Sdarrenr/* Returns: frentry_t* - NULL == search failed, */ 3292145522Sdarrenr/* else pointer to rule for matching state */ 3293255332Scy/* Parameters: fin(I) - pointer to packet information */ 3294145522Sdarrenr/* passp(I) - pointer to filtering result flags */ 3295145522Sdarrenr/* */ 3296145522Sdarrenr/* Check if a packet is associated with an entry in the state table. */ 3297145522Sdarrenr/* ------------------------------------------------------------------------ */ 3298255332Scyfrentry_t * 3299255332Scyipf_state_check(fin, passp) 3300255332Scy fr_info_t *fin; 3301255332Scy u_32_t *passp; 3302145522Sdarrenr{ 3303255332Scy ipf_main_softc_t *softc = fin->fin_main_soft; 3304255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 3305255332Scy ipftqent_t *tqe; 3306145522Sdarrenr ipstate_t *is; 3307145522Sdarrenr frentry_t *fr; 3308145522Sdarrenr tcphdr_t *tcp; 3309145522Sdarrenr ipftq_t *ifq; 3310145522Sdarrenr u_int pass; 3311255332Scy int inout; 3312145522Sdarrenr 3313255332Scy if (softs->ipf_state_lock || (softs->ipf_state_list == NULL)) 3314145522Sdarrenr return NULL; 3315145522Sdarrenr 3316255332Scy if (fin->fin_flx & (FI_SHORT|FI_FRAGBODY|FI_BAD)) { 3317255332Scy SBUMPD(ipf_state_stats, iss_check_bad); 3318255332Scy return NULL; 3319255332Scy } 3320255332Scy 3321145522Sdarrenr if ((fin->fin_flx & FI_TCPUDP) || 3322145522Sdarrenr (fin->fin_fi.fi_p == IPPROTO_ICMP) 3323145522Sdarrenr#ifdef USE_INET6 3324145522Sdarrenr || (fin->fin_fi.fi_p == IPPROTO_ICMPV6) 3325145522Sdarrenr#endif 3326145522Sdarrenr ) 3327145522Sdarrenr tcp = fin->fin_dp; 3328145522Sdarrenr else 3329145522Sdarrenr tcp = NULL; 3330145522Sdarrenr 3331255332Scy ifq = NULL; 3332145522Sdarrenr /* 3333145522Sdarrenr * Search the hash table for matching packet header info. 3334145522Sdarrenr */ 3335255332Scy is = ipf_state_lookup(fin, tcp, &ifq); 3336255332Scy 3337145522Sdarrenr switch (fin->fin_p) 3338145522Sdarrenr { 3339145522Sdarrenr#ifdef USE_INET6 3340145522Sdarrenr case IPPROTO_ICMPV6 : 3341145522Sdarrenr if (is != NULL) 3342145522Sdarrenr break; 3343145522Sdarrenr if (fin->fin_v == 6) { 3344255332Scy is = ipf_checkicmp6matchingstate(fin); 3345145522Sdarrenr } 3346145522Sdarrenr break; 3347145522Sdarrenr#endif 3348145522Sdarrenr case IPPROTO_ICMP : 3349145522Sdarrenr if (is != NULL) 3350145522Sdarrenr break; 3351145522Sdarrenr /* 3352145522Sdarrenr * No matching icmp state entry. Perhaps this is a 3353145522Sdarrenr * response to another state entry. 3354145522Sdarrenr */ 3355255332Scy is = ipf_checkicmpmatchingstate(fin); 3356145522Sdarrenr break; 3357255332Scy 3358145522Sdarrenr case IPPROTO_TCP : 3359145522Sdarrenr if (is == NULL) 3360145522Sdarrenr break; 3361145522Sdarrenr 3362145522Sdarrenr if (is->is_pass & FR_NEWISN) { 3363145522Sdarrenr if (fin->fin_out == 0) 3364255332Scy ipf_fixinisn(fin, is); 3365145522Sdarrenr else if (fin->fin_out == 1) 3366255332Scy ipf_fixoutisn(fin, is); 3367145522Sdarrenr } 3368145522Sdarrenr break; 3369145522Sdarrenr default : 3370145522Sdarrenr if (fin->fin_rev) 3371255332Scy ifq = &softs->ipf_state_udpacktq; 3372145522Sdarrenr else 3373255332Scy ifq = &softs->ipf_state_udptq; 3374145522Sdarrenr break; 3375145522Sdarrenr } 337653642Sguido if (is == NULL) { 3377255332Scy SBUMP(ipf_state_stats.iss_check_miss); 337853642Sguido return NULL; 337953642Sguido } 338092685Sdarrenr 338153642Sguido fr = is->is_rule; 338292685Sdarrenr if (fr != NULL) { 3383145522Sdarrenr if ((fin->fin_out == 0) && (fr->fr_nattag.ipt_num[0] != 0)) { 3384255332Scy if (fin->fin_nattag == NULL) { 3385255332Scy RWLOCK_EXIT(&softc->ipf_state); 3386255332Scy SBUMPD(ipf_state_stats, iss_check_notag); 3387145522Sdarrenr return NULL; 3388255332Scy } 3389255332Scy if (ipf_matchtag(&fr->fr_nattag, fin->fin_nattag)!=0) { 3390255332Scy RWLOCK_EXIT(&softc->ipf_state); 3391255332Scy SBUMPD(ipf_state_stats, iss_check_nattag); 3392145522Sdarrenr return NULL; 3393255332Scy } 3394145522Sdarrenr } 3395255332Scy (void) strncpy(fin->fin_group, FR_NAME(fr, fr_group), 3396255332Scy FR_GROUPLEN); 339792685Sdarrenr fin->fin_icode = fr->fr_icode; 339892685Sdarrenr } 3399145522Sdarrenr 3400145522Sdarrenr fin->fin_rule = is->is_rulen; 3401255332Scy fin->fin_fr = fr; 3402145522Sdarrenr 3403255332Scy /* 3404255332Scy * If this packet is a fragment and the rule says to track fragments, 3405255332Scy * then create a new fragment cache entry. 3406255332Scy */ 3407255332Scy if ((fin->fin_flx & FI_FRAG) && FR_ISPASS(is->is_pass)) 3408255332Scy (void) ipf_frag_new(softc, fin, is->is_pass); 3409255332Scy 3410255332Scy /* 3411255332Scy * For TCP packets, ifq == NULL. For all others, check if this new 3412255332Scy * queue is different to the last one it was on and move it if so. 3413255332Scy */ 3414255332Scy tqe = &is->is_sti; 3415255332Scy if ((tqe->tqe_flags & TQE_RULEBASED) != 0) 3416255332Scy ifq = is->is_tqehead[fin->fin_rev]; 3417255332Scy 3418145522Sdarrenr MUTEX_ENTER(&is->is_lock); 3419255332Scy 3420255332Scy if (ifq != NULL) 3421255332Scy ipf_movequeue(softc->ipf_ticks, tqe, tqe->tqe_ifq, ifq); 3422255332Scy 3423255332Scy inout = (fin->fin_rev << 1) + fin->fin_out; 3424255332Scy is->is_pkts[inout]++; 3425255332Scy is->is_bytes[inout] += fin->fin_plen; 3426255332Scy fin->fin_pktnum = is->is_pkts[inout] + is->is_icmppkts[inout]; 3427255332Scy 3428145522Sdarrenr MUTEX_EXIT(&is->is_lock); 3429255332Scy 3430255332Scy pass = is->is_pass; 3431255332Scy 3432255332Scy if (is->is_flags & IS_STATESYNC) 3433255332Scy ipf_sync_update(softc, SMC_STATE, fin, is->is_sync); 3434255332Scy 3435255332Scy RWLOCK_EXIT(&softc->ipf_state); 3436255332Scy 3437255332Scy SBUMP(ipf_state_stats.iss_hits); 3438255332Scy 3439255332Scy fin->fin_dif = &is->is_dif; 3440255332Scy fin->fin_tif = &is->is_tifs[fin->fin_rev]; 3441145522Sdarrenr fin->fin_flx |= FI_STATE; 3442145522Sdarrenr if ((pass & FR_LOGFIRST) != 0) 3443145522Sdarrenr pass &= ~(FR_LOGFIRST|FR_LOG); 3444145522Sdarrenr *passp = pass; 344553642Sguido return fr; 344653642Sguido} 344753642Sguido 344853642Sguido 3449145522Sdarrenr/* ------------------------------------------------------------------------ */ 3450255332Scy/* Function: ipf_fixoutisn */ 3451145522Sdarrenr/* Returns: Nil */ 3452255332Scy/* Parameters: fin(I) - pointer to packet information */ 3453145522Sdarrenr/* is(I) - pointer to master state structure */ 3454145522Sdarrenr/* */ 3455145522Sdarrenr/* Called only for outbound packets, adjusts the sequence number and the */ 3456145522Sdarrenr/* TCP checksum to match that change. */ 3457145522Sdarrenr/* ------------------------------------------------------------------------ */ 3458255332Scystatic void 3459255332Scyipf_fixoutisn(fin, is) 3460255332Scy fr_info_t *fin; 3461255332Scy ipstate_t *is; 3462145522Sdarrenr{ 3463145522Sdarrenr tcphdr_t *tcp; 3464145522Sdarrenr int rev; 3465145522Sdarrenr u_32_t seq; 3466145522Sdarrenr 3467145522Sdarrenr tcp = fin->fin_dp; 3468145522Sdarrenr rev = fin->fin_rev; 3469145522Sdarrenr if ((is->is_flags & IS_ISNSYN) != 0) { 3470255332Scy if ((rev == 0) && (fin->fin_cksum < FI_CK_L4PART)) { 3471145522Sdarrenr seq = ntohl(tcp->th_seq); 3472145522Sdarrenr seq += is->is_isninc[0]; 3473145522Sdarrenr tcp->th_seq = htonl(seq); 3474255332Scy ipf_fix_outcksum(0, &tcp->th_sum, is->is_sumd[0], 0); 3475145522Sdarrenr } 3476145522Sdarrenr } 3477145522Sdarrenr if ((is->is_flags & IS_ISNACK) != 0) { 3478255332Scy if ((rev == 1) && (fin->fin_cksum < FI_CK_L4PART)) { 3479145522Sdarrenr seq = ntohl(tcp->th_seq); 3480145522Sdarrenr seq += is->is_isninc[1]; 3481145522Sdarrenr tcp->th_seq = htonl(seq); 3482255332Scy ipf_fix_outcksum(0, &tcp->th_sum, is->is_sumd[1], 0); 3483145522Sdarrenr } 3484145522Sdarrenr } 3485145522Sdarrenr} 3486145522Sdarrenr 3487145522Sdarrenr 3488145522Sdarrenr/* ------------------------------------------------------------------------ */ 3489255332Scy/* Function: ipf_fixinisn */ 3490145522Sdarrenr/* Returns: Nil */ 3491145522Sdarrenr/* Parameters: fin(I) - pointer to packet information */ 3492145522Sdarrenr/* is(I) - pointer to master state structure */ 3493145522Sdarrenr/* */ 3494145522Sdarrenr/* Called only for inbound packets, adjusts the acknowledge number and the */ 3495145522Sdarrenr/* TCP checksum to match that change. */ 3496145522Sdarrenr/* ------------------------------------------------------------------------ */ 3497255332Scystatic void 3498255332Scyipf_fixinisn(fin, is) 3499255332Scy fr_info_t *fin; 3500255332Scy ipstate_t *is; 3501145522Sdarrenr{ 3502145522Sdarrenr tcphdr_t *tcp; 3503145522Sdarrenr int rev; 3504145522Sdarrenr u_32_t ack; 3505145522Sdarrenr 3506145522Sdarrenr tcp = fin->fin_dp; 3507145522Sdarrenr rev = fin->fin_rev; 3508145522Sdarrenr if ((is->is_flags & IS_ISNSYN) != 0) { 3509255332Scy if ((rev == 1) && (fin->fin_cksum < FI_CK_L4PART)) { 3510145522Sdarrenr ack = ntohl(tcp->th_ack); 3511145522Sdarrenr ack -= is->is_isninc[0]; 3512145522Sdarrenr tcp->th_ack = htonl(ack); 3513255332Scy ipf_fix_incksum(0, &tcp->th_sum, is->is_sumd[0], 0); 3514145522Sdarrenr } 3515145522Sdarrenr } 3516145522Sdarrenr if ((is->is_flags & IS_ISNACK) != 0) { 3517255332Scy if ((rev == 0) && (fin->fin_cksum < FI_CK_L4PART)) { 3518145522Sdarrenr ack = ntohl(tcp->th_ack); 3519145522Sdarrenr ack -= is->is_isninc[1]; 3520145522Sdarrenr tcp->th_ack = htonl(ack); 3521255332Scy ipf_fix_incksum(0, &tcp->th_sum, is->is_sumd[1], 0); 3522145522Sdarrenr } 3523145522Sdarrenr } 3524145522Sdarrenr} 3525145522Sdarrenr 3526145522Sdarrenr 3527145522Sdarrenr/* ------------------------------------------------------------------------ */ 3528255332Scy/* Function: ipf_state_sync */ 3529145522Sdarrenr/* Returns: Nil */ 3530255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 3531255332Scy/* ifp(I) - pointer to interface */ 3532145522Sdarrenr/* */ 3533145522Sdarrenr/* Walk through all state entries and if an interface pointer match is */ 3534145522Sdarrenr/* found then look it up again, based on its name in case the pointer has */ 3535145522Sdarrenr/* changed since last time. */ 3536145522Sdarrenr/* */ 3537145522Sdarrenr/* If ifp is passed in as being non-null then we are only doing updates for */ 3538145522Sdarrenr/* existing, matching, uses of it. */ 3539145522Sdarrenr/* ------------------------------------------------------------------------ */ 3540255332Scyvoid 3541255332Scyipf_state_sync(softc, ifp) 3542255332Scy ipf_main_softc_t *softc; 3543255332Scy void *ifp; 354460854Sdarrenr{ 3545255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 3546145522Sdarrenr ipstate_t *is; 354792685Sdarrenr int i; 354860854Sdarrenr 3549255332Scy if (softc->ipf_running <= 0) 3550145522Sdarrenr return; 3551145522Sdarrenr 3552255332Scy WRITE_ENTER(&softc->ipf_state); 3553145522Sdarrenr 3554255332Scy if (softc->ipf_running <= 0) { 3555255332Scy RWLOCK_EXIT(&softc->ipf_state); 3556145522Sdarrenr return; 3557145522Sdarrenr } 3558145522Sdarrenr 3559255332Scy for (is = softs->ipf_state_list; is; is = is->is_next) { 3560145522Sdarrenr /* 3561145522Sdarrenr * Look up all the interface names in the state entry. 3562145522Sdarrenr */ 356392685Sdarrenr for (i = 0; i < 4; i++) { 3564145522Sdarrenr if (ifp == NULL || ifp == is->is_ifp[i]) 3565255332Scy is->is_ifp[i] = ipf_resolvenic(softc, 3566255332Scy is->is_ifname[i], 3567145522Sdarrenr is->is_v); 356860854Sdarrenr } 356960854Sdarrenr } 3570255332Scy RWLOCK_EXIT(&softc->ipf_state); 357160854Sdarrenr} 357260854Sdarrenr 357360854Sdarrenr 3574145522Sdarrenr/* ------------------------------------------------------------------------ */ 3575255332Scy/* Function: ipf_state_del */ 3576255332Scy/* Returns: int - 0 = deleted, else refernce count on active struct */ 3577255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 3578255332Scy/* is(I) - pointer to state structure to delete */ 3579145522Sdarrenr/* why(I) - if not 0, log reason why it was deleted */ 3580145522Sdarrenr/* Write Locks: ipf_state */ 3581145522Sdarrenr/* */ 3582145522Sdarrenr/* Deletes a state entry from the enumerated list as well as the hash table */ 3583145522Sdarrenr/* and timeout queue lists. Make adjustments to hash table statistics and */ 3584145522Sdarrenr/* global counters as required. */ 3585145522Sdarrenr/* ------------------------------------------------------------------------ */ 3586255332Scystatic int 3587255332Scyipf_state_del(softc, is, why) 3588255332Scy ipf_main_softc_t *softc; 3589255332Scy ipstate_t *is; 3590255332Scy int why; 359153642Sguido{ 3592255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 3593255332Scy int orphan = 1; 3594255332Scy frentry_t *fr; 359553642Sguido 3596145522Sdarrenr /* 3597145522Sdarrenr * Since we want to delete this, remove it from the state table, 3598145522Sdarrenr * where it can be found & used, first. 3599145522Sdarrenr */ 3600145522Sdarrenr if (is->is_phnext != NULL) { 3601145522Sdarrenr *is->is_phnext = is->is_hnext; 3602145522Sdarrenr if (is->is_hnext != NULL) 3603145522Sdarrenr is->is_hnext->is_phnext = is->is_phnext; 3604255332Scy if (softs->ipf_state_table[is->is_hv] == NULL) 3605255332Scy softs->ipf_state_stats.iss_inuse--; 3606255332Scy softs->ipf_state_stats.iss_bucketlen[is->is_hv]--; 3607145522Sdarrenr 3608145522Sdarrenr is->is_phnext = NULL; 3609145522Sdarrenr is->is_hnext = NULL; 3610255332Scy orphan = 0; 3611145522Sdarrenr } 3612145522Sdarrenr 3613145522Sdarrenr /* 3614255332Scy * Because ipf_state_stats.iss_wild is a count of entries in the state 3615145522Sdarrenr * table that have wildcard flags set, only decerement it once 3616145522Sdarrenr * and do it here. 3617145522Sdarrenr */ 3618145522Sdarrenr if (is->is_flags & (SI_WILDP|SI_WILDA)) { 3619145522Sdarrenr if (!(is->is_flags & SI_CLONED)) { 3620255332Scy ATOMIC_DECL(softs->ipf_state_stats.iss_wild); 362172006Sdarrenr } 3622145522Sdarrenr is->is_flags &= ~(SI_WILDP|SI_WILDA); 362353642Sguido } 3624145522Sdarrenr 3625145522Sdarrenr /* 3626145522Sdarrenr * Next, remove it from the timeout queue it is in. 3627145522Sdarrenr */ 3628170268Sdarrenr if (is->is_sti.tqe_ifq != NULL) 3629255332Scy ipf_deletequeueentry(&is->is_sti); 3630145522Sdarrenr 3631145522Sdarrenr /* 3632145522Sdarrenr * If it is still in use by something else, do not go any further, 3633170268Sdarrenr * but note that at this point it is now an orphan. How can this 3634255332Scy * be? ipf_state_flush() calls ipf_delete() directly because it wants 3635170268Sdarrenr * to empty the table out and if something has a hold on a state 3636170268Sdarrenr * entry (such as ipfstat), it'll do the deref path that'll bring 3637170268Sdarrenr * us back here to do the real delete & free. 3638145522Sdarrenr */ 3639172776Sdarrenr MUTEX_ENTER(&is->is_lock); 3640255332Scy if (is->is_me != NULL) { 3641255332Scy *is->is_me = NULL; 3642255332Scy is->is_me = NULL; 3643255332Scy is->is_ref--; 3644255332Scy } 3645172776Sdarrenr if (is->is_ref > 1) { 3646255332Scy int refs; 3647255332Scy 3648172776Sdarrenr is->is_ref--; 3649255332Scy refs = is->is_ref; 3650172776Sdarrenr MUTEX_EXIT(&is->is_lock); 3651255332Scy if (!orphan) 3652255332Scy softs->ipf_state_stats.iss_orphan++; 3653255332Scy return refs; 3654172776Sdarrenr } 3655172776Sdarrenr MUTEX_EXIT(&is->is_lock); 3656145522Sdarrenr 3657255332Scy fr = is->is_rule; 3658255332Scy is->is_rule = NULL; 3659255332Scy if (fr != NULL) { 3660255332Scy if (fr->fr_srctrack.ht_max_nodes != 0) { 3661255332Scy (void) ipf_ht_node_del(&fr->fr_srctrack, 3662255332Scy is->is_family, &is->is_src); 3663255332Scy } 3664255332Scy } 3665255332Scy 3666172776Sdarrenr is->is_ref = 0; 3667172776Sdarrenr 3668145522Sdarrenr if (is->is_tqehead[0] != NULL) { 3669255332Scy if (ipf_deletetimeoutqueue(is->is_tqehead[0]) == 0) 3670255332Scy ipf_freetimeoutqueue(softc, is->is_tqehead[0]); 3671145522Sdarrenr } 3672145522Sdarrenr if (is->is_tqehead[1] != NULL) { 3673255332Scy if (ipf_deletetimeoutqueue(is->is_tqehead[1]) == 0) 3674255332Scy ipf_freetimeoutqueue(softc, is->is_tqehead[1]); 3675145522Sdarrenr } 3676145522Sdarrenr 3677145522Sdarrenr if (is->is_sync) 3678255332Scy ipf_sync_del_state(softc->ipf_sync_soft, is->is_sync); 3679145522Sdarrenr 3680170268Sdarrenr /* 3681170268Sdarrenr * Now remove it from the linked list of known states 3682170268Sdarrenr */ 3683170268Sdarrenr if (is->is_pnext != NULL) { 3684170268Sdarrenr *is->is_pnext = is->is_next; 3685170268Sdarrenr 3686170268Sdarrenr if (is->is_next != NULL) 3687170268Sdarrenr is->is_next->is_pnext = is->is_pnext; 3688170268Sdarrenr 3689170268Sdarrenr is->is_pnext = NULL; 3690170268Sdarrenr is->is_next = NULL; 3691170268Sdarrenr } 3692170268Sdarrenr 3693255332Scy if (softs->ipf_state_logging != 0 && why != 0) 3694255332Scy ipf_state_log(softc, is, why); 3695145522Sdarrenr 3696153876Sguido if (is->is_p == IPPROTO_TCP) 3697255332Scy softs->ipf_state_stats.iss_fin++; 3698153876Sguido else 3699255332Scy softs->ipf_state_stats.iss_expire++; 3700255332Scy if (orphan) 3701255332Scy softs->ipf_state_stats.iss_orphan--; 3702153876Sguido 3703255332Scy if (fr != NULL) { 3704255332Scy fr->fr_statecnt--; 3705255332Scy (void) ipf_derefrule(softc, &fr); 3706145522Sdarrenr } 3707145522Sdarrenr 3708255332Scy softs->ipf_state_stats.iss_active_proto[is->is_p]--; 3709180778Sdarrenr 371060854Sdarrenr MUTEX_DESTROY(&is->is_lock); 371153642Sguido KFREE(is); 3712255332Scy softs->ipf_state_stats.iss_active--; 3713170268Sdarrenr 3714170268Sdarrenr return 0; 371553642Sguido} 371653642Sguido 371753642Sguido 3718145522Sdarrenr/* ------------------------------------------------------------------------ */ 3719255332Scy/* Function: ipf_state_expire */ 3720145522Sdarrenr/* Returns: Nil */ 3721255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 3722145522Sdarrenr/* */ 3723145522Sdarrenr/* Slowly expire held state for thingslike UDP and ICMP. The algorithm */ 3724145522Sdarrenr/* used here is to keep the queue sorted with the oldest things at the top */ 3725145522Sdarrenr/* and the youngest at the bottom. So if the top one doesn't need to be */ 3726145522Sdarrenr/* expired then neither will any under it. */ 3727145522Sdarrenr/* ------------------------------------------------------------------------ */ 3728255332Scyvoid 3729255332Scyipf_state_expire(softc) 3730255332Scy ipf_main_softc_t *softc; 373153642Sguido{ 3732255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 3733145522Sdarrenr ipftq_t *ifq, *ifqnext; 3734145522Sdarrenr ipftqent_t *tqe, *tqn; 3735145522Sdarrenr ipstate_t *is; 3736153876Sguido SPL_INT(s); 373753642Sguido 3738145522Sdarrenr SPL_NET(s); 3739255332Scy WRITE_ENTER(&softc->ipf_state); 3740255332Scy for (ifq = softs->ipf_state_tcptq; ifq != NULL; ifq = ifq->ifq_next) 3741145522Sdarrenr for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) { 3742255332Scy if (tqe->tqe_die > softc->ipf_ticks) 3743145522Sdarrenr break; 3744145522Sdarrenr tqn = tqe->tqe_next; 3745145522Sdarrenr is = tqe->tqe_parent; 3746255332Scy ipf_state_del(softc, is, ISL_EXPIRE); 3747145522Sdarrenr } 3748145522Sdarrenr 3749255332Scy for (ifq = softs->ipf_state_usertq; ifq != NULL; ifq = ifqnext) { 3750145522Sdarrenr ifqnext = ifq->ifq_next; 3751145522Sdarrenr 3752145522Sdarrenr for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) { 3753255332Scy if (tqe->tqe_die > softc->ipf_ticks) 3754145522Sdarrenr break; 3755145522Sdarrenr tqn = tqe->tqe_next; 3756145522Sdarrenr is = tqe->tqe_parent; 3757255332Scy ipf_state_del(softc, is, ISL_EXPIRE); 3758145522Sdarrenr } 3759145522Sdarrenr } 3760145522Sdarrenr 3761255332Scy for (ifq = softs->ipf_state_usertq; ifq != NULL; ifq = ifqnext) { 3762145522Sdarrenr ifqnext = ifq->ifq_next; 3763145522Sdarrenr 3764145522Sdarrenr if (((ifq->ifq_flags & IFQF_DELETE) != 0) && 3765145522Sdarrenr (ifq->ifq_ref == 0)) { 3766255332Scy ipf_freetimeoutqueue(softc, ifq); 3767145522Sdarrenr } 3768145522Sdarrenr } 3769145522Sdarrenr 3770255332Scy if (softs->ipf_state_doflush) { 3771255332Scy (void) ipf_state_flush(softc, 2, 0); 3772255332Scy softs->ipf_state_doflush = 0; 3773255332Scy softs->ipf_state_wm_last = softc->ipf_ticks; 3774145522Sdarrenr } 3775145522Sdarrenr 3776255332Scy RWLOCK_EXIT(&softc->ipf_state); 3777145522Sdarrenr SPL_X(s); 377853642Sguido} 377953642Sguido 378053642Sguido 3781145522Sdarrenr/* ------------------------------------------------------------------------ */ 3782255332Scy/* Function: ipf_state_flush */ 3783145522Sdarrenr/* Returns: int - 0 == success, -1 == failure */ 3784255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 3785255332Scy/* which(I) - which flush action to perform */ 3786255332Scy/* proto(I) - which protocol to flush (0 == ALL) */ 3787145522Sdarrenr/* Write Locks: ipf_state */ 3788145522Sdarrenr/* */ 3789145522Sdarrenr/* Flush state tables. Three actions currently defined: */ 3790145522Sdarrenr/* which == 0 : flush all state table entries */ 3791145522Sdarrenr/* which == 1 : flush TCP connections which have started to close but are */ 3792145522Sdarrenr/* stuck for some reason. */ 3793145522Sdarrenr/* which == 2 : flush TCP connections which have been idle for a long time, */ 3794145522Sdarrenr/* starting at > 4 days idle and working back in successive half-*/ 3795145522Sdarrenr/* days to at most 12 hours old. If this fails to free enough */ 3796145522Sdarrenr/* slots then work backwards in half hour slots to 30 minutes. */ 3797145522Sdarrenr/* If that too fails, then work backwards in 30 second intervals */ 3798145522Sdarrenr/* for the last 30 minutes to at worst 30 seconds idle. */ 3799145522Sdarrenr/* ------------------------------------------------------------------------ */ 3800255332Scyint 3801255332Scyipf_state_flush(softc, which, proto) 3802255332Scy ipf_main_softc_t *softc; 3803255332Scy int which, proto; 380453642Sguido{ 3805255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 3806145522Sdarrenr ipftqent_t *tqe, *tqn; 3807145522Sdarrenr ipstate_t *is, **isp; 3808255332Scy ipftq_t *ifq; 3809170268Sdarrenr int removed; 3810153876Sguido SPL_INT(s); 381153642Sguido 3812145522Sdarrenr removed = 0; 3813145522Sdarrenr 381453642Sguido SPL_NET(s); 3815145522Sdarrenr 3816170268Sdarrenr switch (which) 3817170268Sdarrenr { 3818170268Sdarrenr case 0 : 3819255332Scy SBUMP(ipf_state_stats.iss_flush_all); 3820170268Sdarrenr /* 3821170268Sdarrenr * Style 0 flush removes everything... 3822170268Sdarrenr */ 3823255332Scy for (isp = &softs->ipf_state_list; ((is = *isp) != NULL); ) { 3824170268Sdarrenr if ((proto != 0) && (is->is_v != proto)) { 3825170268Sdarrenr isp = &is->is_next; 3826170268Sdarrenr continue; 3827170268Sdarrenr } 3828255332Scy if (ipf_state_del(softc, is, ISL_FLUSH) == 0) 3829170268Sdarrenr removed++; 3830170268Sdarrenr else 3831170268Sdarrenr isp = &is->is_next; 3832145522Sdarrenr } 3833170268Sdarrenr break; 3834145522Sdarrenr 3835170268Sdarrenr case 1 : 3836255332Scy SBUMP(ipf_state_stats.iss_flush_closing); 3837170268Sdarrenr /* 3838170268Sdarrenr * Since we're only interested in things that are closing, 3839170268Sdarrenr * we can start with the appropriate timeout queue. 3840170268Sdarrenr */ 3841255332Scy for (ifq = softs->ipf_state_tcptq + IPF_TCPS_CLOSE_WAIT; 3842255332Scy ifq != NULL; ifq = ifq->ifq_next) { 3843170268Sdarrenr 3844170268Sdarrenr for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) { 3845170268Sdarrenr tqn = tqe->tqe_next; 3846170268Sdarrenr is = tqe->tqe_parent; 3847170268Sdarrenr if (is->is_p != IPPROTO_TCP) 3848170268Sdarrenr break; 3849255332Scy if (ipf_state_del(softc, is, ISL_FLUSH) == 0) 3850170268Sdarrenr removed++; 3851170268Sdarrenr } 3852145522Sdarrenr } 3853145522Sdarrenr 3854170268Sdarrenr /* 3855170268Sdarrenr * Also need to look through the user defined queues. 3856170268Sdarrenr */ 3857255332Scy for (ifq = softs->ipf_state_usertq; ifq != NULL; 3858255332Scy ifq = ifq->ifq_next) { 3859170268Sdarrenr for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) { 3860170268Sdarrenr tqn = tqe->tqe_next; 3861170268Sdarrenr is = tqe->tqe_parent; 3862170268Sdarrenr if (is->is_p != IPPROTO_TCP) 3863170268Sdarrenr continue; 3864170268Sdarrenr 3865170268Sdarrenr if ((is->is_state[0] > IPF_TCPS_ESTABLISHED) && 3866170268Sdarrenr (is->is_state[1] > IPF_TCPS_ESTABLISHED)) { 3867255332Scy if (ipf_state_del(softc, is, 3868255332Scy ISL_FLUSH) == 0) 3869170268Sdarrenr removed++; 3870170268Sdarrenr } 3871170268Sdarrenr } 3872170268Sdarrenr } 3873170268Sdarrenr break; 3874170268Sdarrenr 3875170268Sdarrenr case 2 : 3876170268Sdarrenr break; 3877170268Sdarrenr 3878255332Scy /* 3879170268Sdarrenr * Args 5-11 correspond to flushing those particular states 3880170268Sdarrenr * for TCP connections. 3881170268Sdarrenr */ 3882170268Sdarrenr case IPF_TCPS_CLOSE_WAIT : 3883170268Sdarrenr case IPF_TCPS_FIN_WAIT_1 : 3884170268Sdarrenr case IPF_TCPS_CLOSING : 3885170268Sdarrenr case IPF_TCPS_LAST_ACK : 3886170268Sdarrenr case IPF_TCPS_FIN_WAIT_2 : 3887170268Sdarrenr case IPF_TCPS_TIME_WAIT : 3888170268Sdarrenr case IPF_TCPS_CLOSED : 3889255332Scy SBUMP(ipf_state_stats.iss_flush_queue); 3890255332Scy tqn = softs->ipf_state_tcptq[which].ifq_head; 3891170268Sdarrenr while (tqn != NULL) { 3892170268Sdarrenr tqe = tqn; 3893170268Sdarrenr tqn = tqe->tqe_next; 3894170268Sdarrenr is = tqe->tqe_parent; 3895255332Scy if (ipf_state_del(softc, is, ISL_FLUSH) == 0) 3896170268Sdarrenr removed++; 3897170268Sdarrenr } 3898170268Sdarrenr break; 3899170268Sdarrenr 3900170268Sdarrenr default : 3901170268Sdarrenr if (which < 30) 3902170268Sdarrenr break; 3903170268Sdarrenr 3904255332Scy SBUMP(ipf_state_stats.iss_flush_state); 3905255332Scy /* 3906170268Sdarrenr * Take a large arbitrary number to mean the number of seconds 3907170268Sdarrenr * for which which consider to be the maximum value we'll allow 3908170268Sdarrenr * the expiration to be. 3909170268Sdarrenr */ 3910170268Sdarrenr which = IPF_TTLVAL(which); 3911255332Scy for (isp = &softs->ipf_state_list; ((is = *isp) != NULL); ) { 3912170268Sdarrenr if ((proto == 0) || (is->is_v == proto)) { 3913255332Scy if (softc->ipf_ticks - is->is_touched > which) { 3914255332Scy if (ipf_state_del(softc, is, 3915255332Scy ISL_FLUSH) == 0) { 3916170268Sdarrenr removed++; 3917170268Sdarrenr continue; 3918170268Sdarrenr } 3919170268Sdarrenr } 3920170268Sdarrenr } 392160854Sdarrenr isp = &is->is_next; 3922170268Sdarrenr } 3923170268Sdarrenr break; 392455929Sguido } 392553642Sguido 3926145522Sdarrenr if (which != 2) { 3927145522Sdarrenr SPL_X(s); 3928145522Sdarrenr return removed; 3929145522Sdarrenr } 393053642Sguido 3931255332Scy SBUMP(ipf_state_stats.iss_flush_timeout); 3932145522Sdarrenr /* 3933255332Scy * Asked to remove inactive entries because the table is full, try 3934255332Scy * again, 3 times, if first attempt failed with a different criteria 3935255332Scy * each time. The order tried in must be in decreasing age. 3936255332Scy * Another alternative is to implement random drop and drop N entries 3937255332Scy * at random until N have been freed up. 3938145522Sdarrenr */ 3939255332Scy if (softc->ipf_ticks - softs->ipf_state_wm_last > 3940255332Scy softs->ipf_state_wm_freq) { 3941255332Scy removed = ipf_queueflush(softc, ipf_state_flush_entry, 3942255332Scy softs->ipf_state_tcptq, 3943255332Scy softs->ipf_state_usertq, 3944255332Scy &softs->ipf_state_stats.iss_active, 3945255332Scy softs->ipf_state_size, 3946255332Scy softs->ipf_state_wm_low); 3947255332Scy softs->ipf_state_wm_last = softc->ipf_ticks; 3948170268Sdarrenr } 394953642Sguido 3950145522Sdarrenr SPL_X(s); 3951145522Sdarrenr return removed; 3952145522Sdarrenr} 395364580Sdarrenr 395464580Sdarrenr 3955170268Sdarrenr/* ------------------------------------------------------------------------ */ 3956255332Scy/* Function: ipf_state_flush_entry */ 3957170268Sdarrenr/* Returns: int - 0 = entry deleted, else not deleted */ 3958255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 3959255332Scy/* entry(I) - pointer to state structure to delete */ 3960170268Sdarrenr/* Write Locks: ipf_state */ 3961170268Sdarrenr/* */ 3962170268Sdarrenr/* This function is a stepping stone between ipf_queueflush() and */ 3963255332Scy/* ipf_state_del(). It is used so we can provide a uniform interface via */ 3964255332Scy/* the ipf_queueflush() function. */ 3965170268Sdarrenr/* ------------------------------------------------------------------------ */ 3966255332Scystatic int 3967255332Scyipf_state_flush_entry(softc, entry) 3968255332Scy ipf_main_softc_t *softc; 3969255332Scy void *entry; 3970170268Sdarrenr{ 3971255332Scy return ipf_state_del(softc, entry, ISL_FLUSH); 3972255332Scy} 397364580Sdarrenr 3974170268Sdarrenr 3975145522Sdarrenr/* ------------------------------------------------------------------------ */ 3976255332Scy/* Function: ipf_tcp_age */ 3977145522Sdarrenr/* Returns: int - 1 == state transition made, 0 == no change (rejected) */ 3978255332Scy/* Parameters: tqe(I) - pointer to timeout queue information */ 3979145522Sdarrenr/* fin(I) - pointer to packet information */ 3980145522Sdarrenr/* tqtab(I) - TCP timeout queue table this is in */ 3981145522Sdarrenr/* flags(I) - flags from state/NAT entry */ 3982255332Scy/* ok(I) - can we advance state */ 3983145522Sdarrenr/* */ 3984145522Sdarrenr/* Rewritten by Arjan de Vet <Arjan.deVet@adv.iae.nl>, 2000-07-29: */ 3985145522Sdarrenr/* */ 3986145522Sdarrenr/* - (try to) base state transitions on real evidence only, */ 3987145522Sdarrenr/* i.e. packets that are sent and have been received by ipfilter; */ 3988145522Sdarrenr/* diagram 18.12 of TCP/IP volume 1 by W. Richard Stevens was used. */ 3989145522Sdarrenr/* */ 3990145522Sdarrenr/* - deal with half-closed connections correctly; */ 3991145522Sdarrenr/* */ 3992145522Sdarrenr/* - store the state of the source in state[0] such that ipfstat */ 3993145522Sdarrenr/* displays the state as source/dest instead of dest/source; the calls */ 3994255332Scy/* to ipf_tcp_age have been changed accordingly. */ 3995145522Sdarrenr/* */ 3996145522Sdarrenr/* Internal Parameters: */ 3997145522Sdarrenr/* */ 3998145522Sdarrenr/* state[0] = state of source (host that initiated connection) */ 3999145522Sdarrenr/* state[1] = state of dest (host that accepted the connection) */ 4000145522Sdarrenr/* */ 4001145522Sdarrenr/* dir == 0 : a packet from source to dest */ 4002145522Sdarrenr/* dir == 1 : a packet from dest to source */ 4003145522Sdarrenr/* */ 4004170268Sdarrenr/* A typical procession for a connection is as follows: */ 4005170268Sdarrenr/* */ 4006170268Sdarrenr/* +--------------+-------------------+ */ 4007170268Sdarrenr/* | Side '0' | Side '1' | */ 4008170268Sdarrenr/* +--------------+-------------------+ */ 4009170268Sdarrenr/* | 0 -> 1 (SYN) | | */ 4010170268Sdarrenr/* | | 0 -> 2 (SYN-ACK) | */ 4011170268Sdarrenr/* | 1 -> 3 (ACK) | | */ 4012170268Sdarrenr/* | | 2 -> 4 (ACK-PUSH) | */ 4013170268Sdarrenr/* | 3 -> 4 (ACK) | | */ 4014170268Sdarrenr/* | ... | ... | */ 4015170268Sdarrenr/* | | 4 -> 6 (FIN-ACK) | */ 4016170268Sdarrenr/* | 4 -> 5 (ACK) | | */ 4017170268Sdarrenr/* | | 6 -> 6 (ACK-PUSH) | */ 4018170268Sdarrenr/* | 5 -> 5 (ACK) | | */ 4019170268Sdarrenr/* | 5 -> 8 (FIN) | | */ 4020170268Sdarrenr/* | | 6 -> 10 (ACK) | */ 4021170268Sdarrenr/* +--------------+-------------------+ */ 4022170268Sdarrenr/* */ 4023145522Sdarrenr/* Locking: it is assumed that the parent of the tqe structure is locked. */ 4024145522Sdarrenr/* ------------------------------------------------------------------------ */ 4025255332Scyint 4026255332Scyipf_tcp_age(tqe, fin, tqtab, flags, ok) 4027255332Scy ipftqent_t *tqe; 4028255332Scy fr_info_t *fin; 4029255332Scy ipftq_t *tqtab; 4030255332Scy int flags, ok; 4031145522Sdarrenr{ 4032255332Scy ipf_main_softc_t *softc = fin->fin_main_soft; 4033145522Sdarrenr int dlen, ostate, nstate, rval, dir; 4034145522Sdarrenr u_char tcpflags; 4035145522Sdarrenr tcphdr_t *tcp; 4036145522Sdarrenr 4037145522Sdarrenr tcp = fin->fin_dp; 4038145522Sdarrenr 4039145522Sdarrenr rval = 0; 4040145522Sdarrenr dir = fin->fin_rev; 4041145522Sdarrenr tcpflags = tcp->th_flags; 4042153876Sguido dlen = fin->fin_dlen - (TCP_OFF(tcp) << 2); 4043255332Scy ostate = tqe->tqe_state[1 - dir]; 4044255332Scy nstate = tqe->tqe_state[dir]; 4045145522Sdarrenr 4046145522Sdarrenr if (tcpflags & TH_RST) { 4047145522Sdarrenr if (!(tcpflags & TH_PUSH) && !dlen) 4048145522Sdarrenr nstate = IPF_TCPS_CLOSED; 4049145522Sdarrenr else 4050145522Sdarrenr nstate = IPF_TCPS_CLOSE_WAIT; 4051255332Scy 4052255332Scy if (ostate <= IPF_TCPS_ESTABLISHED) { 4053255332Scy tqe->tqe_state[1 - dir] = IPF_TCPS_CLOSE_WAIT; 4054255332Scy } 4055145522Sdarrenr rval = 1; 4056145522Sdarrenr } else { 4057145522Sdarrenr switch (nstate) 4058145522Sdarrenr { 4059170268Sdarrenr case IPF_TCPS_LISTEN: /* 0 */ 4060145522Sdarrenr if ((tcpflags & TH_OPENING) == TH_OPENING) { 4061145522Sdarrenr /* 4062145522Sdarrenr * 'dir' received an S and sends SA in 4063170268Sdarrenr * response, LISTEN -> SYN_RECEIVED 4064145522Sdarrenr */ 4065145522Sdarrenr nstate = IPF_TCPS_SYN_RECEIVED; 4066145522Sdarrenr rval = 1; 4067145522Sdarrenr } else if ((tcpflags & TH_OPENING) == TH_SYN) { 4068170268Sdarrenr /* 'dir' sent S, LISTEN -> SYN_SENT */ 4069145522Sdarrenr nstate = IPF_TCPS_SYN_SENT; 4070145522Sdarrenr rval = 1; 4071145522Sdarrenr } 407264580Sdarrenr /* 4073145522Sdarrenr * the next piece of code makes it possible to get 4074145522Sdarrenr * already established connections into the state table 4075145522Sdarrenr * after a restart or reload of the filter rules; this 4076145522Sdarrenr * does not work when a strict 'flags S keep state' is 4077145522Sdarrenr * used for tcp connections of course 407864580Sdarrenr */ 4079145522Sdarrenr if (((flags & IS_TCPFSM) == 0) && 4080145522Sdarrenr ((tcpflags & TH_ACKMASK) == TH_ACK)) { 4081145522Sdarrenr /* 4082145522Sdarrenr * we saw an A, guess 'dir' is in ESTABLISHED 4083145522Sdarrenr * mode 4084145522Sdarrenr */ 4085145522Sdarrenr switch (ostate) 4086145522Sdarrenr { 4087170268Sdarrenr case IPF_TCPS_LISTEN : 4088145522Sdarrenr case IPF_TCPS_SYN_RECEIVED : 4089145522Sdarrenr nstate = IPF_TCPS_HALF_ESTAB; 4090145522Sdarrenr rval = 1; 4091145522Sdarrenr break; 4092145522Sdarrenr case IPF_TCPS_HALF_ESTAB : 4093145522Sdarrenr case IPF_TCPS_ESTABLISHED : 4094145522Sdarrenr nstate = IPF_TCPS_ESTABLISHED; 4095145522Sdarrenr rval = 1; 4096145522Sdarrenr break; 4097145522Sdarrenr default : 4098145522Sdarrenr break; 4099145522Sdarrenr } 4100145522Sdarrenr } 410195563Sdarrenr /* 4102145522Sdarrenr * TODO: besides regular ACK packets we can have other 4103145522Sdarrenr * packets as well; it is yet to be determined how we 4104145522Sdarrenr * should initialize the states in those cases 410595563Sdarrenr */ 4106145522Sdarrenr break; 410764580Sdarrenr 4108170268Sdarrenr case IPF_TCPS_SYN_SENT: /* 1 */ 4109145522Sdarrenr if ((tcpflags & ~(TH_ECN|TH_CWR)) == TH_SYN) { 411064580Sdarrenr /* 4111145522Sdarrenr * A retransmitted SYN packet. We do not reset 4112255332Scy * the timeout here to ipf_tcptimeout because a 4113145522Sdarrenr * connection connect timeout does not renew 4114145522Sdarrenr * after every packet that is sent. We need to 4115145522Sdarrenr * set rval so as to indicate the packet has 4116145522Sdarrenr * passed the check for its flags being valid 4117145522Sdarrenr * in the TCP FSM. Setting rval to 2 has the 4118145522Sdarrenr * result of not resetting the timeout. 411964580Sdarrenr */ 4120145522Sdarrenr rval = 2; 4121145522Sdarrenr } else if ((tcpflags & (TH_SYN|TH_FIN|TH_ACK)) == 4122145522Sdarrenr TH_ACK) { 412364580Sdarrenr /* 4124145522Sdarrenr * we see an A from 'dir' which is in SYN_SENT 4125145522Sdarrenr * state: 'dir' sent an A in response to an SA 4126145522Sdarrenr * which it received, SYN_SENT -> ESTABLISHED 412764580Sdarrenr */ 4128145522Sdarrenr nstate = IPF_TCPS_ESTABLISHED; 4129145522Sdarrenr rval = 1; 4130145522Sdarrenr } else if (tcpflags & TH_FIN) { 4131145522Sdarrenr /* 4132145522Sdarrenr * we see an F from 'dir' which is in SYN_SENT 4133145522Sdarrenr * state and wants to close its side of the 4134145522Sdarrenr * connection; SYN_SENT -> FIN_WAIT_1 4135145522Sdarrenr */ 4136145522Sdarrenr nstate = IPF_TCPS_FIN_WAIT_1; 4137145522Sdarrenr rval = 1; 4138145522Sdarrenr } else if ((tcpflags & TH_OPENING) == TH_OPENING) { 4139145522Sdarrenr /* 4140145522Sdarrenr * we see an SA from 'dir' which is already in 4141145522Sdarrenr * SYN_SENT state, this means we have a 4142145522Sdarrenr * simultaneous open; SYN_SENT -> SYN_RECEIVED 4143145522Sdarrenr */ 4144145522Sdarrenr nstate = IPF_TCPS_SYN_RECEIVED; 4145145522Sdarrenr rval = 1; 4146145522Sdarrenr } 4147145522Sdarrenr break; 414864580Sdarrenr 4149170268Sdarrenr case IPF_TCPS_SYN_RECEIVED: /* 2 */ 4150145522Sdarrenr if ((tcpflags & (TH_SYN|TH_FIN|TH_ACK)) == TH_ACK) { 4151145522Sdarrenr /* 4152145522Sdarrenr * we see an A from 'dir' which was in 4153145522Sdarrenr * SYN_RECEIVED state so it must now be in 4154145522Sdarrenr * established state, SYN_RECEIVED -> 4155145522Sdarrenr * ESTABLISHED 4156145522Sdarrenr */ 4157145522Sdarrenr nstate = IPF_TCPS_ESTABLISHED; 4158145522Sdarrenr rval = 1; 4159145522Sdarrenr } else if ((tcpflags & ~(TH_ECN|TH_CWR)) == 4160145522Sdarrenr TH_OPENING) { 4161145522Sdarrenr /* 4162145522Sdarrenr * We see an SA from 'dir' which is already in 4163145522Sdarrenr * SYN_RECEIVED state. 4164145522Sdarrenr */ 4165145522Sdarrenr rval = 2; 4166145522Sdarrenr } else if (tcpflags & TH_FIN) { 4167145522Sdarrenr /* 4168145522Sdarrenr * we see an F from 'dir' which is in 4169145522Sdarrenr * SYN_RECEIVED state and wants to close its 4170145522Sdarrenr * side of the connection; SYN_RECEIVED -> 4171145522Sdarrenr * FIN_WAIT_1 4172145522Sdarrenr */ 4173145522Sdarrenr nstate = IPF_TCPS_FIN_WAIT_1; 4174145522Sdarrenr rval = 1; 4175145522Sdarrenr } 4176145522Sdarrenr break; 417764580Sdarrenr 4178170268Sdarrenr case IPF_TCPS_HALF_ESTAB: /* 3 */ 4179153876Sguido if (tcpflags & TH_FIN) { 4180153876Sguido nstate = IPF_TCPS_FIN_WAIT_1; 4181153876Sguido rval = 1; 4182153876Sguido } else if ((tcpflags & TH_ACKMASK) == TH_ACK) { 4183153876Sguido /* 4184153876Sguido * If we've picked up a connection in mid 4185153876Sguido * flight, we could be looking at a follow on 4186153876Sguido * packet from the same direction as the one 4187153876Sguido * that created this state. Recognise it but 4188153876Sguido * do not advance the entire connection's 4189153876Sguido * state. 4190153876Sguido */ 4191153876Sguido switch (ostate) 4192153876Sguido { 4193170268Sdarrenr case IPF_TCPS_LISTEN : 4194153876Sguido case IPF_TCPS_SYN_SENT : 4195153876Sguido case IPF_TCPS_SYN_RECEIVED : 4196153876Sguido rval = 1; 4197153876Sguido break; 4198153876Sguido case IPF_TCPS_HALF_ESTAB : 4199153876Sguido case IPF_TCPS_ESTABLISHED : 4200145522Sdarrenr nstate = IPF_TCPS_ESTABLISHED; 4201145522Sdarrenr rval = 1; 4202153876Sguido break; 4203153876Sguido default : 4204153876Sguido break; 4205145522Sdarrenr } 4206145522Sdarrenr } 4207145522Sdarrenr break; 420864580Sdarrenr 4209170268Sdarrenr case IPF_TCPS_ESTABLISHED: /* 4 */ 4210145522Sdarrenr rval = 1; 4211145522Sdarrenr if (tcpflags & TH_FIN) { 4212145522Sdarrenr /* 4213145522Sdarrenr * 'dir' closed its side of the connection; 4214145522Sdarrenr * this gives us a half-closed connection; 4215145522Sdarrenr * ESTABLISHED -> FIN_WAIT_1 4216145522Sdarrenr */ 4217170268Sdarrenr if (ostate == IPF_TCPS_FIN_WAIT_1) { 4218170268Sdarrenr nstate = IPF_TCPS_CLOSING; 4219170268Sdarrenr } else { 4220170268Sdarrenr nstate = IPF_TCPS_FIN_WAIT_1; 4221170268Sdarrenr } 4222145522Sdarrenr } else if (tcpflags & TH_ACK) { 4223145522Sdarrenr /* 4224145522Sdarrenr * an ACK, should we exclude other flags here? 4225145522Sdarrenr */ 4226145522Sdarrenr if (ostate == IPF_TCPS_FIN_WAIT_1) { 4227145522Sdarrenr /* 4228145522Sdarrenr * We know the other side did an active 4229145522Sdarrenr * close, so we are ACKing the recvd 4230145522Sdarrenr * FIN packet (does the window matching 4231145522Sdarrenr * code guarantee this?) and go into 4232145522Sdarrenr * CLOSE_WAIT state; this gives us a 4233145522Sdarrenr * half-closed connection 4234145522Sdarrenr */ 4235145522Sdarrenr nstate = IPF_TCPS_CLOSE_WAIT; 4236145522Sdarrenr } else if (ostate < IPF_TCPS_CLOSE_WAIT) { 4237145522Sdarrenr /* 4238145522Sdarrenr * still a fully established 4239145522Sdarrenr * connection reset timeout 4240145522Sdarrenr */ 4241145522Sdarrenr nstate = IPF_TCPS_ESTABLISHED; 4242145522Sdarrenr } 4243145522Sdarrenr } 4244145522Sdarrenr break; 424564580Sdarrenr 4246170268Sdarrenr case IPF_TCPS_CLOSE_WAIT: /* 5 */ 4247145522Sdarrenr rval = 1; 4248145522Sdarrenr if (tcpflags & TH_FIN) { 424964580Sdarrenr /* 4250145522Sdarrenr * application closed and 'dir' sent a FIN, 4251145522Sdarrenr * we're now going into LAST_ACK state 425264580Sdarrenr */ 4253145522Sdarrenr nstate = IPF_TCPS_LAST_ACK; 4254145522Sdarrenr } else { 4255145522Sdarrenr /* 4256145522Sdarrenr * we remain in CLOSE_WAIT because the other 4257145522Sdarrenr * side has closed already and we did not 4258145522Sdarrenr * close our side yet; reset timeout 4259145522Sdarrenr */ 4260145522Sdarrenr nstate = IPF_TCPS_CLOSE_WAIT; 4261145522Sdarrenr } 4262145522Sdarrenr break; 426364580Sdarrenr 4264170268Sdarrenr case IPF_TCPS_FIN_WAIT_1: /* 6 */ 4265145522Sdarrenr rval = 1; 4266145522Sdarrenr if ((tcpflags & TH_ACK) && 4267145522Sdarrenr ostate > IPF_TCPS_CLOSE_WAIT) { 4268145522Sdarrenr /* 4269145522Sdarrenr * if the other side is not active anymore 4270145522Sdarrenr * it has sent us a FIN packet that we are 4271145522Sdarrenr * ack'ing now with an ACK; this means both 4272145522Sdarrenr * sides have now closed the connection and 4273145522Sdarrenr * we go into TIME_WAIT 4274145522Sdarrenr */ 4275145522Sdarrenr /* 4276145522Sdarrenr * XXX: how do we know we really are ACKing 4277145522Sdarrenr * the FIN packet here? does the window code 4278145522Sdarrenr * guarantee that? 4279145522Sdarrenr */ 4280255332Scy nstate = IPF_TCPS_LAST_ACK; 4281145522Sdarrenr } else { 4282145522Sdarrenr /* 4283145522Sdarrenr * we closed our side of the connection 4284145522Sdarrenr * already but the other side is still active 4285145522Sdarrenr * (ESTABLISHED/CLOSE_WAIT); continue with 4286145522Sdarrenr * this half-closed connection 4287145522Sdarrenr */ 4288145522Sdarrenr nstate = IPF_TCPS_FIN_WAIT_1; 4289145522Sdarrenr } 4290145522Sdarrenr break; 429164580Sdarrenr 4292170268Sdarrenr case IPF_TCPS_CLOSING: /* 7 */ 4293170268Sdarrenr if ((tcpflags & (TH_FIN|TH_ACK)) == TH_ACK) { 4294170268Sdarrenr nstate = IPF_TCPS_TIME_WAIT; 4295170268Sdarrenr } 4296255332Scy rval = 1; 4297145522Sdarrenr break; 4298145522Sdarrenr 4299170268Sdarrenr case IPF_TCPS_LAST_ACK: /* 8 */ 4300145522Sdarrenr if (tcpflags & TH_ACK) { 4301255332Scy rval = 1; 4302145522Sdarrenr } 4303145522Sdarrenr /* 4304255332Scy * we cannot detect when we go out of LAST_ACK state 4305255332Scy * to CLOSED because that is based on the reception 4306255332Scy * of ACK packets; ipfilter can only detect that a 4307255332Scy * packet has been sent by a host 4308145522Sdarrenr */ 4309145522Sdarrenr break; 4310145522Sdarrenr 4311170268Sdarrenr case IPF_TCPS_FIN_WAIT_2: /* 9 */ 4312170268Sdarrenr /* NOT USED */ 4313145522Sdarrenr break; 4314145522Sdarrenr 4315170268Sdarrenr case IPF_TCPS_TIME_WAIT: /* 10 */ 4316145522Sdarrenr /* we're in 2MSL timeout now */ 4317170268Sdarrenr if (ostate == IPF_TCPS_LAST_ACK) { 4318170268Sdarrenr nstate = IPF_TCPS_CLOSED; 4319255332Scy rval = 1; 4320255332Scy } else { 4321255332Scy rval = 2; 4322170268Sdarrenr } 4323145522Sdarrenr break; 4324145522Sdarrenr 4325170268Sdarrenr case IPF_TCPS_CLOSED: /* 11 */ 4326170268Sdarrenr rval = 2; 4327170268Sdarrenr break; 4328170268Sdarrenr 4329145522Sdarrenr default : 4330255332Scy#if !defined(_KERNEL) 4331145522Sdarrenr abort(); 4332145522Sdarrenr#endif 4333145522Sdarrenr break; 4334145522Sdarrenr } 433553642Sguido } 433695418Sdarrenr 4337145522Sdarrenr /* 4338145522Sdarrenr * If rval == 2 then do not update the queue position, but treat the 4339145522Sdarrenr * packet as being ok. 4340145522Sdarrenr */ 4341145522Sdarrenr if (rval == 2) 4342145522Sdarrenr rval = 1; 4343145522Sdarrenr else if (rval == 1) { 4344255332Scy if (ok) 4345255332Scy tqe->tqe_state[dir] = nstate; 4346145522Sdarrenr if ((tqe->tqe_flags & TQE_RULEBASED) == 0) 4347255332Scy ipf_movequeue(softc->ipf_ticks, tqe, tqe->tqe_ifq, 4348255332Scy tqtab + nstate); 434995418Sdarrenr } 4350145522Sdarrenr 4351145522Sdarrenr return rval; 435253642Sguido} 435353642Sguido 435453642Sguido 4355145522Sdarrenr/* ------------------------------------------------------------------------ */ 4356255332Scy/* Function: ipf_state_log */ 4357145522Sdarrenr/* Returns: Nil */ 4358255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 4359255332Scy/* is(I) - pointer to state structure */ 4360255332Scy/* type(I) - type of log entry to create */ 4361145522Sdarrenr/* */ 4362145522Sdarrenr/* Creates a state table log entry using the state structure and type info. */ 4363145522Sdarrenr/* passed in. Log packet/byte counts, source/destination address and other */ 4364145522Sdarrenr/* protocol specific information. */ 4365145522Sdarrenr/* ------------------------------------------------------------------------ */ 4366255332Scyvoid 4367255332Scyipf_state_log(softc, is, type) 4368255332Scy ipf_main_softc_t *softc; 4369255332Scy struct ipstate *is; 4370255332Scy u_int type; 437153642Sguido{ 4372145522Sdarrenr#ifdef IPFILTER_LOG 437353642Sguido struct ipslog ipsl; 4374145522Sdarrenr size_t sizes[1]; 437553642Sguido void *items[1]; 437653642Sguido int types[1]; 437753642Sguido 4378145522Sdarrenr /* 4379145522Sdarrenr * Copy information out of the ipstate_t structure and into the 4380145522Sdarrenr * structure used for logging. 4381145522Sdarrenr */ 438253642Sguido ipsl.isl_type = type; 4383145522Sdarrenr ipsl.isl_pkts[0] = is->is_pkts[0] + is->is_icmppkts[0]; 4384145522Sdarrenr ipsl.isl_bytes[0] = is->is_bytes[0]; 4385145522Sdarrenr ipsl.isl_pkts[1] = is->is_pkts[1] + is->is_icmppkts[1]; 4386145522Sdarrenr ipsl.isl_bytes[1] = is->is_bytes[1]; 4387145522Sdarrenr ipsl.isl_pkts[2] = is->is_pkts[2] + is->is_icmppkts[2]; 4388145522Sdarrenr ipsl.isl_bytes[2] = is->is_bytes[2]; 4389145522Sdarrenr ipsl.isl_pkts[3] = is->is_pkts[3] + is->is_icmppkts[3]; 4390145522Sdarrenr ipsl.isl_bytes[3] = is->is_bytes[3]; 439153642Sguido ipsl.isl_src = is->is_src; 439253642Sguido ipsl.isl_dst = is->is_dst; 439353642Sguido ipsl.isl_p = is->is_p; 439460854Sdarrenr ipsl.isl_v = is->is_v; 439553642Sguido ipsl.isl_flags = is->is_flags; 4396145522Sdarrenr ipsl.isl_tag = is->is_tag; 4397102520Sdarrenr ipsl.isl_rulen = is->is_rulen; 4398145522Sdarrenr (void) strncpy(ipsl.isl_group, is->is_group, FR_GROUPLEN); 4399145522Sdarrenr 440053642Sguido if (ipsl.isl_p == IPPROTO_TCP || ipsl.isl_p == IPPROTO_UDP) { 440153642Sguido ipsl.isl_sport = is->is_sport; 440253642Sguido ipsl.isl_dport = is->is_dport; 440353642Sguido if (ipsl.isl_p == IPPROTO_TCP) { 440453642Sguido ipsl.isl_state[0] = is->is_state[0]; 440553642Sguido ipsl.isl_state[1] = is->is_state[1]; 440653642Sguido } 440792685Sdarrenr } else if (ipsl.isl_p == IPPROTO_ICMP) { 4408145522Sdarrenr ipsl.isl_itype = is->is_icmp.ici_type; 440992685Sdarrenr } else if (ipsl.isl_p == IPPROTO_ICMPV6) { 4410145522Sdarrenr ipsl.isl_itype = is->is_icmp.ici_type; 441192685Sdarrenr } else { 441253642Sguido ipsl.isl_ps.isl_filler[0] = 0; 441353642Sguido ipsl.isl_ps.isl_filler[1] = 0; 441453642Sguido } 4415145522Sdarrenr 441653642Sguido items[0] = &ipsl; 441753642Sguido sizes[0] = sizeof(ipsl); 441853642Sguido types[0] = 0; 441953642Sguido 4420255332Scy (void) ipf_log_items(softc, IPL_LOGSTATE, NULL, items, sizes, types, 1); 4421145522Sdarrenr#endif 442253642Sguido} 442357096Sguido 442457096Sguido 442560854Sdarrenr#ifdef USE_INET6 4426145522Sdarrenr/* ------------------------------------------------------------------------ */ 4427255332Scy/* Function: ipf_checkicmp6matchingstate */ 4428145522Sdarrenr/* Returns: ipstate_t* - NULL == no match found, */ 4429145522Sdarrenr/* else pointer to matching state entry */ 4430145522Sdarrenr/* Parameters: fin(I) - pointer to packet information */ 4431145522Sdarrenr/* Locks: NULL == no locks, else Read Lock on ipf_state */ 4432145522Sdarrenr/* */ 4433145522Sdarrenr/* If we've got an ICMPv6 error message, using the information stored in */ 4434145522Sdarrenr/* the ICMPv6 packet, look for a matching state table entry. */ 4435145522Sdarrenr/* ------------------------------------------------------------------------ */ 4436255332Scystatic ipstate_t * 4437255332Scyipf_checkicmp6matchingstate(fin) 4438255332Scy fr_info_t *fin; 443957096Sguido{ 4440255332Scy ipf_main_softc_t *softc = fin->fin_main_soft; 4441255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 4442145522Sdarrenr struct icmp6_hdr *ic6, *oic; 4443145522Sdarrenr ipstate_t *is, **isp; 4444145522Sdarrenr u_short sport, dport; 4445145522Sdarrenr i6addr_t dst, src; 444660854Sdarrenr u_short savelen; 4447145522Sdarrenr icmpinfo_t *ic; 444860854Sdarrenr fr_info_t ofin; 444960854Sdarrenr tcphdr_t *tcp; 4450145522Sdarrenr ip6_t *oip6; 4451255332Scy u_char pr; 445260854Sdarrenr u_int hv; 4453255332Scy int type; 445457096Sguido 445560854Sdarrenr /* 445660854Sdarrenr * Does it at least have the return (basic) IP header ? 4457153876Sguido * Is it an actual recognised ICMP error type? 445860854Sdarrenr * Only a basic IP header (no options) should be with 445960854Sdarrenr * an ICMP error header. 446060854Sdarrenr */ 4461153876Sguido if ((fin->fin_v != 6) || (fin->fin_plen < ICMP6ERR_MINPKTLEN) || 4462255332Scy !(fin->fin_flx & FI_ICMPERR)) { 4463255332Scy SBUMPD(ipf_state_stats, iss_icmp_bad); 446460854Sdarrenr return NULL; 4465255332Scy } 4466145522Sdarrenr 4467145522Sdarrenr ic6 = fin->fin_dp; 4468145522Sdarrenr type = ic6->icmp6_type; 446960854Sdarrenr 4470145522Sdarrenr oip6 = (ip6_t *)((char *)ic6 + ICMPERR_ICMPHLEN); 4471255332Scy if (fin->fin_plen < sizeof(*oip6)) { 4472255332Scy SBUMPD(ipf_state_stats, iss_icmp_short); 447360854Sdarrenr return NULL; 4474255332Scy } 447560854Sdarrenr 4476170268Sdarrenr bcopy((char *)fin, (char *)&ofin, sizeof(*fin)); 4477145522Sdarrenr ofin.fin_v = 6; 4478145522Sdarrenr ofin.fin_ifp = fin->fin_ifp; 4479145522Sdarrenr ofin.fin_out = !fin->fin_out; 4480145522Sdarrenr ofin.fin_m = NULL; /* if dereferenced, panic XXX */ 4481145522Sdarrenr ofin.fin_mp = NULL; /* if dereferenced, panic XXX */ 448295563Sdarrenr 4483130886Sdarrenr /* 4484130886Sdarrenr * We make a fin entry to be able to feed it to 4485130886Sdarrenr * matchsrcdst. Note that not all fields are necessary 4486130886Sdarrenr * but this is the cleanest way. Note further we fill 4487130886Sdarrenr * in fin_mp such that if someone uses it we'll get 4488255332Scy * a kernel panic. ipf_matchsrcdst does not use this. 4489130886Sdarrenr * 4490145522Sdarrenr * watch out here, as ip is in host order and oip6 in network 4491130886Sdarrenr * order. Any change we make must be undone afterwards. 4492130886Sdarrenr */ 4493145522Sdarrenr savelen = oip6->ip6_plen; 4494255332Scy oip6->ip6_plen = htons(fin->fin_dlen - ICMPERR_ICMPHLEN); 4495145522Sdarrenr ofin.fin_flx = FI_NOCKSUM; 4496145522Sdarrenr ofin.fin_ip = (ip_t *)oip6; 4497255332Scy (void) ipf_makefrip(sizeof(*oip6), (ip_t *)oip6, &ofin); 4498145522Sdarrenr ofin.fin_flx &= ~(FI_BAD|FI_SHORT); 4499145522Sdarrenr oip6->ip6_plen = savelen; 4500255332Scy pr = ofin.fin_p; 450195563Sdarrenr 4502255332Scy /* 4503255332Scy * an ICMP error can never generate an ICMP error in response. 4504255332Scy */ 4505255332Scy if (ofin.fin_flx & FI_ICMPERR) { 4506255332Scy DT1(iss_icmp6_icmperr, fr_info_t *, &ofin); 4507255332Scy SBUMP(ipf_state_stats.iss_icmp6_icmperr); 4508255332Scy return NULL; 4509255332Scy } 4510255332Scy 4511145522Sdarrenr if (oip6->ip6_nxt == IPPROTO_ICMPV6) { 4512255332Scy oic = ofin.fin_dp; 451360854Sdarrenr /* 4514130886Sdarrenr * an ICMP error can only be generated as a result of an 451560854Sdarrenr * ICMP query, not as the response on an ICMP error 451660854Sdarrenr * 451760854Sdarrenr * XXX theoretically ICMP_ECHOREP and the other reply's are 451860854Sdarrenr * ICMP query's as well, but adding them here seems strange XXX 451960854Sdarrenr */ 4520255332Scy if (!(oic->icmp6_type & ICMP6_INFOMSG_MASK)) { 4521255332Scy DT1(iss_icmp6_notinfo, fr_info_t *, &ofin); 4522255332Scy SBUMP(ipf_state_stats.iss_icmp6_notinfo); 4523255332Scy return NULL; 4524255332Scy } 452560854Sdarrenr 452660854Sdarrenr /* 452760854Sdarrenr * perform a lookup of the ICMP packet in the state table 452860854Sdarrenr */ 4529145522Sdarrenr hv = (pr = oip6->ip6_nxt); 4530145522Sdarrenr src.in6 = oip6->ip6_src; 453160854Sdarrenr hv += src.in4.s_addr; 4532145522Sdarrenr dst.in6 = oip6->ip6_dst; 453360854Sdarrenr hv += dst.in4.s_addr; 453460854Sdarrenr hv += oic->icmp6_id; 453560854Sdarrenr hv += oic->icmp6_seq; 4536145522Sdarrenr hv = DOUBLE_HASH(hv); 453760854Sdarrenr 4538255332Scy READ_ENTER(&softc->ipf_state); 4539255332Scy for (isp = &softs->ipf_state_table[hv]; 4540255332Scy ((is = *isp) != NULL); ) { 4541145522Sdarrenr ic = &is->is_icmp; 4542145522Sdarrenr isp = &is->is_hnext; 454360854Sdarrenr if ((is->is_p == pr) && 4544145522Sdarrenr !(is->is_pass & FR_NOICMPERR) && 4545145522Sdarrenr (oic->icmp6_id == ic->ici_id) && 4546145522Sdarrenr (oic->icmp6_seq == ic->ici_seq) && 4547255332Scy (is = ipf_matchsrcdst(&ofin, is, &src, 4548145522Sdarrenr &dst, NULL, FI_ICMPCMP))) { 454960854Sdarrenr /* 455060854Sdarrenr * in the state table ICMP query's are stored 455160854Sdarrenr * with the type of the corresponding ICMP 455260854Sdarrenr * response. Correct here 455360854Sdarrenr */ 4554145522Sdarrenr if (((ic->ici_type == ICMP6_ECHO_REPLY) && 455560854Sdarrenr (oic->icmp6_type == ICMP6_ECHO_REQUEST)) || 4556145522Sdarrenr (ic->ici_type - 1 == oic->icmp6_type )) { 4557255332Scy if (!ipf_allowstateicmp(fin, is, &src)) 4558255332Scy return is; 455960854Sdarrenr } 456060854Sdarrenr } 4561145522Sdarrenr } 4562255332Scy RWLOCK_EXIT(&softc->ipf_state); 4563255332Scy SBUMPD(ipf_state_stats, iss_icmp6_miss); 456460854Sdarrenr return NULL; 456595563Sdarrenr } 456660854Sdarrenr 4567145522Sdarrenr hv = (pr = oip6->ip6_nxt); 4568145522Sdarrenr src.in6 = oip6->ip6_src; 4569145522Sdarrenr hv += src.i6[0]; 457095418Sdarrenr hv += src.i6[1]; 457195418Sdarrenr hv += src.i6[2]; 457295418Sdarrenr hv += src.i6[3]; 4573145522Sdarrenr dst.in6 = oip6->ip6_dst; 4574145522Sdarrenr hv += dst.i6[0]; 457595418Sdarrenr hv += dst.i6[1]; 457695418Sdarrenr hv += dst.i6[2]; 457795418Sdarrenr hv += dst.i6[3]; 4578130886Sdarrenr 4579255332Scy tcp = NULL; 4580255332Scy 4581255332Scy switch (oip6->ip6_nxt) 4582255332Scy { 4583255332Scy case IPPROTO_TCP : 4584255332Scy case IPPROTO_UDP : 4585145522Sdarrenr tcp = (tcphdr_t *)(oip6 + 1); 4586145522Sdarrenr dport = tcp->th_dport; 4587145522Sdarrenr sport = tcp->th_sport; 4588145522Sdarrenr hv += dport; 4589145522Sdarrenr hv += sport; 4590255332Scy break; 4591255332Scy 4592255332Scy case IPPROTO_ICMPV6 : 4593255332Scy oic = (struct icmp6_hdr *)(oip6 + 1); 4594255332Scy hv += oic->icmp6_id; 4595255332Scy hv += oic->icmp6_seq; 4596255332Scy break; 4597255332Scy 4598255332Scy default : 4599255332Scy break; 4600255332Scy } 4601255332Scy 4602145522Sdarrenr hv = DOUBLE_HASH(hv); 4603145522Sdarrenr 4604255332Scy READ_ENTER(&softc->ipf_state); 4605255332Scy for (isp = &softs->ipf_state_table[hv]; ((is = *isp) != NULL); ) { 4606145522Sdarrenr isp = &is->is_hnext; 460760854Sdarrenr /* 460860854Sdarrenr * Only allow this icmp though if the 460960854Sdarrenr * encapsulated packet was allowed through the 461060854Sdarrenr * other way around. Note that the minimal amount 461160854Sdarrenr * of info present does not allow for checking against 461260854Sdarrenr * tcp internals such as seq and ack numbers. 461360854Sdarrenr */ 4614145522Sdarrenr if ((is->is_p != pr) || (is->is_v != 6) || 4615145522Sdarrenr (is->is_pass & FR_NOICMPERR)) 4616145522Sdarrenr continue; 4617255332Scy is = ipf_matchsrcdst(&ofin, is, &src, &dst, tcp, FI_ICMPCMP); 4618255332Scy if ((is != NULL) && (ipf_allowstateicmp(fin, is, &src) == 0)) 4619145522Sdarrenr return is; 462060854Sdarrenr } 4621255332Scy RWLOCK_EXIT(&softc->ipf_state); 4622255332Scy SBUMPD(ipf_state_stats, iss_icmp_miss); 462360854Sdarrenr return NULL; 462457096Sguido} 462560854Sdarrenr#endif 4626145522Sdarrenr 4627145522Sdarrenr 4628145522Sdarrenr/* ------------------------------------------------------------------------ */ 4629255332Scy/* Function: ipf_sttab_init */ 4630145522Sdarrenr/* Returns: Nil */ 4631255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 4632255332Scy/* tqp(I) - pointer to an array of timeout queues for TCP */ 4633145522Sdarrenr/* */ 4634145522Sdarrenr/* Initialise the array of timeout queues for TCP. */ 4635145522Sdarrenr/* ------------------------------------------------------------------------ */ 4636255332Scyvoid 4637255332Scyipf_sttab_init(softc, tqp) 4638255332Scy ipf_main_softc_t *softc; 4639255332Scy ipftq_t *tqp; 4640145522Sdarrenr{ 4641145522Sdarrenr int i; 4642145522Sdarrenr 4643145522Sdarrenr for (i = IPF_TCP_NSTATES - 1; i >= 0; i--) { 4644255332Scy IPFTQ_INIT(&tqp[i], 0, "ipftq tcp tab"); 4645145522Sdarrenr tqp[i].ifq_next = tqp + i + 1; 4646145522Sdarrenr } 4647145522Sdarrenr tqp[IPF_TCP_NSTATES - 1].ifq_next = NULL; 4648255332Scy tqp[IPF_TCPS_CLOSED].ifq_ttl = softc->ipf_tcpclosed; 4649255332Scy tqp[IPF_TCPS_LISTEN].ifq_ttl = softc->ipf_tcptimeout; 4650255332Scy tqp[IPF_TCPS_SYN_SENT].ifq_ttl = softc->ipf_tcpsynsent; 4651255332Scy tqp[IPF_TCPS_SYN_RECEIVED].ifq_ttl = softc->ipf_tcpsynrecv; 4652255332Scy tqp[IPF_TCPS_ESTABLISHED].ifq_ttl = softc->ipf_tcpidletimeout; 4653255332Scy tqp[IPF_TCPS_CLOSE_WAIT].ifq_ttl = softc->ipf_tcphalfclosed; 4654255332Scy tqp[IPF_TCPS_FIN_WAIT_1].ifq_ttl = softc->ipf_tcphalfclosed; 4655255332Scy tqp[IPF_TCPS_CLOSING].ifq_ttl = softc->ipf_tcptimeout; 4656255332Scy tqp[IPF_TCPS_LAST_ACK].ifq_ttl = softc->ipf_tcplastack; 4657255332Scy tqp[IPF_TCPS_FIN_WAIT_2].ifq_ttl = softc->ipf_tcpclosewait; 4658255332Scy tqp[IPF_TCPS_TIME_WAIT].ifq_ttl = softc->ipf_tcptimewait; 4659255332Scy tqp[IPF_TCPS_HALF_ESTAB].ifq_ttl = softc->ipf_tcptimeout; 4660145522Sdarrenr} 4661145522Sdarrenr 4662145522Sdarrenr 4663145522Sdarrenr/* ------------------------------------------------------------------------ */ 4664255332Scy/* Function: ipf_sttab_destroy */ 4665145522Sdarrenr/* Returns: Nil */ 4666145522Sdarrenr/* Parameters: tqp(I) - pointer to an array of timeout queues for TCP */ 4667145522Sdarrenr/* */ 4668145522Sdarrenr/* Do whatever is necessary to "destroy" each of the entries in the array */ 4669145522Sdarrenr/* of timeout queues for TCP. */ 4670145522Sdarrenr/* ------------------------------------------------------------------------ */ 4671255332Scyvoid 4672255332Scyipf_sttab_destroy(tqp) 4673255332Scy ipftq_t *tqp; 4674145522Sdarrenr{ 4675145522Sdarrenr int i; 4676145522Sdarrenr 4677145522Sdarrenr for (i = IPF_TCP_NSTATES - 1; i >= 0; i--) 4678145522Sdarrenr MUTEX_DESTROY(&tqp[i].ifq_lock); 4679145522Sdarrenr} 4680145522Sdarrenr 4681145522Sdarrenr 4682145522Sdarrenr/* ------------------------------------------------------------------------ */ 4683255332Scy/* Function: ipf_state_deref */ 4684145522Sdarrenr/* Returns: Nil */ 4685255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 4686255332Scy/* isp(I) - pointer to pointer to state table entry */ 4687145522Sdarrenr/* */ 4688145522Sdarrenr/* Decrement the reference counter for this state table entry and free it */ 4689145522Sdarrenr/* if there are no more things using it. */ 4690145522Sdarrenr/* */ 4691172776Sdarrenr/* This function is only called when cleaning up after increasing is_ref by */ 4692172776Sdarrenr/* one earlier in the 'code path' so if is_ref is 1 when entering, we do */ 4693172776Sdarrenr/* have an orphan, otherwise not. However there is a possible race between */ 4694172776Sdarrenr/* the entry being deleted via flushing with an ioctl call (that calls the */ 4695172776Sdarrenr/* delete function directly) and the tail end of packet processing so we */ 4696172776Sdarrenr/* need to grab is_lock before doing the check to synchronise the two code */ 4697172776Sdarrenr/* paths. */ 4698172776Sdarrenr/* */ 4699145522Sdarrenr/* When operating in userland (ipftest), we have no timers to clear a state */ 4700145522Sdarrenr/* entry. Therefore, we make a few simple tests before deleting an entry */ 4701145522Sdarrenr/* outright. We compare states on each side looking for a combination of */ 4702145522Sdarrenr/* TIME_WAIT (should really be FIN_WAIT_2?) and LAST_ACK. Then we factor */ 4703145522Sdarrenr/* in packet direction with the interface list to make sure we don't */ 4704145522Sdarrenr/* prematurely delete an entry on a final inbound packet that's we're also */ 4705145522Sdarrenr/* supposed to route elsewhere. */ 4706145522Sdarrenr/* */ 4707145522Sdarrenr/* Internal parameters: */ 4708145522Sdarrenr/* state[0] = state of source (host that initiated connection) */ 4709145522Sdarrenr/* state[1] = state of dest (host that accepted the connection) */ 4710145522Sdarrenr/* */ 4711145522Sdarrenr/* dir == 0 : a packet from source to dest */ 4712145522Sdarrenr/* dir == 1 : a packet from dest to source */ 4713145522Sdarrenr/* ------------------------------------------------------------------------ */ 4714255332Scyvoid 4715255332Scyipf_state_deref(softc, isp) 4716255332Scy ipf_main_softc_t *softc; 4717255332Scy ipstate_t **isp; 4718145522Sdarrenr{ 4719255332Scy ipstate_t *is = *isp; 4720145522Sdarrenr 4721145522Sdarrenr is = *isp; 4722145522Sdarrenr *isp = NULL; 4723172776Sdarrenr 4724172776Sdarrenr MUTEX_ENTER(&is->is_lock); 4725172776Sdarrenr if (is->is_ref > 1) { 4726172776Sdarrenr is->is_ref--; 4727172776Sdarrenr MUTEX_EXIT(&is->is_lock); 4728145522Sdarrenr#ifndef _KERNEL 4729172776Sdarrenr if ((is->is_sti.tqe_state[0] > IPF_TCPS_ESTABLISHED) || 4730255332Scy (is->is_sti.tqe_state[1] > IPF_TCPS_ESTABLISHED)) { 4731255332Scy ipf_state_del(softc, is, ISL_EXPIRE); 4732172776Sdarrenr } 4733145522Sdarrenr#endif 4734172776Sdarrenr return; 4735145522Sdarrenr } 4736172776Sdarrenr MUTEX_EXIT(&is->is_lock); 4737172776Sdarrenr 4738255332Scy WRITE_ENTER(&softc->ipf_state); 4739255332Scy ipf_state_del(softc, is, ISL_ORPHAN); 4740255332Scy RWLOCK_EXIT(&softc->ipf_state); 4741145522Sdarrenr} 4742145522Sdarrenr 4743145522Sdarrenr 4744145522Sdarrenr/* ------------------------------------------------------------------------ */ 4745255332Scy/* Function: ipf_state_setqueue */ 4746145522Sdarrenr/* Returns: Nil */ 4747255332Scy/* Parameters: softc(I) - pointer to soft context main structure */ 4748255332Scy/* is(I) - pointer to state structure */ 4749255332Scy/* rev(I) - forward(0) or reverse(1) direction */ 4750145522Sdarrenr/* Locks: ipf_state (read or write) */ 4751145522Sdarrenr/* */ 4752145522Sdarrenr/* Put the state entry on its default queue entry, using rev as a helped in */ 4753145522Sdarrenr/* determining which queue it should be placed on. */ 4754145522Sdarrenr/* ------------------------------------------------------------------------ */ 4755255332Scyvoid 4756255332Scyipf_state_setqueue(softc, is, rev) 4757255332Scy ipf_main_softc_t *softc; 4758255332Scy ipstate_t *is; 4759255332Scy int rev; 4760145522Sdarrenr{ 4761255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 4762145522Sdarrenr ipftq_t *oifq, *nifq; 4763145522Sdarrenr 4764145522Sdarrenr if ((is->is_sti.tqe_flags & TQE_RULEBASED) != 0) 4765145522Sdarrenr nifq = is->is_tqehead[rev]; 4766145522Sdarrenr else 4767145522Sdarrenr nifq = NULL; 4768145522Sdarrenr 4769145522Sdarrenr if (nifq == NULL) { 4770145522Sdarrenr switch (is->is_p) 4771145522Sdarrenr { 4772145522Sdarrenr#ifdef USE_INET6 4773145522Sdarrenr case IPPROTO_ICMPV6 : 4774145522Sdarrenr if (rev == 1) 4775255332Scy nifq = &softs->ipf_state_icmpacktq; 4776145522Sdarrenr else 4777255332Scy nifq = &softs->ipf_state_icmptq; 4778145522Sdarrenr break; 4779145522Sdarrenr#endif 4780145522Sdarrenr case IPPROTO_ICMP : 4781145522Sdarrenr if (rev == 1) 4782255332Scy nifq = &softs->ipf_state_icmpacktq; 4783145522Sdarrenr else 4784255332Scy nifq = &softs->ipf_state_icmptq; 4785145522Sdarrenr break; 4786145522Sdarrenr case IPPROTO_TCP : 4787255332Scy nifq = softs->ipf_state_tcptq + is->is_state[rev]; 4788145522Sdarrenr break; 4789145522Sdarrenr 4790145522Sdarrenr case IPPROTO_UDP : 4791145522Sdarrenr if (rev == 1) 4792255332Scy nifq = &softs->ipf_state_udpacktq; 4793145522Sdarrenr else 4794255332Scy nifq = &softs->ipf_state_udptq; 4795145522Sdarrenr break; 4796145522Sdarrenr 4797145522Sdarrenr default : 4798255332Scy nifq = &softs->ipf_state_iptq; 4799145522Sdarrenr break; 4800145522Sdarrenr } 4801145522Sdarrenr } 4802145522Sdarrenr 4803145522Sdarrenr oifq = is->is_sti.tqe_ifq; 4804145522Sdarrenr /* 4805145522Sdarrenr * If it's currently on a timeout queue, move it from one queue to 4806145522Sdarrenr * another, else put it on the end of the newly determined queue. 4807145522Sdarrenr */ 4808145522Sdarrenr if (oifq != NULL) 4809255332Scy ipf_movequeue(softc->ipf_ticks, &is->is_sti, oifq, nifq); 4810145522Sdarrenr else 4811255332Scy ipf_queueappend(softc->ipf_ticks, &is->is_sti, nifq, is); 4812145522Sdarrenr return; 4813145522Sdarrenr} 4814170268Sdarrenr 4815170268Sdarrenr 4816170268Sdarrenr/* ------------------------------------------------------------------------ */ 4817255332Scy/* Function: ipf_state_iter */ 4818170268Sdarrenr/* Returns: int - 0 == success, else error */ 4819255332Scy/* Parameters: softc(I) - pointer to main soft context */ 4820255332Scy/* token(I) - pointer to ipftoken structure */ 4821170268Sdarrenr/* itp(I) - pointer to ipfgeniter structure */ 4822255332Scy/* obj(I) - pointer to data description structure */ 4823170268Sdarrenr/* */ 4824170268Sdarrenr/* This function handles the SIOCGENITER ioctl for the state tables and */ 4825255332Scy/* walks through the list of entries in the state table list (softs->ipf_state_list.) */ 4826170268Sdarrenr/* ------------------------------------------------------------------------ */ 4827255332Scystatic int 4828255332Scyipf_state_iter(softc, token, itp, obj) 4829255332Scy ipf_main_softc_t *softc; 4830255332Scy ipftoken_t *token; 4831255332Scy ipfgeniter_t *itp; 4832255332Scy ipfobj_t *obj; 4833170268Sdarrenr{ 4834255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 4835170268Sdarrenr ipstate_t *is, *next, zero; 4836255332Scy int error; 4837170268Sdarrenr 4838255332Scy if (itp->igi_data == NULL) { 4839255332Scy IPFERROR(100026); 4840170268Sdarrenr return EFAULT; 4841255332Scy } 4842170268Sdarrenr 4843255332Scy if (itp->igi_nitems < 1) { 4844255332Scy IPFERROR(100027); 4845170268Sdarrenr return ENOSPC; 4846255332Scy } 4847170268Sdarrenr 4848255332Scy if (itp->igi_type != IPFGENITER_STATE) { 4849255332Scy IPFERROR(100028); 4850170268Sdarrenr return EINVAL; 4851255332Scy } 4852170268Sdarrenr 4853170268Sdarrenr is = token->ipt_data; 4854170268Sdarrenr if (is == (void *)-1) { 4855255332Scy IPFERROR(100029); 4856170268Sdarrenr return ESRCH; 4857170268Sdarrenr } 4858170268Sdarrenr 4859170268Sdarrenr error = 0; 4860255332Scy obj->ipfo_type = IPFOBJ_IPSTATE; 4861255332Scy obj->ipfo_size = sizeof(ipstate_t); 4862170268Sdarrenr 4863255332Scy READ_ENTER(&softc->ipf_state); 4864255332Scy 4865255332Scy is = token->ipt_data; 4866170268Sdarrenr if (is == NULL) { 4867255332Scy next = softs->ipf_state_list; 4868170268Sdarrenr } else { 4869170268Sdarrenr next = is->is_next; 4870170268Sdarrenr } 4871170268Sdarrenr 4872255332Scy /* 4873255332Scy * If we find a state entry to use, bump its reference count so that 4874255332Scy * it can be used for is_next when we come back. 4875255332Scy */ 4876255332Scy if (next != NULL) { 4877255332Scy MUTEX_ENTER(&next->is_lock); 4878255332Scy next->is_ref++; 4879255332Scy MUTEX_EXIT(&next->is_lock); 4880255332Scy token->ipt_data = next; 4881255332Scy } else { 4882255332Scy bzero(&zero, sizeof(zero)); 4883255332Scy next = &zero; 4884255332Scy token->ipt_data = NULL; 4885255332Scy } 4886255332Scy if (next->is_next == NULL) 4887255332Scy ipf_token_mark_complete(token); 4888170268Sdarrenr 4889255332Scy RWLOCK_EXIT(&softc->ipf_state); 4890170268Sdarrenr 4891255332Scy obj->ipfo_ptr = itp->igi_data; 4892255332Scy error = ipf_outobjk(softc, obj, next); 4893255332Scy if (is != NULL) 4894255332Scy ipf_state_deref(softc, &is); 4895172776Sdarrenr 4896170268Sdarrenr return error; 4897170268Sdarrenr} 4898170268Sdarrenr 4899170268Sdarrenr 4900170268Sdarrenr/* ------------------------------------------------------------------------ */ 4901255332Scy/* Function: ipf_state_gettable */ 4902170268Sdarrenr/* Returns: int - 0 = success, else error */ 4903255332Scy/* Parameters: softc(I) - pointer to main soft context */ 4904255332Scy/* softs(I) - pointer to state context structure */ 4905255332Scy/* data(I) - pointer to ioctl data */ 4906170268Sdarrenr/* */ 4907170268Sdarrenr/* This function handles ioctl requests for tables of state information. */ 4908170268Sdarrenr/* At present the only table it deals with is the hash bucket statistics. */ 4909170268Sdarrenr/* ------------------------------------------------------------------------ */ 4910255332Scystatic int 4911255332Scyipf_state_gettable(softc, softs, data) 4912255332Scy ipf_main_softc_t *softc; 4913255332Scy ipf_state_softc_t *softs; 4914255332Scy char *data; 4915170268Sdarrenr{ 4916170268Sdarrenr ipftable_t table; 4917170268Sdarrenr int error; 4918170268Sdarrenr 4919255332Scy error = ipf_inobj(softc, data, NULL, &table, IPFOBJ_GTABLE); 4920170268Sdarrenr if (error != 0) 4921170268Sdarrenr return error; 4922170268Sdarrenr 4923255332Scy if (table.ita_type != IPFTABLE_BUCKETS) { 4924255332Scy IPFERROR(100031); 4925170268Sdarrenr return EINVAL; 4926255332Scy } 4927170268Sdarrenr 4928255332Scy error = COPYOUT(softs->ipf_state_stats.iss_bucketlen, table.ita_table, 4929255332Scy softs->ipf_state_size * sizeof(u_int)); 4930255332Scy if (error != 0) { 4931255332Scy IPFERROR(100032); 4932170268Sdarrenr error = EFAULT; 4933255332Scy } 4934170268Sdarrenr return error; 4935170268Sdarrenr} 4936255332Scy 4937255332Scy 4938255332Scy/* ------------------------------------------------------------------------ */ 4939255332Scy/* Function: ipf_state_setpending */ 4940255332Scy/* Returns: Nil */ 4941255332Scy/* Parameters: softc(I) - pointer to main soft context */ 4942255332Scy/* is(I) - pointer to state structure */ 4943255332Scy/* Locks: ipf_state (read or write) */ 4944255332Scy/* */ 4945255332Scy/* Put the state entry on to the pending queue - this queue has a very */ 4946255332Scy/* short lifetime where items are put that can't be deleted straight away */ 4947255332Scy/* because of locking issues but we want to delete them ASAP, anyway. */ 4948255332Scy/* ------------------------------------------------------------------------ */ 4949255332Scyvoid 4950255332Scyipf_state_setpending(softc, is) 4951255332Scy ipf_main_softc_t *softc; 4952255332Scy ipstate_t *is; 4953255332Scy{ 4954255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 4955255332Scy ipftq_t *oifq; 4956255332Scy 4957255332Scy oifq = is->is_sti.tqe_ifq; 4958255332Scy if (oifq != NULL) 4959255332Scy ipf_movequeue(softc->ipf_ticks, &is->is_sti, oifq, 4960255332Scy &softs->ipf_state_pending); 4961255332Scy else 4962255332Scy ipf_queueappend(softc->ipf_ticks, &is->is_sti, 4963255332Scy &softs->ipf_state_pending, is); 4964255332Scy 4965255332Scy MUTEX_ENTER(&is->is_lock); 4966255332Scy if (is->is_me != NULL) { 4967255332Scy *is->is_me = NULL; 4968255332Scy is->is_me = NULL; 4969255332Scy is->is_ref--; 4970255332Scy } 4971255332Scy MUTEX_EXIT(&is->is_lock); 4972255332Scy} 4973255332Scy 4974255332Scy 4975255332Scy/* ------------------------------------------------------------------------ */ 4976255332Scy/* Function: ipf_state_matchflush */ 4977255332Scy/* Returns: Nil */ 4978255332Scy/* Parameters: softc(I) - pointer to main soft context */ 4979255332Scy/* data(I) - pointer to state structure */ 4980255332Scy/* Locks: ipf_state (read or write) */ 4981255332Scy/* */ 4982255332Scy/* Flush all entries from the list of state entries that match the */ 4983255332Scy/* properties in the array loaded. */ 4984255332Scy/* ------------------------------------------------------------------------ */ 4985255332Scyint 4986255332Scyipf_state_matchflush(softc, data) 4987255332Scy ipf_main_softc_t *softc; 4988255332Scy caddr_t data; 4989255332Scy{ 4990255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 4991255332Scy int *array, flushed, error; 4992255332Scy ipstate_t *state, *statenext; 4993255332Scy ipfobj_t obj; 4994255332Scy 4995255332Scy error = ipf_matcharray_load(softc, data, &obj, &array); 4996255332Scy if (error != 0) 4997255332Scy return error; 4998255332Scy 4999255332Scy flushed = 0; 5000255332Scy 5001255332Scy for (state = softs->ipf_state_list; state != NULL; state = statenext) { 5002255332Scy statenext = state->is_next; 5003255332Scy if (ipf_state_matcharray(state, array, softc->ipf_ticks) == 0) { 5004255332Scy ipf_state_del(softc, state, ISL_FLUSH); 5005255332Scy flushed++; 5006255332Scy } 5007255332Scy } 5008255332Scy 5009255332Scy obj.ipfo_retval = flushed; 5010255332Scy error = BCOPYOUT(&obj, data, sizeof(obj)); 5011255332Scy 5012255332Scy KFREES(array, array[0] * sizeof(*array)); 5013255332Scy 5014255332Scy return error; 5015255332Scy} 5016255332Scy 5017255332Scy 5018255332Scy/* ------------------------------------------------------------------------ */ 5019255332Scy/* Function: ipf_state_matcharray */ 5020255332Scy/* Returns: int - 0 = no match, 1 = match */ 5021255332Scy/* Parameters: state(I) - pointer to state structure */ 5022255332Scy/* array(I) - pointer to ipf matching expression */ 5023255332Scy/* ticks(I) - current value of ipfilter tick timer */ 5024255332Scy/* Locks: ipf_state (read or write) */ 5025255332Scy/* */ 5026255332Scy/* Compare a state entry with the match array passed in and return a value */ 5027255332Scy/* to indicate whether or not the matching was successful. */ 5028255332Scy/* ------------------------------------------------------------------------ */ 5029255332Scystatic int 5030255332Scyipf_state_matcharray(state, array, ticks) 5031255332Scy ipstate_t *state; 5032255332Scy int *array; 5033255332Scy u_long ticks; 5034255332Scy{ 5035255332Scy int i, n, *x, rv, p; 5036255332Scy ipfexp_t *e; 5037255332Scy 5038255332Scy rv = 0; 5039255332Scy n = array[0]; 5040255332Scy x = array + 1; 5041255332Scy 5042255332Scy for (; n > 0; x += 3 + x[3], rv = 0) { 5043255332Scy e = (ipfexp_t *)x; 5044255332Scy n -= e->ipfe_size; 5045255332Scy if (x[0] == IPF_EXP_END) 5046255332Scy break; 5047255332Scy 5048255332Scy /* 5049255332Scy * If we need to match the protocol and that doesn't match, 5050255332Scy * don't even both with the instruction array. 5051255332Scy */ 5052255332Scy p = e->ipfe_cmd >> 16; 5053255332Scy if ((p != 0) && (p != state->is_p)) 5054255332Scy break; 5055255332Scy 5056255332Scy switch (e->ipfe_cmd) 5057255332Scy { 5058255332Scy case IPF_EXP_IP_PR : 5059255332Scy for (i = 0; !rv && i < e->ipfe_narg; i++) { 5060255332Scy rv |= (state->is_p == e->ipfe_arg0[i]); 5061255332Scy } 5062255332Scy break; 5063255332Scy 5064255332Scy case IPF_EXP_IP_SRCADDR : 5065255332Scy if (state->is_v != 4) 5066255332Scy break; 5067255332Scy for (i = 0; !rv && i < e->ipfe_narg; i++) { 5068255332Scy rv |= ((state->is_saddr & 5069255332Scy e->ipfe_arg0[i * 2 + 1]) == 5070255332Scy e->ipfe_arg0[i * 2]); 5071255332Scy } 5072255332Scy break; 5073255332Scy 5074255332Scy case IPF_EXP_IP_DSTADDR : 5075255332Scy if (state->is_v != 4) 5076255332Scy break; 5077255332Scy for (i = 0; !rv && i < e->ipfe_narg; i++) { 5078255332Scy rv |= ((state->is_daddr & 5079255332Scy e->ipfe_arg0[i * 2 + 1]) == 5080255332Scy e->ipfe_arg0[i * 2]); 5081255332Scy } 5082255332Scy break; 5083255332Scy 5084255332Scy case IPF_EXP_IP_ADDR : 5085255332Scy if (state->is_v != 4) 5086255332Scy break; 5087255332Scy for (i = 0; !rv && i < e->ipfe_narg; i++) { 5088255332Scy rv |= ((state->is_saddr & 5089255332Scy e->ipfe_arg0[i * 2 + 1]) == 5090255332Scy e->ipfe_arg0[i * 2]) || 5091255332Scy ((state->is_daddr & 5092255332Scy e->ipfe_arg0[i * 2 + 1]) == 5093255332Scy e->ipfe_arg0[i * 2]); 5094255332Scy } 5095255332Scy break; 5096255332Scy 5097255332Scy#ifdef USE_INET6 5098255332Scy case IPF_EXP_IP6_SRCADDR : 5099255332Scy if (state->is_v != 6) 5100255332Scy break; 5101255332Scy for (i = 0; !rv && i < x[3]; i++) { 5102255332Scy rv |= IP6_MASKEQ(&state->is_src.in6, 5103255332Scy &e->ipfe_arg0[i * 8 + 4], 5104255332Scy &e->ipfe_arg0[i * 8]); 5105255332Scy } 5106255332Scy break; 5107255332Scy 5108255332Scy case IPF_EXP_IP6_DSTADDR : 5109255332Scy if (state->is_v != 6) 5110255332Scy break; 5111255332Scy for (i = 0; !rv && i < x[3]; i++) { 5112255332Scy rv |= IP6_MASKEQ(&state->is_dst.in6, 5113255332Scy &e->ipfe_arg0[i * 8 + 4], 5114255332Scy &e->ipfe_arg0[i * 8]); 5115255332Scy } 5116255332Scy break; 5117255332Scy 5118255332Scy case IPF_EXP_IP6_ADDR : 5119255332Scy if (state->is_v != 6) 5120255332Scy break; 5121255332Scy for (i = 0; !rv && i < x[3]; i++) { 5122255332Scy rv |= IP6_MASKEQ(&state->is_src.in6, 5123255332Scy &e->ipfe_arg0[i * 8 + 4], 5124255332Scy &e->ipfe_arg0[i * 8]) || 5125255332Scy IP6_MASKEQ(&state->is_dst.in6, 5126255332Scy &e->ipfe_arg0[i * 8 + 4], 5127255332Scy &e->ipfe_arg0[i * 8]); 5128255332Scy } 5129255332Scy break; 5130255332Scy#endif 5131255332Scy 5132255332Scy case IPF_EXP_UDP_PORT : 5133255332Scy case IPF_EXP_TCP_PORT : 5134255332Scy for (i = 0; !rv && i < e->ipfe_narg; i++) { 5135255332Scy rv |= (state->is_sport == e->ipfe_arg0[i]) || 5136255332Scy (state->is_dport == e->ipfe_arg0[i]); 5137255332Scy } 5138255332Scy break; 5139255332Scy 5140255332Scy case IPF_EXP_UDP_SPORT : 5141255332Scy case IPF_EXP_TCP_SPORT : 5142255332Scy for (i = 0; !rv && i < e->ipfe_narg; i++) { 5143255332Scy rv |= (state->is_sport == e->ipfe_arg0[i]); 5144255332Scy } 5145255332Scy break; 5146255332Scy 5147255332Scy case IPF_EXP_UDP_DPORT : 5148255332Scy case IPF_EXP_TCP_DPORT : 5149255332Scy for (i = 0; !rv && i < e->ipfe_narg; i++) { 5150255332Scy rv |= (state->is_dport == e->ipfe_arg0[i]); 5151255332Scy } 5152255332Scy break; 5153255332Scy 5154255332Scy case IPF_EXP_TCP_STATE : 5155255332Scy for (i = 0; !rv && i < e->ipfe_narg; i++) { 5156255332Scy rv |= (state->is_state[0] == e->ipfe_arg0[i]) || 5157255332Scy (state->is_state[1] == e->ipfe_arg0[i]); 5158255332Scy } 5159255332Scy break; 5160255332Scy 5161255332Scy case IPF_EXP_IDLE_GT : 5162255332Scy rv |= (ticks - state->is_touched > e->ipfe_arg0[0]); 5163255332Scy break; 5164255332Scy } 5165255332Scy 5166255332Scy /* 5167255332Scy * Factor in doing a negative match. 5168255332Scy */ 5169255332Scy rv ^= e->ipfe_not; 5170255332Scy 5171255332Scy if (rv == 0) 5172255332Scy break; 5173255332Scy } 5174255332Scy 5175255332Scy return rv; 5176255332Scy} 5177255332Scy 5178255332Scy 5179255332Scy/* ------------------------------------------------------------------------ */ 5180255332Scy/* Function: ipf_state_settimeout */ 5181255332Scy/* Returns: int 0 = success, else failure */ 5182255332Scy/* Parameters: softc(I) - pointer to main soft context */ 5183255332Scy/* t(I) - pointer to tuneable being changed */ 5184255332Scy/* p(I) - pointer to the new value */ 5185255332Scy/* */ 5186255332Scy/* Sets a timeout value for one of the many timeout queues. We find the */ 5187255332Scy/* correct queue using a somewhat manual process of comparing the timeout */ 5188255332Scy/* names for each specific value available and calling ipf_apply_timeout on */ 5189255332Scy/* that queue so that all of the items on it are updated accordingly. */ 5190255332Scy/* ------------------------------------------------------------------------ */ 5191255332Scyint 5192255332Scyipf_state_settimeout(softc, t, p) 5193255332Scy struct ipf_main_softc_s *softc; 5194255332Scy ipftuneable_t *t; 5195255332Scy ipftuneval_t *p; 5196255332Scy{ 5197255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 5198255332Scy 5199255332Scy /* 5200255332Scy * In case there is nothing to do... 5201255332Scy */ 5202255332Scy if (*t->ipft_pint == p->ipftu_int) 5203255332Scy return 0; 5204255332Scy 5205255332Scy if (!strncmp(t->ipft_name, "tcp_", 4)) 5206255332Scy return ipf_settimeout_tcp(t, p, softs->ipf_state_tcptq); 5207255332Scy 5208255332Scy if (!strcmp(t->ipft_name, "udp_timeout")) { 5209255332Scy ipf_apply_timeout(&softs->ipf_state_udptq, p->ipftu_int); 5210255332Scy } else if (!strcmp(t->ipft_name, "udp_ack_timeout")) { 5211255332Scy ipf_apply_timeout(&softs->ipf_state_udpacktq, p->ipftu_int); 5212255332Scy } else if (!strcmp(t->ipft_name, "icmp_timeout")) { 5213255332Scy ipf_apply_timeout(&softs->ipf_state_icmptq, p->ipftu_int); 5214255332Scy } else if (!strcmp(t->ipft_name, "icmp_ack_timeout")) { 5215255332Scy ipf_apply_timeout(&softs->ipf_state_icmpacktq, p->ipftu_int); 5216255332Scy } else if (!strcmp(t->ipft_name, "ip_timeout")) { 5217255332Scy ipf_apply_timeout(&softs->ipf_state_iptq, p->ipftu_int); 5218255332Scy } else { 5219255332Scy IPFERROR(100034); 5220255332Scy return ESRCH; 5221255332Scy } 5222255332Scy 5223255332Scy /* 5224255332Scy * Update the tuneable being set. 5225255332Scy */ 5226255332Scy *t->ipft_pint = p->ipftu_int; 5227255332Scy 5228255332Scy return 0; 5229255332Scy} 5230255332Scy 5231255332Scy 5232255332Scy/* ------------------------------------------------------------------------ */ 5233255332Scy/* Function: ipf_state_rehash */ 5234255332Scy/* Returns: int 0 = success, else failure */ 5235255332Scy/* Parameters: softc(I) - pointer to main soft context */ 5236255332Scy/* t(I) - pointer to tuneable being changed */ 5237255332Scy/* p(I) - pointer to the new value */ 5238255332Scy/* */ 5239255332Scy/* To change the size of the state hash table at runtime, a new table has */ 5240255332Scy/* to be allocated and then all of the existing entries put in it, bumping */ 5241255332Scy/* up the bucketlength for it as we go along. */ 5242255332Scy/* ------------------------------------------------------------------------ */ 5243255332Scyint 5244255332Scyipf_state_rehash(softc, t, p) 5245255332Scy ipf_main_softc_t *softc; 5246255332Scy ipftuneable_t *t; 5247255332Scy ipftuneval_t *p; 5248255332Scy{ 5249255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 5250255332Scy ipstate_t **newtab, *is; 5251255332Scy u_int *bucketlens; 5252255332Scy u_int maxbucket; 5253255332Scy u_int newsize; 5254255332Scy u_int hv; 5255255332Scy int i; 5256255332Scy 5257255332Scy newsize = p->ipftu_int; 5258255332Scy /* 5259255332Scy * In case there is nothing to do... 5260255332Scy */ 5261255332Scy if (newsize == softs->ipf_state_size) 5262255332Scy return 0; 5263255332Scy 5264255332Scy KMALLOCS(newtab, ipstate_t **, newsize * sizeof(ipstate_t *)); 5265255332Scy if (newtab == NULL) { 5266255332Scy IPFERROR(100035); 5267255332Scy return ENOMEM; 5268255332Scy } 5269255332Scy 5270255332Scy KMALLOCS(bucketlens, u_int *, newsize * sizeof(u_int)); 5271255332Scy if (bucketlens == NULL) { 5272255332Scy KFREES(newtab, newsize * sizeof(*softs->ipf_state_table)); 5273255332Scy IPFERROR(100036); 5274255332Scy return ENOMEM; 5275255332Scy } 5276255332Scy 5277255332Scy for (maxbucket = 0, i = newsize; i > 0; i >>= 1) 5278255332Scy maxbucket++; 5279255332Scy maxbucket *= 2; 5280255332Scy 5281255332Scy bzero((char *)newtab, newsize * sizeof(ipstate_t *)); 5282255332Scy bzero((char *)bucketlens, newsize * sizeof(u_int)); 5283255332Scy 5284255332Scy WRITE_ENTER(&softc->ipf_state); 5285255332Scy 5286255332Scy if (softs->ipf_state_table != NULL) { 5287255332Scy KFREES(softs->ipf_state_table, 5288255332Scy softs->ipf_state_size * sizeof(*softs->ipf_state_table)); 5289255332Scy } 5290255332Scy softs->ipf_state_table = newtab; 5291255332Scy 5292255332Scy if (softs->ipf_state_stats.iss_bucketlen != NULL) { 5293255332Scy KFREES(softs->ipf_state_stats.iss_bucketlen, 5294255332Scy softs->ipf_state_size * sizeof(u_int)); 5295255332Scy } 5296255332Scy softs->ipf_state_stats.iss_bucketlen = bucketlens; 5297255332Scy softs->ipf_state_maxbucket = maxbucket; 5298255332Scy softs->ipf_state_size = newsize; 5299255332Scy 5300255332Scy /* 5301255332Scy * Walk through the entire list of state table entries and put them 5302255332Scy * in the new state table, somewhere. Because we have a new table, 5303255332Scy * we need to restart the counter of how many chains are in use. 5304255332Scy */ 5305255332Scy softs->ipf_state_stats.iss_inuse = 0; 5306255332Scy for (is = softs->ipf_state_list; is != NULL; is = is->is_next) { 5307255332Scy is->is_hnext = NULL; 5308255332Scy is->is_phnext = NULL; 5309255332Scy hv = is->is_hv % softs->ipf_state_size; 5310255332Scy 5311255332Scy if (softs->ipf_state_table[hv] != NULL) 5312255332Scy softs->ipf_state_table[hv]->is_phnext = &is->is_hnext; 5313255332Scy else 5314255332Scy softs->ipf_state_stats.iss_inuse++; 5315255332Scy is->is_phnext = softs->ipf_state_table + hv; 5316255332Scy is->is_hnext = softs->ipf_state_table[hv]; 5317255332Scy softs->ipf_state_table[hv] = is; 5318255332Scy softs->ipf_state_stats.iss_bucketlen[hv]++; 5319255332Scy } 5320255332Scy RWLOCK_EXIT(&softc->ipf_state); 5321255332Scy 5322255332Scy return 0; 5323255332Scy} 5324255332Scy 5325255332Scy 5326255332Scy/* ------------------------------------------------------------------------ */ 5327255332Scy/* Function: ipf_state_add_tq */ 5328255332Scy/* Returns: ipftq_t * - NULL = failure, else pointer to new timeout */ 5329255332Scy/* queue */ 5330255332Scy/* Parameters: softc(I) - pointer to main soft context */ 5331255332Scy/* ttl(I) - pointer to the ttl for the new queue */ 5332255332Scy/* */ 5333255332Scy/* Request a pointer to a timeout queue that has a ttl as given by the */ 5334255332Scy/* value being passed in. The timeout queue is added tot the list of those */ 5335255332Scy/* used internally for stateful filtering. */ 5336255332Scy/* ------------------------------------------------------------------------ */ 5337255332Scyipftq_t * 5338255332Scyipf_state_add_tq(softc, ttl) 5339255332Scy ipf_main_softc_t *softc; 5340255332Scy int ttl; 5341255332Scy{ 5342255332Scy ipf_state_softc_t *softs = softc->ipf_state_soft; 5343255332Scy 5344255332Scy return ipf_addtimeoutqueue(softc, &softs->ipf_state_usertq, ttl); 5345255332Scy} 5346255332Scy 5347255332Scy 5348255332Scy#ifndef _KERNEL 5349255332Scy/* 5350255332Scy * Display the built up state table rules and mapping entries. 5351255332Scy */ 5352255332Scyvoid 5353255332Scyipf_state_dump(softc, arg) 5354255332Scy ipf_main_softc_t *softc; 5355255332Scy void *arg; 5356255332Scy{ 5357255332Scy ipf_state_softc_t *softs = arg; 5358255332Scy ipstate_t *ips; 5359255332Scy 5360255332Scy printf("List of active state sessions:\n"); 5361255332Scy for (ips = softs->ipf_state_list; ips != NULL; ) 5362255332Scy ips = printstate(ips, opts & (OPT_DEBUG|OPT_VERBOSE), 5363255332Scy softc->ipf_ticks); 5364255332Scy} 5365255332Scy#endif 5366