1
0

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 <https://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/Socket.h"
  20. #include "util/CString.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. Er_DEFUN(struct Iface* TUNInterface_new(const char* interfaceName,
  42. char assignedInterfaceName[TUNInterface_IFNAMSIZ],
  43. int isTapMode,
  44. EventBase_t* base,
  45. struct Log* logger,
  46. struct Allocator* alloc))
  47. {
  48. if (isTapMode) { Er_raise(alloc, "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. Er_raise(alloc, "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. Er_raise(alloc, "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. CString_safeStrncpy(info.ctl_name, APPLE_UTUN_CONTROL, sizeof info.ctl_name);
  69. if (ioctl(tunFd, CTLIOCGINFO, &info) < 0) {
  70. int err = errno;
  71. close(tunFd);
  72. Er_raise(alloc, "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. Er_raise(alloc, "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. Er_raise(alloc, "getting utun interface name [%s]", strerror(err));
  96. }
  97. struct Iface* iface = Er(Socket_forFd(tunFd, Socket_forFd_FRAMES, alloc));
  98. struct BSDMessageTypeWrapper* bmtw = BSDMessageTypeWrapper_new(alloc, logger);
  99. Iface_plumb(iface, &bmtw->wireSide);
  100. Er_ret(&bmtw->inside);
  101. }