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