npf_nat_test.c revision 1.5 1 1.5 rmind /* $NetBSD: npf_nat_test.c,v 1.5 2014/02/05 03:30:13 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*
4 1.1 rmind * NPF NAT 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 #define NPF_BINAT (NPF_NATIN | NPF_NATOUT)
18 1.1 rmind
19 1.5 rmind #define RANDOM_PORT 45600
20 1.5 rmind
21 1.1 rmind static const struct test_case {
22 1.1 rmind const char * src;
23 1.1 rmind in_port_t sport;
24 1.1 rmind const char * dst;
25 1.1 rmind in_port_t dport;
26 1.1 rmind int ttype;
27 1.1 rmind const char * ifname;
28 1.1 rmind int di;
29 1.1 rmind int ret;
30 1.1 rmind const char * taddr;
31 1.1 rmind in_port_t tport;
32 1.1 rmind } test_cases[] = {
33 1.1 rmind
34 1.1 rmind /*
35 1.1 rmind * Traditional NAPT (outbound NAT):
36 1.1 rmind * map $ext_if dynamic $local_net -> $pub_ip1
37 1.1 rmind */
38 1.1 rmind {
39 1.1 rmind LOCAL_IP1, 15000, REMOTE_IP1, 7000,
40 1.1 rmind NPF_NATOUT, IFNAME_EXT, PFIL_OUT,
41 1.5 rmind RESULT_PASS, PUB_IP1, RANDOM_PORT
42 1.1 rmind },
43 1.1 rmind {
44 1.1 rmind LOCAL_IP1, 15000, REMOTE_IP1, 7000,
45 1.1 rmind NPF_NATOUT, IFNAME_EXT, PFIL_OUT,
46 1.5 rmind RESULT_PASS, PUB_IP1, RANDOM_PORT
47 1.1 rmind },
48 1.1 rmind {
49 1.1 rmind LOCAL_IP1, 15000, REMOTE_IP1, 7000,
50 1.1 rmind NPF_NATOUT, IFNAME_EXT, PFIL_IN,
51 1.1 rmind RESULT_BLOCK, NULL, 0
52 1.1 rmind },
53 1.1 rmind {
54 1.1 rmind REMOTE_IP1, 7000, LOCAL_IP1, 15000,
55 1.1 rmind NPF_NATOUT, IFNAME_EXT, PFIL_IN,
56 1.1 rmind RESULT_BLOCK, NULL, 0
57 1.1 rmind },
58 1.1 rmind {
59 1.5 rmind REMOTE_IP1, 7000, PUB_IP1, RANDOM_PORT,
60 1.1 rmind NPF_NATOUT, IFNAME_INT, PFIL_IN,
61 1.1 rmind RESULT_BLOCK, NULL, 0
62 1.1 rmind },
63 1.1 rmind {
64 1.5 rmind REMOTE_IP1, 7000, PUB_IP1, RANDOM_PORT,
65 1.1 rmind NPF_NATOUT, IFNAME_EXT, PFIL_IN,
66 1.1 rmind RESULT_PASS, LOCAL_IP1, 15000
67 1.1 rmind },
68 1.1 rmind
69 1.1 rmind /*
70 1.1 rmind * NAT redirect (inbound NAT):
71 1.1 rmind * map $ext_if dynamic $local_ip1 port 8000 <- $pub_ip1 port 8000
72 1.1 rmind */
73 1.1 rmind {
74 1.1 rmind REMOTE_IP2, 16000, PUB_IP1, 8000,
75 1.1 rmind NPF_NATIN, IFNAME_EXT, PFIL_IN,
76 1.1 rmind RESULT_PASS, LOCAL_IP1, 6000
77 1.1 rmind },
78 1.1 rmind {
79 1.1 rmind LOCAL_IP1, 6000, REMOTE_IP2, 16000,
80 1.1 rmind NPF_NATIN, IFNAME_EXT, PFIL_OUT,
81 1.1 rmind RESULT_PASS, PUB_IP1, 8000
82 1.1 rmind },
83 1.1 rmind
84 1.1 rmind /*
85 1.1 rmind * Bi-directional NAT (inbound + outbound NAT):
86 1.1 rmind * map $ext_if dynamic $local_ip2 <-> $pub_ip2
87 1.1 rmind */
88 1.1 rmind {
89 1.1 rmind REMOTE_IP2, 17000, PUB_IP2, 9000,
90 1.1 rmind NPF_BINAT, IFNAME_EXT, PFIL_IN,
91 1.1 rmind RESULT_PASS, LOCAL_IP2, 9000
92 1.1 rmind },
93 1.1 rmind {
94 1.1 rmind LOCAL_IP2, 9000, REMOTE_IP2, 17000,
95 1.1 rmind NPF_BINAT, IFNAME_EXT, PFIL_OUT,
96 1.1 rmind RESULT_PASS, PUB_IP2, 9000
97 1.1 rmind },
98 1.1 rmind {
99 1.1 rmind LOCAL_IP2, 18000, REMOTE_IP2, 9000,
100 1.1 rmind NPF_BINAT, IFNAME_EXT, PFIL_OUT,
101 1.1 rmind RESULT_PASS, PUB_IP2, 18000
102 1.1 rmind },
103 1.1 rmind {
104 1.1 rmind REMOTE_IP2, 9000, PUB_IP2, 18000,
105 1.1 rmind NPF_BINAT, IFNAME_EXT, PFIL_IN,
106 1.1 rmind RESULT_PASS, LOCAL_IP2, 18000
107 1.1 rmind },
108 1.1 rmind
109 1.1 rmind };
110 1.1 rmind
111 1.1 rmind static bool
112 1.1 rmind nmatch_addr(const char *saddr, const struct in_addr *addr2)
113 1.1 rmind {
114 1.1 rmind const in_addr_t addr1 = inet_addr(saddr);
115 1.1 rmind return memcmp(&addr1, &addr2->s_addr, sizeof(in_addr_t)) != 0;
116 1.1 rmind }
117 1.1 rmind
118 1.1 rmind static bool
119 1.2 rmind checkresult(bool verbose, unsigned i, struct mbuf *m, ifnet_t *ifp, int error)
120 1.1 rmind {
121 1.1 rmind const struct test_case *t = &test_cases[i];
122 1.1 rmind npf_cache_t npc = { .npc_info = 0 };
123 1.2 rmind nbuf_t nbuf;
124 1.1 rmind
125 1.1 rmind if (verbose) {
126 1.1 rmind printf("packet %d (expected %d ret %d)\n", i+1, t->ret, error);
127 1.1 rmind }
128 1.1 rmind if (error) {
129 1.1 rmind return error == t->ret;
130 1.1 rmind }
131 1.2 rmind
132 1.2 rmind nbuf_init(&nbuf, m, ifp);
133 1.2 rmind if (!npf_cache_all(&npc, &nbuf)) {
134 1.1 rmind printf("error: could not fetch the packet data");
135 1.1 rmind return false;
136 1.1 rmind }
137 1.1 rmind
138 1.2 rmind const struct ip *ip = npc.npc_ip.v4;
139 1.2 rmind const struct udphdr *uh = npc.npc_l4.udp;
140 1.1 rmind
141 1.1 rmind if (verbose) {
142 1.1 rmind printf("\tpost-translation: src %s (%d)",
143 1.1 rmind inet_ntoa(ip->ip_src), ntohs(uh->uh_sport));
144 1.1 rmind printf(" dst %s (%d)\n",
145 1.1 rmind inet_ntoa(ip->ip_dst), ntohs(uh->uh_dport));
146 1.1 rmind }
147 1.3 rmind if (error != t->ret) {
148 1.3 rmind return false;
149 1.3 rmind }
150 1.1 rmind
151 1.1 rmind const bool forw = t->di == PFIL_OUT;
152 1.1 rmind const char *saddr = forw ? t->taddr : t->src;
153 1.1 rmind const char *daddr = forw ? t->dst : t->taddr;
154 1.1 rmind in_addr_t sport = forw ? t->tport : t->sport;
155 1.1 rmind in_addr_t dport = forw ? t->dport : t->tport;
156 1.1 rmind
157 1.1 rmind bool defect = false;
158 1.1 rmind defect |= nmatch_addr(saddr, &ip->ip_src);
159 1.1 rmind defect |= sport != ntohs(uh->uh_sport);
160 1.1 rmind defect |= nmatch_addr(daddr, &ip->ip_dst);
161 1.1 rmind defect |= dport != ntohs(uh->uh_dport);
162 1.3 rmind return !defect;
163 1.1 rmind }
164 1.1 rmind
165 1.1 rmind static struct mbuf *
166 1.1 rmind fill_packet(const struct test_case *t)
167 1.1 rmind {
168 1.1 rmind struct mbuf *m;
169 1.1 rmind struct ip *ip;
170 1.1 rmind struct udphdr *uh;
171 1.1 rmind
172 1.1 rmind m = mbuf_construct(IPPROTO_UDP);
173 1.1 rmind uh = mbuf_return_hdrs(m, false, &ip);
174 1.1 rmind ip->ip_src.s_addr = inet_addr(t->src);
175 1.1 rmind ip->ip_dst.s_addr = inet_addr(t->dst);
176 1.1 rmind uh->uh_sport = htons(t->sport);
177 1.1 rmind uh->uh_dport = htons(t->dport);
178 1.1 rmind return m;
179 1.1 rmind }
180 1.1 rmind
181 1.1 rmind bool
182 1.1 rmind npf_nat_test(bool verbose)
183 1.1 rmind {
184 1.1 rmind for (unsigned i = 0; i < __arraycount(test_cases); i++) {
185 1.1 rmind const struct test_case *t = &test_cases[i];
186 1.1 rmind ifnet_t *ifp = ifunit(t->ifname);
187 1.1 rmind struct mbuf *m = fill_packet(t);
188 1.1 rmind int error;
189 1.1 rmind bool ret;
190 1.1 rmind
191 1.1 rmind if (ifp == NULL) {
192 1.1 rmind printf("Interface %s is not configured.\n", t->ifname);
193 1.1 rmind return false;
194 1.1 rmind }
195 1.1 rmind error = npf_packet_handler(NULL, &m, ifp, t->di);
196 1.2 rmind ret = checkresult(verbose, i, m, ifp, error);
197 1.1 rmind if (m) {
198 1.1 rmind m_freem(m);
199 1.1 rmind }
200 1.1 rmind if (!ret) {
201 1.1 rmind return false;
202 1.1 rmind }
203 1.1 rmind }
204 1.1 rmind return true;
205 1.1 rmind }
206