Home | History | Annotate | Line # | Download | only in npf
npf_state.c revision 1.2
      1  1.2  rmind /*	$NetBSD: npf_state.c,v 1.2 2010/12/18 01:07:25 rmind Exp $	*/
      2  1.1  rmind 
      3  1.1  rmind /*-
      4  1.1  rmind  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  1.1  rmind  * All rights reserved.
      6  1.1  rmind  *
      7  1.1  rmind  * This material is based upon work partially supported by The
      8  1.1  rmind  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      9  1.1  rmind  *
     10  1.1  rmind  * Redistribution and use in source and binary forms, with or without
     11  1.1  rmind  * modification, are permitted provided that the following conditions
     12  1.1  rmind  * are met:
     13  1.1  rmind  * 1. Redistributions of source code must retain the above copyright
     14  1.1  rmind  *    notice, this list of conditions and the following disclaimer.
     15  1.1  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  rmind  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  rmind  *    documentation and/or other materials provided with the distribution.
     18  1.1  rmind  *
     19  1.1  rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  rmind  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  rmind  */
     31  1.1  rmind 
     32  1.1  rmind /*
     33  1.1  rmind  * NPF state engine to track connections.
     34  1.1  rmind  */
     35  1.1  rmind 
     36  1.1  rmind #include <sys/cdefs.h>
     37  1.2  rmind __KERNEL_RCSID(0, "$NetBSD: npf_state.c,v 1.2 2010/12/18 01:07:25 rmind Exp $");
     38  1.1  rmind 
     39  1.1  rmind #include <sys/param.h>
     40  1.1  rmind #include <sys/systm.h>
     41  1.1  rmind 
     42  1.1  rmind #include <sys/mutex.h>
     43  1.1  rmind #include <netinet/in.h>
     44  1.1  rmind #include <netinet/tcp.h>
     45  1.1  rmind #include <netinet/tcp_seq.h>
     46  1.1  rmind 
     47  1.1  rmind #include "npf_impl.h"
     48  1.1  rmind 
     49  1.1  rmind #define	MAXACKWINDOW		66000
     50  1.1  rmind 
     51  1.1  rmind /* Session expiration table.  XXX revisit later */
     52  1.1  rmind static const u_int expire_table[ ] = {
     53  1.1  rmind 	[IPPROTO_TCP]		= 86400,	/* 24 hours */
     54  1.1  rmind 	[IPPROTO_UDP]		= 120,		/* 2 min */
     55  1.1  rmind 	[IPPROTO_ICMP]		= 30		/* 1 min */
     56  1.1  rmind };
     57  1.1  rmind 
     58  1.1  rmind static bool
     59  1.1  rmind npf_tcp_inwindow(const npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst,
     60  1.1  rmind     const bool forw)
     61  1.1  rmind {
     62  1.1  rmind 	const struct tcphdr *th = &npc->npc_l4.tcp;
     63  1.1  rmind 	const int tcpfl = th->th_flags;
     64  1.1  rmind 	npf_tcpstate_t *fstate, *tstate;
     65  1.1  rmind 	int tcpdlen, wscale, ackskew;
     66  1.1  rmind 	tcp_seq seq, ack, end;
     67  1.1  rmind 	uint32_t win;
     68  1.1  rmind 
     69  1.1  rmind 	KASSERT(npf_iscached(npc, NPC_TCP));
     70  1.1  rmind 	tcpdlen = npf_tcpsaw(__UNCONST(npc), &seq, &ack, &win);
     71  1.1  rmind 	end = seq + tcpdlen;
     72  1.1  rmind 	if (tcpfl & TH_SYN) {
     73  1.1  rmind 		end++;
     74  1.1  rmind 	}
     75  1.1  rmind 	if (tcpfl & TH_FIN) {
     76  1.1  rmind 		end++;
     77  1.1  rmind 	}
     78  1.1  rmind 
     79  1.1  rmind 	/*
     80  1.1  rmind 	 * Perform SEQ/ACK numbers check against boundaries.  Reference:
     81  1.1  rmind 	 *
     82  1.1  rmind 	 *	Rooij G., "Real stateful TCP packet filtering in IP Filter",
     83  1.1  rmind 	 *	10th USENIX Security Symposium invited talk, Aug. 2001.
     84  1.1  rmind 	 */
     85  1.1  rmind 
     86  1.1  rmind 	fstate = &nst->nst_tcpst[forw ? 0 : 1];
     87  1.1  rmind 	tstate = &nst->nst_tcpst[forw ? 1 : 0];
     88  1.1  rmind 	win = win ? (win << fstate->nst_wscale) : 1;
     89  1.1  rmind 
     90  1.1  rmind 	if (tcpfl == TH_SYN) {
     91  1.1  rmind 		/*
     92  1.1  rmind 		 * First SYN or re-transmission of SYN.  Initialize all
     93  1.1  rmind 		 * values.  State of other side will get set with a SYN-ACK
     94  1.1  rmind 		 * reply (see below).
     95  1.1  rmind 		 */
     96  1.1  rmind 		fstate->nst_seqend = end;
     97  1.1  rmind 		fstate->nst_ackend = end;
     98  1.1  rmind 		fstate->nst_maxwin = win;
     99  1.1  rmind 		tstate->nst_ackend = 0;
    100  1.1  rmind 		tstate->nst_ackend = 0;
    101  1.1  rmind 		tstate->nst_maxwin = 0;
    102  1.1  rmind 		/*
    103  1.1  rmind 		 * Handle TCP Window Scaling (RFC 1323).  Both sides may
    104  1.1  rmind 		 * send this option in their SYN packets.
    105  1.1  rmind 		 */
    106  1.1  rmind 		if (npf_fetch_tcpopts(npc, nbuf, NULL, &wscale)) {
    107  1.1  rmind 			fstate->nst_wscale = wscale;
    108  1.1  rmind 		} else {
    109  1.1  rmind 			fstate->nst_wscale = 0;
    110  1.1  rmind 		}
    111  1.1  rmind 		tstate->nst_wscale = 0;
    112  1.1  rmind 		/* Done. */
    113  1.1  rmind 		return true;
    114  1.1  rmind 	}
    115  1.1  rmind 	if (fstate->nst_seqend == 0) {
    116  1.1  rmind 		/*
    117  1.1  rmind 		 * Should be a SYN-ACK reply to SYN.  If SYN is not set,
    118  1.1  rmind 		 * then we are in the middle connection and lost tracking.
    119  1.1  rmind 		 */
    120  1.1  rmind 		fstate->nst_seqend = end;
    121  1.1  rmind 		fstate->nst_ackend = end + 1;
    122  1.1  rmind 		fstate->nst_maxwin = 1;
    123  1.1  rmind 
    124  1.1  rmind 		/* Handle TCP Window Scaling (must be ignored if no SYN). */
    125  1.1  rmind 		if (tcpfl & TH_SYN) {
    126  1.1  rmind 			fstate->nst_wscale =
    127  1.1  rmind 			    npf_fetch_tcpopts(npc, nbuf, NULL, &wscale) ?
    128  1.1  rmind 			    wscale : 0;
    129  1.1  rmind 		}
    130  1.1  rmind 	}
    131  1.1  rmind 	if ((tcpfl & TH_ACK) == 0) {
    132  1.1  rmind 		/* Pretend that an ACK was sent. */
    133  1.1  rmind 		ack = tstate->nst_seqend;
    134  1.1  rmind 	} else if ((tcpfl & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST) && ack == 0) {
    135  1.1  rmind 		/* Workaround for some TCP stacks. */
    136  1.1  rmind 		ack = tstate->nst_seqend;
    137  1.1  rmind 	}
    138  1.1  rmind 	if (seq == end) {
    139  1.1  rmind 		/* If packet contains no data - assume it is valid. */
    140  1.1  rmind 		end = fstate->nst_seqend;
    141  1.1  rmind 		seq = end;
    142  1.1  rmind 	}
    143  1.1  rmind 
    144  1.1  rmind 	/*
    145  1.1  rmind 	 * Determine whether the data is within previously noted window,
    146  1.1  rmind 	 * that is, upper boundary for valid data (I).
    147  1.1  rmind 	 */
    148  1.1  rmind 	if (!SEQ_GEQ(fstate->nst_ackend, end)) {
    149  1.2  rmind 		npf_stats_inc(NPF_STAT_INVALID_STATE_TCP1);
    150  1.1  rmind 		return false;
    151  1.1  rmind 	}
    152  1.1  rmind 	/* Lower boundary (II), which is no more than one window back. */
    153  1.1  rmind 	if (!SEQ_GEQ(seq, fstate->nst_seqend - tstate->nst_maxwin)) {
    154  1.2  rmind 		npf_stats_inc(NPF_STAT_INVALID_STATE_TCP2);
    155  1.1  rmind 		return false;
    156  1.1  rmind 	}
    157  1.1  rmind 	/*
    158  1.1  rmind 	 * Boundaries for valid acknowledgments (III, IV) - on predicted
    159  1.1  rmind 	 * window up or down, since packets may be fragmented.
    160  1.1  rmind 	 */
    161  1.1  rmind 	ackskew = tstate->nst_seqend - ack;
    162  1.1  rmind 	if (ackskew < -MAXACKWINDOW || ackskew > MAXACKWINDOW) {
    163  1.2  rmind 		npf_stats_inc(NPF_STAT_INVALID_STATE_TCP3);
    164  1.1  rmind 		return false;
    165  1.1  rmind 	}
    166  1.1  rmind 
    167  1.1  rmind 	/*
    168  1.2  rmind 	 * Packet is passed now.
    169  1.2  rmind 	 *
    170  1.1  rmind 	 * Negative ackskew might be due to fragmented packets.  Since the
    171  1.1  rmind 	 * total length of the packet is unknown - bump the boundary.
    172  1.1  rmind 	 */
    173  1.1  rmind 	if (ackskew < 0) {
    174  1.1  rmind 		tstate->nst_seqend = end;
    175  1.1  rmind 	}
    176  1.1  rmind 	/* Keep track of the maximum window seen. */
    177  1.1  rmind 	if (fstate->nst_maxwin < win) {
    178  1.1  rmind 		fstate->nst_maxwin = win;
    179  1.1  rmind 	}
    180  1.1  rmind 	if (SEQ_GT(end, fstate->nst_seqend)) {
    181  1.1  rmind 		fstate->nst_seqend = end;
    182  1.1  rmind 	}
    183  1.1  rmind 	/* Note the window for upper boundary. */
    184  1.1  rmind 	if (SEQ_GEQ(ack + win, tstate->nst_ackend)) {
    185  1.1  rmind 		tstate->nst_ackend = ack + win;
    186  1.1  rmind 	}
    187  1.1  rmind 	return true;
    188  1.1  rmind }
    189  1.1  rmind 
    190  1.1  rmind static inline bool
    191  1.1  rmind npf_state_tcp(const npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst,
    192  1.1  rmind     const bool forw)
    193  1.1  rmind {
    194  1.1  rmind 	const struct tcphdr *th = &npc->npc_l4.tcp;
    195  1.1  rmind 	const int tcpfl = th->th_flags;
    196  1.2  rmind 	int nstate = 0;
    197  1.1  rmind 
    198  1.1  rmind 	/*
    199  1.1  rmind 	 * Handle 3-way handshake (SYN -> SYN,ACK -> ACK).
    200  1.1  rmind 	 */
    201  1.1  rmind 	switch (nst->nst_state) {
    202  1.1  rmind 	case ST_ESTABLISHED:
    203  1.1  rmind 		/* Common case - connection established. */
    204  1.2  rmind 		if (__predict_false(tcpfl & (TH_FIN | TH_RST))) {
    205  1.2  rmind 			/* Handle connection closure (FIN or RST). */
    206  1.2  rmind 			nstate = ST_CLOSING;
    207  1.1  rmind 		}
    208  1.1  rmind 		break;
    209  1.1  rmind 	case ST_OPENING:
    210  1.1  rmind 		/* SYN has been sent, expecting SYN-ACK. */
    211  1.1  rmind 		if (tcpfl == (TH_SYN | TH_ACK) && !forw) {
    212  1.1  rmind 			/* Received backwards SYN-ACK. */
    213  1.2  rmind 			nstate = ST_ACKNOWLEDGE;
    214  1.1  rmind 		} else if (tcpfl == TH_SYN && forw) {
    215  1.1  rmind 			/* Re-transmission of SYN. */
    216  1.1  rmind 		} else {
    217  1.1  rmind 			return false;
    218  1.1  rmind 		}
    219  1.1  rmind 		break;
    220  1.1  rmind 	case ST_ACKNOWLEDGE:
    221  1.1  rmind 		/* SYN-ACK was seen, expecting ACK. */
    222  1.1  rmind 		if (tcpfl == TH_ACK && forw) {
    223  1.2  rmind 			nstate = ST_ESTABLISHED;
    224  1.1  rmind 		} else {
    225  1.1  rmind 			return false;
    226  1.1  rmind 		}
    227  1.1  rmind 		break;
    228  1.1  rmind 	case ST_CLOSING:
    229  1.1  rmind 		/* XXX TODO */
    230  1.1  rmind 		break;
    231  1.1  rmind 	default:
    232  1.1  rmind 		npf_state_dump(nst);
    233  1.1  rmind 		KASSERT(false);
    234  1.1  rmind 	}
    235  1.2  rmind #if 0
    236  1.2  rmind 	if (!npf_tcp_inwindow(npc, nbuf, nst, forw)) {
    237  1.2  rmind 		return false;
    238  1.2  rmind 	}
    239  1.2  rmind #endif
    240  1.2  rmind 	if (__predict_false(nstate)) {
    241  1.2  rmind 		nst->nst_state = nstate;
    242  1.2  rmind 	}
    243  1.2  rmind 	return true;
    244  1.1  rmind }
    245  1.1  rmind 
    246  1.1  rmind bool
    247  1.1  rmind npf_state_init(const npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst)
    248  1.1  rmind {
    249  1.1  rmind 	const int proto = npf_cache_ipproto(npc);
    250  1.1  rmind 
    251  1.1  rmind 	KASSERT(npf_iscached(npc, NPC_IP46 | NPC_LAYER4));
    252  1.2  rmind 
    253  1.2  rmind 	mutex_init(&nst->nst_lock, MUTEX_DEFAULT, IPL_SOFTNET);
    254  1.2  rmind 	nst->nst_state = ST_OPENING;
    255  1.2  rmind 
    256  1.1  rmind 	if (proto == IPPROTO_TCP) {
    257  1.1  rmind 		const struct tcphdr *th = &npc->npc_l4.tcp;
    258  1.1  rmind 		/* TCP case: must be SYN. */
    259  1.1  rmind 		KASSERT(npf_iscached(npc, NPC_TCP));
    260  1.1  rmind 		if (th->th_flags != TH_SYN) {
    261  1.2  rmind 			npf_stats_inc(NPF_STAT_INVALID_STATE);
    262  1.1  rmind 			return false;
    263  1.1  rmind 		}
    264  1.1  rmind 		/* Initial values for TCP window and sequence tracking. */
    265  1.1  rmind 		if (!npf_tcp_inwindow(npc, nbuf, nst, true)) {
    266  1.2  rmind 			npf_stats_inc(NPF_STAT_INVALID_STATE);
    267  1.1  rmind 			return false;
    268  1.1  rmind 		}
    269  1.1  rmind 	}
    270  1.1  rmind 	return true;
    271  1.1  rmind }
    272  1.1  rmind 
    273  1.1  rmind void
    274  1.1  rmind npf_state_destroy(npf_state_t *nst)
    275  1.1  rmind {
    276  1.1  rmind 
    277  1.1  rmind 	KASSERT(nst->nst_state != 0);
    278  1.1  rmind 	mutex_destroy(&nst->nst_lock);
    279  1.1  rmind }
    280  1.1  rmind 
    281  1.1  rmind bool
    282  1.1  rmind npf_state_inspect(const npf_cache_t *npc, nbuf_t *nbuf,
    283  1.1  rmind     npf_state_t *nst, const bool forw)
    284  1.1  rmind {
    285  1.1  rmind 	const int proto = npf_cache_ipproto(npc);
    286  1.1  rmind 	bool ret;
    287  1.1  rmind 
    288  1.1  rmind 	mutex_enter(&nst->nst_lock);
    289  1.1  rmind 	switch (proto) {
    290  1.1  rmind 	case IPPROTO_TCP:
    291  1.1  rmind 		/* Handle TCP. */
    292  1.1  rmind 		ret = npf_state_tcp(npc, nbuf, nst, forw);
    293  1.1  rmind 		break;
    294  1.1  rmind 	default:
    295  1.1  rmind 		/* Handle UDP or ICMP response for opening session. */
    296  1.1  rmind 		if (nst->nst_state == ST_OPENING && !forw) {
    297  1.1  rmind 			nst->nst_state = ST_ESTABLISHED;
    298  1.1  rmind 		}
    299  1.1  rmind 		ret = true;
    300  1.1  rmind 	}
    301  1.1  rmind 	mutex_exit(&nst->nst_lock);
    302  1.2  rmind 	if (__predict_false(!ret)) {
    303  1.2  rmind 		npf_stats_inc(NPF_STAT_INVALID_STATE);
    304  1.2  rmind 	}
    305  1.1  rmind 	return ret;
    306  1.1  rmind }
    307  1.1  rmind 
    308  1.1  rmind int
    309  1.1  rmind npf_state_etime(const npf_state_t *nst, const int proto)
    310  1.1  rmind {
    311  1.1  rmind 
    312  1.1  rmind 	if (nst->nst_state == ST_ESTABLISHED) {
    313  1.1  rmind 		return expire_table[proto];
    314  1.1  rmind 	}
    315  1.1  rmind 	return 10;	/* XXX TODO */
    316  1.1  rmind }
    317  1.1  rmind 
    318  1.1  rmind #if defined(DDB) || defined(_NPF_TESTING)
    319  1.1  rmind 
    320  1.1  rmind void
    321  1.1  rmind npf_state_dump(npf_state_t *nst)
    322  1.1  rmind {
    323  1.1  rmind 	npf_tcpstate_t *fst = &nst->nst_tcpst[0], *tst = &nst->nst_tcpst[1];
    324  1.1  rmind 
    325  1.1  rmind 	printf("\tstate (%p) %d:\n\t\t"
    326  1.1  rmind 	    "F { seqend %u ackend %u mwin %u wscale %u }\n\t\t"
    327  1.1  rmind 	    "T { seqend %u, ackend %u mwin %u wscale %u }\n",
    328  1.1  rmind 	    nst, nst->nst_state,
    329  1.1  rmind 	    fst->nst_seqend, fst->nst_ackend, fst->nst_maxwin, fst->nst_wscale,
    330  1.1  rmind 	    tst->nst_seqend, tst->nst_ackend, tst->nst_maxwin, tst->nst_wscale
    331  1.1  rmind 	);
    332  1.1  rmind }
    333  1.1  rmind 
    334  1.1  rmind #endif
    335