TUNInterface_sunos.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/Interface.h"
  16. #include "interface/InterfaceWrapper.h"
  17. #include "interface/tuntap/TUNInterface.h"
  18. #include "util/AddrTools.h"
  19. #include "util/Identity.h"
  20. #include "util/events/Pipe.h"
  21. #include "wire/Ethernet.h"
  22. #include "wire/Error.h"
  23. #include <errno.h>
  24. #include <stdio.h>
  25. #include <sys/ioctl.h>
  26. #include <unistd.h>
  27. #include <string.h>
  28. #include <sys/socket.h>
  29. #include <sys/types.h>
  30. #include <stdlib.h>
  31. #include <stddef.h>
  32. #include <net/if.h>
  33. #include <ctype.h>
  34. #include <sys/stropts.h>
  35. #include <sys/sockio.h>
  36. #include <fcntl.h>
  37. #include <net/route.h>
  38. /**
  39. * Since some illumos distributions (namely SmartOS) don't ship `net/if_tun.h`,
  40. * define those IOCTL constants here.
  41. */
  42. #define TUNNEWPPA (('T'<<16) | 0x0001)
  43. #define TUNSETPPA (('T'<<16) | 0x0002)
  44. struct TUNInterface_Illumos_pvt
  45. {
  46. struct Interface generic;
  47. struct Pipe* const pipe;
  48. Identity
  49. };
  50. /**
  51. * Illumos has no concept of packet info, it only supports IPv4 and IPv6
  52. * through TUN devices and it detects it by reading the version byte.
  53. */
  54. static uint16_t ethertypeForPacketType(uint8_t highByte)
  55. {
  56. return ((highByte >> 4) == 6) ? Ethernet_TYPE_IP6 : Ethernet_TYPE_IP4;
  57. }
  58. static uint8_t receiveMessage(struct Message* message, struct Interface* iface)
  59. {
  60. struct TUNInterface_Illumos_pvt* ctx =
  61. Identity_check((struct TUNInterface_Illumos_pvt*)iface->receiverContext);
  62. if (message->length < 4) {
  63. return Error_NONE;
  64. }
  65. Message_shift(message, 4, NULL);
  66. ((uint16_t*) message->bytes)[0] = 0;
  67. ((uint16_t*) message->bytes)[1] = ethertypeForPacketType(message->bytes[4]);
  68. return Interface_receiveMessage(&ctx->generic, message);
  69. }
  70. static uint8_t sendMessage(struct Message* message, struct Interface* iface)
  71. {
  72. struct TUNInterface_Illumos_pvt* ctx = Identity_check((struct TUNInterface_Illumos_pvt*)iface);
  73. Message_shift(message, -4, NULL);
  74. uint16_t ethertype = ((uint16_t*) message->bytes)[-1];
  75. if (ethertype != Ethernet_TYPE_IP6 && ethertype != Ethernet_TYPE_IP4) {
  76. Assert_true(!"Unsupported ethertype");
  77. }
  78. return Interface_sendMessage(&ctx->pipe->iface, message);
  79. }
  80. struct Interface* TUNInterface_new(const char* interfaceName,
  81. char assignedInterfaceName[TUNInterface_IFNAMSIZ],
  82. int isTapMode,
  83. struct EventBase* base,
  84. struct Log* logger,
  85. struct Except* eh,
  86. struct Allocator* alloc)
  87. {
  88. // tap mode is not supported at all by the sunos tun driver.
  89. if (isTapMode) { Except_throw(eh, "tap mode not supported on this platform"); }
  90. // Extract the number eg: 0 from tun0
  91. int ppa = 0;
  92. if (interfaceName) {
  93. for (uint32_t i = 0; i < strlen(interfaceName); i++) {
  94. if (isdigit(interfaceName[i])) {
  95. ppa = atoi(interfaceName);
  96. }
  97. }
  98. }
  99. // Open the descriptor
  100. int tunFd = open("/dev/tun", O_RDWR);
  101. // Either the name is specified and we use TUNSETPPA,
  102. // or it's not specified and we just want a TUNNEWPPA
  103. if (ppa) {
  104. ppa = ioctl(tunFd, TUNSETPPA, ppa);
  105. } else {
  106. ppa = ioctl(tunFd, TUNNEWPPA, -1);
  107. }
  108. int ipFd = open("/dev/ip6", O_RDWR, 0);
  109. int tunFd2 = open("/dev/tun", O_RDWR, 0);
  110. if (tunFd < 0 || ipFd < 0 || ppa < 0 || tunFd2 < 0) {
  111. int err = errno;
  112. close(tunFd);
  113. close(ipFd);
  114. close(tunFd2);
  115. char* error = NULL;
  116. if (tunFd < 0) {
  117. error = "open(\"/dev/tun\")";
  118. } else if (ipFd < 0) {
  119. error = "open(\"/dev/ip6\")";
  120. } else if (ppa < 0) {
  121. error = "ioctl(TUNNEWPPA)";
  122. } else if (tunFd2 < 0) {
  123. error = "open(\"/dev/tun\") (opening for plumbing interface)";
  124. }
  125. Except_throw(eh, "%s [%s]", error, strerror(err));
  126. }
  127. struct lifreq ifr = {
  128. .lifr_ppa = ppa,
  129. .lifr_flags = IFF_IPV6
  130. };
  131. // Since devices are numbered rather than named, it's not possible to have tun0 and cjdns0
  132. // so we'll skip the pretty names and call everything tunX
  133. int maxNameSize = (LIFNAMSIZ < TUNInterface_IFNAMSIZ) ? LIFNAMSIZ : TUNInterface_IFNAMSIZ;
  134. if (assignedInterfaceName) {
  135. snprintf(assignedInterfaceName, maxNameSize, "tun%d", ppa);
  136. }
  137. snprintf(ifr.lifr_name, maxNameSize, "tun%d", ppa);
  138. char* error = NULL;
  139. if (ioctl(tunFd, I_SRDOPT, RMSGD) < 0) {
  140. error = "putting tun into message-discard mode";
  141. } else if (ioctl(tunFd2, I_PUSH, "ip") < 0) {
  142. // add the ip module
  143. error = "ioctl(I_PUSH)";
  144. } else if (ioctl(tunFd2, SIOCSLIFNAME, &ifr) < 0) {
  145. // set the name of the interface and specify it as ipv6
  146. error = "ioctl(SIOCSLIFNAME)";
  147. } else if (ioctl(ipFd, I_LINK, tunFd2) < 0) {
  148. // link the device to the ipv6 router
  149. error = "ioctl(I_LINK)";
  150. }
  151. if (error) {
  152. int err = errno;
  153. close(ipFd);
  154. close(tunFd2);
  155. close(tunFd);
  156. Except_throw(eh, "%s [%s]", error, strerror(err));
  157. }
  158. close(ipFd);
  159. struct Pipe* p = Pipe_forFiles(tunFd, tunFd, base, eh, alloc);
  160. struct TUNInterface_Illumos_pvt* ctx =
  161. Allocator_clone(alloc, (&(struct TUNInterface_Illumos_pvt) {
  162. .pipe = p
  163. }));
  164. Identity_set(ctx);
  165. InterfaceWrapper_wrap(&p->iface, sendMessage, receiveMessage, &ctx->generic);
  166. return &ctx->generic;
  167. }