TUNInterface_sunos.c 6.1 KB

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