TUNInterface_sunos.c 5.7 KB

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