TUNInterface_freebsd.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <ctype.h>
  22. #include <fcntl.h>
  23. #include <stdio.h>
  24. #include <sys/ioctl.h>
  25. #include <unistd.h>
  26. #include <sys/socket.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <stdlib.h>
  30. #include <stddef.h>
  31. #include <net/if.h>
  32. #include <string.h>
  33. #include <netdb.h>
  34. #include <net/if_var.h>
  35. #include <net/if_tun.h>
  36. #include <netinet/in.h>
  37. #include <netinet6/in6_var.h>
  38. #include <netinet6/nd6.h>
  39. struct Iface* TUNInterface_new(const char* interfaceName,
  40. char assignedInterfaceName[TUNInterface_IFNAMSIZ],
  41. int isTapMode,
  42. struct EventBase* base,
  43. struct Log* logger,
  44. struct Except* eh,
  45. struct Allocator* alloc)
  46. {
  47. if (isTapMode) { Except_throw(eh, "tap mode not supported on this platform"); }
  48. // Open the descriptor
  49. int tunFd = open("/dev/tun", O_RDWR);
  50. //Get the resulting device name
  51. const char* assignedDevname;
  52. assignedDevname = fdevname(tunFd);
  53. // Extract the number eg: 0 from tun0
  54. int ppa = 0;
  55. for (uint32_t i = 0; i < strlen(assignedDevname); i++) {
  56. if (isdigit(assignedDevname[i])) {
  57. ppa = atoi(assignedDevname);
  58. }
  59. }
  60. if (tunFd < 0 || ppa < 0 ) {
  61. int err = errno;
  62. close(tunFd);
  63. char* error = NULL;
  64. if (tunFd < 0) {
  65. error = "open(\"/dev/tun\")";
  66. } else if (ppa < 0) {
  67. error = "fdevname/getting number from fdevname";
  68. }
  69. Except_throw(eh, "%s [%s]", error, strerror(err));
  70. }
  71. // Since devices are numbered rather than named, it's not possible to have tun0 and cjdns0
  72. // so we'll skip the pretty names and call everything tunX
  73. if (assignedInterfaceName) {
  74. snprintf(assignedInterfaceName, TUNInterface_IFNAMSIZ, "tun%d", ppa);
  75. }
  76. char* error = NULL;
  77. // We want to send IPv6 through our tun device, so we need to be able to specify "ethertype"
  78. int tunhead = 1;
  79. if (ioctl(tunFd,TUNSIFHEAD,&tunhead) == -1) {
  80. error = "TUNSIFHEAD";
  81. }
  82. if (error) {
  83. int err = errno;
  84. close(tunFd);
  85. Except_throw(eh, "%s [%s]", error, strerror(err));
  86. }
  87. struct Pipe* p = Pipe_forFiles(tunFd, tunFd, base, eh, alloc);
  88. struct BSDMessageTypeWrapper* bmtw = BSDMessageTypeWrapper_new(alloc, logger);
  89. Iface_plumb(&p->iface, &bmtw->wireSide);
  90. return &bmtw->inside;
  91. }