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