2 * Copyright (C) 1995-2002 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
7 #if defined(__sgi) && (IRIX > 602)
8 # include <sys/ptimers.h>
10 #include <sys/errno.h>
11 #include <sys/types.h>
12 #include <sys/param.h>
14 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
16 # include "opt_ipfilter_log.h"
18 #if defined(_KERNEL) && defined(__FreeBSD_version) && \
19 (__FreeBSD_version >= 400000) && !defined(KLD_MODULE)
20 #include "opt_inet6.h"
22 #if !defined(_KERNEL) && !defined(KERNEL) && !defined(__KERNEL__)
28 # include <linux/kernel.h>
29 # include <linux/module.h>
32 #if (defined(KERNEL) || defined(_KERNEL)) && (__FreeBSD_version >= 220000)
33 # include <sys/filio.h>
34 # include <sys/fcntl.h>
35 # if (__FreeBSD_version >= 300000) && !defined(IPFILTER_LKM)
36 # include "opt_ipfilter.h"
39 # include <sys/ioctl.h>
43 # include <sys/protosw.h>
45 #include <sys/socket.h>
46 #if (defined(_KERNEL) || defined(KERNEL)) && !defined(linux)
47 # include <sys/systm.h>
49 #if !defined(__SVR4) && !defined(__svr4__)
51 # include <sys/mbuf.h>
54 # include <sys/filio.h>
55 # include <sys/byteorder.h>
57 # include <sys/dditypes.h>
59 # include <sys/stream.h>
60 # include <sys/kmem.h>
67 #include <net/route.h>
68 #include <netinet/in.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/ip.h>
71 #include <netinet/tcp.h>
73 # include <netinet/ip_var.h>
74 # include <netinet/tcp_fsm.h>
76 #include <netinet/udp.h>
77 #include <netinet/ip_icmp.h>
78 #include "netinet/ip_compat.h"
79 #include <netinet/tcpip.h>
80 #include "netinet/ip_fil.h"
81 #include "netinet/ip_nat.h"
82 #include "netinet/ip_frag.h"
83 #include "netinet/ip_state.h"
85 #include <netinet/icmp6.h>
87 #if (__FreeBSD_version >= 300000)
88 # include <sys/malloc.h>
89 # if (defined(_KERNEL) || defined(KERNEL)) && !defined(IPFILTER_LKM)
90 # include <sys/libkern.h>
91 # include <sys/systm.h>
96 static const char sccsid[] = "@(#)ip_state.c 1.8 6/5/96 (C) 1993-2000 Darren Reed";
97 /*static const char rcsid[] = "@(#)$Id: ip_state.c,v 2.30.2.74 2002/07/27 15:58:10 darrenr Exp $";*/
98 static const char rcsid[] = "@(#)$FreeBSD: src/sys/contrib/ipfilter/netinet/ip_state.c,v 1.21.2.5 2003/03/01 03:55:54 darrenr Exp $";
102 # define MIN(a,b) (((a)<(b))?(a):(b))
105 #define TCP_CLOSE (TH_FIN|TH_RST)
107 static ipstate_t **ips_table = NULL;
108 static int ips_num = 0;
109 static int ips_wild = 0;
110 static ips_stat_t ips_stats;
111 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
112 extern KRWLOCK_T ipf_state, ipf_mutex;
113 extern kmutex_t ipf_rw;
117 static frentry_t *fr_checkicmp6matchingstate __P((ip6_t *, fr_info_t *));
119 static int fr_matchsrcdst __P((ipstate_t *, union i6addr, union i6addr,
120 fr_info_t *, tcphdr_t *));
121 static frentry_t *fr_checkicmpmatchingstate __P((ip_t *, fr_info_t *));
122 static int fr_matchicmpqueryreply __P((int, ipstate_t *, icmphdr_t *, int));
123 static int fr_state_flush __P((int, int));
124 static ips_stat_t *fr_statetstats __P((void));
125 static void fr_delstate __P((ipstate_t *));
126 static int fr_state_remove __P((caddr_t));
127 static void fr_ipsmove __P((ipstate_t **, ipstate_t *, u_int));
128 static int fr_tcpoptions __P((tcphdr_t *));
129 int fr_stputent __P((caddr_t));
130 int fr_stgetent __P((caddr_t));
131 void fr_stinsert __P((ipstate_t *));
134 #define FIVE_DAYS (2 * 5 * 86400) /* 5 days: half closed session */
136 #define TCP_MSL 240 /* 2 minutes */
137 u_long fr_tcpidletimeout = FIVE_DAYS,
138 fr_tcpclosewait = 2 * TCP_MSL,
139 fr_tcplastack = 2 * TCP_MSL,
140 fr_tcptimeout = 2 * TCP_MSL,
142 fr_tcphalfclosed = 2 * 2 * 3600, /* 2 hours */
144 fr_udpacktimeout = 24,
145 fr_icmptimeout = 120,
146 fr_icmpacktimeout = 12;
147 int fr_statemax = IPSTATE_MAX,
148 fr_statesize = IPSTATE_SIZE;
149 int fr_state_doflush = 0,
151 ipstate_t *ips_list = NULL;
153 static int icmpreplytype4[ICMP_MAXTYPE + 1];
155 static int icmpreplytype6[ICMP6_MAXTYPE + 1];
162 KMALLOCS(ips_table, ipstate_t **, fr_statesize * sizeof(ipstate_t *));
163 if (ips_table != NULL)
164 bzero((char *)ips_table, fr_statesize * sizeof(ipstate_t *));
168 /* fill icmp reply type table */
169 for (i = 0; i <= ICMP_MAXTYPE; i++)
170 icmpreplytype4[i] = -1;
171 icmpreplytype4[ICMP_ECHO] = ICMP_ECHOREPLY;
172 icmpreplytype4[ICMP_TSTAMP] = ICMP_TSTAMPREPLY;
173 icmpreplytype4[ICMP_IREQ] = ICMP_IREQREPLY;
174 icmpreplytype4[ICMP_MASKREQ] = ICMP_MASKREPLY;
176 /* fill icmp reply type table */
177 for (i = 0; i <= ICMP6_MAXTYPE; i++)
178 icmpreplytype6[i] = -1;
179 icmpreplytype6[ICMP6_ECHO_REQUEST] = ICMP6_ECHO_REPLY;
180 icmpreplytype6[ICMP6_MEMBERSHIP_QUERY] = ICMP6_MEMBERSHIP_REPORT;
181 icmpreplytype6[ICMP6_NI_QUERY] = ICMP6_NI_REPLY;
182 icmpreplytype6[ND_ROUTER_SOLICIT] = ND_ROUTER_ADVERT;
183 icmpreplytype6[ND_NEIGHBOR_SOLICIT] = ND_NEIGHBOR_ADVERT;
190 static ips_stat_t *fr_statetstats()
192 ips_stats.iss_active = ips_num;
193 ips_stats.iss_table = ips_table;
194 ips_stats.iss_list = ips_list;
200 * flush state tables. two actions currently defined:
201 * which == 0 : flush all state table entries
202 * which == 1 : flush TCP connections which have started to close but are
203 * stuck for some reason.
204 * which == 2 : flush TCP connections which have been idle for a long time,
205 * starting at > 4 days idle and working back in successive half-
206 * days to at most 12 hours old.
208 static int fr_state_flush(which, proto)
211 ipstate_t *is, **isp;
212 #if defined(_KERNEL) && !SOLARIS
215 int delete, removed = 0, try;
218 for (isp = &ips_list; (is = *isp); ) {
221 if ((proto != 0) && (is->is_v != proto))
231 if (is->is_p != IPPROTO_TCP)
233 if ((is->is_state[0] != TCPS_ESTABLISHED) ||
234 (is->is_state[1] != TCPS_ESTABLISHED))
240 if (is->is_p == IPPROTO_TCP)
243 ips_stats.iss_expire++;
245 ipstate_log(is, ISL_FLUSH);
254 * Asked to remove inactive entries, try again if first attempt
255 * failed. In this case, 86400 is half a day because the counter is
256 * activated every half second.
258 if ((which == 2) && (removed == 0)) {
259 try = 86400; /* half a day */
260 for (; (try < FIVE_DAYS) && (removed == 0); try += 86400) {
261 for (isp = &ips_list; (is = *isp); ) {
263 if ((is->is_p == IPPROTO_TCP) &&
264 ((is->is_state[0] == TCPS_ESTABLISHED) ||
265 (is->is_state[1] == TCPS_ESTABLISHED)) &&
266 (is->is_age < try)) {
269 } else if ((is->is_p != IPPROTO_TCP) &&
271 ips_stats.iss_expire++;
276 ipstate_log(is, ISL_FLUSH);
291 static int fr_state_remove(data)
298 error = IRCOPYPTR(data, (caddr_t)&st, sizeof(st));
302 WRITE_ENTER(&ipf_state);
303 for (sp = ips_list; sp; sp = sp->is_next)
304 if ((sp->is_p == st.is_p) && (sp->is_v == st.is_v) &&
305 !bcmp((char *)&sp->is_src, (char *)&st.is_src,
306 sizeof(st.is_src)) &&
307 !bcmp((char *)&sp->is_dst, (char *)&st.is_dst,
308 sizeof(st.is_dst)) &&
309 !bcmp((char *)&sp->is_ps, (char *)&st.is_ps,
312 ipstate_log(sp, ISL_REMOVE);
315 RWLOCK_EXIT(&ipf_state);
318 RWLOCK_EXIT(&ipf_state);
323 int fr_state_ioctl(data, cmd, mode)
325 #if defined(__NetBSD__) || defined(__OpenBSD__)
332 int arg, ret, error = 0;
337 error = fr_state_remove(data);
340 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
343 if (arg == 0 || arg == 1) {
344 WRITE_ENTER(&ipf_state);
345 ret = fr_state_flush(arg, 4);
346 RWLOCK_EXIT(&ipf_state);
347 error = IWCOPY((caddr_t)&ret, data, sizeof(ret));
353 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
356 if (arg == 0 || arg == 1) {
357 WRITE_ENTER(&ipf_state);
358 ret = fr_state_flush(arg, 6);
359 RWLOCK_EXIT(&ipf_state);
360 error = IWCOPY((caddr_t)&ret, data, sizeof(ret));
367 if (!(mode & FWRITE))
372 tmp = ipflog_clear(IPL_LOGSTATE);
373 IWCOPY((char *)&tmp, data, sizeof(tmp));
378 error = IWCOPYPTR((caddr_t)fr_statetstats(), data,
383 arg = (int)iplused[IPL_LOGSTATE];
384 error = IWCOPY((caddr_t)&arg, (caddr_t)data, sizeof(arg));
388 error = fr_lock(data, &fr_state_lock);
391 if (!fr_state_lock) {
395 error = fr_stputent(data);
398 if (!fr_state_lock) {
402 error = fr_stgetent(data);
413 * Copy out state information from the kernel to a user space process.
415 int fr_stgetent(data)
418 register ipstate_t *is, *isn;
422 error = IRCOPYPTR(data, (caddr_t)&ips, sizeof(ips));
430 if (ips.ips_next == NULL)
436 * Make sure the pointer we're copying from exists in the
437 * current list of entries. Security precaution to prevent
438 * copying of random kernel data.
440 for (is = ips_list; is; is = is->is_next)
446 ips.ips_next = isn->is_next;
447 bcopy((char *)isn, (char *)&ips.ips_is, sizeof(ips.ips_is));
449 bcopy((char *)isn->is_rule, (char *)&ips.ips_fr,
451 error = IWCOPYPTR((caddr_t)&ips, data, sizeof(ips));
458 int fr_stputent(data)
461 register ipstate_t *is, *isn;
467 error = IRCOPYPTR(data, (caddr_t)&ips, sizeof(ips));
471 KMALLOC(isn, ipstate_t *);
475 bcopy((char *)&ips.ips_is, (char *)isn, sizeof(*isn));
478 if (isn->is_flags & FI_NEWFR) {
479 KMALLOC(fr, frentry_t *);
484 bcopy((char *)&ips.ips_fr, (char *)fr, sizeof(*fr));
485 out = fr->fr_flags & FR_OUTQUE ? 1 : 0;
487 ips.ips_is.is_rule = fr;
490 * Look up all the interface names in the rule.
492 for (i = 0; i < 4; i++) {
493 name = fr->fr_ifnames[i];
494 if ((name[1] == '\0') &&
495 ((name[0] == '-') || (name[0] == '*'))) {
496 fr->fr_ifas[i] = NULL;
497 } else if (*name != '\0') {
498 fr->fr_ifas[i] = GETUNIT(name,
500 if (fr->fr_ifas[i] == NULL)
501 fr->fr_ifas[i] = (void *)-1;
503 strncpy(isn->is_ifname[i],
504 IFNAME(fr->fr_ifas[i]),
508 isn->is_ifp[out] = fr->fr_ifas[i];
512 * send a copy back to userland of what we ended up
513 * to allow for verification.
515 error = IWCOPYPTR((caddr_t)&ips, data, sizeof(ips));
522 for (is = ips_list; is; is = is->is_next)
523 if (is->is_rule == fr)
537 * Insert a state table entry manually.
540 register ipstate_t *is;
542 register u_int hv = is->is_hv;
546 MUTEX_INIT(&is->is_lock, "ipf state entry", NULL);
549 * Look up all the interface names in the state entry.
551 for (i = 0; i < 4; i++) {
552 name = is->is_ifname[i];
553 if ((name[1] == '\0') &&
554 ((name[0] == '-') || (name[0] == '*'))) {
555 is->is_ifp[0] = NULL;
556 } else if (*name != '\0') {
557 is->is_ifp[i] = GETUNIT(name, is->is_v);
558 if (is->is_ifp[i] == NULL)
559 is->is_ifp[i] = (void *)-1;
565 * add into list table.
568 ips_list->is_pnext = &is->is_next;
569 is->is_pnext = &ips_list;
570 is->is_next = ips_list;
573 ips_table[hv]->is_phnext = &is->is_hnext;
575 ips_stats.iss_inuse++;
576 is->is_phnext = ips_table + hv;
577 is->is_hnext = ips_table[hv];
584 * Create a new ipstate structure and hang it off the hash table.
586 ipstate_t *fr_addstate(ip, fin, stsave, flags)
592 register tcphdr_t *tcp = NULL;
593 register ipstate_t *is;
601 if (fr_state_lock || (fin->fin_off != 0) || (fin->fin_fl & FI_SHORT) ||
602 (fin->fin_misc & FM_BADSTATE))
604 if (ips_num == fr_statemax) {
606 fr_state_doflush = 1;
611 bzero((char *)is, sizeof(*is));
614 * Copy and calculate...
616 hv = (is->is_p = fin->fin_fi.fi_p);
617 is->is_src = fin->fin_fi.fi_src;
619 is->is_dst = fin->fin_fi.fi_dst;
622 if (fin->fin_v == 6) {
623 if ((is->is_p == IPPROTO_ICMPV6) &&
624 IN6_IS_ADDR_MULTICAST(&is->is_dst.in6)) {
626 * So you can do keep state with neighbour discovery.
631 hv += is->is_dst.i6[1];
632 hv += is->is_dst.i6[2];
633 hv += is->is_dst.i6[3];
635 hv += is->is_src.i6[1];
636 hv += is->is_src.i6[2];
637 hv += is->is_src.i6[3];
646 case IPPROTO_ICMPV6 :
647 ic = (struct icmp *)fin->fin_dp;
648 if ((ic->icmp_type & ICMP6_INFOMSG_MASK) == 0)
651 switch (ic->icmp_type)
653 case ICMP6_ECHO_REQUEST :
654 is->is_icmp.ics_type = ic->icmp_type;
655 hv += (is->is_icmp.ics_id = ic->icmp_id);
656 hv += (is->is_icmp.ics_seq = ic->icmp_seq);
658 case ICMP6_MEMBERSHIP_QUERY :
659 case ND_ROUTER_SOLICIT :
660 case ND_NEIGHBOR_SOLICIT :
661 case ICMP6_NI_QUERY :
662 is->is_icmp.ics_type = ic->icmp_type;
667 ATOMIC_INCL(ips_stats.iss_icmp);
668 is->is_age = fr_icmptimeout;
672 ic = (struct icmp *)fin->fin_dp;
674 switch (ic->icmp_type)
680 is->is_icmp.ics_type = ic->icmp_type;
681 hv += (is->is_icmp.ics_id = ic->icmp_id);
682 hv += (is->is_icmp.ics_seq = ic->icmp_seq);
687 ATOMIC_INCL(ips_stats.iss_icmp);
688 is->is_age = fr_icmptimeout;
691 tcp = (tcphdr_t *)fin->fin_dp;
693 if (tcp->th_flags & TH_RST)
696 * The endian of the ports doesn't matter, but the ack and
697 * sequence numbers do as we do mathematics on them later.
699 is->is_sport = htons(fin->fin_data[0]);
700 is->is_dport = htons(fin->fin_data[1]);
701 if ((flags & (FI_W_DPORT|FI_W_SPORT)) == 0) {
705 if ((flags & FI_IGNOREPKT) == 0) {
706 is->is_send = ntohl(tcp->th_seq) + fin->fin_dlen -
707 (off = (tcp->th_off << 2)) +
708 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
709 ((tcp->th_flags & TH_FIN) ? 1 : 0);
710 is->is_maxsend = is->is_send;
712 if ((tcp->th_flags & TH_SYN) &&
713 ((tcp->th_off << 2) >= (sizeof(*tcp) + 4))) {
714 ws = fr_tcpoptions(tcp);
721 is->is_maxswin = ntohs(tcp->th_win);
722 if (is->is_maxswin == 0)
725 if ((tcp->th_flags & TH_OPENING) == TH_SYN)
729 * If we're creating state for a starting connection, start the
730 * timer on it as we'll never see an error if it fails to
733 ATOMIC_INCL(ips_stats.iss_tcp);
737 tcp = (tcphdr_t *)fin->fin_dp;
739 is->is_sport = htons(fin->fin_data[0]);
740 is->is_dport = htons(fin->fin_data[1]);
741 if ((flags & (FI_W_DPORT|FI_W_SPORT)) == 0) {
745 ATOMIC_INCL(ips_stats.iss_udp);
746 is->is_age = fr_udptimeout;
749 is->is_age = fr_udptimeout;
753 KMALLOC(is, ipstate_t *);
755 ATOMIC_INCL(ips_stats.iss_nomem);
758 bcopy((char *)&ips, (char *)is, sizeof(*is));
761 is->is_rule = fin->fin_fr;
762 if (is->is_rule != NULL) {
763 is->is_group = is->is_rule->fr_group;
764 ATOMIC_INC32(is->is_rule->fr_ref);
765 pass = is->is_rule->fr_flags;
766 is->is_frage[0] = is->is_rule->fr_age[0];
767 is->is_frage[1] = is->is_rule->fr_age[1];
768 if (is->is_frage[0] != 0)
769 is->is_age = is->is_frage[0];
771 is->is_ifp[(out << 1) + 1] = is->is_rule->fr_ifas[1];
772 is->is_ifp[(1 - out) << 1] = is->is_rule->fr_ifas[2];
773 is->is_ifp[((1 - out) << 1) + 1] = is->is_rule->fr_ifas[3];
775 if (((ifp = is->is_rule->fr_ifas[1]) != NULL) &&
777 strncpy(is->is_ifname[(out << 1) + 1],
778 IFNAME(ifp), IFNAMSIZ);
779 if (((ifp = is->is_rule->fr_ifas[2]) != NULL) &&
781 strncpy(is->is_ifname[(1 - out) << 1],
782 IFNAME(ifp), IFNAMSIZ);
783 if (((ifp = is->is_rule->fr_ifas[3]) != NULL) &&
785 strncpy(is->is_ifname[((1 - out) << 1) + 1],
786 IFNAME(ifp), IFNAMSIZ);
790 is->is_ifp[out << 1] = fin->fin_ifp;
791 strncpy(is->is_ifname[out << 1], IFNAME(fin->fin_ifp), IFNAMSIZ);
793 WRITE_ENTER(&ipf_state);
796 if ((flags & FI_IGNOREPKT) == 0) {
798 is->is_bytes = fin->fin_dlen + fin->fin_hlen;
801 * We want to check everything that is a property of this packet,
802 * but we don't (automatically) care about it's fragment status as
805 is->is_v = fin->fin_v;
806 is->is_rulen = fin->fin_rule;
807 is->is_opt = fin->fin_fi.fi_optmsk;
808 is->is_optmsk = 0xffffffff;
809 is->is_sec = fin->fin_fi.fi_secmsk;
810 is->is_secmsk = 0xffff;
811 is->is_auth = fin->fin_fi.fi_auth;
812 is->is_authmsk = 0xffff;
813 is->is_flags = fin->fin_fl & FI_CMP;
814 is->is_flags |= FI_CMP << 4;
815 is->is_flags |= flags & (FI_WILDP|FI_WILDA);
816 if (flags & (FI_WILDP|FI_WILDA))
819 if (pass & FR_LOGFIRST)
820 is->is_pass &= ~(FR_LOGFIRST|FR_LOG);
823 if (is->is_p == IPPROTO_TCP) {
824 fr_tcp_age(&is->is_age, is->is_state, fin,
825 0, is->is_fsm); /* 0 = packet from the source */
828 ipstate_log(is, ISL_NEW);
830 RWLOCK_EXIT(&ipf_state);
831 fin->fin_rev = IP6NEQ(is->is_dst, fin->fin_fi.fi_dst);
832 if ((fin->fin_fl & FI_FRAG) && (pass & FR_KEEPFRAG))
833 ipfr_newfrag(ip, fin);
838 static int fr_tcpoptions(tcp)
844 opt = (u_char *) (tcp + 1);
845 last = ((u_char *)tcp) + (tcp->th_off << 2);
847 /* If we don't find wscale here, we need to clear it */
850 /* Termination condition picked such that opt[0 .. 2] exist */
851 while ((opt < last - 2) && (*opt != TCPOPT_EOL)) {
857 /* Proper length ? */
866 /* Unknown options must be two bytes+ */
880 * check to see if a packet with TCP headers fits within the TCP window.
881 * change timeout depending on whether new packet is a SYN-ACK returning for a
882 * SYN or a RST or FIN which indicate time to close up shop.
884 int fr_tcpstate(is, fin, ip, tcp)
885 register ipstate_t *is;
890 register tcp_seq seq, ack, end;
891 register int ackskew;
892 tcpdata_t *fdata, *tdata;
899 * Find difference between last checked packet and this packet.
901 source = IP6EQ(fin->fin_fi.fi_src, is->is_src);
902 if (source && (ntohs(is->is_sport) != fin->fin_data[0]))
904 fdata = &is->is_tcp.ts_data[!source];
905 tdata = &is->is_tcp.ts_data[source];
906 off = tcp->th_off << 2;
907 seq = ntohl(tcp->th_seq);
908 ack = ntohl(tcp->th_ack);
909 win = ntohs(tcp->th_win);
910 end = seq + fin->fin_dlen - off +
911 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
912 ((tcp->th_flags & TH_FIN) ? 1 : 0);
915 if ((tcp->th_flags & TH_SYN) && (off >= sizeof(*tcp) + 4))
916 wscale = fr_tcpoptions(tcp);
920 MUTEX_ENTER(&is->is_lock);
923 fdata->td_wscale = wscale;
924 else if (wscale == -2)
925 fdata->td_wscale = tdata->td_wscale = 0;
926 win <<= fdata->td_wscale;
928 if ((fdata->td_end == 0) &&
929 (!is->is_fsm || ((tcp->th_flags & TH_OPENING) == TH_OPENING))) {
931 * Must be a (outgoing) SYN-ACK in reply to a SYN.
934 fdata->td_maxwin = 1;
935 fdata->td_maxend = end + win;
940 if (!(tcp->th_flags & TH_ACK)) { /* Pretend an ack was sent */
942 } else if (((tcp->th_flags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) &&
944 /* gross hack to get around certain broken tcp stacks */
949 seq = end = fdata->td_end;
951 maxwin = tdata->td_maxwin;
952 ackskew = tdata->td_end - ack;
954 #define SEQ_GE(a,b) ((int)((a) - (b)) >= 0)
955 #define SEQ_GT(a,b) ((int)((a) - (b)) > 0)
956 if ((SEQ_GE(fdata->td_maxend, end)) &&
957 (SEQ_GE(seq, fdata->td_end - maxwin)) &&
958 /* XXX what about big packets */
959 #define MAXACKWINDOW 66000
960 (ackskew >= -MAXACKWINDOW) &&
961 (ackskew <= MAXACKWINDOW)) {
962 /* if ackskew < 0 then this should be due to fragented
963 * packets. There is no way to know the length of the
964 * total packet in advance.
965 * We do know the total length from the fragment cache though.
966 * Note however that there might be more sessions with
967 * exactly the same source and destination paramters in the
968 * state cache (and source and destination is the only stuff
969 * that is saved in the fragment cache). Note further that
970 * some TCP connections in the state cache are hashed with
971 * sport and dport as well which makes it not worthwhile to
973 * Thus, when ackskew is negative but still seems to belong
974 * to this session, we bump up the destinations end value.
977 * Nearing end of connection, start timeout.
979 /* source ? 0 : 1 -> !source */
980 if (fr_tcp_age(&is->is_age, is->is_state, fin, !source,
981 (int)is->is_fsm) == 0) {
985 /* update max window seen */
986 if (fdata->td_maxwin < win)
987 fdata->td_maxwin = win;
988 if (SEQ_GT(end, fdata->td_end))
990 if (SEQ_GE(ack + win, tdata->td_maxend)) {
991 tdata->td_maxend = ack + win;
996 ATOMIC_INCL(ips_stats.iss_hits);
1000 MUTEX_EXIT(&is->is_lock);
1001 if ((ret == 0) && ((tcp->th_flags & TH_OPENING) != TH_SYN))
1002 fin->fin_misc |= FM_BADSTATE;
1008 * Match a state table entry against an IP packet.
1010 static int fr_matchsrcdst(is, src, dst, fin, tcp)
1012 union i6addr src, dst;
1016 int ret = 0, rev, out, flags, idx;
1020 rev = IP6NEQ(is->is_dst, dst);
1023 flags = is->is_flags & (FI_WILDA|FI_WILDP);
1028 flags = is->is_flags;
1032 if (!(flags & FI_W_SPORT) && (sp != is->is_sport))
1034 else if (!(flags & FI_W_DPORT) && (dp != is->is_dport))
1039 idx = (out << 1) + rev;
1041 if ((is->is_ifp[idx] == NULL &&
1042 (*is->is_ifname[idx] == '\0' || *is->is_ifname[idx] == '*')) ||
1043 is->is_ifp[idx] == ifp)
1051 if ((IP6EQ(is->is_dst, dst) || (flags & FI_W_DADDR)) &&
1052 (IP6EQ(is->is_src, src) || (flags & FI_W_SADDR)) &&
1053 (!tcp || ((sp == is->is_sport || flags & FI_W_SPORT) &&
1054 (dp == is->is_dport || flags & FI_W_DPORT)))) {
1058 if ((IP6EQ(is->is_dst, src) || (flags & FI_W_DADDR)) &&
1059 (IP6EQ(is->is_src, dst) || (flags & FI_W_SADDR)) &&
1060 (!tcp || ((sp == is->is_dport || flags & FI_W_DPORT) &&
1061 (dp == is->is_sport || flags & FI_W_SPORT)))) {
1069 * Whether or not this should be here, is questionable, but the aim
1070 * is to get this out of the main line.
1073 flags = is->is_flags & (FI_CMP|(FI_CMP<<4));
1075 if (((fin->fin_fl & (flags >> 4)) != (flags & FI_CMP)) ||
1076 (fin->fin_fi.fi_optmsk != is->is_opt) ||
1077 (fin->fin_fi.fi_secmsk != is->is_sec) ||
1078 (fin->fin_fi.fi_auth != is->is_auth))
1081 flags = is->is_flags & (FI_WILDA|FI_WILDP);
1082 if ((flags & (FI_W_SADDR|FI_W_DADDR))) {
1083 if ((flags & FI_W_SADDR) != 0) {
1085 is->is_src = fin->fin_fi.fi_src;
1087 is->is_src = fin->fin_fi.fi_dst;
1089 } else if ((flags & FI_W_DADDR) != 0) {
1091 is->is_dst = fin->fin_fi.fi_dst;
1093 is->is_dst = fin->fin_fi.fi_src;
1096 is->is_flags &= ~(FI_W_SADDR|FI_W_DADDR);
1097 if ((is->is_flags & (FI_WILDA|FI_WILDP)) == 0)
1101 if ((flags & (FI_W_SPORT|FI_W_DPORT))) {
1102 if ((flags & FI_W_SPORT) != 0) {
1105 is->is_send = htonl(tcp->th_seq);
1108 is->is_send = htonl(tcp->th_ack);
1110 is->is_maxsend = is->is_send + 1;
1111 } else if ((flags & FI_W_DPORT) != 0) {
1114 is->is_dend = htonl(tcp->th_ack);
1117 is->is_dend = htonl(tcp->th_seq);
1119 is->is_maxdend = is->is_dend + 1;
1121 is->is_flags &= ~(FI_W_SPORT|FI_W_DPORT);
1127 if (is->is_ifp[idx] == NULL &&
1128 (*is->is_ifname[idx] == '\0' || *is->is_ifname[idx] == '*'))
1132 is->is_ifp[ret] = ifp;
1133 strncpy(is->is_ifname[ret], IFNAME(ifp),
1134 sizeof(is->is_ifname[ret]));
1140 static int fr_matchicmpqueryreply(v, is, icmp, rev)
1147 * If we matched its type on the way in, then when going out
1148 * it will still be the same type.
1150 if ((!rev && (icmp->icmp_type == is->is_type)) ||
1151 (rev && (icmpreplytype4[is->is_type] == icmp->icmp_type))) {
1152 if (icmp->icmp_type != ICMP_ECHOREPLY)
1154 if ((icmp->icmp_id == is->is_icmp.ics_id) &&
1155 (icmp->icmp_seq == is->is_icmp.ics_seq))
1160 else if (is->is_v == 6) {
1161 if ((!rev && (icmp->icmp_type == is->is_type)) ||
1162 (rev && (icmpreplytype6[is->is_type] == icmp->icmp_type))) {
1163 if (icmp->icmp_type != ICMP6_ECHO_REPLY)
1165 if ((icmp->icmp_id == is->is_icmp.ics_id) &&
1166 (icmp->icmp_seq == is->is_icmp.ics_seq))
1174 static frentry_t *fr_checkicmpmatchingstate(ip, fin)
1178 register ipstate_t *is, **isp;
1179 register u_short sport, dport;
1181 u_short savelen, ohlen;
1182 union i6addr dst, src;
1193 * Does it at least have the return (basic) IP header ?
1194 * Only a basic IP header (no options) should be with
1195 * an ICMP error header.
1197 if (((ip->ip_v != 4) || (ip->ip_hl != 5)) ||
1198 (fin->fin_plen < ICMPERR_MINPKTLEN))
1201 ic = (struct icmp *)fin->fin_dp;
1202 type = ic->icmp_type;
1204 * If it's not an error type, then return
1206 if ((type != ICMP_UNREACH) && (type != ICMP_SOURCEQUENCH) &&
1207 (type != ICMP_REDIRECT) && (type != ICMP_TIMXCEED) &&
1208 (type != ICMP_PARAMPROB))
1211 oip = (ip_t *)((char *)ic + ICMPERR_ICMPHLEN);
1212 ohlen = oip->ip_hl << 2;
1213 if (fin->fin_plen < ICMPERR_MAXPKTLEN + ohlen - sizeof(*oip))
1219 len = fin->fin_dlen - ICMPERR_ICMPHLEN;
1220 if ((len <= 0) || (ohlen > len))
1224 * Is the buffer big enough for all of it ? It's the size of the IP
1225 * header claimed in the encapsulated part which is of concern. It
1226 * may be too big to be in this buffer but not so big that it's
1227 * outside the ICMP packet, leading to TCP deref's causing problems.
1228 * This is possible because we don't know how big oip_hl is when we
1229 * do the pullup early in fr_check() and thus can't gaurantee it is
1238 if ((char *)oip + len > (char *)m->b_wptr)
1241 m = *(mb_t **)fin->fin_mp;
1242 if ((char *)oip + len > (char *)ip + m->m_len)
1249 * in the IPv4 case we must zero the i6addr union otherwise
1250 * the IP6EQ and IP6NEQ macros produce the wrong results because
1251 * of the 'junk' in the unused part of the union
1253 bzero((char *)&src, sizeof(src));
1254 bzero((char *)&dst, sizeof(dst));
1255 bzero((char *)&ofin, sizeof(ofin));
1256 ofin.fin_ifp = fin->fin_ifp;
1257 ofin.fin_out = !fin->fin_out;
1264 icmp = (icmphdr_t *)((char *)oip + ohlen);
1267 * a ICMP error can only be generated as a result of an
1268 * ICMP query, not as the response on an ICMP error
1270 * XXX theoretically ICMP_ECHOREP and the other reply's are
1271 * ICMP query's as well, but adding them here seems strange XXX
1273 if ((icmp->icmp_type != ICMP_ECHO) &&
1274 (icmp->icmp_type != ICMP_TSTAMP) &&
1275 (icmp->icmp_type != ICMP_IREQ) &&
1276 (icmp->icmp_type != ICMP_MASKREQ))
1280 * perform a lookup of the ICMP packet in the state table
1282 hv = (pr = oip->ip_p);
1283 src.in4 = oip->ip_src;
1284 hv += src.in4.s_addr;
1285 dst.in4 = oip->ip_dst;
1286 hv += dst.in4.s_addr;
1287 hv += icmp->icmp_id;
1288 hv += icmp->icmp_seq;
1291 savelen = oip->ip_len;
1293 fr_makefrip(ohlen, oip, &ofin);
1294 oip->ip_len = savelen;
1296 READ_ENTER(&ipf_state);
1297 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext)
1298 if ((is->is_p == pr) && (is->is_v == 4) &&
1299 fr_matchsrcdst(is, src, dst, &ofin, NULL) &&
1300 fr_matchicmpqueryreply(is->is_v, is, icmp, fin->fin_rev)) {
1301 ips_stats.iss_hits++;
1303 is->is_bytes += ip->ip_len;
1307 RWLOCK_EXIT(&ipf_state);
1312 if (fin->fin_plen < ICMPERR_MAXPKTLEN)
1319 tcp = (tcphdr_t *)((char *)oip + ohlen);
1320 dport = tcp->th_dport;
1321 sport = tcp->th_sport;
1323 hv = (pr = oip->ip_p);
1324 src.in4 = oip->ip_src;
1325 hv += src.in4.s_addr;
1326 dst.in4 = oip->ip_dst;
1327 hv += dst.in4.s_addr;
1332 * we make an fin entry to be able to feed it to
1333 * matchsrcdst note that not all fields are encessary
1334 * but this is the cleanest way. Note further we fill
1335 * in fin_mp such that if someone uses it we'll get
1336 * a kernel panic. fr_matchsrcdst does not use this.
1338 * watch out here, as ip is in host order and oip in network
1339 * order. Any change we make must be undone afterwards.
1341 savelen = oip->ip_len;
1343 fr_makefrip(ohlen, oip, &ofin);
1344 oip->ip_len = savelen;
1345 READ_ENTER(&ipf_state);
1346 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) {
1348 * Only allow this icmp though if the
1349 * encapsulated packet was allowed through the
1350 * other way around. Note that the minimal amount
1351 * of info present does not allow for checking against
1352 * tcp internals such as seq and ack numbers.
1354 if ((is->is_p == pr) && (is->is_v == 4) &&
1355 fr_matchsrcdst(is, src, dst, &ofin, tcp)) {
1357 ips_stats.iss_hits++;
1359 is->is_bytes += fin->fin_plen;
1361 * we deliberately do not touch the timeouts
1362 * for the accompanying state table entry.
1363 * It remains to be seen if that is correct. XXX
1368 RWLOCK_EXIT(&ipf_state);
1374 * Move a state hash table entry from its old location at is->is_hv to
1375 * its new location, indexed by hv % fr_statesize.
1377 static void fr_ipsmove(isp, is, hv)
1378 ipstate_t **isp, *is;
1385 * Remove the hash from the old location...
1388 is->is_hnext->is_phnext = isp;
1389 *isp = is->is_hnext;
1390 if (ips_table[hvm] == NULL)
1391 ips_stats.iss_inuse--;
1394 * ...and put the hash in the new one.
1396 hvm = hv % fr_statesize;
1398 isp = &ips_table[hvm];
1400 (*isp)->is_phnext = &is->is_hnext;
1402 ips_stats.iss_inuse++;
1403 is->is_phnext = isp;
1404 is->is_hnext = *isp;
1410 * Check if a packet has a registered state.
1412 frentry_t *fr_checkstate(ip, fin)
1416 union i6addr dst, src;
1417 register ipstate_t *is, **isp;
1419 u_int hv, hvm, hlen, tryagain, pass, v;
1425 if ((ips_list == NULL) || (fin->fin_off != 0) || fr_state_lock ||
1426 (fin->fin_fl & FI_SHORT))
1430 hlen = fin->fin_hlen;
1431 tcp = (tcphdr_t *)((char *)ip + hlen);
1432 ic = (struct icmp *)tcp;
1433 hv = (pr = fin->fin_fi.fi_p);
1434 src = fin->fin_fi.fi_src;
1435 dst = fin->fin_fi.fi_dst;
1436 hv += src.in4.s_addr;
1437 hv += dst.in4.s_addr;
1440 * Search the hash table for matching packet header info.
1441 * At the bottom of this switch statement, the following is expected:
1442 * is == NULL, no lock on ipf_state is held.
1443 * is != NULL, a lock on ipf_state is held.
1445 v = fin->fin_fi.fi_v;
1448 hv += fin->fin_fi.fi_src.i6[1];
1449 hv += fin->fin_fi.fi_src.i6[2];
1450 hv += fin->fin_fi.fi_src.i6[3];
1452 if ((fin->fin_p == IPPROTO_ICMPV6) &&
1453 IN6_IS_ADDR_MULTICAST(&fin->fin_fi.fi_dst.in6)) {
1454 hv -= dst.in4.s_addr;
1456 hv += fin->fin_fi.fi_dst.i6[1];
1457 hv += fin->fin_fi.fi_dst.i6[2];
1458 hv += fin->fin_fi.fi_dst.i6[3];
1466 case IPPROTO_ICMPV6 :
1470 if ((ic->icmp_type == ICMP6_ECHO_REQUEST) ||
1471 (ic->icmp_type == ICMP6_ECHO_REPLY)) {
1476 READ_ENTER(&ipf_state);
1478 hvm = hv % fr_statesize;
1479 for (isp = &ips_table[hvm]; (is = *isp); isp = &is->is_hnext)
1480 if ((is->is_p == pr) && (is->is_v == v) &&
1481 fr_matchsrcdst(is, src, dst, fin, NULL) &&
1482 fr_matchicmpqueryreply(v, is, ic, fin->fin_rev)) {
1484 if (is->is_frage[rev] != 0)
1485 is->is_age = is->is_frage[rev];
1487 is->is_age = fr_icmpacktimeout;
1489 is->is_age = fr_icmptimeout;
1494 if (tryagain && !(is->is_flags & FI_W_DADDR)) {
1495 hv += fin->fin_fi.fi_src.i6[0];
1496 hv += fin->fin_fi.fi_src.i6[1];
1497 hv += fin->fin_fi.fi_src.i6[2];
1498 hv += fin->fin_fi.fi_src.i6[3];
1499 fr_ipsmove(isp, is, hv);
1500 MUTEX_DOWNGRADE(&ipf_state);
1504 RWLOCK_EXIT(&ipf_state);
1507 * No matching icmp state entry. Perhaps this is a
1508 * response to another state entry.
1510 if ((ips_wild != 0) && (v == 6) && (tryagain == 0) &&
1511 !IN6_IS_ADDR_MULTICAST(&fin->fin_fi.fi_src.in6)) {
1512 hv -= fin->fin_fi.fi_src.i6[0];
1513 hv -= fin->fin_fi.fi_src.i6[1];
1514 hv -= fin->fin_fi.fi_src.i6[2];
1515 hv -= fin->fin_fi.fi_src.i6[3];
1517 WRITE_ENTER(&ipf_state);
1521 fr = fr_checkicmp6matchingstate((ip6_t *)ip, fin);
1532 hvm = hv % fr_statesize;
1533 READ_ENTER(&ipf_state);
1534 for (isp = &ips_table[hvm]; (is = *isp); isp = &is->is_hnext)
1535 if ((is->is_p == pr) && (is->is_v == v) &&
1536 fr_matchsrcdst(is, src, dst, fin, NULL) &&
1537 fr_matchicmpqueryreply(v, is, ic, fin->fin_rev)) {
1539 if (is->is_frage[rev] != 0)
1540 is->is_age = is->is_frage[rev];
1541 else if (fin->fin_rev)
1542 is->is_age = fr_icmpacktimeout;
1544 is->is_age = fr_icmptimeout;
1550 RWLOCK_EXIT(&ipf_state);
1552 * No matching icmp state entry. Perhaps this is a
1553 * response to another state entry.
1555 fr = fr_checkicmpmatchingstate(ip, fin);
1561 * Just plain ignore RST flag set with either FIN or SYN.
1563 if ((tcp->th_flags & TH_RST) &&
1564 ((tcp->th_flags & (TH_FIN|TH_SYN|TH_RST)) != TH_RST))
1568 register u_short dport, sport;
1570 dport = tcp->th_dport;
1571 sport = tcp->th_sport;
1575 READ_ENTER(&ipf_state);
1577 hvm = hv % fr_statesize;
1578 for (isp = &ips_table[hvm]; (is = *isp); isp = &is->is_hnext)
1579 if ((is->is_p == pr) && (is->is_v == v) &&
1580 fr_matchsrcdst(is, src, dst, fin, tcp)) {
1582 if ((pr == IPPROTO_TCP)) {
1583 if (!fr_tcpstate(is, fin, ip, tcp))
1585 } else if ((pr == IPPROTO_UDP)) {
1586 if (is->is_frage[rev] != 0)
1587 is->is_age = is->is_frage[rev];
1588 else if (fin->fin_rev)
1589 is->is_age = fr_udpacktimeout;
1591 is->is_age = fr_udptimeout;
1597 !(is->is_flags & (FI_WILDP|FI_WILDA))) {
1600 fr_ipsmove(isp, is, hv);
1601 MUTEX_DOWNGRADE(&ipf_state);
1606 RWLOCK_EXIT(&ipf_state);
1607 if (!tryagain && ips_wild) {
1611 WRITE_ENTER(&ipf_state);
1619 READ_ENTER(&ipf_state);
1620 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) {
1621 if ((is->is_p == pr) && (is->is_v == v) &&
1622 fr_matchsrcdst(is, src, dst, fin, NULL)) {
1624 if (is->is_frage[rev] != 0)
1625 is->is_age = is->is_frage[rev];
1627 is->is_age = fr_udptimeout;
1632 RWLOCK_EXIT(&ipf_state);
1638 ATOMIC_INCL(ips_stats.iss_miss);
1642 MUTEX_ENTER(&is->is_lock);
1643 is->is_bytes += fin->fin_plen;
1644 ips_stats.iss_hits++;
1646 MUTEX_EXIT(&is->is_lock);
1648 fin->fin_rule = is->is_rulen;
1650 fin->fin_group = fr->fr_group;
1651 fin->fin_icode = fr->fr_icode;
1655 RWLOCK_EXIT(&ipf_state);
1656 if ((fin->fin_fl & FI_FRAG) && (pass & FR_KEEPFRAG))
1657 ipfr_newfrag(ip, fin);
1659 if ((tcp != NULL) && (tcp->th_flags & TCP_CLOSE))
1667 * Sync. state entries. If interfaces come or go or just change position,
1670 void ip_statesync(ifp)
1673 register ipstate_t *is;
1676 WRITE_ENTER(&ipf_state);
1677 for (is = ips_list; is; is = is->is_next) {
1678 for (i = 0; i < 4; i++) {
1679 if (is->is_ifp[i] == ifp) {
1680 is->is_ifp[i] = GETUNIT(is->is_ifname[i],
1683 is->is_ifp[i] = (void *)-1;
1687 RWLOCK_EXIT(&ipf_state);
1692 * Must always be called with fr_ipfstate held as a write lock.
1694 static void fr_delstate(is)
1699 if (is->is_flags & (FI_WILDP|FI_WILDA))
1702 is->is_next->is_pnext = is->is_pnext;
1703 *is->is_pnext = is->is_next;
1705 is->is_hnext->is_phnext = is->is_phnext;
1706 *is->is_phnext = is->is_hnext;
1707 if (ips_table[is->is_hv] == NULL)
1708 ips_stats.iss_inuse--;
1715 if (fr->fr_ref == 0) {
1720 MUTEX_DESTROY(&is->is_lock);
1728 * Free memory in use by all state info. kept.
1730 void fr_stateunload()
1732 register ipstate_t *is;
1734 WRITE_ENTER(&ipf_state);
1735 while ((is = ips_list))
1737 ips_stats.iss_inuse = 0;
1739 RWLOCK_EXIT(&ipf_state);
1741 KFREES(ips_table, fr_statesize * sizeof(ipstate_t *));
1747 * Slowly expire held state for thingslike UDP and ICMP. Timeouts are set
1748 * in expectation of this being called twice per second.
1750 void fr_timeoutstate()
1752 register ipstate_t *is, **isp;
1753 #if defined(_KERNEL) && !SOLARIS
1758 WRITE_ENTER(&ipf_state);
1759 for (isp = &ips_list; (is = *isp); )
1760 if (is->is_age && !--is->is_age) {
1761 if (is->is_p == IPPROTO_TCP)
1762 ips_stats.iss_fin++;
1764 ips_stats.iss_expire++;
1766 ipstate_log(is, ISL_EXPIRE);
1771 if (fr_state_doflush) {
1772 (void) fr_state_flush(2, 0);
1773 fr_state_doflush = 0;
1775 RWLOCK_EXIT(&ipf_state);
1781 * Original idea freom Pradeep Krishnan for use primarily with NAT code.
1782 * (pkrishna@netcom.com)
1784 * Rewritten by Arjan de Vet <Arjan.deVet@adv.iae.nl>, 2000-07-29:
1786 * - (try to) base state transitions on real evidence only,
1787 * i.e. packets that are sent and have been received by ipfilter;
1788 * diagram 18.12 of TCP/IP volume 1 by W. Richard Stevens was used.
1790 * - deal with half-closed connections correctly;
1792 * - store the state of the source in state[0] such that ipfstat
1793 * displays the state as source/dest instead of dest/source; the calls
1794 * to fr_tcp_age have been changed accordingly.
1798 * state[0] = state of source (host that initiated connection)
1799 * state[1] = state of dest (host that accepted the connection)
1801 * dir == 0 : a packet from source to dest
1802 * dir == 1 : a packet from dest to source
1805 int fr_tcp_age(age, state, fin, dir, fsm)
1811 tcphdr_t *tcp = (tcphdr_t *)fin->fin_dp;
1812 u_char flags = tcp->th_flags;
1816 ostate = state[1 - dir];
1818 dlen = fin->fin_plen - fin->fin_hlen - (tcp->th_off << 2);
1820 if (flags & TH_RST) {
1821 if (!(tcp->th_flags & TH_PUSH) && !dlen) {
1822 *age = fr_tcpclosed;
1823 state[dir] = TCPS_CLOSED;
1825 *age = fr_tcpclosewait;
1826 state[dir] = TCPS_CLOSE_WAIT;
1835 case TCPS_CLOSED: /* 0 */
1836 if ((flags & TH_OPENING) == TH_OPENING) {
1838 * 'dir' received an S and sends SA in response,
1839 * CLOSED -> SYN_RECEIVED
1841 state[dir] = TCPS_SYN_RECEIVED;
1842 newage = fr_tcptimeout;
1843 } else if ((flags & TH_OPENING) == TH_SYN) {
1844 /* 'dir' sent S, CLOSED -> SYN_SENT */
1845 state[dir] = TCPS_SYN_SENT;
1846 newage = fr_tcptimeout;
1850 * It is apparently possible that a hosts sends two syncs
1851 * before the remote party is able to respond with a SA. In
1852 * such a case the remote server sometimes ACK's the second
1853 * sync, and then responds with a SA. The following code
1854 * is used to prevent this ack from being blocked.
1856 * We do not reset the timeout here to fr_tcptimeout because
1857 * a connection connect timeout does not renew after every
1858 * packet that is sent. We need to set newage to something
1859 * to indicate the packet has passed the check for its flags
1860 * being valid in the TCP FSM.
1862 else if ((ostate == TCPS_SYN_SENT) &&
1863 ((flags & (TH_FIN|TH_SYN|TH_RST|TH_ACK)) == TH_ACK)) {
1868 * The next piece of code makes it possible to get
1869 * already established connections into the state table
1870 * after a restart or reload of the filter rules; this
1871 * does not work when a strict 'flags S keep state' is
1872 * used for tcp connections of course, however, use a
1873 * lower time-out so the state disappears quickly if
1874 * the other side does not pick it up.
1877 (flags & (TH_FIN|TH_SYN|TH_RST|TH_ACK)) == TH_ACK) {
1878 /* we saw an A, guess 'dir' is in ESTABLISHED mode */
1879 if (ostate == TCPS_CLOSED) {
1880 state[dir] = TCPS_ESTABLISHED;
1881 newage = fr_tcptimeout;
1882 } else if (ostate == TCPS_ESTABLISHED) {
1883 state[dir] = TCPS_ESTABLISHED;
1884 newage = fr_tcpidletimeout;
1888 * TODO: besides regular ACK packets we can have other
1889 * packets as well; it is yet to be determined how we
1890 * should initialize the states in those cases
1894 case TCPS_LISTEN: /* 1 */
1898 case TCPS_SYN_SENT: /* 2 */
1899 if ((flags & ~(TH_ECN|TH_CWR)) == TH_SYN) {
1901 * A retransmitted SYN packet. We do not reset the
1902 * timeout here to fr_tcptimeout because a connection
1903 * connect timeout does not renew after every packet
1904 * that is sent. We need to set newage to something
1905 * to indicate the packet has passed the check for its
1906 * flags being valid in the TCP FSM.
1909 } else if ((flags & (TH_SYN|TH_FIN|TH_ACK)) == TH_ACK) {
1911 * We see an A from 'dir' which is in SYN_SENT
1912 * state: 'dir' sent an A in response to an SA
1913 * which it received, SYN_SENT -> ESTABLISHED
1915 state[dir] = TCPS_ESTABLISHED;
1916 newage = fr_tcpidletimeout;
1917 } else if (flags & TH_FIN) {
1919 * We see an F from 'dir' which is in SYN_SENT
1920 * state and wants to close its side of the
1921 * connection; SYN_SENT -> FIN_WAIT_1
1923 state[dir] = TCPS_FIN_WAIT_1;
1924 newage = fr_tcpidletimeout; /* or fr_tcptimeout? */
1925 } else if ((flags & TH_OPENING) == TH_OPENING) {
1927 * We see an SA from 'dir' which is already in
1928 * SYN_SENT state, this means we have a
1929 * simultaneous open; SYN_SENT -> SYN_RECEIVED
1931 state[dir] = TCPS_SYN_RECEIVED;
1932 newage = fr_tcptimeout;
1936 case TCPS_SYN_RECEIVED: /* 3 */
1937 if ((flags & (TH_SYN|TH_FIN|TH_ACK)) == TH_ACK) {
1939 * We see an A from 'dir' which was in SYN_RECEIVED
1940 * state so it must now be in established state,
1941 * SYN_RECEIVED -> ESTABLISHED
1943 state[dir] = TCPS_ESTABLISHED;
1944 newage = fr_tcpidletimeout;
1945 } else if ((flags & ~(TH_ECN|TH_CWR)) == TH_OPENING) {
1947 * We see an SA from 'dir' which is already in
1948 * SYN_RECEIVED state.
1950 newage = fr_tcptimeout;
1951 } else if (flags & TH_FIN) {
1953 * We see an F from 'dir' which is in SYN_RECEIVED
1954 * state and wants to close its side of the connection;
1955 * SYN_RECEIVED -> FIN_WAIT_1
1957 state[dir] = TCPS_FIN_WAIT_1;
1958 newage = fr_tcpidletimeout;
1962 case TCPS_ESTABLISHED: /* 4 */
1963 if (flags & TH_FIN) {
1965 * 'dir' closed its side of the connection; this
1966 * gives us a half-closed connection;
1967 * ESTABLISHED -> FIN_WAIT_1
1969 state[dir] = TCPS_FIN_WAIT_1;
1970 newage = fr_tcphalfclosed;
1971 } else if (flags & TH_ACK) {
1972 /* an ACK, should we exclude other flags here? */
1973 if (ostate == TCPS_FIN_WAIT_1) {
1975 * We know the other side did an active close,
1976 * so we are ACKing the recvd FIN packet (does
1977 * the window matching code guarantee this?)
1978 * and go into CLOSE_WAIT state; this gives us
1979 * a half-closed connection
1981 state[dir] = TCPS_CLOSE_WAIT;
1982 newage = fr_tcphalfclosed;
1983 } else if (ostate < TCPS_CLOSE_WAIT)
1985 * Still a fully established connection,
1988 newage = fr_tcpidletimeout;
1992 case TCPS_CLOSE_WAIT: /* 5 */
1993 if (flags & TH_FIN) {
1995 * Application closed and 'dir' sent a FIN, we're now
1996 * going into LAST_ACK state
1998 newage = fr_tcplastack;
1999 state[dir] = TCPS_LAST_ACK;
2002 * We remain in CLOSE_WAIT because the other side has
2003 * closed already and we did not close our side yet;
2006 newage = fr_tcphalfclosed;
2010 case TCPS_FIN_WAIT_1: /* 6 */
2011 if ((flags & TH_ACK) && ostate > TCPS_CLOSE_WAIT) {
2013 * If the other side is not active anymore it has sent
2014 * us a FIN packet that we are ack'ing now with an ACK;
2015 * this means both sides have now closed the connection
2016 * and we go into TIME_WAIT
2019 * XXX: how do we know we really are ACKing the FIN
2020 * packet here? does the window code guarantee that?
2022 state[dir] = TCPS_TIME_WAIT;
2023 newage = fr_tcptimeout;
2026 * We closed our side of the connection already but the
2027 * other side is still active (ESTABLISHED/CLOSE_WAIT);
2028 * continue with this half-closed connection
2030 newage = fr_tcphalfclosed;
2033 case TCPS_CLOSING: /* 7 */
2037 case TCPS_LAST_ACK: /* 8 */
2038 if (flags & TH_ACK) {
2039 if ((flags & TH_PUSH) || dlen)
2041 * There is still data to be delivered, reset
2044 newage = fr_tcplastack;
2049 * We cannot detect when we go out of LAST_ACK state to CLOSED
2050 * because that is based on the reception of ACK packets;
2051 * ipfilter can only detect that a packet has been sent by a
2056 case TCPS_FIN_WAIT_2: /* 9 */
2060 case TCPS_TIME_WAIT: /* 10 */
2061 newage = fr_tcptimeout; /* default 4 mins */
2062 /* we're in 2MSL timeout now */
2075 void ipstate_log(is, type)
2084 ipsl.isl_type = type;
2085 ipsl.isl_pkts = is->is_pkts;
2086 ipsl.isl_bytes = is->is_bytes;
2087 ipsl.isl_src = is->is_src;
2088 ipsl.isl_dst = is->is_dst;
2089 ipsl.isl_p = is->is_p;
2090 ipsl.isl_v = is->is_v;
2091 ipsl.isl_flags = is->is_flags;
2092 ipsl.isl_rulen = is->is_rulen;
2093 ipsl.isl_group = is->is_group;
2094 if (ipsl.isl_p == IPPROTO_TCP || ipsl.isl_p == IPPROTO_UDP) {
2095 ipsl.isl_sport = is->is_sport;
2096 ipsl.isl_dport = is->is_dport;
2097 if (ipsl.isl_p == IPPROTO_TCP) {
2098 ipsl.isl_state[0] = is->is_state[0];
2099 ipsl.isl_state[1] = is->is_state[1];
2101 } else if (ipsl.isl_p == IPPROTO_ICMP) {
2102 ipsl.isl_itype = is->is_icmp.ics_type;
2103 } else if (ipsl.isl_p == IPPROTO_ICMPV6) {
2104 ipsl.isl_itype = is->is_icmp.ics_type;
2106 ipsl.isl_ps.isl_filler[0] = 0;
2107 ipsl.isl_ps.isl_filler[1] = 0;
2110 sizes[0] = sizeof(ipsl);
2113 (void) ipllog(IPL_LOGSTATE, NULL, items, sizes, types, 1);
2119 frentry_t *fr_checkicmp6matchingstate(ip, fin)
2123 register ipstate_t *is, **isp;
2124 register u_short sport, dport;
2126 struct icmp6_hdr *ic, *oic;
2127 union i6addr dst, src;
2137 * Does it at least have the return (basic) IP header ?
2138 * Only a basic IP header (no options) should be with
2139 * an ICMP error header.
2141 if ((fin->fin_v != 6) || (fin->fin_plen < ICMP6ERR_MINPKTLEN))
2143 ic = (struct icmp6_hdr *)fin->fin_dp;
2144 type = ic->icmp6_type;
2146 * If it's not an error type, then return
2148 if ((type != ICMP6_DST_UNREACH) && (type != ICMP6_PACKET_TOO_BIG) &&
2149 (type != ICMP6_TIME_EXCEEDED) && (type != ICMP6_PARAM_PROB))
2152 oip = (ip6_t *)((char *)ic + ICMPERR_ICMPHLEN);
2153 if (fin->fin_plen < sizeof(*oip))
2156 if ((oip->ip6_nxt != IPPROTO_TCP) && (oip->ip6_nxt != IPPROTO_UDP) &&
2157 (oip->ip6_nxt != IPPROTO_ICMPV6))
2160 bzero((char *)&ofin, sizeof(ofin));
2161 ofin.fin_out = !fin->fin_out;
2162 ofin.fin_ifp = fin->fin_ifp;
2165 if (oip->ip6_nxt == IPPROTO_ICMPV6) {
2166 oic = (struct icmp6_hdr *)(oip + 1);
2168 * a ICMP error can only be generated as a result of an
2169 * ICMP query, not as the response on an ICMP error
2171 * XXX theoretically ICMP_ECHOREP and the other reply's are
2172 * ICMP query's as well, but adding them here seems strange XXX
2174 if (!(oic->icmp6_type & ICMP6_INFOMSG_MASK))
2178 * perform a lookup of the ICMP packet in the state table
2180 hv = (pr = oip->ip6_nxt);
2181 src.in6 = oip->ip6_src;
2182 hv += src.in4.s_addr;
2183 dst.in6 = oip->ip6_dst;
2184 hv += dst.in4.s_addr;
2185 hv += oic->icmp6_id;
2186 hv += oic->icmp6_seq;
2189 oip->ip6_plen = ntohs(oip->ip6_plen);
2190 fr_makefrip(sizeof(*oip), (ip_t *)oip, &ofin);
2191 oip->ip6_plen = htons(oip->ip6_plen);
2193 READ_ENTER(&ipf_state);
2194 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext)
2195 if ((is->is_p == pr) &&
2196 (oic->icmp6_id == is->is_icmp.ics_id) &&
2197 (oic->icmp6_seq == is->is_icmp.ics_seq) &&
2198 fr_matchsrcdst(is, src, dst, &ofin, NULL)) {
2200 * in the state table ICMP query's are stored
2201 * with the type of the corresponding ICMP
2202 * response. Correct here
2204 if (((is->is_type == ICMP6_ECHO_REPLY) &&
2205 (oic->icmp6_type == ICMP6_ECHO_REQUEST)) ||
2206 (is->is_type - 1 == oic->icmp6_type )) {
2207 ips_stats.iss_hits++;
2209 is->is_bytes += fin->fin_plen;
2213 RWLOCK_EXIT(&ipf_state);
2218 tcp = (tcphdr_t *)(oip + 1);
2219 dport = tcp->th_dport;
2220 sport = tcp->th_sport;
2222 hv = (pr = oip->ip6_nxt);
2223 src.in6 = oip->ip6_src;
2224 hv += src.in4.s_addr;
2228 dst.in6 = oip->ip6_dst;
2229 hv += dst.in4.s_addr;
2237 * we make an fin entry to be able to feed it to
2238 * matchsrcdst note that not all fields are encessary
2239 * but this is the cleanest way. Note further we fill
2240 * in fin_mp such that if someone uses it we'll get
2241 * a kernel panic. fr_matchsrcdst does not use this.
2243 * watch out here, as ip is in host order and oip in network
2244 * order. Any change we make must be undone afterwards.
2246 savelen = oip->ip6_plen;
2247 oip->ip6_plen = ip->ip6_plen - sizeof(*ip) - ICMPERR_ICMPHLEN;
2248 fr_makefrip(sizeof(*oip), (ip_t *)oip, &ofin);
2249 oip->ip6_plen = savelen;
2250 READ_ENTER(&ipf_state);
2251 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) {
2253 * Only allow this icmp though if the
2254 * encapsulated packet was allowed through the
2255 * other way around. Note that the minimal amount
2256 * of info present does not allow for checking against
2257 * tcp internals such as seq and ack numbers.
2259 if ((is->is_p == pr) && (is->is_v == 6) &&
2260 fr_matchsrcdst(is, src, dst, &ofin, tcp)) {
2262 ips_stats.iss_hits++;
2264 is->is_bytes += fin->fin_plen;
2266 * we deliberately do not touch the timeouts
2267 * for the accompanying state table entry.
2268 * It remains to be seen if that is correct. XXX
2270 RWLOCK_EXIT(&ipf_state);
2274 RWLOCK_EXIT(&ipf_state);