12393Syz155240 /*
22393Syz155240 * Copyright (C) 1995-2003 by Darren Reed.
32393Syz155240 *
42393Syz155240 * See the IPFILTER.LICENCE file for details on licencing.
52393Syz155240 *
6*13097SJan.Parcel@Sun.COM * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
72393Syz155240 */
82393Syz155240
92393Syz155240 #if defined(KERNEL) || defined(_KERNEL)
102393Syz155240 # undef KERNEL
112393Syz155240 # undef _KERNEL
122393Syz155240 # define KERNEL 1
132393Syz155240 # define _KERNEL 1
142393Syz155240 #endif
152393Syz155240 #include <sys/errno.h>
162393Syz155240 #include <sys/types.h>
172393Syz155240 #include <sys/param.h>
182393Syz155240 #include <sys/file.h>
192393Syz155240 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
202393Syz155240 defined(_KERNEL)
212393Syz155240 # include "opt_ipfilter_log.h"
222393Syz155240 #endif
232393Syz155240 #if defined(_KERNEL) && defined(__FreeBSD_version) && \
242393Syz155240 (__FreeBSD_version >= 400000) && !defined(KLD_MODULE)
252393Syz155240 #include "opt_inet6.h"
262393Syz155240 #endif
272393Syz155240 #if !defined(_KERNEL) && !defined(__KERNEL__)
282393Syz155240 # include <stdio.h>
292393Syz155240 # include <stdlib.h>
302393Syz155240 # include <string.h>
312393Syz155240 # define _KERNEL
322393Syz155240 # ifdef __OpenBSD__
332393Syz155240 struct file;
342393Syz155240 # endif
352393Syz155240 # include <sys/uio.h>
362393Syz155240 # undef _KERNEL
372393Syz155240 #endif
382393Syz155240 #if defined(_KERNEL) && (__FreeBSD_version >= 220000)
392393Syz155240 # include <sys/filio.h>
402393Syz155240 # include <sys/fcntl.h>
412393Syz155240 # if (__FreeBSD_version >= 300000) && !defined(IPFILTER_LKM)
422393Syz155240 # include "opt_ipfilter.h"
432393Syz155240 # endif
442393Syz155240 #else
452393Syz155240 # include <sys/ioctl.h>
462393Syz155240 #endif
472393Syz155240 #include <sys/time.h>
482393Syz155240 #if !defined(linux)
492393Syz155240 # include <sys/protosw.h>
502393Syz155240 #endif
512393Syz155240 #include <sys/socket.h>
522393Syz155240 #if defined(_KERNEL)
532393Syz155240 # include <sys/systm.h>
542393Syz155240 # if !defined(__SVR4) && !defined(__svr4__)
552393Syz155240 # include <sys/mbuf.h>
562393Syz155240 # endif
572393Syz155240 #endif
582393Syz155240 #if defined(__SVR4) || defined(__svr4__)
592393Syz155240 # include <sys/filio.h>
602393Syz155240 # include <sys/byteorder.h>
612393Syz155240 # ifdef _KERNEL
622393Syz155240 # include <sys/dditypes.h>
632393Syz155240 # endif
642393Syz155240 # include <sys/stream.h>
652393Syz155240 # include <sys/kmem.h>
662393Syz155240 #endif
672393Syz155240
682393Syz155240 #include <net/if.h>
692393Syz155240 #ifdef sun
702393Syz155240 # include <net/af.h>
712393Syz155240 #endif
722393Syz155240 #include <net/route.h>
732393Syz155240 #include <netinet/in.h>
742393Syz155240 #include <netinet/in_systm.h>
752393Syz155240 #include <netinet/ip.h>
762393Syz155240 #include <netinet/tcp.h>
772393Syz155240 #if !defined(linux)
782393Syz155240 # include <netinet/ip_var.h>
792393Syz155240 #endif
802393Syz155240 #if !defined(__hpux) && !defined(linux)
812393Syz155240 # include <netinet/tcp_fsm.h>
822393Syz155240 #endif
832393Syz155240 #include <netinet/udp.h>
842393Syz155240 #include <netinet/ip_icmp.h>
852393Syz155240 #include "netinet/ip_compat.h"
862393Syz155240 #include <netinet/tcpip.h>
872393Syz155240 #include "netinet/ip_fil.h"
882393Syz155240 #include "netinet/ip_nat.h"
892393Syz155240 #include "netinet/ip_frag.h"
902393Syz155240 #include "netinet/ip_state.h"
912393Syz155240 #include "netinet/ip_proxy.h"
923448Sdh155122 #include "netinet/ipf_stack.h"
932393Syz155240 #ifdef IPFILTER_SYNC
942393Syz155240 #include "netinet/ip_sync.h"
952393Syz155240 #endif
962393Syz155240 #ifdef IPFILTER_SCAN
972393Syz155240 #include "netinet/ip_scan.h"
982393Syz155240 #endif
992393Syz155240 #ifdef USE_INET6
1002393Syz155240 #include <netinet/icmp6.h>
1012393Syz155240 #endif
1022393Syz155240 #if (__FreeBSD_version >= 300000)
1032393Syz155240 # include <sys/malloc.h>
1042393Syz155240 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
1052393Syz155240 # include <sys/libkern.h>
1062393Syz155240 # include <sys/systm.h>
1072393Syz155240 # endif
1082393Syz155240 #endif
1092393Syz155240 /* END OF INCLUDES */
1102393Syz155240
1112393Syz155240
1122393Syz155240 #if !defined(lint)
1132393Syz155240 static const char sccsid[] = "@(#)ip_state.c 1.8 6/5/96 (C) 1993-2000 Darren Reed";
1142393Syz155240 static const char rcsid[] = "@(#)$Id: ip_state.c,v 2.186.2.36 2005/08/11 19:58:03 darrenr Exp $";
1152393Syz155240 #endif
1162393Syz155240
1172393Syz155240 #ifdef USE_INET6
1182393Syz155240 static ipstate_t *fr_checkicmp6matchingstate __P((fr_info_t *));
1192393Syz155240 #endif
1202393Syz155240 static ipstate_t *fr_matchsrcdst __P((fr_info_t *, ipstate_t *, i6addr_t *,
1212393Syz155240 i6addr_t *, tcphdr_t *, u_32_t));
1222393Syz155240 static ipstate_t *fr_checkicmpmatchingstate __P((fr_info_t *));
1233448Sdh155122 static int fr_state_flush __P((int, int, ipf_stack_t *));
1243448Sdh155122 static ips_stat_t *fr_statetstats __P((ipf_stack_t *));
1253448Sdh155122 static int fr_state_remove __P((caddr_t, ipf_stack_t *));
1263448Sdh155122 static void fr_ipsmove __P((ipstate_t *, u_int, ipf_stack_t *));
1272393Syz155240 static int fr_tcpstate __P((fr_info_t *, tcphdr_t *, ipstate_t *));
1282393Syz155240 static int fr_tcpoptions __P((fr_info_t *, tcphdr_t *, tcpdata_t *));
1292393Syz155240 static ipstate_t *fr_stclone __P((fr_info_t *, tcphdr_t *, ipstate_t *));
1302393Syz155240 static void fr_fixinisn __P((fr_info_t *, ipstate_t *));
1312393Syz155240 static void fr_fixoutisn __P((fr_info_t *, ipstate_t *));
1322393Syz155240 static void fr_checknewisn __P((fr_info_t *, ipstate_t *));
1333448Sdh155122 static int fr_stateiter __P((ipftoken_t *, ipfgeniter_t *, ipf_stack_t *));
1343448Sdh155122
1353448Sdh155122 int fr_stputent __P((caddr_t, ipf_stack_t *));
1363448Sdh155122 int fr_stgetent __P((caddr_t, ipf_stack_t *));
1372393Syz155240
1382393Syz155240 #define ONE_DAY IPF_TTLVAL(1 * 86400) /* 1 day */
1392393Syz155240 #define FIVE_DAYS (5 * ONE_DAY)
1403448Sdh155122 #define DOUBLE_HASH(x, ifs) \
1413448Sdh155122 (((x) + ifs->ifs_ips_seed[(x) % ifs->ifs_fr_statesize]) % ifs->ifs_fr_statesize)
1423448Sdh155122
1432393Syz155240
1442393Syz155240 /* ------------------------------------------------------------------------ */
1452393Syz155240 /* Function: fr_stateinit */
1462393Syz155240 /* Returns: int - 0 == success, -1 == failure */
1478170SJohn.Ojemann@Sun.COM /* Parameters: ifs - ipf stack instance */
1482393Syz155240 /* */
1492393Syz155240 /* Initialise all the global variables used within the state code. */
1502393Syz155240 /* This action also includes initiailising locks. */
1512393Syz155240 /* ------------------------------------------------------------------------ */
fr_stateinit(ifs)1523448Sdh155122 int fr_stateinit(ifs)
1533448Sdh155122 ipf_stack_t *ifs;
1542393Syz155240 {
1557259Sdr146992 #if defined(NEED_LOCAL_RAND) || !defined(_KERNEL)
1567259Sdr146992 struct timeval tv;
1577259Sdr146992 #endif
1582393Syz155240 int i;
1592393Syz155240
1603448Sdh155122 KMALLOCS(ifs->ifs_ips_table, ipstate_t **,
1613448Sdh155122 ifs->ifs_fr_statesize * sizeof(ipstate_t *));
1623448Sdh155122 if (ifs->ifs_ips_table == NULL)
1632393Syz155240 return -1;
1643448Sdh155122 bzero((char *)ifs->ifs_ips_table,
1653448Sdh155122 ifs->ifs_fr_statesize * sizeof(ipstate_t *));
1663448Sdh155122
1673448Sdh155122 KMALLOCS(ifs->ifs_ips_seed, u_long *,
1683448Sdh155122 ifs->ifs_fr_statesize * sizeof(*ifs->ifs_ips_seed));
1693448Sdh155122 if (ifs->ifs_ips_seed == NULL)
1702393Syz155240 return -2;
1717259Sdr146992 #if defined(NEED_LOCAL_RAND) || !defined(_KERNEL)
1727259Sdr146992 tv.tv_sec = 0;
1737259Sdr146992 GETKTIME(&tv);
1747259Sdr146992 #endif
1753448Sdh155122 for (i = 0; i < ifs->ifs_fr_statesize; i++) {
1762393Syz155240 /*
1772393Syz155240 * XXX - ips_seed[X] should be a random number of sorts.
1782393Syz155240 */
1797259Sdr146992 #if !defined(NEED_LOCAL_RAND) && defined(_KERNEL)
1807259Sdr146992 ifs->ifs_ips_seed[i] = ipf_random();
1812393Syz155240 #else
1823448Sdh155122 ifs->ifs_ips_seed[i] = ((u_long)ifs->ifs_ips_seed + i) *
1833448Sdh155122 ifs->ifs_fr_statesize;
1847259Sdr146992 ifs->ifs_ips_seed[i] += tv.tv_sec;
1853448Sdh155122 ifs->ifs_ips_seed[i] *= (u_long)ifs->ifs_ips_seed;
1863448Sdh155122 ifs->ifs_ips_seed[i] ^= 0x5a5aa5a5;
1873448Sdh155122 ifs->ifs_ips_seed[i] *= ifs->ifs_fr_statemax;
1882393Syz155240 #endif
1892393Syz155240 }
1902393Syz155240
1912393Syz155240 /* fill icmp reply type table */
1922393Syz155240 for (i = 0; i <= ICMP_MAXTYPE; i++)
1932393Syz155240 icmpreplytype4[i] = -1;
1942393Syz155240 icmpreplytype4[ICMP_ECHO] = ICMP_ECHOREPLY;
1952393Syz155240 icmpreplytype4[ICMP_TSTAMP] = ICMP_TSTAMPREPLY;
1962393Syz155240 icmpreplytype4[ICMP_IREQ] = ICMP_IREQREPLY;
1972393Syz155240 icmpreplytype4[ICMP_MASKREQ] = ICMP_MASKREPLY;
1982393Syz155240 #ifdef USE_INET6
1992393Syz155240 /* fill icmp reply type table */
2002393Syz155240 for (i = 0; i <= ICMP6_MAXTYPE; i++)
2012393Syz155240 icmpreplytype6[i] = -1;
2022393Syz155240 icmpreplytype6[ICMP6_ECHO_REQUEST] = ICMP6_ECHO_REPLY;
2032393Syz155240 icmpreplytype6[ICMP6_MEMBERSHIP_QUERY] = ICMP6_MEMBERSHIP_REPORT;
2042393Syz155240 icmpreplytype6[ICMP6_NI_QUERY] = ICMP6_NI_REPLY;
2052393Syz155240 icmpreplytype6[ND_ROUTER_SOLICIT] = ND_ROUTER_ADVERT;
2062393Syz155240 icmpreplytype6[ND_NEIGHBOR_SOLICIT] = ND_NEIGHBOR_ADVERT;
2072393Syz155240 #endif
2082393Syz155240
2093448Sdh155122 KMALLOCS(ifs->ifs_ips_stats.iss_bucketlen, u_long *,
2103448Sdh155122 ifs->ifs_fr_statesize * sizeof(u_long));
2113448Sdh155122 if (ifs->ifs_ips_stats.iss_bucketlen == NULL)
2122393Syz155240 return -1;
2133448Sdh155122 bzero((char *)ifs->ifs_ips_stats.iss_bucketlen,
2143448Sdh155122 ifs->ifs_fr_statesize * sizeof(u_long));
2153448Sdh155122
2163448Sdh155122 if (ifs->ifs_fr_state_maxbucket == 0) {
2173448Sdh155122 for (i = ifs->ifs_fr_statesize; i > 0; i >>= 1)
2183448Sdh155122 ifs->ifs_fr_state_maxbucket++;
2193448Sdh155122 ifs->ifs_fr_state_maxbucket *= 2;
2202393Syz155240 }
2212393Syz155240
2223448Sdh155122 fr_sttab_init(ifs->ifs_ips_tqtqb, ifs);
2233448Sdh155122 ifs->ifs_ips_tqtqb[IPF_TCP_NSTATES - 1].ifq_next = &ifs->ifs_ips_udptq;
2243448Sdh155122 ifs->ifs_ips_udptq.ifq_ttl = (u_long)ifs->ifs_fr_udptimeout;
2253448Sdh155122 ifs->ifs_ips_udptq.ifq_ref = 1;
2263448Sdh155122 ifs->ifs_ips_udptq.ifq_head = NULL;
2273448Sdh155122 ifs->ifs_ips_udptq.ifq_tail = &ifs->ifs_ips_udptq.ifq_head;
2283448Sdh155122 MUTEX_INIT(&ifs->ifs_ips_udptq.ifq_lock, "ipftq udp tab");
2293448Sdh155122 ifs->ifs_ips_udptq.ifq_next = &ifs->ifs_ips_udpacktq;
2303448Sdh155122 ifs->ifs_ips_udpacktq.ifq_ttl = (u_long)ifs->ifs_fr_udpacktimeout;
2313448Sdh155122 ifs->ifs_ips_udpacktq.ifq_ref = 1;
2323448Sdh155122 ifs->ifs_ips_udpacktq.ifq_head = NULL;
2333448Sdh155122 ifs->ifs_ips_udpacktq.ifq_tail = &ifs->ifs_ips_udpacktq.ifq_head;
2343448Sdh155122 MUTEX_INIT(&ifs->ifs_ips_udpacktq.ifq_lock, "ipftq udpack tab");
2353448Sdh155122 ifs->ifs_ips_udpacktq.ifq_next = &ifs->ifs_ips_icmptq;
2363448Sdh155122 ifs->ifs_ips_icmptq.ifq_ttl = (u_long)ifs->ifs_fr_icmptimeout;
2373448Sdh155122 ifs->ifs_ips_icmptq.ifq_ref = 1;
2383448Sdh155122 ifs->ifs_ips_icmptq.ifq_head = NULL;
2393448Sdh155122 ifs->ifs_ips_icmptq.ifq_tail = &ifs->ifs_ips_icmptq.ifq_head;
2403448Sdh155122 MUTEX_INIT(&ifs->ifs_ips_icmptq.ifq_lock, "ipftq icmp tab");
2413448Sdh155122 ifs->ifs_ips_icmptq.ifq_next = &ifs->ifs_ips_icmpacktq;
2423448Sdh155122 ifs->ifs_ips_icmpacktq.ifq_ttl = (u_long)ifs->ifs_fr_icmpacktimeout;
2433448Sdh155122 ifs->ifs_ips_icmpacktq.ifq_ref = 1;
2443448Sdh155122 ifs->ifs_ips_icmpacktq.ifq_head = NULL;
2453448Sdh155122 ifs->ifs_ips_icmpacktq.ifq_tail = &ifs->ifs_ips_icmpacktq.ifq_head;
2463448Sdh155122 MUTEX_INIT(&ifs->ifs_ips_icmpacktq.ifq_lock, "ipftq icmpack tab");
2473448Sdh155122 ifs->ifs_ips_icmpacktq.ifq_next = &ifs->ifs_ips_iptq;
2483448Sdh155122 ifs->ifs_ips_iptq.ifq_ttl = (u_long)ifs->ifs_fr_iptimeout;
2493448Sdh155122 ifs->ifs_ips_iptq.ifq_ref = 1;
2503448Sdh155122 ifs->ifs_ips_iptq.ifq_head = NULL;
2513448Sdh155122 ifs->ifs_ips_iptq.ifq_tail = &ifs->ifs_ips_iptq.ifq_head;
2523448Sdh155122 MUTEX_INIT(&ifs->ifs_ips_iptq.ifq_lock, "ipftq ip tab");
2534431San207044 ifs->ifs_ips_iptq.ifq_next = &ifs->ifs_ips_deletetq;
2544431San207044 /* entry's ttl in deletetq is just 1 tick */
2554431San207044 ifs->ifs_ips_deletetq.ifq_ttl = (u_long) 1;
2564431San207044 ifs->ifs_ips_deletetq.ifq_ref = 1;
2574431San207044 ifs->ifs_ips_deletetq.ifq_head = NULL;
2584431San207044 ifs->ifs_ips_deletetq.ifq_tail = &ifs->ifs_ips_deletetq.ifq_head;
2594431San207044 MUTEX_INIT(&ifs->ifs_ips_deletetq.ifq_lock, "state delete queue");
2604431San207044 ifs->ifs_ips_deletetq.ifq_next = NULL;
2613448Sdh155122
2623448Sdh155122 RWLOCK_INIT(&ifs->ifs_ipf_state, "ipf IP state rwlock");
2633448Sdh155122 MUTEX_INIT(&ifs->ifs_ipf_stinsert, "ipf state insert mutex");
2643448Sdh155122 ifs->ifs_fr_state_init = 1;
2653448Sdh155122
2663448Sdh155122 ifs->ifs_ips_last_force_flush = ifs->ifs_fr_ticks;
2672393Syz155240 return 0;
2682393Syz155240 }
2692393Syz155240
2702393Syz155240
2712393Syz155240 /* ------------------------------------------------------------------------ */
2722393Syz155240 /* Function: fr_stateunload */
2732393Syz155240 /* Returns: Nil */
2748170SJohn.Ojemann@Sun.COM /* Parameters: ifs - ipf stack instance */
2752393Syz155240 /* */
2762393Syz155240 /* Release and destroy any resources acquired or initialised so that */
2772393Syz155240 /* IPFilter can be unloaded or re-initialised. */
2782393Syz155240 /* ------------------------------------------------------------------------ */
fr_stateunload(ifs)2793448Sdh155122 void fr_stateunload(ifs)
2803448Sdh155122 ipf_stack_t *ifs;
2812393Syz155240 {
2822393Syz155240 ipftq_t *ifq, *ifqnext;
2832393Syz155240 ipstate_t *is;
2842393Syz155240
2853448Sdh155122 while ((is = ifs->ifs_ips_list) != NULL)
2868170SJohn.Ojemann@Sun.COM (void) fr_delstate(is, 0, ifs);
2872393Syz155240
2882393Syz155240 /*
2892393Syz155240 * Proxy timeout queues are not cleaned here because although they
2902393Syz155240 * exist on the state list, appr_unload is called after fr_stateunload
2912393Syz155240 * and the proxies actually are responsible for them being created.
2922393Syz155240 * Should the proxy timeouts have their own list? There's no real
2932393Syz155240 * justification as this is the only complicationA
2942393Syz155240 */
2953448Sdh155122 for (ifq = ifs->ifs_ips_utqe; ifq != NULL; ifq = ifqnext) {
2962393Syz155240 ifqnext = ifq->ifq_next;
2972393Syz155240 if (((ifq->ifq_flags & IFQF_PROXY) == 0) &&
2982393Syz155240 (fr_deletetimeoutqueue(ifq) == 0))
2993448Sdh155122 fr_freetimeoutqueue(ifq, ifs);
3002393Syz155240 }
3012393Syz155240
3023448Sdh155122 ifs->ifs_ips_stats.iss_inuse = 0;
3033448Sdh155122 ifs->ifs_ips_num = 0;
3043448Sdh155122
3053448Sdh155122 if (ifs->ifs_fr_state_init == 1) {
3063448Sdh155122 fr_sttab_destroy(ifs->ifs_ips_tqtqb);
3073448Sdh155122 MUTEX_DESTROY(&ifs->ifs_ips_udptq.ifq_lock);
3083448Sdh155122 MUTEX_DESTROY(&ifs->ifs_ips_icmptq.ifq_lock);
3093448Sdh155122 MUTEX_DESTROY(&ifs->ifs_ips_udpacktq.ifq_lock);
3103448Sdh155122 MUTEX_DESTROY(&ifs->ifs_ips_icmpacktq.ifq_lock);
3113448Sdh155122 MUTEX_DESTROY(&ifs->ifs_ips_iptq.ifq_lock);
3124431San207044 MUTEX_DESTROY(&ifs->ifs_ips_deletetq.ifq_lock);
3132393Syz155240 }
3142393Syz155240
3153448Sdh155122 if (ifs->ifs_ips_table != NULL) {
3163448Sdh155122 KFREES(ifs->ifs_ips_table,
3173448Sdh155122 ifs->ifs_fr_statesize * sizeof(*ifs->ifs_ips_table));
3183448Sdh155122 ifs->ifs_ips_table = NULL;
3192393Syz155240 }
3202393Syz155240
3213448Sdh155122 if (ifs->ifs_ips_seed != NULL) {
3223448Sdh155122 KFREES(ifs->ifs_ips_seed,
3233448Sdh155122 ifs->ifs_fr_statesize * sizeof(*ifs->ifs_ips_seed));
3243448Sdh155122 ifs->ifs_ips_seed = NULL;
3252393Syz155240 }
3262393Syz155240
3273448Sdh155122 if (ifs->ifs_ips_stats.iss_bucketlen != NULL) {
3283448Sdh155122 KFREES(ifs->ifs_ips_stats.iss_bucketlen,
3293448Sdh155122 ifs->ifs_fr_statesize * sizeof(u_long));
3303448Sdh155122 ifs->ifs_ips_stats.iss_bucketlen = NULL;
3313448Sdh155122 }
3323448Sdh155122
3333448Sdh155122 if (ifs->ifs_fr_state_maxbucket_reset == 1)
3343448Sdh155122 ifs->ifs_fr_state_maxbucket = 0;
3353448Sdh155122
3363448Sdh155122 if (ifs->ifs_fr_state_init == 1) {
3373448Sdh155122 ifs->ifs_fr_state_init = 0;
3383448Sdh155122 RW_DESTROY(&ifs->ifs_ipf_state);
3393448Sdh155122 MUTEX_DESTROY(&ifs->ifs_ipf_stinsert);
3402393Syz155240 }
3412393Syz155240 }
3422393Syz155240
3432393Syz155240
3442393Syz155240 /* ------------------------------------------------------------------------ */
3452393Syz155240 /* Function: fr_statetstats */
3462393Syz155240 /* Returns: ips_state_t* - pointer to state stats structure */
3472393Syz155240 /* Parameters: Nil */
3482393Syz155240 /* */
3492393Syz155240 /* Put all the current numbers and pointers into a single struct and return */
3502393Syz155240 /* a pointer to it. */
3512393Syz155240 /* ------------------------------------------------------------------------ */
fr_statetstats(ifs)3523448Sdh155122 static ips_stat_t *fr_statetstats(ifs)
3533448Sdh155122 ipf_stack_t *ifs;
3542393Syz155240 {
3553448Sdh155122 ifs->ifs_ips_stats.iss_active = ifs->ifs_ips_num;
3563448Sdh155122 ifs->ifs_ips_stats.iss_statesize = ifs->ifs_fr_statesize;
3573448Sdh155122 ifs->ifs_ips_stats.iss_statemax = ifs->ifs_fr_statemax;
3583448Sdh155122 ifs->ifs_ips_stats.iss_table = ifs->ifs_ips_table;
3593448Sdh155122 ifs->ifs_ips_stats.iss_list = ifs->ifs_ips_list;
3603448Sdh155122 ifs->ifs_ips_stats.iss_ticks = ifs->ifs_fr_ticks;
3613448Sdh155122 return &ifs->ifs_ips_stats;
3622393Syz155240 }
3632393Syz155240
3642393Syz155240 /* ------------------------------------------------------------------------ */
3652393Syz155240 /* Function: fr_state_remove */
3662393Syz155240 /* Returns: int - 0 == success, != 0 == failure */
3672393Syz155240 /* Parameters: data(I) - pointer to state structure to delete from table */
3688170SJohn.Ojemann@Sun.COM /* ifs - ipf stack instance */
3692393Syz155240 /* */
3702393Syz155240 /* Search for a state structure that matches the one passed, according to */
3712393Syz155240 /* the IP addresses and other protocol specific information. */
3722393Syz155240 /* ------------------------------------------------------------------------ */
fr_state_remove(data,ifs)3733448Sdh155122 static int fr_state_remove(data, ifs)
3742393Syz155240 caddr_t data;
3753448Sdh155122 ipf_stack_t *ifs;
3762393Syz155240 {
3772393Syz155240 ipstate_t *sp, st;
3782393Syz155240 int error;
3792393Syz155240
3802393Syz155240 sp = &st;
3812393Syz155240 error = fr_inobj(data, &st, IPFOBJ_IPSTATE);
3822393Syz155240 if (error)
3832393Syz155240 return EFAULT;
3842393Syz155240
3853448Sdh155122 WRITE_ENTER(&ifs->ifs_ipf_state);
3863448Sdh155122 for (sp = ifs->ifs_ips_list; sp; sp = sp->is_next)
3872393Syz155240 if ((sp->is_p == st.is_p) && (sp->is_v == st.is_v) &&
3882393Syz155240 !bcmp((caddr_t)&sp->is_src, (caddr_t)&st.is_src,
3892393Syz155240 sizeof(st.is_src)) &&
3907333SJohn.Ojemann@Sun.COM !bcmp((caddr_t)&sp->is_dst, (caddr_t)&st.is_dst,
3912393Syz155240 sizeof(st.is_dst)) &&
3922393Syz155240 !bcmp((caddr_t)&sp->is_ps, (caddr_t)&st.is_ps,
3932393Syz155240 sizeof(st.is_ps))) {
3948170SJohn.Ojemann@Sun.COM (void) fr_delstate(sp, ISL_REMOVE, ifs);
3953448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
3962393Syz155240 return 0;
3972393Syz155240 }
3983448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
3992393Syz155240 return ESRCH;
4002393Syz155240 }
4012393Syz155240
4022393Syz155240
4032393Syz155240 /* ------------------------------------------------------------------------ */
4042393Syz155240 /* Function: fr_state_ioctl */
4052393Syz155240 /* Returns: int - 0 == success, != 0 == failure */
4062393Syz155240 /* Parameters: data(I) - pointer to ioctl data */
4072393Syz155240 /* cmd(I) - ioctl command integer */
4082393Syz155240 /* mode(I) - file mode bits used with open */
4098170SJohn.Ojemann@Sun.COM /* uid(I) - uid of caller */
4108170SJohn.Ojemann@Sun.COM /* ctx(I) - pointer to give the uid context */
4118170SJohn.Ojemann@Sun.COM /* ifs - ipf stack instance */
4122393Syz155240 /* */
4132393Syz155240 /* Processes an ioctl call made to operate on the IP Filter state device. */
4142393Syz155240 /* ------------------------------------------------------------------------ */
fr_state_ioctl(data,cmd,mode,uid,ctx,ifs)4153448Sdh155122 int fr_state_ioctl(data, cmd, mode, uid, ctx, ifs)
4162393Syz155240 caddr_t data;
4172393Syz155240 ioctlcmd_t cmd;
4183448Sdh155122 int mode, uid;
4193448Sdh155122 void *ctx;
4203448Sdh155122 ipf_stack_t *ifs;
4212393Syz155240 {
4222393Syz155240 int arg, ret, error = 0;
4232393Syz155240
4242393Syz155240 switch (cmd)
4252393Syz155240 {
4262393Syz155240 /*
4272393Syz155240 * Delete an entry from the state table.
4282393Syz155240 */
4292393Syz155240 case SIOCDELST :
4307433SJohn.Ojemann@Sun.COM error = fr_state_remove(data, ifs);
4312393Syz155240 break;
4322393Syz155240 /*
4332393Syz155240 * Flush the state table
4342393Syz155240 */
4352393Syz155240 case SIOCIPFFL :
4367433SJohn.Ojemann@Sun.COM error = BCOPYIN(data, (char *)&arg, sizeof(arg));
4377433SJohn.Ojemann@Sun.COM if (error != 0) {
4387433SJohn.Ojemann@Sun.COM error = EFAULT;
4397433SJohn.Ojemann@Sun.COM } else {
4408170SJohn.Ojemann@Sun.COM if (VALID_TABLE_FLUSH_OPT(arg)) {
4417433SJohn.Ojemann@Sun.COM WRITE_ENTER(&ifs->ifs_ipf_state);
4427433SJohn.Ojemann@Sun.COM ret = fr_state_flush(arg, 4, ifs);
4437433SJohn.Ojemann@Sun.COM RWLOCK_EXIT(&ifs->ifs_ipf_state);
4447433SJohn.Ojemann@Sun.COM error = BCOPYOUT((char *)&ret, data,
4457433SJohn.Ojemann@Sun.COM sizeof(ret));
4467433SJohn.Ojemann@Sun.COM if (error != 0)
4477433SJohn.Ojemann@Sun.COM return EFAULT;
4487433SJohn.Ojemann@Sun.COM } else {
4497433SJohn.Ojemann@Sun.COM error = EINVAL;
4507433SJohn.Ojemann@Sun.COM }
4517433SJohn.Ojemann@Sun.COM }
4522393Syz155240 break;
4537433SJohn.Ojemann@Sun.COM
4542393Syz155240 #ifdef USE_INET6
4552393Syz155240 case SIOCIPFL6 :
4567433SJohn.Ojemann@Sun.COM error = BCOPYIN(data, (char *)&arg, sizeof(arg));
4577433SJohn.Ojemann@Sun.COM if (error != 0) {
4587433SJohn.Ojemann@Sun.COM error = EFAULT;
4597433SJohn.Ojemann@Sun.COM } else {
4608170SJohn.Ojemann@Sun.COM if (VALID_TABLE_FLUSH_OPT(arg)) {
4617433SJohn.Ojemann@Sun.COM WRITE_ENTER(&ifs->ifs_ipf_state);
4627433SJohn.Ojemann@Sun.COM ret = fr_state_flush(arg, 6, ifs);
4637433SJohn.Ojemann@Sun.COM RWLOCK_EXIT(&ifs->ifs_ipf_state);
4647433SJohn.Ojemann@Sun.COM error = BCOPYOUT((char *)&ret, data,
4657433SJohn.Ojemann@Sun.COM sizeof(ret));
4667433SJohn.Ojemann@Sun.COM if (error != 0)
4677433SJohn.Ojemann@Sun.COM return EFAULT;
4687433SJohn.Ojemann@Sun.COM } else {
4697433SJohn.Ojemann@Sun.COM error = EINVAL;
4707433SJohn.Ojemann@Sun.COM }
4717433SJohn.Ojemann@Sun.COM }
4722393Syz155240 break;
4732393Syz155240 #endif
4742393Syz155240 #ifdef IPFILTER_LOG
4752393Syz155240 /*
4762393Syz155240 * Flush the state log.
4772393Syz155240 */
4782393Syz155240 case SIOCIPFFB :
4792393Syz155240 if (!(mode & FWRITE))
4802393Syz155240 error = EPERM;
4812393Syz155240 else {
4822393Syz155240 int tmp;
4832393Syz155240
4843448Sdh155122 tmp = ipflog_clear(IPL_LOGSTATE, ifs);
4857433SJohn.Ojemann@Sun.COM error = BCOPYOUT((char *)&tmp, data, sizeof(tmp));
4867433SJohn.Ojemann@Sun.COM if (error != 0)
4877433SJohn.Ojemann@Sun.COM error = EFAULT;
4882393Syz155240 }
4892393Syz155240 break;
4902393Syz155240 /*
4912393Syz155240 * Turn logging of state information on/off.
4922393Syz155240 */
4932393Syz155240 case SIOCSETLG :
4947433SJohn.Ojemann@Sun.COM if (!(mode & FWRITE)) {
4952393Syz155240 error = EPERM;
4967433SJohn.Ojemann@Sun.COM } else {
4977433SJohn.Ojemann@Sun.COM error = BCOPYIN((char *)data,
4987433SJohn.Ojemann@Sun.COM (char *)&ifs->ifs_ipstate_logging,
4997433SJohn.Ojemann@Sun.COM sizeof(ifs->ifs_ipstate_logging));
5007433SJohn.Ojemann@Sun.COM if (error != 0)
5017433SJohn.Ojemann@Sun.COM error = EFAULT;
5022393Syz155240 }
5032393Syz155240 break;
5042393Syz155240 /*
5052393Syz155240 * Return the current state of logging.
5062393Syz155240 */
5072393Syz155240 case SIOCGETLG :
5087433SJohn.Ojemann@Sun.COM error = BCOPYOUT((char *)&ifs->ifs_ipstate_logging,
5097433SJohn.Ojemann@Sun.COM (char *)data,
5107433SJohn.Ojemann@Sun.COM sizeof(ifs->ifs_ipstate_logging));
5117433SJohn.Ojemann@Sun.COM if (error != 0)
5127433SJohn.Ojemann@Sun.COM error = EFAULT;
5132393Syz155240 break;
5142393Syz155240 /*
5152393Syz155240 * Return the number of bytes currently waiting to be read.
5162393Syz155240 */
5172393Syz155240 case FIONREAD :
5183448Sdh155122 arg = ifs->ifs_iplused[IPL_LOGSTATE]; /* returned in an int */
5197433SJohn.Ojemann@Sun.COM error = BCOPYOUT((char *)&arg, data, sizeof(arg));
5207433SJohn.Ojemann@Sun.COM if (error != 0)
5217433SJohn.Ojemann@Sun.COM error = EFAULT;
5222393Syz155240 break;
5232393Syz155240 #endif
5242393Syz155240 /*
5252393Syz155240 * Get the current state statistics.
5262393Syz155240 */
5272393Syz155240 case SIOCGETFS :
5283448Sdh155122 error = fr_outobj(data, fr_statetstats(ifs), IPFOBJ_STATESTAT);
5292393Syz155240 break;
5302393Syz155240 /*
5312393Syz155240 * Lock/Unlock the state table. (Locking prevents any changes, which
5322393Syz155240 * means no packets match).
5332393Syz155240 */
5342393Syz155240 case SIOCSTLCK :
5352393Syz155240 if (!(mode & FWRITE)) {
5362393Syz155240 error = EPERM;
5372393Syz155240 } else {
5387433SJohn.Ojemann@Sun.COM error = fr_lock(data, &ifs->ifs_fr_state_lock);
5392393Syz155240 }
5402393Syz155240 break;
5412393Syz155240 /*
5422393Syz155240 * Add an entry to the current state table.
5432393Syz155240 */
5442393Syz155240 case SIOCSTPUT :
5458170SJohn.Ojemann@Sun.COM if (!ifs->ifs_fr_state_lock || !(mode & FWRITE)) {
5462393Syz155240 error = EACCES;
5472393Syz155240 break;
5482393Syz155240 }
5493448Sdh155122 error = fr_stputent(data, ifs);
5502393Syz155240 break;
5512393Syz155240 /*
5522393Syz155240 * Get a state table entry.
5532393Syz155240 */
5542393Syz155240 case SIOCSTGET :
5553448Sdh155122 if (!ifs->ifs_fr_state_lock) {
5562393Syz155240 error = EACCES;
5572393Syz155240 break;
5582393Syz155240 }
5593448Sdh155122 error = fr_stgetent(data, ifs);
5602393Syz155240 break;
5613448Sdh155122
5623448Sdh155122 case SIOCGENITER :
5633448Sdh155122 {
5643448Sdh155122 ipftoken_t *token;
5653448Sdh155122 ipfgeniter_t iter;
5663448Sdh155122
5673448Sdh155122 error = fr_inobj(data, &iter, IPFOBJ_GENITER);
5683448Sdh155122 if (error != 0)
5693448Sdh155122 break;
5703448Sdh155122
5713448Sdh155122 token = ipf_findtoken(IPFGENITER_STATE, uid, ctx, ifs);
5723448Sdh155122 if (token != NULL)
5733448Sdh155122 error = fr_stateiter(token, &iter, ifs);
5743448Sdh155122 else
5753448Sdh155122 error = ESRCH;
5763448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_tokens);
5773448Sdh155122 break;
5783448Sdh155122 }
5793448Sdh155122
5803448Sdh155122 case SIOCIPFDELTOK :
5817433SJohn.Ojemann@Sun.COM error = BCOPYIN(data, (char *)&arg, sizeof(arg));
5827433SJohn.Ojemann@Sun.COM if (error != 0) {
5837433SJohn.Ojemann@Sun.COM error = EFAULT;
5847433SJohn.Ojemann@Sun.COM } else {
5857433SJohn.Ojemann@Sun.COM error = ipf_deltoken(arg, uid, ctx, ifs);
5867433SJohn.Ojemann@Sun.COM }
5873448Sdh155122 break;
5883448Sdh155122
5892393Syz155240 default :
5902393Syz155240 error = EINVAL;
5912393Syz155240 break;
5922393Syz155240 }
5932393Syz155240 return error;
5942393Syz155240 }
5952393Syz155240
5962393Syz155240
5972393Syz155240 /* ------------------------------------------------------------------------ */
5982393Syz155240 /* Function: fr_stgetent */
5992393Syz155240 /* Returns: int - 0 == success, != 0 == failure */
6002393Syz155240 /* Parameters: data(I) - pointer to state structure to retrieve from table */
6012393Syz155240 /* */
6022393Syz155240 /* Copy out state information from the kernel to a user space process. If */
6032393Syz155240 /* there is a filter rule associated with the state entry, copy that out */
6042393Syz155240 /* as well. The entry to copy out is taken from the value of "ips_next" in */
6052393Syz155240 /* the struct passed in and if not null and not found in the list of current*/
6062393Syz155240 /* state entries, the retrieval fails. */
6072393Syz155240 /* ------------------------------------------------------------------------ */
fr_stgetent(data,ifs)6083448Sdh155122 int fr_stgetent(data, ifs)
6092393Syz155240 caddr_t data;
6103448Sdh155122 ipf_stack_t *ifs;
6112393Syz155240 {
6122393Syz155240 ipstate_t *is, *isn;
6132393Syz155240 ipstate_save_t ips;
6142393Syz155240 int error;
6152393Syz155240
6162393Syz155240 error = fr_inobj(data, &ips, IPFOBJ_STATESAVE);
6172393Syz155240 if (error)
6182393Syz155240 return EFAULT;
6192393Syz155240
6202393Syz155240 isn = ips.ips_next;
6212393Syz155240 if (isn == NULL) {
6223448Sdh155122 isn = ifs->ifs_ips_list;
6232393Syz155240 if (isn == NULL) {
6242393Syz155240 if (ips.ips_next == NULL)
6252393Syz155240 return ENOENT;
6262393Syz155240 return 0;
6272393Syz155240 }
6282393Syz155240 } else {
6292393Syz155240 /*
6302393Syz155240 * Make sure the pointer we're copying from exists in the
6312393Syz155240 * current list of entries. Security precaution to prevent
6322393Syz155240 * copying of random kernel data.
6332393Syz155240 */
6343448Sdh155122 for (is = ifs->ifs_ips_list; is; is = is->is_next)
6352393Syz155240 if (is == isn)
6362393Syz155240 break;
6372393Syz155240 if (!is)
6382393Syz155240 return ESRCH;
6392393Syz155240 }
6402393Syz155240 ips.ips_next = isn->is_next;
6412393Syz155240 bcopy((char *)isn, (char *)&ips.ips_is, sizeof(ips.ips_is));
6422393Syz155240 ips.ips_rule = isn->is_rule;
6432393Syz155240 if (isn->is_rule != NULL)
6442393Syz155240 bcopy((char *)isn->is_rule, (char *)&ips.ips_fr,
6452393Syz155240 sizeof(ips.ips_fr));
6462393Syz155240 error = fr_outobj(data, &ips, IPFOBJ_STATESAVE);
6472393Syz155240 if (error)
6482393Syz155240 return EFAULT;
6492393Syz155240 return 0;
6502393Syz155240 }
6512393Syz155240
6522393Syz155240
6532393Syz155240 /* ------------------------------------------------------------------------ */
6542393Syz155240 /* Function: fr_stputent */
6552393Syz155240 /* Returns: int - 0 == success, != 0 == failure */
6562393Syz155240 /* Parameters: data(I) - pointer to state information struct */
6578170SJohn.Ojemann@Sun.COM /* ifs - ipf stack instance */
6582393Syz155240 /* */
6592393Syz155240 /* This function implements the SIOCSTPUT ioctl: insert a state entry into */
6602393Syz155240 /* the state table. If the state info. includes a pointer to a filter rule */
6612393Syz155240 /* then also add in an orphaned rule (will not show up in any "ipfstat -io" */
6622393Syz155240 /* output. */
6632393Syz155240 /* ------------------------------------------------------------------------ */
fr_stputent(data,ifs)6643448Sdh155122 int fr_stputent(data, ifs)
6652393Syz155240 caddr_t data;
6663448Sdh155122 ipf_stack_t *ifs;
6672393Syz155240 {
6682393Syz155240 ipstate_t *is, *isn;
6692393Syz155240 ipstate_save_t ips;
6702393Syz155240 int error, i;
6712393Syz155240 frentry_t *fr;
6722393Syz155240 char *name;
6732393Syz155240
6742393Syz155240 error = fr_inobj(data, &ips, IPFOBJ_STATESAVE);
6752393Syz155240 if (error)
6762393Syz155240 return EFAULT;
6772393Syz155240
6788170SJohn.Ojemann@Sun.COM /*
6798170SJohn.Ojemann@Sun.COM * Trigger automatic call to fr_state_flush() if the
6808170SJohn.Ojemann@Sun.COM * table has reached capacity specified by hi watermark.
6818170SJohn.Ojemann@Sun.COM */
6828170SJohn.Ojemann@Sun.COM if (ST_TAB_WATER_LEVEL(ifs) > ifs->ifs_state_flush_level_hi)
6838170SJohn.Ojemann@Sun.COM ifs->ifs_fr_state_doflush = 1;
6848170SJohn.Ojemann@Sun.COM
6858170SJohn.Ojemann@Sun.COM /*
6868170SJohn.Ojemann@Sun.COM * If automatic flushing did not do its job, and the table
6878170SJohn.Ojemann@Sun.COM * has filled up, don't try to create a new entry.
6888170SJohn.Ojemann@Sun.COM */
6898170SJohn.Ojemann@Sun.COM if (ifs->ifs_ips_num >= ifs->ifs_fr_statemax) {
6908170SJohn.Ojemann@Sun.COM ATOMIC_INCL(ifs->ifs_ips_stats.iss_max);
6918170SJohn.Ojemann@Sun.COM return ENOMEM;
6928170SJohn.Ojemann@Sun.COM }
6938170SJohn.Ojemann@Sun.COM
6942393Syz155240 KMALLOC(isn, ipstate_t *);
6952393Syz155240 if (isn == NULL)
6962393Syz155240 return ENOMEM;
6972393Syz155240
6982393Syz155240 bcopy((char *)&ips.ips_is, (char *)isn, sizeof(*isn));
6992393Syz155240 bzero((char *)isn, offsetof(struct ipstate, is_pkts));
7002393Syz155240 isn->is_sti.tqe_pnext = NULL;
7012393Syz155240 isn->is_sti.tqe_next = NULL;
7022393Syz155240 isn->is_sti.tqe_ifq = NULL;
7032393Syz155240 isn->is_sti.tqe_parent = isn;
7042393Syz155240 isn->is_ifp[0] = NULL;
7052393Syz155240 isn->is_ifp[1] = NULL;
7062393Syz155240 isn->is_ifp[2] = NULL;
7072393Syz155240 isn->is_ifp[3] = NULL;
7082393Syz155240 isn->is_sync = NULL;
7092393Syz155240 fr = ips.ips_rule;
7102393Syz155240
7112393Syz155240 if (fr == NULL) {
7123448Sdh155122 READ_ENTER(&ifs->ifs_ipf_state);
7133448Sdh155122 fr_stinsert(isn, 0, ifs);
7142393Syz155240 MUTEX_EXIT(&isn->is_lock);
7153448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
7162393Syz155240 return 0;
7172393Syz155240 }
7182393Syz155240
7192393Syz155240 if (isn->is_flags & SI_NEWFR) {
7202393Syz155240 KMALLOC(fr, frentry_t *);
7212393Syz155240 if (fr == NULL) {
7222393Syz155240 KFREE(isn);
7232393Syz155240 return ENOMEM;
7242393Syz155240 }
7252393Syz155240 bcopy((char *)&ips.ips_fr, (char *)fr, sizeof(*fr));
7262393Syz155240 isn->is_rule = fr;
7272393Syz155240 ips.ips_is.is_rule = fr;
7282393Syz155240 MUTEX_NUKE(&fr->fr_lock);
7292393Syz155240 MUTEX_INIT(&fr->fr_lock, "state filter rule lock");
7302393Syz155240
7312393Syz155240 /*
7322393Syz155240 * Look up all the interface names in the rule.
7332393Syz155240 */
7342393Syz155240 for (i = 0; i < 4; i++) {
7352393Syz155240 name = fr->fr_ifnames[i];
7363448Sdh155122 fr->fr_ifas[i] = fr_resolvenic(name, fr->fr_v, ifs);
7372393Syz155240 name = isn->is_ifname[i];
7383448Sdh155122 isn->is_ifp[i] = fr_resolvenic(name, isn->is_v, ifs);
7392393Syz155240 }
7402393Syz155240
7412393Syz155240 fr->fr_ref = 0;
7422393Syz155240 fr->fr_dsize = 0;
7432393Syz155240 fr->fr_data = NULL;
7447131Sdr146992 fr->fr_type = FR_T_NONE;
7452393Syz155240
7463448Sdh155122 fr_resolvedest(&fr->fr_tif, fr->fr_v, ifs);
7473448Sdh155122 fr_resolvedest(&fr->fr_dif, fr->fr_v, ifs);
7483894Sjojemann fr_resolvedest(&fr->fr_rif, fr->fr_v, ifs);
7492393Syz155240
7502393Syz155240 /*
7512393Syz155240 * send a copy back to userland of what we ended up
7522393Syz155240 * to allow for verification.
7532393Syz155240 */
7542393Syz155240 error = fr_outobj(data, &ips, IPFOBJ_STATESAVE);
7552393Syz155240 if (error) {
7562393Syz155240 KFREE(isn);
7572393Syz155240 MUTEX_DESTROY(&fr->fr_lock);
7582393Syz155240 KFREE(fr);
7592393Syz155240 return EFAULT;
7602393Syz155240 }
7613448Sdh155122 READ_ENTER(&ifs->ifs_ipf_state);
7623448Sdh155122 fr_stinsert(isn, 0, ifs);
7632393Syz155240 MUTEX_EXIT(&isn->is_lock);
7643448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
7652393Syz155240
7662393Syz155240 } else {
7673448Sdh155122 READ_ENTER(&ifs->ifs_ipf_state);
7683448Sdh155122 for (is = ifs->ifs_ips_list; is; is = is->is_next)
7692393Syz155240 if (is->is_rule == fr) {
7703448Sdh155122 fr_stinsert(isn, 0, ifs);
7712393Syz155240 MUTEX_EXIT(&isn->is_lock);
7722393Syz155240 break;
7732393Syz155240 }
7742393Syz155240
7752393Syz155240 if (is == NULL) {
7762393Syz155240 KFREE(isn);
7772393Syz155240 isn = NULL;
7782393Syz155240 }
7793448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
7802393Syz155240
7812393Syz155240 return (isn == NULL) ? ESRCH : 0;
7822393Syz155240 }
7832393Syz155240
7842393Syz155240 return 0;
7852393Syz155240 }
7862393Syz155240
7872393Syz155240
7882393Syz155240 /* ------------------------------------------------------------------------ */
7892393Syz155240 /* Function: fr_stinsert */
7902393Syz155240 /* Returns: Nil */
7912393Syz155240 /* Parameters: is(I) - pointer to state structure */
7922393Syz155240 /* rev(I) - flag indicating forward/reverse direction of packet */
7932393Syz155240 /* */
7942393Syz155240 /* Inserts a state structure into the hash table (for lookups) and the list */
7952393Syz155240 /* of state entries (for enumeration). Resolves all of the interface names */
7962393Syz155240 /* to pointers and adjusts running stats for the hash table as appropriate. */
7972393Syz155240 /* */
7982393Syz155240 /* Locking: it is assumed that some kind of lock on ipf_state is held. */
7992393Syz155240 /* Exits with is_lock initialised and held. */
8002393Syz155240 /* ------------------------------------------------------------------------ */
fr_stinsert(is,rev,ifs)8013448Sdh155122 void fr_stinsert(is, rev, ifs)
8022393Syz155240 ipstate_t *is;
8032393Syz155240 int rev;
8043448Sdh155122 ipf_stack_t *ifs;
8052393Syz155240 {
8062393Syz155240 frentry_t *fr;
8072393Syz155240 u_int hv;
8082393Syz155240 int i;
8092393Syz155240
8102393Syz155240 MUTEX_INIT(&is->is_lock, "ipf state entry");
8112393Syz155240
8122393Syz155240 fr = is->is_rule;
8132393Syz155240 if (fr != NULL) {
8142393Syz155240 MUTEX_ENTER(&fr->fr_lock);
8152393Syz155240 fr->fr_ref++;
8162393Syz155240 fr->fr_statecnt++;
8172393Syz155240 MUTEX_EXIT(&fr->fr_lock);
8182393Syz155240 }
8192393Syz155240
8202393Syz155240 /*
8212393Syz155240 * Look up all the interface names in the state entry.
8222393Syz155240 */
8232393Syz155240 for (i = 0; i < 4; i++) {
8242393Syz155240 if (is->is_ifp[i] != NULL)
8252393Syz155240 continue;
8263448Sdh155122 is->is_ifp[i] = fr_resolvenic(is->is_ifname[i], is->is_v, ifs);
8272393Syz155240 }
8282393Syz155240
8292393Syz155240 /*
8302393Syz155240 * If we could trust is_hv, then the modulous would not be needed, but
8312393Syz155240 * when running with IPFILTER_SYNC, this stops bad values.
8322393Syz155240 */
8333448Sdh155122 hv = is->is_hv % ifs->ifs_fr_statesize;
8342393Syz155240 is->is_hv = hv;
8352393Syz155240
8362393Syz155240 /*
8372393Syz155240 * We need to get both of these locks...the first because it is
8382393Syz155240 * possible that once the insert is complete another packet might
8392393Syz155240 * come along, match the entry and want to update it.
8402393Syz155240 */
8412393Syz155240 MUTEX_ENTER(&is->is_lock);
8423448Sdh155122 MUTEX_ENTER(&ifs->ifs_ipf_stinsert);
8432393Syz155240
8442393Syz155240 /*
8452393Syz155240 * add into list table.
8462393Syz155240 */
8473448Sdh155122 if (ifs->ifs_ips_list != NULL)
8483448Sdh155122 ifs->ifs_ips_list->is_pnext = &is->is_next;
8493448Sdh155122 is->is_pnext = &ifs->ifs_ips_list;
8503448Sdh155122 is->is_next = ifs->ifs_ips_list;
8513448Sdh155122 ifs->ifs_ips_list = is;
8523448Sdh155122
8533448Sdh155122 if (ifs->ifs_ips_table[hv] != NULL)
8543448Sdh155122 ifs->ifs_ips_table[hv]->is_phnext = &is->is_hnext;
8552393Syz155240 else
8563448Sdh155122 ifs->ifs_ips_stats.iss_inuse++;
8573448Sdh155122 is->is_phnext = ifs->ifs_ips_table + hv;
8583448Sdh155122 is->is_hnext = ifs->ifs_ips_table[hv];
8593448Sdh155122 ifs->ifs_ips_table[hv] = is;
8603448Sdh155122 ifs->ifs_ips_stats.iss_bucketlen[hv]++;
8613448Sdh155122 ifs->ifs_ips_num++;
8623448Sdh155122 MUTEX_EXIT(&ifs->ifs_ipf_stinsert);
8633448Sdh155122
8643448Sdh155122 fr_setstatequeue(is, rev, ifs);
8652393Syz155240 }
8662393Syz155240
8676252San207044 /* ------------------------------------------------------------------------ */
8686252San207044 /* Function: fr_match_ipv4addrs */
8696252San207044 /* Returns: int - 2 strong match (same addresses, same direction) */
8706252San207044 /* 1 weak match (same address, opposite direction) */
8716252San207044 /* 0 no match */
8726252San207044 /* */
8736252San207044 /* Function matches IPv4 addresses. */
8746252San207044 /* ------------------------------------------------------------------------ */
fr_match_ipv4addrs(is1,is2)8756252San207044 static int fr_match_ipv4addrs(is1, is2)
8766252San207044 ipstate_t *is1;
8776252San207044 ipstate_t *is2;
8786252San207044 {
8796252San207044 int rv;
8806252San207044
8816252San207044 if (is1->is_saddr == is2->is_saddr && is1->is_daddr == is2->is_daddr)
8826252San207044 rv = 2;
8837704SAlexandr.Nedvedicky@Sun.COM else if (is1->is_saddr == is2->is_daddr &&
8846252San207044 is1->is_daddr == is2->is_saddr)
8856252San207044 rv = 1;
8866252San207044 else
8876252San207044 rv = 0;
8887704SAlexandr.Nedvedicky@Sun.COM
8896252San207044 return (rv);
8906252San207044 }
8916252San207044
8926252San207044 /* ------------------------------------------------------------------------ */
8936252San207044 /* Function: fr_match_ipv6addrs */
8946252San207044 /* Returns: int - 2 strong match (same addresses, same direction) */
8956252San207044 /* 1 weak match (same addresses, opposite direction) */
8966252San207044 /* 0 no match */
8976252San207044 /* */
8986252San207044 /* Function matches IPv6 addresses. */
8996252San207044 /* ------------------------------------------------------------------------ */
fr_match_ipv6addrs(is1,is2)9006252San207044 static int fr_match_ipv6addrs(is1, is2)
9016252San207044 ipstate_t *is1;
9026252San207044 ipstate_t *is2;
9036252San207044 {
9046252San207044 int rv;
9056252San207044
9067704SAlexandr.Nedvedicky@Sun.COM if (IP6_EQ(&is1->is_src, &is2->is_src) &&
9077704SAlexandr.Nedvedicky@Sun.COM IP6_EQ(&is1->is_dst, &is2->is_dst))
9086252San207044 rv = 2;
9096252San207044 else if (IP6_EQ(&is1->is_src, &is2->is_dst) &&
9106252San207044 IP6_EQ(&is1->is_dst, &is2->is_src)) {
9116252San207044 rv = 1;
9126252San207044 }
9136252San207044 else
9146252San207044 rv = 0;
9156252San207044
9166252San207044 return (rv);
9176252San207044 }
9186252San207044 /* ------------------------------------------------------------------------ */
9196252San207044 /* Function: fr_match_addresses */
9206252San207044 /* Returns: int - 2 strong match (same addresses, same direction) */
9217704SAlexandr.Nedvedicky@Sun.COM /* 1 weak match (same address, opposite directions) */
9226252San207044 /* 0 no match */
9236252San207044 /* Parameters: is1, is2 pointers to states we are checking */
9246252San207044 /* */
9256252San207044 /* Matches addresses, function uses fr_match_ipvXaddrs() to deal with IPv4 */
9266252San207044 /* and IPv6 address format. */
9276252San207044 /* ------------------------------------------------------------------------ */
fr_match_addresses(is1,is2)9286252San207044 static int fr_match_addresses(is1, is2)
9296252San207044 ipstate_t *is1;
9306252San207044 ipstate_t *is2;
9316252San207044 {
9326252San207044 int rv;
9336252San207044
9346252San207044 if (is1->is_v == 4) {
9356252San207044 rv = fr_match_ipv4addrs(is1, is2);
9367704SAlexandr.Nedvedicky@Sun.COM } else {
9376252San207044 rv = fr_match_ipv6addrs(is1, is2);
9386252San207044 }
9397704SAlexandr.Nedvedicky@Sun.COM
9406252San207044 return (rv);
9416252San207044 }
9426252San207044
9436252San207044 /* ------------------------------------------------------------------------ */
9446252San207044 /* Function: fr_match_ppairs */
9456252San207044 /* Returns: int - 2 strong match (same ports, same direction) */
9466252San207044 /* 1 weak match (same ports, different direction) */
9476252San207044 /* 0 no match */
9486252San207044 /* Parameters ppairs1, ppairs - src, dst ports we want to match. */
9496252San207044 /* */
9506252San207044 /* Matches two port_pair_t types (port pairs). Each port pair contains */
9516252San207044 /* src, dst port, which belong to session (state entry). */
9526252San207044 /* ------------------------------------------------------------------------ */
fr_match_ppairs(ppairs1,ppairs2)9536252San207044 static int fr_match_ppairs(ppairs1, ppairs2)
9546252San207044 port_pair_t *ppairs1;
9556252San207044 port_pair_t *ppairs2;
9566252San207044 {
9576252San207044 int rv;
9586252San207044
9597704SAlexandr.Nedvedicky@Sun.COM if (ppairs1->pp_sport == ppairs2->pp_sport &&
9606252San207044 ppairs1->pp_dport == ppairs2->pp_dport)
9616252San207044 rv = 2;
9627704SAlexandr.Nedvedicky@Sun.COM else if (ppairs1->pp_sport == ppairs2->pp_dport &&
9636252San207044 ppairs1->pp_dport == ppairs2->pp_sport)
9646252San207044 rv = 1;
9656252San207044 else
9666252San207044 rv = 0;
9676252San207044
9686252San207044 return (rv);
9696252San207044 }
9706252San207044
9716252San207044 /* ------------------------------------------------------------------------ */
9726252San207044 /* Function: fr_match_l4_hdr */
9737704SAlexandr.Nedvedicky@Sun.COM /* Returns: int - 0 no match, */
9746252San207044 /* 1 weak match (same ports, different directions) */
9756252San207044 /* 2 strong match (same ports, same direction) */
9766252San207044 /* Parameters is1, is2 - states we want to match */
9776252San207044 /* */
9786252San207044 /* Function matches L4 header data (source ports for TCP, UDP, CallIds for */
9796252San207044 /* GRE protocol). */
9806252San207044 /* ------------------------------------------------------------------------ */
fr_match_l4_hdr(is1,is2)9816252San207044 static int fr_match_l4_hdr(is1, is2)
9826252San207044 ipstate_t *is1;
9836252San207044 ipstate_t *is2;
9846252San207044 {
9856252San207044 int rv = 0;
9866252San207044 port_pair_t pp1;
9876252San207044 port_pair_t pp2;
9886252San207044
9896252San207044 if (is1->is_p != is2->is_p)
9906252San207044 return (0);
9916252San207044
9926252San207044 switch (is1->is_p) {
9936252San207044 case IPPROTO_TCP:
9946252San207044 pp1.pp_sport = is1->is_ps.is_ts.ts_sport;
9956252San207044 pp1.pp_dport = is1->is_ps.is_ts.ts_dport;
9966252San207044 pp2.pp_sport = is2->is_ps.is_ts.ts_sport;
9976252San207044 pp2.pp_dport = is2->is_ps.is_ts.ts_dport;
9986252San207044 rv = fr_match_ppairs(&pp1, &pp2);
9996252San207044 break;
10006252San207044 case IPPROTO_UDP:
10016252San207044 pp1.pp_sport = is1->is_ps.is_us.us_sport;
10026252San207044 pp1.pp_dport = is1->is_ps.is_us.us_dport;
10036252San207044 pp2.pp_sport = is2->is_ps.is_us.us_sport;
10046252San207044 pp2.pp_dport = is2->is_ps.is_us.us_dport;
10056252San207044 rv = fr_match_ppairs(&pp1, &pp2);
10066252San207044 break;
10076252San207044 case IPPROTO_GRE:
10086252San207044 /* greinfo_t can be also interprted as port pair */
10096252San207044 pp1.pp_sport = is1->is_ps.is_ug.gs_call[0];
10106252San207044 pp1.pp_dport = is1->is_ps.is_ug.gs_call[1];
10116252San207044 pp2.pp_sport = is2->is_ps.is_ug.gs_call[0];
10126252San207044 pp2.pp_dport = is2->is_ps.is_ug.gs_call[1];
10136252San207044 rv = fr_match_ppairs(&pp1, &pp2);
10146252San207044 break;
10156252San207044 case IPPROTO_ICMP:
10166252San207044 case IPPROTO_ICMPV6:
10177704SAlexandr.Nedvedicky@Sun.COM if (bcmp(&is1->is_ps, &is2->is_ps, sizeof (icmpinfo_t)))
10186252San207044 rv = 1;
10196252San207044 else
10206252San207044 rv = 0;
10216252San207044 break;
10226252San207044 default:
10236252San207044 rv = 0;
10246252San207044 }
10256252San207044
10266252San207044 return (rv);
10276252San207044 }
10286252San207044
10296252San207044 /* ------------------------------------------------------------------------ */
10306252San207044 /* Function: fr_matchstates */
10316252San207044 /* Returns: int - nonzero match, zero no match */
10326252San207044 /* Parameters is1, is2 - states we want to match */
10336252San207044 /* */
10346252San207044 /* The state entries are equal (identical match) if they belong to the same */
10357704SAlexandr.Nedvedicky@Sun.COM /* session. Any time new state entry is being added the fr_addstate() */
10366252San207044 /* function creates temporal state entry from the data it gets from IP and */
10376252San207044 /* L4 header. The fr_matchstats() must be also aware of packet direction, */
10386252San207044 /* which is also stored within the state entry. We should keep in mind the */
10396252San207044 /* information about packet direction is spread accross L3 (addresses) and */
10406252San207044 /* L4 (ports). There are three possible relationships betwee is1, is2: */
10416252San207044 /* - no match (match(is1, is2) == 0)) */
10427704SAlexandr.Nedvedicky@Sun.COM /* - weak match same addresses (ports), but different */
10436252San207044 /* directions (1) (fr_match_xxxx(is1, is2) == 1) */
10446252San207044 /* - strong match same addresses (ports) and same directions */
10456252San207044 /* (2) (fr_match_xxxx(is1, is2) == 2) */
10466252San207044 /* */
10476252San207044 /* There are functions, which match match addresses (L3 header) in is1, is2 */
10486252San207044 /* and functions, which are used to compare ports (L4 header) data. We say */
10496252San207044 /* the is1 and is2 are same (identical) if there is a match */
10506252San207044 /* (fr_match_l4_hdr(is1, is2) != 0) and matchlevels are same for entries */
10516252San207044 /* (fr_match_l3_hdr(is1, is2) == fr_match_l4_hdr(is1, is2)) for is1, is2. */
10526252San207044 /* Such requirement deals with case as follows: */
10536252San207044 /* suppose there are two connections between hosts A, B. Connection 1: */
10546252San207044 /* a.a.a.a:12345 <=> b.b.b.b:54321 */
10556252San207044 /* Connection 2: */
10567704SAlexandr.Nedvedicky@Sun.COM /* a.a.a.a:54321 <=> b.b.b.b:12345 */
10576252San207044 /* since we've introduced match levels into our fr_matchstates(), we are */
10586252San207044 /* able to identify, which packets belong to connection A and which belong */
10596252San207044 /* to connection B. Assume there are two entries is1, is2. is1 has been */
10606252San207044 /* from con. 1 packet, which travelled from A to B: */
10616252San207044 /* a.a.a.a:12345 -> b.b.b.b:54321 */
10626252San207044 /* while s2, has been created from packet which belongs to con. 2 and is */
10636252San207044 /* also coming from A to B: */
10646252San207044 /* a.a.a.a:54321 -> b.b.b.b:12345 */
10656252San207044 /* fr_match_l3_hdr(is1, is2) == 2 -> strong match, while */
10666252San207044 /* fr_match_l4_hdr(is1, is2) == 1 -> weak match. Since match levels are */
10676252San207044 /* different the state entries are not identical -> no match as a final */
10686252San207044 /* result. */
10696252San207044 /* ------------------------------------------------------------------------ */
fr_matchstates(is1,is2)10706252San207044 static int fr_matchstates(is1, is2)
10717704SAlexandr.Nedvedicky@Sun.COM ipstate_t *is1;
10726252San207044 ipstate_t *is2;
10736252San207044 {
10746252San207044 int rv;
10756252San207044 int amatch;
10766252San207044 int pmatch;
10776252San207044
10786252San207044 if (bcmp(&is1->is_pass, &is2->is_pass,
10797704SAlexandr.Nedvedicky@Sun.COM offsetof(struct ipstate, is_ps) -
10807704SAlexandr.Nedvedicky@Sun.COM offsetof(struct ipstate, is_pass)) == 0) {
10817704SAlexandr.Nedvedicky@Sun.COM
10826252San207044 pmatch = fr_match_l4_hdr(is1, is2);
10836252San207044 amatch = fr_match_addresses(is1, is2);
10847704SAlexandr.Nedvedicky@Sun.COM /*
10856252San207044 * If addresses match (amatch != 0), then 'match levels'
10866252San207044 * must be same for matching entries. If amatch and pmatch
10876252San207044 * have different values (different match levels), then
10886252San207044 * is1 and is2 belong to different sessions.
10896252San207044 */
10906252San207044 rv = (amatch != 0) && (amatch == pmatch);
10916252San207044 }
10926252San207044 else
10936252San207044 rv = 0;
10946252San207044
10956252San207044 return (rv);
10966252San207044 }
10972393Syz155240
10982393Syz155240 /* ------------------------------------------------------------------------ */
10992393Syz155240 /* Function: fr_addstate */
11002393Syz155240 /* Returns: ipstate_t* - NULL == failure, else pointer to new state */
11012393Syz155240 /* Parameters: fin(I) - pointer to packet information */
11022393Syz155240 /* stsave(O) - pointer to place to save pointer to created */
11032393Syz155240 /* state structure. */
11042393Syz155240 /* flags(I) - flags to use when creating the structure */
11052393Syz155240 /* */
11062393Syz155240 /* Creates a new IP state structure from the packet information collected. */
11072393Syz155240 /* Inserts it into the state table and appends to the bottom of the active */
11082393Syz155240 /* list. If the capacity of the table has reached the maximum allowed then */
11092393Syz155240 /* the call will fail and a flush is scheduled for the next timeout call. */
11102393Syz155240 /* ------------------------------------------------------------------------ */
fr_addstate(fin,stsave,flags)11112393Syz155240 ipstate_t *fr_addstate(fin, stsave, flags)
11122393Syz155240 fr_info_t *fin;
11132393Syz155240 ipstate_t **stsave;
11142393Syz155240 u_int flags;
11152393Syz155240 {
11162393Syz155240 ipstate_t *is, ips;
11172393Syz155240 struct icmp *ic;
11182393Syz155240 u_int pass, hv;
11192393Syz155240 frentry_t *fr;
11202393Syz155240 tcphdr_t *tcp;
11212393Syz155240 grehdr_t *gre;
11222393Syz155240 void *ifp;
11232393Syz155240 int out;
11243448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs;
11253448Sdh155122
11263448Sdh155122 if (ifs->ifs_fr_state_lock ||
11272393Syz155240 (fin->fin_flx & (FI_SHORT|FI_STATE|FI_FRAGBODY|FI_BAD)))
11282393Syz155240 return NULL;
11292393Syz155240
11302393Syz155240 if ((fin->fin_flx & FI_OOW) && !(fin->fin_tcpf & TH_SYN))
11312393Syz155240 return NULL;
11322393Syz155240
11332393Syz155240 /*
11348170SJohn.Ojemann@Sun.COM * Trigger automatic call to fr_state_flush() if the
11358170SJohn.Ojemann@Sun.COM * table has reached capacity specified by hi watermark.
11362393Syz155240 */
11378170SJohn.Ojemann@Sun.COM if (ST_TAB_WATER_LEVEL(ifs) > ifs->ifs_state_flush_level_hi)
11388170SJohn.Ojemann@Sun.COM ifs->ifs_fr_state_doflush = 1;
11398170SJohn.Ojemann@Sun.COM
11408170SJohn.Ojemann@Sun.COM /*
11418170SJohn.Ojemann@Sun.COM * If the max number of state entries has been reached, and there is no
11428170SJohn.Ojemann@Sun.COM * limit on the state count for the rule, then do not continue. In the
11438170SJohn.Ojemann@Sun.COM * case where a limit exists, it's ok allow the entries to be created as
11448170SJohn.Ojemann@Sun.COM * long as specified limit itself has not been reached.
11458170SJohn.Ojemann@Sun.COM *
11468170SJohn.Ojemann@Sun.COM * Note that because the lock isn't held on fr, it is possible to exceed
11478170SJohn.Ojemann@Sun.COM * the specified size of the table. However, the cost of this is being
11488170SJohn.Ojemann@Sun.COM * ignored here; as the number by which it can go over is a product of
11498170SJohn.Ojemann@Sun.COM * the number of simultaneous threads that could be executing in here.
11508170SJohn.Ojemann@Sun.COM * So, a limit of 100 won't result in 200, but could result in 101 or 102.
11518170SJohn.Ojemann@Sun.COM *
11528170SJohn.Ojemann@Sun.COM * Also note that, since the automatic flush should have been triggered
11538170SJohn.Ojemann@Sun.COM * well before we reach the maximum number of state table entries, the
11548170SJohn.Ojemann@Sun.COM * likelihood of reaching the max (and thus exceedng it) is minimal.
11558170SJohn.Ojemann@Sun.COM */
11562944Sjojemann fr = fin->fin_fr;
11572944Sjojemann if (fr != NULL) {
11588170SJohn.Ojemann@Sun.COM if ((ifs->ifs_ips_num >= ifs->ifs_fr_statemax) &&
11598170SJohn.Ojemann@Sun.COM (fr->fr_statemax == 0)) {
11603448Sdh155122 ATOMIC_INCL(ifs->ifs_ips_stats.iss_max);
11612944Sjojemann return NULL;
11622944Sjojemann }
11632944Sjojemann if ((fr->fr_statemax != 0) &&
11642944Sjojemann (fr->fr_statecnt >= fr->fr_statemax)) {
11653448Sdh155122 ATOMIC_INCL(ifs->ifs_ips_stats.iss_maxref);
11663448Sdh155122 ifs->ifs_fr_state_doflush = 1;
11672944Sjojemann return NULL;
11682944Sjojemann }
11692393Syz155240 }
11702393Syz155240
11712393Syz155240 ic = NULL;
11722393Syz155240 tcp = NULL;
11732393Syz155240 out = fin->fin_out;
11742393Syz155240 is = &ips;
11752393Syz155240 bzero((char *)is, sizeof(*is));
11766252San207044
11776252San207044 if (fr == NULL) {
11786252San207044 pass = ifs->ifs_fr_flags;
11796252San207044 is->is_tag = FR_NOLOGTAG;
11807704SAlexandr.Nedvedicky@Sun.COM } else {
11816252San207044 pass = fr->fr_flags;
11826252San207044 }
11836252San207044
11843448Sdh155122 is->is_die = 1 + ifs->ifs_fr_ticks;
11856252San207044 /*
11866252San207044 * We want to check everything that is a property of this packet,
11876252San207044 * but we don't (automatically) care about it's fragment status as
11886252San207044 * this may change.
11896252San207044 */
11906252San207044 is->is_pass = pass;
11916252San207044 is->is_v = fin->fin_v;
11926252San207044 is->is_opt[0] = fin->fin_optmsk;
11936252San207044 is->is_optmsk[0] = 0xffffffff;
1194*13097SJan.Parcel@Sun.COM /*
1195*13097SJan.Parcel@Sun.COM * The reverse direction option mask will be set in fr_matchsrcdst(),
1196*13097SJan.Parcel@Sun.COM * when we will see the first packet from the peer. We will leave it
1197*13097SJan.Parcel@Sun.COM * as zero for now.
1198*13097SJan.Parcel@Sun.COM */
1199*13097SJan.Parcel@Sun.COM is->is_optmsk[1] = 0x0;
1200*13097SJan.Parcel@Sun.COM
12016252San207044 if (is->is_v == 6) {
12026252San207044 is->is_opt[0] &= ~0x8;
12036252San207044 is->is_optmsk[0] &= ~0x8;
12046252San207044 }
12056252San207044 is->is_sec = fin->fin_secmsk;
12066252San207044 is->is_secmsk = 0xffff;
12076252San207044 is->is_auth = fin->fin_auth;
12086252San207044 is->is_authmsk = 0xffff;
12092393Syz155240
12102393Syz155240 /*
12112393Syz155240 * Copy and calculate...
12122393Syz155240 */
12132393Syz155240 hv = (is->is_p = fin->fin_fi.fi_p);
12142393Syz155240 is->is_src = fin->fin_fi.fi_src;
12152393Syz155240 hv += is->is_saddr;
12162393Syz155240 is->is_dst = fin->fin_fi.fi_dst;
12172393Syz155240 hv += is->is_daddr;
12182393Syz155240 #ifdef USE_INET6
12192393Syz155240 if (fin->fin_v == 6) {
12202393Syz155240 /*
12212393Syz155240 * For ICMPv6, we check to see if the destination address is
12222393Syz155240 * a multicast address. If it is, do not include it in the
12232393Syz155240 * calculation of the hash because the correct reply will come
12242393Syz155240 * back from a real address, not a multicast address.
12252393Syz155240 */
12262393Syz155240 if ((is->is_p == IPPROTO_ICMPV6) &&
12272393Syz155240 IN6_IS_ADDR_MULTICAST(&is->is_dst.in6)) {
12282393Syz155240 /*
12292393Syz155240 * So you can do keep state with neighbour discovery.
12302393Syz155240 *
12312393Syz155240 * Here we could use the address from the neighbour
12322393Syz155240 * solicit message to put in the state structure and
12332393Syz155240 * we could use that without a wildcard flag too...
12342393Syz155240 */
12352393Syz155240 is->is_flags |= SI_W_DADDR;
12362393Syz155240 hv -= is->is_daddr;
12372393Syz155240 } else {
12382393Syz155240 hv += is->is_dst.i6[1];
12392393Syz155240 hv += is->is_dst.i6[2];
12402393Syz155240 hv += is->is_dst.i6[3];
12412393Syz155240 }
12422393Syz155240 hv += is->is_src.i6[1];
12432393Syz155240 hv += is->is_src.i6[2];
12442393Syz155240 hv += is->is_src.i6[3];
12452393Syz155240 }
12462393Syz155240 #endif
12477433SJohn.Ojemann@Sun.COM if ((fin->fin_v == 4) &&
12487433SJohn.Ojemann@Sun.COM (fin->fin_flx & (FI_MULTICAST|FI_BROADCAST|FI_MBCAST))) {
12497433SJohn.Ojemann@Sun.COM if (fin->fin_out == 0) {
12507433SJohn.Ojemann@Sun.COM flags |= SI_W_DADDR|SI_CLONE;
12517433SJohn.Ojemann@Sun.COM hv -= is->is_daddr;
12527433SJohn.Ojemann@Sun.COM } else {
12537433SJohn.Ojemann@Sun.COM flags |= SI_W_SADDR|SI_CLONE;
12547433SJohn.Ojemann@Sun.COM hv -= is->is_saddr;
12557433SJohn.Ojemann@Sun.COM }
12567433SJohn.Ojemann@Sun.COM }
12572393Syz155240
12582393Syz155240 switch (is->is_p)
12592393Syz155240 {
12602393Syz155240 #ifdef USE_INET6
12612393Syz155240 case IPPROTO_ICMPV6 :
12622393Syz155240 ic = fin->fin_dp;
12632393Syz155240
12642393Syz155240 switch (ic->icmp_type)
12652393Syz155240 {
12662393Syz155240 case ICMP6_ECHO_REQUEST :
12672393Syz155240 is->is_icmp.ici_type = ic->icmp_type;
12682393Syz155240 hv += (is->is_icmp.ici_id = ic->icmp_id);
12692393Syz155240 break;
12702393Syz155240 case ICMP6_MEMBERSHIP_QUERY :
12712393Syz155240 case ND_ROUTER_SOLICIT :
12722393Syz155240 case ND_NEIGHBOR_SOLICIT :
12732393Syz155240 case ICMP6_NI_QUERY :
12742393Syz155240 is->is_icmp.ici_type = ic->icmp_type;
12752393Syz155240 break;
12762393Syz155240 default :
12772393Syz155240 return NULL;
12782393Syz155240 }
12793448Sdh155122 ATOMIC_INCL(ifs->ifs_ips_stats.iss_icmp);
12802393Syz155240 break;
12812393Syz155240 #endif
12822393Syz155240 case IPPROTO_ICMP :
12832393Syz155240 ic = fin->fin_dp;
12842393Syz155240
12852393Syz155240 switch (ic->icmp_type)
12862393Syz155240 {
12872393Syz155240 case ICMP_ECHO :
12882393Syz155240 case ICMP_TSTAMP :
12892393Syz155240 case ICMP_IREQ :
12902393Syz155240 case ICMP_MASKREQ :
12912393Syz155240 is->is_icmp.ici_type = ic->icmp_type;
12922393Syz155240 hv += (is->is_icmp.ici_id = ic->icmp_id);
12932393Syz155240 break;
12942393Syz155240 default :
12952393Syz155240 return NULL;
12962393Syz155240 }
12973448Sdh155122 ATOMIC_INCL(ifs->ifs_ips_stats.iss_icmp);
12982393Syz155240 break;
12992393Syz155240
13002393Syz155240 case IPPROTO_GRE :
13012393Syz155240 gre = fin->fin_dp;
13022393Syz155240
13032393Syz155240 is->is_gre.gs_flags = gre->gr_flags;
13042393Syz155240 is->is_gre.gs_ptype = gre->gr_ptype;
13052393Syz155240 if (GRE_REV(is->is_gre.gs_flags) == 1) {
13062393Syz155240 is->is_call[0] = fin->fin_data[0];
13072393Syz155240 is->is_call[1] = fin->fin_data[1];
13082393Syz155240 }
13092393Syz155240 break;
13102393Syz155240
13112393Syz155240 case IPPROTO_TCP :
13122393Syz155240 tcp = fin->fin_dp;
13132393Syz155240
13142393Syz155240 if (tcp->th_flags & TH_RST)
13152393Syz155240 return NULL;
13162393Syz155240 /*
13172393Syz155240 * The endian of the ports doesn't matter, but the ack and
13182393Syz155240 * sequence numbers do as we do mathematics on them later.
13192393Syz155240 */
13202393Syz155240 is->is_sport = htons(fin->fin_data[0]);
13212393Syz155240 is->is_dport = htons(fin->fin_data[1]);
13222393Syz155240 if ((flags & (SI_W_DPORT|SI_W_SPORT)) == 0) {
13232393Syz155240 hv += is->is_sport;
13242393Syz155240 hv += is->is_dport;
13252393Syz155240 }
13262393Syz155240
13272393Syz155240 /*
13282393Syz155240 * If this is a real packet then initialise fields in the
13292393Syz155240 * state information structure from the TCP header information.
13302393Syz155240 */
13312393Syz155240
13322393Syz155240 is->is_maxdwin = 1;
13332393Syz155240 is->is_maxswin = ntohs(tcp->th_win);
13342393Syz155240 if (is->is_maxswin == 0)
13352393Syz155240 is->is_maxswin = 1;
13362393Syz155240
13372393Syz155240 if ((fin->fin_flx & FI_IGNORE) == 0) {
13382393Syz155240 is->is_send = ntohl(tcp->th_seq) + fin->fin_dlen -
13392393Syz155240 (TCP_OFF(tcp) << 2) +
13402393Syz155240 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
13412393Syz155240 ((tcp->th_flags & TH_FIN) ? 1 : 0);
13422393Syz155240 is->is_maxsend = is->is_send;
13432393Syz155240
13442393Syz155240 /*
13452393Syz155240 * Window scale option is only present in
13462393Syz155240 * SYN/SYN-ACK packet.
13472393Syz155240 */
13482393Syz155240 if ((tcp->th_flags & ~(TH_FIN|TH_ACK|TH_ECNALL)) ==
13492393Syz155240 TH_SYN &&
13502393Syz155240 (TCP_OFF(tcp) > (sizeof(tcphdr_t) >> 2))) {
13512393Syz155240 if (fr_tcpoptions(fin, tcp,
13527704SAlexandr.Nedvedicky@Sun.COM &is->is_tcp.ts_data[0]) == -1) {
13536647San207044 fin->fin_flx |= FI_BAD;
13546647San207044 }
13552393Syz155240 }
13562393Syz155240
13572393Syz155240 if ((fin->fin_out != 0) && (pass & FR_NEWISN) != 0) {
13582393Syz155240 fr_checknewisn(fin, is);
13592393Syz155240 fr_fixoutisn(fin, is);
13602393Syz155240 }
13612393Syz155240
13622393Syz155240 if ((tcp->th_flags & TH_OPENING) == TH_SYN)
13632393Syz155240 flags |= IS_TCPFSM;
13642393Syz155240 else {
13652393Syz155240 is->is_maxdwin = is->is_maxswin * 2;
13662393Syz155240 is->is_dend = ntohl(tcp->th_ack);
13672393Syz155240 is->is_maxdend = ntohl(tcp->th_ack);
13682393Syz155240 is->is_maxdwin *= 2;
13692393Syz155240 }
13702393Syz155240 }
13712393Syz155240
13722393Syz155240 /*
13732393Syz155240 * If we're creating state for a starting connection, start the
13742393Syz155240 * timer on it as we'll never see an error if it fails to
13752393Syz155240 * connect.
13762393Syz155240 */
13773448Sdh155122 ATOMIC_INCL(ifs->ifs_ips_stats.iss_tcp);
13782393Syz155240 break;
13792393Syz155240
13802393Syz155240 case IPPROTO_UDP :
13812393Syz155240 tcp = fin->fin_dp;
13822393Syz155240
13832393Syz155240 is->is_sport = htons(fin->fin_data[0]);
13842393Syz155240 is->is_dport = htons(fin->fin_data[1]);
13852393Syz155240 if ((flags & (SI_W_DPORT|SI_W_SPORT)) == 0) {
13862393Syz155240 hv += tcp->th_dport;
13872393Syz155240 hv += tcp->th_sport;
13882393Syz155240 }
13893448Sdh155122 ATOMIC_INCL(ifs->ifs_ips_stats.iss_udp);
13902393Syz155240 break;
13912393Syz155240
13922393Syz155240 default :
13932393Syz155240 break;
13942393Syz155240 }
13953448Sdh155122 hv = DOUBLE_HASH(hv, ifs);
13962393Syz155240 is->is_hv = hv;
13972393Syz155240 is->is_rule = fr;
13982393Syz155240 is->is_flags = flags & IS_INHERITED;
13992393Syz155240
14002393Syz155240 /*
14012393Syz155240 * Look for identical state.
14022393Syz155240 */
14033448Sdh155122 for (is = ifs->ifs_ips_table[is->is_hv % ifs->ifs_fr_statesize];
14043448Sdh155122 is != NULL;
14052393Syz155240 is = is->is_hnext) {
14066252San207044 if (fr_matchstates(&ips, is) == 1)
14072393Syz155240 break;
14082393Syz155240 }
14097704SAlexandr.Nedvedicky@Sun.COM
14106252San207044 /*
14116252San207044 * we've found a matching state -> state already exists,
14126252San207044 * we are not going to add a duplicate record.
14136252San207044 */
14142393Syz155240 if (is != NULL)
14152393Syz155240 return NULL;
14162393Syz155240
14173448Sdh155122 if (ifs->ifs_ips_stats.iss_bucketlen[hv] >= ifs->ifs_fr_state_maxbucket) {
14183448Sdh155122 ATOMIC_INCL(ifs->ifs_ips_stats.iss_bucketfull);
14192393Syz155240 return NULL;
14202393Syz155240 }
14212393Syz155240 KMALLOC(is, ipstate_t *);
14222393Syz155240 if (is == NULL) {
14233448Sdh155122 ATOMIC_INCL(ifs->ifs_ips_stats.iss_nomem);
14242393Syz155240 return NULL;
14252393Syz155240 }
14262393Syz155240 bcopy((char *)&ips, (char *)is, sizeof(*is));
14272393Syz155240 /*
14282393Syz155240 * Do not do the modulous here, it is done in fr_stinsert().
14292393Syz155240 */
14302393Syz155240 if (fr != NULL) {
14312393Syz155240 (void) strncpy(is->is_group, fr->fr_group, FR_GROUPLEN);
14322393Syz155240 if (fr->fr_age[0] != 0) {
14333448Sdh155122 is->is_tqehead[0] =
14343448Sdh155122 fr_addtimeoutqueue(&ifs->ifs_ips_utqe,
14353448Sdh155122 fr->fr_age[0], ifs);
14362393Syz155240 is->is_sti.tqe_flags |= TQE_RULEBASED;
14372393Syz155240 }
14382393Syz155240 if (fr->fr_age[1] != 0) {
14393448Sdh155122 is->is_tqehead[1] =
14403448Sdh155122 fr_addtimeoutqueue(&ifs->ifs_ips_utqe,
14413448Sdh155122 fr->fr_age[1], ifs);
14422393Syz155240 is->is_sti.tqe_flags |= TQE_RULEBASED;
14432393Syz155240 }
14442393Syz155240 is->is_tag = fr->fr_logtag;
14452393Syz155240
14462393Syz155240 is->is_ifp[(out << 1) + 1] = fr->fr_ifas[1];
14472393Syz155240 is->is_ifp[(1 - out) << 1] = fr->fr_ifas[2];
14482393Syz155240 is->is_ifp[((1 - out) << 1) + 1] = fr->fr_ifas[3];
14492393Syz155240
14502393Syz155240 if (((ifp = fr->fr_ifas[1]) != NULL) &&
14512393Syz155240 (ifp != (void *)-1)) {
14522958Sdr146992 COPYIFNAME(ifp, is->is_ifname[(out << 1) + 1], fr->fr_v);
14532393Syz155240 }
14542393Syz155240 if (((ifp = fr->fr_ifas[2]) != NULL) &&
14552393Syz155240 (ifp != (void *)-1)) {
14562958Sdr146992 COPYIFNAME(ifp, is->is_ifname[(1 - out) << 1], fr->fr_v);
14572393Syz155240 }
14582393Syz155240 if (((ifp = fr->fr_ifas[3]) != NULL) &&
14592393Syz155240 (ifp != (void *)-1)) {
14602958Sdr146992 COPYIFNAME(ifp, is->is_ifname[((1 - out) << 1) + 1], fr->fr_v);
14612393Syz155240 }
14622393Syz155240 }
14632393Syz155240
14642393Syz155240 is->is_ifp[out << 1] = fin->fin_ifp;
14652393Syz155240 if (fin->fin_ifp != NULL) {
14668463SDarren.Reed@Sun.COM COPYIFNAME(fin->fin_ifp, is->is_ifname[out << 1], fin->fin_v);
14672393Syz155240 }
14682393Syz155240
14698624SDarren.Reed@Sun.COM is->is_ref = 1;
14702393Syz155240 is->is_pkts[0] = 0, is->is_bytes[0] = 0;
14712393Syz155240 is->is_pkts[1] = 0, is->is_bytes[1] = 0;
14722393Syz155240 is->is_pkts[2] = 0, is->is_bytes[2] = 0;
14732393Syz155240 is->is_pkts[3] = 0, is->is_bytes[3] = 0;
14742393Syz155240 if ((fin->fin_flx & FI_IGNORE) == 0) {
14752393Syz155240 is->is_pkts[out] = 1;
14762393Syz155240 is->is_bytes[out] = fin->fin_plen;
14772393Syz155240 is->is_flx[out][0] = fin->fin_flx & FI_CMP;
14782393Syz155240 is->is_flx[out][0] &= ~FI_OOW;
14792393Syz155240 }
14802393Syz155240
14812393Syz155240 if (pass & FR_STSTRICT)
14822393Syz155240 is->is_flags |= IS_STRICT;
14832393Syz155240
14842393Syz155240 if (pass & FR_STATESYNC)
14852393Syz155240 is->is_flags |= IS_STATESYNC;
14862393Syz155240
14872393Syz155240 if (flags & (SI_WILDP|SI_WILDA)) {
14883448Sdh155122 ATOMIC_INCL(ifs->ifs_ips_stats.iss_wild);
14892393Syz155240 }
14902393Syz155240 is->is_rulen = fin->fin_rule;
14912393Syz155240
14922393Syz155240
14932393Syz155240 if (pass & FR_LOGFIRST)
14942393Syz155240 is->is_pass &= ~(FR_LOGFIRST|FR_LOG);
14952393Syz155240
14963448Sdh155122 READ_ENTER(&ifs->ifs_ipf_state);
14972393Syz155240 is->is_me = stsave;
14982393Syz155240
14993448Sdh155122 fr_stinsert(is, fin->fin_rev, ifs);
15002393Syz155240
15012393Syz155240 if (fin->fin_p == IPPROTO_TCP) {
15022393Syz155240 /*
15032393Syz155240 * If we're creating state for a starting connection, start the
15042393Syz155240 * timer on it as we'll never see an error if it fails to
15052393Syz155240 * connect.
15062393Syz155240 */
15073448Sdh155122 (void) fr_tcp_age(&is->is_sti, fin, ifs->ifs_ips_tqtqb,
15083448Sdh155122 is->is_flags);
15092393Syz155240 MUTEX_EXIT(&is->is_lock);
15102393Syz155240 #ifdef IPFILTER_SCAN
15112393Syz155240 if ((is->is_flags & SI_CLONE) == 0)
15122393Syz155240 (void) ipsc_attachis(is);
15132393Syz155240 #endif
15142393Syz155240 } else {
15152393Syz155240 MUTEX_EXIT(&is->is_lock);
15162393Syz155240 }
15172393Syz155240 #ifdef IPFILTER_SYNC
15182393Syz155240 if ((is->is_flags & IS_STATESYNC) && ((is->is_flags & SI_CLONE) == 0))
15192393Syz155240 is->is_sync = ipfsync_new(SMC_STATE, fin, is);
15202393Syz155240 #endif
15213448Sdh155122 if (ifs->ifs_ipstate_logging)
15223448Sdh155122 ipstate_log(is, ISL_NEW, ifs);
15233448Sdh155122
15243448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
15252393Syz155240 fin->fin_rev = IP6_NEQ(&is->is_dst, &fin->fin_daddr);
15262393Syz155240 fin->fin_flx |= FI_STATE;
15272393Syz155240 if (fin->fin_flx & FI_FRAG)
15282393Syz155240 (void) fr_newfrag(fin, pass ^ FR_KEEPSTATE);
15292393Syz155240
15302393Syz155240 return is;
15312393Syz155240 }
15322393Syz155240
15332393Syz155240
15342393Syz155240 /* ------------------------------------------------------------------------ */
15352393Syz155240 /* Function: fr_tcpoptions */
15362393Syz155240 /* Returns: int - 1 == packet matches state entry, 0 == it does not */
15372393Syz155240 /* Parameters: fin(I) - pointer to packet information */
15382393Syz155240 /* tcp(I) - pointer to TCP packet header */
15392393Syz155240 /* td(I) - pointer to TCP data held as part of the state */
15402393Syz155240 /* */
15412393Syz155240 /* Look after the TCP header for any options and deal with those that are */
15422393Syz155240 /* present. Record details about those that we recogise. */
15432393Syz155240 /* ------------------------------------------------------------------------ */
fr_tcpoptions(fin,tcp,td)15442393Syz155240 static int fr_tcpoptions(fin, tcp, td)
15452393Syz155240 fr_info_t *fin;
15462393Syz155240 tcphdr_t *tcp;
15472393Syz155240 tcpdata_t *td;
15482393Syz155240 {
15492393Syz155240 int off, mlen, ol, i, len, retval;
15502393Syz155240 char buf[64], *s, opt;
15512393Syz155240 mb_t *m = NULL;
15522393Syz155240
15532393Syz155240 len = (TCP_OFF(tcp) << 2);
15542393Syz155240 if (fin->fin_dlen < len)
15552393Syz155240 return 0;
15562393Syz155240 len -= sizeof(*tcp);
15572393Syz155240
15582393Syz155240 off = fin->fin_plen - fin->fin_dlen + sizeof(*tcp) + fin->fin_ipoff;
15592393Syz155240
15602393Syz155240 m = fin->fin_m;
15612393Syz155240 mlen = MSGDSIZE(m) - off;
15622393Syz155240 if (len > mlen) {
15632393Syz155240 len = mlen;
15642393Syz155240 retval = 0;
15652393Syz155240 } else {
15662393Syz155240 retval = 1;
15672393Syz155240 }
15682393Syz155240
15692393Syz155240 COPYDATA(m, off, len, buf);
15702393Syz155240
15712393Syz155240 for (s = buf; len > 0; ) {
15722393Syz155240 opt = *s;
15732393Syz155240 if (opt == TCPOPT_EOL)
15742393Syz155240 break;
15752393Syz155240 else if (opt == TCPOPT_NOP)
15762393Syz155240 ol = 1;
15772393Syz155240 else {
15782393Syz155240 if (len < 2)
15792393Syz155240 break;
15802393Syz155240 ol = (int)*(s + 1);
15812393Syz155240 if (ol < 2 || ol > len)
15822393Syz155240 break;
15832393Syz155240
15842393Syz155240 /*
15852393Syz155240 * Extract the TCP options we are interested in out of
15862393Syz155240 * the header and store them in the the tcpdata struct.
15872393Syz155240 */
15882393Syz155240 switch (opt)
15892393Syz155240 {
15902393Syz155240 case TCPOPT_WINDOW :
15912393Syz155240 if (ol == TCPOLEN_WINDOW) {
15922393Syz155240 i = (int)*(s + 2);
15932393Syz155240 if (i > TCP_WSCALE_MAX)
15942393Syz155240 i = TCP_WSCALE_MAX;
15952393Syz155240 else if (i < 0)
15962393Syz155240 i = 0;
15972393Syz155240 td->td_winscale = i;
15987704SAlexandr.Nedvedicky@Sun.COM td->td_winflags |= TCP_WSCALE_SEEN |
15997704SAlexandr.Nedvedicky@Sun.COM TCP_WSCALE_FIRST;
16006647San207044 } else
16016647San207044 retval = -1;
16022393Syz155240 break;
16032393Syz155240 case TCPOPT_MAXSEG :
16042393Syz155240 /*
16052393Syz155240 * So, if we wanted to set the TCP MAXSEG,
16062393Syz155240 * it should be done here...
16072393Syz155240 */
16082393Syz155240 if (ol == TCPOLEN_MAXSEG) {
16092393Syz155240 i = (int)*(s + 2);
16102393Syz155240 i <<= 8;
16112393Syz155240 i += (int)*(s + 3);
16122393Syz155240 td->td_maxseg = i;
16136647San207044 } else
16146647San207044 retval = -1;
16156647San207044 break;
16166647San207044 case TCPOPT_SACK_PERMITTED :
16176647San207044 if (ol == TCPOLEN_SACK_PERMITTED)
16186647San207044 td->td_winflags |= TCP_SACK_PERMIT;
16196647San207044 else
16206647San207044 retval = -1;
16212393Syz155240 break;
16222393Syz155240 }
16232393Syz155240 }
16242393Syz155240 len -= ol;
16252393Syz155240 s += ol;
16262393Syz155240 }
16272393Syz155240 return retval;
16282393Syz155240 }
16292393Syz155240
16302393Syz155240
16312393Syz155240 /* ------------------------------------------------------------------------ */
16322393Syz155240 /* Function: fr_tcpstate */
16332393Syz155240 /* Returns: int - 1 == packet matches state entry, 0 == it does not */
16342393Syz155240 /* Parameters: fin(I) - pointer to packet information */
16352393Syz155240 /* tcp(I) - pointer to TCP packet header */
16362393Syz155240 /* is(I) - pointer to master state structure */
16372393Syz155240 /* */
16382393Syz155240 /* Check to see if a packet with TCP headers fits within the TCP window. */
16392393Syz155240 /* Change timeout depending on whether new packet is a SYN-ACK returning */
16402393Syz155240 /* for a SYN or a RST or FIN which indicate time to close up shop. */
16412393Syz155240 /* ------------------------------------------------------------------------ */
fr_tcpstate(fin,tcp,is)16422393Syz155240 static int fr_tcpstate(fin, tcp, is)
16432393Syz155240 fr_info_t *fin;
16442393Syz155240 tcphdr_t *tcp;
16452393Syz155240 ipstate_t *is;
16462393Syz155240 {
16472393Syz155240 int source, ret = 0, flags;
16482393Syz155240 tcpdata_t *fdata, *tdata;
16493448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs;
16502393Syz155240
16512393Syz155240 source = !fin->fin_rev;
16522393Syz155240 if (((is->is_flags & IS_TCPFSM) != 0) && (source == 1) &&
16532393Syz155240 (ntohs(is->is_sport) != fin->fin_data[0]))
16542393Syz155240 source = 0;
16552393Syz155240 fdata = &is->is_tcp.ts_data[!source];
16562393Syz155240 tdata = &is->is_tcp.ts_data[source];
16572393Syz155240
16582393Syz155240 MUTEX_ENTER(&is->is_lock);
16594431San207044
16604431San207044 /*
16614431San207044 * If a SYN packet is received for a connection that is in a half
16624431San207044 * closed state, then move its state entry to deletetq. In such case
16634431San207044 * the SYN packet will be consequently dropped. This allows new state
16644431San207044 * entry to be created with a retransmited SYN packet.
16654431San207044 */
16664431San207044 if ((tcp->th_flags & TH_OPENING) == TH_SYN) {
16679888SAlexandr.Nedvedicky@Sun.COM if ((is->is_state[source] > IPF_TCPS_ESTABLISHED) &&
16689888SAlexandr.Nedvedicky@Sun.COM (is->is_state[!source] > IPF_TCPS_ESTABLISHED)) {
16699888SAlexandr.Nedvedicky@Sun.COM is->is_state[source] = IPF_TCPS_CLOSED;
16709888SAlexandr.Nedvedicky@Sun.COM is->is_state[!source] = IPF_TCPS_CLOSED;
16714431San207044 /*
16724431San207044 * Do not update is->is_sti.tqe_die in case state entry
16734431San207044 * is already present in deletetq. It prevents state
16744431San207044 * entry ttl update by retransmitted SYN packets, which
16754431San207044 * may arrive before timer tick kicks off. The SYN
16764431San207044 * packet will be dropped again.
16774431San207044 */
16784431San207044 if (is->is_sti.tqe_ifq != &ifs->ifs_ips_deletetq)
16794431San207044 fr_movequeue(&is->is_sti, is->is_sti.tqe_ifq,
16804431San207044 &fin->fin_ifs->ifs_ips_deletetq,
16814431San207044 fin->fin_ifs);
16824431San207044
16834431San207044 MUTEX_EXIT(&is->is_lock);
16844431San207044 return 0;
16854431San207044 }
16864431San207044 }
16874431San207044
16882393Syz155240 if (fr_tcpinwindow(fin, fdata, tdata, tcp, is->is_flags)) {
16892393Syz155240 #ifdef IPFILTER_SCAN
16902393Syz155240 if (is->is_flags & (IS_SC_CLIENT|IS_SC_SERVER)) {
16912393Syz155240 ipsc_packet(fin, is);
16922393Syz155240 if (FR_ISBLOCK(is->is_pass)) {
16932393Syz155240 MUTEX_EXIT(&is->is_lock);
16942393Syz155240 return 1;
16952393Syz155240 }
16962393Syz155240 }
16972393Syz155240 #endif
16982393Syz155240
16992393Syz155240 /*
17002393Syz155240 * Nearing end of connection, start timeout.
17012393Syz155240 */
17023448Sdh155122 ret = fr_tcp_age(&is->is_sti, fin, ifs->ifs_ips_tqtqb,
17033448Sdh155122 is->is_flags);
17042393Syz155240 if (ret == 0) {
17052393Syz155240 MUTEX_EXIT(&is->is_lock);
17062393Syz155240 return 0;
17072393Syz155240 }
17082393Syz155240
17092393Syz155240 /*
17102393Syz155240 * set s0's as appropriate. Use syn-ack packet as it
17112393Syz155240 * contains both pieces of required information.
17122393Syz155240 */
17132393Syz155240 /*
17142393Syz155240 * Window scale option is only present in SYN/SYN-ACK packet.
17152393Syz155240 * Compare with ~TH_FIN to mask out T/TCP setups.
17162393Syz155240 */
17172393Syz155240 flags = tcp->th_flags & ~(TH_FIN|TH_ECNALL);
17182393Syz155240 if (flags == (TH_SYN|TH_ACK)) {
17192393Syz155240 is->is_s0[source] = ntohl(tcp->th_ack);
17202393Syz155240 is->is_s0[!source] = ntohl(tcp->th_seq) + 1;
17217704SAlexandr.Nedvedicky@Sun.COM if (TCP_OFF(tcp) > (sizeof (tcphdr_t) >> 2)) {
17226647San207044 (void) fr_tcpoptions(fin, tcp, fdata);
17232393Syz155240 }
17242393Syz155240 if ((fin->fin_out != 0) && (is->is_pass & FR_NEWISN))
17252393Syz155240 fr_checknewisn(fin, is);
17262393Syz155240 } else if (flags == TH_SYN) {
17272393Syz155240 is->is_s0[source] = ntohl(tcp->th_seq) + 1;
17282393Syz155240 if ((TCP_OFF(tcp) > (sizeof(tcphdr_t) >> 2)))
17296647San207044 (void) fr_tcpoptions(fin, tcp, tdata);
17302393Syz155240
17312393Syz155240 if ((fin->fin_out != 0) && (is->is_pass & FR_NEWISN))
17322393Syz155240 fr_checknewisn(fin, is);
17332393Syz155240
17342393Syz155240 }
17352393Syz155240 ret = 1;
17362393Syz155240 } else
17372393Syz155240 fin->fin_flx |= FI_OOW;
17382393Syz155240 MUTEX_EXIT(&is->is_lock);
17392393Syz155240 return ret;
17402393Syz155240 }
17412393Syz155240
17422393Syz155240
17432393Syz155240 /* ------------------------------------------------------------------------ */
17442393Syz155240 /* Function: fr_checknewisn */
17452393Syz155240 /* Returns: Nil */
17462393Syz155240 /* Parameters: fin(I) - pointer to packet information */
17472393Syz155240 /* is(I) - pointer to master state structure */
17482393Syz155240 /* */
17492393Syz155240 /* Check to see if this TCP connection is expecting and needs a new */
17502393Syz155240 /* sequence number for a particular direction of the connection. */
17512393Syz155240 /* */
17522393Syz155240 /* NOTE: This does not actually change the sequence numbers, only gets new */
17532393Syz155240 /* one ready. */
17542393Syz155240 /* ------------------------------------------------------------------------ */
fr_checknewisn(fin,is)17552393Syz155240 static void fr_checknewisn(fin, is)
17562393Syz155240 fr_info_t *fin;
17572393Syz155240 ipstate_t *is;
17582393Syz155240 {
17592393Syz155240 u_32_t sumd, old, new;
17602393Syz155240 tcphdr_t *tcp;
17612393Syz155240 int i;
17622393Syz155240
17632393Syz155240 i = fin->fin_rev;
17642393Syz155240 tcp = fin->fin_dp;
17652393Syz155240
17662393Syz155240 if (((i == 0) && !(is->is_flags & IS_ISNSYN)) ||
17672393Syz155240 ((i == 1) && !(is->is_flags & IS_ISNACK))) {
17682393Syz155240 old = ntohl(tcp->th_seq);
17692393Syz155240 new = fr_newisn(fin);
17702393Syz155240 is->is_isninc[i] = new - old;
17712393Syz155240 CALC_SUMD(old, new, sumd);
17722393Syz155240 is->is_sumd[i] = (sumd & 0xffff) + (sumd >> 16);
17732393Syz155240
17742393Syz155240 is->is_flags |= ((i == 0) ? IS_ISNSYN : IS_ISNACK);
17752393Syz155240 }
17762393Syz155240 }
17772393Syz155240
17782393Syz155240
17792393Syz155240 /* ------------------------------------------------------------------------ */
17802393Syz155240 /* Function: fr_tcpinwindow */
17812393Syz155240 /* Returns: int - 1 == packet inside TCP "window", 0 == not inside. */
17822393Syz155240 /* Parameters: fin(I) - pointer to packet information */
17832393Syz155240 /* fdata(I) - pointer to tcp state informatio (forward) */
17842393Syz155240 /* tdata(I) - pointer to tcp state informatio (reverse) */
17852393Syz155240 /* tcp(I) - pointer to TCP packet header */
17862393Syz155240 /* */
17872393Syz155240 /* Given a packet has matched addresses and ports, check to see if it is */
17882393Syz155240 /* within the TCP data window. In a show of generosity, allow packets that */
17892393Syz155240 /* are within the window space behind the current sequence # as well. */
17902393Syz155240 /* ------------------------------------------------------------------------ */
fr_tcpinwindow(fin,fdata,tdata,tcp,flags)17912393Syz155240 int fr_tcpinwindow(fin, fdata, tdata, tcp, flags)
17922393Syz155240 fr_info_t *fin;
17932393Syz155240 tcpdata_t *fdata, *tdata;
17942393Syz155240 tcphdr_t *tcp;
17952393Syz155240 int flags;
17962393Syz155240 {
17972393Syz155240 tcp_seq seq, ack, end;
17982393Syz155240 int ackskew, tcpflags;
17992393Syz155240 u_32_t win, maxwin;
18006647San207044 int dsize, inseq;
18012393Syz155240
18022393Syz155240 /*
18032393Syz155240 * Find difference between last checked packet and this packet.
18042393Syz155240 */
18052393Syz155240 tcpflags = tcp->th_flags;
18062393Syz155240 seq = ntohl(tcp->th_seq);
18072393Syz155240 ack = ntohl(tcp->th_ack);
18086647San207044
18092393Syz155240 if (tcpflags & TH_SYN)
18102393Syz155240 win = ntohs(tcp->th_win);
18112393Syz155240 else
18122393Syz155240 win = ntohs(tcp->th_win) << fdata->td_winscale;
18137704SAlexandr.Nedvedicky@Sun.COM
18146647San207044 /*
18156647San207044 * win 0 means the receiving endpoint has closed the window, because it
18166647San207044 * has not enough memory to receive data from sender. In such case we
18176647San207044 * are pretending window size to be 1 to let TCP probe data through.
18186647San207044 * TCP probe data can be either 0 or 1 octet of data, the RFC does not
18196647San207044 * state this accurately, so we have to allow 1 octet (win = 1) even if
18206647San207044 * the window is closed (win == 0).
18216647San207044 */
18222393Syz155240 if (win == 0)
18232393Syz155240 win = 1;
18242393Syz155240
18256647San207044 dsize = fin->fin_dlen - (TCP_OFF(tcp) << 2) +
18267704SAlexandr.Nedvedicky@Sun.COM ((tcpflags & TH_SYN) ? 1 : 0) + ((tcpflags & TH_FIN) ? 1 : 0);
18276647San207044
18282393Syz155240 /*
18292393Syz155240 * if window scaling is present, the scaling is only allowed
18302393Syz155240 * for windows not in the first SYN packet. In that packet the
18312393Syz155240 * window is 65535 to specify the largest window possible
18322393Syz155240 * for receivers not implementing the window scale option.
18332393Syz155240 * Currently, we do not assume TTCP here. That means that
18342393Syz155240 * if we see a second packet from a host (after the initial
18352393Syz155240 * SYN), we can assume that the receiver of the SYN did
18362393Syz155240 * already send back the SYN/ACK (and thus that we know if
18372393Syz155240 * the receiver also does window scaling)
18382393Syz155240 */
18392393Syz155240 if (!(tcpflags & TH_SYN) && (fdata->td_winflags & TCP_WSCALE_FIRST)) {
18406647San207044 fdata->td_maxwin = win;
18412393Syz155240 }
18422393Syz155240
18436647San207044 end = seq + dsize;
18442393Syz155240
18452393Syz155240 if ((fdata->td_end == 0) &&
18462393Syz155240 (!(flags & IS_TCPFSM) ||
18472393Syz155240 ((tcpflags & TH_OPENING) == TH_OPENING))) {
18482393Syz155240 /*
18492393Syz155240 * Must be a (outgoing) SYN-ACK in reply to a SYN.
18502393Syz155240 */
18516647San207044 fdata->td_end = end - 1;
18522393Syz155240 fdata->td_maxwin = 1;
18532393Syz155240 fdata->td_maxend = end + win;
18542393Syz155240 }
18552393Syz155240
18562393Syz155240 if (!(tcpflags & TH_ACK)) { /* Pretend an ack was sent */
18572393Syz155240 ack = tdata->td_end;
18582393Syz155240 } else if (((tcpflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) &&
18592393Syz155240 (ack == 0)) {
18602393Syz155240 /* gross hack to get around certain broken tcp stacks */
18612393Syz155240 ack = tdata->td_end;
18622393Syz155240 }
18632393Syz155240
18642393Syz155240 maxwin = tdata->td_maxwin;
18652393Syz155240 ackskew = tdata->td_end - ack;
18662393Syz155240
18672393Syz155240 /*
18682393Syz155240 * Strict sequencing only allows in-order delivery.
18692393Syz155240 */
18702393Syz155240 if ((flags & IS_STRICT) != 0) {
18712393Syz155240 if (seq != fdata->td_end) {
18727704SAlexandr.Nedvedicky@Sun.COM DTRACE_PROBE(strict_check);
18732393Syz155240 return 0;
18742393Syz155240 }
18752393Syz155240 }
18762393Syz155240
18772393Syz155240 #define SEQ_GE(a,b) ((int)((a) - (b)) >= 0)
18782393Syz155240 #define SEQ_GT(a,b) ((int)((a) - (b)) > 0)
18796647San207044 inseq = 0;
18807704SAlexandr.Nedvedicky@Sun.COM DTRACE_PROBE4(
18817704SAlexandr.Nedvedicky@Sun.COM dyn_params,
18827704SAlexandr.Nedvedicky@Sun.COM int, dsize,
18837704SAlexandr.Nedvedicky@Sun.COM int, ackskew,
18847704SAlexandr.Nedvedicky@Sun.COM int, maxwin,
18857704SAlexandr.Nedvedicky@Sun.COM int, win
18867704SAlexandr.Nedvedicky@Sun.COM );
18872393Syz155240 if (
18882393Syz155240 #if defined(_KERNEL)
18897704SAlexandr.Nedvedicky@Sun.COM /*
18907704SAlexandr.Nedvedicky@Sun.COM * end <-> s + n
18917704SAlexandr.Nedvedicky@Sun.COM * maxend <-> ack + win
18927704SAlexandr.Nedvedicky@Sun.COM * this is upperbound check
18937704SAlexandr.Nedvedicky@Sun.COM */
18942393Syz155240 (SEQ_GE(fdata->td_maxend, end)) &&
18957704SAlexandr.Nedvedicky@Sun.COM /*
18967704SAlexandr.Nedvedicky@Sun.COM * this is lowerbound check
18977704SAlexandr.Nedvedicky@Sun.COM */
18982393Syz155240 (SEQ_GE(seq, fdata->td_end - maxwin)) &&
18992393Syz155240 #endif
19002393Syz155240 /* XXX what about big packets */
19012393Syz155240 #define MAXACKWINDOW 66000
19022393Syz155240 (-ackskew <= (MAXACKWINDOW << fdata->td_winscale)) &&
19032393Syz155240 ( ackskew <= (MAXACKWINDOW << fdata->td_winscale))) {
19046647San207044 inseq = 1;
19056647San207044 /*
19066647San207044 * Microsoft Windows will send the next packet to the right of the
19076647San207044 * window if SACK is in use.
19086647San207044 */
19096647San207044 } else if ((seq == fdata->td_maxend) && (ackskew == 0) &&
19106647San207044 (fdata->td_winflags & TCP_SACK_PERMIT) &&
19116647San207044 (tdata->td_winflags & TCP_SACK_PERMIT)) {
19126647San207044 inseq = 1;
19136647San207044 /*
19146647San207044 * RST ACK with SEQ equal to 0 is sent by some OSes (i.e. Solaris) as a
19156647San207044 * response to initial SYN packet, when there is no application
19166647San207044 * listeing to on a port, where the SYN packet has came to.
19176647San207044 */
19186647San207044 } else if ((seq == 0) && (tcpflags == (TH_RST|TH_ACK)) &&
19197704SAlexandr.Nedvedicky@Sun.COM (ackskew >= -1) && (ackskew <= 1)) {
19206647San207044 inseq = 1;
19216647San207044 } else if (!(flags & IS_TCPFSM)) {
19226647San207044
19236647San207044 if (!(fdata->td_winflags &
19246647San207044 (TCP_WSCALE_SEEN|TCP_WSCALE_FIRST))) {
19256647San207044 /*
19266647San207044 * No TCPFSM and no window scaling, so make some
19276647San207044 * extra guesses.
19286647San207044 */
19296647San207044 if ((seq == fdata->td_maxend) && (ackskew == 0))
19306647San207044 inseq = 1;
19316647San207044 else if (SEQ_GE(seq + maxwin, fdata->td_end - maxwin))
19326647San207044 inseq = 1;
19336647San207044 }
19346647San207044 }
19356647San207044
19366647San207044 if (inseq) {
19372393Syz155240 /* if ackskew < 0 then this should be due to fragmented
19382393Syz155240 * packets. There is no way to know the length of the
19392393Syz155240 * total packet in advance.
19402393Syz155240 * We do know the total length from the fragment cache though.
19412393Syz155240 * Note however that there might be more sessions with
19422393Syz155240 * exactly the same source and destination parameters in the
19432393Syz155240 * state cache (and source and destination is the only stuff
19442393Syz155240 * that is saved in the fragment cache). Note further that
19452393Syz155240 * some TCP connections in the state cache are hashed with
19462393Syz155240 * sport and dport as well which makes it not worthwhile to
19472393Syz155240 * look for them.
19482393Syz155240 * Thus, when ackskew is negative but still seems to belong
19492393Syz155240 * to this session, we bump up the destinations end value.
19502393Syz155240 */
19517704SAlexandr.Nedvedicky@Sun.COM if (ackskew < 0) {
19527704SAlexandr.Nedvedicky@Sun.COM DTRACE_PROBE2(end_update_td,
19537704SAlexandr.Nedvedicky@Sun.COM int, tdata->td_end,
19547704SAlexandr.Nedvedicky@Sun.COM int, ack
19557704SAlexandr.Nedvedicky@Sun.COM );
19562393Syz155240 tdata->td_end = ack;
19577704SAlexandr.Nedvedicky@Sun.COM }
19582393Syz155240
19592393Syz155240 /* update max window seen */
19607704SAlexandr.Nedvedicky@Sun.COM if (fdata->td_maxwin < win) {
19617704SAlexandr.Nedvedicky@Sun.COM DTRACE_PROBE2(win_update_fd,
19627704SAlexandr.Nedvedicky@Sun.COM int, fdata->td_maxwin,
19637704SAlexandr.Nedvedicky@Sun.COM int, win
19647704SAlexandr.Nedvedicky@Sun.COM );
19652393Syz155240 fdata->td_maxwin = win;
19667704SAlexandr.Nedvedicky@Sun.COM }
19677704SAlexandr.Nedvedicky@Sun.COM
19687704SAlexandr.Nedvedicky@Sun.COM if (SEQ_GT(end, fdata->td_end)) {
19697704SAlexandr.Nedvedicky@Sun.COM DTRACE_PROBE2(end_update_fd,
19707704SAlexandr.Nedvedicky@Sun.COM int, fdata->td_end,
19717704SAlexandr.Nedvedicky@Sun.COM int, end
19727704SAlexandr.Nedvedicky@Sun.COM );
19732393Syz155240 fdata->td_end = end;
19747704SAlexandr.Nedvedicky@Sun.COM }
19757704SAlexandr.Nedvedicky@Sun.COM
19767704SAlexandr.Nedvedicky@Sun.COM if (SEQ_GE(ack + win, tdata->td_maxend)) {
19777704SAlexandr.Nedvedicky@Sun.COM DTRACE_PROBE2(max_end_update_td,
19787704SAlexandr.Nedvedicky@Sun.COM int, tdata->td_maxend,
19797704SAlexandr.Nedvedicky@Sun.COM int, ack + win
19807704SAlexandr.Nedvedicky@Sun.COM );
19812393Syz155240 tdata->td_maxend = ack + win;
19827704SAlexandr.Nedvedicky@Sun.COM }
19837704SAlexandr.Nedvedicky@Sun.COM
19842393Syz155240 return 1;
19852393Syz155240 }
19866647San207044 fin->fin_flx |= FI_OOW;
19877704SAlexandr.Nedvedicky@Sun.COM
19887704SAlexandr.Nedvedicky@Sun.COM #if defined(_KERNEL)
19897704SAlexandr.Nedvedicky@Sun.COM if (!(SEQ_GE(seq, fdata->td_end - maxwin)))
19907704SAlexandr.Nedvedicky@Sun.COM fin->fin_flx |= FI_NEG_OOW;
19917704SAlexandr.Nedvedicky@Sun.COM #endif
19927704SAlexandr.Nedvedicky@Sun.COM
19932393Syz155240 return 0;
19942393Syz155240 }
19952393Syz155240
19962393Syz155240
19972393Syz155240 /* ------------------------------------------------------------------------ */
19982393Syz155240 /* Function: fr_stclone */
19992393Syz155240 /* Returns: ipstate_t* - NULL == cloning failed, */
20002393Syz155240 /* else pointer to new state structure */
20012393Syz155240 /* Parameters: fin(I) - pointer to packet information */
20022393Syz155240 /* tcp(I) - pointer to TCP/UDP header */
20032393Syz155240 /* is(I) - pointer to master state structure */
20042393Syz155240 /* */
20052393Syz155240 /* Create a "duplcate" state table entry from the master. */
20062393Syz155240 /* ------------------------------------------------------------------------ */
fr_stclone(fin,tcp,is)20072393Syz155240 static ipstate_t *fr_stclone(fin, tcp, is)
20082393Syz155240 fr_info_t *fin;
20092393Syz155240 tcphdr_t *tcp;
20102393Syz155240 ipstate_t *is;
20112393Syz155240 {
20122393Syz155240 ipstate_t *clone;
20132393Syz155240 u_32_t send;
20143448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs;
20153448Sdh155122
20168170SJohn.Ojemann@Sun.COM /*
20178170SJohn.Ojemann@Sun.COM * Trigger automatic call to fr_state_flush() if the
20188170SJohn.Ojemann@Sun.COM * table has reached capacity specified by hi watermark.
20198170SJohn.Ojemann@Sun.COM */
20208170SJohn.Ojemann@Sun.COM if (ST_TAB_WATER_LEVEL(ifs) > ifs->ifs_state_flush_level_hi)
20218170SJohn.Ojemann@Sun.COM ifs->ifs_fr_state_doflush = 1;
20228170SJohn.Ojemann@Sun.COM
20238170SJohn.Ojemann@Sun.COM /*
20248170SJohn.Ojemann@Sun.COM * If automatic flushing did not do its job, and the table
20258170SJohn.Ojemann@Sun.COM * has filled up, don't try to create a new entry. A NULL
20268170SJohn.Ojemann@Sun.COM * return will indicate that the cloning has failed.
20278170SJohn.Ojemann@Sun.COM */
20288170SJohn.Ojemann@Sun.COM if (ifs->ifs_ips_num >= ifs->ifs_fr_statemax) {
20293448Sdh155122 ATOMIC_INCL(ifs->ifs_ips_stats.iss_max);
20302393Syz155240 return NULL;
20312393Syz155240 }
20328170SJohn.Ojemann@Sun.COM
20332393Syz155240 KMALLOC(clone, ipstate_t *);
20342393Syz155240 if (clone == NULL)
20352393Syz155240 return NULL;
20362393Syz155240 bcopy((char *)is, (char *)clone, sizeof(*clone));
20372393Syz155240
20382393Syz155240 MUTEX_NUKE(&clone->is_lock);
20392393Syz155240
20403448Sdh155122 clone->is_die = ONE_DAY + ifs->ifs_fr_ticks;
20412393Syz155240 clone->is_state[0] = 0;
20422393Syz155240 clone->is_state[1] = 0;
20432393Syz155240 send = ntohl(tcp->th_seq) + fin->fin_dlen - (TCP_OFF(tcp) << 2) +
20442393Syz155240 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
20452393Syz155240 ((tcp->th_flags & TH_FIN) ? 1 : 0);
20462393Syz155240
20472393Syz155240 if (fin->fin_rev == 1) {
20482393Syz155240 clone->is_dend = send;
20492393Syz155240 clone->is_maxdend = send;
20502393Syz155240 clone->is_send = 0;
20512393Syz155240 clone->is_maxswin = 1;
20522393Syz155240 clone->is_maxdwin = ntohs(tcp->th_win);
20532393Syz155240 if (clone->is_maxdwin == 0)
20542393Syz155240 clone->is_maxdwin = 1;
20552393Syz155240 } else {
20562393Syz155240 clone->is_send = send;
20572393Syz155240 clone->is_maxsend = send;
20582393Syz155240 clone->is_dend = 0;
20592393Syz155240 clone->is_maxdwin = 1;
20602393Syz155240 clone->is_maxswin = ntohs(tcp->th_win);
20612393Syz155240 if (clone->is_maxswin == 0)
20622393Syz155240 clone->is_maxswin = 1;
20632393Syz155240 }
20642393Syz155240
20652393Syz155240 clone->is_flags &= ~SI_CLONE;
20662393Syz155240 clone->is_flags |= SI_CLONED;
20673448Sdh155122 fr_stinsert(clone, fin->fin_rev, ifs);
20688624SDarren.Reed@Sun.COM clone->is_ref = 1;
20692393Syz155240 if (clone->is_p == IPPROTO_TCP) {
20703448Sdh155122 (void) fr_tcp_age(&clone->is_sti, fin, ifs->ifs_ips_tqtqb,
20712393Syz155240 clone->is_flags);
20722393Syz155240 }
20732393Syz155240 MUTEX_EXIT(&clone->is_lock);
20742393Syz155240 #ifdef IPFILTER_SCAN
20752393Syz155240 (void) ipsc_attachis(is);
20762393Syz155240 #endif
20772393Syz155240 #ifdef IPFILTER_SYNC
20782393Syz155240 if (is->is_flags & IS_STATESYNC)
20792393Syz155240 clone->is_sync = ipfsync_new(SMC_STATE, fin, clone);
20802393Syz155240 #endif
20812393Syz155240 return clone;
20822393Syz155240 }
20832393Syz155240
20842393Syz155240
20852393Syz155240 /* ------------------------------------------------------------------------ */
20862393Syz155240 /* Function: fr_matchsrcdst */
20872393Syz155240 /* Returns: Nil */
20882393Syz155240 /* Parameters: fin(I) - pointer to packet information */
20892393Syz155240 /* is(I) - pointer to state structure */
20902393Syz155240 /* src(I) - pointer to source address */
20912393Syz155240 /* dst(I) - pointer to destination address */
20922393Syz155240 /* tcp(I) - pointer to TCP/UDP header */
20932393Syz155240 /* */
20942393Syz155240 /* Match a state table entry against an IP packet. The logic below is that */
20952393Syz155240 /* ret gets set to one if the match succeeds, else remains 0. If it is */
20962393Syz155240 /* still 0 after the test. no match. */
20972393Syz155240 /* ------------------------------------------------------------------------ */
fr_matchsrcdst(fin,is,src,dst,tcp,cmask)20982393Syz155240 static ipstate_t *fr_matchsrcdst(fin, is, src, dst, tcp, cmask)
20992393Syz155240 fr_info_t *fin;
21002393Syz155240 ipstate_t *is;
21012393Syz155240 i6addr_t *src, *dst;
21022393Syz155240 tcphdr_t *tcp;
21032393Syz155240 u_32_t cmask;
21042393Syz155240 {
21052393Syz155240 int ret = 0, rev, out, flags, flx = 0, idx;
21062393Syz155240 u_short sp, dp;
21072393Syz155240 u_32_t cflx;
21082393Syz155240 void *ifp;
21093448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs;
21102393Syz155240
21112393Syz155240 rev = IP6_NEQ(&is->is_dst, dst);
21122393Syz155240 ifp = fin->fin_ifp;
21132393Syz155240 out = fin->fin_out;
21142393Syz155240 flags = is->is_flags;
21152393Syz155240 sp = 0;
21162393Syz155240 dp = 0;
21172393Syz155240
21182393Syz155240 if (tcp != NULL) {
21192393Syz155240 sp = htons(fin->fin_sport);
21202393Syz155240 dp = ntohs(fin->fin_dport);
21212393Syz155240 }
21222393Syz155240 if (!rev) {
21232393Syz155240 if (tcp != NULL) {
21242393Syz155240 if (!(flags & SI_W_SPORT) && (sp != is->is_sport))
21252393Syz155240 rev = 1;
21262393Syz155240 else if (!(flags & SI_W_DPORT) && (dp != is->is_dport))
21272393Syz155240 rev = 1;
21282393Syz155240 }
21292393Syz155240 }
21302393Syz155240
21312393Syz155240 idx = (out << 1) + rev;
21322393Syz155240
21332393Syz155240 /*
21342393Syz155240 * If the interface for this 'direction' is set, make sure it matches.
21352393Syz155240 * An interface name that is not set matches any, as does a name of *.
21362393Syz155240 */
21372393Syz155240 if ((is->is_ifp[idx] == NULL &&
21382393Syz155240 (*is->is_ifname[idx] == '\0' || *is->is_ifname[idx] == '*')) ||
21392393Syz155240 is->is_ifp[idx] == ifp)
21402393Syz155240 ret = 1;
21412393Syz155240
2142*13097SJan.Parcel@Sun.COM if (ret == 0) {
2143*13097SJan.Parcel@Sun.COM DTRACE_PROBE(no_match_on_iface);
21442393Syz155240 return NULL;
2145*13097SJan.Parcel@Sun.COM }
21462393Syz155240 ret = 0;
21472393Syz155240
21482393Syz155240 /*
21492393Syz155240 * Match addresses and ports.
21502393Syz155240 */
21512393Syz155240 if (rev == 0) {
21522393Syz155240 if ((IP6_EQ(&is->is_dst, dst) || (flags & SI_W_DADDR)) &&
21532393Syz155240 (IP6_EQ(&is->is_src, src) || (flags & SI_W_SADDR))) {
21542393Syz155240 if (tcp) {
21552393Syz155240 if ((sp == is->is_sport || flags & SI_W_SPORT)&&
21562393Syz155240 (dp == is->is_dport || flags & SI_W_DPORT))
21572393Syz155240 ret = 1;
21582393Syz155240 } else {
21592393Syz155240 ret = 1;
21602393Syz155240 }
21612393Syz155240 }
21622393Syz155240 } else {
21632393Syz155240 if ((IP6_EQ(&is->is_dst, src) || (flags & SI_W_DADDR)) &&
21642393Syz155240 (IP6_EQ(&is->is_src, dst) || (flags & SI_W_SADDR))) {
21652393Syz155240 if (tcp) {
21662393Syz155240 if ((dp == is->is_sport || flags & SI_W_SPORT)&&
21672393Syz155240 (sp == is->is_dport || flags & SI_W_DPORT))
21682393Syz155240 ret = 1;
21692393Syz155240 } else {
21702393Syz155240 ret = 1;
21712393Syz155240 }
21722393Syz155240 }
21732393Syz155240 }
21742393Syz155240
2175*13097SJan.Parcel@Sun.COM if (ret == 0) {
2176*13097SJan.Parcel@Sun.COM DTRACE_PROBE(no_match_on_addrs);
21772393Syz155240 return NULL;
2178*13097SJan.Parcel@Sun.COM }
21792393Syz155240 /*
21802393Syz155240 * Whether or not this should be here, is questionable, but the aim
21812393Syz155240 * is to get this out of the main line.
21822393Syz155240 */
21832393Syz155240 if (tcp == NULL)
21842393Syz155240 flags = is->is_flags & ~(SI_WILDP|SI_NEWFR|SI_CLONE|SI_CLONED);
21852393Syz155240
21862393Syz155240 /*
21872393Syz155240 * Only one of the source or destination address can be flaged as a
21882393Syz155240 * wildcard. Fill in the missing address, if set.
21892393Syz155240 * For IPv6, if the address being copied in is multicast, then
21902393Syz155240 * don't reset the wild flag - multicast causes it to be set in the
21912393Syz155240 * first place!
21922393Syz155240 */
21932393Syz155240 if ((flags & (SI_W_SADDR|SI_W_DADDR))) {
21942393Syz155240 fr_ip_t *fi = &fin->fin_fi;
21952393Syz155240
21962393Syz155240 if ((flags & SI_W_SADDR) != 0) {
21972393Syz155240 if (rev == 0) {
21982393Syz155240 #ifdef USE_INET6
21992393Syz155240 if (is->is_v == 6 &&
22002393Syz155240 IN6_IS_ADDR_MULTICAST(&fi->fi_src.in6))
22012393Syz155240 /*EMPTY*/;
22022393Syz155240 else
22032393Syz155240 #endif
22042393Syz155240 {
22052393Syz155240 is->is_src = fi->fi_src;
22062393Syz155240 is->is_flags &= ~SI_W_SADDR;
22072393Syz155240 }
22082393Syz155240 } else {
22092393Syz155240 #ifdef USE_INET6
22102393Syz155240 if (is->is_v == 6 &&
22112393Syz155240 IN6_IS_ADDR_MULTICAST(&fi->fi_dst.in6))
22122393Syz155240 /*EMPTY*/;
22132393Syz155240 else
22142393Syz155240 #endif
22152393Syz155240 {
22162393Syz155240 is->is_src = fi->fi_dst;
22172393Syz155240 is->is_flags &= ~SI_W_SADDR;
22182393Syz155240 }
22192393Syz155240 }
22202393Syz155240 } else if ((flags & SI_W_DADDR) != 0) {
22212393Syz155240 if (rev == 0) {
22222393Syz155240 #ifdef USE_INET6
22232393Syz155240 if (is->is_v == 6 &&
22242393Syz155240 IN6_IS_ADDR_MULTICAST(&fi->fi_dst.in6))
22252393Syz155240 /*EMPTY*/;
22262393Syz155240 else
22272393Syz155240 #endif
22282393Syz155240 {
22292393Syz155240 is->is_dst = fi->fi_dst;
22302393Syz155240 is->is_flags &= ~SI_W_DADDR;
22312393Syz155240 }
22322393Syz155240 } else {
22332393Syz155240 #ifdef USE_INET6
22342393Syz155240 if (is->is_v == 6 &&
22352393Syz155240 IN6_IS_ADDR_MULTICAST(&fi->fi_src.in6))
22362393Syz155240 /*EMPTY*/;
22372393Syz155240 else
22382393Syz155240 #endif
22392393Syz155240 {
22402393Syz155240 is->is_dst = fi->fi_src;
22412393Syz155240 is->is_flags &= ~SI_W_DADDR;
22422393Syz155240 }
22432393Syz155240 }
22442393Syz155240 }
22452393Syz155240 if ((is->is_flags & (SI_WILDA|SI_WILDP)) == 0) {
22463448Sdh155122 ATOMIC_DECL(ifs->ifs_ips_stats.iss_wild);
22472393Syz155240 }
22482393Syz155240 }
22492393Syz155240
22502393Syz155240 flx = fin->fin_flx & cmask;
22512393Syz155240 cflx = is->is_flx[out][rev];
22522393Syz155240
22532393Syz155240 /*
22542393Syz155240 * Match up any flags set from IP options.
22552393Syz155240 */
22562393Syz155240 if ((cflx && (flx != (cflx & cmask))) ||
22572393Syz155240 ((fin->fin_optmsk & is->is_optmsk[rev]) != is->is_opt[rev]) ||
22582393Syz155240 ((fin->fin_secmsk & is->is_secmsk) != is->is_sec) ||
2259*13097SJan.Parcel@Sun.COM ((fin->fin_auth & is->is_authmsk) != is->is_auth)) {
2260*13097SJan.Parcel@Sun.COM DTRACE_PROBE4(no_match_on_flags,
2261*13097SJan.Parcel@Sun.COM int, (cflx && (flx != (cflx & cmask))),
2262*13097SJan.Parcel@Sun.COM int,
2263*13097SJan.Parcel@Sun.COM ((fin->fin_optmsk & is->is_optmsk[rev]) != is->is_opt[rev]),
2264*13097SJan.Parcel@Sun.COM int, ((fin->fin_secmsk & is->is_secmsk) != is->is_sec),
2265*13097SJan.Parcel@Sun.COM int, ((fin->fin_auth & is->is_authmsk) != is->is_auth)
2266*13097SJan.Parcel@Sun.COM );
22672393Syz155240 return NULL;
2268*13097SJan.Parcel@Sun.COM }
22692393Syz155240 /*
22702393Syz155240 * Only one of the source or destination port can be flagged as a
22712393Syz155240 * wildcard. When filling it in, fill in a copy of the matched entry
22722393Syz155240 * if it has the cloning flag set.
22732393Syz155240 */
22742393Syz155240 if ((fin->fin_flx & FI_IGNORE) != 0) {
22752393Syz155240 fin->fin_rev = rev;
22762393Syz155240 return is;
22772393Syz155240 }
22782393Syz155240
22792393Syz155240 if ((flags & (SI_W_SPORT|SI_W_DPORT))) {
22802393Syz155240 if ((flags & SI_CLONE) != 0) {
22812393Syz155240 ipstate_t *clone;
22822393Syz155240
22832393Syz155240 clone = fr_stclone(fin, tcp, is);
22842393Syz155240 if (clone == NULL)
22852393Syz155240 return NULL;
22862393Syz155240 is = clone;
22872393Syz155240 } else {
22883448Sdh155122 ATOMIC_DECL(ifs->ifs_ips_stats.iss_wild);
22892393Syz155240 }
22902393Syz155240
22912393Syz155240 if ((flags & SI_W_SPORT) != 0) {
22922393Syz155240 if (rev == 0) {
22932393Syz155240 is->is_sport = sp;
22942393Syz155240 is->is_send = ntohl(tcp->th_seq);
22952393Syz155240 } else {
22962393Syz155240 is->is_sport = dp;
22972393Syz155240 is->is_send = ntohl(tcp->th_ack);
22982393Syz155240 }
22992393Syz155240 is->is_maxsend = is->is_send + 1;
23002393Syz155240 } else if ((flags & SI_W_DPORT) != 0) {
23012393Syz155240 if (rev == 0) {
23022393Syz155240 is->is_dport = dp;
23032393Syz155240 is->is_dend = ntohl(tcp->th_ack);
23042393Syz155240 } else {
23052393Syz155240 is->is_dport = sp;
23062393Syz155240 is->is_dend = ntohl(tcp->th_seq);
23072393Syz155240 }
23082393Syz155240 is->is_maxdend = is->is_dend + 1;
23092393Syz155240 }
23102393Syz155240 is->is_flags &= ~(SI_W_SPORT|SI_W_DPORT);
23113448Sdh155122 if ((flags & SI_CLONED) && ifs->ifs_ipstate_logging)
23123448Sdh155122 ipstate_log(is, ISL_CLONE, ifs);
23132393Syz155240 }
23142393Syz155240
23152393Syz155240 ret = -1;
23162393Syz155240
23172393Syz155240 if (is->is_flx[out][rev] == 0) {
23182393Syz155240 is->is_flx[out][rev] = flx;
2319*13097SJan.Parcel@Sun.COM /*
2320*13097SJan.Parcel@Sun.COM * If we are dealing with the first packet coming in reverse
2321*13097SJan.Parcel@Sun.COM * direction (sent by peer), then we have to set options into
2322*13097SJan.Parcel@Sun.COM * state.
2323*13097SJan.Parcel@Sun.COM */
2324*13097SJan.Parcel@Sun.COM if (rev == 1 && is->is_optmsk[1] == 0x0) {
2325*13097SJan.Parcel@Sun.COM is->is_optmsk[1] = 0xffffffff;
2326*13097SJan.Parcel@Sun.COM is->is_opt[1] = fin->fin_optmsk;
2327*13097SJan.Parcel@Sun.COM DTRACE_PROBE(set_rev_opts);
2328*13097SJan.Parcel@Sun.COM }
23292393Syz155240 if (is->is_v == 6) {
23302393Syz155240 is->is_opt[rev] &= ~0x8;
23312393Syz155240 is->is_optmsk[rev] &= ~0x8;
23322393Syz155240 }
23332393Syz155240 }
23342393Syz155240
23352393Syz155240 /*
23362393Syz155240 * Check if the interface name for this "direction" is set and if not,
23372393Syz155240 * fill it in.
23382393Syz155240 */
23392393Syz155240 if (is->is_ifp[idx] == NULL &&
23402393Syz155240 (*is->is_ifname[idx] == '\0' || *is->is_ifname[idx] == '*')) {
23412393Syz155240 is->is_ifp[idx] = ifp;
23422958Sdr146992 COPYIFNAME(ifp, is->is_ifname[idx], fin->fin_v);
23432393Syz155240 }
23442393Syz155240 fin->fin_rev = rev;
23452393Syz155240 return is;
23462393Syz155240 }
23472393Syz155240
23482393Syz155240
23492393Syz155240 /* ------------------------------------------------------------------------ */
23502393Syz155240 /* Function: fr_checkicmpmatchingstate */
23512393Syz155240 /* Returns: Nil */
23522393Syz155240 /* Parameters: fin(I) - pointer to packet information */
23532393Syz155240 /* */
23542393Syz155240 /* If we've got an ICMP error message, using the information stored in the */
23552393Syz155240 /* ICMP packet, look for a matching state table entry. */
23562393Syz155240 /* */
23572393Syz155240 /* If we return NULL then no lock on ipf_state is held. */
23582393Syz155240 /* If we return non-null then a read-lock on ipf_state is held. */
23592393Syz155240 /* ------------------------------------------------------------------------ */
fr_checkicmpmatchingstate(fin)23602393Syz155240 static ipstate_t *fr_checkicmpmatchingstate(fin)
23612393Syz155240 fr_info_t *fin;
23622393Syz155240 {
23632393Syz155240 ipstate_t *is, **isp;
23642393Syz155240 u_short sport, dport;
23652393Syz155240 u_char pr;
23662393Syz155240 int backward, i, oi;
23672393Syz155240 i6addr_t dst, src;
23682393Syz155240 struct icmp *ic;
23692393Syz155240 u_short savelen;
23702393Syz155240 icmphdr_t *icmp;
23712393Syz155240 fr_info_t ofin;
23722393Syz155240 tcphdr_t *tcp;
23732393Syz155240 int len;
23742393Syz155240 ip_t *oip;
23752393Syz155240 u_int hv;
23763448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs;
23772393Syz155240
23782393Syz155240 /*
23792393Syz155240 * Does it at least have the return (basic) IP header ?
23802393Syz155240 * Is it an actual recognised ICMP error type?
23812393Syz155240 * Only a basic IP header (no options) should be with
23822393Syz155240 * an ICMP error header.
23832393Syz155240 */
23842393Syz155240 if ((fin->fin_v != 4) || (fin->fin_hlen != sizeof(ip_t)) ||
23852393Syz155240 (fin->fin_plen < ICMPERR_MINPKTLEN) ||
23862393Syz155240 !(fin->fin_flx & FI_ICMPERR))
23872393Syz155240 return NULL;
23882393Syz155240 ic = fin->fin_dp;
23892393Syz155240
23902393Syz155240 oip = (ip_t *)((char *)ic + ICMPERR_ICMPHLEN);
23912393Syz155240 /*
23922393Syz155240 * Check if the at least the old IP header (with options) and
23932393Syz155240 * 8 bytes of payload is present.
23942393Syz155240 */
23952393Syz155240 if (fin->fin_plen < ICMPERR_MAXPKTLEN + ((IP_HL(oip) - 5) << 2))
23962393Syz155240 return NULL;
23972393Syz155240
23982393Syz155240 /*
23992393Syz155240 * Sanity Checks.
24002393Syz155240 */
24012393Syz155240 len = fin->fin_dlen - ICMPERR_ICMPHLEN;
24022393Syz155240 if ((len <= 0) || ((IP_HL(oip) << 2) > len))
24032393Syz155240 return NULL;
24042393Syz155240
24052393Syz155240 /*
24062393Syz155240 * Is the buffer big enough for all of it ? It's the size of the IP
24072393Syz155240 * header claimed in the encapsulated part which is of concern. It
24082393Syz155240 * may be too big to be in this buffer but not so big that it's
24092393Syz155240 * outside the ICMP packet, leading to TCP deref's causing problems.
24102393Syz155240 * This is possible because we don't know how big oip_hl is when we
24112393Syz155240 * do the pullup early in fr_check() and thus can't guarantee it is
24122393Syz155240 * all here now.
24132393Syz155240 */
24142393Syz155240 #ifdef _KERNEL
24152393Syz155240 {
24162393Syz155240 mb_t *m;
24172393Syz155240
24182393Syz155240 m = fin->fin_m;
24192393Syz155240 # if defined(MENTAT)
24202393Syz155240 if ((char *)oip + len > (char *)m->b_wptr)
24212393Syz155240 return NULL;
24222393Syz155240 # else
24232393Syz155240 if ((char *)oip + len > (char *)fin->fin_ip + m->m_len)
24242393Syz155240 return NULL;
24252393Syz155240 # endif
24262393Syz155240 }
24272393Syz155240 #endif
24283607Szf203873 bcopy((char *)fin, (char *)&ofin, sizeof(*fin));
24292393Syz155240
24302393Syz155240 /*
24312393Syz155240 * in the IPv4 case we must zero the i6addr union otherwise
24322393Syz155240 * the IP6_EQ and IP6_NEQ macros produce the wrong results because
24332393Syz155240 * of the 'junk' in the unused part of the union
24342393Syz155240 */
24352393Syz155240 bzero((char *)&src, sizeof(src));
24362393Syz155240 bzero((char *)&dst, sizeof(dst));
24372393Syz155240
24382393Syz155240 /*
24392393Syz155240 * we make an fin entry to be able to feed it to
24402393Syz155240 * matchsrcdst note that not all fields are encessary
24412393Syz155240 * but this is the cleanest way. Note further we fill
24422393Syz155240 * in fin_mp such that if someone uses it we'll get
24432393Syz155240 * a kernel panic. fr_matchsrcdst does not use this.
24442393Syz155240 *
24452393Syz155240 * watch out here, as ip is in host order and oip in network
24462393Syz155240 * order. Any change we make must be undone afterwards, like
24472393Syz155240 * oip->ip_off - it is still in network byte order so fix it.
24482393Syz155240 */
24492393Syz155240 savelen = oip->ip_len;
24502393Syz155240 oip->ip_len = len;
24512393Syz155240 oip->ip_off = ntohs(oip->ip_off);
24522393Syz155240
24532393Syz155240 ofin.fin_flx = FI_NOCKSUM;
24542393Syz155240 ofin.fin_v = 4;
24552393Syz155240 ofin.fin_ip = oip;
24562393Syz155240 ofin.fin_m = NULL; /* if dereferenced, panic XXX */
24572393Syz155240 ofin.fin_mp = NULL; /* if dereferenced, panic XXX */
24582393Syz155240 ofin.fin_plen = fin->fin_dlen - ICMPERR_ICMPHLEN;
24592393Syz155240 (void) fr_makefrip(IP_HL(oip) << 2, oip, &ofin);
24602393Syz155240 ofin.fin_ifp = fin->fin_ifp;
24612393Syz155240 ofin.fin_out = !fin->fin_out;
24622393Syz155240 /*
24632393Syz155240 * Reset the short and bad flag here because in fr_matchsrcdst()
24642393Syz155240 * the flags for the current packet (fin_flx) are compared against
24652393Syz155240 * those for the existing session.
24662393Syz155240 */
24672393Syz155240 ofin.fin_flx &= ~(FI_BAD|FI_SHORT);
24682393Syz155240
24692393Syz155240 /*
24702393Syz155240 * Put old values of ip_len and ip_off back as we don't know
24712393Syz155240 * if we have to forward the packet (or process it again.
24722393Syz155240 */
24732393Syz155240 oip->ip_len = savelen;
24742393Syz155240 oip->ip_off = htons(oip->ip_off);
24752393Syz155240
24762393Syz155240 switch (oip->ip_p)
24772393Syz155240 {
24782393Syz155240 case IPPROTO_ICMP :
24792393Syz155240 /*
24802393Syz155240 * an ICMP error can only be generated as a result of an
24812393Syz155240 * ICMP query, not as the response on an ICMP error
24822393Syz155240 *
24832393Syz155240 * XXX theoretically ICMP_ECHOREP and the other reply's are
24842393Syz155240 * ICMP query's as well, but adding them here seems strange XXX
24852393Syz155240 */
24862393Syz155240 if ((ofin.fin_flx & FI_ICMPERR) != 0)
24872393Syz155240 return NULL;
24882393Syz155240
24892393Syz155240 /*
24902393Syz155240 * perform a lookup of the ICMP packet in the state table
24912393Syz155240 */
24922393Syz155240 icmp = (icmphdr_t *)((char *)oip + (IP_HL(oip) << 2));
24932393Syz155240 hv = (pr = oip->ip_p);
24942393Syz155240 src.in4 = oip->ip_src;
24952393Syz155240 hv += src.in4.s_addr;
24962393Syz155240 dst.in4 = oip->ip_dst;
24972393Syz155240 hv += dst.in4.s_addr;
24982393Syz155240 hv += icmp->icmp_id;
24993448Sdh155122 hv = DOUBLE_HASH(hv, ifs);
25003448Sdh155122
25013448Sdh155122 READ_ENTER(&ifs->ifs_ipf_state);
25023448Sdh155122 for (isp = &ifs->ifs_ips_table[hv]; ((is = *isp) != NULL); ) {
25032393Syz155240 isp = &is->is_hnext;
25042393Syz155240 if ((is->is_p != pr) || (is->is_v != 4))
25052393Syz155240 continue;
25062393Syz155240 if (is->is_pass & FR_NOICMPERR)
25072393Syz155240 continue;
25082393Syz155240 is = fr_matchsrcdst(&ofin, is, &src, &dst,
25092393Syz155240 NULL, FI_ICMPCMP);
25102393Syz155240 if (is != NULL) {
25112393Syz155240 if ((is->is_pass & FR_NOICMPERR) != 0) {
25123448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
25132393Syz155240 return NULL;
25142393Syz155240 }
25152393Syz155240 /*
25162393Syz155240 * i : the index of this packet (the icmp
25172393Syz155240 * unreachable)
25182393Syz155240 * oi : the index of the original packet found
25192393Syz155240 * in the icmp header (i.e. the packet
25202393Syz155240 * causing this icmp)
25212393Syz155240 * backward : original packet was backward
25222393Syz155240 * compared to the state
25232393Syz155240 */
25242393Syz155240 backward = IP6_NEQ(&is->is_src, &src);
25252393Syz155240 fin->fin_rev = !backward;
25262393Syz155240 i = (!backward << 1) + fin->fin_out;
25272393Syz155240 oi = (backward << 1) + ofin.fin_out;
25282393Syz155240 if (is->is_icmppkts[i] > is->is_pkts[oi])
25292393Syz155240 continue;
25303448Sdh155122 ifs->ifs_ips_stats.iss_hits++;
25312393Syz155240 is->is_icmppkts[i]++;
25322393Syz155240 return is;
25332393Syz155240 }
25342393Syz155240 }
25353448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
25362393Syz155240 return NULL;
25372393Syz155240 case IPPROTO_TCP :
25382393Syz155240 case IPPROTO_UDP :
25392393Syz155240 break;
25402393Syz155240 default :
25412393Syz155240 return NULL;
25422393Syz155240 }
25432393Syz155240
25442393Syz155240 tcp = (tcphdr_t *)((char *)oip + (IP_HL(oip) << 2));
25452393Syz155240 dport = tcp->th_dport;
25462393Syz155240 sport = tcp->th_sport;
25472393Syz155240
25482393Syz155240 hv = (pr = oip->ip_p);
25492393Syz155240 src.in4 = oip->ip_src;
25502393Syz155240 hv += src.in4.s_addr;
25512393Syz155240 dst.in4 = oip->ip_dst;
25522393Syz155240 hv += dst.in4.s_addr;
25532393Syz155240 hv += dport;
25542393Syz155240 hv += sport;
25553448Sdh155122 hv = DOUBLE_HASH(hv, ifs);
25563448Sdh155122
25573448Sdh155122 READ_ENTER(&ifs->ifs_ipf_state);
25583448Sdh155122 for (isp = &ifs->ifs_ips_table[hv]; ((is = *isp) != NULL); ) {
25592393Syz155240 isp = &is->is_hnext;
25602393Syz155240 /*
25612393Syz155240 * Only allow this icmp though if the
25622393Syz155240 * encapsulated packet was allowed through the
25632393Syz155240 * other way around. Note that the minimal amount
25642393Syz155240 * of info present does not allow for checking against
25652393Syz155240 * tcp internals such as seq and ack numbers. Only the
25662393Syz155240 * ports are known to be present and can be even if the
25672393Syz155240 * short flag is set.
25682393Syz155240 */
25692393Syz155240 if ((is->is_p == pr) && (is->is_v == 4) &&
25702393Syz155240 (is = fr_matchsrcdst(&ofin, is, &src, &dst,
25712393Syz155240 tcp, FI_ICMPCMP))) {
25722393Syz155240 /*
25732393Syz155240 * i : the index of this packet (the icmp unreachable)
25742393Syz155240 * oi : the index of the original packet found in the
25752393Syz155240 * icmp header (i.e. the packet causing this icmp)
25762393Syz155240 * backward : original packet was backward compared to
25772393Syz155240 * the state
25782393Syz155240 */
25792393Syz155240 backward = IP6_NEQ(&is->is_src, &src);
25802393Syz155240 fin->fin_rev = !backward;
25812393Syz155240 i = (!backward << 1) + fin->fin_out;
25822393Syz155240 oi = (backward << 1) + ofin.fin_out;
25832393Syz155240
25842393Syz155240 if (((is->is_pass & FR_NOICMPERR) != 0) ||
25852393Syz155240 (is->is_icmppkts[i] > is->is_pkts[oi]))
25862393Syz155240 break;
25873448Sdh155122 ifs->ifs_ips_stats.iss_hits++;
25882393Syz155240 is->is_icmppkts[i]++;
25892393Syz155240 /*
25902393Syz155240 * we deliberately do not touch the timeouts
25912393Syz155240 * for the accompanying state table entry.
25922393Syz155240 * It remains to be seen if that is correct. XXX
25932393Syz155240 */
25942393Syz155240 return is;
25952393Syz155240 }
25962393Syz155240 }
25973448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
25982393Syz155240 return NULL;
25992393Syz155240 }
26002393Syz155240
26012393Syz155240
26022393Syz155240 /* ------------------------------------------------------------------------ */
26032393Syz155240 /* Function: fr_ipsmove */
26042393Syz155240 /* Returns: Nil */
26052393Syz155240 /* Parameters: is(I) - pointer to state table entry */
26062393Syz155240 /* hv(I) - new hash value for state table entry */
26072393Syz155240 /* Write Locks: ipf_state */
26082393Syz155240 /* */
26092393Syz155240 /* Move a state entry from one position in the hash table to another. */
26102393Syz155240 /* ------------------------------------------------------------------------ */
fr_ipsmove(is,hv,ifs)26113448Sdh155122 static void fr_ipsmove(is, hv, ifs)
26122393Syz155240 ipstate_t *is;
26132393Syz155240 u_int hv;
26143448Sdh155122 ipf_stack_t *ifs;
26152393Syz155240 {
26162393Syz155240 ipstate_t **isp;
26172393Syz155240 u_int hvm;
26182393Syz155240
26193448Sdh155122 ASSERT(rw_read_locked(&ifs->ifs_ipf_state.ipf_lk) == 0);
26202393Syz155240
26212393Syz155240 hvm = is->is_hv;
26222393Syz155240 /*
26232393Syz155240 * Remove the hash from the old location...
26242393Syz155240 */
26252393Syz155240 isp = is->is_phnext;
26262393Syz155240 if (is->is_hnext)
26272393Syz155240 is->is_hnext->is_phnext = isp;
26282393Syz155240 *isp = is->is_hnext;
26293448Sdh155122 if (ifs->ifs_ips_table[hvm] == NULL)
26303448Sdh155122 ifs->ifs_ips_stats.iss_inuse--;
26313448Sdh155122 ifs->ifs_ips_stats.iss_bucketlen[hvm]--;
26322393Syz155240
26332393Syz155240 /*
26342393Syz155240 * ...and put the hash in the new one.
26352393Syz155240 */
26363448Sdh155122 hvm = DOUBLE_HASH(hv, ifs);
26372393Syz155240 is->is_hv = hvm;
26383448Sdh155122 isp = &ifs->ifs_ips_table[hvm];
26392393Syz155240 if (*isp)
26402393Syz155240 (*isp)->is_phnext = &is->is_hnext;
26412393Syz155240 else
26423448Sdh155122 ifs->ifs_ips_stats.iss_inuse++;
26433448Sdh155122 ifs->ifs_ips_stats.iss_bucketlen[hvm]++;
26442393Syz155240 is->is_phnext = isp;
26452393Syz155240 is->is_hnext = *isp;
26462393Syz155240 *isp = is;
26472393Syz155240 }
26482393Syz155240
26492393Syz155240
26502393Syz155240 /* ------------------------------------------------------------------------ */
26512393Syz155240 /* Function: fr_stlookup */
26522393Syz155240 /* Returns: ipstate_t* - NULL == no matching state found, */
26532393Syz155240 /* else pointer to state information is returned */
26542393Syz155240 /* Parameters: fin(I) - pointer to packet information */
26552393Syz155240 /* tcp(I) - pointer to TCP/UDP header. */
26562393Syz155240 /* */
26572393Syz155240 /* Search the state table for a matching entry to the packet described by */
26582393Syz155240 /* the contents of *fin. */
26592393Syz155240 /* */
26602393Syz155240 /* If we return NULL then no lock on ipf_state is held. */
26612393Syz155240 /* If we return non-null then a read-lock on ipf_state is held. */
26622393Syz155240 /* ------------------------------------------------------------------------ */
fr_stlookup(fin,tcp,ifqp)26632393Syz155240 ipstate_t *fr_stlookup(fin, tcp, ifqp)
26642393Syz155240 fr_info_t *fin;
26652393Syz155240 tcphdr_t *tcp;
26662393Syz155240 ipftq_t **ifqp;
26672393Syz155240 {
26682393Syz155240 u_int hv, hvm, pr, v, tryagain;
26692393Syz155240 ipstate_t *is, **isp;
26702393Syz155240 u_short dport, sport;
26712393Syz155240 i6addr_t src, dst;
26722393Syz155240 struct icmp *ic;
26732393Syz155240 ipftq_t *ifq;
26742393Syz155240 int oow;
26753448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs;
26762393Syz155240
26772393Syz155240 is = NULL;
26782393Syz155240 ifq = NULL;
26792393Syz155240 tcp = fin->fin_dp;
26802393Syz155240 ic = (struct icmp *)tcp;
26812393Syz155240 hv = (pr = fin->fin_fi.fi_p);
26822393Syz155240 src = fin->fin_fi.fi_src;
26832393Syz155240 dst = fin->fin_fi.fi_dst;
26842393Syz155240 hv += src.in4.s_addr;
26852393Syz155240 hv += dst.in4.s_addr;
26862393Syz155240
26872393Syz155240 v = fin->fin_fi.fi_v;
26882393Syz155240 #ifdef USE_INET6
26892393Syz155240 if (v == 6) {
26902393Syz155240 hv += fin->fin_fi.fi_src.i6[1];
26912393Syz155240 hv += fin->fin_fi.fi_src.i6[2];
26922393Syz155240 hv += fin->fin_fi.fi_src.i6[3];
26932393Syz155240
26942393Syz155240 if ((fin->fin_p == IPPROTO_ICMPV6) &&
26952393Syz155240 IN6_IS_ADDR_MULTICAST(&fin->fin_fi.fi_dst.in6)) {
26962393Syz155240 hv -= dst.in4.s_addr;
26972393Syz155240 } else {
26982393Syz155240 hv += fin->fin_fi.fi_dst.i6[1];
26992393Syz155240 hv += fin->fin_fi.fi_dst.i6[2];
27002393Syz155240 hv += fin->fin_fi.fi_dst.i6[3];
27012393Syz155240 }
27022393Syz155240 }
27032393Syz155240 #endif
27047433SJohn.Ojemann@Sun.COM if ((v == 4) &&
27057433SJohn.Ojemann@Sun.COM (fin->fin_flx & (FI_MULTICAST|FI_BROADCAST|FI_MBCAST))) {
27067433SJohn.Ojemann@Sun.COM if (fin->fin_out == 0) {
27077433SJohn.Ojemann@Sun.COM hv -= src.in4.s_addr;
27087433SJohn.Ojemann@Sun.COM } else {
27097433SJohn.Ojemann@Sun.COM hv -= dst.in4.s_addr;
27107433SJohn.Ojemann@Sun.COM }
27117433SJohn.Ojemann@Sun.COM }
27122393Syz155240
27132393Syz155240 /*
27142393Syz155240 * Search the hash table for matching packet header info.
27152393Syz155240 */
27162393Syz155240 switch (pr)
27172393Syz155240 {
27182393Syz155240 #ifdef USE_INET6
27192393Syz155240 case IPPROTO_ICMPV6 :
27202393Syz155240 tryagain = 0;
27212393Syz155240 if (v == 6) {
27222393Syz155240 if ((ic->icmp_type == ICMP6_ECHO_REQUEST) ||
27232393Syz155240 (ic->icmp_type == ICMP6_ECHO_REPLY)) {
27242393Syz155240 hv += ic->icmp_id;
27252393Syz155240 }
27262393Syz155240 }
27273448Sdh155122 READ_ENTER(&ifs->ifs_ipf_state);
27282393Syz155240 icmp6again:
27293448Sdh155122 hvm = DOUBLE_HASH(hv, ifs);
27303448Sdh155122 for (isp = &ifs->ifs_ips_table[hvm]; ((is = *isp) != NULL); ) {
27312393Syz155240 isp = &is->is_hnext;
27322393Syz155240 if ((is->is_p != pr) || (is->is_v != v))
27332393Syz155240 continue;
27342393Syz155240 is = fr_matchsrcdst(fin, is, &src, &dst, NULL, FI_CMP);
27352393Syz155240 if (is != NULL &&
27362393Syz155240 fr_matchicmpqueryreply(v, &is->is_icmp,
27372393Syz155240 ic, fin->fin_rev)) {
27382393Syz155240 if (fin->fin_rev)
27393448Sdh155122 ifq = &ifs->ifs_ips_icmpacktq;
27402393Syz155240 else
27413448Sdh155122 ifq = &ifs->ifs_ips_icmptq;
27422393Syz155240 break;
27432393Syz155240 }
27442393Syz155240 }
27452393Syz155240
27462393Syz155240 if (is != NULL) {
27472393Syz155240 if ((tryagain != 0) && !(is->is_flags & SI_W_DADDR)) {
27482393Syz155240 hv += fin->fin_fi.fi_src.i6[0];
27492393Syz155240 hv += fin->fin_fi.fi_src.i6[1];
27502393Syz155240 hv += fin->fin_fi.fi_src.i6[2];
27512393Syz155240 hv += fin->fin_fi.fi_src.i6[3];
27523448Sdh155122 fr_ipsmove(is, hv, ifs);
27533448Sdh155122 MUTEX_DOWNGRADE(&ifs->ifs_ipf_state);
27542393Syz155240 }
27552393Syz155240 break;
27562393Syz155240 }
27573448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
27582393Syz155240
27592393Syz155240 /*
27602393Syz155240 * No matching icmp state entry. Perhaps this is a
27612393Syz155240 * response to another state entry.
27622393Syz155240 *
27632393Syz155240 * XXX With some ICMP6 packets, the "other" address is already
27642393Syz155240 * in the packet, after the ICMP6 header, and this could be
27652393Syz155240 * used in place of the multicast address. However, taking
27662393Syz155240 * advantage of this requires some significant code changes
27672393Syz155240 * to handle the specific types where that is the case.
27682393Syz155240 */
27693448Sdh155122 if ((ifs->ifs_ips_stats.iss_wild != 0) && (v == 6) && (tryagain == 0) &&
27702393Syz155240 !IN6_IS_ADDR_MULTICAST(&fin->fin_fi.fi_src.in6)) {
27712393Syz155240 hv -= fin->fin_fi.fi_src.i6[0];
27722393Syz155240 hv -= fin->fin_fi.fi_src.i6[1];
27732393Syz155240 hv -= fin->fin_fi.fi_src.i6[2];
27742393Syz155240 hv -= fin->fin_fi.fi_src.i6[3];
27752393Syz155240 tryagain = 1;
27763448Sdh155122 WRITE_ENTER(&ifs->ifs_ipf_state);
27772393Syz155240 goto icmp6again;
27782393Syz155240 }
27792393Syz155240
27802393Syz155240 is = fr_checkicmp6matchingstate(fin);
27812393Syz155240 if (is != NULL)
27822393Syz155240 return is;
27832393Syz155240 break;
27842393Syz155240 #endif
27852393Syz155240
27862393Syz155240 case IPPROTO_ICMP :
27872393Syz155240 if (v == 4) {
27882393Syz155240 hv += ic->icmp_id;
27892393Syz155240 }
27903448Sdh155122 hv = DOUBLE_HASH(hv, ifs);
27913448Sdh155122 READ_ENTER(&ifs->ifs_ipf_state);
27923448Sdh155122 for (isp = &ifs->ifs_ips_table[hv]; ((is = *isp) != NULL); ) {
27932393Syz155240 isp = &is->is_hnext;
27942393Syz155240 if ((is->is_p != pr) || (is->is_v != v))
27952393Syz155240 continue;
27962393Syz155240 is = fr_matchsrcdst(fin, is, &src, &dst, NULL, FI_CMP);
27972393Syz155240 if (is != NULL &&
27982393Syz155240 fr_matchicmpqueryreply(v, &is->is_icmp,
27992393Syz155240 ic, fin->fin_rev)) {
28002393Syz155240 if (fin->fin_rev)
28013448Sdh155122 ifq = &ifs->ifs_ips_icmpacktq;
28022393Syz155240 else
28033448Sdh155122 ifq = &ifs->ifs_ips_icmptq;
28042393Syz155240 break;
28052393Syz155240 }
28062393Syz155240 }
28072393Syz155240 if (is == NULL) {
28083448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
28092393Syz155240 }
28102393Syz155240 break;
28112393Syz155240
28122393Syz155240 case IPPROTO_TCP :
28132393Syz155240 case IPPROTO_UDP :
28142393Syz155240 ifqp = NULL;
28152393Syz155240 sport = htons(fin->fin_data[0]);
28162393Syz155240 hv += sport;
28172393Syz155240 dport = htons(fin->fin_data[1]);
28182393Syz155240 hv += dport;
28192393Syz155240 oow = 0;
28202393Syz155240 tryagain = 0;
28213448Sdh155122 READ_ENTER(&ifs->ifs_ipf_state);
28222393Syz155240 retry_tcpudp:
28233448Sdh155122 hvm = DOUBLE_HASH(hv, ifs);
28243448Sdh155122 for (isp = &ifs->ifs_ips_table[hvm]; ((is = *isp) != NULL); ) {
28252393Syz155240 isp = &is->is_hnext;
28262393Syz155240 if ((is->is_p != pr) || (is->is_v != v))
28272393Syz155240 continue;
28282393Syz155240 fin->fin_flx &= ~FI_OOW;
28292393Syz155240 is = fr_matchsrcdst(fin, is, &src, &dst, tcp, FI_CMP);
28302393Syz155240 if (is != NULL) {
28312393Syz155240 if (pr == IPPROTO_TCP) {
28322393Syz155240 if (!fr_tcpstate(fin, tcp, is)) {
28332393Syz155240 oow |= fin->fin_flx & FI_OOW;
28342393Syz155240 continue;
28352393Syz155240 }
28362393Syz155240 }
28372393Syz155240 break;
28382393Syz155240 }
28392393Syz155240 }
28402393Syz155240 if (is != NULL) {
28412393Syz155240 if (tryagain &&
28422393Syz155240 !(is->is_flags & (SI_CLONE|SI_WILDP|SI_WILDA))) {
28432393Syz155240 hv += dport;
28442393Syz155240 hv += sport;
28453448Sdh155122 fr_ipsmove(is, hv, ifs);
28463448Sdh155122 MUTEX_DOWNGRADE(&ifs->ifs_ipf_state);
28472393Syz155240 }
28482393Syz155240 break;
28492393Syz155240 }
28503448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
28513448Sdh155122
28527433SJohn.Ojemann@Sun.COM if (ifs->ifs_ips_stats.iss_wild) {
28537433SJohn.Ojemann@Sun.COM if (tryagain == 0) {
28547433SJohn.Ojemann@Sun.COM hv -= dport;
28557433SJohn.Ojemann@Sun.COM hv -= sport;
28567433SJohn.Ojemann@Sun.COM } else if (tryagain == 1) {
28577433SJohn.Ojemann@Sun.COM hv = fin->fin_fi.fi_p;
28587433SJohn.Ojemann@Sun.COM /*
28597433SJohn.Ojemann@Sun.COM * If we try to pretend this is a reply to a
28607433SJohn.Ojemann@Sun.COM * multicast/broadcast packet then we need to
28617433SJohn.Ojemann@Sun.COM * exclude part of the address from the hash
28627433SJohn.Ojemann@Sun.COM * calculation.
28637433SJohn.Ojemann@Sun.COM */
28647433SJohn.Ojemann@Sun.COM if (fin->fin_out == 0) {
28657433SJohn.Ojemann@Sun.COM hv += src.in4.s_addr;
28667433SJohn.Ojemann@Sun.COM } else {
28677433SJohn.Ojemann@Sun.COM hv += dst.in4.s_addr;
28687433SJohn.Ojemann@Sun.COM }
28697433SJohn.Ojemann@Sun.COM hv += dport;
28707433SJohn.Ojemann@Sun.COM hv += sport;
28717433SJohn.Ojemann@Sun.COM }
28727433SJohn.Ojemann@Sun.COM tryagain++;
28737433SJohn.Ojemann@Sun.COM if (tryagain <= 2) {
28747433SJohn.Ojemann@Sun.COM WRITE_ENTER(&ifs->ifs_ipf_state);
28757433SJohn.Ojemann@Sun.COM goto retry_tcpudp;
28767433SJohn.Ojemann@Sun.COM }
28772393Syz155240 }
28782393Syz155240 fin->fin_flx |= oow;
28792393Syz155240 break;
28802393Syz155240
28812393Syz155240 #if 0
28822393Syz155240 case IPPROTO_GRE :
28832393Syz155240 gre = fin->fin_dp;
28842393Syz155240 if (GRE_REV(gre->gr_flags) == 1) {
28852393Syz155240 hv += gre->gr_call;
28862393Syz155240 }
28872393Syz155240 /* FALLTHROUGH */
28882393Syz155240 #endif
28892393Syz155240 default :
28902393Syz155240 ifqp = NULL;
28913448Sdh155122 hvm = DOUBLE_HASH(hv, ifs);
28923448Sdh155122 READ_ENTER(&ifs->ifs_ipf_state);
28933448Sdh155122 for (isp = &ifs->ifs_ips_table[hvm]; ((is = *isp) != NULL); ) {
28942393Syz155240 isp = &is->is_hnext;
28952393Syz155240 if ((is->is_p != pr) || (is->is_v != v))
28962393Syz155240 continue;
28972393Syz155240 is = fr_matchsrcdst(fin, is, &src, &dst, NULL, FI_CMP);
28982393Syz155240 if (is != NULL) {
28993448Sdh155122 ifq = &ifs->ifs_ips_iptq;
29002393Syz155240 break;
29012393Syz155240 }
29022393Syz155240 }
29032393Syz155240 if (is == NULL) {
29043448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
29052393Syz155240 }
29062393Syz155240 break;
29072393Syz155240 }
29082393Syz155240
29092393Syz155240 if ((is != NULL) && ((is->is_sti.tqe_flags & TQE_RULEBASED) != 0) &&
29102393Syz155240 (is->is_tqehead[fin->fin_rev] != NULL))
29112393Syz155240 ifq = is->is_tqehead[fin->fin_rev];
29122393Syz155240 if (ifq != NULL && ifqp != NULL)
29132393Syz155240 *ifqp = ifq;
29142393Syz155240 return is;
29152393Syz155240 }
29162393Syz155240
29172393Syz155240
29182393Syz155240 /* ------------------------------------------------------------------------ */
29192393Syz155240 /* Function: fr_updatestate */
29202393Syz155240 /* Returns: Nil */
29212393Syz155240 /* Parameters: fin(I) - pointer to packet information */
29222393Syz155240 /* is(I) - pointer to state table entry */
29232393Syz155240 /* Read Locks: ipf_state */
29242393Syz155240 /* */
29252393Syz155240 /* Updates packet and byte counters for a newly received packet. Seeds the */
29262393Syz155240 /* fragment cache with a new entry as required. */
29272393Syz155240 /* ------------------------------------------------------------------------ */
fr_updatestate(fin,is,ifq)29282393Syz155240 void fr_updatestate(fin, is, ifq)
29292393Syz155240 fr_info_t *fin;
29302393Syz155240 ipstate_t *is;
29312393Syz155240 ipftq_t *ifq;
29322393Syz155240 {
29332393Syz155240 ipftqent_t *tqe;
29342393Syz155240 int i, pass;
29353448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs;
29362393Syz155240
29372393Syz155240 i = (fin->fin_rev << 1) + fin->fin_out;
29382393Syz155240
29392393Syz155240 /*
29402393Syz155240 * For TCP packets, ifq == NULL. For all others, check if this new
29412393Syz155240 * queue is different to the last one it was on and move it if so.
29422393Syz155240 */
29432393Syz155240 tqe = &is->is_sti;
29442393Syz155240 MUTEX_ENTER(&is->is_lock);
29452393Syz155240 if ((tqe->tqe_flags & TQE_RULEBASED) != 0)
29462393Syz155240 ifq = is->is_tqehead[fin->fin_rev];
29472393Syz155240
29482393Syz155240 if (ifq != NULL)
29493448Sdh155122 fr_movequeue(tqe, tqe->tqe_ifq, ifq, ifs);
29502393Syz155240
29512393Syz155240 is->is_pkts[i]++;
29528624SDarren.Reed@Sun.COM fin->fin_pktnum = is->is_pkts[i] + is->is_icmppkts[i];
29532393Syz155240 is->is_bytes[i] += fin->fin_plen;
29542393Syz155240 MUTEX_EXIT(&is->is_lock);
29552393Syz155240
29562393Syz155240 #ifdef IPFILTER_SYNC
29572393Syz155240 if (is->is_flags & IS_STATESYNC)
29582393Syz155240 ipfsync_update(SMC_STATE, fin, is->is_sync);
29592393Syz155240 #endif
29602393Syz155240
29613448Sdh155122 ATOMIC_INCL(ifs->ifs_ips_stats.iss_hits);
29622393Syz155240
29632393Syz155240 fin->fin_fr = is->is_rule;
29642393Syz155240
29652393Syz155240 /*
29662393Syz155240 * If this packet is a fragment and the rule says to track fragments,
29672393Syz155240 * then create a new fragment cache entry.
29682393Syz155240 */
29692393Syz155240 pass = is->is_pass;
29702393Syz155240 if ((fin->fin_flx & FI_FRAG) && FR_ISPASS(pass))
29712393Syz155240 (void) fr_newfrag(fin, pass ^ FR_KEEPSTATE);
29722393Syz155240 }
29732393Syz155240
29742393Syz155240
29752393Syz155240 /* ------------------------------------------------------------------------ */
29762393Syz155240 /* Function: fr_checkstate */
29772393Syz155240 /* Returns: frentry_t* - NULL == search failed, */
29782393Syz155240 /* else pointer to rule for matching state */
29792393Syz155240 /* Parameters: ifp(I) - pointer to interface */
29802393Syz155240 /* passp(I) - pointer to filtering result flags */
29812393Syz155240 /* */
29822393Syz155240 /* Check if a packet is associated with an entry in the state table. */
29832393Syz155240 /* ------------------------------------------------------------------------ */
fr_checkstate(fin,passp)29842393Syz155240 frentry_t *fr_checkstate(fin, passp)
29852393Syz155240 fr_info_t *fin;
29862393Syz155240 u_32_t *passp;
29872393Syz155240 {
29882393Syz155240 ipstate_t *is;
29892393Syz155240 frentry_t *fr;
29902393Syz155240 tcphdr_t *tcp;
29912393Syz155240 ipftq_t *ifq;
29922393Syz155240 u_int pass;
29933448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs;
29943448Sdh155122
29953448Sdh155122 if (ifs->ifs_fr_state_lock || (ifs->ifs_ips_list == NULL) ||
29962393Syz155240 (fin->fin_flx & (FI_SHORT|FI_STATE|FI_FRAGBODY|FI_BAD)))
29972393Syz155240 return NULL;
29982393Syz155240
29992393Syz155240 is = NULL;
30002393Syz155240 if ((fin->fin_flx & FI_TCPUDP) ||
30012393Syz155240 (fin->fin_fi.fi_p == IPPROTO_ICMP)
30022393Syz155240 #ifdef USE_INET6
30032393Syz155240 || (fin->fin_fi.fi_p == IPPROTO_ICMPV6)
30042393Syz155240 #endif
30052393Syz155240 )
30062393Syz155240 tcp = fin->fin_dp;
30072393Syz155240 else
30082393Syz155240 tcp = NULL;
30092393Syz155240
30102393Syz155240 /*
30112393Syz155240 * Search the hash table for matching packet header info.
30122393Syz155240 */
30132393Syz155240 ifq = NULL;
30148624SDarren.Reed@Sun.COM is = fr_stlookup(fin, tcp, &ifq);
30152393Syz155240 switch (fin->fin_p)
30162393Syz155240 {
30172393Syz155240 #ifdef USE_INET6
30182393Syz155240 case IPPROTO_ICMPV6 :
30192393Syz155240 if (is != NULL)
30202393Syz155240 break;
30212393Syz155240 if (fin->fin_v == 6) {
30222393Syz155240 is = fr_checkicmp6matchingstate(fin);
30232393Syz155240 if (is != NULL)
30242393Syz155240 goto matched;
30252393Syz155240 }
30262393Syz155240 break;
30272393Syz155240 #endif
30282393Syz155240 case IPPROTO_ICMP :
30292393Syz155240 if (is != NULL)
30302393Syz155240 break;
30312393Syz155240 /*
30322393Syz155240 * No matching icmp state entry. Perhaps this is a
30332393Syz155240 * response to another state entry.
30342393Syz155240 */
30352393Syz155240 is = fr_checkicmpmatchingstate(fin);
30362393Syz155240 if (is != NULL)
30372393Syz155240 goto matched;
30382393Syz155240 break;
30392393Syz155240 case IPPROTO_TCP :
30402393Syz155240 if (is == NULL)
30412393Syz155240 break;
30422393Syz155240
30432393Syz155240 if (is->is_pass & FR_NEWISN) {
30442393Syz155240 if (fin->fin_out == 0)
30452393Syz155240 fr_fixinisn(fin, is);
30462393Syz155240 else if (fin->fin_out == 1)
30472393Syz155240 fr_fixoutisn(fin, is);
30482393Syz155240 }
30492393Syz155240 break;
30502393Syz155240 default :
30512393Syz155240 if (fin->fin_rev)
30523448Sdh155122 ifq = &ifs->ifs_ips_udpacktq;
30532393Syz155240 else
30543448Sdh155122 ifq = &ifs->ifs_ips_udptq;
30552393Syz155240 break;
30562393Syz155240 }
30572393Syz155240 if (is == NULL) {
30583448Sdh155122 ATOMIC_INCL(ifs->ifs_ips_stats.iss_miss);
30592393Syz155240 return NULL;
30602393Syz155240 }
30612393Syz155240
30622393Syz155240 matched:
30632393Syz155240 fr = is->is_rule;
30642393Syz155240 if (fr != NULL) {
30652393Syz155240 if ((fin->fin_out == 0) && (fr->fr_nattag.ipt_num[0] != 0)) {
306611761SZdenek.Kotala@Sun.COM if (fin->fin_nattag == NULL) {
306711761SZdenek.Kotala@Sun.COM RWLOCK_EXIT(&ifs->ifs_ipf_state);
30682393Syz155240 return NULL;
306911761SZdenek.Kotala@Sun.COM }
307011761SZdenek.Kotala@Sun.COM if (fr_matchtag(&fr->fr_nattag, fin->fin_nattag) != 0) {
307111761SZdenek.Kotala@Sun.COM RWLOCK_EXIT(&ifs->ifs_ipf_state);
30722393Syz155240 return NULL;
307311761SZdenek.Kotala@Sun.COM }
30742393Syz155240 }
30752393Syz155240 (void) strncpy(fin->fin_group, fr->fr_group, FR_GROUPLEN);
30762393Syz155240 fin->fin_icode = fr->fr_icode;
30772393Syz155240 }
30782393Syz155240
30792393Syz155240 fin->fin_rule = is->is_rulen;
30802393Syz155240 pass = is->is_pass;
30812393Syz155240 fr_updatestate(fin, is, ifq);
30828624SDarren.Reed@Sun.COM
30833448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
30842393Syz155240 fin->fin_flx |= FI_STATE;
30852393Syz155240 if ((pass & FR_LOGFIRST) != 0)
30862393Syz155240 pass &= ~(FR_LOGFIRST|FR_LOG);
30872393Syz155240 *passp = pass;
30882393Syz155240 return fr;
30892393Syz155240 }
30902393Syz155240
30912393Syz155240
30922393Syz155240 /* ------------------------------------------------------------------------ */
30932393Syz155240 /* Function: fr_fixoutisn */
30942393Syz155240 /* Returns: Nil */
30952393Syz155240 /* Parameters: fin(I) - pointer to packet information */
30962393Syz155240 /* is(I) - pointer to master state structure */
30972393Syz155240 /* */
30982393Syz155240 /* Called only for outbound packets, adjusts the sequence number and the */
30992393Syz155240 /* TCP checksum to match that change. */
31002393Syz155240 /* ------------------------------------------------------------------------ */
fr_fixoutisn(fin,is)31012393Syz155240 static void fr_fixoutisn(fin, is)
31022393Syz155240 fr_info_t *fin;
31032393Syz155240 ipstate_t *is;
31042393Syz155240 {
31052393Syz155240 tcphdr_t *tcp;
31062393Syz155240 int rev;
31072393Syz155240 u_32_t seq;
31082393Syz155240
31092393Syz155240 tcp = fin->fin_dp;
31102393Syz155240 rev = fin->fin_rev;
31112393Syz155240 if ((is->is_flags & IS_ISNSYN) != 0) {
31122393Syz155240 if (rev == 0) {
31132393Syz155240 seq = ntohl(tcp->th_seq);
31142393Syz155240 seq += is->is_isninc[0];
31152393Syz155240 tcp->th_seq = htonl(seq);
31162958Sdr146992 fix_outcksum(&tcp->th_sum, is->is_sumd[0]);
31172393Syz155240 }
31182393Syz155240 }
31192393Syz155240 if ((is->is_flags & IS_ISNACK) != 0) {
31202393Syz155240 if (rev == 1) {
31212393Syz155240 seq = ntohl(tcp->th_seq);
31222393Syz155240 seq += is->is_isninc[1];
31232393Syz155240 tcp->th_seq = htonl(seq);
31242958Sdr146992 fix_outcksum(&tcp->th_sum, is->is_sumd[1]);
31252393Syz155240 }
31262393Syz155240 }
31272393Syz155240 }
31282393Syz155240
31292393Syz155240
31302393Syz155240 /* ------------------------------------------------------------------------ */
31312393Syz155240 /* Function: fr_fixinisn */
31322393Syz155240 /* Returns: Nil */
31332393Syz155240 /* Parameters: fin(I) - pointer to packet information */
31342393Syz155240 /* is(I) - pointer to master state structure */
31352393Syz155240 /* */
31362393Syz155240 /* Called only for inbound packets, adjusts the acknowledge number and the */
31372393Syz155240 /* TCP checksum to match that change. */
31382393Syz155240 /* ------------------------------------------------------------------------ */
fr_fixinisn(fin,is)31392393Syz155240 static void fr_fixinisn(fin, is)
31402393Syz155240 fr_info_t *fin;
31412393Syz155240 ipstate_t *is;
31422393Syz155240 {
31432393Syz155240 tcphdr_t *tcp;
31442393Syz155240 int rev;
31452393Syz155240 u_32_t ack;
31462393Syz155240
31472393Syz155240 tcp = fin->fin_dp;
31482393Syz155240 rev = fin->fin_rev;
31492393Syz155240 if ((is->is_flags & IS_ISNSYN) != 0) {
31502393Syz155240 if (rev == 1) {
31512393Syz155240 ack = ntohl(tcp->th_ack);
31522393Syz155240 ack -= is->is_isninc[0];
31532393Syz155240 tcp->th_ack = htonl(ack);
31542958Sdr146992 fix_incksum(&tcp->th_sum, is->is_sumd[0]);
31552393Syz155240 }
31562393Syz155240 }
31572393Syz155240 if ((is->is_flags & IS_ISNACK) != 0) {
31582393Syz155240 if (rev == 0) {
31592393Syz155240 ack = ntohl(tcp->th_ack);
31602393Syz155240 ack -= is->is_isninc[1];
31612393Syz155240 tcp->th_ack = htonl(ack);
31622958Sdr146992 fix_incksum(&tcp->th_sum, is->is_sumd[1]);
31632393Syz155240 }
31642393Syz155240 }
31652393Syz155240 }
31662393Syz155240
31672393Syz155240
31682393Syz155240 /* ------------------------------------------------------------------------ */
31692393Syz155240 /* Function: fr_statesync */
31702393Syz155240 /* Returns: Nil */
31712958Sdr146992 /* Parameters: action(I) - type of synchronisation to do */
31722958Sdr146992 /* v(I) - IP version being sync'd (v4 or v6) */
31732958Sdr146992 /* ifp(I) - interface identifier associated with action */
31742958Sdr146992 /* name(I) - name associated with ifp parameter */
31752393Syz155240 /* */
31762393Syz155240 /* Walk through all state entries and if an interface pointer match is */
31772393Syz155240 /* found then look it up again, based on its name in case the pointer has */
31782393Syz155240 /* changed since last time. */
31792393Syz155240 /* */
31802393Syz155240 /* If ifp is passed in as being non-null then we are only doing updates for */
31812393Syz155240 /* existing, matching, uses of it. */
31822393Syz155240 /* ------------------------------------------------------------------------ */
fr_statesync(action,v,ifp,name,ifs)31833448Sdh155122 void fr_statesync(action, v, ifp, name, ifs)
31842958Sdr146992 int action, v;
31852393Syz155240 void *ifp;
31862958Sdr146992 char *name;
31873448Sdh155122 ipf_stack_t *ifs;
31882393Syz155240 {
31892393Syz155240 ipstate_t *is;
31902393Syz155240 int i;
31912393Syz155240
31923448Sdh155122 if (ifs->ifs_fr_running <= 0)
31932393Syz155240 return;
31942393Syz155240
31953448Sdh155122 WRITE_ENTER(&ifs->ifs_ipf_state);
31963448Sdh155122
31973448Sdh155122 if (ifs->ifs_fr_running <= 0) {
31983448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
31992393Syz155240 return;
32002393Syz155240 }
32012393Syz155240
32022958Sdr146992 switch (action)
32032958Sdr146992 {
32042958Sdr146992 case IPFSYNC_RESYNC :
32053448Sdh155122 for (is = ifs->ifs_ips_list; is; is = is->is_next) {
32062958Sdr146992 if (v != 0 && is->is_v != v)
32072958Sdr146992 continue;
32082958Sdr146992 /*
32092958Sdr146992 * Look up all the interface names in the state entry.
32102958Sdr146992 */
32112958Sdr146992 for (i = 0; i < 4; i++) {
32122393Syz155240 is->is_ifp[i] = fr_resolvenic(is->is_ifname[i],
32133448Sdh155122 is->is_v, ifs);
32142958Sdr146992 }
32152393Syz155240 }
32162958Sdr146992 break;
32172958Sdr146992 case IPFSYNC_NEWIFP :
32183448Sdh155122 for (is = ifs->ifs_ips_list; is; is = is->is_next) {
32192958Sdr146992 if (v != 0 && is->is_v != v)
32202958Sdr146992 continue;
32212958Sdr146992 /*
32222958Sdr146992 * Look up all the interface names in the state entry.
32232958Sdr146992 */
32242958Sdr146992 for (i = 0; i < 4; i++) {
32252958Sdr146992 if (!strncmp(is->is_ifname[i], name,
32262958Sdr146992 sizeof(is->is_ifname[i])))
32272958Sdr146992 is->is_ifp[i] = ifp;
32282958Sdr146992 }
32292958Sdr146992 }
32302958Sdr146992 break;
32312958Sdr146992 case IPFSYNC_OLDIFP :
32323448Sdh155122 for (is = ifs->ifs_ips_list; is; is = is->is_next) {
32332958Sdr146992 if (v != 0 && is->is_v != v)
32342958Sdr146992 continue;
32352958Sdr146992 /*
32362958Sdr146992 * Look up all the interface names in the state entry.
32372958Sdr146992 */
32382958Sdr146992 for (i = 0; i < 4; i++) {
32392958Sdr146992 if (is->is_ifp[i] == ifp)
32402958Sdr146992 is->is_ifp[i] = (void *)-1;
32412958Sdr146992 }
32422958Sdr146992 }
32432958Sdr146992 break;
32442393Syz155240 }
32453448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
32462393Syz155240 }
32472393Syz155240
32482393Syz155240
324911105SAlexandr.Nedvedicky@Sun.COM #if SOLARIS2 >= 10
325011105SAlexandr.Nedvedicky@Sun.COM /* ------------------------------------------------------------------------ */
325111105SAlexandr.Nedvedicky@Sun.COM /* Function: fr_stateifindexsync */
325211105SAlexandr.Nedvedicky@Sun.COM /* Returns: void */
325311105SAlexandr.Nedvedicky@Sun.COM /* Parameters: ifp - current network interface descriptor (ifindex) */
325411105SAlexandr.Nedvedicky@Sun.COM /* newifp - new interface descriptor (new ifindex) */
325511105SAlexandr.Nedvedicky@Sun.COM /* ifs - pointer to IPF stack */
325611105SAlexandr.Nedvedicky@Sun.COM /* */
325711105SAlexandr.Nedvedicky@Sun.COM /* Write Locks: assumes ipf_mutex is locked */
325811105SAlexandr.Nedvedicky@Sun.COM /* */
325911105SAlexandr.Nedvedicky@Sun.COM /* Updates all interface indeces matching to ifp with new interface index */
326011105SAlexandr.Nedvedicky@Sun.COM /* value. */
326111105SAlexandr.Nedvedicky@Sun.COM /* ------------------------------------------------------------------------ */
fr_stateifindexsync(ifp,newifp,ifs)326211105SAlexandr.Nedvedicky@Sun.COM void fr_stateifindexsync(ifp, newifp, ifs)
326311105SAlexandr.Nedvedicky@Sun.COM void *ifp;
326411105SAlexandr.Nedvedicky@Sun.COM void *newifp;
326511105SAlexandr.Nedvedicky@Sun.COM ipf_stack_t *ifs;
326611105SAlexandr.Nedvedicky@Sun.COM {
326711105SAlexandr.Nedvedicky@Sun.COM ipstate_t *is;
326811105SAlexandr.Nedvedicky@Sun.COM int i;
326911105SAlexandr.Nedvedicky@Sun.COM
327011105SAlexandr.Nedvedicky@Sun.COM WRITE_ENTER(&ifs->ifs_ipf_state);
327111105SAlexandr.Nedvedicky@Sun.COM
327211105SAlexandr.Nedvedicky@Sun.COM for (is = ifs->ifs_ips_list; is != NULL; is = is->is_next) {
327311105SAlexandr.Nedvedicky@Sun.COM
327411105SAlexandr.Nedvedicky@Sun.COM for (i = 0; i < 4; i++) {
327511105SAlexandr.Nedvedicky@Sun.COM if (is->is_ifp[i] == ifp)
327611105SAlexandr.Nedvedicky@Sun.COM is->is_ifp[i] = newifp;
327711105SAlexandr.Nedvedicky@Sun.COM }
327811105SAlexandr.Nedvedicky@Sun.COM }
327911105SAlexandr.Nedvedicky@Sun.COM
328011105SAlexandr.Nedvedicky@Sun.COM RWLOCK_EXIT(&ifs->ifs_ipf_state);
328111105SAlexandr.Nedvedicky@Sun.COM }
328211105SAlexandr.Nedvedicky@Sun.COM #endif
328311105SAlexandr.Nedvedicky@Sun.COM
32842393Syz155240 /* ------------------------------------------------------------------------ */
32852393Syz155240 /* Function: fr_delstate */
32868170SJohn.Ojemann@Sun.COM /* Returns: int - 0 = entry deleted, else ref count on entry */
32872393Syz155240 /* Parameters: is(I) - pointer to state structure to delete */
32882393Syz155240 /* why(I) - if not 0, log reason why it was deleted */
32898170SJohn.Ojemann@Sun.COM /* ifs - ipf stack instance */
32903187Sdr146992 /* Write Locks: ipf_state/ipf_global */
32912393Syz155240 /* */
32922393Syz155240 /* Deletes a state entry from the enumerated list as well as the hash table */
32932393Syz155240 /* and timeout queue lists. Make adjustments to hash table statistics and */
32942393Syz155240 /* global counters as required. */
32952393Syz155240 /* ------------------------------------------------------------------------ */
fr_delstate(is,why,ifs)32968170SJohn.Ojemann@Sun.COM int fr_delstate(is, why, ifs)
32972393Syz155240 ipstate_t *is;
32982393Syz155240 int why;
32993448Sdh155122 ipf_stack_t *ifs;
33002393Syz155240 {
33017432SJohn.Ojemann@Sun.COM int removed = 0;
33022393Syz155240
33033448Sdh155122 ASSERT(rw_write_held(&ifs->ifs_ipf_global.ipf_lk) == 0 ||
33043448Sdh155122 rw_write_held(&ifs->ifs_ipf_state.ipf_lk) == 0);
33052393Syz155240
33062393Syz155240 /*
33077432SJohn.Ojemann@Sun.COM * Start by removing the entry from the hash table of state entries
33087432SJohn.Ojemann@Sun.COM * so it will not be "used" again.
33097432SJohn.Ojemann@Sun.COM *
33107432SJohn.Ojemann@Sun.COM * It will remain in the "list" of state entries until all references
33117432SJohn.Ojemann@Sun.COM * have been accounted for.
33122393Syz155240 */
33132393Syz155240 if (is->is_phnext != NULL) {
33147432SJohn.Ojemann@Sun.COM removed = 1;
33152393Syz155240 *is->is_phnext = is->is_hnext;
33162393Syz155240 if (is->is_hnext != NULL)
33172393Syz155240 is->is_hnext->is_phnext = is->is_phnext;
33183448Sdh155122 if (ifs->ifs_ips_table[is->is_hv] == NULL)
33193448Sdh155122 ifs->ifs_ips_stats.iss_inuse--;
33203448Sdh155122 ifs->ifs_ips_stats.iss_bucketlen[is->is_hv]--;
33212393Syz155240
33222393Syz155240 is->is_phnext = NULL;
33232393Syz155240 is->is_hnext = NULL;
33242393Syz155240 }
33252393Syz155240
33262393Syz155240 /*
33273448Sdh155122 * Because ifs->ifs_ips_stats.iss_wild is a count of entries in the state
33282393Syz155240 * table that have wildcard flags set, only decerement it once
33292393Syz155240 * and do it here.
33302393Syz155240 */
33312393Syz155240 if (is->is_flags & (SI_WILDP|SI_WILDA)) {
33322393Syz155240 if (!(is->is_flags & SI_CLONED)) {
33333448Sdh155122 ATOMIC_DECL(ifs->ifs_ips_stats.iss_wild);
33342393Syz155240 }
33352393Syz155240 is->is_flags &= ~(SI_WILDP|SI_WILDA);
33362393Syz155240 }
33372393Syz155240
33382393Syz155240 /*
33392393Syz155240 * Next, remove it from the timeout queue it is in.
33402393Syz155240 */
33412393Syz155240 fr_deletequeueentry(&is->is_sti);
33422393Syz155240
33432393Syz155240 is->is_me = NULL;
33442393Syz155240
33452393Syz155240 /*
33462393Syz155240 * If it is still in use by something else, do not go any further,
33472393Syz155240 * but note that at this point it is now an orphan.
33482393Syz155240 */
33495055Sdr146992 MUTEX_ENTER(&is->is_lock);
33505055Sdr146992 if (is->is_ref > 1) {
33515055Sdr146992 is->is_ref--;
33525055Sdr146992 MUTEX_EXIT(&is->is_lock);
33537432SJohn.Ojemann@Sun.COM if (removed)
33547432SJohn.Ojemann@Sun.COM ifs->ifs_ips_stats.iss_orphans++;
33558170SJohn.Ojemann@Sun.COM return (is->is_ref);
33565055Sdr146992 }
33575055Sdr146992 MUTEX_EXIT(&is->is_lock);
33585055Sdr146992
33595055Sdr146992 is->is_ref = 0;
33602393Syz155240
33617432SJohn.Ojemann@Sun.COM /*
33627432SJohn.Ojemann@Sun.COM * If entry has already been removed from table,
33637432SJohn.Ojemann@Sun.COM * it means we're simply cleaning up an orphan.
33647432SJohn.Ojemann@Sun.COM */
33657432SJohn.Ojemann@Sun.COM if (!removed)
33667432SJohn.Ojemann@Sun.COM ifs->ifs_ips_stats.iss_orphans--;
33677432SJohn.Ojemann@Sun.COM
33686274Sjojemann if (is->is_tqehead[0] != NULL)
33696274Sjojemann (void) fr_deletetimeoutqueue(is->is_tqehead[0]);
33706274Sjojemann
33716274Sjojemann if (is->is_tqehead[1] != NULL)
33726274Sjojemann (void) fr_deletetimeoutqueue(is->is_tqehead[1]);
33732393Syz155240
33742393Syz155240 #ifdef IPFILTER_SYNC
33752393Syz155240 if (is->is_sync)
33762393Syz155240 ipfsync_del(is->is_sync);
33772393Syz155240 #endif
33782393Syz155240 #ifdef IPFILTER_SCAN
33792393Syz155240 (void) ipsc_detachis(is);
33802393Syz155240 #endif
33812393Syz155240
33827432SJohn.Ojemann@Sun.COM /*
33837432SJohn.Ojemann@Sun.COM * Now remove it from master list of state table entries.
33847432SJohn.Ojemann@Sun.COM */
33857432SJohn.Ojemann@Sun.COM if (is->is_pnext != NULL) {
33867432SJohn.Ojemann@Sun.COM *is->is_pnext = is->is_next;
33877432SJohn.Ojemann@Sun.COM if (is->is_next != NULL) {
33887432SJohn.Ojemann@Sun.COM is->is_next->is_pnext = is->is_pnext;
33897432SJohn.Ojemann@Sun.COM is->is_next = NULL;
33907432SJohn.Ojemann@Sun.COM }
33917432SJohn.Ojemann@Sun.COM is->is_pnext = NULL;
33927432SJohn.Ojemann@Sun.COM }
33937432SJohn.Ojemann@Sun.COM
33943448Sdh155122 if (ifs->ifs_ipstate_logging != 0 && why != 0)
33953448Sdh155122 ipstate_log(is, why, ifs);
33962393Syz155240
33972393Syz155240 if (is->is_rule != NULL) {
33982393Syz155240 is->is_rule->fr_statecnt--;
33993448Sdh155122 (void)fr_derefrule(&is->is_rule, ifs);
34002393Syz155240 }
34012393Syz155240
34022393Syz155240 MUTEX_DESTROY(&is->is_lock);
34032393Syz155240 KFREE(is);
34043448Sdh155122 ifs->ifs_ips_num--;
34058170SJohn.Ojemann@Sun.COM
34068170SJohn.Ojemann@Sun.COM return (0);
34072393Syz155240 }
34082393Syz155240
34092393Syz155240
34102393Syz155240 /* ------------------------------------------------------------------------ */
34112393Syz155240 /* Function: fr_timeoutstate */
34122393Syz155240 /* Returns: Nil */
34138170SJohn.Ojemann@Sun.COM /* Parameters: ifs - ipf stack instance */
34142393Syz155240 /* */
34152393Syz155240 /* Slowly expire held state for thingslike UDP and ICMP. The algorithm */
34162393Syz155240 /* used here is to keep the queue sorted with the oldest things at the top */
34172393Syz155240 /* and the youngest at the bottom. So if the top one doesn't need to be */
34182393Syz155240 /* expired then neither will any under it. */
34192393Syz155240 /* ------------------------------------------------------------------------ */
fr_timeoutstate(ifs)34203448Sdh155122 void fr_timeoutstate(ifs)
34213448Sdh155122 ipf_stack_t *ifs;
34222393Syz155240 {
34232393Syz155240 ipftq_t *ifq, *ifqnext;
34242393Syz155240 ipftqent_t *tqe, *tqn;
34252393Syz155240 ipstate_t *is;
34262393Syz155240 SPL_INT(s);
34272393Syz155240
34282393Syz155240 SPL_NET(s);
34293448Sdh155122 WRITE_ENTER(&ifs->ifs_ipf_state);
34303448Sdh155122 for (ifq = ifs->ifs_ips_tqtqb; ifq != NULL; ifq = ifq->ifq_next)
34312393Syz155240 for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) {
34323448Sdh155122 if (tqe->tqe_die > ifs->ifs_fr_ticks)
34332393Syz155240 break;
34342393Syz155240 tqn = tqe->tqe_next;
34352393Syz155240 is = tqe->tqe_parent;
34368170SJohn.Ojemann@Sun.COM (void) fr_delstate(is, ISL_EXPIRE, ifs);
34372393Syz155240 }
34382393Syz155240
34396274Sjojemann for (ifq = ifs->ifs_ips_utqe; ifq != NULL; ifq = ifq->ifq_next) {
34402393Syz155240 for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) {
34413448Sdh155122 if (tqe->tqe_die > ifs->ifs_fr_ticks)
34422393Syz155240 break;
34432393Syz155240 tqn = tqe->tqe_next;
34442393Syz155240 is = tqe->tqe_parent;
34458170SJohn.Ojemann@Sun.COM (void) fr_delstate(is, ISL_EXPIRE, ifs);
34462393Syz155240 }
34472393Syz155240 }
34482393Syz155240
34493448Sdh155122 for (ifq = ifs->ifs_ips_utqe; ifq != NULL; ifq = ifqnext) {
34502393Syz155240 ifqnext = ifq->ifq_next;
34512393Syz155240
34522393Syz155240 if (((ifq->ifq_flags & IFQF_DELETE) != 0) &&
34532393Syz155240 (ifq->ifq_ref == 0)) {
34543448Sdh155122 fr_freetimeoutqueue(ifq, ifs);
34552393Syz155240 }
34562393Syz155240 }
34572393Syz155240
34583448Sdh155122 if (ifs->ifs_fr_state_doflush) {
34598170SJohn.Ojemann@Sun.COM (void) fr_state_flush(FLUSH_TABLE_EXTRA, 0, ifs);
34603448Sdh155122 ifs->ifs_fr_state_doflush = 0;
34612393Syz155240 }
34623448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
34632393Syz155240 SPL_X(s);
34642393Syz155240 }
34652393Syz155240
34662393Syz155240
34678170SJohn.Ojemann@Sun.COM /* ---------------------------------------------------------------------- */
34688170SJohn.Ojemann@Sun.COM /* Function: fr_state_flush */
34698170SJohn.Ojemann@Sun.COM /* Returns: int - 0 == success, -1 == failure */
34708170SJohn.Ojemann@Sun.COM /* Parameters: flush_option - how to flush the active State table */
34718170SJohn.Ojemann@Sun.COM /* proto - IP version to flush (4, 6, or both) */
34728170SJohn.Ojemann@Sun.COM /* ifs - ipf stack instance */
34738170SJohn.Ojemann@Sun.COM /* Write Locks: ipf_state */
34748170SJohn.Ojemann@Sun.COM /* */
34758170SJohn.Ojemann@Sun.COM /* Flush state tables. Three possible flush options currently defined: */
34768170SJohn.Ojemann@Sun.COM /* */
34778170SJohn.Ojemann@Sun.COM /* FLUSH_TABLE_ALL : Flush all state table entries */
34788170SJohn.Ojemann@Sun.COM /* */
34798170SJohn.Ojemann@Sun.COM /* FLUSH_TABLE_CLOSING : Flush entries with TCP connections which */
34808170SJohn.Ojemann@Sun.COM /* have started to close on both ends using */
34818170SJohn.Ojemann@Sun.COM /* ipf_flushclosing(). */
34828170SJohn.Ojemann@Sun.COM /* */
34838170SJohn.Ojemann@Sun.COM /* FLUSH_TABLE_EXTRA : First, flush entries which are "almost" closed. */
34848170SJohn.Ojemann@Sun.COM /* Then, if needed, flush entries with TCP */
34858170SJohn.Ojemann@Sun.COM /* connections which have been idle for a long */
34868170SJohn.Ojemann@Sun.COM /* time with ipf_extraflush(). */
34878170SJohn.Ojemann@Sun.COM /* ---------------------------------------------------------------------- */
fr_state_flush(flush_option,proto,ifs)34888170SJohn.Ojemann@Sun.COM static int fr_state_flush(flush_option, proto, ifs)
34898170SJohn.Ojemann@Sun.COM int flush_option, proto;
34903448Sdh155122 ipf_stack_t *ifs;
34912393Syz155240 {
34928170SJohn.Ojemann@Sun.COM ipstate_t *is, *isn;
34938170SJohn.Ojemann@Sun.COM int removed;
34942393Syz155240 SPL_INT(s);
34952393Syz155240
34962393Syz155240 removed = 0;
34972393Syz155240
34982393Syz155240 SPL_NET(s);
34998170SJohn.Ojemann@Sun.COM switch (flush_option)
35008170SJohn.Ojemann@Sun.COM {
35018170SJohn.Ojemann@Sun.COM case FLUSH_TABLE_ALL:
35028170SJohn.Ojemann@Sun.COM isn = ifs->ifs_ips_list;
35038170SJohn.Ojemann@Sun.COM while ((is = isn) != NULL) {
35048170SJohn.Ojemann@Sun.COM isn = is->is_next;
35058170SJohn.Ojemann@Sun.COM if ((proto != 0) && (is->is_v != proto))
35068170SJohn.Ojemann@Sun.COM continue;
35078170SJohn.Ojemann@Sun.COM if (fr_delstate(is, ISL_FLUSH, ifs) == 0)
35088170SJohn.Ojemann@Sun.COM removed++;
35092393Syz155240 }
35108170SJohn.Ojemann@Sun.COM break;
35118170SJohn.Ojemann@Sun.COM
35128170SJohn.Ojemann@Sun.COM case FLUSH_TABLE_CLOSING:
35138170SJohn.Ojemann@Sun.COM removed = ipf_flushclosing(STATE_FLUSH,
35148170SJohn.Ojemann@Sun.COM IPF_TCPS_CLOSE_WAIT,
35158170SJohn.Ojemann@Sun.COM ifs->ifs_ips_tqtqb,
35168170SJohn.Ojemann@Sun.COM ifs->ifs_ips_utqe,
35178170SJohn.Ojemann@Sun.COM ifs);
35188170SJohn.Ojemann@Sun.COM break;
35198170SJohn.Ojemann@Sun.COM
35208170SJohn.Ojemann@Sun.COM case FLUSH_TABLE_EXTRA:
35218170SJohn.Ojemann@Sun.COM removed = ipf_flushclosing(STATE_FLUSH,
35228170SJohn.Ojemann@Sun.COM IPF_TCPS_FIN_WAIT_2,
35238170SJohn.Ojemann@Sun.COM ifs->ifs_ips_tqtqb,
35248170SJohn.Ojemann@Sun.COM ifs->ifs_ips_utqe,
35258170SJohn.Ojemann@Sun.COM ifs);
35268170SJohn.Ojemann@Sun.COM
35278170SJohn.Ojemann@Sun.COM /*
35288170SJohn.Ojemann@Sun.COM * Be sure we haven't done this in the last 10 seconds.
35298170SJohn.Ojemann@Sun.COM */
35308170SJohn.Ojemann@Sun.COM if (ifs->ifs_fr_ticks - ifs->ifs_ips_last_force_flush <
35318170SJohn.Ojemann@Sun.COM IPF_TTLVAL(10))
35322393Syz155240 break;
35338170SJohn.Ojemann@Sun.COM ifs->ifs_ips_last_force_flush = ifs->ifs_fr_ticks;
35348170SJohn.Ojemann@Sun.COM removed += ipf_extraflush(STATE_FLUSH,
35358170SJohn.Ojemann@Sun.COM &ifs->ifs_ips_tqtqb[IPF_TCPS_ESTABLISHED],
35368170SJohn.Ojemann@Sun.COM ifs->ifs_ips_utqe,
35378170SJohn.Ojemann@Sun.COM ifs);
35388170SJohn.Ojemann@Sun.COM break;
35398170SJohn.Ojemann@Sun.COM
35408170SJohn.Ojemann@Sun.COM default: /* Flush Nothing */
35418170SJohn.Ojemann@Sun.COM break;
35422393Syz155240 }
35432393Syz155240
35442393Syz155240 SPL_X(s);
35458170SJohn.Ojemann@Sun.COM return (removed);
35462393Syz155240 }
35472393Syz155240
35482393Syz155240
35492393Syz155240 /* ------------------------------------------------------------------------ */
35502393Syz155240 /* Function: fr_tcp_age */
35512393Syz155240 /* Returns: int - 1 == state transition made, 0 == no change (rejected) */
35522393Syz155240 /* Parameters: tq(I) - pointer to timeout queue information */
35532393Syz155240 /* fin(I) - pointer to packet information */
35542393Syz155240 /* tqtab(I) - TCP timeout queue table this is in */
35552393Syz155240 /* flags(I) - flags from state/NAT entry */
35562393Syz155240 /* */
35572393Syz155240 /* Rewritten by Arjan de Vet <Arjan.deVet@adv.iae.nl>, 2000-07-29: */
35582393Syz155240 /* */
35592393Syz155240 /* - (try to) base state transitions on real evidence only, */
35602393Syz155240 /* i.e. packets that are sent and have been received by ipfilter; */
35612393Syz155240 /* diagram 18.12 of TCP/IP volume 1 by W. Richard Stevens was used. */
35622393Syz155240 /* */
35632393Syz155240 /* - deal with half-closed connections correctly; */
35642393Syz155240 /* */
35652393Syz155240 /* - store the state of the source in state[0] such that ipfstat */
35662393Syz155240 /* displays the state as source/dest instead of dest/source; the calls */
35672393Syz155240 /* to fr_tcp_age have been changed accordingly. */
35682393Syz155240 /* */
35692393Syz155240 /* Internal Parameters: */
35702393Syz155240 /* */
35712393Syz155240 /* state[0] = state of source (host that initiated connection) */
35722393Syz155240 /* state[1] = state of dest (host that accepted the connection) */
35732393Syz155240 /* */
35742393Syz155240 /* dir == 0 : a packet from source to dest */
35752393Syz155240 /* dir == 1 : a packet from dest to source */
35762393Syz155240 /* */
35772393Syz155240 /* Locking: it is assumed that the parent of the tqe structure is locked. */
35782393Syz155240 /* ------------------------------------------------------------------------ */
fr_tcp_age(tqe,fin,tqtab,flags)35792393Syz155240 int fr_tcp_age(tqe, fin, tqtab, flags)
35802393Syz155240 ipftqent_t *tqe;
35812393Syz155240 fr_info_t *fin;
35822393Syz155240 ipftq_t *tqtab;
35832393Syz155240 int flags;
35842393Syz155240 {
35852393Syz155240 int dlen, ostate, nstate, rval, dir;
35862393Syz155240 u_char tcpflags;
35872393Syz155240 tcphdr_t *tcp;
35883448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs;
35892393Syz155240
35902393Syz155240 tcp = fin->fin_dp;
35912393Syz155240
35922393Syz155240 rval = 0;
35932393Syz155240 dir = fin->fin_rev;
35942393Syz155240 tcpflags = tcp->th_flags;
35952393Syz155240 dlen = fin->fin_dlen - (TCP_OFF(tcp) << 2);
35962393Syz155240
35979888SAlexandr.Nedvedicky@Sun.COM ostate = tqe->tqe_state[1 - dir];
35989888SAlexandr.Nedvedicky@Sun.COM nstate = tqe->tqe_state[dir];
35999888SAlexandr.Nedvedicky@Sun.COM
36009888SAlexandr.Nedvedicky@Sun.COM DTRACE_PROBE4(
36019888SAlexandr.Nedvedicky@Sun.COM indata,
36029888SAlexandr.Nedvedicky@Sun.COM fr_info_t *, fin,
36039888SAlexandr.Nedvedicky@Sun.COM int, ostate,
36049888SAlexandr.Nedvedicky@Sun.COM int, nstate,
36059888SAlexandr.Nedvedicky@Sun.COM u_char, tcpflags
36069888SAlexandr.Nedvedicky@Sun.COM );
36079888SAlexandr.Nedvedicky@Sun.COM
36082393Syz155240 if (tcpflags & TH_RST) {
36092393Syz155240 if (!(tcpflags & TH_PUSH) && !dlen)
36102393Syz155240 nstate = IPF_TCPS_CLOSED;
36112393Syz155240 else
36122393Syz155240 nstate = IPF_TCPS_CLOSE_WAIT;
36139888SAlexandr.Nedvedicky@Sun.COM
36149888SAlexandr.Nedvedicky@Sun.COM /*
36159888SAlexandr.Nedvedicky@Sun.COM * Once RST is received, we must advance peer's state to
36169888SAlexandr.Nedvedicky@Sun.COM * CLOSE_WAIT.
36179888SAlexandr.Nedvedicky@Sun.COM */
36189888SAlexandr.Nedvedicky@Sun.COM if (ostate <= IPF_TCPS_ESTABLISHED) {
36199888SAlexandr.Nedvedicky@Sun.COM tqe->tqe_state[1 - dir] = IPF_TCPS_CLOSE_WAIT;
36209888SAlexandr.Nedvedicky@Sun.COM }
36212393Syz155240 rval = 1;
36222393Syz155240 } else {
36232393Syz155240
36242393Syz155240 switch (nstate)
36252393Syz155240 {
36269888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_LISTEN: /* 0 */
36272393Syz155240 if ((tcpflags & TH_OPENING) == TH_OPENING) {
36282393Syz155240 /*
36292393Syz155240 * 'dir' received an S and sends SA in
36302393Syz155240 * response, CLOSED -> SYN_RECEIVED
36312393Syz155240 */
36322393Syz155240 nstate = IPF_TCPS_SYN_RECEIVED;
36332393Syz155240 rval = 1;
36342393Syz155240 } else if ((tcpflags & TH_OPENING) == TH_SYN) {
36352393Syz155240 /* 'dir' sent S, CLOSED -> SYN_SENT */
36362393Syz155240 nstate = IPF_TCPS_SYN_SENT;
36372393Syz155240 rval = 1;
36382393Syz155240 }
36392393Syz155240 /*
36402393Syz155240 * the next piece of code makes it possible to get
36412393Syz155240 * already established connections into the state table
36422393Syz155240 * after a restart or reload of the filter rules; this
36432393Syz155240 * does not work when a strict 'flags S keep state' is
36442393Syz155240 * used for tcp connections of course
36452393Syz155240 */
36462393Syz155240 if (((flags & IS_TCPFSM) == 0) &&
36472393Syz155240 ((tcpflags & TH_ACKMASK) == TH_ACK)) {
36482393Syz155240 /*
36492393Syz155240 * we saw an A, guess 'dir' is in ESTABLISHED
36502393Syz155240 * mode
36512393Syz155240 */
36522393Syz155240 switch (ostate)
36532393Syz155240 {
36549888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_LISTEN :
36552393Syz155240 case IPF_TCPS_SYN_RECEIVED :
36562393Syz155240 nstate = IPF_TCPS_HALF_ESTAB;
36572393Syz155240 rval = 1;
36582393Syz155240 break;
36592393Syz155240 case IPF_TCPS_HALF_ESTAB :
36602393Syz155240 case IPF_TCPS_ESTABLISHED :
36612393Syz155240 nstate = IPF_TCPS_ESTABLISHED;
36622393Syz155240 rval = 1;
36632393Syz155240 break;
36642393Syz155240 default :
36652393Syz155240 break;
36662393Syz155240 }
36672393Syz155240 }
36682393Syz155240 /*
36692393Syz155240 * TODO: besides regular ACK packets we can have other
36702393Syz155240 * packets as well; it is yet to be determined how we
36712393Syz155240 * should initialize the states in those cases
36722393Syz155240 */
36732393Syz155240 break;
36742393Syz155240
36759888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_SYN_SENT: /* 1 */
36762393Syz155240 if ((tcpflags & ~(TH_ECN|TH_CWR)) == TH_SYN) {
36772393Syz155240 /*
36782393Syz155240 * A retransmitted SYN packet. We do not reset
36792393Syz155240 * the timeout here to fr_tcptimeout because a
36802393Syz155240 * connection connect timeout does not renew
36812393Syz155240 * after every packet that is sent. We need to
36822393Syz155240 * set rval so as to indicate the packet has
36832393Syz155240 * passed the check for its flags being valid
36842393Syz155240 * in the TCP FSM. Setting rval to 2 has the
36852393Syz155240 * result of not resetting the timeout.
36862393Syz155240 */
36872393Syz155240 rval = 2;
36882393Syz155240 } else if ((tcpflags & (TH_SYN|TH_FIN|TH_ACK)) ==
36892393Syz155240 TH_ACK) {
36902393Syz155240 /*
36912393Syz155240 * we see an A from 'dir' which is in SYN_SENT
36922393Syz155240 * state: 'dir' sent an A in response to an SA
36932393Syz155240 * which it received, SYN_SENT -> ESTABLISHED
36942393Syz155240 */
36952393Syz155240 nstate = IPF_TCPS_ESTABLISHED;
36962393Syz155240 rval = 1;
36972393Syz155240 } else if (tcpflags & TH_FIN) {
36982393Syz155240 /*
36992393Syz155240 * we see an F from 'dir' which is in SYN_SENT
37002393Syz155240 * state and wants to close its side of the
37012393Syz155240 * connection; SYN_SENT -> FIN_WAIT_1
37022393Syz155240 */
37032393Syz155240 nstate = IPF_TCPS_FIN_WAIT_1;
37042393Syz155240 rval = 1;
37052393Syz155240 } else if ((tcpflags & TH_OPENING) == TH_OPENING) {
37062393Syz155240 /*
37072393Syz155240 * we see an SA from 'dir' which is already in
37082393Syz155240 * SYN_SENT state, this means we have a
37092393Syz155240 * simultaneous open; SYN_SENT -> SYN_RECEIVED
37102393Syz155240 */
37112393Syz155240 nstate = IPF_TCPS_SYN_RECEIVED;
37122393Syz155240 rval = 1;
37132393Syz155240 }
37142393Syz155240 break;
37152393Syz155240
37169888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_SYN_RECEIVED: /* 2 */
37172393Syz155240 if ((tcpflags & (TH_SYN|TH_FIN|TH_ACK)) == TH_ACK) {
37182393Syz155240 /*
37192393Syz155240 * we see an A from 'dir' which was in
37202393Syz155240 * SYN_RECEIVED state so it must now be in
37212393Syz155240 * established state, SYN_RECEIVED ->
37222393Syz155240 * ESTABLISHED
37232393Syz155240 */
37242393Syz155240 nstate = IPF_TCPS_ESTABLISHED;
37252393Syz155240 rval = 1;
37262393Syz155240 } else if ((tcpflags & ~(TH_ECN|TH_CWR)) ==
37272393Syz155240 TH_OPENING) {
37282393Syz155240 /*
37292393Syz155240 * We see an SA from 'dir' which is already in
37302393Syz155240 * SYN_RECEIVED state.
37312393Syz155240 */
37322393Syz155240 rval = 2;
37332393Syz155240 } else if (tcpflags & TH_FIN) {
37342393Syz155240 /*
37352393Syz155240 * we see an F from 'dir' which is in
37362393Syz155240 * SYN_RECEIVED state and wants to close its
37372393Syz155240 * side of the connection; SYN_RECEIVED ->
37382393Syz155240 * FIN_WAIT_1
37392393Syz155240 */
37402393Syz155240 nstate = IPF_TCPS_FIN_WAIT_1;
37412393Syz155240 rval = 1;
37422393Syz155240 }
37432393Syz155240 break;
37442393Syz155240
37459888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_HALF_ESTAB: /* 3 */
37469888SAlexandr.Nedvedicky@Sun.COM if (tcpflags & TH_FIN) {
37479888SAlexandr.Nedvedicky@Sun.COM nstate = IPF_TCPS_FIN_WAIT_1;
37489888SAlexandr.Nedvedicky@Sun.COM rval = 1;
37499888SAlexandr.Nedvedicky@Sun.COM } else if ((tcpflags & TH_ACKMASK) == TH_ACK) {
37509888SAlexandr.Nedvedicky@Sun.COM /*
37519888SAlexandr.Nedvedicky@Sun.COM * If we've picked up a connection in mid
37529888SAlexandr.Nedvedicky@Sun.COM * flight, we could be looking at a follow on
37539888SAlexandr.Nedvedicky@Sun.COM * packet from the same direction as the one
37549888SAlexandr.Nedvedicky@Sun.COM * that created this state. Recognise it but
37559888SAlexandr.Nedvedicky@Sun.COM * do not advance the entire connection's
37569888SAlexandr.Nedvedicky@Sun.COM * state.
37579888SAlexandr.Nedvedicky@Sun.COM */
37589888SAlexandr.Nedvedicky@Sun.COM switch (ostate)
37599888SAlexandr.Nedvedicky@Sun.COM {
37609888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_LISTEN :
37619888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_SYN_SENT :
37629888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_SYN_RECEIVED :
37639888SAlexandr.Nedvedicky@Sun.COM rval = 1;
37649888SAlexandr.Nedvedicky@Sun.COM break;
37659888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_HALF_ESTAB :
37669888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_ESTABLISHED :
37672393Syz155240 nstate = IPF_TCPS_ESTABLISHED;
37689888SAlexandr.Nedvedicky@Sun.COM rval = 1;
37699888SAlexandr.Nedvedicky@Sun.COM break;
37709888SAlexandr.Nedvedicky@Sun.COM default :
37719888SAlexandr.Nedvedicky@Sun.COM break;
37722393Syz155240 }
37732393Syz155240 }
37742393Syz155240 break;
37752393Syz155240
37769888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_ESTABLISHED: /* 4 */
37772393Syz155240 rval = 1;
37782393Syz155240 if (tcpflags & TH_FIN) {
37792393Syz155240 /*
37802393Syz155240 * 'dir' closed its side of the connection;
37812393Syz155240 * this gives us a half-closed connection;
37822393Syz155240 * ESTABLISHED -> FIN_WAIT_1
37832393Syz155240 */
37849888SAlexandr.Nedvedicky@Sun.COM if (ostate == IPF_TCPS_FIN_WAIT_1) {
37859888SAlexandr.Nedvedicky@Sun.COM nstate = IPF_TCPS_CLOSING;
37869888SAlexandr.Nedvedicky@Sun.COM } else {
37879888SAlexandr.Nedvedicky@Sun.COM nstate = IPF_TCPS_FIN_WAIT_1;
37889888SAlexandr.Nedvedicky@Sun.COM }
37892393Syz155240 } else if (tcpflags & TH_ACK) {
37902393Syz155240 /*
37912393Syz155240 * an ACK, should we exclude other flags here?
37922393Syz155240 */
37932393Syz155240 if (ostate == IPF_TCPS_FIN_WAIT_1) {
37942393Syz155240 /*
37952393Syz155240 * We know the other side did an active
37962393Syz155240 * close, so we are ACKing the recvd
37972393Syz155240 * FIN packet (does the window matching
37982393Syz155240 * code guarantee this?) and go into
37992393Syz155240 * CLOSE_WAIT state; this gives us a
38002393Syz155240 * half-closed connection
38012393Syz155240 */
38022393Syz155240 nstate = IPF_TCPS_CLOSE_WAIT;
38032393Syz155240 } else if (ostate < IPF_TCPS_CLOSE_WAIT) {
38042393Syz155240 /*
38052393Syz155240 * still a fully established
38062393Syz155240 * connection reset timeout
38072393Syz155240 */
38082393Syz155240 nstate = IPF_TCPS_ESTABLISHED;
38092393Syz155240 }
38102393Syz155240 }
38112393Syz155240 break;
38122393Syz155240
38139888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_CLOSE_WAIT: /* 5 */
38142393Syz155240 rval = 1;
38152393Syz155240 if (tcpflags & TH_FIN) {
38162393Syz155240 /*
38172393Syz155240 * application closed and 'dir' sent a FIN,
38182393Syz155240 * we're now going into LAST_ACK state
38192393Syz155240 */
38202393Syz155240 nstate = IPF_TCPS_LAST_ACK;
38212393Syz155240 } else {
38222393Syz155240 /*
38232393Syz155240 * we remain in CLOSE_WAIT because the other
38242393Syz155240 * side has closed already and we did not
38252393Syz155240 * close our side yet; reset timeout
38262393Syz155240 */
38272393Syz155240 nstate = IPF_TCPS_CLOSE_WAIT;
38282393Syz155240 }
38292393Syz155240 break;
38302393Syz155240
38319888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_FIN_WAIT_1: /* 6 */
38322393Syz155240 rval = 1;
38332393Syz155240 if ((tcpflags & TH_ACK) &&
38342393Syz155240 ostate > IPF_TCPS_CLOSE_WAIT) {
38352393Syz155240 /*
38362393Syz155240 * if the other side is not active anymore
38372393Syz155240 * it has sent us a FIN packet that we are
38382393Syz155240 * ack'ing now with an ACK; this means both
38392393Syz155240 * sides have now closed the connection and
38409888SAlexandr.Nedvedicky@Sun.COM * we go into LAST_ACK
38412393Syz155240 */
38422393Syz155240 /*
38432393Syz155240 * XXX: how do we know we really are ACKing
38442393Syz155240 * the FIN packet here? does the window code
38452393Syz155240 * guarantee that?
38462393Syz155240 */
38479888SAlexandr.Nedvedicky@Sun.COM nstate = IPF_TCPS_LAST_ACK;
38482393Syz155240 } else {
38492393Syz155240 /*
38502393Syz155240 * we closed our side of the connection
38512393Syz155240 * already but the other side is still active
38522393Syz155240 * (ESTABLISHED/CLOSE_WAIT); continue with
38532393Syz155240 * this half-closed connection
38542393Syz155240 */
38552393Syz155240 nstate = IPF_TCPS_FIN_WAIT_1;
38562393Syz155240 }
38572393Syz155240 break;
38582393Syz155240
38599888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_CLOSING: /* 7 */
38609888SAlexandr.Nedvedicky@Sun.COM if ((tcpflags & (TH_FIN|TH_ACK)) == TH_ACK) {
38619888SAlexandr.Nedvedicky@Sun.COM nstate = IPF_TCPS_TIME_WAIT;
38629888SAlexandr.Nedvedicky@Sun.COM }
38639888SAlexandr.Nedvedicky@Sun.COM rval = 1;
38642393Syz155240 break;
38652393Syz155240
38669888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_LAST_ACK: /* 8 */
38672393Syz155240 /*
38686647San207044 * We want to reset timer here to keep state in table.
38696647San207044 * If we would allow the state to time out here, while
38706647San207044 * there would still be packets being retransmitted, we
38716647San207044 * would cut off line between the two peers preventing
38726647San207044 * them to close connection properly.
38732393Syz155240 */
38746647San207044 rval = 1;
38752393Syz155240 break;
38762393Syz155240
38779888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_FIN_WAIT_2: /* 9 */
38789888SAlexandr.Nedvedicky@Sun.COM /* NOT USED */
38792393Syz155240 break;
38802393Syz155240
38819888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_TIME_WAIT: /* 10 */
38822393Syz155240 /* we're in 2MSL timeout now */
38839888SAlexandr.Nedvedicky@Sun.COM if (ostate == IPF_TCPS_LAST_ACK) {
38849888SAlexandr.Nedvedicky@Sun.COM nstate = IPF_TCPS_CLOSED;
38859888SAlexandr.Nedvedicky@Sun.COM rval = 1;
38869888SAlexandr.Nedvedicky@Sun.COM } else {
38879888SAlexandr.Nedvedicky@Sun.COM rval = 2;
38889888SAlexandr.Nedvedicky@Sun.COM }
38899888SAlexandr.Nedvedicky@Sun.COM break;
38909888SAlexandr.Nedvedicky@Sun.COM
38919888SAlexandr.Nedvedicky@Sun.COM case IPF_TCPS_CLOSED: /* 11 */
38929888SAlexandr.Nedvedicky@Sun.COM rval = 2;
38932393Syz155240 break;
38942393Syz155240
38952393Syz155240 default :
38962393Syz155240 #if defined(_KERNEL)
38979888SAlexandr.Nedvedicky@Sun.COM ASSERT(nstate >= IPF_TCPS_LISTEN &&
38989888SAlexandr.Nedvedicky@Sun.COM nstate <= IPF_TCPS_CLOSED);
38992393Syz155240 #else
39002393Syz155240 abort();
39012393Syz155240 #endif
39022393Syz155240 break;
39032393Syz155240 }
39042393Syz155240 }
39052393Syz155240
39062393Syz155240 /*
39072393Syz155240 * If rval == 2 then do not update the queue position, but treat the
39082393Syz155240 * packet as being ok.
39092393Syz155240 */
39109888SAlexandr.Nedvedicky@Sun.COM if (rval == 2) {
39119888SAlexandr.Nedvedicky@Sun.COM DTRACE_PROBE1(state_keeping_timer, int, nstate);
39122393Syz155240 rval = 1;
39139888SAlexandr.Nedvedicky@Sun.COM }
39142393Syz155240 else if (rval == 1) {
39152393Syz155240 tqe->tqe_state[dir] = nstate;
39169888SAlexandr.Nedvedicky@Sun.COM /*
39179888SAlexandr.Nedvedicky@Sun.COM * The nstate can either advance to a new state, or remain
39189888SAlexandr.Nedvedicky@Sun.COM * unchanged, resetting the timer by moving to the bottom of
39199888SAlexandr.Nedvedicky@Sun.COM * the queue.
39209888SAlexandr.Nedvedicky@Sun.COM */
39219888SAlexandr.Nedvedicky@Sun.COM DTRACE_PROBE1(state_done, int, nstate);
39229888SAlexandr.Nedvedicky@Sun.COM
39232393Syz155240 if ((tqe->tqe_flags & TQE_RULEBASED) == 0)
39243448Sdh155122 fr_movequeue(tqe, tqe->tqe_ifq, tqtab + nstate, ifs);
39252393Syz155240 }
39262393Syz155240
39272393Syz155240 return rval;
39282393Syz155240 }
39292393Syz155240
39302393Syz155240
39312393Syz155240 /* ------------------------------------------------------------------------ */
39322393Syz155240 /* Function: ipstate_log */
39332393Syz155240 /* Returns: Nil */
39342393Syz155240 /* Parameters: is(I) - pointer to state structure */
39352393Syz155240 /* type(I) - type of log entry to create */
39362393Syz155240 /* */
39372393Syz155240 /* Creates a state table log entry using the state structure and type info. */
39382393Syz155240 /* passed in. Log packet/byte counts, source/destination address and other */
39392393Syz155240 /* protocol specific information. */
39402393Syz155240 /* ------------------------------------------------------------------------ */
ipstate_log(is,type,ifs)39413448Sdh155122 void ipstate_log(is, type, ifs)
39422393Syz155240 struct ipstate *is;
39432393Syz155240 u_int type;
39443448Sdh155122 ipf_stack_t *ifs;
39452393Syz155240 {
39462393Syz155240 #ifdef IPFILTER_LOG
39472393Syz155240 struct ipslog ipsl;
39482393Syz155240 size_t sizes[1];
39492393Syz155240 void *items[1];
39502393Syz155240 int types[1];
39512393Syz155240
39522393Syz155240 /*
39532393Syz155240 * Copy information out of the ipstate_t structure and into the
39542393Syz155240 * structure used for logging.
39552393Syz155240 */
39562393Syz155240 ipsl.isl_type = type;
39572393Syz155240 ipsl.isl_pkts[0] = is->is_pkts[0] + is->is_icmppkts[0];
39582393Syz155240 ipsl.isl_bytes[0] = is->is_bytes[0];
39592393Syz155240 ipsl.isl_pkts[1] = is->is_pkts[1] + is->is_icmppkts[1];
39602393Syz155240 ipsl.isl_bytes[1] = is->is_bytes[1];
39612393Syz155240 ipsl.isl_pkts[2] = is->is_pkts[2] + is->is_icmppkts[2];
39622393Syz155240 ipsl.isl_bytes[2] = is->is_bytes[2];
39632393Syz155240 ipsl.isl_pkts[3] = is->is_pkts[3] + is->is_icmppkts[3];
39642393Syz155240 ipsl.isl_bytes[3] = is->is_bytes[3];
39652393Syz155240 ipsl.isl_src = is->is_src;
39662393Syz155240 ipsl.isl_dst = is->is_dst;
39672393Syz155240 ipsl.isl_p = is->is_p;
39682393Syz155240 ipsl.isl_v = is->is_v;
39692393Syz155240 ipsl.isl_flags = is->is_flags;
39702393Syz155240 ipsl.isl_tag = is->is_tag;
39712393Syz155240 ipsl.isl_rulen = is->is_rulen;
39722393Syz155240 (void) strncpy(ipsl.isl_group, is->is_group, FR_GROUPLEN);
39732393Syz155240
39742393Syz155240 if (ipsl.isl_p == IPPROTO_TCP || ipsl.isl_p == IPPROTO_UDP) {
39752393Syz155240 ipsl.isl_sport = is->is_sport;
39762393Syz155240 ipsl.isl_dport = is->is_dport;
39772393Syz155240 if (ipsl.isl_p == IPPROTO_TCP) {
39782393Syz155240 ipsl.isl_state[0] = is->is_state[0];
39792393Syz155240 ipsl.isl_state[1] = is->is_state[1];
39802393Syz155240 }
39812393Syz155240 } else if (ipsl.isl_p == IPPROTO_ICMP) {
39822393Syz155240 ipsl.isl_itype = is->is_icmp.ici_type;
39832393Syz155240 } else if (ipsl.isl_p == IPPROTO_ICMPV6) {
39842393Syz155240 ipsl.isl_itype = is->is_icmp.ici_type;
39852393Syz155240 } else {
39862393Syz155240 ipsl.isl_ps.isl_filler[0] = 0;
39872393Syz155240 ipsl.isl_ps.isl_filler[1] = 0;
39882393Syz155240 }
39892393Syz155240
39902393Syz155240 items[0] = &ipsl;
39912393Syz155240 sizes[0] = sizeof(ipsl);
39922393Syz155240 types[0] = 0;
39932393Syz155240
39943448Sdh155122 if (ipllog(IPL_LOGSTATE, NULL, items, sizes, types, 1, ifs)) {
39953448Sdh155122 ATOMIC_INCL(ifs->ifs_ips_stats.iss_logged);
39962393Syz155240 } else {
39973448Sdh155122 ATOMIC_INCL(ifs->ifs_ips_stats.iss_logfail);
39982393Syz155240 }
39992393Syz155240 #endif
40002393Syz155240 }
40012393Syz155240
40022393Syz155240
40032393Syz155240 #ifdef USE_INET6
40042393Syz155240 /* ------------------------------------------------------------------------ */
40052393Syz155240 /* Function: fr_checkicmp6matchingstate */
40062393Syz155240 /* Returns: ipstate_t* - NULL == no match found, */
40072393Syz155240 /* else pointer to matching state entry */
40082393Syz155240 /* Parameters: fin(I) - pointer to packet information */
40092393Syz155240 /* Locks: NULL == no locks, else Read Lock on ipf_state */
40102393Syz155240 /* */
40112393Syz155240 /* If we've got an ICMPv6 error message, using the information stored in */
40122393Syz155240 /* the ICMPv6 packet, look for a matching state table entry. */
40132393Syz155240 /* ------------------------------------------------------------------------ */
fr_checkicmp6matchingstate(fin)40142393Syz155240 static ipstate_t *fr_checkicmp6matchingstate(fin)
40152393Syz155240 fr_info_t *fin;
40162393Syz155240 {
40172393Syz155240 struct icmp6_hdr *ic6, *oic;
40182393Syz155240 int backward, i;
40192393Syz155240 ipstate_t *is, **isp;
40202393Syz155240 u_short sport, dport;
40212393Syz155240 i6addr_t dst, src;
40222393Syz155240 u_short savelen;
40232393Syz155240 icmpinfo_t *ic;
40242393Syz155240 fr_info_t ofin;
40252393Syz155240 tcphdr_t *tcp;
40262393Syz155240 ip6_t *oip6;
40272393Syz155240 u_char pr;
40282393Syz155240 u_int hv;
40293448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs;
40302393Syz155240
40312393Syz155240 /*
40322393Syz155240 * Does it at least have the return (basic) IP header ?
40332393Syz155240 * Is it an actual recognised ICMP error type?
40342393Syz155240 * Only a basic IP header (no options) should be with
40352393Syz155240 * an ICMP error header.
40362393Syz155240 */
40372393Syz155240 if ((fin->fin_v != 6) || (fin->fin_plen < ICMP6ERR_MINPKTLEN) ||
40382393Syz155240 !(fin->fin_flx & FI_ICMPERR))
40392393Syz155240 return NULL;
40402393Syz155240
40412393Syz155240 ic6 = fin->fin_dp;
40422393Syz155240
40432393Syz155240 oip6 = (ip6_t *)((char *)ic6 + ICMPERR_ICMPHLEN);
40442393Syz155240 if (fin->fin_plen < sizeof(*oip6))
40452393Syz155240 return NULL;
40462393Syz155240
40473607Szf203873 bcopy((char *)fin, (char *)&ofin, sizeof(*fin));
40482393Syz155240 ofin.fin_v = 6;
40492393Syz155240 ofin.fin_ifp = fin->fin_ifp;
40502393Syz155240 ofin.fin_out = !fin->fin_out;
40512393Syz155240 ofin.fin_m = NULL; /* if dereferenced, panic XXX */
40522393Syz155240 ofin.fin_mp = NULL; /* if dereferenced, panic XXX */
40532393Syz155240
40542393Syz155240 /*
40552393Syz155240 * We make a fin entry to be able to feed it to
40562393Syz155240 * matchsrcdst. Note that not all fields are necessary
40572393Syz155240 * but this is the cleanest way. Note further we fill
40582393Syz155240 * in fin_mp such that if someone uses it we'll get
40592393Syz155240 * a kernel panic. fr_matchsrcdst does not use this.
40602393Syz155240 *
40612393Syz155240 * watch out here, as ip is in host order and oip6 in network
40622393Syz155240 * order. Any change we make must be undone afterwards.
40632393Syz155240 */
40642393Syz155240 savelen = oip6->ip6_plen;
40652393Syz155240 oip6->ip6_plen = fin->fin_dlen - ICMPERR_ICMPHLEN;
40662393Syz155240 ofin.fin_flx = FI_NOCKSUM;
40672393Syz155240 ofin.fin_ip = (ip_t *)oip6;
40682393Syz155240 ofin.fin_plen = oip6->ip6_plen;
40692393Syz155240 (void) fr_makefrip(sizeof(*oip6), (ip_t *)oip6, &ofin);
40702393Syz155240 ofin.fin_flx &= ~(FI_BAD|FI_SHORT);
40712393Syz155240 oip6->ip6_plen = savelen;
40722393Syz155240
40732393Syz155240 if (oip6->ip6_nxt == IPPROTO_ICMPV6) {
40742393Syz155240 oic = (struct icmp6_hdr *)(oip6 + 1);
40752393Syz155240 /*
40762393Syz155240 * an ICMP error can only be generated as a result of an
40772393Syz155240 * ICMP query, not as the response on an ICMP error
40782393Syz155240 *
40792393Syz155240 * XXX theoretically ICMP_ECHOREP and the other reply's are
40802393Syz155240 * ICMP query's as well, but adding them here seems strange XXX
40812393Syz155240 */
40822393Syz155240 if (!(oic->icmp6_type & ICMP6_INFOMSG_MASK))
40832393Syz155240 return NULL;
40842393Syz155240
40852393Syz155240 /*
40862393Syz155240 * perform a lookup of the ICMP packet in the state table
40872393Syz155240 */
40882393Syz155240 hv = (pr = oip6->ip6_nxt);
40892393Syz155240 src.in6 = oip6->ip6_src;
40902393Syz155240 hv += src.in4.s_addr;
40912393Syz155240 dst.in6 = oip6->ip6_dst;
40922393Syz155240 hv += dst.in4.s_addr;
40932393Syz155240 hv += oic->icmp6_id;
40942393Syz155240 hv += oic->icmp6_seq;
40953448Sdh155122 hv = DOUBLE_HASH(hv, ifs);
40963448Sdh155122
40973448Sdh155122 READ_ENTER(&ifs->ifs_ipf_state);
40983448Sdh155122 for (isp = &ifs->ifs_ips_table[hv]; ((is = *isp) != NULL); ) {
40992393Syz155240 ic = &is->is_icmp;
41002393Syz155240 isp = &is->is_hnext;
41012393Syz155240 if ((is->is_p == pr) &&
41022393Syz155240 !(is->is_pass & FR_NOICMPERR) &&
41032393Syz155240 (oic->icmp6_id == ic->ici_id) &&
41042393Syz155240 (oic->icmp6_seq == ic->ici_seq) &&
41052393Syz155240 (is = fr_matchsrcdst(&ofin, is, &src,
41062393Syz155240 &dst, NULL, FI_ICMPCMP))) {
41072393Syz155240 /*
41082393Syz155240 * in the state table ICMP query's are stored
41092393Syz155240 * with the type of the corresponding ICMP
41102393Syz155240 * response. Correct here
41112393Syz155240 */
41122393Syz155240 if (((ic->ici_type == ICMP6_ECHO_REPLY) &&
41132393Syz155240 (oic->icmp6_type == ICMP6_ECHO_REQUEST)) ||
41142393Syz155240 (ic->ici_type - 1 == oic->icmp6_type )) {
41153448Sdh155122 ifs->ifs_ips_stats.iss_hits++;
41162393Syz155240 backward = IP6_NEQ(&is->is_dst, &src);
41172393Syz155240 fin->fin_rev = !backward;
41182393Syz155240 i = (backward << 1) + fin->fin_out;
41192393Syz155240 is->is_icmppkts[i]++;
41202393Syz155240 return is;
41212393Syz155240 }
41222393Syz155240 }
41232393Syz155240 }
41243448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
41252393Syz155240 return NULL;
41262393Syz155240 }
41272393Syz155240
41282393Syz155240 hv = (pr = oip6->ip6_nxt);
41292393Syz155240 src.in6 = oip6->ip6_src;
41302393Syz155240 hv += src.i6[0];
41312393Syz155240 hv += src.i6[1];
41322393Syz155240 hv += src.i6[2];
41332393Syz155240 hv += src.i6[3];
41342393Syz155240 dst.in6 = oip6->ip6_dst;
41352393Syz155240 hv += dst.i6[0];
41362393Syz155240 hv += dst.i6[1];
41372393Syz155240 hv += dst.i6[2];
41382393Syz155240 hv += dst.i6[3];
41392393Syz155240
41402393Syz155240 if ((oip6->ip6_nxt == IPPROTO_TCP) || (oip6->ip6_nxt == IPPROTO_UDP)) {
41412393Syz155240 tcp = (tcphdr_t *)(oip6 + 1);
41422393Syz155240 dport = tcp->th_dport;
41432393Syz155240 sport = tcp->th_sport;
41442393Syz155240 hv += dport;
41452393Syz155240 hv += sport;
41462393Syz155240 } else
41472393Syz155240 tcp = NULL;
41483448Sdh155122 hv = DOUBLE_HASH(hv, ifs);
41493448Sdh155122
41503448Sdh155122 READ_ENTER(&ifs->ifs_ipf_state);
41513448Sdh155122 for (isp = &ifs->ifs_ips_table[hv]; ((is = *isp) != NULL); ) {
41522393Syz155240 isp = &is->is_hnext;
41532393Syz155240 /*
41542393Syz155240 * Only allow this icmp though if the
41552393Syz155240 * encapsulated packet was allowed through the
41562393Syz155240 * other way around. Note that the minimal amount
41572393Syz155240 * of info present does not allow for checking against
41582393Syz155240 * tcp internals such as seq and ack numbers.
41592393Syz155240 */
41602393Syz155240 if ((is->is_p != pr) || (is->is_v != 6) ||
41612393Syz155240 (is->is_pass & FR_NOICMPERR))
41622393Syz155240 continue;
41632393Syz155240 is = fr_matchsrcdst(&ofin, is, &src, &dst, tcp, FI_ICMPCMP);
41642393Syz155240 if (is != NULL) {
41653448Sdh155122 ifs->ifs_ips_stats.iss_hits++;
41662393Syz155240 backward = IP6_NEQ(&is->is_dst, &src);
41672393Syz155240 fin->fin_rev = !backward;
41682393Syz155240 i = (backward << 1) + fin->fin_out;
41692393Syz155240 is->is_icmppkts[i]++;
41702393Syz155240 /*
41712393Syz155240 * we deliberately do not touch the timeouts
41722393Syz155240 * for the accompanying state table entry.
41732393Syz155240 * It remains to be seen if that is correct. XXX
41742393Syz155240 */
41752393Syz155240 return is;
41762393Syz155240 }
41772393Syz155240 }
41783448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
41792393Syz155240 return NULL;
41802393Syz155240 }
41812393Syz155240 #endif
41822393Syz155240
41832393Syz155240
41842393Syz155240 /* ------------------------------------------------------------------------ */
41852393Syz155240 /* Function: fr_sttab_init */
41862393Syz155240 /* Returns: Nil */
41872393Syz155240 /* Parameters: tqp(I) - pointer to an array of timeout queues for TCP */
41882393Syz155240 /* */
41892393Syz155240 /* Initialise the array of timeout queues for TCP. */
41902393Syz155240 /* ------------------------------------------------------------------------ */
fr_sttab_init(tqp,ifs)41913448Sdh155122 void fr_sttab_init(tqp, ifs)
41922393Syz155240 ipftq_t *tqp;
41933448Sdh155122 ipf_stack_t *ifs;
41942393Syz155240 {
41952393Syz155240 int i;
41962393Syz155240
41972393Syz155240 for (i = IPF_TCP_NSTATES - 1; i >= 0; i--) {
41982393Syz155240 tqp[i].ifq_ttl = 0;
41992393Syz155240 tqp[i].ifq_ref = 1;
42002393Syz155240 tqp[i].ifq_head = NULL;
42012393Syz155240 tqp[i].ifq_tail = &tqp[i].ifq_head;
42022393Syz155240 tqp[i].ifq_next = tqp + i + 1;
42032393Syz155240 MUTEX_INIT(&tqp[i].ifq_lock, "ipftq tcp tab");
42042393Syz155240 }
42052393Syz155240 tqp[IPF_TCP_NSTATES - 1].ifq_next = NULL;
42063448Sdh155122 tqp[IPF_TCPS_CLOSED].ifq_ttl = ifs->ifs_fr_tcpclosed;
42073448Sdh155122 tqp[IPF_TCPS_LISTEN].ifq_ttl = ifs->ifs_fr_tcptimeout;
42083448Sdh155122 tqp[IPF_TCPS_SYN_SENT].ifq_ttl = ifs->ifs_fr_tcptimeout;
42093448Sdh155122 tqp[IPF_TCPS_SYN_RECEIVED].ifq_ttl = ifs->ifs_fr_tcptimeout;
42103448Sdh155122 tqp[IPF_TCPS_ESTABLISHED].ifq_ttl = ifs->ifs_fr_tcpidletimeout;
42113448Sdh155122 tqp[IPF_TCPS_CLOSE_WAIT].ifq_ttl = ifs->ifs_fr_tcphalfclosed;
42123448Sdh155122 tqp[IPF_TCPS_FIN_WAIT_1].ifq_ttl = ifs->ifs_fr_tcphalfclosed;
42133448Sdh155122 tqp[IPF_TCPS_CLOSING].ifq_ttl = ifs->ifs_fr_tcptimeout;
42143448Sdh155122 tqp[IPF_TCPS_LAST_ACK].ifq_ttl = ifs->ifs_fr_tcplastack;
42153448Sdh155122 tqp[IPF_TCPS_FIN_WAIT_2].ifq_ttl = ifs->ifs_fr_tcpclosewait;
42163448Sdh155122 tqp[IPF_TCPS_TIME_WAIT].ifq_ttl = ifs->ifs_fr_tcptimeout;
42173448Sdh155122 tqp[IPF_TCPS_HALF_ESTAB].ifq_ttl = ifs->ifs_fr_tcptimeout;
42182393Syz155240 }
42192393Syz155240
42202393Syz155240
42212393Syz155240 /* ------------------------------------------------------------------------ */
42222393Syz155240 /* Function: fr_sttab_destroy */
42232393Syz155240 /* Returns: Nil */
42242393Syz155240 /* Parameters: tqp(I) - pointer to an array of timeout queues for TCP */
42252393Syz155240 /* */
42262393Syz155240 /* Do whatever is necessary to "destroy" each of the entries in the array */
42272393Syz155240 /* of timeout queues for TCP. */
42282393Syz155240 /* ------------------------------------------------------------------------ */
fr_sttab_destroy(tqp)42292393Syz155240 void fr_sttab_destroy(tqp)
42302393Syz155240 ipftq_t *tqp;
42312393Syz155240 {
42322393Syz155240 int i;
42332393Syz155240
42342393Syz155240 for (i = IPF_TCP_NSTATES - 1; i >= 0; i--)
42352393Syz155240 MUTEX_DESTROY(&tqp[i].ifq_lock);
42362393Syz155240 }
42372393Syz155240
42382393Syz155240
42392393Syz155240 /* ------------------------------------------------------------------------ */
42402393Syz155240 /* Function: fr_statederef */
42412393Syz155240 /* Returns: Nil */
42422393Syz155240 /* Parameters: isp(I) - pointer to pointer to state table entry */
42435417Sjojemann /* ifs - ipf stack instance */
42442393Syz155240 /* */
42452393Syz155240 /* Decrement the reference counter for this state table entry and free it */
42462393Syz155240 /* if there are no more things using it. */
42472393Syz155240 /* */
42482393Syz155240 /* Internal parameters: */
42492393Syz155240 /* state[0] = state of source (host that initiated connection) */
42502393Syz155240 /* state[1] = state of dest (host that accepted the connection) */
42512393Syz155240 /* ------------------------------------------------------------------------ */
fr_statederef(isp,ifs)42525417Sjojemann void fr_statederef(isp, ifs)
42532393Syz155240 ipstate_t **isp;
42543448Sdh155122 ipf_stack_t *ifs;
42552393Syz155240 {
42565417Sjojemann ipstate_t *is;
42575417Sjojemann
42582393Syz155240 is = *isp;
42592393Syz155240 *isp = NULL;
42605055Sdr146992
42615055Sdr146992 MUTEX_ENTER(&is->is_lock);
42625055Sdr146992 if (is->is_ref > 1) {
42635055Sdr146992 is->is_ref--;
42645055Sdr146992 MUTEX_EXIT(&is->is_lock);
42652393Syz155240 #ifndef _KERNEL
42665055Sdr146992 if ((is->is_sti.tqe_state[0] > IPF_TCPS_ESTABLISHED) ||
42672393Syz155240 (is->is_sti.tqe_state[1] > IPF_TCPS_ESTABLISHED)) {
42688170SJohn.Ojemann@Sun.COM (void) fr_delstate(is, ISL_ORPHAN, ifs);
42695055Sdr146992 }
42702393Syz155240 #endif
42715055Sdr146992 return;
42722393Syz155240 }
42735055Sdr146992 MUTEX_EXIT(&is->is_lock);
42745055Sdr146992
42755055Sdr146992 WRITE_ENTER(&ifs->ifs_ipf_state);
42768170SJohn.Ojemann@Sun.COM (void) fr_delstate(is, ISL_EXPIRE, ifs);
42773448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_state);
42782393Syz155240 }
42792393Syz155240
42802393Syz155240
42812393Syz155240 /* ------------------------------------------------------------------------ */
42822393Syz155240 /* Function: fr_setstatequeue */
42832393Syz155240 /* Returns: Nil */
42842393Syz155240 /* Parameters: is(I) - pointer to state structure */
42852393Syz155240 /* rev(I) - forward(0) or reverse(1) direction */
42862393Syz155240 /* Locks: ipf_state (read or write) */
42872393Syz155240 /* */
42882393Syz155240 /* Put the state entry on its default queue entry, using rev as a helped in */
42892393Syz155240 /* determining which queue it should be placed on. */
42902393Syz155240 /* ------------------------------------------------------------------------ */
fr_setstatequeue(is,rev,ifs)42913448Sdh155122 void fr_setstatequeue(is, rev, ifs)
42922393Syz155240 ipstate_t *is;
42932393Syz155240 int rev;
42943448Sdh155122 ipf_stack_t *ifs;
42952393Syz155240 {
42962393Syz155240 ipftq_t *oifq, *nifq;
42972393Syz155240
42982393Syz155240
42992393Syz155240 if ((is->is_sti.tqe_flags & TQE_RULEBASED) != 0)
43002393Syz155240 nifq = is->is_tqehead[rev];
43012393Syz155240 else
43022393Syz155240 nifq = NULL;
43032393Syz155240
43042393Syz155240 if (nifq == NULL) {
43052393Syz155240 switch (is->is_p)
43062393Syz155240 {
43072393Syz155240 #ifdef USE_INET6
43082393Syz155240 case IPPROTO_ICMPV6 :
43092393Syz155240 if (rev == 1)
43103448Sdh155122 nifq = &ifs->ifs_ips_icmpacktq;
43112393Syz155240 else
43123448Sdh155122 nifq = &ifs->ifs_ips_icmptq;
43132393Syz155240 break;
43142393Syz155240 #endif
43152393Syz155240 case IPPROTO_ICMP :
43162393Syz155240 if (rev == 1)
43173448Sdh155122 nifq = &ifs->ifs_ips_icmpacktq;
43182393Syz155240 else
43193448Sdh155122 nifq = &ifs->ifs_ips_icmptq;
43202393Syz155240 break;
43212393Syz155240 case IPPROTO_TCP :
43223448Sdh155122 nifq = ifs->ifs_ips_tqtqb + is->is_state[rev];
43232393Syz155240 break;
43242393Syz155240
43252393Syz155240 case IPPROTO_UDP :
43262393Syz155240 if (rev == 1)
43273448Sdh155122 nifq = &ifs->ifs_ips_udpacktq;
43282393Syz155240 else
43293448Sdh155122 nifq = &ifs->ifs_ips_udptq;
43302393Syz155240 break;
43312393Syz155240
43322393Syz155240 default :
43333448Sdh155122 nifq = &ifs->ifs_ips_iptq;
43342393Syz155240 break;
43352393Syz155240 }
43362393Syz155240 }
43372393Syz155240
43382393Syz155240 oifq = is->is_sti.tqe_ifq;
43392393Syz155240 /*
43402393Syz155240 * If it's currently on a timeout queue, move it from one queue to
43412393Syz155240 * another, else put it on the end of the newly determined queue.
43422393Syz155240 */
43432393Syz155240 if (oifq != NULL)
43443448Sdh155122 fr_movequeue(&is->is_sti, oifq, nifq, ifs);
43452393Syz155240 else
43463448Sdh155122 fr_queueappend(&is->is_sti, nifq, is, ifs);
43472393Syz155240 return;
43482393Syz155240 }
43493448Sdh155122
43503448Sdh155122
43513448Sdh155122 /* ------------------------------------------------------------------------ */
43523448Sdh155122 /* Function: fr_stateiter */
43533448Sdh155122 /* Returns: int - 0 == success, else error */
43543448Sdh155122 /* Parameters: token(I) - pointer to ipftoken structure */
43553448Sdh155122 /* itp(I) - pointer to ipfgeniter structure */
43563448Sdh155122 /* */
43573448Sdh155122 /* This function handles the SIOCGENITER ioctl for the state tables and */
43583448Sdh155122 /* walks through the list of entries in the state table list (ips_list.) */
43593448Sdh155122 /* ------------------------------------------------------------------------ */
fr_stateiter(token,itp,ifs)43603448Sdh155122 static int fr_stateiter(token, itp, ifs)
43613448Sdh155122 ipftoken_t *token;
43623448Sdh155122 ipfgeniter_t *itp;
43633448Sdh155122 ipf_stack_t *ifs;
43643448Sdh155122 {
43653448Sdh155122 ipstate_t *is, *next, zero;
43665417Sjojemann int error, count;
43675417Sjojemann char *dst;
43683448Sdh155122
43693448Sdh155122 if (itp->igi_data == NULL)
43703448Sdh155122 return EFAULT;
43713448Sdh155122
43725417Sjojemann if (itp->igi_nitems == 0)
43735417Sjojemann return EINVAL;
43745417Sjojemann
43753448Sdh155122 if (itp->igi_type != IPFGENITER_STATE)
43763448Sdh155122 return EINVAL;
43773448Sdh155122
43785417Sjojemann error = 0;
43795417Sjojemann
43803448Sdh155122 READ_ENTER(&ifs->ifs_ipf_state);
43816518Sjojemann
43826518Sjojemann /*
43836518Sjojemann * Get "previous" entry from the token and find the next entry.
43846518Sjojemann */
43856518Sjojemann is = token->ipt_data;
43863448Sdh155122 if (is == NULL) {
43873448Sdh155122 next = ifs->ifs_ips_list;
43883448Sdh155122 } else {
43893448Sdh155122 next = is->is_next;
43903448Sdh155122 }
43913448Sdh155122
43926518Sjojemann dst = itp->igi_data;
43935417Sjojemann for (count = itp->igi_nitems; count > 0; count--) {
43946518Sjojemann /*
43956518Sjojemann * If we found an entry, add a reference to it and update the token.
43966518Sjojemann * Otherwise, zero out data to be returned and NULL out token.
43976518Sjojemann */
43985417Sjojemann if (next != NULL) {
43995417Sjojemann MUTEX_ENTER(&next->is_lock);
44005417Sjojemann next->is_ref++;
44015417Sjojemann MUTEX_EXIT(&next->is_lock);
44025417Sjojemann token->ipt_data = next;
44035417Sjojemann } else {
44045417Sjojemann bzero(&zero, sizeof(zero));
44055417Sjojemann next = &zero;
44066518Sjojemann token->ipt_data = NULL;
44075417Sjojemann }
44086518Sjojemann
44096518Sjojemann /*
44106518Sjojemann * Safe to release lock now the we have a reference.
44116518Sjojemann */
44125417Sjojemann RWLOCK_EXIT(&ifs->ifs_ipf_state);
44135417Sjojemann
44143448Sdh155122 /*
44156518Sjojemann * Copy out data and clean up references and tokens.
44165417Sjojemann */
44175417Sjojemann error = COPYOUT(next, dst, sizeof(*next));
44185417Sjojemann if (error != 0)
44195417Sjojemann error = EFAULT;
44206518Sjojemann if (token->ipt_data == NULL) {
44216518Sjojemann ipf_freetoken(token, ifs);
44226518Sjojemann break;
44236518Sjojemann } else {
44246518Sjojemann if (is != NULL)
44256518Sjojemann fr_statederef(&is, ifs);
44266518Sjojemann if (next->is_next == NULL) {
44276518Sjojemann ipf_freetoken(token, ifs);
44286518Sjojemann break;
44296518Sjojemann }
44306518Sjojemann }
44316518Sjojemann
44325417Sjojemann if ((count == 1) || (error != 0))
44335417Sjojemann break;
44345417Sjojemann
44356518Sjojemann READ_ENTER(&ifs->ifs_ipf_state);
44365417Sjojemann dst += sizeof(*next);
44375417Sjojemann is = next;
44385417Sjojemann next = is->is_next;
44393448Sdh155122 }
44403448Sdh155122
44413448Sdh155122 return error;
44423448Sdh155122 }
4443