| /* |
| * Copyright (C) 1995-2003 by Darren Reed. |
| * |
| * See the IPFILTER.LICENCE file for details on licencing. |
| * |
| * Copyright 2007 Sun Microsystems, Inc. All rights reserved. |
| * Use is subject to license terms. |
| */ |
| |
| #pragma ident "%Z%%M% %I% %E% SMI" |
| |
| #if defined(KERNEL) || defined(_KERNEL) |
| # undef KERNEL |
| # undef _KERNEL |
| # define KERNEL 1 |
| # define _KERNEL 1 |
| #endif |
| #include <sys/errno.h> |
| #include <sys/types.h> |
| #include <sys/param.h> |
| #include <sys/file.h> |
| #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \ |
| defined(_KERNEL) |
| # include "opt_ipfilter_log.h" |
| #endif |
| #if defined(_KERNEL) && defined(__FreeBSD_version) && \ |
| (__FreeBSD_version >= 400000) && !defined(KLD_MODULE) |
| #include "opt_inet6.h" |
| #endif |
| #if !defined(_KERNEL) && !defined(__KERNEL__) |
| # include <stdio.h> |
| # include <stdlib.h> |
| # include <string.h> |
| # define _KERNEL |
| # ifdef __OpenBSD__ |
| struct file; |
| # endif |
| # include <sys/uio.h> |
| # undef _KERNEL |
| #endif |
| #if defined(_KERNEL) && (__FreeBSD_version >= 220000) |
| # include <sys/filio.h> |
| # include <sys/fcntl.h> |
| # if (__FreeBSD_version >= 300000) && !defined(IPFILTER_LKM) |
| # include "opt_ipfilter.h" |
| # endif |
| #else |
| # include <sys/ioctl.h> |
| #endif |
| #include <sys/time.h> |
| #if !defined(linux) |
| # include <sys/protosw.h> |
| #endif |
| #include <sys/socket.h> |
| #if defined(_KERNEL) |
| # include <sys/systm.h> |
| # if !defined(__SVR4) && !defined(__svr4__) |
| # include <sys/mbuf.h> |
| # endif |
| #endif |
| #if defined(__SVR4) || defined(__svr4__) |
| # include <sys/filio.h> |
| # include <sys/byteorder.h> |
| # ifdef _KERNEL |
| # include <sys/dditypes.h> |
| # endif |
| # include <sys/stream.h> |
| # include <sys/kmem.h> |
| #endif |
| |
| #include <net/if.h> |
| #ifdef sun |
| # include <net/af.h> |
| #endif |
| #include <net/route.h> |
| #include <netinet/in.h> |
| #include <netinet/in_systm.h> |
| #include <netinet/ip.h> |
| #include <netinet/tcp.h> |
| #if !defined(linux) |
| # include <netinet/ip_var.h> |
| #endif |
| #if !defined(__hpux) && !defined(linux) |
| # include <netinet/tcp_fsm.h> |
| #endif |
| #include <netinet/udp.h> |
| #include <netinet/ip_icmp.h> |
| #include "netinet/ip_compat.h" |
| #include <netinet/tcpip.h> |
| #include "netinet/ip_fil.h" |
| #include "netinet/ip_nat.h" |
| #include "netinet/ip_frag.h" |
| #include "netinet/ip_state.h" |
| #include "netinet/ip_proxy.h" |
| #include "netinet/ipf_stack.h" |
| #ifdef IPFILTER_SYNC |
| #include "netinet/ip_sync.h" |
| #endif |
| #ifdef IPFILTER_SCAN |
| #include "netinet/ip_scan.h" |
| #endif |
| #ifdef USE_INET6 |
| #include <netinet/icmp6.h> |
| #endif |
| #if (__FreeBSD_version >= 300000) |
| # include <sys/malloc.h> |
| # if defined(_KERNEL) && !defined(IPFILTER_LKM) |
| # include <sys/libkern.h> |
| # include <sys/systm.h> |
| # endif |
| #endif |
| /* END OF INCLUDES */ |
| |
| |
| #if !defined(lint) |
| static const char sccsid[] = "@(#)ip_state.c 1.8 6/5/96 (C) 1993-2000 Darren Reed"; |
| static const char rcsid[] = "@(#)$Id: ip_state.c,v 2.186.2.36 2005/08/11 19:58:03 darrenr Exp $"; |
| #endif |
| |
| #ifdef USE_INET6 |
| static ipstate_t *fr_checkicmp6matchingstate __P((fr_info_t *)); |
| #endif |
| static ipstate_t *fr_matchsrcdst __P((fr_info_t *, ipstate_t *, i6addr_t *, |
| i6addr_t *, tcphdr_t *, u_32_t)); |
| static ipstate_t *fr_checkicmpmatchingstate __P((fr_info_t *)); |
| static int fr_state_flush __P((int, int, ipf_stack_t *)); |
| static ips_stat_t *fr_statetstats __P((ipf_stack_t *)); |
| static void fr_delstate __P((ipstate_t *, int, ipf_stack_t *)); |
| static int fr_state_remove __P((caddr_t, ipf_stack_t *)); |
| static void fr_ipsmove __P((ipstate_t *, u_int, ipf_stack_t *)); |
| static int fr_tcpstate __P((fr_info_t *, tcphdr_t *, ipstate_t *)); |
| static int fr_tcpoptions __P((fr_info_t *, tcphdr_t *, tcpdata_t *)); |
| static ipstate_t *fr_stclone __P((fr_info_t *, tcphdr_t *, ipstate_t *)); |
| static void fr_fixinisn __P((fr_info_t *, ipstate_t *)); |
| static void fr_fixoutisn __P((fr_info_t *, ipstate_t *)); |
| static void fr_checknewisn __P((fr_info_t *, ipstate_t *)); |
| static int fr_stateiter __P((ipftoken_t *, ipfgeniter_t *, ipf_stack_t *)); |
| |
| int fr_stputent __P((caddr_t, ipf_stack_t *)); |
| int fr_stgetent __P((caddr_t, ipf_stack_t *)); |
| |
| #define ONE_DAY IPF_TTLVAL(1 * 86400) /* 1 day */ |
| #define FIVE_DAYS (5 * ONE_DAY) |
| #define DOUBLE_HASH(x, ifs) \ |
| (((x) + ifs->ifs_ips_seed[(x) % ifs->ifs_fr_statesize]) % ifs->ifs_fr_statesize) |
| |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_stateinit */ |
| /* Returns: int - 0 == success, -1 == failure */ |
| /* Parameters: Nil */ |
| /* */ |
| /* Initialise all the global variables used within the state code. */ |
| /* This action also includes initiailising locks. */ |
| /* ------------------------------------------------------------------------ */ |
| int fr_stateinit(ifs) |
| ipf_stack_t *ifs; |
| { |
| int i; |
| |
| ifs->ifs_fr_tcpidletimeout = FIVE_DAYS; |
| ifs->ifs_fr_tcpclosewait = IPF_TTLVAL(TCP_MSL); |
| ifs->ifs_fr_tcplastack = IPF_TTLVAL(TCP_MSL); |
| ifs->ifs_fr_tcptimeout = IPF_TTLVAL(TCP_MSL); |
| ifs->ifs_fr_tcpclosed = IPF_TTLVAL(60); |
| ifs->ifs_fr_tcphalfclosed = IPF_TTLVAL(2 * 3600); /* 2 hours */ |
| ifs->ifs_fr_udptimeout = IPF_TTLVAL(120); |
| ifs->ifs_fr_udpacktimeout = IPF_TTLVAL(12); |
| ifs->ifs_fr_icmptimeout = IPF_TTLVAL(60); |
| ifs->ifs_fr_icmpacktimeout = IPF_TTLVAL(6); |
| ifs->ifs_fr_iptimeout = IPF_TTLVAL(60); |
| ifs->ifs_fr_statemax = IPSTATE_MAX; |
| ifs->ifs_fr_statesize = IPSTATE_SIZE; |
| ifs->ifs_fr_state_maxbucket_reset = 1; |
| #ifdef IPFILTER_LOG |
| ifs->ifs_ipstate_logging = 1; |
| #else |
| ifs->ifs_ipstate_logging = 0; |
| #endif |
| |
| KMALLOCS(ifs->ifs_ips_table, ipstate_t **, |
| ifs->ifs_fr_statesize * sizeof(ipstate_t *)); |
| if (ifs->ifs_ips_table == NULL) |
| return -1; |
| bzero((char *)ifs->ifs_ips_table, |
| ifs->ifs_fr_statesize * sizeof(ipstate_t *)); |
| |
| KMALLOCS(ifs->ifs_ips_seed, u_long *, |
| ifs->ifs_fr_statesize * sizeof(*ifs->ifs_ips_seed)); |
| if (ifs->ifs_ips_seed == NULL) |
| return -2; |
| for (i = 0; i < ifs->ifs_fr_statesize; i++) { |
| /* |
| * XXX - ips_seed[X] should be a random number of sorts. |
| */ |
| #if (__FreeBSD_version >= 400000) |
| ifs->ifs_ips_seed[i] = arc4random(); |
| #else |
| ifs->ifs_ips_seed[i] = ((u_long)ifs->ifs_ips_seed + i) * |
| ifs->ifs_fr_statesize; |
| ifs->ifs_ips_seed[i] ^= 0xa5a55a5a; |
| ifs->ifs_ips_seed[i] *= (u_long)ifs->ifs_ips_seed; |
| ifs->ifs_ips_seed[i] ^= 0x5a5aa5a5; |
| ifs->ifs_ips_seed[i] *= ifs->ifs_fr_statemax; |
| #endif |
| } |
| |
| /* fill icmp reply type table */ |
| for (i = 0; i <= ICMP_MAXTYPE; i++) |
| icmpreplytype4[i] = -1; |
| icmpreplytype4[ICMP_ECHO] = ICMP_ECHOREPLY; |
| icmpreplytype4[ICMP_TSTAMP] = ICMP_TSTAMPREPLY; |
| icmpreplytype4[ICMP_IREQ] = ICMP_IREQREPLY; |
| icmpreplytype4[ICMP_MASKREQ] = ICMP_MASKREPLY; |
| #ifdef USE_INET6 |
| /* fill icmp reply type table */ |
| for (i = 0; i <= ICMP6_MAXTYPE; i++) |
| icmpreplytype6[i] = -1; |
| icmpreplytype6[ICMP6_ECHO_REQUEST] = ICMP6_ECHO_REPLY; |
| icmpreplytype6[ICMP6_MEMBERSHIP_QUERY] = ICMP6_MEMBERSHIP_REPORT; |
| icmpreplytype6[ICMP6_NI_QUERY] = ICMP6_NI_REPLY; |
| icmpreplytype6[ND_ROUTER_SOLICIT] = ND_ROUTER_ADVERT; |
| icmpreplytype6[ND_NEIGHBOR_SOLICIT] = ND_NEIGHBOR_ADVERT; |
| #endif |
| |
| KMALLOCS(ifs->ifs_ips_stats.iss_bucketlen, u_long *, |
| ifs->ifs_fr_statesize * sizeof(u_long)); |
| if (ifs->ifs_ips_stats.iss_bucketlen == NULL) |
| return -1; |
| bzero((char *)ifs->ifs_ips_stats.iss_bucketlen, |
| ifs->ifs_fr_statesize * sizeof(u_long)); |
| |
| if (ifs->ifs_fr_state_maxbucket == 0) { |
| for (i = ifs->ifs_fr_statesize; i > 0; i >>= 1) |
| ifs->ifs_fr_state_maxbucket++; |
| ifs->ifs_fr_state_maxbucket *= 2; |
| } |
| |
| fr_sttab_init(ifs->ifs_ips_tqtqb, ifs); |
| ifs->ifs_ips_tqtqb[IPF_TCP_NSTATES - 1].ifq_next = &ifs->ifs_ips_udptq; |
| ifs->ifs_ips_udptq.ifq_ttl = (u_long)ifs->ifs_fr_udptimeout; |
| ifs->ifs_ips_udptq.ifq_ref = 1; |
| ifs->ifs_ips_udptq.ifq_head = NULL; |
| ifs->ifs_ips_udptq.ifq_tail = &ifs->ifs_ips_udptq.ifq_head; |
| MUTEX_INIT(&ifs->ifs_ips_udptq.ifq_lock, "ipftq udp tab"); |
| ifs->ifs_ips_udptq.ifq_next = &ifs->ifs_ips_udpacktq; |
| ifs->ifs_ips_udpacktq.ifq_ttl = (u_long)ifs->ifs_fr_udpacktimeout; |
| ifs->ifs_ips_udpacktq.ifq_ref = 1; |
| ifs->ifs_ips_udpacktq.ifq_head = NULL; |
| ifs->ifs_ips_udpacktq.ifq_tail = &ifs->ifs_ips_udpacktq.ifq_head; |
| MUTEX_INIT(&ifs->ifs_ips_udpacktq.ifq_lock, "ipftq udpack tab"); |
| ifs->ifs_ips_udpacktq.ifq_next = &ifs->ifs_ips_icmptq; |
| ifs->ifs_ips_icmptq.ifq_ttl = (u_long)ifs->ifs_fr_icmptimeout; |
| ifs->ifs_ips_icmptq.ifq_ref = 1; |
| ifs->ifs_ips_icmptq.ifq_head = NULL; |
| ifs->ifs_ips_icmptq.ifq_tail = &ifs->ifs_ips_icmptq.ifq_head; |
| MUTEX_INIT(&ifs->ifs_ips_icmptq.ifq_lock, "ipftq icmp tab"); |
| ifs->ifs_ips_icmptq.ifq_next = &ifs->ifs_ips_icmpacktq; |
| ifs->ifs_ips_icmpacktq.ifq_ttl = (u_long)ifs->ifs_fr_icmpacktimeout; |
| ifs->ifs_ips_icmpacktq.ifq_ref = 1; |
| ifs->ifs_ips_icmpacktq.ifq_head = NULL; |
| ifs->ifs_ips_icmpacktq.ifq_tail = &ifs->ifs_ips_icmpacktq.ifq_head; |
| MUTEX_INIT(&ifs->ifs_ips_icmpacktq.ifq_lock, "ipftq icmpack tab"); |
| ifs->ifs_ips_icmpacktq.ifq_next = &ifs->ifs_ips_iptq; |
| ifs->ifs_ips_iptq.ifq_ttl = (u_long)ifs->ifs_fr_iptimeout; |
| ifs->ifs_ips_iptq.ifq_ref = 1; |
| ifs->ifs_ips_iptq.ifq_head = NULL; |
| ifs->ifs_ips_iptq.ifq_tail = &ifs->ifs_ips_iptq.ifq_head; |
| MUTEX_INIT(&ifs->ifs_ips_iptq.ifq_lock, "ipftq ip tab"); |
| ifs->ifs_ips_iptq.ifq_next = NULL; |
| |
| RWLOCK_INIT(&ifs->ifs_ipf_state, "ipf IP state rwlock"); |
| MUTEX_INIT(&ifs->ifs_ipf_stinsert, "ipf state insert mutex"); |
| ifs->ifs_fr_state_init = 1; |
| |
| ifs->ifs_ips_last_force_flush = ifs->ifs_fr_ticks; |
| return 0; |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_stateunload */ |
| /* Returns: Nil */ |
| /* Parameters: Nil */ |
| /* */ |
| /* Release and destroy any resources acquired or initialised so that */ |
| /* IPFilter can be unloaded or re-initialised. */ |
| /* ------------------------------------------------------------------------ */ |
| void fr_stateunload(ifs) |
| ipf_stack_t *ifs; |
| { |
| ipftq_t *ifq, *ifqnext; |
| ipstate_t *is; |
| |
| while ((is = ifs->ifs_ips_list) != NULL) |
| fr_delstate(is, 0, ifs); |
| |
| /* |
| * Proxy timeout queues are not cleaned here because although they |
| * exist on the state list, appr_unload is called after fr_stateunload |
| * and the proxies actually are responsible for them being created. |
| * Should the proxy timeouts have their own list? There's no real |
| * justification as this is the only complicationA |
| */ |
| for (ifq = ifs->ifs_ips_utqe; ifq != NULL; ifq = ifqnext) { |
| ifqnext = ifq->ifq_next; |
| if (((ifq->ifq_flags & IFQF_PROXY) == 0) && |
| (fr_deletetimeoutqueue(ifq) == 0)) |
| fr_freetimeoutqueue(ifq, ifs); |
| } |
| |
| ifs->ifs_ips_stats.iss_inuse = 0; |
| ifs->ifs_ips_num = 0; |
| |
| if (ifs->ifs_fr_state_init == 1) { |
| fr_sttab_destroy(ifs->ifs_ips_tqtqb); |
| MUTEX_DESTROY(&ifs->ifs_ips_udptq.ifq_lock); |
| MUTEX_DESTROY(&ifs->ifs_ips_icmptq.ifq_lock); |
| MUTEX_DESTROY(&ifs->ifs_ips_udpacktq.ifq_lock); |
| MUTEX_DESTROY(&ifs->ifs_ips_icmpacktq.ifq_lock); |
| MUTEX_DESTROY(&ifs->ifs_ips_iptq.ifq_lock); |
| } |
| |
| if (ifs->ifs_ips_table != NULL) { |
| KFREES(ifs->ifs_ips_table, |
| ifs->ifs_fr_statesize * sizeof(*ifs->ifs_ips_table)); |
| ifs->ifs_ips_table = NULL; |
| } |
| |
| if (ifs->ifs_ips_seed != NULL) { |
| KFREES(ifs->ifs_ips_seed, |
| ifs->ifs_fr_statesize * sizeof(*ifs->ifs_ips_seed)); |
| ifs->ifs_ips_seed = NULL; |
| } |
| |
| if (ifs->ifs_ips_stats.iss_bucketlen != NULL) { |
| KFREES(ifs->ifs_ips_stats.iss_bucketlen, |
| ifs->ifs_fr_statesize * sizeof(u_long)); |
| ifs->ifs_ips_stats.iss_bucketlen = NULL; |
| } |
| |
| if (ifs->ifs_fr_state_maxbucket_reset == 1) |
| ifs->ifs_fr_state_maxbucket = 0; |
| |
| if (ifs->ifs_fr_state_init == 1) { |
| ifs->ifs_fr_state_init = 0; |
| RW_DESTROY(&ifs->ifs_ipf_state); |
| MUTEX_DESTROY(&ifs->ifs_ipf_stinsert); |
| } |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_statetstats */ |
| /* Returns: ips_state_t* - pointer to state stats structure */ |
| /* Parameters: Nil */ |
| /* */ |
| /* Put all the current numbers and pointers into a single struct and return */ |
| /* a pointer to it. */ |
| /* ------------------------------------------------------------------------ */ |
| static ips_stat_t *fr_statetstats(ifs) |
| ipf_stack_t *ifs; |
| { |
| ifs->ifs_ips_stats.iss_active = ifs->ifs_ips_num; |
| ifs->ifs_ips_stats.iss_statesize = ifs->ifs_fr_statesize; |
| ifs->ifs_ips_stats.iss_statemax = ifs->ifs_fr_statemax; |
| ifs->ifs_ips_stats.iss_table = ifs->ifs_ips_table; |
| ifs->ifs_ips_stats.iss_list = ifs->ifs_ips_list; |
| ifs->ifs_ips_stats.iss_ticks = ifs->ifs_fr_ticks; |
| return &ifs->ifs_ips_stats; |
| } |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_state_remove */ |
| /* Returns: int - 0 == success, != 0 == failure */ |
| /* Parameters: data(I) - pointer to state structure to delete from table */ |
| /* */ |
| /* Search for a state structure that matches the one passed, according to */ |
| /* the IP addresses and other protocol specific information. */ |
| /* ------------------------------------------------------------------------ */ |
| static int fr_state_remove(data, ifs) |
| caddr_t data; |
| ipf_stack_t *ifs; |
| { |
| ipstate_t *sp, st; |
| int error; |
| |
| sp = &st; |
| error = fr_inobj(data, &st, IPFOBJ_IPSTATE); |
| if (error) |
| return EFAULT; |
| |
| WRITE_ENTER(&ifs->ifs_ipf_state); |
| for (sp = ifs->ifs_ips_list; sp; sp = sp->is_next) |
| if ((sp->is_p == st.is_p) && (sp->is_v == st.is_v) && |
| !bcmp((caddr_t)&sp->is_src, (caddr_t)&st.is_src, |
| sizeof(st.is_src)) && |
| !bcmp((caddr_t)&sp->is_dst, (caddr_t)&st.is_src, |
| sizeof(st.is_dst)) && |
| !bcmp((caddr_t)&sp->is_ps, (caddr_t)&st.is_ps, |
| sizeof(st.is_ps))) { |
| fr_delstate(sp, ISL_REMOVE, ifs); |
| RWLOCK_EXIT(&ifs->ifs_ipf_state); |
| return 0; |
| } |
| RWLOCK_EXIT(&ifs->ifs_ipf_state); |
| return ESRCH; |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_state_ioctl */ |
| /* Returns: int - 0 == success, != 0 == failure */ |
| /* Parameters: data(I) - pointer to ioctl data */ |
| /* cmd(I) - ioctl command integer */ |
| /* mode(I) - file mode bits used with open */ |
| /* */ |
| /* Processes an ioctl call made to operate on the IP Filter state device. */ |
| /* ------------------------------------------------------------------------ */ |
| int fr_state_ioctl(data, cmd, mode, uid, ctx, ifs) |
| caddr_t data; |
| ioctlcmd_t cmd; |
| int mode, uid; |
| void *ctx; |
| ipf_stack_t *ifs; |
| { |
| int arg, ret, error = 0; |
| |
| switch (cmd) |
| { |
| /* |
| * Delete an entry from the state table. |
| */ |
| case SIOCDELST : |
| error = fr_state_remove(data, ifs); |
| break; |
| /* |
| * Flush the state table |
| */ |
| case SIOCIPFFL : |
| BCOPYIN(data, (char *)&arg, sizeof(arg)); |
| if (arg == 0 || arg == 1) { |
| WRITE_ENTER(&ifs->ifs_ipf_state); |
| ret = fr_state_flush(arg, 4, ifs); |
| RWLOCK_EXIT(&ifs->ifs_ipf_state); |
| BCOPYOUT((char *)&ret, data, sizeof(ret)); |
| } else |
| error = EINVAL; |
| break; |
| #ifdef USE_INET6 |
| case SIOCIPFL6 : |
| BCOPYIN(data, (char *)&arg, sizeof(arg)); |
| if (arg == 0 || arg == 1) { |
| WRITE_ENTER(&ifs->ifs_ipf_state); |
| ret = fr_state_flush(arg, 6, ifs); |
| RWLOCK_EXIT(&ifs->ifs_ipf_state); |
| BCOPYOUT((char *)&ret, data, sizeof(ret)); |
| } else |
| error = EINVAL; |
| break; |
| #endif |
| #ifdef IPFILTER_LOG |
| /* |
| * Flush the state log. |
| */ |
| case SIOCIPFFB : |
| if (!(mode & FWRITE)) |
| error = EPERM; |
| else { |
| int tmp; |
| |
| tmp = ipflog_clear(IPL_LOGSTATE, ifs); |
| BCOPYOUT((char *)&tmp, data, sizeof(tmp)); |
| } |
| break; |
| /* |
| * Turn logging of state information on/off. |
| */ |
| case SIOCSETLG : |
| if (!(mode & FWRITE)) |
| error = EPERM; |
| else { |
| BCOPYIN((char *)data, |
| (char *)&ifs->ifs_ipstate_logging, |
| sizeof(ifs->ifs_ipstate_logging)); |
| } |
| break; |
| /* |
| * Return the current state of logging. |
| */ |
| case SIOCGETLG : |
| BCOPYOUT((char *)&ifs->ifs_ipstate_logging, (char *)data, |
| sizeof(ifs->ifs_ipstate_logging)); |
| break; |
| /* |
| * Return the number of bytes currently waiting to be read. |
| */ |
| case FIONREAD : |
| arg = ifs->ifs_iplused[IPL_LOGSTATE]; /* returned in an int */ |
| BCOPYOUT((char *)&arg, data, sizeof(arg)); |
| break; |
| #endif |
| /* |
| * Get the current state statistics. |
| */ |
| case SIOCGETFS : |
| error = fr_outobj(data, fr_statetstats(ifs), IPFOBJ_STATESTAT); |
| break; |
| /* |
| * Lock/Unlock the state table. (Locking prevents any changes, which |
| * means no packets match). |
| */ |
| case SIOCSTLCK : |
| if (!(mode & FWRITE)) { |
| error = EPERM; |
| } else { |
| fr_lock(data, &ifs->ifs_fr_state_lock); |
| } |
| break; |
| /* |
| * Add an entry to the current state table. |
| */ |
| case SIOCSTPUT : |
| if (!ifs->ifs_fr_state_lock || !(mode &FWRITE)) { |
| error = EACCES; |
| break; |
| } |
| error = fr_stputent(data, ifs); |
| break; |
| /* |
| * Get a state table entry. |
| */ |
| case SIOCSTGET : |
| if (!ifs->ifs_fr_state_lock) { |
| error = EACCES; |
| break; |
| } |
| error = fr_stgetent(data, ifs); |
| break; |
| |
| case SIOCGENITER : |
| { |
| ipftoken_t *token; |
| ipfgeniter_t iter; |
| |
| error = fr_inobj(data, &iter, IPFOBJ_GENITER); |
| if (error != 0) |
| break; |
| |
| token = ipf_findtoken(IPFGENITER_STATE, uid, ctx, ifs); |
| if (token != NULL) |
| error = fr_stateiter(token, &iter, ifs); |
| else |
| error = ESRCH; |
| RWLOCK_EXIT(&ifs->ifs_ipf_tokens); |
| break; |
| } |
| |
| case SIOCIPFDELTOK : |
| (void) BCOPYIN(data, (char *)&arg, sizeof(arg)); |
| error = ipf_deltoken(arg, uid, ctx, ifs); |
| break; |
| |
| default : |
| error = EINVAL; |
| break; |
| } |
| return error; |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_stgetent */ |
| /* Returns: int - 0 == success, != 0 == failure */ |
| /* Parameters: data(I) - pointer to state structure to retrieve from table */ |
| /* */ |
| /* Copy out state information from the kernel to a user space process. If */ |
| /* there is a filter rule associated with the state entry, copy that out */ |
| /* as well. The entry to copy out is taken from the value of "ips_next" in */ |
| /* the struct passed in and if not null and not found in the list of current*/ |
| /* state entries, the retrieval fails. */ |
| /* ------------------------------------------------------------------------ */ |
| int fr_stgetent(data, ifs) |
| caddr_t data; |
| ipf_stack_t *ifs; |
| { |
| ipstate_t *is, *isn; |
| ipstate_save_t ips; |
| int error; |
| |
| error = fr_inobj(data, &ips, IPFOBJ_STATESAVE); |
| if (error) |
| return EFAULT; |
| |
| isn = ips.ips_next; |
| if (isn == NULL) { |
| isn = ifs->ifs_ips_list; |
| if (isn == NULL) { |
| if (ips.ips_next == NULL) |
| return ENOENT; |
| return 0; |
| } |
| } else { |
| /* |
| * Make sure the pointer we're copying from exists in the |
| * current list of entries. Security precaution to prevent |
| * copying of random kernel data. |
| */ |
| for (is = ifs->ifs_ips_list; is; is = is->is_next) |
| if (is == isn) |
| break; |
| if (!is) |
| return ESRCH; |
| } |
| ips.ips_next = isn->is_next; |
| bcopy((char *)isn, (char *)&ips.ips_is, sizeof(ips.ips_is)); |
| ips.ips_rule = isn->is_rule; |
| if (isn->is_rule != NULL) |
| bcopy((char *)isn->is_rule, (char *)&ips.ips_fr, |
| sizeof(ips.ips_fr)); |
| error = fr_outobj(data, &ips, IPFOBJ_STATESAVE); |
| if (error) |
| return EFAULT; |
| return 0; |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_stputent */ |
| /* Returns: int - 0 == success, != 0 == failure */ |
| /* Parameters: data(I) - pointer to state information struct */ |
| /* */ |
| /* This function implements the SIOCSTPUT ioctl: insert a state entry into */ |
| /* the state table. If the state info. includes a pointer to a filter rule */ |
| /* then also add in an orphaned rule (will not show up in any "ipfstat -io" */ |
| /* output. */ |
| /* ------------------------------------------------------------------------ */ |
| int fr_stputent(data, ifs) |
| caddr_t data; |
| ipf_stack_t *ifs; |
| { |
| ipstate_t *is, *isn; |
| ipstate_save_t ips; |
| int error, i; |
| frentry_t *fr; |
| char *name; |
| |
| error = fr_inobj(data, &ips, IPFOBJ_STATESAVE); |
| if (error) |
| return EFAULT; |
| |
| KMALLOC(isn, ipstate_t *); |
| if (isn == NULL) |
| return ENOMEM; |
| |
| bcopy((char *)&ips.ips_is, (char *)isn, sizeof(*isn)); |
| bzero((char *)isn, offsetof(struct ipstate, is_pkts)); |
| isn->is_sti.tqe_pnext = NULL; |
| isn->is_sti.tqe_next = NULL; |
| isn->is_sti.tqe_ifq = NULL; |
| isn->is_sti.tqe_parent = isn; |
| isn->is_ifp[0] = NULL; |
| isn->is_ifp[1] = NULL; |
| isn->is_ifp[2] = NULL; |
| isn->is_ifp[3] = NULL; |
| isn->is_sync = NULL; |
| fr = ips.ips_rule; |
| |
| if (fr == NULL) { |
| READ_ENTER(&ifs->ifs_ipf_state); |
| fr_stinsert(isn, 0, ifs); |
| MUTEX_EXIT(&isn->is_lock); |
| RWLOCK_EXIT(&ifs->ifs_ipf_state); |
| return 0; |
| } |
| |
| if (isn->is_flags & SI_NEWFR) { |
| KMALLOC(fr, frentry_t *); |
| if (fr == NULL) { |
| KFREE(isn); |
| return ENOMEM; |
| } |
| bcopy((char *)&ips.ips_fr, (char *)fr, sizeof(*fr)); |
| isn->is_rule = fr; |
| ips.ips_is.is_rule = fr; |
| MUTEX_NUKE(&fr->fr_lock); |
| MUTEX_INIT(&fr->fr_lock, "state filter rule lock"); |
| |
| /* |
| * Look up all the interface names in the rule. |
| */ |
| for (i = 0; i < 4; i++) { |
| name = fr->fr_ifnames[i]; |
| fr->fr_ifas[i] = fr_resolvenic(name, fr->fr_v, ifs); |
| name = isn->is_ifname[i]; |
| isn->is_ifp[i] = fr_resolvenic(name, isn->is_v, ifs); |
| } |
| |
| fr->fr_ref = 0; |
| fr->fr_dsize = 0; |
| fr->fr_data = NULL; |
| |
| fr_resolvedest(&fr->fr_tif, fr->fr_v, ifs); |
| fr_resolvedest(&fr->fr_dif, fr->fr_v, ifs); |
| |
| /* |
| * send a copy back to userland of what we ended up |
| * to allow for verification. |
| */ |
| error = fr_outobj(data, &ips, IPFOBJ_STATESAVE); |
| if (error) { |
| KFREE(isn); |
| MUTEX_DESTROY(&fr->fr_lock); |
| KFREE(fr); |
| return EFAULT; |
| } |
| READ_ENTER(&ifs->ifs_ipf_state); |
| fr_stinsert(isn, 0, ifs); |
| MUTEX_EXIT(&isn->is_lock); |
| RWLOCK_EXIT(&ifs->ifs_ipf_state); |
| |
| } else { |
| READ_ENTER(&ifs->ifs_ipf_state); |
| for (is = ifs->ifs_ips_list; is; is = is->is_next) |
| if (is->is_rule == fr) { |
| fr_stinsert(isn, 0, ifs); |
| MUTEX_EXIT(&isn->is_lock); |
| break; |
| } |
| |
| if (is == NULL) { |
| KFREE(isn); |
| isn = NULL; |
| } |
| RWLOCK_EXIT(&ifs->ifs_ipf_state); |
| |
| return (isn == NULL) ? ESRCH : 0; |
| } |
| |
| return 0; |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_stinsert */ |
| /* Returns: Nil */ |
| /* Parameters: is(I) - pointer to state structure */ |
| /* rev(I) - flag indicating forward/reverse direction of packet */ |
| /* */ |
| /* Inserts a state structure into the hash table (for lookups) and the list */ |
| /* of state entries (for enumeration). Resolves all of the interface names */ |
| /* to pointers and adjusts running stats for the hash table as appropriate. */ |
| /* */ |
| /* Locking: it is assumed that some kind of lock on ipf_state is held. */ |
| /* Exits with is_lock initialised and held. */ |
| /* ------------------------------------------------------------------------ */ |
| void fr_stinsert(is, rev, ifs) |
| ipstate_t *is; |
| int rev; |
| ipf_stack_t *ifs; |
| { |
| frentry_t *fr; |
| u_int hv; |
| int i; |
| |
| MUTEX_INIT(&is->is_lock, "ipf state entry"); |
| |
| fr = is->is_rule; |
| if (fr != NULL) { |
| MUTEX_ENTER(&fr->fr_lock); |
| fr->fr_ref++; |
| fr->fr_statecnt++; |
| MUTEX_EXIT(&fr->fr_lock); |
| } |
| |
| /* |
| * Look up all the interface names in the state entry. |
| */ |
| for (i = 0; i < 4; i++) { |
| if (is->is_ifp[i] != NULL) |
| continue; |
| is->is_ifp[i] = fr_resolvenic(is->is_ifname[i], is->is_v, ifs); |
| } |
| |
| /* |
| * If we could trust is_hv, then the modulous would not be needed, but |
| * when running with IPFILTER_SYNC, this stops bad values. |
| */ |
| hv = is->is_hv % ifs->ifs_fr_statesize; |
| is->is_hv = hv; |
| |
| /* |
| * We need to get both of these locks...the first because it is |
| * possible that once the insert is complete another packet might |
| * come along, match the entry and want to update it. |
| */ |
| MUTEX_ENTER(&is->is_lock); |
| MUTEX_ENTER(&ifs->ifs_ipf_stinsert); |
| |
| /* |
| * add into list table. |
| */ |
| if (ifs->ifs_ips_list != NULL) |
| ifs->ifs_ips_list->is_pnext = &is->is_next; |
| is->is_pnext = &ifs->ifs_ips_list; |
| is->is_next = ifs->ifs_ips_list; |
| ifs->ifs_ips_list = is; |
| |
| if (ifs->ifs_ips_table[hv] != NULL) |
| ifs->ifs_ips_table[hv]->is_phnext = &is->is_hnext; |
| else |
| ifs->ifs_ips_stats.iss_inuse++; |
| is->is_phnext = ifs->ifs_ips_table + hv; |
| is->is_hnext = ifs->ifs_ips_table[hv]; |
| ifs->ifs_ips_table[hv] = is; |
| ifs->ifs_ips_stats.iss_bucketlen[hv]++; |
| ifs->ifs_ips_num++; |
| MUTEX_EXIT(&ifs->ifs_ipf_stinsert); |
| |
| fr_setstatequeue(is, rev, ifs); |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_addstate */ |
| /* Returns: ipstate_t* - NULL == failure, else pointer to new state */ |
| /* Parameters: fin(I) - pointer to packet information */ |
| /* stsave(O) - pointer to place to save pointer to created */ |
| /* state structure. */ |
| /* flags(I) - flags to use when creating the structure */ |
| /* */ |
| /* Creates a new IP state structure from the packet information collected. */ |
| /* Inserts it into the state table and appends to the bottom of the active */ |
| /* list. If the capacity of the table has reached the maximum allowed then */ |
| /* the call will fail and a flush is scheduled for the next timeout call. */ |
| /* ------------------------------------------------------------------------ */ |
| ipstate_t *fr_addstate(fin, stsave, flags) |
| fr_info_t *fin; |
| ipstate_t **stsave; |
| u_int flags; |
| { |
| ipstate_t *is, ips; |
| struct icmp *ic; |
| u_int pass, hv; |
| frentry_t *fr; |
| tcphdr_t *tcp; |
| grehdr_t *gre; |
| void *ifp; |
| int out; |
| ipf_stack_t *ifs = fin->fin_ifs; |
| |
| if (ifs->ifs_fr_state_lock || |
| (fin->fin_flx & (FI_SHORT|FI_STATE|FI_FRAGBODY|FI_BAD))) |
| return NULL; |
| |
| if ((fin->fin_flx & FI_OOW) && !(fin->fin_tcpf & TH_SYN)) |
| return NULL; |
| |
| /* |
| * If a "keep state" rule has reached the maximum number of references |
| * to it, then schedule an automatic flush in case we can clear out |
| * some "dead old wood". Note that because the lock isn't held on |
| * fr it is possible that we could overflow. The cost of overflowing |
| * is being ignored here as the number by which it can overflow is |
| * a product of the number of simultaneous threads that could be |
| * executing in here, so a limit of 100 won't result in 200, but could |
| * result in 101 or 102. |
| */ |
| fr = fin->fin_fr; |
| if (fr != NULL) { |
| if ((ifs->ifs_ips_num == ifs->ifs_fr_statemax) && (fr->fr_statemax == 0)) { |
| ATOMIC_INCL(ifs->ifs_ips_stats.iss_max); |
| ifs->ifs_fr_state_doflush = 1; |
| return NULL; |
| } |
| if ((fr->fr_statemax != 0) && |
| (fr->fr_statecnt >= fr->fr_statemax)) { |
| ATOMIC_INCL(ifs->ifs_ips_stats.iss_maxref); |
| ifs->ifs_fr_state_doflush = 1; |
| return NULL; |
| } |
| } |
| |
| pass = (fr == NULL) ? 0 : fr->fr_flags; |
| |
| ic = NULL; |
| tcp = NULL; |
| out = fin->fin_out; |
| is = &ips; |
| bzero((char *)is, sizeof(*is)); |
| is->is_die = 1 + ifs->ifs_fr_ticks; |
| |
| /* |
| * Copy and calculate... |
| */ |
| hv = (is->is_p = fin->fin_fi.fi_p); |
| is->is_src = fin->fin_fi.fi_src; |
| hv += is->is_saddr; |
| is->is_dst = fin->fin_fi.fi_dst; |
| hv += is->is_daddr; |
| #ifdef USE_INET6 |
| if (fin->fin_v == 6) { |
| /* |
| * For ICMPv6, we check to see if the destination address is |
| * a multicast address. If it is, do not include it in the |
| * calculation of the hash because the correct reply will come |
| * back from a real address, not a multicast address. |
| */ |
| if ((is->is_p == IPPROTO_ICMPV6) && |
| IN6_IS_ADDR_MULTICAST(&is->is_dst.in6)) { |
| /* |
| * So you can do keep state with neighbour discovery. |
| * |
| * Here we could use the address from the neighbour |
| * solicit message to put in the state structure and |
| * we could use that without a wildcard flag too... |
| */ |
| is->is_flags |= SI_W_DADDR; |
| hv -= is->is_daddr; |
| } else { |
| hv += is->is_dst.i6[1]; |
| hv += is->is_dst.i6[2]; |
| hv += is->is_dst.i6[3]; |
| } |
| hv += is->is_src.i6[1]; |
| hv += is->is_src.i6[2]; |
| hv += is->is_src.i6[3]; |
| } |
| #endif |
| |
| switch (is->is_p) |
| { |
| #ifdef USE_INET6 |
| case IPPROTO_ICMPV6 : |
| ic = fin->fin_dp; |
| |
| switch (ic->icmp_type) |
| { |
| case ICMP6_ECHO_REQUEST : |
| is->is_icmp.ici_type = ic->icmp_type; |
| hv += (is->is_icmp.ici_id = ic->icmp_id); |
| break; |
| case ICMP6_MEMBERSHIP_QUERY : |
| case ND_ROUTER_SOLICIT : |
| case ND_NEIGHBOR_SOLICIT : |
| case ICMP6_NI_QUERY : |
| is->is_icmp.ici_type = ic->icmp_type; |
| break; |
| default : |
| return NULL; |
| } |
| ATOMIC_INCL(ifs->ifs_ips_stats.iss_icmp); |
| break; |
| #endif |
| case IPPROTO_ICMP : |
| ic = fin->fin_dp; |
| |
| switch (ic->icmp_type) |
| { |
| case ICMP_ECHO : |
| case ICMP_TSTAMP : |
| case ICMP_IREQ : |
| case ICMP_MASKREQ : |
| is->is_icmp.ici_type = ic->icmp_type; |
| hv += (is->is_icmp.ici_id = ic->icmp_id); |
| break; |
| default : |
| return NULL; |
| } |
| ATOMIC_INCL(ifs->ifs_ips_stats.iss_icmp); |
| break; |
| |
| case IPPROTO_GRE : |
| gre = fin->fin_dp; |
| |
| is->is_gre.gs_flags = gre->gr_flags; |
| is->is_gre.gs_ptype = gre->gr_ptype; |
| if (GRE_REV(is->is_gre.gs_flags) == 1) { |
| is->is_call[0] = fin->fin_data[0]; |
| is->is_call[1] = fin->fin_data[1]; |
| } |
| break; |
| |
| case IPPROTO_TCP : |
| tcp = fin->fin_dp; |
| |
| if (tcp->th_flags & TH_RST) |
| return NULL; |
| /* |
| * The endian of the ports doesn't matter, but the ack and |
| * sequence numbers do as we do mathematics on them later. |
| */ |
| is->is_sport = htons(fin->fin_data[0]); |
| is->is_dport = htons(fin->fin_data[1]); |
| if ((flags & (SI_W_DPORT|SI_W_SPORT)) == 0) { |
| hv += is->is_sport; |
| hv += is->is_dport; |
| } |
| |
| /* |
| * If this is a real packet then initialise fields in the |
| * state information structure from the TCP header information. |
| */ |
| |
| is->is_maxdwin = 1; |
| is->is_maxswin = ntohs(tcp->th_win); |
| if (is->is_maxswin == 0) |
| is->is_maxswin = 1; |
| |
| if ((fin->fin_flx & FI_IGNORE) == 0) { |
| is->is_send = ntohl(tcp->th_seq) + fin->fin_dlen - |
| (TCP_OFF(tcp) << 2) + |
| ((tcp->th_flags & TH_SYN) ? 1 : 0) + |
| ((tcp->th_flags & TH_FIN) ? 1 : 0); |
| is->is_maxsend = is->is_send; |
| |
| /* |
| * Window scale option is only present in |
| * SYN/SYN-ACK packet. |
| */ |
| if ((tcp->th_flags & ~(TH_FIN|TH_ACK|TH_ECNALL)) == |
| TH_SYN && |
| (TCP_OFF(tcp) > (sizeof(tcphdr_t) >> 2))) { |
| if (fr_tcpoptions(fin, tcp, |
| &is->is_tcp.ts_data[0])) |
| is->is_swinflags = TCP_WSCALE_SEEN| |
| TCP_WSCALE_FIRST; |
| } |
| |
| if ((fin->fin_out != 0) && (pass & FR_NEWISN) != 0) { |
| fr_checknewisn(fin, is); |
| fr_fixoutisn(fin, is); |
| } |
| |
| if ((tcp->th_flags & TH_OPENING) == TH_SYN) |
| flags |= IS_TCPFSM; |
| else { |
| is->is_maxdwin = is->is_maxswin * 2; |
| is->is_dend = ntohl(tcp->th_ack); |
| is->is_maxdend = ntohl(tcp->th_ack); |
| is->is_maxdwin *= 2; |
| } |
| } |
| |
| /* |
| * If we're creating state for a starting connection, start the |
| * timer on it as we'll never see an error if it fails to |
| * connect. |
| */ |
| ATOMIC_INCL(ifs->ifs_ips_stats.iss_tcp); |
| break; |
| |
| case IPPROTO_UDP : |
| tcp = fin->fin_dp; |
| |
| is->is_sport = htons(fin->fin_data[0]); |
| is->is_dport = htons(fin->fin_data[1]); |
| if ((flags & (SI_W_DPORT|SI_W_SPORT)) == 0) { |
| hv += tcp->th_dport; |
| hv += tcp->th_sport; |
| } |
| ATOMIC_INCL(ifs->ifs_ips_stats.iss_udp); |
| break; |
| |
| default : |
| break; |
| } |
| hv = DOUBLE_HASH(hv, ifs); |
| is->is_hv = hv; |
| is->is_rule = fr; |
| is->is_flags = flags & IS_INHERITED; |
| |
| /* |
| * Look for identical state. |
| */ |
| for (is = ifs->ifs_ips_table[is->is_hv % ifs->ifs_fr_statesize]; |
| is != NULL; |
| is = is->is_hnext) { |
| if (bcmp(&ips.is_src, &is->is_src, |
| offsetof(struct ipstate, is_ps) - |
| offsetof(struct ipstate, is_src)) == 0) |
| break; |
| } |
| if (is != NULL) |
| return NULL; |
| |
| if (ifs->ifs_ips_stats.iss_bucketlen[hv] >= ifs->ifs_fr_state_maxbucket) { |
| ATOMIC_INCL(ifs->ifs_ips_stats.iss_bucketfull); |
| return NULL; |
| } |
| KMALLOC(is, ipstate_t *); |
| if (is == NULL) { |
| ATOMIC_INCL(ifs->ifs_ips_stats.iss_nomem); |
| return NULL; |
| } |
| bcopy((char *)&ips, (char *)is, sizeof(*is)); |
| /* |
| * Do not do the modulous here, it is done in fr_stinsert(). |
| */ |
| if (fr != NULL) { |
| (void) strncpy(is->is_group, fr->fr_group, FR_GROUPLEN); |
| if (fr->fr_age[0] != 0) { |
| is->is_tqehead[0] = |
| fr_addtimeoutqueue(&ifs->ifs_ips_utqe, |
| fr->fr_age[0], ifs); |
| is->is_sti.tqe_flags |= TQE_RULEBASED; |
| } |
| if (fr->fr_age[1] != 0) { |
| is->is_tqehead[1] = |
| fr_addtimeoutqueue(&ifs->ifs_ips_utqe, |
| fr->fr_age[1], ifs); |
| is->is_sti.tqe_flags |= TQE_RULEBASED; |
| } |
| is->is_tag = fr->fr_logtag; |
| |
| is->is_ifp[(out << 1) + 1] = fr->fr_ifas[1]; |
| is->is_ifp[(1 - out) << 1] = fr->fr_ifas[2]; |
| is->is_ifp[((1 - out) << 1) + 1] = fr->fr_ifas[3]; |
| |
| if (((ifp = fr->fr_ifas[1]) != NULL) && |
| (ifp != (void *)-1)) { |
| COPYIFNAME(ifp, is->is_ifname[(out << 1) + 1], fr->fr_v); |
| } |
| if (((ifp = fr->fr_ifas[2]) != NULL) && |
| (ifp != (void *)-1)) { |
| COPYIFNAME(ifp, is->is_ifname[(1 - out) << 1], fr->fr_v); |
| } |
| if (((ifp = fr->fr_ifas[3]) != NULL) && |
| (ifp != (void *)-1)) { |
| COPYIFNAME(ifp, is->is_ifname[((1 - out) << 1) + 1], fr->fr_v); |
| } |
| } else { |
| pass = ifs->ifs_fr_flags; |
| is->is_tag = FR_NOLOGTAG; |
| } |
| |
| is->is_ifp[out << 1] = fin->fin_ifp; |
| if (fin->fin_ifp != NULL) { |
| COPYIFNAME(fin->fin_ifp, is->is_ifname[out << 1], fr->fr_v); |
| } |
| |
| /* |
| * It may seem strange to set is_ref to 2, but fr_check() will call |
| * fr_statederef() after calling fr_addstate() and the idea is to |
| * have it exist at the end of fr_check() with is_ref == 1. |
| */ |
| is->is_ref = 2; |
| is->is_pass = pass; |
| is->is_pkts[0] = 0, is->is_bytes[0] = 0; |
| is->is_pkts[1] = 0, is->is_bytes[1] = 0; |
| is->is_pkts[2] = 0, is->is_bytes[2] = 0; |
| is->is_pkts[3] = 0, is->is_bytes[3] = 0; |
| if ((fin->fin_flx & FI_IGNORE) == 0) { |
| is->is_pkts[out] = 1; |
| is->is_bytes[out] = fin->fin_plen; |
| is->is_flx[out][0] = fin->fin_flx & FI_CMP; |
| is->is_flx[out][0] &= ~FI_OOW; |
| } |
| |
| if (pass & FR_STSTRICT) |
| is->is_flags |= IS_STRICT; |
| |
| if (pass & FR_STATESYNC) |
| is->is_flags |= IS_STATESYNC; |
| |
| /* |
| * We want to check everything that is a property of this packet, |
| * but we don't (automatically) care about it's fragment status as |
| * this may change. |
| */ |
| is->is_v = fin->fin_v; |
| is->is_opt[0] = fin->fin_optmsk; |
| is->is_optmsk[0] = 0xffffffff; |
| is->is_optmsk[1] = 0xffffffff; |
| if (is->is_v == 6) { |
| is->is_opt[0] &= ~0x8; |
| is->is_optmsk[0] &= ~0x8; |
| is->is_optmsk[1] &= ~0x8; |
| } |
| is->is_sec = fin->fin_secmsk; |
| is->is_secmsk = 0xffff; |
| is->is_auth = fin->fin_auth; |
| is->is_authmsk = 0xffff; |
| if (flags & (SI_WILDP|SI_WILDA)) { |
| ATOMIC_INCL(ifs->ifs_ips_stats.iss_wild); |
| } |
| is->is_rulen = fin->fin_rule; |
| |
| |
| if (pass & FR_LOGFIRST) |
| is->is_pass &= ~(FR_LOGFIRST|FR_LOG); |
| |
| READ_ENTER(&ifs->ifs_ipf_state); |
| is->is_me = stsave; |
| |
| fr_stinsert(is, fin->fin_rev, ifs); |
| |
| if (fin->fin_p == IPPROTO_TCP) { |
| /* |
| * If we're creating state for a starting connection, start the |
| * timer on it as we'll never see an error if it fails to |
| * connect. |
| */ |
| (void) fr_tcp_age(&is->is_sti, fin, ifs->ifs_ips_tqtqb, |
| is->is_flags); |
| MUTEX_EXIT(&is->is_lock); |
| #ifdef IPFILTER_SCAN |
| if ((is->is_flags & SI_CLONE) == 0) |
| (void) ipsc_attachis(is); |
| #endif |
| } else { |
| MUTEX_EXIT(&is->is_lock); |
| } |
| #ifdef IPFILTER_SYNC |
| if ((is->is_flags & IS_STATESYNC) && ((is->is_flags & SI_CLONE) == 0)) |
| is->is_sync = ipfsync_new(SMC_STATE, fin, is); |
| #endif |
| if (ifs->ifs_ipstate_logging) |
| ipstate_log(is, ISL_NEW, ifs); |
| |
| RWLOCK_EXIT(&ifs->ifs_ipf_state); |
| fin->fin_state = is; |
| fin->fin_rev = IP6_NEQ(&is->is_dst, &fin->fin_daddr); |
| fin->fin_flx |= FI_STATE; |
| if (fin->fin_flx & FI_FRAG) |
| (void) fr_newfrag(fin, pass ^ FR_KEEPSTATE); |
| |
| return is; |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_tcpoptions */ |
| /* Returns: int - 1 == packet matches state entry, 0 == it does not */ |
| /* Parameters: fin(I) - pointer to packet information */ |
| /* tcp(I) - pointer to TCP packet header */ |
| /* td(I) - pointer to TCP data held as part of the state */ |
| /* */ |
| /* Look after the TCP header for any options and deal with those that are */ |
| /* present. Record details about those that we recogise. */ |
| /* ------------------------------------------------------------------------ */ |
| static int fr_tcpoptions(fin, tcp, td) |
| fr_info_t *fin; |
| tcphdr_t *tcp; |
| tcpdata_t *td; |
| { |
| int off, mlen, ol, i, len, retval; |
| char buf[64], *s, opt; |
| mb_t *m = NULL; |
| |
| len = (TCP_OFF(tcp) << 2); |
| if (fin->fin_dlen < len) |
| return 0; |
| len -= sizeof(*tcp); |
| |
| off = fin->fin_plen - fin->fin_dlen + sizeof(*tcp) + fin->fin_ipoff; |
| |
| m = fin->fin_m; |
| mlen = MSGDSIZE(m) - off; |
| if (len > mlen) { |
| len = mlen; |
| retval = 0; |
| } else { |
| retval = 1; |
| } |
| |
| COPYDATA(m, off, len, buf); |
| |
| for (s = buf; len > 0; ) { |
| opt = *s; |
| if (opt == TCPOPT_EOL) |
| break; |
| else if (opt == TCPOPT_NOP) |
| ol = 1; |
| else { |
| if (len < 2) |
| break; |
| ol = (int)*(s + 1); |
| if (ol < 2 || ol > len) |
| break; |
| |
| /* |
| * Extract the TCP options we are interested in out of |
| * the header and store them in the the tcpdata struct. |
| */ |
| switch (opt) |
| { |
| case TCPOPT_WINDOW : |
| if (ol == TCPOLEN_WINDOW) { |
| i = (int)*(s + 2); |
| if (i > TCP_WSCALE_MAX) |
| i = TCP_WSCALE_MAX; |
| else if (i < 0) |
| i = 0; |
| td->td_winscale = i; |
| } |
| break; |
| case TCPOPT_MAXSEG : |
| /* |
| * So, if we wanted to set the TCP MAXSEG, |
| * it should be done here... |
| */ |
| if (ol == TCPOLEN_MAXSEG) { |
| i = (int)*(s + 2); |
| i <<= 8; |
| i += (int)*(s + 3); |
| td->td_maxseg = i; |
| } |
| break; |
| } |
| } |
| len -= ol; |
| s += ol; |
| } |
| return retval; |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_tcpstate */ |
| /* Returns: int - 1 == packet matches state entry, 0 == it does not */ |
| /* Parameters: fin(I) - pointer to packet information */ |
| /* tcp(I) - pointer to TCP packet header */ |
| /* is(I) - pointer to master state structure */ |
| /* */ |
| /* Check to see if a packet with TCP headers fits within the TCP window. */ |
| /* Change timeout depending on whether new packet is a SYN-ACK returning */ |
| /* for a SYN or a RST or FIN which indicate time to close up shop. */ |
| /* ------------------------------------------------------------------------ */ |
| static int fr_tcpstate(fin, tcp, is) |
| fr_info_t *fin; |
| tcphdr_t *tcp; |
| ipstate_t *is; |
| { |
| int source, ret = 0, flags; |
| tcpdata_t *fdata, *tdata; |
| ipf_stack_t *ifs = fin->fin_ifs; |
| |
| source = !fin->fin_rev; |
| if (((is->is_flags & IS_TCPFSM) != 0) && (source == 1) && |
| (ntohs(is->is_sport) != fin->fin_data[0])) |
| source = 0; |
| fdata = &is->is_tcp.ts_data[!source]; |
| tdata = &is->is_tcp.ts_data[source]; |
| |
| MUTEX_ENTER(&is->is_lock); |
| if (fr_tcpinwindow(fin, fdata, tdata, tcp, is->is_flags)) { |
| #ifdef IPFILTER_SCAN |
| if (is->is_flags & (IS_SC_CLIENT|IS_SC_SERVER)) { |
| ipsc_packet(fin, is); |
| if (FR_ISBLOCK(is->is_pass)) { |
| MUTEX_EXIT(&is->is_lock); |
| return 1; |
| } |
| } |
| #endif |
| |
| /* |
| * Nearing end of connection, start timeout. |
| */ |
| ret = fr_tcp_age(&is->is_sti, fin, ifs->ifs_ips_tqtqb, |
| is->is_flags); |
| if (ret == 0) { |
| MUTEX_EXIT(&is->is_lock); |
| return 0; |
| } |
| |
| /* |
| * set s0's as appropriate. Use syn-ack packet as it |
| * contains both pieces of required information. |
| */ |
| /* |
| * Window scale option is only present in SYN/SYN-ACK packet. |
| * Compare with ~TH_FIN to mask out T/TCP setups. |
| */ |
| flags = tcp->th_flags & ~(TH_FIN|TH_ECNALL); |
| if (flags == (TH_SYN|TH_ACK)) { |
| is->is_s0[source] = ntohl(tcp->th_ack); |
| is->is_s0[!source] = ntohl(tcp->th_seq) + 1; |
| if ((TCP_OFF(tcp) > (sizeof(tcphdr_t) >> 2)) && |
| tdata->td_winscale) { |
| if (fr_tcpoptions(fin, tcp, fdata)) { |
| fdata->td_winflags = TCP_WSCALE_SEEN| |
| TCP_WSCALE_FIRST; |
| } else { |
| if (!fdata->td_winscale) |
| tdata->td_winscale = 0; |
| } |
| } |
| if ((fin->fin_out != 0) && (is->is_pass & FR_NEWISN)) |
| fr_checknewisn(fin, is); |
| } else if (flags == TH_SYN) { |
| is->is_s0[source] = ntohl(tcp->th_seq) + 1; |
| if ((TCP_OFF(tcp) > (sizeof(tcphdr_t) >> 2))) |
| if (fr_tcpoptions(fin, tcp, tdata)) { |
| tdata->td_winflags = TCP_WSCALE_SEEN| |
| TCP_WSCALE_FIRST; |
| } |
| |
| if ((fin->fin_out != 0) && (is->is_pass & FR_NEWISN)) |
| fr_checknewisn(fin, is); |
| |
| } |
| ret = 1; |
| } else |
| fin->fin_flx |= FI_OOW; |
| MUTEX_EXIT(&is->is_lock); |
| return ret; |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_checknewisn */ |
| /* Returns: Nil */ |
| /* Parameters: fin(I) - pointer to packet information */ |
| /* is(I) - pointer to master state structure */ |
| /* */ |
| /* Check to see if this TCP connection is expecting and needs a new */ |
| /* sequence number for a particular direction of the connection. */ |
| /* */ |
| /* NOTE: This does not actually change the sequence numbers, only gets new */ |
| /* one ready. */ |
| /* ------------------------------------------------------------------------ */ |
| static void fr_checknewisn(fin, is) |
| fr_info_t *fin; |
| ipstate_t *is; |
| { |
| u_32_t sumd, old, new; |
| tcphdr_t *tcp; |
| int i; |
| |
| i = fin->fin_rev; |
| tcp = fin->fin_dp; |
| |
| if (((i == 0) && !(is->is_flags & IS_ISNSYN)) || |
| ((i == 1) && !(is->is_flags & IS_ISNACK))) { |
| old = ntohl(tcp->th_seq); |
| new = fr_newisn(fin); |
| is->is_isninc[i] = new - old; |
| CALC_SUMD(old, new, sumd); |
| is->is_sumd[i] = (sumd & 0xffff) + (sumd >> 16); |
| |
| is->is_flags |= ((i == 0) ? IS_ISNSYN : IS_ISNACK); |
| } |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_tcpinwindow */ |
| /* Returns: int - 1 == packet inside TCP "window", 0 == not inside. */ |
| /* Parameters: fin(I) - pointer to packet information */ |
| /* fdata(I) - pointer to tcp state informatio (forward) */ |
| /* tdata(I) - pointer to tcp state informatio (reverse) */ |
| /* tcp(I) - pointer to TCP packet header */ |
| /* */ |
| /* Given a packet has matched addresses and ports, check to see if it is */ |
| /* within the TCP data window. In a show of generosity, allow packets that */ |
| /* are within the window space behind the current sequence # as well. */ |
| /* ------------------------------------------------------------------------ */ |
| int fr_tcpinwindow(fin, fdata, tdata, tcp, flags) |
| fr_info_t *fin; |
| tcpdata_t *fdata, *tdata; |
| tcphdr_t *tcp; |
| int flags; |
| { |
| tcp_seq seq, ack, end; |
| int ackskew, tcpflags; |
| u_32_t win, maxwin; |
| |
| /* |
| * Find difference between last checked packet and this packet. |
| */ |
| tcpflags = tcp->th_flags; |
| seq = ntohl(tcp->th_seq); |
| ack = ntohl(tcp->th_ack); |
| if (tcpflags & TH_SYN) |
| win = ntohs(tcp->th_win); |
| else |
| win = ntohs(tcp->th_win) << fdata->td_winscale; |
| if (win == 0) |
| win = 1; |
| |
| /* |
| * if window scaling is present, the scaling is only allowed |
| * for windows not in the first SYN packet. In that packet the |
| * window is 65535 to specify the largest window possible |
| * for receivers not implementing the window scale option. |
| * Currently, we do not assume TTCP here. That means that |
| * if we see a second packet from a host (after the initial |
| * SYN), we can assume that the receiver of the SYN did |
| * already send back the SYN/ACK (and thus that we know if |
| * the receiver also does window scaling) |
| */ |
| if (!(tcpflags & TH_SYN) && (fdata->td_winflags & TCP_WSCALE_FIRST)) { |
| if (tdata->td_winflags & TCP_WSCALE_SEEN) { |
| fdata->td_winflags &= ~TCP_WSCALE_FIRST; |
| fdata->td_maxwin = win; |
| } else { |
| fdata->td_winscale = 0; |
| fdata->td_winflags = 0; |
| tdata->td_winscale = 0; |
| tdata->td_winflags = 0; |
| } |
| } |
| |
| end = seq + fin->fin_dlen - (TCP_OFF(tcp) << 2) + |
| ((tcpflags & TH_SYN) ? 1 : 0) + ((tcpflags & TH_FIN) ? 1 : 0); |
| |
| if ((fdata->td_end == 0) && |
| (!(flags & IS_TCPFSM) || |
| ((tcpflags & TH_OPENING) == TH_OPENING))) { |
| /* |
| * Must be a (outgoing) SYN-ACK in reply to a SYN. |
| */ |
| fdata->td_end = end; |
| fdata->td_maxwin = 1; |
| fdata->td_maxend = end + win; |
| } |
| |
| if (!(tcpflags & TH_ACK)) { /* Pretend an ack was sent */ |
| ack = tdata->td_end; |
| } else if (((tcpflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) && |
| (ack == 0)) { |
| /* gross hack to get around certain broken tcp stacks */ |
| ack = tdata->td_end; |
| } |
| |
| if (seq == end) |
| seq = end = fdata->td_end; |
| |
| maxwin = tdata->td_maxwin; |
| ackskew = tdata->td_end - ack; |
| |
| /* |
| * Strict sequencing only allows in-order delivery. |
| */ |
| if ((flags & IS_STRICT) != 0) { |
| if (seq != fdata->td_end) { |
| return 0; |
| } |
| } |
| |
| #define SEQ_GE(a,b) ((int)((a) - (b)) >= 0) |
| #define SEQ_GT(a,b) ((int)((a) - (b)) > 0) |
| if ( |
| #if defined(_KERNEL) |
| (SEQ_GE(fdata->td_maxend, end)) && |
| (SEQ_GE(seq, fdata->td_end - maxwin)) && |
| #endif |
| /* XXX what about big packets */ |
| #define MAXACKWINDOW 66000 |
| (-ackskew <= (MAXACKWINDOW << fdata->td_winscale)) && |
| ( ackskew <= (MAXACKWINDOW << fdata->td_winscale))) { |
| |
| /* if ackskew < 0 then this should be due to fragmented |
| * packets. There is no way to know the length of the |
| * total packet in advance. |
| * We do know the total length from the fragment cache though. |
| * Note however that there might be more sessions with |
| * exactly the same source and destination parameters in the |
| * state cache (and source and destination is the only stuff |
| * that is saved in the fragment cache). Note further that |
| * some TCP connections in the state cache are hashed with |
| * sport and dport as well which makes it not worthwhile to |
| * look for them. |
| * Thus, when ackskew is negative but still seems to belong |
| * to this session, we bump up the destinations end value. |
| */ |
| if (ackskew < 0) |
| tdata->td_end = ack; |
| |
| /* update max window seen */ |
| if (fdata->td_maxwin < win) |
| fdata->td_maxwin = win; |
| if (SEQ_GT(end, fdata->td_end)) |
| fdata->td_end = end; |
| if (SEQ_GE(ack + win, tdata->td_maxend)) |
| tdata->td_maxend = ack + win; |
| return 1; |
| } |
| return 0; |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_stclone */ |
| /* Returns: ipstate_t* - NULL == cloning failed, */ |
| /* else pointer to new state structure */ |
| /* Parameters: fin(I) - pointer to packet information */ |
| /* tcp(I) - pointer to TCP/UDP header */ |
| /* is(I) - pointer to master state structure */ |
| /* */ |
| /* Create a "duplcate" state table entry from the master. */ |
| /* ------------------------------------------------------------------------ */ |
| static ipstate_t *fr_stclone(fin, tcp, is) |
| fr_info_t *fin; |
| tcphdr_t *tcp; |
| ipstate_t *is; |
| { |
| ipstate_t *clone; |
| u_32_t send; |
| ipf_stack_t *ifs = fin->fin_ifs; |
| |
| if (ifs->ifs_ips_num == ifs->ifs_fr_statemax) { |
| ATOMIC_INCL(ifs->ifs_ips_stats.iss_max); |
| ifs->ifs_fr_state_doflush = 1; |
| return NULL; |
| } |
| KMALLOC(clone, ipstate_t *); |
| if (clone == NULL) |
| return NULL; |
| bcopy((char *)is, (char *)clone, sizeof(*clone)); |
| |
| MUTEX_NUKE(&clone->is_lock); |
| |
| clone->is_die = ONE_DAY + ifs->ifs_fr_ticks; |
| clone->is_state[0] = 0; |
| clone->is_state[1] = 0; |
| send = ntohl(tcp->th_seq) + fin->fin_dlen - (TCP_OFF(tcp) << 2) + |
| ((tcp->th_flags & TH_SYN) ? 1 : 0) + |
| ((tcp->th_flags & TH_FIN) ? 1 : 0); |
| |
| if (fin->fin_rev == 1) { |
| clone->is_dend = send; |
| clone->is_maxdend = send; |
| clone->is_send = 0; |
| clone->is_maxswin = 1; |
| clone->is_maxdwin = ntohs(tcp->th_win); |
| if (clone->is_maxdwin == 0) |
| clone->is_maxdwin = 1; |
| } else { |
| clone->is_send = send; |
| clone->is_maxsend = send; |
| clone->is_dend = 0; |
| clone->is_maxdwin = 1; |
| clone->is_maxswin = ntohs(tcp->th_win); |
| if (clone->is_maxswin == 0) |
| clone->is_maxswin = 1; |
| } |
| |
| clone->is_flags &= ~SI_CLONE; |
| clone->is_flags |= SI_CLONED; |
| fr_stinsert(clone, fin->fin_rev, ifs); |
| clone->is_ref = 2; |
| if (clone->is_p == IPPROTO_TCP) { |
| (void) fr_tcp_age(&clone->is_sti, fin, ifs->ifs_ips_tqtqb, |
| clone->is_flags); |
| } |
| MUTEX_EXIT(&clone->is_lock); |
| #ifdef IPFILTER_SCAN |
| (void) ipsc_attachis(is); |
| #endif |
| #ifdef IPFILTER_SYNC |
| if (is->is_flags & IS_STATESYNC) |
| clone->is_sync = ipfsync_new(SMC_STATE, fin, clone); |
| #endif |
| return clone; |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_matchsrcdst */ |
| /* Returns: Nil */ |
| /* Parameters: fin(I) - pointer to packet information */ |
| /* is(I) - pointer to state structure */ |
| /* src(I) - pointer to source address */ |
| /* dst(I) - pointer to destination address */ |
| /* tcp(I) - pointer to TCP/UDP header */ |
| /* */ |
| /* Match a state table entry against an IP packet. The logic below is that */ |
| /* ret gets set to one if the match succeeds, else remains 0. If it is */ |
| /* still 0 after the test. no match. */ |
| /* ------------------------------------------------------------------------ */ |
| static ipstate_t *fr_matchsrcdst(fin, is, src, dst, tcp, cmask) |
| fr_info_t *fin; |
| ipstate_t *is; |
| i6addr_t *src, *dst; |
| tcphdr_t *tcp; |
| u_32_t cmask; |
| { |
| int ret = 0, rev, out, flags, flx = 0, idx; |
| u_short sp, dp; |
| u_32_t cflx; |
| void *ifp; |
| ipf_stack_t *ifs = fin->fin_ifs; |
| |
| rev = IP6_NEQ(&is->is_dst, dst); |
| ifp = fin->fin_ifp; |
| out = fin->fin_out; |
| flags = is->is_flags; |
| sp = 0; |
| dp = 0; |
| |
| if (tcp != NULL) { |
| sp = htons(fin->fin_sport); |
| dp = ntohs(fin->fin_dport); |
| } |
| if (!rev) { |
| if (tcp != NULL) { |
| if (!(flags & SI_W_SPORT) && (sp != is->is_sport)) |
| rev = 1; |
| else if (!(flags & SI_W_DPORT) && (dp != is->is_dport)) |
| rev = 1; |
| } |
| } |
| |
| idx = (out << 1) + rev; |
| |
| /* |
| * If the interface for this 'direction' is set, make sure it matches. |
| * An interface name that is not set matches any, as does a name of *. |
| */ |
| if ((is->is_ifp[idx] == NULL && |
| (*is->is_ifname[idx] == '\0' || *is->is_ifname[idx] == '*')) || |
| is->is_ifp[idx] == ifp) |
| ret = 1; |
| |
| if (ret == 0) |
| return NULL; |
| ret = 0; |
| |
| /* |
| * Match addresses and ports. |
| */ |
| if (rev == 0) { |
| if ((IP6_EQ(&is->is_dst, dst) || (flags & SI_W_DADDR)) && |
| (IP6_EQ(&is->is_src, src) || (flags & SI_W_SADDR))) { |
| if (tcp) { |
| if ((sp == is->is_sport || flags & SI_W_SPORT)&& |
| (dp == is->is_dport || flags & SI_W_DPORT)) |
| ret = 1; |
| } else { |
| ret = 1; |
| } |
| } |
| } else { |
| if ((IP6_EQ(&is->is_dst, src) || (flags & SI_W_DADDR)) && |
| (IP6_EQ(&is->is_src, dst) || (flags & SI_W_SADDR))) { |
| if (tcp) { |
| if ((dp == is->is_sport || flags & SI_W_SPORT)&& |
| (sp == is->is_dport || flags & SI_W_DPORT)) |
| ret = 1; |
| } else { |
| ret = 1; |
| } |
| } |
| } |
| |
| if (ret == 0) |
| return NULL; |
| |
| /* |
| * Whether or not this should be here, is questionable, but the aim |
| * is to get this out of the main line. |
| */ |
| if (tcp == NULL) |
| flags = is->is_flags & ~(SI_WILDP|SI_NEWFR|SI_CLONE|SI_CLONED); |
| |
| /* |
| * Only one of the source or destination address can be flaged as a |
| * wildcard. Fill in the missing address, if set. |
| * For IPv6, if the address being copied in is multicast, then |
| * don't reset the wild flag - multicast causes it to be set in the |
| * first place! |
| */ |
| if ((flags & (SI_W_SADDR|SI_W_DADDR))) { |
| fr_ip_t *fi = &fin->fin_fi; |
| |
| if ((flags & SI_W_SADDR) != 0) { |
| if (rev == 0) { |
| #ifdef USE_INET6 |
| if (is->is_v == 6 && |
| IN6_IS_ADDR_MULTICAST(&fi->fi_src.in6)) |
| /*EMPTY*/; |
| else |
| #endif |
| { |
| is->is_src = fi->fi_src; |
| is->is_flags &= ~SI_W_SADDR; |
| } |
| } else { |
| #ifdef USE_INET6 |
| if (is->is_v == 6 && |
| IN6_IS_ADDR_MULTICAST(&fi->fi_dst.in6)) |
| /*EMPTY*/; |
| else |
| #endif |
| { |
| is->is_src = fi->fi_dst; |
| is->is_flags &= ~SI_W_SADDR; |
| } |
| } |
| } else if ((flags & SI_W_DADDR) != 0) { |
| if (rev == 0) { |
| #ifdef USE_INET6 |
| if (is->is_v == 6 && |
| IN6_IS_ADDR_MULTICAST(&fi->fi_dst.in6)) |
| /*EMPTY*/; |
| else |
| #endif |
| { |
| is->is_dst = fi->fi_dst; |
| is->is_flags &= ~SI_W_DADDR; |
| } |
| } else { |
| #ifdef USE_INET6 |
| if (is->is_v == 6 && |
| IN6_IS_ADDR_MULTICAST(&fi->fi_src.in6)) |
| /*EMPTY*/; |
| else |
| #endif |
| { |
| is->is_dst = fi->fi_src; |
| is->is_flags &= ~SI_W_DADDR; |
| } |
| } |
| } |
| if ((is->is_flags & (SI_WILDA|SI_WILDP)) == 0) { |
| ATOMIC_DECL(ifs->ifs_ips_stats.iss_wild); |
| } |
| } |
| |
| flx = fin->fin_flx & cmask; |
| cflx = is->is_flx[out][rev]; |
| |
| /* |
| * Match up any flags set from IP options. |
| */ |
| if ((cflx && (flx != (cflx & cmask))) || |
| ((fin->fin_optmsk & is->is_optmsk[rev]) != is->is_opt[rev]) || |
| ((fin->fin_secmsk & is->is_secmsk) != is->is_sec) || |
| ((fin->fin_auth & is->is_authmsk) != is->is_auth)) |
| return NULL; |
| |
| /* |
| * Only one of the source or destination port can be flagged as a |
| * wildcard. When filling it in, fill in a copy of the matched entry |
| * if it has the cloning flag set. |
| */ |
| if ((fin->fin_flx & FI_IGNORE) != 0) { |
| fin->fin_rev = rev; |
| return is; |
| } |
| |
| if ((flags & (SI_W_SPORT|SI_W_DPORT))) { |
| if ((flags & SI_CLONE) != 0) { |
| ipstate_t *clone; |
| |
| clone = fr_stclone(fin, tcp, is); |
| if (clone == NULL) |
| return NULL; |
| is = clone; |
| } else { |
| ATOMIC_DECL(ifs->ifs_ips_stats.iss_wild); |
| } |
| |
| if ((flags & SI_W_SPORT) != 0) { |
| if (rev == 0) { |
| is->is_sport = sp; |
| is->is_send = ntohl(tcp->th_seq); |
| } else { |
| is->is_sport = dp; |
| is->is_send = ntohl(tcp->th_ack); |
| } |
| is->is_maxsend = is->is_send + 1; |
| } else if ((flags & SI_W_DPORT) != 0) { |
| if (rev == 0) { |
| is->is_dport = dp; |
| is->is_dend = ntohl(tcp->th_ack); |
| } else { |
| is->is_dport = sp; |
| is->is_dend = ntohl(tcp->th_seq); |
| } |
| is->is_maxdend = is->is_dend + 1; |
| } |
| is->is_flags &= ~(SI_W_SPORT|SI_W_DPORT); |
| if ((flags & SI_CLONED) && ifs->ifs_ipstate_logging) |
| ipstate_log(is, ISL_CLONE, ifs); |
| } |
| |
| ret = -1; |
| |
| if (is->is_flx[out][rev] == 0) { |
| is->is_flx[out][rev] = flx; |
| is->is_opt[rev] = fin->fin_optmsk; |
| if (is->is_v == 6) { |
| is->is_opt[rev] &= ~0x8; |
| is->is_optmsk[rev] &= ~0x8; |
| } |
| } |
| |
| /* |
| * Check if the interface name for this "direction" is set and if not, |
| * fill it in. |
| */ |
| if (is->is_ifp[idx] == NULL && |
| (*is->is_ifname[idx] == '\0' || *is->is_ifname[idx] == '*')) { |
| is->is_ifp[idx] = ifp; |
| COPYIFNAME(ifp, is->is_ifname[idx], fin->fin_v); |
| } |
| fin->fin_rev = rev; |
| return is; |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_checkicmpmatchingstate */ |
| /* Returns: Nil */ |
| /* Parameters: fin(I) - pointer to packet information */ |
| /* */ |
| /* If we've got an ICMP error message, using the information stored in the */ |
| /* ICMP packet, look for a matching state table entry. */ |
| /* */ |
| /* If we return NULL then no lock on ipf_state is held. */ |
| /* If we return non-null then a read-lock on ipf_state is held. */ |
| /* ------------------------------------------------------------------------ */ |
| static ipstate_t *fr_checkicmpmatchingstate(fin) |
| fr_info_t *fin; |
| { |
| ipstate_t *is, **isp; |
| u_short sport, dport; |
| u_char pr; |
| int backward, i, oi; |
| i6addr_t dst, src; |
| struct icmp *ic; |
| u_short savelen; |
| icmphdr_t *icmp; |
| fr_info_t ofin; |
| tcphdr_t *tcp; |
| int len; |
| ip_t *oip; |
| u_int hv; |
| ipf_stack_t *ifs = fin->fin_ifs; |
| |
| /* |
| * Does it at least have the return (basic) IP header ? |
| * Is it an actual recognised ICMP error type? |
| * Only a basic IP header (no options) should be with |
| * an ICMP error header. |
| */ |
| if ((fin->fin_v != 4) || (fin->fin_hlen != sizeof(ip_t)) || |
| (fin->fin_plen < ICMPERR_MINPKTLEN) || |
| !(fin->fin_flx & FI_ICMPERR)) |
| return NULL; |
| ic = fin->fin_dp; |
| |
| oip = (ip_t *)((char *)ic + ICMPERR_ICMPHLEN); |
| /* |
| * Check if the at least the old IP header (with options) and |
| * 8 bytes of payload is present. |
| */ |
| if (fin->fin_plen < ICMPERR_MAXPKTLEN + ((IP_HL(oip) - 5) << 2)) |
| return NULL; |
| |
| /* |
| * Sanity Checks. |
| */ |
| len = fin->fin_dlen - ICMPERR_ICMPHLEN; |
| if ((len <= 0) || ((IP_HL(oip) << 2) > len)) |
| return NULL; |
| |
| /* |
| * Is the buffer big enough for all of it ? It's the size of the IP |
| * header claimed in the encapsulated part which is of concern. It |
| * may be too big to be in this buffer but not so big that it's |
| * outside the ICMP packet, leading to TCP deref's causing problems. |
| * This is possible because we don't know how big oip_hl is when we |
| * do the pullup early in fr_check() and thus can't guarantee it is |
| * all here now. |
| */ |
| #ifdef _KERNEL |
| { |
| mb_t *m; |
| |
| m = fin->fin_m; |
| # if defined(MENTAT) |
| if ((char *)oip + len > (char *)m->b_wptr) |
| return NULL; |
| # else |
| if ((char *)oip + len > (char *)fin->fin_ip + m->m_len) |
| return NULL; |
| # endif |
| } |
| #endif |
| bcopy((char *)fin, (char *)&ofin, sizeof(fin)); |
| |
| /* |
| * in the IPv4 case we must zero the i6addr union otherwise |
| * the IP6_EQ and IP6_NEQ macros produce the wrong results because |
| * of the 'junk' in the unused part of the union |
| */ |
| bzero((char *)&src, sizeof(src)); |
| bzero((char *)&dst, sizeof(dst)); |
| |
| /* |
| * we make an fin entry to be able to feed it to |
| * matchsrcdst note that not all fields are encessary |
| * but this is the cleanest way. Note further we fill |
| * in fin_mp such that if someone uses it we'll get |
| * a kernel panic. fr_matchsrcdst does not use this. |
| * |
| * watch out here, as ip is in host order and oip in network |
| * order. Any change we make must be undone afterwards, like |
| * oip->ip_off - it is still in network byte order so fix it. |
| */ |
| savelen = oip->ip_len; |
| oip->ip_len = len; |
| oip->ip_off = ntohs(oip->ip_off); |
| |
| ofin.fin_flx = FI_NOCKSUM; |
| ofin.fin_v = 4; |
| ofin.fin_ip = oip; |
| ofin.fin_m = NULL; /* if dereferenced, panic XXX */ |
| ofin.fin_mp = NULL; /* if dereferenced, panic XXX */ |
| ofin.fin_plen = fin->fin_dlen - ICMPERR_ICMPHLEN; |
| (void) fr_makefrip(IP_HL(oip) << 2, oip, &ofin); |
| ofin.fin_ifp = fin->fin_ifp; |
| ofin.fin_out = !fin->fin_out; |
| /* |
| * Reset the short and bad flag here because in fr_matchsrcdst() |
| * the flags for the current packet (fin_flx) are compared against |
| * those for the existing session. |
| */ |
| ofin.fin_flx &= ~(FI_BAD|FI_SHORT); |
| |
| /* |
| * Put old values of ip_len and ip_off back as we don't know |
| * if we have to forward the packet (or process it again. |
| */ |
| oip->ip_len = savelen; |
| oip->ip_off = htons(oip->ip_off); |
| |
| switch (oip->ip_p) |
| { |
| case IPPROTO_ICMP : |
| /* |
| * an ICMP error can only be generated as a result of an |
| * ICMP query, not as the response on an ICMP error |
| * |
| * XXX theoretically ICMP_ECHOREP and the other reply's are |
| * ICMP query's as well, but adding them here seems strange XXX |
| */ |
| if ((ofin.fin_flx & FI_ICMPERR) != 0) |
| return NULL; |
| |
| /* |
| * perform a lookup of the ICMP packet in the state table |
| */ |
| icmp = (icmphdr_t *)((char *)oip + (IP_HL(oip) << 2)); |
| hv = (pr = oip->ip_p); |
| src.in4 = oip->ip_src; |
| hv += src.in4.s_addr; |
| dst.in4 = oip->ip_dst; |
| hv += dst.in4.s_addr; |
| hv += icmp->icmp_id; |
| hv = DOUBLE_HASH(hv, ifs); |
| |
| READ_ENTER(&ifs->ifs_ipf_state); |
| for (isp = &ifs->ifs_ips_table[hv]; ((is = *isp) != NULL); ) { |
| isp = &is->is_hnext; |
| if ((is->is_p != pr) || (is->is_v != 4)) |
| continue; |
| if (is->is_pass & FR_NOICMPERR) |
| continue; |
| is = fr_matchsrcdst(&ofin, is, &src, &dst, |
| NULL, FI_ICMPCMP); |
| if (is != NULL) { |
| if ((is->is_pass & FR_NOICMPERR) != 0) { |
| RWLOCK_EXIT(&ifs->ifs_ipf_state); |
| return NULL; |
| } |
| /* |
| * i : the index of this packet (the icmp |
| * unreachable) |
| * oi : the index of the original packet found |
| * in the icmp header (i.e. the packet |
| * causing this icmp) |
| * backward : original packet was backward |
| * compared to the state |
| */ |
| backward = IP6_NEQ(&is->is_src, &src); |
| fin->fin_rev = !backward; |
| i = (!backward << 1) + fin->fin_out; |
| oi = (backward << 1) + ofin.fin_out; |
| if (is->is_icmppkts[i] > is->is_pkts[oi]) |
| continue; |
| ifs->ifs_ips_stats.iss_hits++; |
| is->is_icmppkts[i]++; |
| return is; |
| } |
| } |
| RWLOCK_EXIT(&ifs->ifs_ipf_state); |
| return NULL; |
| case IPPROTO_TCP : |
| case IPPROTO_UDP : |
| break; |
| default : |
| return NULL; |
| } |
| |
| tcp = (tcphdr_t *)((char *)oip + (IP_HL(oip) << 2)); |
| dport = tcp->th_dport; |
| sport = tcp->th_sport; |
| |
| hv = (pr = oip->ip_p); |
| src.in4 = oip->ip_src; |
| hv += src.in4.s_addr; |
| dst.in4 = oip->ip_dst; |
| hv += dst.in4.s_addr; |
| hv += dport; |
| hv += sport; |
| hv = DOUBLE_HASH(hv, ifs); |
| |
| READ_ENTER(&ifs->ifs_ipf_state); |
| for (isp = &ifs->ifs_ips_table[hv]; ((is = *isp) != NULL); ) { |
| isp = &is->is_hnext; |
| /* |
| * Only allow this icmp though if the |
| * encapsulated packet was allowed through the |
| * other way around. Note that the minimal amount |
| * of info present does not allow for checking against |
| * tcp internals such as seq and ack numbers. Only the |
| * ports are known to be present and can be even if the |
| * short flag is set. |
| */ |
| if ((is->is_p == pr) && (is->is_v == 4) && |
| (is = fr_matchsrcdst(&ofin, is, &src, &dst, |
| tcp, FI_ICMPCMP))) { |
| /* |
| * i : the index of this packet (the icmp unreachable) |
| * oi : the index of the original packet found in the |
| * icmp header (i.e. the packet causing this icmp) |
| * backward : original packet was backward compared to |
| * the state |
| */ |
| backward = IP6_NEQ(&is->is_src, &src); |
| fin->fin_rev = !backward; |
| i = (!backward << 1) + fin->fin_out; |
| oi = (backward << 1) + ofin.fin_out; |
| |
| if (((is->is_pass & FR_NOICMPERR) != 0) || |
| (is->is_icmppkts[i] > is->is_pkts[oi])) |
| break; |
| ifs->ifs_ips_stats.iss_hits++; |
| is->is_icmppkts[i]++; |
| /* |
| * we deliberately do not touch the timeouts |
| * for the accompanying state table entry. |
| * It remains to be seen if that is correct. XXX |
| */ |
| return is; |
| } |
| } |
| RWLOCK_EXIT(&ifs->ifs_ipf_state); |
| return NULL; |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_ipsmove */ |
| /* Returns: Nil */ |
| /* Parameters: is(I) - pointer to state table entry */ |
| /* hv(I) - new hash value for state table entry */ |
| /* Write Locks: ipf_state */ |
| /* */ |
| /* Move a state entry from one position in the hash table to another. */ |
| /* ------------------------------------------------------------------------ */ |
| static void fr_ipsmove(is, hv, ifs) |
| ipstate_t *is; |
| u_int hv; |
| ipf_stack_t *ifs; |
| { |
| ipstate_t **isp; |
| u_int hvm; |
| |
| ASSERT(rw_read_locked(&ifs->ifs_ipf_state.ipf_lk) == 0); |
| |
| hvm = is->is_hv; |
| /* |
| * Remove the hash from the old location... |
| */ |
| isp = is->is_phnext; |
| if (is->is_hnext) |
| is->is_hnext->is_phnext = isp; |
| *isp = is->is_hnext; |
| if (ifs->ifs_ips_table[hvm] == NULL) |
| ifs->ifs_ips_stats.iss_inuse--; |
| ifs->ifs_ips_stats.iss_bucketlen[hvm]--; |
| |
| /* |
| * ...and put the hash in the new one. |
| */ |
| hvm = DOUBLE_HASH(hv, ifs); |
| is->is_hv = hvm; |
| isp = &ifs->ifs_ips_table[hvm]; |
| if (*isp) |
| (*isp)->is_phnext = &is->is_hnext; |
| else |
| ifs->ifs_ips_stats.iss_inuse++; |
| ifs->ifs_ips_stats.iss_bucketlen[hvm]++; |
| is->is_phnext = isp; |
| is->is_hnext = *isp; |
| *isp = is; |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_stlookup */ |
| /* Returns: ipstate_t* - NULL == no matching state found, */ |
| /* else pointer to state information is returned */ |
| /* Parameters: fin(I) - pointer to packet information */ |
| /* tcp(I) - pointer to TCP/UDP header. */ |
| /* */ |
| /* Search the state table for a matching entry to the packet described by */ |
| /* the contents of *fin. */ |
| /* */ |
| /* If we return NULL then no lock on ipf_state is held. */ |
| /* If we return non-null then a read-lock on ipf_state is held. */ |
| /* ------------------------------------------------------------------------ */ |
| ipstate_t *fr_stlookup(fin, tcp, ifqp) |
| fr_info_t *fin; |
| tcphdr_t *tcp; |
| ipftq_t **ifqp; |
| { |
| u_int hv, hvm, pr, v, tryagain; |
| ipstate_t *is, **isp; |
| u_short dport, sport; |
| i6addr_t src, dst; |
| struct icmp *ic; |
| ipftq_t *ifq; |
| int oow; |
| ipf_stack_t *ifs = fin->fin_ifs; |
| |
| is = NULL; |
| ifq = NULL; |
| tcp = fin->fin_dp; |
| ic = (struct icmp *)tcp; |
| hv = (pr = fin->fin_fi.fi_p); |
| src = fin->fin_fi.fi_src; |
| dst = fin->fin_fi.fi_dst; |
| hv += src.in4.s_addr; |
| hv += dst.in4.s_addr; |
| |
| v = fin->fin_fi.fi_v; |
| #ifdef USE_INET6 |
| if (v == 6) { |
| hv += fin->fin_fi.fi_src.i6[1]; |
| hv += fin->fin_fi.fi_src.i6[2]; |
| hv += fin->fin_fi.fi_src.i6[3]; |
| |
| if ((fin->fin_p == IPPROTO_ICMPV6) && |
| IN6_IS_ADDR_MULTICAST(&fin->fin_fi.fi_dst.in6)) { |
| hv -= dst.in4.s_addr; |
| } else { |
| hv += fin->fin_fi.fi_dst.i6[1]; |
| hv += fin->fin_fi.fi_dst.i6[2]; |
| hv += fin->fin_fi.fi_dst.i6[3]; |
| } |
| } |
| #endif |
| |
| /* |
| * Search the hash table for matching packet header info. |
| */ |
| switch (pr) |
| { |
| #ifdef USE_INET6 |
| case IPPROTO_ICMPV6 : |
| tryagain = 0; |
| if (v == 6) { |
| if ((ic->icmp_type == ICMP6_ECHO_REQUEST) || |
| (ic->icmp_type == ICMP6_ECHO_REPLY)) { |
| hv += ic->icmp_id; |
| } |
| } |
| READ_ENTER(&ifs->ifs_ipf_state); |
| icmp6again: |
| hvm = DOUBLE_HASH(hv, ifs); |
| for (isp = &ifs->ifs_ips_table[hvm]; ((is = *isp) != NULL); ) { |
| isp = &is->is_hnext; |
| if ((is->is_p != pr) || (is->is_v != v)) |
| continue; |
| is = fr_matchsrcdst(fin, is, &src, &dst, NULL, FI_CMP); |
| if (is != NULL && |
| fr_matchicmpqueryreply(v, &is->is_icmp, |
| ic, fin->fin_rev)) { |
| if (fin->fin_rev) |
| ifq = &ifs->ifs_ips_icmpacktq; |
| else |
| ifq = &ifs->ifs_ips_icmptq; |
| break; |
| } |
| } |
| |
| if (is != NULL) { |
| if ((tryagain != 0) && !(is->is_flags & SI_W_DADDR)) { |
| hv += fin->fin_fi.fi_src.i6[0]; |
| hv += fin->fin_fi.fi_src.i6[1]; |
| hv += fin->fin_fi.fi_src.i6[2]; |
| hv += fin->fin_fi.fi_src.i6[3]; |
| fr_ipsmove(is, hv, ifs); |
| MUTEX_DOWNGRADE(&ifs->ifs_ipf_state); |
| } |
| break; |
| } |
| RWLOCK_EXIT(&ifs->ifs_ipf_state); |
| |
| /* |
| * No matching icmp state entry. Perhaps this is a |
| * response to another state entry. |
| * |
| * XXX With some ICMP6 packets, the "other" address is already |
| * in the packet, after the ICMP6 header, and this could be |
| * used in place of the multicast address. However, taking |
| * advantage of this requires some significant code changes |
| * to handle the specific types where that is the case. |
| */ |
| if ((ifs->ifs_ips_stats.iss_wild != 0) && (v == 6) && (tryagain == 0) && |
| !IN6_IS_ADDR_MULTICAST(&fin->fin_fi.fi_src.in6)) { |
| hv -= fin->fin_fi.fi_src.i6[0]; |
| hv -= fin->fin_fi.fi_src.i6[1]; |
| hv -= fin->fin_fi.fi_src.i6[2]; |
| hv -= fin->fin_fi.fi_src.i6[3]; |
| tryagain = 1; |
| WRITE_ENTER(&ifs->ifs_ipf_state); |
| goto icmp6again; |
| } |
| |
| is = fr_checkicmp6matchingstate(fin); |
| if (is != NULL) |
| return is; |
| break; |
| #endif |
| |
| case IPPROTO_ICMP : |
| if (v == 4) { |
| hv += ic->icmp_id; |
| } |
| hv = DOUBLE_HASH(hv, ifs); |
| READ_ENTER(&ifs->ifs_ipf_state); |
| for (isp = &ifs->ifs_ips_table[hv]; ((is = *isp) != NULL); ) { |
| isp = &is->is_hnext; |
| if ((is->is_p != pr) || (is->is_v != v)) |
| continue; |
| is = fr_matchsrcdst(fin, is, &src, &dst, NULL, FI_CMP); |
| if (is != NULL && |
| fr_matchicmpqueryreply(v, &is->is_icmp, |
| ic, fin->fin_rev)) { |
| if (fin->fin_rev) |
| ifq = &ifs->ifs_ips_icmpacktq; |
| else |
| ifq = &ifs->ifs_ips_icmptq; |
| break; |
| } |
| } |
| if (is == NULL) { |
| RWLOCK_EXIT(&ifs->ifs_ipf_state); |
| } |
| break; |
| |
| case IPPROTO_TCP : |
| case IPPROTO_UDP : |
| ifqp = NULL; |
| sport = htons(fin->fin_data[0]); |
| hv += sport; |
| dport = htons(fin->fin_data[1]); |
| hv += dport; |
| oow = 0; |
| tryagain = 0; |
| READ_ENTER(&ifs->ifs_ipf_state); |
| retry_tcpudp: |
| hvm = DOUBLE_HASH(hv, ifs); |
| for (isp = &ifs->ifs_ips_table[hvm]; ((is = *isp) != NULL); ) { |
| isp = &is->is_hnext; |
| if ((is->is_p != pr) || (is->is_v != v)) |
| continue; |
| fin->fin_flx &= ~FI_OOW; |
| is = fr_matchsrcdst(fin, is, &src, &dst, tcp, FI_CMP); |
| if (is != NULL) { |
| if (pr == IPPROTO_TCP) { |
| if (!fr_tcpstate(fin, tcp, is)) { |
| oow |= fin->fin_flx & FI_OOW; |
| continue; |
| } |
| } |
| break; |
| } |
| } |
| if (is != NULL) { |
| if (tryagain && |
| !(is->is_flags & (SI_CLONE|SI_WILDP|SI_WILDA))) { |
| hv += dport; |
| hv += sport; |
| fr_ipsmove(is, hv, ifs); |
| MUTEX_DOWNGRADE(&ifs->ifs_ipf_state); |
| } |
| break; |
| } |
| RWLOCK_EXIT(&ifs->ifs_ipf_state); |
| |
| if (!tryagain && ifs->ifs_ips_stats.iss_wild) { |
| hv -= dport; |
| hv -= sport; |
| tryagain = 1; |
| WRITE_ENTER(&ifs->ifs_ipf_state); |
| goto retry_tcpudp; |
| } |
| fin->fin_flx |= oow; |
| break; |
| |
| #if 0 |
| case IPPROTO_GRE : |
| gre = fin->fin_dp; |
| if (GRE_REV(gre->gr_flags) == 1) { |
| hv += gre->gr_call; |
| } |
| /* FALLTHROUGH */ |
| #endif |
| default : |
| ifqp = NULL; |
| hvm = DOUBLE_HASH(hv, ifs); |
| READ_ENTER(&ifs->ifs_ipf_state); |
| for (isp = &ifs->ifs_ips_table[hvm]; ((is = *isp) != NULL); ) { |
| isp = &is->is_hnext; |
| if ((is->is_p != pr) || (is->is_v != v)) |
| continue; |
| is = fr_matchsrcdst(fin, is, &src, &dst, NULL, FI_CMP); |
| if (is != NULL) { |
| ifq = &ifs->ifs_ips_iptq; |
| break; |
| } |
| } |
| if (is == NULL) { |
| RWLOCK_EXIT(&ifs->ifs_ipf_state); |
| } |
| break; |
| } |
| |
| if ((is != NULL) && ((is->is_sti.tqe_flags & TQE_RULEBASED) != 0) && |
| (is->is_tqehead[fin->fin_rev] != NULL)) |
| ifq = is->is_tqehead[fin->fin_rev]; |
| if (ifq != NULL && ifqp != NULL) |
| *ifqp = ifq; |
| return is; |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_updatestate */ |
| /* Returns: Nil */ |
| /* Parameters: fin(I) - pointer to packet information */ |
| /* is(I) - pointer to state table entry */ |
| /* Read Locks: ipf_state */ |
| /* */ |
| /* Updates packet and byte counters for a newly received packet. Seeds the */ |
| /* fragment cache with a new entry as required. */ |
| /* ------------------------------------------------------------------------ */ |
| void fr_updatestate(fin, is, ifq) |
| fr_info_t *fin; |
| ipstate_t *is; |
| ipftq_t *ifq; |
| { |
| ipftqent_t *tqe; |
| int i, pass; |
| ipf_stack_t *ifs = fin->fin_ifs; |
| |
| i = (fin->fin_rev << 1) + fin->fin_out; |
| |
| /* |
| * For TCP packets, ifq == NULL. For all others, check if this new |
| * queue is different to the last one it was on and move it if so. |
| */ |
| tqe = &is->is_sti; |
| MUTEX_ENTER(&is->is_lock); |
| if ((tqe->tqe_flags & TQE_RULEBASED) != 0) |
| ifq = is->is_tqehead[fin->fin_rev]; |
| |
| if (ifq != NULL) |
| fr_movequeue(tqe, tqe->tqe_ifq, ifq, ifs); |
| |
| is->is_pkts[i]++; |
| is->is_bytes[i] += fin->fin_plen; |
| MUTEX_EXIT(&is->is_lock); |
| |
| #ifdef IPFILTER_SYNC |
| if (is->is_flags & IS_STATESYNC) |
| ipfsync_update(SMC_STATE, fin, is->is_sync); |
| #endif |
| |
| ATOMIC_INCL(ifs->ifs_ips_stats.iss_hits); |
| |
| fin->fin_fr = is->is_rule; |
| |
| /* |
| * If this packet is a fragment and the rule says to track fragments, |
| * then create a new fragment cache entry. |
| */ |
| pass = is->is_pass; |
| if ((fin->fin_flx & FI_FRAG) && FR_ISPASS(pass)) |
| (void) fr_newfrag(fin, pass ^ FR_KEEPSTATE); |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_checkstate */ |
| /* Returns: frentry_t* - NULL == search failed, */ |
| /* else pointer to rule for matching state */ |
| /* Parameters: ifp(I) - pointer to interface */ |
| /* passp(I) - pointer to filtering result flags */ |
| /* */ |
| /* Check if a packet is associated with an entry in the state table. */ |
| /* ------------------------------------------------------------------------ */ |
| frentry_t *fr_checkstate(fin, passp) |
| fr_info_t *fin; |
| u_32_t *passp; |
| { |
| ipstate_t *is; |
| frentry_t *fr; |
| tcphdr_t *tcp; |
| ipftq_t *ifq; |
| u_int pass; |
| ipf_stack_t *ifs = fin->fin_ifs; |
| |
| if (ifs->ifs_fr_state_lock || (ifs->ifs_ips_list == NULL) || |
| (fin->fin_flx & (FI_SHORT|FI_STATE|FI_FRAGBODY|FI_BAD))) |
| return NULL; |
| |
| is = NULL; |
| if ((fin->fin_flx & FI_TCPUDP) || |
| (fin->fin_fi.fi_p == IPPROTO_ICMP) |
| #ifdef USE_INET6 |
| || (fin->fin_fi.fi_p == IPPROTO_ICMPV6) |
| #endif |
| ) |
| tcp = fin->fin_dp; |
| else |
| tcp = NULL; |
| |
| /* |
| * Search the hash table for matching packet header info. |
| */ |
| ifq = NULL; |
| is = fin->fin_state; |
| if (is == NULL) |
| is = fr_stlookup(fin, tcp, &ifq); |
| switch (fin->fin_p) |
| { |
| #ifdef USE_INET6 |
| case IPPROTO_ICMPV6 : |
| if (is != NULL) |
| break; |
| if (fin->fin_v == 6) { |
| is = fr_checkicmp6matchingstate(fin); |
| if (is != NULL) |
| goto matched; |
| } |
| break; |
| #endif |
| case IPPROTO_ICMP : |
| if (is != NULL) |
| break; |
| /* |
| * No matching icmp state entry. Perhaps this is a |
| * response to another state entry. |
| */ |
| is = fr_checkicmpmatchingstate(fin); |
| if (is != NULL) |
| goto matched; |
| break; |
| case IPPROTO_TCP : |
| if (is == NULL) |
| break; |
| |
| if (is->is_pass & FR_NEWISN) { |
| if (fin->fin_out == 0) |
| fr_fixinisn(fin, is); |
| else if (fin->fin_out == 1) |
| fr_fixoutisn(fin, is); |
| } |
| break; |
| default : |
| if (fin->fin_rev) |
| ifq = &ifs->ifs_ips_udpacktq; |
| else |
| ifq = &ifs->ifs_ips_udptq; |
| break; |
| } |
| if (is == NULL) { |
| ATOMIC_INCL(ifs->ifs_ips_stats.iss_miss); |
| return NULL; |
| } |
| |
| matched: |
| fr = is->is_rule; |
| if (fr != NULL) { |
| if ((fin->fin_out == 0) && (fr->fr_nattag.ipt_num[0] != 0)) { |
| if (fin->fin_nattag == NULL) |
| return NULL; |
| if (fr_matchtag(&fr->fr_nattag, fin->fin_nattag) != 0) |
| return NULL; |
| } |
| (void) strncpy(fin->fin_group, fr->fr_group, FR_GROUPLEN); |
| fin->fin_icode = fr->fr_icode; |
| } |
| |
| fin->fin_rule = is->is_rulen; |
| pass = is->is_pass; |
| fr_updatestate(fin, is, ifq); |
| if (fin->fin_out == 1) |
| fin->fin_nat = is->is_nat[fin->fin_rev]; |
| |
| fin->fin_state = is; |
| is->is_touched = ifs->ifs_fr_ticks; |
| MUTEX_ENTER(&is->is_lock); |
| is->is_ref++; |
| MUTEX_EXIT(&is->is_lock); |
| RWLOCK_EXIT(&ifs->ifs_ipf_state); |
| fin->fin_flx |= FI_STATE; |
| if ((pass & FR_LOGFIRST) != 0) |
| pass &= ~(FR_LOGFIRST|FR_LOG); |
| *passp = pass; |
| return fr; |
| } |
| |
| |
| /* ------------------------------------------------------------------------ */ |
| /* Function: fr_fixoutisn */ |
| /* Returns: Nil */ |
| /* Parameters: fin(I) - pointer to packet information */ |
| /* is(I) - pointer to master state structure */ |
| /* */ |
| /* Called only for outbound packets, adjusts the sequence number and the */ |
| /* TCP checksum to match that change. */ |
| /* ------------------------------------------------------------------------ */ |
| static void fr_fixoutisn(fin, is) |
| fr_info_t *fin; |
| ipstate_t *is; |
| { |
| tcphdr_t *tcp; |
| int rev; |
| u_32_t seq; |
| |
| tcp = fin->fin_dp; |
| rev = fin->fin_rev; |
| if ((is->is_flags & IS_ISNSYN |