Home | History | Annotate | Line # | Download | only in libnpftest
npf_rule_test.c revision 1.25
      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.23  riastrad 	 * Pass in from any of the { fe80::1, fe80:1000:0:0/95,
     57  1.23  riastrad 	 * fe80::2, fe80::2000:0:0/96, fe80::3, fe80::3000:0:0/97 }
     58  1.23  riastrad 	 * group.
     59  1.21  riastrad 	 */
     60  1.23  riastrad 	{			/* fe80::1 */
     61  1.21  riastrad 		.af = AF_INET6,
     62  1.21  riastrad 		.src = "fe80::1", .dst = "fe80::adec:c91c:d116:7592",
     63  1.22  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
     64  1.21  riastrad 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
     65  1.21  riastrad 	},
     66  1.23  riastrad 	{			/* fe80::1000:0:0/95 */
     67  1.23  riastrad 		.af = AF_INET6,
     68  1.23  riastrad 		.src = "fe80::1001:0:0", .dst = "fe80::adec:c91c:d116:7592",
     69  1.23  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
     70  1.23  riastrad 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
     71  1.23  riastrad 	},
     72  1.23  riastrad 	{			/* fe80::1000:0:0/95, one bit off */
     73  1.23  riastrad 		.af = AF_INET6,
     74  1.23  riastrad 		.src = "fe80::1003:0:0", .dst = "fe80::adec:c91c:d116:7592",
     75  1.23  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
     76  1.23  riastrad 		.stateful_ret = RESULT_BLOCK,	.ret = RESULT_BLOCK
     77  1.23  riastrad 	},
     78  1.23  riastrad 	{			/* fe80::2 */
     79  1.21  riastrad 		.af = AF_INET6,
     80  1.21  riastrad 		.src = "fe80::2", .dst = "fe80::adec:c91c:d116:7592",
     81  1.22  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
     82  1.21  riastrad 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
     83  1.21  riastrad 	},
     84  1.23  riastrad 	{			/* fe80::2000:0:0/96 */
     85  1.23  riastrad 		.af = AF_INET6,
     86  1.23  riastrad 		.src = "fe80::2000:8000:0", .dst = "fe80::adec:c91c:d116:7592",
     87  1.23  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
     88  1.23  riastrad 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
     89  1.23  riastrad 	},
     90  1.23  riastrad 	{			/* fe80::2000:0:0/96, one bit off */
     91  1.23  riastrad 		.af = AF_INET6,
     92  1.23  riastrad 		.src = "fe80::2001:8000:0", .dst = "fe80::adec:c91c:d116:7592",
     93  1.23  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
     94  1.23  riastrad 		.stateful_ret = RESULT_BLOCK,	.ret = RESULT_BLOCK
     95  1.23  riastrad 	},
     96  1.23  riastrad 	{			/* fe80::3 */
     97  1.23  riastrad 		.af = AF_INET6,
     98  1.23  riastrad 		.src = "fe80::3", .dst = "fe80::adec:c91c:d116:7592",
     99  1.23  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
    100  1.23  riastrad 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
    101  1.23  riastrad 	},
    102  1.23  riastrad 	{			/* fe80::3000:0:0/97 */
    103  1.23  riastrad 		.af = AF_INET6,
    104  1.23  riastrad 		.src = "fe80::3000:7fff:0", .dst = "fe80::adec:c91c:d116:7592",
    105  1.23  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
    106  1.23  riastrad 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
    107  1.23  riastrad 	},
    108  1.23  riastrad 	{			/* fe80::3000:0:0/97, one bit off */
    109  1.23  riastrad 		.af = AF_INET6,
    110  1.23  riastrad 		.src = "fe80::3000:ffff:0", .dst = "fe80::adec:c91c:d116:7592",
    111  1.23  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
    112  1.23  riastrad 		.stateful_ret = RESULT_BLOCK,	.ret = RESULT_BLOCK
    113  1.23  riastrad 	},
    114  1.21  riastrad 	{
    115  1.21  riastrad 		.af = AF_INET6,
    116  1.23  riastrad 		.src = "fe80::4", .dst = "fe80::adec:c91c:d116:7592",
    117  1.22  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
    118  1.22  riastrad 		.stateful_ret = RESULT_BLOCK,	.ret = RESULT_BLOCK
    119  1.22  riastrad 	},
    120  1.22  riastrad 
    121  1.22  riastrad 	/*
    122  1.23  riastrad 	 * Pass in from anywhere _not_ in that group, as long as it is
    123  1.23  riastrad 	 * to that group.
    124  1.22  riastrad 	 */
    125  1.23  riastrad 	{			/* fe80::1 */
    126  1.22  riastrad 		.af = AF_INET6,
    127  1.22  riastrad 		.src = "fe80::adec:c91c:d116:7592", .dst = "fe80::1",
    128  1.22  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
    129  1.22  riastrad 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
    130  1.22  riastrad 	},
    131  1.23  riastrad 	{			/* fe80::1000:0:0/95 */
    132  1.23  riastrad 		.af = AF_INET6,
    133  1.23  riastrad 		.src = "fe80::adec:c91c:d116:7592", .dst = "fe80::1001:0:0",
    134  1.23  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
    135  1.23  riastrad 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
    136  1.23  riastrad 	},
    137  1.23  riastrad 	{			/* fe80::1000:0:0/95, one bit off */
    138  1.23  riastrad 		.af = AF_INET6,
    139  1.23  riastrad 		.src = "fe80::adec:c91c:d116:7592", .dst = "fe80::1003:0:0",
    140  1.23  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
    141  1.23  riastrad 		.stateful_ret = RESULT_BLOCK,	.ret = RESULT_BLOCK
    142  1.23  riastrad 	},
    143  1.23  riastrad 	{			/* fe80::2 */
    144  1.22  riastrad 		.af = AF_INET6,
    145  1.22  riastrad 		.src = "fe80::adec:c91c:d116:7592", .dst = "fe80::2",
    146  1.22  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
    147  1.22  riastrad 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
    148  1.22  riastrad 	},
    149  1.23  riastrad 	{			/* fe80::2000:0:0/96 */
    150  1.23  riastrad 		.af = AF_INET6,
    151  1.23  riastrad 		.src = "fe80::adec:c91c:d116:7592", .dst = "fe80::2000:8000:0",
    152  1.23  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
    153  1.23  riastrad 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
    154  1.23  riastrad 	},
    155  1.23  riastrad 	{			/* fe80::2000:0:0/96, one bit off */
    156  1.23  riastrad 		.af = AF_INET6,
    157  1.23  riastrad 		.src = "fe80::adec:c91c:d116:7592", .dst = "fe80::2001:8000:0",
    158  1.23  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
    159  1.23  riastrad 		.stateful_ret = RESULT_BLOCK,	.ret = RESULT_BLOCK
    160  1.23  riastrad 	},
    161  1.23  riastrad 	{			/* fe80::3 */
    162  1.23  riastrad 		.af = AF_INET6,
    163  1.23  riastrad 		.src = "fe80::adec:c91c:d116:7592", .dst = "fe80::3",
    164  1.23  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
    165  1.23  riastrad 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
    166  1.23  riastrad 	},
    167  1.23  riastrad 	{			/* fe80::3000:0:0/97 */
    168  1.23  riastrad 		.af = AF_INET6,
    169  1.23  riastrad 		.src = "fe80::adec:c91c:d116:7592", .dst = "fe80::3000:7fff:0",
    170  1.23  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
    171  1.23  riastrad 		.stateful_ret = RESULT_PASS,	.ret = RESULT_PASS
    172  1.23  riastrad 	},
    173  1.23  riastrad 	{			/* fe80::3000:0:0/97, one bit off */
    174  1.23  riastrad 		.af = AF_INET6,
    175  1.23  riastrad 		.src = "fe80::adec:c91c:d116:7592", .dst = "fe80::3000:ffff:0",
    176  1.23  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
    177  1.23  riastrad 		.stateful_ret = RESULT_BLOCK,	.ret = RESULT_BLOCK
    178  1.23  riastrad 	},
    179  1.22  riastrad 	{
    180  1.22  riastrad 		.af = AF_INET6,
    181  1.23  riastrad 		.src = "fe80::adec:c91c:d116:7592", .dst = "fe80::4",
    182  1.22  riastrad 		.ifname = IFNAME_INT,		.di = PFIL_IN,
    183  1.21  riastrad 		.stateful_ret = RESULT_BLOCK,	.ret = RESULT_BLOCK
    184  1.21  riastrad 	},
    185  1.21  riastrad 
    186   1.1     rmind 	/* Block. */
    187  1.20  riastrad 	{
    188  1.20  riastrad 		.af = AF_INET,
    189  1.20  riastrad 		.src = "10.1.1.1",		.dst = "10.1.1.4",
    190   1.1     rmind 		.ifname = IFNAME_INT,		.di = PFIL_OUT,
    191   1.1     rmind 		.stateful_ret = RESULT_BLOCK,	.ret = RESULT_BLOCK
    192   1.1     rmind 	},
    193  1.25   mlelstv 	{	.src = "10.1.1.3",		.dst = "10.1.1.1",
    194  1.25   mlelstv 		.ifname = IFNAME_INT,		.di = PFIL_IN,
    195  1.25   mlelstv 		.stateful_ret = RESULT_BLOCK,	.ret = RESULT_BLOCK
    196  1.25   mlelstv 	},
    197   1.1     rmind 
    198   1.1     rmind };
    199   1.1     rmind 
    200   1.1     rmind static int
    201  1.17     rmind run_raw_testcase(unsigned i)
    202   1.1     rmind {
    203  1.17     rmind 	const struct test_case *t = &test_cases[i];
    204  1.13  christos 	npf_t *npf = npf_getkernctx();
    205  1.17     rmind 	npf_cache_t *npc;
    206  1.17     rmind 	struct mbuf *m;
    207   1.1     rmind 	npf_rule_t *rl;
    208  1.17     rmind 	int slock, error;
    209  1.17     rmind 
    210  1.20  riastrad 	m = mbuf_get_pkt(t->af, IPPROTO_UDP, t->src, t->dst, 9000, 9000);
    211  1.24       joe 	npc = get_cached_pkt(m, t->ifname, NPF_RULE_LAYER_3);
    212   1.1     rmind 
    213  1.19     rmind 	slock = npf_config_read_enter(npf);
    214  1.24       joe 	rl = npf_ruleset_inspect(npc, npf_config_ruleset(npf), t->di, NPF_RULE_LAYER_3);
    215   1.1     rmind 	if (rl) {
    216  1.17     rmind 		npf_match_info_t mi;
    217  1.14  christos 		error = npf_rule_conclude(rl, &mi);
    218   1.1     rmind 	} else {
    219   1.1     rmind 		error = ENOENT;
    220   1.1     rmind 	}
    221  1.19     rmind 	npf_config_read_exit(npf, slock);
    222  1.17     rmind 
    223  1.17     rmind 	put_cached_pkt(npc);
    224   1.1     rmind 	return error;
    225   1.1     rmind }
    226   1.1     rmind 
    227   1.4     rmind static int
    228  1.17     rmind run_handler_testcase(unsigned i)
    229   1.4     rmind {
    230   1.8     rmind 	const struct test_case *t = &test_cases[i];
    231  1.13  christos 	ifnet_t *ifp = npf_test_getif(t->ifname);
    232  1.17     rmind 	npf_t *npf = npf_getkernctx();
    233  1.17     rmind 	struct mbuf *m;
    234   1.4     rmind 	int error;
    235   1.4     rmind 
    236  1.20  riastrad 	m = mbuf_get_pkt(t->af, IPPROTO_UDP, t->src, t->dst, 9000, 9000);
    237  1.18     rmind 	error = npfk_packet_handler(npf, &m, ifp, t->di);
    238  1.17     rmind 	if (m) {
    239  1.17     rmind 		m_freem(m);
    240  1.17     rmind 	}
    241   1.4     rmind 	return error;
    242   1.4     rmind }
    243   1.4     rmind 
    244   1.4     rmind static npf_rule_t *
    245   1.4     rmind npf_blockall_rule(void)
    246   1.4     rmind {
    247  1.13  christos 	npf_t *npf = npf_getkernctx();
    248  1.15     rmind 	nvlist_t *rule = nvlist_create(0);
    249  1.17     rmind 	npf_rule_t *rl;
    250   1.4     rmind 
    251  1.15     rmind 	nvlist_add_number(rule, "attr",
    252   1.7     rmind 	    NPF_RULE_IN | NPF_RULE_OUT | NPF_RULE_DYNAMIC);
    253  1.17     rmind 	rl = npf_rule_alloc(npf, rule);
    254  1.17     rmind 	nvlist_destroy(rule);
    255  1.17     rmind 	return rl;
    256   1.4     rmind }
    257   1.4     rmind 
    258  1.17     rmind static bool
    259  1.17     rmind test_static(bool verbose)
    260   1.1     rmind {
    261   1.1     rmind 	for (unsigned i = 0; i < __arraycount(test_cases); i++) {
    262   1.1     rmind 		const struct test_case *t = &test_cases[i];
    263  1.17     rmind 		int error, serror;
    264   1.1     rmind 
    265  1.17     rmind 		if (npf_test_getif(t->ifname) == NULL) {
    266   1.1     rmind 			printf("Interface %s is not configured.\n", t->ifname);
    267   1.1     rmind 			return false;
    268   1.1     rmind 		}
    269   1.1     rmind 
    270  1.17     rmind 		error = run_raw_testcase(i);
    271  1.17     rmind 		serror = run_handler_testcase(i);
    272   1.1     rmind 
    273   1.1     rmind 		if (verbose) {
    274  1.16     rmind 			printf("rule test %d:\texpected %d (stateful) and %d\n"
    275  1.16     rmind 			    "\t\t-> returned %d and %d\n",
    276   1.1     rmind 			    i + 1, t->stateful_ret, t->ret, serror, error);
    277   1.1     rmind 		}
    278  1.17     rmind 		CHECK_TRUE(error == t->ret);
    279  1.17     rmind 		CHECK_TRUE(serror == t->stateful_ret)
    280   1.1     rmind 	}
    281  1.17     rmind 	return true;
    282  1.17     rmind }
    283  1.17     rmind 
    284  1.17     rmind static bool
    285  1.17     rmind test_dynamic(void)
    286  1.17     rmind {
    287  1.17     rmind 	npf_t *npf = npf_getkernctx();
    288  1.17     rmind 	npf_ruleset_t *rlset;
    289  1.17     rmind 	npf_rule_t *rl;
    290  1.17     rmind 	uint64_t id;
    291  1.17     rmind 	int error;
    292   1.4     rmind 
    293   1.8     rmind 	/*
    294   1.8     rmind 	 * Test dynamic NPF rules.
    295   1.8     rmind 	 */
    296   1.8     rmind 
    297  1.17     rmind 	error = run_raw_testcase(0);
    298  1.16     rmind 	CHECK_TRUE(error == RESULT_PASS);
    299   1.4     rmind 
    300  1.13  christos 	npf_config_enter(npf);
    301  1.13  christos 	rlset = npf_config_ruleset(npf);
    302   1.4     rmind 
    303   1.4     rmind 	rl = npf_blockall_rule();
    304   1.4     rmind 	error = npf_ruleset_add(rlset, "test-rules", rl);
    305  1.16     rmind 	CHECK_TRUE(error == 0);
    306   1.4     rmind 
    307  1.17     rmind 	error = run_raw_testcase(0);
    308  1.16     rmind 	CHECK_TRUE(error == RESULT_BLOCK);
    309   1.4     rmind 
    310   1.6     rmind 	id = npf_rule_getid(rl);
    311   1.6     rmind 	error = npf_ruleset_remove(rlset, "test-rules", id);
    312  1.16     rmind 	CHECK_TRUE(error == 0);
    313   1.4     rmind 
    314  1.13  christos 	npf_config_exit(npf);
    315   1.4     rmind 
    316  1.17     rmind 	error = run_raw_testcase(0);
    317  1.16     rmind 	CHECK_TRUE(error == RESULT_PASS);
    318   1.4     rmind 
    319  1.16     rmind 	return true;
    320   1.1     rmind }
    321  1.17     rmind 
    322  1.17     rmind bool
    323  1.17     rmind npf_rule_test(bool verbose)
    324  1.17     rmind {
    325  1.17     rmind 	bool ok;
    326  1.17     rmind 
    327  1.17     rmind 	ok = test_static(verbose);
    328  1.17     rmind 	CHECK_TRUE(ok);
    329  1.17     rmind 
    330  1.17     rmind 	ok = test_dynamic();
    331  1.17     rmind 	CHECK_TRUE(ok);
    332  1.17     rmind 
    333  1.17     rmind 	return true;
    334  1.17     rmind }
    335