npf_ext_test.c revision 1.2 1 1.1 joe
2 1.2 joe /*-
3 1.2 joe * Copyright (c) 2026 The NetBSD Foundation, Inc.
4 1.2 joe * All rights reserved.
5 1.2 joe *
6 1.2 joe * This code is derived from software contributed to The NetBSD Foundation
7 1.2 joe * by Emmanuel Nyarko.
8 1.2 joe *
9 1.2 joe * Redistribution and use in source and binary forms, with or without
10 1.2 joe * modification, are permitted provided that the following conditions
11 1.2 joe * are met:
12 1.2 joe * 1. Redistributions of source code must retain the above copyright
13 1.2 joe * notice, this list of conditions and the following disclaimer.
14 1.2 joe * 2. Redistributions in binary form must reproduce the above copyright
15 1.2 joe * notice, this list of conditions and the following disclaimer in the
16 1.2 joe * documentation and/or other materials provided with the distribution.
17 1.2 joe *
18 1.2 joe * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.2 joe * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.2 joe * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.2 joe * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.2 joe * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.2 joe * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.2 joe * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.2 joe * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.2 joe * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.2 joe * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.2 joe * POSSIBILITY OF SUCH DAMAGE.
29 1.2 joe */
30 1.2 joe
31 1.2 joe /*
32 1.2 joe * NPF tests of extension.
33 1.2 joe */
34 1.1 joe
35 1.1 joe #ifdef _KERNEL
36 1.1 joe #include <sys/types.h>
37 1.1 joe #endif
38 1.1 joe
39 1.1 joe #include "npf_impl.h"
40 1.1 joe #include "npf_test.h"
41 1.1 joe
42 1.1 joe static const struct test_case {
43 1.1 joe const char * src;
44 1.1 joe in_port_t sport;
45 1.1 joe const char * dst;
46 1.1 joe in_port_t dport;
47 1.1 joe const char * ifname;
48 1.1 joe int di;
49 1.1 joe int af;
50 1.1 joe } test_cases[] = {
51 1.1 joe {
52 1.1 joe LOCAL_IP60, 15000, LOCAL_IP70, 7000,
53 1.2 joe IFNAME_EXT, PFIL_OUT, AF_INET
54 1.1 joe },
55 1.1 joe {
56 1.1 joe LOCAL_IP60, 15040, LOCAL_IP70, 6000,
57 1.2 joe IFNAME_EXT, PFIL_OUT, AF_INET
58 1.1 joe },
59 1.1 joe
60 1.1 joe };
61 1.1 joe
62 1.1 joe #define TCP_OPT_LEN 4
63 1.1 joe static struct mbuf *
64 1.1 joe mbuf_get_pkt_with_option(int af, int proto, const char *src, const char *dst,
65 1.1 joe int sport, int dport)
66 1.1 joe {
67 1.1 joe struct mbuf *m;
68 1.1 joe struct ip *ip;
69 1.1 joe struct tcphdr *th;
70 1.1 joe void *ipsrc, *ipdst;
71 1.1 joe int size = 0;
72 1.1 joe uint8_t *opt;
73 1.1 joe uint16_t mss = 1400;
74 1.1 joe
75 1.1 joe m = m_gethdr(M_WAITOK, MT_HEADER);
76 1.1 joe ip = mtod(m, struct ip *);
77 1.1 joe
78 1.1 joe ip->ip_v = IPVERSION;
79 1.1 joe ip->ip_hl = sizeof(struct ip) >> 2;
80 1.1 joe ip->ip_off = 0;
81 1.1 joe ip->ip_ttl = 64;
82 1.1 joe ip->ip_p = proto;
83 1.1 joe
84 1.1 joe ipsrc = &ip->ip_src.s_addr;
85 1.1 joe ipdst = &ip->ip_dst.s_addr;
86 1.1 joe
87 1.1 joe npf_inet_pton(af, src, ipsrc);
88 1.1 joe npf_inet_pton(af, dst, ipdst);
89 1.1 joe
90 1.1 joe th = mbuf_return_hdrs(m, false, &ip);
91 1.1 joe size = sizeof(struct ip) + sizeof(struct tcphdr) + TCP_OPT_LEN;
92 1.1 joe th->th_flags |= TH_SYN;
93 1.1 joe th->th_off = (sizeof(struct tcphdr) + TCP_OPT_LEN) >> 2;
94 1.1 joe th->th_sport = htons(sport);
95 1.1 joe th->th_dport = htons(dport);
96 1.1 joe
97 1.1 joe opt = (uint8_t *)th + sizeof(struct tcphdr);
98 1.1 joe opt[0] = TCPOPT_MAXSEG; /* Kind = 2 */
99 1.1 joe opt[1] = TCPOLEN_MAXSEG; /* Length = 4 */
100 1.1 joe opt[2] = (mss >> 8) & 0xff; /* 0x05 */
101 1.1 joe opt[3] = mss & 0xff; /* 0x29 */
102 1.1 joe
103 1.1 joe ip->ip_len = htons(size);
104 1.1 joe
105 1.1 joe m->m_pkthdr.len = size;
106 1.1 joe m->m_len = size;
107 1.1 joe m->m_next = NULL;
108 1.1 joe return m;
109 1.1 joe }
110 1.1 joe
111 1.1 joe static int
112 1.1 joe run_raw_testcase(npf_cache_t *npc, unsigned i, bool verbose)
113 1.1 joe {
114 1.1 joe const struct test_case *t = &test_cases[i];
115 1.1 joe npf_t *npf = npf_getkernctx();
116 1.1 joe int slock, error;
117 1.1 joe npf_rproc_t *rp;
118 1.1 joe npf_rule_t *rl;
119 1.1 joe npf_match_info_t mi;
120 1.1 joe int decision = 1;
121 1.1 joe
122 1.1 joe slock = npf_config_read_enter(npf);
123 1.1 joe rl = npf_ruleset_inspect(npc, npf_config_ruleset(npf), t->di, NPF_RULE_LAYER_3);
124 1.1 joe if (rl) {
125 1.1 joe error = npf_rule_conclude(rl, &mi);
126 1.1 joe } else {
127 1.1 joe error = ENOENT;
128 1.1 joe }
129 1.1 joe npf_config_read_exit(npf, slock);
130 1.1 joe
131 1.2 joe if (error)
132 1.2 joe return error;
133 1.2 joe
134 1.1 joe rp = npf_rule_getrproc(rl);
135 1.1 joe assert(rp != NULL);
136 1.1 joe
137 1.1 joe error = npf_rproc_run(npc, rp, &mi, &decision);
138 1.1 joe
139 1.1 joe if (error == 1) {
140 1.1 joe npf_rproc_release(rp);
141 1.1 joe }
142 1.1 joe
143 1.1 joe return error;
144 1.1 joe }
145 1.1 joe
146 1.1 joe static void
147 1.1 joe npf_get_normalize_params(npf_cache_t *npc, uint16_t *ip_ttl, uint8_t *ip_id, int *df, uint16_t *maxmss)
148 1.1 joe {
149 1.1 joe int wscale;
150 1.1 joe struct ip *ip = npc->npc_ip.v4;
151 1.1 joe *ip_id = ip->ip_id;
152 1.1 joe *ip_ttl = ip->ip_ttl;
153 1.1 joe *df = ip->ip_off & IP_DF;
154 1.1 joe npf_fetch_tcpopts(npc, maxmss, &wscale);
155 1.1 joe }
156 1.1 joe
157 1.1 joe bool
158 1.1 joe npf_procedure_test(bool verbose)
159 1.1 joe {
160 1.1 joe int ret;
161 1.1 joe for (unsigned i = 0; i < __arraycount(test_cases); i++) {
162 1.1 joe const struct test_case *t = &test_cases[i];
163 1.1 joe npf_cache_t *npc;
164 1.1 joe struct mbuf *m;
165 1.1 joe uint16_t ip_ttl;
166 1.1 joe uint8_t ip_id;
167 1.1 joe int df;
168 1.1 joe uint16_t maxmss;
169 1.1 joe
170 1.1 joe if (npf_test_getif(t->ifname) == NULL) {
171 1.1 joe printf("Interface %s is not configured.\n", t->ifname);
172 1.1 joe return false;
173 1.1 joe }
174 1.1 joe
175 1.1 joe m = mbuf_get_pkt_with_option(t->af, IPPROTO_TCP, t->src, t->dst, 9000, 9000);
176 1.1 joe npc = get_cached_pkt(m, t->ifname, NPF_RULE_LAYER_3);
177 1.1 joe
178 1.1 joe ret = run_raw_testcase(npc, i, verbose);
179 1.1 joe if (verbose) {
180 1.1 joe printf("%s: expected return in raw testcase 1, returned %d\n", __func__, ret);
181 1.1 joe }
182 1.1 joe CHECK_TRUE(ret == 1);
183 1.1 joe
184 1.1 joe npf_get_normalize_params(npc, &ip_ttl, &ip_id, &df, &maxmss);
185 1.1 joe maxmss = ntohs(maxmss);
186 1.1 joe
187 1.1 joe if (verbose) {
188 1.1 joe printf("Normalization:\texpected 164 ttl but got %u\n", ip_ttl);
189 1.1 joe printf("Normalization:\texpected 1321 maxmss but got %u\n", maxmss);
190 1.1 joe }
191 1.1 joe CHECK_TRUE((df & IP_DF) == 0);
192 1.1 joe CHECK_TRUE(maxmss == 1321);
193 1.1 joe CHECK_TRUE(ip_ttl == 164);
194 1.1 joe put_cached_pkt(npc);
195 1.1 joe }
196 1.1 joe
197 1.1 joe return true;
198 1.1 joe }
199