TUNInterface_darwin.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "exception/Except.h"
  16. #include "interface/Interface.h"
  17. #include "interface/tuntap/TUNInterface.h"
  18. #include "interface/tuntap/BSDMessageTypeWrapper.h"
  19. #include "util/AddrTools.h"
  20. #include "util/events/Pipe.h"
  21. #include <errno.h>
  22. #include <stdio.h>
  23. #include <sys/ioctl.h>
  24. #include <unistd.h>
  25. #include <sys/socket.h>
  26. #include <sys/types.h>
  27. #include <stdlib.h>
  28. #include <stddef.h>
  29. #include <net/if.h>
  30. #include <string.h>
  31. #include <netdb.h>
  32. #include <net/if_var.h>
  33. #include <netinet/in_var.h>
  34. #include <netinet6/nd6.h>
  35. #include <netinet/in.h>
  36. #include <sys/kern_control.h>
  37. #include <sys/sys_domain.h>
  38. #include <sys/kern_event.h>
  39. #define APPLE_UTUN_CONTROL "com.apple.net.utun_control"
  40. #define UTUN_OPT_IFNAME 2
  41. struct Interface* TUNInterface_new(const char* interfaceName,
  42. char assignedInterfaceName[TUNInterface_IFNAMSIZ],
  43. struct EventBase* base,
  44. struct Log* logger,
  45. struct Except* eh,
  46. struct Allocator* alloc)
  47. {
  48. int maxNameSize = (IFNAMSIZ < TUNInterface_IFNAMSIZ) ? IFNAMSIZ : TUNInterface_IFNAMSIZ;
  49. int tunUnit = 0; /* allocate dynamically by default */
  50. if (interfaceName) {
  51. int parsedUnit = 0;
  52. if (sscanf(interfaceName, "utun%i", &parsedUnit) != 1 || parsedUnit < 0) {
  53. Except_raise(eh,
  54. TUNInterface_new_BAD_TUNNEL,
  55. "Invalid utun device %s",
  56. interfaceName);
  57. }
  58. tunUnit = parsedUnit + 1; /* device number used is unit - 1*/
  59. }
  60. Log_info(logger,
  61. "Initializing utun interface: %s\n",
  62. (interfaceName ? interfaceName : "auto"));
  63. int tunFd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
  64. if (tunFd < 0) {
  65. Except_raise(eh, TUNInterface_new_INTERNAL,
  66. "socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL) [%s]",
  67. strerror(errno));
  68. }
  69. /* get the utun control id */
  70. struct ctl_info info;
  71. memset(&info, 0, sizeof(info));
  72. strncpy(info.ctl_name, APPLE_UTUN_CONTROL, strlen(APPLE_UTUN_CONTROL));
  73. if (ioctl(tunFd, CTLIOCGINFO, &info) < 0) {
  74. int err = errno;
  75. close(tunFd);
  76. Except_raise(eh,
  77. TUNInterface_new_INTERNAL,
  78. "getting utun device id [%s]",
  79. strerror(err));
  80. }
  81. /* connect the utun device */
  82. struct sockaddr_ctl addr;
  83. addr.sc_id = info.ctl_id;
  84. addr.sc_len = sizeof(addr);
  85. addr.sc_family = AF_SYSTEM;
  86. addr.ss_sysaddr = AF_SYS_CONTROL;
  87. addr.sc_unit = tunUnit;
  88. if (connect(tunFd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
  89. int err = errno;
  90. close(tunFd);
  91. Except_raise(eh,
  92. TUNInterface_new_INTERNAL,
  93. "connecting to utun device [%s]",
  94. strerror(err));
  95. }
  96. /* retrieve the assigned utun interface name */
  97. if (getsockopt(tunFd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME,
  98. assignedInterfaceName, (uint32_t*) &maxNameSize) >= 0) {
  99. Log_info(logger, "Initialized utun interface [%s]\n", assignedInterfaceName);
  100. } else {
  101. int err = errno;
  102. close(tunFd);
  103. Except_raise(eh,
  104. TUNInterface_new_INTERNAL,
  105. "getting utun interface name [%s]",
  106. strerror(err));
  107. }
  108. struct Pipe* p = Pipe_forFiles(tunFd, tunFd, base, eh, alloc);
  109. return BSDMessageTypeWrapper_new(&p->iface, logger);
  110. }