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