Home | History | Annotate | Line # | Download | only in libnpftest
npf_state_test.c revision 1.6.6.1
      1  1.6.6.1  pgoyette /*	$NetBSD: npf_state_test.c,v 1.6.6.1 2017/01/07 08:57:00 pgoyette Exp $	*/
      2      1.1     rmind 
      3      1.1     rmind /*
      4      1.1     rmind  * NPF state tracking test.
      5      1.1     rmind  *
      6      1.1     rmind  * Public Domain.
      7      1.1     rmind  */
      8      1.1     rmind 
      9  1.6.6.1  pgoyette #ifdef _KERNEL
     10      1.1     rmind #include <sys/types.h>
     11      1.1     rmind #include <sys/kmem.h>
     12  1.6.6.1  pgoyette #endif
     13      1.1     rmind 
     14      1.1     rmind #include "npf_impl.h"
     15      1.1     rmind #include "npf_test.h"
     16      1.1     rmind 
     17      1.1     rmind typedef struct {
     18      1.1     rmind 	int		tcpfl;		/* TCP flags. */
     19      1.1     rmind 	int		tlen;		/* TCP data length. */
     20      1.1     rmind 	uint32_t	seq;		/* SEQ number. */
     21      1.1     rmind 	uint32_t	ack;		/* ACK number. */
     22      1.1     rmind 	uint32_t	win;		/* TCP Window. */
     23      1.1     rmind 	int		flags;		/* Direction et al. */
     24      1.1     rmind } tcp_meta_t;
     25      1.1     rmind 
     26      1.1     rmind #define	S	TH_SYN
     27      1.1     rmind #define	A	TH_ACK
     28      1.1     rmind #define	F	TH_FIN
     29      1.1     rmind #define	OUT	0x1
     30      1.1     rmind #define	IN	0x2
     31      1.1     rmind #define	ERR	0x4
     32      1.1     rmind #define	CLEAR	.flags = 0
     33      1.1     rmind 
     34      1.1     rmind static const tcp_meta_t packet_sequence[] = {
     35      1.1     rmind 	/*
     36      1.1     rmind 	 *	TCP data	SEQ	ACK		WIN
     37      1.1     rmind 	 */
     38      1.1     rmind 
     39      1.1     rmind 	/* Out of order ACK. */
     40      1.1     rmind 	{ S,	0,		9999,	0,		4096,	OUT	},
     41      1.1     rmind 	{ S|A,	0,		9,	10000,		2048,	IN	},
     42      1.1     rmind 	{ A,	0,		10000,	10,		4096,	OUT	},
     43      1.1     rmind 	/* --- */
     44      1.1     rmind 	{ A,	0,		10,	10000,		2048,	IN	},
     45      1.1     rmind 	{ A,	1000,		10000,	10,		4096,	OUT	},
     46      1.1     rmind 	{ A,	1000,		11000,	10,		4096,	OUT	},
     47      1.1     rmind 	{ A,	0,		10,	12000,		2048,	IN	},
     48      1.1     rmind 	{ A,	0,		10,	13000,		2048,	IN	},
     49      1.1     rmind 	{ A,	1000,		12000,	10,		4096,	OUT	},
     50      1.1     rmind 	{ A,	0,		10,	11000,		1048,	IN	},
     51      1.1     rmind 	/* --- */
     52      1.1     rmind 	{ A,	1000,		14000,	10,		4096,	OUT	},
     53      1.1     rmind 	{ A,	0,		10,	13000,		2048,	IN	},
     54      1.1     rmind 	{ CLEAR },
     55      1.1     rmind 
     56      1.1     rmind 	/* Retransmission after out of order ACK and missing ACK. */
     57      1.1     rmind 	{ S,	0,		9999,	0,		1000,	OUT	},
     58      1.1     rmind 	{ S|A,	0,		9,	10000,		4000,	IN	},
     59      1.1     rmind 	{ A,	0,		10000,	10,		1000,	OUT	},
     60      1.1     rmind 	/* --- */
     61      1.1     rmind 	{ A,	1000,		10000,	10,		1000,	OUT	},
     62      1.1     rmind 	{ A,	0,		10,	11000,		4000,	IN	},
     63      1.1     rmind 	{ A,	1000,		11000,	10,		1000,	OUT	},
     64      1.1     rmind 	{ A,	1000,		12000,	10,		1000,	OUT	},
     65      1.1     rmind 	{ A,	1000,		13000,	10,		1000,	OUT	},
     66      1.1     rmind 	{ A,	1000,		14000,	10,		1000,	OUT	},
     67      1.1     rmind 	/* --- Assume the first was delayed; second was lost after us. */
     68      1.1     rmind 	{ A,	0,		10,	15000,		4000,	IN	},
     69      1.1     rmind 	{ A,	0,		10,	15000,		2000,	IN	},
     70      1.1     rmind 	/* --- */
     71      1.1     rmind 	{ A,	1000,		12000,	10,		1000,	OUT	},
     72      1.1     rmind 	{ CLEAR },
     73      1.1     rmind 
     74      1.2     rmind 	/* FIN exchange with retransmit. */
     75      1.2     rmind 	{ S,	0,		999,	0,		1000,	OUT	},
     76      1.2     rmind 	{ S|A,	0,		9,	1000,		2000,	IN	},
     77      1.2     rmind 	{ A,	0,		1000,	10,		1000,	OUT	},
     78      1.2     rmind 	/* --- */
     79      1.2     rmind 	{ F,	0,		10,	1000,		2000,	IN	},
     80      1.2     rmind 	{ F,	0,		1000,	10,		1000,	OUT	},
     81      1.2     rmind 	{ A,	0,		1000,	11,		1000,	OUT	},
     82      1.2     rmind 	/* --- */
     83      1.2     rmind 	{ F,	0,		1000,	11,		1000,	OUT	},
     84      1.2     rmind 	{ F,	0,		1000,	11,		1000,	OUT	},
     85      1.2     rmind 	{ A,	0,		11,	1001,		2000,	OUT	},
     86      1.2     rmind 	{ CLEAR },
     87      1.2     rmind 
     88      1.1     rmind 	/* Out of window. */
     89      1.1     rmind 	{ S,	0,		9,	0,		8760,	OUT	},
     90      1.1     rmind 	{ S|A,	0,		9999,	10,		1000,	IN	},
     91      1.1     rmind 	{ A,	0,		10,	10000,		8760,	OUT	},
     92      1.1     rmind 	/* --- */
     93      1.1     rmind 	{ A,	1460,		10000,	10,		1000,	IN	},
     94      1.1     rmind 	{ A,	1460,		11460,	10,		1000,	IN	},
     95      1.1     rmind 	{ A,	0,		10,	12920,		8760,	OUT	},
     96      1.1     rmind 	{ A,	1460,		12920,	10,		1000,	IN	},
     97      1.1     rmind 	{ A,	0,		10,	14380,		8760,	OUT	},
     98      1.1     rmind 	{ A,	1460,		17300,	10,		1000,	IN	},
     99      1.1     rmind 	{ A,	0,		10,	14380,		8760,	OUT	},
    100      1.1     rmind 	{ A,	1460,		18760,	10,		1000,	IN	},
    101      1.1     rmind 	{ A,	0,		10,	14380,		8760,	OUT	},
    102      1.1     rmind 	{ A,	1460,		20220,	10,		1000,	IN	},
    103      1.1     rmind 	{ A,	0,		10,	14380,		8760,	OUT	},
    104      1.1     rmind 	{ A,	1460,		21680,	10,		1000,	IN	},
    105      1.1     rmind 	{ A,	0,		10,	14380,		8760,	OUT	},
    106      1.1     rmind 	/* --- */
    107      1.1     rmind 	{ A,	1460,		14380,	10,		1000,	IN	},
    108      1.1     rmind 	{ A,	1460,		23140,	10,		1000,	IN|ERR	},
    109      1.1     rmind 	{ CLEAR },
    110      1.1     rmind 
    111      1.1     rmind };
    112      1.1     rmind 
    113      1.1     rmind #undef S
    114      1.1     rmind #undef A
    115      1.1     rmind #undef F
    116      1.1     rmind 
    117      1.1     rmind static struct mbuf *
    118      1.1     rmind construct_packet(const tcp_meta_t *p)
    119      1.1     rmind {
    120      1.1     rmind 	struct mbuf *m = mbuf_construct(IPPROTO_TCP);
    121      1.1     rmind 	struct ip *ip;
    122      1.1     rmind 	struct tcphdr *th;
    123      1.1     rmind 
    124      1.1     rmind 	th = mbuf_return_hdrs(m, false, &ip);
    125      1.1     rmind 
    126      1.1     rmind 	/* Imitate TCP payload, set TCP sequence numbers, flags and window. */
    127      1.1     rmind 	ip->ip_len = htons(sizeof(struct ip) + sizeof(struct tcphdr) + p->tlen);
    128      1.1     rmind 	th->th_seq = htonl(p->seq);
    129      1.1     rmind 	th->th_ack = htonl(p->ack);
    130      1.1     rmind 	th->th_flags = p->tcpfl;
    131      1.1     rmind 	th->th_win = htons(p->win);
    132      1.1     rmind 	return m;
    133      1.1     rmind }
    134      1.1     rmind 
    135      1.1     rmind static bool
    136      1.1     rmind process_packet(const int i, npf_state_t *nst, bool *snew)
    137      1.1     rmind {
    138      1.5     rmind 	ifnet_t *dummy_ifp = npf_test_addif(IFNAME_TEST, false, false);
    139      1.1     rmind 	const tcp_meta_t *p = &packet_sequence[i];
    140  1.6.6.1  pgoyette 	npf_cache_t npc = { .npc_info = 0, .npc_ctx = npf_getkernctx() };
    141      1.4     rmind 	nbuf_t nbuf;
    142      1.1     rmind 	int ret;
    143      1.1     rmind 
    144      1.1     rmind 	if (p->flags == 0) {
    145      1.1     rmind 		npf_state_destroy(nst);
    146      1.1     rmind 		*snew = true;
    147      1.1     rmind 		return true;
    148      1.1     rmind 	}
    149      1.1     rmind 
    150  1.6.6.1  pgoyette 	nbuf_init(npf_getkernctx(), &nbuf, construct_packet(p), dummy_ifp);
    151      1.6     rmind 	npc.npc_nbuf = &nbuf;
    152      1.6     rmind 	ret = npf_cache_all(&npc);
    153      1.1     rmind 	KASSERT((ret & NPC_IPFRAG) == 0);
    154      1.1     rmind 
    155      1.1     rmind 	if (*snew) {
    156      1.6     rmind 		ret = npf_state_init(&npc, nst);
    157      1.1     rmind 		KASSERT(ret == true);
    158      1.1     rmind 		*snew = false;
    159      1.1     rmind 	}
    160      1.6     rmind 	ret = npf_state_inspect(&npc, nst, p->flags == OUT);
    161      1.4     rmind 	m_freem(nbuf.nb_mbuf);
    162      1.1     rmind 
    163      1.1     rmind 	return ret ? true : (p->flags & ERR) != 0;
    164      1.1     rmind }
    165      1.1     rmind 
    166      1.1     rmind bool
    167      1.1     rmind npf_state_test(bool verbose)
    168      1.1     rmind {
    169      1.1     rmind 	npf_state_t nst;
    170      1.1     rmind 	bool snew = true;
    171      1.3     rmind 	bool ok = true;
    172      1.1     rmind 
    173      1.1     rmind 	for (u_int i = 0; i < __arraycount(packet_sequence); i++) {
    174      1.1     rmind 		if (process_packet(i, &nst, &snew)) {
    175      1.1     rmind 			continue;
    176      1.1     rmind 		}
    177      1.1     rmind 		if (verbose) {
    178      1.1     rmind 			printf("Failed on packet %d, state dump:\n", i);
    179      1.1     rmind 			npf_state_dump(&nst);
    180      1.1     rmind 		}
    181      1.3     rmind 		ok = false;
    182      1.1     rmind 	}
    183      1.3     rmind 	return ok;
    184      1.1     rmind }
    185