Home | History | Annotate | Line # | Download | only in libnpftest
npf_rule_test.c revision 1.21
      1   1.1     rmind /*
      2  1.16     rmind  * NPF ruleset tests.
      3   1.1     rmind  *
      4   1.1     rmind  * Public Domain.
      5   1.1     rmind  */
      6   1.1     rmind 
      7  1.13  christos #ifdef _KERNEL
      8   1.1     rmind #include <sys/types.h>
      9  1.13  christos #endif
     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.20  riastrad 	int		af;
     19   1.1     rmind 	const char *	src;
     20   1.1     rmind 	const char *	dst;
     21   1.1     rmind 	const char *	ifname;
     22   1.1     rmind 	int		di;
     23   1.1     rmind 	int		stateful_ret;
     24   1.1     rmind 	int		ret;
     25   1.1     rmind } test_cases[] = {
     26   1.1     rmind 
     27   1.1     rmind 	/* Stateful pass. */
     28   1.1     rmind 	{
     29  1.20  riastrad 		.af = AF_INET,
     30   1.1     rmind 		.src = "10.1.1.1",		.dst = "10.1.1.2",
     31   1.1     rmind 		.ifname = IFNAME_INT,		.di = PFIL_OUT,
     32   1.1     rmind 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
     33   1.1     rmind 	},
     34   1.1     rmind 	{
     35  1.20  riastrad 		.af = AF_INET,
     36   1.1     rmind 		.src = "10.1.1.2",		.dst = "10.1.1.1",
     37   1.1     rmind 		.ifname = IFNAME_INT,		.di = PFIL_IN,
     38   1.1     rmind 		.stateful_ret = RESULT_PASS,	.ret = RESULT_BLOCK
     39   1.1     rmind 	},
     40   1.1     rmind 
     41   1.1     rmind 	/* Pass forwards stream only. */
     42   1.1     rmind 	{
     43  1.20  riastrad 		.af = AF_INET,
     44   1.1     rmind 		.src = "10.1.1.1",		.dst = "10.1.1.3",
     45   1.1     rmind 		.ifname = IFNAME_INT,		.di = PFIL_OUT,
     46   1.1     rmind 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
     47   1.1     rmind 	},
     48   1.1     rmind 	{
     49  1.20  riastrad 		.af = AF_INET,
     50   1.1     rmind 		.src = "10.1.1.3",		.dst = "10.1.1.1",
     51   1.1     rmind 		.ifname = IFNAME_INT,		.di = PFIL_IN,
     52   1.1     rmind 		.stateful_ret = RESULT_BLOCK,	.ret = RESULT_BLOCK
     53   1.1     rmind 	},
     54   1.1     rmind 
     55  1.21  riastrad 	/*
     56  1.21  riastrad 	 * Pass any of the { fe80::1, fe80::2 } group but nothing else
     57  1.21  riastrad 	 * in fe80::/112.
     58  1.21  riastrad 	 */
     59  1.21  riastrad 	{
     60  1.21  riastrad 		.af = AF_INET6,
     61  1.21  riastrad 		.src = "fe80::1", .dst = "fe80::adec:c91c:d116:7592",
     62  1.21  riastrad 		.ifname = IFNAME_EXT,		.di = PFIL_IN,
     63  1.21  riastrad 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
     64  1.21  riastrad 	},
     65  1.21  riastrad 	{
     66  1.21  riastrad 		.af = AF_INET6,
     67  1.21  riastrad 		.src = "fe80::2", .dst = "fe80::adec:c91c:d116:7592",
     68  1.21  riastrad 		.ifname = IFNAME_EXT,		.di = PFIL_IN,
     69  1.21  riastrad 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
     70  1.21  riastrad 	},
     71  1.21  riastrad 	{
     72  1.21  riastrad 		.af = AF_INET6,
     73  1.21  riastrad 		.src = "fe80::3", .dst = "fe80::adec:c91c:d116:7592",
     74  1.21  riastrad 		.ifname = IFNAME_EXT,		.di = PFIL_IN,
     75  1.21  riastrad 		.stateful_ret = RESULT_BLOCK,	.ret = RESULT_BLOCK
     76  1.21  riastrad 	},
     77  1.21  riastrad 
     78   1.1     rmind 	/* Block. */
     79  1.20  riastrad 	{
     80  1.20  riastrad 		.af = AF_INET,
     81  1.20  riastrad 		.src = "10.1.1.1",		.dst = "10.1.1.4",
     82   1.1     rmind 		.ifname = IFNAME_INT,		.di = PFIL_OUT,
     83   1.1     rmind 		.stateful_ret = RESULT_BLOCK,	.ret = RESULT_BLOCK
     84   1.1     rmind 	},
     85   1.1     rmind 
     86   1.1     rmind };
     87   1.1     rmind 
     88   1.1     rmind static int
     89  1.17     rmind run_raw_testcase(unsigned i)
     90   1.1     rmind {
     91  1.17     rmind 	const struct test_case *t = &test_cases[i];
     92  1.13  christos 	npf_t *npf = npf_getkernctx();
     93  1.17     rmind 	npf_cache_t *npc;
     94  1.17     rmind 	struct mbuf *m;
     95   1.1     rmind 	npf_rule_t *rl;
     96  1.17     rmind 	int slock, error;
     97  1.17     rmind 
     98  1.20  riastrad 	m = mbuf_get_pkt(t->af, IPPROTO_UDP, t->src, t->dst, 9000, 9000);
     99  1.17     rmind 	npc = get_cached_pkt(m, t->ifname);
    100   1.1     rmind 
    101  1.19     rmind 	slock = npf_config_read_enter(npf);
    102  1.17     rmind 	rl = npf_ruleset_inspect(npc, npf_config_ruleset(npf), t->di, NPF_LAYER_3);
    103   1.1     rmind 	if (rl) {
    104  1.17     rmind 		npf_match_info_t mi;
    105  1.14  christos 		error = npf_rule_conclude(rl, &mi);
    106   1.1     rmind 	} else {
    107   1.1     rmind 		error = ENOENT;
    108   1.1     rmind 	}
    109  1.19     rmind 	npf_config_read_exit(npf, slock);
    110  1.17     rmind 
    111  1.17     rmind 	put_cached_pkt(npc);
    112   1.1     rmind 	return error;
    113   1.1     rmind }
    114   1.1     rmind 
    115   1.4     rmind static int
    116  1.17     rmind run_handler_testcase(unsigned i)
    117   1.4     rmind {
    118   1.8     rmind 	const struct test_case *t = &test_cases[i];
    119  1.13  christos 	ifnet_t *ifp = npf_test_getif(t->ifname);
    120  1.17     rmind 	npf_t *npf = npf_getkernctx();
    121  1.17     rmind 	struct mbuf *m;
    122   1.4     rmind 	int error;
    123   1.4     rmind 
    124  1.20  riastrad 	m = mbuf_get_pkt(t->af, IPPROTO_UDP, t->src, t->dst, 9000, 9000);
    125  1.18     rmind 	error = npfk_packet_handler(npf, &m, ifp, t->di);
    126  1.17     rmind 	if (m) {
    127  1.17     rmind 		m_freem(m);
    128  1.17     rmind 	}
    129   1.4     rmind 	return error;
    130   1.4     rmind }
    131   1.4     rmind 
    132   1.4     rmind static npf_rule_t *
    133   1.4     rmind npf_blockall_rule(void)
    134   1.4     rmind {
    135  1.13  christos 	npf_t *npf = npf_getkernctx();
    136  1.15     rmind 	nvlist_t *rule = nvlist_create(0);
    137  1.17     rmind 	npf_rule_t *rl;
    138   1.4     rmind 
    139  1.15     rmind 	nvlist_add_number(rule, "attr",
    140   1.7     rmind 	    NPF_RULE_IN | NPF_RULE_OUT | NPF_RULE_DYNAMIC);
    141  1.17     rmind 	rl = npf_rule_alloc(npf, rule);
    142  1.17     rmind 	nvlist_destroy(rule);
    143  1.17     rmind 	return rl;
    144   1.4     rmind }
    145   1.4     rmind 
    146  1.17     rmind static bool
    147  1.17     rmind test_static(bool verbose)
    148   1.1     rmind {
    149   1.1     rmind 	for (unsigned i = 0; i < __arraycount(test_cases); i++) {
    150   1.1     rmind 		const struct test_case *t = &test_cases[i];
    151  1.17     rmind 		int error, serror;
    152   1.1     rmind 
    153  1.17     rmind 		if (npf_test_getif(t->ifname) == NULL) {
    154   1.1     rmind 			printf("Interface %s is not configured.\n", t->ifname);
    155   1.1     rmind 			return false;
    156   1.1     rmind 		}
    157   1.1     rmind 
    158  1.17     rmind 		error = run_raw_testcase(i);
    159  1.17     rmind 		serror = run_handler_testcase(i);
    160   1.1     rmind 
    161   1.1     rmind 		if (verbose) {
    162  1.16     rmind 			printf("rule test %d:\texpected %d (stateful) and %d\n"
    163  1.16     rmind 			    "\t\t-> returned %d and %d\n",
    164   1.1     rmind 			    i + 1, t->stateful_ret, t->ret, serror, error);
    165   1.1     rmind 		}
    166  1.17     rmind 		CHECK_TRUE(error == t->ret);
    167  1.17     rmind 		CHECK_TRUE(serror == t->stateful_ret)
    168   1.1     rmind 	}
    169  1.17     rmind 	return true;
    170  1.17     rmind }
    171  1.17     rmind 
    172  1.17     rmind static bool
    173  1.17     rmind test_dynamic(void)
    174  1.17     rmind {
    175  1.17     rmind 	npf_t *npf = npf_getkernctx();
    176  1.17     rmind 	npf_ruleset_t *rlset;
    177  1.17     rmind 	npf_rule_t *rl;
    178  1.17     rmind 	uint64_t id;
    179  1.17     rmind 	int error;
    180   1.4     rmind 
    181   1.8     rmind 	/*
    182   1.8     rmind 	 * Test dynamic NPF rules.
    183   1.8     rmind 	 */
    184   1.8     rmind 
    185  1.17     rmind 	error = run_raw_testcase(0);
    186  1.16     rmind 	CHECK_TRUE(error == RESULT_PASS);
    187   1.4     rmind 
    188  1.13  christos 	npf_config_enter(npf);
    189  1.13  christos 	rlset = npf_config_ruleset(npf);
    190   1.4     rmind 
    191   1.4     rmind 	rl = npf_blockall_rule();
    192   1.4     rmind 	error = npf_ruleset_add(rlset, "test-rules", rl);
    193  1.16     rmind 	CHECK_TRUE(error == 0);
    194   1.4     rmind 
    195  1.17     rmind 	error = run_raw_testcase(0);
    196  1.16     rmind 	CHECK_TRUE(error == RESULT_BLOCK);
    197   1.4     rmind 
    198   1.6     rmind 	id = npf_rule_getid(rl);
    199   1.6     rmind 	error = npf_ruleset_remove(rlset, "test-rules", id);
    200  1.16     rmind 	CHECK_TRUE(error == 0);
    201   1.4     rmind 
    202  1.13  christos 	npf_config_exit(npf);
    203   1.4     rmind 
    204  1.17     rmind 	error = run_raw_testcase(0);
    205  1.16     rmind 	CHECK_TRUE(error == RESULT_PASS);
    206   1.4     rmind 
    207  1.16     rmind 	return true;
    208   1.1     rmind }
    209  1.17     rmind 
    210  1.17     rmind bool
    211  1.17     rmind npf_rule_test(bool verbose)
    212  1.17     rmind {
    213  1.17     rmind 	bool ok;
    214  1.17     rmind 
    215  1.17     rmind 	ok = test_static(verbose);
    216  1.17     rmind 	CHECK_TRUE(ok);
    217  1.17     rmind 
    218  1.17     rmind 	ok = test_dynamic();
    219  1.17     rmind 	CHECK_TRUE(ok);
    220  1.17     rmind 
    221  1.17     rmind 	return true;
    222  1.17     rmind }
    223