1
0

TUNAdapter.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "interface/Iface.h"
  16. #include "memory/Allocator.h"
  17. #include "util/Identity.h"
  18. #include "net/TUNAdapter.h"
  19. #include "wire/DataHeader.h"
  20. #include "wire/RouteHeader.h"
  21. #include "wire/Headers.h"
  22. #include "interface/tuntap/TUNMessageType.h"
  23. #include "wire/Ethernet.h"
  24. #include "crypto/AddressCalc.h"
  25. #include "util/Defined.h"
  26. #include "util/AddrTools.h"
  27. #include "wire/Error.h"
  28. struct TUNAdapter_pvt
  29. {
  30. struct TUNAdapter pub;
  31. struct Log* log;
  32. uint8_t myIp6[16];
  33. Identity
  34. };
  35. static Iface_DEFUN incomingFromTunIf(Message_t* msg, struct Iface* tunIf)
  36. {
  37. struct TUNAdapter_pvt* ud = Identity_containerOf(tunIf, struct TUNAdapter_pvt, pub.tunIf);
  38. uint16_t ethertype = Er_assert(TUNMessageType_pop(msg));
  39. int version = Headers_getIpVersion(Message_bytes(msg));
  40. if ((ethertype == Ethernet_TYPE_IP4 && version != 4)
  41. || (ethertype == Ethernet_TYPE_IP6 && version != 6))
  42. {
  43. Log_debug(ud->log, "DROP packet because ip version [%d] "
  44. "doesn't match ethertype [%u].", version, Endian_bigEndianToHost16(ethertype));
  45. return Error(msg, "INVALID");
  46. }
  47. if (ethertype == Ethernet_TYPE_IP4) {
  48. return Iface_next(&ud->pub.ipTunnelIf, msg);
  49. }
  50. if (ethertype != Ethernet_TYPE_IP6) {
  51. Log_debug(ud->log, "DROP packet unknown ethertype [%u]",
  52. Endian_bigEndianToHost16(ethertype));
  53. return Error(msg, "INVALID");
  54. }
  55. if (Message_getLength(msg) < Headers_IP6Header_SIZE) {
  56. Log_debug(ud->log, "DROP runt");
  57. return Error(msg, "RUNT");
  58. }
  59. struct Headers_IP6Header* header = (struct Headers_IP6Header*) Message_bytes(msg);
  60. if (!AddressCalc_validAddress(header->destinationAddr)) {
  61. return Iface_next(&ud->pub.ipTunnelIf, msg);
  62. }
  63. if (Bits_memcmp(header->sourceAddr, ud->myIp6, 16)) {
  64. if (Defined(Log_DEBUG)) {
  65. uint8_t expectedSource[40];
  66. AddrTools_printIp(expectedSource, ud->myIp6);
  67. uint8_t packetSource[40];
  68. AddrTools_printIp(packetSource, header->sourceAddr);
  69. Log_debug(ud->log,
  70. "DROP packet from [%s] because all messages must have source address [%s]",
  71. packetSource, expectedSource);
  72. }
  73. return Error(msg, "INVALID");
  74. }
  75. if (!Bits_memcmp(header->destinationAddr, ud->myIp6, 16)) {
  76. // I'm Gonna Sit Right Down and Write Myself a Letter
  77. Er_assert(TUNMessageType_push(msg, ethertype));
  78. return Iface_next(tunIf, msg);
  79. }
  80. // first move the dest addr to the right place.
  81. #pragma GCC diagnostic push
  82. #ifdef __clang__
  83. #pragma GCC diagnostic ignored "-Wfortify-source"
  84. #endif
  85. Bits_memmove(header->destinationAddr - DataHeader_SIZE, header->destinationAddr, 16);
  86. #pragma GCC diagnostic pop
  87. Er_assert(Message_eshift(msg, DataHeader_SIZE + RouteHeader_SIZE - Headers_IP6Header_SIZE));
  88. struct RouteHeader* rh = (struct RouteHeader*) Message_bytes(msg);
  89. struct DataHeader* dh = (struct DataHeader*) &rh[1];
  90. Bits_memset(dh, 0, DataHeader_SIZE);
  91. DataHeader_setContentType(dh, header->nextHeader);
  92. DataHeader_setVersion(dh, DataHeader_CURRENT_VERSION);
  93. // Other than the ipv6 addr at the end, everything is zeros right down the line.
  94. Bits_memset(rh, 0, RouteHeader_SIZE - 16);
  95. return Iface_next(&ud->pub.upperDistributorIf, msg);
  96. }
  97. static Iface_DEFUN sendToTunIf(Message_t* msg, struct TUNAdapter_pvt* ud)
  98. {
  99. if (!ud->pub.tunIf.connectedIf) {
  100. Log_debug(ud->log, "DROP message for tun because no device is defined");
  101. // Nothing inherently wrong with this message
  102. return NULL;
  103. }
  104. return Iface_next(&ud->pub.tunIf, msg);
  105. }
  106. static Iface_DEFUN incomingFromIpTunnelIf(Message_t* msg, struct Iface* ipTunnelIf)
  107. {
  108. struct TUNAdapter_pvt* ud =
  109. Identity_containerOf(ipTunnelIf, struct TUNAdapter_pvt, pub.ipTunnelIf);
  110. return sendToTunIf(msg, ud);
  111. }
  112. static Iface_DEFUN incomingFromUpperDistributorIf(Message_t* msg,
  113. struct Iface* upperDistributorIf)
  114. {
  115. struct TUNAdapter_pvt* ud =
  116. Identity_containerOf(upperDistributorIf, struct TUNAdapter_pvt, pub.upperDistributorIf);
  117. Assert_true(Message_getLength(msg) >= RouteHeader_SIZE + DataHeader_SIZE);
  118. struct RouteHeader* hdr = (struct RouteHeader*) Message_bytes(msg);
  119. struct DataHeader* dh = (struct DataHeader*) &hdr[1];
  120. enum ContentType type = DataHeader_getContentType(dh);
  121. Assert_true(type <= ContentType_IP6_MAX);
  122. // Shift ip address into destination slot.
  123. #pragma GCC diagnostic push
  124. #ifdef __clang__
  125. #pragma GCC diagnostic ignored "-Wfortify-source"
  126. #endif
  127. Bits_memmove(hdr->ip6 + DataHeader_SIZE - 16, hdr->ip6, 16);
  128. #pragma GCC diagnostic pop
  129. // put my address as destination.
  130. Bits_memcpy(&hdr->ip6[DataHeader_SIZE], ud->myIp6, 16);
  131. Er_assert(Message_eshift(msg, Headers_IP6Header_SIZE - DataHeader_SIZE - RouteHeader_SIZE));
  132. struct Headers_IP6Header* ip6 = (struct Headers_IP6Header*) Message_bytes(msg);
  133. Bits_memset(ip6, 0, Headers_IP6Header_SIZE - 32);
  134. Headers_setIpVersion(ip6);
  135. ip6->payloadLength_be = Endian_bigEndianToHost16(Message_getLength(msg) - Headers_IP6Header_SIZE);
  136. ip6->nextHeader = type;
  137. ip6->hopLimit = 42;
  138. Er_assert(TUNMessageType_push(msg, Ethernet_TYPE_IP6));
  139. return sendToTunIf(msg, ud);
  140. }
  141. struct TUNAdapter* TUNAdapter_new(struct Allocator* allocator, struct Log* log, uint8_t myIp6[16])
  142. {
  143. struct Allocator* alloc = Allocator_child(allocator);
  144. struct TUNAdapter_pvt* out = Allocator_calloc(alloc, sizeof(struct TUNAdapter_pvt), 1);
  145. out->pub.tunIf.send = incomingFromTunIf;
  146. out->pub.ipTunnelIf.send = incomingFromIpTunnelIf;
  147. out->pub.upperDistributorIf.send = incomingFromUpperDistributorIf;
  148. out->log = log;
  149. Identity_set(out);
  150. Bits_memcpy(out->myIp6, myIp6, 16);
  151. return &out->pub;
  152. }