TUNInterface_freebsd.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/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. Er_DEFUN(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 Allocator* alloc))
  45. {
  46. char deviceFile[TUNInterface_IFNAMSIZ];
  47. if (isTapMode) { Er_raise(alloc, "tap mode not supported on this platform"); }
  48. // We are on FreeBSD so we just need to read /dev/tunxx to create the tun interface
  49. if (interfaceName) {
  50. snprintf(deviceFile,TUNInterface_IFNAMSIZ,"/dev/%s",interfaceName);
  51. } else {
  52. snprintf(deviceFile,TUNInterface_IFNAMSIZ,"%s","/dev/tun");
  53. }
  54. // Open the descriptor
  55. int tunFd = open(deviceFile, O_RDWR);
  56. //Get the resulting device name
  57. const char* assignedDevname;
  58. assignedDevname = fdevname(tunFd);
  59. // Extract the number eg: 0 from tun0
  60. int ppa = 0;
  61. for (uint32_t i = 0; i < strlen(assignedDevname); i++) {
  62. if (isdigit(assignedDevname[i])) {
  63. ppa = atoi(assignedDevname+i);
  64. break;
  65. }
  66. }
  67. if (tunFd < 0 || ppa < 0 ) {
  68. int err = errno;
  69. close(tunFd);
  70. char* error = NULL;
  71. if (tunFd < 0) {
  72. error = "open(\"/dev/tun\")";
  73. } else if (ppa < 0) {
  74. error = "fdevname/getting number from fdevname";
  75. }
  76. Er_raise(alloc, "%s [%s]", error, strerror(err));
  77. }
  78. // Since devices are numbered rather than named, it's not possible to have tun0 and cjdns0
  79. // so we'll skip the pretty names and call everything tunX
  80. if (assignedInterfaceName) {
  81. snprintf(assignedInterfaceName, TUNInterface_IFNAMSIZ, "tun%d", ppa);
  82. }
  83. char* error = NULL;
  84. // We want to send IPv6 through our tun device, so we need to be able to specify "ethertype"
  85. int tunhead = 1;
  86. if (ioctl(tunFd,TUNSIFHEAD,&tunhead) == -1) {
  87. error = "TUNSIFHEAD";
  88. }
  89. // This is not a point-to-point interface
  90. int tunsifmode = IFF_BROADCAST;
  91. if (ioctl(tunFd,TUNSIFMODE,&tunsifmode) == -1) {
  92. error = "TUNSIFMODE";
  93. }
  94. if (error) {
  95. int err = errno;
  96. close(tunFd);
  97. Er_raise(alloc, "%s [%s]", error, strerror(err));
  98. }
  99. struct Pipe* p = Er(Pipe_forFd(tunFd, false, base, logger, alloc));
  100. struct BSDMessageTypeWrapper* bmtw = BSDMessageTypeWrapper_new(alloc, logger);
  101. Iface_plumb(&p->iface, &bmtw->wireSide);
  102. Er_ret(&bmtw->inside);
  103. }