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