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