TUNAdapter.c 6.4 KB

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