1 /* 2 * NPF NAT tests. 3 * 4 * Public Domain. 5 */ 6 7 #ifdef _KERNEL 8 #include <sys/types.h> 9 #endif 10 11 #include "npf_impl.h" 12 #include "npf_test.h" 13 14 #define RESULT_PASS 0 15 #define RESULT_BLOCK ENETUNREACH 16 17 #define NPF_BINAT (NPF_NATIN | NPF_NATOUT) 18 19 #define RANDOM_PORT 18791 20 21 static const struct test_case { 22 const char * src; 23 in_port_t sport; 24 const char * dst; 25 in_port_t dport; 26 int ttype; 27 const char * ifname; 28 int di; 29 int ret; 30 int af; 31 const char * taddr; 32 in_port_t tport; 33 } test_cases[] = { 34 35 /* 36 * NAT using /32 for IPV4 should be treated as a single IP 37 * with no mask. 38 */ 39 { 40 LOCAL_IP7, 15000, REMOTE_IP1, 7000, 41 NPF_NATOUT, IFNAME_EXT, PFIL_OUT, 42 RESULT_PASS, AF_INET, PUB_IP4, 15000 43 }, 44 /* 45 * Traditional NAPT (outbound NAT): 46 * map $ext_if dynamic $local_net -> $pub_ip1 47 */ 48 { 49 LOCAL_IP1, 15000, REMOTE_IP1, 7000, 50 NPF_NATOUT, IFNAME_EXT, PFIL_OUT, 51 RESULT_PASS, AF_INET, PUB_IP1, RANDOM_PORT 52 }, 53 { 54 LOCAL_IP1, 15000, REMOTE_IP1, 7000, 55 NPF_NATOUT, IFNAME_EXT, PFIL_OUT, 56 RESULT_PASS, AF_INET, PUB_IP1, RANDOM_PORT 57 }, 58 { 59 LOCAL_IP1, 15000, REMOTE_IP1, 7000, 60 NPF_NATOUT, IFNAME_EXT, PFIL_IN, 61 RESULT_BLOCK, AF_INET, NULL, 0 62 }, 63 { 64 REMOTE_IP1, 7000, LOCAL_IP1, 15000, 65 NPF_NATOUT, IFNAME_EXT, PFIL_IN, 66 RESULT_BLOCK, AF_INET, NULL, 0 67 }, 68 { 69 REMOTE_IP1, 7000, PUB_IP1, RANDOM_PORT, 70 NPF_NATOUT, IFNAME_INT, PFIL_IN, 71 RESULT_BLOCK, AF_INET, NULL, 0 72 }, 73 { 74 REMOTE_IP1, 7000, PUB_IP1, RANDOM_PORT, 75 NPF_NATOUT, IFNAME_EXT, PFIL_IN, 76 RESULT_PASS, AF_INET, LOCAL_IP1, 15000 77 }, 78 79 /* 80 * NAT redirect (inbound NAT): 81 * map $ext_if dynamic $local_ip1 port 6000 <- $pub_ip1 port 8000 82 */ 83 { 84 REMOTE_IP2, 16000, PUB_IP1, 8000, 85 NPF_NATIN, IFNAME_EXT, PFIL_IN, 86 RESULT_PASS, AF_INET, LOCAL_IP1, 6000 87 }, 88 { 89 LOCAL_IP1, 6000, REMOTE_IP2, 16000, 90 NPF_NATIN, IFNAME_EXT, PFIL_OUT, 91 RESULT_PASS, AF_INET, PUB_IP1, 8000 92 }, 93 94 /* 95 * Bi-directional NAT (inbound + outbound NAT): 96 * map $ext_if dynamic $local_ip2 <-> $pub_ip2 97 */ 98 { 99 REMOTE_IP2, 17000, PUB_IP2, 9000, 100 NPF_BINAT, IFNAME_EXT, PFIL_IN, 101 RESULT_PASS, AF_INET, LOCAL_IP2, 9000 102 }, 103 { 104 LOCAL_IP2, 9000, REMOTE_IP2, 17000, 105 NPF_BINAT, IFNAME_EXT, PFIL_OUT, 106 RESULT_PASS, AF_INET, PUB_IP2, 9000 107 }, 108 { 109 LOCAL_IP2, 18000, REMOTE_IP2, 9000, 110 NPF_BINAT, IFNAME_EXT, PFIL_OUT, 111 RESULT_PASS, AF_INET, PUB_IP2, 18000 112 }, 113 { 114 REMOTE_IP2, 9000, PUB_IP2, 18000, 115 NPF_BINAT, IFNAME_EXT, PFIL_IN, 116 RESULT_PASS, AF_INET, LOCAL_IP2, 18000 117 }, 118 119 /* 120 * Static NAT: plain translation both ways. 121 * map $ext_if static $local_ip3 <-> $pub_ip3 122 */ 123 { 124 LOCAL_IP3, 19000, REMOTE_IP3, 10000, 125 NPF_BINAT, IFNAME_EXT, PFIL_OUT, 126 RESULT_PASS, AF_INET, PUB_IP3, 19000 127 }, 128 { 129 REMOTE_IP3, 10000, PUB_IP3, 19000, 130 NPF_BINAT, IFNAME_EXT, PFIL_IN, 131 RESULT_PASS, AF_INET, LOCAL_IP3, 19000 132 }, 133 134 /* 135 * NETMAP case: 136 * map $ext_if static algo netmap $net_a <-> $net_b 137 */ 138 { 139 NET_A_IP1, 12345, REMOTE_IP4, 12345, 140 NPF_BINAT, IFNAME_EXT, PFIL_OUT, 141 RESULT_PASS, AF_INET, NET_B_IP1, 12345 142 }, 143 144 /* 145 * NPTv6 case: 146 * map $ext_if static algo npt66 $net6_inner <-> $net6_outer 147 */ 148 { 149 LOCAL_IP6, 1000, REMOTE_IP6, 1001, 150 NPF_BINAT, IFNAME_EXT, PFIL_OUT, 151 RESULT_PASS, AF_INET6, EXPECTED_IP6, 1000 152 }, 153 { 154 REMOTE_IP6, 1001, EXPECTED_IP6, 1000, 155 NPF_BINAT, IFNAME_EXT, PFIL_IN, 156 RESULT_PASS, AF_INET6, LOCAL_IP6, 1000 157 }, 158 159 }; 160 161 static bool 162 match_addr(int af, const char *saddr, const npf_addr_t *addr2) 163 { 164 npf_addr_t addr1; 165 size_t len; 166 167 npf_inet_pton(af, saddr, &addr1); 168 len = af == AF_INET ? sizeof(struct in_addr) : sizeof(struct in6_addr); 169 return memcmp(&addr1, addr2, len) == 0; 170 } 171 172 static bool 173 checkresult(bool verbose, unsigned i, struct mbuf *m, ifnet_t *ifp, int error) 174 { 175 const struct test_case *t = &test_cases[i]; 176 const int af = t->af; 177 npf_cache_t npc; 178 nbuf_t nbuf; 179 180 if (verbose) { 181 printf("packet %d (expected %d ret %d)\n", i+1, t->ret, error); 182 } 183 if (error) { 184 return error == t->ret; 185 } 186 187 nbuf_init(npf_getkernctx(), &nbuf, m, ifp); 188 memset(&npc, 0, sizeof(npf_cache_t)); 189 npc.npc_ctx = npf_getkernctx(); 190 npc.npc_nbuf = &nbuf; 191 192 if (!npf_cache_all(&npc)) { 193 printf("error: could not fetch the packet data"); 194 return false; 195 } 196 197 const struct udphdr *uh = npc.npc_l4.udp; 198 199 if (verbose) { 200 char sbuf[64], dbuf[64]; 201 202 npf_inet_ntop(af, npc.npc_ips[NPF_SRC], sbuf, sizeof(sbuf)); 203 npf_inet_ntop(af, npc.npc_ips[NPF_DST], dbuf, sizeof(dbuf)); 204 205 printf("\tpost-translation: "); 206 printf("src %s (%d) ", sbuf, ntohs(uh->uh_sport)); 207 printf("dst %s (%d)\n", dbuf, ntohs(uh->uh_dport)); 208 } 209 if (error != t->ret) { 210 return false; 211 } 212 213 const bool forw = t->di == PFIL_OUT; 214 const char *saddr = forw ? t->taddr : t->src; 215 const char *daddr = forw ? t->dst : t->taddr; 216 in_addr_t sport = forw ? t->tport : t->sport; 217 in_addr_t dport = forw ? t->dport : t->tport; 218 219 CHECK_TRUE(match_addr(af, saddr, npc.npc_ips[NPF_SRC])); 220 CHECK_TRUE(sport == ntohs(uh->uh_sport)); 221 CHECK_TRUE(match_addr(af, daddr, npc.npc_ips[NPF_DST])); 222 CHECK_TRUE(dport == ntohs(uh->uh_dport)); 223 224 return true; 225 } 226 227 bool 228 npf_nat_test(bool verbose) 229 { 230 npf_t *npf = npf_getkernctx(); 231 232 for (unsigned i = 0; i < __arraycount(test_cases); i++) { 233 const struct test_case *t = &test_cases[i]; 234 ifnet_t *ifp = npf_test_getif(t->ifname); 235 struct mbuf *m; 236 int error; 237 bool ret; 238 239 if (ifp == NULL) { 240 printf("Interface %s is not configured.\n", t->ifname); 241 return false; 242 } 243 m = mbuf_get_pkt(t->af, IPPROTO_UDP, 244 t->src, t->dst, t->sport, t->dport); 245 error = npfk_packet_handler(npf, &m, ifp, t->di); 246 ret = checkresult(verbose, i, m, ifp, error); 247 if (m) { 248 m_freem(m); 249 } 250 CHECK_TRUE(ret); 251 } 252 return true; 253 } 254