Home | History | Annotate | Line # | Download | only in libnpftest
npf_rule_test.c revision 1.11
      1  1.11  rmind /*	$NetBSD: npf_rule_test.c,v 1.11 2014/07/20 00:37:41 rmind Exp $	*/
      2   1.1  rmind 
      3   1.1  rmind /*
      4   1.1  rmind  * NPF ruleset test.
      5   1.1  rmind  *
      6   1.1  rmind  * Public Domain.
      7   1.1  rmind  */
      8   1.1  rmind 
      9   1.1  rmind #include <sys/types.h>
     10   1.1  rmind 
     11   1.1  rmind #include "npf_impl.h"
     12   1.1  rmind #include "npf_test.h"
     13   1.1  rmind 
     14   1.1  rmind #define	RESULT_PASS	0
     15   1.1  rmind #define	RESULT_BLOCK	ENETUNREACH
     16   1.1  rmind 
     17   1.1  rmind static const struct test_case {
     18   1.1  rmind 	const char *	src;
     19   1.1  rmind 	const char *	dst;
     20   1.1  rmind 	const char *	ifname;
     21   1.1  rmind 	int		di;
     22   1.1  rmind 	int		stateful_ret;
     23   1.1  rmind 	int		ret;
     24   1.1  rmind } test_cases[] = {
     25   1.1  rmind 
     26   1.1  rmind 	/* Stateful pass. */
     27   1.1  rmind 	{
     28   1.1  rmind 		.src = "10.1.1.1",		.dst = "10.1.1.2",
     29   1.1  rmind 		.ifname = IFNAME_INT,		.di = PFIL_OUT,
     30   1.1  rmind 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
     31   1.1  rmind 	},
     32   1.1  rmind 	{
     33   1.1  rmind 		.src = "10.1.1.2",		.dst = "10.1.1.1",
     34   1.1  rmind 		.ifname = IFNAME_INT,		.di = PFIL_IN,
     35   1.1  rmind 		.stateful_ret = RESULT_PASS,	.ret = RESULT_BLOCK
     36   1.1  rmind 	},
     37   1.1  rmind 
     38   1.1  rmind 	/* Pass forwards stream only. */
     39   1.1  rmind 	{
     40   1.1  rmind 		.src = "10.1.1.1",		.dst = "10.1.1.3",
     41   1.1  rmind 		.ifname = IFNAME_INT,		.di = PFIL_OUT,
     42   1.1  rmind 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
     43   1.1  rmind 	},
     44   1.1  rmind 	{
     45   1.1  rmind 		.src = "10.1.1.3",		.dst = "10.1.1.1",
     46   1.1  rmind 		.ifname = IFNAME_INT,		.di = PFIL_IN,
     47   1.1  rmind 		.stateful_ret = RESULT_BLOCK,	.ret = RESULT_BLOCK
     48   1.1  rmind 	},
     49   1.1  rmind 
     50   1.1  rmind 	/* Block. */
     51   1.1  rmind 	{	.src = "10.1.1.1",		.dst = "10.1.1.4",
     52   1.1  rmind 		.ifname = IFNAME_INT,		.di = PFIL_OUT,
     53   1.1  rmind 		.stateful_ret = RESULT_BLOCK,	.ret = RESULT_BLOCK
     54   1.1  rmind 	},
     55   1.1  rmind 
     56   1.1  rmind };
     57   1.1  rmind 
     58   1.1  rmind static struct mbuf *
     59   1.1  rmind fill_packet(const struct test_case *t)
     60   1.1  rmind {
     61   1.1  rmind 	struct mbuf *m;
     62   1.1  rmind 	struct ip *ip;
     63   1.1  rmind 	struct udphdr *uh;
     64   1.1  rmind 
     65   1.1  rmind 	m = mbuf_construct(IPPROTO_UDP);
     66   1.1  rmind 	uh = mbuf_return_hdrs(m, false, &ip);
     67   1.1  rmind 	ip->ip_src.s_addr = inet_addr(t->src);
     68   1.1  rmind 	ip->ip_dst.s_addr = inet_addr(t->dst);
     69   1.1  rmind 	uh->uh_sport = htons(9000);
     70   1.1  rmind 	uh->uh_dport = htons(9000);
     71   1.1  rmind 	return m;
     72   1.1  rmind }
     73   1.1  rmind 
     74   1.1  rmind static int
     75   1.1  rmind npf_rule_raw_test(bool verbose, struct mbuf *m, ifnet_t *ifp, int di)
     76   1.1  rmind {
     77   1.1  rmind 	npf_cache_t npc = { .npc_info = 0 };
     78   1.3  rmind 	nbuf_t nbuf;
     79   1.1  rmind 	npf_rule_t *rl;
     80   1.1  rmind 	int retfl, error;
     81   1.1  rmind 
     82   1.3  rmind 	nbuf_init(&nbuf, m, ifp);
     83  1.11  rmind 	npc.npc_nbuf = &nbuf;
     84  1.11  rmind 	npf_cache_all(&npc);
     85   1.3  rmind 
     86   1.4  rmind 	int slock = npf_config_read_enter();
     87  1.11  rmind 	rl = npf_ruleset_inspect(&npc, npf_config_ruleset(),
     88   1.3  rmind 	    di, NPF_LAYER_3);
     89   1.1  rmind 	if (rl) {
     90   1.4  rmind 		error = npf_rule_conclude(rl, &retfl);
     91   1.1  rmind 	} else {
     92   1.1  rmind 		error = ENOENT;
     93   1.1  rmind 	}
     94   1.4  rmind 	npf_config_read_exit(slock);
     95   1.1  rmind 	return error;
     96   1.1  rmind }
     97   1.1  rmind 
     98   1.4  rmind static int
     99   1.8  rmind npf_test_case(u_int i, bool verbose)
    100   1.4  rmind {
    101   1.8  rmind 	const struct test_case *t = &test_cases[i];
    102   1.4  rmind 	ifnet_t *ifp = ifunit(t->ifname);
    103   1.4  rmind 	int error;
    104   1.4  rmind 
    105   1.4  rmind 	struct mbuf *m = fill_packet(t);
    106   1.4  rmind 	error = npf_rule_raw_test(verbose, m, ifp, t->di);
    107   1.4  rmind 	m_freem(m);
    108   1.4  rmind 	return error;
    109   1.4  rmind }
    110   1.4  rmind 
    111   1.4  rmind static npf_rule_t *
    112   1.4  rmind npf_blockall_rule(void)
    113   1.4  rmind {
    114   1.4  rmind 	prop_dictionary_t rldict;
    115   1.4  rmind 
    116   1.4  rmind 	rldict = prop_dictionary_create();
    117   1.4  rmind 	prop_dictionary_set_uint32(rldict, "attributes",
    118   1.7  rmind 	    NPF_RULE_IN | NPF_RULE_OUT | NPF_RULE_DYNAMIC);
    119   1.4  rmind 	return npf_rule_alloc(rldict);
    120   1.4  rmind }
    121   1.4  rmind 
    122   1.1  rmind bool
    123   1.1  rmind npf_rule_test(bool verbose)
    124   1.1  rmind {
    125   1.4  rmind 	npf_ruleset_t *rlset;
    126   1.4  rmind 	npf_rule_t *rl;
    127   1.2  rmind 	bool fail = false;
    128   1.6  rmind 	uint64_t id;
    129   1.4  rmind 	int error;
    130   1.2  rmind 
    131   1.1  rmind 	for (unsigned i = 0; i < __arraycount(test_cases); i++) {
    132   1.1  rmind 		const struct test_case *t = &test_cases[i];
    133   1.1  rmind 		ifnet_t *ifp = ifunit(t->ifname);
    134   1.4  rmind 		int serror;
    135   1.1  rmind 
    136   1.1  rmind 		if (ifp == NULL) {
    137   1.1  rmind 			printf("Interface %s is not configured.\n", t->ifname);
    138   1.1  rmind 			return false;
    139   1.1  rmind 		}
    140   1.1  rmind 
    141   1.2  rmind 		struct mbuf *m = fill_packet(t);
    142   1.1  rmind 		error = npf_rule_raw_test(verbose, m, ifp, t->di);
    143   1.1  rmind 		serror = npf_packet_handler(NULL, &m, ifp, t->di);
    144   1.1  rmind 
    145   1.1  rmind 		if (m) {
    146   1.1  rmind 			m_freem(m);
    147   1.1  rmind 		}
    148   1.1  rmind 
    149   1.1  rmind 		if (verbose) {
    150   1.1  rmind 			printf("Rule test %d, expected %d (stateful) and %d \n"
    151   1.1  rmind 			    "-> returned %d and %d.\n",
    152   1.1  rmind 			    i + 1, t->stateful_ret, t->ret, serror, error);
    153   1.1  rmind 		}
    154   1.2  rmind 		fail |= (serror != t->stateful_ret || error != t->ret);
    155   1.1  rmind 	}
    156   1.4  rmind 
    157   1.8  rmind 	/*
    158   1.8  rmind 	 * Test dynamic NPF rules.
    159   1.8  rmind 	 */
    160   1.8  rmind 
    161   1.8  rmind 	error = npf_test_case(0, verbose);
    162   1.4  rmind 	assert(error == RESULT_PASS);
    163   1.4  rmind 
    164   1.4  rmind 	npf_config_enter();
    165   1.4  rmind 	rlset = npf_config_ruleset();
    166   1.4  rmind 
    167   1.4  rmind 	rl = npf_blockall_rule();
    168   1.4  rmind 	error = npf_ruleset_add(rlset, "test-rules", rl);
    169   1.4  rmind 	fail |= error != 0;
    170   1.4  rmind 
    171   1.8  rmind 	error = npf_test_case(0, verbose);
    172   1.4  rmind 	fail |= (error != RESULT_BLOCK);
    173   1.4  rmind 
    174   1.6  rmind 	id = npf_rule_getid(rl);
    175   1.6  rmind 	error = npf_ruleset_remove(rlset, "test-rules", id);
    176   1.5  rmind 	fail |= error != 0;
    177   1.4  rmind 
    178   1.4  rmind 	npf_config_exit();
    179   1.4  rmind 
    180   1.8  rmind 	error = npf_test_case(0, verbose);
    181   1.4  rmind 	fail |= (error != RESULT_PASS);
    182   1.4  rmind 
    183   1.2  rmind 	return !fail;
    184   1.1  rmind }
    185