TUNInterface_freebsd.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/tuntap/TUNInterface.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 Interface* TUNInterface_new(const char* interfaceName,
  40. char assignedInterfaceName[TUNInterface_IFNAMSIZ],
  41. struct EventBase* base,
  42. struct Log* logger,
  43. struct Except* eh,
  44. struct Allocator* alloc)
  45. {
  46. // Open the descriptor
  47. int tunFd = open("/dev/tun", O_RDWR);
  48. //Get the resulting device name
  49. const char* assignedDevname;
  50. assignedDevname = fdevname(tunFd);
  51. // Extract the number eg: 0 from tun0
  52. int ppa = 0;
  53. for (uint32_t i = 0; i < strlen(assignedDevname); i++) {
  54. if (isdigit(assignedDevname[i])) {
  55. ppa = atoi(assignedDevname);
  56. }
  57. }
  58. if (tunFd < 0 || ppa < 0 ) {
  59. int err = errno;
  60. close(tunFd);
  61. char* error = NULL;
  62. if (tunFd < 0) {
  63. error = "open(\"/dev/tun\")";
  64. } else if (ppa < 0) {
  65. error = "fdevname/getting number from fdevname";
  66. }
  67. Except_throw(eh, "%s [%s]", error, strerror(err));
  68. }
  69. // Since devices are numbered rather than named, it's not possible to have tun0 and cjdns0
  70. // so we'll skip the pretty names and call everything tunX
  71. snprintf(assignedInterfaceName, TUNInterface_IFNAMSIZ, "tun%d", ppa);
  72. char* error = NULL;
  73. // We want to send IPv6 through our tun device, so we need to be able to specify "ethertype"
  74. int tunhead = 1;
  75. if (ioctl(tunFd,TUNSIFHEAD,&tunhead) == -1) {
  76. error = "TUNSIFHEAD";
  77. }
  78. if (error) {
  79. int err = errno;
  80. close(tunFd);
  81. Except_throw(eh, "%s [%s]", error, strerror(err));
  82. }
  83. struct Pipe* p = Pipe_forFiles(tunFd, tunFd, base, eh, alloc);
  84. return BSDMessageTypeWrapper_new(&p->iface, logger);
  85. }