npf_test_subr.c revision 1.3 1 /* $NetBSD: npf_test_subr.c,v 1.3 2012/08/12 03:35:14 rmind 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 unsigned
29 npf_test_addif(const char *ifname, unsigned if_idx, bool verbose)
30 {
31 ifnet_t *ifp = if_alloc(IFT_OTHER);
32
33 /*
34 * This is a "fake" interface with explicitly set index.
35 */
36 strlcpy(ifp->if_xname, ifname, sizeof(ifp->if_xname));
37 if (verbose) {
38 printf("+ Interface %s\n", ifp->if_xname);
39 }
40 ifp->if_dlt = DLT_NULL;
41 if_attach(ifp);
42 ifp->if_index = if_idx;
43 if_alloc_sadl(ifp);
44 return if_idx;
45 }
46
47 unsigned
48 npf_test_getif(const char *ifname)
49 {
50 ifnet_t *ifp = ifunit(ifname);
51 return ifp ? ifp->if_index : 0;
52 }
53
54 /*
55 * State sampler - this routine is called from inside of NPF state engine.
56 */
57 void
58 npf_state_sample(npf_state_t *nst, bool retval)
59 {
60 /* Pointer will serve as an ID. */
61 cstream_ptr = nst;
62 memcpy(&cstream_state, nst, sizeof(npf_state_t));
63 cstream_retval = retval;
64 }
65
66 int
67 npf_test_handlepkt(const void *data, size_t len, unsigned idx,
68 bool forw, int64_t *result)
69 {
70 ifnet_t ifp = { .if_index = idx };
71 struct mbuf *m;
72 int i = 0, error;
73
74 m = mbuf_getwithdata(data, len);
75 error = npf_packet_handler(NULL, &m, &ifp, forw ? PFIL_OUT : PFIL_IN);
76 if (error) {
77 assert(m == NULL);
78 return error;
79 }
80 assert(m != NULL);
81 m_freem(m);
82
83 const int di = forw ? NPF_FLOW_FORW : NPF_FLOW_BACK;
84 npf_tcpstate_t *fstate = &cstream_state.nst_tcpst[di];
85 npf_tcpstate_t *tstate = &cstream_state.nst_tcpst[!di];
86
87 result[i++] = (intptr_t)cstream_ptr;
88 result[i++] = cstream_retval;
89 result[i++] = cstream_state.nst_state;
90
91 result[i++] = fstate->nst_end;
92 result[i++] = fstate->nst_maxend;
93 result[i++] = fstate->nst_maxwin;
94 result[i++] = fstate->nst_wscale;
95
96 result[i++] = tstate->nst_end;
97 result[i++] = tstate->nst_maxend;
98 result[i++] = tstate->nst_maxwin;
99 result[i++] = tstate->nst_wscale;
100
101 return 0;
102 }
103