IpTunnel_test.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "benc/String.h"
  16. #include "benc/Dict.h"
  17. #include "benc/serialization/standard/BencMessageWriter.h"
  18. #include "memory/Allocator.h"
  19. #include "interface/tuntap/TUNMessageType.h"
  20. #include "util/log/Log.h"
  21. #include "util/log/FileWriterLog.h"
  22. #include "util/events/EventBase.h"
  23. #include "crypto/random/Random.h"
  24. #include "crypto/Key.h"
  25. #include "tunnel/IpTunnel.h"
  26. #include "util/Bits.h"
  27. #include "util/Checksum.h"
  28. #include "util/CString.h"
  29. #include "util/Escape.h"
  30. #include "util/GlobalConfig.h"
  31. #include "wire/DataHeader.h"
  32. #include "wire/Message.h"
  33. #include "wire/Headers.h"
  34. #include "wire/Ethernet.h"
  35. struct Context
  36. {
  37. struct Allocator* alloc;
  38. struct Log* log;
  39. struct Random* rand;
  40. struct EventBase* base;
  41. uint8_t pubKey[32];
  42. uint8_t ipv6[16];
  43. // Per-request
  44. uint8_t sendingAddress[16];
  45. String* expectedResponse;
  46. int called;
  47. Identity
  48. };
  49. struct IfaceContext
  50. {
  51. struct Iface iface;
  52. struct Context* ctx;
  53. };
  54. static Iface_DEFUN responseWithIpCallback(struct Message* message, struct Iface* iface)
  55. {
  56. struct Context* ctx = Identity_check(((struct IfaceContext*)iface)->ctx);
  57. struct RouteHeader* rh = (struct RouteHeader*) message->msgbytes;
  58. Assert_true(!Bits_memcmp(ctx->ipv6, rh->ip6, 16));
  59. Assert_true(!Bits_memcmp(ctx->pubKey, rh->publicKey, 32));
  60. Er_assert(Message_eshift(message, -(RouteHeader_SIZE + DataHeader_SIZE)));
  61. struct Headers_IP6Header* ip = (struct Headers_IP6Header*) message->msgbytes;
  62. Assert_true(Headers_getIpVersion(ip) == 6);
  63. uint16_t length = Endian_bigEndianToHost16(ip->payloadLength_be);
  64. Assert_true(length + Headers_IP6Header_SIZE == Message_getLength(message));
  65. Assert_true(ip->nextHeader == 17);
  66. Assert_true(Bits_isZero(ip->sourceAddr, 32));
  67. Er_assert(Message_eshift(message, -Headers_IP6Header_SIZE));
  68. struct Headers_UDPHeader* uh = (struct Headers_UDPHeader*) message->msgbytes;
  69. Assert_true(!Checksum_udpIp6_be(ip->sourceAddr, message->msgbytes, length));
  70. Assert_true(uh->srcPort_be == 0);
  71. Assert_true(uh->destPort_be == 0);
  72. Assert_true(Endian_bigEndianToHost16(uh->length_be) + Headers_UDPHeader_SIZE == length);
  73. Er_assert(Message_eshift(message, -Headers_UDPHeader_SIZE));
  74. struct Allocator* alloc = Allocator_child(ctx->alloc);
  75. char* messageContent = Escape_getEscaped(message->msgbytes, Message_getLength(message), alloc);
  76. char* expectedContent =
  77. Escape_getEscaped(ctx->expectedResponse->bytes, ctx->expectedResponse->len, alloc);
  78. Log_debug(ctx->log, "Response: [%s]", messageContent);
  79. Log_debug(ctx->log, "Expected: [%s]", expectedContent);
  80. Allocator_free(alloc);
  81. // We can't check that the message is an exact match because the padding depends on the
  82. // alignment of the output but we can make sure the right content is there...
  83. // Message should start with "d0000" (with some number of zeros)
  84. Assert_true((int)ctx->expectedResponse->len == Message_getLength(message));
  85. Assert_true(!Bits_memcmp(message->msgbytes, ctx->expectedResponse->bytes, Message_getLength(message)));
  86. ctx->called |= 2;
  87. return NULL;
  88. }
  89. static Iface_DEFUN messageToTun(struct Message* msg, struct Iface* iface)
  90. {
  91. struct Context* ctx = Identity_check(((struct IfaceContext*)iface)->ctx);
  92. uint16_t type = Er_assert(TUNMessageType_pop(msg));
  93. if (type == Ethernet_TYPE_IP6) {
  94. struct Headers_IP6Header* ip = (struct Headers_IP6Header*) msg->msgbytes;
  95. Assert_true(Headers_getIpVersion(ip) == 6);
  96. Assert_true(!Bits_memcmp(ip->sourceAddr, ctx->sendingAddress, 16));
  97. Er_assert(Message_eshift(msg, -Headers_IP6Header_SIZE));
  98. ctx->called |= 4;
  99. } else if (type == Ethernet_TYPE_IP4) {
  100. struct Headers_IP4Header* ip = (struct Headers_IP4Header*) msg->msgbytes;
  101. Assert_true(Headers_getIpVersion(ip) == 4);
  102. Assert_true(!Bits_memcmp(ip->sourceAddr, ctx->sendingAddress, 4));
  103. Er_assert(Message_eshift(msg, -Headers_IP4Header_SIZE));
  104. ctx->called |= 1;
  105. } else {
  106. Assert_failure("unrecognized message type %u", (unsigned int)type);
  107. }
  108. Assert_true(Message_getLength(msg) == 12 && CString_strcmp(msg->msgbytes, "hello world") == 0);
  109. return NULL;
  110. }
  111. static void pushRouteDataHeaders(struct Context* ctx, struct Message* message)
  112. {
  113. Er_assert(Message_eshift(message, RouteHeader_SIZE + DataHeader_SIZE));
  114. struct RouteHeader* rh = (struct RouteHeader*) message->msgbytes;
  115. struct DataHeader* dh = (struct DataHeader*) &rh[1];
  116. Bits_memset(rh, 0, RouteHeader_SIZE + DataHeader_SIZE);
  117. Bits_memcpy(rh->ip6, ctx->ipv6, 16);
  118. Bits_memcpy(rh->publicKey, ctx->pubKey, 32);
  119. DataHeader_setContentType(dh, ContentType_IPTUN);
  120. }
  121. static bool trySend4(struct Allocator* alloc,
  122. uint32_t addr,
  123. struct Iface* sendTo,
  124. struct Context* ctx)
  125. {
  126. struct Message* msg4 = Message_new(0, 512, alloc);
  127. Er_assert(Message_epush(msg4, "hello world", 12));
  128. Er_assert(Message_epush(msg4, NULL, Headers_IP4Header_SIZE));
  129. struct Headers_IP4Header* iph = (struct Headers_IP4Header*) msg4->msgbytes;
  130. Headers_setIpVersion(iph);
  131. uint32_t addr_be = Endian_hostToBigEndian32(addr);
  132. Bits_memcpy(iph->sourceAddr, &addr_be, 4);
  133. Bits_memcpy(ctx->sendingAddress, &addr_be, 4);
  134. Bits_memcpy(iph->destAddr, ((uint8_t[]){ 11, 0, 0, 1 }), 4);
  135. pushRouteDataHeaders(ctx, msg4);
  136. Iface_send(sendTo, msg4);
  137. if (ctx->called == 1) {
  138. ctx->called = 0;
  139. return true;
  140. }
  141. Assert_true(ctx->called == 0);
  142. return false;
  143. }
  144. static bool trySend6(struct Allocator* alloc,
  145. uint64_t addrHigh,
  146. uint64_t addrLow,
  147. struct Iface* sendTo,
  148. struct Context* ctx)
  149. {
  150. struct Message* msg6 = Message_new(0, 512, alloc);
  151. Er_assert(Message_epush(msg6, "hello world", 12));
  152. Er_assert(Message_epush(msg6, NULL, Headers_IP6Header_SIZE));
  153. struct Headers_IP6Header* iph = (struct Headers_IP6Header*) msg6->msgbytes;
  154. Headers_setIpVersion(iph);
  155. uint64_t addrHigh_be = Endian_hostToBigEndian64(addrHigh);
  156. uint64_t addrLow_be = Endian_hostToBigEndian64(addrLow);
  157. Bits_memcpy(iph->sourceAddr, &addrHigh_be, 8);
  158. Bits_memcpy(&iph->sourceAddr[8], &addrLow_be, 8);
  159. Bits_memcpy(ctx->sendingAddress, iph->sourceAddr, 16);
  160. uint8_t destAddr[16] = { 20, 01 };
  161. destAddr[15] = 1;
  162. Bits_memcpy(iph->destinationAddr, destAddr, 16);
  163. pushRouteDataHeaders(ctx, msg6);
  164. Iface_send(sendTo, msg6);
  165. if (ctx->called == 4) {
  166. ctx->called = 0;
  167. return true;
  168. }
  169. Assert_true(ctx->called == 0);
  170. return false;
  171. }
  172. static String* getExpectedResponse(struct Sockaddr* sa4, int prefix4, int alloc4,
  173. struct Sockaddr* sa6, int prefix6, int alloc6,
  174. struct Allocator* allocator)
  175. {
  176. Assert_true(alloc6 >= prefix6);
  177. Assert_true(alloc4 >= prefix4);
  178. struct Allocator* alloc = Allocator_child(allocator);
  179. Dict* addresses = Dict_new(alloc);
  180. if (sa4) {
  181. uint8_t* addr = NULL;
  182. Assert_true(Sockaddr_getAddress(sa4, &addr) == 4);
  183. String* addrStr = String_newBinary(addr, 4, alloc);
  184. Dict_putString(addresses, String_new("ip4", alloc), addrStr, alloc);
  185. Dict_putInt(addresses, String_new("ip4Prefix", alloc), prefix4, alloc);
  186. Dict_putInt(addresses, String_new("ip4Alloc", alloc), alloc4, alloc);
  187. }
  188. if (sa6) {
  189. uint8_t* addr = NULL;
  190. Assert_true(Sockaddr_getAddress(sa6, &addr) == 16);
  191. String* addrStr = String_newBinary(addr, 16, alloc);
  192. Dict_putString(addresses, String_new("ip6", alloc), addrStr, alloc);
  193. Dict_putInt(addresses, String_new("ip6Prefix", alloc), prefix6, alloc);
  194. Dict_putInt(addresses, String_new("ip6Alloc", alloc), alloc6, alloc);
  195. }
  196. Dict* output = Dict_new(alloc);
  197. Dict_putDict(output, String_new("addresses", alloc), addresses, alloc);
  198. Dict_putString(output, String_new("txid", alloc), String_new("abcd", alloc), alloc);
  199. struct Message* msg = Message_new(0, 512, alloc);
  200. Er_assert(BencMessageWriter_write(output, msg));
  201. String* outStr = String_newBinary(msg->msgbytes, Message_getLength(msg), allocator);
  202. Allocator_free(alloc);
  203. return outStr;
  204. }
  205. static void testAddr(struct Context* ctx,
  206. char* addr4, int prefix4, int alloc4,
  207. char* addr6, int prefix6, int alloc6)
  208. {
  209. struct Allocator* alloc = Allocator_child(ctx->alloc);
  210. struct GlobalConfig* gc = GlobalConfig_new(alloc);
  211. struct IpTunnel* ipTun = IpTunnel_new(ctx->log, ctx->base, alloc, ctx->rand, NULL, gc);
  212. struct Sockaddr* sa4 = NULL;
  213. struct Sockaddr_storage ip6ToGive;
  214. struct Sockaddr_storage ip4ToGive;
  215. if (addr4) {
  216. Assert_true(!Sockaddr_parse(addr4, &ip4ToGive));
  217. sa4 = &ip4ToGive.addr;
  218. Assert_true(Sockaddr_getFamily(sa4) == Sockaddr_AF_INET);
  219. }
  220. struct Sockaddr* sa6 = NULL;
  221. if (addr6) {
  222. Assert_true(!Sockaddr_parse(addr6, &ip6ToGive));
  223. sa6 = &ip6ToGive.addr;
  224. Assert_true(Sockaddr_getFamily(sa6) == Sockaddr_AF_INET6);
  225. }
  226. IpTunnel_allowConnection(ctx->pubKey,
  227. sa6, prefix6, alloc6,
  228. sa4, prefix4, alloc4,
  229. ipTun);
  230. struct Message* msg = Message_new(64, 512, alloc);
  231. const char* requestForAddresses =
  232. "d"
  233. "1:q" "21:IpTunnel_getAddresses"
  234. "4:txid" "4:abcd"
  235. "e";
  236. CString_strcpy(msg->msgbytes, requestForAddresses);
  237. Er_assert(Message_truncate(msg, CString_strlen(requestForAddresses)));
  238. Er_assert(Message_epush(msg, NULL, Headers_UDPHeader_SIZE));
  239. struct Headers_UDPHeader* uh = (struct Headers_UDPHeader*) msg->msgbytes;
  240. uh->length_be = Endian_hostToBigEndian16(Message_getLength(msg) - Headers_UDPHeader_SIZE);
  241. uint16_t* checksum_be = &((struct Headers_UDPHeader*) msg->msgbytes)->checksum_be;
  242. *checksum_be = 0;
  243. uint32_t length = Message_getLength(msg);
  244. // Because of old reasons, we need to have at least an empty IPv6 header
  245. Er_assert(Message_epush(msg, NULL, Headers_IP6Header_SIZE));
  246. struct Headers_IP6Header* ip = (struct Headers_IP6Header*) msg->msgbytes;
  247. Headers_setIpVersion(ip);
  248. ip->payloadLength_be = Endian_hostToBigEndian16(Message_getLength(msg) - Headers_IP6Header_SIZE);
  249. ip->nextHeader = 17;
  250. *checksum_be = Checksum_udpIp6_be(ip->sourceAddr, (uint8_t*) uh, length);
  251. pushRouteDataHeaders(ctx, msg);
  252. struct IfaceContext* nodeIf = Allocator_calloc(alloc, sizeof(struct IfaceContext), 1);
  253. nodeIf->ctx = ctx;
  254. nodeIf->iface.send = responseWithIpCallback;
  255. struct IfaceContext* tunIf = Allocator_calloc(alloc, sizeof(struct IfaceContext), 1);
  256. tunIf->ctx = ctx;
  257. tunIf->iface.send = messageToTun;
  258. Iface_plumb(&nodeIf->iface, &ipTun->nodeInterface);
  259. Iface_plumb(&tunIf->iface, &ipTun->tunInterface);
  260. ctx->expectedResponse =
  261. getExpectedResponse(sa4, prefix4, alloc4, sa6, prefix6, alloc6, alloc);
  262. Iface_send(&nodeIf->iface, msg);
  263. Assert_true(ctx->called == 2);
  264. ctx->called = 0;
  265. if (sa4) {
  266. uint8_t* addrBytes = NULL;
  267. Assert_true(Sockaddr_getAddress(sa4, &addrBytes) == 4);
  268. uint32_t addr;
  269. Bits_memcpy(&addr, addrBytes, 4);
  270. addr = Endian_bigEndianToHost32(addr);
  271. // Send from the address specified
  272. Assert_true(trySend4(alloc, addr, &nodeIf->iface, ctx));
  273. if (alloc4 < 32) {
  274. // Send from another (random) address in the prefix
  275. uint32_t flip = Random_uint32(ctx->rand) >> alloc4;
  276. if (prefix4 != 32) {
  277. Assert_true(trySend4(alloc, addr ^ flip, &nodeIf->iface, ctx));
  278. } else {
  279. // If netSize is not specified, we do not allow multi-address
  280. Assert_true(!trySend4(alloc, addr ^ flip, &nodeIf->iface, ctx));
  281. }
  282. } else {
  283. Assert_true(!trySend4(alloc, addr ^ 1, &nodeIf->iface, ctx));
  284. }
  285. } else {
  286. uint32_t addr = Random_uint32(ctx->rand);
  287. Assert_true(!trySend4(alloc, addr, &nodeIf->iface, ctx));
  288. }
  289. if (sa6) {
  290. uint8_t* addrBytes = NULL;
  291. Assert_true(Sockaddr_getAddress(sa6, &addrBytes) == 16);
  292. uint64_t addrHigh;
  293. uint64_t addrLow;
  294. Bits_memcpy(&addrHigh, addrBytes, 8);
  295. Bits_memcpy(&addrLow, &addrBytes[8], 8);
  296. addrHigh = Endian_bigEndianToHost64(addrHigh);
  297. addrLow = Endian_bigEndianToHost64(addrLow);
  298. Assert_true(trySend6(alloc, addrHigh, addrLow, &nodeIf->iface, ctx));
  299. if (alloc6 < 128) {
  300. // Send from another (random) address in the prefix
  301. uint64_t flipHigh = Random_uint64(ctx->rand);
  302. uint64_t flipLow = Random_uint64(ctx->rand);
  303. if (alloc6 > 64) {
  304. flipHigh = flipHigh >> (alloc6 - 64);
  305. } else {
  306. flipHigh = 0;
  307. flipLow = flipLow >> alloc6;
  308. }
  309. if (prefix6 != 128) {
  310. Assert_true(trySend6(alloc,
  311. addrHigh ^ flipHigh,
  312. addrLow ^ flipLow,
  313. &nodeIf->iface,
  314. ctx) == true);
  315. } else {
  316. // If netSize is not specified, we do not allow multi-address
  317. Assert_true(trySend6(alloc,
  318. addrHigh ^ flipHigh,
  319. addrLow ^ flipLow,
  320. &nodeIf->iface,
  321. ctx) == false);
  322. }
  323. } else {
  324. Assert_true(!trySend6(alloc, addrHigh, addrLow ^ 1, &nodeIf->iface, ctx));
  325. }
  326. } else {
  327. uint64_t addr = Random_uint64(ctx->rand);
  328. Assert_true(!trySend6(alloc, 0, addr, &nodeIf->iface, ctx));
  329. }
  330. Allocator_free(alloc);
  331. }
  332. int main()
  333. {
  334. struct Allocator* alloc = Allocator_new(1<<20);
  335. struct EventBase* eb = EventBase_new(alloc);
  336. struct Log* logger = FileWriterLog_new(stdout, alloc);
  337. struct Random* rand = Random_new(alloc, logger, NULL);
  338. struct Context* ctx = Allocator_calloc(alloc, sizeof(struct Context), 1);
  339. uint8_t privateKey[32];
  340. Identity_set(ctx);
  341. ctx->alloc = alloc;
  342. ctx->log = logger;
  343. ctx->rand = rand;
  344. ctx->base = eb;
  345. Assert_true(!Key_gen(ctx->ipv6, ctx->pubKey, privateKey, rand));
  346. testAddr(ctx, "192.168.1.1", 0, 32, NULL, 0, 0);
  347. testAddr(ctx, "192.168.1.1", 16, 24, NULL, 0, 0);
  348. testAddr(ctx, "192.168.1.1", 24, 32, NULL, 0, 0);
  349. testAddr(ctx, "192.168.1.1", 16, 24, "fd00::1", 0, 64);
  350. testAddr(ctx, "192.168.1.1", 16, 24, "fd00::1", 8, 64);
  351. testAddr(ctx, "192.168.1.1", 16, 24, "fd00::1", 64, 128);
  352. EventBase_beginLoop(eb);
  353. Allocator_free(alloc);
  354. return 0;
  355. }