2 * Copyright (C) 1995-2002 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 #if defined(__sgi) && (IRIX > 602)
7 # include <sys/ptimers.h>
10 #include <sys/types.h>
11 #include <sys/param.h>
13 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
15 # include "opt_ipfilter_log.h"
17 #if defined(_KERNEL) && defined(__FreeBSD_version) && \
18 (__FreeBSD_version >= 400000) && !defined(KLD_MODULE)
19 #include "opt_inet6.h"
21 #if !defined(_KERNEL) && !defined(KERNEL) && !defined(__KERNEL__)
27 # include <linux/kernel.h>
28 # include <linux/module.h>
31 #if (defined(KERNEL) || defined(_KERNEL)) && (__FreeBSD_version >= 220000)
32 # include <sys/filio.h>
33 # include <sys/fcntl.h>
34 # if (__FreeBSD_version >= 300000) && !defined(IPFILTER_LKM)
35 # include "opt_ipfilter.h"
38 # include <sys/ioctl.h>
42 # include <sys/protosw.h>
44 #include <sys/socket.h>
45 #if (defined(_KERNEL) || defined(KERNEL)) && !defined(linux)
46 # include <sys/systm.h>
48 #if !defined(__SVR4) && !defined(__svr4__)
50 # include <sys/mbuf.h>
53 # include <sys/filio.h>
54 # include <sys/byteorder.h>
56 # include <sys/dditypes.h>
58 # include <sys/stream.h>
59 # include <sys/kmem.h>
66 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/tcp.h>
72 # include <netinet/ip_var.h>
73 # include <netinet/tcp_fsm.h>
75 #include <netinet/udp.h>
76 #include <netinet/ip_icmp.h>
77 #include "netinet/ip_compat.h"
78 #include <netinet/tcpip.h>
79 #include "netinet/ip_fil.h"
80 #include "netinet/ip_nat.h"
81 #include "netinet/ip_frag.h"
82 #include "netinet/ip_state.h"
84 #include <netinet/icmp6.h>
86 #if (__FreeBSD_version >= 300000)
87 # include <sys/malloc.h>
88 # if (defined(_KERNEL) || defined(KERNEL)) && !defined(IPFILTER_LKM)
89 # include <sys/libkern.h>
90 # include <sys/systm.h>
95 static const char sccsid[] = "@(#)ip_state.c 1.8 6/5/96 (C) 1993-2000 Darren Reed";
96 static const char rcsid[] = "@(#)$Id: ip_state.c,v 2.30.2.77 2002/12/06 11:40:24 darrenr Exp $";
100 # define MIN(a,b) (((a)<(b))?(a):(b))
103 #define TCP_CLOSE (TH_FIN|TH_RST)
105 static ipstate_t **ips_table = NULL;
106 static int ips_num = 0;
107 static int ips_wild = 0;
108 static ips_stat_t ips_stats;
109 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
110 extern KRWLOCK_T ipf_state, ipf_mutex;
111 extern kmutex_t ipf_rw;
115 static frentry_t *fr_checkicmp6matchingstate __P((ip6_t *, fr_info_t *));
117 static int fr_matchsrcdst __P((ipstate_t *, union i6addr, union i6addr,
118 fr_info_t *, tcphdr_t *));
119 static frentry_t *fr_checkicmpmatchingstate __P((ip_t *, fr_info_t *));
120 static int fr_matchicmpqueryreply __P((int, ipstate_t *, icmphdr_t *, int));
121 static int fr_state_flush __P((int, int));
122 static ips_stat_t *fr_statetstats __P((void));
123 static void fr_delstate __P((ipstate_t *));
124 static int fr_state_remove __P((caddr_t));
125 static void fr_ipsmove __P((ipstate_t **, ipstate_t *, u_int));
126 static int fr_tcpoptions __P((tcphdr_t *));
127 int fr_stputent __P((caddr_t));
128 int fr_stgetent __P((caddr_t));
129 void fr_stinsert __P((ipstate_t *));
132 #define FIVE_DAYS (2 * 5 * 86400) /* 5 days: half closed session */
134 #define TCP_MSL 240 /* 2 minutes */
135 u_long fr_tcpidletimeout = FIVE_DAYS,
136 fr_tcpclosewait = 2 * TCP_MSL,
137 fr_tcplastack = 2 * TCP_MSL,
138 fr_tcptimeout = 2 * TCP_MSL,
140 fr_tcphalfclosed = 2 * 2 * 3600, /* 2 hours */
142 fr_udpacktimeout = 24,
143 fr_icmptimeout = 120,
144 fr_icmpacktimeout = 12;
145 int fr_statemax = IPSTATE_MAX,
146 fr_statesize = IPSTATE_SIZE;
147 int fr_state_doflush = 0,
149 ipstate_t *ips_list = NULL;
151 static int icmpreplytype4[ICMP_MAXTYPE + 1];
153 static int icmpreplytype6[ICMP6_MAXTYPE + 1];
160 KMALLOCS(ips_table, ipstate_t **, fr_statesize * sizeof(ipstate_t *));
161 if (ips_table != NULL)
162 bzero((char *)ips_table, fr_statesize * sizeof(ipstate_t *));
166 /* fill icmp reply type table */
167 for (i = 0; i <= ICMP_MAXTYPE; i++)
168 icmpreplytype4[i] = -1;
169 icmpreplytype4[ICMP_ECHO] = ICMP_ECHOREPLY;
170 icmpreplytype4[ICMP_TSTAMP] = ICMP_TSTAMPREPLY;
171 icmpreplytype4[ICMP_IREQ] = ICMP_IREQREPLY;
172 icmpreplytype4[ICMP_MASKREQ] = ICMP_MASKREPLY;
174 /* fill icmp reply type table */
175 for (i = 0; i <= ICMP6_MAXTYPE; i++)
176 icmpreplytype6[i] = -1;
177 icmpreplytype6[ICMP6_ECHO_REQUEST] = ICMP6_ECHO_REPLY;
178 icmpreplytype6[ICMP6_MEMBERSHIP_QUERY] = ICMP6_MEMBERSHIP_REPORT;
179 icmpreplytype6[ICMP6_NI_QUERY] = ICMP6_NI_REPLY;
180 icmpreplytype6[ND_ROUTER_SOLICIT] = ND_ROUTER_ADVERT;
181 icmpreplytype6[ND_NEIGHBOR_SOLICIT] = ND_NEIGHBOR_ADVERT;
188 static ips_stat_t *fr_statetstats()
190 ips_stats.iss_active = ips_num;
191 ips_stats.iss_table = ips_table;
192 ips_stats.iss_list = ips_list;
198 * flush state tables. two actions currently defined:
199 * which == 0 : flush all state table entries
200 * which == 1 : flush TCP connections which have started to close but are
201 * stuck for some reason.
202 * which == 2 : flush TCP connections which have been idle for a long time,
203 * starting at > 4 days idle and working back in successive half-
204 * days to at most 12 hours old.
206 static int fr_state_flush(which, proto)
209 ipstate_t *is, **isp;
210 #if defined(_KERNEL) && !SOLARIS
213 int delete, removed = 0, try;
216 for (isp = &ips_list; (is = *isp); ) {
219 if ((proto != 0) && (is->is_v != proto))
229 if (is->is_p != IPPROTO_TCP)
231 if ((is->is_state[0] != TCPS_ESTABLISHED) ||
232 (is->is_state[1] != TCPS_ESTABLISHED))
238 if (is->is_p == IPPROTO_TCP)
241 ips_stats.iss_expire++;
243 ipstate_log(is, ISL_FLUSH);
252 * Asked to remove inactive entries, try again if first attempt
253 * failed. In this case, 86400 is half a day because the counter is
254 * activated every half second.
256 if ((which == 2) && (removed == 0)) {
257 try = 86400; /* half a day */
258 for (; (try < FIVE_DAYS) && (removed == 0); try += 86400) {
259 for (isp = &ips_list; (is = *isp); ) {
261 if ((is->is_p == IPPROTO_TCP) &&
262 ((is->is_state[0] == TCPS_ESTABLISHED) ||
263 (is->is_state[1] == TCPS_ESTABLISHED)) &&
264 (is->is_age < try)) {
267 } else if ((is->is_p != IPPROTO_TCP) &&
269 ips_stats.iss_expire++;
274 ipstate_log(is, ISL_FLUSH);
289 static int fr_state_remove(data)
296 error = IRCOPYPTR(data, (caddr_t)&st, sizeof(st));
300 WRITE_ENTER(&ipf_state);
301 for (sp = ips_list; sp; sp = sp->is_next)
302 if ((sp->is_p == st.is_p) && (sp->is_v == st.is_v) &&
303 !bcmp((char *)&sp->is_src, (char *)&st.is_src,
304 sizeof(st.is_src)) &&
305 !bcmp((char *)&sp->is_dst, (char *)&st.is_dst,
306 sizeof(st.is_dst)) &&
307 !bcmp((char *)&sp->is_ps, (char *)&st.is_ps,
310 ipstate_log(sp, ISL_REMOVE);
313 RWLOCK_EXIT(&ipf_state);
316 RWLOCK_EXIT(&ipf_state);
321 int fr_state_ioctl(data, cmd, mode)
323 #if defined(__NetBSD__) || defined(__OpenBSD__)
330 int arg, ret, error = 0;
335 error = fr_state_remove(data);
338 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
341 if (arg == 0 || arg == 1) {
342 WRITE_ENTER(&ipf_state);
343 ret = fr_state_flush(arg, 4);
344 RWLOCK_EXIT(&ipf_state);
345 error = IWCOPY((caddr_t)&ret, data, sizeof(ret));
351 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
354 if (arg == 0 || arg == 1) {
355 WRITE_ENTER(&ipf_state);
356 ret = fr_state_flush(arg, 6);
357 RWLOCK_EXIT(&ipf_state);
358 error = IWCOPY((caddr_t)&ret, data, sizeof(ret));
365 if (!(mode & FWRITE))
370 tmp = ipflog_clear(IPL_LOGSTATE);
371 IWCOPY((char *)&tmp, data, sizeof(tmp));
376 error = IWCOPYPTR((caddr_t)fr_statetstats(), data,
381 arg = (int)iplused[IPL_LOGSTATE];
382 error = IWCOPY((caddr_t)&arg, (caddr_t)data, sizeof(arg));
386 error = fr_lock(data, &fr_state_lock);
389 if (!fr_state_lock) {
393 error = fr_stputent(data);
396 if (!fr_state_lock) {
400 error = fr_stgetent(data);
411 * Copy out state information from the kernel to a user space process.
413 int fr_stgetent(data)
416 register ipstate_t *is, *isn;
420 error = IRCOPYPTR(data, (caddr_t)&ips, sizeof(ips));
428 if (ips.ips_next == NULL)
434 * Make sure the pointer we're copying from exists in the
435 * current list of entries. Security precaution to prevent
436 * copying of random kernel data.
438 for (is = ips_list; is; is = is->is_next)
444 ips.ips_next = isn->is_next;
445 bcopy((char *)isn, (char *)&ips.ips_is, sizeof(ips.ips_is));
447 bcopy((char *)isn->is_rule, (char *)&ips.ips_fr,
449 error = IWCOPYPTR((caddr_t)&ips, data, sizeof(ips));
456 int fr_stputent(data)
459 register ipstate_t *is, *isn;
465 error = IRCOPYPTR(data, (caddr_t)&ips, sizeof(ips));
469 KMALLOC(isn, ipstate_t *);
473 bcopy((char *)&ips.ips_is, (char *)isn, sizeof(*isn));
476 if (isn->is_flags & FI_NEWFR) {
477 KMALLOC(fr, frentry_t *);
482 bcopy((char *)&ips.ips_fr, (char *)fr, sizeof(*fr));
483 out = fr->fr_flags & FR_OUTQUE ? 1 : 0;
485 ips.ips_is.is_rule = fr;
488 * Look up all the interface names in the rule.
490 for (i = 0; i < 4; i++) {
491 name = fr->fr_ifnames[i];
492 if ((name[1] == '\0') &&
493 ((name[0] == '-') || (name[0] == '*'))) {
494 fr->fr_ifas[i] = NULL;
495 } else if (*name != '\0') {
496 fr->fr_ifas[i] = GETUNIT(name,
498 if (fr->fr_ifas[i] == NULL)
499 fr->fr_ifas[i] = (void *)-1;
501 strncpy(isn->is_ifname[i],
502 IFNAME(fr->fr_ifas[i]),
506 isn->is_ifp[out] = fr->fr_ifas[i];
510 * send a copy back to userland of what we ended up
511 * to allow for verification.
513 error = IWCOPYPTR((caddr_t)&ips, data, sizeof(ips));
520 for (is = ips_list; is; is = is->is_next)
521 if (is->is_rule == fr)
535 * Insert a state table entry manually.
538 register ipstate_t *is;
540 register u_int hv = is->is_hv;
544 MUTEX_INIT(&is->is_lock, "ipf state entry", NULL);
547 * Look up all the interface names in the state entry.
549 for (i = 0; i < 4; i++) {
550 name = is->is_ifname[i];
551 if ((name[1] == '\0') &&
552 ((name[0] == '-') || (name[0] == '*'))) {
553 is->is_ifp[0] = NULL;
554 } else if (*name != '\0') {
555 is->is_ifp[i] = GETUNIT(name, is->is_v);
556 if (is->is_ifp[i] == NULL)
557 is->is_ifp[i] = (void *)-1;
563 * add into list table.
566 ips_list->is_pnext = &is->is_next;
567 is->is_pnext = &ips_list;
568 is->is_next = ips_list;
571 ips_table[hv]->is_phnext = &is->is_hnext;
573 ips_stats.iss_inuse++;
574 is->is_phnext = ips_table + hv;
575 is->is_hnext = ips_table[hv];
582 * Create a new ipstate structure and hang it off the hash table.
584 ipstate_t *fr_addstate(ip, fin, stsave, flags)
590 register tcphdr_t *tcp = NULL;
591 register ipstate_t *is;
599 if (fr_state_lock || (fin->fin_off != 0) || (fin->fin_fl & FI_SHORT) ||
600 (fin->fin_misc & FM_BADSTATE))
602 if (ips_num == fr_statemax) {
604 fr_state_doflush = 1;
609 bzero((char *)is, sizeof(*is));
612 * Copy and calculate...
614 hv = (is->is_p = fin->fin_fi.fi_p);
615 is->is_src = fin->fin_fi.fi_src;
617 is->is_dst = fin->fin_fi.fi_dst;
620 if (fin->fin_v == 6) {
621 if ((is->is_p == IPPROTO_ICMPV6) &&
622 IN6_IS_ADDR_MULTICAST(&is->is_dst.in6)) {
624 * So you can do keep state with neighbour discovery.
629 hv += is->is_dst.i6[1];
630 hv += is->is_dst.i6[2];
631 hv += is->is_dst.i6[3];
633 hv += is->is_src.i6[1];
634 hv += is->is_src.i6[2];
635 hv += is->is_src.i6[3];
644 case IPPROTO_ICMPV6 :
645 ic = (struct icmp *)fin->fin_dp;
646 if ((ic->icmp_type & ICMP6_INFOMSG_MASK) == 0)
649 switch (ic->icmp_type)
651 case ICMP6_ECHO_REQUEST :
652 is->is_icmp.ics_type = ic->icmp_type;
653 hv += (is->is_icmp.ics_id = ic->icmp_id);
654 hv += (is->is_icmp.ics_seq = ic->icmp_seq);
656 case ICMP6_MEMBERSHIP_QUERY :
657 case ND_ROUTER_SOLICIT :
658 case ND_NEIGHBOR_SOLICIT :
659 case ICMP6_NI_QUERY :
660 is->is_icmp.ics_type = ic->icmp_type;
665 ATOMIC_INCL(ips_stats.iss_icmp);
666 is->is_age = fr_icmptimeout;
670 ic = (struct icmp *)fin->fin_dp;
672 switch (ic->icmp_type)
678 is->is_icmp.ics_type = ic->icmp_type;
679 hv += (is->is_icmp.ics_id = ic->icmp_id);
680 hv += (is->is_icmp.ics_seq = ic->icmp_seq);
685 ATOMIC_INCL(ips_stats.iss_icmp);
686 is->is_age = fr_icmptimeout;
689 tcp = (tcphdr_t *)fin->fin_dp;
691 if (tcp->th_flags & TH_RST)
694 * The endian of the ports doesn't matter, but the ack and
695 * sequence numbers do as we do mathematics on them later.
697 is->is_sport = htons(fin->fin_data[0]);
698 is->is_dport = htons(fin->fin_data[1]);
699 if ((flags & (FI_W_DPORT|FI_W_SPORT)) == 0) {
703 if ((flags & FI_IGNOREPKT) == 0) {
704 is->is_send = ntohl(tcp->th_seq) + fin->fin_dlen -
705 (off = (tcp->th_off << 2)) +
706 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
707 ((tcp->th_flags & TH_FIN) ? 1 : 0);
708 is->is_maxsend = is->is_send;
710 if ((tcp->th_flags & TH_SYN) &&
711 ((tcp->th_off << 2) >= (sizeof(*tcp) + 4))) {
712 ws = fr_tcpoptions(tcp);
719 is->is_maxswin = ntohs(tcp->th_win);
720 if (is->is_maxswin == 0)
723 if ((tcp->th_flags & TH_OPENING) == TH_SYN)
727 * If we're creating state for a starting connection, start the
728 * timer on it as we'll never see an error if it fails to
731 ATOMIC_INCL(ips_stats.iss_tcp);
735 tcp = (tcphdr_t *)fin->fin_dp;
737 is->is_sport = htons(fin->fin_data[0]);
738 is->is_dport = htons(fin->fin_data[1]);
739 if ((flags & (FI_W_DPORT|FI_W_SPORT)) == 0) {
743 ATOMIC_INCL(ips_stats.iss_udp);
744 is->is_age = fr_udptimeout;
747 is->is_age = fr_udptimeout;
751 KMALLOC(is, ipstate_t *);
753 ATOMIC_INCL(ips_stats.iss_nomem);
756 bcopy((char *)&ips, (char *)is, sizeof(*is));
759 is->is_rule = fin->fin_fr;
760 if (is->is_rule != NULL) {
761 is->is_group = is->is_rule->fr_group;
762 ATOMIC_INC32(is->is_rule->fr_ref);
763 pass = is->is_rule->fr_flags;
764 is->is_frage[0] = is->is_rule->fr_age[0];
765 is->is_frage[1] = is->is_rule->fr_age[1];
766 if (is->is_frage[0] != 0)
767 is->is_age = is->is_frage[0];
769 is->is_ifp[(out << 1) + 1] = is->is_rule->fr_ifas[1];
770 is->is_ifp[(1 - out) << 1] = is->is_rule->fr_ifas[2];
771 is->is_ifp[((1 - out) << 1) + 1] = is->is_rule->fr_ifas[3];
773 if (((ifp = is->is_rule->fr_ifas[1]) != NULL) &&
775 strncpy(is->is_ifname[(out << 1) + 1],
776 IFNAME(ifp), IFNAMSIZ);
777 if (((ifp = is->is_rule->fr_ifas[2]) != NULL) &&
779 strncpy(is->is_ifname[(1 - out) << 1],
780 IFNAME(ifp), IFNAMSIZ);
781 if (((ifp = is->is_rule->fr_ifas[3]) != NULL) &&
783 strncpy(is->is_ifname[((1 - out) << 1) + 1],
784 IFNAME(ifp), IFNAMSIZ);
788 is->is_ifp[out << 1] = fin->fin_ifp;
789 strncpy(is->is_ifname[out << 1], IFNAME(fin->fin_ifp), IFNAMSIZ);
791 WRITE_ENTER(&ipf_state);
794 if ((flags & FI_IGNOREPKT) == 0) {
796 is->is_bytes = fin->fin_dlen + fin->fin_hlen;
799 * We want to check everything that is a property of this packet,
800 * but we don't (automatically) care about it's fragment status as
803 is->is_v = fin->fin_v;
804 is->is_rulen = fin->fin_rule;
805 is->is_opt = fin->fin_fi.fi_optmsk;
806 is->is_optmsk = 0xffffffff;
807 is->is_sec = fin->fin_fi.fi_secmsk;
808 is->is_secmsk = 0xffff;
809 is->is_auth = fin->fin_fi.fi_auth;
810 is->is_authmsk = 0xffff;
811 is->is_flags = fin->fin_fl & FI_CMP;
812 is->is_flags |= FI_CMP << 4;
813 is->is_flags |= flags & (FI_WILDP|FI_WILDA);
814 if (flags & (FI_WILDP|FI_WILDA))
817 if (pass & FR_LOGFIRST)
818 is->is_pass &= ~(FR_LOGFIRST|FR_LOG);
821 if (is->is_p == IPPROTO_TCP) {
822 fr_tcp_age(&is->is_age, is->is_state, fin,
823 0, is->is_fsm); /* 0 = packet from the source */
826 ipstate_log(is, ISL_NEW);
828 RWLOCK_EXIT(&ipf_state);
829 fin->fin_rev = IP6NEQ(is->is_dst, fin->fin_fi.fi_dst);
830 if ((fin->fin_fl & FI_FRAG) && (pass & FR_KEEPFRAG))
831 ipfr_newfrag(ip, fin);
836 static int fr_tcpoptions(tcp)
842 opt = (u_char *) (tcp + 1);
843 last = ((u_char *)tcp) + (tcp->th_off << 2);
845 /* If we don't find wscale here, we need to clear it */
848 /* Termination condition picked such that opt[0 .. 2] exist */
849 while ((opt < last - 2) && (*opt != TCPOPT_EOL)) {
855 /* Proper length ? */
864 /* Unknown options must be two bytes+ */
878 * check to see if a packet with TCP headers fits within the TCP window.
879 * change timeout depending on whether new packet is a SYN-ACK returning for a
880 * SYN or a RST or FIN which indicate time to close up shop.
882 int fr_tcpstate(is, fin, ip, tcp)
883 register ipstate_t *is;
888 register tcp_seq seq, ack, end;
889 register int ackskew;
890 tcpdata_t *fdata, *tdata;
897 * Find difference between last checked packet and this packet.
899 source = IP6EQ(fin->fin_fi.fi_src, is->is_src);
900 if (source && (ntohs(is->is_sport) != fin->fin_data[0]))
902 fdata = &is->is_tcp.ts_data[!source];
903 tdata = &is->is_tcp.ts_data[source];
904 off = tcp->th_off << 2;
905 seq = ntohl(tcp->th_seq);
906 ack = ntohl(tcp->th_ack);
907 win = ntohs(tcp->th_win);
908 end = seq + fin->fin_dlen - off +
909 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
910 ((tcp->th_flags & TH_FIN) ? 1 : 0);
913 if ((tcp->th_flags & TH_SYN) && (off >= sizeof(*tcp) + 4))
914 wscale = fr_tcpoptions(tcp);
918 MUTEX_ENTER(&is->is_lock);
921 fdata->td_wscale = wscale;
922 else if (wscale == -2)
923 fdata->td_wscale = tdata->td_wscale = 0;
924 win <<= fdata->td_wscale;
926 if ((fdata->td_end == 0) &&
927 (!is->is_fsm || ((tcp->th_flags & TH_OPENING) == TH_OPENING))) {
929 * Must be a (outgoing) SYN-ACK in reply to a SYN.
932 fdata->td_maxwin = 1;
933 fdata->td_maxend = end + win;
938 if (!(tcp->th_flags & TH_ACK)) { /* Pretend an ack was sent */
940 } else if (((tcp->th_flags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) &&
942 /* gross hack to get around certain broken tcp stacks */
947 seq = end = fdata->td_end;
949 maxwin = tdata->td_maxwin;
950 ackskew = tdata->td_end - ack;
952 #define SEQ_GE(a,b) ((int)((a) - (b)) >= 0)
953 #define SEQ_GT(a,b) ((int)((a) - (b)) > 0)
954 if ((SEQ_GE(fdata->td_maxend, end)) &&
955 (SEQ_GE(seq, fdata->td_end - maxwin)) &&
956 /* XXX what about big packets */
957 #define MAXACKWINDOW 66000
958 (ackskew >= -MAXACKWINDOW) &&
959 (ackskew <= MAXACKWINDOW)) {
960 /* if ackskew < 0 then this should be due to fragented
961 * packets. There is no way to know the length of the
962 * total packet in advance.
963 * We do know the total length from the fragment cache though.
964 * Note however that there might be more sessions with
965 * exactly the same source and destination paramters in the
966 * state cache (and source and destination is the only stuff
967 * that is saved in the fragment cache). Note further that
968 * some TCP connections in the state cache are hashed with
969 * sport and dport as well which makes it not worthwhile to
971 * Thus, when ackskew is negative but still seems to belong
972 * to this session, we bump up the destinations end value.
975 * Nearing end of connection, start timeout.
977 /* source ? 0 : 1 -> !source */
978 if (fr_tcp_age(&is->is_age, is->is_state, fin, !source,
979 (int)is->is_fsm) == 0) {
983 /* update max window seen */
984 if (fdata->td_maxwin < win)
985 fdata->td_maxwin = win;
986 if (SEQ_GT(end, fdata->td_end))
988 if (SEQ_GE(ack + win, tdata->td_maxend)) {
989 tdata->td_maxend = ack + win;
994 ATOMIC_INCL(ips_stats.iss_hits);
998 MUTEX_EXIT(&is->is_lock);
999 if ((ret == 0) && ((tcp->th_flags & TH_OPENING) != TH_SYN))
1000 fin->fin_misc |= FM_BADSTATE;
1006 * Match a state table entry against an IP packet.
1008 static int fr_matchsrcdst(is, src, dst, fin, tcp)
1010 union i6addr src, dst;
1014 int ret = 0, rev, out, flags, idx;
1018 rev = IP6NEQ(is->is_dst, dst);
1021 flags = is->is_flags & (FI_WILDA|FI_WILDP);
1026 flags = is->is_flags;
1030 if (!(flags & FI_W_SPORT) && (sp != is->is_sport))
1032 else if (!(flags & FI_W_DPORT) && (dp != is->is_dport))
1037 idx = (out << 1) + rev;
1039 if ((is->is_ifp[idx] == NULL &&
1040 (*is->is_ifname[idx] == '\0' || *is->is_ifname[idx] == '*')) ||
1041 is->is_ifp[idx] == ifp)
1049 if ((IP6EQ(is->is_dst, dst) || (flags & FI_W_DADDR)) &&
1050 (IP6EQ(is->is_src, src) || (flags & FI_W_SADDR)) &&
1051 (!tcp || ((sp == is->is_sport || flags & FI_W_SPORT) &&
1052 (dp == is->is_dport || flags & FI_W_DPORT)))) {
1056 if ((IP6EQ(is->is_dst, src) || (flags & FI_W_DADDR)) &&
1057 (IP6EQ(is->is_src, dst) || (flags & FI_W_SADDR)) &&
1058 (!tcp || ((sp == is->is_dport || flags & FI_W_DPORT) &&
1059 (dp == is->is_sport || flags & FI_W_SPORT)))) {
1067 * Whether or not this should be here, is questionable, but the aim
1068 * is to get this out of the main line.
1071 flags = is->is_flags & (FI_CMP|(FI_CMP<<4));
1073 if (((fin->fin_fl & (flags >> 4)) != (flags & FI_CMP)) ||
1074 (fin->fin_fi.fi_optmsk != is->is_opt) ||
1075 (fin->fin_fi.fi_secmsk != is->is_sec) ||
1076 (fin->fin_fi.fi_auth != is->is_auth))
1079 flags = is->is_flags & (FI_WILDA|FI_WILDP);
1080 if ((flags & (FI_W_SADDR|FI_W_DADDR))) {
1081 if ((flags & FI_W_SADDR) != 0) {
1083 is->is_src = fin->fin_fi.fi_src;
1085 is->is_src = fin->fin_fi.fi_dst;
1087 } else if ((flags & FI_W_DADDR) != 0) {
1089 is->is_dst = fin->fin_fi.fi_dst;
1091 is->is_dst = fin->fin_fi.fi_src;
1094 is->is_flags &= ~(FI_W_SADDR|FI_W_DADDR);
1095 if ((is->is_flags & (FI_WILDA|FI_WILDP)) == 0)
1099 if ((flags & (FI_W_SPORT|FI_W_DPORT))) {
1100 if ((flags & FI_W_SPORT) != 0) {
1103 is->is_send = htonl(tcp->th_seq);
1106 is->is_send = htonl(tcp->th_ack);
1108 is->is_maxsend = is->is_send + 1;
1109 } else if ((flags & FI_W_DPORT) != 0) {
1112 is->is_dend = htonl(tcp->th_ack);
1115 is->is_dend = htonl(tcp->th_seq);
1117 is->is_maxdend = is->is_dend + 1;
1119 is->is_flags &= ~(FI_W_SPORT|FI_W_DPORT);
1125 if (is->is_ifp[idx] == NULL &&
1126 (*is->is_ifname[idx] == '\0' || *is->is_ifname[idx] == '*'))
1130 is->is_ifp[ret] = ifp;
1131 strncpy(is->is_ifname[ret], IFNAME(ifp),
1132 sizeof(is->is_ifname[ret]));
1138 static int fr_matchicmpqueryreply(v, is, icmp, rev)
1145 * If we matched its type on the way in, then when going out
1146 * it will still be the same type.
1148 if ((!rev && (icmp->icmp_type == is->is_type)) ||
1149 (rev && (icmpreplytype4[is->is_type] == icmp->icmp_type))) {
1150 if (icmp->icmp_type != ICMP_ECHOREPLY)
1152 if ((icmp->icmp_id == is->is_icmp.ics_id) &&
1153 (icmp->icmp_seq == is->is_icmp.ics_seq))
1158 else if (is->is_v == 6) {
1159 if ((!rev && (icmp->icmp_type == is->is_type)) ||
1160 (rev && (icmpreplytype6[is->is_type] == icmp->icmp_type))) {
1161 if (icmp->icmp_type != ICMP6_ECHO_REPLY)
1163 if ((icmp->icmp_id == is->is_icmp.ics_id) &&
1164 (icmp->icmp_seq == is->is_icmp.ics_seq))
1172 static frentry_t *fr_checkicmpmatchingstate(ip, fin)
1176 register ipstate_t *is, **isp;
1177 register u_short sport, dport;
1179 u_short savelen, ohlen;
1180 union i6addr dst, src;
1191 * Does it at least have the return (basic) IP header ?
1192 * Only a basic IP header (no options) should be with
1193 * an ICMP error header.
1195 if (((ip->ip_v != 4) || (ip->ip_hl != 5)) ||
1196 (fin->fin_plen < ICMPERR_MINPKTLEN))
1199 ic = (struct icmp *)fin->fin_dp;
1200 type = ic->icmp_type;
1202 * If it's not an error type, then return
1204 if ((type != ICMP_UNREACH) && (type != ICMP_SOURCEQUENCH) &&
1205 (type != ICMP_REDIRECT) && (type != ICMP_TIMXCEED) &&
1206 (type != ICMP_PARAMPROB))
1209 oip = (ip_t *)((char *)ic + ICMPERR_ICMPHLEN);
1210 ohlen = oip->ip_hl << 2;
1211 if (fin->fin_plen < ICMPERR_MAXPKTLEN + ohlen - sizeof(*oip))
1217 len = fin->fin_dlen - ICMPERR_ICMPHLEN;
1218 if ((len <= 0) || (ohlen > len))
1222 * Is the buffer big enough for all of it ? It's the size of the IP
1223 * header claimed in the encapsulated part which is of concern. It
1224 * may be too big to be in this buffer but not so big that it's
1225 * outside the ICMP packet, leading to TCP deref's causing problems.
1226 * This is possible because we don't know how big oip_hl is when we
1227 * do the pullup early in fr_check() and thus can't gaurantee it is
1236 if ((char *)oip + len > (char *)m->b_wptr)
1239 m = *(mb_t **)fin->fin_mp;
1240 if ((char *)oip + len > (char *)ip + m->m_len)
1247 * in the IPv4 case we must zero the i6addr union otherwise
1248 * the IP6EQ and IP6NEQ macros produce the wrong results because
1249 * of the 'junk' in the unused part of the union
1251 bzero((char *)&src, sizeof(src));
1252 bzero((char *)&dst, sizeof(dst));
1253 bzero((char *)&ofin, sizeof(ofin));
1254 ofin.fin_ifp = fin->fin_ifp;
1255 ofin.fin_out = !fin->fin_out;
1262 icmp = (icmphdr_t *)((char *)oip + ohlen);
1265 * a ICMP error can only be generated as a result of an
1266 * ICMP query, not as the response on an ICMP error
1268 * XXX theoretically ICMP_ECHOREP and the other reply's are
1269 * ICMP query's as well, but adding them here seems strange XXX
1271 if ((icmp->icmp_type != ICMP_ECHO) &&
1272 (icmp->icmp_type != ICMP_TSTAMP) &&
1273 (icmp->icmp_type != ICMP_IREQ) &&
1274 (icmp->icmp_type != ICMP_MASKREQ))
1278 * perform a lookup of the ICMP packet in the state table
1280 hv = (pr = oip->ip_p);
1281 src.in4 = oip->ip_src;
1282 hv += src.in4.s_addr;
1283 dst.in4 = oip->ip_dst;
1284 hv += dst.in4.s_addr;
1285 hv += icmp->icmp_id;
1286 hv += icmp->icmp_seq;
1289 savelen = oip->ip_len;
1291 fr_makefrip(ohlen, oip, &ofin);
1292 oip->ip_len = savelen;
1294 READ_ENTER(&ipf_state);
1295 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext)
1296 if ((is->is_p == pr) && (is->is_v == 4) &&
1297 fr_matchsrcdst(is, src, dst, &ofin, NULL) &&
1298 fr_matchicmpqueryreply(is->is_v, is, icmp, fin->fin_rev)) {
1299 ips_stats.iss_hits++;
1301 is->is_bytes += ip->ip_len;
1305 RWLOCK_EXIT(&ipf_state);
1310 if (fin->fin_plen < ICMPERR_MAXPKTLEN)
1317 tcp = (tcphdr_t *)((char *)oip + ohlen);
1318 dport = tcp->th_dport;
1319 sport = tcp->th_sport;
1321 hv = (pr = oip->ip_p);
1322 src.in4 = oip->ip_src;
1323 hv += src.in4.s_addr;
1324 dst.in4 = oip->ip_dst;
1325 hv += dst.in4.s_addr;
1330 * we make an fin entry to be able to feed it to
1331 * matchsrcdst note that not all fields are encessary
1332 * but this is the cleanest way. Note further we fill
1333 * in fin_mp such that if someone uses it we'll get
1334 * a kernel panic. fr_matchsrcdst does not use this.
1336 * watch out here, as ip is in host order and oip in network
1337 * order. Any change we make must be undone afterwards.
1339 savelen = oip->ip_len;
1341 fr_makefrip(ohlen, oip, &ofin);
1342 oip->ip_len = savelen;
1343 READ_ENTER(&ipf_state);
1344 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) {
1346 * Only allow this icmp though if the
1347 * encapsulated packet was allowed through the
1348 * other way around. Note that the minimal amount
1349 * of info present does not allow for checking against
1350 * tcp internals such as seq and ack numbers.
1352 if ((is->is_p == pr) && (is->is_v == 4) &&
1353 fr_matchsrcdst(is, src, dst, &ofin, tcp)) {
1355 ips_stats.iss_hits++;
1357 is->is_bytes += fin->fin_plen;
1359 * we deliberately do not touch the timeouts
1360 * for the accompanying state table entry.
1361 * It remains to be seen if that is correct. XXX
1366 RWLOCK_EXIT(&ipf_state);
1372 * Move a state hash table entry from its old location at is->is_hv to
1373 * its new location, indexed by hv % fr_statesize.
1375 static void fr_ipsmove(isp, is, hv)
1376 ipstate_t **isp, *is;
1383 * Remove the hash from the old location...
1386 is->is_hnext->is_phnext = isp;
1387 *isp = is->is_hnext;
1388 if (ips_table[hvm] == NULL)
1389 ips_stats.iss_inuse--;
1392 * ...and put the hash in the new one.
1394 hvm = hv % fr_statesize;
1396 isp = &ips_table[hvm];
1398 (*isp)->is_phnext = &is->is_hnext;
1400 ips_stats.iss_inuse++;
1401 is->is_phnext = isp;
1402 is->is_hnext = *isp;
1408 * Check if a packet has a registered state.
1410 frentry_t *fr_checkstate(ip, fin)
1414 union i6addr dst, src;
1415 register ipstate_t *is, **isp;
1417 u_int hv, hvm, hlen, tryagain, pass, v;
1423 if ((ips_list == NULL) || (fin->fin_off != 0) || fr_state_lock ||
1424 (fin->fin_fl & FI_SHORT))
1428 hlen = fin->fin_hlen;
1429 tcp = (tcphdr_t *)((char *)ip + hlen);
1430 ic = (struct icmp *)tcp;
1431 hv = (pr = fin->fin_fi.fi_p);
1432 src = fin->fin_fi.fi_src;
1433 dst = fin->fin_fi.fi_dst;
1434 hv += src.in4.s_addr;
1435 hv += dst.in4.s_addr;
1438 * Search the hash table for matching packet header info.
1439 * At the bottom of this switch statement, the following is expected:
1440 * is == NULL, no lock on ipf_state is held.
1441 * is != NULL, a lock on ipf_state is held.
1443 v = fin->fin_fi.fi_v;
1446 hv += fin->fin_fi.fi_src.i6[1];
1447 hv += fin->fin_fi.fi_src.i6[2];
1448 hv += fin->fin_fi.fi_src.i6[3];
1450 if ((fin->fin_p == IPPROTO_ICMPV6) &&
1451 IN6_IS_ADDR_MULTICAST(&fin->fin_fi.fi_dst.in6)) {
1452 hv -= dst.in4.s_addr;
1454 hv += fin->fin_fi.fi_dst.i6[1];
1455 hv += fin->fin_fi.fi_dst.i6[2];
1456 hv += fin->fin_fi.fi_dst.i6[3];
1464 case IPPROTO_ICMPV6 :
1468 if ((ic->icmp_type == ICMP6_ECHO_REQUEST) ||
1469 (ic->icmp_type == ICMP6_ECHO_REPLY)) {
1474 READ_ENTER(&ipf_state);
1476 hvm = hv % fr_statesize;
1477 for (isp = &ips_table[hvm]; (is = *isp); isp = &is->is_hnext)
1478 if ((is->is_p == pr) && (is->is_v == v) &&
1479 fr_matchsrcdst(is, src, dst, fin, NULL) &&
1480 fr_matchicmpqueryreply(v, is, ic, fin->fin_rev)) {
1482 if (is->is_frage[rev] != 0)
1483 is->is_age = is->is_frage[rev];
1485 is->is_age = fr_icmpacktimeout;
1487 is->is_age = fr_icmptimeout;
1492 if (tryagain && !(is->is_flags & FI_W_DADDR)) {
1493 hv += fin->fin_fi.fi_src.i6[0];
1494 hv += fin->fin_fi.fi_src.i6[1];
1495 hv += fin->fin_fi.fi_src.i6[2];
1496 hv += fin->fin_fi.fi_src.i6[3];
1497 fr_ipsmove(isp, is, hv);
1498 MUTEX_DOWNGRADE(&ipf_state);
1502 RWLOCK_EXIT(&ipf_state);
1505 * No matching icmp state entry. Perhaps this is a
1506 * response to another state entry.
1508 if ((ips_wild != 0) && (v == 6) && (tryagain == 0) &&
1509 !IN6_IS_ADDR_MULTICAST(&fin->fin_fi.fi_src.in6)) {
1510 hv -= fin->fin_fi.fi_src.i6[0];
1511 hv -= fin->fin_fi.fi_src.i6[1];
1512 hv -= fin->fin_fi.fi_src.i6[2];
1513 hv -= fin->fin_fi.fi_src.i6[3];
1515 WRITE_ENTER(&ipf_state);
1519 fr = fr_checkicmp6matchingstate((ip6_t *)ip, fin);
1530 hvm = hv % fr_statesize;
1531 READ_ENTER(&ipf_state);
1532 for (isp = &ips_table[hvm]; (is = *isp); isp = &is->is_hnext)
1533 if ((is->is_p == pr) && (is->is_v == v) &&
1534 fr_matchsrcdst(is, src, dst, fin, NULL) &&
1535 fr_matchicmpqueryreply(v, is, ic, fin->fin_rev)) {
1537 if (is->is_frage[rev] != 0)
1538 is->is_age = is->is_frage[rev];
1539 else if (fin->fin_rev)
1540 is->is_age = fr_icmpacktimeout;
1542 is->is_age = fr_icmptimeout;
1548 RWLOCK_EXIT(&ipf_state);
1550 * No matching icmp state entry. Perhaps this is a
1551 * response to another state entry.
1553 fr = fr_checkicmpmatchingstate(ip, fin);
1559 * Just plain ignore RST flag set with either FIN or SYN.
1561 if ((tcp->th_flags & TH_RST) &&
1562 ((tcp->th_flags & (TH_FIN|TH_SYN|TH_RST)) != TH_RST))
1566 register u_short dport, sport;
1568 dport = tcp->th_dport;
1569 sport = tcp->th_sport;
1573 READ_ENTER(&ipf_state);
1575 hvm = hv % fr_statesize;
1576 for (isp = &ips_table[hvm]; (is = *isp); isp = &is->is_hnext)
1577 if ((is->is_p == pr) && (is->is_v == v) &&
1578 fr_matchsrcdst(is, src, dst, fin, tcp)) {
1580 if ((pr == IPPROTO_TCP)) {
1581 if (!fr_tcpstate(is, fin, ip, tcp))
1583 } else if ((pr == IPPROTO_UDP)) {
1584 if (is->is_frage[rev] != 0)
1585 is->is_age = is->is_frage[rev];
1586 else if (fin->fin_rev)
1587 is->is_age = fr_udpacktimeout;
1589 is->is_age = fr_udptimeout;
1595 !(is->is_flags & (FI_WILDP|FI_WILDA))) {
1598 fr_ipsmove(isp, is, hv);
1599 MUTEX_DOWNGRADE(&ipf_state);
1604 RWLOCK_EXIT(&ipf_state);
1605 if (!tryagain && ips_wild) {
1609 WRITE_ENTER(&ipf_state);
1617 READ_ENTER(&ipf_state);
1618 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) {
1619 if ((is->is_p == pr) && (is->is_v == v) &&
1620 fr_matchsrcdst(is, src, dst, fin, NULL)) {
1622 if (is->is_frage[rev] != 0)
1623 is->is_age = is->is_frage[rev];
1625 is->is_age = fr_udptimeout;
1630 RWLOCK_EXIT(&ipf_state);
1636 ATOMIC_INCL(ips_stats.iss_miss);
1640 MUTEX_ENTER(&is->is_lock);
1641 is->is_bytes += fin->fin_plen;
1642 ips_stats.iss_hits++;
1644 MUTEX_EXIT(&is->is_lock);
1646 fin->fin_rule = is->is_rulen;
1648 fin->fin_group = fr->fr_group;
1649 fin->fin_icode = fr->fr_icode;
1653 RWLOCK_EXIT(&ipf_state);
1654 if ((fin->fin_fl & FI_FRAG) && (pass & FR_KEEPFRAG))
1655 ipfr_newfrag(ip, fin);
1657 if ((tcp != NULL) && (tcp->th_flags & TCP_CLOSE))
1665 * Sync. state entries. If interfaces come or go or just change position,
1668 void ip_statesync(ifp)
1671 register ipstate_t *is;
1674 WRITE_ENTER(&ipf_state);
1675 for (is = ips_list; is; is = is->is_next) {
1676 for (i = 0; i < 4; i++) {
1677 if (is->is_ifp[i] == ifp) {
1678 is->is_ifpin = GETUNIT(is->is_ifname[i],
1681 is->is_ifp[i] = (void *)-1;
1685 RWLOCK_EXIT(&ipf_state);
1690 * Must always be called with fr_ipfstate held as a write lock.
1692 static void fr_delstate(is)
1697 if (is->is_flags & (FI_WILDP|FI_WILDA))
1700 is->is_next->is_pnext = is->is_pnext;
1701 *is->is_pnext = is->is_next;
1703 is->is_hnext->is_phnext = is->is_phnext;
1704 *is->is_phnext = is->is_hnext;
1705 if (ips_table[is->is_hv] == NULL)
1706 ips_stats.iss_inuse--;
1713 if (fr->fr_ref == 0) {
1718 MUTEX_DESTROY(&is->is_lock);
1726 * Free memory in use by all state info. kept.
1728 void fr_stateunload()
1730 register ipstate_t *is;
1732 WRITE_ENTER(&ipf_state);
1733 while ((is = ips_list))
1735 ips_stats.iss_inuse = 0;
1737 RWLOCK_EXIT(&ipf_state);
1739 KFREES(ips_table, fr_statesize * sizeof(ipstate_t *));
1745 * Slowly expire held state for thingslike UDP and ICMP. Timeouts are set
1746 * in expectation of this being called twice per second.
1748 void fr_timeoutstate()
1750 register ipstate_t *is, **isp;
1751 #if defined(_KERNEL) && !SOLARIS
1756 WRITE_ENTER(&ipf_state);
1757 for (isp = &ips_list; (is = *isp); )
1758 if (is->is_age && !--is->is_age) {
1759 if (is->is_p == IPPROTO_TCP)
1760 ips_stats.iss_fin++;
1762 ips_stats.iss_expire++;
1764 ipstate_log(is, ISL_EXPIRE);
1769 if (fr_state_doflush) {
1770 (void) fr_state_flush(2, 0);
1771 fr_state_doflush = 0;
1773 RWLOCK_EXIT(&ipf_state);
1779 * Original idea freom Pradeep Krishnan for use primarily with NAT code.
1780 * (pkrishna@netcom.com)
1782 * Rewritten by Arjan de Vet <Arjan.deVet@adv.iae.nl>, 2000-07-29:
1784 * - (try to) base state transitions on real evidence only,
1785 * i.e. packets that are sent and have been received by ipfilter;
1786 * diagram 18.12 of TCP/IP volume 1 by W. Richard Stevens was used.
1788 * - deal with half-closed connections correctly;
1790 * - store the state of the source in state[0] such that ipfstat
1791 * displays the state as source/dest instead of dest/source; the calls
1792 * to fr_tcp_age have been changed accordingly.
1796 * state[0] = state of source (host that initiated connection)
1797 * state[1] = state of dest (host that accepted the connection)
1799 * dir == 0 : a packet from source to dest
1800 * dir == 1 : a packet from dest to source
1803 int fr_tcp_age(age, state, fin, dir, fsm)
1809 tcphdr_t *tcp = (tcphdr_t *)fin->fin_dp;
1810 u_char flags = tcp->th_flags;
1814 ostate = state[1 - dir];
1816 dlen = fin->fin_plen - fin->fin_hlen - (tcp->th_off << 2);
1818 if (flags & TH_RST) {
1819 if (!(tcp->th_flags & TH_PUSH) && !dlen) {
1820 *age = fr_tcpclosed;
1821 state[dir] = TCPS_CLOSED;
1823 *age = fr_tcpclosewait;
1824 state[dir] = TCPS_CLOSE_WAIT;
1833 case TCPS_CLOSED: /* 0 */
1834 if ((flags & TH_OPENING) == TH_OPENING) {
1836 * 'dir' received an S and sends SA in response,
1837 * CLOSED -> SYN_RECEIVED
1839 state[dir] = TCPS_SYN_RECEIVED;
1840 newage = fr_tcptimeout;
1841 } else if ((flags & TH_OPENING) == TH_SYN) {
1842 /* 'dir' sent S, CLOSED -> SYN_SENT */
1843 state[dir] = TCPS_SYN_SENT;
1844 newage = fr_tcptimeout;
1847 * The next piece of code makes it possible to get
1848 * already established connections into the state table
1849 * after a restart or reload of the filter rules; this
1850 * does not work when a strict 'flags S keep state' is
1851 * used for tcp connections of course
1853 if (!fsm && (flags & (TH_FIN|TH_SYN|TH_RST|TH_ACK)) == TH_ACK) {
1854 /* we saw an A, guess 'dir' is in ESTABLISHED mode */
1855 if (state[1 - dir] == TCPS_CLOSED ||
1856 state[1 - dir] == TCPS_ESTABLISHED) {
1857 state[dir] = TCPS_ESTABLISHED;
1858 newage = fr_tcpidletimeout;
1862 * TODO: besides regular ACK packets we can have other
1863 * packets as well; it is yet to be determined how we
1864 * should initialize the states in those cases
1868 case TCPS_LISTEN: /* 1 */
1872 case TCPS_SYN_SENT: /* 2 */
1873 if ((flags & ~(TH_ECN|TH_CWR)) == TH_SYN) {
1875 * A retransmitted SYN packet. We do not reset the
1876 * timeout here to fr_tcptimeout because a connection
1877 * connect timeout does not renew after every packet
1878 * that is sent. We need to set newage to something
1879 * to indicate the packet has passed the check for its
1880 * flags being valid in the TCP FSM.
1883 } else if ((flags & (TH_SYN|TH_FIN|TH_ACK)) == TH_ACK) {
1885 * We see an A from 'dir' which is in SYN_SENT
1886 * state: 'dir' sent an A in response to an SA
1887 * which it received, SYN_SENT -> ESTABLISHED
1889 state[dir] = TCPS_ESTABLISHED;
1890 newage = fr_tcpidletimeout;
1891 } else if (flags & TH_FIN) {
1893 * We see an F from 'dir' which is in SYN_SENT
1894 * state and wants to close its side of the
1895 * connection; SYN_SENT -> FIN_WAIT_1
1897 state[dir] = TCPS_FIN_WAIT_1;
1898 newage = fr_tcpidletimeout; /* or fr_tcptimeout? */
1899 } else if ((flags & TH_OPENING) == TH_OPENING) {
1901 * We see an SA from 'dir' which is already in
1902 * SYN_SENT state, this means we have a
1903 * simultaneous open; SYN_SENT -> SYN_RECEIVED
1905 state[dir] = TCPS_SYN_RECEIVED;
1906 newage = fr_tcptimeout;
1910 case TCPS_SYN_RECEIVED: /* 3 */
1911 if ((flags & (TH_SYN|TH_FIN|TH_ACK)) == TH_ACK) {
1913 * We see an A from 'dir' which was in SYN_RECEIVED
1914 * state so it must now be in established state,
1915 * SYN_RECEIVED -> ESTABLISHED
1917 state[dir] = TCPS_ESTABLISHED;
1918 newage = fr_tcpidletimeout;
1919 } else if ((flags & ~(TH_ECN|TH_CWR)) == TH_OPENING) {
1921 * We see an SA from 'dir' which is already in
1922 * SYN_RECEIVED state.
1924 newage = fr_tcptimeout;
1925 } else if (flags & TH_FIN) {
1927 * We see an F from 'dir' which is in SYN_RECEIVED
1928 * state and wants to close its side of the connection;
1929 * SYN_RECEIVED -> FIN_WAIT_1
1931 state[dir] = TCPS_FIN_WAIT_1;
1932 newage = fr_tcpidletimeout;
1936 case TCPS_ESTABLISHED: /* 4 */
1937 if (flags & TH_FIN) {
1939 * 'dir' closed its side of the connection; this
1940 * gives us a half-closed connection;
1941 * ESTABLISHED -> FIN_WAIT_1
1943 state[dir] = TCPS_FIN_WAIT_1;
1944 newage = fr_tcphalfclosed;
1945 } else if (flags & TH_ACK) {
1946 /* an ACK, should we exclude other flags here? */
1947 if (ostate == TCPS_FIN_WAIT_1) {
1949 * We know the other side did an active close,
1950 * so we are ACKing the recvd FIN packet (does
1951 * the window matching code guarantee this?)
1952 * and go into CLOSE_WAIT state; this gives us
1953 * a half-closed connection
1955 state[dir] = TCPS_CLOSE_WAIT;
1956 newage = fr_tcphalfclosed;
1957 } else if (ostate < TCPS_CLOSE_WAIT)
1959 * Still a fully established connection,
1962 newage = fr_tcpidletimeout;
1966 case TCPS_CLOSE_WAIT: /* 5 */
1967 if (flags & TH_FIN) {
1969 * Application closed and 'dir' sent a FIN, we're now
1970 * going into LAST_ACK state
1972 newage = fr_tcplastack;
1973 state[dir] = TCPS_LAST_ACK;
1976 * We remain in CLOSE_WAIT because the other side has
1977 * closed already and we did not close our side yet;
1980 newage = fr_tcphalfclosed;
1984 case TCPS_FIN_WAIT_1: /* 6 */
1985 if ((flags & TH_ACK) && ostate > TCPS_CLOSE_WAIT) {
1987 * If the other side is not active anymore it has sent
1988 * us a FIN packet that we are ack'ing now with an ACK;
1989 * this means both sides have now closed the connection
1990 * and we go into TIME_WAIT
1993 * XXX: how do we know we really are ACKing the FIN
1994 * packet here? does the window code guarantee that?
1996 state[dir] = TCPS_TIME_WAIT;
1997 newage = fr_tcptimeout;
2000 * We closed our side of the connection already but the
2001 * other side is still active (ESTABLISHED/CLOSE_WAIT);
2002 * continue with this half-closed connection
2004 newage = fr_tcphalfclosed;
2007 case TCPS_CLOSING: /* 7 */
2011 case TCPS_LAST_ACK: /* 8 */
2012 if (flags & TH_ACK) {
2013 if ((flags & TH_PUSH) || dlen)
2015 * There is still data to be delivered, reset
2018 newage = fr_tcplastack;
2023 * We cannot detect when we go out of LAST_ACK state to CLOSED
2024 * because that is based on the reception of ACK packets;
2025 * ipfilter can only detect that a packet has been sent by a
2030 case TCPS_FIN_WAIT_2: /* 9 */
2034 case TCPS_TIME_WAIT: /* 10 */
2035 newage = fr_tcptimeout; /* default 4 mins */
2036 /* we're in 2MSL timeout now */
2049 void ipstate_log(is, type)
2058 ipsl.isl_type = type;
2059 ipsl.isl_pkts = is->is_pkts;
2060 ipsl.isl_bytes = is->is_bytes;
2061 ipsl.isl_src = is->is_src;
2062 ipsl.isl_dst = is->is_dst;
2063 ipsl.isl_p = is->is_p;
2064 ipsl.isl_v = is->is_v;
2065 ipsl.isl_flags = is->is_flags;
2066 ipsl.isl_rulen = is->is_rulen;
2067 ipsl.isl_group = is->is_group;
2068 if (ipsl.isl_p == IPPROTO_TCP || ipsl.isl_p == IPPROTO_UDP) {
2069 ipsl.isl_sport = is->is_sport;
2070 ipsl.isl_dport = is->is_dport;
2071 if (ipsl.isl_p == IPPROTO_TCP) {
2072 ipsl.isl_state[0] = is->is_state[0];
2073 ipsl.isl_state[1] = is->is_state[1];
2075 } else if (ipsl.isl_p == IPPROTO_ICMP) {
2076 ipsl.isl_itype = is->is_icmp.ics_type;
2077 } else if (ipsl.isl_p == IPPROTO_ICMPV6) {
2078 ipsl.isl_itype = is->is_icmp.ics_type;
2080 ipsl.isl_ps.isl_filler[0] = 0;
2081 ipsl.isl_ps.isl_filler[1] = 0;
2084 sizes[0] = sizeof(ipsl);
2087 (void) ipllog(IPL_LOGSTATE, NULL, items, sizes, types, 1);
2093 frentry_t *fr_checkicmp6matchingstate(ip, fin)
2097 register ipstate_t *is, **isp;
2098 register u_short sport, dport;
2100 struct icmp6_hdr *ic, *oic;
2101 union i6addr dst, src;
2111 * Does it at least have the return (basic) IP header ?
2112 * Only a basic IP header (no options) should be with
2113 * an ICMP error header.
2115 if ((fin->fin_v != 6) || (fin->fin_plen < ICMP6ERR_MINPKTLEN))
2117 ic = (struct icmp6_hdr *)fin->fin_dp;
2118 type = ic->icmp6_type;
2120 * If it's not an error type, then return
2122 if ((type != ICMP6_DST_UNREACH) && (type != ICMP6_PACKET_TOO_BIG) &&
2123 (type != ICMP6_TIME_EXCEEDED) && (type != ICMP6_PARAM_PROB))
2126 oip = (ip6_t *)((char *)ic + ICMPERR_ICMPHLEN);
2127 if (fin->fin_plen < sizeof(*oip))
2130 if ((oip->ip6_nxt != IPPROTO_TCP) && (oip->ip6_nxt != IPPROTO_UDP) &&
2131 (oip->ip6_nxt != IPPROTO_ICMPV6))
2134 bzero((char *)&ofin, sizeof(ofin));
2135 ofin.fin_out = !fin->fin_out;
2136 ofin.fin_ifp = fin->fin_ifp;
2139 if (oip->ip6_nxt == IPPROTO_ICMPV6) {
2140 oic = (struct icmp6_hdr *)(oip + 1);
2142 * a ICMP error can only be generated as a result of an
2143 * ICMP query, not as the response on an ICMP error
2145 * XXX theoretically ICMP_ECHOREP and the other reply's are
2146 * ICMP query's as well, but adding them here seems strange XXX
2148 if (!(oic->icmp6_type & ICMP6_INFOMSG_MASK))
2152 * perform a lookup of the ICMP packet in the state table
2154 hv = (pr = oip->ip6_nxt);
2155 src.in6 = oip->ip6_src;
2156 hv += src.in4.s_addr;
2157 dst.in6 = oip->ip6_dst;
2158 hv += dst.in4.s_addr;
2159 hv += oic->icmp6_id;
2160 hv += oic->icmp6_seq;
2163 oip->ip6_plen = ntohs(oip->ip6_plen);
2164 fr_makefrip(sizeof(*oip), (ip_t *)oip, &ofin);
2165 oip->ip6_plen = htons(oip->ip6_plen);
2167 READ_ENTER(&ipf_state);
2168 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext)
2169 if ((is->is_p == pr) &&
2170 (oic->icmp6_id == is->is_icmp.ics_id) &&
2171 (oic->icmp6_seq == is->is_icmp.ics_seq) &&
2172 fr_matchsrcdst(is, src, dst, &ofin, NULL)) {
2174 * in the state table ICMP query's are stored
2175 * with the type of the corresponding ICMP
2176 * response. Correct here
2178 if (((is->is_type == ICMP6_ECHO_REPLY) &&
2179 (oic->icmp6_type == ICMP6_ECHO_REQUEST)) ||
2180 (is->is_type - 1 == oic->icmp6_type )) {
2181 ips_stats.iss_hits++;
2183 is->is_bytes += fin->fin_plen;
2187 RWLOCK_EXIT(&ipf_state);
2192 tcp = (tcphdr_t *)(oip + 1);
2193 dport = tcp->th_dport;
2194 sport = tcp->th_sport;
2196 hv = (pr = oip->ip6_nxt);
2197 src.in6 = oip->ip6_src;
2198 hv += src.in4.s_addr;
2202 dst.in6 = oip->ip6_dst;
2203 hv += dst.in4.s_addr;
2211 * we make an fin entry to be able to feed it to
2212 * matchsrcdst note that not all fields are encessary
2213 * but this is the cleanest way. Note further we fill
2214 * in fin_mp such that if someone uses it we'll get
2215 * a kernel panic. fr_matchsrcdst does not use this.
2217 * watch out here, as ip is in host order and oip in network
2218 * order. Any change we make must be undone afterwards.
2220 savelen = oip->ip6_plen;
2221 oip->ip6_plen = ip->ip6_plen - sizeof(*ip) - ICMPERR_ICMPHLEN;
2222 fr_makefrip(sizeof(*oip), (ip_t *)oip, &ofin);
2223 oip->ip6_plen = savelen;
2224 READ_ENTER(&ipf_state);
2225 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) {
2227 * Only allow this icmp though if the
2228 * encapsulated packet was allowed through the
2229 * other way around. Note that the minimal amount
2230 * of info present does not allow for checking against
2231 * tcp internals such as seq and ack numbers.
2233 if ((is->is_p == pr) && (is->is_v == 6) &&
2234 fr_matchsrcdst(is, src, dst, &ofin, tcp)) {
2236 ips_stats.iss_hits++;
2238 is->is_bytes += fin->fin_plen;
2240 * we deliberately do not touch the timeouts
2241 * for the accompanying state table entry.
2242 * It remains to be seen if that is correct. XXX
2244 RWLOCK_EXIT(&ipf_state);
2248 RWLOCK_EXIT(&ipf_state);