TUNInterface_darwin.c 4.1 KB

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