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