Home | History | Annotate | Line # | Download | only in libnpftest
npf_test_subr.c revision 1.12.14.2
      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.14.2    martin 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.2    martin 	npfk_sysinit(0);
     67  1.12.14.2    martin 	npf = npfk_create(0, &npftest_mbufops, &npftest_ifops);
     68  1.12.14.2    martin 	npfk_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.12.14.2    martin 
     76  1.12.14.2    martin 	(void)npf_test_addif(IFNAME_DUMMY, false, false);
     77        1.4     rmind }
     78        1.4     rmind 
     79       1.12  christos void
     80       1.12  christos npf_test_fini(void)
     81       1.12  christos {
     82       1.12  christos 	npf_t *npf = npf_getkernctx();
     83  1.12.14.2    martin 	npfk_thread_unregister(npf);
     84  1.12.14.2    martin 	npfk_destroy(npf);
     85  1.12.14.2    martin 	npfk_sysfini();
     86       1.12  christos }
     87       1.12  christos 
     88        1.1     rmind int
     89  1.12.14.1  christos npf_test_load(const void *buf, size_t len, bool verbose)
     90        1.1     rmind {
     91  1.12.14.1  christos 	nvlist_t *npf_dict;
     92  1.12.14.1  christos 	npf_error_t error;
     93  1.12.14.1  christos 
     94  1.12.14.1  christos 	npf_dict = nvlist_unpack(buf, len, 0);
     95  1.12.14.1  christos 	if (!npf_dict) {
     96  1.12.14.1  christos 		printf("%s: could not unpack the nvlist\n", __func__);
     97  1.12.14.1  christos 		return EINVAL;
     98  1.12.14.1  christos 	}
     99  1.12.14.1  christos 	load_npf_config_ifs(npf_dict, verbose);
    100  1.12.14.1  christos 
    101  1.12.14.1  christos 	// Note: npf_dict will be consumed by npf_load().
    102  1.12.14.2    martin 	return npfk_load(npf_getkernctx(), npf_dict, &error);
    103        1.1     rmind }
    104        1.1     rmind 
    105        1.6     rmind ifnet_t *
    106        1.6     rmind npf_test_addif(const char *ifname, bool reg, bool verbose)
    107        1.3     rmind {
    108       1.12  christos 	npf_t *npf = npf_getkernctx();
    109  1.12.14.1  christos 	ifnet_t *ifp = kmem_zalloc(sizeof(*ifp), KM_SLEEP);
    110        1.3     rmind 
    111        1.3     rmind 	/*
    112        1.3     rmind 	 * This is a "fake" interface with explicitly set index.
    113        1.6     rmind 	 * Note: test modules may not setup pfil(9) hooks and if_attach()
    114        1.6     rmind 	 * may not trigger npf_ifmap_attach(), so we call it manually.
    115        1.3     rmind 	 */
    116        1.3     rmind 	strlcpy(ifp->if_xname, ifname, sizeof(ifp->if_xname));
    117       1.12  christos 	TAILQ_INSERT_TAIL(&npftest_ifnet_list, ifp, if_list);
    118        1.6     rmind 
    119  1.12.14.2    martin 	npfk_ifmap_attach(npf, ifp);
    120        1.6     rmind 	if (reg) {
    121       1.12  christos 		npf_ifmap_register(npf, ifname);
    122        1.6     rmind 	}
    123        1.6     rmind 
    124        1.6     rmind 	if (verbose) {
    125        1.6     rmind 		printf("+ Interface %s\n", ifname);
    126        1.6     rmind 	}
    127        1.6     rmind 	return ifp;
    128        1.3     rmind }
    129        1.3     rmind 
    130  1.12.14.1  christos static void
    131  1.12.14.1  christos load_npf_config_ifs(nvlist_t *npf_dict, bool verbose)
    132  1.12.14.1  christos {
    133  1.12.14.1  christos 	const nvlist_t * const *iflist;
    134  1.12.14.1  christos 	const nvlist_t *dbg_dict;
    135  1.12.14.1  christos 	size_t nitems;
    136  1.12.14.1  christos 
    137  1.12.14.1  christos 	dbg_dict = dnvlist_get_nvlist(npf_dict, "debug", NULL);
    138  1.12.14.1  christos 	if (!dbg_dict) {
    139  1.12.14.1  christos 		return;
    140  1.12.14.1  christos 	}
    141  1.12.14.1  christos 	if (!nvlist_exists_nvlist_array(dbg_dict, "interfaces")) {
    142  1.12.14.1  christos 		return;
    143  1.12.14.1  christos 	}
    144  1.12.14.1  christos 	iflist = nvlist_get_nvlist_array(dbg_dict, "interfaces", &nitems);
    145  1.12.14.1  christos 	for (unsigned i = 0; i < nitems; i++) {
    146  1.12.14.1  christos 		const nvlist_t *ifdict = iflist[i];
    147  1.12.14.1  christos 		const char *ifname;
    148  1.12.14.1  christos 
    149  1.12.14.1  christos 		if ((ifname = nvlist_get_string(ifdict, "name")) != NULL) {
    150  1.12.14.1  christos 			(void)npf_test_addif(ifname, true, verbose);
    151  1.12.14.1  christos 		}
    152  1.12.14.1  christos 	}
    153  1.12.14.1  christos }
    154  1.12.14.1  christos 
    155       1.12  christos static const char *
    156       1.12  christos npftest_ifop_getname(ifnet_t *ifp)
    157       1.12  christos {
    158       1.12  christos 	return ifp->if_xname;
    159       1.12  christos }
    160       1.12  christos 
    161        1.6     rmind ifnet_t *
    162        1.3     rmind npf_test_getif(const char *ifname)
    163        1.3     rmind {
    164       1.12  christos 	ifnet_t *ifp;
    165       1.12  christos 
    166       1.12  christos 	TAILQ_FOREACH(ifp, &npftest_ifnet_list, if_list) {
    167       1.12  christos 		if (!strcmp(ifp->if_xname, ifname))
    168       1.12  christos 			return ifp;
    169       1.12  christos 	}
    170       1.12  christos 	return NULL;
    171       1.12  christos }
    172       1.12  christos 
    173       1.12  christos static void
    174       1.12  christos npftest_ifop_flush(void *arg)
    175       1.12  christos {
    176       1.12  christos 	ifnet_t *ifp;
    177       1.12  christos 
    178       1.12  christos 	TAILQ_FOREACH(ifp, &npftest_ifnet_list, if_list)
    179       1.12  christos 		ifp->if_softc = arg;
    180       1.12  christos }
    181       1.12  christos 
    182       1.12  christos static void *
    183       1.12  christos npftest_ifop_getmeta(const ifnet_t *ifp)
    184       1.12  christos {
    185       1.12  christos 	return ifp->if_softc;
    186       1.12  christos }
    187       1.12  christos 
    188       1.12  christos static void
    189       1.12  christos npftest_ifop_setmeta(ifnet_t *ifp, void *arg)
    190       1.12  christos {
    191       1.12  christos 	ifp->if_softc = arg;
    192        1.3     rmind }
    193        1.3     rmind 
    194        1.1     rmind /*
    195        1.1     rmind  * State sampler - this routine is called from inside of NPF state engine.
    196        1.1     rmind  */
    197        1.4     rmind static void
    198        1.1     rmind npf_state_sample(npf_state_t *nst, bool retval)
    199        1.1     rmind {
    200        1.1     rmind 	/* Pointer will serve as an ID. */
    201        1.1     rmind 	cstream_ptr = nst;
    202        1.1     rmind 	memcpy(&cstream_state, nst, sizeof(npf_state_t));
    203        1.1     rmind 	cstream_retval = retval;
    204        1.1     rmind }
    205        1.1     rmind 
    206        1.1     rmind int
    207        1.6     rmind npf_test_statetrack(const void *data, size_t len, ifnet_t *ifp,
    208        1.1     rmind     bool forw, int64_t *result)
    209        1.1     rmind {
    210       1.12  christos 	npf_t *npf = npf_getkernctx();
    211        1.1     rmind 	struct mbuf *m;
    212        1.1     rmind 	int i = 0, error;
    213        1.1     rmind 
    214        1.1     rmind 	m = mbuf_getwithdata(data, len);
    215  1.12.14.2    martin 	error = npfk_packet_handler(npf, &m, ifp, forw ? PFIL_OUT : PFIL_IN);
    216        1.1     rmind 	if (error) {
    217        1.1     rmind 		assert(m == NULL);
    218        1.1     rmind 		return error;
    219        1.1     rmind 	}
    220        1.1     rmind 	assert(m != NULL);
    221        1.1     rmind 	m_freem(m);
    222        1.1     rmind 
    223        1.1     rmind 	const int di = forw ? NPF_FLOW_FORW : NPF_FLOW_BACK;
    224        1.1     rmind 	npf_tcpstate_t *fstate = &cstream_state.nst_tcpst[di];
    225        1.1     rmind 	npf_tcpstate_t *tstate = &cstream_state.nst_tcpst[!di];
    226        1.1     rmind 
    227        1.1     rmind 	result[i++] = (intptr_t)cstream_ptr;
    228        1.1     rmind 	result[i++] = cstream_retval;
    229        1.1     rmind 	result[i++] = cstream_state.nst_state;
    230        1.1     rmind 
    231        1.1     rmind 	result[i++] = fstate->nst_end;
    232        1.1     rmind 	result[i++] = fstate->nst_maxend;
    233        1.1     rmind 	result[i++] = fstate->nst_maxwin;
    234        1.2     rmind 	result[i++] = fstate->nst_wscale;
    235        1.1     rmind 
    236        1.1     rmind 	result[i++] = tstate->nst_end;
    237        1.1     rmind 	result[i++] = tstate->nst_maxend;
    238        1.1     rmind 	result[i++] = tstate->nst_maxwin;
    239        1.2     rmind 	result[i++] = tstate->nst_wscale;
    240        1.1     rmind 
    241        1.1     rmind 	return 0;
    242        1.1     rmind }
    243        1.7     rmind 
    244        1.9     rmind int
    245        1.9     rmind npf_inet_pton(int af, const char *src, void *dst)
    246        1.9     rmind {
    247        1.9     rmind 	return _pton_func(af, src, dst);
    248        1.9     rmind }
    249        1.9     rmind 
    250        1.9     rmind const char *
    251        1.9     rmind npf_inet_ntop(int af, const void *src, char *dst, socklen_t size)
    252        1.9     rmind {
    253        1.9     rmind 	return _ntop_func(af, src, dst, size);
    254        1.9     rmind }
    255        1.9     rmind 
    256       1.12  christos #ifdef _KERNEL
    257        1.7     rmind /*
    258       1.11       tls  * Need to override cprng_fast32() -- we need deterministic PRNG.
    259        1.7     rmind  */
    260        1.7     rmind uint32_t
    261       1.11       tls cprng_fast32(void)
    262        1.7     rmind {
    263        1.9     rmind 	return (uint32_t)(_random_func ? _random_func() : random());
    264        1.7     rmind }
    265       1.12  christos #endif
    266