npf_test_subr.c revision 1.7 1 /* $NetBSD: npf_test_subr.c,v 1.7 2014/02/05 03:30:13 rmind Exp $ */
2
3 /*
4 * NPF initialisation and handler routines.
5 *
6 * Public Domain.
7 */
8
9 #include <sys/types.h>
10 #include <sys/cprng.h>
11 #include <net/if.h>
12 #include <net/if_types.h>
13
14 #include "npf_impl.h"
15 #include "npf_test.h"
16
17 /* State of the current stream. */
18 static npf_state_t cstream_state;
19 static void * cstream_ptr;
20 static bool cstream_retval;
21
22 static void npf_state_sample(npf_state_t *, bool);
23
24 void
25 npf_test_init(void)
26 {
27 npf_state_setsampler(npf_state_sample);
28 }
29
30 int
31 npf_test_load(const void *xml)
32 {
33 prop_dictionary_t npf_dict = prop_dictionary_internalize(xml);
34 return npfctl_reload(0, npf_dict);
35 }
36
37 ifnet_t *
38 npf_test_addif(const char *ifname, bool reg, bool verbose)
39 {
40 ifnet_t *ifp = if_alloc(IFT_OTHER);
41
42 /*
43 * This is a "fake" interface with explicitly set index.
44 * Note: test modules may not setup pfil(9) hooks and if_attach()
45 * may not trigger npf_ifmap_attach(), so we call it manually.
46 */
47 strlcpy(ifp->if_xname, ifname, sizeof(ifp->if_xname));
48 ifp->if_dlt = DLT_NULL;
49 ifp->if_index = 0;
50 if_attach(ifp);
51 if_alloc_sadl(ifp);
52
53 npf_ifmap_attach(ifp);
54 if (reg) {
55 npf_ifmap_register(ifname);
56 }
57
58 if (verbose) {
59 printf("+ Interface %s\n", ifname);
60 }
61 return ifp;
62 }
63
64 ifnet_t *
65 npf_test_getif(const char *ifname)
66 {
67 return ifunit(ifname);
68 }
69
70 /*
71 * State sampler - this routine is called from inside of NPF state engine.
72 */
73 static void
74 npf_state_sample(npf_state_t *nst, bool retval)
75 {
76 /* Pointer will serve as an ID. */
77 cstream_ptr = nst;
78 memcpy(&cstream_state, nst, sizeof(npf_state_t));
79 cstream_retval = retval;
80 }
81
82 int
83 npf_test_statetrack(const void *data, size_t len, ifnet_t *ifp,
84 bool forw, int64_t *result)
85 {
86 struct mbuf *m;
87 int i = 0, error;
88
89 m = mbuf_getwithdata(data, len);
90 error = npf_packet_handler(NULL, &m, ifp, forw ? PFIL_OUT : PFIL_IN);
91 if (error) {
92 assert(m == NULL);
93 return error;
94 }
95 assert(m != NULL);
96 m_freem(m);
97
98 const int di = forw ? NPF_FLOW_FORW : NPF_FLOW_BACK;
99 npf_tcpstate_t *fstate = &cstream_state.nst_tcpst[di];
100 npf_tcpstate_t *tstate = &cstream_state.nst_tcpst[!di];
101
102 result[i++] = (intptr_t)cstream_ptr;
103 result[i++] = cstream_retval;
104 result[i++] = cstream_state.nst_state;
105
106 result[i++] = fstate->nst_end;
107 result[i++] = fstate->nst_maxend;
108 result[i++] = fstate->nst_maxwin;
109 result[i++] = fstate->nst_wscale;
110
111 result[i++] = tstate->nst_end;
112 result[i++] = tstate->nst_maxend;
113 result[i++] = tstate->nst_maxwin;
114 result[i++] = tstate->nst_wscale;
115
116 return 0;
117 }
118
119 /*
120 * Need to override for cprng_fast32() -- we need deterministic PRNG.
121 */
122 uint32_t
123 _arc4random(void)
124 {
125 return random();
126 }
127