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.6 2004/02/12 22:35:47 joerg 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(__DragonFly__) || \
24 (defined(__FreeBSD_version) && \
25 __FreeBSD_version >= 400000)) && !defined(KLD_MODULE)
26 #include "opt_inet6.h"
28 #if !defined(_KERNEL) && !defined(KERNEL) && !defined(__KERNEL__)
34 # include <linux/kernel.h>
35 # include <linux/module.h>
38 #if (defined(KERNEL) || defined(_KERNEL)) && (defined(__DragonFly__) || __FreeBSD_version >= 220000)
39 # include <sys/filio.h>
40 # include <sys/fcntl.h>
41 # if (defined(__DragonFly__) || __FreeBSD_version >= 300000) && !defined(IPFILTER_LKM)
42 # include "opt_ipfilter.h"
45 # include <sys/ioctl.h>
49 # include <sys/protosw.h>
51 #include <sys/socket.h>
52 #if (defined(_KERNEL) || defined(KERNEL)) && !defined(linux)
53 # include <sys/systm.h>
55 #if !defined(__SVR4) && !defined(__svr4__)
57 # include <sys/mbuf.h>
60 # include <sys/filio.h>
61 # include <sys/byteorder.h>
63 # include <sys/dditypes.h>
65 # include <sys/stream.h>
66 # include <sys/kmem.h>
73 #include <net/route.h>
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/tcp.h>
79 # include <netinet/ip_var.h>
80 # include <netinet/tcp_fsm.h>
82 #include <netinet/udp.h>
83 #include <netinet/ip_icmp.h>
84 #include "ip_compat.h"
85 #include <netinet/tcpip.h>
91 #include <netinet/icmp6.h>
93 #if defined(__DragonFly__) || (__FreeBSD_version >= 300000)
94 # include <sys/malloc.h>
95 # if (defined(_KERNEL) || defined(KERNEL)) && !defined(IPFILTER_LKM)
96 # include <sys/libkern.h>
97 # include <sys/systm.h>
101 static const char sccsid[] = "@(#)ip_state.c 1.8 6/5/96 (C) 1993-2000 Darren Reed";
104 # define MIN(a,b) (((a)<(b))?(a):(b))
107 #define TCP_CLOSE (TH_FIN|TH_RST)
109 static ipstate_t **ips_table = NULL;
110 static int ips_num = 0;
111 static int ips_wild = 0;
112 static ips_stat_t ips_stats;
113 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
114 extern KRWLOCK_T ipf_state, ipf_mutex;
115 extern kmutex_t ipf_rw;
119 static frentry_t *fr_checkicmp6matchingstate (ip6_t *, fr_info_t *);
121 static int fr_matchsrcdst (ipstate_t *, union i6addr, union i6addr,
122 fr_info_t *, tcphdr_t *);
123 static frentry_t *fr_checkicmpmatchingstate (ip_t *, fr_info_t *);
124 static int fr_matchicmpqueryreply (int, ipstate_t *, icmphdr_t *, int);
125 static int fr_state_flush (int, int);
126 static ips_stat_t *fr_statetstats (void);
127 static void fr_delstate (ipstate_t *);
128 static int fr_state_remove (caddr_t);
129 static void fr_ipsmove (ipstate_t **, ipstate_t *, u_int);
130 static int fr_tcpoptions (tcphdr_t *);
131 int fr_stputent (caddr_t);
132 int fr_stgetent (caddr_t);
133 void fr_stinsert (ipstate_t *);
136 #define FIVE_DAYS (2 * 5 * 86400) /* 5 days: half closed session */
138 #define TCP_MSL 240 /* 2 minutes */
139 u_long fr_tcpidletimeout = FIVE_DAYS,
140 fr_tcpclosewait = 2 * TCP_MSL,
141 fr_tcplastack = 2 * TCP_MSL,
142 fr_tcptimeout = 2 * TCP_MSL,
144 fr_tcphalfclosed = 2 * 2 * 3600, /* 2 hours */
146 fr_udpacktimeout = 24,
147 fr_icmptimeout = 120,
148 fr_icmpacktimeout = 12;
149 int fr_statemax = IPSTATE_MAX,
150 fr_statesize = IPSTATE_SIZE;
151 int fr_state_doflush = 0,
153 ipstate_t *ips_list = NULL;
155 static int icmpreplytype4[ICMP_MAXTYPE + 1];
157 static int icmpreplytype6[ICMP6_MAXTYPE + 1];
164 KMALLOCS(ips_table, ipstate_t **, fr_statesize * sizeof(ipstate_t *));
165 if (ips_table != NULL)
166 bzero((char *)ips_table, fr_statesize * sizeof(ipstate_t *));
170 /* fill icmp reply type table */
171 for (i = 0; i <= ICMP_MAXTYPE; i++)
172 icmpreplytype4[i] = -1;
173 icmpreplytype4[ICMP_ECHO] = ICMP_ECHOREPLY;
174 icmpreplytype4[ICMP_TSTAMP] = ICMP_TSTAMPREPLY;
175 icmpreplytype4[ICMP_IREQ] = ICMP_IREQREPLY;
176 icmpreplytype4[ICMP_MASKREQ] = ICMP_MASKREPLY;
178 /* fill icmp reply type table */
179 for (i = 0; i <= ICMP6_MAXTYPE; i++)
180 icmpreplytype6[i] = -1;
181 icmpreplytype6[ICMP6_ECHO_REQUEST] = ICMP6_ECHO_REPLY;
182 icmpreplytype6[ICMP6_MEMBERSHIP_QUERY] = ICMP6_MEMBERSHIP_REPORT;
183 icmpreplytype6[ICMP6_NI_QUERY] = ICMP6_NI_REPLY;
184 icmpreplytype6[ND_ROUTER_SOLICIT] = ND_ROUTER_ADVERT;
185 icmpreplytype6[ND_NEIGHBOR_SOLICIT] = ND_NEIGHBOR_ADVERT;
192 static ips_stat_t *fr_statetstats()
194 ips_stats.iss_active = ips_num;
195 ips_stats.iss_table = ips_table;
196 ips_stats.iss_list = ips_list;
202 * flush state tables. two actions currently defined:
203 * which == 0 : flush all state table entries
204 * which == 1 : flush TCP connections which have started to close but are
205 * stuck for some reason.
206 * which == 2 : flush TCP connections which have been idle for a long time,
207 * starting at > 4 days idle and working back in successive half-
208 * days to at most 12 hours old.
210 static int fr_state_flush(which, proto)
213 ipstate_t *is, **isp;
214 #if defined(_KERNEL) && !SOLARIS
217 int delete, removed = 0, try;
220 for (isp = &ips_list; (is = *isp); ) {
223 if ((proto != 0) && (is->is_v != proto))
233 if (is->is_p != IPPROTO_TCP)
235 if ((is->is_state[0] != TCPS_ESTABLISHED) ||
236 (is->is_state[1] != TCPS_ESTABLISHED))
242 if (is->is_p == IPPROTO_TCP)
245 ips_stats.iss_expire++;
247 ipstate_log(is, ISL_FLUSH);
256 * Asked to remove inactive entries, try again if first attempt
257 * failed. In this case, 86400 is half a day because the counter is
258 * activated every half second.
260 if ((which == 2) && (removed == 0)) {
261 try = 86400; /* half a day */
262 for (; (try < FIVE_DAYS) && (removed == 0); try += 86400) {
263 for (isp = &ips_list; (is = *isp); ) {
265 if ((is->is_p == IPPROTO_TCP) &&
266 ((is->is_state[0] == TCPS_ESTABLISHED) ||
267 (is->is_state[1] == TCPS_ESTABLISHED)) &&
268 (is->is_age < try)) {
271 } else if ((is->is_p != IPPROTO_TCP) &&
273 ips_stats.iss_expire++;
278 ipstate_log(is, ISL_FLUSH);
293 static int fr_state_remove(data)
300 error = IRCOPYPTR(data, (caddr_t)&st, sizeof(st));
304 WRITE_ENTER(&ipf_state);
305 for (sp = ips_list; sp; sp = sp->is_next)
306 if ((sp->is_p == st.is_p) && (sp->is_v == st.is_v) &&
307 !bcmp((char *)&sp->is_src, (char *)&st.is_src,
308 sizeof(st.is_src)) &&
309 !bcmp((char *)&sp->is_dst, (char *)&st.is_dst,
310 sizeof(st.is_dst)) &&
311 !bcmp((char *)&sp->is_ps, (char *)&st.is_ps,
314 ipstate_log(sp, ISL_REMOVE);
317 RWLOCK_EXIT(&ipf_state);
320 RWLOCK_EXIT(&ipf_state);
325 int fr_state_ioctl(data, cmd, mode)
327 #if defined(__NetBSD__) || defined(__OpenBSD__)
334 int arg, ret, error = 0;
339 error = fr_state_remove(data);
342 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
345 if (arg == 0 || arg == 1) {
346 WRITE_ENTER(&ipf_state);
347 ret = fr_state_flush(arg, 4);
348 RWLOCK_EXIT(&ipf_state);
349 error = IWCOPY((caddr_t)&ret, data, sizeof(ret));
355 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
358 if (arg == 0 || arg == 1) {
359 WRITE_ENTER(&ipf_state);
360 ret = fr_state_flush(arg, 6);
361 RWLOCK_EXIT(&ipf_state);
362 error = IWCOPY((caddr_t)&ret, data, sizeof(ret));
369 if (!(mode & FWRITE))
374 tmp = ipflog_clear(IPL_LOGSTATE);
375 IWCOPY((char *)&tmp, data, sizeof(tmp));
380 error = IWCOPYPTR((caddr_t)fr_statetstats(), data,
385 arg = (int)iplused[IPL_LOGSTATE];
386 error = IWCOPY((caddr_t)&arg, (caddr_t)data, sizeof(arg));
390 error = fr_lock(data, &fr_state_lock);
393 if (!fr_state_lock) {
397 error = fr_stputent(data);
400 if (!fr_state_lock) {
404 error = fr_stgetent(data);
415 * Copy out state information from the kernel to a user space process.
417 int fr_stgetent(data)
424 error = IRCOPYPTR(data, (caddr_t)&ips, sizeof(ips));
432 if (ips.ips_next == NULL)
438 * Make sure the pointer we're copying from exists in the
439 * current list of entries. Security precaution to prevent
440 * copying of random kernel data.
442 for (is = ips_list; is; is = is->is_next)
448 ips.ips_next = isn->is_next;
449 bcopy((char *)isn, (char *)&ips.ips_is, sizeof(ips.ips_is));
451 bcopy((char *)isn->is_rule, (char *)&ips.ips_fr,
453 error = IWCOPYPTR((caddr_t)&ips, data, sizeof(ips));
460 int fr_stputent(data)
469 error = IRCOPYPTR(data, (caddr_t)&ips, sizeof(ips));
473 KMALLOC(isn, ipstate_t *);
477 bcopy((char *)&ips.ips_is, (char *)isn, sizeof(*isn));
480 if (isn->is_flags & FI_NEWFR) {
481 KMALLOC(fr, frentry_t *);
486 bcopy((char *)&ips.ips_fr, (char *)fr, sizeof(*fr));
487 out = fr->fr_flags & FR_OUTQUE ? 1 : 0;
489 ips.ips_is.is_rule = fr;
492 * Look up all the interface names in the rule.
494 for (i = 0; i < 4; i++) {
495 name = fr->fr_ifnames[i];
496 if ((name[1] == '\0') &&
497 ((name[0] == '-') || (name[0] == '*'))) {
498 fr->fr_ifas[i] = NULL;
499 } else if (*name != '\0') {
500 fr->fr_ifas[i] = GETUNIT(name,
502 if (fr->fr_ifas[i] == NULL)
503 fr->fr_ifas[i] = (void *)-1;
505 strncpy(isn->is_ifname[i],
506 IFNAME(fr->fr_ifas[i]),
510 isn->is_ifp[out] = fr->fr_ifas[i];
514 * send a copy back to userland of what we ended up
515 * to allow for verification.
517 error = IWCOPYPTR((caddr_t)&ips, data, sizeof(ips));
524 for (is = ips_list; is; is = is->is_next)
525 if (is->is_rule == fr)
539 * Insert a state table entry manually.
544 u_int hv = is->is_hv;
548 MUTEX_INIT(&is->is_lock, "ipf state entry", NULL);
551 * Look up all the interface names in the state entry.
553 for (i = 0; i < 4; i++) {
554 name = is->is_ifname[i];
555 if ((name[1] == '\0') &&
556 ((name[0] == '-') || (name[0] == '*'))) {
557 is->is_ifp[0] = NULL;
558 } else if (*name != '\0') {
559 is->is_ifp[i] = GETUNIT(name, is->is_v);
560 if (is->is_ifp[i] == NULL)
561 is->is_ifp[i] = (void *)-1;
567 * add into list table.
570 ips_list->is_pnext = &is->is_next;
571 is->is_pnext = &ips_list;
572 is->is_next = ips_list;
575 ips_table[hv]->is_phnext = &is->is_hnext;
577 ips_stats.iss_inuse++;
578 is->is_phnext = ips_table + hv;
579 is->is_hnext = ips_table[hv];
586 * Create a new ipstate structure and hang it off the hash table.
588 ipstate_t *fr_addstate(ip, fin, stsave, flags)
594 tcphdr_t *tcp = NULL;
603 if (fr_state_lock || (fin->fin_off != 0) || (fin->fin_fl & FI_SHORT) ||
604 (fin->fin_misc & FM_BADSTATE))
606 if (ips_num == fr_statemax) {
608 fr_state_doflush = 1;
613 bzero((char *)is, sizeof(*is));
616 * Copy and calculate...
618 hv = (is->is_p = fin->fin_fi.fi_p);
619 is->is_src = fin->fin_fi.fi_src;
621 is->is_dst = fin->fin_fi.fi_dst;
624 if (fin->fin_v == 6) {
625 if ((is->is_p == IPPROTO_ICMPV6) &&
626 IN6_IS_ADDR_MULTICAST(&is->is_dst.in6)) {
628 * So you can do keep state with neighbour discovery.
633 hv += is->is_dst.i6[1];
634 hv += is->is_dst.i6[2];
635 hv += is->is_dst.i6[3];
637 hv += is->is_src.i6[1];
638 hv += is->is_src.i6[2];
639 hv += is->is_src.i6[3];
648 case IPPROTO_ICMPV6 :
649 ic = (struct icmp *)fin->fin_dp;
650 if ((ic->icmp_type & ICMP6_INFOMSG_MASK) == 0)
653 switch (ic->icmp_type)
655 case ICMP6_ECHO_REQUEST :
656 is->is_icmp.ics_type = ic->icmp_type;
657 hv += (is->is_icmp.ics_id = ic->icmp_id);
658 hv += (is->is_icmp.ics_seq = ic->icmp_seq);
660 case ICMP6_MEMBERSHIP_QUERY :
661 case ND_ROUTER_SOLICIT :
662 case ND_NEIGHBOR_SOLICIT :
663 case ICMP6_NI_QUERY :
664 is->is_icmp.ics_type = ic->icmp_type;
669 ATOMIC_INCL(ips_stats.iss_icmp);
670 is->is_age = fr_icmptimeout;
674 ic = (struct icmp *)fin->fin_dp;
676 switch (ic->icmp_type)
682 is->is_icmp.ics_type = ic->icmp_type;
683 hv += (is->is_icmp.ics_id = ic->icmp_id);
684 hv += (is->is_icmp.ics_seq = ic->icmp_seq);
689 ATOMIC_INCL(ips_stats.iss_icmp);
690 is->is_age = fr_icmptimeout;
693 tcp = (tcphdr_t *)fin->fin_dp;
695 if (tcp->th_flags & TH_RST)
698 * The endian of the ports doesn't matter, but the ack and
699 * sequence numbers do as we do mathematics on them later.
701 is->is_sport = htons(fin->fin_data[0]);
702 is->is_dport = htons(fin->fin_data[1]);
703 if ((flags & (FI_W_DPORT|FI_W_SPORT)) == 0) {
707 if ((flags & FI_IGNOREPKT) == 0) {
708 is->is_send = ntohl(tcp->th_seq) + fin->fin_dlen -
709 (off = (tcp->th_off << 2)) +
710 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
711 ((tcp->th_flags & TH_FIN) ? 1 : 0);
712 is->is_maxsend = is->is_send;
714 if ((tcp->th_flags & TH_SYN) &&
715 ((tcp->th_off << 2) >= (sizeof(*tcp) + 4))) {
716 ws = fr_tcpoptions(tcp);
723 is->is_maxswin = ntohs(tcp->th_win);
724 if (is->is_maxswin == 0)
727 if ((tcp->th_flags & TH_OPENING) == TH_SYN)
731 * If we're creating state for a starting connection, start the
732 * timer on it as we'll never see an error if it fails to
735 ATOMIC_INCL(ips_stats.iss_tcp);
739 tcp = (tcphdr_t *)fin->fin_dp;
741 is->is_sport = htons(fin->fin_data[0]);
742 is->is_dport = htons(fin->fin_data[1]);
743 if ((flags & (FI_W_DPORT|FI_W_SPORT)) == 0) {
747 ATOMIC_INCL(ips_stats.iss_udp);
748 is->is_age = fr_udptimeout;
751 is->is_age = fr_udptimeout;
755 KMALLOC(is, ipstate_t *);
757 ATOMIC_INCL(ips_stats.iss_nomem);
760 bcopy((char *)&ips, (char *)is, sizeof(*is));
763 is->is_rule = fin->fin_fr;
764 if (is->is_rule != NULL) {
765 is->is_group = is->is_rule->fr_group;
766 ATOMIC_INC32(is->is_rule->fr_ref);
767 pass = is->is_rule->fr_flags;
768 is->is_frage[0] = is->is_rule->fr_age[0];
769 is->is_frage[1] = is->is_rule->fr_age[1];
770 if (is->is_frage[0] != 0)
771 is->is_age = is->is_frage[0];
773 is->is_ifp[(out << 1) + 1] = is->is_rule->fr_ifas[1];
774 is->is_ifp[(1 - out) << 1] = is->is_rule->fr_ifas[2];
775 is->is_ifp[((1 - out) << 1) + 1] = is->is_rule->fr_ifas[3];
777 if (((ifp = is->is_rule->fr_ifas[1]) != NULL) &&
779 strncpy(is->is_ifname[(out << 1) + 1],
780 IFNAME(ifp), IFNAMSIZ);
781 if (((ifp = is->is_rule->fr_ifas[2]) != NULL) &&
783 strncpy(is->is_ifname[(1 - out) << 1],
784 IFNAME(ifp), IFNAMSIZ);
785 if (((ifp = is->is_rule->fr_ifas[3]) != NULL) &&
787 strncpy(is->is_ifname[((1 - out) << 1) + 1],
788 IFNAME(ifp), IFNAMSIZ);
792 is->is_ifp[out << 1] = fin->fin_ifp;
793 strncpy(is->is_ifname[out << 1], IFNAME(fin->fin_ifp), IFNAMSIZ);
795 WRITE_ENTER(&ipf_state);
798 if ((flags & FI_IGNOREPKT) == 0) {
800 is->is_bytes = fin->fin_dlen + fin->fin_hlen;
803 * We want to check everything that is a property of this packet,
804 * but we don't (automatically) care about it's fragment status as
807 is->is_v = fin->fin_v;
808 is->is_rulen = fin->fin_rule;
809 is->is_opt = fin->fin_fi.fi_optmsk;
810 is->is_optmsk = 0xffffffff;
811 is->is_sec = fin->fin_fi.fi_secmsk;
812 is->is_secmsk = 0xffff;
813 is->is_auth = fin->fin_fi.fi_auth;
814 is->is_authmsk = 0xffff;
815 is->is_flags = fin->fin_fl & FI_CMP;
816 is->is_flags |= FI_CMP << 4;
817 is->is_flags |= flags & (FI_WILDP|FI_WILDA);
818 if (flags & (FI_WILDP|FI_WILDA))
821 if (pass & FR_LOGFIRST)
822 is->is_pass &= ~(FR_LOGFIRST|FR_LOG);
825 if (is->is_p == IPPROTO_TCP) {
826 fr_tcp_age(&is->is_age, is->is_state, fin,
827 0, is->is_fsm); /* 0 = packet from the source */
830 ipstate_log(is, ISL_NEW);
832 RWLOCK_EXIT(&ipf_state);
833 fin->fin_rev = IP6NEQ(is->is_dst, fin->fin_fi.fi_dst);
834 if ((fin->fin_fl & FI_FRAG) && (pass & FR_KEEPFRAG))
835 ipfr_newfrag(ip, fin);
840 static int fr_tcpoptions(tcp)
846 opt = (u_char *) (tcp + 1);
847 last = ((u_char *)tcp) + (tcp->th_off << 2);
849 /* If we don't find wscale here, we need to clear it */
852 /* Termination condition picked such that opt[0 .. 2] exist */
853 while ((opt < last - 2) && (*opt != TCPOPT_EOL)) {
859 /* Proper length ? */
868 /* Unknown options must be two bytes+ */
882 * check to see if a packet with TCP headers fits within the TCP window.
883 * change timeout depending on whether new packet is a SYN-ACK returning for a
884 * SYN or a RST or FIN which indicate time to close up shop.
886 int fr_tcpstate(is, fin, ip, tcp)
892 tcp_seq seq, ack, end;
894 tcpdata_t *fdata, *tdata;
901 * Find difference between last checked packet and this packet.
903 source = IP6EQ(fin->fin_fi.fi_src, is->is_src);
904 if (source && (ntohs(is->is_sport) != fin->fin_data[0]))
906 fdata = &is->is_tcp.ts_data[!source];
907 tdata = &is->is_tcp.ts_data[source];
908 off = tcp->th_off << 2;
909 seq = ntohl(tcp->th_seq);
910 ack = ntohl(tcp->th_ack);
911 win = ntohs(tcp->th_win);
912 end = seq + fin->fin_dlen - off +
913 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
914 ((tcp->th_flags & TH_FIN) ? 1 : 0);
917 if ((tcp->th_flags & TH_SYN) && (off >= sizeof(*tcp) + 4))
918 wscale = fr_tcpoptions(tcp);
922 MUTEX_ENTER(&is->is_lock);
925 fdata->td_wscale = wscale;
926 else if (wscale == -2)
927 fdata->td_wscale = tdata->td_wscale = 0;
928 win <<= fdata->td_wscale;
930 if ((fdata->td_end == 0) &&
931 (!is->is_fsm || ((tcp->th_flags & TH_OPENING) == TH_OPENING))) {
933 * Must be a (outgoing) SYN-ACK in reply to a SYN.
936 fdata->td_maxwin = 1;
937 fdata->td_maxend = end + win;
942 if (!(tcp->th_flags & TH_ACK)) { /* Pretend an ack was sent */
944 } else if (((tcp->th_flags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) &&
946 /* gross hack to get around certain broken tcp stacks */
951 seq = end = fdata->td_end;
953 maxwin = tdata->td_maxwin;
954 ackskew = tdata->td_end - ack;
956 #define SEQ_GE(a,b) ((int)((a) - (b)) >= 0)
957 #define SEQ_GT(a,b) ((int)((a) - (b)) > 0)
958 if ((SEQ_GE(fdata->td_maxend, end)) &&
959 (SEQ_GE(seq, fdata->td_end - maxwin)) &&
960 /* XXX what about big packets */
961 #define MAXACKWINDOW 66000
962 (ackskew >= -MAXACKWINDOW) &&
963 (ackskew <= MAXACKWINDOW)) {
964 /* if ackskew < 0 then this should be due to fragented
965 * packets. There is no way to know the length of the
966 * total packet in advance.
967 * We do know the total length from the fragment cache though.
968 * Note however that there might be more sessions with
969 * exactly the same source and destination paramters in the
970 * state cache (and source and destination is the only stuff
971 * that is saved in the fragment cache). Note further that
972 * some TCP connections in the state cache are hashed with
973 * sport and dport as well which makes it not worthwhile to
975 * Thus, when ackskew is negative but still seems to belong
976 * to this session, we bump up the destinations end value.
979 * Nearing end of connection, start timeout.
981 /* source ? 0 : 1 -> !source */
982 if (fr_tcp_age(&is->is_age, is->is_state, fin, !source,
983 (int)is->is_fsm) == 0) {
987 /* update max window seen */
988 if (fdata->td_maxwin < win)
989 fdata->td_maxwin = win;
990 if (SEQ_GT(end, fdata->td_end))
992 if (SEQ_GE(ack + win, tdata->td_maxend)) {
993 tdata->td_maxend = ack + win;
998 ATOMIC_INCL(ips_stats.iss_hits);
1002 MUTEX_EXIT(&is->is_lock);
1003 if ((ret == 0) && ((tcp->th_flags & TH_OPENING) != TH_SYN))
1004 fin->fin_misc |= FM_BADSTATE;
1010 * Match a state table entry against an IP packet.
1012 static int fr_matchsrcdst(is, src, dst, fin, tcp)
1014 union i6addr src, dst;
1018 int ret = 0, rev, out, flags, idx;
1022 rev = IP6NEQ(is->is_dst, dst);
1025 flags = is->is_flags & (FI_WILDA|FI_WILDP);
1030 flags = is->is_flags;
1034 if (!(flags & FI_W_SPORT) && (sp != is->is_sport))
1036 else if (!(flags & FI_W_DPORT) && (dp != is->is_dport))
1041 idx = (out << 1) + rev;
1043 if ((is->is_ifp[idx] == NULL &&
1044 (*is->is_ifname[idx] == '\0' || *is->is_ifname[idx] == '*')) ||
1045 is->is_ifp[idx] == ifp)
1053 if ((IP6EQ(is->is_dst, dst) || (flags & FI_W_DADDR)) &&
1054 (IP6EQ(is->is_src, src) || (flags & FI_W_SADDR)) &&
1055 (!tcp || ((sp == is->is_sport || flags & FI_W_SPORT) &&
1056 (dp == is->is_dport || flags & FI_W_DPORT)))) {
1060 if ((IP6EQ(is->is_dst, src) || (flags & FI_W_DADDR)) &&
1061 (IP6EQ(is->is_src, dst) || (flags & FI_W_SADDR)) &&
1062 (!tcp || ((sp == is->is_dport || flags & FI_W_DPORT) &&
1063 (dp == is->is_sport || flags & FI_W_SPORT)))) {
1071 * Whether or not this should be here, is questionable, but the aim
1072 * is to get this out of the main line.
1075 flags = is->is_flags & (FI_CMP|(FI_CMP<<4));
1077 if (((fin->fin_fl & (flags >> 4)) != (flags & FI_CMP)) ||
1078 (fin->fin_fi.fi_optmsk != is->is_opt) ||
1079 (fin->fin_fi.fi_secmsk != is->is_sec) ||
1080 (fin->fin_fi.fi_auth != is->is_auth))
1083 flags = is->is_flags & (FI_WILDA|FI_WILDP);
1084 if ((flags & (FI_W_SADDR|FI_W_DADDR))) {
1085 if ((flags & FI_W_SADDR) != 0) {
1087 is->is_src = fin->fin_fi.fi_src;
1089 is->is_src = fin->fin_fi.fi_dst;
1091 } else if ((flags & FI_W_DADDR) != 0) {
1093 is->is_dst = fin->fin_fi.fi_dst;
1095 is->is_dst = fin->fin_fi.fi_src;
1098 is->is_flags &= ~(FI_W_SADDR|FI_W_DADDR);
1099 if ((is->is_flags & (FI_WILDA|FI_WILDP)) == 0)
1103 if ((flags & (FI_W_SPORT|FI_W_DPORT))) {
1104 if ((flags & FI_W_SPORT) != 0) {
1107 is->is_send = htonl(tcp->th_seq);
1110 is->is_send = htonl(tcp->th_ack);
1112 is->is_maxsend = is->is_send + 1;
1113 } else if ((flags & FI_W_DPORT) != 0) {
1116 is->is_dend = htonl(tcp->th_ack);
1119 is->is_dend = htonl(tcp->th_seq);
1121 is->is_maxdend = is->is_dend + 1;
1123 is->is_flags &= ~(FI_W_SPORT|FI_W_DPORT);
1129 if (is->is_ifp[idx] == NULL &&
1130 (*is->is_ifname[idx] == '\0' || *is->is_ifname[idx] == '*'))
1134 is->is_ifp[ret] = ifp;
1135 strncpy(is->is_ifname[ret], IFNAME(ifp),
1136 sizeof(is->is_ifname[ret]));
1142 static int fr_matchicmpqueryreply(v, is, icmp, rev)
1149 * If we matched its type on the way in, then when going out
1150 * it will still be the same type.
1152 if ((!rev && (icmp->icmp_type == is->is_type)) ||
1153 (rev && (icmpreplytype4[is->is_type] == icmp->icmp_type))) {
1154 if (icmp->icmp_type != ICMP_ECHOREPLY)
1156 if ((icmp->icmp_id == is->is_icmp.ics_id) &&
1157 (icmp->icmp_seq == is->is_icmp.ics_seq))
1162 else if (is->is_v == 6) {
1163 if ((!rev && (icmp->icmp_type == is->is_type)) ||
1164 (rev && (icmpreplytype6[is->is_type] == icmp->icmp_type))) {
1165 if (icmp->icmp_type != ICMP6_ECHO_REPLY)
1167 if ((icmp->icmp_id == is->is_icmp.ics_id) &&
1168 (icmp->icmp_seq == is->is_icmp.ics_seq))
1176 static frentry_t *fr_checkicmpmatchingstate(ip, fin)
1180 ipstate_t *is, **isp;
1181 u_short sport, dport;
1183 u_short savelen, ohlen;
1184 union i6addr dst, src;
1195 * Does it at least have the return (basic) IP header ?
1196 * Only a basic IP header (no options) should be with
1197 * an ICMP error header.
1199 if (((ip->ip_v != 4) || (ip->ip_hl != 5)) ||
1200 (fin->fin_plen < ICMPERR_MINPKTLEN))
1203 ic = (struct icmp *)fin->fin_dp;
1204 type = ic->icmp_type;
1206 * If it's not an error type, then return
1208 if ((type != ICMP_UNREACH) && (type != ICMP_SOURCEQUENCH) &&
1209 (type != ICMP_REDIRECT) && (type != ICMP_TIMXCEED) &&
1210 (type != ICMP_PARAMPROB))
1213 oip = (ip_t *)((char *)ic + ICMPERR_ICMPHLEN);
1214 ohlen = oip->ip_hl << 2;
1215 if (fin->fin_plen < ICMPERR_MAXPKTLEN + ohlen - sizeof(*oip))
1221 len = fin->fin_dlen - ICMPERR_ICMPHLEN;
1222 if ((len <= 0) || (ohlen > len))
1226 * Is the buffer big enough for all of it ? It's the size of the IP
1227 * header claimed in the encapsulated part which is of concern. It
1228 * may be too big to be in this buffer but not so big that it's
1229 * outside the ICMP packet, leading to TCP deref's causing problems.
1230 * This is possible because we don't know how big oip_hl is when we
1231 * do the pullup early in fr_check() and thus can't gaurantee it is
1240 if ((char *)oip + len > (char *)m->b_wptr)
1243 m = *(mb_t **)fin->fin_mp;
1244 if ((char *)oip + len > (char *)ip + m->m_len)
1251 * in the IPv4 case we must zero the i6addr union otherwise
1252 * the IP6EQ and IP6NEQ macros produce the wrong results because
1253 * of the 'junk' in the unused part of the union
1255 bzero((char *)&src, sizeof(src));
1256 bzero((char *)&dst, sizeof(dst));
1257 bzero((char *)&ofin, sizeof(ofin));
1258 ofin.fin_ifp = fin->fin_ifp;
1259 ofin.fin_out = !fin->fin_out;
1266 icmp = (icmphdr_t *)((char *)oip + ohlen);
1269 * a ICMP error can only be generated as a result of an
1270 * ICMP query, not as the response on an ICMP error
1272 * XXX theoretically ICMP_ECHOREP and the other reply's are
1273 * ICMP query's as well, but adding them here seems strange XXX
1275 if ((icmp->icmp_type != ICMP_ECHO) &&
1276 (icmp->icmp_type != ICMP_TSTAMP) &&
1277 (icmp->icmp_type != ICMP_IREQ) &&
1278 (icmp->icmp_type != ICMP_MASKREQ))
1282 * perform a lookup of the ICMP packet in the state table
1284 hv = (pr = oip->ip_p);
1285 src.in4 = oip->ip_src;
1286 hv += src.in4.s_addr;
1287 dst.in4 = oip->ip_dst;
1288 hv += dst.in4.s_addr;
1289 hv += icmp->icmp_id;
1290 hv += icmp->icmp_seq;
1293 savelen = oip->ip_len;
1295 fr_makefrip(ohlen, oip, &ofin);
1296 oip->ip_len = savelen;
1298 READ_ENTER(&ipf_state);
1299 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext)
1300 if ((is->is_p == pr) && (is->is_v == 4) &&
1301 fr_matchsrcdst(is, src, dst, &ofin, NULL) &&
1302 fr_matchicmpqueryreply(is->is_v, is, icmp, fin->fin_rev)) {
1303 ips_stats.iss_hits++;
1305 is->is_bytes += ip->ip_len;
1309 RWLOCK_EXIT(&ipf_state);
1314 if (fin->fin_plen < ICMPERR_MAXPKTLEN)
1321 tcp = (tcphdr_t *)((char *)oip + ohlen);
1322 dport = tcp->th_dport;
1323 sport = tcp->th_sport;
1325 hv = (pr = oip->ip_p);
1326 src.in4 = oip->ip_src;
1327 hv += src.in4.s_addr;
1328 dst.in4 = oip->ip_dst;
1329 hv += dst.in4.s_addr;
1334 * we make an fin entry to be able to feed it to
1335 * matchsrcdst note that not all fields are encessary
1336 * but this is the cleanest way. Note further we fill
1337 * in fin_mp such that if someone uses it we'll get
1338 * a kernel panic. fr_matchsrcdst does not use this.
1340 * watch out here, as ip is in host order and oip in network
1341 * order. Any change we make must be undone afterwards.
1343 savelen = oip->ip_len;
1345 fr_makefrip(ohlen, oip, &ofin);
1346 oip->ip_len = savelen;
1347 READ_ENTER(&ipf_state);
1348 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) {
1350 * Only allow this icmp though if the
1351 * encapsulated packet was allowed through the
1352 * other way around. Note that the minimal amount
1353 * of info present does not allow for checking against
1354 * tcp internals such as seq and ack numbers.
1356 if ((is->is_p == pr) && (is->is_v == 4) &&
1357 fr_matchsrcdst(is, src, dst, &ofin, tcp)) {
1359 ips_stats.iss_hits++;
1361 is->is_bytes += fin->fin_plen;
1363 * we deliberately do not touch the timeouts
1364 * for the accompanying state table entry.
1365 * It remains to be seen if that is correct. XXX
1370 RWLOCK_EXIT(&ipf_state);
1376 * Move a state hash table entry from its old location at is->is_hv to
1377 * its new location, indexed by hv % fr_statesize.
1379 static void fr_ipsmove(isp, is, hv)
1380 ipstate_t **isp, *is;
1387 * Remove the hash from the old location...
1390 is->is_hnext->is_phnext = isp;
1391 *isp = is->is_hnext;
1392 if (ips_table[hvm] == NULL)
1393 ips_stats.iss_inuse--;
1396 * ...and put the hash in the new one.
1398 hvm = hv % fr_statesize;
1400 isp = &ips_table[hvm];
1402 (*isp)->is_phnext = &is->is_hnext;
1404 ips_stats.iss_inuse++;
1405 is->is_phnext = isp;
1406 is->is_hnext = *isp;
1412 * Check if a packet has a registered state.
1414 frentry_t *fr_checkstate(ip, fin)
1418 union i6addr dst, src;
1419 ipstate_t *is, **isp;
1421 u_int hv, hvm, hlen, tryagain, pass, v;
1427 if ((ips_list == NULL) || (fin->fin_off != 0) || fr_state_lock ||
1428 (fin->fin_fl & FI_SHORT))
1432 hlen = fin->fin_hlen;
1433 tcp = (tcphdr_t *)((char *)ip + hlen);
1434 ic = (struct icmp *)tcp;
1435 hv = (pr = fin->fin_fi.fi_p);
1436 src = fin->fin_fi.fi_src;
1437 dst = fin->fin_fi.fi_dst;
1438 hv += src.in4.s_addr;
1439 hv += dst.in4.s_addr;
1442 * Search the hash table for matching packet header info.
1443 * At the bottom of this switch statement, the following is expected:
1444 * is == NULL, no lock on ipf_state is held.
1445 * is != NULL, a lock on ipf_state is held.
1447 v = fin->fin_fi.fi_v;
1450 hv += fin->fin_fi.fi_src.i6[1];
1451 hv += fin->fin_fi.fi_src.i6[2];
1452 hv += fin->fin_fi.fi_src.i6[3];
1454 if ((fin->fin_p == IPPROTO_ICMPV6) &&
1455 IN6_IS_ADDR_MULTICAST(&fin->fin_fi.fi_dst.in6)) {
1456 hv -= dst.in4.s_addr;
1458 hv += fin->fin_fi.fi_dst.i6[1];
1459 hv += fin->fin_fi.fi_dst.i6[2];
1460 hv += fin->fin_fi.fi_dst.i6[3];
1468 case IPPROTO_ICMPV6 :
1472 if ((ic->icmp_type == ICMP6_ECHO_REQUEST) ||
1473 (ic->icmp_type == ICMP6_ECHO_REPLY)) {
1478 READ_ENTER(&ipf_state);
1480 hvm = hv % fr_statesize;
1481 for (isp = &ips_table[hvm]; (is = *isp); isp = &is->is_hnext)
1482 if ((is->is_p == pr) && (is->is_v == v) &&
1483 fr_matchsrcdst(is, src, dst, fin, NULL) &&
1484 fr_matchicmpqueryreply(v, is, ic, fin->fin_rev)) {
1486 if (is->is_frage[rev] != 0)
1487 is->is_age = is->is_frage[rev];
1489 is->is_age = fr_icmpacktimeout;
1491 is->is_age = fr_icmptimeout;
1496 if (tryagain && !(is->is_flags & FI_W_DADDR)) {
1497 hv += fin->fin_fi.fi_src.i6[0];
1498 hv += fin->fin_fi.fi_src.i6[1];
1499 hv += fin->fin_fi.fi_src.i6[2];
1500 hv += fin->fin_fi.fi_src.i6[3];
1501 fr_ipsmove(isp, is, hv);
1502 MUTEX_DOWNGRADE(&ipf_state);
1506 RWLOCK_EXIT(&ipf_state);
1509 * No matching icmp state entry. Perhaps this is a
1510 * response to another state entry.
1512 if ((ips_wild != 0) && (v == 6) && (tryagain == 0) &&
1513 !IN6_IS_ADDR_MULTICAST(&fin->fin_fi.fi_src.in6)) {
1514 hv -= fin->fin_fi.fi_src.i6[0];
1515 hv -= fin->fin_fi.fi_src.i6[1];
1516 hv -= fin->fin_fi.fi_src.i6[2];
1517 hv -= fin->fin_fi.fi_src.i6[3];
1519 WRITE_ENTER(&ipf_state);
1523 fr = fr_checkicmp6matchingstate((ip6_t *)ip, fin);
1534 hvm = hv % fr_statesize;
1535 READ_ENTER(&ipf_state);
1536 for (isp = &ips_table[hvm]; (is = *isp); isp = &is->is_hnext)
1537 if ((is->is_p == pr) && (is->is_v == v) &&
1538 fr_matchsrcdst(is, src, dst, fin, NULL) &&
1539 fr_matchicmpqueryreply(v, is, ic, fin->fin_rev)) {
1541 if (is->is_frage[rev] != 0)
1542 is->is_age = is->is_frage[rev];
1543 else if (fin->fin_rev)
1544 is->is_age = fr_icmpacktimeout;
1546 is->is_age = fr_icmptimeout;
1552 RWLOCK_EXIT(&ipf_state);
1554 * No matching icmp state entry. Perhaps this is a
1555 * response to another state entry.
1557 fr = fr_checkicmpmatchingstate(ip, fin);
1563 * Just plain ignore RST flag set with either FIN or SYN.
1565 if ((tcp->th_flags & TH_RST) &&
1566 ((tcp->th_flags & (TH_FIN|TH_SYN|TH_RST)) != TH_RST))
1570 u_short dport, sport;
1572 dport = tcp->th_dport;
1573 sport = tcp->th_sport;
1577 READ_ENTER(&ipf_state);
1579 hvm = hv % fr_statesize;
1580 for (isp = &ips_table[hvm]; (is = *isp); isp = &is->is_hnext)
1581 if ((is->is_p == pr) && (is->is_v == v) &&
1582 fr_matchsrcdst(is, src, dst, fin, tcp)) {
1584 if ((pr == IPPROTO_TCP)) {
1585 if (!fr_tcpstate(is, fin, ip, tcp))
1587 } else if ((pr == IPPROTO_UDP)) {
1588 if (is->is_frage[rev] != 0)
1589 is->is_age = is->is_frage[rev];
1590 else if (fin->fin_rev)
1591 is->is_age = fr_udpacktimeout;
1593 is->is_age = fr_udptimeout;
1599 !(is->is_flags & (FI_WILDP|FI_WILDA))) {
1602 fr_ipsmove(isp, is, hv);
1603 MUTEX_DOWNGRADE(&ipf_state);
1608 RWLOCK_EXIT(&ipf_state);
1609 if (!tryagain && ips_wild) {
1613 WRITE_ENTER(&ipf_state);
1621 READ_ENTER(&ipf_state);
1622 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) {
1623 if ((is->is_p == pr) && (is->is_v == v) &&
1624 fr_matchsrcdst(is, src, dst, fin, NULL)) {
1626 if (is->is_frage[rev] != 0)
1627 is->is_age = is->is_frage[rev];
1629 is->is_age = fr_udptimeout;
1634 RWLOCK_EXIT(&ipf_state);
1640 ATOMIC_INCL(ips_stats.iss_miss);
1644 MUTEX_ENTER(&is->is_lock);
1645 is->is_bytes += fin->fin_plen;
1646 ips_stats.iss_hits++;
1648 MUTEX_EXIT(&is->is_lock);
1650 fin->fin_rule = is->is_rulen;
1652 fin->fin_group = fr->fr_group;
1653 fin->fin_icode = fr->fr_icode;
1657 RWLOCK_EXIT(&ipf_state);
1658 if ((fin->fin_fl & FI_FRAG) && (pass & FR_KEEPFRAG))
1659 ipfr_newfrag(ip, fin);
1661 if ((tcp != NULL) && (tcp->th_flags & TCP_CLOSE))
1669 * Sync. state entries. If interfaces come or go or just change position,
1672 void ip_statesync(ifp)
1678 WRITE_ENTER(&ipf_state);
1679 for (is = ips_list; is; is = is->is_next) {
1680 for (i = 0; i < 4; i++) {
1681 if (is->is_ifp[i] == ifp) {
1682 is->is_ifp[i] = GETUNIT(is->is_ifname[i],
1685 is->is_ifp[i] = (void *)-1;
1689 RWLOCK_EXIT(&ipf_state);
1694 * Must always be called with fr_ipfstate held as a write lock.
1696 static void fr_delstate(is)
1701 if (is->is_flags & (FI_WILDP|FI_WILDA))
1704 is->is_next->is_pnext = is->is_pnext;
1705 *is->is_pnext = is->is_next;
1707 is->is_hnext->is_phnext = is->is_phnext;
1708 *is->is_phnext = is->is_hnext;
1709 if (ips_table[is->is_hv] == NULL)
1710 ips_stats.iss_inuse--;
1717 if (fr->fr_ref == 0) {
1722 MUTEX_DESTROY(&is->is_lock);
1730 * Free memory in use by all state info. kept.
1732 void fr_stateunload()
1736 WRITE_ENTER(&ipf_state);
1737 while ((is = ips_list))
1739 ips_stats.iss_inuse = 0;
1741 RWLOCK_EXIT(&ipf_state);
1743 KFREES(ips_table, fr_statesize * sizeof(ipstate_t *));
1749 * Slowly expire held state for thingslike UDP and ICMP. Timeouts are set
1750 * in expectation of this being called twice per second.
1752 void fr_timeoutstate()
1754 ipstate_t *is, **isp;
1755 #if defined(_KERNEL) && !SOLARIS
1760 WRITE_ENTER(&ipf_state);
1761 for (isp = &ips_list; (is = *isp); )
1762 if (is->is_age && !--is->is_age) {
1763 if (is->is_p == IPPROTO_TCP)
1764 ips_stats.iss_fin++;
1766 ips_stats.iss_expire++;
1768 ipstate_log(is, ISL_EXPIRE);
1773 if (fr_state_doflush) {
1774 (void) fr_state_flush(2, 0);
1775 fr_state_doflush = 0;
1777 RWLOCK_EXIT(&ipf_state);
1783 * Original idea freom Pradeep Krishnan for use primarily with NAT code.
1784 * (pkrishna@netcom.com)
1786 * Rewritten by Arjan de Vet <Arjan.deVet@adv.iae.nl>, 2000-07-29:
1788 * - (try to) base state transitions on real evidence only,
1789 * i.e. packets that are sent and have been received by ipfilter;
1790 * diagram 18.12 of TCP/IP volume 1 by W. Richard Stevens was used.
1792 * - deal with half-closed connections correctly;
1794 * - store the state of the source in state[0] such that ipfstat
1795 * displays the state as source/dest instead of dest/source; the calls
1796 * to fr_tcp_age have been changed accordingly.
1800 * state[0] = state of source (host that initiated connection)
1801 * state[1] = state of dest (host that accepted the connection)
1803 * dir == 0 : a packet from source to dest
1804 * dir == 1 : a packet from dest to source
1807 int fr_tcp_age(age, state, fin, dir, fsm)
1813 tcphdr_t *tcp = (tcphdr_t *)fin->fin_dp;
1814 u_char flags = tcp->th_flags;
1818 ostate = state[1 - dir];
1820 dlen = fin->fin_plen - fin->fin_hlen - (tcp->th_off << 2);
1822 if (flags & TH_RST) {
1823 if (!(tcp->th_flags & TH_PUSH) && !dlen) {
1824 *age = fr_tcpclosed;
1825 state[dir] = TCPS_CLOSED;
1827 *age = fr_tcpclosewait;
1828 state[dir] = TCPS_CLOSE_WAIT;
1837 case TCPS_CLOSED: /* 0 */
1838 if ((flags & TH_OPENING) == TH_OPENING) {
1840 * 'dir' received an S and sends SA in response,
1841 * CLOSED -> SYN_RECEIVED
1843 state[dir] = TCPS_SYN_RECEIVED;
1844 newage = fr_tcptimeout;
1845 } else if ((flags & TH_OPENING) == TH_SYN) {
1846 /* 'dir' sent S, CLOSED -> SYN_SENT */
1847 state[dir] = TCPS_SYN_SENT;
1848 newage = fr_tcptimeout;
1852 * It is apparently possible that a hosts sends two syncs
1853 * before the remote party is able to respond with a SA. In
1854 * such a case the remote server sometimes ACK's the second
1855 * sync, and then responds with a SA. The following code
1856 * is used to prevent this ack from being blocked.
1858 * We do not reset the timeout here to fr_tcptimeout because
1859 * a connection connect timeout does not renew after every
1860 * packet that is sent. We need to set newage to something
1861 * to indicate the packet has passed the check for its flags
1862 * being valid in the TCP FSM.
1864 else if ((ostate == TCPS_SYN_SENT) &&
1865 ((flags & (TH_FIN|TH_SYN|TH_RST|TH_ACK)) == TH_ACK)) {
1870 * The next piece of code makes it possible to get
1871 * already established connections into the state table
1872 * after a restart or reload of the filter rules; this
1873 * does not work when a strict 'flags S keep state' is
1874 * used for tcp connections of course, however, use a
1875 * lower time-out so the state disappears quickly if
1876 * the other side does not pick it up.
1879 (flags & (TH_FIN|TH_SYN|TH_RST|TH_ACK)) == TH_ACK) {
1880 /* we saw an A, guess 'dir' is in ESTABLISHED mode */
1881 if (ostate == TCPS_CLOSED) {
1882 state[dir] = TCPS_ESTABLISHED;
1883 newage = fr_tcptimeout;
1884 } else if (ostate == TCPS_ESTABLISHED) {
1885 state[dir] = TCPS_ESTABLISHED;
1886 newage = fr_tcpidletimeout;
1890 * TODO: besides regular ACK packets we can have other
1891 * packets as well; it is yet to be determined how we
1892 * should initialize the states in those cases
1896 case TCPS_LISTEN: /* 1 */
1900 case TCPS_SYN_SENT: /* 2 */
1901 if ((flags & ~(TH_ECN|TH_CWR)) == TH_SYN) {
1903 * A retransmitted SYN packet. We do not reset the
1904 * timeout here to fr_tcptimeout because a connection
1905 * connect timeout does not renew after every packet
1906 * that is sent. We need to set newage to something
1907 * to indicate the packet has passed the check for its
1908 * flags being valid in the TCP FSM.
1911 } else if ((flags & (TH_SYN|TH_FIN|TH_ACK)) == TH_ACK) {
1913 * We see an A from 'dir' which is in SYN_SENT
1914 * state: 'dir' sent an A in response to an SA
1915 * which it received, SYN_SENT -> ESTABLISHED
1917 state[dir] = TCPS_ESTABLISHED;
1918 newage = fr_tcpidletimeout;
1919 } else if (flags & TH_FIN) {
1921 * We see an F from 'dir' which is in SYN_SENT
1922 * state and wants to close its side of the
1923 * connection; SYN_SENT -> FIN_WAIT_1
1925 state[dir] = TCPS_FIN_WAIT_1;
1926 newage = fr_tcpidletimeout; /* or fr_tcptimeout? */
1927 } else if ((flags & TH_OPENING) == TH_OPENING) {
1929 * We see an SA from 'dir' which is already in
1930 * SYN_SENT state, this means we have a
1931 * simultaneous open; SYN_SENT -> SYN_RECEIVED
1933 state[dir] = TCPS_SYN_RECEIVED;
1934 newage = fr_tcptimeout;
1938 case TCPS_SYN_RECEIVED: /* 3 */
1939 if ((flags & (TH_SYN|TH_FIN|TH_ACK)) == TH_ACK) {
1941 * We see an A from 'dir' which was in SYN_RECEIVED
1942 * state so it must now be in established state,
1943 * SYN_RECEIVED -> ESTABLISHED
1945 state[dir] = TCPS_ESTABLISHED;
1946 newage = fr_tcpidletimeout;
1947 } else if ((flags & ~(TH_ECN|TH_CWR)) == TH_OPENING) {
1949 * We see an SA from 'dir' which is already in
1950 * SYN_RECEIVED state.
1952 newage = fr_tcptimeout;
1953 } else if (flags & TH_FIN) {
1955 * We see an F from 'dir' which is in SYN_RECEIVED
1956 * state and wants to close its side of the connection;
1957 * SYN_RECEIVED -> FIN_WAIT_1
1959 state[dir] = TCPS_FIN_WAIT_1;
1960 newage = fr_tcpidletimeout;
1964 case TCPS_ESTABLISHED: /* 4 */
1965 if (flags & TH_FIN) {
1967 * 'dir' closed its side of the connection; this
1968 * gives us a half-closed connection;
1969 * ESTABLISHED -> FIN_WAIT_1
1971 state[dir] = TCPS_FIN_WAIT_1;
1972 newage = fr_tcphalfclosed;
1973 } else if (flags & TH_ACK) {
1974 /* an ACK, should we exclude other flags here? */
1975 if (ostate == TCPS_FIN_WAIT_1) {
1977 * We know the other side did an active close,
1978 * so we are ACKing the recvd FIN packet (does
1979 * the window matching code guarantee this?)
1980 * and go into CLOSE_WAIT state; this gives us
1981 * a half-closed connection
1983 state[dir] = TCPS_CLOSE_WAIT;
1984 newage = fr_tcphalfclosed;
1985 } else if (ostate < TCPS_CLOSE_WAIT)
1987 * Still a fully established connection,
1990 newage = fr_tcpidletimeout;
1994 case TCPS_CLOSE_WAIT: /* 5 */
1995 if (flags & TH_FIN) {
1997 * Application closed and 'dir' sent a FIN, we're now
1998 * going into LAST_ACK state
2000 newage = fr_tcplastack;
2001 state[dir] = TCPS_LAST_ACK;
2004 * We remain in CLOSE_WAIT because the other side has
2005 * closed already and we did not close our side yet;
2008 newage = fr_tcphalfclosed;
2012 case TCPS_FIN_WAIT_1: /* 6 */
2013 if ((flags & TH_ACK) && ostate > TCPS_CLOSE_WAIT) {
2015 * If the other side is not active anymore it has sent
2016 * us a FIN packet that we are ack'ing now with an ACK;
2017 * this means both sides have now closed the connection
2018 * and we go into TIME_WAIT
2021 * XXX: how do we know we really are ACKing the FIN
2022 * packet here? does the window code guarantee that?
2024 state[dir] = TCPS_TIME_WAIT;
2025 newage = fr_tcptimeout;
2028 * We closed our side of the connection already but the
2029 * other side is still active (ESTABLISHED/CLOSE_WAIT);
2030 * continue with this half-closed connection
2032 newage = fr_tcphalfclosed;
2035 case TCPS_CLOSING: /* 7 */
2039 case TCPS_LAST_ACK: /* 8 */
2040 if (flags & TH_ACK) {
2041 if ((flags & TH_PUSH) || dlen)
2043 * There is still data to be delivered, reset
2046 newage = fr_tcplastack;
2051 * We cannot detect when we go out of LAST_ACK state to CLOSED
2052 * because that is based on the reception of ACK packets;
2053 * ipfilter can only detect that a packet has been sent by a
2058 case TCPS_FIN_WAIT_2: /* 9 */
2062 case TCPS_TIME_WAIT: /* 10 */
2063 newage = fr_tcptimeout; /* default 4 mins */
2064 /* we're in 2MSL timeout now */
2077 void ipstate_log(is, type)
2086 ipsl.isl_type = type;
2087 ipsl.isl_pkts = is->is_pkts;
2088 ipsl.isl_bytes = is->is_bytes;
2089 ipsl.isl_src = is->is_src;
2090 ipsl.isl_dst = is->is_dst;
2091 ipsl.isl_p = is->is_p;
2092 ipsl.isl_v = is->is_v;
2093 ipsl.isl_flags = is->is_flags;
2094 ipsl.isl_rulen = is->is_rulen;
2095 ipsl.isl_group = is->is_group;
2096 if (ipsl.isl_p == IPPROTO_TCP || ipsl.isl_p == IPPROTO_UDP) {
2097 ipsl.isl_sport = is->is_sport;
2098 ipsl.isl_dport = is->is_dport;
2099 if (ipsl.isl_p == IPPROTO_TCP) {
2100 ipsl.isl_state[0] = is->is_state[0];
2101 ipsl.isl_state[1] = is->is_state[1];
2103 } else if (ipsl.isl_p == IPPROTO_ICMP) {
2104 ipsl.isl_itype = is->is_icmp.ics_type;
2105 } else if (ipsl.isl_p == IPPROTO_ICMPV6) {
2106 ipsl.isl_itype = is->is_icmp.ics_type;
2108 ipsl.isl_ps.isl_filler[0] = 0;
2109 ipsl.isl_ps.isl_filler[1] = 0;
2112 sizes[0] = sizeof(ipsl);
2115 (void) ipllog(IPL_LOGSTATE, NULL, items, sizes, types, 1);
2121 frentry_t *fr_checkicmp6matchingstate(ip, fin)
2125 ipstate_t *is, **isp;
2126 u_short sport, dport;
2128 struct icmp6_hdr *ic, *oic;
2129 union i6addr dst, src;
2139 * Does it at least have the return (basic) IP header ?
2140 * Only a basic IP header (no options) should be with
2141 * an ICMP error header.
2143 if ((fin->fin_v != 6) || (fin->fin_plen < ICMP6ERR_MINPKTLEN))
2145 ic = (struct icmp6_hdr *)fin->fin_dp;
2146 type = ic->icmp6_type;
2148 * If it's not an error type, then return
2150 if ((type != ICMP6_DST_UNREACH) && (type != ICMP6_PACKET_TOO_BIG) &&
2151 (type != ICMP6_TIME_EXCEEDED) && (type != ICMP6_PARAM_PROB))
2154 oip = (ip6_t *)((char *)ic + ICMPERR_ICMPHLEN);
2155 if (fin->fin_plen < sizeof(*oip))
2158 if ((oip->ip6_nxt != IPPROTO_TCP) && (oip->ip6_nxt != IPPROTO_UDP) &&
2159 (oip->ip6_nxt != IPPROTO_ICMPV6))
2162 bzero((char *)&ofin, sizeof(ofin));
2163 ofin.fin_out = !fin->fin_out;
2164 ofin.fin_ifp = fin->fin_ifp;
2167 if (oip->ip6_nxt == IPPROTO_ICMPV6) {
2168 oic = (struct icmp6_hdr *)(oip + 1);
2170 * a ICMP error can only be generated as a result of an
2171 * ICMP query, not as the response on an ICMP error
2173 * XXX theoretically ICMP_ECHOREP and the other reply's are
2174 * ICMP query's as well, but adding them here seems strange XXX
2176 if (!(oic->icmp6_type & ICMP6_INFOMSG_MASK))
2180 * perform a lookup of the ICMP packet in the state table
2182 hv = (pr = oip->ip6_nxt);
2183 src.in6 = oip->ip6_src;
2184 hv += src.in4.s_addr;
2185 dst.in6 = oip->ip6_dst;
2186 hv += dst.in4.s_addr;
2187 hv += oic->icmp6_id;
2188 hv += oic->icmp6_seq;
2191 oip->ip6_plen = ntohs(oip->ip6_plen);
2192 fr_makefrip(sizeof(*oip), (ip_t *)oip, &ofin);
2193 oip->ip6_plen = htons(oip->ip6_plen);
2195 READ_ENTER(&ipf_state);
2196 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext)
2197 if ((is->is_p == pr) &&
2198 (oic->icmp6_id == is->is_icmp.ics_id) &&
2199 (oic->icmp6_seq == is->is_icmp.ics_seq) &&
2200 fr_matchsrcdst(is, src, dst, &ofin, NULL)) {
2202 * in the state table ICMP query's are stored
2203 * with the type of the corresponding ICMP
2204 * response. Correct here
2206 if (((is->is_type == ICMP6_ECHO_REPLY) &&
2207 (oic->icmp6_type == ICMP6_ECHO_REQUEST)) ||
2208 (is->is_type - 1 == oic->icmp6_type )) {
2209 ips_stats.iss_hits++;
2211 is->is_bytes += fin->fin_plen;
2215 RWLOCK_EXIT(&ipf_state);
2220 tcp = (tcphdr_t *)(oip + 1);
2221 dport = tcp->th_dport;
2222 sport = tcp->th_sport;
2224 hv = (pr = oip->ip6_nxt);
2225 src.in6 = oip->ip6_src;
2226 hv += src.in4.s_addr;
2230 dst.in6 = oip->ip6_dst;
2231 hv += dst.in4.s_addr;
2239 * we make an fin entry to be able to feed it to
2240 * matchsrcdst note that not all fields are encessary
2241 * but this is the cleanest way. Note further we fill
2242 * in fin_mp such that if someone uses it we'll get
2243 * a kernel panic. fr_matchsrcdst does not use this.
2245 * watch out here, as ip is in host order and oip in network
2246 * order. Any change we make must be undone afterwards.
2248 savelen = oip->ip6_plen;
2249 oip->ip6_plen = ip->ip6_plen - sizeof(*ip) - ICMPERR_ICMPHLEN;
2250 fr_makefrip(sizeof(*oip), (ip_t *)oip, &ofin);
2251 oip->ip6_plen = savelen;
2252 READ_ENTER(&ipf_state);
2253 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) {
2255 * Only allow this icmp though if the
2256 * encapsulated packet was allowed through the
2257 * other way around. Note that the minimal amount
2258 * of info present does not allow for checking against
2259 * tcp internals such as seq and ack numbers.
2261 if ((is->is_p == pr) && (is->is_v == 6) &&
2262 fr_matchsrcdst(is, src, dst, &ofin, tcp)) {
2264 ips_stats.iss_hits++;
2266 is->is_bytes += fin->fin_plen;
2268 * we deliberately do not touch the timeouts
2269 * for the accompanying state table entry.
2270 * It remains to be seen if that is correct. XXX
2272 RWLOCK_EXIT(&ipf_state);
2276 RWLOCK_EXIT(&ipf_state);