TUNInterface_darwin.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_throw(eh, "Invalid utun device %s", interfaceName);
  54. }
  55. tunUnit = parsedUnit + 1; /* device number used is unit - 1*/
  56. }
  57. Log_info(logger,
  58. "Initializing utun interface: %s\n",
  59. (interfaceName ? interfaceName : "auto"));
  60. int tunFd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
  61. if (tunFd < 0) {
  62. Except_throw(eh, "socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL) [%s]", strerror(errno));
  63. }
  64. /* get the utun control id */
  65. struct ctl_info info;
  66. memset(&info, 0, sizeof(info));
  67. strncpy(info.ctl_name, APPLE_UTUN_CONTROL, strlen(APPLE_UTUN_CONTROL));
  68. if (ioctl(tunFd, CTLIOCGINFO, &info) < 0) {
  69. int err = errno;
  70. close(tunFd);
  71. Except_throw(eh, "getting utun device id [%s]", strerror(err));
  72. }
  73. /* connect the utun device */
  74. struct sockaddr_ctl addr;
  75. addr.sc_id = info.ctl_id;
  76. addr.sc_len = sizeof(addr);
  77. addr.sc_family = AF_SYSTEM;
  78. addr.ss_sysaddr = AF_SYS_CONTROL;
  79. addr.sc_unit = tunUnit;
  80. if (connect(tunFd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
  81. int err = errno;
  82. close(tunFd);
  83. Except_throw(eh, "connecting to utun device [%s]", strerror(err));
  84. }
  85. /* retrieve the assigned utun interface name */
  86. if (getsockopt(tunFd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME,
  87. assignedInterfaceName, (uint32_t*) &maxNameSize) >= 0) {
  88. Log_info(logger, "Initialized utun interface [%s]\n", assignedInterfaceName);
  89. } else {
  90. int err = errno;
  91. close(tunFd);
  92. Except_throw(eh, "getting utun interface name [%s]", strerror(err));
  93. }
  94. struct Pipe* p = Pipe_forFiles(tunFd, tunFd, base, eh, alloc);
  95. return BSDMessageTypeWrapper_new(&p->iface, logger);
  96. }