Home | History | Annotate | Line # | Download | only in libnpftest
npf_test_subr.c revision 1.17
      1 /*
      2  * NPF initialisation and handler routines.
      3  *
      4  * Public Domain.
      5  */
      6 
      7 #ifdef _KERNEL
      8 #include <sys/types.h>
      9 #include <sys/cprng.h>
     10 #include <sys/kmem.h>
     11 #include <net/if.h>
     12 #include <net/if_types.h>
     13 #endif
     14 
     15 #include "npf_impl.h"
     16 #include "npf_test.h"
     17 
     18 /* State of the current stream. */
     19 static npf_state_t	cstream_state;
     20 static void *		cstream_ptr;
     21 static bool		cstream_retval;
     22 
     23 static long		(*_random_func)(void);
     24 static int		(*_pton_func)(int, const char *, void *);
     25 static const char *	(*_ntop_func)(int, const void *, char *, socklen_t);
     26 
     27 static void		npf_state_sample(npf_state_t *, bool);
     28 
     29 static void		load_npf_config_ifs(nvlist_t *, bool);
     30 
     31 #ifndef __NetBSD__
     32 /*
     33  * Standalone NPF: we define the same struct ifnet members
     34  * to reduce the npf_ifops_t implementation differences.
     35  */
     36 struct ifnet {
     37 	char		if_xname[32];
     38 	void *		if_softc;
     39 	TAILQ_ENTRY(ifnet) if_list;
     40 };
     41 #endif
     42 
     43 static TAILQ_HEAD(, ifnet) npftest_ifnet_list =
     44     TAILQ_HEAD_INITIALIZER(npftest_ifnet_list);
     45 
     46 static const char *	npftest_ifop_getname(npf_t *, ifnet_t *);
     47 static ifnet_t *	npftest_ifop_lookup(npf_t *, const char *);
     48 static void		npftest_ifop_flush(npf_t *, void *);
     49 static void *		npftest_ifop_getmeta(npf_t *, const ifnet_t *);
     50 static void		npftest_ifop_setmeta(npf_t *, ifnet_t *, void *);
     51 
     52 const npf_ifops_t npftest_ifops = {
     53 	.getname	= npftest_ifop_getname,
     54 	.lookup		= npftest_ifop_lookup,
     55 	.flush		= npftest_ifop_flush,
     56 	.getmeta	= npftest_ifop_getmeta,
     57 	.setmeta	= npftest_ifop_setmeta,
     58 };
     59 
     60 void
     61 npf_test_init(int (*pton_func)(int, const char *, void *),
     62     const char *(*ntop_func)(int, const void *, char *, socklen_t),
     63     long (*rndfunc)(void))
     64 {
     65 	npf_t *npf;
     66 
     67 	npfk_sysinit(0);
     68 	npf = npfk_create(0, &npftest_mbufops, &npftest_ifops, NULL);
     69 	npfk_thread_register(npf);
     70 	npf_setkernctx(npf);
     71 
     72 	npf_state_setsampler(npf_state_sample);
     73 	_pton_func = pton_func;
     74 	_ntop_func = ntop_func;
     75 	_random_func = rndfunc;
     76 
     77 	(void)npf_test_addif(IFNAME_DUMMY, false, false);
     78 }
     79 
     80 void
     81 npf_test_fini(void)
     82 {
     83 	npf_t *npf = npf_getkernctx();
     84 	npfk_thread_unregister(npf);
     85 	npfk_destroy(npf);
     86 	npfk_sysfini();
     87 }
     88 
     89 int
     90 npf_test_load(const void *buf, size_t len, bool verbose)
     91 {
     92 	nvlist_t *npf_dict;
     93 	npf_error_t error;
     94 	int ret;
     95 
     96 	npf_dict = nvlist_unpack(buf, len, 0);
     97 	if (!npf_dict) {
     98 		printf("%s: could not unpack the nvlist\n", __func__);
     99 		return EINVAL;
    100 	}
    101 	load_npf_config_ifs(npf_dict, verbose);
    102 	ret = npfk_load(npf_getkernctx(), npf_dict, &error);
    103 	nvlist_destroy(npf_dict);
    104 	return ret;
    105 }
    106 
    107 ifnet_t *
    108 npf_test_addif(const char *ifname, bool reg, bool verbose)
    109 {
    110 	npf_t *npf = npf_getkernctx();
    111 	ifnet_t *ifp = kmem_zalloc(sizeof(*ifp), KM_SLEEP);
    112 
    113 	/*
    114 	 * This is a "fake" interface with explicitly set index.
    115 	 * Note: test modules may not setup pfil(9) hooks and if_attach()
    116 	 * may not trigger npf_ifmap_attach(), so we call it manually.
    117 	 */
    118 	strlcpy(ifp->if_xname, ifname, sizeof(ifp->if_xname));
    119 	TAILQ_INSERT_TAIL(&npftest_ifnet_list, ifp, if_list);
    120 
    121 	npfk_ifmap_attach(npf, ifp);
    122 	if (reg) {
    123 		npf_ifmap_register(npf, ifname);
    124 	}
    125 
    126 	if (verbose) {
    127 		printf("+ Interface %s\n", ifname);
    128 	}
    129 	return ifp;
    130 }
    131 
    132 ifnet_t *
    133 npf_test_getif(const char *ifname)
    134 {
    135 	ifnet_t *ifp;
    136 
    137 	TAILQ_FOREACH(ifp, &npftest_ifnet_list, if_list) {
    138 		if (!strcmp(ifp->if_xname, ifname))
    139 			return ifp;
    140 	}
    141 	return NULL;
    142 }
    143 
    144 static void
    145 load_npf_config_ifs(nvlist_t *npf_dict, bool verbose)
    146 {
    147 	const nvlist_t * const *iflist;
    148 	const nvlist_t *dbg_dict;
    149 	size_t nitems;
    150 
    151 	dbg_dict = dnvlist_get_nvlist(npf_dict, "debug", NULL);
    152 	if (!dbg_dict) {
    153 		return;
    154 	}
    155 	if (!nvlist_exists_nvlist_array(dbg_dict, "interfaces")) {
    156 		return;
    157 	}
    158 	iflist = nvlist_get_nvlist_array(dbg_dict, "interfaces", &nitems);
    159 	for (unsigned i = 0; i < nitems; i++) {
    160 		const nvlist_t *ifdict = iflist[i];
    161 		const char *ifname;
    162 
    163 		if ((ifname = nvlist_get_string(ifdict, "name")) != NULL) {
    164 			(void)npf_test_addif(ifname, true, verbose);
    165 		}
    166 	}
    167 }
    168 
    169 static const char *
    170 npftest_ifop_getname(npf_t *npf __unused, ifnet_t *ifp)
    171 {
    172 	return ifp->if_xname;
    173 }
    174 
    175 static ifnet_t *
    176 npftest_ifop_lookup(npf_t *npf __unused, const char *ifname)
    177 {
    178 	return npf_test_getif(ifname);
    179 }
    180 
    181 static void
    182 npftest_ifop_flush(npf_t *npf __unused, void *arg)
    183 {
    184 	ifnet_t *ifp;
    185 
    186 	TAILQ_FOREACH(ifp, &npftest_ifnet_list, if_list)
    187 		ifp->if_softc = arg;
    188 }
    189 
    190 static void *
    191 npftest_ifop_getmeta(npf_t *npf __unused, const ifnet_t *ifp)
    192 {
    193 	return ifp->if_softc;
    194 }
    195 
    196 static void
    197 npftest_ifop_setmeta(npf_t *npf __unused, ifnet_t *ifp, void *arg)
    198 {
    199 	ifp->if_softc = arg;
    200 }
    201 
    202 /*
    203  * State sampler - this routine is called from inside of NPF state engine.
    204  */
    205 static void
    206 npf_state_sample(npf_state_t *nst, bool retval)
    207 {
    208 	/* Pointer will serve as an ID. */
    209 	cstream_ptr = nst;
    210 	memcpy(&cstream_state, nst, sizeof(npf_state_t));
    211 	cstream_retval = retval;
    212 }
    213 
    214 int
    215 npf_test_statetrack(const void *data, size_t len, ifnet_t *ifp,
    216     bool forw, int64_t *result)
    217 {
    218 	npf_t *npf = npf_getkernctx();
    219 	struct mbuf *m;
    220 	int i = 0, error;
    221 
    222 	m = mbuf_getwithdata(data, len);
    223 	error = npfk_packet_handler(npf, &m, ifp, forw ? PFIL_OUT : PFIL_IN);
    224 	if (error) {
    225 		assert(m == NULL);
    226 		return error;
    227 	}
    228 	assert(m != NULL);
    229 	m_freem(m);
    230 
    231 	const int di = forw ? NPF_FLOW_FORW : NPF_FLOW_BACK;
    232 	npf_tcpstate_t *fstate = &cstream_state.nst_tcpst[di];
    233 	npf_tcpstate_t *tstate = &cstream_state.nst_tcpst[!di];
    234 
    235 	result[i++] = (intptr_t)cstream_ptr;
    236 	result[i++] = cstream_retval;
    237 	result[i++] = cstream_state.nst_state;
    238 
    239 	result[i++] = fstate->nst_end;
    240 	result[i++] = fstate->nst_maxend;
    241 	result[i++] = fstate->nst_maxwin;
    242 	result[i++] = fstate->nst_wscale;
    243 
    244 	result[i++] = tstate->nst_end;
    245 	result[i++] = tstate->nst_maxend;
    246 	result[i++] = tstate->nst_maxwin;
    247 	result[i++] = tstate->nst_wscale;
    248 
    249 	return 0;
    250 }
    251 
    252 int
    253 npf_inet_pton(int af, const char *src, void *dst)
    254 {
    255 	return _pton_func(af, src, dst);
    256 }
    257 
    258 const char *
    259 npf_inet_ntop(int af, const void *src, char *dst, socklen_t size)
    260 {
    261 	return _ntop_func(af, src, dst, size);
    262 }
    263 
    264 #ifdef _KERNEL
    265 /*
    266  * Need to override cprng_fast32() -- we need deterministic PRNG.
    267  */
    268 uint32_t
    269 cprng_fast32(void)
    270 {
    271 	return (uint32_t)(_random_func ? _random_func() : random());
    272 }
    273 #endif
    274