npf_test_subr.c revision 1.1.2.2 1 /* $NetBSD: npf_test_subr.c,v 1.1.2.2 2012/06/26 00:07:19 riz Exp $ */
2
3 /*
4 * NPF initialisation and handler routines.
5 *
6 * Public Domain.
7 */
8
9 #include <sys/types.h>
10 #include <net/if.h>
11 #include <net/if_types.h>
12
13 #include "npf_impl.h"
14 #include "npf_test.h"
15
16 /* State of the current stream. */
17 static npf_state_t cstream_state;
18 static void * cstream_ptr;
19 static bool cstream_retval;
20
21 int
22 npf_test_load(const void *xml)
23 {
24 prop_dictionary_t npf_dict = prop_dictionary_internalize(xml);
25 return npfctl_reload(0, npf_dict);
26 }
27
28 /*
29 * State sampler - this routine is called from inside of NPF state engine.
30 */
31 void
32 npf_state_sample(npf_state_t *nst, bool retval)
33 {
34 /* Pointer will serve as an ID. */
35 cstream_ptr = nst;
36 memcpy(&cstream_state, nst, sizeof(npf_state_t));
37 cstream_retval = retval;
38 }
39
40 int
41 npf_test_handlepkt(const void *data, size_t len, unsigned idx,
42 bool forw, int64_t *result)
43 {
44 ifnet_t ifp = { .if_index = idx };
45 struct mbuf *m;
46 int i = 0, error;
47
48 m = mbuf_getwithdata(data, len);
49 error = npf_packet_handler(NULL, &m, &ifp, forw ? PFIL_OUT : PFIL_IN);
50 if (error) {
51 assert(m == NULL);
52 return error;
53 }
54 assert(m != NULL);
55 m_freem(m);
56
57 const int di = forw ? NPF_FLOW_FORW : NPF_FLOW_BACK;
58 npf_tcpstate_t *fstate = &cstream_state.nst_tcpst[di];
59 npf_tcpstate_t *tstate = &cstream_state.nst_tcpst[!di];
60
61 result[i++] = (intptr_t)cstream_ptr;
62 result[i++] = cstream_retval;
63 result[i++] = cstream_state.nst_state;
64
65 result[i++] = fstate->nst_end;
66 result[i++] = fstate->nst_maxend;
67 result[i++] = fstate->nst_maxwin;
68
69 result[i++] = tstate->nst_end;
70 result[i++] = tstate->nst_maxend;
71 result[i++] = tstate->nst_maxwin;
72
73 return 0;
74 }
75