2 * Copyright (C) 1995-2002 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * @(#)ip_state.c 1.8 6/5/96 (C) 1993-2000 Darren Reed
7 * @(#)$Id: ip_state.c,v 2.30.2.74 2002/07/27 15:58:10 darrenr Exp $
8 * $FreeBSD: src/sys/contrib/ipfilter/netinet/ip_state.c,v 1.21.2.5 2003/03/01 03:55:54 darrenr Exp $
9 * $DragonFly: src/sys/contrib/ipfilter/netinet/ip_state.c,v 1.2 2003/06/17 04:28:20 dillon Exp $
12 #if defined(__sgi) && (IRIX > 602)
13 # include <sys/ptimers.h>
15 #include <sys/errno.h>
16 #include <sys/types.h>
17 #include <sys/param.h>
19 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
21 # include "opt_ipfilter_log.h"
23 #if defined(_KERNEL) && defined(__FreeBSD_version) && \
24 (__FreeBSD_version >= 400000) && !defined(KLD_MODULE)
25 #include "opt_inet6.h"
27 #if !defined(_KERNEL) && !defined(KERNEL) && !defined(__KERNEL__)
33 # include <linux/kernel.h>
34 # include <linux/module.h>
37 #if (defined(KERNEL) || defined(_KERNEL)) && (__FreeBSD_version >= 220000)
38 # include <sys/filio.h>
39 # include <sys/fcntl.h>
40 # if (__FreeBSD_version >= 300000) && !defined(IPFILTER_LKM)
41 # include "opt_ipfilter.h"
44 # include <sys/ioctl.h>
48 # include <sys/protosw.h>
50 #include <sys/socket.h>
51 #if (defined(_KERNEL) || defined(KERNEL)) && !defined(linux)
52 # include <sys/systm.h>
54 #if !defined(__SVR4) && !defined(__svr4__)
56 # include <sys/mbuf.h>
59 # include <sys/filio.h>
60 # include <sys/byteorder.h>
62 # include <sys/dditypes.h>
64 # include <sys/stream.h>
65 # include <sys/kmem.h>
72 #include <net/route.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #include <netinet/tcp.h>
78 # include <netinet/ip_var.h>
79 # include <netinet/tcp_fsm.h>
81 #include <netinet/udp.h>
82 #include <netinet/ip_icmp.h>
83 #include "netinet/ip_compat.h"
84 #include <netinet/tcpip.h>
85 #include "netinet/ip_fil.h"
86 #include "netinet/ip_nat.h"
87 #include "netinet/ip_frag.h"
88 #include "netinet/ip_state.h"
90 #include <netinet/icmp6.h>
92 #if (__FreeBSD_version >= 300000)
93 # include <sys/malloc.h>
94 # if (defined(_KERNEL) || defined(KERNEL)) && !defined(IPFILTER_LKM)
95 # include <sys/libkern.h>
96 # include <sys/systm.h>
100 static const char sccsid[] = "@(#)ip_state.c 1.8 6/5/96 (C) 1993-2000 Darren Reed";
103 # define MIN(a,b) (((a)<(b))?(a):(b))
106 #define TCP_CLOSE (TH_FIN|TH_RST)
108 static ipstate_t **ips_table = NULL;
109 static int ips_num = 0;
110 static int ips_wild = 0;
111 static ips_stat_t ips_stats;
112 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
113 extern KRWLOCK_T ipf_state, ipf_mutex;
114 extern kmutex_t ipf_rw;
118 static frentry_t *fr_checkicmp6matchingstate __P((ip6_t *, fr_info_t *));
120 static int fr_matchsrcdst __P((ipstate_t *, union i6addr, union i6addr,
121 fr_info_t *, tcphdr_t *));
122 static frentry_t *fr_checkicmpmatchingstate __P((ip_t *, fr_info_t *));
123 static int fr_matchicmpqueryreply __P((int, ipstate_t *, icmphdr_t *, int));
124 static int fr_state_flush __P((int, int));
125 static ips_stat_t *fr_statetstats __P((void));
126 static void fr_delstate __P((ipstate_t *));
127 static int fr_state_remove __P((caddr_t));
128 static void fr_ipsmove __P((ipstate_t **, ipstate_t *, u_int));
129 static int fr_tcpoptions __P((tcphdr_t *));
130 int fr_stputent __P((caddr_t));
131 int fr_stgetent __P((caddr_t));
132 void fr_stinsert __P((ipstate_t *));
135 #define FIVE_DAYS (2 * 5 * 86400) /* 5 days: half closed session */
137 #define TCP_MSL 240 /* 2 minutes */
138 u_long fr_tcpidletimeout = FIVE_DAYS,
139 fr_tcpclosewait = 2 * TCP_MSL,
140 fr_tcplastack = 2 * TCP_MSL,
141 fr_tcptimeout = 2 * TCP_MSL,
143 fr_tcphalfclosed = 2 * 2 * 3600, /* 2 hours */
145 fr_udpacktimeout = 24,
146 fr_icmptimeout = 120,
147 fr_icmpacktimeout = 12;
148 int fr_statemax = IPSTATE_MAX,
149 fr_statesize = IPSTATE_SIZE;
150 int fr_state_doflush = 0,
152 ipstate_t *ips_list = NULL;
154 static int icmpreplytype4[ICMP_MAXTYPE + 1];
156 static int icmpreplytype6[ICMP6_MAXTYPE + 1];
163 KMALLOCS(ips_table, ipstate_t **, fr_statesize * sizeof(ipstate_t *));
164 if (ips_table != NULL)
165 bzero((char *)ips_table, fr_statesize * sizeof(ipstate_t *));
169 /* fill icmp reply type table */
170 for (i = 0; i <= ICMP_MAXTYPE; i++)
171 icmpreplytype4[i] = -1;
172 icmpreplytype4[ICMP_ECHO] = ICMP_ECHOREPLY;
173 icmpreplytype4[ICMP_TSTAMP] = ICMP_TSTAMPREPLY;
174 icmpreplytype4[ICMP_IREQ] = ICMP_IREQREPLY;
175 icmpreplytype4[ICMP_MASKREQ] = ICMP_MASKREPLY;
177 /* fill icmp reply type table */
178 for (i = 0; i <= ICMP6_MAXTYPE; i++)
179 icmpreplytype6[i] = -1;
180 icmpreplytype6[ICMP6_ECHO_REQUEST] = ICMP6_ECHO_REPLY;
181 icmpreplytype6[ICMP6_MEMBERSHIP_QUERY] = ICMP6_MEMBERSHIP_REPORT;
182 icmpreplytype6[ICMP6_NI_QUERY] = ICMP6_NI_REPLY;
183 icmpreplytype6[ND_ROUTER_SOLICIT] = ND_ROUTER_ADVERT;
184 icmpreplytype6[ND_NEIGHBOR_SOLICIT] = ND_NEIGHBOR_ADVERT;
191 static ips_stat_t *fr_statetstats()
193 ips_stats.iss_active = ips_num;
194 ips_stats.iss_table = ips_table;
195 ips_stats.iss_list = ips_list;
201 * flush state tables. two actions currently defined:
202 * which == 0 : flush all state table entries
203 * which == 1 : flush TCP connections which have started to close but are
204 * stuck for some reason.
205 * which == 2 : flush TCP connections which have been idle for a long time,
206 * starting at > 4 days idle and working back in successive half-
207 * days to at most 12 hours old.
209 static int fr_state_flush(which, proto)
212 ipstate_t *is, **isp;
213 #if defined(_KERNEL) && !SOLARIS
216 int delete, removed = 0, try;
219 for (isp = &ips_list; (is = *isp); ) {
222 if ((proto != 0) && (is->is_v != proto))
232 if (is->is_p != IPPROTO_TCP)
234 if ((is->is_state[0] != TCPS_ESTABLISHED) ||
235 (is->is_state[1] != TCPS_ESTABLISHED))
241 if (is->is_p == IPPROTO_TCP)
244 ips_stats.iss_expire++;
246 ipstate_log(is, ISL_FLUSH);
255 * Asked to remove inactive entries, try again if first attempt
256 * failed. In this case, 86400 is half a day because the counter is
257 * activated every half second.
259 if ((which == 2) && (removed == 0)) {
260 try = 86400; /* half a day */
261 for (; (try < FIVE_DAYS) && (removed == 0); try += 86400) {
262 for (isp = &ips_list; (is = *isp); ) {
264 if ((is->is_p == IPPROTO_TCP) &&
265 ((is->is_state[0] == TCPS_ESTABLISHED) ||
266 (is->is_state[1] == TCPS_ESTABLISHED)) &&
267 (is->is_age < try)) {
270 } else if ((is->is_p != IPPROTO_TCP) &&
272 ips_stats.iss_expire++;
277 ipstate_log(is, ISL_FLUSH);
292 static int fr_state_remove(data)
299 error = IRCOPYPTR(data, (caddr_t)&st, sizeof(st));
303 WRITE_ENTER(&ipf_state);
304 for (sp = ips_list; sp; sp = sp->is_next)
305 if ((sp->is_p == st.is_p) && (sp->is_v == st.is_v) &&
306 !bcmp((char *)&sp->is_src, (char *)&st.is_src,
307 sizeof(st.is_src)) &&
308 !bcmp((char *)&sp->is_dst, (char *)&st.is_dst,
309 sizeof(st.is_dst)) &&
310 !bcmp((char *)&sp->is_ps, (char *)&st.is_ps,
313 ipstate_log(sp, ISL_REMOVE);
316 RWLOCK_EXIT(&ipf_state);
319 RWLOCK_EXIT(&ipf_state);
324 int fr_state_ioctl(data, cmd, mode)
326 #if defined(__NetBSD__) || defined(__OpenBSD__)
333 int arg, ret, error = 0;
338 error = fr_state_remove(data);
341 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
344 if (arg == 0 || arg == 1) {
345 WRITE_ENTER(&ipf_state);
346 ret = fr_state_flush(arg, 4);
347 RWLOCK_EXIT(&ipf_state);
348 error = IWCOPY((caddr_t)&ret, data, sizeof(ret));
354 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
357 if (arg == 0 || arg == 1) {
358 WRITE_ENTER(&ipf_state);
359 ret = fr_state_flush(arg, 6);
360 RWLOCK_EXIT(&ipf_state);
361 error = IWCOPY((caddr_t)&ret, data, sizeof(ret));
368 if (!(mode & FWRITE))
373 tmp = ipflog_clear(IPL_LOGSTATE);
374 IWCOPY((char *)&tmp, data, sizeof(tmp));
379 error = IWCOPYPTR((caddr_t)fr_statetstats(), data,
384 arg = (int)iplused[IPL_LOGSTATE];
385 error = IWCOPY((caddr_t)&arg, (caddr_t)data, sizeof(arg));
389 error = fr_lock(data, &fr_state_lock);
392 if (!fr_state_lock) {
396 error = fr_stputent(data);
399 if (!fr_state_lock) {
403 error = fr_stgetent(data);
414 * Copy out state information from the kernel to a user space process.
416 int fr_stgetent(data)
419 register ipstate_t *is, *isn;
423 error = IRCOPYPTR(data, (caddr_t)&ips, sizeof(ips));
431 if (ips.ips_next == NULL)
437 * Make sure the pointer we're copying from exists in the
438 * current list of entries. Security precaution to prevent
439 * copying of random kernel data.
441 for (is = ips_list; is; is = is->is_next)
447 ips.ips_next = isn->is_next;
448 bcopy((char *)isn, (char *)&ips.ips_is, sizeof(ips.ips_is));
450 bcopy((char *)isn->is_rule, (char *)&ips.ips_fr,
452 error = IWCOPYPTR((caddr_t)&ips, data, sizeof(ips));
459 int fr_stputent(data)
462 register ipstate_t *is, *isn;
468 error = IRCOPYPTR(data, (caddr_t)&ips, sizeof(ips));
472 KMALLOC(isn, ipstate_t *);
476 bcopy((char *)&ips.ips_is, (char *)isn, sizeof(*isn));
479 if (isn->is_flags & FI_NEWFR) {
480 KMALLOC(fr, frentry_t *);
485 bcopy((char *)&ips.ips_fr, (char *)fr, sizeof(*fr));
486 out = fr->fr_flags & FR_OUTQUE ? 1 : 0;
488 ips.ips_is.is_rule = fr;
491 * Look up all the interface names in the rule.
493 for (i = 0; i < 4; i++) {
494 name = fr->fr_ifnames[i];
495 if ((name[1] == '\0') &&
496 ((name[0] == '-') || (name[0] == '*'))) {
497 fr->fr_ifas[i] = NULL;
498 } else if (*name != '\0') {
499 fr->fr_ifas[i] = GETUNIT(name,
501 if (fr->fr_ifas[i] == NULL)
502 fr->fr_ifas[i] = (void *)-1;
504 strncpy(isn->is_ifname[i],
505 IFNAME(fr->fr_ifas[i]),
509 isn->is_ifp[out] = fr->fr_ifas[i];
513 * send a copy back to userland of what we ended up
514 * to allow for verification.
516 error = IWCOPYPTR((caddr_t)&ips, data, sizeof(ips));
523 for (is = ips_list; is; is = is->is_next)
524 if (is->is_rule == fr)
538 * Insert a state table entry manually.
541 register ipstate_t *is;
543 register u_int hv = is->is_hv;
547 MUTEX_INIT(&is->is_lock, "ipf state entry", NULL);
550 * Look up all the interface names in the state entry.
552 for (i = 0; i < 4; i++) {
553 name = is->is_ifname[i];
554 if ((name[1] == '\0') &&
555 ((name[0] == '-') || (name[0] == '*'))) {
556 is->is_ifp[0] = NULL;
557 } else if (*name != '\0') {
558 is->is_ifp[i] = GETUNIT(name, is->is_v);
559 if (is->is_ifp[i] == NULL)
560 is->is_ifp[i] = (void *)-1;
566 * add into list table.
569 ips_list->is_pnext = &is->is_next;
570 is->is_pnext = &ips_list;
571 is->is_next = ips_list;
574 ips_table[hv]->is_phnext = &is->is_hnext;
576 ips_stats.iss_inuse++;
577 is->is_phnext = ips_table + hv;
578 is->is_hnext = ips_table[hv];
585 * Create a new ipstate structure and hang it off the hash table.
587 ipstate_t *fr_addstate(ip, fin, stsave, flags)
593 register tcphdr_t *tcp = NULL;
594 register ipstate_t *is;
602 if (fr_state_lock || (fin->fin_off != 0) || (fin->fin_fl & FI_SHORT) ||
603 (fin->fin_misc & FM_BADSTATE))
605 if (ips_num == fr_statemax) {
607 fr_state_doflush = 1;
612 bzero((char *)is, sizeof(*is));
615 * Copy and calculate...
617 hv = (is->is_p = fin->fin_fi.fi_p);
618 is->is_src = fin->fin_fi.fi_src;
620 is->is_dst = fin->fin_fi.fi_dst;
623 if (fin->fin_v == 6) {
624 if ((is->is_p == IPPROTO_ICMPV6) &&
625 IN6_IS_ADDR_MULTICAST(&is->is_dst.in6)) {
627 * So you can do keep state with neighbour discovery.
632 hv += is->is_dst.i6[1];
633 hv += is->is_dst.i6[2];
634 hv += is->is_dst.i6[3];
636 hv += is->is_src.i6[1];
637 hv += is->is_src.i6[2];
638 hv += is->is_src.i6[3];
647 case IPPROTO_ICMPV6 :
648 ic = (struct icmp *)fin->fin_dp;
649 if ((ic->icmp_type & ICMP6_INFOMSG_MASK) == 0)
652 switch (ic->icmp_type)
654 case ICMP6_ECHO_REQUEST :
655 is->is_icmp.ics_type = ic->icmp_type;
656 hv += (is->is_icmp.ics_id = ic->icmp_id);
657 hv += (is->is_icmp.ics_seq = ic->icmp_seq);
659 case ICMP6_MEMBERSHIP_QUERY :
660 case ND_ROUTER_SOLICIT :
661 case ND_NEIGHBOR_SOLICIT :
662 case ICMP6_NI_QUERY :
663 is->is_icmp.ics_type = ic->icmp_type;
668 ATOMIC_INCL(ips_stats.iss_icmp);
669 is->is_age = fr_icmptimeout;
673 ic = (struct icmp *)fin->fin_dp;
675 switch (ic->icmp_type)
681 is->is_icmp.ics_type = ic->icmp_type;
682 hv += (is->is_icmp.ics_id = ic->icmp_id);
683 hv += (is->is_icmp.ics_seq = ic->icmp_seq);
688 ATOMIC_INCL(ips_stats.iss_icmp);
689 is->is_age = fr_icmptimeout;
692 tcp = (tcphdr_t *)fin->fin_dp;
694 if (tcp->th_flags & TH_RST)
697 * The endian of the ports doesn't matter, but the ack and
698 * sequence numbers do as we do mathematics on them later.
700 is->is_sport = htons(fin->fin_data[0]);
701 is->is_dport = htons(fin->fin_data[1]);
702 if ((flags & (FI_W_DPORT|FI_W_SPORT)) == 0) {
706 if ((flags & FI_IGNOREPKT) == 0) {
707 is->is_send = ntohl(tcp->th_seq) + fin->fin_dlen -
708 (off = (tcp->th_off << 2)) +
709 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
710 ((tcp->th_flags & TH_FIN) ? 1 : 0);
711 is->is_maxsend = is->is_send;
713 if ((tcp->th_flags & TH_SYN) &&
714 ((tcp->th_off << 2) >= (sizeof(*tcp) + 4))) {
715 ws = fr_tcpoptions(tcp);
722 is->is_maxswin = ntohs(tcp->th_win);
723 if (is->is_maxswin == 0)
726 if ((tcp->th_flags & TH_OPENING) == TH_SYN)
730 * If we're creating state for a starting connection, start the
731 * timer on it as we'll never see an error if it fails to
734 ATOMIC_INCL(ips_stats.iss_tcp);
738 tcp = (tcphdr_t *)fin->fin_dp;
740 is->is_sport = htons(fin->fin_data[0]);
741 is->is_dport = htons(fin->fin_data[1]);
742 if ((flags & (FI_W_DPORT|FI_W_SPORT)) == 0) {
746 ATOMIC_INCL(ips_stats.iss_udp);
747 is->is_age = fr_udptimeout;
750 is->is_age = fr_udptimeout;
754 KMALLOC(is, ipstate_t *);
756 ATOMIC_INCL(ips_stats.iss_nomem);
759 bcopy((char *)&ips, (char *)is, sizeof(*is));
762 is->is_rule = fin->fin_fr;
763 if (is->is_rule != NULL) {
764 is->is_group = is->is_rule->fr_group;
765 ATOMIC_INC32(is->is_rule->fr_ref);
766 pass = is->is_rule->fr_flags;
767 is->is_frage[0] = is->is_rule->fr_age[0];
768 is->is_frage[1] = is->is_rule->fr_age[1];
769 if (is->is_frage[0] != 0)
770 is->is_age = is->is_frage[0];
772 is->is_ifp[(out << 1) + 1] = is->is_rule->fr_ifas[1];
773 is->is_ifp[(1 - out) << 1] = is->is_rule->fr_ifas[2];
774 is->is_ifp[((1 - out) << 1) + 1] = is->is_rule->fr_ifas[3];
776 if (((ifp = is->is_rule->fr_ifas[1]) != NULL) &&
778 strncpy(is->is_ifname[(out << 1) + 1],
779 IFNAME(ifp), IFNAMSIZ);
780 if (((ifp = is->is_rule->fr_ifas[2]) != NULL) &&
782 strncpy(is->is_ifname[(1 - out) << 1],
783 IFNAME(ifp), IFNAMSIZ);
784 if (((ifp = is->is_rule->fr_ifas[3]) != NULL) &&
786 strncpy(is->is_ifname[((1 - out) << 1) + 1],
787 IFNAME(ifp), IFNAMSIZ);
791 is->is_ifp[out << 1] = fin->fin_ifp;
792 strncpy(is->is_ifname[out << 1], IFNAME(fin->fin_ifp), IFNAMSIZ);
794 WRITE_ENTER(&ipf_state);
797 if ((flags & FI_IGNOREPKT) == 0) {
799 is->is_bytes = fin->fin_dlen + fin->fin_hlen;
802 * We want to check everything that is a property of this packet,
803 * but we don't (automatically) care about it's fragment status as
806 is->is_v = fin->fin_v;
807 is->is_rulen = fin->fin_rule;
808 is->is_opt = fin->fin_fi.fi_optmsk;
809 is->is_optmsk = 0xffffffff;
810 is->is_sec = fin->fin_fi.fi_secmsk;
811 is->is_secmsk = 0xffff;
812 is->is_auth = fin->fin_fi.fi_auth;
813 is->is_authmsk = 0xffff;
814 is->is_flags = fin->fin_fl & FI_CMP;
815 is->is_flags |= FI_CMP << 4;
816 is->is_flags |= flags & (FI_WILDP|FI_WILDA);
817 if (flags & (FI_WILDP|FI_WILDA))
820 if (pass & FR_LOGFIRST)
821 is->is_pass &= ~(FR_LOGFIRST|FR_LOG);
824 if (is->is_p == IPPROTO_TCP) {
825 fr_tcp_age(&is->is_age, is->is_state, fin,
826 0, is->is_fsm); /* 0 = packet from the source */
829 ipstate_log(is, ISL_NEW);
831 RWLOCK_EXIT(&ipf_state);
832 fin->fin_rev = IP6NEQ(is->is_dst, fin->fin_fi.fi_dst);
833 if ((fin->fin_fl & FI_FRAG) && (pass & FR_KEEPFRAG))
834 ipfr_newfrag(ip, fin);
839 static int fr_tcpoptions(tcp)
845 opt = (u_char *) (tcp + 1);
846 last = ((u_char *)tcp) + (tcp->th_off << 2);
848 /* If we don't find wscale here, we need to clear it */
851 /* Termination condition picked such that opt[0 .. 2] exist */
852 while ((opt < last - 2) && (*opt != TCPOPT_EOL)) {
858 /* Proper length ? */
867 /* Unknown options must be two bytes+ */
881 * check to see if a packet with TCP headers fits within the TCP window.
882 * change timeout depending on whether new packet is a SYN-ACK returning for a
883 * SYN or a RST or FIN which indicate time to close up shop.
885 int fr_tcpstate(is, fin, ip, tcp)
886 register ipstate_t *is;
891 register tcp_seq seq, ack, end;
892 register int ackskew;
893 tcpdata_t *fdata, *tdata;
900 * Find difference between last checked packet and this packet.
902 source = IP6EQ(fin->fin_fi.fi_src, is->is_src);
903 if (source && (ntohs(is->is_sport) != fin->fin_data[0]))
905 fdata = &is->is_tcp.ts_data[!source];
906 tdata = &is->is_tcp.ts_data[source];
907 off = tcp->th_off << 2;
908 seq = ntohl(tcp->th_seq);
909 ack = ntohl(tcp->th_ack);
910 win = ntohs(tcp->th_win);
911 end = seq + fin->fin_dlen - off +
912 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
913 ((tcp->th_flags & TH_FIN) ? 1 : 0);
916 if ((tcp->th_flags & TH_SYN) && (off >= sizeof(*tcp) + 4))
917 wscale = fr_tcpoptions(tcp);
921 MUTEX_ENTER(&is->is_lock);
924 fdata->td_wscale = wscale;
925 else if (wscale == -2)
926 fdata->td_wscale = tdata->td_wscale = 0;
927 win <<= fdata->td_wscale;
929 if ((fdata->td_end == 0) &&
930 (!is->is_fsm || ((tcp->th_flags & TH_OPENING) == TH_OPENING))) {
932 * Must be a (outgoing) SYN-ACK in reply to a SYN.
935 fdata->td_maxwin = 1;
936 fdata->td_maxend = end + win;
941 if (!(tcp->th_flags & TH_ACK)) { /* Pretend an ack was sent */
943 } else if (((tcp->th_flags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) &&
945 /* gross hack to get around certain broken tcp stacks */
950 seq = end = fdata->td_end;
952 maxwin = tdata->td_maxwin;
953 ackskew = tdata->td_end - ack;
955 #define SEQ_GE(a,b) ((int)((a) - (b)) >= 0)
956 #define SEQ_GT(a,b) ((int)((a) - (b)) > 0)
957 if ((SEQ_GE(fdata->td_maxend, end)) &&
958 (SEQ_GE(seq, fdata->td_end - maxwin)) &&
959 /* XXX what about big packets */
960 #define MAXACKWINDOW 66000
961 (ackskew >= -MAXACKWINDOW) &&
962 (ackskew <= MAXACKWINDOW)) {
963 /* if ackskew < 0 then this should be due to fragented
964 * packets. There is no way to know the length of the
965 * total packet in advance.
966 * We do know the total length from the fragment cache though.
967 * Note however that there might be more sessions with
968 * exactly the same source and destination paramters in the
969 * state cache (and source and destination is the only stuff
970 * that is saved in the fragment cache). Note further that
971 * some TCP connections in the state cache are hashed with
972 * sport and dport as well which makes it not worthwhile to
974 * Thus, when ackskew is negative but still seems to belong
975 * to this session, we bump up the destinations end value.
978 * Nearing end of connection, start timeout.
980 /* source ? 0 : 1 -> !source */
981 if (fr_tcp_age(&is->is_age, is->is_state, fin, !source,
982 (int)is->is_fsm) == 0) {
986 /* update max window seen */
987 if (fdata->td_maxwin < win)
988 fdata->td_maxwin = win;
989 if (SEQ_GT(end, fdata->td_end))
991 if (SEQ_GE(ack + win, tdata->td_maxend)) {
992 tdata->td_maxend = ack + win;
997 ATOMIC_INCL(ips_stats.iss_hits);
1001 MUTEX_EXIT(&is->is_lock);
1002 if ((ret == 0) && ((tcp->th_flags & TH_OPENING) != TH_SYN))
1003 fin->fin_misc |= FM_BADSTATE;
1009 * Match a state table entry against an IP packet.
1011 static int fr_matchsrcdst(is, src, dst, fin, tcp)
1013 union i6addr src, dst;
1017 int ret = 0, rev, out, flags, idx;
1021 rev = IP6NEQ(is->is_dst, dst);
1024 flags = is->is_flags & (FI_WILDA|FI_WILDP);
1029 flags = is->is_flags;
1033 if (!(flags & FI_W_SPORT) && (sp != is->is_sport))
1035 else if (!(flags & FI_W_DPORT) && (dp != is->is_dport))
1040 idx = (out << 1) + rev;
1042 if ((is->is_ifp[idx] == NULL &&
1043 (*is->is_ifname[idx] == '\0' || *is->is_ifname[idx] == '*')) ||
1044 is->is_ifp[idx] == ifp)
1052 if ((IP6EQ(is->is_dst, dst) || (flags & FI_W_DADDR)) &&
1053 (IP6EQ(is->is_src, src) || (flags & FI_W_SADDR)) &&
1054 (!tcp || ((sp == is->is_sport || flags & FI_W_SPORT) &&
1055 (dp == is->is_dport || flags & FI_W_DPORT)))) {
1059 if ((IP6EQ(is->is_dst, src) || (flags & FI_W_DADDR)) &&
1060 (IP6EQ(is->is_src, dst) || (flags & FI_W_SADDR)) &&
1061 (!tcp || ((sp == is->is_dport || flags & FI_W_DPORT) &&
1062 (dp == is->is_sport || flags & FI_W_SPORT)))) {
1070 * Whether or not this should be here, is questionable, but the aim
1071 * is to get this out of the main line.
1074 flags = is->is_flags & (FI_CMP|(FI_CMP<<4));
1076 if (((fin->fin_fl & (flags >> 4)) != (flags & FI_CMP)) ||
1077 (fin->fin_fi.fi_optmsk != is->is_opt) ||
1078 (fin->fin_fi.fi_secmsk != is->is_sec) ||
1079 (fin->fin_fi.fi_auth != is->is_auth))
1082 flags = is->is_flags & (FI_WILDA|FI_WILDP);
1083 if ((flags & (FI_W_SADDR|FI_W_DADDR))) {
1084 if ((flags & FI_W_SADDR) != 0) {
1086 is->is_src = fin->fin_fi.fi_src;
1088 is->is_src = fin->fin_fi.fi_dst;
1090 } else if ((flags & FI_W_DADDR) != 0) {
1092 is->is_dst = fin->fin_fi.fi_dst;
1094 is->is_dst = fin->fin_fi.fi_src;
1097 is->is_flags &= ~(FI_W_SADDR|FI_W_DADDR);
1098 if ((is->is_flags & (FI_WILDA|FI_WILDP)) == 0)
1102 if ((flags & (FI_W_SPORT|FI_W_DPORT))) {
1103 if ((flags & FI_W_SPORT) != 0) {
1106 is->is_send = htonl(tcp->th_seq);
1109 is->is_send = htonl(tcp->th_ack);
1111 is->is_maxsend = is->is_send + 1;
1112 } else if ((flags & FI_W_DPORT) != 0) {
1115 is->is_dend = htonl(tcp->th_ack);
1118 is->is_dend = htonl(tcp->th_seq);
1120 is->is_maxdend = is->is_dend + 1;
1122 is->is_flags &= ~(FI_W_SPORT|FI_W_DPORT);
1128 if (is->is_ifp[idx] == NULL &&
1129 (*is->is_ifname[idx] == '\0' || *is->is_ifname[idx] == '*'))
1133 is->is_ifp[ret] = ifp;
1134 strncpy(is->is_ifname[ret], IFNAME(ifp),
1135 sizeof(is->is_ifname[ret]));
1141 static int fr_matchicmpqueryreply(v, is, icmp, rev)
1148 * If we matched its type on the way in, then when going out
1149 * it will still be the same type.
1151 if ((!rev && (icmp->icmp_type == is->is_type)) ||
1152 (rev && (icmpreplytype4[is->is_type] == icmp->icmp_type))) {
1153 if (icmp->icmp_type != ICMP_ECHOREPLY)
1155 if ((icmp->icmp_id == is->is_icmp.ics_id) &&
1156 (icmp->icmp_seq == is->is_icmp.ics_seq))
1161 else if (is->is_v == 6) {
1162 if ((!rev && (icmp->icmp_type == is->is_type)) ||
1163 (rev && (icmpreplytype6[is->is_type] == icmp->icmp_type))) {
1164 if (icmp->icmp_type != ICMP6_ECHO_REPLY)
1166 if ((icmp->icmp_id == is->is_icmp.ics_id) &&
1167 (icmp->icmp_seq == is->is_icmp.ics_seq))
1175 static frentry_t *fr_checkicmpmatchingstate(ip, fin)
1179 register ipstate_t *is, **isp;
1180 register u_short sport, dport;
1182 u_short savelen, ohlen;
1183 union i6addr dst, src;
1194 * Does it at least have the return (basic) IP header ?
1195 * Only a basic IP header (no options) should be with
1196 * an ICMP error header.
1198 if (((ip->ip_v != 4) || (ip->ip_hl != 5)) ||
1199 (fin->fin_plen < ICMPERR_MINPKTLEN))
1202 ic = (struct icmp *)fin->fin_dp;
1203 type = ic->icmp_type;
1205 * If it's not an error type, then return
1207 if ((type != ICMP_UNREACH) && (type != ICMP_SOURCEQUENCH) &&
1208 (type != ICMP_REDIRECT) && (type != ICMP_TIMXCEED) &&
1209 (type != ICMP_PARAMPROB))
1212 oip = (ip_t *)((char *)ic + ICMPERR_ICMPHLEN);
1213 ohlen = oip->ip_hl << 2;
1214 if (fin->fin_plen < ICMPERR_MAXPKTLEN + ohlen - sizeof(*oip))
1220 len = fin->fin_dlen - ICMPERR_ICMPHLEN;
1221 if ((len <= 0) || (ohlen > len))
1225 * Is the buffer big enough for all of it ? It's the size of the IP
1226 * header claimed in the encapsulated part which is of concern. It
1227 * may be too big to be in this buffer but not so big that it's
1228 * outside the ICMP packet, leading to TCP deref's causing problems.
1229 * This is possible because we don't know how big oip_hl is when we
1230 * do the pullup early in fr_check() and thus can't gaurantee it is
1239 if ((char *)oip + len > (char *)m->b_wptr)
1242 m = *(mb_t **)fin->fin_mp;
1243 if ((char *)oip + len > (char *)ip + m->m_len)
1250 * in the IPv4 case we must zero the i6addr union otherwise
1251 * the IP6EQ and IP6NEQ macros produce the wrong results because
1252 * of the 'junk' in the unused part of the union
1254 bzero((char *)&src, sizeof(src));
1255 bzero((char *)&dst, sizeof(dst));
1256 bzero((char *)&ofin, sizeof(ofin));
1257 ofin.fin_ifp = fin->fin_ifp;
1258 ofin.fin_out = !fin->fin_out;
1265 icmp = (icmphdr_t *)((char *)oip + ohlen);
1268 * a ICMP error can only be generated as a result of an
1269 * ICMP query, not as the response on an ICMP error
1271 * XXX theoretically ICMP_ECHOREP and the other reply's are
1272 * ICMP query's as well, but adding them here seems strange XXX
1274 if ((icmp->icmp_type != ICMP_ECHO) &&
1275 (icmp->icmp_type != ICMP_TSTAMP) &&
1276 (icmp->icmp_type != ICMP_IREQ) &&
1277 (icmp->icmp_type != ICMP_MASKREQ))
1281 * perform a lookup of the ICMP packet in the state table
1283 hv = (pr = oip->ip_p);
1284 src.in4 = oip->ip_src;
1285 hv += src.in4.s_addr;
1286 dst.in4 = oip->ip_dst;
1287 hv += dst.in4.s_addr;
1288 hv += icmp->icmp_id;
1289 hv += icmp->icmp_seq;
1292 savelen = oip->ip_len;
1294 fr_makefrip(ohlen, oip, &ofin);
1295 oip->ip_len = savelen;
1297 READ_ENTER(&ipf_state);
1298 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext)
1299 if ((is->is_p == pr) && (is->is_v == 4) &&
1300 fr_matchsrcdst(is, src, dst, &ofin, NULL) &&
1301 fr_matchicmpqueryreply(is->is_v, is, icmp, fin->fin_rev)) {
1302 ips_stats.iss_hits++;
1304 is->is_bytes += ip->ip_len;
1308 RWLOCK_EXIT(&ipf_state);
1313 if (fin->fin_plen < ICMPERR_MAXPKTLEN)
1320 tcp = (tcphdr_t *)((char *)oip + ohlen);
1321 dport = tcp->th_dport;
1322 sport = tcp->th_sport;
1324 hv = (pr = oip->ip_p);
1325 src.in4 = oip->ip_src;
1326 hv += src.in4.s_addr;
1327 dst.in4 = oip->ip_dst;
1328 hv += dst.in4.s_addr;
1333 * we make an fin entry to be able to feed it to
1334 * matchsrcdst note that not all fields are encessary
1335 * but this is the cleanest way. Note further we fill
1336 * in fin_mp such that if someone uses it we'll get
1337 * a kernel panic. fr_matchsrcdst does not use this.
1339 * watch out here, as ip is in host order and oip in network
1340 * order. Any change we make must be undone afterwards.
1342 savelen = oip->ip_len;
1344 fr_makefrip(ohlen, oip, &ofin);
1345 oip->ip_len = savelen;
1346 READ_ENTER(&ipf_state);
1347 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) {
1349 * Only allow this icmp though if the
1350 * encapsulated packet was allowed through the
1351 * other way around. Note that the minimal amount
1352 * of info present does not allow for checking against
1353 * tcp internals such as seq and ack numbers.
1355 if ((is->is_p == pr) && (is->is_v == 4) &&
1356 fr_matchsrcdst(is, src, dst, &ofin, tcp)) {
1358 ips_stats.iss_hits++;
1360 is->is_bytes += fin->fin_plen;
1362 * we deliberately do not touch the timeouts
1363 * for the accompanying state table entry.
1364 * It remains to be seen if that is correct. XXX
1369 RWLOCK_EXIT(&ipf_state);
1375 * Move a state hash table entry from its old location at is->is_hv to
1376 * its new location, indexed by hv % fr_statesize.
1378 static void fr_ipsmove(isp, is, hv)
1379 ipstate_t **isp, *is;
1386 * Remove the hash from the old location...
1389 is->is_hnext->is_phnext = isp;
1390 *isp = is->is_hnext;
1391 if (ips_table[hvm] == NULL)
1392 ips_stats.iss_inuse--;
1395 * ...and put the hash in the new one.
1397 hvm = hv % fr_statesize;
1399 isp = &ips_table[hvm];
1401 (*isp)->is_phnext = &is->is_hnext;
1403 ips_stats.iss_inuse++;
1404 is->is_phnext = isp;
1405 is->is_hnext = *isp;
1411 * Check if a packet has a registered state.
1413 frentry_t *fr_checkstate(ip, fin)
1417 union i6addr dst, src;
1418 register ipstate_t *is, **isp;
1420 u_int hv, hvm, hlen, tryagain, pass, v;
1426 if ((ips_list == NULL) || (fin->fin_off != 0) || fr_state_lock ||
1427 (fin->fin_fl & FI_SHORT))
1431 hlen = fin->fin_hlen;
1432 tcp = (tcphdr_t *)((char *)ip + hlen);
1433 ic = (struct icmp *)tcp;
1434 hv = (pr = fin->fin_fi.fi_p);
1435 src = fin->fin_fi.fi_src;
1436 dst = fin->fin_fi.fi_dst;
1437 hv += src.in4.s_addr;
1438 hv += dst.in4.s_addr;
1441 * Search the hash table for matching packet header info.
1442 * At the bottom of this switch statement, the following is expected:
1443 * is == NULL, no lock on ipf_state is held.
1444 * is != NULL, a lock on ipf_state is held.
1446 v = fin->fin_fi.fi_v;
1449 hv += fin->fin_fi.fi_src.i6[1];
1450 hv += fin->fin_fi.fi_src.i6[2];
1451 hv += fin->fin_fi.fi_src.i6[3];
1453 if ((fin->fin_p == IPPROTO_ICMPV6) &&
1454 IN6_IS_ADDR_MULTICAST(&fin->fin_fi.fi_dst.in6)) {
1455 hv -= dst.in4.s_addr;
1457 hv += fin->fin_fi.fi_dst.i6[1];
1458 hv += fin->fin_fi.fi_dst.i6[2];
1459 hv += fin->fin_fi.fi_dst.i6[3];
1467 case IPPROTO_ICMPV6 :
1471 if ((ic->icmp_type == ICMP6_ECHO_REQUEST) ||
1472 (ic->icmp_type == ICMP6_ECHO_REPLY)) {
1477 READ_ENTER(&ipf_state);
1479 hvm = hv % fr_statesize;
1480 for (isp = &ips_table[hvm]; (is = *isp); isp = &is->is_hnext)
1481 if ((is->is_p == pr) && (is->is_v == v) &&
1482 fr_matchsrcdst(is, src, dst, fin, NULL) &&
1483 fr_matchicmpqueryreply(v, is, ic, fin->fin_rev)) {
1485 if (is->is_frage[rev] != 0)
1486 is->is_age = is->is_frage[rev];
1488 is->is_age = fr_icmpacktimeout;
1490 is->is_age = fr_icmptimeout;
1495 if (tryagain && !(is->is_flags & FI_W_DADDR)) {
1496 hv += fin->fin_fi.fi_src.i6[0];
1497 hv += fin->fin_fi.fi_src.i6[1];
1498 hv += fin->fin_fi.fi_src.i6[2];
1499 hv += fin->fin_fi.fi_src.i6[3];
1500 fr_ipsmove(isp, is, hv);
1501 MUTEX_DOWNGRADE(&ipf_state);
1505 RWLOCK_EXIT(&ipf_state);
1508 * No matching icmp state entry. Perhaps this is a
1509 * response to another state entry.
1511 if ((ips_wild != 0) && (v == 6) && (tryagain == 0) &&
1512 !IN6_IS_ADDR_MULTICAST(&fin->fin_fi.fi_src.in6)) {
1513 hv -= fin->fin_fi.fi_src.i6[0];
1514 hv -= fin->fin_fi.fi_src.i6[1];
1515 hv -= fin->fin_fi.fi_src.i6[2];
1516 hv -= fin->fin_fi.fi_src.i6[3];
1518 WRITE_ENTER(&ipf_state);
1522 fr = fr_checkicmp6matchingstate((ip6_t *)ip, fin);
1533 hvm = hv % fr_statesize;
1534 READ_ENTER(&ipf_state);
1535 for (isp = &ips_table[hvm]; (is = *isp); isp = &is->is_hnext)
1536 if ((is->is_p == pr) && (is->is_v == v) &&
1537 fr_matchsrcdst(is, src, dst, fin, NULL) &&
1538 fr_matchicmpqueryreply(v, is, ic, fin->fin_rev)) {
1540 if (is->is_frage[rev] != 0)
1541 is->is_age = is->is_frage[rev];
1542 else if (fin->fin_rev)
1543 is->is_age = fr_icmpacktimeout;
1545 is->is_age = fr_icmptimeout;
1551 RWLOCK_EXIT(&ipf_state);
1553 * No matching icmp state entry. Perhaps this is a
1554 * response to another state entry.
1556 fr = fr_checkicmpmatchingstate(ip, fin);
1562 * Just plain ignore RST flag set with either FIN or SYN.
1564 if ((tcp->th_flags & TH_RST) &&
1565 ((tcp->th_flags & (TH_FIN|TH_SYN|TH_RST)) != TH_RST))
1569 register u_short dport, sport;
1571 dport = tcp->th_dport;
1572 sport = tcp->th_sport;
1576 READ_ENTER(&ipf_state);
1578 hvm = hv % fr_statesize;
1579 for (isp = &ips_table[hvm]; (is = *isp); isp = &is->is_hnext)
1580 if ((is->is_p == pr) && (is->is_v == v) &&
1581 fr_matchsrcdst(is, src, dst, fin, tcp)) {
1583 if ((pr == IPPROTO_TCP)) {
1584 if (!fr_tcpstate(is, fin, ip, tcp))
1586 } else if ((pr == IPPROTO_UDP)) {
1587 if (is->is_frage[rev] != 0)
1588 is->is_age = is->is_frage[rev];
1589 else if (fin->fin_rev)
1590 is->is_age = fr_udpacktimeout;
1592 is->is_age = fr_udptimeout;
1598 !(is->is_flags & (FI_WILDP|FI_WILDA))) {
1601 fr_ipsmove(isp, is, hv);
1602 MUTEX_DOWNGRADE(&ipf_state);
1607 RWLOCK_EXIT(&ipf_state);
1608 if (!tryagain && ips_wild) {
1612 WRITE_ENTER(&ipf_state);
1620 READ_ENTER(&ipf_state);
1621 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) {
1622 if ((is->is_p == pr) && (is->is_v == v) &&
1623 fr_matchsrcdst(is, src, dst, fin, NULL)) {
1625 if (is->is_frage[rev] != 0)
1626 is->is_age = is->is_frage[rev];
1628 is->is_age = fr_udptimeout;
1633 RWLOCK_EXIT(&ipf_state);
1639 ATOMIC_INCL(ips_stats.iss_miss);
1643 MUTEX_ENTER(&is->is_lock);
1644 is->is_bytes += fin->fin_plen;
1645 ips_stats.iss_hits++;
1647 MUTEX_EXIT(&is->is_lock);
1649 fin->fin_rule = is->is_rulen;
1651 fin->fin_group = fr->fr_group;
1652 fin->fin_icode = fr->fr_icode;
1656 RWLOCK_EXIT(&ipf_state);
1657 if ((fin->fin_fl & FI_FRAG) && (pass & FR_KEEPFRAG))
1658 ipfr_newfrag(ip, fin);
1660 if ((tcp != NULL) && (tcp->th_flags & TCP_CLOSE))
1668 * Sync. state entries. If interfaces come or go or just change position,
1671 void ip_statesync(ifp)
1674 register ipstate_t *is;
1677 WRITE_ENTER(&ipf_state);
1678 for (is = ips_list; is; is = is->is_next) {
1679 for (i = 0; i < 4; i++) {
1680 if (is->is_ifp[i] == ifp) {
1681 is->is_ifp[i] = GETUNIT(is->is_ifname[i],
1684 is->is_ifp[i] = (void *)-1;
1688 RWLOCK_EXIT(&ipf_state);
1693 * Must always be called with fr_ipfstate held as a write lock.
1695 static void fr_delstate(is)
1700 if (is->is_flags & (FI_WILDP|FI_WILDA))
1703 is->is_next->is_pnext = is->is_pnext;
1704 *is->is_pnext = is->is_next;
1706 is->is_hnext->is_phnext = is->is_phnext;
1707 *is->is_phnext = is->is_hnext;
1708 if (ips_table[is->is_hv] == NULL)
1709 ips_stats.iss_inuse--;
1716 if (fr->fr_ref == 0) {
1721 MUTEX_DESTROY(&is->is_lock);
1729 * Free memory in use by all state info. kept.
1731 void fr_stateunload()
1733 register ipstate_t *is;
1735 WRITE_ENTER(&ipf_state);
1736 while ((is = ips_list))
1738 ips_stats.iss_inuse = 0;
1740 RWLOCK_EXIT(&ipf_state);
1742 KFREES(ips_table, fr_statesize * sizeof(ipstate_t *));
1748 * Slowly expire held state for thingslike UDP and ICMP. Timeouts are set
1749 * in expectation of this being called twice per second.
1751 void fr_timeoutstate()
1753 register ipstate_t *is, **isp;
1754 #if defined(_KERNEL) && !SOLARIS
1759 WRITE_ENTER(&ipf_state);
1760 for (isp = &ips_list; (is = *isp); )
1761 if (is->is_age && !--is->is_age) {
1762 if (is->is_p == IPPROTO_TCP)
1763 ips_stats.iss_fin++;
1765 ips_stats.iss_expire++;
1767 ipstate_log(is, ISL_EXPIRE);
1772 if (fr_state_doflush) {
1773 (void) fr_state_flush(2, 0);
1774 fr_state_doflush = 0;
1776 RWLOCK_EXIT(&ipf_state);
1782 * Original idea freom Pradeep Krishnan for use primarily with NAT code.
1783 * (pkrishna@netcom.com)
1785 * Rewritten by Arjan de Vet <Arjan.deVet@adv.iae.nl>, 2000-07-29:
1787 * - (try to) base state transitions on real evidence only,
1788 * i.e. packets that are sent and have been received by ipfilter;
1789 * diagram 18.12 of TCP/IP volume 1 by W. Richard Stevens was used.
1791 * - deal with half-closed connections correctly;
1793 * - store the state of the source in state[0] such that ipfstat
1794 * displays the state as source/dest instead of dest/source; the calls
1795 * to fr_tcp_age have been changed accordingly.
1799 * state[0] = state of source (host that initiated connection)
1800 * state[1] = state of dest (host that accepted the connection)
1802 * dir == 0 : a packet from source to dest
1803 * dir == 1 : a packet from dest to source
1806 int fr_tcp_age(age, state, fin, dir, fsm)
1812 tcphdr_t *tcp = (tcphdr_t *)fin->fin_dp;
1813 u_char flags = tcp->th_flags;
1817 ostate = state[1 - dir];
1819 dlen = fin->fin_plen - fin->fin_hlen - (tcp->th_off << 2);
1821 if (flags & TH_RST) {
1822 if (!(tcp->th_flags & TH_PUSH) && !dlen) {
1823 *age = fr_tcpclosed;
1824 state[dir] = TCPS_CLOSED;
1826 *age = fr_tcpclosewait;
1827 state[dir] = TCPS_CLOSE_WAIT;
1836 case TCPS_CLOSED: /* 0 */
1837 if ((flags & TH_OPENING) == TH_OPENING) {
1839 * 'dir' received an S and sends SA in response,
1840 * CLOSED -> SYN_RECEIVED
1842 state[dir] = TCPS_SYN_RECEIVED;
1843 newage = fr_tcptimeout;
1844 } else if ((flags & TH_OPENING) == TH_SYN) {
1845 /* 'dir' sent S, CLOSED -> SYN_SENT */
1846 state[dir] = TCPS_SYN_SENT;
1847 newage = fr_tcptimeout;
1851 * It is apparently possible that a hosts sends two syncs
1852 * before the remote party is able to respond with a SA. In
1853 * such a case the remote server sometimes ACK's the second
1854 * sync, and then responds with a SA. The following code
1855 * is used to prevent this ack from being blocked.
1857 * We do not reset the timeout here to fr_tcptimeout because
1858 * a connection connect timeout does not renew after every
1859 * packet that is sent. We need to set newage to something
1860 * to indicate the packet has passed the check for its flags
1861 * being valid in the TCP FSM.
1863 else if ((ostate == TCPS_SYN_SENT) &&
1864 ((flags & (TH_FIN|TH_SYN|TH_RST|TH_ACK)) == TH_ACK)) {
1869 * The next piece of code makes it possible to get
1870 * already established connections into the state table
1871 * after a restart or reload of the filter rules; this
1872 * does not work when a strict 'flags S keep state' is
1873 * used for tcp connections of course, however, use a
1874 * lower time-out so the state disappears quickly if
1875 * the other side does not pick it up.
1878 (flags & (TH_FIN|TH_SYN|TH_RST|TH_ACK)) == TH_ACK) {
1879 /* we saw an A, guess 'dir' is in ESTABLISHED mode */
1880 if (ostate == TCPS_CLOSED) {
1881 state[dir] = TCPS_ESTABLISHED;
1882 newage = fr_tcptimeout;
1883 } else if (ostate == TCPS_ESTABLISHED) {
1884 state[dir] = TCPS_ESTABLISHED;
1885 newage = fr_tcpidletimeout;
1889 * TODO: besides regular ACK packets we can have other
1890 * packets as well; it is yet to be determined how we
1891 * should initialize the states in those cases
1895 case TCPS_LISTEN: /* 1 */
1899 case TCPS_SYN_SENT: /* 2 */
1900 if ((flags & ~(TH_ECN|TH_CWR)) == TH_SYN) {
1902 * A retransmitted SYN packet. We do not reset the
1903 * timeout here to fr_tcptimeout because a connection
1904 * connect timeout does not renew after every packet
1905 * that is sent. We need to set newage to something
1906 * to indicate the packet has passed the check for its
1907 * flags being valid in the TCP FSM.
1910 } else if ((flags & (TH_SYN|TH_FIN|TH_ACK)) == TH_ACK) {
1912 * We see an A from 'dir' which is in SYN_SENT
1913 * state: 'dir' sent an A in response to an SA
1914 * which it received, SYN_SENT -> ESTABLISHED
1916 state[dir] = TCPS_ESTABLISHED;
1917 newage = fr_tcpidletimeout;
1918 } else if (flags & TH_FIN) {
1920 * We see an F from 'dir' which is in SYN_SENT
1921 * state and wants to close its side of the
1922 * connection; SYN_SENT -> FIN_WAIT_1
1924 state[dir] = TCPS_FIN_WAIT_1;
1925 newage = fr_tcpidletimeout; /* or fr_tcptimeout? */
1926 } else if ((flags & TH_OPENING) == TH_OPENING) {
1928 * We see an SA from 'dir' which is already in
1929 * SYN_SENT state, this means we have a
1930 * simultaneous open; SYN_SENT -> SYN_RECEIVED
1932 state[dir] = TCPS_SYN_RECEIVED;
1933 newage = fr_tcptimeout;
1937 case TCPS_SYN_RECEIVED: /* 3 */
1938 if ((flags & (TH_SYN|TH_FIN|TH_ACK)) == TH_ACK) {
1940 * We see an A from 'dir' which was in SYN_RECEIVED
1941 * state so it must now be in established state,
1942 * SYN_RECEIVED -> ESTABLISHED
1944 state[dir] = TCPS_ESTABLISHED;
1945 newage = fr_tcpidletimeout;
1946 } else if ((flags & ~(TH_ECN|TH_CWR)) == TH_OPENING) {
1948 * We see an SA from 'dir' which is already in
1949 * SYN_RECEIVED state.
1951 newage = fr_tcptimeout;
1952 } else if (flags & TH_FIN) {
1954 * We see an F from 'dir' which is in SYN_RECEIVED
1955 * state and wants to close its side of the connection;
1956 * SYN_RECEIVED -> FIN_WAIT_1
1958 state[dir] = TCPS_FIN_WAIT_1;
1959 newage = fr_tcpidletimeout;
1963 case TCPS_ESTABLISHED: /* 4 */
1964 if (flags & TH_FIN) {
1966 * 'dir' closed its side of the connection; this
1967 * gives us a half-closed connection;
1968 * ESTABLISHED -> FIN_WAIT_1
1970 state[dir] = TCPS_FIN_WAIT_1;
1971 newage = fr_tcphalfclosed;
1972 } else if (flags & TH_ACK) {
1973 /* an ACK, should we exclude other flags here? */
1974 if (ostate == TCPS_FIN_WAIT_1) {
1976 * We know the other side did an active close,
1977 * so we are ACKing the recvd FIN packet (does
1978 * the window matching code guarantee this?)
1979 * and go into CLOSE_WAIT state; this gives us
1980 * a half-closed connection
1982 state[dir] = TCPS_CLOSE_WAIT;
1983 newage = fr_tcphalfclosed;
1984 } else if (ostate < TCPS_CLOSE_WAIT)
1986 * Still a fully established connection,
1989 newage = fr_tcpidletimeout;
1993 case TCPS_CLOSE_WAIT: /* 5 */
1994 if (flags & TH_FIN) {
1996 * Application closed and 'dir' sent a FIN, we're now
1997 * going into LAST_ACK state
1999 newage = fr_tcplastack;
2000 state[dir] = TCPS_LAST_ACK;
2003 * We remain in CLOSE_WAIT because the other side has
2004 * closed already and we did not close our side yet;
2007 newage = fr_tcphalfclosed;
2011 case TCPS_FIN_WAIT_1: /* 6 */
2012 if ((flags & TH_ACK) && ostate > TCPS_CLOSE_WAIT) {
2014 * If the other side is not active anymore it has sent
2015 * us a FIN packet that we are ack'ing now with an ACK;
2016 * this means both sides have now closed the connection
2017 * and we go into TIME_WAIT
2020 * XXX: how do we know we really are ACKing the FIN
2021 * packet here? does the window code guarantee that?
2023 state[dir] = TCPS_TIME_WAIT;
2024 newage = fr_tcptimeout;
2027 * We closed our side of the connection already but the
2028 * other side is still active (ESTABLISHED/CLOSE_WAIT);
2029 * continue with this half-closed connection
2031 newage = fr_tcphalfclosed;
2034 case TCPS_CLOSING: /* 7 */
2038 case TCPS_LAST_ACK: /* 8 */
2039 if (flags & TH_ACK) {
2040 if ((flags & TH_PUSH) || dlen)
2042 * There is still data to be delivered, reset
2045 newage = fr_tcplastack;
2050 * We cannot detect when we go out of LAST_ACK state to CLOSED
2051 * because that is based on the reception of ACK packets;
2052 * ipfilter can only detect that a packet has been sent by a
2057 case TCPS_FIN_WAIT_2: /* 9 */
2061 case TCPS_TIME_WAIT: /* 10 */
2062 newage = fr_tcptimeout; /* default 4 mins */
2063 /* we're in 2MSL timeout now */
2076 void ipstate_log(is, type)
2085 ipsl.isl_type = type;
2086 ipsl.isl_pkts = is->is_pkts;
2087 ipsl.isl_bytes = is->is_bytes;
2088 ipsl.isl_src = is->is_src;
2089 ipsl.isl_dst = is->is_dst;
2090 ipsl.isl_p = is->is_p;
2091 ipsl.isl_v = is->is_v;
2092 ipsl.isl_flags = is->is_flags;
2093 ipsl.isl_rulen = is->is_rulen;
2094 ipsl.isl_group = is->is_group;
2095 if (ipsl.isl_p == IPPROTO_TCP || ipsl.isl_p == IPPROTO_UDP) {
2096 ipsl.isl_sport = is->is_sport;
2097 ipsl.isl_dport = is->is_dport;
2098 if (ipsl.isl_p == IPPROTO_TCP) {
2099 ipsl.isl_state[0] = is->is_state[0];
2100 ipsl.isl_state[1] = is->is_state[1];
2102 } else if (ipsl.isl_p == IPPROTO_ICMP) {
2103 ipsl.isl_itype = is->is_icmp.ics_type;
2104 } else if (ipsl.isl_p == IPPROTO_ICMPV6) {
2105 ipsl.isl_itype = is->is_icmp.ics_type;
2107 ipsl.isl_ps.isl_filler[0] = 0;
2108 ipsl.isl_ps.isl_filler[1] = 0;
2111 sizes[0] = sizeof(ipsl);
2114 (void) ipllog(IPL_LOGSTATE, NULL, items, sizes, types, 1);
2120 frentry_t *fr_checkicmp6matchingstate(ip, fin)
2124 register ipstate_t *is, **isp;
2125 register u_short sport, dport;
2127 struct icmp6_hdr *ic, *oic;
2128 union i6addr dst, src;
2138 * Does it at least have the return (basic) IP header ?
2139 * Only a basic IP header (no options) should be with
2140 * an ICMP error header.
2142 if ((fin->fin_v != 6) || (fin->fin_plen < ICMP6ERR_MINPKTLEN))
2144 ic = (struct icmp6_hdr *)fin->fin_dp;
2145 type = ic->icmp6_type;
2147 * If it's not an error type, then return
2149 if ((type != ICMP6_DST_UNREACH) && (type != ICMP6_PACKET_TOO_BIG) &&
2150 (type != ICMP6_TIME_EXCEEDED) && (type != ICMP6_PARAM_PROB))
2153 oip = (ip6_t *)((char *)ic + ICMPERR_ICMPHLEN);
2154 if (fin->fin_plen < sizeof(*oip))
2157 if ((oip->ip6_nxt != IPPROTO_TCP) && (oip->ip6_nxt != IPPROTO_UDP) &&
2158 (oip->ip6_nxt != IPPROTO_ICMPV6))
2161 bzero((char *)&ofin, sizeof(ofin));
2162 ofin.fin_out = !fin->fin_out;
2163 ofin.fin_ifp = fin->fin_ifp;
2166 if (oip->ip6_nxt == IPPROTO_ICMPV6) {
2167 oic = (struct icmp6_hdr *)(oip + 1);
2169 * a ICMP error can only be generated as a result of an
2170 * ICMP query, not as the response on an ICMP error
2172 * XXX theoretically ICMP_ECHOREP and the other reply's are
2173 * ICMP query's as well, but adding them here seems strange XXX
2175 if (!(oic->icmp6_type & ICMP6_INFOMSG_MASK))
2179 * perform a lookup of the ICMP packet in the state table
2181 hv = (pr = oip->ip6_nxt);
2182 src.in6 = oip->ip6_src;
2183 hv += src.in4.s_addr;
2184 dst.in6 = oip->ip6_dst;
2185 hv += dst.in4.s_addr;
2186 hv += oic->icmp6_id;
2187 hv += oic->icmp6_seq;
2190 oip->ip6_plen = ntohs(oip->ip6_plen);
2191 fr_makefrip(sizeof(*oip), (ip_t *)oip, &ofin);
2192 oip->ip6_plen = htons(oip->ip6_plen);
2194 READ_ENTER(&ipf_state);
2195 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext)
2196 if ((is->is_p == pr) &&
2197 (oic->icmp6_id == is->is_icmp.ics_id) &&
2198 (oic->icmp6_seq == is->is_icmp.ics_seq) &&
2199 fr_matchsrcdst(is, src, dst, &ofin, NULL)) {
2201 * in the state table ICMP query's are stored
2202 * with the type of the corresponding ICMP
2203 * response. Correct here
2205 if (((is->is_type == ICMP6_ECHO_REPLY) &&
2206 (oic->icmp6_type == ICMP6_ECHO_REQUEST)) ||
2207 (is->is_type - 1 == oic->icmp6_type )) {
2208 ips_stats.iss_hits++;
2210 is->is_bytes += fin->fin_plen;
2214 RWLOCK_EXIT(&ipf_state);
2219 tcp = (tcphdr_t *)(oip + 1);
2220 dport = tcp->th_dport;
2221 sport = tcp->th_sport;
2223 hv = (pr = oip->ip6_nxt);
2224 src.in6 = oip->ip6_src;
2225 hv += src.in4.s_addr;
2229 dst.in6 = oip->ip6_dst;
2230 hv += dst.in4.s_addr;
2238 * we make an fin entry to be able to feed it to
2239 * matchsrcdst note that not all fields are encessary
2240 * but this is the cleanest way. Note further we fill
2241 * in fin_mp such that if someone uses it we'll get
2242 * a kernel panic. fr_matchsrcdst does not use this.
2244 * watch out here, as ip is in host order and oip in network
2245 * order. Any change we make must be undone afterwards.
2247 savelen = oip->ip6_plen;
2248 oip->ip6_plen = ip->ip6_plen - sizeof(*ip) - ICMPERR_ICMPHLEN;
2249 fr_makefrip(sizeof(*oip), (ip_t *)oip, &ofin);
2250 oip->ip6_plen = savelen;
2251 READ_ENTER(&ipf_state);
2252 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) {
2254 * Only allow this icmp though if the
2255 * encapsulated packet was allowed through the
2256 * other way around. Note that the minimal amount
2257 * of info present does not allow for checking against
2258 * tcp internals such as seq and ack numbers.
2260 if ((is->is_p == pr) && (is->is_v == 6) &&
2261 fr_matchsrcdst(is, src, dst, &ofin, tcp)) {
2263 ips_stats.iss_hits++;
2265 is->is_bytes += fin->fin_plen;
2267 * we deliberately do not touch the timeouts
2268 * for the accompanying state table entry.
2269 * It remains to be seen if that is correct. XXX
2271 RWLOCK_EXIT(&ipf_state);
2275 RWLOCK_EXIT(&ipf_state);