NetBSD/sys/netinet/ip_state.c

817 lines
18 KiB
C
Raw Normal View History

1998-11-22 18:17:18 +03:00
/* $NetBSD: ip_state.c,v 1.16 1998/11/22 15:17:20 mrg Exp $ */
/*
1998-11-22 18:17:18 +03:00
* Copyright (C) 1995-1998 by Darren Reed.
*
* Redistribution and use in source and binary forms are permitted
* provided that this notice is preserved and due credit is given
* to the original author and the contributors.
*/
#if !defined(lint)
static const char sccsid[] = "@(#)ip_state.c 1.8 6/5/96 (C) 1993-1995 Darren Reed";
1998-11-22 18:17:18 +03:00
static const char rcsid[] = "@(#)Id: ip_state.c,v 2.0.2.24.2.25 1998/11/22 01:50:31 darrenr Exp ";
#endif
1998-11-22 18:17:18 +03:00
#include <sys/errno.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/file.h>
1997-11-14 15:40:06 +03:00
#if !defined(_KERNEL) && !defined(KERNEL) && !defined(__KERNEL__)
1998-11-22 18:17:18 +03:00
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
1997-11-14 15:40:06 +03:00
#else
# ifdef linux
# include <linux/kernel.h>
# include <linux/module.h>
# endif
#endif
1997-05-25 16:40:11 +04:00
#if defined(KERNEL) && (__FreeBSD_version >= 220000)
# include <sys/filio.h>
# include <sys/fcntl.h>
#else
# include <sys/ioctl.h>
#endif
1997-11-14 15:40:06 +03:00
#include <sys/time.h>
#include <sys/uio.h>
1997-11-14 15:40:06 +03:00
#ifndef linux
#include <sys/protosw.h>
1997-11-14 15:40:06 +03:00
#endif
#include <sys/socket.h>
1997-11-14 15:40:06 +03:00
#if defined(_KERNEL) && !defined(linux)
# include <sys/systm.h>
#endif
#if !defined(__SVR4) && !defined(__svr4__)
1997-11-14 15:40:06 +03:00
# ifndef linux
# include <sys/mbuf.h>
# endif
#else
1997-05-25 16:40:11 +04:00
# include <sys/filio.h>
# include <sys/byteorder.h>
# include <sys/dditypes.h>
# 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>
1997-11-14 15:40:06 +03:00
#ifndef linux
# include <netinet/ip_var.h>
# include <netinet/tcp_fsm.h>
#endif
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
1997-05-25 16:40:11 +04:00
#include "netinet/ip_compat.h"
1997-11-14 15:40:06 +03:00
#include <netinet/tcpip.h>
1997-05-25 16:40:11 +04:00
#include "netinet/ip_fil.h"
#include "netinet/ip_nat.h"
#include "netinet/ip_frag.h"
1997-07-05 09:38:14 +04:00
#include "netinet/ip_proxy.h"
1997-05-25 16:40:11 +04:00
#include "netinet/ip_state.h"
#ifndef MIN
#define MIN(a,b) (((a)<(b))?(a):(b))
#endif
#define TCP_CLOSE (TH_FIN|TH_RST)
ipstate_t *ips_table[IPSTATE_SIZE];
int ips_num = 0;
ips_stat_t ips_stats;
#if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
1998-11-22 18:17:18 +03:00
extern KRWLOCK_T ipf_state;
1998-07-12 19:23:59 +04:00
extern kmutex_t ipf_rw;
#endif
static int fr_matchsrcdst __P((ipstate_t *, struct in_addr, struct in_addr,
fr_info_t *, void *, u_short, u_short));
static int fr_state_flush __P((int));
static ips_stat_t *fr_statetstats __P((void));
#define FIVE_DAYS (2 * 5 * 86400) /* 5 days: half closed session */
u_long fr_tcpidletimeout = FIVE_DAYS,
fr_tcpclosewait = 60,
fr_tcplastack = 20,
fr_tcptimeout = 120,
fr_tcpclosed = 1,
fr_udptimeout = 120,
fr_icmptimeout = 120;
static ips_stat_t *fr_statetstats()
{
ips_stats.iss_active = ips_num;
ips_stats.iss_table = ips_table;
return &ips_stats;
}
1997-11-14 15:40:06 +03:00
/*
* flush state tables. two actions currently defined:
* which == 0 : flush all state table entries
* which == 1 : flush TCP connections which have started to close but are
* stuck for some reason.
*/
static int fr_state_flush(which)
1997-11-14 15:40:06 +03:00
int which;
{
register int i;
register ipstate_t *is, **isp;
#if defined(_KERNEL) && !SOLARIS
int s;
#endif
int delete, removed = 0;
SPL_NET(s);
1998-07-12 19:23:59 +04:00
WRITE_ENTER(&ipf_state);
1997-11-14 15:40:06 +03:00
for (i = 0; i < IPSTATE_SIZE; i++)
for (isp = &ips_table[i]; (is = *isp); ) {
delete = 0;
switch (which)
{
case 0 :
delete = 1;
break;
case 1 :
if ((is->is_p == IPPROTO_TCP) &&
(((is->is_state[0] <= TCPS_ESTABLISHED) &&
1998-05-17 20:50:15 +04:00
(is->is_state[1] > TCPS_ESTABLISHED)) ||
((is->is_state[1] <= TCPS_ESTABLISHED) &&
(is->is_state[0] > TCPS_ESTABLISHED))))
1997-11-14 15:40:06 +03:00
delete = 1;
break;
}
if (delete) {
*isp = is->is_next;
if (is->is_p == IPPROTO_TCP)
ips_stats.iss_fin++;
else
ips_stats.iss_expire++;
#ifdef IPFILTER_LOG
ipstate_log(is, ISL_FLUSH);
#endif
KFREE(is);
ips_num--;
removed++;
} else
isp = &is->is_next;
}
1998-07-12 19:23:59 +04:00
RWLOCK_EXIT(&ipf_state);
1997-11-14 15:40:06 +03:00
SPL_X(s);
return removed;
}
1997-05-25 16:40:11 +04:00
int fr_state_ioctl(data, cmd, mode)
caddr_t data;
#if defined(__NetBSD__) || defined(__OpenBSD__)
1997-07-05 09:38:14 +04:00
u_long cmd;
#else
1997-05-25 16:40:11 +04:00
int cmd;
1997-07-05 09:38:14 +04:00
#endif
1997-05-25 16:40:11 +04:00
int mode;
{
1997-11-14 15:40:06 +03:00
int arg, ret, error = 0;
1997-05-25 16:40:11 +04:00
switch (cmd)
{
1997-11-14 15:40:06 +03:00
case SIOCIPFFL :
IRCOPY(data, (caddr_t)&arg, sizeof(arg));
if (arg == 0 || arg == 1) {
ret = fr_state_flush(arg);
IWCOPY((caddr_t)&ret, data, sizeof(ret));
} else
error = EINVAL;
break;
1997-05-25 16:40:11 +04:00
case SIOCGIPST :
IWCOPY((caddr_t)fr_statetstats(), data, sizeof(ips_stat_t));
break;
case FIONREAD :
#ifdef IPFILTER_LOG
IWCOPY((caddr_t)&iplused[IPL_LOGSTATE], (caddr_t)data,
sizeof(iplused[IPL_LOGSTATE]));
1997-05-25 16:40:11 +04:00
#endif
break;
default :
1997-11-14 15:40:06 +03:00
return EINVAL;
1997-05-25 16:40:11 +04:00
}
1997-11-14 15:40:06 +03:00
return error;
1997-05-25 16:40:11 +04:00
}
/*
* Create a new ipstate structure and hang it off the hash table.
*/
int fr_addstate(ip, fin, pass)
ip_t *ip;
fr_info_t *fin;
u_int pass;
{
ipstate_t ips;
register ipstate_t *is = &ips;
register u_int hv;
1998-11-22 18:17:18 +03:00
if ((ip->ip_off & IP_OFFMASK) || (fin->fin_fi.fi_fl & FI_SHORT))
return -1;
if (ips_num == IPSTATE_MAX) {
ips_stats.iss_max++;
return -1;
}
ips.is_age = 1;
ips.is_state[0] = 0;
ips.is_state[1] = 0;
/*
* Copy and calculate...
*/
hv = (is->is_p = ip->ip_p);
hv += (is->is_src.s_addr = ip->ip_src.s_addr);
hv += (is->is_dst.s_addr = ip->ip_dst.s_addr);
switch (ip->ip_p)
{
case IPPROTO_ICMP :
{
struct icmp *ic = (struct icmp *)fin->fin_dp;
switch (ic->icmp_type)
{
case ICMP_ECHO :
is->is_icmp.ics_type = ICMP_ECHOREPLY; /* XXX */
hv += (is->is_icmp.ics_id = ic->icmp_id);
hv += (is->is_icmp.ics_seq = ic->icmp_seq);
break;
case ICMP_TSTAMP :
case ICMP_IREQ :
case ICMP_MASKREQ :
is->is_icmp.ics_type = ic->icmp_type + 1;
break;
default :
return -1;
}
1998-07-12 19:23:59 +04:00
ATOMIC_INC(ips_stats.iss_icmp);
is->is_age = fr_icmptimeout;
break;
}
case IPPROTO_TCP :
{
register tcphdr_t *tcp = (tcphdr_t *)fin->fin_dp;
/*
* The endian of the ports doesn't matter, but the ack and
* sequence numbers do as we do mathematics on them later.
*/
hv += (is->is_dport = tcp->th_dport);
hv += (is->is_sport = tcp->th_sport);
is->is_seq = ntohl(tcp->th_seq);
is->is_ack = ntohl(tcp->th_ack);
is->is_swin = ntohs(tcp->th_win);
is->is_dwin = is->is_swin; /* start them the same */
/*
1997-11-14 15:40:06 +03:00
* 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.
*/
if ((tcp->th_flags & (TH_SYN|TH_ACK)) == TH_SYN)
is->is_ack = 0; /* Trumpet WinSock 'ism */
1998-07-12 19:23:59 +04:00
MUTEX_ENTER(&ipf_rw);
ips_stats.iss_tcp++;
fr_tcp_age(&is->is_age, is->is_state, ip, fin,
tcp->th_sport == is->is_sport);
1998-07-12 19:23:59 +04:00
MUTEX_EXIT(&ipf_rw);
break;
}
case IPPROTO_UDP :
{
register tcphdr_t *tcp = (tcphdr_t *)fin->fin_dp;
hv += (is->is_dport = tcp->th_dport);
hv += (is->is_sport = tcp->th_sport);
1998-07-12 19:23:59 +04:00
ATOMIC_INC(ips_stats.iss_udp);
is->is_age = fr_udptimeout;
break;
}
default :
return -1;
}
KMALLOC(is, ipstate_t *, sizeof(*is));
if (is == NULL) {
1998-07-12 19:23:59 +04:00
ATOMIC_INC(ips_stats.iss_nomem);
return -1;
}
bcopy((char *)&ips, (char *)is, sizeof(*is));
hv %= IPSTATE_SIZE;
1998-07-12 19:23:59 +04:00
WRITE_ENTER(&ipf_state);
is->is_pass = pass;
is->is_pkts = 1;
is->is_bytes = ip->ip_len;
/*
1998-07-12 19:23:59 +04:00
* 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.
*/
1998-07-12 19:23:59 +04:00
is->is_opt = fin->fin_fi.fi_optmsk;
is->is_optmsk = 0xffffffff;
is->is_sec = fin->fin_fi.fi_secmsk;
is->is_secmsk = 0xffff;
is->is_auth = fin->fin_fi.fi_auth;
is->is_authmsk = 0xffff;
is->is_flags = fin->fin_fi.fi_fl;
is->is_flags |= FI_OPTIONS|FI_TCPUDP|FI_SHORT;
is->is_flags |= fin->fin_fi.fi_fl << 4;
/*
* add into table.
*/
is->is_next = ips_table[hv];
ips_table[hv] = is;
if (fin->fin_out) {
is->is_ifpin = NULL;
is->is_ifpout = fin->fin_ifp;
} else {
is->is_ifpin = fin->fin_ifp;
is->is_ifpout = NULL;
}
if (pass & FR_LOGFIRST)
is->is_pass &= ~(FR_LOGFIRST|FR_LOG);
1998-07-12 19:23:59 +04:00
ATOMIC_INC(ips_num);
1997-05-25 16:40:11 +04:00
#ifdef IPFILTER_LOG
ipstate_log(is, ISL_NEW);
#endif
1998-07-12 19:23:59 +04:00
RWLOCK_EXIT(&ipf_state);
1997-05-25 16:40:11 +04:00
if (fin->fin_fi.fi_fl & FI_FRAG)
ipfr_newfrag(ip, fin, pass ^ FR_KEEPSTATE);
return 0;
}
/*
* 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.
*/
int fr_tcpstate(is, fin, ip, tcp)
register ipstate_t *is;
fr_info_t *fin;
ip_t *ip;
tcphdr_t *tcp;
{
register int seqskew, ackskew;
register tcp_seq seq, ack;
1998-11-22 18:17:18 +03:00
u_short win;
int source;
/*
* Find difference between last checked packet and this packet.
*/
seq = ntohl(tcp->th_seq);
ack = ntohl(tcp->th_ack);
source = (ip->ip_src.s_addr == is->is_src.s_addr);
1997-07-05 09:38:14 +04:00
if (!(tcp->th_flags & TH_ACK)) /* Pretend an ack was sent */
ack = source ? is->is_ack : is->is_seq;
if (source) {
1997-11-14 15:40:06 +03:00
if (!is->is_seq)
/*
* Must be an outgoing SYN-ACK in reply to a SYN.
*/
is->is_seq = seq;
seqskew = seq - is->is_seq;
1998-11-22 18:17:18 +03:00
ackskew = (ack - 1) - is->is_ack;
} else {
if (!is->is_ack)
/*
* Must be a SYN-ACK in reply to a SYN.
*/
is->is_ack = seq;
ackskew = seq - is->is_ack;
1998-11-22 18:17:18 +03:00
seqskew = (ack - 1) - is->is_seq;
}
/*
* Make skew values absolute
*/
if (seqskew < 0)
seqskew = -seqskew;
if (ackskew < 0)
ackskew = -ackskew;
/*
* If the difference in sequence and ack numbers is within the
* window size of the connection, store these values and match
* the packet.
*/
1998-11-22 18:17:18 +03:00
win = ntohs(tcp->th_win);
if ((seqskew <= is->is_dwin) && (ackskew <= is->is_swin)) {
if (source) {
is->is_seq = seq;
is->is_ack = ack;
1998-11-22 18:17:18 +03:00
if (win != 0)
is->is_swin = win;
} else {
is->is_seq = ack;
is->is_ack = seq;
1998-11-22 18:17:18 +03:00
if (win != 0)
is->is_dwin = win;
}
1998-07-12 19:23:59 +04:00
ATOMIC_INC(ips_stats.iss_hits);
1997-05-25 16:40:11 +04:00
is->is_pkts++;
is->is_bytes += ip->ip_len;
/*
* Nearing end of connection, start timeout.
*/
1998-07-12 19:23:59 +04:00
MUTEX_ENTER(&ipf_rw);
fr_tcp_age(&is->is_age, is->is_state, ip, fin, source);
1998-07-12 19:23:59 +04:00
MUTEX_EXIT(&ipf_rw);
return 1;
}
return 0;
}
static int fr_matchsrcdst(is, src, dst, fin, tcp, sp, dp)
ipstate_t *is;
struct in_addr src, dst;
fr_info_t *fin;
void *tcp;
u_short sp, dp;
{
int ret = 0, rev, out;
void *ifp;
rev = (is->is_dst.s_addr != dst.s_addr);
ifp = fin->fin_ifp;
out = fin->fin_out;
if (!rev) {
if (out) {
if (!is->is_ifpout)
is->is_ifpout = ifp;
} else {
if (!is->is_ifpin)
is->is_ifpin = ifp;
}
} else {
if (out) {
if (!is->is_ifpin)
is->is_ifpin = ifp;
} else {
if (!is->is_ifpout)
is->is_ifpout = ifp;
}
}
if (!rev) {
if (((out && is->is_ifpout == ifp) ||
(!out && is->is_ifpin == ifp)) &&
(is->is_dst.s_addr == dst.s_addr) &&
(is->is_src.s_addr == src.s_addr) &&
(!tcp || ((sp == is->is_sport) &&
(dp == is->is_dport)))) {
ret = 1;
}
} else {
if (((out && is->is_ifpin == ifp) ||
(!out && is->is_ifpout == ifp)) &&
(is->is_dst.s_addr == src.s_addr) &&
(is->is_src.s_addr == dst.s_addr) &&
(!tcp || ((sp == is->is_dport) &&
(dp == is->is_sport)))) {
ret = 1;
}
}
/*
* Whether or not this should be here, is questionable, but the aim
* is to get this out of the main line.
*/
if (ret) {
if (((fin->fin_fi.fi_optmsk & is->is_optmsk) != is->is_opt) ||
((fin->fin_fi.fi_secmsk & is->is_secmsk) != is->is_sec) ||
((fin->fin_fi.fi_auth & is->is_authmsk) != is->is_auth) ||
1998-07-12 19:23:59 +04:00
((fin->fin_fi.fi_fl & (is->is_flags & 0xf)) !=
(is->is_flags >> 4)))
ret = 0;
}
return ret;
}
/*
* Check if a packet has a registered state.
*/
int fr_checkstate(ip, fin)
ip_t *ip;
fr_info_t *fin;
{
register struct in_addr dst, src;
register ipstate_t *is, **isp;
register u_char pr;
struct icmp *ic;
tcphdr_t *tcp;
u_int hv, hlen, pass;
1998-11-22 18:17:18 +03:00
if ((ip->ip_off & IP_OFFMASK) || (fin->fin_fi.fi_fl & FI_SHORT))
return 0;
hlen = fin->fin_hlen;
tcp = (tcphdr_t *)((char *)ip + hlen);
ic = (struct icmp *)tcp;
hv = (pr = ip->ip_p);
hv += (src.s_addr = ip->ip_src.s_addr);
hv += (dst.s_addr = ip->ip_dst.s_addr);
/*
* Search the hash table for matching packet header info.
*/
switch (ip->ip_p)
{
case IPPROTO_ICMP :
hv += ic->icmp_id;
hv += ic->icmp_seq;
hv %= IPSTATE_SIZE;
1998-07-12 19:23:59 +04:00
READ_ENTER(&ipf_state);
for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_next)
if ((is->is_p == pr) &&
(ic->icmp_id == is->is_icmp.ics_id) &&
(ic->icmp_seq == is->is_icmp.ics_seq) &&
fr_matchsrcdst(is, src, dst, fin, NULL, 0, 0)) {
if (is->is_icmp.ics_type != ic->icmp_type)
continue;
1998-07-12 19:23:59 +04:00
pass = is->is_pass;
RWLOCK_EXIT(&ipf_state);
if (fin->fin_fi.fi_fl & FI_FRAG)
ipfr_newfrag(ip, fin,
pass ^ FR_KEEPSTATE);
WRITE_ENTER(&ipf_state);
is->is_age = fr_icmptimeout;
is->is_bytes += ip->ip_len;
ips_stats.iss_hits++;
1998-07-12 19:23:59 +04:00
is->is_pkts++;
RWLOCK_EXIT(&ipf_state);
1997-05-25 16:40:11 +04:00
return pass;
}
1998-07-12 19:23:59 +04:00
RWLOCK_EXIT(&ipf_state);
break;
case IPPROTO_TCP :
{
register u_short dport = tcp->th_dport, sport = tcp->th_sport;
hv += dport;
hv += sport;
hv %= IPSTATE_SIZE;
1998-07-12 19:23:59 +04:00
WRITE_ENTER(&ipf_state);
for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_next)
if ((is->is_p == pr) &&
fr_matchsrcdst(is, src, dst, fin, tcp,
sport, dport)) {
if (fr_tcpstate(is, fin, ip, tcp)) {
1997-05-25 16:40:11 +04:00
pass = is->is_pass;
#ifdef _KERNEL
1998-07-12 19:23:59 +04:00
RWLOCK_EXIT(&ipf_state);
#else
if (tcp->th_flags & TCP_CLOSE) {
*isp = is->is_next;
isp = &ips_table[hv];
KFREE(is);
}
#endif
1998-07-12 19:23:59 +04:00
if (fin->fin_fi.fi_fl & FI_FRAG)
ipfr_newfrag(ip, fin,
pass ^ FR_KEEPSTATE);
1997-05-25 16:40:11 +04:00
return pass;
}
}
1998-07-12 19:23:59 +04:00
RWLOCK_EXIT(&ipf_state);
break;
}
case IPPROTO_UDP :
{
register u_short dport = tcp->th_dport, sport = tcp->th_sport;
hv += dport;
hv += sport;
hv %= IPSTATE_SIZE;
/*
* Nothing else to match on but ports. and IP#'s
*/
1998-07-12 19:23:59 +04:00
READ_ENTER(&ipf_state);
for (is = ips_table[hv]; is; is = is->is_next)
if ((is->is_p == pr) &&
fr_matchsrcdst(is, src, dst, fin,
tcp, sport, dport)) {
1998-07-12 19:23:59 +04:00
pass = is->is_pass;
MUTEX_ENTER(&ipf_rw);
is->is_bytes += ip->ip_len;
is->is_age = fr_udptimeout;
1998-07-12 19:23:59 +04:00
ips_stats.iss_hits++;
is->is_pkts++;
MUTEX_EXIT(&ipf_rw);
RWLOCK_EXIT(&ipf_state);
if (fin->fin_fi.fi_fl & FI_FRAG)
ipfr_newfrag(ip, fin,
pass ^ FR_KEEPSTATE);
return pass;
}
1998-07-12 19:23:59 +04:00
RWLOCK_EXIT(&ipf_state);
break;
}
default :
break;
}
1998-07-12 19:23:59 +04:00
ATOMIC_INC(ips_stats.iss_miss);
return 0;
}
/*
* Free memory in use by all state info. kept.
*/
void fr_stateunload()
{
register int i;
register ipstate_t *is, **isp;
1998-07-12 19:23:59 +04:00
WRITE_ENTER(&ipf_state);
for (i = 0; i < IPSTATE_SIZE; i++)
for (isp = &ips_table[i]; (is = *isp); ) {
*isp = is->is_next;
KFREE(is);
}
1998-11-22 18:17:18 +03:00
ips_num = 0;
1998-07-12 19:23:59 +04:00
RWLOCK_EXIT(&ipf_state);
}
/*
* Slowly expire held state for thingslike UDP and ICMP. Timeouts are set
* in expectation of this being called twice per second.
*/
void fr_timeoutstate()
{
register int i;
register ipstate_t *is, **isp;
1997-07-05 09:38:14 +04:00
#if defined(_KERNEL) && !SOLARIS
int s;
1997-07-05 09:38:14 +04:00
#endif
SPL_NET(s);
1998-07-12 19:23:59 +04:00
WRITE_ENTER(&ipf_state);
for (i = 0; i < IPSTATE_SIZE; i++)
for (isp = &ips_table[i]; (is = *isp); )
if (is->is_age && !--is->is_age) {
*isp = is->is_next;
if (is->is_p == IPPROTO_TCP)
ips_stats.iss_fin++;
else
ips_stats.iss_expire++;
1997-05-25 16:40:11 +04:00
#ifdef IPFILTER_LOG
ipstate_log(is, ISL_EXPIRE);
#endif
KFREE(is);
ips_num--;
} else
isp = &is->is_next;
1998-07-12 19:23:59 +04:00
RWLOCK_EXIT(&ipf_state);
SPL_X(s);
}
/*
* Original idea freom Pradeep Krishnan for use primarily with NAT code.
* (pkrishna@netcom.com)
*/
void fr_tcp_age(age, state, ip, fin, dir)
u_long *age;
u_char *state;
ip_t *ip;
fr_info_t *fin;
int dir;
{
tcphdr_t *tcp = (tcphdr_t *)fin->fin_dp;
u_char flags = tcp->th_flags;
int dlen, ostate;
ostate = state[1 - dir];
dlen = ip->ip_len - fin->fin_hlen - (tcp->th_off << 2);
if (flags & TH_RST) {
if (!(tcp->th_flags & TH_PUSH) && !dlen) {
*age = fr_tcpclosed;
state[dir] = TCPS_CLOSED;
} else {
*age = fr_tcpclosewait;
state[dir] = TCPS_CLOSE_WAIT;
}
return;
}
*age = fr_tcptimeout; /* 1 min */
switch(state[dir])
{
case TCPS_FIN_WAIT_2:
case TCPS_CLOSED:
if ((flags & TH_OPENING) == TH_OPENING)
state[dir] = TCPS_SYN_RECEIVED;
else if (flags & TH_SYN)
state[dir] = TCPS_SYN_SENT;
break;
case TCPS_SYN_RECEIVED:
if ((flags & (TH_FIN|TH_ACK)) == TH_ACK) {
state[dir] = TCPS_ESTABLISHED;
*age = fr_tcpidletimeout;
}
break;
case TCPS_SYN_SENT:
if ((flags & (TH_FIN|TH_ACK)) == TH_ACK) {
state[dir] = TCPS_ESTABLISHED;
*age = fr_tcpidletimeout;
}
break;
case TCPS_ESTABLISHED:
if (flags & TH_FIN) {
state[dir] = TCPS_CLOSE_WAIT;
if (!(flags & TH_PUSH) && !dlen &&
ostate > TCPS_ESTABLISHED)
*age = fr_tcplastack;
else
*age = fr_tcpclosewait;
} else
*age = fr_tcpidletimeout;
break;
case TCPS_CLOSE_WAIT:
if ((flags & TH_FIN) && !(flags & TH_PUSH) && !dlen &&
ostate > TCPS_ESTABLISHED) {
*age = fr_tcplastack;
state[dir] = TCPS_LAST_ACK;
} else
*age = fr_tcpclosewait;
break;
case TCPS_LAST_ACK:
if (flags & TH_ACK) {
state[dir] = TCPS_FIN_WAIT_2;
if (!(flags & TH_PUSH) && !dlen &&
ostate > TCPS_ESTABLISHED)
*age = fr_tcplastack;
else {
*age = fr_tcpclosewait;
state[dir] = TCPS_CLOSE_WAIT;
}
}
break;
}
}
1997-05-25 16:40:11 +04:00
#ifdef IPFILTER_LOG
void ipstate_log(is, type)
struct ipstate *is;
u_short type;
{
struct ipslog ipsl;
void *items[1];
size_t sizes[1];
int types[1];
1997-05-25 16:40:11 +04:00
1998-11-22 18:17:18 +03:00
ipsl.isl_type = type;
1997-05-25 16:40:11 +04:00
ipsl.isl_pkts = is->is_pkts;
ipsl.isl_bytes = is->is_bytes;
ipsl.isl_src = is->is_src;
ipsl.isl_dst = is->is_dst;
ipsl.isl_p = is->is_p;
ipsl.isl_flags = is->is_flags;
if (ipsl.isl_p == IPPROTO_TCP || ipsl.isl_p == IPPROTO_UDP) {
ipsl.isl_sport = is->is_sport;
ipsl.isl_dport = is->is_dport;
1998-11-22 18:17:18 +03:00
if (ipsl.isl_p == IPPROTO_TCP) {
ipsl.isl_state[0] = is->is_state[0];
ipsl.isl_state[1] = is->is_state[1];
}
1997-05-25 16:40:11 +04:00
} else if (ipsl.isl_p == IPPROTO_ICMP)
ipsl.isl_itype = is->is_icmp.ics_type;
else {
ipsl.isl_ps.isl_filler[0] = 0;
ipsl.isl_ps.isl_filler[1] = 0;
}
items[0] = &ipsl;
sizes[0] = sizeof(ipsl);
types[0] = 0;
1997-05-25 16:40:11 +04:00
(void) ipllog(IPL_LOGSTATE, 0, items, sizes, types, 1);
1997-05-25 16:40:11 +04:00
}
#endif